main.a65 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 main menu !
  24. jsr snes_init
  25. cli
  26. lda #$01
  27. sta $420d ; fast cpu
  28. jsr setup_gfx
  29. jsr colortest
  30. jsr tests
  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 menu_init
  50. jsr tests
  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 menuloop
  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 $420b
  136. stz $420c
  137. rts
  138. waitblank:
  139. php
  140. sep #$30 : .as : .xs
  141. - lda $4212
  142. and #$80
  143. bne -
  144. - lda $4212
  145. and #$80
  146. beq -
  147. plp
  148. rts
  149. colortest:
  150. sep #$20 : .as
  151. rep #$10 : .xl
  152. stz $2130
  153. rts
  154. setup_gfx:
  155. sep #$20 : .as
  156. rep #$10 : .xl
  157. stz $4200
  158. stz $420b
  159. stz $420c
  160. ;clear tilemap buffers
  161. ldx #$8000
  162. stx $2181
  163. lda #$00
  164. sta $2183
  165. DMA0(#$08, #$8000, #^zero, #!zero, #$80)
  166. ;generate fonts
  167. jsr genfonts
  168. ;clear BG1 tilemap
  169. ldx #BG1_TILE_BASE
  170. stx $2116
  171. DMA0(#$09, #$1000, #^zero, #!zero, #$18)
  172. ;clear BG2 tilemap
  173. ldx #BG2_TILE_BASE
  174. stx $2116
  175. DMA0(#$09, #$1000, #^zero, #!zero, #$18)
  176. ;clear OAM tables
  177. ldx #$0000
  178. stx $2102
  179. DMA0(#$08, #$544, #^zero, #!zero, #$04)
  180. ;copy logo tiles
  181. ldx #$2000
  182. stx $2116
  183. DMA0(#$01, #$4000, #^logo, #!logo, #$18)
  184. ;generate logo tilemap
  185. ldx #BG1_TILE_BASE
  186. stx $2116
  187. ldx #$0100
  188. - stx $2118
  189. inx
  190. cpx #$01e0
  191. bne -
  192. ;copy sprites tiles
  193. ldx #OAM_TILE_BASE
  194. stx $2116
  195. DMA0(#$01, #$500, #^logospr, #!logospr, #$18)
  196. ;set OAM tables
  197. ldx #$0000
  198. stx $2102
  199. DMA0(#$00, #$60, #^oam_data_l, #!oam_data_l, #$04)
  200. ldx #$0100
  201. stx $2102
  202. DMA0(#$00, #$09, #^oam_data_h, #!oam_data_h, #$04)
  203. ;set palette
  204. stz $2121
  205. DMA0(#$00, #$200, #^palette, #!palette, #$22)
  206. stz $2121
  207. ;copy hdma tables so we can work "without" the cartridge
  208. ;palette
  209. lda #^hdma_pal
  210. ldx #!hdma_pal
  211. stx $2181
  212. sta $2183
  213. DMA0(#$00, #52, #^hdma_pal_src, #!hdma_pal_src, #$80)
  214. ;CG addr for palette
  215. lda #^hdma_cg_addr
  216. ldx #!hdma_cg_addr
  217. stx $2181
  218. sta $2183
  219. DMA0(#$00, #227, #^hdma_cg_addr_src, #!hdma_cg_addr_src, #$80)
  220. ;screen mode
  221. lda #^hdma_mode
  222. ldx #!hdma_mode
  223. stx $2181
  224. sta $2183
  225. DMA0(#$00, #5, #^hdma_mode_src, #!hdma_mode_src, #$80)
  226. ;bg scroll
  227. lda #^hdma_scroll
  228. ldx #!hdma_scroll
  229. stx $2181
  230. sta $2183
  231. DMA0(#$00, #11, #^hdma_scroll_src, #!hdma_scroll_src, #$80);
  232. ;color math
  233. lda #^hdma_math
  234. ldx #!hdma_math
  235. stx $2181
  236. sta $2183
  237. DMA0(#$00, #19, #^hdma_math_src, #!hdma_math_src, #$80);
  238. ;copy infinite loop to WRAM
  239. lda #$80
  240. sta infloop
  241. lda #$fe
  242. sta infloop+1
  243. lda #^wram_fadeloop
  244. ldx #!wram_fadeloop
  245. stx $2181
  246. sta $2183
  247. DMA0(#$00, #$6C, #^fadeloop, #!fadeloop, #$80);
  248. rts
  249. tests:
  250. sep #$20 : .as ;8-bit accumulator
  251. rep #$10 : .xl ;16-bit index
  252. lda #$03 ;mode 3, mode 5 via HDMA
  253. sta $2105
  254. lda #$58 ;Tilemap addr 0xB000
  255. ora #$02 ;SC size 32x64
  256. sta $2107 ;for BG1
  257. lda #$50 ;Tilemap addr 0xA000
  258. ora #$02 ;SC size 32x64
  259. sta $2108 ;for BG2
  260. lda #$40 ;chr base addr:
  261. sta $210b ;BG1=0x0000, BG2=0x8000
  262. lda #$13 ;enable BG1+BG2+OBJ
  263. sta $212c ;BG Main
  264. lda #$13 ;enable BG1+BG2+OBJ
  265. sta $212d ;BG Sub
  266. lda #$20 ;Window 1 for color
  267. sta $2125 ;Color window
  268. lda #$01 ;cut off leftmost subscreen pixel garbage
  269. sta $2126
  270. lda #$fe
  271. sta $2127
  272. jsr setup_224
  273. lda #$10
  274. sta $2130
  275. lda #$1f
  276. sta $212e
  277. sta $212f
  278. ; stz $2121
  279. lda #8
  280. sta bar_yl
  281. stz cur_bright
  282. stz tgt_bright
  283. rts
  284. screen_on:
  285. stz $2100 ;screen on, 0% brightness
  286. lda #$0f
  287. sta tgt_bright
  288. rts
  289. snes_init:
  290. sep #$20 : .as ;8-bit accumulator
  291. rep #$10 : .xl ;16-bit index
  292. stz $4200 ;
  293. lda #$ff
  294. sta $4201 ;
  295. stz $4202 ;
  296. stz $4203 ;
  297. stz $4204 ;
  298. stz $4205 ;
  299. stz $4206 ;
  300. stz $4207 ;
  301. stz $4208 ;
  302. stz $4209 ;
  303. stz $420a ;
  304. stz $420b ;
  305. stz $420c ;
  306. stz $420d ;
  307. lda #$8f
  308. sta $2100 ;INIDISP: force blank
  309. lda #$03 ; 8x8+16x16; name=0; base=3
  310. sta $2101 ;
  311. stz $2102 ;
  312. stz $2103 ;
  313. ; stz $2104 ; (OAM Data?!)
  314. ; stz $2104 ; (OAM Data?!)
  315. stz $2105 ;
  316. stz $2106 ;
  317. stz $2107 ;
  318. stz $2108 ;
  319. stz $2109 ;
  320. stz $210a ;
  321. stz $210b ;
  322. stz $210c ;
  323. stz $210d ;
  324. stz $210d ;
  325. stz $210e ;
  326. stz $210e ;
  327. stz $210f ;
  328. stz $210f ;
  329. lda #$05
  330. sta $2110 ;
  331. stz $2110 ;
  332. stz $2111 ;
  333. stz $2111 ;
  334. stz $2112 ;
  335. stz $2112 ;
  336. stz $2113 ;
  337. stz $2113 ;
  338. stz $2114 ;
  339. stz $2114 ;
  340. lda #$80
  341. sta $2115 ;
  342. stz $2116 ;
  343. stz $2117 ;
  344. ; stz $2118 ;(VRAM Data?!)
  345. ; stz $2119 ;(VRAM Data?!)
  346. stz $211a ;
  347. stz $211b ;
  348. lda #$01
  349. sta $211b ;
  350. stz $211c ;
  351. stz $211c ;
  352. stz $211d ;
  353. stz $211d ;
  354. stz $211e ;
  355. sta $211e ;
  356. stz $211f ;
  357. stz $211f ;
  358. stz $2120 ;
  359. stz $2120 ;
  360. stz $2121 ;
  361. ; stz $2122 ; (CG Data?!)
  362. ; stz $2122 ; (CG Data?!)
  363. stz $2123 ;
  364. stz $2124 ;
  365. stz $2125 ;
  366. stz $2126 ;
  367. stz $2127 ;
  368. stz $2128 ;
  369. stz $2129 ;
  370. stz $212a ;
  371. stz $212b ;
  372. stz $212c ;
  373. stz $212d ;
  374. stz $212e ;
  375. stz $212f ;
  376. lda #$30
  377. sta $2130 ;
  378. stz $2131 ;
  379. lda #$e0
  380. sta $2132 ;
  381. stz $2133 ;
  382. ;clear WRAM lower page
  383. ; ldx #$0200
  384. ; stx $2181
  385. ; lda #$00
  386. ; sta $2183
  387. ; DMA0(#$08, #$FF00, #^zero, #!zero, #$80)
  388. ; ldx #$0000
  389. ; stx $2181
  390. ; lda #$00
  391. ; sta $2183
  392. ; DMA0(#$08, #$1e0, #^zero, #!zero, #$80)
  393. rts
  394. fadeloop:
  395. sep #$30 : .as : .xs
  396. ldx cur_bright
  397. and #$00
  398. pha
  399. plb
  400. lda #$7f
  401. sta hdma_math_selection+2
  402. lda #$30
  403. sta hdma_math_selection+1
  404. lda #$bf
  405. sta hdma_math+1
  406. sta hdma_math+4
  407. sta hdma_math+16
  408. lda #$e0
  409. sta hdma_math+2
  410. sta hdma_math+5
  411. sta hdma_math+17
  412. fadeloop_start
  413. lda $4212
  414. and #$80
  415. bne fadeloop_start
  416. - lda $4212
  417. and #$80
  418. bne +
  419. bra -
  420. + lda hdma_math+2
  421. inc
  422. inc
  423. sta hdma_math+2
  424. sta hdma_math+5
  425. sta hdma_math+17
  426. txa
  427. dec
  428. sta $2100
  429. tax
  430. bne fadeloop_start
  431. stz $2126
  432. stz $212e
  433. stz $2131
  434. stz $2130
  435. lda #$00
  436. sta @hdma_math
  437. stz $2100
  438. - bra -
  439. fadeloop_end:
  440. .byt $ff
  441. genfonts:
  442. php
  443. rep #$10 : .xl
  444. sep #$20 : .as
  445. ;clear VRAM font areas
  446. ldx #$0000
  447. stx $2116
  448. DMA0(#$09, #$4000, #^zero, #!zero, #$18)
  449. ldx #$4000
  450. stx $2116
  451. DMA0(#$09, #$2000, #^zero, #!zero, #$18)
  452. sep #$10 : .xs
  453. rep #$20 : .al
  454. stz $2116
  455. ldx #$01
  456. stx $4300
  457. ldx #^font
  458. stx $4304
  459. lda #!font
  460. sta $4302
  461. lda #$0010
  462. sta $4305
  463. ldx #$18
  464. stx $4301
  465. lda #$0000
  466. - sta $2116
  467. ldx #$10
  468. stx $4305
  469. ldx #$01
  470. stx $420b
  471. clc
  472. adc #$20
  473. cmp #$2000
  474. bne -
  475. ldx #^font
  476. stx $4304
  477. lda #!font
  478. sta $4302
  479. lda #$4000
  480. - sta $2116
  481. ldx #$10
  482. stx $4305
  483. ldx #$01
  484. stx $420b
  485. clc
  486. adc #$10
  487. cmp #$5000
  488. bne -
  489. plp
  490. rts