| |
与任何其他OOP语言(如C ++和Java)中的类一样,SystemVerilog类定义是在内存中构造的对象的模板。一旦创建,该对象将持久存储在内存中,直到它被解除引用并由自动后台进程进行垃圾回收。类模板定义了类的成员,它们可以是数据变量或方法。在SystemVerilog中,方法可以是非耗时的函数,也可以是消耗时间的任务。由于类对象必须在它存在于内存之前构造,因此必须从模块启动SystemVerilog测试平台中类层次结构的创建,因为模块是仿真开始时存在的静态对象。出于同样的原因,类不能包含模块。类被称为动态对象,因为它们可以在仿真的整个生命周期中来和去。
//
// 示例如何在静态对象(模块)中构造类
//
//
// 包含消息和一些便捷方法的示例类
//
class example;
string message;
function void set_message(string ip_string);
message = ip_string;
endfunction: set_message
function void print();
$display("%s", message);
endfunction: print
endclass: example
//
// 使用类的模块 - 类被构造,使用和解引用
// 在仿真开始后的初始块中
//
module tb;
example C; // Null handle after elaboration
initial begin
C = new(); // 处理指向内存中的C对象
C.set_message("This object has been created");
#10;
C.print();
C = null; // C已被解除引用,对象可以被垃圾收集
end
endmodule: tb