Skip to Content
Under the HoodEmulator InternalsZX Spectrum Next: keyboard, joysticks and mouse

Input: One Matrix, Many Hands

Plug a PC keyboard into a Next, press the up arrow, and a program written in 1983 sees CAPS SHIFT and 7, as if your fingers had found both keys on a rubber Spectrum. Plug in a joystick, set it to Sinclair mode, push it left, and the same old program sees the key 6. Neither the PC keyboard nor the joystick knows anything about the Spectrum. So who is doing the translating, and how does the program never notice?

Input is the part of a machine that everybody uses and nobody looks at. Yet it decides whether an old game works at all, whether a new one can tell a cursor key from CAPS SHIFT and 7, and whether a player with a Mega Drive pad gets all six buttons or only two. The Next keeps every old convention and adds a handful of new ones on top, and the way it does that is one of the neatest pieces of design in the machine.

You should know two chapters first. Memory, Ports and NextRegs: How the Z80N Reaches the Machine explains how ports are decoded and how a NextReg switches a group of them off. The Beeper: EAR, MIC and the Tape explains bit 6 of port $FE, the tape input, which shares its byte with the keyboard.

The road ahead

We start with the keyboard as the Spectrum knew it, eight half-rows of five keys behind port $FE, and the Next’s own keyboard, which has seven columns instead of five. Then comes the clerk, the scanner that walks the membrane and keeps a copy of it for the CPU, and the key idea of the chapter: the PS/2 keyboard and the joysticks do not talk to the CPU at all, they press keys in the clerk’s scan. If you read only one section, read that one. After it we look at the sixteen extra keys and the timing trick behind them, the PS/2 keyboard, the joystick modes, the reader that asks every joystick whether it is a Mega Drive pad, the Kempston mouse, the two buttons on the side of the case, and resets.

The facts are for core 3.02.01. Times are for the 28 MHz master clock of VGA mode 0, which paces the keyboard scanner and the joystick reader.

The Spectrum’s keyboard

The Spectrum’s 40 keys sit in a matrix: eight rows of five keys, each key a switch at the crossing of one row wire and one column wire. To read it, the CPU executes an IN from port $FE, any port with address line A0 low. The upper half of the address chooses the rows: every one of A8 to A15 that is 0 selects its half-row, and bits 0 to 4 of the byte read are the five columns. A pressed key reads 0; bits 5 and 7 always read 1, and bit 6 is the tape input.

So IN A,($FE) with $FB in A reads the row of Q, W, E, R and T, and a program that puts $00 in the upper byte reads all eight rows at once: each column bit is then 0 if a key in that column is down in any row. That is how a ROM checks “is any key pressed?” in one instruction.

The Next keeps every bit of this. But its own keyboard has seven columns, not five:

port $FE, bits 0-4extra columnsbit 0bit 1bit 2bit 3bit 4col 5col 6A8CAPSZXCVEXTENDUPA9ASDFGCAPS LOCKGRAPHA10QWERTTRUE VIDINV VIDA1112345BREAKEDITA1209876;“A13POIUY,.A14ENTERLKJHDELETERIGHTA15SPACESYMMNBLEFTDOWN
The Next’s keyboard matrix. Address lines A8 to A15 select the rows; columns 0 to 4, in blue, are the Spectrum’s 40 keys and read in bits 0 to 4 of port $FE. Columns 5 and 6, in orange, hold the Next’s sixteen extra keys, which port $FE cannot see directly.

The two extra columns hold the sixteen keys a Spectrum+ or a 128 had as separate keys but the 48K made with shifts: the arrows, DELETE, EDIT, BREAK, the punctuation that needed SYMBOL SHIFT, and a few more. A Spectrum program reads only bits 0 to 4, so it cannot see columns 5 and 6. How the extra keys still reach it is the story of the next three sections.

The clerk

