how to create a singleton object

how to create a singleton object

Creating a global singleton object that can be referenced from elsewhere in the testbench is sometimes a good thing. This is very similar to the way static variables in a class work - only one variable is created and made accessible for all class objects. In this case, a single class object is created that can be accessed from other testbench components. As an example, you could have such a class object to contain all the design or testbench specification features like number of masters and slaves, or clock frequency requirements for each interface, etc. Let's see how to effectively create a singleton object.

First, we have to define the class which will act as a singleton.

class mySingleton;
  function new (string name = "mySingleton");
    // Do something here
     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;

Note that a static and local variable of the same class type is created. This will be returned by the static function get() which can be called from somewhere outside the testbench. Next we'll develop a small environment and test to try out this feature.

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

Note that I have created a local m_sg class object and called the static function get() of the same class to access the singleton object. The first invocation will create a new m_sg object within the class and all further calls will return the same m_sg instance.

This implementation is similar to how UVM handles its singleton classes.

CDNS-UVM-1.1d (15.10-s004)
(C) 2007-2013 Mentor Graphics Corporation
(C) 2007-2013 Cadence Design Systems, Inc.
(C) 2006-2013 Synopsys, Inc.
(C) 2011-2013 Cypress Semiconductor Corp.
UVM_INFO @ 0: reporter [RNTST] Running test my_test...
UVM_INFO @ 0: uvm_test_top [TEST] Hey bud, this works well !
UVM_INFO @ 0: uvm_test_top.m_env [ENV] Hey bud, this works well !

--- UVM Report catcher Summary ---
Remove redundant register sequences with this neat...
Save time on re-compilation of the TB with an exte...


No comments made yet. Be the first to submit a comment
Already Registered? Login Here
Tuesday, 20 March 2018