System Verilog面向对象OOP-1

这篇博客探讨了面向对象编程(OOP)的概念,如封装、继承和多态,并通过SystemVerilog的实例展示了如何在硬件描述语言中应用这些概念。文章通过创建Rectangle和Square类来说明类的继承和方法重写,并通过实例展示了对象的创建和使用,进一步解释了OOP在HDL中的灵活性和实用性。
摘要由CSDN通过智能技术生成

面向对象(Object Oriented Programming)的特性:
封装:有个包,包里面很多东西,找到包的的名字就可以去拿里面东西
继承:子类的包里面东西比父类(基类)包里面的东西还多
多态:子类和父类的包里面都有一个椅子,并且都叫椅子,但是椅子的功能不一样,至于用哪一把椅子,可以自己决定去调用哪一把椅子
优势:Veriog使用接口层次方式,OOP直接使用函数或者任务例化调用的方式

HDLSV
moduleclass
instanceobject
instance namehandle
registers & wiresproperties (variables)
behavioral blocks(always,initial),task,functionstasks and functions
portcalls,mailbox

variables(properties)用来系统建模;
task & function(methods)用来控制,验证平台的运行,控制数据的传输。
properties 和 methods 就是一个类的成员变量

module 一开始就确定是否应该被例化,静态的;内部的变量和方法对外部公共开放
class随时构造new(),动态创建对象,更加灵活;根据需要选择受保护protected和local

继承:公共特性功能全部放在Basic Class中,很容易拓展一个新的class,定制全新的功能

多态:子类和父类中的function同名?到时候用谁的方法?
直接用基类变量来存放子类变量,会报错,使用虚函数virtual;子类直接存放父类,会报错,需要强制转换$cast();

子类和父类中的property和method同名?到时候用谁?
派生类(子类)可以重载override从基类继承的成员定义。

System veilog面向对象实操

class rectangle;
    int length;
    int width;
    
    function new(int l, int w);//method,调用new函数分配内存
      length = l;
      width  = w;
    endfunction
    
    function int area();
      return length * width;
    endfunction
    function int circumference;
      return 2*(length+width);
    endfunction 

 endclass
  
class square extends rectangle;
      function new(int side);
      super.new(.l(side), .w(side));//super关键字指示显示器显式的引用父类中定义的数据成员和方法,也就是调用rectangle中构造的函数,并使用side传入长度和宽度函数。
    endfunction
 endclass
  
  module top_class ;
    rectangle rectangle_h;//声明一个指向长方形的句柄;
    square    square_h;
    
    initial begin
    
      rectangle_h = new(.l(50),.w(20));//为对象分配内存
      $display("rectangle area: %0d", rectangle_h.area());
      $display("rectangle circumference: %0d", rectangle_h.circumference());

      square_h = new(.side(50));
      $display("square area: %0d", square_h.area());
      $display("square circumference: %0d", square_h.circumference());
      
    end
  endmodule

makefile文件

FILES   = classes.sv
all: clean comp run
VCS =	vcs -full64\
       	+v2k\
       	-sverilog\
        -sim_res=1ps\
	-l compile.log\

RUN   = ./simv -l vcs.log

comp:
	$(VCS) $(FILES)

run:
	$(RUN) 

clean:
	rm -rf AN.DB DVEfiles csrc *.simv *.simv.daidir ucli.key 64
	rm -rf *.log* *.vpd *.fsdb *.vdb novas* *verdi*
	rm -rf simv*

编译,仿真结果如下

rectangle area: 1000
rectangle circumference: 140
square area: 2500
square circumference: 200
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值