aboutsummaryrefslogtreecommitdiff
path: root/shared
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <msabate@suse.com>2024-04-02 22:02:59 +0200
committerMiquel Sabaté Solà <mikisabate@gmail.com>2025-02-04 21:04:01 +0100
commit1d1f08646352df00977a88adb99e04d54e4dfd5d (patch)
tree4efb0259708c0b67ec4ed3876cb487e72073b584 /shared
parentc6bc9014c10858b8cfd482289c9691b11ef6ad39 (diff)
downloadcode.nes-1d1f08646352df00977a88adb99e04d54e4dfd5d.tar.gz
code.nes-1d1f08646352df00977a88adb99e04d54e4dfd5d.zip
basics: Add a flickering example
Add an example on how to flicker sprites so to not surpass the scanline limit for them. Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
Diffstat (limited to 'shared')
-rw-r--r--shared/diskun.s95
-rw-r--r--shared/joypad.s56
2 files changed, 151 insertions, 0 deletions
diff --git a/shared/diskun.s b/shared/diskun.s
new file mode 100644
index 0000000..d8bab90
--- /dev/null
+++ b/shared/diskun.s
@@ -0,0 +1,95 @@
+;;; Simple sprite that can be moved with the dpad easily. Use it to have
+;;; something funny moving.
+
+.include "joypad.s"
+
+.scope Diskun
+ m_screen_x = $30
+ m_screen_y = $31
+
+ .proc init_palettes
+ lda #$3F
+ sta $2006 ; PPUADDR
+ lda #$00
+ sta $2006 ; PPUADDR
+
+ ldx #0
+ @load_palettes_loop:
+ lda palettes, x
+ sta $2007 ; PPUDATA
+ inx
+ cpx #$20
+ bne @load_palettes_loop
+ rts
+ palettes:
+ DEFAULT_COLOR = $11
+
+ ;; Background
+ .byte DEFAULT_COLOR, $36, $17, $0F
+ .byte DEFAULT_COLOR, $00, $00, $00
+ .byte DEFAULT_COLOR, $00, $00, $00
+ .byte DEFAULT_COLOR, $00, $00, $00
+
+ ;; Foreground
+ .byte DEFAULT_COLOR, $28, $0F, $30
+ .byte DEFAULT_COLOR, $00, $00, $00
+ .byte DEFAULT_COLOR, $00, $00, $00
+ .byte DEFAULT_COLOR, $00, $00, $00
+
+ rts
+ .endproc
+
+ .proc update
+ lda #Joypad::BUTTON_UP
+ and Joypad::m_buttons1
+ beq @check_down
+
+ dec Diskun::m_screen_y
+ dec Diskun::m_screen_y
+ jmp @check_left
+ @check_down:
+ lda #Joypad::BUTTON_DOWN
+ and Joypad::m_buttons1
+ beq @check_left
+
+ inc Diskun::m_screen_y
+ inc Diskun::m_screen_y
+ @check_left:
+ lda #Joypad::BUTTON_LEFT
+ and Joypad::m_buttons1
+ beq @check_right
+
+ dec Diskun::m_screen_x
+ dec Diskun::m_screen_x
+ @check_right:
+ lda #Joypad::BUTTON_RIGHT
+ and Joypad::m_buttons1
+ beq @end
+
+ inc Diskun::m_screen_x
+ inc Diskun::m_screen_x
+ @end:
+ rts
+ .endproc
+
+ .proc nmi_update
+ lda Diskun::m_screen_x
+ sta $203
+ sta $20B
+ clc
+ adc #8
+ sta $207
+ sta $20F
+
+ lda Diskun::m_screen_y
+ sta $200
+ sta $204
+ clc
+ adc #8
+ sta $208
+ sta $20C
+
+ rts
+ .endproc
+.endscope
+
diff --git a/shared/joypad.s b/shared/joypad.s
new file mode 100644
index 0000000..2abf959
--- /dev/null
+++ b/shared/joypad.s
@@ -0,0 +1,56 @@
+;;; TODO: safe read
+
+.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