Repository navigation
Expand file tree
/
Copy pathkbd.cpp
More file actions
183 lines (166 loc) · 5.18 KB
/
Copy pathkbd.cpp
File metadata and controls
183 lines (166 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <cstdint>
#include <compat.h>
#include <debugging.h>
#include "kbd.h"
// http://www.6502.org/users/andre/petindex/keyboards.html
// maps scan codes to pet rows/cols (graphics keyboard)
static const uint8_t scanmap[128] PROGMEM = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 0x00
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 0x08
0xff, 0xff, 0x80, 0xff, 0xff, 0x20, 0x00, 0xff, // 0x10
0xff, 0xff, 0x60, 0x50, 0x40, 0x30, 0x10, 0xff, // 0x18
0xff, 0x61, 0x70, 0x41, 0x21, 0x11, 0x01, 0xff, // 0x20
0xff, 0x92, 0x71, 0x51, 0x22, 0x31, 0x02, 0xff, // 0x28
0xff, 0x72, 0x62, 0x52, 0x42, 0x32, 0x25, 0xff, // 0x30
0xff, 0xff, 0x63, 0x43, 0x23, 0x03, 0x57, 0xff, // 0x38
0xff, 0x73, 0x53, 0x33, 0x24, 0x14, 0x04, 0xff, // 0x40
0xff, 0x96, 0x37, 0x44, 0x64, 0x34, 0x87, 0xff, // 0x48
0xff, 0xff, 0x12, 0xff, 0x91, 0x97, 0xff, 0xff, // 0x50
0xff, 0x85, 0x65, 0x82, 0xff, 0x01, 0xff, 0xff, // 0x58
0xff, 0x13, 0xff, 0xff, 0xff, 0x05, 0x17, 0xff, // 0x60
0xff, 0x66, 0xff, 0x46, 0x26, 0xff, 0xff, 0xff, // 0x68
0x86, 0x96, 0x76, 0x56, 0x47, 0x36, 0xff, 0x25, // 0x70
0xff, 0x77, 0x67, 0x87, 0x57, 0x27, 0xff, 0xff, // 0x78
};
// accessed by R-Shift
static const uint8_t shiftmap[128] PROGMEM = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 0x00
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 0x08
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, // 0x10
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x10, 0xff, // 0x18
0xff, 0xff, 0xff, 0xff, 0xff, 0x11, 0xff, 0xff, // 0x20
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x02, 0xff, // 0x28
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x25, 0xff, // 0x30
0xff, 0xff, 0xff, 0xff, 0xff, 0x03, 0x57, 0xff, // 0x38
0xff, 0x93, 0xff, 0xff, 0xff, 0x14, 0x04, 0xff, // 0x40
0xff, 0x84, 0x74, 0xff, 0x54, 0xff, 0xff, 0xff, // 0x48
0xff, 0xff, 0x81, 0xff, 0xff, 0x77, 0xff, 0xff, // 0x50
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 0x58
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 0x60
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 0x68
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 0x70
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 0x78
};
// accessed by Ctrl
static const uint8_t ctrlmap[128] PROGMEM = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 0x00
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 0x08
0xff, 0xff, 0xff, 0xff, 0xff, 0x16, 0xff, 0xff, // 0x10
0xff, 0xff, 0xff, 0x06, 0xff, 0xff, 0xff, 0xff, // 0x18
0xff, 0x94, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 0x20
0xff, 0xff, 0xff, 0xff, 0x17, 0x90, 0xff, 0xff, // 0x28
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 0x30
0xff, 0xff, 0x65, 0xff, 0xff, 0xff, 0xff, 0xff, // 0x38
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 0x40
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 0x48
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 0x50
0xff, 0xff, 0xff, 0x07, 0xff, 0xff, 0xff, 0xff, // 0x58
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 0x60
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 0x68
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 0x70
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 0x78
};
uint8_t kbd::_map(uint8_t scan) const {
if (_shift)
return pgm_read_byte(shiftmap + scan);
if (_ctrl)
return pgm_read_byte(ctrlmap + scan);
return pgm_read_byte(scanmap + scan);
}
void kbd::_reset(uint8_t k) {
_rows[(k & 0xf0) >> 4] &= ~(1 << (k & 0x0f));
}
void kbd::up(uint16_t scan) {
DBG_EMU("key up: %04x", scan);
if (scan == PS2_RSHIFT) {
reset(); // reset any depressed keys so they don't get stuck
_shift = false;
} else if (is_ps2_ctrl(scan)) {
reset();
_ctrl = false;
} else if (scan < 0x80)
_reset(_map(scan));
else if (scan & PS2_MOD_EXT) {
switch(scan) {
case PS2_END:
_reset(0x80); // fallthrough
case PS2_HOME:
_reset(0x06);
break;
case PS2_UP:
_reset(0x80); // fallthrough
case PS2_DOWN:
_reset(0x16);
break;
case PS2_LEFT:
_reset(0x80); // fallthrough
case PS2_RIGHT:
_reset(0x07);
break;
case PS2_INS:
_reset(0x80); // fallthrough
case PS2_DEL:
_reset(0x17);
break;
case PS2_KEYPAD_ENTER:
_reset(_map(0x5a));
break;
case PS2_KEYPAD_SLASH:
_reset(_map(0x4a));
break;
default: // ignore other extended keys
break;
}
}
}
void kbd::_set(uint8_t k) {
_rows[(k & 0xf0) >> 4] |= 1 << (k & 0x0f);
}
void kbd::down(uint16_t scan) {
DBG_EMU("key down: %04x", scan);
if (scan == PS2_RSHIFT) {
reset(); // reset any depressed keys so they don't get stuck
_shift = true;
} else if (is_ps2_ctrl(scan)) {
reset();
_ctrl = true;
} else if (scan < 0x80)
_set(_map(scan));
else if (scan & PS2_MOD_EXT) {
switch (scan) {
case PS2_END:
_set(0x80); // fallthrough
case PS2_HOME:
_set(0x06);
break;
case PS2_UP:
_set(0x80); // fallthrough
case PS2_DOWN:
_set(0x16);
break;
case PS2_LEFT:
_set(0x80); // fallthrough
case PS2_RIGHT:
_set(0x07);
break;
case PS2_INS:
_set(0x80); // fallthrough
case PS2_DEL:
_set(0x17);
break;
case PS2_KEYPAD_ENTER:
_set(_map(0x5a));
break;
case PS2_KEYPAD_SLASH:
_set(_map(0x4a));
break;
default: // ignore other extended keys
break;
}
}
}
void kbd::reset() {
for (int i = sizeof(_rows); i--; )
_rows[i] = 0;
_ctrl = _shift = false;
}