forked from brendanalford/zx-diagnostics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.asm
241 lines (170 loc) · 3.47 KB
/
input.asm
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
;
; ZX Diagnostics - fixing ZX Spectrums in the 21st Century
; https://github.com/brendanalford/zx-diagnostics
;
; Original code by Dylan Smith
; Modifications and 128K support by Brendan Alford
;
; This code is free software; you can redistribute it and/or
; modify it under the terms of the GNU Lesser General Public
; License as published by the Free Software Foundation;
; version 2.1 of the License.
;
; This code is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
; Lesser General Public License for more details.
;
; input.asm
;
define CAPS_SHIFT 0x01
define SYMBOL_SHIFT 0x02
define KEY_LEFT 0x05
define KEY_RIGHT 0x06
define KEY_UP 0x07
define KEY_DOWN 0x08
define BREAK 0x09
define DELETE 0x10
define ENTER 0x13
keylookup_norm
defb " ", SYMBOL_SHIFT, "MNB", ENTER ,"LKJHPOIUY09876"
defb "12345QWERTASDFG", CAPS_SHIFT, "ZXCV"
keylookup_lower
defb " ", SYMBOL_SHIFT, "mnb", ENTER ,"lkjhpoiuy09876"
defb "12345qwertasdfg", CAPS_SHIFT, "zxcv"
keylookup_caps
defb BREAK, SYMBOL_SHIFT, "MNB", ENTER ,"LKJHPOIUY"
defb DELETE, "9", KEY_RIGHT, KEY_UP, KEY_DOWN
defb "1234", KEY_LEFT, "QWERTASDFG", CAPS_SHIFT, "ZXCV"
keylookup_symshift
defb " ", SYMBOL_SHIFT, ".,*", ENTER ,"=+-^", 0x22, ";i][_)('&"
defb "!@#$%qwe<>~|\\{}", CAPS_SHIFT, ":", 0x60, "?/"
;
; Scans the keyboard for a single keypress.
; A set to 1 on entry implies full upper/lower case.
; Returns with carry flag set and key in accumulator if
; found, or carry flag clear if no key pressed.
;
scan_keys
push ix
push hl
push bc
push af
ld bc, 0x7ffe
ld ix, v_keybuffer
key_row_read
in a, (c)
and 0x1f
ld (ix), a
inc ix
rrc b
ld a, b
cp 0x7f
jr nz, key_row_read
; Rows read into bitmap
pop af
ld ix, v_keybuffer
ld hl, keylookup_lower
cp 1
jr z, no_force_upcase
ld hl, keylookup_norm
no_force_upcase
bit 0, (ix+7)
jr nz, no_caps_pressed
ld hl, keylookup_caps
no_caps_pressed
bit 1, (ix)
jr nz, no_sym_pressed
ld hl, keylookup_symshift
no_sym_pressed
ld b, 8
map_row_read
ld a, 0xff
ld c, b
ld b, 5
key_loop
bit 0, (ix)
jr nz, key_next
; Key found, lookup from table
ld a, (hl)
cp 0x03
; If it's Caps or Symbol shift, continue scanning
jr c, key_next
; Else return with key in A
pop bc
pop hl
pop ix
scf
ret
key_next
inc hl
srl (ix)
djnz key_loop
map_row_next
inc ix
ld b, c
djnz map_row_read
pop bc
cp 0xff
jr z, no_key
pop hl
pop ix
scf
ret
no_key
pop hl
pop ix
and a ; reset carry flag
ret
;
; Waits for a key press (and release)
; Returns the key pressed in A
;
get_key
push bc
get_key_scan
call scan_keys
jr nc, get_key_scan
ld b, a
debounce_key
xor a
in a, (0xfe)
and 0x1f
cp 0x1f
jr nz, debounce_key
ld a, b
pop bc
ret
IFDEF TESTROM
; Check to see if the upper three bits of the Kempston port are
; at any time non-zero - this would indicate a faulty interface or
; one that's not present.
; Bit 7 of v_kempston reflects result.
detect_kempston
ld hl, v_kempston
xor a
ld (hl), a
detect_kemp_loop
in a, (0x1f)
ld c, a
and 0xe0
; Are any of the top bits set?
cp 0
ret nz
; Loop a bit to make sure
djnz detect_kemp_loop
; Kempston appears to be present
set 7, (hl)
ret
; Reads Kempston I/F values (if present) and stores them
; in system variable v_kempston.
read_kempston
ld hl, v_kempston
bit 7, (hl)
ret z
in a, (0x1f)
and 0x1f
or 0x80
ld (hl), a
ret
ENDIF