Constraints with unpacked array

Hi,
I have the below code which throws an error saying “Illegal non-integral expression in random constraint”. I understand the error as unpacked array is not a integral expression. but can some body show me the code to define a separate array and then use foreach loop to break the aggregate constraint to set of integral constraint. appreciate the help !



class hello_world #(int p);
 
  rand bit [7:0] a [0:3][0:3];
//  int p; 
 
  
constraint c1 {
 
        foreach(a[i,j]) {
          a.sum with (item.index(1) == i ? int'(item): 0) == 27; // rows
          a.sum with (item.index(2) == j ? int'(item): 0) == 27; // cols
        }
          a.sum with (item.index(1) == item.index(2) ? int'(item) : 0) ==27; // diag1
  a.sum with ( (item.index(1) + item.index(2)==2) ? int'(item) : 0) ==27; // diag2
  }
  
  function void display ();
                 $display ("The value of array is %p",a);
 
  endfunction
  
  function new ;
   // p =15;
  endfunction
 
endclass
 
module test;
  hello_world #(27)m1;
 
 
  initial begin
    m1=new();
    m1.randomize();
    m1.display();
 
  end
 
endmodule


In reply to rag123:

Array reduction method only reduce one dimension. You have a two-dimensional array.

class hello_world;
  rand bit [7:0] a [0:3][0:3];
  constraint c1 {
    foreach(a[i]) {
      a[i].sum with (int'(item)) == 27;          // rows
       a.sum with (int'(a[item.index][i]))== 27; // cols
    }
      a.sum with (int'(a[item.index][item.index])) ==27; // diag1
    a.sum with (int'(a[item.index][3-item.index])) ==27; // diag2
  }
 
  function void display ();
                 $display ("The value of array is %p",a);
 
  endfunction
endclass
 
module test;
  hello_world m1;
  initial begin
    m1=new();
    assert(m1.randomize());
    m1.display();
  end
endmodule

In reply to dave_59:

Thanks Dave :) Can you let me know if my understanding is correct on how the foreach unrolls?





constraint c1 {
    foreach(a[i]) {
      a[i].sum with (int'(item)) == 27;          // rows // unrolled as a[0]+a[1]+a[2]+a[3] ==27 for each rows
       a.sum with (int'(a[item.index][i]))== 27; // cols // unrolled as a[0][0] + a[1][0] + a[2][0] + a[3][0] ==27 for each column
    }
      a.sum with (int'(a[item.index][item.index])) ==27; // diag1 
    a.sum with (int'(a[item.index][3-item.index])) ==27; // diag2 
  }