diff options
| author | Miquel Sabaté Solà <mikisabate@gmail.com> | 2025-03-13 22:52:49 +0100 |
|---|---|---|
| committer | Miquel Sabaté Solà <mikisabate@gmail.com> | 2025-03-13 22:52:49 +0100 |
| commit | 9ff2033e936689135210989a5fee057a4a13527e (patch) | |
| tree | dd41ffd78f451d28a554f105ff40017ed31106a2 /include | |
| parent | 2627b459d9a19ce7f1b7f3a359dca3b30b66b34e (diff) | |
| download | jetpac.nes-9ff2033e936689135210989a5fee057a4a13527e.tar.gz jetpac.nes-9ff2033e936689135210989a5fee057a4a13527e.zip | |
Add a title and a main screens
This commit adds the skeleton for having a title and a main screen. For
now the title menu doesn't do much, as the selection is simply ignored,
but at least it already knows how to cycle between these two states.
Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
Diffstat (limited to 'include')
| -rw-r--r-- | include/asm.s | 22 | ||||
| -rw-r--r-- | include/globals.s | 4 | ||||
| -rw-r--r-- | include/joypad.s | 72 | ||||
| -rw-r--r-- | include/ppu.s | 4 |
4 files changed, 101 insertions, 1 deletions
diff --git a/include/asm.s b/include/asm.s new file mode 100644 index 0000000..67b195c --- /dev/null +++ b/include/asm.s @@ -0,0 +1,22 @@ +;; Jump And Link: jump to subroutine but use the return address that the caller +;; had whenever the given subroutine runs `rts`. In other words, "link" the +;; return address from the caller to the callee. +;; +;; This is in practice the same as using `jmp` but it bears the semantic +;; connotation of the `jsr` one. That is, instead of this: +;; +;; jsr subroutine +;; rts +;; +;; It's more adviseable to do the following for better stack management: +;; +;; jmp subroutine +;; +;; That being said, the `jmp` instruction is also used in many other contexts, +;; and so sometimes it's needed to clarify that: "no, I have not messed up, +;; using `jmp` here instead of `jsr` is deliberate". Hence, instead of adding a +;; comment every time this small optimization is being done, use this +;; pseudo-instruction. +.macro JAL ADDR + jmp ADDR +.endmacro diff --git a/include/globals.s b/include/globals.s index d22b217..f4e4861 100644 --- a/include/globals.s +++ b/include/globals.s @@ -29,6 +29,8 @@ ;; | Bit | Short name | Meaning when set | ;; |-----+------------+-------------------------------------------------------------| ;; | 7 | render | Game logic is over, block main code until NMI code is over. | - ;; | 6-0 | - | Unused | + ;; | 6 | ppu | PPU registers have to be touched | + ;; | 5-2 | - | Unused | + ;; | 1-0 | game | 0: title; 1: game; 2: game over, 3: game over (coin) | zp_flags = $20 .endscope diff --git a/include/joypad.s b/include/joypad.s new file mode 100644 index 0000000..141877f --- /dev/null +++ b/include/joypad.s @@ -0,0 +1,72 @@ +.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 + + ;; After running a `read_*` function these two variables will contain the + ;; given result. + zp_buttons1 = $22 + zp_buttons2 = $23 + + ;;; + ;; Safely read a controller 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 + + ;; 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 + + rts + .endproc + + ;;; + ;; 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 + + ;; 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 + +;; Shortcut for reading the joypad from the first player safely. +.macro READ_JOYPAD1 + ldx #$00 + jsr Joypad::read_x +.endmacro diff --git a/include/ppu.s b/include/ppu.s index fa0df19..dd24422 100644 --- a/include/ppu.s +++ b/include/ppu.s @@ -5,4 +5,8 @@ SCROLL = $2005 ADDRESS = $2006 DATA = $2007 + + ;; Shadow for the PPU::CONTROL value. Touch this value instead of accessing + ;; the PPU register directly. + zp_control = $80 .endscope |
