# How to deal with PS/2 keyboard input and CPU interrupts

First of all, I'm not sure if this is the right place to ask this question or not. It was either here or the Computer Science stack exchange site, but I thought perhaps it would be better posted here since it feels like a lower level question more about the design of circuits than anything else.

My question is about two different things, the PS/2 protocol and device-to-host communication, and CPU interrupts related to the PS/2 input.

I've only been learning about electronics and computer architecture for a few weeks now, so maybe I'm missing something obvious, but I've been having a hard time understanding how to tell when a scan code has been sent from a keyboard to a computer and how to deal with the input after it has been sent.

What I mean by that is, I think I understand the protocol on paper, but I'm not sure how it would be built as an actual circuit.

From what I've learnt so far, and from what I understand from the protocol, I think I should be able to just connect the VCC, GND, Clock and Data wires up to a shift register of some kind (SIPO?) and just read the input data from the shift register.

If this is the case then how do I tell that the input has actually fully arrived in the shift register? Is it enough to just connect the shift register outputs up to some more circuitry which checks the start, stop and parity bits?

This leads to my second question about CPU interrupts. When I have verified that I do actually have a full scan code from the keyboard, I'm not sure what I should do with that input when it comes to CPU interrupts.

What I mean by that is, should I copy the data that's in the shift register out to RAM somewhere? Or should the CPU just access the data directly in the shift register?

The reason I ask this is because I'm not sure if it's possible for the shift register to have been overwritten with some other data while the CPU is in the middle of trying to handle the interrupt. If that is a possibility then I thought that trying to copy the data out to somewhere like RAM would be safer than trying to get the CPU to just read each scan code directly/indirectly from the shift register.

Also, since some scan codes are made up of more than one byte of data I'm not sure if reading directly from the shift register would be the correct way to do it in the first place.

I've tried looking around on Google for information about this but I mostly just get results for PS/2 adapters, or the PlayStation 2 etc., so if anyone has any information about how to deal with the data from a PS/2 keyboard it would be very much appreciated.

• @jsotola Sorry I should have been more specific. I'm building a simple CPU by wiring components together myself and I'm trying to figure out when exactly I should send an interrupt request to the CPU and whether the data should be read directly from the shift register or not. Mar 22 '18 at 0:43
• @jsotola I'm manually wiring together logic gates, DFFs etc., to build registers, an ALU, RAM, and all of the other components you would expect to see in a very simple computer. So the CPU I'm using is of my own creation and I'm trying to figure out how to get the input from a PS/2 keyboard and where my CPU should read the data from when it receives the interrupt. Mar 22 '18 at 0:57
• very clear now ... awesome !! Mar 22 '18 at 1:09
• @jsotola Sorry for the confusion. It's a bit difficult to explain when I'm unclear on some of the details myself :) Mar 22 '18 at 1:12
• unless you use a keyboard interface IC, there would not be an interrupt .... do a google image search for ps/2 protocol waveform to see the clock and data waveforms Mar 22 '18 at 1:20

## 2 Answers

You may want to take a look at this I²C-PS/2 adapter, which deserializes the PS/2 keyboard protocol in the input direction in pure hardware.

If you are using a hardware shift register, the shift register usually has a register/latch that the contents of the shift register are copied into for the parallel output. This value will remain stable until the next value is fully shifted into the register and the LE (or equivalent) input is triggered.

If you are doing this on a microcontroller that does not have hardware support for PS/2 or an SPI port flexible enough to handle the signals (rare), just use an edge triggered interrupt on the clock signal and build a simple state machine that shifts the data into a temporary memory location (or locations for values longer than the processor word length) and then makes a copy of this value once en entire sequence is shifted in.

There are PS/2 open source libraries available for Arduino and other platforms, take a look at the source code for those and see how they work.