I have a query related to generating constraints :
I have a complex scenario where each of the parameters need to be randomized and need to satisfy an arithmetic equation like below
rand bit[3:0] max_val;
rand bit[7:0] bin[8];
rand bit[1:0] if_yes;
constraint constraint1 {
(32 * max_val + ‘d480) > ( ‘d96 + bin[0] + bin[1] + bin[2] + bin[3] + bin[4] + bin[5] + bin[6] + bin[7] + ((if_yes == 1)? 5 : ((if_yes ==2)? 2: (if_yes == 3)? 4:0)));
}
Now after randomization the randomized parmeters donot satisfy the above equation. I am expecting that the constraint is still passing because of bit width mismatch between LHS and RHS. How to modify the above constraints in such a scenario?
Would be grateful to receive any help or feedback regarding the same
Thanks
Soumya
*In reply to soumyarayaprolu:*Can you explain what you mean by a “bit width mismatch between LHS and RHS”? In a relational operator, sizes are extended to match. If you were expecting truncation, you need an explicit size cast.
Also, you are using un-sized decimal literals. You should be using explicit sizes.
In reply to soumyarayaprolu:
Constraints are working just fine. Can you give the scenario for which you are getting unexpected results?
module test;
class A;
rand bit[3:0] max_val;
rand bit[7:0] bin[8];
rand bit[1:0] if_yes;
rand int a, b;
constraint c1 {
(32 * max_val + 'd480) == a;
}
constraint c2{
(
'd96 + bin[0] + bin[1] + bin[2] + bin[3] + bin[4] + bin[5] + bin[6] + bin[7] +
((if_yes == 1)? 5 : ((if_yes ==2)? 2: (if_yes == 3)? 4:0))
) == b;
}
constraint c3{
a > b;
}
endclass
A a_h;
initial
begin
a_h = new();
repeat(10)
begin
assert(a_h.randomize());
$display("%0d -- %0d -- %p", a_h.max_val, a_h.if_yes, a_h.bin);
$display("%0d -- %0d", a_h.a, a_h.b);
end
end
endmodule
In reply to mayurkubavat:
thanks dave_59 and mayurkubavat for your inputs. Adding explicit sized decimal literals helped me fix the above issue