aboutsummaryrefslogtreecommitdiff
path: root/tests/src/shared
diff options
context:
space:
mode:
Diffstat (limited to 'tests/src/shared')
-rw-r--r--tests/src/shared/diskun.s96
-rw-r--r--tests/src/shared/joypad.s86
2 files changed, 182 insertions, 0 deletions
diff --git a/tests/src/shared/diskun.s b/tests/src/shared/diskun.s
new file mode 100644
index 0000000..b644a83
--- /dev/null
+++ b/tests/src/shared/diskun.s
@@ -0,0 +1,96 @@
+;;; Simple sprite that can be moved with the dpad easily. Use it to have
+;;; something funny moving.
+;; Taken from https://github.com/mssola/code.nes.
+
+.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/tests/src/shared/joypad.s b/tests/src/shared/joypad.s
new file mode 100644
index 0000000..afd0a68
--- /dev/null
+++ b/tests/src/shared/joypad.s
@@ -0,0 +1,86 @@
+;; Taken from https://github.com/mssola/code.nes.
+
+.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 `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 a
+ sta Joypad::JOYPAD1
+
+ ;; 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 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
+
+ ;; NOTE: fallthrough
+
+;;;
+;; 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
+
+ ;; 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
+ pha
+ jsr joypad_unsafe_read_x
+ pla
+ cmp Joypad::m_buttons1, x
+ bne @joypad_read_x_reread
+
+ rts