Key Coverage Methods in SystemVerilog
1. void sample()
The sample() method is one of the most fundamental tools in a covergroup. It triggers the evaluation of all coverpoints and cross coverage within the associated covergroup. By default, sampling occurs when covergroup variables are assigned new values during a simulation, but you can manually call sample() to control when coverage is collected.
Example:
class Testbench;
rand bit [7:0] addr;
rand bit [3:0] opcode;
covergroup cg @(posedge clk);
coverpoint addr;
coverpoint opcode;
cross addr, opcode;
endgroup
function void run_test();
cg c_inst = new();
foreach (addr, opcode) begin
addr = $random;
opcode = $random;
c_inst.sample(); // Explicit sampling
end
endfunction
endclass
In this example:
- The
sample()method ensures that coverage is recorded for theaddrandopcodevalues at every iteration.
2. void set_inst_name(string name)
The set_inst_name() method assigns a unique name to a specific instance of a covergroup. This is particularly useful in simulations where multiple instances of the same covergroup are created, as it helps in differentiating and analyzing coverage data.
Example:
cg c_inst1 = new();
cg c_inst2 = new();
c_inst1.set_inst_name("Instance1");
c_inst2.set_inst_name("Instance2");
Here:
c_inst1andc_inst2are instances of the same covergroup.- Assigning unique names allows the simulator to distinguish their individual contributions to coverage.
3. real get_coverage()
The get_coverage() method returns the overall coverage percentage of a covergroup. This includes all coverpoints and cross coverage within the group, aggregated into a single percentage value. It is a quick way to determine how much of the intended functionality has been tested.
Example:
cg c_inst = new();
// Run the simulation and sample coverage
$display("Overall coverage: %f%%", c_inst.get_coverage());
- The output provides a percentage value, such as
75.0, indicating that 75% of the coverage goals have been achieved.
4. real get_inst_coverage()
The get_inst_coverage() method retrieves the coverage percentage for a specific instance of a covergroup. This is useful when multiple instances are used, and you want to analyze their contributions separately.
Example:
cg c_inst1 = new();
cg c_inst2 = new();
c_inst1.sample();
$display("Coverage for Instance1: %f%%", c_inst1.get_inst_coverage());
$display("Coverage for Instance2: %f%%", c_inst2.get_inst_coverage());
In this case:
c_inst1.get_inst_coverage()andc_inst2.get_inst_coverage()provide individual coverage statistics for their respective instances.
Additional Coverage Methods
void stop(): Prevents further sampling for a covergroup. Useful when you want to freeze coverage collection after a certain point in the simulation.
c_inst.stop();
void start(): Resumes sampling for a covergroup that was previously stopped.
c_inst.start();
void reset(): Clears all coverage data for the covergroup, effectively resetting it to an unsampled state.
c_inst.reset();
Practical Applications of Coverage Methods
- Selective Sampling: Using
sample()only during specific conditions or events in the testbench, such as when the DUT enters a certain state. - Instance Tracking: Naming covergroup instances with
set_inst_name()for better coverage analysis in multi-instance designs. - Coverage Metrics: Using
get_coverage()andget_inst_coverage()to monitor progress during regression runs. - Debugging: Stopping (
stop()) and restarting (start()) coverage collection to isolate specific parts of the simulation.
