All episodes · From One Switch to a CPU · Episode 17

How Your CPU Notices a Key Press: Bus, Polling, Interrupts, DMA

Pressing play loads the video from YouTube.

You press a key, and a few milliseconds later the letter is on the screen. But the processor was busy running billions of instructions for other things. How did it notice?
We connect the CPU, memory and devices with a bus, try polling (asking again and again), switch to interrupts (the device speaks up, the CPU saves where it was and jumps to a handler), and hand big copies to a DMA controller.

Every animation in this video is drawn by code, and the narration is an AI voice.

Transcript

You press a key. A few milliseconds later, the letter appears on the screen. But the processor was busy, running billions of instructions for other things. So how did it notice? Today: how the CPU talks to keyboards, screens and disks. Buses, polling, interrupts, and DMA.

The CPU, the memory, and the devices are connected by shared wires called a bus. Some lines carry an address, saying who is being talked to. Some carry the data itself. And control lines say read or write. Each device has a few registers of its own: one saying whether it has news, one holding the news. The CPU reads them just like memory addresses.

So the simplest approach: keep asking. Has a key been pressed? No. Now? No. Now? That's called polling. It works, but it wastes time. The processor checks thousands of times, and almost every answer is no.

The better way: let the device speak up. When a key is pressed, the keyboard, or the chip that looks after it, raises a signal on an interrupt line. The CPU finishes the instruction it's on, saves where it was, its program counter and registers, and jumps to a small program called an interrupt handler. The handler reads the key, hands it to the operating system, and then the CPU picks up exactly where it left off, as if nothing happened. No time wasted asking. The CPU only stops when there's actually something to do.

But what about moving a whole file from the disk into memory? Copying every byte through the CPU would keep it busy for ages. So a helper called a DMA controller does the copying itself, straight from the device into memory, while the CPU gets on with other work. When the transfer is done, it sends a single interrupt: your data is ready.

So: devices sit on the bus, polling asks again and again, interrupts let devices speak up, and DMA moves big data without the CPU. We've now met every part of a computer. Next time, the season finale: one key press, followed all the way through the whole machine. See you there.

Next episode, the season finale: one key press, followed through the whole machine.

Buy the Developer a Coffee

Entirely up to you: it works the same either way.

Every cup turns into the next update.

WeChat Pay QR code
WeChat Pay
Alipay QR code
Alipay
PayPal QR code
PayPal