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 PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
57 PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
60 static int16_t pc98_wait_response(void)
63 uint8_t timeout = 255;
64 while (timeout-- && (code = serial_recv2()) == -1) _delay_ms(1);
68 static void pc98_inhibit_repeat(void)
73 while (serial_recv()) ;
77 code = pc98_wait_response();
78 if (code != -1) xprintf("PC98: send 9C: %02X\n", code);
79 if (code != 0xFA) goto RETRY;
83 code = pc98_wait_response();
84 if (code != -1) xprintf("PC98: send 70: %02X\n", code);
85 if (code != 0xFA) goto RETRY;
88 void matrix_init(void)
90 debug_keyboard = true;
91 PC98_RST_DDR |= (1<<PC98_RST_BIT);
92 PC98_RDY_DDR |= (1<<PC98_RDY_BIT);
93 PC98_RTY_DDR |= (1<<PC98_RTY_BIT);
94 PC98_RST_PORT |= (1<<PC98_RST_BIT);
95 PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
96 PC98_RTY_PORT |= (1<<PC98_RTY_BIT);
103 PC98_RST_PORT &= ~(1<<PC98_RST_BIT);
105 PC98_RST_PORT |= (1<<PC98_RST_BIT);
107 PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
111 pc98_inhibit_repeat();
115 PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
117 // initialize matrix state: all keys off
118 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
124 uint8_t matrix_scan(void)
127 PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
129 code = serial_recv2();
130 PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
131 if (code == -1) return 0;
133 print_hex8(code); print(" ");
137 if (matrix_is_on(ROW(code), COL(code))) {
138 matrix[ROW(code)] &= ~(1<<COL(code));
142 if (!matrix_is_on(ROW(code), COL(code))) {
143 matrix[ROW(code)] |= (1<<COL(code));
150 uint8_t matrix_get_row(uint8_t row)
155 void led_set(uint8_t usb_led)
157 #ifdef PC98_LED_CONTROL
158 uint8_t led_state = 0x70;
159 if (usb_led & (1<<USB_LED_NUM_LOCK)) led_state |= (1<<0);
160 if (usb_led & (1<<USB_LED_CAPS_LOCK)) led_state |= (1<<2);
161 xprintf("led_set: %02X\n", led_state);
165 pc98_send(led_state);
166 // responses(FA or FC) will be ignored in matrix_scan()