Facebook/LinkedIn login is now deprecated, please disconnect our access to your social profile.
User dashboard under chipverify.com/connect will be deprecated from Oct 1, 2020, Read More.
Let us contribute to a cleaner Earth, Go Green Updated: May 31, 2020
2 minutes reading time (429 words)

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);
      super.new (name, parent);
   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)
//--------------------------- 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);
   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);

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 tb_top.sv(45) @ 0: uvm_test_top [TEST] Hey bud, this works well !
UVM_INFO tb_top.sv(29) @ 0: uvm_test_top.m_env [ENV] Hey bud, this works well !

--- UVM Report catcher Summary ---
Effective usage of uvm_reg methods
Save time on re-compilation of the TB with an exte...

By accepting you will be accessing a service provided by a third-party external to https://www.chipverify.com/

You consent to our cookies if you continue to use our website. To know more about cookies, see our privacy policy. I accept cookies from this site.