-
Notifications
You must be signed in to change notification settings - Fork 2
/
keyb.c
279 lines (243 loc) · 6.29 KB
/
keyb.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
////////////////////////////////////////////////////////////////////////////////
//
// Tetris for ZX Spectrum 48K
//
// (C) 2019, 2020 David Crespo - https://github.com/dcrespo3d/zx-spectrum-tetris
//
////////////////////////////////////////////////////////////////////////////////
#include "keyb.h"
#include "print_tile.h"
__asm
._zxkey54321 defb 0
._zxkey67890 defb 0
._zxkeyTREWQ defb 0
._zxkeyYUIOP defb 0
._zxkeyGFDSA defb 0
._zxkeyHJKLe defb 0
._zxkeyVCXZc defb 0
._zxkeyBNMys defb 0
._nxkey0 defb 0
._nxkey1 defb 0
._keyUp defb 0
._keyDown defb 0
._keyLeft defb 0
._keyRight defb 0
._keyBut1 defb 0
._keyBut2 defb 0
;;----------------------------------------------------------------------------------------
;; reads nextreg in A into A
ReadNextReg:
; Input
; A = nextreg to read
; Output:
; A = value in nextreg
; Uses:
; A, [currently selected NextReg on I/O port $243B]
push bc
ld bc, $243B ; TBBLUE_REGISTER_SELECT_P_243B
out (c),a
inc b ; bc = TBBLUE_REGISTER_ACCESS_P_253B
in a,(c) ; read desired NextReg state
pop bc
ret
__endasm
void keyb_init()
{
// Cancel entries in 8x5 matrix for extended keys
__asm__("NEXTREG $68,$10");
}
void keyb_read_spectrum_next()
{
__asm
LD A, $B0
CALL ReadNextReg
LD (_nxkey0), A
LD A, $B1
CALL ReadNextReg
LD (_nxkey1), A
__endasm
}
void keyb_read_spectrum_std()
{
__asm
PUSH BC
LD A, $B0
CALL ReadNextReg
LD (_nxkey0), A
LD A, $B1
CALL ReadNextReg
LD (_nxkey1), A
LD C, $FE
LD B, $F7 ;12345
IN A, (C)
NEG
DEC A
AND A, $1F
LD (_zxkey54321), A
LD C, $FE
LD B, $EF ;09876
IN A, (C)
NEG
DEC A
AND A, $1F
LD (_zxkey67890), A
LD C, $FE
LD B, $FB ;QWERT
IN A, (C)
NEG
DEC A
AND A, $1F
LD (_zxkeyTREWQ), A
LD C, $FE
LD B, $DF ;POIUY
IN A, (C)
NEG
DEC A
AND A, $1F
LD (_zxkeyYUIOP), A
LD C, $FE
LD B, $FD ;ASDFG
IN A, (C)
NEG
DEC A
AND A, $1F
LD (_zxkeyGFDSA), A
LD C, $FE
LD B, $BF ;-LKJH
IN A, (C)
NEG
DEC A
AND A, $1F
LD (_zxkeyHJKLe), A
LD C, $FE
LD B, $FE ;-ZXCV
IN A, (C)
NEG
DEC A
AND A, $1F
LD (_zxkeyVCXZc), A
LD C, $FE
LD B, $7F ;--MNB
IN A, (C)
NEG
DEC A
AND A, $1F
LD (_zxkeyBNMys), A
POP BC
__endasm
}
void keyb_print()
{
print_set_pos(0, 31);
print_char('1');
print_hex_byte(zxkey54321);
print_char(' ');
print_char('0');
print_hex_byte(zxkey67890);
print_char(' ');
print_char('Q');
print_hex_byte(zxkeyTREWQ);
print_char(' ');
print_char('P');
print_hex_byte(zxkeyYUIOP);
print_char(' ');
print_char('A');
print_hex_byte(zxkeyGFDSA);
print_char(' ');
print_char('e');
print_hex_byte(zxkeyHJKLe);
print_char(' ');
print_char('c');
print_hex_byte(zxkeyVCXZc);
print_char(' ');
print_char('s');
print_hex_byte(zxkeyBNMys);
print_char(' ');
print_char('n');
print_hex_byte(nxkey0);
print_char(' ');
print_char('N');
print_hex_byte(nxkey1);
print_char(' ');
}
// ubyte zxkey_any()
// {
// if (zxkey54321) return 1;
// if (zxkey67890) return 1;
// if (zxkeyTREWQ) return 1;
// if (zxkeyYUIOP) return 1;
// if (zxkeyGFDSA) return 1;
// if (zxkeyHJKLe) return 1;
// if (zxkeyVCXZc) return 1;
// if (zxkeyBNMys) return 1;
// return 0;
// }
#define DEF_KEY_DOWN_FUNC(NAME, VAR, MASK) \
ubyte NAME() { \
__asm__("LD A, ("#VAR")"); \
__asm__("AND "#MASK""); \
__asm__("LD HL, 0"); \
__asm__("JR Z, ASMPC+3"); \
__asm__("INC HL"); \
}
DEF_KEY_DOWN_FUNC(zxkey1, _zxkey54321, 0x01);
DEF_KEY_DOWN_FUNC(zxkey2, _zxkey54321, 0x02);
DEF_KEY_DOWN_FUNC(zxkey3, _zxkey54321, 0x04);
DEF_KEY_DOWN_FUNC(zxkey4, _zxkey54321, 0x08);
DEF_KEY_DOWN_FUNC(zxkey5, _zxkey54321, 0x10);
DEF_KEY_DOWN_FUNC(zxkey6, _zxkey67890, 0x10);
DEF_KEY_DOWN_FUNC(zxkey7, _zxkey67890, 0x08);
DEF_KEY_DOWN_FUNC(zxkey8, _zxkey67890, 0x04);
DEF_KEY_DOWN_FUNC(zxkey9, _zxkey67890, 0x02);
DEF_KEY_DOWN_FUNC(zxkey0, _zxkey67890, 0x01);
DEF_KEY_DOWN_FUNC(zxkeyQ, _zxkeyTREWQ, 0x01);
DEF_KEY_DOWN_FUNC(zxkeyW, _zxkeyTREWQ, 0x02);
DEF_KEY_DOWN_FUNC(zxkeyE, _zxkeyTREWQ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N, _zxkeyBNMys, 0x08);
DEF_KEY_DOWN_FUNC(zxkeyM, _zxkeyBNMys, 0x04);
DEF_KEY_DOWN_FUNC(zxkeySYM, _zxkeyBNMys, 0x02);
DEF_KEY_DOWN_FUNC(zxkeySPC, _zxkeyBNMys, 0x01);
DEF_KEY_DOWN_FUNC(nxkeyUp, _nxkey0, 0x08);
DEF_KEY_DOWN_FUNC(nxkeyDown, _nxkey0, 0x04);
DEF_KEY_DOWN_FUNC(nxkeyLeft, _nxkey0, 0x02);
DEF_KEY_DOWN_FUNC(nxkeyRight, _nxkey0, 0x01);
DEF_KEY_DOWN_FUNC(nxkeyDel, _nxkey1, 0x80);
void keyb_update()
{
keyb_read_spectrum_std();
keyb_read_spectrum_next();
keyUp = zxkeyQ();
keyUp |= nxkeyUp();
keyDown = zxkeyA();
keyDown |= nxkeyDown();
keyLeft = zxkeyO();
keyLeft |= nxkeyLeft();
keyRight = zxkeyP();
keyRight |= nxkeyRight();
keyBut1 = zxkeySPC();
keyBut2 = zxkey0();
keyBut2 |= nxkeyDel();
}