Extracting field names of a packed struct

Hello…
Is there a way to extract the fields of a packed struct type and the order in which the names appear in the struct ?
Thanks…

In reply to jjose:

You need to be more specific by what you mean by “extract”. to what?

In reply to dave_59:

I apologize for my short question. I would say extract into string array, which I can use to create something else.

an example.
struct packed {
logic a;
logic b;
logic c;
} my_struct_t;

I am hoping to get the names a,b and c in that specific order. I want to implement a generic function which can do some operation on many different types of such packed structs. I wonder if there is any built in system Verilog capability to do this.
Thanks…

You can do a bit-stream cast.

typedef bit streambit_t[];

streambits_t mybits;
my_struct_t mystruct;

mybits = streambits_t'(mystruct);


See section 6.24.3 Bit-stream casting of the 1800-2012 LRM

In reply to dave_59:

I think this isn’t what he means. He wants to be able to introspect the packed struct type and get a list of strings that represent the names of the fields.

What you want is called introspection/reflection and it isn’t easily achieved in SystemVerilog. You can theoretically do something like this using the VPI. There are caveats however. First, your simulator vendor has to support the required parts of the VPI object model (and hopefully document them well enough, as you won’t find much on the net). Second, you’re going to have to get proficient in writing C code for this.

In reply to Tudor Timi:

Thanks and I appreciate all your input. I read about VPI when I searched for this topic. I just wanted to check if there are any direct options that I am not aware of…
Thanks again…

In reply to Tudor Timi:

Now I see that from there response. I updated the question title.

In reply to jjose:

It might help to explain the algorithm you are trying to achieve with this mechanism and perhaps we could suggest an alternative data structure that does not require this introspection.

In reply to dave_59:

Hi,
I faced the same challenge where I have two variables of packed struct type, and need to compare them and print debug information of the non-equal fields only.
Any idea?

In reply to earbili:

There is no automated way within the SystemVerilog language to automatic this. You can use a macro to help iterating field by field manually.

It is possible to use the VPI to iterate over members of a struct variable in C, but can get complicated if the members are 4-state, or larger than 32-bits.