Computer Science › Section C · Data manipulation › Topic C.5

Communicating with other devices

A CPU and its memory are useless without keyboards, screens, drives and networks. This topic shows how the processor reaches those devices through controllers, how the two sides keep in step, and how data rates decide what a connection can carry.

Beyond the CPU and memory

The CPU and main memory form the core of a computer, but everything useful passes through other devices, called peripherals. Input devices send data in: keyboards, mice, microphones, cameras and sensors. Output devices present results: displays, speakers, printers, and actuators such as motors that act on the physical world. Many devices, such as touchscreens and network adapters, do both. The question is how bit patterns get between the CPU and hardware that works at very different speeds and in very different forms.

Controllers and ports

The CPU does not drive a device directly. It works through a controller, circuitry that translates between the computer's internal signals and what the device needs. It may be built into the motherboard or sit on a plug-in card, and is often a small computer itself, with its own processor and memory running a program. External devices connect at a port. Shared standards let one controller handle many products: a single USB controller can address up to 127 devices. Other standards include HDMI and DisplayPort for screens, Thunderbolt for fast external devices, and PCI Express inside the case for graphics cards and SSDs. Wi-Fi and Bluetooth do the same job without cables.

Memory-mapped I/O and I/O instructions

Controllers attach to the same bus as the CPU and main memory, so the CPU can talk to them with ordinary data transfers. In memory-mapped I/O, each controller answers to a few reserved addresses, and main memory ignores those addresses. A STORE to one of them sends a byte to the device, and a LOAD from one reads a byte from it, with no special instructions needed. ARM processors work this way. The alternative is to give the machine language separate I/O instructions that name a device port rather than a memory address; x86 processors have such instructions, named IN and OUT, although they also use memory mapping for many devices.

Handshaking and status words

A computer can produce data far faster than a printer can accept it, and bytes poured at it blindly would be lost. So transfers involve handshaking, a two-way exchange in which the device reports its condition and the computer waits until the device is ready. The report usually takes the form of a status word, a bit map in which each bit means something. For a printer, one bit might mean "ready for more data", another "out of paper" and a third "paper jam". The masking techniques from "Arithmetic, logic, shift, and rotate instructions" are exactly how a program reads such bits.

Worked example: waiting for a display

Suppose a version of Sparrow maps a display controller to two addresses: reading 0xFE gives its status word, whose bit 0 is 1 when it is ready, and storing to 0xFF sends it one character. R5 holds 0x48, the ASCII code for "H". The program at 0x40: 1. 0x2F01 puts the mask 00000001 in RF. 2. At 0x42, 0x1EFE reads the status into RE. 3. 0x7EEF ANDs RE with the mask, keeping only bit 0. 4. 0xDE42: if the result is 0, the display is busy, so jump back to 0x42 and ask again. 5. 0x35FF sends the character. 6. 0x0000 halts. Check: in a simulation where the display became ready on the third status read, the loop ran three times and 0x48 reached the display once.

Polling versus interrupts

The loop in that example is polling: the CPU keeps asking the device whether it is ready. Polling is simple, but every check is CPU time spent doing nothing useful. The usual alternative is an interrupt. The device sends a signal to the CPU when it needs attention, for example when a key is pressed or a disk read finishes. The CPU then pauses its current program, saves enough information to resume it, runs a short routine called an interrupt handler for that device, and carries on where it left off. Interrupts let one processor respond quickly to many devices without constantly checking them.

Direct memory access

Because a controller sits on the bus, it can also read and write main memory itself in moments when the processor leaves the bus free. This is direct memory access (DMA). To load a block of data from an SSD, the CPU sends the request to the drive's controller, naming where in memory the data should go, and then gets on with other work. The controller copies the data into memory by DMA and usually raises an interrupt when it has finished. The two work at the same time, so the processor is not tied up for the whole of a slow transfer.

The von Neumann bottleneck

DMA has a cost: the CPU and several controllers now compete for the same bus. However well a system is built, the shared path between processor and memory limits performance, because every instruction and most data must cross it. This limit is called the von Neumann bottleneck, after the architecture in which one connection carries both. Caches ease it by keeping frequently used data close to the CPU. More generally, a system runs only as fast as its slowest busy part: if a video export keeps the drive working flat out while the CPU sits partly idle, a faster drive will help more than a faster processor.

Serial and parallel links

A parallel link sends several bits at the same moment, each on its own wire; a serial link sends bits one after another along a single path. Parallel seems faster, and a CPU's internal buses are parallel. But at very high speeds, keeping many wires precisely in step becomes difficult, so most modern connections are fast serial links: USB, SATA, Ethernet and the lanes of PCI Express all work this way. Over longer distances, homes reach the internet by serial links such as cable, optical fibre or satellite, as "The internet: architecture and addressing" explains.

Worked example: data rates

Data rates are measured in bits per second (bps), with kbps, Mbps and Gbps for thousands, millions and billions. A lower-case b means bits and a capital B means bytes, so 8 Mbps moves 1 MB per second. How long does a 12 MB photo take over a 100 Mbps link? 1. Convert to bits: 12 MB × 8 = 96 megabits. 2. Divide by the rate: 96 ÷ 100 = 0.96 seconds. 3. Check the units: megabits divided by megabits per second leaves seconds. Real transfers take a little longer, because protocols add extra control bits. The highest rate a link can carry is often loosely called its bandwidth.

Common mistakes to avoid

First, mixing bits and bytes: 100 Mbps is 12.5 MB per second, not 100. Second, thinking the CPU drives devices directly instead of through controllers. Third, assuming memory-mapped I/O needs special instructions; ordinary LOAD and STORE do the job. Fourth, believing parallel links are always faster; most fast modern links are serial. Fifth, confusing polling, where the CPU asks, with interrupts, where the device tells.

Key takeaways

Peripherals connect through controllers, often small computers themselves, at ports defined by standards such as USB, HDMI and PCI Express. With memory-mapped I/O a device answers to reserved addresses, so LOAD and STORE reach it; some machines add separate I/O instructions. Handshaking uses status words so the computer waits for the device. Polling asks repeatedly; interrupts let the device signal. DMA lets controllers use memory while the CPU works, at the cost of more competition for the bus, adding to the von Neumann bottleneck. Rates are in bits per second, and 1 byte is 8 bits.

Flashcards

Flashcard

Peripheral

A device outside the CPU and main memory

Keyboards, displays, drives, network adapters.

Test your understanding

Question 1 of 5

A machine uses memory-mapped I/O. How does a program send a byte to a device's controller?

Question 2 of 5

What is the main advantage of interrupts over polling?

Question 3 of 5

During a DMA transfer from a drive, what is the CPU doing?

Question 4 of 5

About how long does a 30 MB file take to transfer over a 240 Mbps link, ignoring protocol overhead?

Question 5 of 5

Why do most fast modern connections, such as USB and PCI Express, use serial links?

← Previous topicNext topic →