On a 48K, the CPU’s address lines drive the keyboard’s rows directly: the read happens while the IN is on the bus. The Next does not work that way. The FPGA has its own scanner, which I call the clerk: it walks the membrane one row at a time, writes down what it finds, and hands the CPU its notes.

  1. The clerk pulls one row wire low and reads the seven column wires: a pressed key on that row pulls its column low too.
  2. It moves to the next row every 18.3 µs, 512 clocks of the master clock.
  3. After the eighth row it takes one more step, in which it copies its eight rows of notes into the matrix copy, all at once. A whole scan is nine steps, 164.6 µs: about 120 scans in a 50 Hz frame.
  4. A read of port $FE never touches the membrane. The row lines of the IN select rows of the matrix copy, the selected rows are combined, and the result is on the bus at once.

So a key pressed now shows up in a read at most one scan later, about a sixth of a millisecond, which no program can tell from “at once”. And because the copy is updated all at once, a program that reads the eight rows one after another can never catch half of an old scan and half of a new one in the same row.

One matrix, many hands

Here is the idea I like best in the whole input design. The clerk’s column wires do not come only from the membrane. Two more sources press keys in the clerk’s scan, at the very moment he reads a row:

  • The PS/2 keyboard. Its keys are translated into membrane keys by a table, and while the clerk reads row 3, the PS/2 side pulls low the columns of every row-3 key that a PS/2 key is holding.
  • The joysticks in the key modes (Sinclair, Cursor, user-defined). Pushing a joystick left in Sinclair mode pulls the column of key 6 while the clerk reads row 4.

All three are combined before the clerk writes anything down: a key is down when any of them presses it.

membrane8 rows, 7 columnsPS/2 keyboardvia the keymapjoysticks, key modesvia the joystick mapthe clerkscans a rowevery 18.3 µs,copies the matrixafter each scanport $FErows by A15-A8,columns in bits 0-4,extra keys as pairsNextRegs $B0, $B1the 16 extra keysthemselvesa key is down when any of the three presses it
One matrix, many hands. The membrane, the PS/2 keyboard and the joysticks in their key modes all press keys in the same scan. The clerk copies the result, and two readers take from the copy: port $FE, which sees the Spectrum’s five columns, and NextRegs $B0 and $B1, which see the sixteen extra keys.

Hey, notice what this buys. Everything that comes after the clerk (port $FE, the extra keys, the NMI button’s function keys) works the same for every hand. A Sinclair joystick and a finger on the membrane are the same key to the machine, so a program written before the Next existed works with all of them, without knowing any of them. If you ask me, this is how you add hardware to an old machine: by making the new hardware pretend to be the old one, as early as possible.

The sixteen extra keys

An extra key on its own would be invisible to a Spectrum program, so each one also enters two matrix keys, the pair a 48K owner would have pressed:

  • CAPS SHIFT and a key: EXTEND = CAPS + SYMBOL SHIFT, EDIT = CAPS + 1, CAPS LOCK = CAPS + 2, TRUE VIDEO = CAPS + 3, INV VIDEO = CAPS + 4, LEFT = CAPS + 5, DOWN = CAPS + 6, UP = CAPS + 7, RIGHT = CAPS + 8, GRAPH = CAPS + 9, DELETE = CAPS + 0, BREAK = CAPS + SPACE.
  • SYMBOL SHIFT and a key: ; = SYM + O, " = SYM + P, , = SYM + N, . = SYM + M.

A new program can do better than decode those pairs. The Extended Keys 0 and 1 registers, NextRegs $B0 and $B1, report the sixteen keys themselves, one bit each, 1 for pressed:

$B0: ; " , . UP DOWN LEFT RIGHT (bit 7 ... bit 0) $B1: DELETE EDIT BREAK INV TRUE GRAPH CAPSLOCK EXTEND

And bit 4 of the ULA Control register, NextReg $68, stops the extra keys from entering their pairs. A game that reads the arrows from $B0 sets it, and then an arrow is just an arrow: no stray CAPS SHIFT, no stray 7. The real CAPS SHIFT and 7 on the membrane are not affected. Here is the whole job:

