Submatrix Constraint Question

DUT computes the histogram of an image composed of 8x8 pixels. Each pixel can be either red, green, blue or defective. Valid images must comply with the following requirements:
a. No more than 25% defective pixels.
b. No sub-matrix of 3X3 size should have all defective pixels.
Examples (D is defective, ‘.’ stands for any of red/green/blue)
Bad (violates condition b)
DDD…
DDD…
DDD…




Good
DD.D…
DDD…
DDD…




BAD

.DDD…
.DDD…
.DDD…



I am able to solve first part through the below code.

module tb;
  
typedef enum bit [1:0] {red, blue, green,defective} pixel_t;

class matrix;

rand pixel_t a[8][8];

 constraint c2{ 
   a.sum(item1) with (item1.sum(item2) with (int'(item2==defective))) < 16;
  }



endclass

Endmodule

How do I solve the 2nd part?

module tb;
  typedef enum bit [1:0] {red, blu, gre,DEF} pixel_t;
  
  class matrix;
    rand pixel_t a[8][8];
    constraint c1{ // easier to read brute force approach
      foreach(a[i,j])
        i inside {[1:6]} && j inside {[1:6]} ->
        !(
          a[i-1][j-1]==DEF &&
          a[i-1][j  ]==DEF &&
          a[i-1][j+1]==DEF &&
          a[i  ][j-1]==DEF &&
          a[i  ][j  ]==DEF &&
          a[i  ][j+1]==DEF &&
          a[i+1][j-1]==DEF &&
          a[i+1][j  ]==DEF &&
          a[i+1][j+1]==DEF
        );
    }
    constraint c1_alternative{ // harder to read, but more programmable
      foreach(a[i,j])
        i inside {[1:6]} && j inside {[1:6]} ->
        !a.and(R) with (R.index inside {[i-1:i+1]} ? 
                        R.and(C) with (C.index inside {[j-1:j+1]} ? C==DEF : '1)
                        :'1);
    }
    constraint c2{ 
       a.sum(item1) with (item1.sum(item2) with (int'(item2==DEF))) <= 16;
    }
  endclass
  
  matrix m = new;
  initial repeat(5) begin
    assert(m.randomize());
    foreach (m.a[i]) $display("%p", m.a[i]);
    $display;
  end
endmodule
2 Likes

Do you mind explaining how this above iteration works?

Also, I tried using the “dist” but I see all ONLY defective being picked when I use the below constraint


constraint c2 {foreach (a[i,j]){
    a[i][j] dist{{RED, GREEN, BLUE} :/ 75, DEF:/ 25};
  }};

Any feedback why this will not work?

Thanks much!

Your syntax is concatenating {RED, GREEN, BLUE}.

You want a range, or list individually

a[i][j] dist{[red:gre] :/ 75, DEF:/ 25};