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.

563 lines
12 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
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
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 "init.asm"
  3. .INCLUDE "registers.asm"
  4. ; Assumes 16-bit X & 8-bit A.
  5. ; Stores result to A.
  6. ; Modifies X.
  7. ; Updates $0018-$0019 to point at the next available random byte.
  8. .MACRO GetRandomByte
  9. ldx $18
  10. lda $028000, X ; $028000: beginning of ROM bank 2.
  11. inx
  12. cpx #$8000 ; This is the size of the entire ROM bank.
  13. bne +
  14. ldx #0
  15. +
  16. stx $18
  17. .ENDM
  18. .BANK 0 SLOT 0
  19. .ORG 0
  20. .SECTION "MainCode"
  21. ; Memory layout:
  22. ; 0000-000F: scratch space for functions.
  23. ; 0010-0011: controller state of joypad #1.
  24. ; 0012-0013: controller state of joypad #2.
  25. ; 0014-0017: 32-bit counter of vblanks.
  26. ; 0018-0019: 16-bit pointer to next random byte.
  27. ; 0020-0021: (x, y) coordinates of player.
  28. ; 0022-0024: RGB color values to use for background color, from [0-31].
  29. ;
  30. ; Sprite table buffers -- copied each frame to OAM during VBlank, using DMA.
  31. ; 0100-02FF: table 1 (4 bytes each: x/y coord, tile #, flip/priority/palette)
  32. ; 0300-031F: table 2 (2 bits each: high x-coord bit, size)
  33. Start:
  34. InitializeSNES
  35. jsr LoadPaletteAndTileData
  36. jsr InitializeSpriteTables
  37. ; Set screen mode: 16x16 tiles for backgrounds, mode 1.
  38. lda #%11000001
  39. sta SCREENMODE
  40. ; Set sprite size to 16x16 (small) and 32x32 (large).
  41. lda #%01100000
  42. sta OAMSIZE
  43. ; Main screen: enable sprites & BG3.
  44. lda #%00010100
  45. sta MSENABLE
  46. ; Turn on the screen.
  47. ; Format: x000bbbb
  48. ; x: 0 = screen on, 1 = screen off, bbbb: Brightness ($0-$F)
  49. lda #%00001111
  50. sta INIDISP
  51. ; Enable NMI interrupt & joypad.
  52. ; n-vh---j n: NMI interrupt enable v: vertical counter enable
  53. ; h: horizontal counter enable j: joypad enable
  54. lda #%10000001
  55. sta NMITIMEN
  56. ; Store zeroes to the controller status registers.
  57. ; TODO(mcmillen): is this needed? I think the system will overwrite these
  58. ; automatically.
  59. stz JOY1H
  60. stz JOY1L
  61. ; Write something recognizable into our scratch space.
  62. jsr FillScratch
  63. ; Start the background color as a dark blue.
  64. lda #4
  65. sta $24
  66. ; Player's initial starting location.
  67. lda #(256 / 4)
  68. sta $20
  69. lda #((224 - 16) / 2)
  70. sta $21
  71. MainLoop:
  72. wai ; Wait for interrupt.
  73. jmp MainLoop
  74. LoadPaletteAndTileData:
  75. ; For more details on DMA, see:
  76. ; http://wiki.superfamicom.org/snes/show/Grog%27s+Guide+to+DMA+and+HDMA+on+the+SNES
  77. ; http://wiki.superfamicom.org/snes/show/Making+a+Small+Game+-+Tic-Tac-Toe
  78. ;
  79. ; A lot of the graphics-related registers are explained in Qwertie's doc:
  80. ; http://emu-docs.org/Super%20NES/General/snesdoc.html
  81. ; ... but be careful, because there are some errors in this doc.
  82. ;
  83. ; bazz's tutorial (available from http://wiki.superfamicom.org/snes/) is
  84. ; quite helpful with palette / sprites / DMA, especially starting at
  85. ; http://wiki.superfamicom.org/snes/show/Working+with+VRAM+-+Loading+the+Palette
  86. ; 16-bit X/Y registers. Used for DMA source address & transfer size, both of
  87. ; which want 16-bit values.
  88. rep #%00010000
  89. ; 8-bit A/B registers. Used for DMA source bank & destination address.
  90. sep #%00100000
  91. ; Initialize the palette memory in a loop.
  92. ; We could also do this with a DMA transfer (like we do with the tile data
  93. ; below), but it seems overkill for just a few bytes. :)
  94. ; TODO(mcmillen): do it with a DMA transfer.
  95. ; First, sprite palette data:
  96. ldx #0
  97. lda #128 ; Palette entries for sprites start at 128.
  98. sta CGADDR
  99. -
  100. lda.l SpritePalette, X
  101. sta CGDATA
  102. inx
  103. cpx #32 ; 32 bytes of palette data.
  104. bne -
  105. ; Now, BG3 palette data.
  106. ; Palette entries for BG3 start at 0.
  107. ; TODO(mcmillen): BG2 started at 32, but maybe that's only in mode 0?
  108. ldx #0
  109. lda #0
  110. sta CGADDR
  111. -
  112. lda.l TilePalette, X
  113. sta CGDATA
  114. inx
  115. cpx #8 ; 8 bytes of palette data.
  116. bne -
  117. ; TODO(mcmillen): make the "DMA stuff into VRAM" a macro or function.
  118. ; Set VMADDR to where we want the DMA to start. We'll store sprite data
  119. ; at the beginning of VRAM.
  120. ldx #$0000
  121. stx VMADDR
  122. ; DMA 0 source address & bank.
  123. ldx #SpriteData
  124. stx DMA0SRC
  125. lda #:SpriteData
  126. sta DMA0SRCBANK
  127. ; DMA 0 transfer size. Equal to the size of sprites32.pic.
  128. ldx #2048
  129. stx DMA0SIZE
  130. ; DMA 0 control register.
  131. ; Transfer type 001 = 2 addresses, LH.
  132. lda #%00000001
  133. sta DMA0CTRL
  134. ; DMA 0 destination.
  135. lda #$18 ; The upper byte is assumed to be $21, so this is $2118 & $2119.
  136. sta DMA0DST
  137. ; Enable DMA channel 0.
  138. lda #%00000001
  139. sta DMAENABLE
  140. ; Store background tile data at byte $2000 of VRAM.
  141. ; (VMADDR is a word address, so multiply by 2 to get the byte address.)
  142. ldx #$1000
  143. stx VMADDR
  144. ; DMA 0 source address & bank.
  145. ldx #TileData
  146. stx DMA0SRC
  147. lda #:TileData
  148. sta DMA0SRCBANK
  149. ; DMA 0 transfer size. Equal to the size of tiles.pic.
  150. ldx #512
  151. stx DMA0SIZE
  152. ; DMA 0 control register.
  153. ; Transfer type 001 = 2 addresses, LH.
  154. lda #%00000001
  155. sta DMA0CTRL
  156. ; DMA 0 destination.
  157. lda #$18 ; The upper byte is assumed to be $21, so this is $2118 & $2119.
  158. sta DMA0DST
  159. ; Enable DMA channel 0.
  160. lda #%00000001
  161. sta DMAENABLE
  162. ; Tell the system that the BG3 tilemap starts at $4000.
  163. lda #%00100000
  164. sta BG3TILEMAP
  165. ; ... and that the background tile data for BG3 starts at $2000.
  166. lda #%00000001
  167. sta BG34NBA
  168. ; Set up the BG3 tilemap.
  169. ; VRAM write mode: increments the address every time we write a word.
  170. lda #%10000000
  171. sta VMAIN
  172. ; Set word address for accessing VRAM.
  173. ldx #$2000 ; BG 3 tilemap starts here. (Byte address $4000.)
  174. stx VMADDR
  175. ; Now write entries into the tile map.
  176. ldy #0
  177. -
  178. GetRandomByte
  179. sta $00
  180. ldx #$0000 ; This is a blank tile.
  181. ; 1 in 8 chance that we choose a non-blank tile.
  182. and #%00000111
  183. cmp #%00000111
  184. bne +
  185. ldx #$0002
  186. lda $00
  187. and #%10000000
  188. cmp #%10000000
  189. bne +
  190. ldx #$8002 ; Flip vertically.
  191. +
  192. stx VMDATA
  193. iny
  194. ; The tile map is 32x32 (1024 entries).
  195. cpy #1024
  196. bne -
  197. rts
  198. InitializeSpriteTables:
  199. ; This page is a good reference on SNES sprite formats:
  200. ; http://wiki.superfamicom.org/snes/show/SNES+Sprites
  201. ; It uses the same approach we're using, in which we keep a buffer of the
  202. ; sprite tables in RAM, and DMA the sprite tables to the system's OAM
  203. ; during VBlank.
  204. rep #%00110000 ; 16-bit A/X/Y.
  205. ldx #$0000
  206. ; Fill sprite table 1. 4 bytes per sprite, laid out as follows:
  207. ; Byte 1: xxxxxxxx x: X coordinate
  208. ; Byte 2: yyyyyyyy y: Y coordinate
  209. ; Byte 3: cccccccc c: Starting tile #
  210. ; Byte 4: vhoopppc v: vertical flip h: horizontal flip o: priority bits
  211. ; p: palette #
  212. lda #$01
  213. -
  214. sta $0100, X ; We keep our sprite table at $0100 and DMA it to OAM later.
  215. inx
  216. inx
  217. inx
  218. inx
  219. cpx #$0200
  220. bne -
  221. ; Fill sprite table 2. 2 bits per sprite, like so:
  222. ; bits 0,2,4,6 - High bit of the sprite's x-coordinate.
  223. ; bits 1,3,5,7 - Toggle Sprite size: 0 - small size 1 - large size
  224. ; Setting all the high bits keeps the sprites offscreen.
  225. lda #%0101010101010101
  226. -
  227. sta $0100, X
  228. inx
  229. inx
  230. cpx #$0220
  231. bne -
  232. sep #%00100000 ; 8-bit A.
  233. rts
  234. VBlankHandler:
  235. jsr VBlankCounter ; DEBUG
  236. jsr JoypadHandler
  237. jsr SetBackgroundColor
  238. jsr UpdateGraphics
  239. jsr DMASpriteTables
  240. rti
  241. VBlankCounter:
  242. ; Increment a counter of how many VBlanks we've done.
  243. inc $14
  244. lda $14
  245. cmp #$00
  246. bne VBlankCounterDone
  247. inc $15
  248. lda $15
  249. cmp #$00
  250. bne VBlankCounterDone
  251. inc $16
  252. lda $16
  253. cmp #$00
  254. bne VBlankCounterDone
  255. inc $17
  256. VBlankCounterDone:
  257. rts
  258. JoypadHandler:
  259. jsr JoypadDebug ; DEBUG
  260. JoypadUp:
  261. lda JOY1H
  262. and #$08 ; Up
  263. cmp #$08
  264. bne JoypadDown ; Button not pressed.
  265. lda $21
  266. cmp #0
  267. beq JoypadDown ; Value saturated.
  268. dec $21
  269. dec $21
  270. JoypadDown:
  271. lda JOY1H
  272. and #$04
  273. cmp #$04
  274. bne JoypadLeft ; Button not pressed.
  275. lda $21
  276. cmp #(224 - 32)
  277. beq JoypadLeft ; Value saturated.
  278. inc $21
  279. inc $21
  280. JoypadLeft:
  281. lda JOY1H
  282. and #$02 ; Left
  283. cmp #$02
  284. bne JoypadRight ; Button not pressed.
  285. lda $20
  286. cmp #0
  287. beq JoypadRight ; Value saturated.
  288. dec $20
  289. dec $20
  290. JoypadRight:
  291. lda JOY1H
  292. and #$01
  293. cmp #$01 ; Right
  294. bne JoypadB ; Button not pressed.
  295. lda $20
  296. cmp #(256 - 32)
  297. beq JoypadB ; Value saturated.
  298. inc $20
  299. inc $20
  300. JoypadB:
  301. lda JOY1H
  302. and #$80 ; B
  303. cmp #$80
  304. bne JoypadA ; Button not pressed.
  305. lda $22
  306. cmp #0
  307. beq JoypadA ; Value saturated.
  308. dec $22
  309. JoypadA:
  310. lda JOY1L
  311. and #$80 ; A
  312. cmp #$80
  313. bne JoypadY ; Button not pressed.
  314. lda $22
  315. cmp #31
  316. beq JoypadY ; Value saturated.
  317. inc $22
  318. JoypadY:
  319. lda JOY1H
  320. and #$40 ; Y
  321. cmp #$40
  322. bne JoypadX ; Button not pressed.
  323. lda $23
  324. cmp #0
  325. beq JoypadX ; Value saturated.
  326. dec $23
  327. JoypadX:
  328. lda JOY1L
  329. and #$40 ; X
  330. cmp #$40
  331. bne JoypadL ; Button not pressed.
  332. lda $23
  333. cmp #31
  334. beq JoypadL ; Value saturated.
  335. inc $23
  336. JoypadL:
  337. lda JOY1L
  338. and #$20 ; L
  339. cmp #$20
  340. bne JoypadR ; Button not pressed.
  341. lda $24
  342. cmp #0
  343. beq JoypadR ; Value saturated.
  344. dec $24
  345. JoypadR:
  346. lda JOY1L
  347. and #$10 ; R
  348. cmp #$10
  349. bne JoypadDone ; Button not pressed.
  350. lda $24
  351. cmp #31
  352. beq JoypadDone ; Value saturated.
  353. inc $24
  354. ; TODO(mcmillen): have Start and Select do something too.
  355. JoypadDone:
  356. rts
  357. JoypadDebug:
  358. ; Load joypad registers into RAM for easier inspection.
  359. lda JOY1L
  360. sta $10
  361. lda JOY1H
  362. sta $11
  363. lda JOY2L
  364. sta $12
  365. lda JOY2H
  366. sta $13
  367. rts
  368. SetBackgroundColor:
  369. ; $22 $23 $24 are (R, G, B), each ranging from [0-31].
  370. ; The palette color format is 15-bit: [0bbbbbgg][gggrrrrr]
  371. ; Set the background color.
  372. ; Entry 0 corresponds to the SNES background color.
  373. stz CGADDR
  374. ; Compute and the low-order byte and store it in CGDATA.
  375. lda $23 ; Green.
  376. .rept 5
  377. asl
  378. .endr
  379. ora $22 ; Red.
  380. sta CGDATA
  381. ; Compute the high-order byte and store it in CGDATA.
  382. lda $24 ; Blue.
  383. .rept 2
  384. asl
  385. .endr
  386. sta $00
  387. lda $23 ; Green.
  388. .rept 3
  389. lsr
  390. .endr
  391. ora $00
  392. sta CGDATA
  393. rts
  394. UpdateGraphics:
  395. ; Copy player coords into sprite table.
  396. lda $0020
  397. sta $0100
  398. lda $0021
  399. sta $0101
  400. ; Choose which sprite based on frame count.
  401. ; Set priority bits so that the sprite is drawn in front.
  402. lda #%00110000
  403. sta $0103
  404. ; Clear x-MSB so that the sprite is displayed.
  405. lda $0300
  406. and #%11111110
  407. ora #%00000010 ; ... and make it the large size. (32x32)
  408. sta $0300
  409. ; Make the background scroll. Horizontal over time; vertical depending on
  410. ; player's y-coordinate.
  411. lda $14
  412. sta BG3HOFS
  413. lda $15
  414. sta BG3HOFS
  415. lda $21
  416. .rept 3
  417. lsr
  418. .endr
  419. sta BG3VOFS
  420. stz BG3VOFS
  421. rts
  422. DMASpriteTables:
  423. rep #%00010000 ; 16-bit X/Y.
  424. sep #%00100000 ; 8-bit A.
  425. ; Store at the base OAM address.
  426. ldx #$0000
  427. stx OAMADDR
  428. ; Default DMA control; destination $2104 (OAM data register).
  429. stz DMA0CTRL
  430. lda #$04
  431. sta DMA0DST
  432. ; Our sprites start at $0100 in bank 0 and are #$220 bytes long.
  433. ldx #$0100
  434. stx DMA0SRC
  435. stz DMA0SRCBANK
  436. ldx #$0220
  437. stx DMA0SIZE
  438. ; Kick off the DMA transfer.
  439. lda #%00000001
  440. sta DMAENABLE
  441. rts
  442. FillScratch:
  443. lda #$42 ; ASCII "B"
  444. ldx #0
  445. -
  446. sta $00, X
  447. inx
  448. cpx #$10
  449. bne -
  450. rts
  451. .ENDS
  452. ; Bank 1 is used for our graphics assets.
  453. .BANK 1 SLOT 0
  454. .ORG 0
  455. .SECTION "GraphicsData"
  456. SpriteData:
  457. .INCBIN "sprites32.pic"
  458. SpritePalette:
  459. .INCBIN "sprites32.clr"
  460. TileData:
  461. .INCBIN "tiles.pic"
  462. TilePalette:
  463. .INCBIN "tiles.clr"
  464. .ENDS
  465. ; Fill an entire bank with random numbers.
  466. .SEED 1
  467. .BANK 2 SLOT 0
  468. .ORG 0
  469. .SECTION "RandomBytes"
  470. .DBRND 32 * 1024, 0, 255
  471. .ENDS