Constraint in a 2d pixel matrix

Let’s assume there’s a 2d pixel matrix 1920 x 1080 in size. I want to constrain the matrix such that in the neighborhood of an off pixel(0) there are only ON pixels(1-255). The pattern should be something like this, for a smaller 5x4 matrix:
x x x x
x 0 x 0
x x x x
x 0 x 0
x x x x

What would be a better way to solve this constraint, assuming I don’t use the foreach loop something as described below:


foreach(mat[i][j]){
  mat[i][i] = 0;
}
foreach(mat[i][j]){
  if(mat[i][j] == 0) begin
     mat[i-1][j] = $urandom_range(1,255);
     mat[i][j-1] = $urandom_range(1,255);
     mat[i-1][j-1] = $urandom_range(1,255);
     ..etc
  end
}

Can we use the reduction operations somehow for the problem

In reply to sparsh.gupta:

I think the best approach would be to constrain zero elements not to be adjacent.

constraint not_neighbor { foreach (mat[i][j]) {
        i>0 && j>0             -> mat[i][j] || mat[i-1][j-1];
        i>0                    -> mat[i][j] || mat[i-1][j];
        i>0 && j< $size(mat,2)-1 -> mat[i][j] != { mat[i-1][j+1];
        ... // I'll let you fill this in. You need one line for each side and corner
        i<$size(mat,1)-1 && j< $size(mat,2)-1 -> mat[i][j] != { mat[i+1][j+1];
} }

In reply to dave_59:

Hi Dave, does arr and mat refer to the same two dimensional array? Please clarify. If not then what does two dimensional variable arr needed for?

In reply to srshan_rocks:

another solution can be as follows:

module abc;
  
  class pixel;
    rand bit a [3][3];
    
    constraint c1 {
      
      foreach(a[i,j]){
        if(j>0)   a[i][j] == 0 -> a[i][j-1]   == 1;
        if(j<3)  a[i][j] == 0 -> a[i][j+1]   == 1;
        
        if(i>0)  a[i][j] == 0 -> a[i-1][j]   == 1;  
        if(i<3)  a[i][j] == 0 -> a[i+1][j]   == 1;
        
        if(i > 0 && j > 0)  a[i][j] == 0 -> a[i-1][j-1] == 1;  
        if(i < 3 && j < 3)  a[i][j] == 0 -> a[i+1][j+1] == 1;
        
        if(i < 3 && j > 0)  a[i][j] == 0 -> a[i+1][j-1] == 1;  
        if(i > 0 && j < 3)  a[i][j] == 0 -> a[i-1][j+1] == 1;
      }
    }
  endclass
 
  initial begin
    pixel p1  = new();
    p1.randomize();
    foreach(p1.a[i]) begin
      $display(p1.a[i]);
    end
  end 
          
endmodule

In reply to srshan_rocks:

I think those were typos/copy/paste errors. fixed.