+ isr_state = isr_state>>1;
+ if (dbit) isr_state |= 0x8000;
+
+ // isr_state: state of receiving data from keyboard
+ //
+ // This should be initialized with 0x8000 before receiving data and
+ // the MSB '*1' works as marker to discrimitate between protocols.
+ // It stores sampled bit at MSB after right shift on each clock falling edge.
+ //
+ // XT protocol has two variants of signaling; XT_IBM and XT_Clone.
+ // XT_IBM uses two start bits 0 and 1 while XT_Clone uses just start bit 1.
+ // https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-XT-Keyboard-Protocol
+ //
+ // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
+ // -----------------------------------------------------
+ // *1 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0 Initial state(0x8000)
+ //
+ // x x x x x x x x | 0 0 0 0 0 0 0 0 midway(0-7 bits received)
+ // x x x x x x x x | *1 0 0 0 0 0 0 0 midway(8 bits received)
+ // b6 b5 b4 b3 b2 b1 b0 1 | 0 *1 0 0 0 0 0 0 XT_IBM-midway ^1
+ // b7 b6 b5 b4 b3 b2 b1 b0 | 0 *1 0 0 0 0 0 0 AT-midway ^1
+ // b7 b6 b5 b4 b3 b2 b1 b0 | 1 *1 0 0 0 0 0 0 XT_Clone-done ^3
+ // b6 b5 b4 b3 b2 b1 b0 1 | 1 *1 0 0 0 0 0 0 XT_IBM-error ^3
+ // pr b7 b6 b5 b4 b3 b2 b1 | 0 0 *1 0 0 0 0 0 AT-midway[b0=0]
+ // b7 b6 b5 b4 b3 b2 b1 b0 | 1 0 *1 0 0 0 0 0 XT_IBM-done ^2
+ // pr b7 b6 b5 b4 b3 b2 b1 | 1 0 *1 0 0 0 0 0 AT-midway[b0=1] ^2
+ // b7 b6 b5 b4 b3 b2 b1 b0 | 1 1 *1 0 0 0 0 0 XT_IBM-error-done
+ // x x x x x x x x | x 1 1 0 0 0 0 0 illegal
+ // st pr b7 b6 b5 b4 b3 b2 | b1 b0 0 *1 0 0 0 0 AT-done
+ // x x x x x x x x | x x 1 *1 0 0 0 0 illegal
+ // all other states than above illegal
+ //
+ // ^1: AT and XT_IBM takes same state.
+ // ^2: AT and XT_IBM takes same state in case that AT b0 is 1,
+ // we have to check AT stop bit to discriminate between the two protocol.
+ switch (isr_state & 0xFF) {
+ case 0b00000000:
+ case 0b10000000:
+ case 0b01000000: // ^1
+ case 0b00100000:
+ // midway
+ goto NEXT;