Halt, Resume and Step
A halt lands between two instructions. A step is one instruction, however long it takes. A trigger stops the hart at an address without touching the program.
You Will Be Able To
- Say where a halt stops a hart, and why its registers and its next pc agree.
- Count a single step in instructions, and predict what the hart shows after it.
- Explain how a trigger stops a hart at an address, and why it works on code in ROM.
- Lesson
- S8.9
- Time
- 25 min
- Runs on
- Your browser
- Check
- 5 questions
The hart has halted and the debugger shows a register and an address. You click Step once. The address moves on by four, and three clock edges have passed. One click, one instruction, three edges: which of those did you ask for?
Before You Start
You need What a Debugger Needs From the Chip: the debug module, the hart, a halt request and its reply.
You need A Processor in One Picture: the PC, and an instruction that takes several clock edges when it uses the bus.
| Question | |
|---|---|
| 1 | Where exactly does a halt stop a hart? |
| 2 | Which two things does a halted hart hand over, and why must they agree? |
| 3 | How long is a single step? |
| 4 | When does a trigger stop the hart: before or after the instruction at its address? |
| 5 | A program, a halt, a step and a trigger: what does the hart show at each stop? |
The Idea: Stop on a Boundary
An instruction changes the state of a processor in several small moves. A load, for one, sends an address, waits for the device, then writes a register. Stop in the middle and the register holds half an answer.
So a hart stops only on an instruction boundary: the point where one instruction has finished and the next has not started. At that point two things agree. The registers hold the result of every instruction so far. And one address, the next pc, names the instruction that comes next.
From that one idea come the three tools of run control. A halt stops the hart on the next boundary. A single step lets it run to the boundary after that, which is one instruction later. A trigger picks a boundary by its address: the hart stops just before the instruction at that address.
Without boundaries a debugger would show you states that no program ever had. With them, every stop is a state your program really passed through.
New Words
| Word | Meaning |
|---|---|
| Run control | The requests that move a hart between running and halted: halt, resume and step. |
| Instruction boundary | The point between two instructions: one has finished, the next has not started. |
| Saved next pc | The address of the instruction that a halted hart will run next. The hart saves it when it halts. This page calls it the next pc. |
| Single step | A request to run one instruction and halt again. This page calls it a step. |
| Trigger | A comparator in the hart that holds one address. It halts the hart when the next pc equals that address. |
| Breakpoint | A place in a program where you want the hart to stop. A trigger is one way to make one. |
One Picture: Running and Halted
- Processor
- Debug
- Control / FSM
- Memory
- Two states
A hart is running or halted. While it runs, the debugger sees nothing of it. While it is halted, the debugger can read and change its registers.
- Halt
A halt request does not act at once. The hart finishes the instruction in progress and does not start the next one. That point is an instruction boundary.
- Resume
A resume request sends the hart back to its program. It starts with the instruction that the halt left out.
- Step
A step request lets the hart run one instruction. Then the hart halts again by itself. The instruction may take one clock edge or twenty.
- Trigger
A trigger holds one address. When the next instruction is at that address, the hart halts before it runs. Nobody has to send a halt request.
Read the diagram as text
- Running: the hart runs its program
- Halted: the hart waits; the debugger can look and change
- One instruction: the hart runs exactly one instruction
- Trigger: a comparator that holds one address
- Running → Halted: halt request: at the next boundary
- Halted → Running: resume request
- Halted → One instruction: step request
- One instruction → Halted: when it has finished
- Running → Trigger: next pc
- Trigger → Halted: on a match
Run control. Every arrow into "Halted" ends on an instruction boundary.
The Smallest Example, Worked by Hand
Here is a program of six instructions. No simulator yet. Each instruction needs a known number of clock edges. A load is slow because it waits for a device.
| Address | Instruction | Clock edges |
|---|---|---|
| 00 | Load 41 into x5 | 4 |
| 04 | Add 1 to x5 | 1 |
| 08 | Load 60 into x5 | 3 |
| 0c | Add 1 to x5 | 1 |
| 10 | Add 1 to x5 | 1 |
| 14 | Add 1 to x5 | 1 |
The hart starts at 00 on edge e1. A halt request arrives after e2. Then come two steps. Last, a trigger is set at 14 and the hart is resumed. One rule of our hart is needed. A step request or a resume request takes one edge. The instruction starts on the edge after it.
| What the debugger does | What the hart runs | Edges | Halted after | Next pc | x5 |
|---|---|---|---|---|---|
| Halt request after e2 | The rest of the load at 00 | e3, e4 | e4 | 04 | 41 |
| Step | The add at 04 | e5 for the request, e6 | e6 | 08 | 42 |
| Step | The load at 08 | e7 for the request, e8 to e10 | e10 | 0c | 60 |
| Trigger at 14, resume | The adds at 0c and 10 | e11 for the request, e12, e13 | e14 | 14 | 62 |
Read the first row. The halt was asked after e2 and landed after e4. The pair agrees: x5 holds what the load at 00 put there, and the next pc names the instruction after it.
Read the two steps. The first runs one instruction in one edge. The second runs one instruction in three edges. Both are one step.
Read the last row. The hart runs 0c and 10. Then the next pc is 14, the trigger matches on the edge after, and the hart halts. x5 is 62, not 63: the add at 14 has not run.
The Rules
Rule 1: A halt lands on an instruction boundary.
The instruction in progress finishes. The next one does not start.
Rule 2: A halted hart hands over a pair that agrees.
Its registers, and the next pc. Both belong to the same boundary.
Rule 3: A resume starts at the next pc.
If the debugger changed a register, the program goes on with the new value.
Rule 4: A step is one instruction, not one clock edge.
The hart runs one instruction and halts again by itself.
Rule 5: A trigger halts the hart before the instruction at its address.
It compares the next pc with the address it holds.
Rule 6: A trigger does not change the program.
So it works on code in a ROM. The other kind of breakpoint replaces an instruction, and needs memory that can be written.
Sources for These Rules
Rules 1 to 5 follow the RISC-V External Debug Support specification. There the saved address is a register named dpc. The step request is a bit of a register named dcsr. That document is not in our reference library. These are well-known facts about it and were not checked against its text.
A breakpoint made by replacing an instruction with a break instruction: Patterson and Hennessy, Computer Organization and Design, ARM edition, section 5.14.
The program, its edge counts, and the one edge that takes a step or a resume request are choices of our lab.
Now Run It
The lab at the end of this page runs that program on a model of one hart. Test code plays the debugger. Each row that starts with e shows the hart after that clock edge.
e1 running pc=00 x5=00000000
e2 running pc=00 x5=00000000
halt requested
e3 running pc=00 x5=00000000
e4 halted pc=04 x5=00000041
step requested
e5 running pc=04 x5=00000041
e6 halted pc=08 x5=00000042
step requested
e7 running pc=08 x5=00000042
e8 running pc=08 x5=00000042
e9 running pc=08 x5=00000042
e10 halted pc=0c x5=00000060
trigger set at 14, resume requested
e11 running pc=0c x5=00000060
e12 running pc=10 x5=00000061
e13 running pc=14 x5=00000062
e14 halted pc=14 x5=00000062Rule 1 at work, in rows e3 and e4. The halt is asked after e2. After e3 the hart still runs. After e4 the load is done and the hart is halted.
Rule 2, in row e4: next pc 04 and x5 at 41. They belong to the same boundary.
Rule 4, in the two steps. The first ends after e6. The second ends after e10: rows e8 and e9 show the hart still inside the load at 08.
Rule 5, in the last two rows. After e13 the next pc is 14 and the hart still runs. After e14 it is halted, with x5 at 62. The add at 14 did not run.
This lab leaves things out. No processor runs here: a model says how many edges each instruction takes and what it leaves in x5. Test code drives the debug module directly, with no probe and no serial pins. The single edge that takes a request is a choice of this model.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Save the next pc | Save the address of the last instruction | Resume knows where to go, even after a jump. One more register in the hart. |
| The hart counts the one instruction of a step | The debugger sends resume, then halt, as fast as it can | A step is exact. Over a serial cable the debugger is thousands of instructions too slow. |
| A trigger: a comparator on the next pc | Replace the instruction by a break instruction | The program is untouched and may sit in a ROM. Each trigger costs a comparator, so a hart has only a few. |
The Trap: A Step Counted in Clock Edges
A test script wanted to run ten instructions. It asked for ten steps and waited ten clock periods. Then it read the registers. Three of the ten instructions were loads from a slow device, and the hart was still inside the seventh.
The script had mixed two clocks of thought. A step is counted in instructions. Its length in edges depends on the instruction. The fix is the reply again: after each step, wait for halted.
A second trap sits in rule 6. A breakpoint is set in boot code, and the hart runs straight through it. The debugger had tried to write a break instruction into the ROM. A ROM ignores a write. A trigger would have worked.
The third trap is in our lab, and you can see it. After the stop at 14, a second resume with the trigger still set prints "e16 halted pc=14 x5=00000062". The hart halted again at once and ran nothing. A debugger first steps past the address, then sets the trigger again.
At Scale
This section looks ahead. The same three tools meet reset, data and more than one hart.
Halt at reset
To catch the first instruction, the halt request is armed while the hart is still in reset. The hart then halts before it runs anything.
A trigger on data
A trigger can also compare the address of a load or a store. It answers the question: who wrote this word?
A step into a handler
An interrupt may be waiting when you step. The one instruction that runs can then be the first of the handler.
Many harts
One hart halted while seven run is a new kind of fault to study. Debug modules can halt a group of harts together.
Try It
Run the lab. Find the four rows that say "halted", and check each against the program at the top of the file.
Change the cost of the instruction at 08 from 3 to 6. After which edge does the second step end now?
Change the trigger address from 14 to 10. What does x5 hold when the hart halts?
Names Printed by the Lab
| Name | Meaning |
|---|---|
| e1, e2, ... | Rising edges of the clock, counted from the release of reset. A row shows the hart after that edge. |
| running, halted | The state of the hart after the edge. |
| pc | The next pc: the address of the instruction that the hart will run next. Two hex digits. |
| x5 | The register that the program works on, in hexadecimal. |
e1 running pc=00 x5=00000000 e2 running pc=00 x5=00000000 halt requested e3 running pc=00 x5=00000000 e4 halted pc=04 x5=00000041 step requested e5 running pc=04 x5=00000041 e6 halted pc=08 x5=00000042 step requested e7 running pc=08 x5=00000042 e8 running pc=08 x5=00000042 e9 running pc=08 x5=00000042 e10 halted pc=0c x5=00000060 trigger set at 14, resume requested e11 running pc=0c x5=00000060 e12 running pc=10 x5=00000061 e13 running pc=14 x5=00000062 e14 halted pc=14 x5=00000062Read 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
Common Mistakes
Wrong Count a step as one clock edge.
One instruction can take many edges when it uses the bus.
Fix After a step, wait for halted.
Wrong Expect the instruction at a trigger address to have run.
The hart halts before it.
Fix Read the registers as they were just before that instruction.
Wrong Set a breakpoint in ROM by replacing an instruction.
A ROM cannot be written, so the program does not change.
Fix Use a trigger, which compares addresses.
Remember
- A halt lands on an instruction boundary. The registers and the next pc belong to that boundary.
- A resume starts at the next pc.
- A step is one instruction, not one clock edge.
- A trigger halts the hart before the instruction at its address, and leaves the program untouched.