SystemVerilog Part II
SystemVerilog Part II
Spring 2023/2024
SystemVerilog II
2
Classes (1)
Class is a user-defined data type
Classes must be declared in a module, package, interface, or a
program.
Class declaration contains data items (attributes/properties), and
subroutines (tasks and functions) operate on these data items
Class objects can be dynamically created and deleted during
simulation
Used in Object-Oriented (OO) programming for testbenches and
simulation models
module myModule
class myClass
bit[7:0] address;
bit[7:0] data;
endclass
…
3
Classes (2)
A variable of a class type is called a handle whose unintialized
value is null.
A class instance must be created for handle using a constructor.
Procedural call to function new
It allocates memory area to hold the instance
SystemVerilog has no destructor, i.e., automatic garbage
collection
module myModule
class myClass
bit[7:0] address;
bit[7:0] data;
endclass
4
Class Attributes and Methods
class myClass
int address;
int data;
...
endclass
5
External Method Declaration
It is used for better readability
Define the method prototype in the class prefixed by the
keyword extern
The prototype is the first line that identifies the method
type, name, and arguments
extern function int getData();
Then, the method is implemented outside the class
declaration, but in the same scope
7
Implicit (Default) Class Constructor
Method new is special class method called
constructor.
Defined by default for all classes.
When the object is created using the new
constructor, the object’s fields are
initialized to their default initial values
based on their data type
8
Explicit Class Constructor
You can explicitly define the constructor to
initialize the object’s fields
The function new will not have a return
type, even it is not allowed to have a void
as a return value
class myClass
int address;
function new()
address = 100;;
endfunction
...
endclass
class myClass
int address;
function new(input int a)
address = a;;
endfunction
...
endclass
10
Complete Class Example
11
this Keyword
The this keyword is used to unambiguously refer to
class properties, value parameters, local value
parameters, or methods of the current instance.
this keyword shall be used within non-static class
methods, constraints, etc.
class Demo;
integer x;
function new (integer x);
this.x = x;
endfunction
endclass
12
Outline
Classes
Static Properties and Methods
Aggregate Classes
Inheritance
Polymorphism
Randomization
13
Static Attributes
14
Static Attributes: Example
15
Static Methods
16
Static Methods Example
17
Outline
Classes
Static Properties and Methods
Aggregate Classes
Inheritance
Polymorphism
Randomization
18
Aggregate Classes
19
Aggregate Class Example
20
Outline
Classes
Static Properties and Methods
Aggregate Classes
Inheritance
Polymorphism
Randomization
21
Inheritance (1)
A class extends another class using the keyword extends.
Only single inheritance is allowed, i.e., each subclass has
only one parent
The subclass inherits all the members of the parent class
It can add more members
It can re-declare (override) parent members
Parent members are accessed as if they were members of
the subclass
The parent’s constructor is automatically called by the
subclass constructor
As the first line of the of the subclass’s constructor
22
Inheritance (2)
23
Inheritance (3)
Super keyword allows the subclass to access the parent
members
You can only pass arguments one level at a time.
super.super.new() is NOT allowed
24
Simple Inheritance Example
25
Inheritance with Constructors Example
26
Polymorphism (I)
Polymorphism allows the use of a variable of the
superclass (even if it is an abstract class) type to hold
subclass objects and to reference the methods of those
subclasses directly from the superclass variable
(handle).
packets[1].send(); shall invoke the send method
associated with the TokenPacket class
BasePacket packets[100];
packets[0] = ep;
packets[1] = tp;
27
Polymorphism (II)
28
Virtual Methods
A method of a class may be identified with the keyword
virtual
A virtual method shall override a method in all of its base classes
A non-virtual method shall only override a method in that class
and its descendants
Virtual method overrides in subclasses shall have the same
prototype of the function in the superclass including the
arguments names.
However, the return type of a virtual function shall be either a
matching type or a derived class type
29
Virtual Methods Example
30
Abstract Classes & Pure Virtual Methods (1)
A base class may be characterized as being abstract by
identifying it with the keyword virtual
We cannot construct objects directly from an abstract class
The abstract class constructor may only be called indirectly
through the chaining of constructor calls originating in an
extended non-abstract object.
A pure virtual is a method in an abstract class that is declared
as a prototype only without providing an implementation
The pure virtual method shall be indicated with the
keyword pure together without an implementation
An extended subclass may provide an implementation by
overriding the pure virtual method with a virtual method
having a method body.
31
Abstract Classes & Pure Virtual Methods (2)
Abstract classes may be extended to further abstract
classes
But, all pure virtual methods shall have overridden
implementations in order to be extended by a non-
abstract class
Any class may be extended into an abstract class, and
may provide additional or overridden pure virtual
methods.
32
Abstract Class Example
33
Outline
Classes
Static Properties and Methods
Aggregate Classes
Inheritance
Polymorphism
Randomization
34
Randomization
Class attributes can be defined as random using rand
and randc
rand: random with uniform distribution
randc : random-cyclic randomly iterates through all
values without repetition
– When an iteration is complete, a new random iteration
automatically starts
35
randomize() Function
randomize() function randomizes the object’s random
attributes.
Every class has a built-in randomize() virtual method.
You cannot re-declare this method
It returns 1 on success, 0 otherwise.
36
pre_randomize()and post_randomize()
randomize() automatically calls two “callback”
functions:
– pre_randomize() before randomization.
– post_randomize() after successful randomization.
If defined, these methods are automatically called on
randomization.
The pre/post_randomize declarations must match
the prototypes shown, i.e., they must be void functions
with no arguments.
37
pre_randomize()and post_randomize()Example
38
Randomization in Aggregate Classes
Randomize can operate hierarchically on aggregate
classes
– The class instance property must be declared as rand.
– Otherwise, that instance is skipped for randomization.
39
Randomization in Aggregate Classes: Example
40
In-Line Random Variable Control with randomize()
41
Controlling Randomization: rand_mode()
Every random attribute has an enable switch rand_mode
– Enabled by default (1)
– If disabled (0), the attribute will not be randomized
Mode can be written with task rand_mode and read with
function rand_mode
Called off a random property, the task changes the mode
of that property.
Called off an instance, the task changes the mode or all
random properties of the instance.
Only random attributes have rand_mode
– Calling method off a non-random attribute generates a compile
error
42
rand_mode()Example
43
Constraint Blocks
Constraints restrict the random data generation to exclude
values or change the probability distribution
44
Constraint Blocks Example
45
Constraint Block Inheritance
Constraint blocks are class members and are inherited
just like any other members
46
inside Operator
The inside operator is particularly useful in constraint
expressions.
– The operator can also be negated to generate a value outside of a set.
47
Weight Distributions
You can change distribution by defining weights for values using
the operator dist operator .
– Default value is 1
49
Conditional Constraints
Implication, using -> operator
if … else
50
Iterative Constraints
You can use a loop to apply separate constraints to each
array element
51
Constraint_mode()
Every constraint block has an enable switch called
constraint_mode.
– Enabled by default (1).
– If disabled (0), the constraint block will not be used.
53
Randomization Procedure and Its Effects
54