user1978273
user1978273

Reputation: 514

SV Randomization giving unexpected values

I have the following sample code

class c;
  rand int a;
  constraint a_c {
    soft a == 10;
  }
  function void post_randomize();
    $display("From c the random value is %0d\n", a);
  endfunction
endclass

class b extends c;
  constraint a_c_1 {
    a inside {[10:100]};
  }
  function void post_randomize();
    $display("From b the random value is %0d\n", a);
  endfunction
endclass

module m;
  c c_obj;
  b b_obj;
  initial begin
    c_obj = new();
    b_obj = new();
    repeat (100) begin
      c_obj.randomize() with {a inside {[10:100]};};
      b_obj.randomize();
    end
  end
endmodule

// Both randomizations give 10.

So, if the base class has soft constraint to fixed 10, unless we disable the constraint or override using same name, we can never relax the constrained values. I was expecting it to relax the constraint since the child is relaxing it.

Upvotes: 2

Views: 819

Answers (2)

nick_g
nick_g

Reputation: 489

If you try to do, for example, c_obj.randomize() with {a inside {[11:100]};};, the soft constraint inside c class will be suppressed by the inline constraint, and will randomize a to values ranging from 11 to 100. The same holds for b class, if, for example, you alter the code as:

constraint a_c_1 {
   a inside {[11:100]};
}

In this case, the constraint inside b class will suppress the soft constraint in c class, so b_obj will randomize a to values from 11 to 100, again.

Otherwise, if all these constraints stay as they are, because of the fact that there is no conflict, you will always get the value of 10 in a variable.

Upvotes: 0

dave_59
dave_59

Reputation: 42698

The extended class c does not override the constraint in class b; it adds to the constraint set. Since the added constraint is not in conflict with the soft constraint, the soft constraint stays. There must be no possible solution with the with the soft constraint before it can be removed.

If you want to relax a constraint, you must override it or disable it.

Upvotes: 2

Related Questions