Spectrum Keyboard
Contents |
Spectrum keyboard
This is actually pretty trivial. I have added two extra complexities to give me some flexibity later.
Theory
The Spectrum reads its keyboard from port $FE. It reads it 5 keys at a time. It relies on a bug in the z80 CPU in that performing an
IN (C),A
actually puts the entire contents of BC on the address bus, not just C
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 |
Changes
There are two major changes i did at this stage. They can be done seperately, and have been done for the most part.
Buffering
Part of the circuit is wired directly to the CPU bus via diodes. This has the effect of loading the bus. Normally, this is not a problem, as the keyboard membrane inside the speccy isnt long and only adds of the order of 20cm to the bus.
However, as we want a more complex keyboard, if we wired it directly to the CPU bus in this manner, we would load the bus substantially more. (The ribbon i ended up using was 30cm long, and the wiring to the keys makes this even larger). A larger bus would reveal itself in hard to trace intermittent crashes. In addition, the composite key circuitry would also load the bus.
As such I buffered the Address lines before the diodes using a 74LS245. There is no need to buffer the data lines as i am still wiring them to their own dedicated pins on the ULA, which provides its own buffer. The diodes are still required because of the possibility of shorting out two lines on the 74LS245.
If i was not using the ULA for this, i would also need to provide a buffer for the data lines which would be enabled with /IORQ + /A0 + /RD and make sure it is all connected to the CPU bus rather than the ULA side of the bus.
Composite keys
Note: I would Strongly recommend you get a basic keyboard up, running and tested before you start on composite keys. It wil make your life much easier.
There are three classes of composite keys. Two can be done simply with diode networks, whereas the third one requires some active components. The table below gives details of which circuit is requires for which key.
| Key combo | Shift key | Base key | Circuit | Details |
|---|---|---|---|---|
| Extend mode | Caps | Symbol | Different | |
| Edit mode | Caps | 1 | Same Col | Column: D0 Row1: /A11 Row2: /A8 |
| Caps lock | Caps | 2 | Different | |
| True video | Caps | 3 | Different | |
| Inv video | Caps | 4 | Different | |
| Cursor Left | Caps | 5 | Different | |
| Cursor Down | Caps | 6 | Different | |
| Cursor Up | Caps | 7 | Different | |
| Cursor Right | Caps | 8 | Different | |
| Graphics mode | Caps | 9 | Different | |
| Escape | Caps | Space | Same Col | Column: D0 Row1: /A15 Row2: /A8 |
| Delete | Caps | 0 | Same Col | Column: D0 Row1: /A12 Row2: /A8 |
| + | Symbol | K | Different | |
| - | Symbol | J | Different | |
| < | Symbol | R | Different | |
| > | Symbol | T | Different | |
| = | Symbol | L | Same col | Column: D1 Row1: /A14 Row2: /A15 |
| / | Symbol | V | Different | |
| , | Symbol | M | Same Row | Row: A15 Col1: D1 Col2: D2 |
| . | Symbol | N | Same Row | Row: A15 Col1: D1 Col2: D3 |
| * | Symbol | B | Same Row | Row: A15 Col1: D1 Col2: D4 |
| " | Symbol | P | Different |
Circuit for keys on the same row
This circuit is simply two diodes and a pull-up resistor. The resistor is to ensure that a "1" is recorded
Circuit for keys on the same Column
This circuit is even simpler, simply two diodes. No pull-up resistor is required because there is already suficcient pull-up.
Circuit for keys on different rows and columns
This is where things get a little tricky. Not too tricky mind.. As there is no usable shortcuts, we need to create gates which act as if both keys are pressed.
The basic gate uses a 74LS32 and some diodes as shown below. You need one of these gates for each key being pressed. (EG, Cursor left requires "Caps shift" and "5")
However, via the means of Wired-Or logic (Diodes to you and me), you can use one gate repeatedly.
Note that in theory, if a gate is used by one key (EG, in normal use, they key "2" will only ever be used by "Caps lock"), you can dispense with the diode going to that key's gate.
However, i have found that its best to put the diode in anyway because consistancy helps in checking when things go wrong and also, and Certainly for the top row of keys, it means you can implement a set of buffered sinclair joysticks / cursor joystick for only a few additional diodes.
Finished product
Testing
At this stage, if everything is wired in, we have a 16k spectrum Inlcuding its keyboard. So simply plugging in the video section and the power should result in a the computer resetting and showing the standard "(C) Sinclair research 1982" prompt. You should now be able to type commands.
You should now press every basic key to make sure it works:
1-0,Q-P,A-L,Z-M and space
Press enter to make sure thats recognised.
Press Caps shift + P. You should get a capital P
Press Symbol shift + P. You should get a quote character.
If a failiure occurs, it usually occurs in a line, EG,
If "1" fails, usually, either "1,2,3,4,5" will fail or "1","Q","A","Caps shift","0","p","Enter" and space" will fail. If only one key fails, its usually a duff keyswitch and this should be the first thing to check.
Using the table above, this should give you a clue as to what address line or data line to look at.
Try
1 0 p <Symb shift+p> hello <Symb shift+p> Enter 2 0 g 1 0 Enter r Enter
This should print a screen of "Hello" and end with "Scroll?" Press caps shift and space to stop this.
For the composite keys, you need to check each one individually.
If one of the diode matrix keys fails, you need to check to see: 1: if you havent got a duff key. 2: If the diodes are wired incorrectly.
For the complex keys, you will need to check each of the two gates that makes up the key. If you are only getting the one key, its worth disconnecting the other gate and checking to see if this properly emulates the keypress. I was unfortunate enough to discover a bad 74LS32 when i was doing mine.
Parts list
General
- Active
- 1n4148 x8
- Passive
- A keyboard.
- Connectors and cables to your own tastes.
Buffer:
- Active
- Passive
- Capacitor 100nf
Composite keys:
- Active
- 74LS32x?? (You need two gates out of the 4 on each chip for each key to be pressed, but they can be shared, so, it varies. Its best to assume 2 gates covering the shifts and one gate for every key you need.)
- 1n4148x?? Your going to need a lot of these. Generally, 2 for each composite key + an extra 1 for each gate. Just get a bag of 50. There cheap, and there used everywere.




