RTL Studio Open IDE

3.1 · SYSTEMVERILOG CLASS

Downcasting with $cast (UVM Key 2)

Safely downcast class handles with $cast for UVM-style verification.

Watch on YouTube

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 $cast to 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

OperationDirectionImplicit?$cast Required?
UpcastChild → ParentYesNo
DowncastParent → ChildNoYes

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.

  1. In student/tb/tb.sv, locate the two if ($cast(__TODO__)) statements.
  2. Replace the first __TODO__ with ep2, p to downcast the parent handle p (which holds an ErrorPacket) to ep2.
  3. Replace the second __TODO__ with ep2, p to attempt downcasting p (now holding a plain Packet) to ep2.
  4. Run Simulation and verify the Log panel shows expected INFO: messages.
  5. 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

  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 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.