; The arrows from NextReg $B0, without CAPS SHIFT Arrows: ld bc,$243b ld a,$68 ; ULA Control out (c),a inc b in a,(c) or %00010000 ; bit 4: no matrix entries out (c),a dec b ld a,$b0 ; Extended Keys 0 out (c),a inc b in a,(c) and %00001111 ; UP DOWN LEFT RIGHT ret

How it works:

  • Ports $243B and $253B select and access a NextReg, so the first half reads NextReg $68, sets bit 4 and writes it back, keeping the ULA’s other settings.
  • The second half reads NextReg $B0 and keeps the four arrow bits. With UP and LEFT held, A is $0A.
  • A soft reset clears bit 4 of $68, so the next program gets the pairs back. Set it again after a reset if you rely on it.

Why CAPS SHIFT comes early

There is a subtle trap in “UP is CAPS SHIFT and 7”. A program that reads the rows one at a time may read row 0 (CAPS SHIFT) in one copy and row 4 (the 7) in the next. If the two keys arrived in the same scan, a read straddling them could see 7 without CAPS SHIFT, and the cursor key would type a 7. I always remember this one because it is so easy to dismiss and so annoying to meet.

The clerk prevents it by treating the shift as the senior partner. For every extra key, the shift enters the matrix one scan before the other key and leaves two scans after it:

0123456789scanUP heldCAPS SHIFT7each column is one copy of the matrix, 164.6 µs; the CPU reads the latest
UP held during scans 2 to 4, as the CPU sees it in the matrix copies. CAPS SHIFT is down from the copy after scan 2 to the copy after scan 7; the 7 only from the copy after scan 3 to the copy after scan 5. Any read that sees the 7 also sees CAPS SHIFT.

So whatever order a program reads the rows in, and however slowly, it cannot see the 7 without its CAPS SHIFT, in either direction. The price is one scan of delay on the 7, 165 µs, which nobody will ever notice.

The PS/2 keyboard

A PS/2 keyboard does not have rows and columns; it sends a scan code when a key goes down and another when it comes up. The Next turns those codes into membrane keys with a keymap, a table of 512 entries: one for each scan code, plain and with the $E0 prefix that marks the extended keys of a PC keyboard. Each entry names a membrane row and column, and two flags: add CAPS SHIFT, add SYMBOL SHIFT. A US-layout /, for example, is SYMBOL SHIFT and V; the PC’s arrow keys are the Next’s arrow keys in column 6; Backspace is DELETE, Esc is BREAK, Tab is TRUE VIDEO.

Three details make it work like a keyboard and not like a list of codes:

  • The shifts are counted, not switched. Hold / (SYM + V) and , (SYM + N) together, release /, and SYMBOL SHIFT must stay down for the ,. The PS/2 side keeps a count of the keys that ask for each shift, and the shift is down while the count is above zero.
  • Repeats are ignored. A PC keyboard repeats the code of a held key, but a membrane key does not repeat, so a code identical to the last one is thrown away. The Spectrum ROM does its own key repeat, as it always did.
  • Pause clears everything. If a key ever seems stuck, the Pause/Break key resets the PS/2 side’s state.

Some entries are not keys at all but function keys, the machine’s own controls. In the US table F3 switches between 50 and 60 Hz, F4 is a soft reset, F8 changes the CPU speed, and F11 and F12 switch the expansion bus on and off; F9 and F10 stand in for the NMI and DivMMC buttons. NextReg $06 can disable the speed, 50/60 Hz and expansion bus hotkeys. What matters here is that they come from the same table, so a rewritten table can move them.

The table can be rewritten. NextReg $28 bit 7 = 0 selects the keymap, bit 0 is the top bit of the address (the $E0 flag) and NextReg $29 the other eight; each write of NextReg $2B stores an entry and steps the address. That is how a keyboard with another national layout can be made to type what its keycaps say. And bit 2 of NextReg $06 says which pins of the PS/2 connector carry the keyboard and which the mouse, so a splitter cable can bring both; it can only be changed in config mode, at boot.

Joysticks as keys

