2 Copyright 2012 Jun Wako <wakojun@gmail.com>
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.
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.
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/>.
21 #include <util/delay.h>
27 #include "protocol/serial.h"
46 static uint8_t matrix[MATRIX_ROWS];
47 #define ROW(code) ((code>>3)&0xF)
48 #define COL(code) (code&0x07)
51 static void pc98_send(uint8_t data)
53 xprintf("s%02X ", data);
54 PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
58 PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
61 static int16_t pc98_wait_response(void)
64 uint8_t timeout = 255;
65 while (timeout-- && (code = serial_recv2()) == -1) _delay_ms(1);
67 // Keyboards require RDY pulse >=37us to send next data
68 // https://archive.org/stream/PC9800TechnicalDataBookHARDWARE1993/PC-9800TechnicalDataBook_HARDWARE1993#page/n157
69 PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
71 PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
73 xprintf("r%04X ", code);
77 static void pc98_inhibit_repeat(void)
82 code = pc98_wait_response();
83 if (code != -1) dprintf("send 9C: %02X\n", code);
84 if (code != 0xFA) return;
87 code = pc98_wait_response();
88 if (code != -1) dprintf("send 70: %02X\n", code);
89 if (code != 0xFA) goto RETRY;
92 static bool pc98_is_newtype(void)
96 code = pc98_wait_response();
97 if (code != 0xFA) return false;
99 code = pc98_wait_response();
100 if (code != 0xA0) return false;
102 code = pc98_wait_response();
103 if (code != 0x80) return false;
108 static uint8_t pc98_led = 0;
109 static void pc98_led_set(void)
114 code = pc98_wait_response();
115 if (code != -1) dprintf("send 9D: %02X\n", code);
116 if (code != 0xFA) return;
119 code = pc98_wait_response();
120 if (code != -1) dprintf("send %02X: %02X\n", pc98_led, code);
121 if (code != 0xFA) goto RETRY;
124 void matrix_init(void)
126 PC98_RST_DDR |= (1<<PC98_RST_BIT);
127 PC98_RDY_DDR |= (1<<PC98_RDY_BIT);
128 PC98_RTY_DDR |= (1<<PC98_RTY_BIT);
129 PC98_RST_PORT |= (1<<PC98_RST_BIT);
130 PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
131 PC98_RTY_PORT |= (1<<PC98_RTY_BIT);
137 // https://archive.org/stream/PC9800TechnicalDataBookHARDWARE1993/PC-9800TechnicalDataBook_HARDWARE1993#page/n359
138 PC98_RDY_PORT |= (1<<PC98_RDY_BIT); // RDY: high
139 PC98_RST_PORT &= ~(1<<PC98_RST_BIT); // RST: low
140 _delay_us(15); // > 13us
141 PC98_RST_PORT |= (1<<PC98_RST_BIT); // RST: high
144 if (pc98_is_newtype()) xprintf("new type\n"); else xprintf("old type\n");
145 pc98_inhibit_repeat();
147 // initialize matrix state: all keys off
148 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
150 // ready to receive from keyboard
151 PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT); // RDY: low
156 uint8_t matrix_scan(void)
159 code = serial_recv2();
161 #ifdef PC98_LED_CONTROL
162 // Before sending command we have to make sure that there is no unprocessed key in queue
163 // otherwise keys will be missed during sending command
172 xprintf("r%02X ", code);
176 if (matrix_is_on(ROW(code), COL(code))) {
177 matrix[ROW(code)] &= ~(1<<COL(code));
181 if (!matrix_is_on(ROW(code), COL(code))) {
182 matrix[ROW(code)] |= (1<<COL(code));
186 // Keyboards require RDY pulse >=37us to send next data
187 // https://archive.org/stream/PC9800TechnicalDataBookHARDWARE1993/PC-9800TechnicalDataBook_HARDWARE1993#page/n157
188 PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
190 PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
196 uint8_t matrix_get_row(uint8_t row)
201 void led_set(uint8_t usb_led)
203 // https://archive.org/stream/PC9800TechnicalDataBookHARDWARE1993/PC-9800TechnicalDataBook_HARDWARE1993#page/n161
204 // http://www.webtech.co.jp/company/doc/undocumented_mem/io_kb.txt
206 if (usb_led & (1<<USB_LED_NUM_LOCK)) pc98_led |= (1<<0);
207 if (usb_led & (1<<USB_LED_CAPS_LOCK)) pc98_led |= (1<<2);
208 dprintf("usb_led: %02X\n", usb_led);
209 dprintf("pc98_led: %02X\n", pc98_led);