Classes: SV For Verification (Systemverilog LRM)
Classes: SV For Verification (Systemverilog LRM)
Packet p = new;
p.command = INIT;
p.address = $random;
packet_time = p.time_requested;
Net types (supply0, supply1, tri, triand, trior, etc.) -> not compatible with
dynamically allocated data
CANNOT be declared as a class property!!!
OBJECT METHODS
Object methods can be used by qualifying class property names with
an instance name.
Packet p = new;
Status = p.current_status();
CONSTRUCTORS
• SV does not require complex memory allocation and deallocation as
in C++.
Example:
In case where all instances of a class need access to a common file descriptor:
class Packet;
static integer fileId = $fopen(“data”, “r”);
Now fileId will be created and initialized once. Thereafter, every Packet object access file
descriptor in the usual way.
Static class properties can be used without creating an object of that type
STATIC METHODS
• A static method can be called outside the class, even with no class
instantiation.
class TwoTasks;
static task foo(); ……… endtask Static class method with automatic variable lifetime
task static bar(); ……… endtask Non-static class method with static variable lifetime
endclass
class Demo;
integer x;
x is a property of the class and an argument to function
function new(integer x); new
this.x = x;
endfunction
endclass
ASSIGNMENT, RENAMING & COPYING
Packet p1;
creates a variable p1, that can hold the handle of an object of class
Packet, but initial value of p1 is null.
The object does not exist, and p1 does not contain actual handle, until
an instance of type Packet is created using
p1 = new
ASSIGNMENT, RENAMING & COPYING
Packet p1;
Packet p2;
p1 = new; new executed only once. So, only one object has been created.
p2 = p1; Still there is only 1 object, which can be referred to with either the name p1 or p2
Packet p1;
Packet p2;
p1 = new;
p2 = new p1; Object p2 copies ALL class properties of object p1 -> SHALLOW COPY
class B;
integer i = 1;
A a = new;
endclass
Custom method written to copy the object specified as its argument into its instance
DEEP COPY of p1
INHERITANCE & SUBCLASSES
Packet class can be extended so that the packets can be chained
together into a list.
class LinkedPacket;
Packet packet_c;
LinkedPacket next;
LinkedPacket lp = new;
Packet p = lp;
• The first action new() takes, before any code defined in the
function is evaluated is to invoke the new() method of its superclass,
and so on up the inheritance hierarchy.
function new();
super.new(5);
endfunction
class Packet;
local integer i;
function integer compare(Packet other);
compare = (this.i == other.i);
endfunction
endclass
other.i should not be visible inside this packet since it is local class property
being referenced from outside this instance. However, this is ALLOWED within the
same class.
CONSTANT CLASS PROPERTIES
• Class properties can be made read-only by a const declaration.
• Since class objects are dynamic objects, class properties allow 2 forms
of read-only variables:
Global constants
Instance constants
CONSTANT CLASS PROPERTIES
Global constant class properties Instance constant class properties
- Include an initial value as part of their - Do not include an initial value in their declaration,
declaration. only the const qualifier.
- Cannot be assigned a value anywhere other - Can be assigned a value at run-time, but the
than in the declaration. assignment can only be done once in the
corresponding class constructor.
- Typically, global constants are also declared static, - Cannot be declared static since that would
since they are constant for all instances of the class. disallow all assignments in the constructor.
ABSTRACT CLASSES & VIRTUAL METHODS
• A set of classes can be created that can be viewed as all being derived from
a common base class.
• Example:
A common base class of type BasePacket sets out structure of
packets but is incomplete and would never be instantiated.
From this base class, though number of useful subclasses could be derived,
such as Ethernet packets, token-ring packets, GPSS packets, etc.
Each of these packets might look very similar, all needing the same set of
methods, but they could vary significantly in terms of their internal details.
ABSTRACT CLASSES & VIRTUAL METHODS
• A base class sets out the prototype for the subclasses.
• Virtual method overrides a method in all the base classes, whereas normal method only
overrides a method in that class and its descendants.
• There is only implementation of a virtual method per class hierarchy, and it is always the
one in the latest derived class.
• When subclasses override the virtual methods, they MUST follow the prototype exactly.
ABSTRACT CLASSES & VIRTUAL METHODS
virtual class BasePacket;
virtual function integer send(bit[31:0] data);
endfunction - If an abstract class has any virtual
endclass methods, all of the methods MUST
be overridden ( and provided with a
method body) for the subclass to
class EtherPacket extends BasePacket;
be instantiated.
function integer send(bit[31:0] data);
// body of the function - If any virtual methods have no
endfunction implementation, the subclass needs
to be abstract.
endclass
Can be instantiated
ABSTRACT CLASSES & VIRTUAL METHODS
• An abstract class can contain methods for which there is only a
prototype and no implementation.
• Example:
Packet -> base class
BasePacket -> object of Packet. Defines all public methods
that are used by its subclasses(send, receive, print, etc.) as virtual.
POLYMORPHISM: DYNAMIC METHOD LOOKUP
Even though BasePacket is abstract, it can be used to declare a variable:
BasePacket packets[100];
Now instances of various packet objects can be created, and put into the array:
EtherPacket ep = new;
TokenPacket tp = new;
GPSSPacket gp= new;
packets[0] = ep;
packets[1] = tp;
packets[2] = gp;
POLYMORPHISM: DYNAMIC METHOD LOOKUP
Since methods are declared as virtual, appropriate subclass methods
can be accessed from superclass variable, even though compiler didn’t
know – at compile time – what was going to be loaded into it.
e.g. packets[1].send();
shall invoke send method associated with TokenPacket class.
• Syntax:
class_identifier :: {class_identifier::} identifier
Base b = new;
int bin = 123;
b.print(Base::bin, bin);
Base::print(Base::hex, 66);
CLASS SCOPE RESOLUTION OPERATOR ::
• In SV, class scope resolution operator applies to all static elements of
a class:
- static methods
- typedef
- enumerations
- structures
- unions
- nested class declarations
CLASS SCOPE RESOLUTION OPERATOR ::
class StringList; class StringTree;
class Node; class Node;
string name; string name;
Node link; Node left, right;
endclass endclass
endclass endclass
Any type can supplied as parameter, including a user-defined type such as class or struct.
TYPEDEF CLASS
CLASS & STRUCTURES
MEMORY MANAGEMENT