Debugging: From a Failure to a Cause

Walk back from the first wrong output to the first wrong signal. Read the log before the waves, find the earliest difference, and cut the search in half by stimulus and by time.

You Will Be Able To

  • Start from the log: the first error, its time and its values.
  • Walk back from an output to the signal that first went wrong, one step at a time.
  • Find the earliest difference between what happened and what the specification says.
  • Cut a search in half: shorter stimulus, earlier time.
Lesson
B4.4
Time
24 min
Runs on
Your browser
Check
5 questions

Four bytes went out and three came back right. The last one, c3, arrived as 00. Somewhere in a few hundred clocks one signal went wrong first, and every later error just followed it.

The Path Back from the First Wrong Output to the First Wrong SignalStep through. Debugging walks backwards. It goes from the wrong byte in the log to the wire, then the register, then the clock where it went wrong.
  • Result
  • I/O
  • Storage
  • Control / FSM
  • Driver
  • Data
The Path Back From the First Wrong Output to the First Wrong SignalStep through. Debugging walks backwards. It goes from the wrong byte in the log to the wire, then the register, then the clock where it went wrong. result Log: byte 3 wrong got 00, want c3 io tx wire data bits all 0 storage frame register holds 200, shouldhold 386 control The clock thatfilled it one clock after start driver data input already 00 by then decoded by thereceivershifted outfilledread too late
  1. result Log: byte 3 wronggot 00, want c3
  2. io tx wiredata bits all 0
    • decoded by the receiver → Log: byte 3 wrong
  3. storage frame registerholds 200, should hold 386
    • shifted out → tx wire
  4. control The clock that filled itone clock after start
    • filled → frame register
  5. driver data inputalready 00 by then
    • read too late → The clock that filled it
  1. Start at the log

    The first wrong output: byte 3 arrived as 00 instead of c3.

  2. Back to the wire

    The receiver decoded what tx carried. The data bits on tx were all 0.

  3. Back to the register

    tx shows frame[0], so the wrong bits were in frame.

  4. Back to the clock

    frame was filled one clock after start, when data had already changed to 00.

Read the diagram as text
  • Log: byte 3 wrong: got 00, want c3
  • tx wire: data bits all 0
  • frame register: holds 200, should hold 386
  • The clock that filled it: one clock after start
  • data input: already 00 by then
  • tx wire → Log: byte 3 wrong: decoded by the receiver
  • frame register → tx wire: shifted out
  • The clock that filled it → frame register: filled
  • data input → The clock that filled it: read too late

An idea drawing of the walk in the lab below. The hex values are from the recorded run.

Before You Start

Read How a Simulator Thinks and Regressions, Seeds and the Life of a Bug first. The transmitter is the one from The Verification Plan.

Five questions this page answers
Question
1Why read the log before the waveform?
2What does it mean to walk back from an output?
3What is the earliest difference, and why look for it?
4How do you cut a search in half?
5When is a bug found, and when is it only described?

The Idea: Every Error Has a First Wrong Signal

A failure in a log is the end of a chain. Some signal went wrong first. Then the signals it drives went wrong, and so on, until something reached the bench's checks. Debugging means walking that chain backwards, from the error you see to the signal that started it.

The log comes first, because it is fast to read and says where to look. Which check failed, at what time, with what values? Then the waveform, but only around that time and only for the signals on the path. Ask of each signal: is this what the specification says it should be, right now? The first one that is not is the earliest difference.

When the search is too wide, cut it in half. Make the stimulus shorter until the failure barely survives. Then look at half the time, then half again. Each cut halves what is left to read.

New Words

New words on this page
WordMeaning
SymptomWhat the bench sees: a wrong value, a missing answer, a timeout.
Root causeThe line of code, or the missing line, that made the first wrong signal.
Earliest differenceThe first moment a signal differs from what the specification says it should be.
Walk backGoing from a wrong signal to the signals that drive it, one step at a time.
BisectCutting a search in half, again and again: half the stimulus, or half the time.

The Smallest Example: The Path, Worked by Hand

A frame is ten bits: a start bit 0, eight data bits low bit first, a stop bit 1. As a register, frame = {1, data, 0}. For data = c3, that is the ten bits 1 11000011 0, which is 386 in hex. Work out what frame would hold if data were 00.

frame for two values of data
dataframe in bitsframe in hex
c31 11000011 0386
001 00000000 0200

So a frame of 200 means the transmitter sent the byte 00 with a correct start and stop bit. The question becomes: why did frame get data = 00 instead of c3?

The Rules

  1. Rule 1: Read the log first.

    The first error, its time and its values tell you where and when to look.

  2. Rule 2: Walk back one step at a time.

    From the wrong output, to the signal that drives it, to the signal that drives that.

  3. Rule 3: Find the earliest difference.

    Compare each signal with the specification, not with your guess. The first one that differs is where the bug acts.

  4. Rule 4: Bisect when the search is wide.

    Shorten the stimulus until the failure barely survives; halve the time window each step.

  5. Rule 5: A cause explains every symptom.

    Before you fix, check the cause explains why the other bytes passed too.

