palettes.asm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. _RunPaletteCommand:
  2. call GetPredefRegisters
  3. ld a, b
  4. cp $ff
  5. jr nz, .next
  6. ld a, [wDefaultPaletteCommand] ; use default command if command ID is $ff
  7. .next
  8. cp UPDATE_PARTY_MENU_BLK_PACKET
  9. jp z, UpdatePartyMenuBlkPacket
  10. ld l, a
  11. ld h, 0
  12. add hl, hl
  13. ld de, SetPalFunctions
  14. add hl, de
  15. ld a, [hli]
  16. ld h, [hl]
  17. ld l, a
  18. ld de, SendSGBPackets
  19. push de
  20. jp hl
  21. SetPal_BattleBlack:
  22. ld hl, PalPacket_Black
  23. ld de, BlkPacket_Battle
  24. ret
  25. ; uses PalPacket_Empty to build a packet based on mon IDs and health color
  26. SetPal_Battle:
  27. ld hl, PalPacket_Empty
  28. ld de, wPalPacket
  29. ld bc, $10
  30. call CopyData
  31. ld a, [wPlayerBattleStatus3]
  32. ld hl, wBattleMonSpecies
  33. call DeterminePaletteID
  34. ld b, a
  35. ld a, [wEnemyBattleStatus3]
  36. ld hl, wEnemyMonSpecies2
  37. call DeterminePaletteID
  38. ld c, a
  39. ld hl, wPalPacket + 1
  40. ld a, [wPlayerHPBarColor]
  41. add PAL_GREENBAR
  42. ld [hli], a
  43. inc hl
  44. ld a, [wEnemyHPBarColor]
  45. add PAL_GREENBAR
  46. ld [hli], a
  47. inc hl
  48. ld a, b
  49. ld [hli], a
  50. inc hl
  51. ld a, c
  52. ld [hl], a
  53. ld hl, wPalPacket
  54. ld de, BlkPacket_Battle
  55. ld a, SET_PAL_BATTLE
  56. ld [wDefaultPaletteCommand], a
  57. ret
  58. SetPal_TownMap:
  59. ld hl, PalPacket_TownMap
  60. ld de, BlkPacket_WholeScreen
  61. ret
  62. ; uses PalPacket_Empty to build a packet based the mon ID
  63. SetPal_StatusScreen:
  64. ld hl, PalPacket_Empty
  65. ld de, wPalPacket
  66. ld bc, $10
  67. call CopyData
  68. ld a, [wcf91]
  69. cp VICTREEBEL + 1
  70. jr c, .pokemon
  71. ld a, $1 ; not pokemon
  72. .pokemon
  73. call DeterminePaletteIDOutOfBattle
  74. push af
  75. ld hl, wPalPacket + 1
  76. ld a, [wStatusScreenHPBarColor]
  77. add PAL_GREENBAR
  78. ld [hli], a
  79. inc hl
  80. pop af
  81. ld [hl], a
  82. ld hl, wPalPacket
  83. ld de, BlkPacket_StatusScreen
  84. ret
  85. SetPal_PartyMenu:
  86. ld hl, PalPacket_PartyMenu
  87. ld de, wPartyMenuBlkPacket
  88. ret
  89. SetPal_Pokedex:
  90. ld hl, PalPacket_Pokedex
  91. ld de, wPalPacket
  92. ld bc, $10
  93. call CopyData
  94. ld a, [wcf91]
  95. call DeterminePaletteIDOutOfBattle
  96. ld hl, wPalPacket + 3
  97. ld [hl], a
  98. ld hl, wPalPacket
  99. ld de, BlkPacket_Pokedex
  100. ret
  101. SetPal_Slots:
  102. ld hl, PalPacket_Slots
  103. ld de, BlkPacket_Slots
  104. ret
  105. SetPal_TitleScreen:
  106. ld hl, PalPacket_Titlescreen
  107. ld de, BlkPacket_Titlescreen
  108. ret
  109. ; used mostly for menus and the Oak intro
  110. SetPal_Generic:
  111. ld hl, PalPacket_Generic
  112. ld de, BlkPacket_WholeScreen
  113. ret
  114. SetPal_NidorinoIntro:
  115. ld hl, PalPacket_NidorinoIntro
  116. ld de, BlkPacket_NidorinoIntro
  117. ret
  118. SetPal_GameFreakIntro:
  119. ld hl, PalPacket_GameFreakIntro
  120. ld de, BlkPacket_GameFreakIntro
  121. ld a, SET_PAL_GENERIC
  122. ld [wDefaultPaletteCommand], a
  123. ret
  124. ; uses PalPacket_Empty to build a packet based on the current map
  125. SetPal_Overworld:
  126. ld hl, PalPacket_Empty
  127. ld de, wPalPacket
  128. ld bc, $10
  129. call CopyData
  130. ld a, [wCurMapTileset]
  131. cp CEMETERY
  132. jr z, .PokemonTowerOrAgatha
  133. cp CAVERN
  134. jr z, .caveOrBruno
  135. ld a, [wCurMap]
  136. cp REDS_HOUSE_1F
  137. jr c, .townOrRoute
  138. cp UNKNOWN_DUNGEON_2
  139. jr c, .normalDungeonOrBuilding
  140. cp NAME_RATERS_HOUSE
  141. jr c, .caveOrBruno
  142. cp LORELEIS_ROOM
  143. jr z, .Lorelei
  144. cp BRUNOS_ROOM
  145. jr z, .caveOrBruno
  146. .normalDungeonOrBuilding
  147. ld a, [wLastMap] ; town or route that current dungeon or building is located
  148. .townOrRoute
  149. cp SAFFRON_CITY + 1
  150. jr c, .town
  151. ld a, PAL_ROUTE - 1
  152. .town
  153. inc a ; a town's palette ID is its map ID + 1
  154. ld hl, wPalPacket + 1
  155. ld [hld], a
  156. ld de, BlkPacket_WholeScreen
  157. ld a, SET_PAL_OVERWORLD
  158. ld [wDefaultPaletteCommand], a
  159. ret
  160. .PokemonTowerOrAgatha
  161. ld a, PAL_GREYMON - 1
  162. jr .town
  163. .caveOrBruno
  164. ld a, PAL_CAVE - 1
  165. jr .town
  166. .Lorelei
  167. xor a
  168. jr .town
  169. ; used when a Pokemon is the only thing on the screen
  170. ; such as evolution, trading and the Hall of Fame
  171. SetPal_PokemonWholeScreen:
  172. push bc
  173. ld hl, PalPacket_Empty
  174. ld de, wPalPacket
  175. ld bc, $10
  176. call CopyData
  177. pop bc
  178. ld a, c
  179. and a
  180. ld a, PAL_BLACK
  181. jr nz, .next
  182. ld a, [wWholeScreenPaletteMonSpecies]
  183. call DeterminePaletteIDOutOfBattle
  184. .next
  185. ld [wPalPacket + 1], a
  186. ld hl, wPalPacket
  187. ld de, BlkPacket_WholeScreen
  188. ret
  189. SetPal_TrainerCard:
  190. ld hl, BlkPacket_TrainerCard
  191. ld de, wTrainerCardBlkPacket
  192. ld bc, $40
  193. call CopyData
  194. ld de, BadgeBlkDataLengths
  195. ld hl, wTrainerCardBlkPacket + 2
  196. ld a, [wObtainedBadges]
  197. ld c, 8
  198. .badgeLoop
  199. srl a
  200. push af
  201. jr c, .haveBadge
  202. ; The player doens't have the badge, so zero the badge's blk data.
  203. push bc
  204. ld a, [de]
  205. ld c, a
  206. xor a
  207. .zeroBadgeDataLoop
  208. ld [hli], a
  209. dec c
  210. jr nz, .zeroBadgeDataLoop
  211. pop bc
  212. jr .nextBadge
  213. .haveBadge
  214. ; The player does have the badge, so skip past the badge's blk data.
  215. ld a, [de]
  216. .skipBadgeDataLoop
  217. inc hl
  218. dec a
  219. jr nz, .skipBadgeDataLoop
  220. .nextBadge
  221. pop af
  222. inc de
  223. dec c
  224. jr nz, .badgeLoop
  225. ld hl, PalPacket_TrainerCard
  226. ld de, wTrainerCardBlkPacket
  227. ret
  228. SetPalFunctions:
  229. dw SetPal_BattleBlack
  230. dw SetPal_Battle
  231. dw SetPal_TownMap
  232. dw SetPal_StatusScreen
  233. dw SetPal_Pokedex
  234. dw SetPal_Slots
  235. dw SetPal_TitleScreen
  236. dw SetPal_NidorinoIntro
  237. dw SetPal_Generic
  238. dw SetPal_Overworld
  239. dw SetPal_PartyMenu
  240. dw SetPal_PokemonWholeScreen
  241. dw SetPal_GameFreakIntro
  242. dw SetPal_TrainerCard
  243. ; The length of the blk data of each badge on the Trainer Card.
  244. ; The Rainbow Badge has 3 entries because of its many colors.
  245. BadgeBlkDataLengths:
  246. db 6 ; Boulder Badge
  247. db 6 ; Cascade Badge
  248. db 6 ; Thunder Badge
  249. db 6 * 3 ; Rainbow Badge
  250. db 6 ; Soul Badge
  251. db 6 ; Marsh Badge
  252. db 6 ; Volcano Badge
  253. db 6 ; Earth Badge
  254. DeterminePaletteID:
  255. bit TRANSFORMED, a ; a is battle status 3
  256. ld a, PAL_GREYMON ; if the mon has used Transform, use Ditto's palette
  257. ret nz
  258. ld a, [hl]
  259. DeterminePaletteIDOutOfBattle:
  260. ld [wd11e], a
  261. and a ; is the mon index 0?
  262. jr z, .skipDexNumConversion
  263. push bc
  264. predef IndexToPokedex
  265. pop bc
  266. ld a, [wd11e]
  267. .skipDexNumConversion
  268. ld e, a
  269. ld d, 0
  270. ld hl, MonsterPalettes ; not just for Pokemon, Trainers use it too
  271. add hl, de
  272. ld a, [hl]
  273. ret
  274. InitPartyMenuBlkPacket:
  275. ld hl, BlkPacket_PartyMenu
  276. ld de, wPartyMenuBlkPacket
  277. ld bc, $30
  278. jp CopyData
  279. UpdatePartyMenuBlkPacket:
  280. ; Update the blk packet with the palette of the HP bar that is
  281. ; specified in [wWhichPartyMenuHPBar].
  282. ld hl, wPartyMenuHPBarColors
  283. ld a, [wWhichPartyMenuHPBar]
  284. ld e, a
  285. ld d, 0
  286. add hl, de
  287. ld e, l
  288. ld d, h
  289. ld a, [de]
  290. and a
  291. ld e, (1 << 2) | 1 ; green
  292. jr z, .next
  293. dec a
  294. ld e, (2 << 2) | 2 ; yellow
  295. jr z, .next
  296. ld e, (3 << 2) | 3 ; red
  297. .next
  298. push de
  299. ld hl, wPartyMenuBlkPacket + 8 + 1
  300. ld bc, 6
  301. ld a, [wWhichPartyMenuHPBar]
  302. call AddNTimes
  303. pop de
  304. ld [hl], e
  305. ret
  306. SendSGBPacket:
  307. ;check number of packets
  308. ld a, [hl]
  309. and $07
  310. ret z
  311. ; store number of packets in B
  312. ld b, a
  313. .loop2
  314. ; save B for later use
  315. push bc
  316. ; disable ReadJoypad to prevent it from interfering with sending the packet
  317. ld a, 1
  318. ld [hDisableJoypadPolling], a
  319. ; send RESET signal (P14=LOW, P15=LOW)
  320. xor a
  321. ld [rJOYP], a
  322. ; set P14=HIGH, P15=HIGH
  323. ld a, $30
  324. ld [rJOYP], a
  325. ;load length of packets (16 bytes)
  326. ld b, $10
  327. .nextByte
  328. ;set bit counter (8 bits per byte)
  329. ld e, $08
  330. ; get next byte in the packet
  331. ld a, [hli]
  332. ld d, a
  333. .nextBit0
  334. bit 0, d
  335. ; if 0th bit is not zero set P14=HIGH,P15=LOW (send bit 1)
  336. ld a, $10
  337. jr nz, .next0
  338. ; else (if 0th bit is zero) set P14=LOW,P15=HIGH (send bit 0)
  339. ld a, $20
  340. .next0
  341. ld [rJOYP], a
  342. ; must set P14=HIGH,P15=HIGH between each "pulse"
  343. ld a, $30
  344. ld [rJOYP], a
  345. ; rotation will put next bit in 0th position (so we can always use command
  346. ; "bit 0,d" to fetch the bit that has to be sent)
  347. rr d
  348. ; decrease bit counter so we know when we have sent all 8 bits of current byte
  349. dec e
  350. jr nz, .nextBit0
  351. dec b
  352. jr nz, .nextByte
  353. ; send bit 1 as a "stop bit" (end of parameter data)
  354. ld a, $20
  355. ld [rJOYP], a
  356. ; set P14=HIGH,P15=HIGH
  357. ld a, $30
  358. ld [rJOYP], a
  359. xor a
  360. ld [hDisableJoypadPolling], a
  361. ; wait for about 70000 cycles
  362. call Wait7000
  363. ; restore (previously pushed) number of packets
  364. pop bc
  365. dec b
  366. ; return if there are no more packets
  367. ret z
  368. ; else send 16 more bytes
  369. jr .loop2
  370. LoadSGB:
  371. xor a
  372. ld [wOnSGB], a
  373. call CheckSGB
  374. ret nc
  375. ld a, 1
  376. ld [wOnSGB], a
  377. ld a, [wGBC]
  378. and a
  379. jr z, .notGBC
  380. ret
  381. .notGBC
  382. di
  383. call PrepareSuperNintendoVRAMTransfer
  384. ei
  385. ld a, 1
  386. ld [wCopyingSGBTileData], a
  387. ld de, ChrTrnPacket
  388. ld hl, SGBBorderGraphics
  389. call CopyGfxToSuperNintendoVRAM
  390. xor a
  391. ld [wCopyingSGBTileData], a
  392. ld de, PctTrnPacket
  393. ld hl, BorderPalettes
  394. call CopyGfxToSuperNintendoVRAM
  395. xor a
  396. ld [wCopyingSGBTileData], a
  397. ld de, PalTrnPacket
  398. ld hl, SuperPalettes
  399. call CopyGfxToSuperNintendoVRAM
  400. call ClearVram
  401. ld hl, MaskEnCancelPacket
  402. jp SendSGBPacket
  403. PrepareSuperNintendoVRAMTransfer:
  404. ld hl, .packetPointers
  405. ld c, 9
  406. .loop
  407. push bc
  408. ld a, [hli]
  409. push hl
  410. ld h, [hl]
  411. ld l, a
  412. call SendSGBPacket
  413. pop hl
  414. inc hl
  415. pop bc
  416. dec c
  417. jr nz, .loop
  418. ret
  419. .packetPointers
  420. ; Only the first packet is needed.
  421. dw MaskEnFreezePacket
  422. dw DataSnd_72548
  423. dw DataSnd_72558
  424. dw DataSnd_72568
  425. dw DataSnd_72578
  426. dw DataSnd_72588
  427. dw DataSnd_72598
  428. dw DataSnd_725a8
  429. dw DataSnd_725b8
  430. CheckSGB:
  431. ; Returns whether the game is running on an SGB in carry.
  432. ld hl, MltReq2Packet
  433. di
  434. call SendSGBPacket
  435. ld a, 1
  436. ld [hDisableJoypadPolling], a
  437. ei
  438. call Wait7000
  439. ld a, [rJOYP]
  440. and $3
  441. cp $3
  442. jr nz, .isSGB
  443. ld a, $20
  444. ld [rJOYP], a
  445. ld a, [rJOYP]
  446. ld a, [rJOYP]
  447. call Wait7000
  448. call Wait7000
  449. ld a, $30
  450. ld [rJOYP], a
  451. call Wait7000
  452. call Wait7000
  453. ld a, $10
  454. ld [rJOYP], a
  455. ld a, [rJOYP]
  456. ld a, [rJOYP]
  457. ld a, [rJOYP]
  458. ld a, [rJOYP]
  459. ld a, [rJOYP]
  460. ld a, [rJOYP]
  461. call Wait7000
  462. call Wait7000
  463. ld a, $30
  464. ld [rJOYP], a
  465. ld a, [rJOYP]
  466. ld a, [rJOYP]
  467. ld a, [rJOYP]
  468. call Wait7000
  469. call Wait7000
  470. ld a, [rJOYP]
  471. and $3
  472. cp $3
  473. jr nz, .isSGB
  474. call SendMltReq1Packet
  475. and a
  476. ret
  477. .isSGB
  478. call SendMltReq1Packet
  479. scf
  480. ret
  481. SendMltReq1Packet:
  482. ld hl, MltReq1Packet
  483. call SendSGBPacket
  484. jp Wait7000
  485. CopyGfxToSuperNintendoVRAM:
  486. di
  487. push de
  488. call DisableLCD
  489. ld a, $e4
  490. ld [rBGP], a
  491. ld de, vChars1
  492. ld a, [wCopyingSGBTileData]
  493. and a
  494. jr z, .notCopyingTileData
  495. call CopySGBBorderTiles
  496. jr .next
  497. .notCopyingTileData
  498. ld bc, $1000
  499. call CopyData
  500. .next
  501. ld hl, vBGMap0
  502. ld de, $c
  503. ld a, $80
  504. ld c, $d
  505. .loop
  506. ld b, $14
  507. .innerLoop
  508. ld [hli], a
  509. inc a
  510. dec b
  511. jr nz, .innerLoop
  512. add hl, de
  513. dec c
  514. jr nz, .loop
  515. ld a, $e3
  516. ld [rLCDC], a
  517. pop hl
  518. call SendSGBPacket
  519. xor a
  520. ld [rBGP], a
  521. ei
  522. ret
  523. Wait7000:
  524. ; Each loop takes 9 cycles so this routine actually waits 63000 cycles.
  525. ld de, 7000
  526. .loop
  527. nop
  528. nop
  529. nop
  530. dec de
  531. ld a, d
  532. or e
  533. jr nz, .loop
  534. ret
  535. SendSGBPackets:
  536. ld a, [wGBC]
  537. and a
  538. jr z, .notGBC
  539. push de
  540. call InitGBCPalettes
  541. pop hl
  542. call EmptyFunc5
  543. ret
  544. .notGBC
  545. push de
  546. call SendSGBPacket
  547. pop hl
  548. jp SendSGBPacket
  549. InitGBCPalettes:
  550. ld a, $80 ; index 0 with auto-increment
  551. ld [rBGPI], a
  552. inc hl
  553. ld c, $20
  554. .loop
  555. ld a, [hli]
  556. inc hl
  557. add a
  558. add a
  559. add a
  560. ld de, SuperPalettes
  561. add e
  562. jr nc, .noCarry
  563. inc d
  564. .noCarry
  565. ld a, [de]
  566. ld [rBGPD], a
  567. dec c
  568. jr nz, .loop
  569. ret
  570. EmptyFunc5:
  571. ret
  572. CopySGBBorderTiles:
  573. ; SGB tile data is stored in a 4BPP planar format.
  574. ; Each tile is 32 bytes. The first 16 bytes contain bit planes 1 and 2, while
  575. ; the second 16 bytes contain bit planes 3 and 4.
  576. ; This function converts 2BPP planar data into this format by mapping
  577. ; 2BPP colors 0-3 to 4BPP colors 0-3. 4BPP colors 4-15 are not used.
  578. ld b, 128
  579. .tileLoop
  580. ; Copy bit planes 1 and 2 of the tile data.
  581. ld c, 16
  582. .copyLoop
  583. ld a, [hli]
  584. ld [de], a
  585. inc de
  586. dec c
  587. jr nz, .copyLoop
  588. ; Zero bit planes 3 and 4.
  589. ld c, 16
  590. xor a
  591. .zeroLoop
  592. ld [de], a
  593. inc de
  594. dec c
  595. jr nz, .zeroLoop
  596. dec b
  597. jr nz, .tileLoop
  598. ret
  599. INCLUDE "data/sgb_packets.asm"
  600. INCLUDE "data/mon_palettes.asm"
  601. INCLUDE "data/super_palettes.asm"
  602. INCLUDE "data/sgb_border.asm"