sv语法中virtualmethod和purevirtualmethod理解
举例说明:
class A_base ;
int s0 =2;
int s1 =3;
function print_s0()
$disaplay("print s0 value is %d", s0)
endfunction
virtual function print_s1()
$display("print s1 value is %d",s1)
endfunction
endclass
class A extends A_base;
int s0=5;
int s1=7;
function print_s0()
$disaplay("print s0 value is %d", s0)
endfunction
virtual function print_s1()
$display("print s1 value is %d",s1)
endfunction
endclass
virtual class B_base extends A;
pure virtual task M( );
task body():
...........
endtask
endclass
class B extends um B_base;
task M( );
endtask
endclass
class test_base;
B b;
task build_phase()
b=B::get_type_id::create("b");
endtask
task run_phase()
b.start(vsqr):
endtask
endclass
A_base p1=new();
A p2=new();
initial begin
p1.print_s0();// s0=2
p1.print_s1(); //s1=3
p1=p2;//p1 has a handle of A object.
p1.print_s0();//s0=2
p1.print_s1();//s1=7, so the function was the last derived function
p2.print_s0();//s0=5
p2.print_s1();//s1=7
end
Notes :
1. A_base object&A object的关系. A is derived from A_base, they are all newed, so they are both implemented. p1=p2 意味着p1 get a handle/vector(句柄) from A 实体. virtual function in A_base and A means they are polymorphic(多态),the function used last is the last derived one(传递的最后一个function)
2. B_base为virtual. 这个叫abstract class. 特性是它被extend之后的,里面的task会被implemented或者override。在B_base 里面的pure virtual task M 在它的derived class B里面被implemented。这个是pure virtual的特性。
3. 关于virtual, sv里面的一段话: A virtual method shall override a method in all of its base classes, whereas a non-virtual method shall only override a method in that class and its descendants. One way to view this is that there is only one implementation of a virtual method per class hierarchy, and it is always the one in the latest derived class。
4. 关于pure virtual,sv里面的一段话:
An object of an abstract class shall not be constructed directly. Its constructor may only be called indirectly through the chaining of constructor calls originating in an extended non-abstract object.
A virtual method in an abstract class may be declared as a prototype without providing an implementation.This is called a pure virtual method and shall be indicated with the keyword pure together with not providing a method body. An extended subclass may provide an implementation by overriding the pure virtual method with a virtual method having a method body.
5. 通过#4, 我们知道abstract class只能在后面的non-abstract object 调用的时候被启动。 也就是说test_base里面.start 实际上是调用的 abstract class里面的body.虽然写的是non-abstract 的handle(对象)b.start(). 作用:丰富base class的框架,便于分清层次关系。
sv中的virtualmethod和cast,多态
举例说明
枚举类型的缺省类型为双状态int,可以使用简单的赋值表达式把枚举类型变量的值直接赋值给非枚举变量 如int.但SV不允许在没有进行显示类型转换的情况下把int变量直接赋值给枚举变量。SV要求显式的类型转换的目的在于让你意识到可能的数据越界情况。
typedef enum bit[1:0] {RED=0,BLUE,GREEN} COLOR_E;
COLOR_E color,c2;
int c;
initial begin
color = BLUE; // 赋值一个已知的合法的值
c = color; // 将枚举变量赋值给int,此时为 1
c = c+1; // int型变量递增
if(!$cast(color,c)) // 将整型显示转换回枚举类型,如果越界会报错
$display("cast failed for c= ",c); // c的值此时为2
$display("Color is /%s",color,color.name());
c++; // c的值为3,对于枚举类型已然越界
c2 = COLOR_E'(c); // 不做类型检查,下句c2.name()由于越界而打印不出
$display("c2 is /$s",c2,c2.name()); // 打印:c2 is 3/
原文链接:https://blog.csdn.net/lbt_dvshare/article/details/105155525
4-3 竟然是自己调自己。看起来会用在基类里面的重载。
class transaction;
virtual function transaction copy(input transaction to);
if(to==null)
copy = new();
else
copy = to;
copy.src = this.src;
return copy;
endfunction
endclass
class badtr extends transaction;
virtual function transaction copy(input transaction to);
badtr bad;
if(to==null)
bad = new();
else
$cast(bad,to);
super.copy(bad);
bad.bad_csm = this.bad_csm;
return bad;
endfunction
endclass
badtr b_tr1 = new();
badtr b_tr2 = new();
badtr b_tr3;