aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <mssola@mssola.com>2026-03-08 22:09:31 +0100
committerMiquel Sabaté Solà <mssola@mssola.com>2026-03-08 22:14:15 +0100
commitf1d1d0efee9faa3067f7b0fc8b9a2aebb17f1ccd (patch)
treeeb6c2008a025f6ae88f7dc1e3f61360599189e0e
parentfeed0b705daff0001253b6ec89b61c9821d2fe21 (diff)
downloadjetpac.nes-f1d1d0efee9faa3067f7b0fc8b9a2aebb17f1ccd.tar.gz
jetpac.nes-f1d1d0efee9faa3067f7b0fc8b9a2aebb17f1ccd.zip
Support reading from the second controller
Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
-rw-r--r--.nasm/memory.txt7
-rw-r--r--.nasm/segments.txt6
-rw-r--r--include/joypad.s39
-rw-r--r--src/bullets.s2
-rw-r--r--src/driver.s2
-rw-r--r--src/jetpac.s10
-rw-r--r--src/player.s8
-rw-r--r--src/title.s8
8 files changed, 41 insertions, 41 deletions
diff --git a/.nasm/memory.txt b/.nasm/memory.txt
index dcdef5e..129737f 100644
--- a/.nasm/memory.txt
+++ b/.nasm/memory.txt
@@ -17,8 +17,7 @@ $10: zp_displayed
$11: zp_timer
$20: zp_flags
$21: zp_prev
-$22: zp_buttons1
-$23: zp_buttons2
+$22: zp_buttons
$24: zp_level
$25: zp_level_kind
$26: zp_multiplayer
@@ -66,8 +65,8 @@ $2006: m_address
$2007: m_data
$4010: m_dmc
$4014: m_dma
-$4016: m_joypad1
+$4016: m_joypad
$4017: m_frame_counter
--- Summary (in bytes) ---
-- Internal RAM: 391/2048 (19.09%)
+- Internal RAM: 390/2048 (19.04%)
diff --git a/.nasm/segments.txt b/.nasm/segments.txt
index e5b18a5..0b837c2 100644
--- a/.nasm/segments.txt
+++ b/.nasm/segments.txt
@@ -1,8 +1,4 @@
- HEADER: 16/16 (100%)
-<<<<<<< HEAD
-- ROM0: 6609/32762 (20.17%)
-=======
-- ROM0: 6658/32762 (20.32%)
->>>>>>> 823e1f6ed102 (State game over only when all players are dead)
+- ROM0: 6671/32762 (20.36%)
- ROMV: 6/6 (100%)
- ROM2: 8192/8192 (100%)
diff --git a/include/joypad.s b/include/joypad.s
index cc15d02..784bb81 100644
--- a/include/joypad.s
+++ b/include/joypad.s
@@ -9,17 +9,18 @@
BUTTON_LEFT = 1 << 1
BUTTON_RIGHT = 1 << 0
- ;; Port addresses for controllers.
- m_joypad1 = $4016
- ;; m_joypad2 = $4017
+ ;; Port address for starting the latch process for both controllers and
+ ;; reading from Joypad 1. The second Joypad can be accessed by reading from
+ ;; $4017, which means that functions down below will simply use an indexed
+ ;; load with the proper value on the index register (i.e. +1).
+ m_joypad = $4016
;; The previous reading from the latest read controller.
- zp_prev = $21
+ zp_prev = $21
- ;; After running a `read_*` function these two variables will contain the
- ;; given result.
- zp_buttons1 = $22
- zp_buttons2 = $23
+ ;; After running a `read_*` function this variable will contain the given
+ ;; result.
+ zp_buttons = $22
;;;
;; Safely read a controller via a re-read algorithm the joypad as indexed by
@@ -35,11 +36,11 @@
;; 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
+ lda Joypad::zp_buttons
tay
jsr Joypad::unsafe_read_x
tya
- cmp Joypad::zp_buttons1, x
+ cmp Joypad::zp_buttons
bne @reread
rts
@@ -52,26 +53,26 @@
.proc unsafe_read_x
;; Start the latch process.
lda #$01
- sta Joypad::m_joypad1
- sta Joypad::zp_buttons1, x ; Bit as a guard for the loop below.
+ sta Joypad::m_joypad
+ sta Joypad::zp_buttons ; Bit as a guard for the loop below.
lsr
- sta Joypad::m_joypad1
+ sta Joypad::m_joypad
;; Now the joypad is ready to accept reads.
@loop:
- lda Joypad::m_joypad1, x
+ lda Joypad::m_joypad, 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
+ rol Joypad::zp_buttons ; Carry -> bit 0; bit 7 -> Carry
bcc @loop
rts
.endproc
.endscope
-;; Shortcut for reading the joypad from the first player safely.
-.macro READ_JOYPAD1
- ldx #$00
- lda Joypad::zp_buttons1
+;; Shortcut for reading the joypad indexed by 'x' (0 for controller 1; 1 for
+;; controller 2).
+.macro READ_JOYPAD_X
+ lda Joypad::zp_buttons
sta Joypad::zp_prev
jsr Joypad::read_x
.endmacro
diff --git a/src/bullets.s b/src/bullets.s
index 58b6a4a..3cc74b9 100644
--- a/src/bullets.s
+++ b/src/bullets.s
@@ -104,7 +104,7 @@
@check_bullets_pressed:
;; Is the B button pressed? If not go to `@move_bullets` directly.
lda #(Joypad::BUTTON_B)
- and Joypad::zp_buttons1
+ and Joypad::zp_buttons
beq @move_bullets
;; The B button was pressed. Reset the bullet timer.
diff --git a/src/driver.s b/src/driver.s
index 3bf4399..942bf82 100644
--- a/src/driver.s
+++ b/src/driver.s
@@ -179,7 +179,7 @@
;; Check if the player is toggling the `pause` state.
lda #(Joypad::BUTTON_START | Joypad::BUTTON_SELECT)
- and Joypad::zp_buttons1
+ and Joypad::zp_buttons
beq @skip_pause_handling
;; What does the timer say, is the player allowed to do it?
diff --git a/src/jetpac.s b/src/jetpac.s
index 25fdf5a..5fdd01f 100644
--- a/src/jetpac.s
+++ b/src/jetpac.s
@@ -125,8 +125,8 @@
;; variables defined before
sta Globals::zp_flags
sta Globals::zp_multiplayer
- sta Joypad::zp_buttons1
- sta Joypad::zp_buttons2
+ sta Joypad::zp_buttons
+ sta Joypad::zp_prev
sta Player::zp_state
;; Initialize the level. We allow the build system to pass its own value for
@@ -187,7 +187,11 @@
sta PPU::m_mask
@main_game_loop:
- READ_JOYPAD1
+ ;; Select the joypad from the active player and read it.
+ lda Globals::zp_multiplayer
+ and #$01
+ tax
+ READ_JOYPAD_X
lda Globals::zp_flags
and #%00000011
diff --git a/src/player.s b/src/player.s
index 53695b4..60005af 100644
--- a/src/player.s
+++ b/src/player.s
@@ -249,7 +249,7 @@
bit zp_state
bpl @check_thrust
lda #Joypad::BUTTON_DOWN
- and Joypad::zp_buttons1
+ and Joypad::zp_buttons
beq @check_thrust
lda #0
sta zp_velocity_y
@@ -258,7 +258,7 @@
@check_thrust:
;; Check if the player is asking to thrust, otherwise apply gravity.
lda #(Joypad::BUTTON_UP | Joypad::BUTTON_A)
- and Joypad::zp_buttons1
+ and Joypad::zp_buttons
beq @set_gravity
;; Player is throttling, reflect that on the player's state.
@@ -349,7 +349,7 @@
.proc update_horizontal_position
lda #Joypad::BUTTON_LEFT
- and Joypad::zp_buttons1
+ and Joypad::zp_buttons
beq @check_right
;; We are facing left, reflect that on the state and the sprite.
@@ -374,7 +374,7 @@
;; Same as the part above but applied to going right.
@check_right:
lda #Joypad::BUTTON_RIGHT
- and Joypad::zp_buttons1
+ and Joypad::zp_buttons
beq @nothing
lda #%01000100
diff --git a/src/title.s b/src/title.s
index 06ca1cb..d091922 100644
--- a/src/title.s
+++ b/src/title.s
@@ -38,7 +38,7 @@
bne @end
lda #Joypad::BUTTON_UP
- and Joypad::zp_buttons1
+ and Joypad::zp_buttons
beq @check_down
lda #SPRITE_Y_POSITION0
@@ -49,7 +49,7 @@
@check_down:
lda #Joypad::BUTTON_DOWN
- and Joypad::zp_buttons1
+ and Joypad::zp_buttons
beq @check_select
lda #SPRITE_Y_POSITION1
@@ -60,13 +60,13 @@
@check_select:
lda #Joypad::BUTTON_SELECT
- and Joypad::zp_buttons1
+ and Joypad::zp_buttons
bne @do_select
;; If none of the above has been pressed, our only possibility is the
;; start button. If that's the case, jump there, otherwise quit.
lda #(Joypad::BUTTON_START | Joypad::BUTTON_A)
- and Joypad::zp_buttons1
+ and Joypad::zp_buttons
beq @end
JAL start