Unable to Create multiple instance of covergroup

module trial ();

logic [1:0] connection_rate[8:1];// = 'hx;
logic [1:0] cnnctn_rate = 2'bx;

   covergroup conn_cg ();
     connection_rate_cp : coverpoint cnnctn_rate
                           {
                             bins connection_rate_link_3 = {0}; 
                             bins connection_rate_link_6 = {1}; 
                             bins connection_rate_link_12 = {2}; 
                             bins connection_rate_link_22_5 = {3}; 
                           }   
   endgroup

   conn_cg conn_cg_i[8] ;
   
   initial  begin
    foreach (conn_cg_i[k]) begin
      conn_cg_i[k] = new();
      $display("created for %0d",k);
    end
   end


initial begin
  #5ns;
  for(int i = 0 ; i < 8 ; i++) begin
    connection_rate[i+1] = 2'bx;
    if (`Test.MaxRate == 4'h9)
    connection_rate[i+1] = 2'b0;
    else if (`Test.MaxRate == 4'hA)
    connection_rate[i+1] = 2'b1; 
    else if (`Test.MaxRate == 4'hB)
    connection_rate[i+1] = 2'b10; 
    else if (`Test.MaxRate == 4'hC)
    connection_rate[i+1] = 2'b11;
 
    cnnctn_rate = connection_rate[i+1];
    conn_cg_i[i].sample();
  end
end
endmodule

In the above code i am not able to generate multiple instance of conn_cg.
i tried declaring the cover group outside module too , referring to some post in the same forum but that too didn’t work. how can i generate multiple instances of cover group in module

thanks,
samir

In reply to samir singh:

By default, implementations only save covergroup information for the type, and not per instance. Use this option on your covergroup.

option.per_instance = 1;

BTW, some suggestions for your code.

a more efficient way to model this is to use a lookup table instead of the if/else series, and you can use a sampling argument instead of creating an intermediate cnnctn_rate variable.

module trial ();
 
logic [1:0] connection_rate[int] = '{'h9:'b00,'hA:'b01,'hB:'b10,'hC:'b11};
 
   covergroup conn_cg () with function sample(int rate);
     option.per_instance = 1;
     connection_rate_cp : coverpoint rate
                           {
                             bins connection_rate_link_3 = {0}; 
                             bins connection_rate_link_6 = {1}; 
                             bins connection_rate_link_12 = {2}; 
                             bins connection_rate_link_22_5 = {3}; 
                           }   
   endgroup
 
   conn_cg conn_cg_i[8] ;
 
   initial  begin
    foreach (conn_cg_i[k]) begin
      conn_cg_i[k] = new();
      $display("created for %0d",k);
    end
   end
initial begin
  #5ns;
    foreach (conn_cg_i[i]) conn_cg_i[i].sample(connection_rate[`Test.MaxRate]);
  end
end
endmodule