Sub topic 1
I/O Interface Modules and Device Registers
Prerequisites
This chapter assumes you already know the following:
- Basic computer organization: the CPU, registers, memory and the buses that connect them.
- The instruction cycle (fetch, decode, execute).
- Binary and hexadecimal numbers, because register addresses and bit masks are written in hex.
- How a stack and a subroutine call and return work. You need this later for interrupts, but not in this chapter.
Why a CPU cannot talk to a device directly
A keyboard, a disk and a network card differ from the CPU and from each other in several ways:
- Speed. A CPU executes billions of instructions per second. A keyboard produces a few bytes per second. A disk produces megabytes per second.
- Data format. Devices may send data one bit at a time (serial), a byte at a time, or in blocks. The CPU works with words.
- Signal levels and timing. Each device has its own voltages, clocks and control signals.
- Control behavior. A printer needs "start a line", a disk needs "seek to cylinder 40", and a keyboard needs nothing.
If the CPU had to handle all of this itself, every processor would need custom logic for every device. The standard solution is to put a hardware layer between them. This layer is called the I/O interface module (also called an I/O controller, I/O adapter or device controller).
What an I/O interface module does
An I/O interface module is a hardware unit that connects one or more peripheral devices to the system bus. The CPU sees a uniform, simple set of registers. The module hides the device's electrical and timing details behind them.
The usual responsibilities are:
- Control and timing. It coordinates the flow of data between the device and the rest of the system.
- CPU communication. It decodes commands from the CPU, reports status back, and recognizes its own address on the bus.
- Device communication. It sends commands to the device and reads the device's signals.
- Data buffering. It holds data temporarily so that a slow device and a fast CPU can each run at their own speed.
- Error detection. It detects problems such as a parity error, a paper jam or a failed seek, and reports them in a status register.
- Data conversion. It converts between serial and parallel form, and between device signal levels and bus levels.
The module has two sides. The CPU side connects to the address, data and control buses. The device side connects through device-specific lines, usually called a port.
The device registers
The CPU never reaches into the device. It reads and writes a small set of registers inside the interface module. Almost every interface has these three kinds.
Status register
The status register reports the current condition of the device and the module. The CPU reads it. Typical bits are:
- READY (or DONE): the device has data for the CPU to read, or is ready to accept new data.
- BUSY: the device is working and cannot accept a new command.
- ERROR: the last operation failed.
- Interrupt pending: the module has requested an interrupt that has not been serviced yet.
Control register
The control register carries commands from the CPU to the module. The CPU writes it. Typical bits are:
- START or GO: begin an operation.
- Direction or mode: read versus write, or which operating mode to use.
- Interrupt enable: whether the module may raise an interrupt when it finishes.
- Reset: return the module to a known state.
Data register
The data register holds the actual byte or word being transferred. On input, the device places data into it and the CPU reads it. On output, the CPU writes it and the module passes it to the device. Some designs use separate input and output data registers (often called a data-in and a data-out buffer). Others use one register for both directions.
The following table summarizes the three register types.
| Register | Written by | Read by | Purpose |
|---|---|---|---|
| Status | Interface module | CPU | Report device and module state |
| Control | CPU | Interface module | Issue commands and set modes |
| Data | CPU (output) or device (input) | Device (output) or CPU (input) | Hold the byte or word being transferred |
Note that status and control are sometimes combined into one physical register with different bits for each purpose. Some bits are read-only and some are write-only, so reading back a control register does not always give you what you wrote.
A concrete register layout
Suppose a simple input device has an interface with two registers at consecutive addresses. Bit 0 of the status register is READY, and bit 7 is ERROR.
| Offset | Name | Bits |
|---|---|---|
| +0 | STATUS | bit 0 = READY, bit 7 = ERROR |
| +1 | DATA | 8-bit received byte |
To test READY, the CPU reads STATUS and applies a mask. A mask is a constant with 1 in the bits you care about, combined with the register value using bitwise AND. The result is nonzero only if the chosen bit is set.
STATUS_READY = 0x01 # bit 0
STATUS_ERROR = 0x80 # bit 7
def describe(status):
ready = (status & STATUS_READY) != 0
error = (status & STATUS_ERROR) != 0
return f"status=0x{status:02X} ready={ready} error={error}"
for value in (0x00, 0x01, 0x81, 0x80):
print(describe(value))This prints:
status=0x00 ready=False error=False
status=0x01 ready=True error=False
status=0x81 ready=True error=True
status=0x80 ready=False error=TrueThe same pattern appears again in the C examples in the next chapters, where the CPU waits for STATUS_REG & READY_BIT to become nonzero.
Side effects of reading and writing registers
Device registers are not ordinary memory. Their behavior differs in ways that matter for correct code and for exam questions.
- Reading the data register can change the device state. On many interfaces, reading the data register clears the READY bit and acknowledges the interrupt. A second read may return the next byte, or stale data.
- Writing the control register can trigger an action. Writing the START bit begins an operation immediately.
- The value can change without the CPU doing anything. The device updates the status register on its own. A compiler may assume that two reads of the same location return the same value and remove the second one. In C, the
volatilequalifier tells the compiler not to make this assumption. Without it, a polling loop can be optimized into an infinite loop that never rereads the register. - Some bits are write-1-to-clear. Writing a 1 to an error bit clears it, while writing 0 leaves it unchanged.
Ports and addressing
Each register in an interface module has an address. The set of registers for one device is called a port. The CPU selects a register by placing its address on the address bus. Address-decoding logic in the module compares that address with its own assigned range and responds only on a match.
There are two ways to give these registers addresses:
- They can share the memory address space (memory-mapped I/O).
- They can have their own separate address space with special instructions (isolated or port-mapped I/O).
The next chapter covers the two schemes in detail. For this chapter, the point is that either way the CPU talks to the device only by reading and writing these registers.
How a transfer uses the registers
Here is the typical sequence for reading one byte from an input device, using the registers described above:
- The CPU writes the control register to start the operation, setting the START bit.
- The device works. The module sets BUSY and clears READY.
- When the device has produced the byte, the module places it in the data register and sets READY.
- The CPU learns that READY is set. It can find out by reading the status register repeatedly or by receiving an interrupt. These are the subjects of the next chapters.
- The CPU reads the data register. The module clears READY, which readies it for the next byte.
- If ERROR is set, the CPU runs error-handling code instead of using the data.
The way the CPU learns about step 4 is what separates the three I/O techniques in this tutorial:
| Technique | How the CPU learns the device is ready | How data reaches memory |
|---|---|---|
| Programmed I/O | CPU repeatedly reads the status register | CPU reads the data register and stores the value |
| Interrupt-driven I/O | Module raises an interrupt request | CPU reads the data register in the interrupt service routine |
| DMA | Controller signals once, after the whole block | DMA controller moves data directly to memory |
Common mistakes
- Treating a device register like RAM. Reading twice is not the same as reading once, and a value written is not always the value read back.
- Forgetting the mask. Comparing the whole status register with 1 fails when other status bits are set. Use
(status & READY) != 0. - Forgetting that the interface adds time. The device is slow, but the module also takes time to decode commands and set status bits. Exam problems often give this as a fixed overhead.
- Mixing up the direction of status and control. The CPU reads status and writes control. The module does the opposite.
- Confusing the module with the device. The interface module is the electronics on the bus side. The device is the keyboard, disk or printer behind it.
Exercises
Exercise
A device interface has an 8-bit status register. Bit 0 is READY, bit 1 is BUSY and bit 7 is ERROR. The CPU reads the value 0x83. Which conditions are true, and what mask would you use to test only for ERROR?
Show answer
0x83 is 1000 0011 in binary. Bits 7, 1 and 0 are set, so ERROR, BUSY and READY are all true. This is an unusual combination that a real device would probably not report, but the arithmetic is the same. To test only for ERROR, use the mask 0x80 and check that (status & 0x80) is nonzero.
Exercise
Why is the data register usually declared volatile in C code that polls a device? What can go wrong without it?
Show answer
The device can change the register without any instruction from the CPU. Without volatile, the compiler may assume the value cannot change, read the register once, and keep the result in a CPU register. A polling loop such as while ((STATUS & READY) == 0) would then test the cached value forever and never see the device become ready.
Exercise
Name the three kinds of register in a typical I/O interface module, and state for each whether the CPU reads it, writes it, or both.
Show answer
The status register is read by the CPU (the module writes it). The control register is written by the CPU (the module reads it). The data register is read by the CPU on input and written by the CPU on output, so the CPU does both depending on direction.