The Next has two joystick connectors, and the Peripheral 1 Setting register, NextReg $05, gives each a three-bit mode: joystick 1 in bits 7–6 with bit 3 on top, joystick 2 in bits 5–4 with bit 1 on top. A strange packing, I admit; it grew one bit at a time. The other bits are the 50/60 Hz and scandoubler settings, so always read $05 and change only your bits. The modes are:

  • 000 Sinclair 2: RIGHT 2, LEFT 1, DOWN 3, UP 4, fire 5.
  • 011 Sinclair 1: RIGHT 7, LEFT 6, DOWN 8, UP 9, fire 0.
  • 010 Cursor: RIGHT 8, LEFT 5, DOWN 6, UP 7, fire 0, the keys a Protek or AGF interface pressed, and without CAPS SHIFT, as those interfaces did.
  • 111 user-defined keys: each of the twelve buttons presses a key you choose.
  • 001 and 100 Kempston 1 and Kempston 2, and 101 and 110 MD 1 and MD 2: the port modes of the next section.

The first four are pure key presses in the clerk’s scan, from a table I call the joystick map: 64 entries, twelve buttons and a few presets for each connector. A user-defined joystick is programmed through the keymap registers with NextReg $28 bit 7 = 1, NextReg $29 = 0 for the left connector or 16 for the right, and twelve writes of NextReg $2B, one per button in the order R, L, D, U, B, C, A, START, Y, Z, X, MODE. Each byte is a membrane key, row in bits 5–3 and column in bits 2–0; column 7 means no key. Here is the left joystick as the classic Q, A, O, P and SPACE:

; Left joystick as Q A O P and SPACE (mode 111) SetQaop: nextreg $28,$80 ; the key joystick's map nextreg $29,$00 ; left connector, button 0 ld hl,Qaop ld b,12 QNext: ld a,(hl) nextreg $2b,a ; store, step to next button inc hl djnz QNext ld bc,$243b ; read NextReg $05 ld a,$05 out (c),a inc b ; $253B in a,(c) or %11001000 ; joystick 1: mode 111 nextreg $05,a ret Qaop: .defb $28,$29,$08,$10 ; R=P L=O D=A U=Q .defb $38,$07,$07,$07 ; B=SPACE; C A START: none .defb $07,$07,$07,$07 ; Y Z X MODE: none

How it works:

  • P is row 5, column 0, so its entry is %101000, $28; O is $29, A $08, Q $10 and SPACE, row 7, $38. $07 is column 7 of row 0: no key.
  • The OR sets joystick 1’s three mode bits to 111 and leaves joystick 2’s bits, the 50/60 Hz bit and the scandoubler bit as they were.
  • A key can also be one of the extra keys in columns 5 and 6: a joystick can press UP, and the clerk then enters CAPS SHIFT and 7 as for the key itself.

The map survives a soft reset; only loading the core, a hard reset, restores its presets. And it does one more job, which I find clever: in the Kempston and MD modes, a button that the port does not report (C, A, START on a Kempston, X, Y, Z, MODE on an MD pad) presses the user-defined key of that button. So a six-button pad on a Kempston port can still have a pause key.

Kempston ports and Mega Drive pads

The port modes do not press keys; a program reads the joystick as a byte, 1 for pressed:

  • Kempston 1, mode 001, reads on port $1F; Kempston 2, mode 100, on port $37. Bits 0–5 are RIGHT, LEFT, DOWN, UP, fire (B) and second fire (C); bits 6–7 read 0.
  • MD 1, mode 101, and MD 2, mode 110, use the same ports and add A in bit 6 and START in bit 7. A six-button pad’s X, Y, Z and MODE are in the Extended MD Pad Buttons register, NextReg $B2: the left pad in bits 3–0 (X, Z, Y, MODE), the right pad in bits 7–4.

Both connectors can be set to the same port, and their bits are then OR-ed: two players on one Kempston port, whichever pushes. A port that no connector uses does not answer at all, so it reads $FF, which a careful program can take as “no Kempston joystick here”. Bits 6 and 7 of NextReg $82 switch ports $1F and $37 off. After the core is loaded, joystick 1 is Kempston 1 and joystick 2 is Sinclair 2.

