Skip to content
Tutorials Center
My learning

Topics

Lesson 49 of 67 ยท Input/Output

I/O Organization: Polling, Interrupts and DMA

See how the CPU talks to devices: controllers, memory-mapped I/O, polling, interrupts and DMA, with Python simulations that compare their cost.

A processor that could only add numbers would be useless. It becomes a computer when it can read a key press, draw a pixel, store a file or send a packet. All of that is input/output (I/O), and it has a problem: devices are slow and unpredictable compared with a CPU. A keyboard delivers a few bytes per second. A CPU can run billions of instructions in the same second.

In this lesson you will see how devices attach to a computer through controllers and registers, how the CPU addresses them with memory-mapped I/O, and the three classic ways to move data: polling, interrupts and direct memory access (DMA). You will simulate all three and compare how much useful work the CPU gets done. It builds on the Von Neumann model and the components of a computer.

Devices, controllers and registers

The CPU never talks to a spinning disk platter or to the glass of a screen directly. Each device comes with a device controller, a small circuit (often a tiny processor of its own) that handles the messy electrical details. The controller exposes a handful of registers to the CPU:

  • A status register: is the device ready? busy? did an error happen?
  • A command (control) register: start, stop, reset, choose a mode.
  • One or more data registers: the bytes going in or out.

The software that knows how to use those registers is the device driver. Applications call the operating system, the operating system calls a driver, and the driver reads and writes controller registers.

flowchart LR
  CPU["CPU"] <-->|"system bus"| MEM["Main memory"]
  CPU <-->|"system bus"| C1["Keyboard<br/>controller"]
  CPU <-->|"system bus"| C2["Disk<br/>controller"]
  CPU <-->|"system bus"| C3["Network<br/>controller"]
  C1 --- K["Keyboard"]
  C2 --- D["Disk"]
  C3 --- N["Cable / radio"]

Each device has a controller that exposes registers on the bus.

Memory-mapped I/O and port-mapped I/O

How does an instruction name a device register? There are two designs.

Memory-mapped I/O gives device registers ordinary addresses. A range of the address space does not reach RAM at all; the address decoder routes it to a controller instead. The CPU uses its normal load and store instructions, so no special I/O instructions are needed. Almost every modern architecture, including ARM and RISC-V, works this way.

Port-mapped I/O uses a separate address space for devices, reached with special instructions. The classic x86 in and out instructions do this. The cost is a second, small address space and extra opcodes.

Memory-mapped I/O has one catch: a load from a device register is not like a load from RAM. Reading a data register may remove a byte from the device's queue, so the value can change between two reads. That is why such regions must never be cached, and why compilers need a hint (volatile in C) not to optimize repeated reads away.

The next example builds a tiny memory-mapped system. Addresses 0 to 15 are RAM. Address 0x10 is the status register of a toy serial device, and 0x11 is its data register.

Python A memory-mapped serial device

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The loop uses only load, yet each load of the data register changes the device. The last lines make the side effect obvious: two reads of the very same address return 65 ("A") and then 66 ("B"). RAM would return the same value twice.

Method 1: programmed I/O (polling)

The simplest scheme is polling: the CPU runs a loop that reads the status register until the device says "ready", then moves the byte. You just saw it in the example above.

TEXT
wait:   load  r1, STATUS
        and   r1, r1, READY_BIT
        beq   r1, zero, wait      ; not ready, ask again
        load  r2, DATA            ; ready: take the byte

Polling is easy to write and has no hardware requirements, and it responds quickly because the CPU is always looking. Its weakness is waste. While the CPU spins in the loop it does nothing useful. If a device is ready once every 10,000 cycles, then 9,999 of those cycles are spent asking "are we there yet?". Polling makes sense only when the device is fast, or the system has nothing better to do (tiny embedded controllers, or a network card under such heavy load that a packet is always waiting).

Method 2: interrupt-driven I/O

With interrupts the device tells the CPU when it needs attention. The CPU keeps running its program. When the device raises an interrupt request (IRQ) line, the CPU finishes the current instruction and then:

  1. Saves the program counter and flags, so it can come back.
  2. Looks up the address of the right interrupt service routine (ISR) in the interrupt vector table, indexed by the interrupt number.
  3. Runs the ISR, which usually reads the data register, stores the byte in a buffer and acknowledges the device.
  4. Restores the saved state and returns to the interrupted program as if nothing happened.
sequenceDiagram
  participant P as Program
  participant C as CPU
  participant D as Device
  P->>C: normal instructions
  D->>C: IRQ (byte ready)
  C->>C: save PC and flags
  C->>C: jump through vector table
  C->>D: ISR reads data register
  C->>P: return and resume

