forked from EtchedPixels/FUZIX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpckeyboard.c
439 lines (386 loc) · 14.8 KB
/
cpckeyboard.c
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#include <kernel.h>
#include <kdata.h>
#include <printf.h>
#include <stdbool.h>
#include <keycode.h>
#include <vt.h>
#include <tty.h>
#include <input.h>
#include <devinput.h>
/* buffer for port scan procedure */
uint8_t keybuf[10];
/* Previous state */
uint8_t keymap[10];
struct vt_repeat keyrepeat = { 150, 25 };
static uint8_t kbd_timer;
static uint8_t keybyte, keybit;
static uint8_t newkey;
static int keysdown = 0;
uint8_t keyboard[10][8] = {
{KEY_UP, KEY_RIGHT, KEY_DOWN, '9', '6', '3',13, '.'},
{KEY_LEFT ,KEY_COPY , '7', '8', '5', '1', '2', '0'},
{KEY_DEL, '[', 13, ']', '4', 0/*SHIFT*/, '\\', 0/*CONTROL*/ },
{'^', '-', '@', 'p', ';', ':', '/', '.'},
{'0', '9', 'o', 'i', 'l', 'k', 'm', ','},
{'8', '7', 'u', 'y', 'h', 'j', 'n', ' '},
{'6', '5', 'r', 't', 'g', 'f', 'b', 'v'},
{'4', '3', 'e', 'w', 's', 'd', 'c', 'x'},
{'1', '2', KEY_ESC, 'q', KEY_TAB, 'a', KEY_CAPSLOCK, 'z'},
{KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, 13, ' ', 0, KEY_BS}
};
/* SHIFT MODE */
uint8_t shiftkeyboard[10][8] = {
{KEY_UP, KEY_RIGHT, KEY_DOWN, KEY_F9, KEY_F6, KEY_F3,KEY_ENTER, KEY_F11},
{KEY_LEFT ,KEY_PASTE , KEY_F7, KEY_F8, KEY_F5, KEY_F1, KEY_F2, KEY_F10},
{KEY_DEL, '{', 13, '}', KEY_F4, 0/*SHIFT*/, '`', 0/*CONTROL*/ },
{KEY_POUND, '=', '|', 'P', '+', '*', '?', '>'},
{'_', ')', 'O', 'I', 'L', 'K', 'M', '<'},
{'(', 39/*'*/, 'U', 'Y', 'H', 'J', 'N', ' '},
{'&', '%', 'R', 'T', 'G', 'F', 'B', 'V'},
{'$', '#', 'E', 'W', 'S', 'D', 'C', 'X'},
{'!', '"', KEY_ESC, 'Q', KEY_TAB, 'A', 0/*CAPSLOCK*/, 'Z'},
{KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, 13, ' ', 0, KEY_BS}
};
static uint8_t shiftmask[10] = { 0, 0, 0xA0, 0, 0, 0, 0, 0, 0, 0 };
static uint8_t capslock = 0;
static void keydecode(void)
{
uint8_t m = 0;
uint8_t c;
uint8_t sh = keymap[2] & 0x20; /* SHIFT */
uint8_t ct = keymap[2] & 0x80; /* CONTROL */
if (sh) {
m = KEYPRESS_SHIFT;
c = shiftkeyboard[keybyte][keybit];
} else {
c = keyboard[keybyte][keybit];
}
if (c == KEY_CAPSLOCK) {
capslock = 1 - capslock;
return;
}
if (capslock && c >= 'a' && c <= 'z')
c -= 'a' - 'A';
if (ct) {
m |= KEYPRESS_CTRL;
if (c >= 'a' && c <= 'z')
c = CTRL(c - ('a' - 'A'));
else
c = CTRL(c);
}
if (c) {
switch (keyboard_grab) {
case 0:
vt_inproc(1, c);
break;
case 1:
if (!input_match_meta(c)) {
vt_inproc(1, c);
break;
}
/* Fall through */
case 2:
queue_input(KEYPRESS_DOWN);
queue_input(c);
break;
case 3:
/* Queue an event giving the base key (unshifted)
and the state of shift/ctrl/alt */
queue_input(KEYPRESS_DOWN | m);
queue_input(keyboard[keybyte][keybit]);
break;
}
}
}
void tty_pollirq(void)
{
int i;
newkey = 0;
/* Nothing changed, and chance of key repeat work - so done */
if (!update_keyboard() && !keysdown)
return;
for (i = 0; i < 10; i++) {
int n;
uint8_t key = (~keybuf[i]) ^ keymap[i];
if (key) {
uint8_t m = 0x80;
for (n = 7; n >=0; n--) {
if ((key & m) && (keymap[i] & m))
if (!(shiftmask[i] & m)) {
if (keyboard_grab == 3) {
queue_input(KEYPRESS_UP);
queue_input(keyboard[i][n]);
}
keysdown--;
}
if ((key & m) && !(keymap[i] & m)) {
if (!(shiftmask[i] & m)) {
keysdown++;
newkey = 1;
keybyte = i;
keybit = n;
}
}
m >>= 1;
}
}
keymap[i] = ~keybuf[i];
}
if (keysdown && keysdown < 3) {
if (newkey) {
keydecode();
kbd_timer = keyrepeat.first;
} else if (! --kbd_timer) {
keydecode();
kbd_timer = keyrepeat.continual;
}
}
}
static uint8_t update_keyboard(void) __naked
{
__asm
ld bc, #0xF782 ;; [3] Configure PPI 8255: Set Both Port A and Port C as Output.
out (c), c ;; [4] 82 = 1000 0010 : (B7=1)=> I/O Mode, (B6-5=00)=> Mode 1,
;; (B4=0)=> Port A=Output, (B3=0)=> Port Cu=Output,
;; (B2=0)=> Group B, Mode 0,(B1=1)=> Port B=Input, (B0=0)=> Port Cl=Output
ld bc, #0xF40E ;; [3] Write (0Eh = 14) on PPI 8255 Port A (F4h): the register we want to select on AY-3-8912
ld e, b ;; [1] Save F4h into E to use it later in the loop
out (c), c ;; [4]
ld bc, #0xF6C0 ;; [3] Write (C0h = 11 000000b) on PPI Port C (F6h): operation > select register
ld d, b ;; [1] Save F6h into D to use it later in the loop
out (c), c ;; [4]
.dw #0x71ED ; out (c), 0 ;; [4] out (C), 0 => Write 0 on PPI's Port C to put PSG's in inactive mode
;; .... (required in between different operations)
ld bc, #0xF792 ;; [3] Configure PPI 8255: Set Port A = Input, Port C = Output.
out (c), c ;; [4] 92h= 1001 0010 : (B7=1)=> I/O Mode, (B6-5=00)=> Mode 1,
;; (B4=1)=> Port A=Input, (B3=0)=> Port Cu=Output,
;; (B2=0)=> Group B, Mode 0, (B1=1)=> Port B=Input, (B0=0)=> Port Cl=Output
;; Read Loop (Unrolled version): We read the 10-bytes that define the pressed/not pressed status
;;
ld a, #0x40 ;; [2] A refers to the next keyboard line to be read (40h to 49h)
ld hl, #_keybuf ;; [3] HL Points to the start of the keyboardBuffer,
;; ... where scanned data will be stored
;; Read line 40h
ld b, d ;; [1] B = F6h => Write the value of A to PPI's Port C to select next Matrix Line
out (c), a ;; [4]
ld b, e ;; [1] B = F4h => Read from PPI's Port A: Pressed/Not Pressed Values from PSG
ini ;; [5] The read value is written to (HL), then HL<-HL+1 and B<-B-1
inc a ;; [1] Loop: Increment A => Next Matrix Line.
;; Read line 41h
ld b, d ;; [1] Same for line 41h
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
inc a ;; [1]
;; Read line 42h
ld b, d ;; [1] Same for line 42h
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
inc a ;; [1]
;; Read line 43h
ld b, d ;; [1] Same for line 43h
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
inc a ;; [1]
;; Read line 44h
ld b, d ;; [1] Same for line 44h
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
inc a ;; [1]
;; Read line 45h
ld b, d ;; [1] Same for line 45h
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
inc a ;; [1]
;; Read line 46h
ld b, d ;; [1] Same for line 46h
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
inc a ;; [1]
;; Read line 47h
ld b, d ;; [1] Same for line 47h
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
inc a ;; [1]
;; Read line 48h
ld b, d ;; [1] Same for line 48h
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
inc a ;; [1]
;; Read line 49h
ld b, d ;; [1] Same for line 49h (Except we don't have to increase a anymore)
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
;; Restore PPI status to Port A=Output, Port C=Output
;;
ld bc, #0xF782 ;; [3] Put again PPI in Output/Output mode for Ports A/C.
out (c), c ;; [4]
ld hl, #_keybuf ;; [3] HL Points to the start of the keyboard status buffer
ld b, #9 ;; [2] We are going to do 9 AND operations against the first byte of the buffer
ld a, (hl) ;; [2] A = First byte from keyboardStatusBuffer
inc hl ;; [2] HL points to the next byte from the KeyboardStatusBuffer
and (hl) ;; [2] A = A & NextByte (The byte pointed by HL)
inc hl ;; [2] Repeat for byte Buffer+2
and (hl) ;; [2]
inc hl ;; [2] Repeat for byte Buffer+3
and (hl) ;; [2]
inc hl ;; [2] Repeat for byte Buffer+4
and (hl) ;; [2]
inc hl ;; [2] Repeat for byte Buffer+5
and (hl) ;; [2]
inc hl ;; [2] Repeat for byte Buffer+6
and (hl) ;; [2]
inc hl ;; [2] Repeat for byte Buffer+7
and (hl) ;; [2]
inc hl ;; [2] Repeat for byte Buffer+8
and (hl) ;; [2]
inc hl ;; [2] Repeat for byte Buffer+9
and (hl) ;; [2]
inc a ;; [1] A holds the result of ANDing the 10 bytes. If no key is pressed, all bits should
;; ... be 1, so A=0xFF. If we add 1, A=0, we return FALSE (no key is pressed).
;; ... If any key is pressed, some bit will be 0, so A != 0xFF, which means A+1 != 0, and
;; ... we will be returning TRUE (A > 0)
ld l, a ;; [1] L = A (Set return value for C calls in L)
ret
__endasm;
}
/*{
//from https://github.com/lronaldo/cpctelera/blob/master/cpctelera/src/keyboard/cpct_scanKeyboard_if.s
//and https://github.com/lronaldo/cpctelera/blob/master/cpctelera/src/keyboard/cpct_isAnyKeyPressed_f.s
__asm
;; Configure PPI: Select Register 14 (the one connected with keyboard status) and set it for reading
;;
ex af, af'
push af
ex af, af'
ld bc, #0xF782 ;; [3] Configure PPI 8255: Set Both Port A and Port C as Output.
out (c), c ;; [4] 82 = 1000 0010 : (B7=1)=> I/O Mode, (B6-5=00)=> Mode 1,
;; (B4=0)=> Port A=Output, (B3=0)=> Port Cu=Output,
;; (B2=0)=> Group B, Mode 0,(B1=1)=> Port B=Input, (B0=0)=> Port Cl=Output
ld bc, #0xF40E ;; [3] Write (0Eh = 14) on PPI 8255 Port A (F4h): the register we want to select on AY-3-8912
ld e, b ;; [1] Save F4h into E to use it later in the loop
out (c), c ;; [4]
ld bc, #0xF6C0 ;; [3] Write (C0h = 11 000000b) on PPI Port C (F6h): operation > select register
ld d, b ;; [1] Save F6h into D to use it later in the loop
out (c), c ;; [4]
.dw #0x71ED ; out (c), 0 ;; [4] out (C), 0 => Write 0 on PPI's Port C to put PSG's in inactive mode
;; .... (required in between different operations)
ld bc, #0xF792 ;; [3] Configure PPI 8255: Set Port A = Input, Port C = Output.
out (c), c ;; [4] 92h= 1001 0010 : (B7=1)=> I/O Mode, (B6-5=00)=> Mode 1,
;; (B4=1)=> Port A=Input, (B3=0)=> Port Cu=Output,
;; (B2=0)=> Group B, Mode 0, (B1=1)=> Port B=Input, (B0=0)=> Port Cl=Output
;; Read Loop (Unrolled version): We read the 10-bytes that define the pressed/not pressed status
;;
ld a, #0x40 ;; [2] A refers to the next keyboard line to be read (40h to 49h)
ld hl, #_keybuf ;; [3] HL Points to the start of the keyboardBuffer,
;; ... where scanned data will be stored
;; Read line 40h
ld b, d ;; [1] B = F6h => Write the value of A to PPI's Port C to select next Matrix Line
out (c), a ;; [4]
ld b, e ;; [1] B = F4h => Read from PPI's Port A: Pressed/Not Pressed Values from PSG
ini ;; [5] The read value is written to (HL), then HL<-HL+1 and B<-B-1
ex af, af'
ld a, (hl)
ex af, af'
inc a ;; [1] Loop: Increment A => Next Matrix Line.
;; Read line 41h
ld b, d ;; [1] Same for line 41h
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
ex af, af'
and (hl)
ex af, af'
inc a ;; [1]
;; Read line 42h
ld b, d ;; [1] Same for line 42h
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
ex af, af'
and (hl)
ex af, af'
inc a ;; [1]
;; Read line 43h
ld b, d ;; [1] Same for line 43h
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
ex af, af'
and (hl)
ex af, af'
inc a ;; [1]
;; Read line 44h
ld b, d ;; [1] Same for line 44h
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
ex af, af'
and (hl)
ex af, af'
inc a ;; [1]
;; Read line 45h
ld b, d ;; [1] Same for line 45h
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
ex af, af'
and (hl)
ex af, af'
inc a ;; [1]
;; Read line 46h
ld b, d ;; [1] Same for line 46h
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
ex af, af'
and (hl)
ex af, af'
inc a ;; [1]
;; Read line 47h
ld b, d ;; [1] Same for line 47h
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
ex af, af'
and (hl)
ex af, af'
inc a ;; [1]
;; Read line 48h
ld b, d ;; [1] Same for line 48h
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
ex af, af'
and (hl)
ex af, af'
inc a ;; [1]
;; Read line 49h
ld b, d ;; [1] Same for line 49h (Except we dont have to increase a anymore)
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
;; Restore PPI status to Port A=Output, Port C=Output
;;
ld bc, #0xF782 ;; [3] Put again PPI in Output/Output mode for Ports A/C.
out (c), c ;; [4]
ex af, af'
and (hl)
inc a ;; [1] A holds the result of ANDing the 10 bytes. If no key is pressed, all bits should
;; ... be 1, so A=0xFF. If we add 1, A=0, we return FALSE (no key is pressed).
;; ... If any key is pressed, some bit will be 0, so A != 0xFF, which means A+1 != 0, and
;; ... we will be returning TRUE (A > 0)
ld l, a ;; [1] L = A (Set return value for C calls in L)
pop af
ex af, af'
ret ;; [3] Return
__endasm;
}*/