class fs_array;
rand bit [7:0] array1[3][4];
int i,j;
rand int n;
// ************************* incorrect output *******************************************
constraint array_c {
foreach (array1[i]) {
array1[i].sum(item) with ((int'(item) < 80) ? item : 0) == 100; // Trying : each row total is 100 (with each element value less than 80)
}
}
// ****************************************************************************************
// 180 23 189 77
//
// 229 41 59 98
//
// 41 173 237 59
function void display();
foreach (array1[i]) begin
foreach (array1[i][j]) begin
$write(" %d ",array1[i][j]);
end
$display("\n");
end
endfunction
endclass
module fixedsize_array_randomization;
fs_array pkt;
initial begin
pkt = new();
pkt.randomize();
pkt.display();
end
endmodule
// *******Gives expected output ****** only difference from above constraint is "(int'(item) < 26" ********************
constraint array_c {
foreach (array1[i]) {
array1[i].sum(item) with ((int'(item) < 26) ? item : 0) == 100; // Trying : each row total is 100 (with each element value less than 26)
}
}
// ****************************************************************************************
// 25 25 25 25
//
// 25 25 25 25
//
// 25 25 25 25
class fs_array;
rand bit [7:0] array1[3][4];
int i,j;
rand int n;
// ************************* incorrect output *******************************************
constraint array_c {
foreach (array1[i]) {
array1[i].sum(item) with ((int'(item)))== 100; // Gives sum of 100, pardon bracket error if any
}
}
function void display();
foreach (array1[i]) begin
foreach (array1[i][j]) begin
$write(" %d ",array1[i][j]);
end
$display("\n");
end
endfunction
endclass
module fixedsize_array_randomization;
fs_array pkt;
initial begin
pkt = new();
pkt.randomize();
pkt.display();
end
endmodule
Hey sa5691, sorry for not being clear on my requirement.
I need each element’s value to be less than 80 and sum of row to be 100.
I can do this seperatly and get correct result. But I want to specify this using sum method of array.
constraint array_c {
foreach (array1[i]) { // for each row
foreach (array1[,j]) { // for each column in 1 row // meaning element
array1[i][j] < 80; // value of that element less than 80
}
array1[i].sum(item) with (int'(item)) == 100; // each row total is 100
}
}
You first try was very close. Your cast int’(item) < 80 is unnecessary because the width of the conditional expression does not propagate to the result of the conditional operator ?:— it’s just true or false. The result width is the maximum width of the second(true) or third(false) operand. Since the false operand is plain decimal number, its width is at least 32 bits, so the result will be at least 32 bits
That third operand needs to be a value greater than 100 so that item < 80 must always be true.
Hi Dave, Could you please help me understand why the third operand should be 101. Why cant it be 0
“That third operand needs to be a value greater than 100 so that item < 80 must always be true.” How will that work?