SystemVerilog Datatype: Class (Part 1)
A new entrant in SystemVerilog, the class datatype brings in
the essence of object oriented programming to the language. However,
as you will see in this article, you do not need to know object oriented
programming methodology to appreciate -- or even use -- the power of
So, What's in a Class ?
A class is a collection of two types of things: some data and a set of
subroutines (i.e. Verilog tasks and functions) that operate on that data.
Each data part of a class is known as a class property (or, simply
property) and a subroutine defined in a class is called a class
method (or, just method).
Here, the important thing to remember is that a method has access
to the properties of the same class only. Barring few exceptions, a class
method can not operate on properties outside the scope of the same class.
Let us see an example of a class. The following example is from a
hardware model of a graphics engine. A graphics engine manages a
display by drawing and re-drawing various parts of the display. In order
to draw or paint a segment of the display, the engine divides it into
multiple triangular spaces and then paints the area of each triangle
with a given color.
Before we build this model, we notice that each triangle is an
independent entity that needs to be managed separately. However,
once the triangles are defined (in terms of the co-ordinates of
their vertices), the same set of functions (paint a triangle with
a given color, keep track of when this triangle was last updated
and so on) apply to all triangles.
This creates an opportunity for us to represent each triangle as a
We create a class that defines and operates on a triangular space
of a display screen. This class Triangle has four properties
and three methods - seven members in all.
The first property lastFillTime is a variable of type time that
denotes the time when a traingular space was last filled up with some color.
The triangular space itself is defined by the three vertices contained in
the variables p1, p2 and p3 of union type point
defined earlier. Needless to say, you can have as
many properties in a class and of as many varied types as you want.
The methods that work on these properties are defined below that. The
function new() creates a new triangle, the task fillIt()
fills the triangle with a specified color (passed as an argument) and,
lastly, the function isFilled() returns a boolean variable that indicates
if the triangle is filled.
// class properties
point p1, p2, p3;
// class methods
... // all initialization goes here
// fill the triangle with the color defined by
// the rgb structure
task fillIt (struct rgb color);
... // details on how to fill
// is the triangle filled?
function bit isFilled();
... // details here
We skip the details of the internal structure of the subroutines as
they are not relevant for our discussion. However, the rest of the
definition will be good enough for us to understand various interesting
aspects of a class - starting with the notion of object instance and
Next: Object instance and