Tutorials

Learn More

Randomization can be disabled using the rand_mode method.

  1. rand_mode() is a function which returns 1 if randomization is enabled else returns 0.
  2. By default, rand_mode is enabled. I.e. rand_mode(1)
  3. To disable randomization, rand_mode(0) is used.
  4. Randomization can be enabled once again for the previously disabled case.
  5. 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.

  1. constraint_mode() is a function which returns 1 if constraint is enabled and else returns 0.
  2. By default, constraint mode is enabled i.e. constraint_mode(1)
  3. To disable constraint, constraint_mode(0) is used.
  4. A constraint can be enabled once again for the previously disabled case.
  5. 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