Day 101: Constrained randomization II: distributions and solve-before
Constrained randomization II
Uniform randomness wastes effort on boring cases. Distribution constraints (dist) weight values toward the interesting ones — corner addresses, boundary lengths, rare error injections. `solve...before` controls the order the solver assigns fields, shaping the resulting distribution. And implication (->) makes one field's constraint depend on another (e.g. if is_write, constrain data).
class txn;
rand bit [7:0] len;
rand bit err;
rand bit [1:0] kind;
// weight boundary and small lengths heavily
constraint c_len { len dist { 0:=5, 1:=20, [2:254]:=1, 255:=20 }; }
// decide kind first so len distribution isn't skewed by the solver
constraint c_ord { solve kind before len; }
// errors only on a particular kind
constraint c_imp { (kind == 2'b11) -> err == 1; }
endclassCorners are where bugs hide
Bugs cluster at boundaries — empty/full FIFOs, min/max lengths, address wraps. Uniform random spends almost all its samples in the boring middle. Weighting the distribution toward 0, 1, max−1, and max concentrates stimulus where failures live. Pair this with coverage (Day 102) to *confirm* you actually hit those corners.
Key terms
- dist
- A constraint assigning relative weights to values/ranges, shaping the random distribution.
- solve...before
- Controls the order the solver assigns fields, influencing the resulting distribution.
- Implication (->)
- A conditional constraint: if the antecedent holds, the consequent must too.
- Corner case
- A boundary or rare condition (empty/full, min/max) where bugs concentrate.
Before moving on, you should be able to
Why use a weighted dist constraint instead of plain uniform randomization?