Tutorials

Protocols

Learn More

Coverage methods such as cg.sample() work on a particular covergroup. Coverage system tasks and functions work at simulation level: they name or load a coverage database and report the overall functional-coverage percentage.

The covergroup provides a set of system tasks and functions in coverage as follows.

Task or function

Kind

Return value

Purpose

$set_coverage_db_name(name)

System task

None

Selects the coverage-database name used when coverage is saved at the end of the run

$load_coverage_db(name)

System task

None

Loads cumulative coverage for all covergroup types from an earlier database

$get_coverage()

System function

real, 0.0 to 100.0

Returns overall coverage across all covergroup types

$get_inst_coverage()

System function

real, 0.0 to 100.0

Returns overall per-instance coverage across covergroup instances, where supported

 

The $ prefix identifies a system task or system function. A task performs an action and does not return a value; a function returns a value that can be printed, compared, or stored.

$set_coverage_db_name - Name the Database

Call $set_coverage_db_name before coverage is saved, preferably near the start of simulation. It changes the database name; it does not take a sample and it does not calculate coverage.

initial begin
  $set_coverage_db_name("my_cg");
end

Part

Meaning

$set_coverage_db_name

The predefined coverage system task

“my_cg”

The requested database name

Result

Coverage collected during the run is associated with this database name when saved

The database format and any filename extension are simulator-dependent, so the created file may not be a plain text file named exactly my_cg.

$get_coverage - Read Overall Coverage

$get_coverage() returns one real percentage for all covergroup types in the current simulation. It is useful for a high-level progress message, but it does not identify which bin is still missing.

real overall_cov;

overall_cov = $get_coverage();
$display("Overall coverage = %0.2f%%", overall_cov);

Expression

Scope of the result

Typical question

$get_coverage()

All covergroup types

How much functional coverage has the complete simulation reached?

cg.get_coverage()

The type represented by cg, or the selected coverage item

How much cumulative/type coverage has this covergroup or item reached?

cg.get_inst_coverage()

One covergroup instance or selected item

How much coverage has this particular instance reached?

Do not assume that five samples mean 5% coverage. The percentage depends on the number of bins, crosses, weights, goals, and hit requirements.

Using the Return Value

if ($get_coverage() >= 90.0)
  $display("Coverage goal reached");
else
  $display("More stimulus is required");

Use a comparison for progress reporting, not as proof that every important scenario was tested. A high overall value can hide a low-priority or low-weight coverage hole.

$load_coverage_db - Continue from an Earlier Run

A single test rarely reaches every bin. $load_coverage_db starts the new simulation with cumulative coverage from a previously saved database, and new samples add to those loaded counts.

initial begin
  $load_coverage_db("regression_cov");
  $set_coverage_db_name("regression_cov");
end

Order

Operation

What it does

1

$load_coverage_db(“regression_cov”)

Restores cumulative covergroup coverage from an earlier run

2

$set_coverage_db_name(“regression_cov”)

Selects the database name for the combined result saved at the end

3

Run the new test

Adds hits produced by the current stimulus

Load the database before meaningful sampling begins. The saved database must match the coverage model closely enough for the simulator to merge the information.

System tasks and functions coverage example

The following version keeps the two 8-bit coverpoints, their cross, and the same five value pairs shown on the source page. Assignment and sampling occur in one task, so each pair is sampled exactly once without a race between parallel processes.

module func_coverage;
  bit [7:0] addr, data;
  covergroup c_group;
    cp1: coverpoint addr;
    cp2: coverpoint data;
    cp1_X_cp2: cross cp1, cp2;
  endgroup : c_group

  c_group cg = new();
  
  initial begin
    $set_coverage_db_name("my_cg");
    
    forever begin
      cg.sample();
      #5;
    end
  end
  
  initial begin
    $monitor("At time = %0t: addr = %0d, data = %0d", $time, addr, data);
    repeat(5) begin
      addr = $random;
      data = $random;
      #5;
    end
    $display("Coverage = %f", $get_coverage());
    $finish;
  end
  
endmodule

Output:

At time = 0: addr = 36, data = 129
At time = 5: addr = 9, data = 99
At time = 10: addr = 13, data = 141
At time = 15: addr = 101, data = 18
At time = 20: addr = 1, data = 13
Coverage = 5.777995

Common Mistakes

  • Confusing $get_coverage() with get_coverage(). The first is simulation-wide; the second targets a covergroup type or item.
  • Calling $load_coverage_db after sampling has already started instead of loading previous results at the beginning.
  • Expecting $set_coverage_db_name to save coverage immediately. It selects a database name; saving normally occurs through the simulator’s coverage flow.
  • Sampling variables while they are being updated in another process at the same simulation time, which can create a race.
  • Using a high overall percentage as the only sign-off check without reviewing uncovered bins and important crosses.
  • Assuming every simulator supports the same database calls, filename rules, and merge behavior.