An interrupt borrows the CPU for a short handler, then gives it back.

Interrupts are not free. Saving state, switching to the handler and returning costs hundreds of cycles, and the work disturbs caches and pipelines. They also come in flavors worth knowing:

  • Maskable interrupts can be switched off for a while (critical sections); non-maskable ones (hardware failure) cannot.
  • Priorities decide what happens when several devices call at once. A higher priority device may interrupt a lower priority handler.
  • A programmable interrupt controller (the PIC, APIC or GIC, depending on the machine) collects the IRQ lines and presents them to the CPU in order.
  • Exceptions (divide by zero, page fault) and traps (system calls) use the same vector mechanism, but come from inside the CPU.

Interrupts remove the waiting, but each byte still costs one interrupt. For a disk that delivers hundreds of megabytes per second, taking an interrupt per byte would drown the CPU.

Vectored and non-vectored interrupts

The two ways a CPU finds the service routine give two classes of interrupt.

  • Vectored interrupts: the interrupt source implies a fixed address (or a table slot) for its handler. The CPU jumps there with no extra lookup, so the response is fast. The 8085's TRAP, RST 5.5, RST 6.5 and RST 7.5 pins are vectored: each one jumps to a hard-wired address (0024H, 002CH, 0034H and 003CH).
  • Non-vectored interrupts: the CPU does not know the handler's address. After accepting the request it signals an acknowledge, and the device supplies the information, either the handler address or an instruction such as RST n, on the data bus. The 8085's INTR pin works this way. It is flexible (many devices share one line) but slower, because of the acknowledge cycle and the extra hardware.

The 8085 also shows priority and masking clearly: TRAP is highest and cannot be masked, then RST 7.5, RST 6.5, RST 5.5 and last INTR. The three RST lines can be masked individually and all of them (and INTR) are blocked by the disable-interrupts instruction DI.

Python Choosing which interrupt the CPU accepts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last line shows why the software interrupt RST n is so compact: it is a one-byte instruction whose target is simply 8 times n, so a device that must answer an INTR only has to place one byte on the data bus.

Method 3: direct memory access (DMA)

The fix is to take the CPU out of the byte-moving business. A DMA controller is a small piece of hardware that can become a bus master and copy data between a device and memory by itself. The CPU only sets up the job:

  1. The driver writes the memory address, the byte count and the direction into DMA registers.
  2. The driver starts the transfer and goes back to other work.
  3. The DMA controller moves the data, one bus transaction at a time. If it needs the bus while the CPU wants it, an arbiter decides who goes first (see Buses and Interconnects).
  4. When the count reaches zero, the controller raises one interrupt: "done".

Instead of one interrupt per byte, there is one setup and one completion interrupt per block. The cost of a DMA transfer is therefore almost independent of its size, as far as the CPU is concerned. Memory is still shared, so the DMA controller can slow the CPU slightly, either by stealing single bus cycles (cycle stealing) or by holding the bus for a short burst.

DMA brings its own care points. The data in main memory may be newer than the data in the CPU cache (or the other way round), so drivers must flush or invalidate cache lines around a transfer, or use hardware that keeps them coherent. DMA also needs protection: a buggy or malicious device writing anywhere in memory is dangerous, so modern systems put an IOMMU between devices and memory, just as the MMU protects programs.

Comparing the three methods

Let us put numbers on it. The model below transfers 1,000 bytes from a slow device that produces one byte every 5,000 CPU cycles. We assume a polling check costs 10 cycles, an interrupt (entry, handler and return) costs 400 cycles per byte, and a DMA setup costs 2,000 cycles plus one 400-cycle completion interrupt. We measure how many cycles of the whole transfer the CPU spends on the transfer itself, and how many it keeps free for other work.

Python Cost of polling, interrupts and DMA

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The device sets the pace, so the transfer takes the same time in all three cases. What differs is how much of that time belongs to the CPU. Polling keeps it at 100 percent. Interrupts cut the cost to 8 percent. DMA reduces it to well under 0.1 percent.

Interrupts do not always win, though. Make the device fast and the picture changes. This second experiment varies the gap between bytes and shows the CPU load of interrupts against polling: once bytes arrive more often than every 400 cycles, the interrupt scheme cannot even keep up, and per-byte interrupts stop making sense long before that.

Python When does polling beat interrupts?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Real systems mix the techniques. A network driver may use interrupts when traffic is light and switch to polling when packets arrive in a flood. A disk or network card moves its data with DMA but still uses an interrupt to say "finished". Even a keyboard, the slowest device in the machine, is happily served by interrupts.

