IT236 Microprocessor and Computer Architecture

Microprocessor and Computer ArchitectureUnit 89 min read

Interrupts and DMA – Control Flow, Asynchronous Events, Direct Memory Access

Unit 8 of Microprocessor and Computer Architecture: explains the mechanisms of interrupt handling, types of interrupts, interrupt controller architecture, context switching, and Direct Memory Access (DMA) operations, including practical examples and performance implications.

Key points

  • Interrupts allow asynchronous event handling by temporarily suspending the current program flow.
  • The interrupt controller decodes interrupt requests, prioritizes them, and generates vector addresses.
  • DMA offloads data transfer between peripherals and memory, freeing CPU cycles.
  • Proper context saving/restoring is essential to maintain program correctness during interrupts.
  • Interrupt latency, masking, and priority schemes directly affect real‑time system performance.

1. Interrupt Fundamentals

An interrupt is a hardware or software signal that temporarily halts the normal execution of a program, transfers control to a special routine called an Interrupt Service Routine (ISR), and then resumes the original program.
Interrupts are essential for responsive systems: they enable devices to notify the CPU when they need service (e.g., keyboard input, timer expiry, network packet arrival).

Feature Hardware Interrupt Software Interrupt
Source External device or peripheral Instruction (INT) or exception
Trigger Rising edge on IRQ line CPU executes instruction
Latency Determined by controller and bus Determined by instruction fetch
Maskability Often maskable (IRQ line can be disabled) Usually maskable via flags

1.1 Interrupt Vector Table

The CPU uses an Interrupt Vector Table (IVT) to locate the ISR for a given interrupt. Each entry contains the address of the ISR.

08162431InterruptNumber8 bitsISR Address24 bits
Example 32-bit IVT entry (e.g., x86 architecture)

The table is usually stored in a fixed memory region (e.g., 0x0000–0x03FF in 8086).

1.2 Interrupt Handling Flow

flowchart TD  
    "CPU executing user code" --> "Interrupt request (IRQ) detected"  
    "IRQ detected" --> "CPU pushes PC and SR onto stack"  
    "Stack updated" --> "CPU reads vector address from IVT"  
    "Vector address fetched" --> "CPU jumps to ISR"  
    "ISR executes" --> "ISR may read/write registers"  
    "ISR completes" --> "CPU executes IRET (return from interrupt)"  
    "IRET" --> "CPU restores PC and SR from stack"  
    "CPU resumes user code"
The IRET instruction restores the program counter and status register, ensuring seamless return.

2. Types of Interrupts

Type Source Maskability Typical Use
Maskable Hardware Interrupt (IRQ) Peripherals (timer, UART, etc.) Yes General device I/O
Non‑Maskable Interrupt (NMI) Critical faults (e.g., parity error) No System failure, emergency
Software Interrupt CPU instruction (INT) Yes System calls, exceptions
Vector Interrupt CPU exception (divide by zero) Yes Fault handling

2.1 Maskable vs Non‑Maskable

Maskable interrupts can be disabled by setting a mask bit in the interrupt controller or status register. Non‑maskable interrupts bypass the mask and must be serviced immediately, ensuring high‑priority events are handled.

3. Interrupt Controllers

Modern CPUs use an Interrupt Controller (e.g., Intel’s 8259A, ARM’s GIC) to manage multiple IRQ lines, prioritize them, and generate the correct vector.

CPUCPU CoreInterrupt ControllerIRQ LinesInterrupt SourcesPeripherals
Interaction between CPU and Interrupt Controller (e.g., 8259A or ARM GIC)

The controller typically has two stages: interrupt request and interrupt acknowledge. The CPU acknowledges the interrupt, allowing the controller to clear the request flag.

4. Context Switching in Interrupts

During an interrupt, the CPU must preserve the state of the interrupted program. This involves:

  1. Saving registers (general‑purpose, program counter, status register) onto the stack or a dedicated context area.
  2. Switching to ISR context (loading ISR registers).
  3. Restoring registers upon ISR completion.

The amount of context saved depends on the architecture. In 8‑bit microcontrollers, only a few registers are saved; in 32‑bit CPUs, the entire register file may be preserved.

4.1 Worked Example: Timer Interrupt on an 8051

Step Action Register/Memory Value
1 Timer overflows → IRQ0 T0 overflow flag 1
2 CPU detects IRQ0 PC = 0x1234
3 CPU pushes PC, PSW onto stack Stack[SP] = 0x1234, 0x02
4 CPU reads vector from IVT (0x0004) PC = 0x0004
5 ISR executes: increments counter ACC = ACC + 1
6 ISR completes: executes RETI PC restored from stack 0x1234
7 CPU resumes user code

The ISR runs in 12 machine cycles, while the interrupt latency is 12 cycles plus the time to push registers.

