Skip to content

Instantly share code, notes, and snippets.

@8bitmatt
Created March 15, 2019 05:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 8bitmatt/fa4c8bd33456fbe4d03796f74036a2f5 to your computer and use it in GitHub Desktop.
Save 8bitmatt/fa4c8bd33456fbe4d03796f74036a2f5 to your computer and use it in GitHub Desktop.
NESMaker gamepad / controller documentation and examples
; NESMaker Gamepad / Controller bits
; 7 - 6 - 5 - 4 - 3 - 2 - 1 - 0
; | | | | | | | + -- A
; | | | | | | +------- B
; | | | | | + ---------- Select
; | | | | +--------------- Start
; | | | +------------------- Up
; | | +----------------------- Down
; | + -------------------------- Left
; +------------------------------- Right
; Verified/tested
; - nesmaker_4_1_5, Basic_NoScroll
; - This should also work in all previous versions and cores
; How to use
; ======================
; Writing code in NESMaker to use the gamepad directly should not be used inside an "Input" script.
; Input scripts will already be mapped to the gamepad and execute on certain button presses, releases etc.
; Checking the gamepad directly (like the following examples) is best used in a custom Tile script or
; the Extra Controller Read script (ExtraInputControl:)
; NOTE: You can check for multiple buttons pressed at the same time, look near the end of this document!
; Example A press
; ==========================================
LDA gamepad
AND #%00000001 ; checking for A button
BNE +
; A was not pressed, do stuff here then JMP to end
JMP doneAButton
+
; A was pressed, do some stuff here...
doneAButton:
; NOTE: if this is inside the ExtraInputControl you need to RTS at the very end of the file, Tile scripts don't need RTS
; ==========================================
; Example A press - alternate
; This is smaller code, easier to understand. BUT if you do a lot of stuff
; when A is pressed the BEQ might not be able to branch, "doneAButton" would be out of range.
; The remaining examples will not use this alternate version to help avoid those problems.
; ==========================================
LDA gamepad
AND #%00000001 ; checking for A button
BEQ doneAButton ; A was not pressed, branch to end
; A was pressed, do some stuff here...
doneAButton:
; if inside the ExtraInputControl you need to RTS at the very end, tile scripts dont need RTS
; ==========================================
; Example B press
; ==========================================
LDA gamepad
AND #%00000010 ; checking for B button
BNE +
; B was not pressed, do stuff here then JMP to end
JMP doneBButton
+
; B was pressed, do some stuff here...
doneBButton:
; ==========================================
; Example Select press
; ==========================================
LDA gamepad
AND #%00000100 ; checking for Select button
BNE +
; Select was not pressed, do stuff here then JMP to end
JMP doneSelectButton
+
; Select was pressed, do some stuff here...
doneSelectButton:
; ==========================================
; Example Start press
; ==========================================
LDA gamepad
AND #%00001000 ; checking for Start button
BNE +
; Start was not pressed, do stuff here then JMP to end
JMP doneStartButton
+
; Start was pressed, do some stuff here...
doneStartButton:
; ==========================================
; Example Up (d-pad) press
; ==========================================
LDA gamepad
AND #%00010000 ; checking for Up button
BNE +
; Up was not pressed, do stuff here then JMP to end
JMP doneUpButton
+
; Up was pressed, do some stuff here...
doneUpButton:
; ==========================================
; Example Down (d-pad) press
; ==========================================
LDA gamepad
AND #%00100000 ; checking for Down button
BNE +
; Down was not pressed, do stuff here then JMP to end
JMP doneDownButton
+
; Down was pressed, do some stuff here...
doneDownButton:
; ==========================================
; Example Left (d-pad) press
; ==========================================
LDA gamepad
AND #%01000000 ; checking for Left button
BNE +
; Left was not pressed, do stuff here then JMP to end
JMP doneLeftButton
+
; Left was pressed, do some stuff here...
doneLeftButton:
; ==========================================
; Example Right (d-pad) press
; ==========================================
LDA gamepad
AND #%10000000 ; checking for Right button
BNE +
; Right was not pressed, do stuff here then JMP to end
JMP doneRightButton
+
; Right was pressed, do some stuff here...
doneRightButton:
; ==========================================
; ==================================================================================
; More examples - multiple buttons
; ==================================================================================
; Example Any direction (d-pad)
; ==========================================
LDA gamepad
AND #%11110000 ; checking all d-pad directions
BNE +
; Nothing on the d-pad was pressed, do stuff here then JMP to end
JMP doneAnyDpad
+
; At least one of the d-pad buttons was pressed, do some stuff here...
doneAnyDpad:
; ==========================================
; Example Any button
; ==========================================
LDA gamepad
AND #%11111111 ; checking all buttons
BNE +
; Nothing was pressed, do stuff here then JMP to end
JMP doneAnyButton
+
; At least one button was pressed, do some stuff here...
doneAnyButton:
; ==========================================
; Example A and B both pressed
; ==========================================
LDA gamepad
AND #%00000011 ; checking A and B buttons
CMP #%00000011 ; compare to see if both are pressed
BEQ +
; A and B were not pressed, do stuff here then JMP to end
JMP doneABButtons
+
; Both A and B buttons were pressed, do some stuff here...
doneABButtons:
; ==========================================
; Example Only A Button and nothing else
; ==========================================
LDA gamepad
CMP #%00000001 ; compare to see if only A was pressed
BEQ +
; A was not pressed, or something else was pressed too. do stuff here then JMP to end
JMP doneOnlyAButton
+
; Only A was pressed, do some stuff here...
doneOnlyAButton:
; ==========================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment