Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

Disable Constraints Example 1

SystemVerilog Constraints

constraint_mode() called as a function to query whether the c_num constraint is enabled before randomizing.

Open in IDE
testbench.sv
class Fruits;
  rand bit[3:0]  num; 				// Declare a 4-bit variable that can be randomized
  
  constraint c_num { num > 4;  		// Constraint is by default enabled, and applied
                    num < 9; }; 	// during randomization giving num a value between 4 and 9
endclass

module tb;
  initial begin
    Fruits f = new ();
    
	// 1. Print value of num before randomization
    $display ("Before randomization num = %0d", f.num); 	
    
    // 2. Call "constraint_mode" as a function, the return type gives status of constraint
    if (f.c_num.constraint_mode ())
      $display ("Constraint c_num is enabled");
    else
      $display ("Constraint c_num is disabled");
      
    // 3. Randomize the class object
    f.randomize ();
    
    // 4. Display value of num after randomization
    $display ("After randomization num = %0d", f.num);    
  end
endmodule
design.sv
// Code your design here
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.