Functional Coverage: What Was Tested

Count the situations the plan asks for, not the lines of code. Cross two features into bins, read the empty ones, and aim new stimulus at each hole.

You Will Be Able To

  • Turn plan rows into coverage bins, and cross two features into a table of bins.
  • Leave out the pairs that cannot happen, and say why.
  • Read a hit table, find its holes, and choose a way to fill each one.
  • Explain how functional coverage differs from code coverage.
Lesson
B3.6
Time
22 min
Runs on
Your browser
Check
5 questions

Three hundred random ALU operations, three hundred passes. The hit table has eleven bins, and three are still zero. One of them is an addition whose result wraps to zero, the very place the ALU bug of the directed-tests lesson lived.

The Plan's Features as Bins Filling UpStep through. Each situation the plan asks for becomes a bin. Every checked transaction drops a count into its bin. An empty bin is a situation no test has reached yet.
  • Design
  • Storage
  • Control / FSM
  • Step
  • Data
The Plan's Features as Bins Filling UpStep through. Each situation the plan asks for becomes a bin. Every checked transaction drops a count into its bin. An empty bin is a situation no test has reached yet. design Plan rows each operation witheach kind of result storage Operations ADD, SUB, AND, XOR storage Result kinds ordinary, carry, zero,zero with carry storage Cross: 11 bins every legal pair ofthe two control Empty bins a situation neverreached step Aimed stimulus a new constraint or adirected case namesnamescrossed withcrossed withcount 0answered by
  1. design Plan rowseach operation with each kind of result
    • names → Operations
    • names → Result kinds
  2. storage OperationsADD, SUB, AND, XOR
    • crossed with → Cross: 11 bins
  3. storage Result kindsordinary, carry, zero, zero with carry
    • crossed with → Cross: 11 bins
  4. storage Cross: 11 binsevery legal pair of the two
    • count 0 → Empty bins
  5. control Empty binsa situation never reached
    • answered by → Aimed stimulus
  6. step Aimed stimulusa new constraint or a directed case
  1. Read the plan

    The plan names what must be tested: each operation, and each kind of result.

  2. Cross them

    Every legal pair becomes a bin. Some pairs cannot happen, such as an AND with a carry, and are left out.

  3. Find the holes

    After the run, a bin with a count of 0 is a situation no test reached.

  4. Aim at them

    A new constraint or a directed case fills the hole, and the run is repeated.

Read the diagram as text
  • Plan rows: each operation with each kind of result
  • Operations: ADD, SUB, AND, XOR
  • Result kinds: ordinary, carry, zero, zero with carry
  • Cross: 11 bins: every legal pair of the two
  • Empty bins: a situation never reached
  • Aimed stimulus: a new constraint or a directed case
  • Plan rows → Operations: names
  • Plan rows → Result kinds: names
  • Operations → Cross: 11 bins: crossed with
  • Result kinds → Cross: 11 bins: crossed with
  • Cross: 11 bins → Empty bins: count 0
  • Empty bins → Aimed stimulus: answered by

An idea drawing of the coverage model in the lab below.

Before You Start

Read Random and Constrained-Random Stimulus and Code Coverage: What Ran first. The ALU, its flags and its bug come from Directed Tests.

Five questions this page answers
Question
1What does functional coverage count, and who writes it?
2What is a cross, and why leave some of its pairs out?
3What does an empty bin mean?
4What can you do about a hole?
5How is functional coverage different from code coverage?

The Idea: A Record of What the Tests Reached

Code coverage, in the last lesson, counted the design's own lines. It could not count a rule the designer forgot. Functional coverage turns the question round. You read the plan, list the situations that must happen in the tests, and count how often each one did.

Each situation is a bin. "An ADD that carries" is a bin. "A SUB that gives zero" is a bin. Every checked transaction drops a count into the bins it matches. After the run, the table says exactly which situations your stimulus reached, and which it never did.

This matters most with random stimulus. You no longer choose the inputs, so you cannot know from the test what it did. Functional coverage is how a random bench reports its own reach. An empty bin is a hole: a situation the plan asked for and no test produced.

New Words

New words on this page
WordMeaning
Functional coverageCounts, written by you from the plan, of which situations the tests reached.
BinOne situation with its own count, such as "ADD with a carry".
Coverage modelThe full set of bins for a design, written from its plan.
CrossEvery pair of the values of two features, each pair its own bin.
HoleA bin with a count of 0: a situation no test reached.
SampleRecording one transaction into the bins it matches.

