Lesson 4: What Is Functional Coverage, and How Do Covergroups Measure It?
Functional coverage measures whether the test exercised the cases in your verification plan, not just whether it passed. You describe the interesting values as bins in a covergroup, sample it as transactions happen, and read back the percentage of bins hit. Random tests make coverage essential: a passing random test tells you nothing about the cases it never generated.
What you'll learn
- Covergroups, coverpoints, bins and crosses
- Sampling at the right moment
- Reading coverage results, and why a cross matters
- Closing a coverage hole by steering the stimulus
Conventions and assumptions
Every lesson tests the same small design: a registered 8-bit ALU (add, sub, and, or, xor) whose 9-bit result appears one clock after in_valid. All code was compiled and run in AMD Vivado xsim 2025.2, and the output shown is copied from those runs. The complete files are in the site's repository under scripts/verification.
The covergroup
// The inputs as the DUT sampled them, lined up with the result one clock later.
logic [2:0] op_q; logic [7:0] a_q, b_q;
always_ff @(posedge clk) if (in_valid) begin op_q <= op; a_q <= a; b_q <= b; end
covergroup alu_cg @(posedge clk iff out_valid);
cp_op: coverpoint op_q { bins add = {0}; bins sub = {1}; bins and_op = {2}; bins or_op = {3}; bins xor_op = {4}; }
cp_a: coverpoint a_q { bins zero = {0}; bins max = {255}; bins mid = {[1:254]}; }
cp_b: coverpoint b_q { bins zero = {0}; bins max = {255}; bins mid = {[1:254]}; }
cp_carry: coverpoint y[8] iff (op_q == 0) { bins no_carry = {0}; bins carry = {1}; }
op_x_a: cross cp_op, cp_a; // every operation with every kind of a
endgroup
It samples on each clock where out_valid is high, so the result and the inputs that produced it (registered into op_q, a_q, b_q) are lined up. cp_carry uses iff so it only counts additions. op_x_a crosses the five operations with the three kinds of a: 15 combinations.
What the run showed
| After | Total | cp_a | op × a cross |
|---|---|---|---|
| 200 plain random transactions | 82.7% | 100.0% | 46.7% |
| 200 more, with a = 0 and 255 at 10% each | 100.0% | 100.0% | 100.0% |
After 200 plain random transactions, cp_a was already 100%: a = 0 and a = 255 had each turned up at least once. But the cross was 46.7%, 7 of its 15 bins, because those corner values hadn't appeared with every operation. Only the cross showed that some operations had never been tested with a corner value of a. Steering a toward its corners with randcase closed every bin in the next 200 transactions.
Exercises
- E1. Add a coverpoint for subtractions that wrap.
- E2. Why did cp_a reach 100% while the cross didn't?
- E3. Which bins would you add for b, and should b be crossed with a?
Solutions
- E1. cp_wrap: coverpoint y[8] iff (op_q == 1) { bins wraps = {1}; bins no_wrap = {0}; } A subtraction wraps exactly when a < b, which sets bit 8 of the 9-bit result.
- E2. cp_a only needs each kind of a once, with any operation. The cross needs each kind with each of the five operations.
- E3. The same zero, max and mid bins. Crossing a and b (9 combinations) checks cases such as 255 + 255 and 0 − 255, which are exactly where carry and wrap bugs hide.
Common mistakes
- Sampling on every clock instead of only when a transaction completes, which counts idle cycles.
- Sampling inputs and outputs from different transactions in a pipelined design.
- Treating 100% coverage as done when the plan itself is missing cases.
- Crossing everything with everything, which creates bins nobody cares about and hides the ones that matter.
Check your understanding
- Q1. What is the difference between code coverage and functional coverage? Answer: code coverage says which lines and branches ran; functional coverage says which specified scenarios were exercised.
- Q2. How many bins does op_x_a have? Answer: 15, five operations times three kinds of a.
- Q3. What does iff do on a coverpoint? Answer: it only samples when its condition is true, as cp_carry does for additions.
The course
Related
Last verified
Run in Vivado xsim 2025.2 on 14 November 2026; outputs copied from that run. Language rules checked against IEEE 1800-2023.