What a Debugger Needs from the Chip

A debugger looks like a window into the processor. It is a chain of four links, and every request waits for a reply from the far end.

You Will Be Able To

  • Name the four things a debugger asks of a chip, and the blocks that carry each request.
  • Say what each link of the debug chain does to a request.
  • Explain why a halt request is not a halted processor, and what to wait for.
Lesson
S8.1
Time
24 min
Runs on
Your browser
Check
5 questions

Your firmware should print a letter when a switch is pressed. Today the terminal stays blank. You click Stop in the debugger, and a moment later it shows you a register. Your laptop has no wire to that register.

Before You Start

You need A Processor in One Picture: registers and the PC. An instruction that uses the bus takes several clock edges.

You need From Board to Chip: the blocks of our chip and its pins.

Five questions this page answers
Question
1What four things does a debugger need from a chip?
2Which blocks carry a request from the laptop to the processor?
3What does each link of the chain do to the request?
4Why is a halt request not the same as a halted processor?
5A halt request arrives in the middle of an instruction. When does the reply come?

The Idea: Four Requests and a Chain

A debugger is a program on your laptop. It needs four things from the chip. It must stop the processor. It must look at registers and memory. It must change them. Then it must let the processor go on.

None of that state is on a pin. So the chip carries a block whose only job is to serve the debugger: the debug module. It sits beside the processor. It can ask the processor to stop, and it can reach the registers of a stopped processor.

The laptop reaches the debug module through a few pins, one bit at a time. So one click passes through a chain of five boxes. They are the debugger, a cable adapter, a collector of bits, the debug module and the processor. Each link hands the request to the next in a new form.

One thing follows from the chain, and it is the heart of this page. A request is not a result. The processor cannot stop in the middle of an instruction. It stops a little later, and only it knows when. Without that reply, the debugger would show you a register that is still changing.

New Words

New words on this page
WordMeaning
DebuggerA program on a laptop that stops a processor, shows its state, changes it and lets it run again.
Debug probeA small box between the laptop and a few pins of the chip that are kept for the debugger. It turns commands into serial bits. Called the probe on this page.
Debug transportA block inside the chip that collects serial bits from the debug pins into one request.
Debug moduleA block inside the chip that serves the debugger: it asks the processor to stop and reaches its registers.
HartShort for hardware thread: one processor, seen as one stream of instructions. Our chip has one hart.
x5One register of the processor. The labs of this module work on it.
Halt requestA wire from the debug module to the hart. 1 means: please stop.
HaltedA wire from the hart to the debug module. 1 means: I have stopped. It is the reply to a halt request.
Resume requestA wire from the debug module to the hart. A pulse on it means: go on.
Debug chainThe five boxes in a row: debugger, debug probe, debug transport, debug module, hart.

One Picture: The Debug Chain

From a Click on the Laptop to the Processor, and BackStep through the picture. It starts with a laptop that has no way in, and ends with a reply on the screen.
  • Control / FSM
  • Pads
  • Debug
  • Processor
From a Click on the Laptop to the Processor, and BackStep through the picture. It starts with a laptop that has no way in, and ends with a reply on the screen. component Debugger a program on yourlaptop component Debug probe a small box betweenthe laptop and thedebug pins component Debug transport inside the chip:collects serial bitsinto one request component Debug module inside the chip:serves the debugger component Hart the processor, as onestream ofinstructions a commandserial bits on a fewpinsone requesthalt requesthaltedone replyserial bitsa result
  1. component Debuggera program on your laptop
    • a command → Debug probe
  2. component Hartthe processor, as one stream of instructions
    • halted → Debug module
  3. component Debug moduleinside the chip: serves the debugger
    • halt request → Hart
    • one reply → Debug transport
  4. component Debug probea small box between the laptop and the debug pins
    • serial bits on a few pins → Debug transport
    • a result → Debugger
  5. component Debug transportinside the chip: collects serial bits into one request
    • one request → Debug module
    • serial bits → Debug probe
  1. No way in

    The debugger knows which register you want to see. The register is a few flip-flops deep inside the chip. No pin of the package shows it.

  2. A block that serves the debugger

    The chip carries a debug module beside the processor. It can ask the hart to stop, and it can reach the registers of a stopped hart.

  3. A few pins and a cable

    Pins cost money, so the way in is serial: a few pins, one bit at a time. The debug transport collects the bits. A debug probe drives the pins for the laptop.

  4. The request goes inward

    One click crosses four links. Its meaning stays the same. Only its form changes: a command, serial bits, one request, one wire.

  5. The reply comes outward

    Only the hart knows when it has stopped. Its answer crosses the same four links in the other direction. Until it arrives, the debugger knows nothing.

