-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.asm
executable file
·306 lines (212 loc) · 4.49 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
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
; /////////////////////////////////////////////////////////////////////////////
; Znake (ZX Spectrum 48K)
; -----------------------------------------------------------------------------
; input.asm
; -----------------------------------------------------------------------------
; Copyright (C) 2016, Chris Wyatt
; All rights reserved
; Distributed under the Apache 2 license (see LICENSE)
; /////////////////////////////////////////////////////////////////////////////
check_input:
ld a,(snake_direction_current)
ld d,a
; Check for input from Kempston joystick port
ld a,(last_input)
ld b,a
; Map Q, A, O, P to Kempston bits: 000FUDLR
ld a,0xfb ; 7 R E W Q
in a,(0xfe)
cpl
rlca
rlca
rlca
and 0x08
ld c,a
ld a,0xfd ; G F D S A
in a,(0xfe)
cpl
rlca
rlca
and 0x04
or c
ld c,a
ld a,0xdf ; Y U I O P
in a,(0xfe)
cpl
and 0x03
or c
ld c,a
ld hl,flags
bit 1,(hl)
jr z,skip_check_kempston_input
in a,(0x1f)
and 0x0f
or c
ld c,a
skip_check_kempston_input:
cp b
ret z
ld (last_input),a
or a
jp po,kempston_joy_hv
; Kempston diagonal
jp po,kempston_joy_hv
; up-right (bits 0 and 3)
cp 0x09
jr z,kempston_joy_up_right
; down-right (bits 0 and 2)
cp 0x05
jr z,kempston_joy_down_right
; down-left (bits 1 and 2)
cp 0x06
jr z,kempston_joy_down_left
; up-left (bits 1 and 3)
cp 0x0a
jr z,kempston_joy_up_left
ret
kempston_joy_up_left:
; If current direction is up
bit 3,d
jr nz,queue_left
; If current direction is down
bit 2,d
jr nz,queue_left_up
; If current direction is left
bit 1,d
jr nz,queue_up
; Current direction is right
jp queue_up_left
kempston_joy_down_left:
; If current direction is up
bit 3,d
jr nz,queue_left_down
; If current direction is down
bit 2,d
jr nz,queue_left
; If current direction is left
bit 1,d
jr nz,queue_down
; Current direction is right
jp queue_down_left
kempston_joy_down_right:
; If current direction is up
bit 3,d
jr nz,queue_right_down
; If current direction is down
bit 2,d
jr nz,queue_right
; If current direction is left
bit 1,d
jr nz,queue_down_right
; Current direction is right
jp queue_down
kempston_joy_up_right:
; If current direction is up
bit 3,d
jr nz,queue_right
; If current direction is down
bit 2,d
jr nz,queue_right_up
; If current direction is left
bit 1,d
jr nz,queue_up_right
; Current direction is right
queue_up:
ld a,0x08
ld (snake_direction_queue),a
ret
queue_down:
ld a,0x04
ld (snake_direction_queue),a
ret
queue_left:
ld a,0x02
ld (snake_direction_queue),a
ret
queue_right:
ld a,0x01
ld (snake_direction_queue),a
ret
queue_up_left:
ld a,0x28
ld (snake_direction_queue),a
ret
queue_up_right:
ld a,0x18
ld (snake_direction_queue),a
ret
queue_down_left:
ld a,0x24
ld (snake_direction_queue),a
ret
queue_down_right:
ld a,0x14
ld (snake_direction_queue),a
ret
queue_left_up:
ld a,0x82
ld (snake_direction_queue),a
ret
queue_left_down:
ld a,0x42
ld (snake_direction_queue),a
ret
queue_right_up:
ld a,0x81
ld (snake_direction_queue),a
ret
queue_right_down:
ld a,0x41
ld (snake_direction_queue),a
ret
; Deal with queuing horizontal/vertical direction requests
kempston_joy_hv:
ld a,(snake_direction_queue)
ld e,a
or a
jp po,queue_one
; Use current direction as the next queued direction
ld a,d
queue_one:
cp 0x04
jr nc,next_queued_v
; Next queued direction is horizontal
; Capture only vertical directions
ld a,c
and 0x0c
ret z
jp kempston_joy_v_check
next_queued_v:
; Capture only horizontal directions
ld a,c
and 0x03
ret z
kempston_joy_h_check:
; If left and right pins are both on, return
cp 0x03
ret z
jp check_queue
kempston_joy_v_check:
; If up and down pins are both on, return
cp 0x0c
ret z
check_queue:
ld c,a
ld a,e
; If queue contains 1 direction, append
or a
jp po,append_to_direction_queue
; Otherwise replace
ld a,c
ld (snake_direction_queue),a
ret
append_to_direction_queue:
ld a,c
rlca
rlca
rlca
rlca
and 0xf0
or e
ld (snake_direction_queue),a
ret