Reset and Interrupts in One Picture
A program moves the PC with jumps. Two things outside the program can move it too: a reset, and an interrupt.
You Will Be Able To
- Say what a reset does to the PC, and why a chip needs one.
- Follow one interrupt: the request, the forced jump, the handler and the return.
- Explain what the handler must leave untouched, and why.
- Lesson
- S0.8
- Time
- 25 min
- Runs on
- Your browser
- Check
- 5 questions
Switch a chip on, and its flip-flops wake up holding anything at all. The PC could point at any box. Later, while a program is busy counting, a timer runs out and needs an answer now. Both problems have the same kind of cure: something outside the program sets the PC.
Before You Start
You need S0.6: the PC, registers, a jump, and the six instructions of the teaching processor. You need S0.2: devices at addresses.
| Question | |
|---|---|
| 1 | What does a reset do, and why does a chip need one? |
| 2 | What is an interrupt, and what would a program have to do without one? |
| 3 | What does the processor remember when it takes an interrupt, and where does the PC go? |
| 4 | How does the main program get the PC back? |
| 5 | What must a handler leave untouched? |
The Idea: Setting the PC from Outside
A reset is a wire that forces every flip-flop to a known value. For the PC, that value is one fixed address. So after a reset the processor always fetches its first instruction from the same box, whatever the flip-flops held before. Without a reset, a chip would start in a different place every time.
An interrupt solves a different problem. A device sometimes needs the processor quickly: a timer has run out, or a byte has arrived. The program could keep asking each device "anything yet?" in a loop. That is called polling. It wastes instructions when nothing has happened, and it answers late when something has.
With an interrupt, the device raises a wire, and the processor does three things. It finishes the instruction in progress. It remembers the PC. Then it forces the PC to a fixed address, where a short program for that device begins. That short program is the handler. Its last instruction puts the remembered address back into the PC.
The main program is never told. It loses a little time, and nothing else.
New Words
| Word | Meaning |
|---|---|
| Reset | A wire that forces every flip-flop to a known starting value. While it is 1, nothing runs. |
| Reset address | The fixed address that a reset puts into the PC. Here it is 0. |
| Interrupt | A jump forced by a device, not by the program. The old PC is remembered so the program can carry on later. |
| Interrupt request | The wire a device raises to ask for an interrupt. |
| Pending | Asked for and not yet taken. A pending bit remembers the request. |
| Handler | The short program that serves an interrupt. It starts at a fixed address. Here that is 8. |
| Saved PC | The register that remembers where the main program was. Also called the return address. |
| RETI | Return from interrupt: the instruction that copies the saved PC back into the PC. It is kind 6 on this processor. |
| Polling | Asking a device again and again, in a loop, whether it needs anything. |
One Picture: Reset, Request, Jump, Return
- Clock / reset
- Processor
- Memory
- Peripheral
- Reset
While the reset wire is 1, the PC is forced to address 0 and nothing runs. When it goes to 0, the first fetch comes from box 0.
- The main program runs
Fetch, decode, execute, as in S0.6. The program knows nothing about the timer.
- The timer asks
The timer raises its request. The pending bit goes to 1 and stays there. The instruction in progress is not disturbed.
- A forced jump
Between two instructions the processor copies the PC into the saved PC. Then it forces the PC to address 8 and clears the pending bit.
- The handler runs
The next fetches come from boxes 8, 9 and 10. This short program serves the timer.
- The return
The last instruction of the handler is RETI. It copies the saved PC back into the PC. The main program carries on where it was.
Read the diagram as text
- Reset: one wire to every flip-flop
- Program counter (PC): the address of the next instruction
- Main program: boxes 0 to 6
- Saved PC: a register that remembers one address
- Handler: boxes 8 to 10
- Timer: a device that asks for attention
- Pending bit: remembers the request
- Reset → Program counter (PC): force address 0
- Program counter (PC) → Main program: fetch
- Timer → Pending bit: interrupt request
- Program counter (PC) → Saved PC: the old PC
- Pending bit → Program counter (PC): force address 8
- Program counter (PC) → Handler: fetch
- Saved PC → Program counter (PC): RETI: the saved address
The addresses 0 and 8 are choices of this teaching processor. Every real processor fixes such addresses in some way.
Compare the forced jump with the JUMPNZ of S0.6. Both write the PC. A JUMPNZ is in the program, and the program knows where it goes. The forced jump is in no program. So the processor must save the old PC, or the way back would be lost.
The Smallest Example, Worked by Hand
The main program adds 3 + 2 + 1 in a loop, with the count in r1 and the sum in r0. The handler, at boxes 8 to 10, copies the switches at address 15 to the lamps at address 14. It uses only r3.
| Box | Instruction | Meaning |
|---|---|---|
| 0 | LOAD r1, 12 | main: r1 = the count, 3 |
| 1 | LOAD r2, 13 | main: r2 = 1 |
| 2 | ADD r0, r1 | main: add the count to the sum |
| 3 | SUB r1, r2 | main: one fewer |
| 4 | JUMPNZ r1, 2 | main: not zero yet, back to box 2 |
| 5 | STORE r0, 11 | main: keep the sum in box 11 |
| 6 | HALT | main: stop |
| 8 | LOAD r3, 15 | handler: read the switches |
| 9 | STORE r3, 14 | handler: show them on the lamps |
| 10 | RETI | handler: return |
Reset is held for two clock edges. The timer fires at edge 6, when the main program has just run the ADD in box 2. One instruction runs at each edge.
| Edge | PC | What runs | Saved PC | Afterwards |
|---|---|---|---|---|
| e5 | 2 | ADD r0, r1 | r0 = 3, next PC = 3 | |
| e6 | 3, forced to 8 | the interrupt is taken, then LOAD r3, 15 | 3 | r3 = 5, the switch value |
| e7 | 9 | STORE r3, 14 | 3 | lamps = 5 |
| e8 | 10 | RETI | 3 | next PC = 3 |
| e9 | 3 | SUB r1, r2 | 3 | r1 = 2: the main program carries on |
The instruction in box 3 was due at e6. It ran at e9 instead. The main program lost three edges and nothing else: r0, r1 and r2 are exactly as it left them.
The Rules
Rule 1: A reset forces the PC to one fixed address.
While the reset wire is 1, no instruction runs. When it goes to 0, the first fetch is from that address.
Rule 2: An interrupt request is remembered until it is taken.
The pending bit holds it. A request is never lost because the processor was busy.
Rule 3: The processor takes an interrupt only between two instructions.
It saves the PC, forces the PC to the address of the handler, and clears the pending bit.
Rule 4: The handler ends with a return.
RETI copies the saved PC back into the PC. The next fetch is the instruction the main program was about to run.
Rule 5: The handler must leave the main program's registers as it found them.
The processor saves only the PC. Everything else is the handler's duty.
Sources for These Rules
The textbook "Computer Organization and Design" by Patterson and Hennessy, section 4.9. It covers interrupts and exceptions, the saved address and the fixed address of the handler. That book calls an event from outside the processor an interrupt, and one from inside an exception.
The reset address 0, the handler address 8, the pending bit and RETI are our own teaching processor. Real processors differ in the details and agree on the five rules.
The RISC-V names near the end of this page come from the RISC-V rule book, which our reference library does not hold. They are marked as RISC-V facts.
Now Run It
The lab at the end of this page is the S0.6 processor with a reset, a timer and the RETI instruction. Each row starts with the number of its clock edge.
e1 reset is 1: the PC is forced to 0
e2 reset is 1: the PC is forced to 0
e3 pc=0 LOAD r1, 12 r1=3
e4 pc=1 LOAD r2, 13 r2=1
e5 pc=2 ADD r0, r1 r0=3
e6 the timer fires: pending=1
e6 interrupt taken: saved_pc=3, the PC is forced to 8
e6 pc=8 LOAD r3, 15 r3=5
e7 pc=9 STORE r3, 14 address 14 takes 5
e8 pc=10 RETI next pc=3
e9 pc=3 SUB r1, r2 r1=2
e10 pc=4 JUMPNZ r1, 2 next pc=2
e11 pc=2 ADD r0, r1 r0=5
e12 pc=3 SUB r1, r2 r1=1
e13 pc=4 JUMPNZ r1, 2 next pc=2
e14 pc=2 ADD r0, r1 r0=6
e15 pc=3 SUB r1, r2 r1=0
e16 pc=4 JUMPNZ r1, 2 next pc=5
e17 pc=5 STORE r0, 11 address 11 takes 6
e18 pc=6 HALT
box 11 holds 6, lamps=5, interrupts taken=1Rule 1 at work: e1 and e2 run nothing, and the first instruction at e3 comes from pc=0. Rule 3 at work: at e6 the saved PC is 3 and the PC is forced to 8. Rule 4 at work: the RETI at e8 ends with next pc=3. Then e9 runs the SUB that was due at e6.
The last row is the proof that the main program was not harmed. Box 11 holds 6, which is 3 + 2 + 1. The lamps show 5, the switch value, so the handler did its job too.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| The device interrupts the program | The program polls every device | No wasted instructions, and a quick answer. The processor needs a pending bit and a saved PC. |
| Take the interrupt between two instructions | Stop in the middle of one | There is never a half-done instruction to remember. The device waits for at most one instruction. |
| The processor saves only the PC | It saves every register | The forced jump is quick. The handler must save any register it wants to use. |
| A fixed address for the handler | The device supplies an address | The processor needs no extra wires from the device. Software must put the handler at that address. |
The Trap: A Handler That Borrows a Register
A colleague rewrites the handler to use r1 in place of r3. It still copies the switches to the lamps. The sum in box 11 is now sometimes wrong, and only when the timer fires inside the loop.
box 11 holds 13, lamps=5, interrupts taken=1The main program was counting with r1. The handler left the switch value, 5, in it. After the return the loop counted down from 5, and the sum came to 13. Nothing warned anyone. This is rule 5. Such bugs are hard to find, because the fault shows in the main program, far from the handler that caused it.
At Scale
This section looks ahead. A real chip has dozens of interrupt sources and several kinds of reset. The five rules stay the same.
Many devices, one choice
With many requests at once, a block called an interrupt controller picks the most urgent one. The lesson The Interrupt Path follows a request through it.
The request stays up
Here the timer asks once. A real device usually keeps its request raised until the handler tells it to stop.
Switching interrupts off
A processor has a bit that makes it ignore requests for a while. Requests stay pending meanwhile, as in rule 2.
RISC-V names
In RISC-V the saved PC is a register named mepc, and the return instruction is mret. The address of the handler is held in a register named mtvec, so software can choose it.
After reset
In ap_soc the reset address is 0, where the Boot ROM sits. The lesson The Boot Flow follows the first instructions.
The names mepc, mret and mtvec are RISC-V facts, taken from that rule book.
Try It
Run the lab. Find the row where the interrupt is taken, and the row where the main program carries on.
Change FIRE to 9. Predict the saved PC before you run it.
Change FIRE to 1, so that the timer fires during reset. Predict the edge at which the interrupt is taken.
Make the handler use r1 in place of r3. Predict the number in box 11.
e1 reset is 1: the PC is forced to 0 e2 reset is 1: the PC is forced to 0 e3 pc=0 LOAD r1, 12 r1=3 e4 pc=1 LOAD r2, 13 r2=1 e5 pc=2 ADD r0, r1 r0=3 e6 the timer fires: pending=1 e6 interrupt taken: saved_pc=3, the PC is forced to 8 e6 pc=8 LOAD r3, 15 r3=5 e7 pc=9 STORE r3, 14 address 14 takes 5 e8 pc=10 RETI next pc=3 e9 pc=3 SUB r1, r2 r1=2 e10 pc=4 JUMPNZ r1, 2 next pc=2 e11 pc=2 ADD r0, r1 r0=5 e12 pc=3 SUB r1, r2 r1=1 e13 pc=4 JUMPNZ r1, 2 next pc=2 e14 pc=2 ADD r0, r1 r0=6 e15 pc=3 SUB r1, r2 r1=0 e16 pc=4 JUMPNZ r1, 2 next pc=5 e17 pc=5 STORE r0, 11 address 11 takes 6 e18 pc=6 HALT box 11 holds 6, lamps=5, interrupts taken=1Check Yourself
Common Mistakes
Wrong Expect an interrupt to cut an instruction in half.
The processor takes it only between two instructions.
Fix Let the instruction in progress finish. The forced jump comes before the next fetch.
Wrong Use any free-looking register in a handler.
The main program may be using it, and the processor saves only the PC.
Fix Save every register the handler uses, and put it back before the return.
Wrong End a handler with an ordinary jump back.
The handler cannot know where the main program was. Only the saved PC knows.
Fix End the handler with the return instruction, which uses the saved PC.
Remember
- A reset forces the PC to one fixed address, so a chip always starts in the same place.
- An interrupt is a jump forced by a device: the request stays pending until the processor takes it.
- The processor takes it between two instructions, saves the PC and forces the PC to the handler.
- The handler ends with a return that restores the saved PC; the main program carries on unaware.
- The processor saves only the PC. A handler that changes a register breaks the main program.