Hi Everyone !!!
In this blog we are going to see the concepts of Structure in System Verilog.
- Structure is a collection of variables and/or constants under a single name. The entire collection can be referenced, using the name of the structure. Each member within the structure also has a name, which is used to select it from the structure.
- Structure can be accessed as whole or separately.
- Wire Structure
module TB;
- Typed and anonymous Structure
- Packed Structure
- Signed Packed Structure
- The signing of unpacked structures is not allowed. The following declaration would be considered illegal:
- If all data types within a packed structure are 2-state, the structure as a whole is treated as a 2-state vector.
- If any data type within a packed structure is 4-state, the structure as a whole is treated as a 4-state vector. If there are also 2-state members in the structure, there is an implicit conversion from 4-state to 2-state when reading those members and from 2-state to 4-state when writing them.
- Only packed structures should be an integral type only.
- A packed structure can be used with a typedef.
- A structure can be assigned as a whole and passed to or from a subroutine as a whole.
- Members of a structure data type can be assigned individual default member values by using an initial assignment with the declaration of each member. The assigned expression shall be a constant expression.
- An example of initializing members of a structure type is as follows:
int addr = 1 + constant;
int crc;
byte data [4] = '{4{1}};
} my_data;
my_data pi; // initialization defined by the typedef.
if an explicit initial value expression is used with the declaration of a variable, the initial assignment
expression within the structure data type shall be ignored. For example:
my_data pi = '{1,2,'{2,3,4,5}};
- my_data pi= '{default:0}; // set all members of my_data to 0 (allowed)
- my_data pi = '{addr:0,crc:16,data:1} // (allowed-list by name)
- my_data pi = '{5,10,1} // (allowed-list by order)
- my_data pi ='{addr:6,1,0} // not allowed (illegal to mix listing by name and orger-leads to compilation error)
- Structures can be passed as an argument to task and functions.
- Both unpacked and packed structures are synthesizable. Synthesis supports passing structures through module ports, and in/out of tasks and functions.
- Because a packed structure is stored as a vector, operations on the complete structure are treated as vector operations. Therefore, math operations, logical operations, and any other operation that can be performed on vectors can also be performed on packed structures EX:
- typedef struct{ logic a;
- logic[7:0] b;
- logic[31:0] c;} my_data;
- my_data pin,pout;
- initial begin
- pot=pin<<2;
- end
Difference between array and structure?
- Structure differs from an array, in that an array is a collection of elements that are all the same type and size, where as a structure is a collection of variables and or constants that can be different types and different sizes.
- Another difference is that array elements can be accessed by using array index where as in structure elements are accessed by using structure name.
― Roy T. Bennett,