OVM Sample Called, Still Yielding 0% Coverage

Hi, I am working on a coverage class in OVM. I have a covergroup, and an array (a hex value), and I only want to run a sample on my covergroup when the value of the array is greater than zero.

I have print statements in my if statement, so I know that I am hitting the if statement (data is getting set to the array). However, my covergroup has 0% coverage, which I don’t understand because I feel that the sample() call has to be happening since I’m hitting the if. Is there any way you can be sampling a covergroup and still get 0% coverage for the covergroup?

Also, note that when I did not have the sample() statement inside the if, I had 100% coverage. But whenever I encase it in the if, I cannot get it to have any coverage at all.

I simplified this as much as I could for this example, so this probably isn’t 100% syntactically correct, but here is the general idea:

class cov_example extends ovm_component;
    bit coverage_enable = 1;
	
    bit [63:0] array1;

    covergroup  example_cg;
        option.per_instance = 1; // Will indicate that coverage is present, yielding coverage results
        array1 : coverpoint array1 {
            bins x = {array1[0]};
            bins none = default;
        }
    endgroup : example_cg

    `ovm_component_utils_begin(cov_example)
        `ovm_field_int(coverage_enable, OVM_DEFAULT)
    `ovm_component_utils_end

    function new (string name, ovm_component parent);
        super.new(name, parent);

        example_cg = new();
        example_cg.set_inst_name({get_full_name(), ".example_cg"});

    endfunction : new

    virtual function void build();

        super.build();

    endfunction : build


    function void write(ovm_sequence_item pkt);

	someFunction(); // Assume that this is a function that sets array1 to some nonzero value.
        if (coverage_enable) begin
		if( array1 > 0)begin
					
               example_cg.sample();

            end
            
    end

endclass : cov_example

In reply to rachel.greenlee:
What is the intended meaning of

bins x = {array1[0]};

Because array[0] == 0 when the convergroup gets constructed, this is equivalent to

bins x = {0};

which I’m sure is not what you intended. If you only callsample() when array is non-zero, you will get 0% coverage.

In reply to dave_59:

This is my first time to make a coverage class, and I think there are gaps in my understanding as to what occurs when I call sample(). My intention is to, like I said, set the bin only when the data is > 0. Could I do something like

 bins x = {array1}; 

and achieve my desired results?

In reply to rachel.greenlee:

A coverpoint defines an expression to be sampled when sample() gets called. bin values are defined when the covergroup gets constructed. Each time you call sample(), the value of the sampled coverpoint expression gets compared with set of bin values defined at construction. If the values match, the bin is set as “covered”.

It seems what you really want is

  covergroup  example_cg;
        option.per_instance = 1; // set this if there will be multiple instance of this class
        array_is_nonzero : coverpoint array1 >0 {
            bins x = {1}; // will be hit if array1 > 0 is true
        }
    endgroup : example_cg


Ok thank you! I actually ended up figuring this out. What I did was sample it no matter what, and check in the covergroups, but a slightly different way than you showed me:

  
covergroup  example_cg;
        option.per_instance = 1; 
        array1 : coverpoint array1 {
            bins x = {[1:$]};
            bins none = {0};
        }
    endgroup : example_cg