Systemverilog Disable Randomization

Disable randomization

The variable declared without rand or randc will not get random values on randomization. what if for some reason it is required not to generate a random value for a random variable. Yes, it is possible to disable the randomization of a variable by using the systemverilog randomization method rand_mode.

rand_mode method

The rand_mode() method is used to disable the randomization of a variable declared with the rand/randc keyword.

  • rand_mode(1) means randomization enabled
  • rand_mode(0) means randomization disabled
  • The default value of rand_mode is 1, i.e enabled
  • Once the randomization is disabled, it is required to make rand_mode(1) enable back the randomization
  • rand_mode can be called as SystemVerilog method, the randomization enables/disable status of a variable can be obtained by calling vairble.rand_mode().
  • the rand_mode method returns 1 if randomization is enabled else returns 0

rand_mode syntax

<object_hanlde>.<variable_name>.rand_mode(enable);
//enable = 1, randomization enable
//enable = 0, randomization disable

randomization disable examples

without randomization disable

In the below example,
The class packet has random variables addr and data, on randomization, these variables will get random value.

class packet;
  rand byte addr;
  rand byte data;   
endclass

module rand_methods;
  initial begin
    packet pkt;
    pkt = new();
    
    //calling randomize method
    pkt.randomize();
    
    $display("\taddr = %0d \t data = %0d",pkt.addr,pkt.data);
  end
endmodule

Simulator Output

addr = 110 data = 116

Click to execute on   

randomization disable for a class variable

In the below example,
The class packet has random variables addr and data, randomization is disabled for a variable addr, on randomization only data will get random value. The addr will not get any random value.
rand_mode() method is called in a display to know the status.

class packet;
  rand byte addr;
  rand byte data;   
endclass

module rand_methods;
  initial begin
    packet pkt;
    pkt = new();
    
    //disable rand_mode of addr variable of pkt
    pkt.addr.rand_mode(0);
    
    //calling randomize method
    pkt.randomize();
    
    $display("\taddr = %0d \t data = %0d",pkt.addr,pkt.data);
    
    $display("\taddr.rand_mode() = %0d \t data.rand_mode() = %0d",pkt.addr.rand_mode(),pkt.data.rand_mode());
  end
endmodule

Simulator Output

addr = 0 data = 110
addr.rand_mode() = 0 data.rand_mode() = 1

Click to execute on   

randomization disable for all class variable

In the below example,
randomization for all the class variable is disabled by calling obj.rand_mode(0);

class packet;
  rand byte addr;
  rand byte data;   
endclass

module rand_methods;
  initial begin
    packet pkt;
    pkt = new();
    
    $display("\taddr.rand_mode() = %0d \t data.rand_mode() = %0d",pkt.addr.rand_mode(),pkt.data.rand_mode());
    
    //disable rand_mode of object
    pkt.rand_mode(0);
    
    //calling randomize method
    pkt.randomize();
    
    $display("\taddr = %0d \t data = %0d",pkt.addr,pkt.data);
    
    $display("\taddr.rand_mode() = %0d \t data.rand_mode() = %0d",pkt.addr.rand_mode(),pkt.data.rand_mode());
  end
endmodule

Simulator Output

addr.rand_mode() = 1 data.rand_mode() = 1
addr = 0 data = 0
addr.rand_mode() = 0 data.rand_mode() = 0

Click to execute on   

❮ Previous Next ❯