Reputation: 43
I need some help.
I am trying to count the number of ones in unpacked array.
Below are the codes.
module test_rand;
bit [7:0] mask_unpacked [0:7];
int numOnes[0:7];
initial begin
mask_unpacked[0] = 8'b00001111;
mask_unpacked[1] = 8'b00001111;
mask_unpacked[2] = 8'b00000000;
mask_unpacked[3] = 8'b00000000;
mask_unpacked[4] = 8'b00000000;
mask_unpacked[5] = 8'b00000000;
mask_unpacked[6] = 8'b00000000;
mask_unpacked[7] = 8'b00000000;
$display("*********************************");
foreach (mask_unpacked[i]) begin
$display("mask_unpacked = %p",mask_unpacked[i]);
numOnes= $countones(mask_unpacked[i]);
$display("countones = %p", numOnes[i]);
end
$display("*********************************");
end
endmodule
But, I got error message
Error-[ICTA] Incompatible complex type
testbench.sv, 16
Incompatible complex type assignment
Type of source expression is incompatible with type of target expression.
Mismatching types cannot be used in assignments, initializations and
instantiations. The type of the target is 'int$[0:7]', while the type of the
source is 'int'.
Source Expression: $countones(mask_unpacked[i])
How to resolve this?
Upvotes: 0
Views: 145
Reputation: 42623
I think you meant to write:
numOnes[i]= $countones(mask_unpacked[i]);
But if you want the total number of ones in the whole unpacked array, you would write:
module test_rand;
bit [7:0] mask_unpacked [8];
int numOnes;
initial begin
mask_unpacked[0] = 8'b00001111;
mask_unpacked[1] = 8'b00001111;
mask_unpacked[2] = 8'b00000000;
mask_unpacked[3] = 8'b00000000;
mask_unpacked[4] = 8'b00000000;
mask_unpacked[5] = 8'b00000000;
mask_unpacked[6] = 8'b00000000;
mask_unpacked[7] = 8'b00000000;
numOnes = mask_unpacked.sum() with ($countones(item));
$display("countones = %d", numOnes);
end
endmodule
Upvotes: 1