aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <mikisabate@gmail.com>2025-03-11 17:00:47 +0100
committerMiquel Sabaté Solà <mikisabate@gmail.com>2025-03-11 17:00:47 +0100
commit428c301a80641361f51af8102628b545cf9ae63e (patch)
treee8888ec3a369bb617e892ff783334ae4f46ce355
parenteb9544cb3df7158f7fba5ed346997989102d0155 (diff)
downloadcode.nes-428c301a80641361f51af8102628b545cf9ae63e.tar.gz
code.nes-428c301a80641361f51af8102628b545cf9ae63e.zip
shared: Bring joypad into a proper style
Put functions inside of the scope, avoid useless fallthroughs and be more realistic on what is needed from elsewhere. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
-rw-r--r--basics/flicker.s2
-rw-r--r--fx/blink.s2
-rw-r--r--scroll/include/driver.s2
-rw-r--r--scroll/include/player.s8
-rw-r--r--scroll/level.s2
-rw-r--r--scroll/sprite0.s2
-rw-r--r--scroll/toggle.s8
-rw-r--r--shared/diskun.s8
-rw-r--r--shared/joypad.s110
9 files changed, 66 insertions, 78 deletions
diff --git a/basics/flicker.s b/basics/flicker.s
index 2522cb2..5677968 100644
--- a/basics/flicker.s
+++ b/basics/flicker.s
@@ -38,7 +38,7 @@
@main_game_loop:
;; NOTE: the logic is pretty simple: read the pad, move the player
;; accordingly, and apply the flickering effect.
- jsr joypad_read
+ READ_JOYPAD1
jsr Diskun::update
;; NOTE: comment this `jsr` out if you want to see what happens if no
diff --git a/fx/blink.s b/fx/blink.s
index c9a38b1..ce63b2c 100644
--- a/fx/blink.s
+++ b/fx/blink.s
@@ -277,7 +277,7 @@
sta $2001
@main_game_loop:
- jsr joypad_read
+ READ_JOYPAD1
jsr Diskun::update
lda #%10000000
diff --git a/scroll/include/driver.s b/scroll/include/driver.s
index abb3f5e..acf5a11 100644
--- a/scroll/include/driver.s
+++ b/scroll/include/driver.s
@@ -50,7 +50,7 @@
;; If `select` is pressed, then go for a new level. Otherwise check
;; whether there is a pending column to be loaded.
lda #Joypad::BUTTON_SELECT
- and Joypad::m_buttons1
+ and Joypad::zp_buttons1
beq @check_column
;; Set the counter.
diff --git a/scroll/include/player.s b/scroll/include/player.s
index 239cec6..a4f750c 100644
--- a/scroll/include/player.s
+++ b/scroll/include/player.s
@@ -76,7 +76,7 @@
.proc update
;; Is the player requesting to go up?
lda #Joypad::BUTTON_UP
- and Joypad::m_buttons1
+ and Joypad::zp_buttons1
beq @check_down
;; If we are already at the top disregard this button press and check
@@ -108,7 +108,7 @@
@check_down:
;; Is the player requesting to go down?
lda #Joypad::BUTTON_DOWN
- and Joypad::m_buttons1
+ and Joypad::zp_buttons1
beq @check_left
;; We have to move down unless we are already at the very bottom.
@@ -135,7 +135,7 @@
@check_left:
;; Is the player requesting to go left?
lda #Joypad::BUTTON_LEFT
- and Joypad::m_buttons1
+ and Joypad::zp_buttons1
beq @check_right
;; We have to move left unless we are already at the leftmost edge.
@@ -160,7 +160,7 @@
@check_right:
;; Last check! Is the player requesting to go right?
lda #Joypad::BUTTON_RIGHT
- and Joypad::m_buttons1
+ and Joypad::zp_buttons1
bne @check_level_end
rts
diff --git a/scroll/level.s b/scroll/level.s
index de386db..28de7f0 100644
--- a/scroll/level.s
+++ b/scroll/level.s
@@ -57,7 +57,7 @@
sta PPU::CONTROL
@main_game_loop:
- jsr joypad_read
+ READ_JOYPAD1
jsr Player::update
jsr Driver::update
diff --git a/scroll/sprite0.s b/scroll/sprite0.s
index 4bd940d..a1944e6 100644
--- a/scroll/sprite0.s
+++ b/scroll/sprite0.s
@@ -142,7 +142,7 @@ BACKGROUND_ROW_OFFSET = 1
sta PPU::CONTROL
@main_game_loop:
- jsr joypad_read
+ READ_JOYPAD1
jsr Player::update
jsr Driver::update
diff --git a/scroll/toggle.s b/scroll/toggle.s
index b9214b4..32fc39f 100644
--- a/scroll/toggle.s
+++ b/scroll/toggle.s
@@ -70,11 +70,11 @@
bne @end
;; Read the joypad.
- jsr joypad_read
+ READ_JOYPAD1
;; Is the player pressing left? If so then the direction is to the left.
lda #Joypad::BUTTON_LEFT
- and Joypad::m_buttons1
+ and Joypad::zp_buttons1
beq @check_right
inc Vars::zp_scrolling
lda #0
@@ -84,7 +84,7 @@
@check_right:
;; Is the player pressing right? If so then the direction is to the right.
lda #Joypad::BUTTON_RIGHT
- and Joypad::m_buttons1
+ and Joypad::zp_buttons1
beq @check_select
inc Vars::zp_scrolling
lda #1
@@ -95,7 +95,7 @@
;; Is the player pressing Select? Then the direction depends on the current
;; nametable.
lda #Joypad::BUTTON_SELECT
- and Joypad::m_buttons1
+ and Joypad::zp_buttons1
beq @end
inc Vars::zp_scrolling
ldx #0
diff --git a/shared/diskun.s b/shared/diskun.s
index 8349748..ffe8028 100644
--- a/shared/diskun.s
+++ b/shared/diskun.s
@@ -41,7 +41,7 @@
.proc update
lda #Joypad::BUTTON_UP
- and Joypad::m_buttons1
+ and Joypad::zp_buttons1
beq @check_down
dec Diskun::m_screen_y
@@ -49,21 +49,21 @@
jmp @check_left
@check_down:
lda #Joypad::BUTTON_DOWN
- and Joypad::m_buttons1
+ and Joypad::zp_buttons1
beq @check_left
inc Diskun::m_screen_y
inc Diskun::m_screen_y
@check_left:
lda #Joypad::BUTTON_LEFT
- and Joypad::m_buttons1
+ and Joypad::zp_buttons1
beq @check_right
dec Diskun::m_screen_x
dec Diskun::m_screen_x
@check_right:
lda #Joypad::BUTTON_RIGHT
- and Joypad::m_buttons1
+ and Joypad::zp_buttons1
beq @end
inc Diskun::m_screen_x
diff --git a/shared/joypad.s b/shared/joypad.s
index 7bfc575..05797fe 100644
--- a/shared/joypad.s
+++ b/shared/joypad.s
@@ -15,70 +15,58 @@
;; After running a `joypad_read_*` function these two variables will contain
;; the given result.
- m_buttons1 = $22
- m_buttons2 = $23
-.endscope
-
-;;;
-;; Read the first joypad. This method is fast but it might be vulnerable to the
-;; DPCM bug (see: https://www.nesdev.org/wiki/Controller_reading_code).
-joypad_unsafe_read:
- ldx #$00
- ;; NOTE: fallthrough
-
-;;;
-;; Read the joypad as indexed by the X register (0 for controller 1; 1 for
-;; controller 2). This method is fast but it might be vulnerable to the DPCM bug
-;; (see: https://www.nesdev.org/wiki/Controller_reading_code).
-joypad_unsafe_read_x:
- ;; Start the latch process.
- lda #$01
- sta Joypad::JOYPAD1
- sta Joypad::m_buttons1, x ; Bit as a guard for the loop below.
- lsr
- sta Joypad::JOYPAD1
+ zp_buttons1 = $22
+ zp_buttons2 = $23
- ;; Now the joypad is ready to accept reads.
-@joypad_unsafe_read_x_loop:
- lda Joypad::JOYPAD1, x
- and #%00000011 ; Ignore bits other than controller.
- cmp #$01 ; Set carry if and only if nonzero.
- rol Joypad::m_buttons1, x ; Carry -> bit 0; bit 7 -> Carry
- bcc @joypad_unsafe_read_x_loop
- rts
+ ;;;
+ ;; Safely read via a re-read algorithm the joypad as indexed by the X register
+ ;; (0 for controller 1; 1 for controller 2).
+ .proc read_x
+ jsr Joypad::unsafe_read_x
-;;;
-;; Safely read the first controller via a re-read algorithm.
-joypad_read:
- ldx #$00
-
- ;; NOTE: uncomment these two lines to also read safely the second
- ;; controller.
- ;;
- ;; jsr joypad_read_x
- ;; inx
+ ;; The main idea around a re-read algorithm is that you read the
+ ;; controller "unsafely" once, then you do it again and compare both
+ ;; reads. If they were the same then we are on the safe side. Otherwise
+ ;; we would need to loop until we get two identical reads. This sounds
+ ;; bad but in practice it's not so much (and hey, if it worked for Super
+ ;; Mario Bros. 3, it should work for us too :P). Otherwise there is the
+ ;; algorithm via OAM DMA, but it sure is tricky.
+ @reread:
+ lda Joypad::zp_buttons1, x
+ tay
+ jsr Joypad::unsafe_read_x
+ tya
+ cmp Joypad::zp_buttons1, x
+ bne @reread
- ;; NOTE: fallthrough
+ rts
+ .endproc
-;;;
-;; Safely read via a re-read algorithm the joypad as indexed by the X register
-;; (0 for controller 1; 1 for controller 2).
-joypad_read_x:
- jsr joypad_unsafe_read_x
+ ;;;
+ ;; Read the joypad as indexed by the X register (0 for controller 1; 1 for
+ ;; controller 2). This method is fast but it might be vulnerable to the DPCM
+ ;; bug (see: https://www.nesdev.org/wiki/Controller_reading_code).
+ .proc unsafe_read_x
+ ;; Start the latch process.
+ lda #$01
+ sta Joypad::JOYPAD1
+ sta Joypad::zp_buttons1, x ; Bit as a guard for the loop below.
+ lsr
+ sta Joypad::JOYPAD1
- ;; The main idea around a re-read algorithm is that you read the controller
- ;; "unsafely" once, then you do it again and compare both reads. If they
- ;; were the same then we are on the safe side. Otherwise we would need to
- ;; loop until we get two identical reads. This sounds bad but in practice
- ;; it's not so much (and hey, if it worked for Super Mario Bros. 3, it
- ;; should work for us too :P). Otherwise there is the algorithm via OAM DMA,
- ;; but it sure is tricky.
-@joypad_read_x_reread:
- lda Joypad::m_buttons1, x
- tay
- jsr joypad_unsafe_read_x
- tya
- cmp Joypad::m_buttons1, x
- bne @joypad_read_x_reread
+ ;; Now the joypad is ready to accept reads.
+ @loop:
+ lda Joypad::JOYPAD1, x
+ and #%00000011 ; Ignore bits other than controller.
+ cmp #$01 ; Set carry if and only if nonzero.
+ rol Joypad::zp_buttons1, x ; Carry -> bit 0; bit 7 -> Carry
+ bcc @loop
+ rts
+ .endproc
+.endscope
- rts
+;; Shortcut for reading the joypad from the first player safely.
+.macro READ_JOYPAD1
+ ldx #$00
+ jsr Joypad::read_x
+.endmacro