System-verilog constraint for 2D array, where value of an element should not match to any of its neighbors

Example-

  rand bit[11:0] arr[][]
  Define constraint such that - 
  arr[i][j] != { arr[i-1][j-1]  OR 
                 arr[i-1][j]  OR 
                 arr[i-1][j+1]  OR 
                 arr[i][j-1]  OR 
                 arr[i][j+1]  OR 
                 arr[i+1][j-1]  OR 
                 arr[i+1][j]  OR 
                 arr[i+1][j+1]  
              } 

In reply to gst_beginner :

constraint not_neighbor { foreach (arr[i][j]) {
        i>0 && j>0             -> arr[i][j] != { arr[i-1][j-1];
        i>0 &&                 -> arr[i][j] != { arr[i-1][j];
        i>0 && j< $size(arr,2)-1 -> arr[i][j] != { arr[i-1][j+1];
        ... // I'll let you fill this in
        i<$size(arr,1)-1 && j< $size(arr,2)-1 -> arr[i][j] != { arr[i+1][j+1];
} }

DO check this for proper operator precedence.

In reply to dave_59:

Hi Dave

Thanks for the solution. Would you be able to fill-up rest of the conditions above ? I just want to cross verify.

Thanks in advance.

In reply to dave_59:

In reply to gst_beginner :

constraint not_neighbor { foreach (arr[i][j]) {
i>0 && j>0             -> arr[i][j] != { arr[i-1][j-1];
i>0 &&                 -> arr[i][j] != { arr[i-1][j];
i>0 && j< $size(arr,2)-1 -> arr[i][j] != { arr[i-1][j+1];
... // I'll let you fill this in
i<$size(arr,1)-1 && j< $size(arr,2)-1 -> arr[i][j] != { arr[i+1][j+1];
} }

DO check this for proper operator precedence.

Hi Dave,

How to constraint the size of 2-D array, in this case, how do I define the size of arr in this constraint?

thanks
charlie

In reply to dave_59:

Hi Dave,


constraint not_neighbor { foreach (arr[i][j]) {
        i>0 && j>0             -> arr[i][j] !=  arr[i-1][j-1];
        i>0                    -> arr[i][j] !=  arr[i-1][j];
        i>0 && j< $size(arr,2)-1 -> arr[i][j] !=  arr[i-1][j+1];
        j>0                    ->  arr[i][j] != arr[i][j-1];
        j< $size(arr,2)-1      ->  arr[i][j] != arr[i][j+1]; 
        i>0 && j>0             ->  arr[i]j] != arr[i+1][j-1]; 
        i<$size(arr,1)-1      ->  arr[i]j] != arr[i+1][j];
        i<$size(arr,1)-1 && j< $size(arr,2)-1 -> arr[i][j] != { arr[i+1][j+1];
} }



question:
j< $size(arr,2)-1 → what does this do?

Did i fill in right above?

Thanks
Neelima

In reply to n347:

$size(arr,2) is the size of arr’s second dimension, in this example index j.

The implications are to prevent out-of-bounds index selects. Any time there is a +1 or -1 in the comparison, you need a guard to prevent out-of bounds. I do believe

        i>0 && j>0             ->  arr[i]j] != arr[i+1][j-1]; 

should have been

        i<$size(arr,1)-1 && j>0             ->  arr[i]j] != arr[i+1][j-1]; 

In reply to dave_59:

okay, got it. Thanks a lot Dave!