The interviewer

Behind the ports sits a part of the FPGA I call the interviewer, because it questions every joystick in the same way, whatever is plugged in. Two facts shape it.

First, the board reads both connectors through one set of input pins. A switch chooses which connector the pins see, and the interviewer flips it at every step: left, right, left, right.

Second, a Mega Drive pad has more buttons than wires. It shows a different set of buttons depending on its select line, pin 7, which the Next drives: with select high, the pad reports UP, DOWN, LEFT, RIGHT, B and C on the six input pins; with select low, it reports A and START, and marks itself as a pad by pulling the LEFT and RIGHT pins low together, something no joystick can do. A six-button pad goes further: on the third low pulse it pulls UP and DOWN low together, and on the third high pulse it reports X, Y, Z and MODE.

So the interviewer runs a fixed script, sixteen steps of 4.57 µs:

pin 7(select)connectorLRLRLRLRLRLRLRLRstartafreshpad?A, STARTU D L RB, Csixbuttons?X Y ZMODE16 steps of 4.57 µs, 73 µs; then a rest of 496 steps, 2.27 ms; the cycle repeats every 2.34 mspurple: what any joystick answers; red: questions only a Mega Drive pad answers
The joystick reader’s script, to scale in steps of 4.57 µs. Pin 7 goes low and high four times, each level lasting two steps, one for each connector. The second low asks “are you a pad?” and takes A and START; the second high takes the directions, B and C; the third low asks “six buttons?”, and the third high takes X, Y, Z and MODE. Then the reader rests for 2.27 ms so the pads can reset.
  1. The first low pulse clears the answers and starts afresh.
  2. The second low pulse: if LEFT and RIGHT both read pressed, the connector holds a pad, and pins 6 and 9 are A and START.
  3. The second high pulse: pins 1, 2, 3, 4, 6 and 9 are UP, DOWN, LEFT, RIGHT, B and C. This is the reading every joystick gives.
  4. The third low pulse: if UP and DOWN both read pressed, the pad has six buttons.
  5. The third high pulse, only for a six-button pad: X, Y, Z and MODE.
  6. A rest of 496 steps, 2.27 ms, before the next script. A six-button pad needs that pause to know that the next pulse starts a new count, and the select pulses stay at 9.1 µs, well under the 15 µs a pad allows before it gets confused.

Here is the charm of it: an ordinary Atari-style joystick ignores pin 7 altogether. It never pulls LEFT and RIGHT together, so it never “answers” the pad questions, and the interviewer just takes its directions and fire buttons from step 3. One script reads everything, and every joystick is read 427 times a second.

The interviewer also has a second job. The Joystick I/O Mode register, NextReg $0B, can take pin 7 for other uses (a bit you set, a clock from the CTC, a UART’s transmit line), and while it does, the interviewer stops its script, presses no keys, and only reports the six raw pins on the Kempston ports. The UARTs chapter tells the serial side of that story.

The Kempston mouse

The mouse is a PS/2 mouse, and the Next presents it the way the Kempston mouse interface did: as counters. At power-on the FPGA resets the mouse, sends it the sequence of sample rates (200, 100, 80) that switches a wheel mouse into reporting its wheel, and asks it to report. From then on, every packet the mouse sends, up to 80 a second, adds its movement to three counters:

  • Port $FBDF: X, an 8-bit count that goes up as the mouse moves right and wraps from 255 to 0.
  • Port $FFDF: Y, which goes up as the mouse moves up, the opposite of screen rows.
  • Port $FADF: the buttons and the wheel. Bit 0 is the right button, bit 1 the left, bit 2 the middle, and a pressed button reads 0, unlike the joysticks; bit 3 always reads 1; bits 7–4 are a 4-bit wheel count that wraps.

Only the low byte and A8 to A11 of the address are decoded, so $xBDF works for any x. A counter is not a position: it says how far the mouse has moved since you last looked, and only the difference matters. The nice part is that an 8-bit subtraction gives the right signed answer even across the wrap, as long as the mouse moves less than 128 counts between two reads:

