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.

Five questions this page answers
Question
1What does a reset do, and why does a chip need one?
2What is an interrupt, and what would a program have to do without one?
3What does the processor remember when it takes an interrupt, and where does the PC go?
4How does the main program get the PC back?
5What 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

New words on this page
WordMeaning
ResetA wire that forces every flip-flop to a known starting value. While it is 1, nothing runs.
Reset addressThe fixed address that a reset puts into the PC. Here it is 0.
InterruptA jump forced by a device, not by the program. The old PC is remembered so the program can carry on later.
Interrupt requestThe wire a device raises to ask for an interrupt.
PendingAsked for and not yet taken. A pending bit remembers the request.
HandlerThe short program that serves an interrupt. It starts at a fixed address. Here that is 8.
Saved PCThe register that remembers where the main program was. Also called the return address.
RETIReturn from interrupt: the instruction that copies the saved PC back into the PC. It is kind 6 on this processor.
PollingAsking a device again and again, in a loop, whether it needs anything.

One Picture: Reset, Request, Jump, Return

Two Ways into the PC from OutsideStep through the picture. A reset starts the program. A timer then breaks into it, and the program carries on.
  • Clock / reset
  • Processor
  • Memory
  • Peripheral
Two Ways Into the PC From OutsideStep through the picture. A reset starts the program. A timer then breaks into it, and the program carries on. component Reset one wire to everyflip-flop component Program counter(PC) the address of thenext instruction component Main program boxes 0 to 6 component Saved PC a register thatremembers oneaddress component Handler boxes 8 to 10 component Timer a device that asksfor attention component Pending bit remembers therequest force address 0fetchinterrupt requestthe old PCforce address 8fetchRETI: the savedaddress
  1. component Resetone wire to every flip-flop
    • force address 0 → Program counter (PC)
  2. component Program counter (PC)the address of the next instruction
    • fetch → Main program
    • the old PC → Saved PC
    • fetch → Handler
  3. component Main programboxes 0 to 6
  4. component Timera device that asks for attention
    • interrupt request → Pending bit
  5. component Pending bitremembers the request
    • force address 8 → Program counter (PC)
  6. component Saved PCa register that remembers one address
    • RETI: the saved address → Program counter (PC)
  7. component Handlerboxes 8 to 10
  1. 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.

  2. The main program runs

    Fetch, decode, execute, as in S0.6. The program knows nothing about the timer.

  3. The timer asks

    The timer raises its request. The pending bit goes to 1 and stays there. The instruction in progress is not disturbed.

  4. 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.

  5. The handler runs

    The next fetches come from boxes 8, 9 and 10. This short program serves the timer.

  6. 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.

The two programs
BoxInstructionMeaning
0LOAD r1, 12main: r1 = the count, 3
1LOAD r2, 13main: r2 = 1
2ADD r0, r1main: add the count to the sum
3SUB r1, r2main: one fewer
4JUMPNZ r1, 2main: not zero yet, back to box 2
5STORE r0, 11main: keep the sum in box 11
6HALTmain: stop
8LOAD r3, 15handler: read the switches
9STORE r3, 14handler: show them on the lamps
10RETIhandler: 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.

Edges 5 to 9, by hand
EdgePCWhat runsSaved PCAfterwards
e52ADD r0, r1r0 = 3, next PC = 3
e63, forced to 8the interrupt is taken, then LOAD r3, 153r3 = 5, the switch value
e79STORE r3, 143lamps = 5
e810RETI3next PC = 3
e93SUB r1, r23r1 = 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

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

Printed by the lab. An indented row reports a reset, the timer or the forced jump. The other rows are instructions.
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=1

Rule 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 choiceThe alternativeWhat the choice buys, and costs
The device interrupts the programThe program polls every deviceNo wasted instructions, and a quick answer. The processor needs a pending bit and a saved PC.
Take the interrupt between two instructionsStop in the middle of oneThere is never a half-done instruction to remember. The device waits for at most one instruction.
The processor saves only the PCIt saves every registerThe forced jump is quick. The handler must save any register it wants to use.
A fixed address for the handlerThe device supplies an addressThe 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.

The last row of the same lab, with the handler using r1.
box 11 holds 13, lamps=5, interrupts taken=1

The 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 page1 source, 1 handler
ap_socmany sources, one controller that chooses

This section looks ahead. A real chip has dozens of interrupt sources and several kinds of reset. The five rules stay the same.

  1. 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.

  2. The request stays up

    Here the timer asks once. A real device usually keeps its request raised until the handler tells it to stop.

  3. Switching interrupts off

    A processor has a bit that makes it ignore requests for a while. Requests stay pending meanwhile, as in rule 2.

  4. 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.

  5. 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.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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=1

Check Yourself

