包的使用
-
为了可以在多个模块(硬件)或者类(软件)之间共享用户定义的类型,SV添加了包(package)
-
包的概念参考与VHDL,用户自定义的类型如:类、方法、变量、枚举类等都可以在包中定义
package definitions; parameter VERSION = "1.1"; typedef enum {ADD, SUB, MUL} opcodes_t; typedef struct { logic [31:0] a, b; opcodes_t opcode; } instruction_t; function automatic [31:0] multiplier(input[31:0] a, b); return a*b; endfunction endpackage
-
导入包
-
方法一
// 包a package pkg_a; class packet_a; endclass int a=1; endpackage // 包b package pkg_b; class packet_b; endclass int b; endpackage module testbench_top(); initial begin $display("pkg_a::a is %0d", pkg_a::a); // 使用::获取包内的成员 $display("pkg_b::b is %0d", pkg_b::b); end endmodule
-
方法二
// 包a package pkg_a; class packet_a; endclass int a=1; endpackage // 包b package pkg_b; class packet_b; endclass int b; endpackage module testbench_top(); import pkg_a::a; // 使用import导入指定包内的指定成员,如果模块内有同名成员编译会报错 import pkg_b::b; initial begin $display("pkg_a::a is %0d", a); $display("pkg_b::b is %0d", b); end endmodule
-
方法三
// 包a package pkg_a; class packet_a; endclass int a=1; int val = 10; endpackage // 包b package pkg_b; class packet_b; endclass int b; int val = 11; endpackage module testbench_top(); import pkg_a::*; // 使用import导入指定包内的所有成员,模块里面找不到的时候才去包里找,所以模块内和包内存在同名成员时不冲突 import pkg_b::*; initial begin $display("pkg_a::a is %0d", a); $display("pkg_b::b is %0d", b); $display("val is %0d", pkg_a::val); // 两个包内有同名变量时,使用的时候需要指定具体哪个包 end endmodule
-