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