Corner Cases and How to Think of Them

Find the inputs that break designs, on purpose. Test the ends of every range, two events in one cycle, back-to-back traffic, and reset at the worst moment.

You Will Be Able To

  • Find the number lines of a design and test at each end and one step past it.
  • List the moments when two events happen in one cycle, and test each.
  • Add corners in time: back-to-back traffic, reset during traffic and pointer wrap.
  • Explain why a corner bug often shows its first error far from its cause.
Lesson
B3.3
Time
22 min
Runs on
Your browser
Check
5 questions

A FIFO passed a thousand random cycles last week. Today a test pushes and pops in one clock with one entry waiting, and soon the FIFO says full with three inside. The bug was in one line all along, and random traffic had never stood on that spot.

A Number Line with Its Ends and Seams MarkedStep through. The entries a four-deep FIFO can hold run from 0 to 4. The ends of that line, and the moments when two things happen at once, are where bugs gather.
  • Storage
  • Control / FSM
  • Clock / reset
  • Data
  • Reset
A Number Line With Its Ends and Seams MarkedStep through. The entries a four-deep FIFO can hold run from 0 to 4. The ends of that line, and the moments when two things happen at once, are where bugs gather. storage 0 entries: empty a pop must be refused storage 1 to 3 entries push and pop bothtaken storage 4 entries: full a push must berefused control Push and poptogether at 0, in the middle, at4 clock reset Reset duringtraffic everything back toempty control Pointer wrap the fifth push reusesslot 0 pushpushcount staysback to empty
  1. storage 0 entries: emptya pop must be refused
    • push → 1 to 3 entries
  2. storage 1 to 3 entriespush and pop both taken
    • push → 4 entries: full
  3. storage 4 entries: fulla push must be refused
  4. control Push and pop togetherat 0, in the middle, at 4
    • count stays → 1 to 3 entries
  5. clock reset Reset during trafficeverything back to empty
    • back to empty → 0 entries: empty
  6. control Pointer wrapthe fifth push reuses slot 0
  1. Find the line

    Any count, length, address or level is a number line. For the FIFO it is the number of entries, 0 to 4.

  2. Mark the ends

    At 0 a pop must be refused; at 4 a push must be refused. Test at each end and one step past it.

  3. Mark the seams

    Two events in one cycle meet at a seam: push and pop together, at each end and in the middle.

  4. Add time

    Reset in the middle of traffic, and the pointer wrapping back to slot 0, are corners in time.

Read the diagram as text
  • 0 entries: empty: a pop must be refused
  • 1 to 3 entries: push and pop both taken
  • 4 entries: full: a push must be refused
  • Push and pop together: at 0, in the middle, at 4
  • Reset during traffic: everything back to empty
  • Pointer wrap: the fifth push reuses slot 0
  • 0 entries: empty → 1 to 3 entries: push
  • 1 to 3 entries → 4 entries: full: push
  • Push and pop together → 1 to 3 entries: count stays
  • Reset during traffic → 0 entries: empty: back to empty

An idea drawing of the FIFO's corners. The lab below drives seven of them.

Before You Start

Read Directed Tests and Random and Constrained-Random Stimulus first. A FIFO is a small memory that hands entries out in the order they came in. Start From the FIFO Rules shows one in full.

Five questions this page answers
Question
1Where do the corners of a design come from?
2Why do bugs gather at the ends of ranges and at seams?
3What does "two events in one cycle" mean for a FIFO?
4Which corners live in time rather than in values?
5Why can a corner bug fail far from the cycle that caused it?

The Idea: Bugs Live at the Edges

Designers write the common path first and test it most. The ends of a range get a special line of code each. One refuses the push when full, one refuses the pop when empty, one wraps the pointer. Each special line is a fresh chance for a mistake, and each runs rarely.

Random stimulus visits the middle of a range far more often than its ends. A FIFO four entries deep spends most random cycles holding one, two or three entries. A cycle that pushes and pops at once while exactly one entry waits may happen, or may not. So corners are not left to chance. You list them and drive them on purpose.

Listing them is a skill with a method. Find every number line in the design: a count, a length, an address, a level. Mark its ends. Then ask what happens when two events meet in one cycle, and what happens when time does something awkward.

New Words

New words on this page
WordMeaning
Corner caseAn input or moment at the edge of what a design handles: an end of a range, a seam, or awkward timing.
Boundary valueA value at an end of a range, such as 0 or 4 entries, or one step past it.
Simultaneous eventsTwo things a design must handle in the same clock cycle, such as a push and a pop.
Back-to-backOne request straight after another, with no idle cycle between them.
Pointer wrapA read or write position going from the last slot back to slot 0.
FIFOFirst in, first out: a small memory that hands entries out in arrival order.

The Smallest Example: The FIFO's Corner List

