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.

968 lines
20 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. .INCLUDE "memory.asm"
  5. ; Sets A to 8-bit (& enables 8-bit "B" register).
  6. .MACRO SetA8Bit
  7. sep #%00100000 ; 8-bit A/B.
  8. .ENDM
  9. ; Sets A to 16-bit.
  10. .MACRO SetA16Bit
  11. rep #%00100000 ; 16-bit A.
  12. .ENDM
  13. ; Sets X/Y to 16-bit.
  14. .MACRO SetXY16Bit
  15. rep #%00010000 ; 16-bit X/Y.
  16. .ENDM
  17. ; Stores result to A.
  18. ; Assumes 16-bit X & 8-bit A.
  19. ; Modifies X.
  20. ; Updates randomBytePtr.
  21. .MACRO GetRandomByte
  22. ldx randomBytePtr
  23. lda $028000, X ; $028000: beginning of ROM bank 2.
  24. inx
  25. cpx #$8000 ; This is the size of the entire ROM bank.
  26. bne +++
  27. ldx #0
  28. +++
  29. stx randomBytePtr
  30. .ENDM
  31. .BANK 0 SLOT 0
  32. .ORG 0
  33. .SECTION "MainCode"
  34. Start:
  35. InitSNES
  36. ; By default we assume 16-bit X/Y and 8-bit A.
  37. ; If any code wants to change this, it's expected to do so itself,
  38. ; and to change them back to the defaults before returning.
  39. SetXY16Bit
  40. SetA8Bit
  41. jsr LoadPaletteAndTileData
  42. jsr InitWorld
  43. ; Set screen mode: 16x16 tiles for backgrounds, mode 1.
  44. lda #%11000001
  45. sta BGMODE
  46. ; Set sprite size to 8x8 (small) and 32x32 (large).
  47. lda #%00100000
  48. sta OAMSIZE
  49. ; Main screen: enable sprites & BG3.
  50. lda #%00010100
  51. sta MSENABLE
  52. ; Turn on the screen.
  53. ; Format: x000bbbb
  54. ; x: 0 = screen on, 1 = screen off, bbbb: Brightness ($0-$F)
  55. lda #%00001111
  56. sta INIDISP
  57. jmp MainLoop
  58. LoadPaletteAndTileData:
  59. ; For more details on DMA, see:
  60. ; http://wiki.superfamicom.org/snes/show/Grog%27s+Guide+to+DMA+and+HDMA+on+the+SNES
  61. ; http://wiki.superfamicom.org/snes/show/Making+a+Small+Game+-+Tic-Tac-Toe
  62. ;
  63. ; A lot of the graphics-related registers are explained in Qwertie's doc:
  64. ; http://emu-docs.org/Super%20NES/General/snesdoc.html
  65. ; ... but be careful, because there are some errors in this doc.
  66. ;
  67. ; bazz's tutorial (available from http://wiki.superfamicom.org/snes/) is
  68. ; quite helpful with palette / sprites / DMA, especially starting at
  69. ; http://wiki.superfamicom.org/snes/show/Working+with+VRAM+-+Loading+the+Palette
  70. ; Initialize the palette memory in a loop.
  71. ; We could also do this with a DMA transfer (like we do with the tile data
  72. ; below), but it seems overkill for just a few bytes. :)
  73. ; TODO(mcmillen): do it with a DMA transfer.
  74. ; First, sprite palette data:
  75. ldx #0
  76. lda #128 ; Palette entries for sprites start at 128.
  77. sta CGADDR
  78. -
  79. lda.l SpritePalette, X
  80. sta CGDATA
  81. inx
  82. cpx #32 ; 32 bytes of palette data.
  83. bne -
  84. ; Now, BG3 palette data.
  85. ; Palette entries for BG3 start at 0.
  86. ldx #0
  87. lda #0
  88. sta CGADDR
  89. -
  90. lda.l TilePalette, X
  91. sta CGDATA
  92. inx
  93. cpx #8 ; 8 bytes of palette data.
  94. bne -
  95. ; TODO(mcmillen): make the "DMA stuff into VRAM" a macro or function.
  96. ; Set VMADDR to where we want the DMA to start. We'll store sprite data
  97. ; at the beginning of VRAM.
  98. ldx #$0000
  99. stx VMADDR
  100. ; DMA 0 source address & bank.
  101. ldx #SpriteData
  102. stx DMA0SRC
  103. lda #:SpriteData
  104. sta DMA0SRCBANK
  105. ; DMA 0 transfer size. Equal to the size of sprites32.pic.
  106. ldx #2048
  107. stx DMA0SIZE
  108. ; DMA 0 control register.
  109. ; Transfer type 001 = 2 addresses, LH.
  110. lda #%00000001
  111. sta DMA0CTRL
  112. ; DMA 0 destination.
  113. lda #$18 ; The upper byte is assumed to be $21, so this is $2118 & $2119.
  114. sta DMA0DST
  115. ; Enable DMA channel 0.
  116. lda #%00000001
  117. sta DMAENABLE
  118. ; Store background tile data at byte $2000 of VRAM.
  119. ; (VMADDR is a word address, so multiply by 2 to get the byte address.)
  120. ldx #$1000
  121. stx VMADDR
  122. ; DMA 0 source address & bank.
  123. ldx #TileData
  124. stx DMA0SRC
  125. lda #:TileData
  126. sta DMA0SRCBANK
  127. ; DMA 0 transfer size. Equal to the size of tiles.pic.
  128. ldx #512
  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. ; Tell the system that the BG3 tilemap starts at $4000.
  141. lda #%00100000
  142. sta BG3TILEMAP
  143. ; ... and that the background tile data for BG3 starts at $2000.
  144. lda #%00000001
  145. sta BG34NBA
  146. ; Set up the BG3 tilemap.
  147. ; VRAM write mode: increments the address every time we write a word.
  148. lda #%10000000
  149. sta VMAIN
  150. ; Set word address for accessing VRAM.
  151. ldx #$2000 ; BG 3 tilemap starts here. (Byte address $4000.)
  152. stx VMADDR
  153. ; Now write entries into the tile map.
  154. ldy #0
  155. -
  156. GetRandomByte
  157. sta $00
  158. ldx #$0000 ; This is a blank tile.
  159. ; 1 in 8 chance that we choose a non-blank tile.
  160. bit #%00000111
  161. bne +
  162. ldx #$0002
  163. bit #%10000000
  164. bne +
  165. ldx #$8002 ; Flip vertically.
  166. +
  167. stx VMDATA
  168. iny
  169. ; The tile map is 32x32 (1024 entries).
  170. cpy #1024
  171. bne -
  172. rts
  173. InitWorld:
  174. ; Start the background color as a dark blue.
  175. lda #4
  176. sta backgroundBlue
  177. ; Player's initial starting location and health.
  178. lda #(256 / 4)
  179. sta playerX
  180. lda #((224 - 32) / 2)
  181. sta playerY
  182. lda #20
  183. sta playerHealth
  184. ; (x-velocity, y-velocity) of 4 different player shot patterns.
  185. lda #6
  186. sta shotVelocityTable
  187. lda #0
  188. sta shotVelocityTable + 1
  189. lda #3
  190. sta shotVelocityTable + 2
  191. lda #3
  192. sta shotVelocityTable + 3
  193. lda #0
  194. sta shotVelocityTable + 4
  195. lda #6
  196. sta shotVelocityTable + 5
  197. lda #-3
  198. sta shotVelocityTable + 6
  199. lda #3
  200. sta shotVelocityTable + 7
  201. lda #-6
  202. sta shotVelocityTable + 8
  203. lda #0
  204. sta shotVelocityTable + 9
  205. lda #-3
  206. sta shotVelocityTable + 10
  207. lda #-3
  208. sta shotVelocityTable + 11
  209. lda #0
  210. sta shotVelocityTable + 12
  211. lda #-6
  212. sta shotVelocityTable + 13
  213. lda #3
  214. sta shotVelocityTable + 14
  215. lda #-3
  216. sta shotVelocityTable + 15
  217. rts
  218. MainLoop:
  219. lda #%10000001 ; Enable NMI interrupt & auto joypad read.
  220. sta NMITIMEN
  221. wai ; Wait for interrupt.
  222. lda #%00000001 ; Disable NMI interrupt while processing.
  223. sta NMITIMEN
  224. jsr JoypadRead
  225. jsr JoypadHandler
  226. jsr UpdateWorld
  227. jsr UpdateSprites
  228. jsr FillSecondarySpriteTable
  229. jsr SetBackgroundColor
  230. bra MainLoop
  231. JoypadRead:
  232. ; Load joypad registers into RAM for easy inspection & manipulation.
  233. -
  234. lda HVBJOY
  235. bit #$01 ; If auto-joypad read is happening, loop.
  236. bne -
  237. ldx JOY1L
  238. stx joy1
  239. ldx JOY2L
  240. stx joy2
  241. rts
  242. JoypadHandler:
  243. JoypadUp:
  244. lda joy1 + 1
  245. bit #$08 ; Up
  246. beq JoypadDown ; Button not pressed.
  247. lda playerY
  248. cmp #0
  249. beq JoypadDown ; Value saturated.
  250. dec playerY
  251. dec playerY
  252. JoypadDown:
  253. lda joy1 + 1
  254. bit #$04 ; Down
  255. beq JoypadLeft ; Button not pressed.
  256. lda playerY
  257. cmp #(224 - 32)
  258. beq JoypadLeft ; Value saturated.
  259. inc playerY
  260. inc playerY
  261. JoypadLeft:
  262. lda joy1 + 1
  263. bit #$02 ; Left
  264. beq JoypadRight ; Button not pressed.
  265. lda playerX
  266. cmp #0
  267. beq JoypadRight ; Value saturated.
  268. dec playerX
  269. dec playerX
  270. JoypadRight:
  271. lda joy1 + 1
  272. bit #$01 ; Right
  273. beq JoypadStart ; Button not pressed.
  274. lda playerX
  275. cmp #(256 - 32)
  276. beq JoypadStart ; Value saturated.
  277. inc playerX
  278. inc playerX
  279. JoypadStart:
  280. lda joy1 + 1
  281. bit #$10 ; Start
  282. beq JoypadSelect ; Button not pressed.
  283. lda backgroundRed
  284. cmp #31
  285. beq JoypadSelect ; Value saturated.
  286. inc backgroundRed
  287. JoypadSelect:
  288. lda joy1 + 1
  289. bit #$20 ; Select
  290. beq JoypadY ; Button not pressed.
  291. lda backgroundRed
  292. cmp #0
  293. beq JoypadY ; Value saturated.
  294. dec backgroundRed
  295. JoypadY:
  296. lda joy1 + 1
  297. bit #$40 ; Y
  298. beq JoypadX ; Button not pressed.
  299. lda backgroundGreen
  300. cmp #0
  301. beq JoypadX ; Value saturated.
  302. dec backgroundGreen
  303. JoypadX:
  304. lda joy1
  305. bit #$40 ; X
  306. beq JoypadL ; Button not pressed.
  307. lda backgroundGreen
  308. cmp #31
  309. beq JoypadL ; Value saturated.
  310. inc backgroundGreen
  311. JoypadL:
  312. lda joy1
  313. bit #$20 ; L
  314. beq JoypadR ; Button not pressed.
  315. lda backgroundBlue
  316. cmp #0
  317. beq JoypadR ; Value saturated.
  318. dec backgroundBlue
  319. JoypadR:
  320. lda joy1
  321. bit #$10 ; R
  322. beq JoypadB ; Button not pressed.
  323. lda backgroundBlue
  324. cmp #31
  325. beq JoypadB ; Value saturated.
  326. inc backgroundBlue
  327. JoypadB:
  328. lda joy1 + 1
  329. bit #$80 ; B
  330. beq JoypadDone
  331. jsr MaybeShoot
  332. JoypadDone:
  333. rts
  334. MaybeShoot:
  335. ; If the cooldown timer is non-zero, don't shoot.
  336. lda shotCooldown
  337. cmp #0
  338. bne MaybeShootDone
  339. ; Find the first empty spot in the shots array.
  340. ldx #playerShotArray
  341. -
  342. lda 0, X
  343. cmp #0
  344. beq +
  345. .rept shotSize
  346. inx
  347. .endr
  348. ; If we went all the way to the end, bail out.
  349. cpx #(playerShotArray + playerShotArrayLength * shotSize)
  350. beq MaybeShootDone
  351. bra -
  352. +
  353. ; Enable shot; set its position based on player position.
  354. ; TODO(mcmillen): it might be easier/faster to keep N arrays: one for each
  355. ; field of shot (shotSpriteArray, shotXArray, shotYArray, ...)
  356. lda #8 ; Sprite number.
  357. sta 0, X
  358. lda playerX
  359. clc
  360. adc #28
  361. sta 1, X
  362. lda playerY
  363. clc
  364. adc #14
  365. sta 2, X
  366. ; Get x- and y-velocity out of shotVelocityTable.
  367. lda nextShotState
  368. and #%00000000 ; 8 possibilities if we use #%00000111.
  369. ldy #0
  370. -
  371. cmp #0
  372. beq +
  373. .rept 2
  374. iny
  375. .endr
  376. dec A
  377. bra -
  378. +
  379. inc nextShotState
  380. ; x-velocity.
  381. lda shotVelocityTable, Y
  382. sta 3, X
  383. ; y-velocity.
  384. lda shotVelocityTable + 1, Y
  385. sta 4, X
  386. ; Set cooldown timer.
  387. lda #8
  388. sta shotCooldown
  389. MaybeShootDone:
  390. rts
  391. UpdateWorld:
  392. jsr UpdateShotCooldown
  393. jsr SpawnEnemyShots
  394. jsr UpdateShotPositions
  395. jsr CheckCollisionsWithPlayer
  396. jsr UpdateBackgroundScroll
  397. rts
  398. UpdateShotCooldown:
  399. ; Update shot cooldown.
  400. lda shotCooldown
  401. cmp #0
  402. beq +
  403. dec A
  404. sta shotCooldown
  405. +
  406. rts
  407. SpawnEnemyShots:
  408. lda vBlankCounter
  409. bit #%00001111 ; Spawn shots every this-many frames.
  410. beq +
  411. rts
  412. +
  413. ldy #0
  414. -
  415. lda enemyShotArray, Y
  416. cmp #0
  417. beq +
  418. .rept 6
  419. iny
  420. .endr
  421. cpy #(enemyShotArrayLength * shotSize)
  422. bne -
  423. rts ; Too many shots; bail.
  424. +
  425. lda #9 ; Sprite number.
  426. sta enemyShotArray, Y
  427. lda #254
  428. sta enemyShotArray + 1, Y ; x.
  429. lda #((224 - 32) / 2)
  430. and #%01111111
  431. sta enemyShotArray + 2, Y ; y.
  432. lda #-4
  433. sta enemyShotArray + 3, Y ; x-velocity.
  434. GetRandomByte
  435. and #%00000111 ; [0, 7]
  436. clc
  437. adc #-3 ; [-3, 4]
  438. cmp #4
  439. bne +
  440. lda #0 ; [-3, 3] with 2x chance of zero.
  441. +
  442. sta enemyShotArray + 4, Y ; y-velocity.
  443. rts
  444. UpdateShotPositions:
  445. ldx #0
  446. UpdateShot: ; Updates position of one shot.
  447. lda playerShotArray, X
  448. cmp #0
  449. beq ShotDone
  450. ; Add to the x-coordinate. If the carry bit is set, we went off the edge
  451. ; of the screen, so disable the shot.
  452. lda playerShotArray + 3, X ; x-velocity.
  453. sta $00
  454. bit #%10000000 ; Check whether the velocity is negative.
  455. bne UpdateShotWithNegativeXVelocity
  456. lda playerShotArray + 1, X
  457. clc
  458. adc $00
  459. bcs DisableShot
  460. sta playerShotArray + 1, X ; Store new x-coord.
  461. bra UpdateShotY
  462. UpdateShotWithNegativeXVelocity:
  463. ; TODO(mcmillen): wrap sprites when they go negative here, like we do
  464. ; with y-velocities.
  465. lda playerShotArray + 1, X ; Current x.
  466. clc
  467. adc $00
  468. bcc DisableShot
  469. sta playerShotArray + 1, X
  470. UpdateShotY:
  471. ; Add to the y-coordinate.
  472. lda playerShotArray + 4, X ; y-velocity.
  473. sta $00
  474. bit #%10000000 ; Check whether the velocity is negative.
  475. bne UpdateShotWithNegativeYVelocity
  476. lda playerShotArray + 2, X
  477. clc
  478. adc $00
  479. cmp #224
  480. bcs DisableShot
  481. sta playerShotArray + 2, X ; Store new y-coord.
  482. bra ShotDone
  483. UpdateShotWithNegativeYVelocity:
  484. lda playerShotArray + 2, X ; Current y.
  485. cmp #224
  486. bcs + ; If the shot was "off the top" before moving, maybe we'll reap it.
  487. adc $00 ; Otherwise, just update it,
  488. sta playerShotArray + 2, X ; save the result,
  489. bra ShotDone ; and we know it shouldn't be reaped.
  490. +
  491. clc
  492. adc $00
  493. cmp #224
  494. bcc DisableShot ; If it's now wrapped around, reap it.
  495. sta playerShotArray + 2, X
  496. bra ShotDone
  497. DisableShot:
  498. stz playerShotArray, X
  499. ShotDone:
  500. ; TODO(mcmillen): in places where we .rept inx (etc), is it faster to use
  501. ; actual addition?
  502. .rept shotSize
  503. inx
  504. .endr
  505. cpx #(playerShotArrayLength * enemyShotArrayLength * shotSize)
  506. bne UpdateShot
  507. rts
  508. CheckCollisionsWithPlayer:
  509. ; Store player position statically.
  510. clc
  511. lda playerX
  512. adc #16 ; Can't overflow.
  513. sta $00 ; Store the center.
  514. lda playerY
  515. ; Store the center. Our ship is actually 31 pixels tall, so offsetting by
  516. ; 15 feels more "fair": a shot that hits the invisible bottom edge of the
  517. ; ship won't count as a hit.
  518. adc #15
  519. sta $01
  520. ldx #0
  521. --
  522. lda enemyShotArray, X
  523. cmp #0 ; Check whether it's active.
  524. beq ++
  525. ; Find dx.
  526. lda enemyShotArray + 1, X ; x.
  527. clc
  528. adc #2 ; Get the center of the shot.
  529. sbc $00
  530. bpl + ; If the result is positive, great!
  531. eor #$ff ; Otherwise, negate it.
  532. inc A
  533. +
  534. ; A now contains dx, guaranteed to be positive.
  535. cmp #18 ; Threshold for "successful hit".
  536. bcs ++ ; Already too far; bail.
  537. sta $02
  538. ; Find dy.
  539. lda enemyShotArray + 2, X ; y.
  540. clc
  541. adc #2
  542. sbc $01
  543. bpl + ; If the result is positive, great!
  544. eor #$ff ; Otherwise, negate it.
  545. inc A
  546. +
  547. ; A now contains dy, guaranteed to be positive.
  548. clc
  549. adc $02 ; Add dx.
  550. cmp #18 ; Threshold for "successful hit".
  551. bcs ++
  552. ; OK, we got a hit!
  553. ; Disable the shot.
  554. lda #0
  555. sta enemyShotArray, X
  556. ; And decrement the player's life.
  557. lda playerHealth
  558. cmp #0
  559. beq ++
  560. dec playerHealth
  561. ++
  562. .rept shotSize
  563. inx
  564. .endr
  565. cpx #(enemyShotArrayLength * shotSize)
  566. bne --
  567. rts
  568. UpdateBackgroundScroll:
  569. ; Make the background scroll. Horizontal over time; vertical depending on
  570. ; player's y-coordinate.
  571. lda vBlankCounter
  572. sta BG3HOFS
  573. lda vBlankCounter + 1
  574. sta BG3HOFS
  575. lda playerY
  576. .rept 3
  577. lsr
  578. .endr
  579. sta BG3VOFS
  580. stz BG3VOFS
  581. rts
  582. UpdateSprites:
  583. ; This page is a good reference on SNES sprite formats:
  584. ; http://wiki.superfamicom.org/snes/show/SNES+Sprites
  585. ; It uses the same approach we're using, in which we keep a buffer of the
  586. ; sprite tables in RAM, and DMA the sprite tables to the system's OAM
  587. ; during VBlank.
  588. ; Sprite table 1 has 4 bytes per sprite, laid out as follows:
  589. ; Byte 1: xxxxxxxx x: X coordinate
  590. ; Byte 2: yyyyyyyy y: Y coordinate
  591. ; Byte 3: cccccccc c: Starting tile #
  592. ; Byte 4: vhoopppc v: vertical flip h: horizontal flip o: priority bits
  593. ; p: palette #
  594. ; Sprite table 2 has 2 bits per sprite, like so:
  595. ; bits 0,2,4,6 - High bit of the sprite's x-coordinate.
  596. ; bits 1,3,5,7 - Toggle Sprite size: 0 - small size 1 - large size
  597. ; Setting all the high bits keeps the sprites offscreen.
  598. ; Zero out the scratch space for the secondary sprite table.
  599. ldx #0
  600. -
  601. stz spriteTableScratchStart, X
  602. inx
  603. cpx #numSprites
  604. bne -
  605. ldx #0 ; Index into sprite table 1.
  606. ldy #0 ; Index into sprite table 2.
  607. ; Copy player coords into sprite table.
  608. lda playerX
  609. sta spriteTableStart, X
  610. lda playerY
  611. sta spriteTableStart + 1, X
  612. lda #0
  613. sta spriteTableStart + 2, X
  614. ; Set priority bits so that the sprite is drawn in front.
  615. lda #%00010000
  616. sta spriteTableStart + 3, X
  617. lda #%11000000 ; Enable large sprite.
  618. sta spriteTableScratchStart, Y
  619. .rept 4
  620. inx
  621. .endr
  622. iny
  623. ; Now add shots.
  624. sty $00 ; Save sprite table 2 index.
  625. ldy #0 ; Index into playerShotArray.
  626. -
  627. lda playerShotArray, Y
  628. cmp #0
  629. beq + ; If not enabled, skip to next shot.
  630. ; Update sprite table 1.
  631. sta spriteTableStart + 2, X ; sprite number
  632. lda playerShotArray + 1, Y
  633. sta spriteTableStart, X ; x
  634. lda playerShotArray + 2, Y
  635. sta spriteTableStart + 1, X ; y
  636. ; Update secondary sprite table.
  637. phy ; Save playerShotArray index.
  638. ldy $00
  639. lda #%01000000 ; Enable small sprite.
  640. sta spriteTableScratchStart, Y
  641. iny
  642. sty $00
  643. ply ; Restore playerShotArray index.
  644. .rept 4
  645. inx
  646. .endr
  647. +
  648. .rept shotSize
  649. iny
  650. .endr
  651. cpy #((playerShotArrayLength + enemyShotArrayLength) * shotSize)
  652. bne -
  653. ldy $00 ; Restore Y to its rightful self.
  654. ; Now add sprites to show player health.
  655. ; TODO(mcmillen): why aren't they in front?
  656. stz $01
  657. lda #4
  658. sta $02
  659. -
  660. lda $01
  661. cmp playerHealth
  662. beq + ; All done?
  663. lda #10
  664. sta spriteTableStart + 2, X ; sprite number
  665. lda $02
  666. sta spriteTableStart, X ; x
  667. clc
  668. adc #7
  669. sta $02
  670. lda #212
  671. sta spriteTableStart + 1, X ; y
  672. ; Set priority bits so that the sprite is drawn in front.
  673. lda #%00110000
  674. sta spriteTableStart + 3, X
  675. lda #%01000000 ; Enable small sprite.
  676. sta spriteTableScratchStart, Y
  677. .rept 4
  678. inx
  679. .endr
  680. iny
  681. inc $01
  682. bra -
  683. +
  684. ; Now clear out the unused entries in the sprite table.
  685. -
  686. cpx #spriteTable1Size
  687. beq +
  688. lda #1
  689. sta spriteTableStart, X
  690. .rept 4
  691. inx
  692. .endr
  693. bra -
  694. +
  695. rts
  696. FillSecondarySpriteTable:
  697. ; The secondary sprite table wants 2 bits for each sprite: one to set the
  698. ; sprite's size, and one that's the high bit of the sprite's x-coordinate.
  699. ; It's annoying to deal with bitfields when thinking about business logic,
  700. ; so the spriteTableScratch array contains one byte for each sprite, in
  701. ; which the two most significant bits are the "size" and "upper x" bits.
  702. ; This function is meant to be called after UpdateWorld, and packs those
  703. ; bytes into the actual bitfield that the OAM wants for the secondary
  704. ; sprite table.
  705. ;
  706. ; The expected format of every byte in the scratch sprite table is:
  707. ; sx------ s = size (0 = small, 1 = large)
  708. ; x = flipped high x-coordinate (so 1 behaves like "enable").
  709. ldx #0 ; Index into input table.
  710. ldy #0 ; Index into output table.
  711. -
  712. stz $00 ; Current byte; filled out by a set of 4 input table entries.
  713. .rept 4
  714. ; For each byte, the lower-order bits correspond to the lower-numbered
  715. ; sprites; therefore we insert the current sprite's bits "at the top"
  716. ; and shift them right for each successive sprite.
  717. lsr $00
  718. lsr $00
  719. lda spriteTableScratchStart, X
  720. ora $00
  721. sta $00
  722. inx
  723. .endr
  724. lda $00
  725. eor #%01010101
  726. sta spriteTable2Start, Y
  727. iny
  728. cpx #numSprites
  729. bne -
  730. rts
  731. SetBackgroundColor:
  732. ; The background-color bytes are (R, G, B), each ranging from [0-31].
  733. ; The palette color format is 15-bit: [0bbbbbgg][gggrrrrr]
  734. ; Set the background color.
  735. ; Entry 0 corresponds to the SNES background color.
  736. stz CGADDR
  737. ; Compute and the low-order byte and store it in CGDATA.
  738. lda backgroundGreen
  739. .rept 5
  740. asl
  741. .endr
  742. ora backgroundRed
  743. sta CGDATA
  744. ; Compute the high-order byte and store it in CGDATA.
  745. lda backgroundBlue
  746. .rept 2
  747. asl
  748. .endr
  749. sta $00
  750. lda backgroundGreen
  751. .rept 3
  752. lsr
  753. .endr
  754. ora $00
  755. sta CGDATA
  756. rts
  757. VBlankHandler:
  758. jsr VBlankCounter
  759. jsr DMASpriteTables
  760. rti
  761. VBlankCounter:
  762. ; Increment a counter of how many VBlanks we've done.
  763. ; This is a 24-bit counter. At 60 vblanks/second, this will take
  764. ; 77 hours to wrap around; that's good enough for me :)
  765. inc vBlankCounter
  766. bne +
  767. inc vBlankCounter + 1
  768. bne +
  769. inc vBlankCounter + 2
  770. +
  771. rts
  772. DMASpriteTables:
  773. ; Store at the base OAM address.
  774. ldx #$0000
  775. stx OAMADDR
  776. ; Default DMA control; destination $2104 (OAM data register).
  777. stz DMA0CTRL
  778. lda #$04
  779. sta DMA0DST
  780. ; Our sprites start at $0100 in bank 0 and are #$220 bytes long.
  781. ldx #spriteTableStart
  782. stx DMA0SRC
  783. stz DMA0SRCBANK
  784. ldx #spriteTableSize
  785. stx DMA0SIZE
  786. ; Kick off the DMA transfer.
  787. lda #%00000001
  788. sta DMAENABLE
  789. rts
  790. .ENDS
  791. ; Bank 1 is used for our graphics assets.
  792. .BANK 1 SLOT 0
  793. .ORG 0
  794. .SECTION "GraphicsData"
  795. SpriteData:
  796. .INCBIN "sprites32.pic"
  797. SpritePalette:
  798. .INCBIN "sprites32.clr"
  799. TileData:
  800. .INCBIN "tiles.pic"
  801. TilePalette:
  802. .INCBIN "tiles.clr"
  803. .ENDS
  804. ; Fill an entire bank with random numbers.
  805. .SEED 1
  806. .BANK 2 SLOT 0
  807. .ORG 0
  808. .SECTION "RandomBytes"
  809. .DBRND 32 * 1024, 0, 255
  810. .ENDS