Add initial files for PC98
[tmk_keyboard.git] / converter / pc98_usb / matrix.c
CommitLineData
f68c5bf0 1/*
2Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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 */
45static uint8_t matrix[MATRIX_ROWS];
46#define ROW(code) ((code>>3)&0xF)
47#define COL(code) (code&0x07)
48
49static bool is_modified = false;
50
51
52inline
53uint8_t matrix_rows(void)
54{
55 return MATRIX_ROWS;
56}
57
58inline
59uint8_t matrix_cols(void)
60{
61 return MATRIX_COLS;
62}
63
64void matrix_init(void)
65{
66 print_enable = true;
67 debug_enable = true;
68
69 PC98_RST_DDR |= (1<<PC98_RST_BIT);
70 PC98_RDY_DDR |= (1<<PC98_RDY_BIT);
71 PC98_RTY_DDR |= (1<<PC98_RTY_BIT);
72 PC98_RST_PORT |= (1<<PC98_RST_BIT);
73 PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
74 PC98_RTY_PORT |= (1<<PC98_RTY_BIT);
75
76
77 serial_init();
78
79 // PC98 reset
80 PC98_RST_PORT &= ~(1<<PC98_RST_BIT);
81 _delay_us(15);
82 PC98_RST_PORT |= (1<<PC98_RST_BIT);
83 _delay_us(13);
84 PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
85
86 // initialize matrix state: all keys off
87 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
88
89 debug("init\n");
90 return;
91}
92
93uint8_t matrix_scan(void)
94{
95 is_modified = false;
96
97 uint8_t code;
98 PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
99 code = serial_recv();
100 PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
101 if (!code) return 0;
102
103 debug_hex(code); debug(" ");
104
105/*
106 switch (code) {
107 case 0x7E: // reset fail
108 case 0xFE: // layout
109 case 0xFF: // reset success
110 _delay_ms(500);
111 // ignore response byte
112 debug("(response ignored:");
113 while ((code = serial_recv())) { debug(" "); debug_hex(code); }
114 debug(") ");
115 // FALL THROUGH
116 case 0x7F:
117 // all keys up
118 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
119 return 0;
120 }
121
122 if (code&0x80) {
123 // break code
124 if (matrix_is_on(ROW(code), COL(code))) {
125 matrix[ROW(code)] &= ~(1<<COL(code));
126 is_modified = true;
127 }
128 } else {
129 // make code
130 if (!matrix_is_on(ROW(code), COL(code))) {
131 matrix[ROW(code)] |= (1<<COL(code));
132 is_modified = true;
133 }
134 }
135*/
136 return code;
137}
138
139bool matrix_is_modified(void)
140{
141 return is_modified;
142}
143
144inline
145bool matrix_has_ghost(void)
146{
147 return false;
148}
149
150inline
151bool matrix_is_on(uint8_t row, uint8_t col)
152{
153 return (matrix[row] & (1<<col));
154}
155
156inline
157uint8_t matrix_get_row(uint8_t row)
158{
159 return matrix[row];
160}
161
162void matrix_print(void)
163{
164 print("\nr/c 01234567\n");
165 for (uint8_t row = 0; row < matrix_rows(); row++) {
166 phex(row); print(": ");
167 pbin_reverse(matrix_get_row(row));
168 print("\n");
169 }
170}
171
172uint8_t matrix_key_count(void)
173{
174 uint8_t count = 0;
175 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
176 count += bitpop(matrix[i]);
177 }
178 return count;
179}
Imprint / Impressum