SystemVerilog casting意味著將一種數(shù)據(jù)類型轉(zhuǎn)換為另一種數(shù)據(jù)類型。在將一個(gè)變量賦值給另一個(gè)變量時(shí),SystemVerilog要求這兩個(gè)變量具有相同的數(shù)據(jù)類型。
SystemVerilog可以使用(')符號(hào)進(jìn)行強(qiáng)制(靜態(tài))類型轉(zhuǎn)換。另外,在SystemVerilog中還存在動(dòng)態(tài)cast的概念。
SystemVerilog提供了$cast系統(tǒng)任務(wù)/函數(shù),能夠在兩個(gè)不同的數(shù)據(jù)類型變量之間賦值,因?yàn)槿绻苯淤x值的話會(huì)報(bào)編譯錯(cuò)誤。
在SystemVerilog中最常用的動(dòng)態(tài)cast應(yīng)用場(chǎng)景就是類的繼承特性中對(duì)類句柄的賦值。基類句柄可以用來獲取派生類的對(duì)象,反之則不行。
在實(shí)際仿真執(zhí)行的過程中,動(dòng)態(tài)cast會(huì)執(zhí)行相應(yīng)的檢查,是否能夠真正地進(jìn)行賦值。
需要特別注意的是,cast既可以作為fucntion調(diào)用,也可以作為task調(diào)用。區(qū)別就是cast作為函數(shù)調(diào)用時(shí),如果沒有成功會(huì)返回0,然后可以決定是否采取相應(yīng)的操作,而$cast作為任務(wù)調(diào)用失敗后,會(huì)直接停止仿真。
下面SystemVerilog $cast系統(tǒng)函數(shù)/任務(wù)的源碼聲明
function int $cast (target_var, source_exp); task $cast (target_var, source_exp);
下面的例子展示了SystemVerilog中$cast的應(yīng)用:
module tb; typedef enum { soccer=2, cricket=4, football=10 } sports; sports mS; int i; initial begin i = 10; mS = i; //Synopsys-VCS - WARNING - incompatible types //Mentor Questa/Aldec-Riviera - run time ERROR $cast(mS, i); //$cast as a task - match types $display ("Sports=%s", mS.name( )); i = mS; //No Warning or Error $display("i=%0d",i); i=50; //$cast (mS, i); //ERROR - 50 is not a valid value for enum if ($cast (mS, i)) //$cast as a function $display ("Cast passed"); else $display ("Cast failed"); end endmodule
仿真log:
Sports=football i=10 Cast failed V C S S i m u l a t i o n R e p o r t
在“tb” module中,我們定義了“int i”,并定義了一個(gè)enum“sports”并聲明了一個(gè)sports類型的變量“mS”。
將一個(gè)int賦值給enum類型(sports)的變量
mS = i;
由于" i "不是enum類型,存在類型不兼容,所以會(huì)得到Synopsys - VCS的warnning(不是ERROR哦):
Warning-[ENUMASSIGN] Illegal assignment to enum variable testbench.sv, 11 tb, "mS = i;" Only expressions of the enum type can be assigned to an enum variable. The type int is incompatible with the enum 'sports'
對(duì)于相同的代碼,Mentor’s Questa可能會(huì)報(bào)ERROR:
** Error (suppressible): testbench.sv(9): (vlog-8386) An enum variable 'mS' of type 'sports' may only be assigned the same enum typed variable or one of its values. Variable i requires an explicit cast.
大家可能有點(diǎn)奇怪,這個(gè)不兼容類型的賦值到底是warning還是Error,結(jié)論就是這個(gè)這個(gè)enum 值有實(shí)際的分配,其實(shí)對(duì)代碼功能沒有影響,當(dāng)作warning也是沒有問題的。
為了糾正這種類型不兼容的賦值錯(cuò)誤/警告,我們使用$cast將“i”強(qiáng)制轉(zhuǎn)換為enum“mS”:
$cast(mS, i);
在這個(gè)上下文下,$cast就是作為task調(diào)用的。這種動(dòng)態(tài)cast將使int類型" i "和枚舉類型" mS "兼容,仿真會(huì)PASS。因?yàn)閕 = 10, mS得到的值是10,也就是enum中的“football”。所以,仿真log顯示
“sports = football。”
注意下面的枚舉賦值給int類型完全是可以的,反之則不行:
i = mS;
在動(dòng)態(tài)cast中分配不正確的枚舉值(50)。
i=50; $cast(mS, i);
因?yàn)?0不包含在枚舉“sports”的范圍內(nèi),所以會(huì)得到一個(gè)ERROR:
Error-[STASKE_DCF] Dynamic cast failed testbench.sv, 22 Dynamic cast using '$cast' failed. The source expression is not yielding a valid value for the destination variable.
最后,我們使用$cast作為函數(shù)調(diào)用,它將返回“0”(因?yàn)閏ast失?。瑢⒌玫絝ail打印(“Cast failed”)。
審核編輯:湯梓紅。
-
Verilog
+關(guān)注
關(guān)注
28文章
1352瀏覽量
110441 -
System
+關(guān)注
關(guān)注
0文章
165瀏覽量
37089 -
CAST
+關(guān)注
關(guān)注
0文章
8瀏覽量
9396 -
數(shù)據(jù)類型
+關(guān)注
關(guān)注
0文章
236瀏覽量
13667
原文標(biāo)題:?SystemVerilog中的動(dòng)態(tài) Cast
文章出處:【微信號(hào):芯片驗(yàn)證工程師,微信公眾號(hào):芯片驗(yàn)證工程師】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
SystemVerilog中的Virtual Methods
SystemVerilog中的“const”類屬性
CAST工藝自控系統(tǒng)的開發(fā)
SystemVerilog的斷言手冊(cè)
SystemVerilog中的操作方法
SystemVerilog中可以嵌套的數(shù)據(jù)結(jié)構(gòu)
SystemVerilog中的package
SystemVerilog中的Shallow Copy
SystemVerilog中的Semaphores
KUKA機(jī)器人CAST_TO-CAST_FROM簡(jiǎn)析
KUKA機(jī)器人CAST_TO-CAST_FROM數(shù)據(jù)傳輸
![KUKA機(jī)器人<b class='flag-5'>CAST_TO-CAST</b>_FROM數(shù)據(jù)傳輸](https://file1.elecfans.com/web2/M00/88/7D/wKgaomRqxJOASrIXAAIwWUUHbww962.jpg)
帶你了解SystemVerilog中的關(guān)聯(lián)數(shù)組
![帶你了解<b class='flag-5'>SystemVerilog</b><b class='flag-5'>中</b>的關(guān)聯(lián)數(shù)組](https://file1.elecfans.com/web2/M00/89/62/wKgaomSChR-AazDjAAAwrS_L4eA710.png)
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)
評(píng)論