I’d like to know how to calculate the sum of a 2d array using sum() method, or any other more efficient methods.
According to the RFM 2017 7.12.3 Array reduction methods, the second example:
logic [7:0] m [2][2] = '{ '{5, 10}, '{15, 20} };
int y;
y = m.sum with (item.sum with (item)); // y becomes 50 => 5+10+15+20
I should be able to get the sum-product this way. However, I was not able to compile my code using Incisive and QuestaSim. Does anyone know what went wrong?
Basically I just put everything there into a module
module array_2d_sum;
logic [7:0] m [2][2] = '{ '{5, 10}, '{15, 20} };
int y;
y = m.sum with (item.sum with (item)); // y becomes 50 => 5+10+15+20
initial begin
$display("m is %p", m);
end
endmodule
The compilation error in QuestaSim is Error: vlog-13069, pointing to this line y = m.sum with (item.sum with (item)); near “=”: syntax error, unexpected ‘=’.
The same line has another error message: vlog-13205, Syntax error found in the scope following ‘y’. Is there a missing ‘::’?
Basically, I would like to know how to put a constraint on the sum of a 2d array.
module p1;
class c1;
rand int m [2][2];
rand int x;
constraint c1{
foreach(m[i,j])
m[i][j] inside {[-1:1]};
}
constraint c1_2{
x == m.sum with (item.sum with (item));
//only works in in vcs
//x == m.sum();
}
constraint c1_3{
x == 2;
}
endclass
c1 c1_h;
logic [7:0] n [2][2] = '{ '{5, 10}, '{15, 20} };
int n_sum;
initial begin
c1_h = new();
if(c1_h.randomize())
$display("m is %p", c1_h.m);
n_sum = n.sum with (item.sum with (item));
//the sum of 2d array can be calculated this way
$display("n_sum is %d", n_sum);
end
endmodule
I am wondering why my code doesn’t work? Is there a more convenient way to put a constraint on the sum of a 2d array?
The expression in your constraint is taken straight from the LRM section you mention. If your tool has a problem with it, you’ll have to take it up with your tool vendor. This Mentor sponsored public forum is not for discussing tool specific issues.