How to implement coverage collection in UVM?

Hi,

Can you please provide an example for coverage collection in UVM
I tried the following scratch code in uvm_subscriber , but I am getting an error that I cannot understand

class fsm_subscriber extends uvm_subscriber #(fsm_seq_item);
	`uvm_component_utils(fsm_subscriber);
	fsm_seq_item item2;
	covergroup cgrp;
		coverpoint fsm_seq_item.op_a {
bins allowed = {[0:9]};
		}
	endgroup // cgrp
 function new(string name, uvm_component parent);
      super.new(name, parent);
   endfunction: new

virtual function void write (fsm_seq_item t);
	cgrp.sample();
	item2 = t;
	$display("UVM subscriber %0h write function",item2.op_a);
endfunction
endclass

Error :

Illegal to access non-static property ‘op_a’ outside its class scope

In reply to a.nasr:

In line 3 you have declare handle of fsm_seq_item item_2.
However in coverpoint definition you have used fsm_seq_item.op_a.
You can not directly access class property with class name without it’s handle.



covergroup cgrp;
  // Your code
  coverpoint fsm_seq_item.op_a {
    bins allowed = {[0:9]};
  }
 
  // Correction
  coverpoint item2.op_a {
    bins allowed = {[0:9]};
  }

And inside write method you are sampling value before sharing memory with item_2


  // As, item_2 is null before item_2 = t. It will shout null error.
  cgrp.sample();
  item2 = t;

  // Change order
  item2 = t; // share t memory with item_2
  cgrp.sample() // sample cover group


Thanks!