The PS/2 protocol describes a 6-pin connector for connecting keyboards and mice to computer systems. Purple connectors refer to keyboards, green connectors refer to mice. Dual ports exist too.
They were ubiquitous on many old computer systems from before the 2000s. We nowadays use USB for keyboard and mouse input.
Basics
The PS/2 protocol has a 5/6 pin connector. Out of those, we have two unused pins, one for +VCC, and one for GND. This leaves us two useful ports: one is for the device’s internal clock, and the other is for the data, which is compared against the clock. Each PS/2 data packet consists of 11 bits sent serially. Out of these, we have a start, end, parity bit, as well as 8 data bits. Some key characteristics of the packet:
- The start bit (1) must be the opposite of the end bit (0).
- The parity bit indicates odd parity. If 1, the data bits
xor
to 1. - The data bits are sent LSB first, i.e., any interfaces must output in reverse time order.
Each data bit is read on the falling edge of the clock signal (i.e., negedge PS2_CLK
). The clock has a frequency of between 10 to 16.7 kHz, so it must be high for 30-50 μs.
The host can stop the device from communicating by pulling the clock line low for at least 100 μs. Any interrupted keystrokes will be stored in a 16-byte FIFO on the device.
Sending commands
There are a few key steps the host has to follow to send data to the device. These packets can specifically change or report the properties of the device (polling rate, scan code).
- Bring the clock line low for at least 100 μs (from above).
- Bring the data line low — this enters a “request send” state.
- Release the clock line.
- Wait for the device to bring the clock line low.
- For every
negedge
of the PS/2 clock, we set the data line. By default, we send the first data bit already (the low start bit), so the first real bit we send is the first data bit. - We wait for the device to bring the data low, then the clock low.
- Then we wait for the device to release both lines.
Keyboard
Keyboard devices will output an 8-bit (or 2 hex) wide scan code. A guide is below.1 There are three different possible sets of scan codes. Most keyboards follow a consistent set described above, but some keyboards may output the scan codes from the other two by default.
Mouse
There are 4 modes: reset, stream, and some others. By default stream is off so we need to force it on.
The - and -velocity are signed numbers. We can’t move too fast.
todo for controller:
- figure out data packet stream
- light up LEDR for l/r click
Documentation and resources
- For Verilog implementation, see Prof. Jonathan Rose’s website
- https://web.mit.edu/6.111/www/f2005/code/ps2_mouse.v, for the mouse
- https://class.ece.uw.edu/271/hauck2/de1/mouse/ps2.v
- https://www.youtube.com/watch?v=A1YSbLnm4_o
- Computer-Engineering.org
- Derived from this great video by Ben Eater.
- https://oe7twj.at/images/6/6a/PS2_Keyboard.pdf
Footnotes
-
From Prof Paul Chow/Jonathan Rose’s websites. ↩