# FUNCTIONAL COVERAGE FOR DYNAMIC ARRAY

**URL:** <https://verificationacademy.com/forums/t/functional-coverage-for-dynamic-array/41444>\
**Category:** Coverage\
**Tags:** functional-coverage-systemverilog, Coverage\
**Created:** [March 31, 2023, 1:34pm UTC](https://verificationacademy.com/forums/t/functional-coverage-for-dynamic-array/41444 "2023-03-31T13:34:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mahantha\_Deeksha\_S\_B](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/mahantha_deeksha_s_b/32/1072_2.png) [@Mahantha\_Deeksha\_S\_B](https://verificationacademy.com/forums/u/Mahantha_Deeksha_S_B)\
**Post date:** [March 31, 2023, 1:34pm UTC](https://verificationacademy.com/forums/t/functional-coverage-for-dynamic-array/41444/1 "2023-03-31T13:34:55Z")

</div>

i had declared a dynamic array like below:  
rand int data ;  
And was doing the functional coverage for that dynamic array  
c4 : coverpoint data;  
but faced an error like below can anyone help me how to do functional coverage for dynamic arrays  
– Compiling package testbench\_sv\_unit  
\*\* Error: testbench.sv(42): (vlog-2146) Illegal unpacked type expression for coverpoint expression of Coverpoint ‘c4’.  
– Compiling module test  
\*\* Error: testbench.sv(42): (vlog-2146) Illegal unpacked type expression for coverpoint expression of Coverpoint ‘c4’.

---

<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:** [March 31, 2023, 5:51pm UTC](https://verificationacademy.com/forums/t/functional-coverage-for-dynamic-array/41444/2 "2023-03-31T17:51:19Z")

</div>

_In reply to [Mahantha Deeksha S B](https://verificationacademy.com/forums/t/functional-coverage-for-dynamic-array/41444):_

There are two problems with your issue.

Arrays cannot be used for covepoints, only integral values can be captured as bin values. You could create an array of covergroups, one for each element of the array. But the other problem is a dynamic array can change its size during runtime. What exactly is your functional coverage trying to capture? I.e. what samples will it take to get 100% coverage?

---

<div class="post-metadata">

**Author:** ![Mahantha\_Deeksha\_S\_B](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/mahantha_deeksha_s_b/32/1072_2.png) [@Mahantha\_Deeksha\_S\_B](https://verificationacademy.com/forums/u/Mahantha_Deeksha_S_B)\
**Post date:** [March 31, 2023, 6:13pm UTC](https://verificationacademy.com/forums/t/functional-coverage-for-dynamic-array/41444/3 "2023-03-31T18:13:02Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/functional-coverage-for-dynamic-array/41444/2):_

ooh ok i was trying to explore as i was learning SV  
the coverage i was tryn to do was :

```verilog
class packet;
  event cov_ev;
  rand bit [31:0] addr;
  rand int data [];
  rand enum {idle,busy,write,read,resp} mode;
  rand bit [3:0] xfer_id;
  
  //-----------------------------------------------
  constraint c1 {unique{addr};addr[3:0] == 0;unique{xfer_id};};
  //-----------------------------------------------
  constraint c2 {!(addr inside {[32'hf000:32'h1_0000]}); addr dist {['h0000:'h8000]:/ 2,['h1000_00:'hffff_fff0] :/ 2};}//constraints doubt i wanted to get it cleared
  //-----------------------------------------------
  constraint c3 {unique{mode};soft (mode == busy || mode == read) -> (data.size() == 0);soft data.size() <= 300;}
  //-----------------------------------------------
  //constraint c4 {soft data.size() > 0;soft data.size() < 128;}
  //-----------------------------------------------
  constraint c5 {foreach(data[i]) data[i] inside {'h00,'hAA,'hBB,'hCC,'hDD,'h44,'d88,'hFF,'h11,'h22};}
  //-----------------------------------------------
  function void display();
                 $display("The properties values are addr %0h data %0p enum %0s xfer_id %0d", addr, data, mode,xfer_id);
  endfunction
  //-----------------------------------------------
  function void event_gen();
    ->cov_ev;
    $display("the event cov_ev has been generated");
  endfunction
  //-----------------------------------------------
                 covergroup fun_cvrg@(cov_ev);
    option.per_instance = 1;
    c1 : coverpoint addr {
      bins min = {['h0000_0000 :'h0000_EFFFF] };
      bins mid = {['h1_0004 :'h1000_FFFF] };
      bins max = {['hF000_0000 :'hFFFF_FFF0] };
      bins def = default;
                           }
    c2 : coverpoint xfer_id{bins id = {[0:7]};
                           bins def = default;}
    c3 : coverpoint mode{ignore_bins b1 = {idle};
                         bins def = default;}
    c4 : coverpoint data;//shows error for this coverage
    c1Xc2 : cross c1,c2{bins inte = binsof(c1.min) intersect{xfer_id};
                        bins be = binsof(c1.min)||binsof(c2);
                        bins ve = binsof(c1.min)&&binsof(c2);
                       }
    c2Xc3 : cross c2,c3{bins inter = binsof(c2.id) intersect{mode};
                        bins be = binsof(c2.id)||binsof(c3);
                        bins ve = binsof(c2.id)&&binsof(c3);}
  endgroup
  //-----------------------------------------------
  function new();
    fun_cvrg = new;
    data = new[10];
  endfunction
  //-----------------------------------------------
endclass
//-------------------------------------------------
module test;
  int i, k;
  initial begin
    packet covg;
    covg = new();
    for(i = 0;i < 60;i = i + 1) begin
       covg.event_gen();
      k = covg.randomize();//randomising
       covg.fun_cvrg.sample();
       covg.display();
    end
    $display("coverage = %0.2f %%",covg.fun_cvrg.get_inst_coverage());
  end
endmodule

```

and was stuck at 87% coverage without data, dave sir.

---

<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:** [April 1, 2023, 5:02am UTC](https://verificationacademy.com/forums/t/functional-coverage-for-dynamic-array/41444/4 "2023-04-01T05:02:27Z")

</div>

_In reply to [Mahantha Deeksha S B](https://verificationacademy.com/forums/t/functional-coverage-for-dynamic-array/41444/3):_

If you try different seeds, or increase the number of iterations, you will get to 100%.

I understand that you are just experimenting, but functional coverage needs to be traced back to a set of testing requirements. You cannot just ask for “data” to be covered. You need a testing goal in mind that represents what getting 100$ coverage on a coverpoint means. Your constraint on each **data** element restricts it to 10 different values.

A couple of other things about your code:

You initialize data to have 10 elements, but that gets ignored during randomization when you constrain the size to be in the range 1-127.

The **unique** constraints have no effect with there is just a single integral value–it needs an array or multiple values to be compared with to be made unique.

A bin with a **default** value does not get included in the coverage calculation.
