Chapter 31 of 39

I/O Organization & Interfaces

So far we have studied the CPU and the memory system. But a processor that only computes and stores is a closed box: it needs keyboards, displays, disks, SSDs, network cards and sensors to be useful. These are the input/output (I/O) devices, also called peripherals.

Connecting peripherals is harder than connecting memory. Memory is fast, all of it behaves the same way and it is always ready. Peripherals are slow or very fast, use bytes or blocks or packets, are electromechanical or electronic, and are ready only when they decide. In this chapter we see how hardware bridges that gap: the I/O interface (I/O module), how the CPU addresses device registers (isolated vs memory-mapped I/O), how two units with different clocks exchange data safely (strobe and handshaking), serial vs parallel transfer with worked framing calculations, I/O processors and the common real-world interfaces (UART, I2C, SPI, USB, SATA, PCIe).

The three ways of actually moving data (programmed I/O, interrupts and DMA) get their own chapters next: Programmed I/O & Interrupts and Direct Memory Access (DMA).


Why Peripherals Need an Interface

Why can a disk or keyboard not simply hang off the system bus like a memory chip? Because of four kinds of mismatch:

  • Speed mismatch. A keyboard delivers a few bytes per second; an NVMe SSD several gigabytes per second; the CPU executes billions of instructions per second. Something must buffer data and synchronise the two sides.

  • Format mismatch. The CPU works with 32 or 64-bit words; a serial line sends one bit at a time; a disk works in 512-byte or 4 KB sectors.

  • Signal mismatch. Devices may be electromechanical, use different voltages, or have their own timing. The CPU bus has a fixed protocol.

  • Control mismatch. Each device type needs its own commands (seek, rewind, reset, set baud rate) and reports its own status (busy, error, paper out).

I/O interface (I/O module, device controller): the hardware unit placed between the system bus and one or more peripherals. It decodes addresses and commands from the CPU, buffers data, converts formats, reports device status and synchronises the timing of the device with the timing of the CPU.


Structure of an I/O Interface

From the CPU's point of view, an interface is just a small set of registers (often called ports). The CPU reads and writes these registers; the interface logic does the rest.

        System bus side                         Device side
   +----------------------------------------------------------+
   |                  I/O INTERFACE                           |
   |                                                          |
 data lines  <----> [ Data register(s) / buffer ] <---->  data to/from device
   |                                                          |
 address  --> [ Address decoder ] --> chip select,          |
 lines        (is this port mine?)    register select        |
   |                                                          |
 control  --> [ Control logic ] ----> [ Control register ] --> commands to device
 lines        (read / write)          [ Status register  ] <-- ready, busy, error
   |                                                          |
 interrupt <-- request line (optional)                       |
   +----------------------------------------------------------+

Register

Direction

Typical contents

Data register (data-in / data-out)

Both

The byte or word being transferred; may be a FIFO buffer

Status register

Device to CPU

Flag bits: data ready, transmitter empty, busy, error, interrupt pending

Control (command) register

CPU to device

Mode, start/stop, interrupt enable, baud rate, read/write command

The functions of an I/O module, as usually listed in textbooks, are: control and timing, CPU communication (command decoding, data, status reporting, address recognition), device communication, data buffering and error detection (for example parity).

Address Decoding: "Is This Port Mine?"

Every interface watches the address lines and responds only to its own addresses. Suppose an interface has four registers and sits on an 8-bit I/O address bus at base address 40H:

Address   A7 A6 A5 A4 A3 A2 | A1 A0   Register selected
40H        0  1  0  0  0  0 |  0  0   Port A (data)
41H        0  1  0  0  0  0 |  0  1   Port B (data)
42H        0  1  0  0  0  0 |  1  0   Control register
43H        0  1  0  0  0  0 |  1  1   Status register
           \______________/   \___/
           chip select (CS)   register select (RS1 RS0)

CS = A7' . A6 . A5' . A4' . A3' . A2'

The six high bits must match the pattern 010000 for the chip to be selected; the two low bits choose one of its four registers. This is exactly the same idea as decoding memory chips in Main Memory Organization & Memory Chips.


I/O Bus vs Memory Bus

The processor must talk both to memory and to I/O interfaces. There are three classic ways to arrange the buses:

  1. Separate buses: one set of address, data and control lines for memory and a completely separate set for I/O (used where an I/O processor handles I/O).

  2. Common address and data lines, separate control lines: the same address and data lines are shared, but there are different read/write control lines for memory (MEMR, MEMW) and for I/O (IOR, IOW). This gives isolated I/O.

  3. Common bus with common control lines: memory and I/O share everything. This gives memory-mapped I/O.

Isolated I/O (I/O-mapped I/O, port-mapped I/O)

