Goal : To generate a array with sum of all the elements is less than or equal to N(array-size)
class data;
rand bit [5:0] val[]; //a dynamic array of unsigned 6-bit elements
//constraint to generate the sum of all the elements in the array is less than or equal to N(array size)
constraint c1 {
//fixing the size of the array(which is N)
val.size() == 8;
val.sum() with (int'(item)) <= 8; //working
val.sum() <= 8; //not-working
}
function void display();
foreach(val[i])
$display("Index[%0d]:%0d",i,val[i]);
endfunction
endclass
module test;
data d;
initial
begin
d = new();
d.randomize();
d.display();
end
endmodule
I would like to understand how does the .sum() operator work in a constraint. For the above code, with this line: val.sum() with (int’(item)) <= 8; I was able to generate an array with sum of all the elements less than or equal to array-size but when I use this line val.sum() <= 8; the array elements are generated randomly based on the datatype range. Could someone please help me to understand this behaviour.
val.sum() with (int’(item)) <= 8;
val.sum() <= 8