Read the diagram as text
  • Debugger: a program on your laptop
  • Debug probe: a small box between the laptop and the debug pins
  • Debug transport: inside the chip: collects serial bits into one request
  • Debug module: inside the chip: serves the debugger
  • Hart: the processor, as one stream of instructions
  • Debugger → Debug probe: a command
  • Debug probe → Debug transport: serial bits on a few pins
  • Debug transport → Debug module: one request
  • Debug module → Hart: halt request
  • Hart → Debug module: halted
  • Debug module → Debug transport: one reply
  • Debug transport → Debug probe: serial bits
  • Debug probe → Debugger: a result

The debug chain. The lab on this page has only the last two boxes: test code stands in for the first three.

The Smallest Example, Worked by Hand

Follow one click on Stop. Nothing is simulated yet. The first table shows the same request in each link.

One click on Stop, link by link
LinkWhat it receivesWhat it hands on
DebuggerYour click on StopA command for the probe
ProbeThe commandSerial bits on the debug pins
Debug transportThe bits, one at a timeOne whole request for the debug module
Debug moduleThe requestA 1 on the halt request wire
HartThe halt requestNothing yet: it is in the middle of an instruction

Now the clock. Say the hart is in a load that reads a slow device. The load needs four clock edges, e1 to e4. The halt request arrives after e2.

The request and the reply, edge by edge
After edgeHalt requestHaltedWhat the hart is doing
e100The load, first edge
e200The load, second edge
e310The load, third edge. It has seen the request and goes on.
e411The load has finished. Now the hart stops.

The request is 1 for two edges before the reply is 1. A debugger that reads the register after e3 reads it while the load is still filling it. After e4 the register is whole, and the reply says so.

The reply then goes back along the chain. It is a wire, then a status word, then serial bits, then a line on your screen.

The Rules

  1. Rule 1: The debugger never touches state directly.

    Every action is a request to the debug module: stop, look, change, go on.

  2. Rule 2: A request crosses every link of the chain, and so does its reply.

    A break in any link looks, on the laptop, like a chip that does not answer.

  3. Rule 3: A link changes the form of a request, never its meaning.

    A command, serial bits, one request, one wire.

  4. Rule 4: A hart stops only between two instructions.

    The instruction in progress finishes first, however long it takes.

  5. Rule 5: A request is not a result.

    The debugger waits for halted before it reads or changes anything.

  6. Rule 6: Registers can be reached only while the hart is halted.

    A resume request ends that, and the hart runs on from the next instruction.

How a halt lands, and how to run one instruction at a time, is the subject of Halt, Resume and Step. The debug pins have a page of their own.

Sources for These Rules

The names hart, debug module and debug transport, and rules 4 to 6, follow the RISC-V External Debug Support specification. That document is not in our reference library. These statements are well-known facts about it and were not checked against its text.

Registers, the PC and the fetch of an instruction: Harris and Harris, Digital Design and Computer Architecture, ARM edition, sections 6.2.2 and 7.3.1.

The four clock edges of the load, and the moment of the request, are numbers of our lab.

Now Run It

The lab at the end of this page has the last two boxes of the chain: a debug module and one hart. Test code plays the debugger. A row that starts with e shows the two wires after that clock edge.

