Keyboard Trivia

Keyboard Mechanics
International Keyboards
Programming Considerations

Source: IBM PS/2 A Reference Guide, Intertext Publications, McGraw-Hill, 1989 (ISBN: 0-07-009527-2)
Author: TJ Byers


Keyboard Mechanics (Page 162 - 164)

The keyboard is actually a small computer in itself. The keyboard system consists of a Motorola 6805 microprocessor within the keyboard and an Intel 8042 controller on the PS/2’s planar board. The 8042 keyboard controller directs communications traffic between the computer and the keyboard. The controller communicates with the keyboard via a serial link that supports an 11-bit protocol with parity checking.

The microprocessor detects depressed keys by scanning the keyboard in logical order and analyzing each key’s status. All keys are classified as being in one of two states: make or break. When a key is pressed, the 6805 microprocessor recognizes the condition and reports it to the PS/2 computer as a key make. It does this by generating an interrupt signal and displaying the scan line number of the key detected. When the key is released, it’s break code is sent to the PS/2.

The keyboard processor also contains a 16-byte character buffer that allows the keyboard to store up to 16 keystrokes. Should the PS/2 be busy and unable to acknowledge an interrupt, the keyboard buffer safely s remembers the keystroke until the PS/2 has time to assimilate the input. Should you exceed the 16-character limit, the seventeenth keystroke is simply discarded.

Whenever a keyboard interrupt is generated, the BIOS program queries the keyboard computer for the keystroke number. In order to successfully execute the exchange, bi-directional communications must exist between the two units. Communications between the PS/2 and keyboard are carried out through I/O ports 60h and 64h, input and output, respectively. For every keyboard interrupt, the PS/2 responds with a keyboard query. Data transmissions to and from the keyboard consist of an 11-bit word that is sent asynchronously over the serial data port.

The keyboard and computer communicate over a pair of clock and data lines. At the end of these lines is an open collector transistor that allows either the keyboard or computer to force the line low. When a keystroke is detected, the keyboard checks the status of the clock line. If the line is low, the keystroke is stored in the buffer and transmission is deferred until a later time. If, on the other hand, it finds the clock line high, the keyboard does a similar check on the status of the data line. When both lines are high, it means the computer is ready to accept data from the keyboard, and the keyboard proceeds to send out it’s 11-bit code. The code begins with a 0 start bit, followed by 8 data bits, a parity bit, and a stop bit. For each bit transmitted over the data line, a coincidence bit is transmitted on the clock line. This clock bit is used by the computer to decode the data bits. Please note that the decoding process can only take place when the keylock on the front panel is on. Keystrokes entered while the keylock is off (keyboard receiver inhibited) will be lost.

During transmission, the keyboard checks the clock line for a high level at least every 60 ms. If the computer lowers the clock line after the keyboard starts sending data, it signifies that the computer is unable to accept any more data from the keyboard. Consequently, the keystroke is returned to the buffer and communications halted until the clock line goes high again.

Instructions can also be sent from the computer to the keyboard. When the computer wishes to communicate with the keyboard, the PS/2 forces the clock line low for more than 60 ms while it prepares it’s message. This action alerts the keyboard that an incoming message is pending. When the computer is ready, it allows the clock line to go high while pulling the data line low. The data is then sent over the data line, which by now has assumed a high-impedance value.

While all this is going on, the keyboard constantly checks the status of the clock. As soon as the clock line goes high, it begins counting the computer input bits. After the tenth bit, the keyboard forces the data line low. This action signals the computer that the data was received. Each system command or data transmission to the keyboard requires a response from the keyboard before the system send it’s next output. Typically, the keyboard responds within 20 ms. If the keyboard response is invalid or has a parity error, the computer sends the command or data again.

By knowing the timing sequence, it is possible to make modifications to the keyboard through software. This two-way link is vital to the PS/2’s performance. The two-way communications channel is also used to evaluate the well-being of the keyboard’s microprocessor when the computer is first turned on and after each reset operation. (Ed. This is the BAT - Basic Assurance Test).

When passing data from the keyboard to the PS/2, though, you must be aware that the keyboard’s microprocessor has no idea as to the significance of the keys it is reporting. As far as it’s concerned, the keys have no meaning whatsoever. Each key is assigned a unique 8-bit code which is transmitted to the computer following a make. It is up to the BIOS program inside the PS/2 to decide how the keystroke should be interpreted. The 8042 translates the scan codes it receives from the 6805 to those recognized by the BIOS. The translation table is stored in ROM on the 8042 chip and is not accessible to programs. However, the PS/2 keyboard has the capability of switching scan code sets. In fact, the keyboard offers a choice of three scan code sets.

