SystemVerilog OOP for UVM
Note taken from the course SystemVerilog OOP for UVM on Verification Academy
Good reads
Copying handles vs copying objects
Singleton with local qualifiers
Classes
SV & OOP
OOP enabling these concepts:
- Encapsulation
- Inheritance
- Data hiding
- Generic programming (template/parameterization)
- Polymorphism
Classes can be used to model
- Reusable verification environments
- Abstract data & methods that operate on them
SystemVerilog Class Terminology
Class objects are accessed through Class handles (not pointer, since handle are safer and such → handle are safe pointers)
Class basics
Class declaration does not allocate any storage, it only creates a new type
Don’t confuse SVA’s
propertywith class propertiesBoth methods and properties are members of the class
Classes are dynamically created objects (class instance)
Calling new() creates an instance and allocate memory
Object handles
A class variable only refer to a valid object → holding a handle referencing an object

Uninitialized variables have a special value null

Object destruction/de-allocation done automatically after an object has no references
Automatic memory management

Class methods
As with Verilog:
- Tasks can block and consume time
- Functions must be non-blocking and can return values
this
An implicit argument to a method that refers to the current object

Copying handles vs copying objects

After the shallow copies object, the
Pkt2_handPkt1_hhandles are pointing to distinct object, meaningPkt1_h.statusremains 5 after thePkt2_h.status = 10statement
Shallow copy example

Deep copy example
To do a “deep” copy, a custom copy method must be created

Static properties
Get created as soon as we declared the type
Available throughout the simulation
Access the static properties through <Class_name>::<Properties_name>

Static methods
Can be called without a specific object
Cannot access non-static members (no this object)

Inheritance and Polymorphism
Inheritance
- Creates new class type based on an existing class type

- Add new methods or add new behaviors to an existing method

Overriding the method caused the overrided method be hidden from the extended class
Can use the
super.to look back from the base class and access base class’s methods
Constructing Extended classes

Class variables with inheritance
- Base class variable can hold handles to extended objects, but not the reverse

Dynamic casting of class handles


Polymorphism
- Ability to have the same code act differently on different type
- A key requirement for any OOP languages
- Statically at compile time using parameterized type
- Dynamically during run-time using virtual class methods
Virtual class methods
- Chose the method by object type, not class variable

Override must have the same prototype
Once
virtual→ Alwaysvirtual
In the run task, the $display(p_h.status) will be either 5 or 6 since the method will check whether the object calling it is of type ErrorPkt or of type Packet, not requiring for the object in the argument run task to be explicitly of type ErrorPkt or Packet
virtualmethods behave without knowing if it’s dealing with base class object or the base class’s derivatives
Mixing virtual and non-virtual methods
- Clone is a construction with copy

copyis non-virtual here since we want to call the correctcopymethods according to each typeAnd the
copy's argument should be local class type, which allow direct access to local properties without casting
Accessibility and Abstract classes
By default, all class members are public
localquailifer restricts access to only within the declared classprotectedqualifier restricts access to only within the declared class and an extension of that class
Abstract class
- Can never be constructed directly
- Can only construct class extended from base class
- Provide common API for class methodology
- Can declare virtual method prototypes that must be overriden in extended class
Abstract class example
pure virtualtells the extended class must provide the implementation of the method

Extending Abstract class into Concrete

OOP Design pattern examples
Parameterized classes
Generic class to be instantiated as objects of different types or sizes
Uses module-like parameter passing

A generic class & actual parameter values is called a specialization

Parameterized classes with static properties
- Static properties do not get allocated until specialized
- Each specialization has unique set of static properties

SV create static properties of generic class as soon as there is any reference to a specialization of that class
No need construction of that class
Class scope operator
- Access static member properties and methods without instance
className#(parameter overrides)::memberName

The creations of specialization are handled at compile time
Singleton with local qualifiers
- Guarantees only one instance of a class

Construct on first use

Avoid static variable initialization ordering
my_rootme
Factory pattern

- Polymorphic construction
- Delegate construction to another object that can decide what kind of object to create
- Decouples instantiating class type from the actual type being constructed
Proxy class
- Stand-in for the full object it represents
- Defers creating the actual object until it is requested

Proxy class example

Using
$casthere since thefactory["C"].createObj()return aObjectclass’s instance, need to downcast it
Factory registration
- Use static variable initialization to register proxies

Example

Overrides here would replace the D object with C object upon requesting the factory