aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <mikisabate@gmail.com>2025-05-12 21:20:33 +0200
committerMiquel Sabaté Solà <mikisabate@gmail.com>2025-05-12 21:20:33 +0200
commitad830a7f81871e15f43d7255f7af5463d9dd0bc5 (patch)
tree6217d30e13a96f8bc3a7675ab3e45c9e9c3f967a
parentd2cab2f600b4a5efea34fce9e17de0c2ba964c6c (diff)
downloadjetpac.nes-ad830a7f81871e15f43d7255f7af5463d9dd0bc5.tar.gz
jetpac.nes-ad830a7f81871e15f43d7255f7af5463d9dd0bc5.zip
reset: Do not zero out RAM
This is usually done so the programmer can forget about initializing in the future, but it not only hides programming mistakes, but we are also resetting memory addresses which are never to be used by this simple game. Hence, just skip resetting RAM altogether. Last but not least, sprite initialization in `reset` code has also been optimized so instead of around 2000 cycles it takes half of that. This is done by only touching the first byte of the four bytes for a sprite in OAM, which is enough for hiding random sprites and the cost of extra `inx` instructions is far cheaper than all the extra `sta` to absolute address with X-index from the old code. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
-rw-r--r--src/jetpac.s10
-rw-r--r--src/vectors.s38
2 files changed, 23 insertions, 25 deletions
diff --git a/src/jetpac.s b/src/jetpac.s
index 65b5d11..10f6df3 100644
--- a/src/jetpac.s
+++ b/src/jetpac.s
@@ -45,9 +45,17 @@
.include "vectors.s"
.proc main
- ;; Disable the PPU.
+ ;; Disable the PPU and zero out variables which shadow PPU registers.
lda #0
sta PPU::MASK
+ sta PPU::zp_mask
+ sta PPU::zp_control
+
+ ;; Initialize other global variables which the rest of the game assume to
+ ;; have zero as their initial values.
+ sta Globals::zp_flags
+ sta Joypad::zp_buttons1
+ sta Joypad::zp_buttons2
;; Initialize the assets for the game.
jsr Assets::init
diff --git a/src/vectors.s b/src/vectors.s
index 559a59d..8928aaf 100644
--- a/src/vectors.s
+++ b/src/vectors.s
@@ -5,64 +5,54 @@
;; Pretty standard reset function, nothing crazy.
.proc reset
+ ;; Disable interrupts and decimal mode.
sei
cld
+ ;; Disable APU frame counter.
ldx #$40
stx APU::FRAME_COUNTER
+ ;; Setup the stack.
ldx #$FF
txs
+ ;; Disable NMIs and the APU's DMC.
inx
stx PPU::CONTROL
stx PPU::MASK
stx APU::DMC
+ ;; First PPU wait.
bit PPU::STATUS
@vblankwait1:
bit PPU::STATUS
bpl @vblankwait1
- ldx #0
- lda #0
-@ram_reset_loop:
- sta $000, x
- sta $100, x
- sta $300, x
- sta $400, x
- sta $500, x
- sta $600, x
- sta $700, x
- inx
- bne @ram_reset_loop
-
+ ;; Reset all sprites by simply moving the Y coordinate out of screen.
lda #$EF
+ ldx #0
@sprite_reset_loop:
sta $200, x
inx
+ inx
+ inx
+ inx
bne @sprite_reset_loop
+ ;; DMA setup for sprite reset.
lda #$00
sta OAM::ADDRESS
lda #$02
sta OAM::DMA
+ ;; Second PPU wait. After that the PPU is stable.
@vblankwait2:
bit PPU::STATUS
bpl @vblankwait2
- lda #$3F
- sta PPU::ADDRESS
- lda #$00
- sta PPU::ADDRESS
-
- lda #$0F
- ldx #$20
-@palettes_reset_loop:
- sta PPU::DATA
- dex
- bne @palettes_reset_loop
+ ;; NOTE: palettes are not initialized here as it's going to be one of the
+ ;; first things done on `main` code.
jmp main
.endproc