aboutsummaryrefslogtreecommitdiff
path: root/space/include
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <msabate@suse.com>2023-10-24 17:40:19 +0200
committerMiquel Sabaté Solà <mikisabate@gmail.com>2025-02-04 20:58:36 +0100
commit149020464517ff3d021eef5ccd50d3939ad463e6 (patch)
tree056ec1919fe0c0d515e7a0a414916307b768ef84 /space/include
parentbe2414f5b11b182998fca7e87712ff0502c8ee93 (diff)
downloadcode.nes-149020464517ff3d021eef5ccd50d3939ad463e6.tar.gz
code.nes-149020464517ff3d021eef5ccd50d3939ad463e6.zip
Lay out a proper structure for the project
Let's divide things by topic instead of a broad "examples" directory. This will allow for simple files to co-exist together with full-blown projects. Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
Diffstat (limited to 'space/include')
-rw-r--r--space/include/apu.s6
-rw-r--r--space/include/joypad.s63
-rw-r--r--space/include/oam.s13
-rw-r--r--space/include/ppu.s27
4 files changed, 109 insertions, 0 deletions
diff --git a/space/include/apu.s b/space/include/apu.s
new file mode 100644
index 0000000..32ccff2
--- /dev/null
+++ b/space/include/apu.s
@@ -0,0 +1,6 @@
+.segment "CODE"
+
+.scope APU
+ DMC = $4010
+ FRAME_COUNTER = $4017
+.endscope
diff --git a/space/include/joypad.s b/space/include/joypad.s
new file mode 100644
index 0000000..8711c28
--- /dev/null
+++ b/space/include/joypad.s
@@ -0,0 +1,63 @@
+.segment "CODE"
+
+;;;
+;; Joypad controller code. The following memory addresses are reserved: $21-$23.
+;;
+;; Memory address $21 is used for internal purposes, whereas $22 and $23 contain
+;; the bitmask of the buttons that are pressed from each controller.
+;;;
+
+.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
diff --git a/space/include/oam.s b/space/include/oam.s
new file mode 100644
index 0000000..ecd1b7d
--- /dev/null
+++ b/space/include/oam.s
@@ -0,0 +1,13 @@
+.segment "CODE"
+
+.scope OAM
+ ADDR = $2003
+ DMA = $4014
+.endscope
+
+.macro OAM_WRITE_SPRITES
+ lda #$00
+ sta OAM::ADDR
+ lda #$02
+ sta OAM::DMA
+.endmacro
diff --git a/space/include/ppu.s b/space/include/ppu.s
new file mode 100644
index 0000000..23fd978
--- /dev/null
+++ b/space/include/ppu.s
@@ -0,0 +1,27 @@
+.segment "CODE"
+
+.scope PPU
+ CONTROL = $2000
+ MASK = $2001
+ STATUS = $2002
+ SCROLL = $2005
+ ADDRESS = $2006
+ DATA = $2007
+.endscope
+
+.macro PPU_ADDR address
+ lda #.HIBYTE(address)
+ sta PPU::ADDRESS
+ lda #.LOBYTE(address)
+ sta PPU::ADDRESS
+.endmacro
+
+;; WRITE_PPU_DATA is a macro that will write into PPU::ADDRESS the given address
+;; and into PPU::DATA the given byte value.
+.macro WRITE_PPU_DATA address, value
+ bit PPU::STATUS
+
+ PPU_ADDR address
+ lda #value
+ sta PPU::DATA
+.endmacro