Generate coverages by loop and function

Hi,

I want to generate coverage by loop.

This is my partial code but it’s not compiled. It’s kind of a pseudo code because I do not know how it should be in SV. What changes do I need to do ?


   coverpoint do_check_cp(input [0:2]logic s) {
      bins psprintf("%b",s)  = {s};
      //bins do_1_0_0  = {3'b100};   
      //bins do_1_0_1  = {3'b101};   
    }
 do_check_cp do_check_arr = new do_check_arr[3];
foreach(do_check_arr[i]) begin
do_check_arr[i]=new(i<<1);
end

In reply to alexd555:

Your pseudo code does not match your comments, and you never specified what is is that you wanted to sample. Since you were having trouble with covertgroup syntax and explaining what kind of coverage you are looking for it would really help to shoe a complete example that manually calculate coverage without using a covergroup

module manual_coverage;
  bit my_bins[bit [2:0]];
  bit [2:0] sample;
  int sum_of_bins_hit;
  initial begin
    for(int i=0;i<3;i++) // this creates bins 3'b000, 3'b010, and 3'b110
      my_bins[i<<1] = 0;
    // generate random stimulus
    repeat(7) begin
      sample = $urandom;
      if(my_bins.exists(sample)) begin
        $display("sample value %b bin hit", sample);
        my_bins[sample] = 1;
      end 
      else
        $display("sample value %b does not hit a bin",sample);
    end
    sum_of_bins_hit = my_bins.sum() with (int'(item));
    $display("Final coverage is bins hit %d / Total bins %d", sum_of_bins_hit, my_bins.size());
    $display("%d%% Final coverage percentage", 100*sum_of_bins_hit/my_bins.size());
  end
endmodule

In reply to dave_59:

Thanks for your answer.
But how can I convert to coverage group ? I want to sample the sample array. Not per bit. I want to sample the full array.

I need to show the string(for example, “1_0_0”) and if it’s hit in the coverage

In reply to dave_59:

Can you help please ?

In reply to alexd555:

No one can help you without a much better description of what you want to cover. Like I said earlier, it would really help to show some code that generators the pattern you want to cover.

In reply to dave_59:

Can you explain please what you can’t understand ?

I want to cover sequences of ‘1’ and ‘0’ so that it will be clear to the user which sequence was hit.
The sequence should have a dynamic length.

For example

001
001001
01010101

In reply to alexd555:

That’s new information, which is good, but very different from what you originally asked. It seems like you want to detect a pattern of sequential bits in a serial stream, and that pattern can have a variable length. It would really help to write some code that generates the values you want to cover. Then we no longer have to keep guessing what you mean. Like this:

module top;
  
  bit pattern [][] = '{
    {0,0,1},
    {0,0,1,0,0,1},
    {0,1,0,1,0,1,0,1}
  };
  bit clk; initial #5 forever #5 clk = !clk; // posedge on 10,20,30,...
  
  bit stream;
  int select;
  
  initial begin
    repeat (10) begin
      select = $urandom_range(pattern.size());
      foreach(pattern[select][j]) @(posedge clk) begin
        stream = pattern[select][j];
        $display(stream,,select,,j);
      end
    end
    $finish;
  end

endmodule

If you only had a few of these sequences, you could hard-code it in a coverpoint

  covergroup do_check_cp @(posedge clk);
    coverpoint stream {
      bins do_0_0_1  = ( 0=> 0=> 1);
      bins do_0_0_1_0_0_1  = ( 0=> 0=> 1 => 0=> 0=> 1);
      bins do_0_1_0_1_0_1_0_1 = ( 0=> 1=> 0=> 1=>  0=> 1=>  0=> 1 );
    }
  endgroup

Another thing I have to guess is that you have many of these patterns that might be programmable, which is the reason behind the title of this question. That’s not possible with a covergroup alone. You would have to create an array of sequence detectors that set a bit in an array of covergroups. But before I would show you that, you still have more questions to answer. 1) Does the code I wrote for you match the kind of stimulus you want to cover? If not, please fix my code to do exactly what you a looking for. 2) Can the patterns overlap? you showed me 001 and 001001. If they are allowed to overlap, then the second pattern cannot be covered without also covering the first pattern.

In reply to dave_59:

Hi,

This should be programmable.
Let’s assume that we have a rand bit variable that generates by loop (for example: 100 times. but it should be a random number)
I want to cover the changes from 1 to 0 or 0 to 1.
For example:
if all ‘1’ or all ‘0’ then it’s not interesting for me.
But if it’s 100001 or 001000010 then it’s interesting. It’s just an example. it should be a dynamic.

Understand ? Can you help ?

Thanks,
Alex

In reply to dave_59:

Can you help please ?

Thanks,
Alex

In reply to alexd555:

It would really help to write some code that generates the values you want to cover. Then we no longer have to keep guessing what you mean. Like what I wrote above.