Welcome ! This website will help YOU (recent graduates/professionals) learn verification languages like SystemVerilog and UVM. Register for free and access more content !

Constraints can be made active or inactive by the constraint_mode() method. By default all written constraints are in active mode and will be considered by the solver during randomization. When a constraint is made inactive, then it will not be considered in randomization. So, we can dynamically turn on and off a constraint based on the requirements.

Syntax

 
  task object[.constraint_identifier] :: constraint_mode (bit on_off);
 
  function int object[.constraint_identifier] :: constraint_mode ();
 

The object is any expression that yields the object handle in which the constraint is defined and constraint_identifier is the name of the constraint block to which the operation is applied. When called as a task, the argument passed to the task determine whether the specific constraint should be turned on/off. When called as a function, constraint_mode() returns the current state of the specified constraint.

Method

Value Meaning Description
0 OFF Sets constraint to inactive mode
1 ON Set constraint to active mode

The constraint_mode() method is built-in and cannot be overriden.

Example

In the following example, we'll see what effect constraint_mode() has on its constraints.

 
class Fruits;
  rand bit[3:0]  num;
 
  constraint c_num { num > 4; 
                    num < 9; };
endclass
 
module tb;
  initial begin
    Fruits f = new ();
    $display ("Before randomization num = %0d", f.num);
 
    if (f.c_num.constraint_mode ())
      $display ("Constraint c_num is active");
    else
      $display ("Constraint c_num is inactive");
 
    f.randomize ();
    $display ("After randomization num = %0d", f.num);    
  end
endmodule
 
Simulation Log
ncsim> run
Before randomization num = 0
Constraint c_num is active
After randomization num = 8
ncsim: *W,RNQUIE: Simulation is complete.

Now, let's try disabling the constraint using constraint_mode() before attempting to randomizing the variable.

 
module tb;
  initial begin
    Fruits f = new ();
    $display ("Before randomization num = %0d", f.num);
 
    // Turn off constraint
    f.c_num.constraint_mode (0);
 
    if (f.c_num.constraint_mode ())
      $display ("Constraint c_num is active");
    else
      $display ("Constraint c_num is inactive");
    f.randomize ();
    $display ("After randomization num = %0d", f.num);    
  end
endmodule
 
Simulation Log
ncsim> run
Before randomization num = 0
Constraint c_num is inactive
After randomization num = 15
ncsim: *W,RNQUIE: Simulation is complete.

Note that turning off the constraint made the solver choose any value that the variable supported, instead of confining the set of valid values to within those specified by the constraint.

If constraint_mode() method is called on a constraint that does not exist, it will result in a compiler error.

 
  module tb;
    initial begin
      Fruits f = new();
      f.c_does_not_exist.constraint_mode(1);
    end
  endmodule
 
Simulation Log
	f.c_does_not_exist.constraint_mode (1);
                     |
ncvlog: *E,NOTCLM (testbench.sv,11|21): c_does_not_exist is not a class item.

Was this article helpful ?

We use cookies to personalize content and ads, to provide social media features and to analyze our traffic. You consent to our cookies if you continue to use our website. To find out more about the cookies we use and how to delete them, see our privacy policy.

  I accept cookies from this site.
Agree
EU Cookie Directive plugin by www.channeldigital.co.uk