Generate coverages by loop and function

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.