[Edit of Image1]
Hey it's a me again @drifter1!
Today we continue with the Logic Design series on SystemVerilog in order to start getting into Classes. The topic will be split into multiple parts!
So, without further ado, let's dive straight into it!
SystemVerilog can't be OOP without having objects. A new user-defined datatype class is thus introduced, which allows objects to be dynamically created, deleted, assigned and accessed like in C++. Such classes group together data (properties) and tasks/functions (methods) that operate on the data. These properties and methods are commonly referred to as members of the class.
A class can be defined in any module or program (that we haven't discussed yet though), and is enclosed within the class and endclass keywords, as shown below.
class className;
// properties
// methods
endclass
Objects of the class are created using the new() function, which is called a constructor. There can only be one constructor per class, the constructor has to be a function and not a task, and it doesn't return anything. It's common to initialize the data to default values in such a function, as shown below:
function new (property1 = default1, property2 = default2, ...);
this.property1 = property1;
this.property2 = property2;
...
endfunction
The keyword this refers to the properties/methods within the class.
The quickest way of creating an object of a class is the following:
className instanceName;
instanceName = new();
The individual parameters of the new() function can of course be overridden, otherwise the defaults will be used instead.
When the new() function is not called, the object is undefined or of type null, which can be used in conditional statements in order to avoid a null pointer exceptions/dereferences.
Two objects can also point to the same instance, as shown below.
className instanceName2;
instanceName2 = instanceName;
With that the 2nd instance points to the contents of the 1st instance.
The properties and methods of an object (instance) of the class are accessed using the dot (.) operator.
instanceName.property1
instanceName.method1()
In order share properties easily across different instances, SystemVerilog provides the static keyword. This keyword has to be added in front of any property that we wish to share. That way, any change will be reflected to the other instances.
Making properties read-only (constants) is also easy, and is done by adding the const keyword in the declaration. If the constant's initial value is added as part of the declaration, the constant is global and cannot be assigned to a different value anymore. But, it's possible to not include any initial value in the declaration. That way, the constant's value can be assigned in the corresponding class constructor function, becoming a sort of instance-dependent constant.
Arrays of any class are created similar to any other included data type (int, string etc.).
For example, a simple, static, one-dimensional array can be defined as:
className arrayName [N];
Block diagrams and other visualizations were made using draw.io
And this is actually it for today's post!
Next time we will continue on with more on Classes...
See Ya!

Keep on drifting!