International Keyboards (Pages 167 - 168)

An interesting feature of the IBM PS/2 is it’s international keyboard design. IBM no doubt had it’s eye on a very large market when it conceived the PS/2, and the keyboard reflects this attitude. In fact, the international version of the PS/2 actually has a slightly different keyboard than the US version. Instead of 101 keys, the international keyboard supports 102 keys. This keyboard, which is referred to as the WT (World Trade) keyboard in IBM documentation, is available only outside the US. It differs only slightly from the US version in that it has an additional alphabetical key nestled in the crook of the entry key, which is a larger, hook-shaped key rather than the slim Entry key of the US keyboard. The backslash (\) is located between the Z and the left shift key (as it was on the original IBM PC keyboard). Because the PS/2 cannot distinguish between the two keyboards , the setup software asks the user to identify the keyboard by indicating the shape of the Enter key.

The reason the PS/2 can support two different types of keyboards is the way the BIOS handles keyboard inputs. As you recall, the keyboard is unaware of the keystrokes it inputs to the system. It simply cites the number of downed key when queried by the 8042 controller chip. It is the responsibility of the BIOS chip to convert the key depressions into ASCII codes and symbols. In the normal default mode, this translates into the US English set of ASCII alphanumeric characters. In the international PS/2 models, another set of scan codes is used to interpret keyboard input.

Programming Considerations (Pages 173 - 176)

Since the introduction of the IBM Personal Computer line in 1980, IBM has changed the keyboard twice. The first change came with the release of the IBM AT. The 84-key keyboard that accompanied the first model of the AT has a different key layout and produces scan codes distinct from those of the original 83-key PC keyboard. The scan code sets on the two keyboards are different because, for reasons of physical circuit design, it is most efficient to assign scan codes by key location. To maintain software application compatibility with the two keyboards, IBM added a scan code translator to the 8042 controller. After translation, the controller’s  output for a given key is the same as that key’s output for the PC keyboard controller.

The 101-key enhanced keyboard used for the PS/2 introduced yet another keyboard layout. However, instead of coming up with a new set of scan codes for the new layout, IBM devised three. The Select Scan Code command (followed by an option byte of 1, 2, or 3) activates the scan code set of that number. An option byte of zero causes the keyboard to respond with the number of the currently active scan code set. The code sets differ in the values generated for the press and release of each key.

By default, the PS/2 keyboard boots to the 84-key set (set 2) used by the original AT keyboard and does not recognize the presence of the new keys. For most keys, it sends a single-byte press code and a two-byte release code consisting of F0H followed by the press code. These codes need to be translated by the BIOS interrupt 09H routine. For example, the “B” key sends scan code 32H to the BIOS, which interprets it as an “M”. However, with code translation enabled, the 8042 controller translates the scan code to 30H, which is properly interpreted by the BIOS as a “B”. The translation process also converts each two-byte release code to a one-byte code that is the same as the press code with the high bit turned on.

When scan code set 1 is activated, the keyboard produces codes that match the result of translating scan code 2. For the keys common to the enhanced and original PC keyboards, this code set produces the same press and release codes as the original keyboard. In effect, this code set moves the translation from the 8042 to the 6805, thus disabling scan code translation at the 8042 level. Support for the 17 enhanced keys is available through the second scan code set. However, only DOS 4.0 (Ed. and above) can access these extended keys from the keyboard without special programming practices. When using DOS 3.3, you must use the extended interrupt 16H BIOS functions, AH=10H and AH=11H.

Scan code set 3 is similar to scan code set 1 in that it produces the same scan codes for the majority of the ASCII keys and uses the same press/release coding convention. However, some keys are curiously reassigned. For example, with the scan code set 3 activated and 8042 translation enabled, the CapsLock key behaves like the left Ctrl key, and the NumLock key acts like Esc. In fact, if the keyboard were to be relabeled, it would look exactly like the 84-key layout of the original AT keyboard. The support for this scan code set is not yet complete, and using it’s features may result in programming errors.

Content created and/or collected by:
Louis Ohland, Peter Wendt, William Walsh, David Beem, Tatsuo Sunagawa, Jim Shorney, Tim Clarke, Kevin Bowling, Tomáš Slavotínek, and many others.

Ardent Tool of Capitalism - MAD Edition! is maintained by Tomáš Slavotínek.
Last update: 19 Nov 2021 - Changes & Credits | Legal Info & Contact