irq.asm 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. IRQ Routine
  3. Features: jumps to different Irq routines depending on variable
  4. Variables needed:
  5. IrqRoutineNumber db ;
  6. IrqVCounter dw
  7. if IrqRoutineNumber=0, irq is disabled
  8. irqloader:
  9. actual irq routine
  10. irqinit:
  11. this must be called whenever a different irq is asserted
  12. */
  13. ;in: a,8bit=number of irq routine to set,0 is disable
  14. IrqInit:
  15. php
  16. sep #$20
  17. sta.b IrqRoutineNumber
  18. sta.b IrqRoutineNumberBuffer
  19. ;get initial latch value
  20. rep #$31
  21. and.w #%00000111 ;maximum number of routines: 8
  22. asl a ;word pointer
  23. sta.b TempBufferIrq
  24. asl a ;h/v latch values, multiply by 6
  25. clc
  26. adc.b TempBufferIrq
  27. tax
  28. lda.l (IrqRoutineLUT+BaseAdress+2),x
  29. sta.b IrqVCounter
  30. lda.l (IrqRoutineLUT+BaseAdress+4),x
  31. sta.b IrqHCounter
  32. ; sta.w $2109
  33. ; stz.w $2107
  34. plp
  35. rts
  36. IrqLoader:
  37. ; php ;backup status, then switch to 16bit a/x in order to preserve everything and not just 8bit of the accu
  38. rep #$39
  39. pha
  40. phx
  41. phy
  42. phd
  43. phb
  44. lda.w #$0000
  45. tcd
  46. ; jml (IrqLoaderHirom + BaseAdress)
  47. IrqLoaderHirom:
  48. sep #$20
  49. lda.b #$81
  50. pha
  51. plb
  52. rep #$31
  53. lda.b IrqRoutineNumber
  54. and.w #%00000111 ;maximum number of routines: 8
  55. asl a ;word pointer
  56. sta.b TempBufferIrq
  57. asl a ;h/v latch values, multiply by 6
  58. clc
  59. adc.b TempBufferIrq
  60. tax
  61. ; php
  62. jsr (IrqRoutineLUT,x)
  63. ; plp
  64. rep #$39
  65. lda.w $4211 ;clear irq flag
  66. plb
  67. pld
  68. ply
  69. plx
  70. pla
  71. ; plp
  72. rti
  73. IrqRoutineLUT:
  74. .dw IrqRoutine0 ;pointer to routine
  75. .dw 200 ;v-counter for this irq
  76. .dw 130 ;h-counter for this irq
  77. .dw IrqRoutine1
  78. .dw 200
  79. .dw 130
  80. .dw IrqRoutine2
  81. .dw 143 ;v-counter for this irq
  82. .dw 130 ;h-counter for this irq
  83. .dw IrqRoutine3
  84. .dw 0 ;v-counter for this irq
  85. .dw 0 ;h-counter for this irq
  86. .dw IrqRoutine4
  87. .dw 0 ;v-counter for this irq
  88. .dw 0 ;h-counter for this irq
  89. .dw IrqRoutine5
  90. .dw 0 ;v-counter for this irq
  91. .dw 0 ;h-counter for this irq
  92. .dw IrqRoutine6
  93. .dw 0 ;v-counter for this irq
  94. .dw 0 ;h-counter for this irq
  95. .dw IrqRoutine7
  96. .dw 0 ;v-counter for this irq
  97. .dw 0 ;h-counter for this irq
  98. IrqRoutine0:
  99. sep #$20
  100. lda.b #$2f ;set bg color to red
  101. sta.w $2132
  102. rep #$31
  103. inc.w ExtIrqCounter
  104. rts
  105. IrqRoutine1:
  106. sep #$20 ;enable forced blank
  107. lda.b #$8f ;set bg color to blue
  108. sta.w $2132
  109. rep #$31
  110. inc.w VIrqCounter
  111. rts
  112. IrqRoutine2:
  113. sep #$20
  114. /*
  115. lda.b MainScreen ;disable sprites below textbox top
  116. and.b #%01111
  117. sta.w $212c
  118. */
  119. lda.b #$40
  120. dec.b IrqRoutineNumber ;set up dma fifo irq below
  121. stz.w $210f ;reset bg2 scrolling
  122. stz.w $210f
  123. stz.w $2110
  124. stz.w $2110
  125. lda.b BGTilesVram12 ;set bg2 new tile adress
  126. and.b #$0f
  127. ora.b #$70
  128. sta.w $210B
  129. lda.b BG3TilemapVram ;use bg3 map buffer for bg2 textbox after irq split
  130. and.b #%11111100 ;mask off bg size bits
  131. sta.w $2108
  132. rep #$31
  133. lda.w #200 ;set up irq counter for dma fifo
  134. sta.w $4209
  135. lda.w #130
  136. sta.w $4207
  137. rts
  138. IrqRoutine3:
  139. IrqRoutine4:
  140. IrqRoutine5:
  141. IrqRoutine6:
  142. IrqRoutine7:
  143. rts
  144. ;checks if tilemap upload flags are set and issues one transfer per bg neccessary
  145. ;in: 8bit accu
  146. IrqCheckTilemapFlags:
  147. lda.b NMIPaletteUploadFlag
  148. beq IrqDontUploadPal
  149. stz.w $2121 ;upload colour-palette buffer at $7e:0e00 to cg-ram every frame
  150. ldx #PaletteBuffer&$ffff ;start at color 0
  151. stx.w $4302 ;Store the data offset into DMA source offset
  152. ldx #$0200
  153. stx.w $4305 ;Store the size of the data block
  154. lda.b #$7e
  155. sta.w $4304 ;Store the data bank holding the tile data
  156. lda.b #$00 ;Set the DMA mode (byte, normal increment)
  157. sta.w $4300
  158. lda.b #$22 ;Set the destination register ( $2122: CG-RAM Write )
  159. sta.w $4301
  160. lda.b #$01 ;Initiate the DMA transfer
  161. sta.w $420B
  162. stz.b NMIPaletteUploadFlag
  163. IrqDontUploadPal:
  164. lda.b NMIOamUploadFlag
  165. beq IrqDontUploadOAM
  166. stz.w $2102 ;upload to oam adress 0
  167. stz.w $2103
  168. ldx.w #OamBuffer&$ffff ;start at color 0
  169. stx.w $4302 ;Store the data offset into DMA source offset
  170. ldx.w #$0220
  171. stx.w $4305 ;Store the size of the data block
  172. lda.b #$7e
  173. sta.w $4304 ;Store the data bank holding the tile data
  174. lda.b #$00 ;Set the DMA mode (byte, normal increment)
  175. sta.w $4300
  176. lda.b #$04 ;Set the destination register ( $2122: CG-RAM Write )
  177. sta.w $4301
  178. lda.b #$01 ;Initiate the DMA transfer
  179. sta.w $420B
  180. stz.b NMIOamUploadFlag
  181. IrqDontUploadOAM:
  182. lda.b NMIBg1UploadFlag
  183. beq IrqDontUploadBg1
  184. rep #$31
  185. lda.b BG1TilemapVram
  186. and.w #%11 ;get tilemap size
  187. asl a
  188. tax
  189. lda.l (BgTilemapSizeLUT+BaseAdress),x
  190. sta.w $4305 ;Store the size of the data block
  191. ;transfer bg1 tilemap to vram if needed:
  192. sep #$20
  193. ldx.b VramBg1Tilemap
  194. stx.w $2116 ;vram adress $0000
  195. ldx.w #Bg1MapBuffer & $ffff
  196. stx.w $4302 ;Store the data offset into DMA source offset
  197. lda.b #(Bg1MapBuffer >> 16)
  198. sta.w $4304 ;Store the data bank of the source data
  199. lda.b #$80
  200. sta.w $2115 ;set VRAM transfer mode to word-access, increment by 1
  201. lda.b #$01 ;Set the DMA mode (word, normal increment)
  202. sta.w $4300
  203. lda.b #$18 ;Set the destination register (VRAM gate)
  204. sta.w $4301
  205. lda.b #$01 ;Initiate the DMA transfer
  206. sta.w $420B
  207. stz.b NMIBg1UploadFlag
  208. IrqDontUploadBg1:
  209. lda.b NMIBg2UploadFlag
  210. beq IrqDontUploadBg2
  211. rep #$31
  212. lda.b BG2TilemapVram
  213. and.w #%11 ;get tilemap size
  214. asl a
  215. tax
  216. lda.l (BgTilemapSizeLUT+BaseAdress),x
  217. sta.w $4305 ;Store the size of the data block
  218. sep #$20
  219. ldx.b VramBg2Tilemap
  220. stx.w $2116 ;vram adress $0000
  221. ldx.w #Bg2MapBuffer & $ffff
  222. stx.w $4302 ;Store the data offset into DMA source offset
  223. ; ldx.w #$800
  224. ; stx.w $4305 ;Store the size of the data block
  225. lda.b #(Bg2MapBuffer >> 16)
  226. sta.w $4304 ;Store the data bank of the source data
  227. lda.b #$80
  228. sta.w $2115 ;set VRAM transfer mode to word-access, increment by 1
  229. lda.b #$01 ;Set the DMA mode (word, normal increment)
  230. sta.w $4300
  231. lda.b #$18 ;Set the destination register (VRAM gate)
  232. sta.w $4301
  233. lda.b #$01 ;Initiate the DMA transfer
  234. sta.w $420B
  235. stz.b NMIBg2UploadFlag
  236. IrqDontUploadBg2:
  237. lda.b NMIBg3UploadFlag
  238. beq IrqDontUploadBg3
  239. ldx.b DmaFifoPointer
  240. lda.b #9 ;transfer type bg3 tilemap
  241. sta.l DmaFifoEntryType,x
  242. rep #$31
  243. txa ;update fifo entry pointer
  244. clc
  245. adc.w #DmaFifoEntryLength
  246. sta.b DmaFifoPointer
  247. sep #$20
  248. stz.b NMIBg3UploadFlag
  249. IrqDontUploadBg3:
  250. rts
  251. IrqBrightness:
  252. sep #$20
  253. lda.w IrqBrightnessIncDec ;if "done"-flag is set, do nothing
  254. bmi IrqBrightnessExit
  255. lsr a ;increase or decrease?
  256. bcc IrqBrightnessDec
  257. lda.b ScreenBrightness ;done with increasing?
  258. cmp.b #$f
  259. beq IrqBrightnessDone
  260. lda.b ScreenBrightness
  261. inc a ;decrease
  262. sta.b ScreenBrightness
  263. IrqBrightnessExit:
  264. rts
  265. IrqBrightnessDec:
  266. lda.b ScreenBrightness ;done with decreasing?
  267. beq IrqBrightnessDone
  268. lda.b ScreenBrightness
  269. dec a ;decrease
  270. sta.b ScreenBrightness
  271. rts
  272. IrqBrightnessDone:
  273. lda.w IrqBrightnessIncDec
  274. ora.b #$80 ;set "done"-flag
  275. sta.w IrqBrightnessIncDec
  276. rts
  277. WaitBrightnessDone:
  278. php
  279. sep #$20
  280. WaitBrightnessLoop:
  281. lda.w IrqBrightnessIncDec ;wait for irq to finish brightness inc/dec
  282. bpl WaitBrightnessLoop
  283. plp
  284. rts