Tutorials
Protocols
Learn More
Randomization methods
The randomize() method call is used to randomize class variables based on constraints if they are written. Along with randomize() method, SystemVerilog provides two callbacks
- pre_randomize()
- post_randomize()
A sequence of execution of methods:
pre_randomize() -> randomize() -> post_randomize() pre_randomize method
It is used to do an activity just before randomization. This may involve disabling constraint for a particular variable (constraint_mode(0))or disabling randomization itself (rand_mode(0)). Refer disable randomization for more details.
post_randomize method
It is used to do an activity after randomization. This may involve printing randomized values of a class variable. Override the randomized value of a class variable.
pre_randomize and post_randomize methods Examples
A basic example
A basic example with pre_randomization and post_randomization methods which also include constraint_mode(0).
class seq_item;
rand bit [7:0] val1;
rand bit [7:0] val2;
constraint val1_c {val1 > 100; val1 < 200;}
constraint val2_c {val2 > 5; val2 < 8;}
function void pre_randomize();
$display("Inside pre_randomize");
val2_c.constraint_mode(0);
endfunction
function void post_randomize();
$display("Inside post_randomize");
$display("val1 = %0d, val2 = %0d", this.val1, this.val2);
endfunction
endclass
module constraint_example;
seq_item item;
initial begin
item = new();
item.randomize();
end
endmodule Output:
Inside pre_randomize
Inside post_randomize
val1 = 121, val2 = 247 Example with rand_mode
In pre_randomize() method, rand_mode(0) is used to disable randomization. So, in post_randomization() initial values will be printed.
class seq_item;
rand bit [7:0] val1 = 101; // Initialized to avoid constraint failure
rand bit [7:0] val2 = 6;
constraint val1_c {val1 > 100; val1 < 200;}
constraint val2_c {val2 > 5; val2 < 8;}
function void pre_randomize();
$display("Inside pre_randomize");
this.rand_mode(0);
endfunction
function void post_randomize();
$display("Inside post_randomize");
$display("val1 = %0d, val2 = %0d", this.val1, this.val2);
endfunction
endclass
module constraint_example;
seq_item item;
initial begin
item = new();
item.randomize();
end
endmodule Output:
Inside pre_randomize
Inside post_randomize
val1 = 101, val2 = 6 Example with a derived class
Part A: pre_randomization and post_randomization methods defined in a base and derived class
The base class and child class handle call respective pre_randomization and post_randomization methods as mentioned in the below example.
class seq_item;
rand bit [7:0] val1;
rand bit [7:0] val2;
constraint val1_c {val1 > 100; val1 < 200;}
constraint val2_c {val2 > 5; val2 < 8;}
function void pre_randomize();
$display("Inside pre_randomize");
endfunction
function void post_randomize();
$display("Inside post_randomize");
$display("val1 = %0d, val2 = %0d", this.val1, this.val2);
endfunction
endclass
class child_item extends seq_item;
function void pre_randomize();
$display("Inside pre_randomize of child_item class");
endfunction
function void post_randomize();
$display("Inside post_randomize of child_item class");
$display("val1 = %0d, val2 = %0d", this.val1, this.val2);
endfunction
endclass
module constraint_example;
seq_item item;
child_item c_item;
initial begin
item = new();
c_item = new();
item.randomize();
c_item.randomize();
end
endmodule Output:
Inside pre_randomize
Inside post_randomize
val1 = 121, val2 = 7
Inside pre_randomize of child_item class
Inside post_randomize of child_item class
val1 = 107, val2 = 7 Part B: pre_randomization and post_randomization methods defined in a base class only
The child class handle calls pre_randomization and post_randomization methods of the base class as mentioned in the below example.
class seq_item;
rand bit [7:0] val1;
rand bit [7:0] val2;
constraint val1_c {val1 > 100; val1 < 200;}
constraint val2_c {val2 > 5; val2 < 8;}
function void pre_randomize();
$display("Inside pre_randomize");
endfunction
function void post_randomize();
$display("Inside post_randomize");
$display("val1 = %0d, val2 = %0d", this.val1, this.val2);
endfunction
endclass
class child_item extends seq_item;
// No method implemented.
endclass
module constraint_example;
seq_item item;
child_item c_item;
initial begin
item = new();
c_item = new();
item.randomize();
c_item.randomize();
end
endmodule Output:
Inside pre_randomize
Inside post_randomize
val1 = 121, val2 = 7
Inside pre_randomize
Inside post_randomize
val1 = 107, val2 = 7 Note:
- If randomization is failed post_randomization will not be called and the variable will retain previous values.
- Pre and post-randomization functions can not be virtual functions.
System Verilog Tutorials