Predict the outputIn this run the count is 2, the switches show 9 and the timer fires at edge 5. What is the last line the lab prints?
// The processor of S0.6 with a reset and one interrupt. A timer fires once, in the middle of a loop.
module tb;
  logic clk = 0;
  always #5 clk = ~clk;

  logic reset = 1;                     // 1 = held in reset. Released after two clock edges.
  initial begin repeat (2) @(posedge clk); #1 reset = 0; end

  // ---- Memory and two devices, reached through the bus ----
  logic [15:0] mem [0:13];             // addresses 0 to 13
  logic [15:0] lamps    = 0;           // address 14
  logic [15:0] switches = 9;           // address 15
  function logic [15:0] bus_read(input logic [3:0] addr);
    return (addr <= 13) ? mem[addr] : (addr == 14) ? lamps : switches;
  endfunction
  task bus_write(input logic [3:0] addr, input logic [15:0] data);
    if (addr <= 13)      mem[addr] = data;
    else if (addr == 14) lamps     = data;
  endtask

  // ---- The timer: a device that asks for attention once, at the clock edge numbered FIRE ----
  localparam FIRE = 5;
  logic pending = 0;                   // the request, remembered until the processor takes it

  // ---- The processor ----
  localparam HALT = 0, LOAD = 1, ADD = 2, STORE = 3, SUB = 4, JUMPNZ = 5, RETI = 6;   // RETI is new
  localparam RESET_PC = 0;             // the fixed address where a reset starts the program
  localparam HANDLER  = 8;             // the fixed address where an interrupt sends the PC
  logic [3:0]  pc, saved_pc = 0;       // saved_pc remembers where the main program was
  logic        in_handler = 0;         // 1 from the forced jump until RETI: no second interrupt meanwhile
  logic [15:0] r [0:3];
  logic [15:0] word;
  logic [3:0]  kind, reg_no, arg, next_pc;
  int          edge_no = 0, taken = 0;

  initial begin
    for (int i = 0; i < 14; i++) mem[i] = 0;
    for (int i = 0; i < 4; i++)  r[i]   = 0;
    // The main program, from address 0: add the count, then one fewer, down to 1. Store the sum in box 11.
    mem[0]  = 16'h110C;   // LOAD r1, 12     r1 = the count
    mem[1]  = 16'h120D;   // LOAD r2, 13     r2 = 1
    mem[2]  = 16'h2001;   // ADD r0, r1      add the count to the sum
    mem[3]  = 16'h4102;   // SUB r1, r2      one fewer
    mem[4]  = 16'h5102;   // JUMPNZ r1, 2    not zero yet: back to box 2
    mem[5]  = 16'h300B;   // STORE r0, 11    keep the sum
    mem[6]  = 16'h0000;   // HALT
    // The handler, from address 8: copy the switches to the lamps, then return.
    mem[8]  = 16'h130F;   // LOAD r3, 15
    mem[9]  = 16'h330E;   // STORE r3, 14
    mem[10] = 16'h6000;   // RETI
    mem[12] = 2;          // data: the count
    mem[13] = 1;          // data: the constant 1
    forever begin
      @(posedge clk);
      edge_no++;
      if (edge_no == FIRE) begin pending = 1; $display("e%0d       the timer fires: pending=1", edge_no); end
      if (reset) begin                 // RESET: force the PC to its fixed start. Nothing runs.
        pc = RESET_PC;  in_handler = 0;
        $display("e%0d       reset is 1: the PC is forced to %0d", edge_no, pc);
      end else begin
        if (pending && !in_handler) begin        // INTERRUPT: a forced jump between two instructions
          saved_pc = pc;  pc = HANDLER;  in_handler = 1;  pending = 0;  taken++;
          $display("e%0d       interrupt taken: saved_pc=%0d, the PC is forced to %0d", edge_no, saved_pc, pc);
        end
        word = bus_read(pc);                     // from here on, the loop of S0.6
        kind = word[15:12];  reg_no = word[11:8];  arg = word[3:0];
        next_pc = pc + 1;
        case (kind)
          LOAD:   begin r[reg_no] = bus_read(arg);
                    $display("e%0d pc=%0d  LOAD r%0d, %0d     r%0d=%0d", edge_no, pc, reg_no, arg, reg_no, r[reg_no]); end
          STORE:  begin bus_write(arg, r[reg_no]);
                    $display("e%0d pc=%0d  STORE r%0d, %0d    address %0d takes %0d", edge_no, pc, reg_no, arg, arg, r[reg_no]); end
          ADD:    begin r[reg_no] = r[reg_no] + r[arg[1:0]];
                    $display("e%0d pc=%0d  ADD r%0d, r%0d      r%0d=%0d", edge_no, pc, reg_no, arg, reg_no, r[reg_no]); end
          SUB:    begin r[reg_no] = r[reg_no] - r[arg[1:0]];
                    $display("e%0d pc=%0d  SUB r%0d, r%0d      r%0d=%0d", edge_no, pc, reg_no, arg, reg_no, r[reg_no]); end
          JUMPNZ: begin if (r[reg_no] != 0) next_pc = arg;
                    $display("e%0d pc=%0d  JUMPNZ r%0d, %0d   next pc=%0d", edge_no, pc, reg_no, arg, next_pc); end
          RETI:   begin next_pc = saved_pc;  in_handler = 0;   // RETURN: put the saved address back
                    $display("e%0d pc=%0d  RETI            next pc=%0d", edge_no, pc, next_pc); end
          default: begin
                    $display("e%0d pc=%0d  HALT", edge_no, pc);
                    $display("box 11 holds %0d, lamps=%0d, interrupts taken=%0d", mem[11], lamps, taken);
                    $finish; end                 // $finish ends the simulation
        endcase
        pc = next_pc;
      end
    end
  end
endmodule
ConceptReset has just gone from 1 to 0. Where does the first fetch come from?
ConceptWhat does the processor do, by itself, when it takes an interrupt?
ConceptA handler ends with RETI. Which instruction runs next?
ConceptA program polls a device in a loop when it could use an interrupt. What does that cost?

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.

Practice

Report a problem with this page