0
votes

I came across a constraint which was behaving weirdly (illustrated below) :

class A;
  rand bit[3:0] a,b,c,d;

  constraint c_abcd{
      2<a<b<c<d<20;
  }
endclass : A

I know that this is not the right way to specify that constraint and that we need to split it up into multiple constraints. But I expected it to give an error. Instead, it runs without any error and I could see different values for a,b,c,d being generated.

These numbers initially seem to be random, but I guess the system verilog solver behaves in a predictable manner to tackle these constraints.

So can you explain how this is solved? When I randomize with the above constraint, I get the following output :

a = 1101;
b = 0101;
c = 0111;
d = 1100;

Thank You

1
Which compiler are you using? Most likely the compiler is resolving your expression as ((((2<a)<b)<c)<d)<20. Obviously that would not be user's intention. - Puneet Goel
@PuneetGoel : I'm using 'irun'. When you say (2<a) <b , do you mean that if a is > 2 then (2<a) is '1'? and hence 1<b will be evaluated next? If that's the case, I've ran few more seeds and for one of those, I saw the value of 'd' being generated as '0' which should not happen right? Continuing with the above mentioned sequence, 'd' should be > either '0' or '1' and hence should never be '0' - user3547407

1 Answers

5
votes

It turns out the constraint expression you have written evaluates true for any values of a,b,c, and d. Which is equivalent to having no constraint at all.

Since your expression evaluates from left to right, it is the same as writing ((((2<a)<b)<c)<d)<20. The result of the outer most set of ()'s will be either 1'b0 or 1'b1 for any combination of values, and that result is always less than 20.