Issue with constraint

Hi,

can anyone plz help me to solve this

class base;

rand [1:0]var1;

rand [3:0] var2;

rand bit var3;

constraint cnt1 {soft var1 inside {2,3};}

constraint cnt2 { (var1 == 2) -> (var2 == 0 ;  var3 == 0);}

endclass

Now i am using this class in another class like

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

Could you please help me resolve this?

It would really help others to answer your question if you could provide a complete example with actual versus expected output.

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.

In reply to dave_59:

Hi Dave,

how is var1==2 is 1/64 in probability ? isnt it 1/4?

In reply to rag123:

My mistake, it’s 1 in 33

solution # var1 var2 var3
1 2 0 0
2 3 0 0
3 3 0 1
... 3 0 ...
17 3 0 15
18 3 1 0
19 3 1 1
... 3 1 ...
33 3 1 15

In reply to dave_59:

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

Regards
k venkat