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
orwire
in Verilog. -
Method: procedure code that manipulates variables, contained in tasks and functions. Verilog modules have
tasks
andfunctions
plusinitial
andalways
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 | class Transaction; |
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 | class BusTrain; |
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 | BusTran b; // a handle |
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.