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
1 Like
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!!
Hi Dave ,
could we not use rand_mode in pre_randomize() to enable or disable class variable randomization ?
class check ;
rand bit[2:0] len=0;
bit len_en;
function void get_len();
randomize(len);
$display("value of len=%d",len);
endfunction
function void pre_randomize();
len.rand_mode(len_en);
endfunction
endclass
module prefix;
check c1;
initial begin
c1 =new;
c1.len_en=0;
c1.get_len();
c1.len_en=0;
c1.get_len();
c1.len_en=0;
c1.get_len();
c1.len_en=0;
c1.get_len();
end
endmodule
although I am setting len_en ==0 which has been mapped to rand_mode for variable len still I am getting random value
# value of len=5
# value of len=0
# value of len=1
# value of len=1
Could you please elaborate
Regards
Your problem is not with pre_randomize()
.
You are calling randomize(len)
which flips the rand_mode of len
. Just call randomize()
with no arguments.