Sources for These Rules

Debugging from the first failing check backwards is common practice; the walk and the rules are stated in this track's own words.

Reading what a flip-flop sampled at an edge: see the previous lesson and IEEE 1800-2023, clause 4.

The Lab: Byte 3 Arrived as 00

The lab sends four bytes. For the last one, the bench tidies data back to 00 straight after the start clock, which the specification allows. It also prints one row per clock around that start:

Printed by the lab (Icarus Verilog version 12.0).
e126 start=0 data=21 busy=0 frame=3ff tx=1
e127 start=1 data=c3 busy=0 frame=3ff tx=1
e128 start=0 data=00 busy=1 frame=3ff tx=1
e129 start=0 data=00 busy=1 frame=200 tx=0
e130 start=0 data=00 busy=1 frame=200 tx=0
e131 start=0 data=00 busy=1 frame=200 tx=0
e132 start=0 data=00 busy=1 frame=300 tx=0
byte 0: 48 ok
byte 1: 69 ok
byte 2: 21 ok
byte 3: got 00, want c3

Rule 1 at work: the log says byte 3 is wrong, and the other three are right. The rows show the clocks around the last start, so the walk can begin there.

Around the Last Start, One Column per Clock
0123456clkstartdata21c300busyframe3ff200300tx
Scroll horizontally ↔
  1. Cycle 0

    Clock 126: the transmitter is idle; frame holds all ones.

  2. Cycle 1

    Clock 127: start is 1 with data = c3. The specification says data is read in this clock.

  3. Cycle 2

    Clock 128: busy is 1 and the bench has tidied data to 00. frame still holds 3ff: it was not filled at start.

  4. Cycle 3

    Clock 129: frame becomes 200, which is a stop bit, eight zeros and a start bit. The first wrong signal.

Recorded rows, sampled at each rising edge (Icarus Verilog version 12.0). The correct transmitter holds frame = 386 at clock 128.

Rule 3 at work. At clock 127, start is 1 and data is c3, the clock the specification says data is read. At clock 128 frame should hold 386, and it still holds 3ff. That is the earliest difference. At clock 129 frame fills with 200, read from data after the bench changed it.

Now the code. The transmitter does not fill frame at start. It sets a flag and fills frame on the next clock.

The design under test, near the start. {1'b1, data, 1'b0} joins a stop bit, the byte and a start bit.
else if (!busy) begin
  if (start) begin load <= 1; bits <= 10; ticks <= 0; busy <= 1; end
end else begin
  if (load) begin frame <= {1'b1, data, 1'b0}; load <= 0; end   // one clock late
  ...

Rule 5 at work: does this explain the passing bytes too? Yes. For bytes 0 to 2 the bench held data steady after start, so reading it one clock late gave the same value. Only the tidy bench exposed it, and only on the last byte. The Verification Plan flagged that gap in the plan.

Why Walk Back to the Earliest Difference

Why walk back instead of reading the design from the top? Because a design has thousands of signals and the failure touches a handful. Walking back from the symptom only ever visits signals on the path, so the search stays small however big the design.

Why insist on the earliest difference? Because every later difference is a consequence. Fixing a consequence, such as patching the receiver to accept the byte, hides the bug and leaves the cause in place.

Traps

The first trap is debugging in the waveform first. A full waveform of a long run is a wall of signals, and the eye drifts to whatever looks odd. The log narrows it to one time and a few signals. Open the waves second.

The second trap is comparing with your guess instead of the specification. You expect frame to change at clock 129, it does, and you move on. The specification says it should already hold the byte at clock 128. Write the expected value down from the specification before you look.

The third trap is stopping at the first plausible story. "The bench changed data too early" sounds right, and blames the bench. But the specification says data is read in the start clock, so the bench is allowed to change it next. Check the cause against the specification before you assign it.

Where This Goes Next

Debugging is learned by doing. These pages give you more cases to walk back.

Where each idea is taught in depth
The idea from this pageWhere it is taught
An unknown that became a zeroDebug Case: The X That Became Zero
A race between a monitor and the designDebug Case: A Racing Monitor
Debugging a failing assertionDebugging a Failing Assertion
Reading a formal counterexampleReading a Counterexample
The toolbox around a designThe Toolbox and the Map of This Site

Try It

Run it: four bytes, the last with data tidied to 00 straight after start, and rows around the last start.

Fix uart_tx so frame is filled in the start clock itself. Predict the last line.

