Using the PS/2 Keyboard on the Altera UP1 Board |
The Altera UP1 board supports the use of either a mouse or keyboard using the PS/2 interface port. Although the UP1 board includes some initial documentation, this document will fill in the gaps and provide you with a VHD file to utilize the keyboard.
PS/2 Port Definition
The PS/2 port consists of 6 pins
including ground, power(VDD), keyboard data, and a keyboard clock
line. Two of the lines are not used. Both the clock and data
lines are bi-directional. The clock line is controlled by the
keyboard, but is also manipulated by the computer system when it
wants to send data. The data line is the sole source for the
communications between the computer and keyboard.
Keyboard Scan
Codes
Data is passed serially to the
computer from the keyboard using what is known as scan codes.
Each keyboard key has a unique code to identify the key pressed.
There are different varieties of scan codes available to use
depending on the type of keyboard used. The PS/2 keyboard has two
sets of scan codes. The default scan code set is used upon power
on unless the computer system instructs the keyboard to use an
alternate set. Although the default scan code set was used in
this project, only a subset of the keys were actually implemented
as shown below. The full set of scan codes is available from USAR
Systems. Due to limitations in the amount of ram
available, we did not implement all of the keys on the
keyboard. A layout of the keys we did support is shown
below.
PS/2 Keys Implemented in Keyboard Module
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | - | Backspace | |||||
Q | W | E | R | T | Y | U | I | O | P | [ | ] | |||||
A | S | D | F | G | H | J | K | L | Enter | |||||||
Z | X | C | V | B | N | M | , | . | ||||||||
SPACE |
Make and Break Codes
The keyboard scan codes are broken
into 'Make' and 'Break' codes. One make code is sent every
time a key is pressed. Once released, a break code is
sent. For most keys, the break code is a data stream of F0
followed by the scan code for the key. Using this
configuration, the system can tell whether or not the key has
been pressed, and if more than one key is being held down, it can
distinguish which key has been released. One example of
this is when a shift key is held down. While it is held
down, the '3' key should return the value for the '#' symbol
instead of the value for the '3' symbol. Another thing to
note is that if a key is held down, the make code is continuously
sent via the typematic rate until it is released, when the break
code is sent. The full set of make and break codes is
included in the scan code sets available from USAR Systems.
Keyboard Operation
The scan codes are sent serially on
the bi-directional data line. When neither the keyboard nor the
computer want to send data, the data line and the clock line are
high (inactive). The transmission of a single key or command
consists of the following components:
The following describes the sequence of events that occur during a transmission of a command by the keyboard.
This pattern repeats until the keyboard is finished sending data at which point the clock and data lines will return to their inactive state. When implementing the VHD code, it will be necessary to filter the keyboard clock to ensure clean signals.
The computer system can also send commands to the keyboard. These include
The computer system sends data to the keyboard as follows:
Implementing Keyboard Operation with the Scan Codes
The keyboard module is a simple, yet complicated module. Our module has the keyboard clock and keyboard data lines as inputs, as well as the system clock. The outputs are the actual keystroke as well as a signal that distinguishes when a keystroke has been made.
Within the module, we defined a function that checks the parity of the scan code. It basically performed an XOR function on each of the 8 data bits and the parity bit, and returned the result. This is done to compare to the parity bit received from the keyboard. If they don't match, we do not accept the keystroke. We also defined a ROM (initialized using a MIF file) in order to convert the scan code into a code we could understand.
The body of contains the heart of the code. First we define reset conditions for the keyboard. Next we wait for the start bit. After receiving this bit, we loop for an additional 10 clock cycles and store the data in a 10 bit signal. Upon receiving the 10th bit, we assign the start bit back to zero, and wait for it to change again.
Finally, we set up some signals
and logic to determine whether the scan code is a break code or
not. If it matches the break code, we ignore it and the scan code
following it (which is the make code for the signal being
terminated). We also check to see if the ascii code
is Octal "77". If it is, we ignore it. Otherwise, we
send the keystroke to the video, and toggle the signal that is
used to distinguish when a keystroke has been sent.