Hi Everyone !!!
In this blog we are going to see the concepts of Union in System Verilog. Structure and Union is look similar, they serve two different purposes.
Before Get into this Kindly Refresh the concepts about Structures. https://vlsicsinfo.blogspot.com/search/label/structure
A union is a data type that represents a single piece of storage that can be accessed using one of the named member data types. Only one of the data types in the union can be used at a time. By default, a union is unpacked, meaning there is no required representation for how members of the union are stored.
Syntax:
union {
int y;
int unsigned x;
bit a;
} data;
Difference between Structure and Union?
The one major difference that distinguishes structure and union is that the structure has a separate memory location for each of its members whereas, the members of a union share the same memory location.
BASIS OF
COMPARISON
|
STRUCTURE
|
UNION
|
Basic
|
The separate memory location is allotted to each member of the
'structure'.
|
All members of the 'union' share the same memory location.
|
Declaration
|
struct struct_name{
type element1;
type element2;
.
.
} variable1, variable2, ...;
|
union u_name{
type element1;
type element2;
.
.
} variable1, variable2, ...;
|
Keyword
|
'struct'
|
'union'
|
Size
|
Size of Structure= sum of size of all the data members.
|
Size of Union=size of the largest members.
|
Store Value
|
Stores distinct values for all the members.
|
Stores same value for all the members.
|
At a Time
|
A 'structure' stores multiple values, of the different members, of the
'structure'.
|
A 'union' stores a single value at a time for all members.
|
Way of Viewing
|
Provide single way to view each memory location.
|
Provide multiple way to to view same memory location.
|
Program Examples:
// Example for memory allocation for structure and union//
module struct_union;
struct {
int a;
bit b;
int c;} data_s; //Here structure size is sum of all elements (int(32)+bit(1)+int(32)=65)
union {
int a;
bit b;
int c;} data_u; //Here union size is largest of the data type. so size is 32 (By seeing output we will get a clear idea)
initial begin
$display(" Structure Memory =%0d Union Memory=%0d", $bits(data_s),$bits(data_u));
end
endmodule
Note:
$bits---> System function it returns number of bits occupied by the mentioned variable/array/queue/structue/union.
OUTPUT:
Structure Memory =65 Union Memory=32
V C S S i m u l a t i o n R e p o r t
Program Examples:
// Example for string value at a time in structure and union//
module struct_union;
struct {
int a;
bit b;
int c;} data_s;
union {
int a;
bit b;
int c;} data_u;
initial begin
data_s='{a:5,b:1,c:10}; // structure allows to access all parameter at a time
data_u='{a:5,b:1,c:10}; // union allows to access a only one parameter at time. It leads to compilation error
end
endmodule
OUTPUT
Error-[IAP] Illegal assignment pattern
testbench.sv, 14
struct_union, "'{a:5, b:1, c:10}"
Assignment pattern is illegal due to: Assignment Pattern used to assign into
unpacked union target. Make sure this context allows for expressions with no
self-determined type.
The above compilation can be removed by using below technique
module struct_union;
struct {
int a;
bit b;
int c;} data_s;
union {
int a;
bit b;
int c;} data_u;
initial begin
data_s='{a:5,b:1,c:10}; // structure allows to access all parameter at a time
data_u.a=10; // union allows to access a only one parameter at time
data_u.b=1;
data_u.c=11; // At a time only one parameter of union can be accessed
$display(" A=%0d B=%0d C=%0d ",data_u.a,data_u.b,data_u.c);
end
endmodule
OUTPUT
A=11 B=1 C=11
Note: last written will be stored in union. Since "b" is declared as bit its value shows as 1
TYPED and anonymous Union :
A union can be defined as a type using typedef, in the same way as structures. A union that is defined as a user-defined type is referred to as a typed union. If typedef is not used, the union is referred to as an anonymous union
typedef union { int i; shortreal f; } num;
// named union typenum n; //
which is allowed only in typedef format n.f = 0.0; //
set n in floating point format
typedef struct
{ bit i;
union { int i; shortreal f; } n; // anonymous union type
} data; //
Named structureUNPACKED UNION:
By default union is unpacked.If no initial value is specified in the declaration of a variable of an unpacked union type, then the variable shall be initialized to the default initial value for variables of the type of the first member in declaration order of the union type.
An unpacked union can contain any variable type, including real types, unpacked structures and unpacked arrays. Un packed unions are not synthesizable.
TAGGED Union
The qualifier tagged in a union declares it as a tagged union, which is a type-checked union. An ordinary (untagged) union can be updated using a value of one member type and read as a value of another member type, which is a potential type loophole. A tagged union stores both the member value and a tag, i.e., additional bits representing the current member name. The tag and value can only be updated together consistently using a statically type-checked tagged union expressio. The member value can only be read with a type that is consistent with the current tag value (i.e., member name). Thus, it is impossible to store a value of one type and (mis)interpret the bits as another type.
Example:
module struct_union;
union {
int a;
bit b;
int c;} data_u;
initial begin
data_u.a=5; //Hete I am using only one variable of union but I am displaying all the variables in union. It won't produce any compilation
$display(" A=%0d B=%0d C=%0d", data_u.a,data_u.b,data_u.c);
end
endmodule
OUTPUT:
A=5 B=1 C=5
V C S S i m u l a t i o n R e p o r t
The Same example can be done for tagged unionUpdated union variable only allowed to read/use. If we trying to read non written value of any union variable other than written variable it leads to compilation error. By seeing below example we will get a clear idea. For this type of checking purpose tagged unions are used.
module struct_union;
union tagged{
int a;
bit b;
int c;} data_u;
initial begin
data_u= tagged a 5; //Hete I am using only one variable of union but I am displaying all the variables in union. It produce compilation
$display(" A=%0d B=%0d C=%0d", data_u.a,data_u.b,data_u.c);
end
endmodule
OUTPUT:Error-[TU-INVMEMUSG] Invalid member usage of a tagged union.testbench.sv, 10 Member of a tagged union referred is not valid since a different member is in use. The expected tag is 'a', but tag 'b' is used. Please check which member of the tagged union is in use.
PACKED Union:
Packed unions shall only contain members that are of integral data types. The members of a packed, untagged union shall all be the same size (in contrast to an unpacked union or a packed, tagged union, where the members can be different sizes). Thus, a union member that was written as another member can be read back.
A packed union differs from an unpacked union in that when a packed union appears as a primary, it shall be treated as a single vector.
A packed union can also be used as a whole with arithmetic and logical operators, and its behavior is determined by its signedness, with unsigned being the default. One or more bits of a packed union can be selected as if it were a packed array with the range [n-1:0].
Only packed data types and the integer data types shall be legal in packed unions.
If a packed union contains a 2-state member and a 4-state member, the entire union is 4-state. There is an implicit conversion from 4-state to 2-state when reading and from 2-state to 4-state when writing the 2-state member.
Syntax:
typedef union packed { // default unsigned
bit [423:0] a ; // All size should be equal a=424, b=8*53=424 otherwise leads to compilation order
bit [52:0][7:0] b;} data;
Packed union must have same size.
module struct_union;
union packed{ // Here packed union is used
int a;
bit b; // Here different size of data types are used so it leads to compilation order
int c;} data_u;
initial begin
data_u.a=10;
data_u.b=1;
data_u.c=11;
endmodule
OUTPUT:
Error-[PUMSS] Incorrect packed union members size
testbench.sv, 7
"b"
Packed union members must have same size.
Member "b" has different size (1 bits) from next member (32 bits).
Hope this logic's will be useful to everyone !!!
Giving feedback is more precious than writing an article!!! 😊😊😊😊😊
Always welcome both positive and negative feedback's !!!
Feel free to post any queries related SV and UVM
"Be yourself, who else is better qualified" - Frank J. Giblin