Here is the method applied to fifo4 by hand. A push is taken when it is not full, and a pop when it is not empty. Both may happen in one cycle.

Corners of a FIFO four entries deep
KindCornerWhat the specification says
Endpop when emptyrefused; still empty
Endpush when fullrefused; still full, nothing lost
Seampush and pop when emptypush taken, pop refused: 1 entry
Seampush and pop with 1 to 3 entriesboth taken: the count does not change
Seampush and pop when fullpop taken, push refused: 3 entries
Timefive pushes and pops in a rowthe pointers wrap; order is kept
Timereset with entries insideback to empty at once

The Rules

  1. Rule 1: Find every number line.

    Counts, lengths, addresses and levels each have a smallest and a largest value.

  2. Rule 2: Test at each end and one step past it.

    Exactly full, then one push more; exactly empty, then one pop more.

  3. Rule 3: Pair the events.

    For every two inputs that can arrive together, test them together, at each end and in the middle.

  4. Rule 4: Add time.

    Back-to-back traffic, reset during traffic and pointer wrap are corners too.

  5. Rule 5: Check every cycle, not only at the end.

    A corner bug may show its first wrong output several cycles after its cause.

Sources for These Rules

Randomizing beyond data, including delays, synchronization and protocol exceptions: Spear and Tumbush, section 1.7.

Boundary and simultaneous-event thinking is common practice in test design; no single source states it as these rules.

The Lab: Seven Corners of a FIFO

The lab drives fifo4 through seven corners, one or a few cycles each. The bench keeps a model: a queue holding exactly what the FIFO should hold. After every edge it compares full, empty and the oldest entry with the model.

One cycle of stimulus and its check. q is a queue: push_back adds at the end, pop_front removes the oldest, q[0] reads it.
task automatic step(input logic p, input logic g, input logic [7:0] d);
  @(negedge clk) begin push = p; pop = g; din = d; end
  will_push = p && q.size() < 4;     // the model follows the same rules
  will_pop  = g && q.size() > 0;
  @(posedge clk);
  if (will_pop) void'(q.pop_front());
  if (will_push) q.push_back(d);
  #1 if (full !== (q.size() == 4) || empty !== (q.size() == 0)
         || (q.size() > 0 && dout !== q[0])) errors++;
endtask
Printed by the lab with fifo4's planted bug (Icarus Verilog version 12.0). entries is the model's count; full and empty are the FIFO's.
push and pop when empty        entries=1 full=0 empty=0 errors so far=0
push and pop with one entry    entries=1 full=0 empty=0 errors so far=0
pushed to full                 entries=4 full=1 empty=0 errors so far=1
push when full (refused)       entries=4 full=1 empty=0 errors so far=1
push and pop when full         entries=3 full=0 empty=0 errors so far=1
popped to empty                entries=0 full=0 empty=1 errors so far=2
pop when empty (refused)       entries=0 full=0 empty=1 errors so far=2
corners done: 2 errors

Look at the second row. Push and pop with one entry: no error yet, because the flags still look right. The error appears in the next row. Rule 5 at work: the FIFO's own count went to 2 at the seam, so it says full after only three entries. A second error follows at the bottom of the line.

The bug is one line: when push and pop are both taken, the count adds one and forgets the pop. Fix it and every corner passes: corners done: 0 errors. Rule 3 found the cause; rule 5 caught its effect a few clocks later.

Why Corners Are Listed and Checked Every Cycle

Why not trust random stimulus to reach the corners? It does reach some, given enough cycles. But the rarest ones need a run of exact values. Think of four pushes in a row with no pop, then a push and a pop together. A corner list makes sure each is driven at least once, on purpose, in a test someone can read.

Why check every cycle instead of comparing a total at the end? Because the effect of a corner bug wanders. Here the cause sat in one cycle and the first visible error came later. A check that looks only at the end may even see nothing, if the damage cancels out by then.

Traps

The first trap is testing the end but not one step past it. A test fills the FIFO to exactly four entries and stops. It never tries the fifth push, so a FIFO that overwrites its oldest entry when full still passes.

The second trap is treating two inputs separately. A push test and a pop test each pass, so the team moves on. The cycle with both together was never run, and that is where this lab's bug lived.

The third trap is forgetting time. Reset is tested only at the start, when the FIFO is already empty. A reset that keeps old entries looks perfect there. Reset it again while it holds data, then check that the next pop is refused.

Where This Goes Next

Corners are where a plan earns its keep. These pages go further.

Where each idea is taught in depth
The idea from this pageWhere it is taught
Turning corners into checks that travel with the designAssertions: Checks That Travel With the Design
Counting whether each corner was really reachedFunctional Coverage: What Was Tested
The FIFO's boundaries in RTLImplement the FIFO and Its Boundaries
Reset, pointer wrap and stress on a FIFOReset, Wrap and Reproducible Stress
Queues in SystemVerilogQueues and Ordering

