]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - converter/ibmpc_usb/ibmpc_usb.c
lufa: Use the latest LUFA library on github
[max/tmk_keyboard.git] / converter / ibmpc_usb / ibmpc_usb.c
1 /*
2 Copyright 2019 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <stdint.h>
19 #include <stdbool.h>
20 #include "print.h"
21 #include "util.h"
22 #include "debug.h"
23 #include "ibmpc.h"
24 #include "host.h"
25 #include "led.h"
26 #include "matrix.h"
27 #include "timer.h"
28 #include "action.h"
29 #include "ibmpc_usb.h"
30
31
32 static void matrix_make(uint8_t code);
33 static void matrix_break(uint8_t code);
34
35 static int8_t process_cs1(void);
36 static int8_t process_cs2(void);
37 static int8_t process_cs3(void);
38
39
40 static uint8_t matrix[MATRIX_ROWS];
41 #define ROW(code)      ((code>>3)&0x0F)
42 #define COL(code)      (code&0x07)
43
44 static int16_t read_wait(uint16_t wait_ms)
45 {
46     uint16_t start = timer_read();
47     int16_t code;
48     while ((code = ibmpc_host_recv()) == -1 && timer_elapsed(start) < wait_ms);
49     return code;
50 }
51
52 static uint16_t read_keyboard_id(void)
53 {
54     uint16_t id = 0;
55     int16_t  code = 0;
56
57     // Disable
58     code = ibmpc_host_send(0xF5);
59
60     // Read ID
61     code = ibmpc_host_send(0xF2);
62     if (code == -1)  return 0xFFFF;     // XT or No keyboard
63     if (code != 0xFA) return 0xFFFE;    // Broken PS/2?
64
65     code = read_wait(1000);
66     if (code == -1)  return 0x0000;     // AT
67     id = (code & 0xFF)<<8;
68
69     code = read_wait(1000);
70     id |= code & 0xFF;
71
72     // Enable
73     code = ibmpc_host_send(0xF4);
74
75     return id;
76 }
77
78 void matrix_init(void)
79 {
80     debug_enable = true;
81     ibmpc_host_init();
82
83     // hard reset for XT keyboard
84     IBMPC_RESET();
85
86     // initialize matrix state: all keys off
87     for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
88
89     return;
90 }
91
92 /*
93  * keyboard recognition
94  *
95  * 1. Send F2 to get keyboard ID
96  *      a. no ACK(FA): XT keyobard
97  *      b. ACK but no ID: 84-key AT keyboard CodeSet2
98  *      c. ID is AB 83: PS/2 keyboard CodeSet2
99  *      d. ID is BF BF: Terminal keyboard CodeSet3
100  *      e. error on recv: maybe broken PS/2
101  */
102 uint16_t keyboard_id = 0x0000;
103 keyboard_kind_t keyboard_kind = NONE;
104 uint8_t matrix_scan(void)
105 {
106     // scan code reading states
107     static enum {
108         INIT,
109         WAIT_STARTUP,
110         READ_ID,
111         LED_SET,
112         LOOP,
113         END
114     } state = INIT;
115     static uint16_t last_time;
116
117
118     if (ibmpc_error) {
119         xprintf("err: %02X\n", ibmpc_error);
120
121         // when recv error, neither send error nor buffer full
122         if (!(ibmpc_error & (IBMPC_ERR_SEND | IBMPC_ERR_FULL))) {
123             // keyboard init again
124             if (state == LOOP) {
125                 xprintf("init\n");
126                 state = INIT;
127             }
128         }
129
130         // clear or process error
131         ibmpc_error = IBMPC_ERR_NONE;
132     }
133
134     switch (state) {
135         case INIT:
136             ibmpc_protocol = IBMPC_PROTOCOL_AT;
137             keyboard_kind = NONE;
138             keyboard_id = 0x0000;
139             last_time = timer_read();
140             state = WAIT_STARTUP;
141             matrix_clear();
142             clear_keyboard();
143             break;
144         case WAIT_STARTUP:
145             // read and ignore BAT code and other codes when power-up
146             ibmpc_host_recv();
147             if (timer_elapsed(last_time) > 1000) {
148                 state = READ_ID;
149             }
150             break;
151         case READ_ID:
152             keyboard_id = read_keyboard_id();
153             if (ibmpc_error) {
154                 xprintf("err: %02X\n", ibmpc_error);
155                 ibmpc_error = IBMPC_ERR_NONE;
156             }
157             xprintf("ID: %04X\n", keyboard_id);
158             if (0xAB00 == (keyboard_id & 0xFF00)) {
159                 // CodeSet2 PS/2
160                 keyboard_kind = PC_AT;
161             } else if (0xBF00 == (keyboard_id & 0xFF00)) {
162                 // CodeSet3 Terminal
163                 keyboard_kind = PC_TERMINAL;
164             } else if (0x0000 == keyboard_id) {
165                 // CodeSet2 AT
166                 keyboard_kind = PC_AT;
167             } else if (0xFFFF == keyboard_id) {
168                 // CodeSet1 XT
169                 keyboard_kind = PC_XT;
170             } else if (0xFFFE == keyboard_id) {
171                 // CodeSet2 PS/2 fails to response?
172                 keyboard_kind = PC_AT;
173             } else if (0x00FF == keyboard_id) {
174                 // Mouse is not supported
175                 xprintf("Mouse: not supported\n");
176                 keyboard_kind = NONE;
177             } else {
178                 keyboard_kind = PC_AT;
179             }
180
181             // protocol
182             if (keyboard_kind == PC_XT) {
183                 xprintf("kbd: XT\n");
184                 ibmpc_protocol = IBMPC_PROTOCOL_XT;
185             } else if (keyboard_kind == PC_AT) {
186                 xprintf("kbd: AT\n");
187                 ibmpc_protocol = IBMPC_PROTOCOL_AT;
188             } else if (keyboard_kind == PC_TERMINAL) {
189                 xprintf("kbd: Terminal\n");
190                 ibmpc_protocol = IBMPC_PROTOCOL_AT;
191                 // Set all keys - make/break [3]p.23
192                 ibmpc_host_send(0xF8);
193             } else {
194                 xprintf("kbd: Unknown\n");
195                 ibmpc_protocol = IBMPC_PROTOCOL_AT;
196             }
197             state = LED_SET;
198             break;
199         case LED_SET:
200             led_set(host_keyboard_leds());
201             state = LOOP;
202         case LOOP:
203             switch (keyboard_kind) {
204                 case PC_XT:
205                     process_cs1();
206                     break;
207                 case PC_AT:
208                     process_cs2();
209                     break;
210                 case PC_TERMINAL:
211                     process_cs3();
212                     break;
213                 default:
214                     break;
215             }
216             break;
217         default:
218             break;
219     }
220     return 1;
221 }
222
223 inline
224 bool matrix_is_on(uint8_t row, uint8_t col)
225 {
226     return (matrix[row] & (1<<col));
227 }
228
229 inline
230 uint8_t matrix_get_row(uint8_t row)
231 {
232     return matrix[row];
233 }
234
235 uint8_t matrix_key_count(void)
236 {
237     uint8_t count = 0;
238     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
239         count += bitpop(matrix[i]);
240     }
241     return count;
242 }
243
244
245 inline
246 static void matrix_make(uint8_t code)
247 {
248     if (!matrix_is_on(ROW(code), COL(code))) {
249         matrix[ROW(code)] |= 1<<COL(code);
250     }
251 }
252
253 inline
254 static void matrix_break(uint8_t code)
255 {
256     if (matrix_is_on(ROW(code), COL(code))) {
257         matrix[ROW(code)] &= ~(1<<COL(code));
258     }
259 }
260
261 void matrix_clear(void)
262 {
263     for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
264 }
265
266 void led_set(uint8_t usb_led)
267 {
268     if (keyboard_kind != PC_AT) return;
269
270     uint8_t ibmpc_led = 0;
271     if (usb_led &  (1<<USB_LED_SCROLL_LOCK))
272         ibmpc_led |= (1<<IBMPC_LED_SCROLL_LOCK);
273     if (usb_led &  (1<<USB_LED_NUM_LOCK))
274         ibmpc_led |= (1<<IBMPC_LED_NUM_LOCK);
275     if (usb_led &  (1<<USB_LED_CAPS_LOCK))
276         ibmpc_led |= (1<<IBMPC_LED_CAPS_LOCK);
277     ibmpc_host_set_led(ibmpc_led);
278 }
279
280
281 /*******************************************************************************
282  * XT: Scan Code Set 1
283  *
284  * See [3], [a]
285  *
286  * E0-escaped scan codes are translated into unused range of the matrix.(54-7F)
287  *
288  *     01-53: Normal codes used in original XT keyboard
289  *     54-7F: Not used in original XT keyboard
290  *
291  *         0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
292  *     50  -   -   -   -   *   *   x   x   x   x   *   *   *   *   *   *
293  *     60  *   *   *   *   x   x   x   x   x   x   x   x   x   x   x   *
294  *     70  x   *   *   x   *   *   x   *   *   x   *   x   *   x   x   *
295  *
296  * -: codes existed in original XT keyboard
297  * *: E0-escaped codes translated
298  * x: Non-espcaped codes(Some are not used in real keyboards probably)
299  *
300  * Codes assigned in range 54-7F:
301  *
302  *     50  -                60  Up*                 70  KANAx
303  *     51  -                61  Left*               71  Insert*
304  *     52  -                62  Down*               72  Delete*
305  *     53  -                63  Right*              73  ROx
306  *     54  PrintScr*        64  F13x                74  Home*
307  *     55  Pause*           65  F14x                75  End*
308  *     56  Euro2x           66  F15x                76  F24x
309  *     57  F11x             67  F16x                77  PageUp*
310  *     58  F12x             68  F17x                78  PageDown*
311  *     59  Keypad=x         69  F18x                79  HENKANx
312  *     5A  LGUI*            6A  F19x                7A  RCTL*
313  *     5B  RGUI*            6B  F20x                7B  MUHENKANx
314  *     5C  APP*             6C  F21x                7C  RALT*
315  *     5D  Mute*            6D  F22x                7D  JPYx
316  *     5E  Volume Down*     6E  F23x                7E  Keypad,x
317  *     5F  Volume Up*       6F  Keypad Enter*       7F  Keypad/ *
318  */
319 static uint8_t cs1_e0code(uint8_t code) {
320     switch(code) {
321         // Original IBM XT keyboard doesn't use E0-codes probably
322         // Some XT compatilble keyobards need these keys?
323         case 0x37: return 0x54; // Print Screen
324         case 0x46: return 0x55; // Ctrl + Pause
325         case 0x5B: return 0x5A; // Left  GUI
326         case 0x5C: return 0x5B; // Right GUI
327         case 0x5D: return 0x5C; // Application
328         case 0x20: return 0x5D; // Mute
329         case 0x2E: return 0x5E; // Volume Down
330         case 0x30: return 0x5F; // Volume Up
331         case 0x48: return 0x60; // Up
332         case 0x4B: return 0x61; // Left
333         case 0x50: return 0x62; // Down
334         case 0x4D: return 0x63; // Right
335         case 0x1C: return 0x6F; // Keypad Enter
336         case 0x52: return 0x71; // Insert
337         case 0x53: return 0x72; // Delete
338         case 0x47: return 0x74; // Home
339         case 0x4F: return 0x75; // End
340         case 0x49: return 0x77; // Page Up
341         case 0x51: return 0x78; // Page Down
342         case 0x1D: return 0x7A; // Right Ctrl
343         case 0x38: return 0x7C; // Right Alt
344         case 0x35: return 0x7F; // Keypad /
345
346         // Shared matrix cell with other keys
347         case 0x5E: return 0x70; // Power (KANA)
348         case 0x5F: return 0x79; // Sleep (HENKAN)
349         case 0x63: return 0x7B; // Wake  (MUHENKAN)
350
351         default:
352            xprintf("!CS1_?!\n");
353            return code;
354     }
355     return 0x00;
356 }
357
358 static int8_t process_cs1(void)
359 {
360     static enum {
361         INIT,
362         E0,
363         // Pause: E1 1D 45, E1 9D C5
364         E1,
365         E1_1D,
366         E1_9D,
367     } state = INIT;
368
369     uint16_t code = ibmpc_host_recv();
370     if (code == -1) {
371         return 0;
372     }
373
374     switch (state) {
375         case INIT:
376             switch (code) {
377                 case 0xE0:
378                     state = E0;
379                     break;
380                 case 0xE1:
381                     state = E1;
382                     break;
383                 default:
384                     if (code < 0x80)
385                         matrix_make(code);
386                     else
387                         matrix_break(code & 0x7F);
388                     break;
389             }
390             break;
391         case E0:
392             switch (code) {
393                 case 0x2A:
394                 case 0xAA:
395                 case 0x36:
396                 case 0xB6:
397                     //ignore fake shift
398                     state = INIT;
399                     break;
400                 default:
401                     if (code < 0x80)
402                         matrix_make(cs1_e0code(code));
403                     else
404                         matrix_break(cs1_e0code(code & 0x7F));
405                     state = INIT;
406                     break;
407             }
408             break;
409         case E1:
410             switch (code) {
411                 case 0x1D:
412                     state = E1_1D;
413                     break;
414                 case 0x9D:
415                     state = E1_9D;
416                     break;
417                 default:
418                     state = INIT;
419                     break;
420             }
421             break;
422         case E1_1D:
423             switch (code) {
424                 case 0x45:
425                     matrix_make(0x55);
426                     break;
427                 default:
428                     state = INIT;
429                     break;
430             }
431             break;
432         case E1_9D:
433             switch (code) {
434                 case 0x45:
435                     matrix_break(0x55);
436                     break;
437                 default:
438                     state = INIT;
439                     break;
440             }
441             break;
442         default:
443             state = INIT;
444     }
445     return 0;
446 }
447
448
449 /*******************************************************************************
450  * AT, PS/2: Scan Code Set 2
451  *
452  * Exceptional Handling
453  * --------------------
454  * Some keys should be handled exceptionally. See [b].
455  *
456  * Scan codes are varied or prefix/postfix'd depending on modifier key state.
457  *
458  * 1) Insert, Delete, Home, End, PageUp, PageDown, Up, Down, Right, Left
459  *     a) when Num Lock is off
460  *     modifiers | make                      | break
461  *     ----------+---------------------------+----------------------
462  *     Ohter     |                    <make> | <break>
463  *     LShift    | E0 F0 12           <make> | <break>  E0 12
464  *     RShift    | E0 F0 59           <make> | <break>  E0 59
465  *     L+RShift  | E0 F0 12  E0 F0 59 <make> | <break>  E0 59 E0 12
466  *
467  *     b) when Num Lock is on
468  *     modifiers | make                      | break
469  *     ----------+---------------------------+----------------------
470  *     Other     | E0 12              <make> | <break>  E0 F0 12
471  *     Shift'd   |                    <make> | <break>
472  *
473  *     Handling: These prefix/postfix codes are ignored.
474  *
475  *
476  * 2) Keypad /
477  *     modifiers | make                      | break
478  *     ----------+---------------------------+----------------------
479  *     Ohter     |                    <make> | <break>
480  *     LShift    | E0 F0 12           <make> | <break>  E0 12
481  *     RShift    | E0 F0 59           <make> | <break>  E0 59
482  *     L+RShift  | E0 F0 12  E0 F0 59 <make> | <break>  E0 59 E0 12
483  *
484  *     Handling: These prefix/postfix codes are ignored.
485  *
486  *
487  * 3) PrintScreen
488  *     modifiers | make         | break
489  *     ----------+--------------+-----------------------------------
490  *     Other     | E0 12  E0 7C | E0 F0 7C  E0 F0 12
491  *     Shift'd   |        E0 7C | E0 F0 7C
492  *     Control'd |        E0 7C | E0 F0 7C
493  *     Alt'd     |           84 | F0 84
494  *
495  *     Handling: These prefix/postfix codes are ignored, and both scan codes
496  *               'E0 7C' and 84 are seen as PrintScreen.
497  *
498  * 4) Pause
499  *     modifiers | make(no break code)
500  *     ----------+--------------------------------------------------
501  *     Other     | E1 14 77 E1 F0 14 F0 77
502  *     Control'd | E0 7E E0 F0 7E
503  *
504  *     Handling: Both code sequences are treated as a whole.
505  *               And we need a ad hoc 'pseudo break code' hack to get the key off
506  *               because it has no break code.
507  *
508  * Notes:
509  * 'Hanguel/English'(F1) and 'Hanja'(F2) have no break code. See [a].
510  * These two Korean keys need exceptional handling and are not supported for now.
511  *
512  */
513 static uint8_t cs2_e0code(uint8_t code) {
514     switch(code) {
515         // E0 prefixed codes translation See [a].
516         case 0x11: return 0x0F; // right alt
517         case 0x14: return 0x17; // right control
518         case 0x1F: return 0x19; // left GUI
519         case 0x27: return 0x1F; // right GUI
520         case 0x2F: return 0x5C; // apps
521         case 0x4A: return 0x60; // keypad /
522         case 0x5A: return 0x62; // keypad enter
523         case 0x69: return 0x27; // end
524         case 0x6B: return 0x53; // cursor left
525         case 0x6C: return 0x2F; // home
526         case 0x70: return 0x39; // insert
527         case 0x71: return 0x37; // delete
528         case 0x72: return 0x3F; // cursor down
529         case 0x74: return 0x47; // cursor right
530         case 0x75: return 0x4F; // cursor up
531         case 0x7A: return 0x56; // page down
532         case 0x7D: return 0x5E; // page up
533         case 0x7C: return 0x6F; // Print Screen
534         case 0x7E: return 0x00; // Control'd Pause
535
536         case 0x21: return 0x65; // volume down
537         case 0x32: return 0x6E; // volume up
538         case 0x23: return 0x7F; // mute
539         case 0x10: return 0x08; // (WWW search)     -> F13
540         case 0x18: return 0x10; // (WWW favourites) -> F14
541         case 0x20: return 0x18; // (WWW refresh)    -> F15
542         case 0x28: return 0x20; // (WWW stop)       -> F16
543         case 0x30: return 0x28; // (WWW forward)    -> F17
544         case 0x38: return 0x30; // (WWW back)       -> F18
545         case 0x3A: return 0x38; // (WWW home)       -> F19
546         case 0x40: return 0x40; // (my computer)    -> F20
547         case 0x48: return 0x48; // (email)          -> F21
548         case 0x2B: return 0x50; // (calculator)     -> F22
549         case 0x34: return 0x08; // (play/pause)     -> F13
550         case 0x3B: return 0x10; // (stop)           -> F14
551         case 0x15: return 0x18; // (previous track) -> F15
552         case 0x4D: return 0x20; // (next track)     -> F16
553         case 0x50: return 0x28; // (media select)   -> F17
554         case 0x5E: return 0x50; // (ACPI wake)      -> F22
555         case 0x3F: return 0x57; // (ACPI sleep)     -> F23
556         case 0x37: return 0x5F; // (ACPI power)     -> F24
557
558         // https://github.com/tmk/tmk_keyboard/pull/636
559         case 0x03: return 0x18; // Help        DEC LK411 -> F15
560         case 0x04: return 0x08; // F13         DEC LK411
561         case 0x0B: return 0x20; // Do          DEC LK411 -> F16
562         case 0x0C: return 0x10; // F14         DEC LK411
563         case 0x0D: return 0x19; // LCompose    DEC LK411 -> LGUI
564         case 0x79: return 0x6D; // KP-         DEC LK411 -> PCMM
565         case 0x83: return 0x28; // F17         DEC LK411
566         default: return (code & 0x7F);
567     }
568 }
569
570 static int8_t process_cs2(void)
571 {
572     // scan code reading states
573     static enum {
574         INIT,
575         F0,
576         E0,
577         E0_F0,
578         // Pause
579         E1,
580         E1_14,
581         E1_F0,
582         E1_F0_14,
583         E1_F0_14_F0,
584     } state = INIT;
585
586     uint16_t code = ibmpc_host_recv();
587     if (code == -1) {
588         return 0;
589     }
590
591     switch (state) {
592         case INIT:
593             switch (code) {
594                 case 0xE0:
595                     state = E0;
596                     break;
597                 case 0xF0:
598                     state = F0;
599                     break;
600                 case 0xE1:
601                     state = E1;
602                     break;
603                 case 0x83:  // F7
604                     matrix_make(0x02);
605                     state = INIT;
606                     break;
607                 case 0x84:  // Alt'd PrintScreen
608                     matrix_make(0x6F);
609                     state = INIT;
610                     break;
611                 case 0x00:  // Overrun [3]p.26
612                     matrix_clear();
613                     xprintf("!CS2_OVERRUN!\n");
614                     state = INIT;
615                     break;
616                 case 0xAA:  // Self-test passed
617                 case 0xFC:  // Self-test failed
618                     // reset or plugin-in new keyboard
619                     state = INIT;
620                     return -1;
621                     break;
622                 default:    // normal key make
623                     if (code < 0x80) {
624                         matrix_make(code);
625                     } else {
626                         matrix_clear();
627                         xprintf("!CS2_INIT!\n");
628                     }
629                     state = INIT;
630             }
631             break;
632         case E0:    // E0-Prefixed
633             switch (code) {
634                 case 0x12:  // to be ignored
635                 case 0x59:  // to be ignored
636                     state = INIT;
637                     break;
638                 case 0xF0:
639                     state = E0_F0;
640                     break;
641                 default:
642                     if (code < 0x80) {
643                         matrix_make(cs2_e0code(code));
644                     } else {
645                         matrix_clear();
646                         xprintf("!CS2_E0!\n");
647                     }
648                     state = INIT;
649             }
650             break;
651         case F0:    // Break code
652             switch (code) {
653                 case 0x83:  // F7
654                     matrix_break(0x02);
655                     state = INIT;
656                     break;
657                 case 0x84:  // Alt'd PrintScreen
658                     matrix_break(0x6F);
659                     state = INIT;
660                     break;
661                 default:
662                     if (code < 0x80) {
663                         matrix_break(code);
664                     } else {
665                         matrix_clear();
666                         xprintf("!CS2_F0!\n");
667                     }
668                     state = INIT;
669             }
670             break;
671         case E0_F0: // Break code of E0-prefixed
672             switch (code) {
673                 case 0x12:  // to be ignored
674                 case 0x59:  // to be ignored
675                     state = INIT;
676                     break;
677                 default:
678                     if (code < 0x80) {
679                         matrix_break(cs2_e0code(code));
680                     } else {
681                         matrix_clear();
682                         xprintf("!CS2_E0_F0!\n");
683                     }
684                     state = INIT;
685             }
686             break;
687         // Pause make: E1 14 77
688         case E1:
689             switch (code) {
690                 case 0x14:
691                     state = E1_14;
692                     break;
693                 case 0xF0:
694                     state = E1_F0;
695                     break;
696                 default:
697                     state = INIT;
698             }
699             break;
700         case E1_14:
701             switch (code) {
702                 case 0x77:
703                     matrix_make(0x00);
704                     state = INIT;
705                     break;
706                 default:
707                     state = INIT;
708             }
709             break;
710         // Pause break: E1 F0 14 F0 77
711         case E1_F0:
712             switch (code) {
713                 case 0x14:
714                     state = E1_F0_14;
715                     break;
716                 default:
717                     state = INIT;
718             }
719             break;
720         case E1_F0_14:
721             switch (code) {
722                 case 0xF0:
723                     state = E1_F0_14_F0;
724                     break;
725                 default:
726                     state = INIT;
727             }
728             break;
729         case E1_F0_14_F0:
730             switch (code) {
731                 case 0x77:
732                     matrix_break(0x00);
733                     state = INIT;
734                     break;
735                 default:
736                     state = INIT;
737             }
738             break;
739         default:
740             state = INIT;
741     }
742     return 0;
743 }
744
745 /*
746  * Terminal: Scan Code Set 3
747  *
748  * See [3], [7]
749  *
750  * Scan code 0x83 and 0x84 are handled exceptioanally to fit into 1-byte range index.
751  */
752 static int8_t process_cs3(void)
753 {
754     static enum {
755         READY,
756         F0,
757     } state = READY;
758
759     uint16_t code = ibmpc_host_recv();
760     if (code == -1) {
761         return 0;
762     }
763
764     switch (state) {
765         case READY:
766             switch (code) {
767                 case 0x00:
768                 case 0xff:
769                     xprintf("!CS3_%02X!\n", code);
770                     break;
771                 case 0xF0:
772                     state = F0;
773                     break;
774                 case 0x83:  // F7
775                     matrix_make(0x02);
776                     break;
777                 case 0x84:  // keypad -
778                     matrix_make(0x7F);
779                     break;
780                 default:    // normal key make
781                     if (code < 0x80) {
782                         matrix_make(code);
783                     } else {
784                         xprintf("!CS3_%02X!\n", code);
785                     }
786                     state = READY;
787             }
788             break;
789         case F0:    // Break code
790             switch (code) {
791                 case 0x00:
792                 case 0xff:
793                     xprintf("!CS3_F0_%02X!\n", code);
794                     state = READY;
795                     break;
796                 case 0x83:  // F7
797                     matrix_break(0x02);
798                     state = READY;
799                     break;
800                 case 0x84:  // keypad -
801                     matrix_break(0x7F);
802                     state = READY;
803                     break;
804                 default:
805                     if (code < 0x80) {
806                         matrix_break(code);
807                     } else {
808                         xprintf("!CS3_F0_%02X!\n", code);
809                     }
810                     state = READY;
811             }
812             break;
813     }
814     return 0;
815 }
816
817 /*
818  * IBM PC Keyboard Protocol Resources:
819  *
820  * [a] Microsoft USB HID to PS/2 Translation Table - Scan Code Set 1 and 2
821  * http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/translate.pdf
822  *
823  * [b] Microsoft Keyboard Scan Code Specification - Special rules of Scan Code Set 1 and 2
824  * http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/scancode.doc
825  *
826  * [1] PS/2 Reference Manuals - Collection of IBM Personal System/2 documents.
827  * http://www.mcamafia.de/pdf/pdfref.htm
828  *
829  * [2] Keyboard and Auxiliary Device Controller - Signal Timing and Format
830  * http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
831  *
832  * [3] Keyboards(101- and 102-key) - Keyboard Layout, Scan Code Set, POR, and Commands.
833  * http://www.mcamafia.de/pdf/ibm_hitrc11.pdf
834  *
835  * [4] IBM PC XT Keyboard Protocol
836  * https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-XT-Keyboard-Protocol
837  *
838  * [5] IBM Keyboard Scan Code by John Elliott - 83-key, 84-key, 102-key and 122-key
839  * https://www.seasip.info/VintagePC/index.html
840  *
841  * [6] IBM 1391406 Keyboard - Scan Code Set 2 of 102-key PS/2 keyboard
842  * https://www.seasip.info/VintagePC/ibm_1391406.html
843  *
844  * [7] The IBM 6110344 Keyboard - Scan Code Set 3 of 122-key terminal keyboard
845  * https://www.seasip.info/VintagePC/ibm_6110344.html
846  *
847  * [y] TrackPoint Engineering Specifications for version 3E
848  * https://web.archive.org/web/20100526161812/http://wwwcssrv.almaden.ibm.com/trackpoint/download.html
849  *
850  * [z] [Soarer's XT/AT/PS2/Terminal to USB converter]
851  * https://geekhack.org/index.php?topic=17458.0
852  *
853  */