A PS/2 Keyboard Interface
A PS/2 Keyboard Interface

When commencing the journey into retro, 8-bit computing, it's very tempting to start with the microprocessor, add in some ROM and RAM, get some kind of display working, and then add the required hardware such as a keyboard or keypad.

I have decided to work the other way around and start with the keyboard. That means as soon as I have a minimal system up and running, I'll be able to test some input to it, and hopefully get what I type to appear on some kind of display - even if it's simply a set of LEDs displaying the ASCII codes.

I have watched countless videos of other people starting this same 8-bit journey, and when they get to the keyboard, they get a bit stuck. Most 1980s computers had custom keyboards designed specifically for that machine, but for the hobbyist, making a custom keyboard, or having one made is going to be a costly process. So we inevitably turn to what is already available. USB was not a thing in the 1980s, so that's out of the question. A simpler solution is the PS/2 keyboard. The serial PS/2 protocol is simple, and electronically, it is effectively like an I2C interface, requiring only 4 wires, including supply voltage and ground.

I mentioned that the protocol is simple. It is, even if it appears somewhat illogical at first. The PS/2 keyboard does not output character codes. Instead, it outputs a key code representing which key was pressed. So if you press Left Shift, you get a different number than you would if you pressed Right Shift. When you release a key, PS/2 outputs 240 (Hex F0) followed by the key code. So if you press and release "j" for example, you would get 59 followed by 240 59. This process is required to allow key combinations to be handled. For example, Ctrl-Alt-Del pressed and then released would result in 20 17 113 240 20 240 17 240 113. The computer would also need to know whether you were holding Shift. As left Shift produces 18, to type a % character, you would get 18 46 240 46 240 18 (Shift, 5, release 5, release shift).
Just to add a slight complication to the matter, each key code returned would be in 11 bits, including a start bit, parity bit and stop bit.

My original 6502 computer from 1979 had a clock speed of just 750 kHz (0.75 MHz), so its keyboard interface was a lot simpler than that. If it had to spend time extracting the 8 bits from the 11-bit datagram, and then decoding it into an ASCII code, it would expend too many clock cycles on decoding.

For my modern 6502, I'm going to take the burden off my CPU by using an Arduino to do the decoding and turn it into ASCII codes that the CPU can read immediately. Purists may argue that it's not a retro computer if it uses microcontrollers to do some of the work. My argument against that is look at the special chips Commodore and Acorn and others put into their computers to do some of the donkey work.

Leave a Reply

Your email address will not be published. Required fields are marked *