Every piece of information from this forum is lot of worth especially from you Dave
I tried something myself though not working tool is giving item2 not found
class array_2d_unique;
rand bit [4:0] a2d_arr[][];
rand bit [4:0] val[];
constraint sz_c {
a2d_arr.size inside {[4:8]};
foreach(a2d_arr[i]) a2d_arr[i].size() == a2d_arr.size();//Square Array
val.size() == a2d_arr.size();
}
constraint unq_array_c {
unique{val};
foreach(a2d_arr[i,j]){
a2d_arr[i].sum() with (item == val)==1; //Unique row
a2d_arr[j].sum() with (item == val)==1; //Unique col
a2d_arr.sum(item1) with (item1.sum(item2) with ((item1.index == item2.index) && a2d_arr[item1.index][item2.index] == val)) == 1; //Unique col
a2d_arr.sum(item1) with (item1.sum(item2) with (((item1.index + item2.index) == a2d_arr.size()-1) && (a2d_arr[item1.index][item2.index] == val))) == 1; //Unique col
}
}
function void print();
int size;
size = a2d_arr.size();
foreach(a2d_arr[i,j])begin
if(j < size)
$display("%d ",a2d_arr[i][j%size]);
else
$display("\n");
end
endfunction
endclass
OK, I took a moment to examine this. You could easily replace the sum() reduction method with or() reduction and get the same results. That would be more aligned with the intended meaning of the constraint expression.
class C;
rand bit [2:0] arr[][];
constraint sz_c {
arr.size inside {[4:8]};
foreach(arr[i]) arr[i].size() == arr.size();//Square Array
}
constraint unq_array_c {
arr.sum(item1) with (item1.sum(item2) with (
item1.index ==0 ? 0 :
arr[item1.index][item2.index] == arr[item1.index-1][item2.index] ) // adjacent row
) == 0;
arr.sum(item1) with (item1.sum(item2) with (
item2.index ==0 ? 0 :
arr[item1.index][item2.index] == arr[item1.index][item2.index-1] ) // adjacent column
)== 0;
arr.sum(item1) with (item1.sum(item2) with (
item1.index ==0 || item2.index ==0 ? 0 :
arr[item1.index][item2.index] == arr[item1.index-1][item2.index-1] || // diaganal \
arr[item1.index-1][item2.index] == arr[item1.index][item2.index-1] ) // diaganal /
) == 0;
}
function void print();
$display("\narray size = %0d",arr.size);;
foreach(arr[i,j]) begin
$write("%d ",arr[i][j]);
if(j == arr.size-1)
$display("\n");
end
endfunction
endclass
module top;
C c = new;
initial repeat(5) begin
assert(c.randomize);
c.print();
end
endmodule