Tutorials

Learn More

Same as foreach loop, array constraints can also be implemented using foreach loop to iterate over array elements.

Syntax:

constraint <constraint_name> { foreach(variable[i]) variable[i] <condition>}

foreach loop in constraint example

typedef enum {LOW, MID1, MID2, HIGH} scale;
class seq_item;
  rand bit [7:0] value_a[scale];
  rand bit [3:0] array[];
  constraint arr_size_c { value_a.size() == 4; array.size() inside {[2:5]};}
  
  constraint array_c { foreach(array[i]) { array[i] > i*i; } }
  constraint value_a_c {
                         foreach(value_a[i]) {
                           value_a[i] < 100;
                           (i == LOW) -> value_a[i] < 30;
                           (i == HIGH) -> value_a[i] > 70;
                           (i == MID1) -> value_a[i] inside {[30:50]};
                           (i == MID2) -> value_a[i] inside {[51:70]};
                         }
                       }
endclass

module constraint_example;
  seq_item item;
  
  initial begin
    item = new();
    
    repeat(5) begin
      item.randomize();
      foreach(item.value_a[i]) $display("value[%s] = %0d", i.name(), item.value_a[i]);
      foreach(item.array[i]) $display("array[%0d] = %0d", i, item.array[i]);
    end
  end
endmodule

Output:

value[LOW] = 15
value[MID1] = 37
value[MID2] = 54
value[HIGH] = 86
array[0] = 14
array[1] = 10
value[LOW] = 23
value[MID1] = 36
value[MID2] = 60
value[HIGH] = 80
array[0] = 15
array[1] = 3
array[2] = 7
value[LOW] = 6
value[MID1] = 38
value[MID2] = 65
value[HIGH] = 80
array[0] = 5
array[1] = 15
value[LOW] = 9
value[MID1] = 30
value[MID2] = 61
value[HIGH] = 95
array[0] = 4
array[1] = 11
value[LOW] = 20
value[MID1] = 46
value[MID2] = 53
value[HIGH] = 96
array[0] = 11
array[1] = 2

System Verilog Tutorials