IMPORTANT NOTICE: Please be advised that the Verification Academy Forums will be offline for scheduled maintenance on Sunday, November 9th at 1:00 US/Pacific.
You have made the constraints much more complex than they need to be, And technically, you are not allowed a random index into a random array variable.
module abc;
class aa;
rand byte unsigned val[10], val3;
constraint s1 {
foreach(val[i]) {
val[i] < 10;
// every value except val3 must appear only once
val[i] != val3 -> val.sum() with (int'(item==val[i]))== 1;
}
// val3 must appear three times in the array
val.sum() with (int'(item==val3)) == 3;
}
endclass
aa a1 = new();
initial repeat (10) begin
a1.randomize();
$display("%p %d", a1.val, a1.val3);
end
endmodule
In reply to dve12:
You have made the constraints much more complex than they need to be, And technically, you are not allowed a random index into a random array variable.
module abc;
class aa;
rand byte unsigned val[10], val3;
constraint s1 {
foreach(val[i]) {
val[i] < 10;
// every value except val3 must appear only once
val[i] != val3 -> val.sum() with (int'(item==val[i]))== 1;
}
// val3 must appear three times in the array
val.sum() with (int'(item==val3)) == 3;
}
endclass
aa a1 = new();
initial repeat (10) begin
a1.randomize();
$display("%p %d", a1.val, a1.val3);
end
endmodule
Hi Dave,
Could you please explain what the above code is doing.
// every value except val3 must appear only once
val[i] != val3 -> val.sum() with (int'(item==val[i]))== 1;
}
// val3 must appear three times in the array
val.sum() with (int'(item==val3)) == 3;
I have trouble understanding these lines. What exactly ‘item’ refers to here?
What are these lines achieving exactly?
In reply to totochan1985:
sum() is one of the array reduction methods (See section 7.12.3 Array reduction methods).
When used in a constraint, these methods get unrolled into a big expression (Section 18.5.8.2 Array reduction iterative constraints)
‘item’ is replaced with each element of the array.