在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
审核编辑:刘清
-
仿真器
+关注
关注
14文章
1019浏览量
83984 -
Verilog语言
+关注
关注
0文章
113浏览量
8311
原文标题:SystemVerilog中的tagged Unions
文章出处:【微信号:芯片验证工程师,微信公众号:芯片验证工程师】欢迎添加关注!文章转载请注明出处。
发布评论请先 登录
相关推荐
SystemVerilog中的“const”类属性
SystemVerilog的断言手册
Tagged Image File Format (TIFF) Decoder - Download Production Code
![<b class='flag-5'>Tagged</b> Image File Format (TIFF) Decoder - Download Production Code](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
SystemVerilog中$cast的应用
SystemVerilog中的package
SystemVerilog中的Semaphores
Systemverilog中的Driving Strength讲解
![<b class='flag-5'>Systemverilog</b><b class='flag-5'>中</b>的Driving Strength讲解](https://file1.elecfans.com/web2/M00/89/AC/wKgaomSJcSWAUfCrAAF04gZw8WE916.jpg)
评论