Constrained Randomization and Reproducibility
Generate varied protocol-correct transactions and reproduce a failure exactly.
Random, but valid
Constrained randomization generates combinations within a defined space. It does not replace the verification plan. It explores more interactions than a short list of directed tests.
class dma_request;
rand bit [31:0] address;
rand int unsigned length;
rand bit write;
constraint legal_length {
length inside {[1:256]};
}
constraint aligned_address {
address[1:0] == 2'b00;
}
constraint boundary {
address[11:0] + length <= 4096;
}
endclassThese constraints define a legal command: bounded length, aligned address, and a transfer that does not cross a 4 KiB page.
Always check the result
randomize() returns 1 when it finds a solution and 0 when constraints are inconsistent.
dma_request req = new();
assert (req.randomize() with {
write == 1'b1;
length inside {16, 32, 64};
}) else $fatal(1, "Request constraints have no solution");Ignoring the return value can send an old or default value to the driver. A randomization failure is a testbench error that needs diagnosis.
Distribution and interesting cases
dist gives extra weight to selected values without making them mandatory.
constraint length_distribution {
length dist {
1 := 5,
[2:63] := 1,
64 := 5,
256 := 5
};
}Boundary values have extra weight here. Distribution should follow the verification plan and observed coverage rather than an arbitrary choice.
randc cycles through a small domain before repeating values. It is not suitable for wide vectors whose complete cycle would be unrealistic.
Reproduce a bug
A pseudo-random generator is deterministic for a given seed as long as environment, call order, and tool remain comparable. Test logs should record:
- the global or local seed;
- test name and configuration;
- DUT parameters;
- failing transaction and sequence number;
- code and simulator version.
Rerun with the same seed first. Then reduce the scenario to a few transactions to speed up diagnosis.
Key points
- Constraints define the valid or deliberately invalid space to explore.
- Always check the return value of
randomize(). - Distributions target important cases and coverage holes.
randcfits small domains.- Record seed, configuration, and the failing transaction.
📝 Test your knowledge - Chapter quiz