Do you like the discussion forum ?

What are classes ?

class is a user-defined datatype, an OOP construct, that can be used to encapsulate data (property) and tasks/functions (methods) which operate on the data. Here's an example:

class myPacket;
  bit [2:0]  header;
  bit        encode;
  bit [2:0]  mode;
  bit [7:0]  data;
  bit        stop;
  function new (bit [2:0] header = 3'h'1, bit [2:0] mode = 5);
    this.header = header;
    this.encode = 0;
    this.mode   = mode;
    this.stop   = 1;
  function display ();
    $display ("Header = 0x%0h, Encode = %0b, Mode = 0x%0h, Stop = %0b", 
               this.header, this.encode, this.mode, this.stop);

There are a few key things to note in the example above:

  • function new () is called the constructor and is automatically called upon object creation.
  • this keyword is used to refer to the current class. Normally used within a class to refer to its own properties/methods.
  • display () is a function, and rightly so, because displaying values does not consume simulation time.
  • function new () has default values to the arguments, and hence line 6 (below) will create a packet object with values [3'h1, 0, 2'h3, 1]

How can I access signals within a class ?

To do that, you have to create an object of the class, which can be used as a handle to its properties and methods.

module tb_top;
  myPacket pkt0, pkt1;
  initial begin
    pkt0 = new (3'h2, 2'h3);
    pkt0.display ();
    pkt1 = new ();
    pkt1.display ();
Simulation Log

Header = 0x2, Encode = 0, Mode = 0x3, Stop = 1
Header = 0x1, Encode = 0, Mode = 0x5, Stop = 1

How do I create an array of classes ?

An array of classes can be created in a way similar to how you create an int type array.

module tb_top;
  myPacket pkt0 [3];
  initial begin
      for(int i = 0; i < $size (pkt0); i++) begin
            pkt0[i] = new ();
           pkt0[i].display ();

Since each myPacket object had no arguments in the constructor new(), default values are applied.

Simulation Log

Header = 0x1, Encode = 0, Mode = 0x5, Stop = 1
Header = 0x1, Encode = 0, Mode = 0x5, Stop = 1
Header = 0x1, Encode = 0, Mode = 0x5, Stop = 1

What is inheritance ?

Let's say you wanted to have a class with all the properties/methods of myPacket and be able to add more stuff in it without changing myPacket, the best way to do so is by inheritance. In the example below, networkPacket inherits the properties/methods of myPacket using the extend keyword. To call the functions of base class (myPacket), use super keyword.

class networkPkt extends myPacket;
  bit        parity;
  bit [1:0]  crc;
  function new ();
    super.new ();
    this.parity = 1;
    this.crc = 3;
  function display ();
    $display ("Parity = %0b, CRC = 0x%0h", this.parity, this.crc);

What is an abstract/virtual class ?

If you create an abstract class using the virtual keyword, then you cannot create an object of the class. This is useful if you don't want others to create an object of a class and use that in the testbench.

virtual class Base;
   bit [7:0]   data;
   bit         enable;

You may also like:

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.