static属性一般是在编译的时候就已经分配了内存,并被这个类的所有实例共享,
也就是在仿真时刻0之前就已经完成了静态属性的内存分配。
但是,参数化类中的静态属性可能有所区别。参数化类中的静态属性(参数化)是在参数初始化的时候才会分配。
// 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中,我们都声明了静态属性。在访问静态属性“static_wo_p”时没有问题,而在访问静态属性
“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);
下面这个例子更能够展示参数化类中的静态属性和非参数类中的静态属性的区别:
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中的参数T分别被覆盖成byte、byte、int、bit[2:0]、real,所以只有S1(s_byte)和S2(s_byte)中的静态属性counter彼此共享。
参数类的扩展类
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);
上面是一个参数化类的扩展类示例,class1是一个参数化类,参数T默认为"int"。
class2增加了一个参数P,此时参数T为默认的"int"
class3增加了一个参数P,此时参数T覆盖成"integer"
class4增加了一个参数P,此时参数T也覆盖成为P
全部0条评论
快来发表一下你的评论吧 !