Constraint Puzzles
Puzzle: Unique Array without 'unique'?
+Q: Generate an array of size 10 with unique values between 0 and 100, WITHOUT using the `unique` constraint.
rand bit [7:0] arr[10];
constraint c_unique_manual {
foreach (arr[i])
foreach (arr[j])
if (i != j) arr[i] != arr[j];
}
Note: This is O(N^2) complexity, acceptable for small arrays only.
When to use `solve ... before`?
+Used to change the probability distribution, NOT the solution space.
If `A` implies `B` (e.g., `A -> B==0`), randomizing them together might make `A=1` extremely rare. Using `solve A before B` forces the solver to pick A first, then pick B consistent with A. This makes `A=1` happen with 50% probability (if A is 1-bit).
Soft Constraints?
+Constraints declared with `soft` can be overridden by inline constraints (`with {}`) or extended class constraints without causing a conflict failure. If a conflict occurs, the soft constraint is discarded.
constraint c_len { soft len == 10; } // Can be overridden to 20 later
Puzzle: Weighted Distribution without 'dist'?
+Q: Generate a bit 'b' that is 1 (70% prob) and 0 (30% prob) without using `dist`.
rand int temp;
rand bit b;
constraint c_dist {
temp inside {[0:99]};
b == (temp < 70);
}
Are Constraints Bi-directional?
+Yes. `if (mode == 1) len == 10;` implies that if `len != 10`, `mode` CANNOT be 1.
The solver considers all active constraints simultaneously. It does not execute line-by-line like procedural code.
How to disable a specific constraint?
+Use the `constraint_mode()` method on the handle.
pkt.c_len.constraint_mode(0); // Disable only 'c_len'
pkt.constraint_mode(0); // Disable ALL constraints