Constrain a 2D matrix in diamond fashion

I want to randomize an 8x8 2 dimensional array in diamond fashion

rand bit array[8][8];
0 0 0 1 1 0 0 0
0 0 1 1 1 1 0 0
0 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
0 1 1 1 1 1 1 0
0 0 1 1 1 1 0 0
0 0 0 1 1 0 0 0

How to write constraint code for this

I just gave a try. Feel free to optimize this code as much you want


class test;
  
  rand bit arr[8][8];
  bit [8:0] N = 3;
  constraint c1 {
     foreach(arr[i,j]) 
       arr[i][j] inside {[0:512]};}
    
   constraint c2 {
     foreach(arr[i]) {       // begin of parent foreach loop
       if((i==0) || (i==7)) {
         foreach(arr[j])
           if(j==3 || j==4)
             arr[i][j] ==1;
         else arr[i][j] ==0;
         
       }
            
         if((i==1) || (i==6)) {
         foreach(arr[j])
           if(j== 0 || j==1 || j==6 || j==7)
             arr[i][j] ==0;
           else arr[i][j] ==1;
         
       }
           
           if((i==2) || (i==5)) {
         foreach(arr[j])
           if(j== 0 || j==7)
             arr[i][j] ==0;
           else arr[i][j] ==1;
         
       }
             
             if((i==3) || (i==4)) {
               foreach(arr[j])
               arr[i][j]==1;         
       }
     
             }    // end of parent foreach loop
         
  }
  
  
endclass

module top();
  
  test c1;
  
  initial begin
    c1 =new();
    repeat(5) begin
    assert(c1.randomize());
      $display("c1.arr=%p",c1.arr);
    end
    
  end
endmodule
  
  

In reply to bachan21:

Hope this helps, more optimized solution

Thanks,
Juhi Patel.
https://www.linkedin.com/in/juhi-patel-455535149/

In reply to Juhi_Patel:

I liked your solution , how about this alternative ?

class C;
  rand bit data[8][8];

  constraint c_d {
    foreach(data[i,j]) {
      if (((i<=4) && ((j>2-i) && (j<i+5))) ||
         ((i==5) && ((j<i+2) && (j>i-5))) ||
         ((i==6) && ((j<i) && (j>i-5))) ||
         ((i==7) && ((j<i-2) && (j>i-5))) ) {
        data[i][j] == 1;
          } else {
        data[i][j] == 0;
      }
    }
  }
endclass : C

module top();
  C c;
  initial begin
    c = new();
    repeat(1) begin
      if(!c.randomize()) $error("RAND FAILED --- !!!");
      for(int i=0;i<8;i++)begin
        for(int j=0;j<8;j++)begin
          $write ("%0b\t",c.data[i][j]);
        end
        $display();
      end
    end
  end
 
endmodule : top

I should have spent more time to sort the lower half of diamond, for more generic solution instead of directed i values, but leaving on others to sort it out :)

Thank you,
Mega

In reply to Juhi_Patel:

Hi Juhi,
I appreciate your mathematical approach.
I have taken your logic and optimized the code.

class packet;
  rand bit data[8][8];
 
  constraint value {
    foreach(data[i,j]) {
      if (((i<=4) && ((j>2-i) && (j<i+5))) || 
          ((i>4) && (j>i-5) && (i+j <= 11)))
	data[i][j] == 1;
      else
	data[i][j] == 0;
    }
  }

  funtion void print();
    for(int i=0;i<8;i++)begin
      for(int j=0;j<8;j++)
        $write ("%0b\t",data[i][j]);
        $display();
    end
  endfunction : print
endclass : packet

module top();
  C c;
  initial begin
    c = new();
      if(!c.randomize()) $error("RAND FAILED --- !!!");
      c.print();
  end
endmodule : top

In reply to megamind:

Hi Mega,
I have taken your logic and optimized the constrain code.
Thanks for the input thoughts :)

In reply to bachan21:

Great, thanks. Glad!