Constraint Puzzles

SystemVerilog Interview Questions

Beginner Level

Difference between `rand` and `randc`?

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).

What is the `dist` keyword?

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

Explain `solve...before` constraint.

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.

Difference between `->` (Implication) and `if-else` in constraints?

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.

What is a `soft` constraint?

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

How to generate a unique list of values in an array without `unique` keyword?

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];
}
Are constraints bidirectional? Explain.

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.

How do you turn off a constraint?

Using `mode` argument in `constraint_mode()`.

pkt.c_len.constraint_mode(0); // Disable
pkt.randomize();