5. Direct Memory Access (DMA)

DMA allows peripherals to transfer data directly to/from memory without continuous CPU intervention. The CPU initiates the transfer by programming the DMA controller; the controller then handles the bus cycles.

5.1 DMA Architecture

flowchart TD
  subgraph DMA_Architecture
    CPU["CPU"] -->|"Programs DMA"| DMA["DMA Controller"]
    DMA -->|"Requests bus"| MEM["Memory"]
    DMA -->|"Reads/Writes"| PER["Peripheral"]
    MEM -->|"Data transfer"| DMA
    PER -->|"Data transfer"| DMA
  end
  CPU -->|"Handles exceptions"| DMA
DMA Controller as a bridge between CPU, memory, and peripherals

The DMA controller has:

  • Source address (peripheral or memory)
  • Destination address (memory or peripheral)
  • Transfer size
  • Control registers (mode, priority)

5.2 DMA Transfer Sequence

sequenceDiagram  
    participant CPU  
    participant DMA  
    participant MEM  
    participant PERIPH  
    CPU->>DMA: Program transfer (src, dst, size)  
    DMA->>MEM: Request bus access  
    MEM->>DMA: Grant bus access  
    DMA->>PERIPH: Read data  
    DMA->>MEM: Write data  
    loop for each word  
        DMA->>PERIPH: Read next word  
        DMA->>MEM: Write next word  
    end  
    DMA->>CPU: Interrupt (transfer complete)

The CPU can perform other tasks while DMA completes the transfer, improving overall throughput.

5.3 Advantages & Disadvantages

Advantage Disadvantage
Frees CPU cycles Requires DMA controller hardware
Higher transfer rates Complexity in synchronization
Efficient for large blocks Potential bus contention
Supports burst mode Limited to peripherals that support DMA
021.2542.563.7585CPU Load30Data Transfer Speed85Complexity70
Relative performance impact of DMA vs. CPU-driven transfers (arbitrary scale)

6. Interrupt Priorities and Nesting

Interrupt controllers often support priority levels. Higher priority interrupts can pre‑empt lower priority ones (nesting). The CPU must save the context of the lower priority ISR before servicing the higher one.

6.1 Priority Table Example

Priority Interrupt Maskable
0 (Highest) NMI No
1 Timer Yes
2 UART Yes
3 Keyboard Yes

If a UART interrupt occurs while servicing a timer interrupt, the UART ISR may be queued or pre‑empted based on the controller’s policy.

7. Real‑World Applications

7.1 eSewa – Payment Gateway

eSewa’s backend servers use DMA to transfer large transaction logs from network interfaces to disk storage. By offloading the transfer, the CPU can process new payment requests in real time, reducing latency during peak hours.

7.2 Pathao – Ride‑Sharing App

Pathao’s Android app receives GPS updates via the NMEA serial interface. The GPS module triggers a hardware interrupt each time a new NMEA sentence arrives. The ISR parses the sentence and updates the UI, ensuring the driver’s location is always current.

7.3 Ncell – Mobile Network

Ncell’s base station firmware uses non‑maskable interrupts to handle critical power‑failure signals. When a power‑supply fault is detected, the NMI forces an immediate shutdown sequence, preventing data corruption.

8. In the real world

Product Idea Used How It Works
eSewa DMA for log transfer DMA controller moves transaction logs from NIC buffer to SSD without CPU involvement, speeding up log writes.
Pathao UART interrupt for GPS GPS module asserts IRQ; ISR parses NMEA string and updates driver’s map in real time.
Ncell NMI for power fault Power fault line triggers NMI; firmware immediately saves state and powers down safely.

Worked Example – ATM Cash Withdrawal

  1. User inserts card → Card reader triggers hardware interrupt.
  2. ISR reads card data, sends it to CPU via DMA (card data block).
  3. CPU verifies PIN, initiates transaction.
  4. Cash dispenser triggers interrupt when cash is ejected.
  5. ISR updates transaction log via DMA to memory.
  6. CPU finalizes transaction and prints receipt.

The use of DMA for log updates ensures the CPU can handle multiple concurrent withdrawals without bottleneck.

9. Exam tip

  • Understand the flow: Be able to draw the interrupt handling sequence, including context save/restore and vector lookup.
  • Know the types: Memorize the differences between maskable, non‑maskable, hardware, and software interrupts.
  • Work through a trace: Practice tracing an ISR, including register values and stack changes.
  • DMA details: Explain how DMA reduces CPU load and outline the transfer sequence.
  • Priority & nesting: Be ready to explain how nested interrupts are handled and why priority matters.

Good luck!

Based on the TU BIM syllabus for Microprocessor and Computer Architecture (IT236), unit 8.

Discussion

Loading…