The Smallest Example: Build the Cross by Hand

The ALU has four operations and its result has two flags, carry and zero. Cross the operation with the pair of flags. Which pairs can happen?

Operations crossed with result kinds
ordinarycarryzerozero with carry
ADDbinbinbin (only 0 + 0)bin (sum is 256)
SUBbinbin (a borrow)bin (a = b)cannot happen
ANDbincannot happenbincannot happen
XORbincannot happenbin (a = b)cannot happen

AND and XOR never set the carry, and a borrow never leaves a zero result. So 16 pairs become 11 bins. A bin that cannot happen is left out, or it would stay empty forever and hide the real holes.

The Rules

  1. Rule 1: Bins come from the plan.

    Each bin is a situation the specification asks for. You write them; no tool can.

  2. Rule 2: Cross what interacts.

    A cross finds pairs no single bin would ask for, such as a carry and a zero together.

  3. Rule 3: Leave out what cannot happen.

    Impossible bins stay empty forever and hide the real holes. Say why each is left out.

  4. Rule 4: A hole means the stimulus never got there.

    Answer it with a new constraint, a directed case, or a written reason.

  5. Rule 5: Count only checked results.

    A bin filled by a transaction that nobody checked records reach, not correctness.

Sources for These Rules

Functional coverage as feedback to stimulus, finding holes, many seeds: Spear and Tumbush, sections 1.6 and 1.8.

Functional coverage comes from the specification and measures design intent; cross coverage: Mehta, ASIC/SoC Functional Design Verification, sections 7.1 and 7.8.

The Lab: Eleven Bins, Three Holes

The lab draws 300 random ALU operations from one seed. For each, it works out the expected flags with the reference model and drops one count into the matching bin. The bins are a plain table of counters.

hits is a 4 x 4 table of counters. {want[0], want[1]} joins the zero and carry flags into a number from 0 to 3.
want = model(op, a, b);                  // the expected y, c and z
hits[op][{want[0], want[1]}]++;           // bin: [operation][zero, carry]
Printed by the lab (Icarus Verilog version 12.0). A dash is a bin that cannot happen.
op   ordinary  carry  zero  zero+carry
ADD       22     46     0           0
SUB       33     32     0           -
AND       78      -     5           -
XOR       83      -     1           -
8 of 11 bins hit, 3 empty

Rule 4 at work: three holes, all in the zero column. ADD with zero, SUB with zero, and ADD with zero and carry. Random operands are almost never equal, and almost never sum to exactly 256. XOR with zero was hit once, by luck. The ALU bug from the directed-tests lesson lives in the last empty bin.

Now aim at the holes. In one draw in four, the lab sets b to make the result zero. It uses b = a for SUB and XOR, and b = 256 - a for ADD. The table becomes 10 of 11 bins hit, 1 empty. ADD with zero and carry now has 20 hits. One hole stays: ADD with a zero result and no carry, which needs a = b = 0.

Why Bins Are Written by Hand

Why write bins by hand when code coverage is free? Because only you know the plan. No tool could guess that "a sum of exactly 256" matters for this ALU. That knowledge is the whole value of the model.

Why count instead of just running longer? Because a longer run spends most of its time in bins that are already full. The table shows exactly where the stimulus is thin. Then a small, aimed change does more than millions of extra cycles.

Traps

The first trap is a model copied from the design. Someone makes one bin per state of the design's code. The model then repeats code coverage and adds nothing. Bins come from the plan.

The second trap is filling bins with unchecked traffic. A transaction is sampled whether or not the checker compared it. The table says 100 percent while half the results were never checked. Sample only what a check has looked at.

The third trap is the impossible bin. A bin that cannot happen stays at 0 forever. The team gets used to "one hole that is fine", and a real hole hides beside it. Leave impossible pairs out, and write down why.

Where This Goes Next

This lesson counted hits in plain code. The Coverage track teaches the language built for it, and how to close holes.

Where each idea is taught in depth
The idea from this pageWhere it is taught
What coverage measures, with covergroupsWhat Coverage Measures
Covergroups and coverpointsCovergroups and Coverpoints
Choosing binsChoosing Bins
Cross coverage, and leaving pairs outCross Coverage
Closing coverage holesClosing Holes

Try It

Run it: 300 random operations, each counted into its bin of the cross.

Change seed to another number. Do the same three bins stay empty?

