Keyboard

From Sinclair Wiki
Jump to navigation Jump to search

ZX Spectrum keyboard theory and practice

The original ZX spectrum keyboard is a 40 key rubber pad over a two layer membrane providing 8 row inputs and 5 column outputs.

Spectrum 48K+ and 128K/grey +2 keyboards retain the same electrical interface, but use a four layer membrane to provide composite keys.

The black +2s and the +3 use the ULA to create the composite keys and has a slightly different electrical interface to the motherboard.

In all cases, the view to the CPU looks the same. (So pressing "Delete" on any keyboard with looks to the CPU as "0"+"caps" regardless of version)


Z80 View

Programmatically, the port $FE is used to read the keyboard interface.

It reads it 5 keys at a time. It relies on a bug in the z80 CPU in that performing an

 IN A,(C)

actually puts the entire contents of BC on the address bus, not just C

The result is active low, so by default the keys read 1 until pressed. Only the first 5 bits are important.

So, the addresses for the keyboard are:

Port Dec Bin Address line D0 D1 D2 D3 D4
$fefe 65278 %1111 1110 1111 1110 A8 Caps shift Z X C V
$fdfe 65022 %1111 1101 1111 1110 A9 A S D F G
$fbfe 64510 %1111 1011 1111 1110 A10 Q W E R T
$f7fe 63486 %1111 0111 1111 1110 A11 1 2 3 4 5
$effe 61438 %1110 1111 1111 1110 A12 0 9 8 7 6
$dffe 57342 %1101 1111 1111 1110 A13 P O I U Y
$bffe 49150 %1011 1111 1111 1110 A14 Ent L K J H
$7ffe 32766 %0111 1111 1111 1110 A15 Spc Sym shft M N B


Speccy keyboard diagram.jpg


It Is possible to write a simple "Press any key" mechanism in both basic and machine code relatively simply.

 10 LET a=IN 254
 20 IF a>31 then LET a=a-32 : GOTO 20
 30 IF a=31 then GOTO 10

This will exit even if a shift key is pressed. However, the downside with this is that break will still break into the program

in MC:

 kloop:
    ld bc,$00fe
    in a,(c)
    and 00011111b
    cp 00011111b
    jr z,kloop

Software Version differences

There is a difference in behaviours between Issue 2 Spectrums and subsequent Spectrums for the upper 2 bits of any read.

On Issue 2 Spectrums, the upper 2 bits always read 1, whereas on other issue Spectrums the upper 2 bits read 0.

Bit 5 can vary depending on the last thing played on the tape.

As such it is best to either mask the top three bits of the input using AND $e0 or forcibly set them using OR $e0 for compatibility.


There aren't many games that had this issue because the Issue 2 spectrum was quickly replaced, and the problem became obvious. An example of this is Centropods (1983) by Rabbit Software.

Hardware implementation

The keyboard on the Spectrum 48 is a rubber mat on top of a two layer keyboard membrane arranged in a two square grids side by side.

The MSB of the IN port is supplied directly to the keyboard via a set of 8 Diodes to prevent two keys shorting the CPU data lines. The resultant 5 bits is supplied to the ULA. This can be done without additional buffering as the additional run around the keyboard is relatively small.

The keyboard on a Spectrum 48k+ and 128K (Original) are 4 layer membranes that handle composite keys by having two keys in the membrane for each composite key (EG, edit has a "1" layered on top of a "Caps shift") and provides the same 5 pin and 8 pin tails to the motherboard.

The keyboard for a Grey Spectrum +2 is similar except with the tails on the side rather than the top of the membrane.

On the Black +2A/B and +3 Spectrums The keyboard extracts the composite keys instead down to a combination of Diodes and additional 40077 Gate array pins which do the same thing using Logic. This was presumably done to reduce the manufacturing costs of the membranes.


Some third-party keyboard interfaces which plug into the rear of the spectrum work by adding a set of buffers onto the bus bypassing the ULA. This works because the ULA is resistor coupled to the z80 and thus the edge connector. These interfaces will not work reliably on the Black +2 and +3 Machines because of the ULA's direct connection to the CPU and may risk damaging it.

Images

Speccy keyboard diagram.jpg

Speccy48 keyboard.png

Speccy48 membrane.jpg

Speccyplus3 keyboard.jpg

Speccyplus3 membrane.jpg