UVM 设计模式之Singleton

1.Singleton Class:

类似于static 变量,随处可见,随处可用,对于环境整体中的控制类型的变量可以定义为这种变量

class mySingleton;
  function new (string name = "mySingleton");
    // Do something here
     endfunction
 
     static local mySingleton m_sg;       // singleton object
     string name;                         // my test variable
 
     static function mySingleton get();
        if (m_sg == null)
           m_sg = new ();
        return m_sg;
     endfunction
endclass

2.Singleton Class 实现方式:

static 类型的get函数

//--------------------------- my_env -----------------------------
 
class my_env extends uvm_env;
   `uvm_component_utils (my_env)
   function new (string name="my_env", uvm_component parent);
      super.new (name, parent);
   endfunction
 
   virtual function void build_phase (uvm_phase phase);
      mySingleton  m_sg = mySingleton::get();
      super.build_phase (phase);
      `uvm_info ("ENV", $sformatf ("%s", m_sg.name), UVM_MEDIUM)
   endfunction
endclass
 
//--------------------------- my_test -----------------------------
 
class my_test extends uvm_test;
   `uvm_component_utils (my_test)
   function new (string name = "my_test", uvm_component parent);
      super.new (name, parent);
   endfunction
 
   my_env m_env;
 
   virtual function void build_phase (uvm_phase phase);
      mySingleton m_sg = mySingleton::get();
      super.build_phase (phase);
      m_sg.name = "Hey bud, this works well !";
      `uvm_info ("TEST", $sformatf ("%s", m_sg.name), UVM_MEDIUM) 
      m_env = my_env::type_id::create ("m_env", this);
   endfunction
endclass


posted on 2019-06-16 00:15  hematologist  阅读(651)  评论(0编辑  收藏  举报

导航