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.

1438 lines
30 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
  1. .INCLUDE "header.asm"
  2. .INCLUDE "init.asm"
  3. .INCLUDE "registers.asm"
  4. .INCLUDE "memory.asm"
  5. ; TODO: define screen / ship / shot dimensions as constants.
  6. ; Sets A to 8-bit (& enables 8-bit "B" register).
  7. .MACRO SetA8Bit
  8. sep #%00100000 ; 8-bit A/B.
  9. .ENDM
  10. ; Sets A to 16-bit.
  11. .MACRO SetA16Bit
  12. rep #%00100000 ; 16-bit A.
  13. .ENDM
  14. ; Sets X/Y to 16-bit.
  15. .MACRO SetXY16Bit
  16. rep #%00010000 ; 16-bit X/Y.
  17. .ENDM
  18. ; Stores result to A.
  19. ; Assumes 16-bit X & 8-bit A.
  20. ; Modifies X.
  21. ; Updates randomBytePtr.
  22. .MACRO GetRandomByte
  23. ldx randomBytePtr
  24. lda $028000, X ; $028000: beginning of ROM bank 2.
  25. inx
  26. cpx #$8000 ; This is the size of the entire ROM bank.
  27. bne +++
  28. ldx #0
  29. +++
  30. stx randomBytePtr
  31. .ENDM
  32. ; Modifies X and Y to move to the next elements in the sprite tables.
  33. .MACRO AdvanceSpritePointers
  34. .rept 4
  35. inx
  36. .endr
  37. iny
  38. .ENDM
  39. .BANK 0 SLOT 0
  40. .ORG 0
  41. .SECTION "MainCode"
  42. Start:
  43. InitSNES
  44. ; By default we assume 16-bit X/Y and 8-bit A.
  45. ; If any code wants to change this, it's expected to do so itself,
  46. ; and to change them back to the defaults before returning.
  47. SetXY16Bit
  48. SetA8Bit
  49. jsr LoadPaletteAndTileData
  50. jsr InitWorld
  51. ; Set screen mode: 16x16 tiles for backgrounds, mode 1.
  52. lda #%11000001
  53. sta BGMODE
  54. ; Set sprite size to 8x8 (small) and 32x32 (large).
  55. lda #%00100000
  56. sta OAMSIZE
  57. ; Main screen: enable sprites & BG3.
  58. lda #%00010100
  59. sta MSENABLE
  60. ; Turn on the screen.
  61. ; Format: x000bbbb
  62. ; x: 0 = screen on, 1 = screen off, bbbb: Brightness ($0-$F)
  63. lda #%00001111
  64. sta INIDISP
  65. jmp MainLoop
  66. LoadPaletteAndTileData:
  67. ; For more details on DMA, see:
  68. ; http://wiki.superfamicom.org/snes/show/Grog%27s+Guide+to+DMA+and+HDMA+on+the+SNES
  69. ; http://wiki.superfamicom.org/snes/show/Making+a+Small+Game+-+Tic-Tac-Toe
  70. ;
  71. ; A lot of the graphics-related registers are explained in Qwertie's doc:
  72. ; http://emu-docs.org/Super%20NES/General/snesdoc.html
  73. ; ... but be careful, because there are some errors in this doc.
  74. ;
  75. ; bazz's tutorial (available from http://wiki.superfamicom.org/snes/) is
  76. ; quite helpful with palette / sprites / DMA, especially starting at
  77. ; http://wiki.superfamicom.org/snes/show/Working+with+VRAM+-+Loading+the+Palette
  78. ; Initialize the palette memory in a loop.
  79. ; We could also do this with a DMA transfer (like we do with the tile data
  80. ; below), but it seems overkill for just a few bytes. :)
  81. ; TODO: do it with a DMA transfer.
  82. ; First, sprite palette data:
  83. ldx #0
  84. lda #128 ; Palette entries for sprites start at 128.
  85. sta CGADDR
  86. -
  87. lda.l SpritePalette, X
  88. sta CGDATA
  89. inx
  90. cpx #32 ; 32 bytes of palette data.
  91. bne -
  92. ; Now, BG3 palette data.
  93. ; Palette entries for BG3 start at 0.
  94. ldx #0
  95. lda #0
  96. sta CGADDR
  97. -
  98. lda.l TilePalette, X
  99. sta CGDATA
  100. inx
  101. cpx #8 ; 8 bytes of palette data.
  102. bne -
  103. ; TODO: make the "DMA stuff into VRAM" a macro or function.
  104. ; Set VMADDR to where we want the DMA to start. We'll store sprite data
  105. ; at the beginning of VRAM.
  106. ldx #$0000
  107. stx VMADDR
  108. ; DMA 0 source address & bank.
  109. ldx #SpriteData
  110. stx DMA0SRC
  111. lda #:SpriteData
  112. sta DMA0SRCBANK
  113. ; DMA 0 transfer size. Equal to the size of sprites32.pic.
  114. ldx #4096
  115. stx DMA0SIZE
  116. ; DMA 0 control register.
  117. ; Transfer type 001 = 2 addresses, LH.
  118. lda #%00000001
  119. sta DMA0CTRL
  120. ; DMA 0 destination.
  121. lda #$18 ; The upper byte is assumed to be $21, so this is $2118 & $2119.
  122. sta DMA0DST
  123. ; Enable DMA channel 0.
  124. lda #%00000001
  125. sta DMAENABLE
  126. ; Store background tile data at byte $2000 of VRAM.
  127. ; (VMADDR is a word address, so multiply by 2 to get the byte address.)
  128. ldx #$1000
  129. stx VMADDR
  130. ; DMA 0 source address & bank.
  131. ldx #TileData
  132. stx DMA0SRC
  133. lda #:TileData
  134. sta DMA0SRCBANK
  135. ; DMA 0 transfer size. Equal to the size of tiles.pic.
  136. ldx #512
  137. stx DMA0SIZE
  138. ; DMA 0 control register.
  139. ; Transfer type 001 = 2 addresses, LH.
  140. lda #%00000001
  141. sta DMA0CTRL
  142. ; DMA 0 destination.
  143. lda #$18 ; The upper byte is assumed to be $21, so this is $2118 & $2119.
  144. sta DMA0DST
  145. ; Enable DMA channel 0.
  146. lda #%00000001
  147. sta DMAENABLE
  148. ; Tell the system that the BG3 tilemap starts at $4000.
  149. lda #%00100000
  150. sta BG3TILEMAP
  151. ; ... and that the background tile data for BG3 starts at $2000.
  152. lda #%00000001
  153. sta BG34NBA
  154. ; Set up the BG3 tilemap.
  155. ; VRAM write mode: increments the address every time we write a word.
  156. lda #%10000000
  157. sta VMAIN
  158. ; Set word address for accessing VRAM.
  159. ldx #$2000 ; BG 3 tilemap starts here. (Byte address $4000.)
  160. stx VMADDR
  161. ; Now write entries into the tile map.
  162. ldy #0
  163. -
  164. GetRandomByte
  165. sta $00
  166. ldx #$0000 ; This is a blank tile.
  167. ; 1 in 8 chance that we choose a non-blank tile.
  168. bit #%00000111
  169. bne +
  170. ldx #$0002
  171. bit #%10000000
  172. bne +
  173. ldx #$8002 ; Flip vertically.
  174. +
  175. stx VMDATA
  176. iny
  177. ; The tile map is 32x32 (1024 entries).
  178. cpy #1024
  179. bne -
  180. rts
  181. InitWorld:
  182. ; Clear the memory that's used to keep track of normal game state.
  183. SetA16Bit
  184. lda #0
  185. ldx #$20
  186. -
  187. sta 0, X
  188. inx
  189. inx
  190. cpx #$1000
  191. bne -
  192. SetA8Bit
  193. ; Initial enemy ship-spawn cooldown.
  194. lda #30
  195. sta enemyShipSpawnCooldown
  196. ; Player's initial starting location and health.
  197. lda #(256 / 4)
  198. sta playerX
  199. lda #((224 - 32) / 2)
  200. sta playerY
  201. lda #16
  202. sta playerHealth
  203. ; (x-velocity, y-velocity) of 4 different player shot patterns.
  204. lda #6
  205. sta shotVelocityTable
  206. lda #0
  207. sta shotVelocityTable + 1
  208. lda #3
  209. sta shotVelocityTable + 2
  210. lda #3
  211. sta shotVelocityTable + 3
  212. lda #0
  213. sta shotVelocityTable + 4
  214. lda #6
  215. sta shotVelocityTable + 5
  216. lda #-3
  217. sta shotVelocityTable + 6
  218. lda #3
  219. sta shotVelocityTable + 7
  220. lda #-6
  221. sta shotVelocityTable + 8
  222. lda #0
  223. sta shotVelocityTable + 9
  224. lda #-3
  225. sta shotVelocityTable + 10
  226. lda #-3
  227. sta shotVelocityTable + 11
  228. lda #0
  229. sta shotVelocityTable + 12
  230. lda #-6
  231. sta shotVelocityTable + 13
  232. lda #3
  233. sta shotVelocityTable + 14
  234. lda #-3
  235. sta shotVelocityTable + 15
  236. rts
  237. MainLoop:
  238. lda #%10000001 ; Enable NMI interrupt & auto joypad read.
  239. sta NMITIMEN
  240. wai ; Wait for interrupt.
  241. lda #%00000001 ; Disable NMI interrupt while processing.
  242. sta NMITIMEN
  243. jsr JoypadRead
  244. lda playerHealth
  245. cmp #0
  246. beq +
  247. jsr UpdateWorld
  248. bra ++
  249. +
  250. jsr GameOver
  251. ++
  252. jsr UpdateSprites
  253. jsr FillSecondarySpriteTable
  254. jsr SetBackgroundColor
  255. bra MainLoop
  256. GameOver:
  257. ; Wait until the player hits Start.
  258. lda joy1 + 1
  259. bit #$10 ; Start
  260. beq +
  261. jsr InitWorld
  262. +
  263. rts
  264. JoypadRead:
  265. ; Load joypad registers into RAM for easy inspection & manipulation.
  266. -
  267. lda HVBJOY
  268. bit #$01 ; If auto-joypad read is happening, loop.
  269. bne -
  270. ldx JOY1L
  271. stx joy1
  272. ldx JOY2L
  273. stx joy2
  274. rts
  275. ; TODO: move functions around to be in a more sensible order.
  276. JoypadHandler:
  277. JoypadUp:
  278. lda joy1 + 1
  279. bit #$08 ; Up
  280. beq JoypadDown ; Button not pressed.
  281. lda playerY
  282. cmp #0
  283. beq JoypadDown ; Value saturated.
  284. dec playerY
  285. dec playerY
  286. JoypadDown:
  287. lda joy1 + 1
  288. bit #$04 ; Down
  289. beq JoypadLeft ; Button not pressed.
  290. lda playerY
  291. cmp #(224 - 32 - 8 - 4) ; player height, bottom status bar, bottom padding
  292. beq JoypadLeft ; Value saturated.
  293. inc playerY
  294. inc playerY
  295. JoypadLeft:
  296. lda joy1 + 1
  297. bit #$02 ; Left
  298. beq JoypadRight ; Button not pressed.
  299. lda playerX
  300. cmp #0
  301. beq JoypadRight ; Value saturated.
  302. dec playerX
  303. dec playerX
  304. JoypadRight:
  305. lda joy1 + 1
  306. bit #$01 ; Right
  307. beq JoypadY ; Button not pressed.
  308. lda playerX
  309. cmp #(256 - 32)
  310. beq JoypadY ; Value saturated.
  311. inc playerX
  312. inc playerX
  313. JoypadY:
  314. lda joy1 + 1
  315. bit #$40 ; Y
  316. beq JoypadX ; Button not pressed.
  317. lda backgroundRed
  318. cmp #0
  319. beq JoypadX ; Value saturated.
  320. dec backgroundRed
  321. JoypadX:
  322. lda joy1
  323. bit #$40 ; X
  324. beq JoypadL ; Button not pressed.
  325. lda backgroundRed
  326. cmp #31
  327. beq JoypadL ; Value saturated.
  328. inc backgroundRed
  329. JoypadL:
  330. lda joy1
  331. bit #$20 ; L
  332. beq JoypadR ; Button not pressed.
  333. lda backgroundBlue
  334. cmp #0
  335. beq JoypadR ; Value saturated.
  336. dec backgroundBlue
  337. JoypadR:
  338. lda joy1
  339. bit #$10 ; R
  340. beq JoypadB ; Button not pressed.
  341. lda backgroundBlue
  342. cmp #31
  343. beq JoypadB ; Value saturated.
  344. inc backgroundBlue
  345. JoypadB:
  346. lda joy1 + 1
  347. bit #$80 ; B
  348. beq JoypadDone
  349. jsr MaybeShoot
  350. JoypadDone:
  351. rts
  352. MaybeShoot:
  353. ; If the cooldown timer is non-zero, don't shoot.
  354. lda shotCooldown
  355. cmp #0
  356. bne MaybeShootDone
  357. ; Find the first empty spot in the shots array.
  358. ldx #playerShotArray
  359. -
  360. lda 0, X
  361. cmp #0
  362. beq +
  363. .rept shotSize
  364. inx
  365. .endr
  366. ; If we went all the way to the end, bail out.
  367. cpx #(playerShotArray + playerShotArrayLength * shotSize)
  368. bne -
  369. rts
  370. +
  371. ; Enable shot; set its position based on player position.
  372. lda #8 ; Sprite number.
  373. sta 0, X
  374. lda playerX
  375. clc
  376. adc #28
  377. sta 1, X
  378. lda playerY
  379. clc
  380. adc #14
  381. sta 2, X
  382. ; Get x- and y-velocity out of shotVelocityTable.
  383. lda nextShotState
  384. and #%00000000 ; 8 possibilities if we use #%00000111.
  385. ldy #0
  386. -
  387. cmp #0
  388. beq +
  389. .rept 2
  390. iny
  391. .endr
  392. dec A
  393. bra -
  394. +
  395. inc nextShotState
  396. ; x-velocity.
  397. lda shotVelocityTable, Y
  398. sta 3, X
  399. ; y-velocity.
  400. lda shotVelocityTable + 1, Y
  401. sta 4, X
  402. ; Set cooldown timer.
  403. lda #8
  404. sta shotCooldown
  405. MaybeShootDone:
  406. rts
  407. UpdateWorld:
  408. jsr UpdateShotCooldown
  409. jsr UpdateShotPositions
  410. jsr JoypadHandler
  411. jsr SpawnEnemyShips
  412. jsr UpdateEnemyShips
  413. jsr CheckCollisionsWithPlayer
  414. jsr CheckCollisionsWithEnemies
  415. jsr UpdateBackgroundScroll
  416. rts
  417. UpdateShotCooldown:
  418. ; Update shot cooldown.
  419. lda shotCooldown
  420. cmp #0
  421. beq +
  422. dec A
  423. sta shotCooldown
  424. +
  425. rts
  426. SpawnEnemyShips:
  427. lda enemyShipSpawnCooldown
  428. cmp #0
  429. beq +
  430. dec A
  431. sta enemyShipSpawnCooldown
  432. rts
  433. +
  434. GetRandomByte
  435. and #%00111111
  436. clc
  437. adc #32
  438. sta enemyShipSpawnCooldown
  439. ; Find an empty spot in the array.
  440. ldy #0
  441. -
  442. lda enemyShipArray, Y
  443. cmp #0
  444. beq +
  445. .rept enemyShipSize
  446. iny
  447. .endr
  448. cpy #(enemyShipArrayLength * enemyShipSize)
  449. bne -
  450. rts ; Too many ships; bail.
  451. +
  452. lda #4 ; Sprite number.
  453. sta enemyShipArray, Y
  454. lda #254
  455. sta enemyShipArray + 1, Y ; x.
  456. -
  457. GetRandomByte
  458. cmp #(224 - 32 - 8 - 4)
  459. bcs - ; Keep trying.
  460. sta enemyShipArray + 2, Y ; y.
  461. lda #0
  462. sta enemyShipArray + 3, Y ; move AI type.
  463. sta enemyShipArray + 4, Y ; shoot AI type.
  464. lda #12
  465. sta enemyShipArray + 5, Y ; shot cooldown.
  466. rts
  467. ; TODO: reap ships if they move off the top, bottom, or right too.
  468. UpdateEnemyShips:
  469. ldy #0 ; Index into enemyShipArray.
  470. sty $00 ; Index into enemyShotArray.
  471. --
  472. lda enemyShipArray, Y
  473. cmp #0 ; If it's not enabled, skip it.
  474. beq ++
  475. ; Move the ship.
  476. ; TODO: implement different movement based on AI-type.
  477. lda enemyShipArray + 1, Y ; x
  478. clc
  479. adc #-2 ; x-velocity.
  480. bcs +
  481. lda #0
  482. sta enemyShipArray, Y ; reap it.
  483. bra ++
  484. +
  485. sta enemyShipArray + 1, Y ; move it.
  486. lda enemyShipArray + 5, Y ; shot cooldown
  487. cmp #0
  488. beq +
  489. dec A
  490. sta enemyShipArray + 5, Y ; new shot cooldown
  491. bra ++
  492. + ; Add a shot.
  493. ; TODO: implement different shooting based on shoot-type.
  494. lda #12
  495. sta enemyShipArray + 5, Y ; new shot cooldown
  496. lda enemyShipArray + 1, Y
  497. sta $02 ; Enemy ship x.
  498. lda enemyShipArray + 2, Y
  499. sta $03 ; Enemy ship y.
  500. phy
  501. ldy $00
  502. jsr SpawnEnemyShot
  503. sty $00
  504. ply
  505. ++ ; Done processing this ship.
  506. .rept enemyShipSize
  507. iny
  508. .endr
  509. cpy #(enemyShipArrayLength * enemyShipSize)
  510. bne --
  511. rts
  512. ; Expects:
  513. ; Y: index into enemyShotArray (bytes).
  514. ; $02: enemy ship x-position.
  515. ; $03: enemy ship y-position.
  516. ;
  517. ; Modifies:
  518. ; A.
  519. ; Y: to point at the next possible free index into enemyShotArray.
  520. SpawnEnemyShot:
  521. -
  522. ; Bail if at end of array.
  523. cpy #(enemyShotArrayLength * shotSize)
  524. bne +
  525. rts
  526. +
  527. lda enemyShotArray, Y
  528. cmp #0
  529. beq +
  530. ; Try next slot.
  531. .rept shotSize
  532. iny
  533. .endr
  534. bra -
  535. +
  536. ; OK, found a spot.
  537. lda #9 ; Sprite number.
  538. sta enemyShotArray, Y
  539. lda $02 ; Get enemy x.
  540. sta enemyShotArray + 1, Y ; Save as shot x.
  541. lda $03 ; Get enemy y.
  542. clc
  543. adc #((32 - 4) / 2) ; Center it with enemy ship.
  544. sta enemyShotArray + 2, Y ; Save as shot y.
  545. lda #-6
  546. sta enemyShotArray + 3, Y ; x-velocity.
  547. lda #0
  548. sta enemyShotArray + 4, Y ; y-velocity.
  549. rts
  550. UpdateShotPositions:
  551. ldx #0
  552. UpdateShot: ; Updates position of one shot.
  553. lda playerShotArray, X
  554. cmp #0
  555. beq ShotDone
  556. ; Add to the x-coordinate. If the carry bit is set, we went off the edge
  557. ; of the screen, so disable the shot.
  558. lda playerShotArray + 3, X ; x-velocity.
  559. sta $00
  560. bit #%10000000 ; Check whether the velocity is negative.
  561. bne UpdateShotWithNegativeXVelocity
  562. lda playerShotArray + 1, X
  563. clc
  564. adc $00
  565. bcs DisableShot
  566. sta playerShotArray + 1, X ; Store new x-coord.
  567. bra UpdateShotY
  568. UpdateShotWithNegativeXVelocity:
  569. ; TODO: wrap sprites when they go negative here, like we do with
  570. ; y-velocities.
  571. lda playerShotArray + 1, X ; Current x.
  572. clc
  573. adc $00
  574. bcc DisableShot
  575. sta playerShotArray + 1, X
  576. UpdateShotY:
  577. ; Add to the y-coordinate.
  578. lda playerShotArray + 4, X ; y-velocity.
  579. sta $00
  580. bit #%10000000 ; Check whether the velocity is negative.
  581. bne UpdateShotWithNegativeYVelocity
  582. lda playerShotArray + 2, X
  583. clc
  584. adc $00
  585. cmp #224
  586. bcs DisableShot
  587. sta playerShotArray + 2, X ; Store new y-coord.
  588. bra ShotDone
  589. UpdateShotWithNegativeYVelocity:
  590. lda playerShotArray + 2, X ; Current y.
  591. cmp #224
  592. bcs + ; If the shot was "off the top" before moving, maybe we'll reap it.
  593. adc $00 ; Otherwise, just update it,
  594. sta playerShotArray + 2, X ; save the result,
  595. bra ShotDone ; and we know it shouldn't be reaped.
  596. +
  597. clc
  598. adc $00
  599. cmp #224
  600. bcc DisableShot ; If it's now wrapped around, reap it.
  601. sta playerShotArray + 2, X
  602. bra ShotDone
  603. DisableShot:
  604. stz playerShotArray, X
  605. ShotDone:
  606. ; TODO: in places where we .rept inx (etc), is it faster to use actual
  607. ; addition?
  608. .rept shotSize
  609. inx
  610. .endr
  611. cpx #((playerShotArrayLength + enemyShotArrayLength) * shotSize)
  612. bne UpdateShot
  613. rts
  614. ; Expects:
  615. ; $00: x-coordinate of ship's center.
  616. ; $01: y-coordinate of ship's center.
  617. ; $02: half of the shot's size.
  618. ; $03: x-coordinate of shot's upper-left.
  619. ; $04: y-coordinate of shot's upper-left.
  620. ;
  621. ; Modifies:
  622. ; $05
  623. ; A: set to non-zero if there was a collision, zero otherwise.
  624. CheckCollision:
  625. lda $03
  626. clc
  627. adc $02 ; Get the center of the shot.
  628. sbc $00
  629. bpl + ; If the result is positive, great!
  630. eor #$ff ; Otherwise, negate it.
  631. inc A
  632. +
  633. ; A now contains dx, guaranteed to be positive.
  634. cmp #18 ; Threshold for "successful hit".
  635. bcc +
  636. lda #0 ; Already too far to be a hit; bail.
  637. rts
  638. +
  639. sta $05 ; Save dx for later.
  640. ; Find dy.
  641. lda $04
  642. clc
  643. adc $02 ; Get the center of the shot.
  644. sbc $01
  645. bpl + ; If the result is positive, great!
  646. eor #$ff ; Otherwise, negate it.
  647. inc A
  648. +
  649. ; A now contains dy, guaranteed to be positive.
  650. clc
  651. adc $05 ; Add dx.
  652. cmp #18 ; Threshold for "successful hit".
  653. lda #0
  654. bcs +
  655. lda #1 ; Got a hit.
  656. +
  657. rts
  658. CheckCollisionsWithPlayer:
  659. lda #2 ; Half of shot's size.
  660. sta $02
  661. ; Store player position statically.
  662. clc
  663. lda playerX
  664. adc #16 ; Can't overflow.
  665. sta $00 ; Store the center.
  666. lda playerY
  667. ; Store the center. Our ship is actually 31 pixels tall, so offsetting by
  668. ; 15 feels more "fair": a shot that hits the invisible bottom edge of the
  669. ; ship won't count as a hit.
  670. adc #15
  671. sta $01
  672. ldx #0
  673. -
  674. lda enemyShotArray, X
  675. cmp #0 ; Check whether it's active.
  676. beq +
  677. lda enemyShotArray + 1, X ; x.
  678. sta $03
  679. lda enemyShotArray + 2, X ; y.
  680. sta $04
  681. jsr CheckCollision
  682. cmp #0
  683. beq +
  684. ; OK, we got a hit! Disable the shot.
  685. lda #0
  686. sta enemyShotArray, X
  687. ; ... and decrement the player's life.
  688. lda playerHealth
  689. cmp #0
  690. beq +
  691. dec playerHealth
  692. +
  693. .rept shotSize
  694. inx
  695. .endr
  696. cpx #(enemyShotArrayLength * shotSize)
  697. bne -
  698. rts
  699. CheckCollisionsWithEnemies:
  700. lda #2 ; Half of shot's size.
  701. sta $02
  702. ldy #0 ; Index into enemyShipArray.
  703. --
  704. lda enemyShipArray, Y
  705. cmp #0 ; Check whether it's active.
  706. beq ++
  707. ; Store enemy position statically.
  708. clc
  709. lda enemyShipArray + 1, Y ; x.
  710. adc #16 ; Can't overflow.
  711. sta $00 ; Store the center.
  712. lda enemyShipArray + 2, Y ; y.
  713. adc #15
  714. sta $01 ; Store the center.
  715. ldx #0 ; Index into playerShotArray.
  716. -
  717. lda playerShotArray, X
  718. cmp #0
  719. beq +
  720. lda playerShotArray + 1, X ; x.
  721. sta $03
  722. lda playerShotArray + 2, X ; y.
  723. sta $04
  724. jsr CheckCollision
  725. cmp #0
  726. beq +
  727. ; OK, we got a hit! Disable the shot.
  728. lda #0
  729. sta playerShotArray, X
  730. ; ... and also the enemy ship.
  731. sta enemyShipArray, Y
  732. ; Give that player some points. Players love points.
  733. SetA16Bit
  734. sed ; Set decimal mode.
  735. lda playerScore
  736. clc
  737. adc #1 ; We can't just "inc"; it doesn't know about decimal mode.
  738. sta playerScore
  739. cld ; Clear decimal mode.
  740. SetA8Bit
  741. bra ++ ; ... we're done with this ship; no need to check more shots.
  742. +
  743. .rept shotSize
  744. inx
  745. .endr
  746. cpx #(playerShotArrayLength * shotSize)
  747. bne -
  748. ++
  749. .rept enemyShipSize
  750. iny
  751. .endr
  752. cpy #(enemyShipArrayLength * enemyShipSize)
  753. bne --
  754. rts
  755. UpdateBackgroundScroll:
  756. ; Make the background scroll. Horizontal over time; vertical depending on
  757. ; player's y-coordinate.
  758. lda vBlankCounter
  759. sta BG3HOFS
  760. lda vBlankCounter + 1
  761. sta BG3HOFS
  762. lda playerY
  763. .rept 3
  764. lsr
  765. .endr
  766. sta BG3VOFS
  767. stz BG3VOFS
  768. rts
  769. UpdateSprites: ; TODO: refactor into smaller pieces.
  770. ; This page is a good reference on SNES sprite formats:
  771. ; http://wiki.superfamicom.org/snes/show/SNES+Sprites
  772. ; It uses the same approach we're using, in which we keep a buffer of the
  773. ; sprite tables in RAM, and DMA the sprite tables to the system's OAM
  774. ; during VBlank.
  775. ; Sprite table 1 has 4 bytes per sprite, laid out as follows:
  776. ; Byte 1: xxxxxxxx x: X coordinate
  777. ; Byte 2: yyyyyyyy y: Y coordinate
  778. ; Byte 3: cccccccc c: Starting tile #
  779. ; Byte 4: vhoopppc v: vertical flip h: horizontal flip o: priority bits
  780. ; p: palette #
  781. ; Sprite table 2 has 2 bits per sprite, like so:
  782. ; bits 0,2,4,6 - High bit of the sprite's x-coordinate.
  783. ; bits 1,3,5,7 - Toggle Sprite size: 0 - small size 1 - large size
  784. ; Setting all the high bits keeps the sprites offscreen.
  785. ; Zero out the scratch space for the secondary sprite table.
  786. ldx #0
  787. -
  788. stz spriteTableScratchStart, X
  789. inx
  790. cpx #numSprites
  791. bne -
  792. ldx #0 ; Index into sprite table 1.
  793. ldy #0 ; Index into sprite table 2.
  794. jsr MaybeGameOver
  795. ; Copy player coords into sprite table.
  796. lda playerX
  797. sta spriteTableStart, X
  798. lda playerY
  799. sta spriteTableStart + 1, X
  800. lda #0
  801. sta spriteTableStart + 2, X
  802. ; Set priority bits so that the sprite is drawn in front.
  803. lda #%00010000
  804. sta spriteTableStart + 3, X
  805. lda #%11000000 ; Enable large sprite.
  806. sta spriteTableScratchStart, Y
  807. AdvanceSpritePointers
  808. ; Now add enemy ships.
  809. sty $00 ; Save sprite table 2 index.
  810. ldy #0 ; Index into enemyShipArray.
  811. -
  812. lda enemyShipArray, Y
  813. cmp #0 ; If not enabled, skip to next ship.
  814. beq +
  815. ; Update sprite table 1.
  816. sta spriteTableStart + 2, X ; sprite number
  817. lda enemyShipArray + 1, Y
  818. sta spriteTableStart, X ; x
  819. lda enemyShipArray + 2, Y
  820. sta spriteTableStart + 1, X ; y
  821. lda #%01000000 ; flip horizontally.
  822. sta spriteTableStart + 3, X
  823. ; Update secondary sprite table.
  824. phy ; Save enemyShipArray index.
  825. ldy $00
  826. lda #%11000000 ; Enable large sprite.
  827. sta spriteTableScratchStart, Y
  828. AdvanceSpritePointers
  829. sty $00
  830. ply ; Restore enemyShipArray index.
  831. +
  832. .rept enemyShipSize
  833. iny
  834. .endr
  835. cpy #(enemyShipArrayLength * enemyShipSize)
  836. bne -
  837. ldy $00 ; Restore Y to its rightful self.
  838. ; Now add shots.
  839. sty $00 ; Save sprite table 2 index.
  840. ldy #0 ; Index into playerShotArray.
  841. -
  842. lda playerShotArray, Y
  843. cmp #0
  844. beq + ; If not enabled, skip to next shot.
  845. ; Update sprite table 1.
  846. sta spriteTableStart + 2, X ; sprite number
  847. lda playerShotArray + 1, Y
  848. sta spriteTableStart, X ; x
  849. lda playerShotArray + 2, Y
  850. sta spriteTableStart + 1, X ; y
  851. ; Update secondary sprite table.
  852. phy ; Save playerShotArray index.
  853. ldy $00
  854. lda #%01000000 ; Enable small sprite.
  855. sta spriteTableScratchStart, Y
  856. AdvanceSpritePointers
  857. sty $00
  858. ply ; Restore playerShotArray index.
  859. +
  860. .rept shotSize
  861. iny
  862. .endr
  863. cpy #((playerShotArrayLength + enemyShotArrayLength) * shotSize)
  864. bne -
  865. ldy $00 ; Restore Y to its rightful self.
  866. ; Now add sprites to show player health.
  867. stz $01
  868. lda #4
  869. sta $02
  870. -
  871. lda $01
  872. cmp playerHealth
  873. beq + ; All done?
  874. lda #10
  875. sta spriteTableStart + 2, X ; sprite number
  876. lda $02
  877. sta spriteTableStart, X ; x
  878. clc
  879. adc #7
  880. sta $02
  881. lda #212
  882. sta spriteTableStart + 1, X ; y
  883. ; Set priority bits so that the sprite is drawn in front.
  884. lda #%00110000
  885. sta spriteTableStart + 3, X
  886. lda #%01000000 ; Enable small sprite.
  887. sta spriteTableScratchStart, Y
  888. AdvanceSpritePointers
  889. inc $01
  890. bra -
  891. +
  892. ; Sprites to show player score.
  893. lda #(252 - 7 * 6)
  894. sta $00 ; x-position
  895. lda #212
  896. sta $01 ; y-position
  897. stz $02 ; Don't render leading zeroes.
  898. stz $03 ; ... not even for the second digit.
  899. lda playerScore + 1
  900. jsr RenderTwoDigits
  901. lda playerScore
  902. jsr RenderTwoDigits
  903. inc $03 ; Render rightmost zero always.
  904. lda #0
  905. jsr RenderTwoDigits
  906. ; Now clear out the unused entries in the sprite table.
  907. -
  908. cpx #spriteTable1Size
  909. beq +
  910. lda #1
  911. sta spriteTableStart, X
  912. AdvanceSpritePointers
  913. bra -
  914. +
  915. rts
  916. MaybeGameOver:
  917. lda playerHealth
  918. cmp #0
  919. beq +
  920. rts
  921. +
  922. ; Sprites to show "Game Over" text.
  923. lda #80 ; G
  924. sta spriteTableStart + 2, X
  925. lda #112
  926. sta spriteTableStart, X
  927. lda #104
  928. sta spriteTableStart + 1, X
  929. lda #%00110000
  930. sta spriteTableStart + 3, X
  931. lda #%01000000 ; Enable small sprite.
  932. sta spriteTableScratchStart, Y
  933. AdvanceSpritePointers
  934. lda #81 ; A
  935. sta spriteTableStart + 2, X
  936. lda #120
  937. sta spriteTableStart, X
  938. lda #104
  939. sta spriteTableStart + 1, X
  940. lda #%00110000
  941. sta spriteTableStart + 3, X
  942. lda #%01000000 ; Enable small sprite.
  943. sta spriteTableScratchStart, Y
  944. AdvanceSpritePointers
  945. lda #82 ; M
  946. sta spriteTableStart + 2, X
  947. lda #128
  948. sta spriteTableStart, X
  949. lda #104
  950. sta spriteTableStart + 1, X
  951. lda #%00110000
  952. sta spriteTableStart + 3, X
  953. lda #%01000000 ; Enable small sprite.
  954. sta spriteTableScratchStart, Y
  955. AdvanceSpritePointers
  956. lda #83 ; E
  957. sta spriteTableStart + 2, X
  958. lda #136
  959. sta spriteTableStart, X
  960. lda #104
  961. sta spriteTableStart + 1, X
  962. lda #%00110000
  963. sta spriteTableStart + 3, X
  964. lda #%01000000 ; Enable small sprite.
  965. sta spriteTableScratchStart, Y
  966. AdvanceSpritePointers
  967. lda #84 ; O
  968. sta spriteTableStart + 2, X
  969. lda #112
  970. sta spriteTableStart, X
  971. lda #114
  972. sta spriteTableStart + 1, X
  973. lda #%00110000
  974. sta spriteTableStart + 3, X
  975. lda #%01000000 ; Enable small sprite.
  976. sta spriteTableScratchStart, Y
  977. AdvanceSpritePointers
  978. lda #85 ; V
  979. sta spriteTableStart + 2, X
  980. lda #120
  981. sta spriteTableStart, X
  982. lda #114
  983. sta spriteTableStart + 1, X
  984. lda #%00110000
  985. sta spriteTableStart + 3, X
  986. lda #%01000000 ; Enable small sprite.
  987. sta spriteTableScratchStart, Y
  988. AdvanceSpritePointers
  989. lda #83 ; E
  990. sta spriteTableStart + 2, X
  991. lda #128
  992. sta spriteTableStart, X
  993. lda #114
  994. sta spriteTableStart + 1, X
  995. lda #%00110000
  996. sta spriteTableStart + 3, X
  997. lda #%01000000 ; Enable small sprite.
  998. sta spriteTableScratchStart, Y
  999. AdvanceSpritePointers
  1000. lda #86 ; R
  1001. sta spriteTableStart + 2, X
  1002. lda #136
  1003. sta spriteTableStart, X
  1004. lda #114
  1005. sta spriteTableStart + 1, X
  1006. lda #%00110000
  1007. sta spriteTableStart + 3, X
  1008. lda #%01000000 ; Enable small sprite.
  1009. sta spriteTableScratchStart, Y
  1010. AdvanceSpritePointers
  1011. rts
  1012. ; Expects:
  1013. ; A: number to display (a byte where each nybble is from 0-9).
  1014. ; X/Y: pointing at appropriate locations into the sprite tables.
  1015. ; $00: x-position to render the leftmost digit into.
  1016. ; $01: y-position to render the leftmost digit into.
  1017. ; $02: if set, render leading zeroes.
  1018. ; $03: if set, always render the zero for the low-order digit.
  1019. ;
  1020. ; Updates:
  1021. ; X & Y to point at the next locations in the sprite tables.
  1022. ; The sprite tables, to add (up to) 2 sprites for digits.
  1023. ; $00: x-position to render additional digits into.
  1024. ; $02: whether to render leading zeroes.
  1025. ; $04-$06 (scratch).
  1026. RenderTwoDigits:
  1027. ; Store the high digit in $05 and the low digit in $06.
  1028. sta $06
  1029. .rept 4
  1030. lsr
  1031. .endr
  1032. sta $05
  1033. lda $06
  1034. and #$0F
  1035. sta $06
  1036. ; Render the first digit.
  1037. lda $05
  1038. jsr RenderDigit
  1039. lda $00
  1040. clc
  1041. adc #7
  1042. sta $00
  1043. ; Set "render zero" for rightmost digit to true if requested.
  1044. lda $02
  1045. ora $03
  1046. sta $02
  1047. ; Render the second digit.
  1048. lda $06
  1049. jsr RenderDigit
  1050. lda $00
  1051. clc
  1052. adc #7
  1053. sta $00
  1054. rts
  1055. ; Expects:
  1056. ; A: number to display (from 0-9).
  1057. ; X/Y: pointing at appropriate locations into the sprite tables.
  1058. ; $00: x-position to render the digit into.
  1059. ; $01: y-position to render the digit into.
  1060. ; $02: whether to render if the number is zero.
  1061. ;
  1062. ; Updates:
  1063. ; X & Y to point at the next locations in the sprite tables.
  1064. ; The sprite tables, to add (up to) 1 sprite for digits.
  1065. ; $02: whether to render further leading zeroes.
  1066. ; $04 (scratch).
  1067. RenderDigit:
  1068. sta $04
  1069. cmp #0
  1070. bne + ; Non-zero: render it regardless.
  1071. lda $02
  1072. cmp #0
  1073. bne + ; Render because "render zeroes" is set.
  1074. rts ; Nothing to render.
  1075. +
  1076. lda #1
  1077. sta $02 ; Render leading zeroes from here on out.
  1078. lda $04
  1079. clc
  1080. adc #64 ; Base index of digit sprites.
  1081. sta spriteTableStart + 2, X ; sprite number
  1082. lda $00
  1083. sta spriteTableStart, X ; x
  1084. lda $01
  1085. sta spriteTableStart + 1, X ; y
  1086. ; Set priority bits so that the sprite is drawn in front.
  1087. lda #%00110000
  1088. sta spriteTableStart + 3, X
  1089. lda #%01000000 ; Enable small sprite.
  1090. sta spriteTableScratchStart, Y
  1091. AdvanceSpritePointers
  1092. rts
  1093. FillSecondarySpriteTable:
  1094. ; The secondary sprite table wants 2 bits for each sprite: one to set the
  1095. ; sprite's size, and one that's the high bit of the sprite's x-coordinate.
  1096. ; It's annoying to deal with bitfields when thinking about business logic,
  1097. ; so the spriteTableScratch array contains one byte for each sprite, in
  1098. ; which the two most significant bits are the "size" and "upper x" bits.
  1099. ; This function is meant to be called after UpdateWorld, and packs those
  1100. ; bytes into the actual bitfield that the OAM wants for the secondary
  1101. ; sprite table.
  1102. ;
  1103. ; The expected format of every byte in the scratch sprite table is:
  1104. ; sx------ s = size (0 = small, 1 = large)
  1105. ; x = flipped high x-coordinate (so 1 behaves like "enable").
  1106. ldx #0 ; Index into input table.
  1107. ldy #0 ; Index into output table.
  1108. -
  1109. stz $00 ; Current byte; filled out by a set of 4 input table entries.
  1110. .rept 4
  1111. ; For each byte, the lower-order bits correspond to the lower-numbered
  1112. ; sprites; therefore we insert the current sprite's bits "at the top"
  1113. ; and shift them right for each successive sprite.
  1114. lsr $00
  1115. lsr $00
  1116. lda spriteTableScratchStart, X
  1117. ora $00
  1118. sta $00
  1119. inx
  1120. .endr
  1121. lda $00
  1122. eor #%01010101
  1123. sta spriteTable2Start, Y
  1124. iny
  1125. cpx #numSprites
  1126. bne -
  1127. rts
  1128. SetBackgroundColor:
  1129. ; The background-color bytes are (R, G, B), each ranging from [0-31].
  1130. ; The palette color format is 15-bit: [0bbbbbgg][gggrrrrr]
  1131. ; Set the background color.
  1132. ; Entry 0 corresponds to the SNES background color.
  1133. stz CGADDR
  1134. ; Compute and the low-order byte and store it in CGDATA.
  1135. lda backgroundGreen
  1136. .rept 5
  1137. asl
  1138. .endr
  1139. ora backgroundRed
  1140. sta CGDATA
  1141. ; Compute the high-order byte and store it in CGDATA.
  1142. lda backgroundBlue
  1143. .rept 2
  1144. asl
  1145. .endr
  1146. sta $00
  1147. lda backgroundGreen
  1148. .rept 3
  1149. lsr
  1150. .endr
  1151. ora $00
  1152. sta CGDATA
  1153. rts
  1154. VBlankHandler:
  1155. jsr VBlankCounter
  1156. jsr DMASpriteTables
  1157. rti
  1158. VBlankCounter:
  1159. ; Increment a counter of how many VBlanks we've done.
  1160. ; This is a 24-bit counter. At 60 vblanks/second, this will take
  1161. ; 77 hours to wrap around; that's good enough for me :)
  1162. inc vBlankCounter
  1163. bne +
  1164. inc vBlankCounter + 1
  1165. bne +
  1166. inc vBlankCounter + 2
  1167. +
  1168. rts
  1169. DMASpriteTables:
  1170. ; Store at the base OAM address.
  1171. ldx #$0000
  1172. stx OAMADDR
  1173. ; Default DMA control; destination $2104 (OAM data register).
  1174. stz DMA0CTRL
  1175. lda #$04
  1176. sta DMA0DST
  1177. ; Our sprites start at $0100 in bank 0 and are #$220 bytes long.
  1178. ldx #spriteTableStart
  1179. stx DMA0SRC
  1180. stz DMA0SRCBANK
  1181. ldx #spriteTableSize
  1182. stx DMA0SIZE
  1183. ; Kick off the DMA transfer.
  1184. lda #%00000001
  1185. sta DMAENABLE
  1186. rts
  1187. .ENDS
  1188. ; Bank 1 is used for our graphics assets.
  1189. .BANK 1 SLOT 0
  1190. .ORG 0
  1191. .SECTION "GraphicsData"
  1192. SpriteData:
  1193. .INCBIN "sprites32.pic"
  1194. SpritePalette:
  1195. .INCBIN "sprites32.clr"
  1196. TileData:
  1197. .INCBIN "tiles.pic"
  1198. TilePalette:
  1199. .INCBIN "tiles.clr"
  1200. .ENDS
  1201. ; Fill an entire bank with random numbers.
  1202. .SEED 1
  1203. .BANK 2 SLOT 0
  1204. .ORG 0
  1205. .SECTION "RandomBytes"
  1206. .DBRND 32 * 1024, 0, 255
  1207. .ENDS