Where is pre_randomize() used?

Hi,

I have a doubt on pre_randomize() method.
I would like to know the practical application of pre_randomize() method as I have never used it before.

Like, i have used post_randomize() method to calculate parity and CRC.
Can anyone let me know a similar example in which pre_randomize() method will be used ??

Thanks in advance!

There’s no need to use every feature in SystemVerilog, but two applications I have seen are

  • Calling randomize() will not construct any class objects directly. This is most noticeable when randomizing the size of an array of class handles. pre_randomize() can be called to construct an array of class handles to some predetermined maximum size, and then randomize() will shrink the array based on the size of the random solution.
class A;
rand int m;
endclass
class B;
rand A arr[];
constraint arr_size { arr.size < 100;}
function void pre_randomize();
arr = new[100];
foreach(arr[i]) arr[i] = new();
endfunction

endclass

  • People use either pre_ or post_randomize to build a state machine by saving the value of a random value in a non-random state variable
class A;
rand bit [7:0] rvalue;
bit [7:0] svalue;
function void pre_randomize();
svalue = rvalue;
endfunction
constraint never_repeat_same_value { rvalue != svalue; }
endclass

However a new feature of 1800-2012 is a const cast, which means treat the expression as if it were a constant value. So you can now write this constraint much simpler.

class A;
rand bit [7:0] rvalue;
constraint never_repeat_same_value { rvalue != const'(rvalue); }
endclass

In reply to dave_59:

I had never come across randomized class objects in another class. A very good example.
Thank you so much Dave!!