What happen when sample() get called?

Hi,

I have a question about what does simulator do when sample() get called?
In other words, why not bins get hit if sample() is not called?

Without calling sample(), the bins never hit even pkt_len has an initial value. Can somebody give a bit detail on what happen when sample is called?

module tb;
  bit[7:0] pkt_len = $urandom;
  covergroup cgp1;
    pkt_len_cvpt : coverpoint pkt_len {
      bins short_pkt = {[1:100]};
      bins normal_pkt = {[101:255]};
    }
  endgroup

  initial begin
    cgp1 cgp1_inst = new();
    //cgp1_inst.sample();
  end
endmodule

In reply to mlsxdx:

Hi mlsxdx,

sample()is like a triggering for collecting coverage based on the bins you defined. It is something like you write a property for an assertion and if you do not call assert property, the property will not be asserted. assert is something like triggering. Same way, if covergroups are defined, sample triggers for collecting coverage

In reply to mlsxdx:
When you construct a covergroup, you are creating a set of bins which are nothing more than a set of counters. Each time you call sample(), SystemVerilog sees if the coverpoint (in your example, pkt_len) has a bin associated with a value that matches the coverpoint value. If it finds a match, that bin gets incremented (or ‘hit’)

There are two ways of sampling a covergroup. You can explicitly call sample(), or you can implicitly call sample on an event that you define when declaring the covergroup.

Thanks Anudeep and Dave’s excellent reply.

Hi Dave,

I tried implicitly call sample on an event declaring with covergroup. I don’t know why it doesn’t trigger at all.

module tb;
  bit[7:0] pkt_len = $urandom;
  event sample_it;
  covergroup cgp1@(sample_it);
    pkt_len_cvpt : coverpoint pkt_len {
      bins short_pkt = {[1:100]};
      bins normal_pkt = {[101:255]};
    }
  endgroup
 
  initial begin
    cgp1 cgp1_inst = new();
    ->sample_it;
    #1 $finish();
  end
endmodule

In reply to mlsxdx:
Works for me in Questa (although you need to rewrite the declaration of cgp1_inst)

cgp1 cgp1_inst;
initial begin
    cgp1_inst = new(); // constructor called when the begin block starts executing
    ->sample_it;
    #1 $finish();
  end