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.

762 lines
17 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
  1. .INCLUDE "header.asm"
  2. .INCLUDE "init.asm"
  3. .INCLUDE "registers.asm"
  4. ; Memory layout:
  5. ; 0000-000F: scratch space for functions.
  6. ; 0010-0011: controller state of joypad #1.
  7. ; 0012-0013: controller state of joypad #2.
  8. ; 0014-0016: 24-bit counter of vblanks.
  9. ; 0017-0019: RGB color values to use for background color, from [0-31].
  10. ; 001A-001B: 16-bit pointer to next random byte.
  11. ; [gap]
  12. ; 0020-0021: (x, y) coordinates of player.
  13. ; 0022: shot cooldown timer.
  14. ; 0023-0024: index of next shot.
  15. ; [gap]
  16. ; 0030-008F: {sprite, x, y, x-velocity, y-velocity, unused} per shot.
  17. ; If sprite is 0, the shot is disabled.
  18. ; [gap]
  19. ; Sprite table buffers -- copied each frame to OAM during VBlank, using DMA.
  20. ; 0100-02FF: table 1 (4 bytes each: x/y coord, tile #, flip/priority/palette)
  21. ; 0300-031F: table 2 (2 bits each: high x-coord bit, size)
  22. .define joy1 $10
  23. .define joy2 $12
  24. .define vBlankCounter $14
  25. .define backgroundRed $17
  26. .define backgroundGreen $18
  27. .define backgroundBlue $19
  28. .define randomBytePtr $1A
  29. .define playerX $20
  30. .define playerY $21
  31. .define shotCooldown $22
  32. .define nextShotPtr $23
  33. .define shotArray $30
  34. .define shotArrayLength 16
  35. .define shotSize 6
  36. ; TODO(mcmillen): verify that we can relocate these without messing things up.
  37. .define numSprites 128
  38. .define spriteTableStart $100
  39. .define spriteTable1Size $200
  40. .define spriteTable2Start $300
  41. .define spriteTableSize $220
  42. .define spriteTableScratchStart $320
  43. ; Sets A to 8-bit (& enables 8-bit "B" register).
  44. .MACRO SetA8Bit
  45. sep #%00100000 ; 8-bit A/B.
  46. .ENDM
  47. ; Sets A to 16-bit.
  48. .MACRO SetA16Bit
  49. rep #%00100000 ; 16-bit A.
  50. .ENDM
  51. ; Sets X/Y to 16-bit.
  52. .MACRO SetXY16Bit
  53. rep #%00010000 ; 16-bit X/Y.
  54. .ENDM
  55. ; Stores result to A.
  56. ; Assumes 16-bit X & 8-bit A.
  57. ; Modifies X.
  58. ; Updates randomBytePtr.
  59. .MACRO GetRandomByte
  60. ldx randomBytePtr
  61. lda $028000, X ; $028000: beginning of ROM bank 2.
  62. inx
  63. cpx #$8000 ; This is the size of the entire ROM bank.
  64. bne +
  65. ldx #0
  66. +
  67. stx randomBytePtr
  68. .ENDM
  69. .BANK 0 SLOT 0
  70. .ORG 0
  71. .SECTION "MainCode"
  72. Start:
  73. InitializeSNES
  74. ; By default we assume 16-bit X/Y and 8-bit A.
  75. ; If any code wants to change this, it's expected to do so itself,
  76. ; and to change them back to the defaults before returning.
  77. SetXY16Bit
  78. SetA8Bit
  79. jsr LoadPaletteAndTileData
  80. jsr InitializeSpriteTables
  81. jsr InitializeWorld
  82. ; Set screen mode: 16x16 tiles for backgrounds, mode 1.
  83. lda #%11000001
  84. sta BGMODE
  85. ; Set sprite size to 16x16 (small) and 32x32 (large).
  86. lda #%01100000
  87. sta OAMSIZE
  88. ; Main screen: enable sprites & BG3.
  89. lda #%00010100
  90. sta MSENABLE
  91. ; Turn on the screen.
  92. ; Format: x000bbbb
  93. ; x: 0 = screen on, 1 = screen off, bbbb: Brightness ($0-$F)
  94. lda #%00001111
  95. sta INIDISP
  96. jmp MainLoop
  97. LoadPaletteAndTileData:
  98. ; For more details on DMA, see:
  99. ; http://wiki.superfamicom.org/snes/show/Grog%27s+Guide+to+DMA+and+HDMA+on+the+SNES
  100. ; http://wiki.superfamicom.org/snes/show/Making+a+Small+Game+-+Tic-Tac-Toe
  101. ;
  102. ; A lot of the graphics-related registers are explained in Qwertie's doc:
  103. ; http://emu-docs.org/Super%20NES/General/snesdoc.html
  104. ; ... but be careful, because there are some errors in this doc.
  105. ;
  106. ; bazz's tutorial (available from http://wiki.superfamicom.org/snes/) is
  107. ; quite helpful with palette / sprites / DMA, especially starting at
  108. ; http://wiki.superfamicom.org/snes/show/Working+with+VRAM+-+Loading+the+Palette
  109. ; Initialize the palette memory in a loop.
  110. ; We could also do this with a DMA transfer (like we do with the tile data
  111. ; below), but it seems overkill for just a few bytes. :)
  112. ; TODO(mcmillen): do it with a DMA transfer.
  113. ; First, sprite palette data:
  114. ldx #0
  115. lda #128 ; Palette entries for sprites start at 128.
  116. sta CGADDR
  117. -
  118. lda.l SpritePalette, X
  119. sta CGDATA
  120. inx
  121. cpx #32 ; 32 bytes of palette data.
  122. bne -
  123. ; Now, BG3 palette data.
  124. ; Palette entries for BG3 start at 0.
  125. ldx #0
  126. lda #0
  127. sta CGADDR
  128. -
  129. lda.l TilePalette, X
  130. sta CGDATA
  131. inx
  132. cpx #8 ; 8 bytes of palette data.
  133. bne -
  134. ; TODO(mcmillen): make the "DMA stuff into VRAM" a macro or function.
  135. ; Set VMADDR to where we want the DMA to start. We'll store sprite data
  136. ; at the beginning of VRAM.
  137. ldx #$0000
  138. stx VMADDR
  139. ; DMA 0 source address & bank.
  140. ldx #SpriteData
  141. stx DMA0SRC
  142. lda #:SpriteData
  143. sta DMA0SRCBANK
  144. ; DMA 0 transfer size. Equal to the size of sprites32.pic.
  145. ldx #2048
  146. stx DMA0SIZE
  147. ; DMA 0 control register.
  148. ; Transfer type 001 = 2 addresses, LH.
  149. lda #%00000001
  150. sta DMA0CTRL
  151. ; DMA 0 destination.
  152. lda #$18 ; The upper byte is assumed to be $21, so this is $2118 & $2119.
  153. sta DMA0DST
  154. ; Enable DMA channel 0.
  155. lda #%00000001
  156. sta DMAENABLE
  157. ; Store background tile data at byte $2000 of VRAM.
  158. ; (VMADDR is a word address, so multiply by 2 to get the byte address.)
  159. ldx #$1000
  160. stx VMADDR
  161. ; DMA 0 source address & bank.
  162. ldx #TileData
  163. stx DMA0SRC
  164. lda #:TileData
  165. sta DMA0SRCBANK
  166. ; DMA 0 transfer size. Equal to the size of tiles.pic.
  167. ldx #512
  168. stx DMA0SIZE
  169. ; DMA 0 control register.
  170. ; Transfer type 001 = 2 addresses, LH.
  171. lda #%00000001
  172. sta DMA0CTRL
  173. ; DMA 0 destination.
  174. lda #$18 ; The upper byte is assumed to be $21, so this is $2118 & $2119.
  175. sta DMA0DST
  176. ; Enable DMA channel 0.
  177. lda #%00000001
  178. sta DMAENABLE
  179. ; Tell the system that the BG3 tilemap starts at $4000.
  180. lda #%00100000
  181. sta BG3TILEMAP
  182. ; ... and that the background tile data for BG3 starts at $2000.
  183. lda #%00000001
  184. sta BG34NBA
  185. ; Set up the BG3 tilemap.
  186. ; VRAM write mode: increments the address every time we write a word.
  187. lda #%10000000
  188. sta VMAIN
  189. ; Set word address for accessing VRAM.
  190. ldx #$2000 ; BG 3 tilemap starts here. (Byte address $4000.)
  191. stx VMADDR
  192. ; Now write entries into the tile map.
  193. ldy #0
  194. -
  195. GetRandomByte
  196. sta $00
  197. ldx #$0000 ; This is a blank tile.
  198. ; 1 in 8 chance that we choose a non-blank tile.
  199. bit #%00000111
  200. bne +
  201. ldx #$0002
  202. bit #%10000000
  203. bne +
  204. ldx #$8002 ; Flip vertically.
  205. +
  206. stx VMDATA
  207. iny
  208. ; The tile map is 32x32 (1024 entries).
  209. cpy #1024
  210. bne -
  211. rts
  212. InitializeSpriteTables:
  213. ; This page is a good reference on SNES sprite formats:
  214. ; http://wiki.superfamicom.org/snes/show/SNES+Sprites
  215. ; It uses the same approach we're using, in which we keep a buffer of the
  216. ; sprite tables in RAM, and DMA the sprite tables to the system's OAM
  217. ; during VBlank.
  218. SetA16Bit
  219. ldx #$0000
  220. ; Fill sprite table 1. 4 bytes per sprite, laid out as follows:
  221. ; Byte 1: xxxxxxxx x: X coordinate
  222. ; Byte 2: yyyyyyyy y: Y coordinate
  223. ; Byte 3: cccccccc c: Starting tile #
  224. ; Byte 4: vhoopppc v: vertical flip h: horizontal flip o: priority bits
  225. ; p: palette #
  226. lda #$01
  227. -
  228. sta spriteTableStart, X
  229. .rept 4
  230. inx
  231. .endr
  232. cpx #spriteTable1Size
  233. bne -
  234. ; Fill sprite table 2. 2 bits per sprite, like so:
  235. ; bits 0,2,4,6 - High bit of the sprite's x-coordinate.
  236. ; bits 1,3,5,7 - Toggle Sprite size: 0 - small size 1 - large size
  237. ; Setting all the high bits keeps the sprites offscreen.
  238. lda #$FFFF
  239. -
  240. sta spriteTableStart, X
  241. inx
  242. inx
  243. cpx #spriteTableSize
  244. bne -
  245. SetA8Bit
  246. rts
  247. InitializeWorld:
  248. ; Start the background color as a dark blue.
  249. lda #4
  250. sta backgroundBlue
  251. ; Player's initial starting location.
  252. lda #(256 / 4)
  253. sta playerX
  254. lda #((224 - 32) / 2)
  255. sta playerY
  256. ; Next shot pointer starts at the beginning.
  257. ldx #shotArray
  258. stx nextShotPtr
  259. rts
  260. MainLoop:
  261. lda #%10000001 ; Enable NMI interrupt & auto joypad read.
  262. sta NMITIMEN
  263. wai ; Wait for interrupt.
  264. lda #%00000001 ; Disable NMI interrupt while processing.
  265. sta NMITIMEN
  266. jsr JoypadRead
  267. jsr JoypadHandler
  268. jsr UpdateWorld
  269. jsr UpdateSprites
  270. jsr FillSecondarySpriteTable
  271. jsr SetBackgroundColor
  272. jmp MainLoop
  273. JoypadRead:
  274. ; Load joypad registers into RAM for easy inspection & manipulation.
  275. -
  276. lda HVBJOY
  277. bit #$01 ; If auto-joypad read is happening, loop.
  278. bne -
  279. ldx JOY1L
  280. stx joy1
  281. ldx JOY2L
  282. stx joy2
  283. rts
  284. JoypadHandler:
  285. JoypadUp:
  286. lda joy1 + 1
  287. bit #$08 ; Up
  288. beq JoypadDown ; Button not pressed.
  289. lda playerY
  290. cmp #0
  291. beq JoypadDown ; Value saturated.
  292. dec playerY
  293. dec playerY
  294. JoypadDown:
  295. lda joy1 + 1
  296. bit #$04 ; Down
  297. beq JoypadLeft ; Button not pressed.
  298. lda playerY
  299. cmp #(224 - 32)
  300. beq JoypadLeft ; Value saturated.
  301. inc playerY
  302. inc playerY
  303. JoypadLeft:
  304. lda joy1 + 1
  305. bit #$02 ; Left
  306. beq JoypadRight ; Button not pressed.
  307. lda playerX
  308. cmp #0
  309. beq JoypadRight ; Value saturated.
  310. dec playerX
  311. dec playerX
  312. JoypadRight:
  313. lda joy1 + 1
  314. bit #$01 ; Right
  315. beq JoypadStart ; Button not pressed.
  316. lda playerX
  317. cmp #(256 - 32)
  318. beq JoypadStart ; Value saturated.
  319. inc playerX
  320. inc playerX
  321. JoypadStart:
  322. lda joy1 + 1
  323. bit #$10 ; Start
  324. beq JoypadSelect ; Button not pressed.
  325. lda backgroundRed
  326. cmp #31
  327. beq JoypadSelect ; Value saturated.
  328. inc backgroundRed
  329. JoypadSelect:
  330. lda joy1 + 1
  331. bit #$20 ; Select
  332. beq JoypadY ; Button not pressed.
  333. lda backgroundRed
  334. cmp #0
  335. beq JoypadY ; Value saturated.
  336. dec backgroundRed
  337. JoypadY:
  338. lda joy1 + 1
  339. bit #$40 ; Y
  340. beq JoypadX ; Button not pressed.
  341. lda backgroundGreen
  342. cmp #0
  343. beq JoypadX ; Value saturated.
  344. dec backgroundGreen
  345. JoypadX:
  346. lda joy1
  347. bit #$40 ; X
  348. beq JoypadL ; Button not pressed.
  349. lda backgroundGreen
  350. cmp #31
  351. beq JoypadL ; Value saturated.
  352. inc backgroundGreen
  353. JoypadL:
  354. lda joy1
  355. bit #$20 ; L
  356. beq JoypadR ; Button not pressed.
  357. lda backgroundBlue
  358. cmp #0
  359. beq JoypadR ; Value saturated.
  360. dec backgroundBlue
  361. JoypadR:
  362. lda joy1
  363. bit #$10 ; R
  364. beq JoypadB ; Button not pressed.
  365. lda backgroundBlue
  366. cmp #31
  367. beq JoypadB ; Value saturated.
  368. inc backgroundBlue
  369. JoypadB:
  370. lda joy1 + 1
  371. bit #$80 ; B
  372. beq JoypadDone
  373. jsr MaybeShoot
  374. JoypadDone:
  375. rts
  376. MaybeShoot:
  377. ; If the cooldown timer is non-zero, don't shoot.
  378. lda shotCooldown
  379. cmp #0
  380. bne ++
  381. ldx nextShotPtr
  382. ; Enable shot; set its position to player position.
  383. ; TODO(mcmillen): loop through the array until we find an unused shot.
  384. ; TODO(mcmillen): it might be easier/faster to keep N arrays: one for each
  385. ; field of shot (shotSpriteArray, shotXArray, shotYArray, ...)
  386. lda #8 ; Sprite number.
  387. sta 0, X
  388. lda playerX
  389. sta 1, X
  390. lda playerY
  391. sta 2, X
  392. lda #6 ; x-velocity.
  393. sta 3, X
  394. lda #0 ; y-velocity.
  395. sta 4, X
  396. ; Update nextShotPtr.
  397. .rept shotSize
  398. inx
  399. .endr
  400. cpx #(shotArray + shotArrayLength * shotSize)
  401. bne +
  402. ldx #shotArray
  403. +
  404. stx nextShotPtr
  405. ; Set cooldown timer.
  406. lda #10
  407. sta shotCooldown
  408. ++
  409. rts
  410. UpdateWorld:
  411. ; Update shot cooldown.
  412. lda shotCooldown
  413. cmp #0
  414. beq +
  415. dec A
  416. sta shotCooldown
  417. +
  418. ldx #0
  419. ; Update shot position.
  420. UpdateShot:
  421. lda shotArray, X
  422. cmp #0
  423. beq ShotDone
  424. ; Add to the x-coordinate. If the carry bit is set, we went off the edge
  425. ; of the screen, so disable the shot.
  426. lda shotArray + 3, X ; x-velocity.
  427. sta $00
  428. lda shotArray + 1, X
  429. clc
  430. adc $00
  431. bcs DisableShot
  432. sta shotArray + 1, X ; Store new x-coord.
  433. ; Add to the y-coordinate.
  434. lda shotArray + 4, X ; y-velocity.
  435. sta $00
  436. lda shotArray + 2, X
  437. ; no need for clc - if it was set above, we already jumped to DisableShot.
  438. adc $00
  439. cmp #224
  440. bcs DisableShot
  441. sta shotArray + 2, X ; Store new y-coord.
  442. jmp ShotDone
  443. DisableShot:
  444. stz shotArray, X
  445. ShotDone:
  446. ; TODO(mcmillen): in places where we .rept inx (etc), is it faster to use
  447. ; actual addition?
  448. .rept shotSize
  449. inx
  450. .endr
  451. cpx #(shotArrayLength * shotSize)
  452. bne UpdateShot
  453. ; Make the background scroll. Horizontal over time; vertical depending on
  454. ; player's y-coordinate.
  455. lda vBlankCounter
  456. sta BG3HOFS
  457. lda vBlankCounter + 1
  458. sta BG3HOFS
  459. lda playerY
  460. .rept 3
  461. lsr
  462. .endr
  463. sta BG3VOFS
  464. stz BG3VOFS
  465. rts
  466. UpdateSprites:
  467. ; Zero out the scratch space for the secondary sprite table.
  468. ldx #0
  469. -
  470. stz spriteTableScratchStart, X
  471. inx
  472. cpx #numSprites
  473. bne -
  474. ldx #0 ; Index into sprite table 1.
  475. ldy #0 ; Index into sprite table 2.
  476. ; Copy player coords into sprite table.
  477. lda playerX
  478. sta spriteTableStart, X
  479. lda playerY
  480. sta spriteTableStart + 1, X
  481. lda #0
  482. sta spriteTableStart + 2, X
  483. ; Set priority bits so that the sprite is drawn in front.
  484. lda #%00110000
  485. sta spriteTableStart + 3, X
  486. lda #%11000000 ; Enable large sprite.
  487. sta spriteTableScratchStart, Y
  488. .rept 4
  489. inx
  490. .endr
  491. iny
  492. ; Now add shots.
  493. sty $00 ; Save sprite table 2 index.
  494. ldy #0 ; Index into shotArray.
  495. -
  496. lda shotArray, Y
  497. cmp #0
  498. beq + ; If not enabled, skip to next shot.
  499. ; Update sprite table 1.
  500. sta spriteTableStart + 2, X ; sprite number
  501. lda shotArray + 1, Y
  502. sta spriteTableStart, X ; x
  503. lda shotArray + 2, Y
  504. sta spriteTableStart + 1, X ; y
  505. ; Update secondary sprite table.
  506. phy ; Save shotArray index.
  507. ldy $00
  508. lda #%11000000
  509. sta spriteTableScratchStart, Y
  510. iny
  511. sty $00
  512. ply ; Restore shotArrayIndex.
  513. .rept 4
  514. inx
  515. .endr
  516. +
  517. .rept shotSize
  518. iny
  519. .endr
  520. cpy #(shotArrayLength * shotSize)
  521. bne -
  522. rts
  523. FillSecondarySpriteTable:
  524. ; The secondary sprite table wants 2 bits for each sprite: one to set the
  525. ; sprite's size, and one that's the high bit of the sprite's x-coordinate.
  526. ; It's annoying to deal with bitfields when thinking about business logic,
  527. ; so the spriteTableScratch array contains one byte for each sprite, in
  528. ; which the two most significant bits are the "size" and "upper x" bits.
  529. ; This function is meant to be called after UpdateWorld, and packs those
  530. ; bytes into the actual bitfield that the OAM wants for the secondary
  531. ; sprite table.
  532. ;
  533. ; The expected format of every byte in the scratch sprite table is:
  534. ; sx------ s = size (0 = small, 1 = large)
  535. ; x = flipped high x-coordinate (so 1 behaves like "enable").
  536. ldx #0 ; Index into input table.
  537. ldy #0 ; Index into output table.
  538. -
  539. stz $00 ; Current byte; filled out by a set of 4 input table entries.
  540. .rept 4
  541. ; For each byte, the lower-order bits correspond to the lower-numbered
  542. ; sprites; therefore we insert the current sprite's bits "at the top"
  543. ; and shift them right for each successive sprite.
  544. lsr $00
  545. lsr $00
  546. lda spriteTableScratchStart, X
  547. ora $00
  548. sta $00
  549. inx
  550. .endr
  551. lda $00
  552. eor #%01010101
  553. sta spriteTable2Start, Y
  554. iny
  555. cpx #numSprites
  556. bne -
  557. rts
  558. SetBackgroundColor:
  559. ; The background-color bytes are (R, G, B), each ranging from [0-31].
  560. ; The palette color format is 15-bit: [0bbbbbgg][gggrrrrr]
  561. ; Set the background color.
  562. ; Entry 0 corresponds to the SNES background color.
  563. stz CGADDR
  564. ; Compute and the low-order byte and store it in CGDATA.
  565. lda backgroundGreen
  566. .rept 5
  567. asl
  568. .endr
  569. ora backgroundRed
  570. sta CGDATA
  571. ; Compute the high-order byte and store it in CGDATA.
  572. lda backgroundBlue
  573. .rept 2
  574. asl
  575. .endr
  576. sta $00
  577. lda backgroundGreen
  578. .rept 3
  579. lsr
  580. .endr
  581. ora $00
  582. sta CGDATA
  583. rts
  584. VBlankHandler:
  585. jsr VBlankCounter
  586. jsr DMASpriteTables
  587. rti
  588. VBlankCounter:
  589. ; Increment a counter of how many VBlanks we've done.
  590. ; This is a 24-bit counter. At 60 vblanks/second, this will take
  591. ; 77 hours to wrap around; that's good enough for me :)
  592. inc vBlankCounter
  593. bne +
  594. inc vBlankCounter + 1
  595. bne +
  596. inc vBlankCounter + 2
  597. +
  598. rts
  599. DMASpriteTables:
  600. ; Store at the base OAM address.
  601. ldx #$0000
  602. stx OAMADDR
  603. ; Default DMA control; destination $2104 (OAM data register).
  604. stz DMA0CTRL
  605. lda #$04
  606. sta DMA0DST
  607. ; Our sprites start at $0100 in bank 0 and are #$220 bytes long.
  608. ldx #spriteTableStart
  609. stx DMA0SRC
  610. stz DMA0SRCBANK
  611. ldx #spriteTableSize
  612. stx DMA0SIZE
  613. ; Kick off the DMA transfer.
  614. lda #%00000001
  615. sta DMAENABLE
  616. rts
  617. .ENDS
  618. ; Bank 1 is used for our graphics assets.
  619. .BANK 1 SLOT 0
  620. .ORG 0
  621. .SECTION "GraphicsData"
  622. SpriteData:
  623. .INCBIN "sprites32.pic"
  624. SpritePalette:
  625. .INCBIN "sprites32.clr"
  626. TileData:
  627. .INCBIN "tiles.pic"
  628. TilePalette:
  629. .INCBIN "tiles.clr"
  630. .ENDS
  631. ; Fill an entire bank with random numbers.
  632. .SEED 1
  633. .BANK 2 SLOT 0
  634. .ORG 0
  635. .SECTION "RandomBytes"
  636. .DBRND 32 * 1024, 0, 255
  637. .ENDS