1. OOP in SystemVerilog

OOP Terminology

  • Class: a basic building block containing routines and variables, it's analogue in Verilog is a module.

  • Object: an instance of the class. In Verilog, instanitate a module to use it.

  • Handle: a pointor to the object. In veriolog, it stands for the instance when refers to signal and methods from outstide the module.

  • Property: a variable that holds data, such as a reg or wire in Verilog.

  • Method: procedure code that manipulates variables, contained in tasks and functions. Verilog modules have tasks and functions plus initial and always blocks.

  • Prototype: the head of a routine that shows the name, type and arugment list. The body of the routine contains the exectuable code.

In Verilog you build complex designs by creating modules and instantiating them hierarchically. In OOP you create classes and instantiate them (creating objects) to create a similar hierarchy.

Following is an example of a class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Transaction;
bit [31:0]addr, crc, data[8]; // Property

function void display //Method
$display("Transaction:%h", addr);
endfunction: display

function void calc_crc;
crc = addr ^ data.xor;
endfunction:calc_crc

endclass:Transaction

Transaction tr; // Declare a handler, it's a null
Tr = new(); // Allocate a Transaction object

new allocate the space for the BusTran, initializes the variables to the default value, 0 for 2-state variables and X for 4-state ones, and returns the address where object is stored.
The new function not only allocates memory but also initialize the values.
We can also define the new function to set the values of variables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class BusTrain;
logic [31:0]addr, crc, data[8];
function new(logic [31:0]addr=3,d=5);
// does not have a type, as it always returns an objec of the same type
// as the class
// Sets addr and data to fixed values, but leaves crc as X.
// SV allocate space for the object automatically.
this.addr = addr;
foreach(data[i])
data[i] = d;
endfunction
endclass

class Driver;
BusTran bt;
function new(); // Driver's new function
bt = new(); // call the BusTran new function
endfunction
endclass

initial begin
BusTran b;
b = new(10); // addr is initialized to 10, but data is default 5.
end

new() vs new[]

new() can take arguments for setting object values, while new[] only takes single value for the array size.

Object deallocation

Garbage collection is the process of automatically freeing objects that are no longer referenced. SystemVerilog keeps tracking of the number of handles that point to it. When the last handle no longer references an object, SystemVerilog releases the memory for it.

1
2
3
4
5
BusTran b; // a handle
b = new; // calles new to construct an object, allocate the first object
// e.g., b points to the object
b = new; // Allocate the second one, free the first one
b = null' // Deallocate the second

Note the differences with C++:

  • SV handle can only point to objects of one type. C++ a typical untyped pointer is only an address in memory.
  • SV doesn’t allow any modification of a handle or using a handle of one type to refer to an object of another type.
  • Garbage collection in C/C++ is manual, suffering from “memory leaks” when forgets deallocating objects.