FPGA Design Repository
Interface summary

Input ports

  • Reset - Active high reset.

  • Clock - Board clock.

Bidirectional ports

  • PS2Clock - Keyboard controlled clock, do not connect any other clock!

  • PS2Data - Keyboard data.

Output ports

  • CodeReady - Active high signals that a scan code may be read.

  • ScanCode - Bit 9 high signals an extended code and low a regular one.
     Bit 8 high signals break code and low make code.
     The remaining bits 7 down to 0 are the rest of the code.
     Example: "0001011010" is the <Enter> key make code and "0101011010" is the break code.

File dependencies

The PS/2 keyboard module uses three other modules: the PS/2 controller, the keyboard mapper and the debouncer.


warning
  In the PS2Controller.vhd file, there is a constant named "ClockFreq", you must change the value to match to the clock frequency you're using!

Keyboard.vhd
PS2Controller.vhd
KeyboardMapper.vhd
Debouncer.vhd

PS2 Keyboard

Description

What happens when you press a key on your keyboard? You expect the host (your computer or FPGA board) to recognize the key you pressed. Each key has its own code, called a scan code, that's because your keyboard has a controller chip that is scanning all the keys.

When you press a key a scan code is sent by the keyboard to the host, that's a make code signaling a key has been pressed, if you keep pressing the key, after a small delay the make code is repeated over and over again, that delay is called the typematic delay and the rate at which the code is repeated is the typematic rate. Similarly when the key is released another scan code is sent, that's called a break code.

A make code for a regular key is a byte long and the corresponding break code is two bytes long, the break code starts with xF0 (typical hexadecimal notation) and repeats the make code.

Example:

Key Make code Break code
A x1C xF0 x1C
space x29 xF0 x29
1 x16 xF0 x16
enter x5A xF0 x5A

There are also extended keys that are two bytes for the make code and three bytes for the break code, in this case xE0 precedes both the make and the break code.

Example:

Key Make code Break code
key pad enter xE0 x5A xE0 xF0 x5A
up arrow xE0 x75 xE0 xF0 x75
key pad / xE0 x4A xE0 xF0 x4A
insert xE0 x70 xE0 xF0 x70

There are exceptions to these rules, the Print Screen key and the Pause key, but probably you wont be using them in your project, so I'll leave them alone.

Here is a complete table with the make and break codes for the Portuguese keyboard keys.

Actually the scan code refers to the key position, not the key caption, so for example if I'm using the Portuguese keyboard, pressing the key on the right side of number zero will print an apostrophe ' but if I switch to English(UK) it prints a dash or minus sign - , the key pressed and scan code are the same but the operating system reads it differently.

Check out this site with a virtual keyboard to help you understand the corresponding scan codes to the key positions, http://www.barcodeman.com/altek/mule/kbemulator/

OK, so far I've covered what the keyboard does, it sends scan codes to the host, the next question is: how?

The keyboard uses a two wire bidirectional synchronous serial communication protocol called PS/2.

The module presented is the host side (your FPGA board) keyboard controller and it's composed of three other modules, a PS/2 controller, a keyboard mapper and a debouncer.

The PS/2 controller handles the communication directly with the keyboard, receiving scan codes and sending commands over the two wire serial data link as well as passing byte packets to the the keyboard mapper.

The debouncer is used by the PS/2 controller to clean up the noisy signals from the keyboard.

The keyboard mapper interprets the byte sequences received from the PS/2 controller and outputs scan codes, it also sends back command bytes (through the PS/2 controller) to the keyboard for resetting the keyboard and lighting up the LEDs, like when you press <Caps Lock> a command is sent back to the keyboard to light the Caps Lock LED.

Copyright © 2008, Rui T. Sousa | Last modified on: 20/09/08