Constraint for square sub matrices in a bigger matrix

I have a 9x9 matrix as follows M[9][9]
Each A,B,C etc below are sub square matrices of 3x3 so that its a total of 9x9 matrix.


       A B C

M  =   D E F

       G H I


I need to write a constraint for following conditions :
Condition 1 : Any given 3x3 sub square matrices A,B etc above should have a unique max value
e g


      1 1 1               5 6 6  

 A =  3 2 1          B =  6 9 6 

      3 6 8               6 6 6

Amax = 8 , Bmax = 9.

Condition 2 : Amax != Bmax

Condition 3 : Amax != Dmax

For having a max element for one sub square matrix, follo. code works but I dont know how to make it work for all the sub square matrces,



class max;  
  rand bit[4:0] sub_mat[3][3];
  rand bit[4:0] max;
  
  constraint c3 {
    sub_mat.sum(item1) with (item1.sum(item2) with (int'(item2==max))) ==1;
    foreach (mat[i,j]){
      sub_mat[i][j] <= max;
    }
                }    
endclass

Please provide inputs for how to do it for all A,B,C etc sub matrices in bigger matrix M

In reply to prachiparekh27:

Please see solution here

Thanks,
Juhi Patel

Try this.


module tb;
  class square;
    
    typedef logic[4:0] sub_t [3][3];
    
    rand sub_t sub_a[9];
    rand logic[4:0] max[9];
    rand logic[4:0] top[9][9];
    
    constraint array_c{
      unique{max};
      
      foreach(max[i]){
        sub_a[i].sum(item1) with(item1.sum(item2) with (int'(item2 == max[i]))) == 1;
        sub_a[i].sum(item1) with(item1.sum(item2) with (int'(item2 < max[i]))) == 8;  
      }
      
      foreach(top[i,j])
        top[i][j] == sub_a[(i/3)*3 + j/3][i%3][j%3];
      
    }
    
  endclass
 
  
  initial begin
    square inst = new();
    inst.randomize();
    
    $display("max: %p", inst.max);    
    $display("\n");
    
    foreach(inst.top[i])begin
      $display("%p", inst.top[i]);
    end
  end

endmodule

Output:
# max: '{30, 29, 22, 27, 31, 25, 26, 28, 24}
# 
# '{15, 25, 30, 20, 17, 26, 16, 9, 3}
# '{29, 10, 16, 12, 4, 2, 10, 12, 18}
# '{29, 16, 4, 29, 14, 15, 10, 22, 19}
# '{27, 8, 1, 0, 15, 8, 10, 25, 14}
# '{24, 15, 19, 6, 27, 31, 6, 1, 14}
# '{8, 6, 22, 2, 18, 1, 8, 0, 22}
# '{10, 7, 24, 7, 10, 20, 18, 24, 3}
# '{20, 26, 5, 10, 10, 8, 3, 7, 9}
# '{17, 20, 4, 28, 24, 18, 17, 21, 12}