aboutsummaryrefslogtreecommitdiff
path: root/scroll
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <msabate@suse.com>2024-04-02 23:49:52 +0200
committerMiquel Sabaté Solà <mikisabate@gmail.com>2025-02-04 21:04:36 +0100
commit3c6a583190b827c0e88e535d486a4f191ee255c0 (patch)
treefd21f0c22dbd7e340a53dcb8fad1f4e8c07c73bc /scroll
parent1d1f08646352df00977a88adb99e04d54e4dfd5d (diff)
downloadcode.nes-3c6a583190b827c0e88e535d486a4f191ee255c0.tar.gz
code.nes-3c6a583190b827c0e88e535d486a4f191ee255c0.zip
Add a way to safely read from controllers
First of all, move some reading utilities into the new `shared` directory, so controllers can be read by more than one example. Second, even if we keep the example on `basics/input.s` simple, let's provide a safe alternative for the other examples. Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
Diffstat (limited to 'scroll')
-rw-r--r--scroll/include/joypad.s63
1 files changed, 0 insertions, 63 deletions
diff --git a/scroll/include/joypad.s b/scroll/include/joypad.s
deleted file mode 100644
index 8711c28..0000000
--- a/scroll/include/joypad.s
+++ /dev/null
@@ -1,63 +0,0 @@
-.segment "CODE"
-
-;;;
-;; Joypad controller code. The following memory addresses are reserved: $21-$23.
-;;
-;; Memory address $21 is used for internal purposes, whereas $22 and $23 contain
-;; the bitmask of the buttons that are pressed from each controller.
-;;;
-
-.scope Joypad
- ;; Button masks.
- BUTTON_A = 1 << 7
- BUTTON_B = 1 << 6
- BUTTON_SELECT = 1 << 5
- BUTTON_START = 1 << 4
- BUTTON_UP = 1 << 3
- BUTTON_DOWN = 1 << 2
- BUTTON_LEFT = 1 << 1
- BUTTON_RIGHT = 1 << 0
-
- ;; Port addresses for controllers.
- JOYPAD1 = $4016
- JOYPAD2 = $4017
-
- ;; We keep all the information from controller from mainly two variables:
- ;; m_buttons1 and m_buttons2; containing respectively the buttons pressed
- ;; for this frame for both controllers. The m_inv_buttons ($21) is an
- ;; internal variable and should not be used for anything outside of this
- ;; usage.
- m_inv_buttons = $21
- m_buttons1 = $22
- m_buttons2 = $23
-
- ;; READ_CONTROLLER reads the input from the controller mapped into the given
- ;; port, and saves the state into the given `buttons` address.
- ;; Implementation taken from NESHacker's example of smb3-like movement.
- .macro READ_CONTROLLER port, buttons
- lda m_inv_buttons
- tay
- lda #1
- sta port
- sta m_inv_buttons
- lsr
- sta port
- :
- lda port
- lsr
- rol m_inv_buttons
- bcc :-
- tya
- eor m_inv_buttons
- and m_inv_buttons
- sta buttons
- .endmacro
-
- ;; read sets the values for m_buttons1 and m_buttons2 as read from both
- ;; controllers.
- .proc read
- READ_CONTROLLER JOYPAD1, m_buttons1
- READ_CONTROLLER JOYPAD2, m_buttons2
- rts
- .endproc
-.endscope