Randomization with inline constraint

Hi, i am having base class like
class base;
rand [1:0]var;
constraint cnt {var inside {2,3};}
endclass

Now i am using this class in another class like
class basic;
base b;
b=new();
b.randomize(); //i dont want to pass inline constraint to cover 2 and 3 for val in basic class
endclass

i have another requirement where i want to randomize base seq like
class error;
base b;
b.randomize() with {var==0;}
endclass

so for this making soft constraint for base class is not working to override with inline constraint in error seq, plz help me how to do this

Regards
k venkat

In reply to kathula venkatesh:

In base class you can write ::



   class base;
rand [1:0]var;
constraint cnt { soft var inside {2,3};}
endclass


// And override it with in-line hard constraint in class error ::

  class error;
    base b;
    b.randomize() with {var==0;} // Hard in-line Constraint takes priority !!
endclass


In reply to ABD_91:

Hi ABD_91,

using soft constraint inside base class is not working as per expected

Regards
k venkat

In reply to kathula venkatesh:

If there is no in-line constraint to override the base class soft constraint , there is no way your property var would be other than 2 or 3 .

Since in this case only 1 soft constraint is present .

Also what are you expecting and what do you observe in output ?

In reply to ABD_91:

i am expecting var=0 in error class by passing like

class error;
b.randomize() with {var==0;} //in line constraint
endclass
and i am also want to randomize basic class like
class basic;
b.randomize(); //with out inline constraint {var should be 2 or 3 for every randomization}
endclass

so if am using constraint cnt {soft var inside {2,3;}} in base class, it is generating 2 or 3 to my error class even though i am passing with inline constraint as 0

In reply to kathula venkatesh:

I’m not exactly sure if I understood your requirements but something as ABD_91 suggested should work

// Code your testbench here
// or browse Examples
class base;
  rand bit [1:0] x;
  constraint cnt {soft x inside {2,3};}
endclass

class basic;
  base base_h;
  function new();
        base_h = new();
    assert(base_h.randomize());
  endfunction
endclass

class error;
  base base_h;
  function new();
        base_h = new();
    assert(base_h.randomize() with {x == 0;});
  endfunction
endclass


module test();
  error e_h;
  basic  b_h;
  initial begin
    e_h = new();
    b_h = new();
    $display("e_h = %p", e_h.base_h);
    $display("b_h = %p", b_h.base_h);
  end
endmodule


Gives the following output

**$> run
e_h = '{x:'h0}
b_h = '{x:'h3}
**
Keep in mind this is just an example and randomizing inside the constructor is not normal, but it was intended to show that error and basic are generating the base value expected for x (var in your case)

Check your simulator version if it supports soft constraints

HTH,

-R

In reply to kathula venkatesh:

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