前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SystemVerilog中多态与虚方法

SystemVerilog中多态与虚方法

作者头像
数字IC小站
发布2020-06-30 16:08:22
1.3K0
发布2020-06-30 16:08:22
举报
文章被收录于专栏:数字IC小站

在验证过程中,往测试平台中添加新的测试激励很正常的事,这样的话就需要对原来的测试平台进行改进,有的时候需要修改原来的代码甚至可能修改一些数据结构,这个过程中可能会导致在原来的验证平台中引入意外的错误。那么为了保证原有平台或数据结构不变,通过对已经有的基类进行引申或者扩展,从而完善整个验证平台。 从基类做扩展并产生新的子类的过程叫类的派生,当一个类被扩展并创建之后,该派生类就继承了其基类的数据成员、属性和方法,这就是类的继承。 继承后的类可以实现以下功能: 1.继承了原来类中的方法,并可以修改 2.添加新的方法 3.添加新的数据成员 在实现以上功能的同时需要满足一定的规则: 1.子类继承父类的所有数据成员和方法 2.子类可以添加新的数据成员和方法 3.子类可以重写父类中的数据成员和方法 4.如果一个方法被重写,其必须保持和父类中原有定义有一致的参数 5.子类可以通过super操作符来引用父类中的方法和成员 6.被声明为local的数据成员和方法只能对自己可见,对外部和子类都不可见;对声明为protected的数据成员和方法,对外部不可见,对自身和子类可见。

首先看下:

ex1:

代码语言:javascript
复制
class A;
    virtual function void print_name();
        $display ("this is the class a.\n");

    endfunction
endclass:A

class B extends A;
    virtual function void print_name();
        $display ("this is the class b.\n");
    endfunction

    virtual function void new_display();
        $display("this a new method.\n");
    endfunction
endclass:B

program testbench;
    initial begin
        begin:class_test1
            A classa;
            B classb;
            classa = new();
            classb = new();
            classa.print_name();
            classb.print_name();
        end
        begin:class_test2
            A classa;
            B classb;
            classb = new();
            classa = classb;
            classa.print_name();
            classb.print_name();
        end
        begin:class_test3
            A classa1,classa2;
            B classb1,classb2;
            classa2 = new();
            classb1 = new();
            classa1 = classb1;
            classb2 = classb1;
            classa2.print_name();
            classb2.print_name();
            classa2 = classa1;
            $cast(classb2,classa2);
            classa2.print_name();
            classb2.new_display();
            classb2.print_name();
          //  classa2.new_display();
        end
    end
endprogram

上述代码中,很容易理解,因为classa2中不含有new_display这种method,因此会报error,我把其注释掉了,其次,我们使用$cast转换的前提是先把子类赋给父类,这时候才能使用cast把该父类再赋给子类,如果没有前提条件,还是会报error的,仿真结果如下:

代码语言:javascript
复制
# this is the class a.
# 
# this is the class b.
# 
# this is the class b.
# 
# this is the class b.
# 
# this is the class a.
# 
# this is the class b.
# 
# this is the class b.
# 
# this a new method.
# 
# this is the class b.

上述例子中利用了虚方法,如果去掉虚方法,那么情况如下:

ex2:

还是上述代码,只是去掉virtual,则仿真结果如下:

代码语言:javascript
复制
# this is the class a.
# 
# this is the class b.
# 
# this is the class a.
# 
# this is the class b.
# 
# this is the class a.
# 
# this is the class b.
# 
# this is the class a.
# 
# this a new method.
# 
# this is the class b.
#

ex3:

如果父类添加virtual method,但是子类不添加,仿真结果如下:

代码语言:javascript
复制
# this is the class a.
# 
# this is the class b.
# 
# this is the class b.
# 
# this is the class b.
# 
# this is the class a.
# 
# this is the class b.
# 
# this is the class b.
# 
# this a new method.
# 
# this is the class b.
#

ex4:

基类添加virtual method 后面的所有继承类都不添加,代码如下:

代码语言:javascript
复制
class A;
    virtual function void print_name();
        $display ("this is the class a.\n");

    endfunction
endclass:A
class B_novir_vir extends A;
    function void print_name();
        $display ("this is the class b.\n");
    endfunction

    function void new_display();
        $display("this a new method.\n");
    endfunction
endclass:B_novir_vir

class C_novir_vir extends B_novir_vir;
    function void print_name();
        $display ("this is the class C.\n");
    endfunction

endclass:C_novir_vir
program testbench3;
    initial begin
        begin:class_test1
            C_novir_vir classc;
            B_novir_vir classb;
            classb = new();
            classc = new();
            classb.print_name();
            classc.print_name();
        end
        begin:class_test2
            C_novir_vir classc;
            B_novir_vir classb;
            classc = new();
            classb = classc;
            classb.print_name();
            classc.print_name();
        end
        begin:class_test3
            C_novir_vir classc1,classc2;
            B_novir_vir classb1,classb2;
            classb2 = new();
            classc1 = new();
            classb1 = classc1;
            classb2.print_name();
            classc1.print_name();
            classb2 = classb1;
            $cast(classc2,classb2);
            classb2.print_name();
            classc2.new_display();
            classc2.print_name();
        end
    end
endprogram

仿真结果如下,说明只要是基类有virtual method就行,其他子类可以不需要有。

代码语言:javascript
复制
# this is the class b.
# 
# this is the class C.
# 
# this is the class C.
# 
# this is the class C.
# 
# this is the class b.
# 
# this is the class C.
# 
# this is the class C.
# 
# this a new method.
# 
# this is the class C.

你点亮的每个在看,我都认真当成了喜欢、看完记得点亮在看哦~

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-12-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数字IC小站 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档