Randomization control

Hi everyone,

I would like to condition the randomization control of a specific field in an SV class based on the value of another field of that class during randomization. Here is a quick example of what I mean:

enum { CHANGE, KEEP } some_type_e;

class my_class;
  rand some_type_e field1;
  rand int field2;
  rand int field3;
endclass

When I call randomize(), I would like that field1 and field2 always get randomized. Based on the randomized value of field1, I would like to either have field3 keep its value (field1 == KEEP) or change its value (field1 == CHANGE).

Is this possible in SV?

Thanks,
Tudor

Use pre_randomize() for this.

class my_class;
  rand some_type_e field1;
  rand int field2;
  rand int field3;
  local int keep_field3;

  constraint keep {(field1 == KEEP) -> field3 == keep_field3); solve field1 before field3;}
  function void pre_randomize();
    keep_field3 == field3;
endclass

Hi Dave,

Thanks a lot for the quick answer. Just another theoretical question if I may: While your solution works, if I were to have a lot fields I want to keep (like field3), wouldn’t it mean that I need twice as much memory for these on account that I have to store their ‘keep’ values? It would have been nice if it were possible to use field.rand_mode(0) on said fields to just not pass them to the constraint solver, but I’ve tried it and it doesn’t work.

In reply to Tudor Timi:

That is correct; the LRM says you can’t change the rand_mode once randomize() is called. You would essentially make rand_mode a random variable, and the simulator would wind up having to save the keep value anyways.

In reply to dave_59:

BTW, I just found a way to do this without using pre_randomize or extra variables. SystemVerilog has a const’(expression) cast which means treat the expression as a const variable.

class my_class;
  rand some_type_e field1;
  rand int field2;
  rand int field3;
 
  constraint keep {(field1 == KEEP) -> (field3 == const'(field3)); solve field1 before field3;}

endclass

In reply to dave_59:

Really cool, thanks!