Printed by the lab on Icarus. Replies are in hexadecimal.
read x5, hart running: reply=00000000
e1 halt request=0 halted=0
e2 halt request=0 halted=0
debugger: halt request sent
e3 halt request=1 halted=0
e4 halt request=1 halted=1
read status: reply=00000001
read x5: reply=00000041
read next pc: reply=00000004
e5 halt request=0 halted=1
write x5=00000042, then read x5: reply=00000042
debugger: resume request sent
e6 halt request=0 halted=0
read x5, hart running: reply=00000000

Rule 5 at work, in rows e3 and e4. After e3 the halt request is 1 and halted is still 0. After e4 both are 1: the load has finished.

Rule 6, in the first row and the last. The debugger asks for x5 while the hart runs, and gets 00000000. That is not the content of x5. After the halt the same question returns 00000041.

Rule 1 at work, in the three "read" rows after e4. Status is 1, x5 is 41 and the next pc is 4. Then the debugger writes 42 into x5 and reads it back. Last, a resume request: after e6 halted is 0 again.

This lab leaves things out. No processor runs here: a model only says how many edges an instruction takes. The probe and the debug transport are missing, so test code drives the debug module directly. And the 00000000 for a running hart is a choice of this model. A real debug module answers such a read with an error flag.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
A debug module in hardwareA small debug program that the processor runs itselfIt works when the program is broken, or when there is no program yet. It costs gates, pins and a door that must be locked.
A serial way in, on a few pinsA wide port with one pin for each bitPins are saved for the product. Each request takes many clock edges on the cable.
Stop between two instructionsStop the clock of the processor at onceRegisters and the PC agree, and no bus transfer is cut in half. The reply comes late by up to one instruction.

The Trap: A Register Read Too Early

Back to the blank terminal. You click Stop and look at the register that should hold the switch. It shows the old value. So you decide that the switch was never read, and you spend the morning on the switch.

The read was fine. Your script had sent the halt request and read the register in the same breath. The load was still waiting for its slow device. The fix was one line: wait for halted, then read.

A second trap hides in rule 4. A team once stopped the clock of the processor to "halt" it. The processor froze in the middle of a bus transfer. The device at the other end waited for an end that never came.

The third trap is in what a halt does not do. It stops one hart. Timers still count, a serial port still sends its last byte, and a transfer by another block still writes memory. The chip you look at is not frozen. Only its processor is.

At Scale

This page1 hart
A phone chipeight or more harts

This section looks ahead. One hart and one debug module become many, and the way in becomes something to guard.

  1. Many harts, one debug module

    The debug module holds a selector for the hart it talks to. A request to stop goes to the chosen hart, or to a group of them.

  2. Replies at different times

    Each hart stops at the end of its own instruction. Two harts asked together still answer at two moments.

  3. A door to lock

    Whoever holds the probe can read memory. The Debug Module on the Bus shows the lock.

  4. When stopping hides the fault

    Some faults vanish when the processor stops. Trace watches a program without stopping it.

Try It

Run the lab. Read it as two columns: what the debugger asked for, and what came back.

In the program, change the cost of the first instruction from 4 to 7. On which edge does halted become 1 now?

Change after_edge(2) to after_edge(4). The first instruction has already finished. Which instruction does the hart finish before it stops?