The I/O devices have their own address space, separate from memory. The CPU uses special instructions (for example x86 IN and OUT), which activate the I/O read/write control lines instead of the memory ones. Address 40H as an I/O port and address 40H in memory are two completely different things.

On x86, IN AL, imm8 can name 256 ports directly, and using the DX register gives the full 16-bit port space of 65,536 ports.

Memory-Mapped I/O

Some memory addresses are assigned to device registers. There is only one address space and no special I/O instructions: an ordinary load reads a status register and an ordinary store writes a control register. ARM and RISC-V use memory-mapped I/O exclusively; modern x86 systems use it for almost all PCIe devices too.

Point

Isolated I/O

Memory-mapped I/O

Address space

Separate for I/O and memory

One common space; part of it is for I/O

Instructions

Special (IN, OUT)

Any memory-reference instruction (load, store, even add-to-memory)

Control lines

Separate IOR/IOW and MEMR/MEMW

Same read/write lines for both

Memory space available

Full address space for memory

Reduced by the addresses given to I/O

Addressing modes on I/O

Few (only those the I/O instructions offer)

All addressing modes of the ISA

Protection

Make I/O instructions privileged

Do not map the I/O pages into user programs

Caution

Decoding is simpler for small systems

I/O addresses must be marked non-cacheable, otherwise the CPU might read a stale status value from the cache

Examples

Legacy x86 ports (keyboard controller, serial COM ports)

ARM, RISC-V, PCIe devices, graphics frame buffers

Worked example. A processor has a 16-bit address bus and uses memory-mapped I/O, reserving the top 4 KB of the address space for device registers. How much memory can it address?

Total address space = 2^16 = 65,536 bytes = 64 KB
Reserved for I/O    = 4 KB = 4,096 bytes
Usable memory       = 65,536 - 4,096 = 61,440 bytes = 60 KB

With isolated I/O, the same processor could address the full 64 KB of memory plus a separate I/O space.


Synchronous and Asynchronous Data Transfer

Inside the CPU, all registers share one clock, so a transfer is just "on this clock edge, copy". But the CPU and a device interface usually run on independent clocks. If the sender changes the data at the same moment the receiver samples it, the receiver may capture garbage. So asynchronous transfers need control signals that say when data is valid. There are two methods.

Strobe Control

A single control line, the strobe, marks the moment data is valid.

  • Source-initiated: the source puts data on the bus, then raises the strobe; the destination latches the data while the strobe is active; the source removes the strobe, then the data.

  • Destination-initiated: the destination raises the strobe to ask for data; the source responds by placing data on the bus; the destination latches it and drops the strobe.

Source-initiated strobe

