class basic;
base b;
b=new();
repeat (10);
b.randomize(); //
endclass
The problem is if cnt2 constraint is enabled in the base class, var1 is never taking the value 2, if I comment constraint cnt2, both values 2,3 are hitting during randomization.
Adding constraint cnt2, because the requirement says that var2/3 should be 0 when var1=2.
Using solve before constraint also not working to get var1 as 2
Here is one way to slove your problem. There are some syntax mistakes in the above question. you haven’t declared var1 and var2 as bit data type. You can put all conditions in one constraint
class base;
rand bit [1:0]var1;
rand bit [3:0] var2;
rand bit var3;
constraint cnt1 {soft var1 inside {2,3};
(var1==2) -> (var2==0 && var3==0);
solve var1 before var2;}
endclass
module my_module;
base obj;
initial begin
obj=new();
repeat(10) begin
obj.randomize();
$display("var1=%d,var2=%d,var3=%d",obj.var1,obj.var2,obj.var3);
end
end
endmodule
Here is the expected output.
var1=2,var2= 0,var3=0
var1=3,var2= 7,var3=0
var1=2,var2= 0,var3=0
var1=2,var2= 0,var3=0
var1=3,var2= 4,var3=0
var1=3,var2=11,var3=0
var1=2,var2= 0,var3=0
var1=3,var2=13,var3=1
var1=2,var2= 0,var3=0
var1=3,var2= 4,var3=1
In reply to kathula venkatesh:
Your example should have var1==2 true with a 1 in 64 probability. Adding
solve var1 before var2,var3; will give it a 50% probability.
Hi dave,
Thank you for solution, now i am getting results as expected