Assertion error, why?

Hi all,

code as:

rand bit [2:0] aa;
rand bit [2:0] bb;

constraint caa { aa dist {2:=50, 4:=50};}
constraint cbb { bb dist {2:=50, 4:=50};}

……
 assert(randomize(aa));
 assert(randomize(bb));

Assertion error is issued by both assert lines, why ?

Because all active constraints must be met when calling randomize(), regardless of which variables are being randomized. When you call randomize(aa) while bb is still uninitialized to 3’b0, the randomization fails because constraint cbb cannot be met. aa is left in its original state. Same thing happens again for randomize(bb). When you call randomize(null), no variables are randomized, but all active constraints are checked.

When randomizing select variables in a class, it is the user’s responsibility to either disable unwanted constraints with constraint_mode(0), or put the non-random variables unto a state that satisfies the active constraints.

It is also possible to use local scope std::randomize(aa) with { some constraints} . This randomizes the variables in the list as local variables, and ignores the class constraints. See LRM 18.12.

In reply to dave_59:

Dave, would this work then?

rand bit [2:0] aa=2;
rand bit [2:0] bb=2;

constraint caa { aa dist {2:=50, 4:=50};}
constraint cbb { bb dist {2:=50, 4:=50};}

In reply to ben@SystemVerilog.us:

Yes, that puts the non-random variables into a state that meets the constraints.

In reply to dave_59:

Thanks Dave, it works!

It is explicitly demonstrated in LRM that "randomize(null)" will check all constraints in the class, but I don't find an explicit demonstrtion that "randomize(xx)" will also check all constraints, maybe I have missed somthing in LRM. It is easily misunderstood that "randomize(xx)" will only check constraints specified with "xx".

In reply to Wanglj:

Section 18.11 In-line random variable control says “When randomize is called with arguments, those arguments designate the complete set of random variables within that object; all other variables in the object are considered state variables.”

State variables are non-random variables, and are certainly used in constraints. For example { x < max; } where x is a random variable and max is a non-random variable.