-
Notifications
You must be signed in to change notification settings - Fork 2
/
gamepads.asm
386 lines (316 loc) · 6.86 KB
/
gamepads.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
386
.include "header.inc"
.include "snesregs.inc"
.include "misc_macros.inc"
.include "zeropage.inc"
.include "gamepads.inc"
.16BIT
.ramsection "gamepad vars" bank 0 slot RAM_SLOT
; Buffer for reading bytes from controllers. Logic can
; look at those to know the current button status. gamepads.inc
; contains defines that can help masking buttons.
gamepad1_bytes: dsb 4
gamepad2_bytes: dsb 4
; "Button pressed" event bits. When a button goes down, the
; corresponding bit gets set here. Must be cleared manually.
gamepad1_pressed: dsb 4
gamepad2_pressed: dsb 4
; Buffers holding previously read values for edge detection and
; setting the gamepadX_pressed bits.
gamepad1_prev_bytes: dsb 4
gamepad2_prev_bytes: dsb 4
; Those contain the ID bits (cycles 12-16) for each controller;
ctl_id_p1: db
ctl_id_p2: db
.ends
.bank 0
.section "Gamepads" FREE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Send a latch pulse to the controllers.
;
; Assumes 8-bit accumulator.
sendLatch:
pha
stz JOYWR ; Bit 0 is the latch. Make sure it is initially clear;
lda #1.B ; Prepare to set bit 0
sta JOYWR ; Do it
; TODO : How much delay cycles should be used here?
nop
nop
nop
stz JOYWR ; Clear latch
pla
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Just return after 8 nops
gamepads_delay_8nops:
nop
nop
nop
nop
nop
nop
nop
nop
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Read 1 byte from gamepad 1
;
; Assumptions: AXY 8bit
;
; Arguments:
; X: destination index in gamepad1_bytes
;
readGamepad1byte:
pha
phy
ldy #8.B
@lp:
lda JOYA ; Read data, generate clock pulse
lsr ; Move bit 0 from controller to the carry flag
rol gamepad1_bytes, X ; Rotate the bit into memory
dey
bne @lp
ply
pla
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Read 1 byte from gamepad 2
;
; Assumptions: AXY 8bit
;
; Arguments:
; X: destination index in gamepad2_bytes
;
readGamepad2byte:
pha
phy
ldy #8.B
@lp:
lda JOYB ; Read data, generate clock pulse
lsr ; Move bit 0 from controller to the carry flag
rol gamepad2_bytes, X ; Rotate the bit into memory
dey
bne @lp
ply
pla
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Read 32 bits from gamepad in port 1
;
; Bytes stored in gamepad1_bytes[4]
readGamepad1:
pushall
A8
XY8
; First 16 bits
ldx #0.B
jsr readGamepad1byte
inx
jsr readGamepad1byte
inx
; Check the ID bits, decide if more bits shall be read
jsr extractIdBits1
lda ctl_id_p1
cmp #CTL_ID_NTT_KEYPAD
beq @read_more
; Otherwise, zero extra bytes in array
stz gamepad1_bytes, X
inx
stz gamepad1_bytes, X
bra @done
@read_more:
jsr readGamepad1byte
inx
jsr readGamepad1byte
@done:
popall
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Read 32 bits from gamepad in port 2
;
; Assumes: AXY 8 bit
;
; Bytes stored in gamepad2_bytes[4]
readGamepad2:
pushall
ldx #0.B
jsr readGamepad2byte
inx
jsr readGamepad2byte
inx
jsr extractIdBits2
lda ctl_id_p2
cmp #CTL_ID_NTT_KEYPAD
beq @read_more
; Otherwise, zero extra bytes in array
stz gamepad2_bytes, X
inx
stz gamepad2_bytes, X
bra @done
@read_more:
jsr readGamepad2byte
inx
jsr readGamepad2byte
@done:
popall
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Compare the current state of Gamepad 1 with the previous state and
; update gamepad1_pressed[4] to indicate which buttons were pressed
; since last call.
;
gamepad1_detectEvents:
pushall
A16
XY16
lda gamepad1_bytes ; Get latest button status
tax ; Store a copy in X
eor gamepad1_prev_bytes ; XOR with previous status (clears unchanged bits)
and gamepad1_bytes ; Keep only newly set bits (buttons that are down *now*)
ora gamepad1_pressed ; Flag those as pressed (will need to be cleared by 'consumer')
sta gamepad1_pressed ; Save new 'active' buttons
stx gamepad1_prev_bytes ; Save previous state for next pass
lda gamepad1_bytes+2
tax
eor gamepad1_prev_bytes+2
and gamepad1_bytes+2
ora gamepad1_pressed+2
sta gamepad1_pressed+2
stx gamepad1_prev_bytes+2
popall
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Compare the current state of Gamepad 2 with the previous state and
; update gamepad2_pressed[4] to indicate which buttons were pressed
; since last call.
;
gamepad2_detectEvents:
pushall
A16
XY16
lda gamepad2_bytes
tax
eor gamepad2_prev_bytes
and gamepad2_bytes
ora gamepad2_pressed
sta gamepad2_pressed
stx gamepad2_prev_bytes
lda gamepad2_bytes+2
tax
eor gamepad2_prev_bytes+2
and gamepad2_bytes+2
ora gamepad2_pressed+2
sta gamepad2_pressed+2
stx gamepad2_prev_bytes+2
popall
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Extract the ID bytes from gamepad1_bytes and store them
; in ctl_id_p1.
;
extractIdBits1:
pha
php
A8
lda gamepad1_bytes+1
and #$F
sta ctl_id_p1
plp
pla
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Extract the ID bytes from gamepad2_bytes and store them
; in ctl_id_p2.
;
extractIdBits2:
pha
php
A8
lda gamepad2_bytes+1
and #$F
sta ctl_id_p2
plp
pla
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Read 32 bits from both gamepads
;
; Assumes: AXY 8 bit
;
; Bytes stoed in gamepad1_bytes[4] and gamepad2_bytes[4]
readGamepads:
; First send a latch to both controllers
jsr sendLatch
jsr gamepads_delay_8nops
; The shift in 32 bits from each controller
jsr readGamepad1
jsr readGamepad2
; Compute button events (falling edge)
jsr gamepad1_detectEvents
jsr gamepad2_detectEvents
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Initialize gamepad variables.
;
gamepads_init:
pushall
A16
stz gamepad1_prev_bytes
stz gamepad1_prev_bytes + 2
stz gamepad2_prev_bytes
stz gamepad2_prev_bytes + 2
stz gamepad1_pressed
stz gamepad1_pressed + 2
stz gamepad2_pressed
stz gamepad2_pressed + 2
popall
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Get last event from port 1
;
; X: Index into gamepad1_pressed
;
; Returns data in A
;
gamepads_p1_getEvents:
lda gamepad1_pressed,X
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Clear events for controller port 1
;
gamepads_p1_clearEvents:
pha
php
A16
stz gamepad1_pressed
stz gamepad1_pressed + 2
plp
pla
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Get last event from port 2
;
; X: Index into gamepad2_pressed
;
; Returns data in A
;
gamepads_p2_getEvents:
lda gamepad2_pressed,X
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Clear events for controller port 2
;
gamepads_p2_clearEvents:
pha
php
A16
stz gamepad2_pressed
stz gamepad2_pressed + 2
plp
pla
rts
.ends