12

Was the ZX Spectrum better at handling than later PCs-keyboards without having diodes in its key matrix? I encountered this comment on Y Combinator but I fail to understand it.
Couldn't the Speccy method scale to 101-key keyboards and the like?

beagle3 on Mar 8, 2014 [–]

The ZX Spectrum in 1982 (and I'm quite sure its ancestors ZX-80 and ZX-81 from their eponymous years) had a keyboard just as cheap or cheaper, with a similar matrix arrangement), but had no ghosting at all:

Instead of having an "all hot" electrical configuration, it would cycle through the rows with a "one hot" configuration, and read the columns. If I recall correctly, it would be something like 20 cycles of a 4Mhz Z-80 to read one 5-column row (you'd need 8 of them to read the entire 40-key keyboard), and it was done every 50hz/60hz interrupt by the main CPU - modern keyboards have an ASIC on par or 100 times faster than a Z-80 just for the keyboard.

3 Answers 3

26

As far as I did my own experiments with the ZX Spectrum keyboard, I strongly disagree with that quoted text. The ZX Spectrum keyboard did ghosting, of course.

Let's look at the key matrix: ZX Spectrum keyboard

Imagine you press A, S, and Z keys together. It results in a "ghosted" Caps Shift.

When the CPU starts to check the CS-Z-X-C-V row, it drives the A8 line to "0". This level goes through the Z key to the 2-W-S-Z column, through the S key to the row A-S-D-F-G and so on, through the A key to the 1-Q-A-CS column and to the D0 input.

I did those tests on my Spectrum 48k (Issue 2) back in the '80s and got the result I described. It may depend on values of pull-ups and many other circumstances, maybe later models have this ghosting fixed, but I have tried it and it worked that way.

5
  • 2
    Funny enough, that almost 40 years after the original ZX Spectrum, the so called 'ZX Spectrum Next' did nothing to avoid ghosting, which is still present there.
    – lvd
    Commented Sep 17, 2020 at 8:50
  • 7
    Because games were aware of this (and some of them even took advantage of this ghosting for cheat key entering, (if my memory serves well) and because it's complicated to add diodes in a keyboard membrane (you can indeed, but that would have been made the membrane to be more expensive to produce) Commented Sep 17, 2020 at 9:13
  • Clones that use a mechanical keyboard with a proper circuit board have added anti-ghosting features as well. antoniovillena.es/store/product/uno-2mb-custom-case Commented Sep 17, 2020 at 9:15
  • 1
    "more expensive to produce" is not an argument for the already price-skyrocketed Next :)
    – lvd
    Commented Sep 18, 2020 at 8:01
  • 2
    Well... if you want to talk about the keyboard of the Next, we could stay here for hours, but to put it in a few words: they screwed it up. They didn't want to (or didn't know about) use the +2A/3 keyboard matrix scheme. Instead, they tried triple membrane. Didn't work. Had to switch back to double membrane with an semi-incompatible matrix, so the core had to scan the matrix in hardware and "virtualize" the Specrtum keyboard. Amstrad didn't need to do that, and worked like a charm. Commented Sep 18, 2020 at 23:10
2

The code responsible for scanning a keyboard can detect situations where ghosting may occur, and suppress the reporting of any keypress which might be a ghost press (e.g. situations where four or more keys seem to be pressed at once). Doing this slightly complicates keyboard logic, but generally not be a huge amount (if a keyboard scan differs from the previous scan, record the contents of the current scan for comparison with the next one, but don't otherwise act upon it). Alternatively, if a keyboard needs to detect combinations of two modifiers plus certain other keys, it could arrange the keys in question to either ensure that no combination of three keys would form the corners of a rectangle, or at minimum ensure that they would never form the corner of a rectangle whose fourth corner would also be a "plausible" key.

3
  • 1
    As I recall, the ZX Spectrum only supported two modifier keys, and then not both at the same time (as that was actually a keystroke in itself), so it only had to validate a trivial subset of scan results.
    – Neil
    Commented Sep 17, 2020 at 23:40
  • This is possible, but ghosting is not about just 4 keys, it is about 4 keys in a rectangle. If there are for example 8 keys seem to be pressed, cumbersome slow algorithms are needed to detect all rectangle patterns, not to say that there is absolutely no way to detect which three keys of the ghosting four are actually pressed. Same with modifiers, there would be need to scan the list of all allowed modifiers+keys to filter out some (not all!) ghosting. So I suppose even if the ghosting was taken in the accout during software developing, sane resolution would be 'just do nothing'.
    – lvd
    Commented Sep 18, 2020 at 7:58
  • @lvd: There are a variety of keyboard scanning algorithms that can be used, based upon what combinations of keys are expected. If e.g. all modifiers are on one row, and all expected key combinations involve at most one non-modifier key, rejecting any key scan that would show more than one non-modifier key would be fairly simple.
    – supercat
    Commented Sep 18, 2020 at 13:54
1

There are 2 major cause of ghosting in matrix keyboard ...

  1. Voltage drop below TTL H/L threshold

    Depending on the matrix circuitry, once you press keys on multiple rows/columns then the voltage after the pull-ups might drop below the TTL threshold reading all as H or L (depending on circuitry). Because pull-ups are very poor source of current and if the load is big the voltage drops.

    The diodes are for entirely different reason and that is to avoid short circuit in case you do not have 3-state I/O and has almost nothing to do with ghosting (except that short circuit also cause voltage drops).

    Using resistors instead of diodes for non 3-state I/O not only prevents short circuit but also forms a voltage divider with the pull-ups causing variable voltage depending on combination of keystrokes increasing the chance of ghosting.

  2. Poor keyboard handler

    Common old PC keyboards are based on ancient i8048 MCUs with ~128 bytes of RAM where all the registers and stack are packed in it too, so there is not much left for anything else. Also the interface between keyboard and PC is serial, in comparison to direct I/O access on ZX, so most PC keyboards have a poorly-written keyboard handler on the MCU side which just remembers a few key strokes per line instead of having a map for the whole keyboard; once any of the line buffers is full no new key (in that line) is registered until at least one of the registered keys are released. This is solely a SW thing and has nothing to do with HW.

5
  • 1
    This is simply not true. Short-circuiting between non-3-state output (scanning) pins is perfectly prevented by the diodes, one diode per each output. But that is not preventing the ghosting. Ghosting could be only prevented by using a diode per each key.
    – lvd
    Commented Sep 1, 2023 at 11:10
  • The reasoning regarding 'voltage drop' is also misleading. It is exactly the voltage dropping well below the TTL threshold which makes the keyboard work (i.e. sense that some keys are pressed). The only issue here is that a typical voltage drop at the diode is somewhat more than higher threshold of logic 0, as per Martin's picture. Because of that the better way to scan such keyboards is to pull down inputs and have scanning impulses be logic 1, not 0 as in ZX Spectrum.
    – lvd
    Commented Sep 1, 2023 at 11:15
  • @lvd for example in the circuit in Martin Maly's answer if you increase load from single pull up (by pressing keys in different columns) the voltage might drop below TTL threshold and then other columns will also read logical zero L even without pressed keys (causing to register more keys then physically pressed) as the read logic active in low logic state. For positive logic the same issue causes to not register pressed keys anymore. However this will occur only if the combination of resistors values / load current / power source ... and yes you re right diodes shifts voltage in favor of this
    – Spektre
    Commented Sep 1, 2023 at 16:38
  • I can't see what you are speaking about. The Martin's picture assumes the pressed key is logic 0. Yes if the pullups are too low and say someone presses all the keys in the column the output might be not powerful enough to pull the voltage down below the high threshold of logic "0". But this is not what you're talking about, as far as I can see. Neither it is hard to overcome -- just ensure your pullups collectively are not low enough.
    – lvd
    Commented Sep 3, 2023 at 18:04
  • I don't see how in the row with no pressed keys the voltage might drop even by a single millivolt.
    – lvd
    Commented Sep 3, 2023 at 18:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .