Code coverage concepts in Verilog - Time & Space Complexity
When we check code coverage in Verilog, we want to see how much of our code runs during tests.
We ask: How does the time to collect coverage data grow as the code or tests get bigger?
Analyze the time complexity of the following coverage collection snippet.
always @(posedge clk) begin
if (reset) begin
coverage_count <= 0;
end else begin
coverage_count <= coverage_count + 1;
cg_inst.sample();
end
end
// Coverage check for a signal
covergroup cg;
coverpoint signal;
endgroup
cg cg_inst = new();
This code counts events and tracks coverage points each clock cycle.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The coverage count update and coverage point check happen every clock cycle.
- How many times: Once per clock cycle, repeated for the entire test duration.
As the number of clock cycles (test length) grows, the coverage operations run more times.
| Input Size (clock cycles) | Approx. Operations |
|---|---|
| 10 | 10 coverage updates |
| 100 | 100 coverage updates |
| 1000 | 1000 coverage updates |
Pattern observation: The number of coverage operations grows directly with the number of clock cycles.
Time Complexity: O(n)
This means the time to collect coverage grows linearly with the number of clock cycles in the test.
[X] Wrong: "Coverage collection happens only once and does not depend on test length."
[OK] Correct: Coverage updates happen every clock cycle, so longer tests mean more coverage operations.
Understanding how coverage collection scales helps you design efficient tests and tools that run well even as designs grow.
"What if we added multiple coverage points inside the covergroup? How would the time complexity change?"
Practice
Solution
Step 1: Understand code coverage concept
Code coverage tracks which parts of the Verilog design are exercised during simulation.Step 2: Identify the main goal
The goal is to ensure tests cover all design parts, improving test quality.Final Answer:
To check which parts of the design have been tested -> Option CQuick Check:
Code coverage = tested parts check [OK]
- Confusing coverage with simulation speed
- Thinking coverage generates synthesis reports
- Assuming coverage optimizes hardware layout
Solution
Step 1: Recall coverage constructs
SystemVerilog usescovergroupto group coverage points andcoverpointto define individual coverage points.Step 2: Identify the coverage point
The coverage point is specifically defined bycoverpoint, inside acovergroup.Final Answer:
coverpoint -> Option AQuick Check:
Coverage point = coverpoint [OK]
- Confusing covergroup with coverpoint
- Using assertion or property for coverage points
- Mixing coverage with verification constructs
state after these values: 0, 1, 0, 2?
int state; covergroup cg; coverpoint state; endgroup cg cg_inst = new(); state = 0; cg_inst.sample(); state = 1; cg_inst.sample(); state = 0; cg_inst.sample(); state = 2; cg_inst.sample();
Solution
Step 1: Understand coverage counting
Coverage counts unique values sampled by the coverpoint. Values 0,1,2 appear.Step 2: Count unique values
Values sampled: 0 (twice), 1, 2. Unique values are 0,1,2 -> 3 unique hits.Final Answer:
3 -> Option DQuick Check:
Unique states = 3 [OK]
- Counting repeated samples multiple times
- Assuming total samples equal coverage count
- Ignoring unique value concept
int signal;
covergroup cg;
coverpoint signal {
bins low = {0};
bins high = {1};
}
endgroup
cg cg_inst = new();
signal = 2;
cg_inst.sample();Solution
Step 1: Analyze bins coverage
Bins cover values 0 and 1 only. Sampled value is 2, which is not covered.Step 2: Identify error impact
Sampling 2 will not hit any bin, causing incomplete coverage.Final Answer:
Bins do not cover the sampled value 2 -> Option BQuick Check:
Bins coverage missing sampled value [OK]
- Ignoring uncovered sampled values
- Thinking semicolon is missing after coverpoint
- Assuming sample() method is invalid
a and b in SystemVerilog?Solution
Step 1: Understand covergroup composition
A covergroup can contain multiple coverpoints to track individual signals.Step 2: Use cross coverage
Cross coverage tracks combinations of values from multiple coverpoints, hereaandb.Final Answer:
Define coverpoints foraandband add acrosscoverage for both inside the covergroup -> Option AQuick Check:
Cross coverage = combined signal tracking [OK]
- Not using cross coverage for combined signals
- Defining separate covergroups without cross
- Confusing assertions with coverage
Ready to prove it?
Pass the test and this pattern moves unknown
