在Systemverilog中,union可以被声明为tagged unions。
union tagged { int a; byte b; bit [15:0] c; } data;
tagged union包含一个隐式成员,该成员存储tag,也就是标记,它表示这个union最终存储的到底是哪一个成员。
tagged union 是一种类型检查(type-checked)union.
这意味着你不能写入union中的一个成员,而读取另外一个成员。因为在这期间,tagged union会进行读写类型检查
data = tagged a 32'hffff_ffff;
如果从不同的union成员中读取值,仿真器则会报错:
module tagged_union_example; logic [31:0] x; typedef union tagged { int a; byte b; bit [15:0] c; } data; data d1; initial begin d1 = tagged a 32'hffff_ffff; //write to 'a' //read from 'b'. Since 'a' was written last, cannot access //'b'. - Error x = d1.b; $display("x = %h",x); end endmodule
在上面的例子中,我们创建了一个tagged union " data ",并声明" d1 "为" data "类型。然后我们写入成员a:
d1 = tagged a 32'hffff_ffff;
然后我们读取值“d1.b”。因为读写的成员类型不同,所以会打印错误信息:
Error-[TU-INVMEMUSG] Invalid member usage of a tagged union. testbench.sv, 15 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. V C S S i m u l a t i o n R e p o r t
审核编辑:刘清
全部0条评论
快来发表一下你的评论吧 !