Creating new instances of a covergroup using an array

I have following code for covergroup inside a class:

 int skew_val_vl[20];

   covergroup skew_val_cg (int skew_val_vl);
     
      coverpoint skew_val_vl {
       bins skew_range_1 = {[124:0]};
       bins skew_range_2 = {[325:125]};
       bins skew_range_3 = {[526:326]};
       bins skew_range_oor = {[1100:929]};
      
     }
   endgroup : skew_val_cg

function new(string name, uvm_component parent = null);
      super.new(name, parent);
      foreach(skew_val_vl[i])
        skew_val_cg skew_cg[i] = new(skew_val_vl[i]);  //getting error for this line
   endfunction // new

Not sure what I am missing. Can somebody please tell me how to get this working.

Thanks,

Hello,

it would be really helpfull to understand more if you share exact Error which you are getting.

However,few things are missing in above code

  1. you need to create array/define array size before you new each cg instance. because when you say “skew_cg[i]” it doesnt know skew_cg is array or not!!
  2. Last time when i tried to create array of cover-group and define cover-group inside class i was getting compilation issue as tool was not able to resolve the cover-group definition. thats why wrote cover-group outside class.

so try below code, I am sure it will work.



covergroup skew_val_cg (int skew_val_vl);
coverpoint skew_val_vl {
bins skew_range_1 = {[124:0]};
bins skew_range_2 = {[325:125]};
bins skew_range_3 = {[526:326]};
bins skew_range_oor = {[1100:929]};
}
endgroup : skew_val_cg

class sample_class extends uvm_component;
int skew_val_vl[20];

skew_val_cg cg_inst[20];

function new(string name, uvm_component parent = null);
super.new(name, parent);
foreach(skew_val_vl[i])
cg_inst[i] = new(skew_val_vl[i]);
endfunction // new
endclass : sample_class

In reply to Vinay Jain:

Hello Vinay,

Thanks for the suggested code. I tried the same, but get the following error at line

skew_val_cg cg_inst[20]; //Variables of embedded Covergroup type ‘skew_val_cg’ cannot be created.

Not sure if this has to do with the Questasim version that is in use – 10.3.
Let me know if you have any other suggestion. Thnx

You cannot declare an array of an embedded covergroup - this is an LRM restriction because the covergroup declared in a class is an anonymous type and the covergroup name becomes the instance variable. (See 19.4 Using covergroup in classes in the 1800-2012 LRM) You need define the covergroup outside the class

covergroup skew_val_cg (ref int skew_val_vl); // note the use of a ref argument
      coverpoint skew_val_vl {
       bins skew_range_1 = {[124:0]};
       bins skew_range_2 = {[325:125]};
       bins skew_range_3 = {[526:326]};
       bins skew_range_oor = {[1100:929]};
     }
endgroup : skew_val_cg
class my_class extends uvm_component;
 int skew_val_vl[20];
 skew_val_cg sv_cg[20];
  
function new(string name, uvm_component parent = null);
      super.new(name, parent);
      foreach(sv_cg[i])
        sv_cg[i] = new(skew_val_vl[i]);  
   endfunction
endclass

In reply to vsirsikar:

Hello, Did you write cover-group outside class!!?

As Dave mentioned, its SV LRM Restriction.

In reply to dave_59:

Thanks Vinay & Dave,

I had covergroup inside the class. Defining it outside worked.

In reply to vsirsikar:

Hi Dave,
How about array of covergroup inside an interface?
I see it in apb_protocol_monitor_example (coverage cookbook) but when I try to compile code, an error is invoked:

APB_accesses_cg APB_protocol_cg[no_slaves];
              |
ncvlog: *E,SVNOTY (../protocol_monitor/apb_monitor.sv,140|14): Syntactically this identifier appears to begin a datatype but it does not refer to a visible datatype in the current scope.

And this is the code in apb_monitor.sv:

