Constrained Randomization and Reproducibility
Generate varied protocol-correct transactions and reproduce a failure exactly.
Chargement…
Generate varied protocol-correct transactions and reproduce a failure exactly.
Constrained randomization explores a defined transaction space. Constraints should express the contract and verification-plan interactions alongside directed scenarios.
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;
}
endclassHere length is a byte count from 1 to 256. Addresses are four-byte aligned and transfers stay within a 4 KiB page. Active constraints are solved together; source order is not execution order.
constraint write_size {
write -> length inside {16, 32, 64};
}The implication restricts length when write is true. It adds no restriction when write is zero. solve write before length would change distribution without changing legal solutions.
dma_request req = new();
if (!req.randomize() with {
write == 1'b1;
length inside {16, 32, 64};
})
$fatal(1, "Request constraints have no solution");randomize() returns one on success and zero on failure. Ignoring that result can send stale data. with adds constraints rather than replacing class constraints.
The essential generation call stays outside an assertion so it still runs when assertions are disabled.
constraint length_distribution {
length dist {
1 := 5,
[2:63] := 1,
64 := 5,
256 := 5
};
}dist favors selected cases, such as boundaries. := assigns weight to every value in a range; :/ shares one total weight across the range:
constraint length_distribution {
length dist { 1 := 5, [2:63] :/ 20, 64 := 5 };
}The 62 values in [2:63] share a total weight of 20. Adjust distribution for observed coverage holes. randc cycles through a small domain before repeating; a full cycle over a wide vector is impractical.
req.address.rand_mode(0) freezes the address but leaves its constraints active. An incompatible frozen value fails randomization. req.legal_length.constraint_mode(0) disables the named block.
Separate rules for error injection so a test disables only the intended rule. Disabling one large block can permit several unintended errors.
The solver follows width and signedness rules. Extend sums before overflow, bound dynamic arrays, and prefer unsigned addresses and lengths. Pairwise constraints grow quadratically; a procedural algorithm may suit a large unique set better.
Record seed, test, configuration, parameters, failing transaction, code version, and simulator. Recreate that environment first, then reduce the scenario. A seed alone does not guarantee reproduction after changing tools or random-call order.
📝 Test your knowledge - Chapter quiz