gfxvrammisc.asm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. ;init variables:
  2. ;a(8bit) is index into bgmode 2byte pointertable
  3. SetBGMode:
  4. php
  5. rep #$31 ;accu 16bit
  6. and.w #$000f ;16 valid entries
  7. asl a
  8. tax
  9. lda.l BgModeLut+BaseAdress,x
  10. ; lda.b UploadBackgroundPointer+1 ;get source bank of tilemap
  11. sta.b SetBGThreeBytePointerLo ;
  12. sep #$20
  13. lda.b #(:BgModeLut+BaseAdress>>16)
  14. sta.b SetBGThreeBytePointerBank ;
  15. ldy.w #$0000
  16. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  17. sta.b SetIni
  18. iny
  19. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  20. sta.b ScreenMode
  21. iny
  22. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  23. sta.b MainScreen
  24. iny
  25. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  26. sta.b SubScreen
  27. iny
  28. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  29. sta.b CGWsel
  30. iny
  31. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  32. sta.b CgadsubConfig
  33. iny
  34. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  35. sta.b BGTilesVram12
  36. iny
  37. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  38. sta.b BGTilesVram34
  39. iny
  40. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  41. sta.b BG1TilemapVram
  42. iny
  43. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  44. sta.b BG2TilemapVram
  45. iny
  46. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  47. sta.b BG3TilemapVram
  48. iny
  49. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  50. sta.b BG4TilemapVram
  51. iny
  52. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  53. sta.b ObjSel
  54. iny
  55. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  56. jsr IrqInit
  57. iny
  58. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  59. sta.b W12SEL
  60. iny
  61. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  62. sta.b W34SEL
  63. iny
  64. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  65. sta.b WOBJSEL
  66. iny
  67. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  68. sta.b W1L
  69. iny
  70. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  71. sta.b W1R
  72. iny
  73. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  74. sta.b W2L
  75. iny
  76. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  77. sta.b W2R
  78. iny
  79. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  80. sta.b WBGLOG
  81. iny
  82. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  83. sta.b WOBJLOG
  84. iny
  85. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  86. sta.b WMS
  87. iny
  88. lda.b [SetBGThreeBytePointerLo],y ;load byte from table
  89. sta.b WSS
  90. ;init vram locations
  91. rep #$31
  92. lda.b BGTilesVram12
  93. pha
  94. and.w #$00f0
  95. xba
  96. sta.b VramBg2Tiles
  97. pla
  98. and.w #$000f
  99. xba
  100. clc
  101. asl a
  102. asl a
  103. asl a
  104. asl a
  105. sta.b VramBg1Tiles
  106. lda.b BGTilesVram34
  107. pha
  108. and.w #$00f0
  109. xba
  110. sta.b VramBg4Tiles
  111. pla
  112. and.w #$000f
  113. xba
  114. clc
  115. asl a
  116. asl a
  117. asl a
  118. asl a
  119. sta.b VramBg3Tiles
  120. lda.b BG1TilemapVram
  121. and.w #$00fc
  122. xba
  123. sta.b VramBg1Tilemap
  124. lda.b BG2TilemapVram
  125. and.w #$00fc
  126. xba
  127. sta.b VramBg2Tilemap
  128. lda.b BG3TilemapVram
  129. and.w #$00fc
  130. xba
  131. sta.b VramBg3Tilemap
  132. lda.b BG4TilemapVram
  133. and.w #$00fc
  134. xba
  135. sta.b VramBg4Tilemap
  136. lda.b ObjSel
  137. and.w #%111 ;get spritetile base adress
  138. asl a ;shift left by 5
  139. asl a
  140. asl a
  141. asl a
  142. asl a
  143. xba ;shift left by 8
  144. sta.b SpriteTileOffsetVram
  145. plp
  146. rts
  147. ;**********************************
  148. ;Simple Background loader for bg1 and 2
  149. ;in: a,8bit: bit0,1: bg number, 0-3
  150. ; bit2-4: palette to use
  151. ; bit5: priority bit
  152. ;in: x,16bit: pointernumber of background
  153. ;
  154. ;every background consists of 1.tiles,2.tilemap,3.pal+header(relative pointers to all 3 files+eof)
  155. ;
  156. ;uses: TempBuffer0-8
  157. ;**********************************
  158. UploadBackgroundFile:
  159. php
  160. sep #$20
  161. sta.b TempBuffer+2
  162. rep #$31
  163. ; and.w #$ff
  164. txa
  165. sta.b TempBuffer+4 ;multiply background number by 3
  166. asl a
  167. adc.b TempBuffer+4
  168. tax
  169. lda.l UploadBackgroundFileLUT+BaseAdress,x ;get pointer low byte to background
  170. sta.b TempBuffer+4
  171. inx
  172. lda.l UploadBackgroundFileLUT+BaseAdress,x ;get pointer high word to background
  173. sta.b TempBuffer+5
  174. sep #$20
  175. sei
  176. jsr UploadTiles
  177. jsr UploadTilemap
  178. cli
  179. plp
  180. rts
  181. UploadTiles:
  182. sep #$20
  183. ldx.b DmaFifoPointer
  184. lda #1 ;transfer type
  185. sta.l DmaFifoEntryType,x
  186. lda.b TempBuffer+6 ;get source bank
  187. sta.l DmaFifoEntrySrcBank,x ;source 4304
  188. rep #$31
  189. ldy.w #$0000
  190. lda.b [TempBuffer+4],y ;get relative pointer to tiles
  191. sta.b TempBuffer+7
  192. adc.b TempBuffer+4 ;add file offset
  193. sta.l DmaFifoEntrySrcLo,x ;source 4302
  194. sec
  195. iny
  196. iny
  197. lda.b [TempBuffer+4],y ;get relative pointer to tilemap
  198. sbc.b TempBuffer+7 ;substract tile position to get tile length
  199. sta.l DmaFifoEntryCount,x ;length 4305
  200. lda.b TempBuffer+2
  201. and.w #%11
  202. asl a
  203. tay
  204. lda.w VramBg1Tiles,y ;get vram destination
  205. sta.l DmaFifoEntryTarget,x ;vram target 2116
  206. txa ;update fifo entry pointer
  207. clc
  208. adc.w #DmaFifoEntryLength
  209. sta.b DmaFifoPointer
  210. rts
  211. BgMapBufferLUT:
  212. .dw Bg1MapBuffer&$ffff
  213. .dw Bg2MapBuffer&$ffff
  214. .dw Bg3MapBuffer&$ffff
  215. .dw Bg4MapBuffer&$ffff
  216. UploadTilemap:
  217. ;copy tilemap to wram buffer:
  218. php
  219. sep #$20
  220. phb
  221. lda.b #$81
  222. pha
  223. plb
  224. lda.b #Bg1MapBuffer >> 16 ;store target adress(map buffer) in ram port.
  225. and.b #$01 ;only 1 or 0 valid for banks $7e or $7f
  226. sta.w $2183
  227. rep #$31
  228. lda.b TempBuffer+2
  229. and.w #%11
  230. asl a
  231. tax
  232. lda.l (BgMapBufferLUT+BaseAdress),x
  233. sta.w $2181
  234. sep #$20
  235. lda.b TempBuffer+2 ;save layer bit
  236. ; pha
  237. and.b #$3c ;mask off everything but palette and priority bit
  238. sta.b TempBuffer+3
  239. rep #$31 ;accu 16bit
  240. lda.b TempBuffer+5 ;get source bank of tilemap
  241. sta.b ThreeBytePointerHi ;
  242. ldy.w #$0002
  243. lda.b [TempBuffer+4],y ;get relative pointer to tilemap
  244. sta.b TempBuffer+7
  245. adc.b TempBuffer+4
  246. sta.b ThreeBytePointerLo
  247. iny
  248. iny
  249. lda.b [TempBuffer+4],y ;get relative pointer to palette(in order to calculate tilemap length)
  250. sec
  251. sbc.b TempBuffer+7
  252. sta.b TempBuffer ;store total length for dma transfer
  253. and.w #$fffe ;mask off bit0 so the next loop never hangs
  254. tax ;get length, store in x
  255. ldy.w #$0000
  256. sep #$20
  257. DMAUploadTilemapLoop:
  258. lda.b [ThreeBytePointerLo],y ;load word from ThreeBytePointer
  259. sta.w $2180 ;store in bg1 map buffer, low byte
  260. iny
  261. lda.b [ThreeBytePointerLo],y ;load word from ThreeBytePointer
  262. and.b #$c3
  263. ora.b TempBuffer+3 ;or with priority and palette
  264. sta.w $2180 ;store in bg1 map buffer, low byte
  265. iny
  266. dex ;word-dec length counter
  267. dex
  268. bne DMAUploadTilemapLoop ;done if length counter = 0
  269. ; pla
  270. ; sta.b UploadBackgroundLayer
  271. ; jsr DMAUploadBgTilemap
  272. rep #$31
  273. lda.b TempBuffer+2
  274. and.w #%11
  275. tax
  276. inc.b NMIBg1UploadFlag,x ;set corresponding bg tilemap update flag
  277. sep #$20
  278. DMAUploadPaletteBG1:
  279. lda.b #PaletteBuffer >> 16 ;store target adress(palette buffer) in ram port.
  280. and.b #$01 ;only 1 or 0 valid for banks $7e or $7f
  281. lda.b TempBuffer+2
  282. bit.b #%10 ;do an extra shift if this is bg0 or 1(bg0,1:always 16 color palettes /bg2,3: always 4 colors)
  283. beq DMAUploadPaletteBG1jaja
  284. and.b #%00011100 ;mask off everything but palette number
  285. asl a ;multiply by 8(2bytes per color x 4 colors per palette)
  286. bra DMAUploadPaletteBGSelectDone
  287. DMAUploadPaletteBG1jaja:
  288. and.b #%00011100 ;mask off everything but palette number
  289. asl a
  290. asl a
  291. asl a ;multiply by 32(2bytes per color x 16 colors per palette)
  292. DMAUploadPaletteBGSelectDone:
  293. rep #$31
  294. and.w #$00ff
  295. tax
  296. lda.b TempBuffer+5 ;get source bank of palette
  297. sta.b ThreeBytePointerHi ;
  298. ldy.w #$0004
  299. lda.b [TempBuffer+4],y ;get relative pointer to palette
  300. sta.b TempBuffer+7
  301. adc.b TempBuffer+4
  302. sta.b ThreeBytePointerLo
  303. iny
  304. iny
  305. lda.b [TempBuffer+4],y ;get relative pointer to EOF(in order to calculate tilemap length)
  306. sec
  307. sbc.b TempBuffer+7
  308. sta.b TempBuffer
  309. ldy.w #$0000 ;clear target/source counter
  310. DMAUploadPaletteBG1Loop:
  311. sep #$20
  312. lda.b [ThreeBytePointerLo],y ;load word from ThreeBytePointer
  313. sta.l PaletteBuffer,x ;store in bg1 map buffer
  314. rep #$31
  315. iny
  316. inx
  317. dec.b TempBuffer ;word-dec length counter
  318. bne DMAUploadPaletteBG1Loop ;done if length counter = 0
  319. sep #$20
  320. inc.b NMIPaletteUploadFlag
  321. plb
  322. plp
  323. rts
  324. ClearPalette:
  325. PHX
  326. PHP
  327. REP #$30 ; mem/A = 8 bit, X/Y = 16 bit
  328. SEP #$20
  329. STZ $2121
  330. LDX #$0100
  331. ClearPaletteLoop:
  332. STZ $2122
  333. DEX
  334. BNE ClearPaletteLoop
  335. PLP
  336. PLX
  337. RTS
  338. ClearPaletteBuffer:
  339. php
  340. rep #$31
  341. sep #$20
  342. lda.b #0 ;clear word: $0000
  343. ldy.w #$200
  344. ldx.w #PaletteBuffer&$ffff
  345. jsr ClearWRAM
  346. plp
  347. rts
  348. /*
  349. ldx.w #$0200
  350. ClearPaletteBufferLoop:
  351. dex
  352. stz.w PaletteBuffer&$ffff,x
  353. cpx.w #$0000
  354. bne ClearPaletteBufferLoop
  355. rts
  356. */
  357. ClearBg1TilemapBuffer:
  358. php
  359. rep #$31
  360. sep #$20
  361. lda.b #0 ;clear word: $0000
  362. ldy.w #$800
  363. ldx.w #Bg1MapBuffer&$ffff
  364. jsr ClearWRAM
  365. plp
  366. rts
  367. /*
  368. php
  369. rep #$30
  370. lda.w #$0000
  371. ldx.w #$0800
  372. ClearBg1TilemapBufferLoop:
  373. sta.l Bg1MapBuffer,x
  374. dex
  375. dex
  376. bne ClearBg1TilemapBufferLoop
  377. plp
  378. rts
  379. */
  380. ClearBg2TilemapBuffer:
  381. php
  382. rep #$31
  383. sep #$20
  384. lda.b #0 ;clear word: $0000
  385. ldy.w #$800
  386. ldx.w #Bg2MapBuffer&$ffff
  387. jsr ClearWRAM
  388. plp
  389. rts
  390. /*
  391. php
  392. rep #$30
  393. lda.w #$0000
  394. ldx.w #$0800
  395. ClearBg2TilemapBufferLoop:
  396. sta.l Bg2MapBuffer,x
  397. dex
  398. dex
  399. bne ClearBg2TilemapBufferLoop
  400. plp
  401. rts
  402. */
  403. ClearBg3TilemapBuffer:
  404. php
  405. rep #$31
  406. sep #$20
  407. lda.b #2 ;clear word: $2480
  408. ldy.w #$800
  409. ldx.w #Bg3MapBuffer&$ffff
  410. jsr ClearWRAM
  411. plp
  412. rts
  413. /*
  414. php
  415. rep #$30
  416. lda.w #$2480 ;bg3 tilemap clearing tile, priority set
  417. ldx.w #$0800
  418. ClearBg3TilemapBufferLoop:
  419. sta.l Bg3Buffer-2,x
  420. dex
  421. dex
  422. bne ClearBg3TilemapBufferLoop
  423. plp
  424. rts
  425. */
  426. ;uploades any otherwise unfitting piece of gfx-tiles to vram
  427. ;in:a,8bit: tileset number
  428. ; y,16bit: vram target
  429. ; transfer length is determined by tileset
  430. ;uses TempBuffer, TempBuffer+1
  431. GeneralVramUploader:
  432. ; lda.w #(TabTestTile8x8TilesEnd-TabTestTile8x8Tiles)
  433. php
  434. sep #$20
  435. sta.b TempBuffer
  436. phb
  437. lda.b #$7e
  438. pha
  439. plb
  440. rep #$31
  441. sei
  442. tya
  443. ldy.b DmaFifoPointer
  444. sta.w DmaFifoEntryTarget&$ffff,y ;vram target 2116
  445. lda.b TempBuffer ;multiply by 5
  446. and.w #$ff
  447. sta.b TempBuffer
  448. asl a
  449. asl a
  450. clc
  451. adc.b TempBuffer
  452. tax
  453. lda.l (GeneralTilesetsLUT+BaseAdress),x ;get source adress
  454. sta.b TempBuffer
  455. sta.w DmaFifoEntrySrcLo&$ffff,y ;source 4302
  456. inx
  457. lda.l (GeneralTilesetsLUT+BaseAdress),x ;get source adress
  458. sta.w DmaFifoEntrySrcHi&$ffff,y ;source 4303
  459. inx
  460. inx
  461. lda.l (GeneralTilesetsLUT+BaseAdress),x ;get source length
  462. sta.w DmaFifoEntryCount&$ffff,y
  463. sep #$20
  464. lda #1 ;transfer type normal dma
  465. sta.w DmaFifoEntryType&$ffff,y
  466. rep #$31
  467. tya ;update fifo entry pointer
  468. clc
  469. adc.w #DmaFifoEntryLength
  470. sta.b DmaFifoPointer
  471. cli
  472. plb
  473. plp
  474. rts
  475. IncBrightness:
  476. php
  477. rep #$30
  478. sep #$20
  479. lda FrameCounterLo ;load current frame
  480. and BrightnessSpeed ;and check selected bits(the more, the slower)
  481. bne DontIncBrightness ;only change brightness every second frame
  482. lda ScreenBrightness
  483. and #$0f ;and only brightness regs
  484. cmp #$0f ;skip increase
  485. beq DoneIncBrightness ;if maximum brightness is reached
  486. inc ScreenBrightness ;change brightness
  487. plp
  488. rts
  489. DontIncBrightness:
  490. plp
  491. rts ;do nothing
  492. DoneIncBrightness
  493. plp
  494. rts ;and return
  495. DecBrightness:
  496. lda FrameCounterLo ;load current frame
  497. and BrightnessSpeed ;and check selected bits(the more, the slower)
  498. bne DontDecBrightness ;only change brightness every second frame
  499. lda ScreenBrightness
  500. and #$0f ;and only brightness regs
  501. beq DoneDecBrightness
  502. dec ScreenBrightness ;change brightness
  503. rts
  504. DontDecBrightness:
  505. rts ;do nothing
  506. DoneDecBrightness:
  507. inc CurrentEvent ;go to next event if done with brightness change
  508. rts ;and return
  509. DisableScreen:
  510. php
  511. sep #$20
  512. lda.b #%10000000 ;force blanking
  513. sta $2100 ;
  514. plp
  515. rts
  516. EnableScreen:
  517. php
  518. sep #$20
  519. LDA $4210 ;reg $4210 - NMI status, reading resets
  520. cli
  521. lda ScreenBrightness ;setup screen brightness
  522. and #$7f ;screen always on and enabled
  523. sta $2100
  524. plp
  525. rts
  526. ResetScrollOffsets:
  527. php
  528. rep #$31
  529. lda.w #$3ff
  530. sta.w BG1VOfLo ;
  531. sta.w BG2VOfLo ;
  532. stz.w BG1HOfLo
  533. stz.w BG2HOfLo ;reset all bg1/bg2 offsets
  534. stz.w BG2VOfLo ;
  535. stz.w ScreenPixelPositionX
  536. stz.w ScreenPixelPositionY
  537. plp
  538. rts
  539. ;**********************************
  540. ;fade in/fade out
  541. ;in: a/8bit: number of sample when fadeout should start
  542. ;event is incremented automatically once fadeout is complete
  543. ;**********************************
  544. FadeInFadeOut:
  545. cpx.w FrameCounterLo
  546. bpl EventRoutineContinue
  547. lda #$03
  548. sta BrightnessSpeed
  549. jsr DecBrightness
  550. rts
  551. EventRoutineContinue:
  552. lda #$03
  553. sta BrightnessSpeed
  554. jsr IncBrightness
  555. rts