RTL Studio Open IDE

2.4 · SYSTEMVERILOG CLASS

Class and Handle

Define classes and instantiate handles for object-oriented verification.

Watch on YouTube

What you practice

Class and Handle (2.4). Define classes and instantiate handles for object-oriented verification. It is in Classes & OOP Basics — Part 2 · Verification & OOP of the SystemVerilog Masterclass. Software-side verification: dynamic data structures, classes, and the first UVM master keys.

The same IDE is the SystemVerilog simulator. Slang lints the file, Verilator runs it, and the waveform viewer sits next to the source.

What you will learn

You will learn how to define a SystemVerilog class and instantiate it using the new() constructor, a foundational step in building reusable verification components.

Learning objectives

  • Define a class with data fields and methods.
  • Instantiate a class object using the new() constructor.
  • Call a class method to display its state.
  • Understand the distinction between a class (reference type) and a handle (pointer to the object).

Theory

In SystemVerilog, a *class* is a user-defined data type that encapsulates data (properties) and behavior (methods). A *handle* is a reference variable that points to an instance of the class. Memory for the object is allocated only when new() is called — before that, the handle is null.

ConceptDescription
ClassBlueprint defining properties and methods.
HandleReference variable (like a pointer).
new()Constructor that allocates memory and initializes the object.

Instantiation follows: ClassName handle = new();. The handle must be assigned before calling methods — otherwise, a null-pointer error occurs.

Practice

Open this chapter in RtlStudio. Files appear automatically in Explore.

  1. Open student/tb/tb.sv in the editor.
  2. In the Transaction class, replace __TODO__ in the new() function body with ; — the constructor body is already correct.
  3. In the initial block, replace __TODO__ in trans = new(__TODO__) with (); to instantiate the object.
  4. Run Simulation and check the Log panel for expected output.

Summary

You defined a class named Transaction with data fields and a method, then instantiated it using the new() constructor. You assigned the object reference to a handle and invoked its method to verify behavior. The key takeaway is that classes define reusable templates while handles provide access to individual object instances. This foundational OOP concept enables scalable and modular verification environments.

Run it in the Verilog IDE

  1. Open the lesson in the IDE. The SystemVerilog for this topic is already in the editor.
  2. Lint with Slang before you simulate. It covers both Verilog and SystemVerilog in this IDE.
  3. Run Verilator and inspect the VCD. Interfaces, classes, and assertions count only if the simulator agrees.
  4. Run Yosys when the lesson is synthesizable RTL, and keep testbench-only constructs out of that module.

The lesson is done when the simulation matches Class and Handle. Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.