Simple SNES shoot-'em-up game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
4.0 KiB

9 years ago
  1. .INCLUDE "header.inc"
  2. .INCLUDE "InitSNES.asm"
  3. ; Memory layout:
  4. ; 00-0F: scratch space for functions.
  5. ; 10-13: controller state.
  6. ; 14-17: 32-bit counter of vblanks.
  7. ; 20-22: [rgb] color values to use for background color.
  8. ;========================
  9. ; Start
  10. ;========================
  11. .BANK 0 SLOT 0
  12. .ORG 0
  13. .SECTION "MainCode"
  14. Start:
  15. InitSNES ; Initialize SNES.
  16. ; Set the background color.
  17. ; $2121 is the color palette selection register [CGADD].
  18. ; Storing 0 because that's the SNES background color.
  19. stz $2121
  20. ; Turn on the screen.
  21. ; $2100: Screen display register [INIDISP]
  22. ;
  23. ; Format: x000bbbb
  24. ; x: 0 = screen on, 1 = screen off, bbbb: Brightness ($0-$F)
  25. lda #%00001111
  26. sta $2100
  27. ; Enable NMI interrupt & joypad.
  28. ; Register $4200: Counter enable [NMITIMEN]
  29. ; n-vh---j n: NMI interrupt enable v: vertical counter enable
  30. ; h: horizontal counter enable j: joypad enable
  31. lda #$81
  32. sta $4200
  33. ; Store zeroes to the controller status registers.
  34. ; TODO(mcmillen): is this needed?
  35. stz $4218
  36. stz $4219
  37. ; Write something recognizable into our scratch space.
  38. jsr FillScratch
  39. Loop:
  40. wai
  41. jmp Loop
  42. VBlankHandler:
  43. jsr VBlankCounter ; DEBUG
  44. jsr JoypadHandler
  45. jsr SetBackgroundColor
  46. ; jsr FillScratch ; DEBUG
  47. rti
  48. VBlankCounter:
  49. ; Keeps a counter of how many VBlanks we've done.
  50. inc $14
  51. lda $14
  52. cmp #$00
  53. bne VBlankCounterDone
  54. inc $15
  55. lda $15
  56. cmp #$00
  57. bne VBlankCounterDone
  58. inc $16
  59. lda $16
  60. cmp #$00
  61. bne VBlankCounterDone
  62. inc $17
  63. VBlankCounterDone:
  64. rts
  65. JoypadHandler:
  66. ; $4218: Joypad #1 status [JOY1L]
  67. ; Format: AXLR0000
  68. ; $4219: Joypad #1 status [JOY1H]
  69. ; Format: BYsSudlr (s=select, S=start, udlr = joypad)
  70. ; Joypad #2 status is $421A [JOY2L] and $421B [JOY2H].
  71. jsr JoypadDebug ; DEBUG
  72. ; TODO(mcmillen): read joypad from local memory instead of registers?
  73. JoypadUp:
  74. lda $4219
  75. and #$08 ; Up
  76. cmp #$08
  77. bne JoypadDown ; Button not pressed.
  78. lda $20
  79. cmp #31
  80. beq JoypadDown ; Value saturated.
  81. inc $20
  82. JoypadDown:
  83. lda $4219
  84. and #$04 ; Down
  85. cmp #$04
  86. bne JoypadLeft ; Button not pressed.
  87. lda $20
  88. cmp #0
  89. beq JoypadLeft ; Value saturated.
  90. dec $20
  91. JoypadLeft:
  92. lda $4219
  93. and #$02 ; Left
  94. cmp #$02
  95. bne JoypadRight ; Button not pressed.
  96. lda $22
  97. cmp #0
  98. beq JoypadRight ; Value saturated.
  99. dec $22
  100. JoypadRight:
  101. lda $4219
  102. and #$01
  103. cmp #$01 ; Right
  104. bne JoypadB ; Button not pressed.
  105. lda $22
  106. cmp #31
  107. beq JoypadB ; Value saturated.
  108. inc $22
  109. JoypadB:
  110. lda $4219
  111. and #$80 ; B
  112. cmp #$80
  113. bne JoypadY ; Button not pressed.
  114. lda $21
  115. cmp #31
  116. beq JoypadY ; Value saturated.
  117. inc $21
  118. JoypadY:
  119. lda $4219
  120. and #$40 ; Y
  121. cmp #$40
  122. bne JoypadDone ; Button not pressed.
  123. lda $21
  124. cmp #0
  125. beq JoypadDone
  126. dec $21
  127. JoypadDone:
  128. rts
  129. JoypadDebug:
  130. ; Load joypad registers into RAM for easier inspection.
  131. lda $4218
  132. sta $10
  133. lda $4219
  134. sta $11
  135. lda $421A
  136. sta $12
  137. lda $421B
  138. sta $13
  139. rts
  140. SetBackgroundColor:
  141. ; $20 $21 $22 are R, G, B, as appropriate.
  142. ; The color format is 15-bit: [0bbbbbgg][gggrrrrr]
  143. ; We store the low-order byte first.
  144. lda $20 ; RED
  145. sta $00
  146. lda $22 ; BLUE
  147. asl
  148. asl
  149. sta $01
  150. lda $21 ; GREEN.
  151. ; First store the high-order bits of green into $01.
  152. lsr
  153. lsr
  154. lsr
  155. ora $01
  156. pha ; push 1
  157. ; Now store the low-order bits into $00.
  158. lda $21
  159. and #%00000111
  160. asl
  161. asl
  162. asl
  163. asl
  164. asl
  165. ora $00
  166. pha ; push 2
  167. ; Now store the two values from the stack into the palette.
  168. pla ; pull 2
  169. sta $2122
  170. pla ; pull 1
  171. sta $2122
  172. rts
  173. FillScratch:
  174. lda #$42 ; B
  175. ldx #0
  176. FillScratchLoop:
  177. sta $00,X
  178. inx
  179. cpx #$10
  180. bne FillScratchLoop
  181. rts
  182. .ENDS