uvm_factory——我们的工厂(一)

factory 机制是实现(功能):通过一个字符串来创建此字符串所代表的的类的一个实例。

//------------------------------------------------------------------------------
//
// Group: Usage
//
// Using the factory involves three basic operations
//
// 1 - Registering objects and components types with the factory
// 2 - Designing components to use the factory to create objects or components
// 3 - Configuring the factory with type and instance overrides, both within and
//     outside components

factory 的使用方法:

在object 和component 实例化时用`uvm_object_utils(packet)和`uvm_component_utils(comp)进行注册,然后,使用driver0 = B_driver::type_id::create("driver0",this);

最后,根据需要将type和instance overrides.

首先来看`uvm_object_utils(T)的宏定义, uvm_object_utils 又是用uvm_object_utils_begin(T) 实现的:

`define uvm_object_utils(T) \
  `uvm_object_utils_begin(T) \
  `uvm_object_utils_end

uvm_object_utils_begin(T) 由如下几个宏实现。

`define uvm_object_utils_begin(T) \
   `m_uvm_object_registry_internal(T,T)  \
   `m_uvm_object_create_func(T) \
   `m_uvm_get_type_name_func(T) \
   `uvm_field_utils_begin(T) 

首先来看m_uvm_object_reistry_internal(T),这里最重要的是uvm_ object_registry#(T, `"S`"). 

// m_uvm_object_registry_internal
// ------------------------------

//This is needed due to an issue in of passing down strings
//created by args to lower level macros.
`define m_uvm_object_registry_internal(T,S) \
   typedef uvm_object_registry#(T,`"S`") type_id; \
   static function type_id get_type(); \
     return type_id::get(); \
   endfunction \
   virtual function uvm_object_wrapper get_object_type(); \
     return type_id::get(); \
   endfunction 

接着,再看`m_uvm_object_create_func(T), 本质就new uvm_object 的类。

//-----------------------------------------------------------------------------
// INTERNAL MACROS - in support of *_utils macros -- do not use directly
//-----------------------------------------------------------------------------

// m_uvm_object_create_func
// ------------------------

`define m_uvm_object_create_func(T) \
   function uvm_object create (string name=""); \
     T tmp; \
`ifdef UVM_OBJECT_DO_NOT_NEED_CONSTRUCTOR \
     tmp = new(); \
     if (name!="") \
       tmp.set_name(name); \
`else \
     if (name=="") tmp = new(); \
     else tmp = new(name); \
`endif \
     return tmp; \
   endfunction

再往下看 m_uvm_get_type_name_func(T),得到type的名字。

// m_uvm_get_type_name_func
// ----------------------

`define m_uvm_get_type_name_func(T) \
   const static string type_name = `"T`"; \
   virtual function string get_type_name (); \
     return type_name; \
   endfunction 

看完了uvm_object_utils_begin(T) ,我们把目光转向uvm_object_registry#(T,S) type_id; 为什么会有这么一个class,它到底干了些什么?

uvm_object_registry这个类的定义在src/base/uvm_registry.svh里面,这个文件里面定义了uvm_object_registry和uvm_component_registry两个类, 下一节我们先看是前者

uvm_object_registry。uvm_object_registry和uvm_component_registry都继承自uvm_object_wrapper(在uvm_factory.svh中)。

 

posted on 2017-11-20 11:30  dpc525  阅读(734)  评论(0编辑  收藏  举报

导航