The k project

8042 controller

On an IBM PC, the 8042 chip is responsible for managing the keyboard, the mouse, the A20 gate and a few other things. It is a kind of “fourre-tout” chip.

Interfacing with the keyboard

Getting key presses

When a key is pressed or released, the keyboard controller sends an interrupt request to the PIC. The PIC then forwards the interrupt to the CPU and the corresponding interrupt subroutine is called.

The kernel then waits until the output buffer full bit of the status register is set by the keyboard controller and then reads the actual scancode.

Decoding the scancode

x x x x x x x x
| -------------
|       |
|       +--------- Key number
+----------------- Key press (clear) or release (set)

A scancode can correspond to a key press (most significant bit is clear) or a key release (most significant bit is set).

The remaining 7 less significant bits represent a key number. To this key number is applied a translation which results in a standard character which can be stored for further processing (i.e.: in an internal buffer of a character device). This translation method is more commonly refered to as the keymap.

Registers

Input/output buffer

Data can be exchanged with the keyboard controller on I/O port 0x60. For example, when a key is pressed, the scancode is available in the output buffer.

Status register

The keyboard controller’s status register is accessible via I/O port 0x64.

x x x x x x x x
| | | | | | | |
| | | | | | | +---- Output buffer full
| | | | | | +------ Input buffer full
| | | | | +-------- System flag
| | | | +---------- Command/Data
| | | +------------ Keyboard inhibit
| | +-------------- Auxiliary device output buffer
| +---------------- General purpose time-out
+------------------ Parity error

Attached files