What you practice
Downcasting with $cast (UVM Key 2) (3.1). Safely downcast class handles with $cast for UVM-style verification. It is in Downcasting with $cast — Part 3 · Advanced SV & Mini-UVM of the SystemVerilog Masterclass. Timing, concurrency, constrained-random verification, assertions, and a pure-SV Mini-UVM environment.
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
Today you will learn how to safely convert a generic parent-class handle back into a specific child-class handle using SystemVerilog’s $cast operator, a critical technique for UVM-based verification.
Learning objectives
- Understand the difference between upcasting and downcasting in class hierarchies.
- Use
$castto perform safe dynamic type checking and conversion. - Recognize when a downcast succeeds or fails based on the actual object type.
- Access child-specific fields only after successful downcasting.
Theory
In SystemVerilog, assigning a child-class object to a parent-class handle is called *upcasting* and happens implicitly. However, converting a parent handle back to a child handle requires *downcasting*, which must be done explicitly and safely.
The $cast system function performs runtime type checking:
- It returns
1(true) if the source object is compatible with the target type. - It returns
0(false) otherwise. - It assigns the result to the target variable only on success.
Syntax: if ($cast(target_var, source_handle)) begin ... end
| Operation | Direction | Implicit? | $cast Required? |
|---|---|---|---|
| Upcast | Child → Parent | Yes | No |
| Downcast | Parent → Child | No | Yes |
Never assume a parent handle points to a child object — always validate with $cast before accessing child-specific members.
Practice
Open this chapter in RtlStudio. The testbench student/tb/tb.sv already defines the Packet and ErrorPacket classes and a basic test flow.
- In
student/tb/tb.sv, locate the twoif ($cast(__TODO__))statements. - Replace the first
__TODO__withep2, pto downcast the parent handlep(which holds anErrorPacket) toep2. - Replace the second
__TODO__withep2, pto attempt downcastingp(now holding a plainPacket) toep2. - Run Simulation and verify the Log panel shows expected
INFO:messages. - Observe that the first downcast succeeds, while the second fails — confirming
$cast’s runtime safety check.
The DUT (dut) is a simple register and does not affect the class casting logic — it is included only to satisfy the required TB/DUT structure.
Summary
You practiced downcasting using $cast to safely convert a parent class handle back to a child class handle in UVM. By assigning an ErrorPacket object to a Packet handle and then using $cast, you accessed child-specific fields without runtime errors. The key takeaway is that $cast enables safe, explicit type conversion when working with polymorphic handles in testbenches. This technique is essential for accessing extended functionality while maintaining type safety in UVM-based 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 Downcasting with $cast (UVM Key 2). Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.