@@ -115,6 +115,44 @@ Memcopy:
115115 ret
116116; ANCHOR_END: memcpy
117117
118+ ; ANCHOR: input-routine
119+ UpdateKeys:
120+ ; Poll half the controller
121+ ld a , JOYP_GET_BUTTONS
122+ call .onenibble
123+ ld b , a ; B7-4 = 1; B3-0 = unpressed buttons
124+
125+ ; Poll the other half
126+ ld a , JOYP_GET_CTRL_PAD
127+ call .onenibble
128+ swap a ; A7-4 = unpressed directions; A3-0 = 1
129+ xor a , b ; A = pressed buttons + directions
130+ ld b , a ; B = pressed buttons + directions
131+
132+ ; And release the controller
133+ ld a , JOYP_GET_NONE
134+ ldh [ rJOYP ], a
135+
136+ ; Combine with previous wCurKeys to make wNewKeys
137+ ld a , [ wCurKeys ]
138+ xor a , b ; A = keys that changed state
139+ and a , b ; A = keys that changed to pressed
140+ ld [ wNewKeys ], a
141+ ld a , b
142+ ld [ wCurKeys ], a
143+ ret
144+
145+ .onenibble
146+ ldh [ rJOYP ], a ; switch the key matrix
147+ call .knownret ; burn 10 cycles calling a known ret
148+ ldh a , [ rJOYP ] ; ignore value while waiting for the key matrix to settle
149+ ldh a , [ rJOYP ]
150+ ldh a , [ rJOYP ] ; this read counts
151+ or a , $ F0 ; A7-4 = 1; A3-0 = unpressed keys
152+ .knownret
153+ ret
154+ ; ANCHOR_END: input-routine
155+
118156Tiles:
119157 dw ` 33333333
120158 dw ` 33333333
@@ -337,3 +375,9 @@ Unbricked_Title_Screen_Map_Begin:
337375 DB $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0
338376 DB $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , $ 00 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0
339377Unbricked_Title_Screen_Map_End:
378+
379+ ; ANCHOR: vars
380+ SECTION "Input Variables" , WRAM0
381+ wCurKeys: db
382+ wNewKeys: db
383+ ; ANCHOR_END: vars
0 commit comments