diff options
Diffstat (limited to 'converter/pc98_usb/matrix.c')
| -rw-r--r-- | converter/pc98_usb/matrix.c | 215 |
1 files changed, 215 insertions, 0 deletions
diff --git a/converter/pc98_usb/matrix.c b/converter/pc98_usb/matrix.c new file mode 100644 index 000000000..d03aaf10d --- /dev/null +++ b/converter/pc98_usb/matrix.c | |||
| @@ -0,0 +1,215 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2012 Jun Wako <wakojun@gmail.com> | ||
| 3 | |||
| 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. | ||
| 8 | |||
| 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. | ||
| 13 | |||
| 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/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include <stdint.h> | ||
| 19 | #include <stdbool.h> | ||
| 20 | #include <avr/io.h> | ||
| 21 | #include <util/delay.h> | ||
| 22 | #include "print.h" | ||
| 23 | #include "util.h" | ||
| 24 | #include "matrix.h" | ||
| 25 | #include "debug.h" | ||
| 26 | #include "protocol/serial.h" | ||
| 27 | |||
| 28 | |||
| 29 | /* | ||
| 30 | * Matrix Array usage: | ||
| 31 | * | ||
| 32 | * ROW: 16(4bits) | ||
| 33 | * COL: 8(3bits) | ||
| 34 | * | ||
| 35 | * 8bit wide | ||
| 36 | * +---------+ | ||
| 37 | * 0|00 ... 07| | ||
| 38 | * 1|08 ... 0F| | ||
| 39 | * :| ... | | ||
| 40 | * :| ... | | ||
| 41 | * E|70 ... 77| | ||
| 42 | * F|78 ... 7F| | ||
| 43 | * +---------+ | ||
| 44 | */ | ||
| 45 | static uint8_t matrix[MATRIX_ROWS]; | ||
| 46 | #define ROW(code) ((code>>3)&0xF) | ||
| 47 | #define COL(code) (code&0x07) | ||
| 48 | |||
| 49 | static bool is_modified = false; | ||
| 50 | |||
| 51 | |||
| 52 | inline | ||
| 53 | uint8_t matrix_rows(void) | ||
| 54 | { | ||
| 55 | return MATRIX_ROWS; | ||
| 56 | } | ||
| 57 | |||
| 58 | inline | ||
| 59 | uint8_t matrix_cols(void) | ||
| 60 | { | ||
| 61 | return MATRIX_COLS; | ||
| 62 | } | ||
| 63 | |||
| 64 | static void pc98_inhibit_repeat(void) | ||
| 65 | { | ||
| 66 | uint8_t code; | ||
| 67 | |||
| 68 | while (serial_recv()) ; | ||
| 69 | RETRY: | ||
| 70 | PC98_RDY_PORT |= (1<<PC98_RDY_BIT); | ||
| 71 | _delay_ms(500); | ||
| 72 | serial_send(0x9C); | ||
| 73 | |||
| 74 | PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT); | ||
| 75 | _delay_ms(100); | ||
| 76 | while (!(code = serial_recv())) ; | ||
| 77 | print("PC98: send 9C: "); print_hex8(code); print("\n"); | ||
| 78 | if (code != 0xFA) goto RETRY; | ||
| 79 | |||
| 80 | |||
| 81 | |||
| 82 | PC98_RDY_PORT |= (1<<PC98_RDY_BIT); | ||
| 83 | _delay_ms(100); | ||
| 84 | serial_send(0x70); | ||
| 85 | |||
| 86 | PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT); | ||
| 87 | _delay_ms(100); | ||
| 88 | //code = serial_recv(); | ||
| 89 | while (!(code = serial_recv())) ; | ||
| 90 | print("PC98: send 70: "); print_hex8(code); print("\n"); | ||
| 91 | if (code != 0xFA) goto RETRY; | ||
| 92 | } | ||
| 93 | |||
| 94 | void matrix_init(void) | ||
| 95 | { | ||
| 96 | print_enable = true; | ||
| 97 | // debug_enable = true; | ||
| 98 | // debug_matrix = true; | ||
| 99 | |||
| 100 | PC98_RST_DDR |= (1<<PC98_RST_BIT); | ||
| 101 | PC98_RDY_DDR |= (1<<PC98_RDY_BIT); | ||
| 102 | PC98_RTY_DDR |= (1<<PC98_RTY_BIT); | ||
| 103 | PC98_RST_PORT |= (1<<PC98_RST_BIT); | ||
| 104 | PC98_RDY_PORT |= (1<<PC98_RDY_BIT); | ||
| 105 | PC98_RTY_PORT |= (1<<PC98_RTY_BIT); | ||
| 106 | |||
| 107 | |||
| 108 | serial_init(); | ||
| 109 | |||
| 110 | // PC98 reset | ||
| 111 | /* | ||
| 112 | PC98_RST_PORT &= ~(1<<PC98_RST_BIT); | ||
| 113 | _delay_us(15); | ||
| 114 | PC98_RST_PORT |= (1<<PC98_RST_BIT); | ||
| 115 | _delay_us(13); | ||
| 116 | PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT); | ||
| 117 | */ | ||
| 118 | |||
| 119 | _delay_ms(500); | ||
| 120 | pc98_inhibit_repeat(); | ||
| 121 | |||
| 122 | |||
| 123 | // PC98 ready | ||
| 124 | PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT); | ||
| 125 | |||
| 126 | // initialize matrix state: all keys off | ||
| 127 | for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00; | ||
| 128 | |||
| 129 | debug("init\n"); | ||
| 130 | return; | ||
| 131 | } | ||
| 132 | |||
| 133 | uint8_t matrix_scan(void) | ||
| 134 | { | ||
| 135 | is_modified = false; | ||
| 136 | |||
| 137 | uint16_t code; | ||
| 138 | PC98_RDY_PORT |= (1<<PC98_RDY_BIT); | ||
| 139 | _delay_us(30); | ||
| 140 | code = serial_recv2(); | ||
| 141 | PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT); | ||
| 142 | if (code == -1) return 0; | ||
| 143 | |||
| 144 | if (code == 0x60) { | ||
| 145 | pc98_inhibit_repeat(); | ||
| 146 | |||
| 147 | /* | ||
| 148 | PC98_RDY_PORT |= (1<<PC98_RDY_BIT); | ||
| 149 | _delay_ms(100); | ||
| 150 | serial_send(0x96); | ||
| 151 | PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT); | ||
| 152 | */ | ||
| 153 | |||
| 154 | return 0; | ||
| 155 | } | ||
| 156 | |||
| 157 | print_hex8(code); print(" "); | ||
| 158 | |||
| 159 | if (code&0x80) { | ||
| 160 | // break code | ||
| 161 | if (matrix_is_on(ROW(code), COL(code))) { | ||
| 162 | matrix[ROW(code)] &= ~(1<<COL(code)); | ||
| 163 | is_modified = true; | ||
| 164 | } | ||
| 165 | } else { | ||
| 166 | // make code | ||
| 167 | if (!matrix_is_on(ROW(code), COL(code))) { | ||
| 168 | matrix[ROW(code)] |= (1<<COL(code)); | ||
| 169 | is_modified = true; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | return code; | ||
| 173 | } | ||
| 174 | |||
| 175 | bool matrix_is_modified(void) | ||
| 176 | { | ||
| 177 | return is_modified; | ||
| 178 | } | ||
| 179 | |||
| 180 | inline | ||
| 181 | bool matrix_has_ghost(void) | ||
| 182 | { | ||
| 183 | return false; | ||
| 184 | } | ||
| 185 | |||
| 186 | inline | ||
| 187 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
| 188 | { | ||
| 189 | return (matrix[row] & (1<<col)); | ||
| 190 | } | ||
| 191 | |||
| 192 | inline | ||
| 193 | uint8_t matrix_get_row(uint8_t row) | ||
| 194 | { | ||
| 195 | return matrix[row]; | ||
| 196 | } | ||
| 197 | |||
| 198 | void matrix_print(void) | ||
| 199 | { | ||
| 200 | print("\nr/c 01234567\n"); | ||
| 201 | for (uint8_t row = 0; row < matrix_rows(); row++) { | ||
| 202 | phex(row); print(": "); | ||
| 203 | pbin_reverse(matrix_get_row(row)); | ||
| 204 | print("\n"); | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | uint8_t matrix_key_count(void) | ||
| 209 | { | ||
| 210 | uint8_t count = 0; | ||
| 211 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 212 | count += bitpop(matrix[i]); | ||
| 213 | } | ||
| 214 | return count; | ||
| 215 | } | ||
