How to initialize a union in Systemverilog?

Status
Not open for further replies.

boardlanguage

Full Member level 1
Joined
Apr 6, 2007
Messages
96
Helped
7
Reputation
14
Reaction score
1
Trophy points
1,288
Activity points
2,083
I've tried to initialize a union (in Modelsim 6.2f SE) , but it's not working.
I want the union to initialize to '0' (instead of 'X'.) I can't use a 2-state base-type (bit), because the union must be capable of capturing 4-state ('X') values.
Code:
typedef union packed {
  logic blah7;
  logic blah6;
  logic blah5;
  logic blah4;
} t_myunion;

t_myunion foo = { default:0 };
/*
COMPILER ERROR: Illegal assignment of structural literals to union
*/

Is there a way to initialize a union?[/code]
 

Ya. Create it and initialize it in a procedural statement
Sumit
 
Yes, you have two choices:
(a) initialize in procedural code (task/subprogram or other program block)
(b) since you declared the struct/union 'packed', you can simply assign it directly to a 4-state literal value -- no need for the braces {}.
Code:
typedef union packed {
  logic blah7;
  logic blah6;
  logic blah5;
  logic blah4;
} t_myunion;

t_myunion foo = 'h42;// single value, only works for packed union/struct

...so you almost had it right the first time
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…