admin管理员组

文章数量:1122854

SV,class学习笔记五、参数化类(parameterized class)

1.改变类中传递的参数

//定义一个参数化类
class packet #(int size = 1);......
endclass//在创建句柄handle时改变参数
packet #(.size(16)) p2;

Example

class packet #(int size = 1); //定义参数化类bit [size-1 : 0] a;
endclass:packetmodule tb;packet #(8) p1;       //改写参数packet #(.size(16))p2;//改写参数typedef packet #(.size(32)) mypacket; //声明一个与packet类别名的mypacket类同时改写参数mypacket p3;initial beginp1 = new;p2 = new;p3 = new;$display("[size] p1.a  =%0d",$bits(p1.a));$display("[size] p2.a  =%0d",$bits(p2.a));$display("[size] p3.a  =%0d",$bits(p3.a));	end
endmodule

Sim Log

 

 2.将数据类型参数化,便于改写数据类型。

//定义T为int型
class packet # (type T = int);T t;.....endclass//将int改写为real 
packet #(real) p1;

example 

class packet #(type T = int);//将类里的int参数化T t;function T sum (T addr);//定义一个sun函数,返回值类型为T,传递参数类型为Treturn  t + addr;endfunctionendclass:packetmodule tb;packet #(real) real_p1;    //将T改写为 real packet #(bit[3:0]) bit_p2; //将T改写为 bit[3:0]packet p3;initial beginreal_p1 = new;bit_p2 = new;p3 = new;//分别给类中三种不同类型的t赋值,打印real_p1.t = 1.1;$display("return  = %0.2f",real_p1.sum(10));bit_p2.t = 4'b0000;$display("return  = %0b",bit_p2.sum(4'b1010));p3.t = -1;$display("return  = %0d",p3.sum(10));	end
endmodule

sim log

本文标签: SVclass学习笔记五参数化类(parameterized class)