Constraint Puzzles
SystemVerilog Interview Questions
Beginner Level
rand: Standard random variable. Each randomization is independent (replacement allowed).
randc: Random Cyclic. It cycles through all possible values before repeating any value (no replacement until cycle completes).
Used for weighted distribution. `constraint c { val dist { 0:=10, [1:3]:=60 }; }`
- `:=`: The weight is assigned to each item in the range.
- `:/`: The weight is split equally among items in the range.
Intermediate Level
It forces the solver to choose the order of randomization. By default, all variables are solved simultaneously. `solve a before b` forces `a` to be determined first, which changes the probability distribution of `b` if `b` depends on `a`. It does not change the solution space, only the probability.
They are logically equivalent in constraints.
`a -> b` means "If A is true, B must be true". If A is false, B can be anything.
`if (a) b;` works the same way.
A constraint that can be overridden by a hard constraint without causing a failure. Useful for setting default behavior in base classes that can be modified in test cases. `constraint c { soft len == 10; }`
Advanced Level
Use a `foreach` loop to compare every element with every other element.
constraint c_unique {
foreach(arr[i])
foreach(arr[j])
if(i != j) arr[i] != arr[j];
}Yes. constraints resolve all variables simultaneously.
Example: `a == b + c;`
If `a` is constrained to 10 and `b` to 5, the solver forces `c` to be 5. The data flow isn't just RHS to LHS; it's a simultaneous equation solution.
Using `mode` argument in `constraint_mode()`.
pkt.c_len.constraint_mode(0); // Disable pkt.randomize();