Keyboard

The IBM 5150 and 5160 originally used an 83-key IBM “Model-F” keyboard1, IBM Part #1501100 (Part #1501105 in the UK2).

Later revisions of the BIOS ROM for the IBM 5160 contained support for the 101-key Enhanced Keyboard3. This keyboard introduced multi-byte scancodes, which required large changes in the keyboard handling code of the BIOS to accommodate.

IBM changed the keyboard protocol with the IBM 5170 AT, making everyone buy new keyboards. Therefore, keyboards of this era are often described as speaking either the “XT” or “AT” protocol. Some keyboards were made that could switch between both, and adapters were (and are) available.

The 83-key keyboard layout is missing many of the keys that we take for granted on modern keyboard layouts. Unlike modern keyboards, the function keys are arranged in a block on the left side.

The IBM 83-key XT keyboard, with scancodes overlaid

Figure 1.1: IBM PC 83-key Model F Keyboard Layout with scancodes (Click to zoom)

Keyboard Operation

The keyboard communicates with the PC by sending one-byte scancodes when a key is pressed or released. On the original 83-keyboard, each key produces a single-byte scancode. When pressed, the unmodified scancode value is sent. When released, the scancode is sent again, with the MSB set to 1. Since keyboard operation is event-driven, if we were ever to miss processing a ‘key-up’ scancode, this would cause the phenomenon of a “stuck key,” something most PC users have experienced at one point.

The keyboard is a serial device, and the keyboard port a specialized serial port. The electrical details of the keyboard port are not really salient to emulating the keyboard, except for the operation of the clock pin.

Bit 6 of the 8255 PPI’s Port B register, when written with a 0, will pull the keyboard clock line low. When held in this state for approximately 20ms, the keyboard will perform a self-test. When the clock line is released by writing 1 to PPI Port B bit 6, the keyboard will send the special scancode 0xAA. If the keyboard internally detects a physically stuck key, it will send the scancode of that key 10ms after sending 0xAA.4. There’s probably no reason to emulate stuck keys, though.

If you fail to emulate sending the reset scancode 0xAA at the appropriate time, the BIOS will emit a POST error code 301.

If you happen to be using SDL for your emulator, here’s a table of SDL keycode definitions to IBM scancodes:

SDL KeyScancode (hex)
SDLK_A1E
SDLK_B30
SDLK_C2E
SDLK_D20
SDLK_E12
SDLK_F21
SDLK_G22
SDLK_H23
SDLK_I17
SDLK_J24
SDLK_K25
SDLK_L26
SDLK_M32
SDLK_N31
SDLK_O18
SDLK_P19
SDLK_Q10
SDLK_R13
SDLK_S1F
SDLK_T14
SDLK_U16
SDLK_V2F
SDLK_W11
SDLK_X2D
SDLK_Y15
SDLK_Z2C
SDLK_102
SDLK_203
SDLK_304
SDLK_405
SDLK_506
SDLK_607
SDLK_708
SDLK_809
SDLK_90A
SDLK_00B
SDLK_RETURN1C
SDLK_ESCAPE01
SDLK_BACKSPACE0E
SDLK_TAB0F
SDLK_SPACE39
SDLK_MINUS0C
SDLK_EQUALS0D
SDLK_LEFTBRACKET1A
SDLK_RIGHTBRACKET1B
SDLK_BACKSLASH2B
SDLK_SEMICOLON27
SDLK_APOSTROPHE28
SDLK_COMMA33
SDLK_PERIOD34
SDLK_SLASH35
SDLK_GRAVE29
SDLK_LSHIFT2A
SDLK_RSHIFT36
SDLK_LCTRL1D
SDLK_RCTRL1D
SDLK_LALT38
SDLK_RALT38
SDLK_CAPSLOCK3A
SDLK_F13B
SDLK_F23C
SDLK_F33D
SDLK_F43E
SDLK_F53F
SDLK_F640
SDLK_F741
SDLK_F842
SDLK_F943
SDLK_F1044
SDLK_F1157
SDLK_F1258
SDLK_UP48
SDLK_DOWN50
SDLK_LEFT4B
SDLK_RIGHT4D
SDLK_INSERT52
SDLK_DELETE53
SDLK_HOME47
SDLK_END4F
SDLK_PAGEUP49
SDLK_PAGEDOWN51
SDLK_KP_14F
SDLK_KP_250
SDLK_KP_351
SDLK_KP_44B
SDLK_KP_54C
SDLK_KP_64D
SDLK_KP_747
SDLK_KP_848
SDLK_KP_949
SDLK_KP_052
SDLK_KP_PLUS4E
SDLK_KP_MINUS4A
SDLK_KP_PERIOD53
SDLK_KP_ENTER1C
SDLK_KP_DIVIDE35
SDLK_KP_MULTIPLY37
SDLK_KP_EQUALS0D

References


  1. wikipedia.org Model F keyboard.

  2. seasip.info The IBM 1501105 Keyboard

  3. minuszerodegrees.net IBM 5160 - Keyboards

  4. minuszerodegrees.net 5160 Keyboard Startup