Keep the bug, and make the bench hold data at c3 for two more clocks after start. Does byte 3 still fail?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: e126 start=0 data=21 busy=0 frame=3ff tx=1 e127 start=1 data=c3 busy=0 frame=3ff tx=1 e128 start=0 data=00 busy=1 frame=3ff tx=1 e129 start=0 data=00 busy=1 frame=200 tx=0 e130 start=0 data=00 busy=1 frame=200 tx=0 e131 start=0 data=00 busy=1 frame=200 tx=0 e132 start=0 data=00 busy=1 frame=300 tx=0 byte 0: 48 ok byte 1: 69 ok byte 2: 21 ok byte 3: got 00, want c3

Check Yourself

Predict the outputThe transmitter now fills frame in the start clock itself. What is the last line the lab prints?
// uart_tx: sends one byte as a frame: a start bit (0), eight data bits low bit first, a stop bit (1).
// Each bit lasts CPB clocks. start is taken only while busy is 0; data is read in the same clock.
module uart_tx #(parameter int CPB = 4) (input logic clk, rst, start, input logic [7:0] data,
                                          output logic tx, busy);
  logic [9:0] frame;                // the bits still to send, low bit first
  logic [3:0] bits;                 // how many bits are left
  logic [7:0] ticks;                // clocks spent in this bit
  assign tx = busy ? frame[0] : 1'b1;
  always_ff @(posedge clk)
    if (rst) begin busy <= 0; bits <= 0; ticks <= 0; frame <= '1; end
    else if (!busy) begin
      if (start) begin frame <= {1'b1, data, 1'b0}; bits <= 10; ticks <= 0; busy <= 1; end
    end else begin
      if (ticks == CPB - 1) begin
        ticks <= 0;
        frame <= {1'b1, frame[9:1]};
        bits <= bits - 1;
        if (bits == 1) busy <= 0;
      end else ticks <= ticks + 1;
    end
endmodule

module tb;
  logic clk = 0, rst = 1, start = 0, tx, busy;
  logic [7:0] data = 0;
  uart_tx #(.CPB(4)) dut (.clk(clk), .rst(rst), .start(start), .data(data), .tx(tx), .busy(busy));
  always #5 clk = ~clk;
  // The far end of the wire: wait for a falling edge, then sample each bit in its middle.
  logic [7:0] got [$];              // bytes received, in order
  int frame_errors = 0;
  initial forever begin
    logic [7:0] byte_in;
    @(negedge tx);
    repeat (2) @(posedge clk);      // the middle of the start bit
    if (tx === 1'b0) begin
      for (int i = 0; i < 8; i++) begin repeat (4) @(posedge clk); byte_in[i] = tx; end
      repeat (4) @(posedge clk);    // the middle of the stop bit
      if (tx !== 1'b1) frame_errors++;
      got.push_back(byte_in);
    end
  end
  // Send one byte: wait until the transmitter is free, then pulse start for one clock.
  task automatic send(input logic [7:0] d);
    @(negedge clk); while (busy) @(negedge clk);
    data = d; start = 1;
    @(negedge clk) start = 0;
  endtask
  int cycle = 0, watch = 0;
  logic [7:0] want [4];
  // One row per clock around the last start, to read like a waveform.
  always @(posedge clk) begin
    cycle++;
    if (watch > 0) begin
      watch--;
      $display("e%0d start=%b data=%h busy=%b frame=%h tx=%b", cycle, start, data, busy, dut.frame, tx);
    end
  end
  initial begin
    want[0] = 8'h48; want[1] = 8'h69; want[2] = 8'h21; want[3] = 8'hC3;
    @(negedge clk) rst = 0;
    for (int k = 0; k < 3; k++) send(want[k]);
    // The last byte: this bench tidies data back to 0 straight after the start clock.
    @(negedge clk); while (busy) @(negedge clk);
    watch = 7;
    @(negedge clk) begin data = want[3]; start = 1; end
    @(negedge clk) begin start = 0; data = 8'h00; end
    wait (!busy); repeat (8) @(negedge clk);
    for (int k = 0; k < 4; k++)
      if (got[k] === want[k]) $display("byte %0d: %h ok", k, got[k]);
      else $display("byte %0d: got %h, want %h", k, got[k], want[k]);
    $finish;
  end
endmodule
ConceptWhy read the log before the waveform?
ConceptIn the lab, what is the earliest difference?
ConceptBytes 0 to 2 passed with the same bug. Why?
ConceptA failure needs 4,000 clocks of stimulus. What does bisecting mean here?

Common Mistakes

Wrong Open the full waveform first.

Thousands of signals hide the few on the path.

Fix Read the log first; open the waves only around the first error.

Wrong Fix the first difference you notice.

It may be a consequence; the cause stays and returns.

Fix Walk back to the earliest difference and fix that.

Wrong Compare signals with what you expect.

Your expectation may share the bug.

Fix Compare with the specification, written down before you look.

Remember

  • Every failure has a first wrong signal; debugging walks back to it.
  • Read the log first, then the waves around the first error, for the signals on the path.
  • Compare each signal with the specification; the earliest difference is where the bug acts.
  • Bisect stimulus and time when the search is wide; a true cause explains the passes too.

Practice

Report a problem with this page