CSE Roadmap

Core tier · about 3 hours · 7 parts

Part 2 of 7 · 2 min read

Controllers and registers

Think of a vending machine with three slots: one for money, one for the choice, and one for the item that comes out. A small circuit board behind them does the work. The analogy stops in one place: a vending machine has a fixed set of slots, and a device controller exposes a few numbered registers that the processor reads and writes like memory.

In plain words: A device does not talk to the processor directly. It is driven by a device controller, a small circuit that holds a few registers. The usual ones are a status register (is the device busy, done or in error), a command register (what to do) and a data register or buffer (the bytes to move). The processor starts work by writing to the command register and learns the result by reading the status register. These registers are reached in one of two ways. Port-mapped I/O uses special instructions and a separate address space of ports. Memory-mapped I/O gives each register an address in the ordinary address space, so the usual load and store instructions work.

A worked example (made up to show the idea). A controller maps three registers at the addresses 0xFE000000 (status), 0xFE000004 (command) and 0xFE000008 (data). Writing the value 1 to the command register means "read one word". Reading bit 0 of the status register gives 1 when the word is ready. The data register then holds the word.

The key lines. Linux shows the real ranges of a machine. This is the start of both files on the test machine.

$ head -4 /proc/ioports
0000-0cf7 : PCI Bus 0000:00
  0000-001f : dma1
  0020-0021 : pic1
  0040-0043 : timer0
$ head -4 /proc/iomem
00000000-00000fff : Reserved
00001000-0009fbff : System RAM
0009fc00-000fffff : Reserved
  000de000-000defff : AMZNC10C:00

The first file lists port ranges, which are the old port-mapped registers. The second lists memory-mapped ranges, which are ordinary addresses, some used by real memory and some by device registers.

Device controller

A device controller is the electronic part of an I/O device that the processor talks to. It exposes registers for status, commands and data, and it drives the device mechanics or circuits. The OS driver for the device reads and writes those registers.

Example: the controller of a disk accepts a command to read block 1,000, and later shows in its status register that the block is ready.

Check yourself: 1. Which register does the processor read to learn that a device is busy? 2. What is the difference between port-mapped and memory-mapped I/O? 3. Which file of the test machine lists the memory-mapped ranges?