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.
- Storage
- Control / FSM
- Clock / reset
- Data
- Reset
- 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.
- 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.
- Mark the seams
Two events in one cycle meet at a seam: push and pop together, at each end and in the middle.
- 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.
| Question | |
|---|---|
| 1 | Where do the corners of a design come from? |
| 2 | Why do bugs gather at the ends of ranges and at seams? |
| 3 | What does "two events in one cycle" mean for a FIFO? |
| 4 | Which corners live in time rather than in values? |
| 5 | Why 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
| Word | Meaning |
|---|---|
| Corner case | An input or moment at the edge of what a design handles: an end of a range, a seam, or awkward timing. |
| Boundary value | A value at an end of a range, such as 0 or 4 entries, or one step past it. |
| Simultaneous events | Two things a design must handle in the same clock cycle, such as a push and a pop. |
| Back-to-back | One request straight after another, with no idle cycle between them. |
| Pointer wrap | A read or write position going from the last slot back to slot 0. |
| FIFO | First 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.
| Kind | Corner | What the specification says |
|---|---|---|
| End | pop when empty | refused; still empty |
| End | push when full | refused; still full, nothing lost |
| Seam | push and pop when empty | push taken, pop refused: 1 entry |
| Seam | push and pop with 1 to 3 entries | both taken: the count does not change |
| Seam | push and pop when full | pop taken, push refused: 3 entries |
| Time | five pushes and pops in a row | the pointers wrap; order is kept |
| Time | reset with entries inside | back to empty at once |
The Rules
Rule 1: Find every number line.
Counts, lengths, addresses and levels each have a smallest and a largest value.
Rule 2: Test at each end and one step past it.
Exactly full, then one push more; exactly empty, then one pop more.
Rule 3: Pair the events.
For every two inputs that can arrive together, test them together, at each end and in the middle.
Rule 4: Add time.
Back-to-back traffic, reset during traffic and pointer wrap are corners too.
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.
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++;
endtaskpush 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 errorsLook 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.
| The idea from this page | Where it is taught |
|---|---|
| Turning corners into checks that travel with the design | Assertions: Checks That Travel With the Design |
| Counting whether each corner was really reached | Functional Coverage: What Was Tested |
| The FIFO's boundaries in RTL | Implement the FIFO and Its Boundaries |
| Reset, pointer wrap and stress on a FIFO | Reset, Wrap and Reproducible Stress |
| Queues in SystemVerilog | Queues 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?
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 errorsCheck Yourself
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.