Skip to content

SystemVerilog OOP for UVM

Note taken from the course SystemVerilog OOP for UVM on Verification Academy

Good reads

Object handles

Copying handles vs copying objects

Inheritance and Polymorphism

Parameterized classes

Singleton with local qualifiers

Factory pattern

Factory registration

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 property with class properties

Both 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

Untitled

Uninitialized variables have a special value null

Untitled

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

Automatic memory management

Untitled

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

Untitled

Copying handles vs copying objects

Untitled

After the shallow copies object, the Pkt2_h and Pkt1_h handles are pointing to distinct object, meaning Pkt1_h.status remains 5 after the Pkt2_h.status = 10 statement

Shallow copy example

Untitled

Deep copy example

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

Untitled

Static properties

Get created as soon as we declared the type

Available throughout the simulation

Access the static properties through <Class_name>::<Properties_name>

Untitled

Static methods

Can be called without a specific object

Cannot access non-static members (no this object)

Untitled

Inheritance and Polymorphism

Inheritance

  • Creates new class type based on an existing class type

Untitled

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

Untitled

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

Untitled

Class variables with inheritance

  • Base class variable can hold handles to extended objects, but not the reverse

Untitled

Dynamic casting of class handles

Untitled

Untitled

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

Untitled

  • Override must have the same prototype

    Once virtual → Always virtual

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

virtual methods 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

Untitled

copy is non-virtual here since we want to call the correct copy methods according to each type

And 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

  • local quailifer restricts access to only within the declared class
  • protected qualifier 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 virtual tells the extended class must provide the implementation of the method

Untitled

Extending Abstract class into Concrete

Untitled

OOP Design pattern examples

Parameterized classes

  • Generic class to be instantiated as objects of different types or sizes

    • Uses module-like parameter passing

      Untitled

  • A generic class & actual parameter values is called a specialization

Untitled

Parameterized classes with static properties

  • Static properties do not get allocated until specialized
  • Each specialization has unique set of static properties

Untitled

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

Untitled

The creations of specialization are handled at compile time

Singleton with local qualifiers

  • Guarantees only one instance of a class

Untitled

  • Construct on first use

    Untitled

  • Avoid static variable initialization ordering

    • my_root
    • me

Factory pattern

Untitled

  • 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

Untitled

Proxy class example

Untitled

Using $cast here since the factory["C"].createObj() return a Object class’s instance, need to downcast it

Factory registration

  • Use static variable initialization to register proxies

Untitled

Example

Untitled

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

DV Depot