// Functional Coverage for the APB transfers:
//
// Have we seen all possible PSELS activated?
// Have we seen reads/writes to all slaves?
// Have we seen good and bad PSLVERR results from all slaves?
covergroup APB_accesses_cg();

option.per_instance = 1;

RW: coverpoint PWRITE {
  bins read = {0};
  bins write = {1};
}
ERR: coverpoint PSLVERR {
  bins err = {1};
  bins ok = {0};
}

APB_CVR: cross RW, ERR;

endgroup: APB_accesses_cg

// Array of these covergroups
APB_accesses_cg APB_protocol_cg[no_slaves];

Thanks,
P/S: sorry for my bad English

In reply to An Pham:

You can keep the covergroup in another class , and create an array of objects for that class.
this could solve your problem

In reply to shivabachu:

You should be able to declare this array covergroups in a package, interface, or module.

In reply to dave_59:

Hi everyone,
Thanks for your quick replies.
Create a wrapper class for covergroup solved the issue. But it seems that array of covergroup could not be declared inside an interface, or possibly it is the difference between Cadence IUS and Mentor ModelSim.
Thanks,

In reply to An Pham:

Hi Dave,

You mentioned a note for "ref" in this line: covergroup skew_val_cg (ref int skew_val_vl); // note the use of a ref argument

why did you have to use “ref” and what is the use of it ?

Thanks,
Madhu

In reply to mseyunni:

See 19.3 Defining the coverage model: covergroup

In reply to dave_59:

Hi Dave,

I am facing same problem as An Pham.

When i instantiate an array of covergroup inside an interface it says “Syntactically this identifier appears to begin a datatype but it does not refer to a visible datatype in the current scope.”

Any solution to this ?

In reply to vibhatna:

Are you using Questa?

In reply to dave_59:

I am using irun

In reply to vibhatna:

Then you need to take this issue up with Cadence.

In reply to dave_59:

Hi Dave,

To the above class, I added

task test();
foreach(sv_cg[i]) begin
skew_val_vl[i] = 100*i;
sv_cg[i].sample();
end
endtask

and called it for an instance of the class.

When I run vcover report -details <filename.ucdb>, I get 100% result. I also added “option.per_instance=1” to the covergroup definition. Can you tell me, how do I produce overall numbers to accurately reflect my instance coverage?

COVERGROUP COVERAGE:

Covergroup Metric Goal Status


TYPE /por_pub_routing_checker_pkg/skew_val_cg 100.0% 100 Covered
covered/total bins: 4 4
missing/total bins: 0 4
% Hit: 100.0% 100
Coverpoint skew_val_cg::skew_val_vl 100.0% 100 Covered
covered/total bins: 4 4
missing/total bins: 0 4
% Hit: 100.0% 100
bin skew_range_1 4 1 Covered
bin skew_range_2 4 1 Covered
bin skew_range_3 4 1 Covered
bin skew_range_oor 4 1 Covered

Covergroup instance /por_pub_routing_checker_pkg::my_class::sv_cg#1
25.0% 100 Uncovered
covered/total bins: 1 4
missing/total bins: 3 4
% Hit: 25.0% 100
Coverpoint skew_val_vl 25.0% 100 Uncovered
covered/total bins: 1 4
missing/total bins: 3 4
% Hit: 25.0% 100
bin skew_range_1 1 1 Covered
bin skew_range_2 0 1 ZERO
bin skew_range_3 0 1 ZERO
bin skew_range_oor 0 1 ZERO


TOTAL COVERGROUP COVERAGE: 100.0% COVERGROUP TYPES: 1

In reply to dave_59:

Hello Dave,

Can you please give little hint, how the class will recognise the “skew_val_cg” covergroup.
In my case the class is not recognising the covergroup name.

Please guide and help.

Reagrds
Sunil

*In reply to sunils:*It needs to be in the same scope as the class, or you need to put the covergroup in a package and import it.

In reply to dave_59:

Thanks Dave.

-Sunil