port 0tick01IRQ1A001IRQ201IRQ0DMATINY-8IRQ0 → 32r00r30PC0SAVED··200··201··202··203··NET 0 IN r3 2 ADDI r3, 0 4 JZ 0 0 LDI r0, 0 2 ADDI r0, 1 4 JMP 232 IN r334 ST r3, [200]
Lit trace with moving dashes: it carries the value the step is aboutWhite tags riding the traces: the values themselvesFront faces: each part's current value (·· = nothing yet)Round lamps: the interrupt lines IRQ0 to IRQ2, glowing = 1First the CPU polls the keyboard, reading port 0 over and over. Then a key raises an interrupt line instead: the CPU finishes ADDI r0, 1, saves the PC (4), runs the two-line handler at 32 and returns. A timer interrupts on schedule, and a DMA controller copies network bytes into memory without the CPU. Polling, interrupts and DMA are the three ways a CPU deals with devices: with polling the CPU keeps reading a device's status and wastes time waiting; with interrupts the device signals when it needs attention and the CPU runs a handler; with DMA a separate engine copies the data and interrupts once when it is done (Arpaci-Dusseau, Operating Systems: Three Easy Pieces, chapter 36). TINY-8, this course's teaching CPU, has IN and OUT on port 0 but no interrupt line and no return instruction. The figure adds the interrupt as hardware around the unchanged instruction set: a saved-PC register, a fixed handler address 32 for the keyboard's line, and a return that copies the saved PC back. Every instruction, byte and value comes from the TINY-8 simulator.

Talking to devices

  1. I/O means input and output: how a CPU swaps data with devices such as a keyboard or a timer. Devices are much slower than the CPU, and they need attention at moments the CPU cannot predict, such as when you press a key.

An interrupt

Timers and DMA

The working

;; step 1/11 · devices
;; about: TINY-8 CPU, Keyboard, Timer
I/O = input/output

input devices:  keyboard, network card
other devices:  timer, disk, screen

The CPU runs one instruction after
another. A device needs attention at
times the CPU cannot predict.

Two ways to find out:
  polling     the CPU keeps asking
  interrupts  the device speaks up

A lit wire with moving dashes is carrying the value the step is about; the white tags riding on it are those values. The round lamps show each interrupt line: glowing means 1. The Python mirrors the hardware's check between instructions: if the line is up, keep the PC in 'saved' and load the handler's address.