- static uint16_t last_time = 0;
- static enum {
- START,
- BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7,
- PARITY,
- STOP,
- } state = START;
- static uint8_t data = 0;
- static uint8_t parity = 1;
-
- uint8_t dbit = IBMPC_DATA_PIN&(1<<IBMPC_DATA_BIT);
-
- // Reset state when taking more than 1ms
- if (last_time && timer_elapsed(last_time) > 10) {
- ibmpc_error = IBMPC_ERR_TIMEOUT | IBMPC_ERR_RECV | state;
- state = START;
- data = 0;
- parity = 1;
- }
- 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;
- }
+ uint8_t dbit;
+ dbit = IBMPC_DATA_PIN&(1<<IBMPC_DATA_BIT);
+PINB|=0x01;
+ isr_data = isr_data>>1;
+ if (dbit) isr_data |= 0x8000;
+
+ // isr_data: state of receiving data from keyboard
+ // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
+ // -----------------------------------------------------
+ // Initial: *1 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0 MSB sentinel
+ // XT IBM: b7 b6 b5 b4 b3 b2 b1 b0 | s1 s0 *1 0 0 0 0 0 after receiving **
+ // XT Clone: b7 b6 b5 b4 b3 b2 b1 b0 | s1 *1 0 0 0 0 0 0 after receiving
+ // AT: st pr b7 b6 b5 b4 b3 b2 | b1 b0 s0 *1 0 0 0 0 after receiving
+ // AT**: pr b7 b6 b5 b4 b3 b2 b1 | b0 s0 *1 0 0 0 0 0 before stop bit **
+ //
+ // 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)
+ // x x x x x x x x | 0 1 0 0 0 0 0 0 XT IBM-midway or AT-midway
+ // x x x x x x x x | 1 1 0 0 0 0 0 0 XT Clone-done
+ // x x x x x x x x | 0 0 1 0 0 0 0 0 AT-midway
+ // x x x x x x x x | 1 0 1 0 0 0 0 0 XT IBM-done or AT-midway **
+ // x x x x x x x x | x 1 1 0 0 0 0 0 illegal
+ // x x x x x x x x | x x 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
+ // other states than avobe illegal
+ //
+ // **: AT can take same as end sate of XT IBM(1010 000) when b0 is 1,
+ // to discriminate between them we will have to wait a while for stop bit.
+ //
+ // mask for isr_data:
+ // 0x00A0(1010 0000) when XT IBM
+ // 0x00C0(1100 0000) when XT Clone
+ // 0x0010(xx01 0000) when AT
+ //
+ switch (isr_data & 0xFF) {
+ case 0b00000000:
+ case 0b10000000:
+ case 0b01000000:
+ case 0b00100000:
+ // midway
+ return;