VLSI DV Interview Puzzles · All levels

if-else in with Clause

What len values are possible and with what probabilities under tuple counting?

Puzzle

Difficulty: Easy · Puzzle 2 of 6 · Topic: randomize() with Puzzles

What len values are possible and with what probabilities under tuple counting?

Code

systemverilog
class pkt;
  rand bit is_write;
  rand bit [3:0] len;
  constraint c_base { len inside {[1:8]}; }
endclass

pkt p = new();
assert(p.randomize() with {
  is_write == 0;
  if (is_write) len inside {[1:2]};
  else          len inside {[7:8]};
});

Hint

Given is_write==0, only else branch constrains len.

Step-by-step solution

diagram
1) Inline fixes is_write to 0.
2) Therefore only else branch applies: len in {7,8}.
3) Base len 1..8 intersects to {7,8}.
4) Two legal tuples remain, so tuple-count gives 50/50 for len 7 and 8.

Answer

Answer: Only len=7 or 8 is legal, each at 50% under equal tuple weighting.

Why candidates get it wrong

Candidates sometimes enforce both if and else branches, which is incorrect.

Interviewer follow-up

If is_write were unconstrained, what is P(is_write==1) under tuple counting?

Related topics