Home System Verilog SV Classes Singleton Class
Main Menu
Home
System Verilog
    - Constructs
    - SV Classes
        -- Simple Class
        -- Inheritance
        -- Encapsulation
        -- this and super
        -- In Line Constraints
        -- Class Constraints
        -- Virtual Classes
        -- Parameterized Classes
        -- Summary
        -- Singleton Class
    - Functional Coverage SV
    - Examples
    - Tools
    - Links
    - Books
    - Interview Questions SV
Verilog
Methodologies
Open Vera
Digital Concepts
Verification Basics
Protocols
Scripting
Articles
Videos
Interview Questions
Computer Architechture
C and C++
Blog/Article
AsicGuru Blog
Tags Cloud
Ads
Usefull Sites
Know Your IP/Location
Local Information India
Buy Car/Inverter Batteries
Real Estate India
Sports Accessories India
Singleton class in system verilog
Share This Articale:

Singleton in System Verilog :

Sometimes it is required to have only one object of some classes like configuration classes. For this purpose we create singleton classes. Only one object is created for a singleton class and whenever we try to create a new object, same object is returned.

System verilog does not provide construct to create a singleton class. But we can create it with some manupulations

 

// Filename  : singleton.sv
// Author    : Puneet (er.punit@gmail.com)
// Website   : http://www.asicguru.com

class singleton;
  int len;

  static singleton objSingle;

  local function new (int len=0);
    this.len = len;
  endfunction : new

  function void print_len ();
    $display("Len is = %d", len);
  endfunction : print_len

  static function singleton create (int len=0);
    if (objSingle == null) begin
        $display("Object is null creating new object \n");
        objSingle = new(len);
    end

    return objSingle;
  endfunction : create

endclass

program main;

  singleton objSingle1;
  singleton objSingle2;

  initial
  begin
    objSingle1 = singleton::create ( );
    objSingle2 = singleton::create ( );

    $display("Testing Singleton object \n");
    objSingle2.len = 10;
    objSingle1.print_len();   // Call print len function
    objSingle2.print_len();   // Call print len function

    objSingle1.len = 99;
    objSingle1.print_len();   // Call print len function
    objSingle2.print_len();   // Call print len function
  end

endprogram: main




//Output 
Object is null creating new object

Testing Singleton object

Len is =          10
Len is =          10
Len is =          99
Len is =          99
           V C S   S i m u l a t i o n   R e p o r t


Keywords : class   singleton  

This Articles is written/submitted by puneet (Puneet Aggarwal). You can also contribute to Asicguru.com. Click here to start


Prev << Summary

 
Login/Register
Register
Sign In
Login with :-
gmail-yahoo-twitter-facebook
| | |  
  •  
  • Bookmark
    ADS