SystemVerilog中,initial begin-end是仿真開始就會執(zhí)行的代碼塊。比如UVM的test入口函數(shù)run_test,一般就是在initial begin-end中調(diào)用。還有一些tb會在initial begin-end中使用fork join_none,用于創(chuàng)建一些仿真中的后臺進(jìn)程,如時(shí)鐘產(chǎn)生,后門驅(qū)動(dòng)等。
那么initial begin-end真的是仿真最早執(zhí)行的嗎?
如果是消耗仿真時(shí)間的,那initial begin-end中的代碼是仿真開始最早執(zhí)行的。如果不消耗仿真時(shí)間,那還有一種代碼會早于initial begin-end執(zhí)行。
static property/function !!!
static類型變量,無論是全局變量,還是class內(nèi)部參數(shù),會在仿真開始前確定其初始值。如果該初始值是一個(gè)由static類型的function返回值決定,則該function的代碼會在initial begin-end前執(zhí)行完畢。
可以參考如下的測試:
importuvm_pkg::*; `include"uvm_macros.svh" classstatic_wrapper; staticbitfst_flag=cls_func_before_initial("static_wrapper:fst_flag"); staticbitsnd_dlag=cls_func_before_initial("static_wrapper:snd_flag"); staticfunctionbitcls_func_before_initial(stringx); intcnt; cnt++; $display("cls_func_before_initial:",x,"@",$time); return1; endfunction endclass staticfunctionbitglb_func_before_initial(stringx); intcnt; cnt++; $display("glb_func_before_initial:",x,"@",$time); return1; endfunction classtestextendsuvm_test; `uvm_component_utils(test) functionnew(stringname="test",uvm_componentparent=null); super.new(name,parent); endfunction virtualtaskmain_phase(uvm_phasephase); super.main_phase(phase); phase.raise_objection(this); uvm_top.print(); phase.drop_objection(this); endtask endclass programtb_top; staticbitthd_flag=glb_func_before_initial("thd_flag"); initialbegin $display("initialbegin...@",$time); run_test("test"); $display("initialend...@",$time); end endprogram
仿真結(jié)果如下:
cls_func_before_initial:static_wrapper:fst_flag@0 cls_func_before_initial:static_wrapper:snd_flag@0 glb_func_before_initial:thd_flag@0 initialbegin...@0 UVM_INFO@0:reporter[RNTST]Runningtesttest... ------------------------------------- NameTypeSizeValue -------------------------------------uvm_root-@172 uvm_test_toptest-@336 ------------------------------------- UVM_INFO/apps/vcsmx/vcs/S-2021.09//etc/uvm-1.2/src/base/uvm_report_server.svh(904)@0:reporter[UVM/REPORT/SERVER] ---UVMReportSummary--- **Reportcountsbyseverity UVM_INFO:2 UVM_WARNING:0 UVM_ERROR:0 UVM_FATAL:0 **Reportcountsbyid [RNTST]1 [UVM/RELNOTES]1 $finishcalledfromfile"/apps/vcsmx/vcs/S-2021.09//etc/uvm-1.2/src/base/uvm_root.svh",line527. $finishatsimulationtime0
可以看到cls_func_before_initial和glb_func_before_initial兩個(gè)function都會在initial begin-end前執(zhí)行。
上面的例子也可以看到,在run_test之后的代碼塊并不會執(zhí)行,這是因?yàn)閞un_test執(zhí)行結(jié)束后,UVM機(jī)制會直接調(diào)用$finish函數(shù)結(jié)束仿真。
其實(shí)在UVM中,已經(jīng)利用了static變量的初始化這一特性。UVM的工廠模式中,使用uvm_component_utils/uvm_object_utils向工廠中注冊組件時(shí),就利用了這一特性。逐層展開uvm_component_utils宏時(shí),可以看到如下的代碼:
classuvm_component_registry#(typeT=uvm_component,stringTname="")extendsuvm_object_wrapper; //.... localstaticthis_typeme=get(); staticfunctionthis_typeget(); if(me==null)begin uvm_coreservice_tcs=uvm_coreservice_t::get(); uvm_factoryfactory=cs.get_factory(); me=new; factory.register(me); end returnme; endfunction //....
思路打開,利用static變量初始化這一特性,可以嘗試更多的應(yīng)用。
審核編輯:劉清
-
Verilog
+關(guān)注
關(guān)注
29文章
1367瀏覽量
112215 -
UVM
+關(guān)注
關(guān)注
0文章
182瀏覽量
19528
原文標(biāo)題:initial begin-end真的是SystemVerilog 仿真最早執(zhí)行的嗎?
文章出處:【微信號:處芯積律,微信公眾號:處芯積律】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
重點(diǎn)介紹所有綜合編譯器都支持的for和repeat循環(huán)

GPIO Init函數(shù)結(jié)尾沒有begin end用戶編輯區(qū)域,為什么?
pxa255開發(fā)板原理圖及源代碼
為什么msp430g2553在仿真的時(shí)候程序能執(zhí)行,脫機(jī)無法執(zhí)行
begin ...... end 與 fork ...... join 語句的 區(qū)別 ------ 轉(zhuǎn)載
ModleSim仿真的時(shí)候有一個(gè)信號不能被賦值
怎樣把pad designer軟件里Bgn層的BEGIN LAYER改為END LAYER?
SIMULINK仿真的運(yùn)行
initial和always兩者的關(guān)系分析
Verilog的兩種塊語句解析
Verilog的塊語句fork...join 和 begin...end
淺析標(biāo)準(zhǔn)的Verilog對語句有兩種分組方式

評論