1;
2; Super NES controller reading code for NES (not DPCM safe)
3; by Damian Yerrick
4;
5
6.p02
7.exportzp cur_keys, cur_axlr, new_keys, new_axlr
8.export read_spads
9
10.segment "ZEROPAGE"
11cur_keys: .res 2
12cur_axlr: .res 2
13new_keys: .res 2
14new_axlr: .res 2
15
16.segment "CODE"
17.proc read_spads
18
19  ; Save the previous state of which buttons were NOT pressed
20  ldx #3
21savelastloop:
22  lda cur_keys,x
23  eor #$FF
24  sta new_keys,x
25  dex
26  bpl savelastloop
27
28  ; Actually read
29  lda #1
30  sta $4016
31  sta cur_axlr+1  ; This 1 bit will get rotated out of player 2's
32  lsr a           ; cur_axlr through cur_keys to carry.  When it
33  sta cur_keys+1  ; arrives, we're done reading the controllers.
34  sta $4016
35readloop:
36  lda $4016
37  and #$03
38  cmp #$01
39  lsr a
40  rol cur_axlr+0
41  rol cur_keys+0
42  lda $4017
43  and #$03
44  cmp #$01
45  lsr a
46  rol cur_axlr+1
47  rol cur_keys+1
48  bcc readloop
49
50  ; Calculate new presses
51  ldx #3
52calcnewloop:
53  lda cur_keys,x
54  and new_keys,x
55  sta new_keys,x
56  dex
57  bpl calcnewloop
58
59  rts
60.endproc
61