Constraint Interview Question

Write a constraint where you have a 32 bit value bit [31:0] val where you’d want to randomize this to where every randomization would only allow 2 bits to differ from the previous randomization.

Can we solve it by using

$countones(prev_val^cur_val) == 2)

This is so because if only two bits are different in the previous and current value, their XOR should have only two bits set. Any other logic is welcome.

You should try it. Asking better questions on the Verification Academy Forums with EDAPlayground - Verification Horizons

This seems to work for me. Any other solutions/suggestions are welcome.

class abc;
 
  static bit [31:0] prev_val;
  rand bit [31:0] cur_val;
  
 static int count = 0;
 
constraint sel{
  
  (count > 0) -> ($countones((prev_val^cur_val))==2);   
    
  }
  
function void pre_randomize();
  if(count>0) begin
    prev_val = cur_val;
  end
    count+=1;
    
endfunction
    
endclass
    
   

module tb;
  initial begin
  abc xx1 = new();
    repeat(10) begin
    xx1.randomize();
      $display("prev_val = %b",xx1.prev_val);
      $display("cur_val =  %b",xx1.cur_val);
      $display("count =  %d  diff = %d",xx1.count,$countones(xx1.cur_val^xx1.prev_val));
    end
    
  end
endmodule

No need for static variables or a counter.

This also works

class abc;
  rand bit [31:0] cur_val;
  constraint sel{
    $countones( const'(cur_val)^cur_val)==2;
  }
endclass

Could you please explain the need for the “const`” in this solution. Where exactly are you comparing the previous randomized value with the current one?

My apologies, I’m more than a little rusty at this, but does the constraint solver efficiently handle the ‘flip two bits’ constraint via $countones? (A long time ago) I would have generated two values between 0 and 31 and inverted those bits.

That might be fine if there were no other constraints on the random value. But what if there was another constraint like cur_val % 5 == 0 in addition to the number of changed bits? Generating the equations for that would be much less efficient.