- last_time = timer_read();
-
- switch (state) {
- case START:
- if (ibmpc_protocol == IBMPC_PROTOCOL_XT) {
- // ignore start(0) bit
- if (!dbit) return;
- } else {
- if (dbit)
- goto ERROR;
- }
- case BIT0:
- case BIT1:
- case BIT2:
- case BIT3:
- case BIT4:
- case BIT5:
- case BIT6:
- case BIT7:
- data >>= 1;
- if (dbit) {
- data |= 0x80;
- parity++;
- }
- if (state == BIT7 && ibmpc_protocol == IBMPC_PROTOCOL_XT) {
- if (!ringbuf_put(&rb, data)) {
- ibmpc_error = IBMPC_ERR_FULL;
- goto ERROR;
- }
- ibmpc_error = IBMPC_ERR_NONE;
- goto DONE;
- }
+
+ 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
+ // 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
+ // 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;