Polling Interrupts DMA
Who moves the data? CPU CPU (in the ISR) DMA controller
CPU wasted while waiting All of it None None
CPU cost per byte Poll loop One interrupt Almost zero
Hardware needed Status register IRQ line, controller DMA controller, bus mastering
Good for Fast, simple devices Slow or sporadic devices Bulk transfers

DMA transfer modes and the controller

A DMA controller (the classic example is the Intel 8237) takes the processor's place on the bus while it moves a block between a device and memory. To start a transfer, the CPU programs the controller's registers; after that the controller drives the address and control lines itself.

What the controller holds

Register Role
Address register Memory address of the next byte or word to move; incremented (or decremented) after each transfer
Word count register How many transfers remain; the transfer ends when it reaches zero
Control / mode register Direction (read from or write to memory), mode, auto-initialization
Status register Finished, request pending

The 8237 has four independent channels, each with its own address and count registers, so four devices (floppy, sound card, ...) can share one controller. Each device asserts a DREQ (DMA request) line. The controller asks the CPU for the bus with HRQ (hold request); when the CPU finishes its current bus cycle it answers HLDA (hold acknowledge) and floats its bus pins. The controller then sends DACK to the device and performs the transfer. When the count hits zero it raises a terminal count signal and an interrupt to tell the CPU.

sequenceDiagram
  participant Dev as Device
  participant DMA as DMA controller
  participant CPU as CPU
  participant Mem as Memory
  CPU->>DMA: program address, count, mode
  Dev->>DMA: DREQ
  DMA->>CPU: HRQ (bus request)
  CPU-->>DMA: HLDA (bus granted)
  DMA->>Dev: DACK
  loop until count = 0
    Dev->>Mem: data via DMA-driven bus cycle
  end
  DMA->>CPU: interrupt (done)

The CPU only sets up the transfer and handles the final interrupt.

Three ways to share the bus

Mode How the bus is shared CPU effect Best for
Burst (block) The controller keeps the bus until the whole block is moved CPU is frozen for the whole block Fast devices, large blocks
Cycle stealing The controller takes the bus for one transfer at a time, then gives it back CPU slowed slightly, never frozen long Slow devices, real-time CPUs
Transparent (interleaved) The controller uses only cycles in which the CPU is not using the bus No slowdown at all Systems where the CPU has idle bus cycles

Burst mode has the best transfer speed but can starve the CPU (and delay interrupts). Cycle stealing keeps the CPU running at the price of a slower transfer. Transparent mode is invisible to the CPU, but it needs extra logic to detect free cycles and may make the transfer very slow if the CPU is busy.

Python Time to move a block in each DMA mode

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The numbers are simplified, but they show the trade-off the three modes make between transfer speed and interference with the CPU. In a system with a cache, the CPU often runs from the cache while the controller owns the bus, so DMA interference is smaller than it looks. Modern chips integrate DMA engines into the device (bus-master DMA, PCIe) instead of using a shared central controller, but the registers (address, count, control) and the idea of the CPU programming a transfer and getting a completion interrupt remain the same. One more issue is cache coherence: if a device writes memory by DMA, the cache may still hold the old data, so the system must flush or invalidate the affected lines.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the main idea of memory-mapped I/O?

    Memory-mapped I/O routes a range of addresses to controller registers, so no dedicated I/O instructions are needed. Such regions must not be cached.
  2. 2. Why is polling wasteful for a slow device?

    While the CPU spins on the status register it can do nothing else, and almost every check finds the device still not ready.
  3. 3. What does the CPU do first when it accepts an interrupt?

    The CPU saves enough state to resume the interrupted program, then uses the interrupt number to find the right service routine.
  4. 4. Why is DMA better than one interrupt per byte for a fast disk?

    The DMA controller moves the data itself. The CPU pays for setup and one interrupt, not for every byte.
  5. 5. What does an IOMMU add to a DMA system?

    Like the MMU for programs, the IOMMU limits which memory a device may reach, so a faulty device cannot overwrite the whole system.

Summary

  • A device controller exposes status, command and data registers; a driver uses them.
  • Memory-mapped I/O maps those registers into the address space; port-mapped I/O uses separate instructions. Device regions must not be cached.
  • Polling is simple and low-latency but burns CPU cycles while waiting.
  • Interrupts let the CPU work until the device calls; each one costs state saving, a vector-table jump and a return.
  • DMA lets a controller copy blocks to and from memory, and interrupts the CPU only once at the end.
  • Real drivers mix the methods, for example interrupts when idle and polling under heavy load.

Continue with Buses and Interconnects.