0% found this document useful (0 votes)
14 views

SystemVerilog Part II

Uploaded by

Laith Qasem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

SystemVerilog Part II

Uploaded by

Laith Qasem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

ENCS5337: Chip Design Verification

Spring 2023/2024

SystemVerilog II

Dr. Ayman Hroub


Outline
 Classes
 Static Properties and Methods
 Aggregate Classes
 Inheritance
 Polymorphism
 Randomization

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

myClass obj1 = new;


4
Class Attributes and Methods
class myClass
int address;
int data;

task setData (input int newData)


data = newData
endtask

function int getData()


return data;
endfunction

...
endclass

myClass obj1 = new;


initial begin
obj1.data = 10; // or obj1.setData(10)
end

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

function int myClass::getData();


return data;
endfunction
6
External Method Example

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

myClass obj1 = new; //obj1.data is 100


9
Explicit Constructor with Arguments

class myClass
int address;
function new(input int a)
address = a;;
endfunction
...
endclass

myClass obj1 = new (200); //obj1.data is initialized to 200

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

 The class attributes are dynamic by default,


i.e., each class instance has its own copy of
the attributes
 Regarding static attributes, one copy of the
attribute is shared among all class objects
 They are allocated in memory at elaboration
 They can be accessed using null handles

14
Static Attributes: Example

15
Static Methods

 Can only access static attributes or other


static methods
 Besides any class handle, they can be
called from the class name using the
resolution operator ::

16
Static Methods Example

17
Outline
 Classes
 Static Properties and Methods
 Aggregate Classes
 Inheritance
 Polymorphism
 Randomization

18
Aggregate Classes

 A class attribute can be an instance of


another class
 The constructors of the class attributes
must be called explicitly.
 Instance handles must be chained to
reach into hierarchy

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];

EtherPacket ep = new; // extends BasePacket


TokenPacket tp = new; // extends BasePacket

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()

 Specific class variables can be randomized by passing


them as arguments.
– This allows nonrandom (state) properties to be randomized (not
cyclic)

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

 Constraints can be embedded in classes using constraint


blocks

 You declare a constraint block as a class member with the


constraint keyword, followed by an identifier, followed
by a list of constraint items enclosed within curly braces {}

 A block can contain any number of any form of constraints

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

 := operator assigns weight to the item or every value in the range

 :/ operator divides the weight by the number of values in the


range, i.e., for a range of n values and a weight of w, the weight of
each individual value is w/n.

 Fractional weights are possible

 Negative weights are not possible


48
Weight Distributions Examples

49
Conditional Constraints
 Implication, using -> operator
 if … else

50
Iterative Constraints
 You can use a loop to apply separate constraints to each
array element

 Can affect performance for large arrays or complex


constraints.

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.

 Mode can be written with task constraint_mode.

 Mode can be read with function constraint_mode.

 Only constraint blocks have constraint_mode.


52
Constraint_mode() Example

53
Randomization Procedure and Its Effects

 Randomization proceeds as follows:


– All randc properties randomized simultaneously.
– Then all rand properties randomized simultaneously.
– Then constraints are checked.
– Cycle iterates until a solution is found or the random space is
exhausted.
▪ You can only order rand variables of integral types.

54

You might also like