Try It

Run it: seven corners of fifo4, checked against a queue after every clock edge.

Fix the count in fifo4 so a push and a pop together leave it unchanged. Predict the last line.

Add a corner: reset while the FIFO holds three entries, then a pop. What should the model and the flags say?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: push and pop when empty entries=1 full=0 empty=0 errors so far=0 push and pop with one entry entries=1 full=0 empty=0 errors so far=0 pushed to full entries=4 full=1 empty=0 errors so far=1 push when full (refused) entries=4 full=1 empty=0 errors so far=1 push and pop when full entries=3 full=0 empty=0 errors so far=1 popped to empty entries=0 full=0 empty=1 errors so far=2 pop when empty (refused) entries=0 full=0 empty=1 errors so far=2 corners done: 2 errors

Check Yourself

Predict the outputThe count of fifo4 is fixed, so a push and a pop together leave it unchanged. What is the last line the lab prints?
// fifo4: a first-in first-out buffer, four entries deep. dout shows the oldest entry.
// A push is taken when the FIFO is not full; a pop is taken when it is not empty. Both may happen at once.
module fifo4 (input logic clk, rst, push, pop, input logic [7:0] din,
              output logic [7:0] dout, output logic full, empty);
  logic [7:0] mem [0:3];
  logic [1:0] wp, rp;               // write and read pointers
  logic [2:0] count;                // entries held
  wire do_push = push && !full, do_pop = pop && !empty;
  assign full  = (count == 4);
  assign empty = (count == 0);
  assign dout  = mem[rp];
  always_ff @(posedge clk)
    if (rst) begin wp <= 0; rp <= 0; count <= 0; end
    else begin
      if (do_push) begin mem[wp] <= din; wp <= wp + 1; end
      if (do_pop) rp <= rp + 1;
      count <= count + do_push - do_pop;
    end
endmodule

module tb;
  logic clk = 0, rst = 1, push = 0, pop = 0;
  logic [7:0] din = 0, dout;
  logic full, empty;
  logic [7:0] q [$];                // the model: a queue holding what the FIFO should hold
  int errors = 0;
  fifo4 dut (.clk(clk), .rst(rst), .push(push), .pop(pop), .din(din), .dout(dout), .full(full), .empty(empty));
  always #5 clk = ~clk;
  // One cycle of stimulus. The model takes the same push and pop by the same rules.
  task automatic step(input logic p, input logic g, input logic [7:0] d);
    logic will_push, will_pop;
    @(negedge clk) begin push = p; pop = g; din = d; end
    will_push = p && q.size() < 4;
    will_pop  = g && q.size() > 0;
    @(posedge clk);
    if (will_pop) void'(q.pop_front());
    if (will_push) q.push_back(d);
    #1 if (full !== (q.size() == 4) || empty !== (q.size() == 0) || (q.size() > 0 && dout !== q[0])) errors++;
  endtask
  task automatic corner(input string name);
    $display("%-30s entries=%0d full=%0d empty=%0d errors so far=%0d", name, q.size(), full, empty, errors);
  endtask
  initial begin
    @(negedge clk) rst = 0;
    step(1, 1, 8'h10);              corner("push and pop when empty");
    step(1, 1, 8'h11);              corner("push and pop with one entry");
    step(1, 0, 8'h12); step(1, 0, 8'h13); step(1, 0, 8'h14); corner("pushed to full");
    step(1, 0, 8'h15);              corner("push when full (refused)");
    step(1, 1, 8'h16);              corner("push and pop when full");
    step(0, 1, 0); step(0, 1, 0); step(0, 1, 0); corner("popped to empty");
    step(0, 1, 0);                  corner("pop when empty (refused)");
    $display("corners done: %0d errors", errors);
    $finish;
  end
endmodule
Conceptfifo4 is full. A push and a pop arrive in the same cycle. What does its specification say happens?
ConceptWhich of these is a corner in time rather than in value?
ConceptIn the lab, why did the first error appear after the push-and-pop corner rather than at it?
ConceptA test fills a FIFO to exactly full and then drains it. Which bug can still pass?

Common Mistakes

Wrong Fill to exactly full and stop.

The refusal of the next push is never tested.

Fix Test at each end and one step past it.

Wrong Test each input on its own.

Bugs gather where two inputs arrive in the same cycle.

Fix Pair the inputs, at each end and in the middle of the range.

Wrong Compare only the final state at the end of the test.

A corner bug can show and then hide, or show long after its cause.

Fix Check outputs after every cycle against a model.

Remember

  • Bugs gather at the ends of ranges and where two events meet in one cycle.
  • Find every number line, and test at each end and one step past it.
  • Corners in time count too: back-to-back traffic, reset during traffic, pointer wrap.
  • Check every cycle against a model; a corner bug may show its first error later.

Practice

Report a problem with this page