Z80 Assembly 71: I/O Ports and the Keyboard Matrix Scan

The Spectrum’s Key I/O Port The ZX Spectrum is minimalistic, relying on just a few I/O ports to manage all peripherals (keyboard, display, cassette, and beeper). The Main Port: The most crucial I/O address is `FEH′ (254 decimal), which is used for both input and output control. I/O Port Decoding (The Trick) The Spectrum simplifies hardware design by only connecting I/O logic to the lowest bit (A0) and the highest byte (A8-A15) of the Z80’s 16-bit address lines. ...

September 28, 2025

Z80 Assembly 58: Interrupt-Driven Keyboard Handling

The Drawback of Main Loop Polling In simple programs, checking the keyboard (`IN′ instruction) is done within the main program loop. This is inefficient because the CPU spends valuable time polling the keyboard even when no key is pressed, slowing down the main application. The Solution: Timer-Driven Interrupts A robust operating system uses a fixed-rate timer interrupt (e.g., 50 or 60 times per second) to trigger the keyboard scan routine. This ensures the keyboard is checked reliably without interfering with the main application’s logic. ...

September 27, 2025

Z80 Assembly 22: Reading Keyboard Input via I/O Ports

The Keyboard Matrix Scan Unlike modern PCs, Z80 systems don’t receive ASCII codes from the keyboard. Instead, the keyboard is wired as a matrix of rows and columns. To detect a keypress, the Z80 must scan this matrix using its I/O ports. The Two-Step Process: Output (Strobe): The CPU writes a value to an I/O port to select (or “strobe”) a single row of the keyboard matrix. Input (Read): The CPU reads a value from another I/O port. The bits set to ‘1’ in the input byte indicate which keys in the selected row are currently being pressed. Example: Scanning a Single Row Let’s assume our target system uses I/O port FEH for both output (strobe) and input (read) and that the rows are addressed by setting one bit to ‘0’. ...

September 27, 2025