Add the aimed line from the lesson after the draw of b. Predict the last line, then find the bin that stays empty.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: op ordinary carry zero zero+carry ADD 22 46 0 0 SUB 33 32 0 - AND 78 - 5 - XOR 83 - 1 - 8 of 11 bins hit, 3 empty

Check Yourself

Predict the outputIn one draw in four, the lab now sets b so the result is zero. What is the last line it prints?
// alu8: an 8-bit ALU. op: 0 ADD, 1 SUB, 2 AND, 3 XOR; 4 to 7 are reserved.
// c is the carry out of ADD or the borrow of SUB. z is 1 when y is 0.
module alu8 (input logic [2:0] op, input logic [7:0] a, b,
             output logic [7:0] y, output logic c, z);
  logic [8:0] r;                    // nine bits: the result and its carry
  always_comb begin
    case (op)
      3'd0: r = a + b;
      3'd1: r = a - b;
      3'd2: r = {1'b0, a & b};
      3'd3: r = {1'b0, a ^ b};
      default: r = 9'd0;
    endcase
  end
  assign y = r[7:0];
  assign c = r[8];
  assign z = (y == 8'd0);       // the zero flag
endmodule

module tb;
  logic [2:0] op; logic [7:0] a, b, y; logic c, z;
  int seed = 5;
  int unsigned r;
  int hits [4][4];                  // [operation][class]: the coverage model as plain counters
  string ops [4];
  alu8 dut (.op(op), .a(a), .b(b), .y(y), .c(c), .z(z));
  // The reference model: the answer the specification gives, worked out a second way.
  function automatic logic [9:0] model(input logic [2:0] op, input logic [7:0] a, b);
    logic [7:0] y; logic c;
    case (op)
      3'd0: begin y = a + b; c = (a + b) > 255; end
      3'd1: begin y = a - b; c = a < b; end
      3'd2: begin y = a & b; c = 0; end
      default: begin y = a ^ b; c = 0; end
    endcase
    return {y, c, y == 0};
  endfunction
  initial begin
    logic [9:0] want;
    ops[0] = "ADD"; ops[1] = "SUB"; ops[2] = "AND"; ops[3] = "XOR";
    r = $urandom(seed);
    for (int i = 0; i < 300; i++) begin
      op = $urandom_range(3, 0);
      a = $urandom;
      b = $urandom;
      if ($urandom_range(3, 0) == 0) b = (op == 0) ? 8'd0 - a : a;   // aim: equal operands, or a sum of 256
      want = model(op, a, b);
      hits[op][{want[0], want[1]}]++;       // class: {zero, carry}
      #1;
    end
    $display("op   ordinary  carry  zero  zero+carry");
    for (int o = 0; o < 4; o++)
      if (o == 0) $display("%s %8d %6d %5d %11d", ops[o], hits[o][0], hits[o][1], hits[o][2], hits[o][3]);
      else if (o == 1) $display("%s %8d %6d %5d %11s", ops[o], hits[o][0], hits[o][1], hits[o][2], "-");
      else $display("%s %8d %6s %5d %11s", ops[o], hits[o][0], "-", hits[o][2], "-");
    begin
      int filled;                   // eleven bins can happen; the dashes cannot
      filled = 0;
      for (int o = 0; o < 4; o++)
        for (int k = 0; k < 4; k++)
          if (hits[o][k] > 0) filled++;
      $display("%0d of 11 bins hit, %0d empty", filled, 11 - filled);
    end
  end
endmodule
ConceptWho writes the bins of functional coverage?
ConceptWhat does an empty bin tell you?
Concept300 random draws leave "ADD, zero with carry" empty. What is the best next step?
ConceptHow does functional coverage differ from code coverage?

Common Mistakes

Wrong Write bins by reading the design's code.

The model then repeats code coverage and misses what the plan asks for.

Fix Write each bin from a plan row.

Wrong Keep bins that cannot happen.

They stay empty forever and hide the real holes.

Fix Leave them out and record why.

Wrong Fix a hole by running more cycles.

Random draws keep landing in the bins that are already full.

Fix Aim at the hole with a constraint or a directed case.

Remember

  • Functional coverage counts the situations the plan asks for; you write its bins.
  • A cross pairs the values of two features; leave out the pairs that cannot happen.
  • An empty bin is a hole in the stimulus; answer it with aimed stimulus or a reason.
  • Code coverage counts the design's code; functional coverage counts the plan. A project needs both.

Practice

Report a problem with this page