Names Printed by the Lab
NameMeaning
e1, e2, ...Rising edges of the clock, counted from the release of reset.
halt requestThe wire from the debug module to the hart. 1 means: please stop.
haltedThe wire from the hart to the debug module. 1 means: I have stopped.
replyThe word that the debug module hands back for a read.
statusA word of the debug module. Its bit 0 is a copy of halted.
x5One register of the processor.
next pcThe address of the instruction that the hart will run next.
RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: read x5, hart running: reply=00000000 e1 halt request=0 halted=0 e2 halt request=0 halted=0 debugger: halt request sent e3 halt request=1 halted=0 e4 halt request=1 halted=1 read status: reply=00000001 read x5: reply=00000041 read next pc: reply=00000004 e5 halt request=0 halted=1 write x5=00000042, then read x5: reply=00000042 debugger: resume request sent e6 halt request=0 halted=0 read x5, hart running: reply=00000000
Read included file: soc-s81-debug.sv
// Teaching model of a hart's debug boundary. A retiring instruction advances
// the model PC by four. The driver supplies its result; there is no ISA core.
module debug_boundary (
  input logic clk, reset_n, retire, halt_req, resume_req, step_req,
  input logic [31:0] result, input logic increment,
  input logic trigger_en,
  input logic [31:0] trigger_pc,
  input logic reg_write, select_pc,
  input logic [31:0] write_data,
  output logic halted,
  output logic [31:0] read_data
);
  logic [31:0] next_pc, x5;
  logic stepping;
  assign read_data = halted ? (select_pc ? next_pc : x5) : 32'b0;

  always_ff @(posedge clk) begin
    if (!reset_n) begin
      next_pc <= 0;
      x5 <= 0;
      halted <= 0;
      stepping <= 0;
    end else if (halted) begin
      if (reg_write && !select_pc)
        x5 <= write_data;
      if (resume_req || step_req) begin
        halted <= 0;
        stepping <= step_req;
      end
    end else if (trigger_en && next_pc == trigger_pc) begin
      halted <= 1;
    end else if (retire) begin
      next_pc <= next_pc + 4;
      x5 <= increment ? x5 + 1 : result;
      halted <= halt_req || stepping;
      stepping <= 0;
    end
  end
endmodule

