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.

211 lines
3.8 KiB

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