static屬性一般是在編譯的時候就已經(jīng)分配了內(nèi)存,并被這個類的所有實例共享,
也就是在仿真時刻0之前就已經(jīng)完成了靜態(tài)屬性的內(nèi)存分配。
但是,參數(shù)化類中的靜態(tài)屬性可能有所區(qū)別。參數(shù)化類中的靜態(tài)屬性(參數(shù)化)是在參數(shù)初始化的時候才會分配。
// Class with parameters class with_param #(type T = int); static T static_with_p; endclass // Class without Parameters class without_param; static int static_wo_p; endclass module top; initial begin $display("static_wo_p = %0d", without_param :: static_wo_p); $display("static_with_p = %0d", with_param :: static_with_p); end endmodule: top
在上面的兩個class中,一個包含parameter (with_param),還有一個不包含parameter(without_param).
在各自class中,我們都聲明了靜態(tài)屬性。在訪問靜態(tài)屬性“static_wo_p”時沒有問題,而在訪問靜態(tài)屬性
“static_with_p”時,編譯器會報錯(Error或者Warning):
Warning-[PCSRMIO] Class scope used outside of class testbench.sv, 59 "with_param::static_with_p" An unspecialized class scope '::' reference was seen. To access a static member of the default specialization outside the class 'with_param', use 'with_param#( )::' instead. This will be an error in a future release.
需要修改成下面這樣的寫法才能編譯通過。
$display("static_with_p = %0d", with_param # ( ) :: static_with_p);
下面這個例子更能夠展示參數(shù)化類中的靜態(tài)屬性和非參數(shù)類中的靜態(tài)屬性的區(qū)別:
class with_param #(type T = int); static T counter = 2; function new; counter++; endfunction: new endclass: with_param class with_param_extend extends with_param #(real); endclass: with_param_extend typedef with_param #(byte) s_byte; s_byte S1 = new( ); s_byte S2 = new( ); with_param S3 = new( ); with_param #(bit[2:0]) S4 = new( ); with_param_extend S5 = new( ); initial begin $display ("Counter value of S1 instance = %0d", with_param #(byte)::counter); $display ("Counter value of S2 instance = %0d", s_byte:: counter); $display ("Counter value of S3 instance = %0d", with_param #()::counter); $display ("Counter value of S4 instance = %0d", with_param #(bit[2:0])::counter); $ d i s p l a y ( " C o u n t e r value of S5 instance =%0d",with_param_extend::counter); end
仿真log:
Counter value of S1 instance = 4 Counter value of S2 instance = 4 Counter value of S3 instance = 3 Counter value of S4 instance = 3 Counter value of S5 instance = 3.000000 V C S S i m u l a t i o n R e p o r t
上面的例子中S1、S2、S3、S4、S5中的參數(shù)T分別被覆蓋成byte、byte、int、bit[2:0]、real,所以只有S1(s_byte)和S2(s_byte)中的靜態(tài)屬性counter彼此共享。
參數(shù)類的擴(kuò)展類
class class1 #(type T = int); …. endclass class class2 #(type P = real) extends class1; class class3 #(type P = real) extends class1 #(integer); class class4 #(type P = real) extends class1 #(P);
上面是一個參數(shù)化類的擴(kuò)展類示例,class1是一個參數(shù)化類,參數(shù)T默認(rèn)為"int"。
class2增加了一個參數(shù)P,此時參數(shù)T為默認(rèn)的"int"
class3增加了一個參數(shù)P,此時參數(shù)T覆蓋成"integer"
class4增加了一個參數(shù)P,此時參數(shù)T也覆蓋成為P
-
屬性
+關(guān)注
關(guān)注
0文章
23瀏覽量
8530 -
static
+關(guān)注
關(guān)注
0文章
33瀏覽量
10366
原文標(biāo)題:參數(shù)化Class中的靜態(tài)屬性
文章出處:【微信號:芯片驗證工程師,微信公眾號:芯片驗證工程師】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
評論