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.
| Concept | Description |
|---|---|
| Class | Blueprint defining properties and methods. |
| Handle | Reference 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.
- Open
student/tb/tb.svin the editor. - In the
Transactionclass, replace__TODO__in thenew()function body with;— the constructor body is already correct. - In the
initialblock, replace__TODO__intrans = new(__TODO__)with();to instantiate the object. - 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
- Open the lesson in the IDE. The SystemVerilog for this topic is already in the editor.
- Lint with Slang before you simulate. It covers both Verilog and SystemVerilog in this IDE.
- Run Verilator and inspect the VCD. Interfaces, classes, and assertions count only if the simulator agrees.
- 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.