Lesson 3: What Is Constrained Random Verification?

Constrained random verification generates test inputs randomly, but within rules that keep them legal and steer them toward the cases most likely to find bugs. The rules are constraints on the transaction class: ranges, weights, implications between fields, and extra constraints added for a single call. The constraint solver picks values that satisfy all of them, so a few lines describe thousands of tests. Because the distribution is up to the solver, you also measure what it actually produced.

What you'll learn

  • rand fields, constraint blocks and randomize()
  • dist weights, implications and inline constraints
  • Turning a constraint off with constraint_mode
  • Measuring the distribution, and what xsim actually produced
  • The randcase pattern for weights you can rely on

Conventions and assumptions

Every lesson tests the same small design: a registered 8-bit ALU (add, sub, and, or, xor) whose 9-bit result appears one clock after in_valid. All code was compiled and run in AMD Vivado xsim 2025.2, and the output shown is copied from those runs. The complete files are in the site's repository under scripts/verification.

The constraints

lesson3_tb.sv: the transaction
class alu_txn;
  rand bit [2:0] op;
  rand bit [7:0] a, b;

  constraint op_mix    { op dist { 0 := 30, 1 := 30, [2:4] :/ 40 }; }   // add and sub weighted up
  constraint corners_a { a dist { 0 := 10, 255 := 10, [1:254] :/ 80 }; }
  constraint corners_b { b dist { 0 := 10, 255 := 10, [1:254] :/ 80 }; }
  constraint no_wrap   { (op == 1) -> (a >= b); }                         // implication
endclass
How constrained random worksRandom fields and constraints go to the solver, which picks values satisfying every constraint, producing different legal stimulus on each call.Fieldsop, a, bRulesconstraintsSolverrandomize()Stimuluslegal, new
randomize() returns values that satisfy every constraint at once, or fails if none exist.
ConstraintKindMeaning
op_mixdistop is 0 or 1 with weight 30 each; 2 to 4 share 40
corners_a, corners_bdist0 and 255 get 10% each, the 254 values between share 80%
no_wrapimplicationif op is 1 (sub), then a ≥ b
randomize() with { ... }inlineextra constraints for this one call

Measure the result

The testbench randomizes 1,000 times and counts. A hard constraint is easy to check: with no_wrap on, there were 0 subtractions with b greater than a. The inline call asked for a wrapping subtraction with a below 10 and b above 200, and got op 1, a 3, b 252. The weights were another matter.

Methodaddsubandorxora = 0a = 255
Intended300300133133133100100
dist weights (xsim)8950916411512308
randcase first (xsim)31428115211913410094

In Vivado xsim 2025.2 the dist weights weren't honored for this class: subtraction came out far more often than intended and a = 0 never appeared. IEEE 1800 only promises dist weights when nothing else constrains the variable; with other constraints present, a tool is only required to satisfy the hard ones. The lesson is general: never assume a distribution, count it.

Weights you can rely on: randcase first

randcase picks a branch with the given weights using the simulator's random number generator, before the solver runs. Choosing the weighted values that way and passing them to randomize() with an inline constraint gave the intended mix in the same simulator.

lesson3_tb.sv: randcase, then randomize
      randcase
        30: want_op = 0;
        30: want_op = 1;
        40: want_op = 3'($urandom_range(2, 4));
      endcase
      randcase
        10: corner = 0;      // a = 0
        10: corner = 255;    // a = 255
        80: corner = -1;     // anything in between
      endcase
      if (!t.randomize() with { op == want_op; corner >= 0 -> a == corner; corner < 0 -> a inside {[1:254]}; })
        $fatal(1, "randomize failed");

Exercises

  • E1. Write a constraint that makes a and b both even.
  • E2. With no_wrap on, can the solver produce op = 1, a = 0, b = 5?
  • E3. A test needs 5% of transactions with a = b. Write it with randcase.

Solutions

  • E1. constraint even { a[0] == 0; b[0] == 0; }
  • E2. No. The implication requires a ≥ b whenever op is 1, so randomize() never returns that combination; asking for it inline would make randomize() fail.
  • E3. randcase 5: eq = 1; 95: eq = 0; endcase then randomize() with { eq -> a == b; }

Common mistakes

  • Ignoring randomize()'s return value: when constraints conflict it returns 0 and leaves the old values.
  • Assuming dist weights are exact. Count, especially when the field appears in other constraints.
  • Over-constraining until the stimulus is effectively directed, which defeats the point.
  • Forgetting that := gives each value the weight and :/ divides the weight across a range.

Check your understanding

  • Q1. What does op dist { 0 := 30, [2:4] :/ 40 } give each of 2, 3 and 4? Answer: a weight of 40/3 each, while 0 gets 30.
  • Q2. How do you turn off the no_wrap constraint for one test? Answer: t.no_wrap.constraint_mode(0), then constraint_mode(1) to restore it.
  • Q3. Why measure the distribution? Answer: the solver decides it, and as the xsim run shows, it may not match the weights you wrote.

The course

Related

Sources

Last verified

Run in Vivado xsim 2025.2 on 14 November 2026; outputs copied from that run. Language rules checked against IEEE 1800-2023.