; A = the mouse's X movement since the last call MouseDx: ld bc,$fbdf ; the X counter in a,(c) ld hl,LastX ld c,(hl) ; the count read last time ld (hl),a ; keep this one for next time sub c ; the difference, wrapped ret LastX: .defb 0

From a count of 2 to a count of 250, it returns $F8, minus 8: eight counts to the left, across the wrap. Y works the same way with port $FFDF, except that positive means up, so negate it for screen rows.

The Peripheral 5 Setting register, NextReg $0A, adjusts the mouse as each packet arrives. Bit 3 swaps the left and right buttons. Bits 1–0 scale the movement: 00 doubles it, 01, the default, keeps it, 10 halves it and 11 quarters it. My advice: leave the scaling to the user’s menu, and read differences.

Bit 5 of NextReg $83 switches the mouse ports off. Then port $DF, which a Kempston mouse decodes loosely, can become a third address for Kempston joystick 1, as some old programs expect, when the Specdrum’s port is on too.

The two buttons on the side

The Next’s case has three buttons: reset, NMI and Drive, the DivMMC’s NMI. The first two have a part to play in input. Each is first debounced: it must stay in its new state for about 75 ms before it counts.

  • Reset: a short press, under about 0.94 s, is a soft reset; a long one is a hard reset, which reloads the core.
  • NMI: held on its own and released within about 0.94 s, it is the Multiface’s NMI, when bit 3 of NextReg $06 allows it. Held while you press a number key, it turns the number keys into function keys: 1 to 0 are F1 to F10 (F1 a hard reset, F4 a soft reset, and so on). While the NMI button is down, its logic reads rows 3 and 4 of the clerk’s copy for those numbers, and a program reading the keyboard sees no key at all, so the number you press never leaks into the game.

Resets

A soft reset clears NextReg $68 bit 4, so the extra keys enter their pairs again, and sets NextReg $0B back to $01, I/O mode off. It keeps the joystick modes in NextReg $05, the joystick map, the PS/2 keymap and the mouse’s scaling in NextReg $0A. The mouse’s counters are cleared only at power-on, so a program that starts after a reset sees whatever count the last one left; one more reason to work with differences. A hard reset loads the core again, which restores the joystick map’s presets, the US keymap, and the joystick modes Kempston 1 and Sinclair 2.

What to take home

The Next keeps the Spectrum’s keyboard exactly: port $FE, rows chosen by A8 to A15, five columns in bits 0 to 4, 0 for pressed, rows combined when several are selected. But the matrix behind it is 8 by 7, and the CPU never touches the membrane. The clerk scans one row every 18.3 µs and copies the whole matrix after each 164.6 µs scan; port $FE reads that copy.

The PS/2 keyboard and the joysticks in their key modes press keys in the clerk’s scan, so every hand looks the same to every program. The sixteen extra keys in columns 5 and 6 enter their CAPS SHIFT or SYMBOL SHIFT pairs, with the shift arriving one scan early and leaving two scans late, and NextRegs $B0 and $B1 report them on their own; NextReg $68 bit 4 stops the pairs. The PS/2 keymap has 512 entries with shift flags, counts its shifts, ignores repeats, and can be rewritten through NextRegs $28, $29 and $2B.

NextReg $05 gives each joystick a mode: Sinclair 2 and 1, Cursor and user-defined keys press keys through the joystick map; Kempston and MD modes report on ports $1F and $37, 1 for pressed, with A and START in bits 6–7 for a pad and X, Y, Z and MODE in NextReg $B2. The interviewer reads both connectors through one set of pins, asks every joystick the Mega Drive questions in 73 µs, and rests 2.27 ms. The mouse is three counters on $FBDF, $FFDF and $FADF, buttons 0 for pressed, scaled and swapped by NextReg $0A. Read differences, not positions; read $05 before you write it; and when you want arrows, ask $B0, not CAPS SHIFT.

Last updated on