main.a65 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. #include "memmap.i65"
  2. #include "dma.i65"
  3. GAME_MAIN:
  4. sep #$20 : .as
  5. lda #$00
  6. sta @MCU_CMD ; clear MCU command register
  7. rep #$20 : .al
  8. lda #$0000
  9. sta @MCU_PARAM ; clear MCU command parameters
  10. sta @MCU_PARAM+2
  11. sep #$20 : .as
  12. stz $4200 ; inhibit VBlank NMI
  13. rep #$20 : .al
  14. lda @warm_signature ; Was CMD_RESET issued before reset?
  15. cmp #$fa50 ; If yes, then perform warm boot procedure
  16. bne coldboot
  17. lda #$0000
  18. sta @warm_signature
  19. lda @saved_sp ; Restore previous stack pointer
  20. tcs
  21. sep #$20 : .as
  22. ; jsr killdma ; The following initialization processes must not touch memory
  23. jsr waitblank ; structures used by the file selector !
  24. jsr snes_init
  25. cli
  26. lda #$01
  27. sta $420d ; fast cpu
  28. jsr setup_gfx
  29. jsr colortest
  30. jsr video_init
  31. jsr setup_hdma
  32. lda #$0f
  33. sta cur_bright
  34. sta tgt_bright
  35. sta $2100
  36. jmp @set_bank ; Set bios bank, just to be sure
  37. set_bank:
  38. plp ; Restore processor state
  39. rts ; Jump to the routine which called the sub-routine issuing CMD_RESET
  40. coldboot: ; Regular, cold-start init
  41. sep #$20 : .as
  42. ; jsr killdma
  43. jsr waitblank
  44. jsr snes_init
  45. lda #$01
  46. sta $420d ; fast cpu
  47. jsr setup_gfx
  48. jsr colortest
  49. jsr filesel_init
  50. jsr video_init
  51. jsr setup_hdma
  52. jsr screen_on
  53. sep #$20 : .as
  54. lda @RTC_STATUS
  55. beq +
  56. jsl time_init
  57. +
  58. jsr fileselloop
  59. cli
  60. stz $4200
  61. jmp @infloop ;infinite loop in WRAM
  62. killdma:
  63. stz $4300
  64. stz $4301
  65. stz $4302
  66. stz $4303
  67. stz $4304
  68. stz $4305
  69. stz $4306
  70. stz $4307
  71. stz $4308
  72. stz $4309
  73. stz $430a
  74. stz $430b
  75. stz $4310
  76. stz $4311
  77. stz $4312
  78. stz $4313
  79. stz $4314
  80. stz $4315
  81. stz $4316
  82. stz $4317
  83. stz $4318
  84. stz $4319
  85. stz $431a
  86. stz $431b
  87. stz $4320
  88. stz $4321
  89. stz $4322
  90. stz $4323
  91. stz $4324
  92. stz $4325
  93. stz $4326
  94. stz $4327
  95. stz $4328
  96. stz $4329
  97. stz $432a
  98. stz $432b
  99. stz $4330
  100. stz $4331
  101. stz $4332
  102. stz $4333
  103. stz $4334
  104. stz $4335
  105. stz $4336
  106. stz $4337
  107. stz $4338
  108. stz $4339
  109. stz $433a
  110. stz $433b
  111. stz $4340
  112. stz $4341
  113. stz $4342
  114. stz $4343
  115. stz $4344
  116. stz $4345
  117. stz $4346
  118. stz $4347
  119. stz $4348
  120. stz $4349
  121. stz $434a
  122. stz $434b
  123. stz $4350
  124. stz $4351
  125. stz $4352
  126. stz $4353
  127. stz $4354
  128. stz $4355
  129. stz $4356
  130. stz $4357
  131. stz $4358
  132. stz $4359
  133. stz $435a
  134. stz $435b
  135. stz $4360
  136. stz $4361
  137. stz $4362
  138. stz $4363
  139. stz $4364
  140. stz $4365
  141. stz $4366
  142. stz $4367
  143. stz $4368
  144. stz $4369
  145. stz $436a
  146. stz $436b
  147. stz $4370
  148. stz $4371
  149. stz $4372
  150. stz $4373
  151. stz $4374
  152. stz $4375
  153. stz $4376
  154. stz $4377
  155. stz $4378
  156. stz $4379
  157. stz $437a
  158. stz $437b
  159. stz $420b
  160. stz $420c
  161. rts
  162. waitblank:
  163. php
  164. sep #$30 : .as : .xs
  165. - lda $4212
  166. and #$80
  167. bne -
  168. - lda $4212
  169. and #$80
  170. beq -
  171. plp
  172. rts
  173. colortest:
  174. sep #$20 : .as
  175. rep #$10 : .xl
  176. stz $2130
  177. rts
  178. setup_gfx:
  179. sep #$20 : .as
  180. rep #$10 : .xl
  181. stz $4200
  182. stz $420b
  183. stz $420c
  184. ;clear tilemap buffers
  185. ldx #$8000
  186. stx $2181
  187. lda #$00
  188. sta $2183
  189. DMA0(#$08, #$8000, #^zero, #!zero, #$80)
  190. ;generate fonts
  191. jsr genfonts
  192. ;clear BG1 tilemap
  193. ldx #BG1_TILE_BASE
  194. stx $2116
  195. DMA0(#$09, #$1000, #^zero, #!zero, #$18)
  196. ;clear BG2 tilemap
  197. ldx #BG2_TILE_BASE
  198. stx $2116
  199. DMA0(#$09, #$1000, #^zero, #!zero, #$18)
  200. ;clear OAM tables
  201. ldx #$0000
  202. stx $2102
  203. DMA0(#$08, #$220, #^zero, #!zero, #$04)
  204. ;copy logo tiles
  205. ldx #$2000
  206. stx $2116
  207. DMA0(#$01, #$4000, #^logo, #!logo, #$18)
  208. ;generate logo tilemap
  209. ldx #BG1_TILE_BASE
  210. stx $2116
  211. ldx #$0100
  212. - stx $2118
  213. inx
  214. cpx #$01e0
  215. bne -
  216. ;copy sprites tiles
  217. ldx #OAM_TILE_BASE
  218. stx $2116
  219. DMA0(#$01, #$500, #^logospr, #!logospr, #$18)
  220. ;set OAM tables
  221. ldx #$0000
  222. stx $2102
  223. DMA0(#$00, #$60, #^oam_data_l, #!oam_data_l, #$04)
  224. ldx #$0100
  225. stx $2102
  226. DMA0(#$00, #$09, #^oam_data_h, #!oam_data_h, #$04)
  227. ;set palette
  228. stz $2121
  229. DMA0(#$00, #$200, #^palette, #!palette, #$22)
  230. stz $2121
  231. ;copy hdma tables so we can work "without" the cartridge
  232. ;palette
  233. lda #^hdma_pal
  234. ldx #!hdma_pal
  235. stx $2181
  236. sta $2183
  237. DMA0(#$00, #52, #^hdma_pal_src, #!hdma_pal_src, #$80)
  238. ;CG addr for palette
  239. lda #^hdma_cg_addr
  240. ldx #!hdma_cg_addr
  241. stx $2181
  242. sta $2183
  243. DMA0(#$00, #227, #^hdma_cg_addr_src, #!hdma_cg_addr_src, #$80)
  244. ;screen mode
  245. lda #^hdma_mode
  246. ldx #!hdma_mode
  247. stx $2181
  248. sta $2183
  249. DMA0(#$00, #5, #^hdma_mode_src, #!hdma_mode_src, #$80)
  250. ;bg scroll
  251. lda #^hdma_scroll
  252. ldx #!hdma_scroll
  253. stx $2181
  254. sta $2183
  255. DMA0(#$00, #11, #^hdma_scroll_src, #!hdma_scroll_src, #$80);
  256. ;color math
  257. lda #^hdma_math
  258. ldx #!hdma_math
  259. stx $2181
  260. sta $2183
  261. DMA0(#$00, #19, #^hdma_math_src, #!hdma_math_src, #$80);
  262. ;copy infinite loop to WRAM
  263. lda #$80
  264. sta infloop
  265. lda #$fe
  266. sta infloop+1
  267. lda #^wram_fadeloop
  268. ldx #!wram_fadeloop
  269. stx $2181
  270. sta $2183
  271. DMA0(#$00, #$6C, #^fadeloop, #!fadeloop, #$80);
  272. rts
  273. video_init:
  274. sep #$20 : .as ;8-bit accumulator
  275. rep #$10 : .xl ;16-bit index
  276. lda #$03 ;mode 3, mode 5 via HDMA
  277. sta $2105
  278. lda #$58 ;Tilemap addr 0xB000
  279. ora #$02 ;SC size 32x64
  280. sta $2107 ;for BG1
  281. lda #$50 ;Tilemap addr 0xA000
  282. ora #$02 ;SC size 32x64
  283. sta $2108 ;for BG2
  284. lda #$40 ;chr base addr:
  285. sta $210b ;BG1=0x0000, BG2=0x8000
  286. lda #$13 ;enable BG1+BG2+OBJ
  287. sta $212c ;BG Main
  288. lda #$13 ;enable BG1+BG2+OBJ
  289. sta $212d ;BG Sub
  290. lda #$20 ;Window 1 for color
  291. sta $2125 ;Color window
  292. lda #$01 ;cut off leftmost subscreen pixel garbage
  293. sta $2126
  294. lda #$fe
  295. sta $2127
  296. jsr setup_224
  297. lda #$10
  298. sta $2130
  299. lda #$1f
  300. sta $212e
  301. sta $212f
  302. ; stz $2121
  303. lda #8
  304. sta bar_yl
  305. stz cur_bright
  306. stz tgt_bright
  307. rts
  308. screen_on:
  309. stz $2100 ;screen on, 0% brightness
  310. lda #$0f
  311. sta tgt_bright
  312. rts
  313. snes_init:
  314. sep #$20 : .as ;8-bit accumulator
  315. rep #$10 : .xl ;16-bit index
  316. stz $4200 ;
  317. lda #$ff
  318. sta $4201 ;
  319. stz $4202 ;
  320. stz $4203 ;
  321. stz $4204 ;
  322. stz $4205 ;
  323. stz $4206 ;
  324. stz $4207 ;
  325. stz $4208 ;
  326. stz $4209 ;
  327. stz $420a ;
  328. stz $420b ;
  329. stz $420c ;
  330. stz $420d ;
  331. lda #$8f
  332. sta $2100 ;INIDISP: force blank
  333. lda #$03 ; 8x8+16x16; name=0; base=3
  334. sta $2101 ;
  335. stz $2102 ;
  336. stz $2103 ;
  337. ; stz $2104 ; (OAM Data?!)
  338. ; stz $2104 ; (OAM Data?!)
  339. stz $2105 ;
  340. stz $2106 ;
  341. stz $2107 ;
  342. stz $2108 ;
  343. stz $2109 ;
  344. stz $210a ;
  345. stz $210b ;
  346. stz $210c ;
  347. stz $210d ;
  348. stz $210d ;
  349. stz $210e ;
  350. stz $210e ;
  351. stz $210f ;
  352. stz $210f ;
  353. lda #$05
  354. sta $2110 ;
  355. stz $2110 ;
  356. stz $2111 ;
  357. stz $2111 ;
  358. stz $2112 ;
  359. stz $2112 ;
  360. stz $2113 ;
  361. stz $2113 ;
  362. stz $2114 ;
  363. stz $2114 ;
  364. lda #$80
  365. sta $2115 ;
  366. stz $2116 ;
  367. stz $2117 ;
  368. ; stz $2118 ;(VRAM Data?!)
  369. ; stz $2119 ;(VRAM Data?!)
  370. stz $211a ;
  371. stz $211b ;
  372. lda #$01
  373. sta $211b ;
  374. stz $211c ;
  375. stz $211c ;
  376. stz $211d ;
  377. stz $211d ;
  378. stz $211e ;
  379. sta $211e ;
  380. stz $211f ;
  381. stz $211f ;
  382. stz $2120 ;
  383. stz $2120 ;
  384. stz $2121 ;
  385. ; stz $2122 ; (CG Data?!)
  386. ; stz $2122 ; (CG Data?!)
  387. stz $2123 ;
  388. stz $2124 ;
  389. stz $2125 ;
  390. stz $2126 ;
  391. stz $2127 ;
  392. stz $2128 ;
  393. stz $2129 ;
  394. stz $212a ;
  395. stz $212b ;
  396. stz $212c ;
  397. stz $212d ;
  398. stz $212e ;
  399. stz $212f ;
  400. lda #$30
  401. sta $2130 ;
  402. stz $2131 ;
  403. lda #$e0
  404. sta $2132 ;
  405. stz $2133 ;
  406. ;clear WRAM lower page
  407. ; ldx #$0200
  408. ; stx $2181
  409. ; lda #$00
  410. ; sta $2183
  411. ; DMA0(#$08, #$FF00, #^zero, #!zero, #$80)
  412. ; ldx #$0000
  413. ; stx $2181
  414. ; lda #$00
  415. ; sta $2183
  416. ; DMA0(#$08, #$1e0, #^zero, #!zero, #$80)
  417. rts
  418. fadeloop:
  419. sep #$30 : .as : .xs
  420. ldx cur_bright
  421. and #$00
  422. pha
  423. plb
  424. lda #$7f
  425. sta hdma_math_selection+2
  426. lda #$30
  427. sta hdma_math_selection+1
  428. lda #$bf
  429. sta hdma_math+1
  430. sta hdma_math+4
  431. sta hdma_math+16
  432. lda #$e0
  433. sta hdma_math+2
  434. sta hdma_math+5
  435. sta hdma_math+17
  436. fadeloop_start
  437. lda $4212
  438. and #$80
  439. bne fadeloop_start
  440. - lda $4212
  441. and #$80
  442. bne +
  443. bra -
  444. + lda hdma_math+2
  445. inc
  446. inc
  447. sta hdma_math+2
  448. sta hdma_math+5
  449. sta hdma_math+17
  450. txa
  451. dec
  452. sta $2100
  453. tax
  454. bne fadeloop_start
  455. stz $2126
  456. stz $212e
  457. stz $2131
  458. stz $2130
  459. lda #$00
  460. sta @hdma_math
  461. stz $2100
  462. - bra -
  463. fadeloop_end:
  464. .byt $ff
  465. genfonts:
  466. php
  467. rep #$10 : .xl
  468. sep #$20 : .as
  469. ;clear VRAM font areas
  470. ldx #$0000
  471. stx $2116
  472. DMA0(#$09, #$4000, #^zero, #!zero, #$18)
  473. ldx #$4000
  474. stx $2116
  475. DMA0(#$09, #$2000, #^zero, #!zero, #$18)
  476. sep #$10 : .xs
  477. rep #$20 : .al
  478. stz $2116
  479. ldx #$01
  480. stx $4300
  481. ldx #^font
  482. stx $4304
  483. lda #!font
  484. sta $4302
  485. lda #$0010
  486. sta $4305
  487. ldx #$18
  488. stx $4301
  489. lda #$0000
  490. - sta $2116
  491. ldx #$10
  492. stx $4305
  493. ldx #$01
  494. stx $420b
  495. clc
  496. adc #$20
  497. cmp #$2000
  498. bne -
  499. ldx #^font
  500. stx $4304
  501. lda #!font
  502. sta $4302
  503. lda #$4000
  504. - sta $2116
  505. ldx #$10
  506. stx $4305
  507. ldx #$01
  508. stx $420b
  509. clc
  510. adc #$10
  511. cmp #$5000
  512. bne -
  513. plp
  514. rts