# Multiple instances of covergroup using an array. How to connect "ref" signal in the class from an interface?

**URL:** <https://verificationacademy.com/forums/t/multiple-instances-of-covergroup-using-an-array-how-to-connect-ref-signal-in-the-class-from-an-interface/35727>\
**Category:** Coverage\
**Tags:** Coverage, pass-by-reference, Covergroup-instance-for-an-array, array-of-covergroups, SystemVerilog-FunctionalCoverage\
**Created:** [February 8, 2019, 12:46am UTC](https://verificationacademy.com/forums/t/multiple-instances-of-covergroup-using-an-array-how-to-connect-ref-signal-in-the-class-from-an-interface/35727 "2019-02-08T00:46:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinodh1180](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/v/5daacb/32.png) [@vinodh1180](https://verificationacademy.com/forums/u/vinodh1180)\
**Post date:** [February 8, 2019, 12:46am UTC](https://verificationacademy.com/forums/t/multiple-instances-of-covergroup-using-an-array-how-to-connect-ref-signal-in-the-class-from-an-interface/35727/1 "2019-02-08T00:46:02Z")

</div>

**Question** : How do I connect the signal with “ref” keyword (in my case [63:0] flag) in the class before invoking the constructor?  
**Sample code for checking coverage of individual bits of 64-bit bus “flag” being set to 1:**

```verilog
covergroup flag_cvg (input bit [63:0] one_hot_bit_position, ref logic [63:0] flag);
  flag_cvp: coverpoint (one_hot_bit_position & flag) != 0;
  option.per_instance = 1;
endgroup : flag_cvg
class flag_cov;
  g3_vif if_g3;
  logic [63:0] flag;
  flag_cvg flag_bits[64];
  bit cov_en = 1;
  string msg_label = "flag_cov";
  function new( string name = "flag_cov", uvm_component parent = null );
    super.new( name, parent );
    **flag = if_g3.flag; // Is this allowed? I want to connect the signal from interface before invoking the constructor with new() keyword.**
    foreach(flag_bits[i])
      flag_bits[i] = new(1'b1<<i, flag);
  endfunction : new
  task main_phase(uvm_phase phase);
    forever begin
      if(cov_en) begin
	`uvm_info(msg_label,"NOW SAMPLING COVERAGE", UVM_DEBUG)
          sample_flag_cvg();
      end
    end
  endtask : main_phase
  task sample_flag_cvg();
    forever begin
      @(negedge if_g3.clkb);
      foreach(flag_bits[i])
        flag_bits[i].sample();
    end
  endtask : sample_flag_cvg
endclass : flag_cov

```

The interface looks like this:

```verilog
interface if_g3;
  logic [63:0] flag = `FLAG_within.dout_sampled[63:0]; // DUT circuit signal connected to a logic type in interface for accessing inside the class
endinterface // if_g3

```

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [February 8, 2019, 5:35am UTC](https://verificationacademy.com/forums/t/multiple-instances-of-covergroup-using-an-array-how-to-connect-ref-signal-in-the-class-from-an-interface/35727/2 "2019-02-08T05:35:39Z")

</div>

_In reply to [vinodh1180](https://verificationacademy.com/forums/t/multiple-instances-of-covergroup-using-an-array-how-to-connect-ref-signal-in-the-class-from-an-interface/35727):_

The procedural assignment is allowed, but does not give you the functionality you are looking for. A better idea is defining your covergroup as

```verilog
overgroup flag_cvg (input bit [63:0] one_hot_bit_position) with function sample(logic [63:0] flag);
  flag_cvp: coverpoint (one_hot_bit_position & flag) != 0;
  option.per_instance = 1;
endgroup : flag_cvg

```

Then pass the flag value at each sample

```verilog
foreach(flag_bits[i])
        flag_bits[i].sample(vif.flag);

```

---

<div class="post-metadata">

**Author:** ![vinodh1180](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/v/5daacb/32.png) [@vinodh1180](https://verificationacademy.com/forums/u/vinodh1180)\
**Post date:** [February 9, 2019, 2:07am UTC](https://verificationacademy.com/forums/t/multiple-instances-of-covergroup-using-an-array-how-to-connect-ref-signal-in-the-class-from-an-interface/35727/3 "2019-02-09T02:07:07Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/multiple-instances-of-covergroup-using-an-array-how-to-connect-ref-signal-in-the-class-from-an-interface/35727/2):_

Thanks for your suggestion Dave. So essentially my code will look like this. Am I right?

```verilog
covergroup flag_cvg (input bit [63:0] one_hot_bit_position) with function sample(logic [63:0] flag);
  flag_cvp: coverpoint (one_hot_bit_position & flag) != 0;
  option.per_instance = 1;
endgroup : flag_cvg

class flag_cov;
  g3_vif if_g3;
  flag_cvg flag_bits[64];
  bit cov_en = 1;
  string msg_label = "flag_cov";
  function new( string name = "flag_cov", uvm_component parent = null );
    super.new( name, parent );
    foreach(flag_bits[i])
      flag_bits[i] = new(1'b1<<i); // Is this right?
  endfunction : new

  task main_phase(uvm_phase phase);
    forever begin
      if(cov_en) begin
	`uvm_info(msg_label,"NOW SAMPLING COVERAGE", UVM_DEBUG)
          sample_flag_cvg();
      end
    end
  endtask : main_phase

  task sample_flag_cvg();
    forever begin
      @(negedge if_g3.clkb);
      foreach(flag_bits[i])
        flag_bits[i].sample(if_g3.flag);
    end
  endtask : sample_flag_cvg
endclass : flag_cov

```
