qd16cmdhandler.asm 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. .include "routines/main.h"
  2. .section "qd16cmd" semifree
  3. /*
  4. processes commands from qd16 microcontroller.
  5. communicates using physical rom adresses $00:0000(ACK), $00:0001(CMD), $00:0000(DAT)
  6. check for open bus before processing any commands
  7. */
  8. QD16CommandPoll:
  9. php
  10. sep #$20
  11. lda.w Qd16Flags
  12. bit.b #%100
  13. bne QD16CommandPollCancel ;cancel manual command polling if irq-com mode is enabled
  14. jsr QD16CommandHandler
  15. QD16CommandPollCancel:
  16. plp
  17. rts
  18. QD16CommandHandler:
  19. php
  20. rep #$31
  21. lda.l QD16McCommand ;get data first(in case the mc is just about to steal the bus again)
  22. sta.b tmp
  23. lda.l QD16McHandshake ;has microcontroller disabled rom access for snes?(reading open bus returns differing values here)
  24. cmp.l QD16OpenBusMirror
  25. bne QD16OpenBus
  26. sep #$20
  27. cmp.b #QD16McAck
  28. bne QD16OpenBus ;either open bus or the mc hasn't written any new command yet
  29. lda.b tmp
  30. bit.b #$f8 ;valid command present?(max number of commands is 8)
  31. bne QD16OpenBus ;if any of the upper 5 bits are set, command is invalid
  32. lda.b #QD16SnesAck
  33. sta.l $c00000 ;tell mc that data has been received
  34. ;handshake complete, process data
  35. rep #$31
  36. lda.b tmp
  37. pha
  38. and.w #$7 ;8 commands max
  39. asl a
  40. tax
  41. pla
  42. jsr (QD16CmdLUT,x) ;execute command
  43. QD16OpenBus:
  44. plp
  45. rts
  46. QD16CmdLUT:
  47. .dw QD16GetBankCount
  48. .dw QD16EnableIrqCom
  49. .dw QD16UpdateLoadPercentage
  50. .dw QD16FadeOut
  51. .dw QD16DrawHeader
  52. .dw QD16CmdVoid
  53. .dw QD16CmdVoid
  54. .dw QD16CmdVoid
  55. QD16CmdVoid:
  56. rts
  57. ;this can be triggered by an explicit mc-command. apart from that, it's also triggered when mc reports bank 0 has been uploaded.
  58. QD16DrawHeader:
  59. rts
  60. QD16EnableIrqCom:
  61. rep #$31
  62. lda.l QD16RamIrq ;did mc copy irq-vector to correct location in rom?
  63. cmp.l QD16RomIrq
  64. ; beq QD16EnableIrqVectorOk ;debug
  65. lda.w #$adba ;clear mc-ack, signalize error, don't enter irq-mode
  66. sta.l QD16McHandshake
  67. rts
  68. QD16EnableIrqVectorOk:
  69. sep #$20
  70. lda.b #1
  71. sta.w IrqRoutineNumber
  72. lda.b #%100
  73. ora.w Qd16Flags
  74. sta.w Qd16Flags ;set "enable irq-com"-flag
  75. rts
  76. ;input:x,header position in bank
  77. QD16CheckHeaderValid:
  78. php
  79. sep #$20
  80. lda.w Qd16Flags
  81. bit.b #%10
  82. bne QD16CheckHeaderNoHirom ;skip header drawing if a valid header has been found before
  83. rep #$31
  84. lda.l RomVectorReset ;reset vector
  85. bpl QD16CheckHeaderNoLorom
  86. lda.l RomHeaderMap ;rom map, lorom?
  87. lsr a
  88. bcs QD16CheckHeaderNoLorom
  89. lda.l RomHeaderSize ;rom size
  90. and.w #$ff
  91. cmp.w #$7 ;smaller than 4mbit?
  92. bcc QD16CheckHeaderNoLorom
  93. cmp.w #$e ;bigger than 64mbit?
  94. bcs QD16CheckHeaderNoLorom
  95. ldx.w #LoromOffset ;header found
  96. jsr QD16CheckTitle
  97. bcc QD16CheckHeaderNoLorom
  98. sep #$20
  99. lda.b #%10
  100. ora.w Qd16Flags
  101. sta.w Qd16Flags ;set "header found, title printed"-flag
  102. plp
  103. sec
  104. rts
  105. QD16CheckHeaderNoLorom:
  106. rep #$31
  107. lda.l RomVectorReset+HiromOffset ;reset vector high byte
  108. bpl QD16CheckHeaderNoHirom
  109. lda.l RomHeaderMap+HiromOffset ;rom map, lorom?
  110. lsr a
  111. bcc QD16CheckHeaderNoHirom
  112. lda.l RomHeaderSize+HiromOffset ;rom size
  113. and.w #$ff
  114. cmp.w #$7 ;smaller than 4mbit?
  115. bcc QD16CheckHeaderNoHirom
  116. cmp.w #$e ;bigger than 64mbit?
  117. bcs QD16CheckHeaderNoHirom
  118. ldx.w #HiromOffset ;header found
  119. jsr QD16CheckTitle
  120. bcc QD16CheckHeaderNoHirom
  121. sep #$20
  122. lda.b #%10
  123. ora.w Qd16Flags
  124. sta.w Qd16Flags ;set "header found, title printed"-flag
  125. plp
  126. sec
  127. rts
  128. QD16CheckHeaderNoHirom:
  129. plp
  130. clc
  131. rts
  132. ;input:x,16bit:lo/hirom pointer.
  133. ;checks if bytes are within ranges $20-$7f and $a7-$df(kana)
  134. QD16CheckTitle:
  135. php
  136. phx
  137. sep #$20
  138. ldy.w #0
  139. QD16CheckTitleLoop:
  140. ;check if chars are in ASCII/kana range and return null if any invalid ones are found.
  141. lda.l RomHeaderTitle,x
  142. cmp.b #$20
  143. bcc QD16CheckTitleVoid
  144. ;is bigger than $20
  145. cmp.b #$df+1
  146. bcs QD16CheckTitleVoid
  147. ;is between $20 and $df
  148. bpl QD16CheckTitleCharOk
  149. ;is between $80 and $df
  150. cmp.b #$a7
  151. bcc QD16CheckTitleVoid
  152. ;is between $a7 and $df
  153. QD16CheckTitleCharOk:
  154. inx ;is between $a7 and $df or between $20 and $7f
  155. iny
  156. cpy.w #21
  157. bne QD16CheckTitleLoop
  158. plx ;title is ok, ready to print
  159. plp
  160. sec
  161. rts
  162. QD16CheckTitleVoid:
  163. plx ;title is not present/contains bad chars
  164. plp
  165. clc
  166. rts
  167. QD16GetBankCount:
  168. sep #$20
  169. xba
  170. sta.w QD16BankCount
  171. lda.b #%1
  172. ora.w Qd16Flags
  173. sta.w Qd16Flags ;set "rom upload commenced"-flag
  174. rts
  175. QD16UpdateLoadPercentage:
  176. sep #$20
  177. xba
  178. cmp.b #101
  179. bcc QD16UpdateLoadPercentageNoOver
  180. lda.b #100
  181. QD16UpdateLoadPercentageNoOver:
  182. cmp.w PercentageBuff
  183. beq QD16UpdateLoadPercentageNoUpdate
  184. sta.w PercentageBuff
  185. rep #$31
  186. lda.w #2 ;print percentage bar
  187. jsr VwfCreateTextbox
  188. QD16UpdateLoadPercentageNoUpdate:
  189. sep #$20
  190. lda.b #%1
  191. ora.w Qd16Flags
  192. sta.w Qd16Flags ;set "rom upload commenced"-flag
  193. rts
  194. /*
  195. ? = loadstat
  196. 100 = bankcount
  197. */
  198. QD16UpdateLoadstatus:
  199. sep #$20
  200. xba
  201. sta.l $4202 ;current banks*100
  202. lda.b #100
  203. sta.l $4203
  204. nop
  205. nop
  206. nop
  207. nop
  208. rep #$31
  209. lda.l $4216
  210. sta.l $4204
  211. sep #$20
  212. lda.w QD16BankCount ;divide by 100
  213. sta.l $4206
  214. nop ;wait some cycles for division to finish
  215. nop
  216. nop
  217. nop
  218. nop
  219. nop
  220. nop
  221. nop
  222. rep #$31
  223. lda.l $4214
  224. sta.w Qd16LoadProgress
  225. QD16DivErr:
  226. rts
  227. QD16Cmd2:
  228. rts
  229. QD16FadeOut:
  230. sep #$20
  231. jsr PalEffectInit
  232. lda.b #13
  233. jsr PalEffectCreate
  234. rts
  235. .ends