Disable randomization
Randomization can be disabled using the rand_mode method.
- rand_mode() is a function which returns 1 if randomization is enabled else returns 0.
- By default, rand_mode is enabled. I.e. rand_mode(1)
- To disable randomization, rand_mode(0) is used.
- Randomization can be enabled once again for the previously disabled case.
- Randomization needs to be disabled or enabled (if disabled before) before calling the randomize() method.
Methods of using rand_mode
Complete class randomization can be disabled
<object_handle>.rand_mode(0);
Particular variable randomization can be disabled
<object_handle>.<variable>.rand_mode(0);
class seq_item;
rand bit [7:0] value1;
rand bit [7:0] value2;
constraint value1_c {value1 inside {[10:30]};}
constraint value2_c {value2 inside {40,70, 80};}
endclass
module constraint_example;
seq_item item;
initial begin
item = new();
item.randomize();
$display("Before disabling randomization: value1 = %0d, value2 = %0d", item.value1, item.value2);
item.rand_mode(0); // To disable randomization for all class variables
item.randomize();
$display("After disabling randomization for all variables in a class (Retain old values): value1 = %0d, value2 = %0d", item.value1, item.value2);
item.rand_mode(1); // To enable randomization
item.randomize();
$display("After enabling randomization: value1 = %0d, value2 = %0d", item.value1, item.value2);
item.value2.rand_mode(0); // To disable randomization for value2 variable alone
item.randomize();
$display("After disabling randomization for value2 variables in a class: value1 = %0d, value2 = %0d", item.value1, item.value2);
$display("rand_mode function returns for value1 = %0d, value2 = %0d", item.value1.rand_mode(), item.value2.rand_mode());
end
endmodule
Output:
Before disabling randomization: value1 = 14, value2 = 70
After disabling randomization for all variables in a class (Retain old values): value1 = 14, value2 = 70
After enabling randomization: value1 = 15, value2 = 80
After disabling randomization for value2 variables in a class: value1 = 22, value2 = 80
rand_mode function returns for value1 = 1, value2 = 0
Disable Constraint
Similar to disabling randomization, the constraint can also be disabled.
- constraint_mode() is a function which returns 1 if constraint is enabled and else returns 0.
- By default, constraint mode is enabled i.e. constraint_mode(1)
- To disable constraint, constraint_mode(0) is used.
- A constraint can be enabled once again for the previously disabled case.
- A Constraint needs to be disabled or enabled (if disabled before) before calling the randomize() method.
Disable Constraint Example
class seq_item;
rand bit [7:0] value1;
rand bit [7:0] value2;
constraint value1_c {value1 inside {[10:30]};}
constraint value2_c {value2 inside {40,70, 80};}
endclass
module constraint_example;
seq_item item;
initial begin
item = new();
item.randomize();
$display("Before disabling constraint");
$display("item: value1 = %0d, value2 = %0d", item.value1, item.value2);
item.value2_c.constraint_mode(0); // To disable constraint for value2 using handle item2
item.randomize();
$display("After disabling constraint for all value2 alone");
$display("item: value1 = %0d, value2 = %0d", item.value1, item.value2);
$display("constraint_mode function returns for value1 = %0d, value2 = %0d",item.value1_c.constraint_mode(), item.value2_c.constraint_mode());
end
endmodule
Output:
Before disabling constraint
item: value1 = 14, value2 = 70
After disabling constraint for all value2 alone
item: value1 = 15, value2 = 63
constraint_mode function returns for value1 = 1, value2 = 0
System Verilog Tutorials