Electronics Directory Articles/ Tutorials eBooks

About Us

FORUM Links Contact Us

SystemVerilog Tutorial PART 12:  by Abhiram Rao

Classes in SystemVerilog- 2


 <Previous     TOC      Next>

This is an example of how to use the OO Classes. Here we define a class. A class is like a blueprint to a home, it spells out the framework and design of the house first we declare variables that define general properties of all houses even though a specific house will have unique data.










program example_classes();

   // here we define a class. A class is like a blueprint

   // to a home, it spells out the framework and design

   // of the house


   class House_Blueprint;

   // first we declare variables that define general properities

   // of all houses even though a specific house will

   // have unique data

      int number_of_rooms;

      bit family_room_lightswitch;

      static string street_name; // all homes here share the same street


      // on building a house, initialize defaults

      // and build the house based on argument liss

      function new(int rooms, string street = "");

        if (street != "")

           street_name = street;

           number_of_rooms = rooms;

           family_room_lightswitch = 0;


   // we can flip the lights of a specific house with

   // a method call

   task hit_the_switch() ;

       family_room_lightswitch = !family_room_lightswitch;


   // we can return values in function calls

   function bit lights() ;

      lights = family_room_lightswitch;




House_Blueprint red, blue, yellow, green;

  // here we declare handles, they don't point to anything yet

  int result;



     // red now points to a home with 4 rooms

     red = new(4, "middlefield");

     // green now points to a home with 6 rooms

     // note the second arg is missing!

     // Don't worry: Its a default arguement, so its optional

     green = new(6);

     // here we turn on the light switch in the red house

     // by accessing the method



     // we can do the same thing by directly accessing the variable,

     // as long as it is a public variable

     // note that while this is less coding, the purpose of

     // methods is to check the input and do the calculations

     // to prevent introduction of incorrect data

     // (ie -1 rooms wouldn't make sense )

     red.family_room_lightswitch = 1;

     // here we can get the results of the lights being on or off

     // in the room. Note the red house and the green house

     // are based on the same blueprint, but have different

     // values

     result = red.lights();

     result = green.lights();





The this keyword is used to unambiguously refer to class properties or methods of the current instance. The this keyword denotes a predefined object handle that refers to the object that was used to invoke the subroutine that this is used within. The this keyword shall only be used within non-static class methods, otherwise an error shall be issued. For example, the following declaration is a common way to write an initialization task:

class Demo ;

   integer x;

   function new (integer x)

      this.x = x;



The x is now both a property of the class and an argument to the function new. In the function new, an unqualified reference to x shall be resolved by looking at the innermost scope, in this case the subroutine argument declaration. To access the instance class property, it is qualified with the this keyword, to refer to the current instance.

Inheritance and subclasses:

A key feature of SystemVerilog classes is inheritance. Inheritance allows to create classes which are derived from other classes, so that they automatically include some of its "parent's" members, plus its own. In other words, inheritance is a process or technique by which one class, the derived class, inherits the members and methods of another class, the base class. Additional members and methods are usually added to the derived class to make it more specialized. For example, in the below example class Circle inherits the properties of class Shapes. The base class part of an object is always constructed first and destroyed last. The subclass part of an object is constructed last and destroyed first.


 class Shapes;

    // every shape must have a total size, and a start coordinate

    int total_size;

    int start_x, start_y;

    bit[7:0] color;

    // every shape must implement draw and color actions

    task draw();



    // by defining code here, it becomes optional for

    // derived methods to define it there.

    // because it is virtual, the derived object method

    // will override this call, unless super is used by

    // the derived object to call this one

     task setcolor (bit [7:0] color);

       this.color = color;





class Circle extends Shapes;

  bit [30:0] circle_d_data;

  function new(int size = 4);

      total_size = size * 3;


  // here is how I would draw a circle

  // I keep it virtual so that one someone

  // can create a 3D circle and build upon

  // this example

   task draw();

     // do the actual draw here

     $display("drawing a circle...\n");





The super keyword is used from within a derived class to refer to members of the parent class. It is necessary to use super to access members of a parent class when those members are overridden by the derived class.

class Packet;                       //parent class

   integer value;

   function integer delay();

      delay = value * value;




class LinkedPacket extends Packet;  //derived class

    integer value;

    function integer delay();

        delay = super.delay()+ value * super.value;




The member can be a member declared a level up or be inherited by the class one level up. There is no way to reach higher (for example, super.super.count is not allowed). Subclasses (or derived classes) are classes that are extensions of the current class. Whereas superclasses (parent classes or base classes) are classes that the current class is extended from, beginning with the original base class.

When using the super within new, super.new shall be the first statement executed in the constructor. This is because the superclass must be initialized before the current class and if the user code doesn’t provide an initialization, the compiler shall insert a call to super.new automatically.


<Previous     TOC      Next>


Home   |    About Us   |   Articles/ Tutorials   |   Downloads   |   Feedback   |   Links   |   eBooks   |   Privacy Policy
Copyright © 2005-2007 electroSofts.com.