A Short Class On SystemVerilog Classes - Verification Horizons
A Short Class On SystemVerilog Classes - Verification Horizons
In this article
Class Types
Class Objects
Class Handles
Class Variables
Class Types, Objects, Handles, and Variables
Class Dismissed
It is often said that the English language is one of the most difficult languages to learn: inconsistent spelling rules;
the same words are used with different meanings in different contexts. “Why does a farmer produce produce?”
Working on the SystemVerilog IEEE 1800 standard, I understand that even more clearly now. The word class is
used many times in different contexts to mean slightly different things that the reader is expected to understand.
With brevity, but little more attention to clarity, I will explain some of these different contexts.
Class Types
When you declare a class, you are declaring set of members and a set of methods that operate on those
members.
bit member2;
endfunction
endclass
Consider this a user defined type, like a typedef. We are declaring the form and behavior of a type, but nothing is
allocated to store the values of this type.
Class Objects
A class object is a particular instance of a class type. Each instance is an allocation of the members defined by
the class type. The only way to create an object is to call the class constructor using the built-in new() method of a
class. We classify class objects as dynamic objects because executing a procedural statement is the only way to
create them.
Class Handles
Each time you call the new() method, it constructs a new class object and the method returns a class handle to
the class object. A handle is an indirect reference to a class object, like a pointer to an address in memory. Unlike
pointers in other languages such as C/C++, you are very limited in what you can do with a handle in
SystemVerilog.
Class Variables
A class variable is where you store the class handle that references a particular class object of a particular class
type. Declaring a class variable does not create a class object, only the space to hold the class handles. This
contrasts with other data types where the declaration of a variable creates an object of that type and gives you a
symbolic name to reference those objects. For example:
This creates and allocates the space for two MyStruct type objects and you can use StructVar1.member1 to
access one of its members. On the other hand:
This creates and allocates the space for two MyClass variables, but only allocates the space to hold handles to
MyClass objects, not the objects themselves. If you tried to access ClassVar1.member1 now, you would get a null
handle reference error because the initial value of a class variable is the special value null. One of the nice things
about handles instead of pointers is that they remove the possibility of corrupt object references that access
uninitialized or de-allocated memory.
ClassVar1 = new();
This calls the constructor of the MyClass type that returns a handle to a MyClass object and then stores that
handle in the MyClass variable, ClassVar1. You can now access ClassVar1.member1 because ClassVar1
references an actual object. If you then do:
ClassVar2 = ClassVar1;
Both class variables reference now the same class object – but there is still only one object of MyClass.
ClassVar1.member1 and ClassVar2.member1 refer to the same class member.
Class Dismissed
Just remember to add a few extra words when mentioning classes and it will make things much clearer to the
reader.
There are whole other classes of class thingy’s I could have explored, but I hope this class on classes will
motivate you to learn more of the subtle meanings behind the words.
This article first appeared on the Siemens Digital Industries Software blog at
https://blogs.sw.siemens.com/verificationhorizons/2013/07/16/class-on-classes/