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.

788 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: next-shot state.
  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 nextShotState $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. rts
  257. MainLoop:
  258. lda #%10000001 ; Enable NMI interrupt & auto joypad read.
  259. sta NMITIMEN
  260. wai ; Wait for interrupt.
  261. lda #%00000001 ; Disable NMI interrupt while processing.
  262. sta NMITIMEN
  263. jsr JoypadRead
  264. jsr JoypadHandler
  265. jsr UpdateWorld
  266. jsr UpdateSprites
  267. jsr FillSecondarySpriteTable
  268. jsr SetBackgroundColor
  269. jmp MainLoop
  270. JoypadRead:
  271. ; Load joypad registers into RAM for easy inspection & manipulation.
  272. -
  273. lda HVBJOY
  274. bit #$01 ; If auto-joypad read is happening, loop.
  275. bne -
  276. ldx JOY1L
  277. stx joy1
  278. ldx JOY2L
  279. stx joy2
  280. rts
  281. JoypadHandler:
  282. JoypadUp:
  283. lda joy1 + 1
  284. bit #$08 ; Up
  285. beq JoypadDown ; Button not pressed.
  286. lda playerY
  287. cmp #0
  288. beq JoypadDown ; Value saturated.
  289. dec playerY
  290. dec playerY
  291. JoypadDown:
  292. lda joy1 + 1
  293. bit #$04 ; Down
  294. beq JoypadLeft ; Button not pressed.
  295. lda playerY
  296. cmp #(224 - 32)
  297. beq JoypadLeft ; Value saturated.
  298. inc playerY
  299. inc playerY
  300. JoypadLeft:
  301. lda joy1 + 1
  302. bit #$02 ; Left
  303. beq JoypadRight ; Button not pressed.
  304. lda playerX
  305. cmp #0
  306. beq JoypadRight ; Value saturated.
  307. dec playerX
  308. dec playerX
  309. JoypadRight:
  310. lda joy1 + 1
  311. bit #$01 ; Right
  312. beq JoypadStart ; Button not pressed.
  313. lda playerX
  314. cmp #(256 - 32)
  315. beq JoypadStart ; Value saturated.
  316. inc playerX
  317. inc playerX
  318. JoypadStart:
  319. lda joy1 + 1
  320. bit #$10 ; Start
  321. beq JoypadSelect ; Button not pressed.
  322. lda backgroundRed
  323. cmp #31
  324. beq JoypadSelect ; Value saturated.
  325. inc backgroundRed
  326. JoypadSelect:
  327. lda joy1 + 1
  328. bit #$20 ; Select
  329. beq JoypadY ; Button not pressed.
  330. lda backgroundRed
  331. cmp #0
  332. beq JoypadY ; Value saturated.
  333. dec backgroundRed
  334. JoypadY:
  335. lda joy1 + 1
  336. bit #$40 ; Y
  337. beq JoypadX ; Button not pressed.
  338. lda backgroundGreen
  339. cmp #0
  340. beq JoypadX ; Value saturated.
  341. dec backgroundGreen
  342. JoypadX:
  343. lda joy1
  344. bit #$40 ; X
  345. beq JoypadL ; Button not pressed.
  346. lda backgroundGreen
  347. cmp #31
  348. beq JoypadL ; Value saturated.
  349. inc backgroundGreen
  350. JoypadL:
  351. lda joy1
  352. bit #$20 ; L
  353. beq JoypadR ; Button not pressed.
  354. lda backgroundBlue
  355. cmp #0
  356. beq JoypadR ; Value saturated.
  357. dec backgroundBlue
  358. JoypadR:
  359. lda joy1
  360. bit #$10 ; R
  361. beq JoypadB ; Button not pressed.
  362. lda backgroundBlue
  363. cmp #31
  364. beq JoypadB ; Value saturated.
  365. inc backgroundBlue
  366. JoypadB:
  367. lda joy1 + 1
  368. bit #$80 ; B
  369. beq JoypadDone
  370. jsr MaybeShoot
  371. JoypadDone:
  372. rts
  373. MaybeShoot:
  374. ; If the cooldown timer is non-zero, don't shoot.
  375. lda shotCooldown
  376. cmp #0
  377. bne MaybeShootDone
  378. ; Find the first empty spot in the shots array.
  379. ldx #shotArray
  380. -
  381. lda 0, X
  382. cmp #0
  383. beq +
  384. .rept shotSize
  385. inx
  386. .endr
  387. ; If we went all the way to the end, bail out.
  388. cpx #(shotArray + shotArrayLength * shotSize)
  389. beq MaybeShootDone
  390. jmp -
  391. +
  392. ; Enable shot; set its position to player position.
  393. ; TODO(mcmillen): it might be easier/faster to keep N arrays: one for each
  394. ; field of shot (shotSpriteArray, shotXArray, shotYArray, ...)
  395. lda #8 ; Sprite number.
  396. sta 0, X
  397. lda playerX
  398. sta 1, X
  399. lda playerY
  400. sta 2, X
  401. lda #3 ; x-velocity.
  402. sta 3, X
  403. lda nextShotState
  404. cmp #1
  405. beq +
  406. lda #1
  407. sta 4, X
  408. inc nextShotState
  409. jmp ++
  410. +
  411. lda #-1
  412. sta 4, X
  413. dec nextShotState
  414. ++
  415. ; Set cooldown timer.
  416. lda #10
  417. sta shotCooldown
  418. MaybeShootDone:
  419. rts
  420. UpdateWorld:
  421. ; Update shot cooldown.
  422. lda shotCooldown
  423. cmp #0
  424. beq +
  425. dec A
  426. sta shotCooldown
  427. +
  428. ldx #0
  429. ; Update shot position.
  430. UpdateShot:
  431. lda shotArray, X
  432. cmp #0
  433. beq ShotDone
  434. ; Add to the x-coordinate. If the carry bit is set, we went off the edge
  435. ; of the screen, so disable the shot.
  436. lda shotArray + 3, X ; x-velocity.
  437. sta $00
  438. lda shotArray + 1, X
  439. clc
  440. adc $00
  441. bcs DisableShot
  442. sta shotArray + 1, X ; Store new x-coord.
  443. UpdateShotY:
  444. ; Add to the y-coordinate.
  445. lda shotArray + 4, X ; y-velocity.
  446. sta $00
  447. bit #%10000000 ; Check whether the velocity is negative.
  448. bne UpdateShotWithNegativeYVelocity
  449. lda shotArray + 2, X
  450. adc $00
  451. cmp #224
  452. bcs DisableShot
  453. sta shotArray + 2, X ; Store new y-coord.
  454. jmp ShotDone
  455. UpdateShotWithNegativeYVelocity:
  456. lda shotArray + 2, X ; Current Y.
  457. cmp #224
  458. bcs + ; If the shot was "off the top" before moving, maybe we'll reap it.
  459. adc $00 ; Otherwise, just update it,
  460. sta shotArray + 2, X ; save the result,
  461. jmp ShotDone ; and we know it shouldn't be reaped.
  462. +
  463. adc $00
  464. cmp #224
  465. bcc DisableShot ; If it's now wrapped around, reap it.
  466. sta shotArray + 2, X
  467. jmp ShotDone
  468. DisableShot:
  469. stz shotArray, X
  470. ShotDone:
  471. ; TODO(mcmillen): in places where we .rept inx (etc), is it faster to use
  472. ; actual addition?
  473. .rept shotSize
  474. inx
  475. .endr
  476. cpx #(shotArrayLength * shotSize)
  477. bne UpdateShot
  478. ; Make the background scroll. Horizontal over time; vertical depending on
  479. ; player's y-coordinate.
  480. lda vBlankCounter
  481. sta BG3HOFS
  482. lda vBlankCounter + 1
  483. sta BG3HOFS
  484. lda playerY
  485. .rept 3
  486. lsr
  487. .endr
  488. sta BG3VOFS
  489. stz BG3VOFS
  490. rts
  491. UpdateSprites:
  492. ; Zero out the scratch space for the secondary sprite table.
  493. ldx #0
  494. -
  495. stz spriteTableScratchStart, X
  496. inx
  497. cpx #numSprites
  498. bne -
  499. ldx #0 ; Index into sprite table 1.
  500. ldy #0 ; Index into sprite table 2.
  501. ; Copy player coords into sprite table.
  502. lda playerX
  503. sta spriteTableStart, X
  504. lda playerY
  505. sta spriteTableStart + 1, X
  506. lda #0
  507. sta spriteTableStart + 2, X
  508. ; Set priority bits so that the sprite is drawn in front.
  509. lda #%00110000
  510. sta spriteTableStart + 3, X
  511. lda #%11000000 ; Enable large sprite.
  512. sta spriteTableScratchStart, Y
  513. .rept 4
  514. inx
  515. .endr
  516. iny
  517. ; Now add shots.
  518. sty $00 ; Save sprite table 2 index.
  519. ldy #0 ; Index into shotArray.
  520. -
  521. lda shotArray, Y
  522. cmp #0
  523. beq + ; If not enabled, skip to next shot.
  524. ; Update sprite table 1.
  525. sta spriteTableStart + 2, X ; sprite number
  526. lda shotArray + 1, Y
  527. sta spriteTableStart, X ; x
  528. lda shotArray + 2, Y
  529. sta spriteTableStart + 1, X ; y
  530. ; Update secondary sprite table.
  531. phy ; Save shotArray index.
  532. ldy $00
  533. lda #%11000000
  534. sta spriteTableScratchStart, Y
  535. iny
  536. sty $00
  537. ply ; Restore shotArrayIndex.
  538. .rept 4
  539. inx
  540. .endr
  541. +
  542. .rept shotSize
  543. iny
  544. .endr
  545. cpy #(shotArrayLength * shotSize)
  546. bne -
  547. rts
  548. FillSecondarySpriteTable:
  549. ; The secondary sprite table wants 2 bits for each sprite: one to set the
  550. ; sprite's size, and one that's the high bit of the sprite's x-coordinate.
  551. ; It's annoying to deal with bitfields when thinking about business logic,
  552. ; so the spriteTableScratch array contains one byte for each sprite, in
  553. ; which the two most significant bits are the "size" and "upper x" bits.
  554. ; This function is meant to be called after UpdateWorld, and packs those
  555. ; bytes into the actual bitfield that the OAM wants for the secondary
  556. ; sprite table.
  557. ;
  558. ; The expected format of every byte in the scratch sprite table is:
  559. ; sx------ s = size (0 = small, 1 = large)
  560. ; x = flipped high x-coordinate (so 1 behaves like "enable").
  561. ldx #0 ; Index into input table.
  562. ldy #0 ; Index into output table.
  563. -
  564. stz $00 ; Current byte; filled out by a set of 4 input table entries.
  565. .rept 4
  566. ; For each byte, the lower-order bits correspond to the lower-numbered
  567. ; sprites; therefore we insert the current sprite's bits "at the top"
  568. ; and shift them right for each successive sprite.
  569. lsr $00
  570. lsr $00
  571. lda spriteTableScratchStart, X
  572. ora $00
  573. sta $00
  574. inx
  575. .endr
  576. lda $00
  577. eor #%01010101
  578. sta spriteTable2Start, Y
  579. iny
  580. cpx #numSprites
  581. bne -
  582. rts
  583. SetBackgroundColor:
  584. ; The background-color bytes are (R, G, B), each ranging from [0-31].
  585. ; The palette color format is 15-bit: [0bbbbbgg][gggrrrrr]
  586. ; Set the background color.
  587. ; Entry 0 corresponds to the SNES background color.
  588. stz CGADDR
  589. ; Compute and the low-order byte and store it in CGDATA.
  590. lda backgroundGreen
  591. .rept 5
  592. asl
  593. .endr
  594. ora backgroundRed
  595. sta CGDATA
  596. ; Compute the high-order byte and store it in CGDATA.
  597. lda backgroundBlue
  598. .rept 2
  599. asl
  600. .endr
  601. sta $00
  602. lda backgroundGreen
  603. .rept 3
  604. lsr
  605. .endr
  606. ora $00
  607. sta CGDATA
  608. rts
  609. VBlankHandler:
  610. jsr VBlankCounter
  611. jsr DMASpriteTables
  612. rti
  613. VBlankCounter:
  614. ; Increment a counter of how many VBlanks we've done.
  615. ; This is a 24-bit counter. At 60 vblanks/second, this will take
  616. ; 77 hours to wrap around; that's good enough for me :)
  617. inc vBlankCounter
  618. bne +
  619. inc vBlankCounter + 1
  620. bne +
  621. inc vBlankCounter + 2
  622. +
  623. rts
  624. DMASpriteTables:
  625. ; Store at the base OAM address.
  626. ldx #$0000
  627. stx OAMADDR
  628. ; Default DMA control; destination $2104 (OAM data register).
  629. stz DMA0CTRL
  630. lda #$04
  631. sta DMA0DST
  632. ; Our sprites start at $0100 in bank 0 and are #$220 bytes long.
  633. ldx #spriteTableStart
  634. stx DMA0SRC
  635. stz DMA0SRCBANK
  636. ldx #spriteTableSize
  637. stx DMA0SIZE
  638. ; Kick off the DMA transfer.
  639. lda #%00000001
  640. sta DMAENABLE
  641. rts
  642. .ENDS
  643. ; Bank 1 is used for our graphics assets.
  644. .BANK 1 SLOT 0
  645. .ORG 0
  646. .SECTION "GraphicsData"
  647. SpriteData:
  648. .INCBIN "sprites32.pic"
  649. SpritePalette:
  650. .INCBIN "sprites32.clr"
  651. TileData:
  652. .INCBIN "tiles.pic"
  653. TilePalette:
  654. .INCBIN "tiles.clr"
  655. .ENDS
  656. ; Fill an entire bank with random numbers.
  657. .SEED 1
  658. .BANK 2 SLOT 0
  659. .ORG 0
  660. .SECTION "RandomBytes"
  661. .DBRND 32 * 1024, 0, 255
  662. .ENDS