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