It’s always much more helpful if you explain what “did not work” means. Did you get a compiler error, or didn’t get results you were expecting? If you did not get the results you were expecting, what were they?
You should have gotten a compiler error because the sum() method only works on one dimension of an array at a time. Each ‘item’ would be an array of nine elements and it is illegal to compare that to zero. You need to nest the sum reduction methods.
constraint c2{
a.sum(item1) with (item1.sum(item2) with (item2==0 ? 1 : 0))) <= 10;
}
I assume limited means no more than but if you meant exactly you need to say so. And instead of the conditional operator, I think a cast is more readable.
constraint c2{
a.sum(item1) with (item1.sum(item2) with (int'(item2==0))) <= 10;
}
Finally, the first constraint c1 is unnecessary because a single bit can only have the values 0 or 1 anyways.
module tb;
class my_item;
rand bit a[9][9];
constraint c{a.sum() with ($countones(item)) ==10;}
endclass
initial begin
my_item m1;
m1=new();
assert(m1.randomize());
$display ("%p",m1.a);
end
endmodule
item1/item2 are local variable declarations for iterating over each element. It’s very similar to the foreach(a[ index]) iterator except that ‘item’ represents the actual element, instead of just the index in the foreach.
module tb;
class my_item;
rand bit a[9][9];
constraint c{a.sum() with ($countones(item)) ==10;}
endclass
initial begin
my_item m1;
m1=new();
assert(m1.randomize());
$display ("%p",m1.a);
end
endmodule
this is throwing error :
Unsupported operator 'sum over upper dimension of multi-dimensional array' in this constraint expression.
assert(m1.randomize());
In this code $countones(item), item is an unpacked array of 9 elements, each a single bit. $countones wants to see a packed array. So that is not a valid solution.