1. 工厂机制概念
Factory 机制也叫工厂机制,其存在的意义是为了能够方便的替换验证环境中的实例或者已注册的类型,可以提高验证效率和环境复用率。
工厂机制最大的特点:一个子类的指针以父类的类型传递,其表现的行为依然是子类的行为。
- 封装--隐藏实际细节,使代码模块化
- 继承--扩展已存在的代码模块(类)
- 多态--重写/覆盖(override)--子类重新定义父类虚函数的做法(只有继承关系才可以override)
2. Factory步骤
-
将类注册到工厂
//uvm_component 类型例子
class comp extends uvm_component;
`uvm_component_utils(comp)
function new(string name = 'comp',uvm_component parent = null);
super.new(name, parent);
endfunction
.....
//uvm_object 类型例子
class obj extends uvm_object;
`uvm_obj_utils(obj)
function new(string name = 'obj');
super.new(name);
endfunction
.....
-
在例化前设置覆盖对象和类型(可选)
set_type_override_by_type(用得多)
set_inst_override_by_type
注:需要被override的function/task必须是virtual类型
-
创建对象
创建uvm_component 对象:comp_type::type_id::creat(string name,uvm_component parent)
创建uvm_object 对象:object_type::type_id::creat(string name)
module case1;
import uvm_pkg::*;
`include "uvm_macros.svh"
class comp extends component;
`uvm_component_utils(comp)
function new(string name = "comp",uvm_component parent = null);
super.new(name,parent);
endfunction
endclass
class obj extends component;
`uvm_object_utils(obj)
function new(string name = "obj");
super.new(name);
endfunction
endclass
comp c1;//例化
obj o1;//例化
c1= comp:type_id::creat("c1",null);//UVM 创建
o1= obj:type_id::creat("o1");/UVM 创建
endmodule
3. 重载满足条件
- 无论是重载的类还是被重载的类,都要定义在注册的工厂机制内
- 被重载的类实例化时,要是用type_id::creat,不可以使用new的方式
- 重载的类与被重载的类有派生关系,重载的类必须派生自被重载的类
- component 与object之间互相不能重载