Parametric bin assignment?

In reply to Morteza_Seyedi:

There are several solutions, if you do not want to have an array of covergroups inside the wrapper class you could alternatively sample the “index” of your array only when the bit is set, I’m not sure if there are any drawbacks to this, but I could think that potentially could be more efficient than a full array of covergroups specially if you have let’s say a large number signals to cover with a large number of bits, also when reading the coverage report it can be a bit tedious traverse across all the cgs created

Also keep in mind that based your requirement you’ll get 100% coverage with a single value that has all bits set, potentially this is not exactly what you want, this article has interesting examples


class coverage_wrapper #(WIDTH = 16);
  
  covergroup bits_set_cg with function sample(bit [WIDTH-1:0] data, int index);
    option.per_instance = 1;
   
    cp_bits_set : coverpoint index iff(data[index] == 1) { //only sample when bit is set
      bins index_set[] = {[0:WIDTH-1]}; //measure the index that is set
    }
  endgroup
   
  function new();
    bits_set_cg = new();
  endfunction
  
  function void sample_bits_set_cg (bit[WIDTH-1:0] data);
    for(int i=0; i < WIDTH; i++) begin
      bits_set_cg.sample(data, i);
   	end
  endfunction
endclass


module test();
  parameter DW1 = 12;
  parameter DW2 = 16;
  bit [DW1-1:0] data_x;
  bit [DW2-1:0] data_y;
  
  
  coverage_wrapper #(.WIDTH(DW1)) cg_wrap_data_x;
  coverage_wrapper #(.WIDTH(DW2)) cg_wrap_data_y;
  
  initial begin
    cg_wrap_data_x = new();
    cg_wrap_data_y = new();
    repeat(5) begin
      if(!std::randomize(data_x)) $fatal(1, "Randomize Failed!");
      $display("data_x = %b", data_x);
      cg_wrap_data_x.sample_bits_set_cg(data_x);
      
      if(!std::randomize(data_y)) $fatal(1, "Randomize Failed!");
      $display("data_y = %b", data_y);
      cg_wrap_data_y.sample_bits_set_cg(data_y);
      
    end
    $display("cg_wrap_data_x.bits_set_cg = %0f%%", cg_wrap_data_x.bits_set_cg.get_inst_coverage());
    $display("cg_wrap_data_y.bits_set_cg = %0f%%", cg_wrap_data_y.bits_set_cg.get_inst_coverage());
  end
  
endmodule


HTH,
-R