-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathbuttons.asm
385 lines (367 loc) · 7.4 KB
/
buttons.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
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
BUTTONS .rs 1 ; currently pressed buttons
BUTTONS_TMP .rs 1 ; temporary variable for buttons
BUTTONS_HOLD_TIME .rs 1 ; up/down/left/right buttons hold time
KONAMI_CODE_STATE .rs 1 ; Konami Code state
.if GAMES_COUNT >= 11
MAXIMUM_SCROLL .equ GAMES_COUNT - 11
.else
MAXIMUM_SCROLL .equ 0
.endif
; controller reading, two times
read_controller:
pha
tya
pha
txa
pha
jsr .real ; first read
ldx <BUTTONS_TMP
jsr .real ; second read
cpx <BUTTONS_TMP ; lets compare values
bne .end ; ignore it if not equal
stx <BUTTONS ; storing value
txa
and #%11110000 ; up/down/left/right
beq .no_up_down ; is pressed?
inc <BUTTONS_HOLD_TIME ; increasing hold time
lda <BUTTONS_HOLD_TIME
cmp #BUTTON_REPEAT_FRAMES ; is it holding long enought?
bcc .end ; no
lda #0 ; yes, it's long enought, so lets "release" buttons
sta <BUTTONS
lda #BUTTON_REPEAT_FRAMES - 10 ; autorepeat time = 10
sta <BUTTONS_HOLD_TIME
jmp .end
.no_up_down:
lda #0 ; reset hold time
sta <BUTTONS_HOLD_TIME
.end:
pla
tax
pla
tay
pla
rts
; real controller read, stores buttons to BUTTONS_TMP
.real:
lda #1
sta JOY1
lda #0
sta JOY1
ldy #8
.read_button:
lda JOY1
and #$03
cmp #$01
ror <BUTTONS_TMP
dey
bne .read_button
rts
; check buttons state and do some action
buttons_check:
; if buttons are not pressed at all return immediately
lda <BUTTONS
cmp #$00
bne .start_check
rts
.start_check:
jsr konami_code_check
.button_a:
lda <BUTTONS
and #%00000001
beq .button_b
jmp start_game
.button_b:
lda <BUTTONS
and #%00000010
beq .button_start
; nothing to do
jmp .button_end
.button_start:
lda <BUTTONS
and #%00001000
beq .button_up
jmp start_game
.button_up:
lda <BUTTONS
and #%00010000
beq .button_down
.if ENABLE_SOUND!=0
jsr bleep
.endif
lda <SELECTED_GAME
sec
sbc #1
sta <SELECTED_GAME
lda <SELECTED_GAME+1
sbc #0
sta <SELECTED_GAME+1
bmi .button_up_ovf
jsr check_separator_up
jmp .button_end
.button_up_ovf:
.if GAMES_COUNT < WRAP_GAMES
lda #LOW(GAMES_COUNT - 1)
sta <SELECTED_GAME
lda #HIGH(GAMES_COUNT - 1)
sta <SELECTED_GAME+1
.else
jsr screen_wrap_up
.endif
jsr check_separator_up
jmp .button_end
.button_down:
lda <BUTTONS
and #%00100000
beq .button_left
.if ENABLE_SOUND!=0
jsr bleep
.endif
lda <SELECTED_GAME
clc
adc #1
sta <SELECTED_GAME
lda <SELECTED_GAME+1
adc #0
sta <SELECTED_GAME+1
cmp #HIGH(GAMES_COUNT)
bne .button_down_not_ovf
lda <SELECTED_GAME
cmp #LOW(GAMES_COUNT)
beq .button_down_ovf
.button_down_not_ovf:
jsr check_separator_down
jmp .button_end
.button_down_ovf:
.if GAMES_COUNT < WRAP_GAMES
lda #0
sta <SELECTED_GAME
sta <SELECTED_GAME+1
sta <SCROLL_LINES_TARGET
sta <SCROLL_LINES_TARGET+1
.else
jsr screen_wrap_down
.endif
jsr check_separator_down
jmp .button_end
.button_left:
lda <BUTTONS
and #%01000000
beq .button_right
lda <SELECTED_GAME
bne .button_left_bleep
lda <SELECTED_GAME+1
bne .button_left_bleep
jmp .button_right
.button_left_bleep:
.if ENABLE_SOUND!=0
jsr bleep
.endif
lda <SCROLL_LINES_TARGET
sec
sbc #10
sta <SCROLL_LINES_TARGET
lda <SCROLL_LINES_TARGET+1
sbc #0
sta <SCROLL_LINES_TARGET+1
bmi .button_left_ovf
jmp .button_left2
.button_left_ovf:
lda #0
sta <SCROLL_LINES_TARGET
sta <SCROLL_LINES_TARGET+1
.button_left2:
lda <SELECTED_GAME
sec
sbc #10
sta <SELECTED_GAME
lda <SELECTED_GAME+1
sbc #0
sta <SELECTED_GAME+1
bmi .button_left_ovf2
jsr check_separator_down
jmp .button_end
.button_left_ovf2:
lda #0
sta <SELECTED_GAME
sta <SELECTED_GAME+1
jsr check_separator_down
jmp .button_end
.button_right:
lda <BUTTONS
and #%10000000
bne .button_right_check
jmp .button_end
.button_right_check:
; need to bleep if it's not last game
lda <SELECTED_GAME
clc
adc #1
cmp #LOW(GAMES_COUNT)
bne .button_right_bleep
lda <SELECTED_GAME
clc
adc #1
lda <SELECTED_GAME+1
adc #0
cmp #HIGH(GAMES_COUNT)
bne .button_right_bleep
jmp .button_end
.button_right_bleep:
.if ENABLE_SOUND!=0
jsr bleep
.endif
lda <SCROLL_LINES_TARGET
clc
adc #10
sta <SCROLL_LINES_TARGET
lda <SCROLL_LINES_TARGET+1
adc #0
sta <SCROLL_LINES_TARGET+1
; scrolling overflow test
lda <SCROLL_LINES_TARGET
sec
sbc #LOW(MAXIMUM_SCROLL)
lda <SCROLL_LINES_TARGET+1
sbc #HIGH(MAXIMUM_SCROLL)
bcs .button_right_ovf
.button_right_not_ovf:
jmp .button_right2
.button_right_ovf:
lda #LOW(MAXIMUM_SCROLL)
sta <SCROLL_LINES_TARGET
lda #HIGH(MAXIMUM_SCROLL)
sta <SCROLL_LINES_TARGET+1
.button_right2:
lda <SELECTED_GAME
clc
adc #10
sta <SELECTED_GAME
lda <SELECTED_GAME+1
adc #0
sta <SELECTED_GAME+1
; selected game overflow test
lda <SELECTED_GAME
sec
sbc #LOW(GAMES_COUNT)
lda <SELECTED_GAME+1
sbc #HIGH(GAMES_COUNT)
bcs .button_right_ovf2
.button_right_not_ovf2:
jsr check_separator_up
jmp .button_end
.button_right_ovf2:
lda #LOW(GAMES_COUNT)
sec
sbc #1
sta <SELECTED_GAME
lda #HIGH(GAMES_COUNT)
sbc #0
sta <SELECTED_GAME+1
jsr check_separator_up
jmp .button_end
.button_none:
; this code shouldn't never ever be executed
rts
.button_end:
jsr set_scroll_targets ; updating cursor targets
; saving last cursor position
.if (INSTANT_STATE_SAVE!=0) & (ENABLE_LAST_GAME_SAVING!=0)
jsr save_state
.endif
; waiting until buttons released
jsr wait_buttons_not_pressed
rts
; need to skip separator when scrolling upwards
check_separator_down:
lda <SELECTED_GAME+1
jsr select_prg_bank
ldx <SELECTED_GAME
lda loader_data_game_flags, x
and #$80
beq check_separator_down_end
lda <SELECTED_GAME
clc
adc #1
sta <SELECTED_GAME
lda <SELECTED_GAME+1
adc #0
sta <SELECTED_GAME+1
cmp #HIGH(GAMES_COUNT)
bne check_separator_down
lda <SELECTED_GAME
cmp #LOW(GAMES_COUNT)
bne check_separator_down
.if GAMES_COUNT < WRAP_GAMES
lda #0
sta <SELECTED_GAME
sta <SELECTED_GAME+1
sta <SCROLL_LINES_TARGET
sta <SCROLL_LINES_TARGET+1
.else
jsr screen_wrap_down
.endif
jmp check_separator_down
check_separator_down_end:
rts
; need to skip separator when scrolling downwards
check_separator_up:
lda <SELECTED_GAME+1
jsr select_prg_bank
ldx <SELECTED_GAME
lda loader_data_game_flags, x
and #$80
beq check_separator_up_end
lda <SELECTED_GAME
sec
sbc #1
sta <SELECTED_GAME
lda <SELECTED_GAME+1
sbc #0
sta <SELECTED_GAME+1
bpl check_separator_up
.if GAMES_COUNT < WRAP_GAMES
lda #LOW(GAMES_COUNT - 1)
sta <SELECTED_GAME
lda #HIGH(GAMES_COUNT - 1)
sta <SELECTED_GAME+1
.else
jsr screen_wrap_up
.endif
jmp check_separator_up
check_separator_up_end:
rts
; waiting for button release
wait_buttons_not_pressed:
jsr waitblank ; waiting for v-blank
lda <BUTTONS
bne wait_buttons_not_pressed
rts
; waiting for button release, really
wait_buttons_really_not_pressed:
jsr waitblank ; waiting for v-blank
lda <BUTTONS
bne wait_buttons_really_not_pressed
lda BUTTONS_HOLD_TIME
bne wait_buttons_really_not_pressed
rts
konami_code_check:
ldy <KONAMI_CODE_STATE
lda konami_code, y
cmp <BUTTONS
bne konami_code_check_fail
iny
jmp konami_code_check_end
konami_code_check_fail:
ldy #0
lda konami_code ; in case when newpressed button is first button of code
cmp <BUTTONS
bne konami_code_check_end
iny
konami_code_check_end:
sty <KONAMI_CODE_STATE
rts
konami_code:
.db $10, $10, $20, $20, $40, $80, $40, $80, $02, $01
konami_code_length:
.db 10