Tutorials

Learn More

Till now, what we have seen about writing a constraint in class. There is a possibility that constraints need to be modified during randomization. An example could be scenario generation with a specific value as shown below example.

The important thing to note that inline constraints do not override constraints written inside the class. Constraint solver considers both constraints inside the class and inline constraints. Any conflict in these constraints leads to randomization failure. How to resolve randomization failure can be discussed in soft constraint

 An inline constraint is written on calling a randomize() method using the with” keyword.

Inline constraints Example

class seq_item;
  rand bit [7:0] val1, val2;
 
  constraint val1_c {val1 > 100; val1 < 200;}
  constraint val2_c {val2 > 5; val2 < 80;}
endclass

module constraint_example;
  seq_item item;
  
  initial begin
    item = new();
    
    repeat(5) begin
      item.randomize();
      $display("Before inline constraint: val1 = %0d, val2 = %0d", item.val1, item.val2);
           
      item.randomize with {val1 > 150; val1 < 160;};
      item.randomize with {val2 inside {[10:15]};};
      $display("After inline constraint: val1 = %0d, val2 = %0d", item.val1, item.val2);
    end
  end
endmodule

Output:

Before inline constraint: val1 = 121, val2 = 75
After inline constraint: val1 = 161, val2 = 12
Before inline constraint: val1 = 176, val2 = 42
After inline constraint: val1 = 113, val2 = 13
Before inline constraint: val1 = 194, val2 = 76
After inline constraint: val1 = 161, val2 = 13
Before inline constraint: val1 = 128, val2 = 42
After inline constraint: val1 = 104, val2 = 14
Before inline constraint: val1 = 114, val2 = 79
After inline constraint: val1 = 179, val2 = 15

System Verilog Tutorials