Data    ----<=========== valid data ===========>------
Strobe  ___________|"""""""""""""""""|________________
                   ^ destination latches data here

Weakness: it is one-way trust. In a source-initiated strobe, the source has no idea whether the destination really took the data. In a destination-initiated strobe, the destination does not know whether the source actually placed valid data. Nothing confirms the transfer.

Handshaking

Handshaking adds a second control line in the opposite direction, so each side confirms what the other did. For a source-initiated transfer the lines are data valid (source to destination) and data accepted (destination to source):

Source                                   Destination
  1. place data on bus
  2. raise DATA VALID          ------>
                                          3. latch data
                               <------    4. raise DATA ACCEPTED
  5. drop DATA VALID,
     remove data from bus      ------>
                                          6. see DATA VALID low,
                               <------       drop DATA ACCEPTED
  7. ready for next transfer

For a destination-initiated transfer the lines become ready for data (destination to source) and data valid (source to destination): the destination says it is ready, the source puts data and raises data valid, the destination takes it and drops ready, the source drops data valid.

Why it works: every step waits for a confirmation, so the transfer is correct no matter how fast or slow either side is. If one side never answers, the other can detect it with a timeout and report an error, which is how missing devices are discovered.

Feature

Strobe

Handshaking

Control lines

1

2 (one in each direction)

Confirmation of transfer

None

Yes, every step acknowledged

Works with units of different speeds

Only if timing is guaranteed in advance

Yes, adapts automatically

Error detection

No

Timeout if no response


Parallel vs Serial Transfer

In parallel transfer, all bits of a byte or word travel at once on separate wires. In serial transfer, bits travel one after another on a single line (per direction).

Parallel sounds faster, and for short internal paths it is. But at high speeds the wires of a wide parallel bus have slightly different delays (skew), so the receiver cannot be sure all bits arrived together, and neighbouring wires interfere (crosstalk). Fast serial links avoid both problems and need far fewer pins. That is why parallel ATA was replaced by Serial ATA, PCI by PCI Express and the parallel printer port by USB.

Asynchronous Serial Transfer (UART framing)

With no shared clock, the receiver must find where each character begins. Each character is wrapped in a frame:

idle line = 1
 ...1111 | 0 | d0 d1 d2 d3 d4 d5 d6 d7 | p | 1 | 1111...
         start      8 data bits         parity stop  idle
         bit        (LSB first)        (opt.)  bit(s)
  • The line rests at 1. A start bit (0) gives a 1-to-0 transition that tells the receiver "a character begins now"; it then samples each bit near its middle using its own clock set to the agreed rate.

  • Then come the data bits (5 to 8, least significant first), an optional parity bit for error detection, and one or more stop bits (1) that return the line to idle.

The circuit that does this is a UART (Universal Asynchronous Receiver-Transmitter). The common setting "8N1" means 8 data bits, no parity, 1 stop bit: 10 bits on the line per byte.

Worked example 1. A UART runs at 9600 bits/s. How many characters per second does it carry with (a) 8N1, (b) 8 data bits, even parity, 2 stop bits?

(a) bits per char = 1 start + 8 data + 0 parity + 1 stop = 10
    rate = 9600 / 10 = 960 characters/s       efficiency = 8/10 = 80%

(b) bits per char = 1 + 8 + 1 + 2 = 12
    rate = 9600 / 12 = 800 characters/s       efficiency = 8/12 = 66.7%

Sending a 1 KB file (1,024 bytes) with 8N1 takes 1024 x 10 / 9600 = 1.067 s, not 1024 x 8 / 9600 = 0.853 s. Forgetting the framing bits is the most common mistake in these problems.

Synchronous Serial Transfer

In synchronous transmission, sender and receiver share timing (a clock line, or clock recovered from the data), so characters are sent back to back in a large block. The block starts with a few sync characters instead of a start/stop pair around every character, so overhead is much lower for long blocks.

Worked example 2 (GATE-style). Two links each run at 1200 bits/s. Link T1 is asynchronous and sends each character as 8 information bits, 2 start bits, 1 stop bit and 1 parity bit. Link T2 is synchronous and sends 3 eight-bit sync characters followed by 30 eight-bit information characters. Find the character rate of each.

T1: bits per character = 8 + 2 + 1 + 1 = 12
    rate = 1200 / 12 = 100 characters/s

T2: bits per block = (3 + 30) x 8 = 264 bits, carrying 30 characters
    blocks per second = 1200 / 264 = 4.545...
    rate = 4.545... x 30 = 136.36 characters/s  (about 136)

Synchronous transfer wins because its overhead (3 sync characters) is shared by 30 data characters, while asynchronous transfer pays 4 extra bits on every single character.

High-speed serial links also lose some raw bandwidth to line encoding, which guarantees enough 0-1 transitions for the receiver to recover the clock. SATA uses 8b/10b encoding (every 8 data bits are sent as 10 line bits); PCIe 3.0 and later use the far more efficient 128b/130b.

SATA III: 6 Gbit/s x 8/10 = 4.8 Gbit/s = 600 MB/s of data

PCIe 3.0: 8 GT/s per lane x 128/130 = 7.877 Gbit/s = 984.6 MB/s per lane
          x4 link: about 3.94 GB/s in each direction

Modes of I/O Transfer: an Overview

Once the interface exists, someone has to move each piece of data between the interface and memory. There are three modes, each covered in detail later:

Mode

Who moves the data

How the CPU learns the device is ready

Chapter

Programmed I/O

CPU, by instructions

CPU keeps polling the status register

Programmed I/O & Interrupts

Interrupt-driven I/O

CPU, inside an interrupt handler

Device raises an interrupt

Programmed I/O & Interrupts

Direct Memory Access

DMA controller, directly to memory

One interrupt at the end of the whole block

Direct Memory Access (DMA)


I/O Processors and Channels

Large systems go one step further than DMA: they add an I/O processor (IOP), also called an I/O channel. It is a processor with its own instruction set specialised for I/O. The CPU places a channel program (a list of I/O commands) in memory and tells the IOP to run it. The IOP then performs complete I/O operations, including formatting and error handling, and interrupts the CPU only when the whole program is finished.

IBM mainframes made channels famous. Three types are traditionally named:

  • Selector channel: serves one high-speed device at a time (for example a disk), transferring a whole block before switching.

  • Multiplexer (byte multiplexer) channel: interleaves many slow devices, byte by byte (terminals, card readers).

  • Block multiplexer channel: interleaves several high-speed devices block by block.

The evolution of I/O can be summarised as handing more and more work away from the CPU: CPU controls device directly, then an I/O module, then interrupts, then DMA, then an I/O processor executing its own programs. Modern GPUs, smart NICs and NVMe controllers continue this trend.


Common Real-World Interfaces

Interface

Type

Signal lines / notes

Typical use

UART (RS-232)

Asynchronous serial, point to point

TX, RX (plus ground); start/stop framing

Debug consoles, GPS and Bluetooth modules

I2C

Synchronous serial bus, multi-device

2 wires: SDA (data), SCL (clock); devices have addresses; 100 kbit/s standard, 400 kbit/s fast mode

Sensors, EEPROMs on a board

SPI

Synchronous serial, one master

4 wires: SCLK, MOSI, MISO, SS (one select per slave); full duplex

Flash chips, displays, ADCs

USB

Serial, host-controlled, hot-pluggable

USB 2.0: 480 Mbit/s; USB 3.2 Gen 1: 5 Gbit/s; the host polls devices and supplies power

Keyboards, drives, phones

SATA

Point-to-point serial storage link

SATA III: 6 Gbit/s line rate, 8b/10b, about 600 MB/s

Hard disks, 2.5-inch SSDs

PCI Express

Point-to-point serial lanes, packet based

x1, x4, x8, x16 lanes; 3.0: 8 GT/s per lane, 4.0: 16 GT/s, 5.0: 32 GT/s

GPUs, NVMe SSDs, network cards

Notice the trend: almost everything modern is serial and point-to-point, using memory-mapped registers and DMA.


Common Mistakes

  • Ignoring start, stop and parity bits when computing serial character rates. Always count bits per frame first.

  • Thinking memory-mapped I/O needs special instructions. It is isolated I/O that needs IN/OUT; memory-mapped I/O uses normal loads and stores.

  • Forgetting that memory-mapped I/O reduces memory space, and that its addresses must not be cached.

  • Confusing strobe with handshaking. Strobe is one control line with no acknowledgement; handshaking uses two lines and confirms each step.

  • "Parallel is always faster than serial." At high frequencies skew and crosstalk limit parallel buses; modern fast links are serial.

  • Mixing bits and bytes, or GT/s and GB/s. Line rate (bits or transfers per second) is not data rate; subtract encoding overhead and divide by 8.


GATE and Interview Questions

What is the difference between isolated I/O and memory-mapped I/O?

Isolated I/O has a separate address space for I/O ports, separate control lines (IOR/IOW) and special instructions such as IN/OUT. Memory-mapped I/O assigns device registers to ordinary memory addresses, so normal load/store instructions and all addressing modes work on them, at the cost of some memory address space.

A processor with a 20-bit address bus uses memory-mapped I/O and reserves 64 KB for devices. How much memory can be installed?

220 = 1 MB = 1024 KB total. 1024 - 64 = 960 KB of memory.

An asynchronous link at 4800 bits/s sends 7 data bits, 1 parity bit, 1 start bit and 1 stop bit per character. What is the character rate and efficiency?

Bits per character = 7 + 1 + 1 + 1 = 10, so 4800 / 10 = 480 characters/s. Efficiency = 7/10 = 70% (8/10 = 80% if you count the parity bit as useful).

Why is handshaking better than strobe control?

With a strobe, the initiating unit never knows whether the other side actually supplied or accepted the data. Handshaking adds a reply signal, so each step is acknowledged, units of any speed can communicate, and a missing or failed unit can be detected by a timeout.

Why must memory-mapped device registers be non-cacheable?

A status register changes because of the device, not because of the CPU. If it were cached, the CPU would keep reading the old cached value and never see the device become ready; writes to control registers might also sit in the cache instead of reaching the device.

What is an I/O channel (IOP) and how is it different from DMA?

A DMA controller only moves a block of data between a device and memory as programmed by the CPU. An I/O processor executes its own channel program from memory, can perform several I/O operations, format data and handle errors, and interrupts the CPU only when the whole program is done.


Summary

  • Peripherals differ from the CPU in speed, data format, signals and control, so they connect through an I/O interface with data, status and control registers and an address decoder.

  • Isolated I/O uses a separate address space and IN/OUT instructions; memory-mapped I/O puts device registers in the memory address space and uses ordinary loads and stores.

  • Asynchronous transfers use a strobe (no acknowledgement) or handshaking (two lines, every step confirmed).

  • Asynchronous serial characters carry start, parity and stop bits; at 9600 bit/s, 8N1 gives 960 characters/s. Synchronous blocks share overhead and are more efficient.

  • Data moves by programmed I/O, interrupts or DMA; big systems add I/O processors (channels).

  • Modern interfaces (USB, SATA, PCIe) are fast serial links with encoding overhead such as 8b/10b or 128b/130b.

Next, in Programmed I/O & Interrupts, we see how the CPU waits for a device: by polling it, or by letting the device interrupt.