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
constraintblocks in aclass. - 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.
| Operator | Meaning | Example |
|---|---|---|
< | Less than | data < 100 |
<= | Less than or equal | len <= 15 |
== | Equal to | addr == 4'hA |
inside | Membership in a set/range | data inside {[10:20]} |
Constraints are evaluated at randomization time; if no solution exists, randomize() returns 0 and the testbench should handle failure.
Practice
- Open this chapter in RtlStudio.
Files appear automatically in Explore.
- Open
student/tb/tb.svfrom the Explore tree. - Locate the
constraint c_data { data < __TODO__; }line and replace__TODO__with100to enforcedata < 100. - Also fix the incorrect
initialblocks: setclk = 0andrst_n = 0initially, thenrst_n = 1after reset delay. - 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
- 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 constraint blocks. Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.