Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

ARM pipeline, exceptions and interrupts

Concise study notes on ARM instruction pipelining, pipeline hazards and the ARM exception/interrupt mechanism (vectors, modes, context saving), focused for university exam preparation.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Processor pipelining and interrupts are central to writing fast, responsive embedded firmware. An engineer uses pipelining concepts to reason about instruction throughput and optimise code; exceptions and interrupts determine how the system responds to faults and external events and how to write correct, low-latency handlers.

Key ideas

  • Instruction-level parallelism (ILP): throughput gained because different instructions occupy different stages simultaneously.

  • Hazards: conditions that prevent the next instruction in the pipeline from executing in the next cycle.

    • Structural hazard: hardware resource conflict (e.g., single memory port used for both instruction fetch and data access). Assumption: resources are limited; solution: duplicate resources or stall.
    • Data hazard: when an instruction depends on the result of a previous instruction not yet available (RAW — read after write). Solutions: forwarding (bypass), register renaming (in complex CPUs), or stalls (pipeline bubble).
    • Control hazard: caused by branch instructions; we don't know next PC until branch resolves. Solutions: branch prediction, delayed branches (branch delay slots), flushing the pipeline on mispredict. Note: ARM architectures do not implement branch delay slots.
  • Pipeline control mechanisms: forwarding (bypassing) — route ALU results directly to subsequent stage to avoid waiting for writeback; stall — insert NOPs; flush — clear instructions following a taken branch.

  • Performance measures:

    • CPI (cycles per instruction) — average cycles used per instruction. In an ideal N-stage pipeline, CPI = 1 (one instruction completed per cycle). Hazards increase CPI.
    • Pipeline speedup — ratio of execution time non-pipelined to pipelined (ideal ≈ N, the number of stages) but reduced by stalls and imbalance.
  • ARM exception: a synchronous or asynchronous event that causes the processor to change control flow to a fixed service routine (vector). Examples: reset, undefined instruction, software interrupt (SWI), prefetch abort, data abort, IRQ (normal interrupt), FIQ (fast interrupt).

    • Interrupt: a type of exception caused by an external hardware source (asynchronous). IRQ is the normal interrupt line; FIQ is a higher-priority fast interrupt.
  • Exception entry (typical hardware sequence):

    1. Save current processor state: hardware copies CPSR (Current Program Status Register) to SPSR_<mode> of the new mode.
    2. Change processor mode to the appropriate exception mode (e.g., IRQ mode, FIQ mode, Supervisor, Abort, Undefined).
    3. Bank or switch registers as specified for mode (some registers like R13/R14 are banked per mode; FIQ mode also has R8–R12 banked).
    4. Disable further interrupts as required (set I/F bits in CPSR: I masks IRQ, F masks FIQ) and set other CPSR bits (flags) as specified.
    5. Save return address to LR_<mode> (link register for the exception mode).
    6. Load PC with the exception vector address (fixed addresses in low vectors or high vectors).
    7. Flush or adjust pipeline so subsequent execution begins at the handler.
  • Exception vectors (classic fixed low-vectors): addresses to which PC is set on exception entry (in bytes):

    • Reset: 0x00000000
    • Undefined Instruction: 0x00000004
    • Software Interrupt (SWI): 0x00000008
    • Prefetch Abort: 0x0000000C
    • Data Abort: 0x00000010
    • (reserved): 0x00000014
    • IRQ: 0x00000018
    • FIQ: 0x0000001C Note: some systems support relocating the vector table to high memory (e.g., 0xFFFF0000) under control registers.
  • Return from exception: handler restores register context (either from banked registers or by popping saved values), then restores CPSR from SPSR_<mode> (so user mode and interrupt enable bits return) and returns by restoring PC from LR_<mode> with the architecturally defined offset using an instruction that also updates CPSR (e.g., in ARM state: SUBS PC, LR, #4 for IRQ/FIQ; in ARMv7-A/R: RFE/RFEDB are available).

  • ARM banked registers and modes (brief): modes include User, FIQ, IRQ, Supervisor, Abort, Undefined, System, Monitor (in some ARM versions). Registers R13 (SP) and R14 (LR) are banked for many modes; FIQ has extra banked R8–R12. SPSR is banked per exception mode (not for User/System). These bankings let the hardware supply private registers for fast context switch.

  • Timing and latency: FIQ is designed for low-latency handling because it has extra banked registers (less stack use and fewer register saves).

Drawings to include for exam answers (describe exactly):

  • Pipeline diagram: draw a table with cycles on the X axis (label columns cycle 1, 2, 3, ...) and pipeline stages on the Y axis (instructions I1, I2, I3). Fill boxes to show that in an ideal pipeline I1:F at cycle1, I1:D at cycle2 and I2:F at cycle2, etc. Label hazards by marking where a stall (empty box) is inserted.
  • Exception vector table: draw a vertical list with addresses (left) and exception names (right) as above and label which one is IRQ and which is FIQ.
  • Exception entry flowchart: boxes for CPSR -> SPSR copy, mode switch, LR save, PC set to vector, pipeline flush.

Formulas

CPI_avg = (Total cycles) / (Number of instructions) — CPI_avg: cycles·instruction^-1; applies for measured program runs. Ideal_CPI_pipelined = 1 — applies when pipeline is perfectly balanced and there are no stalls. Speedup = (CPI_non-pipelined) / (CPI_pipelined) — unitless; compares two designs on same clock. Total_cycles_pipelined = N_instructions + Pipeline_stall_cycles (+ pipeline fill/flush overhead) — cycles; for a balanced pipeline with ideal CPI ≈ 1 the base term is N_instructions, ignoring startup/shutdown cost when allowed by the problem. Total_cycles_nonpipelined = N_instructions * CPI_non-pipelined — cycles; where CPI_non-pipelined is the cycles per instruction of the reference scalar design. Branch_penalty = Stall_cycles_per_mispredict — cycles lost per mispredicted branch.

Worked examples

  1. Pipeline CPI and speedup (numerical) Problem: A 5-stage pipeline executes 10,000 instructions. There are 400 data hazards that each cause 1-cycle stall, and 150 taken branches; each taken branch causes a 2-cycle penalty. A non-pipelined reference CPU needs 5 cycles per instruction. Compute total cycles, CPI, and speedup. Solution steps:
  • Ideal pipelined cycles = 10,000 instructions * 1 cycle per instruction = 10,000 cycles.
  • Stalls from data hazards = 400 * 1 = 400 cycles.
  • Stalls from control hazards = 150 * 2 = 300 cycles.
  • Total_cycles = 10,000 + 400 + 300 = 10,700 cycles.
  • CPI_avg = Total_cycles / N_instructions = 10,700 / 10,000 = 1.07 cycles·instruction^-1. (show formula first)
  • Non-pipelined cycles = 10,000 * 5 = 50,000 cycles.
  • Speedup = 50,000 / 10,700 ≈ 4.673. Answer: total cycles = 10,700 cycles; CPI = 1.07; speedup ≈ 4.67.
  1. Exception handling (theory model answer) Question: Explain what hardware steps an ARM core takes on an IRQ and why banked registers help FIQ handlers. Model answer (step-by-step):
  • On IRQ event the processor completes the current instruction, then hardware performs: (1) CPSR is copied into SPSR_irq; (2) CPSR mode bits are set to IRQ mode; (3) the I bit in CPSR is set to disable further IRQs; (4) LR_irq is set to a return address (program counter of the next instruction or adjusted depending on pipeline/state); (5) PC is loaded with the IRQ vector address (0x00000018 in classic vector table); (6) the pipeline is flushed so execution continues at the handler.
  • For FIQ mode the processor additionally uses banked registers R8–R12 and R13–R14 to avoid pushing/popping them on entry — this reduces software overhead and gives lower latency. Answer should mention CPSR→SPSR copy, mode switch, interrupt disable bit set, LR save, PC load to vector, and benefit of banked regs.

(Note to reviewer: exact LR value offsets and exactly which CPSR bits are set on FIQ entry are implementation details — see verify.)

Common mistakes

  • Forgetting SPSR is banked per mode and is used to restore CPSR on return.
  • Claiming FIQ and IRQ have identical register bank behaviour — FIQ has extra banked registers.
  • Using the words "interrupt" and "exception" interchangeably without noting that interrupts are asynchronous external exceptions.

For your exam

Kinds of questions:

  • Definitions: define pipeline, hazards, CPSR, SPSR, IRQ, FIQ, vector table.
  • Short notes: explain forwarding, branch penalty, exception entry sequence.
  • Derivations/numericals: compute CPI, cycles, speedup for given hazards and pipeline depth.
  • Programs/algorithms: show pseudo-code for a minimal IRQ handler that saves context, handles, and restores.
  • Diagrams: draw pipeline timeline or exception vector table and label addresses. What full-marks answers include:
  • Correct definitions with bold terms, correct vector addresses, clear diagrams with labels, stepwise exception entry and return sequence, and correct arithmetic with units for numericals.

Quick check

  1. What are the three classical types of pipeline hazard?
  2. To which fixed address does the ARM jump on an IRQ (classic low-vector table)?
  3. Which register holds the saved CPSR on exception entry (name the register and that it is banked)?

Answers: 1. Structural, data, control. 2. 0x00000018. 3. SPSR_<mode> — the banked Saved Program Status Register for the exception mode.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?