RTL Studio Open IDE

3.6 · SYSTEMVERILOG CLASS

constraint blocks

Limit random value generation to valid protocol ranges with constraint blocks.

Watch on YouTube

What you practice

constraint blocks (3.6). Limit random value generation to valid protocol ranges with constraint blocks. It is in Constraint Random Verification — 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 use SystemVerilog constraint blocks to restrict random value generation to valid protocol ranges, ensuring only legal values are produced during testbench stimulus.

Learning objectives

  • Understand how to declare and use constraint blocks in a class.
  • Write constraint expressions to bound random variables within valid ranges.
  • Verify that generated random values obey the constraints during simulation.
  • Recognize the role of constraints in protocol-compliant verification.

Theory

Constraints in SystemVerilog allow you to specify rules that random variables must satisfy during randomize(). They are declared inside a class using the constraint keyword and braces. Constraints are essential for generating only valid protocol packets, avoiding illegal states, and improving testbench efficiency.

OperatorMeaningExample
<Less thandata < 100
<=Less than or equallen <= 15
==Equal toaddr == 4'hA
insideMembership in a set/rangedata inside {[10:20]}

Constraints are evaluated at randomization time; if no solution exists, randomize() returns 0 and the testbench should handle failure.

Practice

  1. Open this chapter in RtlStudio.

Files appear automatically in Explore.

  1. Open student/tb/tb.sv from the Explore tree.
  2. Locate the constraint c_data { data < __TODO__; } line and replace __TODO__ with 100 to enforce data < 100.
  3. Also fix the incorrect initial blocks: set clk = 0 and rst_n = 0 initially, then rst_n = 1 after reset delay.
  4. Run Simulation from the sidebar, then check the Log panel for expected output.

Summary

You practiced defining constraint blocks to restrict random value generation within valid protocol ranges. By using the constraint keyword alongside rand fields, you ensured generated values adhered to specified limits. This technique is essential for generating realistic test stimuli in verification environments. Constraint blocks provide a declarative way to enforce design-specific rules during randomization.

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 constraint blocks. Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.