SystemVerilog Disable Constraints

Disable Constraints

Constraints in a class can be disabled using the constraint_mode method call. By default all the constraints will be enabled, during the randomization constraint solver will not consider the disabled constraints. the constraint disables method is similar to rand_mode() method.

constraint_mode() method

The constraint_mode() method can be used to disable any particular constraint block.

  • constraint_mode(1) means constraint block is enabled
  • constraint_mode(0) means constraint block is disabled
  • default value of constraint_mode is 1, i.e enabled
  • once the constraint block is disabled, it is required to make constraint_mode(1) enable back the constraint block
  • constraint_mode can be called as like SystemVerilog method, which returns the enable/disable status of a constraint block

constraint_mode syntax

<object_hanlde>.<constraint_block_name>.constraint_mode(enable);
//enable == 1, constraint block enable
//enable == 0, constraint block disable

constraint disable  examples

without constraint disable

In the below example,
The constraint is enabled by default, so on randomization constraint solver will consider the constraint and assigns the random value to variable addr as per the constraint is written.

class packet;
  rand  bit [3:0] addr;
  
  constraint addr_range { addr inside {5,10}; }
endclass

module static_constr;
  initial begin
    packet pkt;
    pkt = new();
    
    pkt.randomize();
    $display("\taddr = %0d",pkt.addr);
  end
endmodule

Simulator Output

addr = 5

Click to execute on   

with constraint disabled

The constraint is disabled by using the constraint_mode method, so on randomization constraint solver will not consider the constraint.

class packet;
  rand  bit [3:0] addr;
  
  constraint addr_range { addr inside {5,10,15}; }
endclass

module static_constr;
  initial begin
    packet pkt;
    pkt = new();
    
    
    $display("Before Constraint disable");
    repeat(2) begin //{
      pkt.randomize();
      $display("\taddr = %0d",pkt.addr);
    end //}
    
    //disabling constraint
    pkt.addr_range.constraint_mode(0);
    
    $display("After Constraint disable");
    repeat(2) begin //{
      pkt.randomize();
      $display("\taddr = %0d",pkt.addr);
    end //}
  end
endmodule

Simulator Output

Before Constraint disable
addr = 15
addr = 5
After Constraint disable
addr = 9
addr = 14

Click to execute on   

calling constraint_mode method

In the below example,
Constrain_mode is called as method to see to the enable/disable status of constraint.

class packet;
  rand  bit [3:0] addr;
  
  constraint addr_range { addr inside {5,10,15}; }
endclass

module static_constr;
  initial begin
    packet pkt;
    pkt = new();
    
    
    $display("Before Constraint disable");
    $display("Value of constraint mode = %0d",pkt.addr_range.constraint_mode());    
    pkt.randomize();
    $display("\taddr = %0d",pkt.addr);
    
    //disabling constraint
    pkt.addr_range.constraint_mode(0);
    
    $display("After Constraint disable");
    $display("Value of constraint mode = %0d",pkt.addr_range.constraint_mode());    
    pkt.randomize();
    $display("\taddr = %0d",pkt.addr);
    
  end
endmodule

Simulator Output

Before Constraint disable
Value of constraint mode = 1
addr = 15
After Constraint disable
Value of constraint mode = 0
addr = 1

Click to execute on   

❮ Previous Next ❯