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

circuit-designcommunicationcomputer-architectureps2

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.

Best Answer

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.