面向对象(Object Oriented Programming)的特性:
封装:有个包,包里面很多东西,找到包的的名字就可以去拿里面东西
继承:子类的包里面东西比父类(基类)包里面的东西还多
多态:子类和父类的包里面都有一个椅子,并且都叫椅子,但是椅子的功能不一样,至于用哪一把椅子,可以自己决定去调用哪一把椅子
优势:Veriog使用接口层次方式,OOP直接使用函数或者任务例化调用的方式
HDL | SV |
---|---|
module | class |
instance | object |
instance name | handle |
registers & wires | properties (variables) |
behavioral blocks(always,initial),task,functions | tasks and functions |
port | calls,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