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.

933 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 #10
  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 #12 ; 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. adc #16 ; Store the center.
  516. sta $01
  517. ldx #0
  518. --
  519. lda enemyShotArray, X
  520. cmp #0 ; Check whether it's active.
  521. beq ++
  522. ; Find dx.
  523. lda enemyShotArray + 1, X ; x.
  524. clc
  525. adc #2 ; Get the center of the shot.
  526. sbc $00
  527. bpl + ; If the result is positive, great!
  528. eor #$ff ; Otherwise, negate it.
  529. inc A
  530. +
  531. ; A now contains dx, guaranteed to be positive.
  532. cmp #18 ; Threshold for "successful hit".
  533. bcs ++ ; Already too far; bail.
  534. sta $02
  535. ; Find dy.
  536. lda enemyShotArray + 2, X ; y.
  537. clc
  538. adc #2
  539. sbc $01
  540. bpl + ; If the result is positive, great!
  541. eor #$ff ; Otherwise, negate it.
  542. inc A
  543. +
  544. ; A now contains dy, guaranteed to be positive.
  545. clc
  546. adc $02 ; Add dx.
  547. cmp #18 ; Threshold for "successful hit".
  548. bcs ++
  549. ; OK, we got a hit!
  550. ; Disable the shot.
  551. lda #0
  552. sta enemyShotArray, X
  553. ; And decrement the player's life.
  554. lda playerHealth
  555. cmp #0
  556. beq ++
  557. dec playerHealth
  558. ++
  559. .rept shotSize
  560. inx
  561. .endr
  562. cpx #(enemyShotArrayLength * shotSize)
  563. bne --
  564. rts
  565. UpdateBackgroundScroll:
  566. ; Make the background scroll. Horizontal over time; vertical depending on
  567. ; player's y-coordinate.
  568. lda vBlankCounter
  569. sta BG3HOFS
  570. lda vBlankCounter + 1
  571. sta BG3HOFS
  572. lda playerY
  573. .rept 3
  574. lsr
  575. .endr
  576. sta BG3VOFS
  577. stz BG3VOFS
  578. rts
  579. UpdateSprites:
  580. ; This page is a good reference on SNES sprite formats:
  581. ; http://wiki.superfamicom.org/snes/show/SNES+Sprites
  582. ; It uses the same approach we're using, in which we keep a buffer of the
  583. ; sprite tables in RAM, and DMA the sprite tables to the system's OAM
  584. ; during VBlank.
  585. ; Sprite table 1 has 4 bytes per sprite, laid out as follows:
  586. ; Byte 1: xxxxxxxx x: X coordinate
  587. ; Byte 2: yyyyyyyy y: Y coordinate
  588. ; Byte 3: cccccccc c: Starting tile #
  589. ; Byte 4: vhoopppc v: vertical flip h: horizontal flip o: priority bits
  590. ; p: palette #
  591. ; Sprite table 2 has 2 bits per sprite, like so:
  592. ; bits 0,2,4,6 - High bit of the sprite's x-coordinate.
  593. ; bits 1,3,5,7 - Toggle Sprite size: 0 - small size 1 - large size
  594. ; Setting all the high bits keeps the sprites offscreen.
  595. ; Zero out the scratch space for the secondary sprite table.
  596. ldx #0
  597. -
  598. stz spriteTableScratchStart, X
  599. inx
  600. cpx #numSprites
  601. bne -
  602. ldx #0 ; Index into sprite table 1.
  603. ldy #0 ; Index into sprite table 2.
  604. ; Copy player coords into sprite table.
  605. lda playerX
  606. sta spriteTableStart, X
  607. lda playerY
  608. sta spriteTableStart + 1, X
  609. lda #0
  610. sta spriteTableStart + 2, X
  611. ; Set priority bits so that the sprite is drawn in front.
  612. lda #%00110000
  613. sta spriteTableStart + 3, X
  614. lda #%11000000 ; Enable large sprite.
  615. sta spriteTableScratchStart, Y
  616. .rept 4
  617. inx
  618. .endr
  619. iny
  620. ; Now add shots.
  621. sty $00 ; Save sprite table 2 index.
  622. ldy #0 ; Index into playerShotArray.
  623. -
  624. lda playerShotArray, Y
  625. cmp #0
  626. beq + ; If not enabled, skip to next shot.
  627. ; Update sprite table 1.
  628. sta spriteTableStart + 2, X ; sprite number
  629. lda playerShotArray + 1, Y
  630. sta spriteTableStart, X ; x
  631. lda playerShotArray + 2, Y
  632. sta spriteTableStart + 1, X ; y
  633. ; Update secondary sprite table.
  634. phy ; Save playerShotArray index.
  635. ldy $00
  636. lda #%01000000
  637. sta spriteTableScratchStart, Y
  638. iny
  639. sty $00
  640. ply ; Restore playerShotArrayIndex.
  641. .rept 4
  642. inx
  643. .endr
  644. +
  645. .rept shotSize
  646. iny
  647. .endr
  648. cpy #((playerShotArrayLength + enemyShotArrayLength) * shotSize)
  649. bne -
  650. ; Now clear out the unused entries in the sprite table.
  651. -
  652. cpx #spriteTable1Size
  653. beq +
  654. lda #1
  655. sta spriteTableStart, X
  656. .rept 4
  657. inx
  658. .endr
  659. bra -
  660. +
  661. rts
  662. FillSecondarySpriteTable:
  663. ; The secondary sprite table wants 2 bits for each sprite: one to set the
  664. ; sprite's size, and one that's the high bit of the sprite's x-coordinate.
  665. ; It's annoying to deal with bitfields when thinking about business logic,
  666. ; so the spriteTableScratch array contains one byte for each sprite, in
  667. ; which the two most significant bits are the "size" and "upper x" bits.
  668. ; This function is meant to be called after UpdateWorld, and packs those
  669. ; bytes into the actual bitfield that the OAM wants for the secondary
  670. ; sprite table.
  671. ;
  672. ; The expected format of every byte in the scratch sprite table is:
  673. ; sx------ s = size (0 = small, 1 = large)
  674. ; x = flipped high x-coordinate (so 1 behaves like "enable").
  675. ldx #0 ; Index into input table.
  676. ldy #0 ; Index into output table.
  677. -
  678. stz $00 ; Current byte; filled out by a set of 4 input table entries.
  679. .rept 4
  680. ; For each byte, the lower-order bits correspond to the lower-numbered
  681. ; sprites; therefore we insert the current sprite's bits "at the top"
  682. ; and shift them right for each successive sprite.
  683. lsr $00
  684. lsr $00
  685. lda spriteTableScratchStart, X
  686. ora $00
  687. sta $00
  688. inx
  689. .endr
  690. lda $00
  691. eor #%01010101
  692. sta spriteTable2Start, Y
  693. iny
  694. cpx #numSprites
  695. bne -
  696. rts
  697. SetBackgroundColor:
  698. ; The background-color bytes are (R, G, B), each ranging from [0-31].
  699. ; The palette color format is 15-bit: [0bbbbbgg][gggrrrrr]
  700. ; Set the background color.
  701. ; Entry 0 corresponds to the SNES background color.
  702. stz CGADDR
  703. ; Compute and the low-order byte and store it in CGDATA.
  704. lda backgroundGreen
  705. .rept 5
  706. asl
  707. .endr
  708. ora backgroundRed
  709. sta CGDATA
  710. ; Compute the high-order byte and store it in CGDATA.
  711. lda backgroundBlue
  712. .rept 2
  713. asl
  714. .endr
  715. sta $00
  716. lda backgroundGreen
  717. .rept 3
  718. lsr
  719. .endr
  720. ora $00
  721. sta CGDATA
  722. rts
  723. VBlankHandler:
  724. jsr VBlankCounter
  725. jsr DMASpriteTables
  726. rti
  727. VBlankCounter:
  728. ; Increment a counter of how many VBlanks we've done.
  729. ; This is a 24-bit counter. At 60 vblanks/second, this will take
  730. ; 77 hours to wrap around; that's good enough for me :)
  731. inc vBlankCounter
  732. bne +
  733. inc vBlankCounter + 1
  734. bne +
  735. inc vBlankCounter + 2
  736. +
  737. rts
  738. DMASpriteTables:
  739. ; Store at the base OAM address.
  740. ldx #$0000
  741. stx OAMADDR
  742. ; Default DMA control; destination $2104 (OAM data register).
  743. stz DMA0CTRL
  744. lda #$04
  745. sta DMA0DST
  746. ; Our sprites start at $0100 in bank 0 and are #$220 bytes long.
  747. ldx #spriteTableStart
  748. stx DMA0SRC
  749. stz DMA0SRCBANK
  750. ldx #spriteTableSize
  751. stx DMA0SIZE
  752. ; Kick off the DMA transfer.
  753. lda #%00000001
  754. sta DMAENABLE
  755. rts
  756. .ENDS
  757. ; Bank 1 is used for our graphics assets.
  758. .BANK 1 SLOT 0
  759. .ORG 0
  760. .SECTION "GraphicsData"
  761. SpriteData:
  762. .INCBIN "sprites32.pic"
  763. SpritePalette:
  764. .INCBIN "sprites32.clr"
  765. TileData:
  766. .INCBIN "tiles.pic"
  767. TilePalette:
  768. .INCBIN "tiles.clr"
  769. .ENDS
  770. ; Fill an entire bank with random numbers.
  771. .SEED 1
  772. .BANK 2 SLOT 0
  773. .ORG 0
  774. .SECTION "RandomBytes"
  775. .DBRND 32 * 1024, 0, 255
  776. .ENDS