In below code, comp_dev_id is a associative array having 2 dimensional index. I want to get total number of elements of this array.
If I use comp_dev_id.num() then it returns me number of first index(dimension) only, not the sum of both dimension.
class base;
int unsigned num_components = 3;
int unsigned num_dev[3];
bit comp_dev_id[int unsigned][int unsigned];
function void rand_comp_dev_id(input int unsigned total_dev_num = 0, output int unsigned component_id, output int unsigned device_id);
int unsigned comp_id;
int unsigned dev_id;
forever begin
void'(std :: randomize(comp_id) with{comp_id inside{[0 : num_components - 1]};});
void'(std :: randomize(dev_id) with{dev_id inside{[0 : num_dev[comp_id] - 1]};});
if (comp_dev_id[comp_id][dev_id] == 1'b0) begin
comp_dev_id[comp_id][dev_id] = 1'b1;
component_id = comp_id;
device_id = dev_id;
break;
end
end
$display("%0d num reached", comp_dev_id.num());
endfunction : rand_comp_dev_id
endclass
module top();
base B;
int unsigned comp_id;
int unsigned dev_id;
initial begin
B = new();
B.num_dev[0] = 3;
B.num_dev[1] = 2;
B.num_dev[2] = 1;
repeat(6) begin
B.rand_comp_dev_id(6, comp_id, dev_id);
end
end
endmodule
//Output:
//1 num reached
//2 num reached
//2 num reached //here I expected 3
//3 num reached //here I expected 4
//3 num reached //here I expected 5
//3 num reached //here I expected 6