Tutorials

Learn More

The Object-Oriented Programming (OOP) concept is based on an object which is nothing but a container for various data types, functions, tasks, etc. It provides data abstraction. Classes allow objects to create and delete dynamically. It also provides a mechanism like a handle or an object pointer that is used to access the object or assign it to some other handle.

Class properties include data members of the different data types.

Class methods include functions and tasks present inside classes.

A basic class syntax:

class <class_name>;
  <data members>  // class properties

  // class methods
  function <function_name>;
  . . .
  endfunction

  task<task_name>;
  . . .
  endtask
endclass

A class object or instance, an object handle

A class defines a data type and an object is an instance of that particular class.

An object handle is nothing but a pointer for an object.

class transaction;
  // class properties and methods
endclass

transaction tr; // variable of class data_type transaction or class handle
tr = new(); // memory is allotted for a variable or object.

If an object is not created, then the default value for the class handle is null.

Constructor

The new function used in the above example is called a class constructor. SystemVerilog class has a built-in new method.

Default values for

  1. 2 state variables – 0
  2. 4 state variables – X


User-defined new function can be written.

function new();
  // code specific
endfunction


function void new();  // This is invalid as void return type is used
  // code specific
endfunction

Though the new function does not have any return type, it can be assigned to the left-hand side class handle.

transaction tr = new(); // This is possible. Declaration and object creation in a single line.

Example for constructor with passing argument

An argument can be passed to the constructor.

function new(bit [31:0] m_data);
  data = m_data;
endfunction

transaction tr = new(10);
class transaction;
  bit [31:0] data;
  int id;
  
  function new (bit [31:0] m_data);
    $display("Inside constructor");
    data = m_data;
  endfunction
endclass

module class_example;
  initial begin
    transaction tr = new(10);
    $display("Value of data = %0h", tr.data);
  end
endmodule

Output:

Inside constructor
Value of data = a

Accessing class methods

Till now, we have seen how to access class properties. Similarly, class methods (tasks and functions) can be accessed using a class handle.

In the below example, update (task) and print (function) methods are accessed by handle tr.

class transaction;
  bit [31:0] data;
  int id;
  
  task update(bit [31:0] m_data, int m_id);
    data = m_data;
    id = m_id;
  endtask
  
  function print(transaction tr);
    $display("Value of data = %0h and id = %0h", tr.data, tr.id);
  endfunction
endclass

module class_example;
  initial begin
    transaction tr = new();
    tr.update(5, 9);
    tr.print(tr);
  end
endmodule

Output:

Value of data = 5 and id = 9

System Verilog Tutorials