// The teaching module uses word selectors, independent of the SoC address map.
// Its bench driver has already performed transport framing.
module debug_register_port (
  input logic halted, write_en,
  input logic [1:0] selector,
  input logic [31:0] hart_data,
  output logic select_pc, write_x5,
  output logic [31:0] reply
);
  always_comb begin
    select_pc = selector == 2;
    write_x5 = halted && write_en && selector == 1;
    case (selector)
      0: reply = {31'b0, halted};
      1, 2: reply = hart_data;
      default: reply = 0;
    endcase
  end
endmodule

Check Yourself

Predict the outputIn this run the first instruction needs six clock edges, e1 to e6. The halt request is sent after e1. The run ends when the hart has halted. What is the last row?
`timescale 1ns/1ns
module tb;
  logic clk = 0;
  always #5 clk = ~clk;                        // e1 is the rising edge at 15 ns, e2 the one at 25 ns

  // The wires between the debugger's side and the hart.
  bit reset_n, retire, increment;
  bit halt_req, resume_req, step_req, trigger_en, write_en;
  logic [1:0] selector = 0;                    // 0: status   1: register x5   2: next pc
  logic [31:0] result = 0, write_data = 0, trigger_pc = 0;
  wire select_pc, reg_write, halted;
  wire [31:0] hart_data, reply;
  debug_boundary hart(.read_data(hart_data), .*);
  debug_register_port dm(.write_x5(reg_write), .*);

  // The program. A model stands in for the processor: it only says how many
  // clock edges each instruction takes, and what the instruction leaves in x5.
  int cost [0:3];
  bit adds [0:3];
  logic [31:0] loads [0:3];
  initial begin
    cost[0] = 6; adds[0] = 0; loads[0] = 32'h41;   // at 00: load 41 into x5, 6 edges
    cost[1] = 1; adds[1] = 1; loads[1] = 32'h00;   // at 04: add 1 to x5, 1 edge
    cost[2] = 1; adds[2] = 1; loads[2] = 32'h00;   // at 08: add 1 to x5, 1 edge
    cost[3] = 1; adds[3] = 1; loads[3] = 32'h00;   // at 0c: add 1 to x5, 1 edge
  end
  int left = 0;
  wire [31:0] pc = hart.next_pc;
  always @(negedge clk) begin
    if (!reset_n || halted) begin retire = 0; left = 0; end
    else begin
      if (left == 0) left = cost[pc/4];        // start the instruction at pc
      left--;
      retire = (left == 0);                    // it finishes on the next rising edge
      increment = adds[pc/4];
      result = loads[pc/4];
    end
  end

  int edges = 0;
  always @(posedge clk) if (reset_n) begin
    edges++;
    #1 $display("e%0d halt request=%b halted=%b", edges, halt_req, halted);
  end
  task automatic after_edge(input int n);      // wait until edge n has been printed
    wait (edges == n); #2;
  endtask
  task automatic read(input logic [1:0] which, output logic [31:0] value);
    selector = which; #1; value = reply;
  endtask

  logic [31:0] got;
  initial begin
    #7 reset_n = 1;
    read(1, got);
    $display("read x5, hart running: reply=%08h", got);
    after_edge(1);
    halt_req = 1;
    $display("debugger: halt request sent");
    wait (halted); #2;
    halt_req = 0;
    $finish;
    read(0, got); $display("read status: reply=%08h", got);
    read(1, got); $display("read x5: reply=%08h", got);
    read(2, got); $display("read next pc: reply=%08h", got);
    write_data = 32'h42; selector = 1; write_en = 1;
    @(posedge clk); #2; write_en = 0;
    read(1, got); $display("write x5=00000042, then read x5: reply=%08h", got);
    resume_req = 1;
    $display("debugger: resume request sent");
    @(posedge clk); #2; resume_req = 0;
    read(1, got); $display("read x5, hart running: reply=%08h", got);
    $finish;
  end
endmodule
Read included file: soc-s81-debug.sv
// Teaching model of a hart's debug boundary. A retiring instruction advances
// the model PC by four. The driver supplies its result; there is no ISA core.
module debug_boundary (
  input logic clk, reset_n, retire, halt_req, resume_req, step_req,
  input logic [31:0] result, input logic increment,
  input logic trigger_en,
  input logic [31:0] trigger_pc,
  input logic reg_write, select_pc,
  input logic [31:0] write_data,
  output logic halted,
  output logic [31:0] read_data
);
  logic [31:0] next_pc, x5;
  logic stepping;
  assign read_data = halted ? (select_pc ? next_pc : x5) : 32'b0;

  always_ff @(posedge clk) begin
    if (!reset_n) begin
      next_pc <= 0;
      x5 <= 0;
      halted <= 0;
      stepping <= 0;
    end else if (halted) begin
      if (reg_write && !select_pc)
        x5 <= write_data;
      if (resume_req || step_req) begin
        halted <= 0;
        stepping <= step_req;
      end
    end else if (trigger_en && next_pc == trigger_pc) begin
      halted <= 1;
    end else if (retire) begin
      next_pc <= next_pc + 4;
      x5 <= increment ? x5 + 1 : result;
      halted <= halt_req || stepping;
      stepping <= 0;
    end
  end
endmodule

// The teaching module uses word selectors, independent of the SoC address map.
// Its bench driver has already performed transport framing.
module debug_register_port (
  input logic halted, write_en,
  input logic [1:0] selector,
  input logic [31:0] hart_data,
  output logic select_pc, write_x5,
  output logic [31:0] reply
);
  always_comb begin
    select_pc = selector == 2;
    write_x5 = halted && write_en && selector == 1;
    case (selector)
      0: reply = {31'b0, halted};
      1, 2: reply = hart_data;
      default: reply = 0;
    endcase
  end
endmodule
ConceptWhat four things does a debugger need from a chip?
ConceptWhich block inside the chip collects serial bits into one request?
ConceptWhat does a link of the chain change in a request?
ConceptWhy is a halt request not the same as a halted processor?

Common Mistakes

Wrong Read a register as soon as the halt request is sent.

The instruction in progress may still be changing it.

Fix Wait for halted, then read.

Wrong Stop the clock of the processor to halt it.

A bus transfer can be cut in half.

Fix Let the hart stop itself between two instructions.

Wrong Treat a halted hart as a frozen chip.

Timers, serial ports and other bus managers keep running.

Fix Ask what else can change the state you are looking at.

Remember

  • A debugger needs four things: stop, look, change, go on. All four are requests to the debug module.
  • A request crosses the debug chain: debugger, probe, debug transport, debug module, hart. The reply crosses it back.
  • A request is not a result. The hart stops between two instructions and then says halted.
  • A halted hart is not a frozen chip.

Practice

Report a problem with this page