vcopy.asm 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. ; this function seems to be used only once
  2. ; it store the address of a row and column of the VRAM background map in hl
  3. ; INPUT: h - row, l - column, b - high byte of background tile map address in VRAM
  4. GetRowColAddressBgMap::
  5. xor a
  6. srl h
  7. rr a
  8. srl h
  9. rr a
  10. srl h
  11. rr a
  12. or l
  13. ld l, a
  14. ld a, b
  15. or h
  16. ld h, a
  17. ret
  18. ; clears a VRAM background map with blank space tiles
  19. ; INPUT: h - high byte of background tile map address in VRAM
  20. ClearBgMap::
  21. ld a, " "
  22. jr .next
  23. ld a, l
  24. .next
  25. ld de, $400 ; size of VRAM background map
  26. ld l, e
  27. .loop
  28. ld [hli], a
  29. dec e
  30. jr nz, .loop
  31. dec d
  32. jr nz, .loop
  33. ret
  34. ; This function redraws a BG row of height 2 or a BG column of width 2.
  35. ; One of its main uses is redrawing the row or column that will be exposed upon
  36. ; scrolling the BG when the player takes a step. Redrawing only the exposed
  37. ; row or column is more efficient than redrawing the entire screen.
  38. ; However, this function is also called repeatedly to redraw the whole screen
  39. ; when necessary. It is also used in trade animation and elevator code.
  40. RedrawRowOrColumn::
  41. ld a, [hRedrawRowOrColumnMode]
  42. and a
  43. ret z
  44. ld b, a
  45. xor a
  46. ld [hRedrawRowOrColumnMode], a
  47. dec b
  48. jr nz, .redrawRow
  49. .redrawColumn
  50. ld hl, wRedrawRowOrColumnSrcTiles
  51. ld a, [hRedrawRowOrColumnDest]
  52. ld e, a
  53. ld a, [hRedrawRowOrColumnDest + 1]
  54. ld d, a
  55. ld c, SCREEN_HEIGHT
  56. .loop1
  57. ld a, [hli]
  58. ld [de], a
  59. inc de
  60. ld a, [hli]
  61. ld [de], a
  62. ld a, BG_MAP_WIDTH - 1
  63. add e
  64. ld e, a
  65. jr nc, .noCarry
  66. inc d
  67. .noCarry
  68. ; the following 4 lines wrap us from bottom to top if necessary
  69. ld a, d
  70. and $03
  71. or $98
  72. ld d, a
  73. dec c
  74. jr nz, .loop1
  75. xor a
  76. ld [hRedrawRowOrColumnMode], a
  77. ret
  78. .redrawRow
  79. ld hl, wRedrawRowOrColumnSrcTiles
  80. ld a, [hRedrawRowOrColumnDest]
  81. ld e, a
  82. ld a, [hRedrawRowOrColumnDest + 1]
  83. ld d, a
  84. push de
  85. call .DrawHalf ; draw upper half
  86. pop de
  87. ld a, BG_MAP_WIDTH ; width of VRAM background map
  88. add e
  89. ld e, a
  90. ; fall through and draw lower half
  91. .DrawHalf
  92. ld c, SCREEN_WIDTH / 2
  93. .loop2
  94. ld a, [hli]
  95. ld [de], a
  96. inc de
  97. ld a, [hli]
  98. ld [de], a
  99. ld a, e
  100. inc a
  101. ; the following 6 lines wrap us from the right edge to the left edge if necessary
  102. and $1f
  103. ld b, a
  104. ld a, e
  105. and $e0
  106. or b
  107. ld e, a
  108. dec c
  109. jr nz, .loop2
  110. ret
  111. ; This function automatically transfers tile number data from the tile map at
  112. ; wTileMap to VRAM during V-blank. Note that it only transfers one third of the
  113. ; background per V-blank. It cycles through which third it draws.
  114. ; This transfer is turned off when walking around the map, but is turned
  115. ; on when talking to sprites, battling, using menus, etc. This is because
  116. ; the above function, RedrawRowOrColumn, is used when walking to
  117. ; improve efficiency.
  118. AutoBgMapTransfer::
  119. ld a, [H_AUTOBGTRANSFERENABLED]
  120. and a
  121. ret z
  122. ld hl, sp + 0
  123. ld a, h
  124. ld [H_SPTEMP], a
  125. ld a, l
  126. ld [H_SPTEMP + 1], a ; save stack pinter
  127. ld a, [H_AUTOBGTRANSFERPORTION]
  128. and a
  129. jr z, .transferTopThird
  130. dec a
  131. jr z, .transferMiddleThird
  132. .transferBottomThird
  133. coord hl, 0, 12
  134. ld sp, hl
  135. ld a, [H_AUTOBGTRANSFERDEST + 1]
  136. ld h, a
  137. ld a, [H_AUTOBGTRANSFERDEST]
  138. ld l, a
  139. ld de, (12 * 32)
  140. add hl, de
  141. xor a ; TRANSFERTOP
  142. jr .doTransfer
  143. .transferTopThird
  144. coord hl, 0, 0
  145. ld sp, hl
  146. ld a, [H_AUTOBGTRANSFERDEST + 1]
  147. ld h, a
  148. ld a, [H_AUTOBGTRANSFERDEST]
  149. ld l, a
  150. ld a, TRANSFERMIDDLE
  151. jr .doTransfer
  152. .transferMiddleThird
  153. coord hl, 0, 6
  154. ld sp, hl
  155. ld a, [H_AUTOBGTRANSFERDEST + 1]
  156. ld h, a
  157. ld a, [H_AUTOBGTRANSFERDEST]
  158. ld l, a
  159. ld de, (6 * 32)
  160. add hl, de
  161. ld a, TRANSFERBOTTOM
  162. .doTransfer
  163. ld [H_AUTOBGTRANSFERPORTION], a ; store next portion
  164. ld b, 6
  165. TransferBgRows::
  166. ; unrolled loop and using pop for speed
  167. rept 20 / 2 - 1
  168. pop de
  169. ld [hl], e
  170. inc l
  171. ld [hl], d
  172. inc l
  173. endr
  174. pop de
  175. ld [hl], e
  176. inc l
  177. ld [hl], d
  178. ld a, 32 - (20 - 1)
  179. add l
  180. ld l, a
  181. jr nc, .ok
  182. inc h
  183. .ok
  184. dec b
  185. jr nz, TransferBgRows
  186. ld a, [H_SPTEMP]
  187. ld h, a
  188. ld a, [H_SPTEMP + 1]
  189. ld l, a
  190. ld sp, hl
  191. ret
  192. ; Copies [H_VBCOPYBGNUMROWS] rows from H_VBCOPYBGSRC to H_VBCOPYBGDEST.
  193. ; If H_VBCOPYBGSRC is XX00, the transfer is disabled.
  194. VBlankCopyBgMap::
  195. ld a, [H_VBCOPYBGSRC] ; doubles as enabling byte
  196. and a
  197. ret z
  198. ld hl, sp + 0
  199. ld a, h
  200. ld [H_SPTEMP], a
  201. ld a, l
  202. ld [H_SPTEMP + 1], a ; save stack pointer
  203. ld a, [H_VBCOPYBGSRC]
  204. ld l, a
  205. ld a, [H_VBCOPYBGSRC + 1]
  206. ld h, a
  207. ld sp, hl
  208. ld a, [H_VBCOPYBGDEST]
  209. ld l, a
  210. ld a, [H_VBCOPYBGDEST + 1]
  211. ld h, a
  212. ld a, [H_VBCOPYBGNUMROWS]
  213. ld b, a
  214. xor a
  215. ld [H_VBCOPYBGSRC], a ; disable transfer so it doesn't continue next V-blank
  216. jr TransferBgRows
  217. VBlankCopyDouble::
  218. ; Copy [H_VBCOPYDOUBLESIZE] 1bpp tiles
  219. ; from H_VBCOPYDOUBLESRC to H_VBCOPYDOUBLEDEST.
  220. ; While we're here, convert to 2bpp.
  221. ; The process is straightforward:
  222. ; copy each byte twice.
  223. ld a, [H_VBCOPYDOUBLESIZE]
  224. and a
  225. ret z
  226. ld hl, sp + 0
  227. ld a, h
  228. ld [H_SPTEMP], a
  229. ld a, l
  230. ld [H_SPTEMP + 1], a
  231. ld a, [H_VBCOPYDOUBLESRC]
  232. ld l, a
  233. ld a, [H_VBCOPYDOUBLESRC + 1]
  234. ld h, a
  235. ld sp, hl
  236. ld a, [H_VBCOPYDOUBLEDEST]
  237. ld l, a
  238. ld a, [H_VBCOPYDOUBLEDEST + 1]
  239. ld h, a
  240. ld a, [H_VBCOPYDOUBLESIZE]
  241. ld b, a
  242. xor a ; transferred
  243. ld [H_VBCOPYDOUBLESIZE], a
  244. .loop
  245. rept 3
  246. pop de
  247. ld [hl], e
  248. inc l
  249. ld [hl], e
  250. inc l
  251. ld [hl], d
  252. inc l
  253. ld [hl], d
  254. inc l
  255. endr
  256. pop de
  257. ld [hl], e
  258. inc l
  259. ld [hl], e
  260. inc l
  261. ld [hl], d
  262. inc l
  263. ld [hl], d
  264. inc hl
  265. dec b
  266. jr nz, .loop
  267. ld a, l
  268. ld [H_VBCOPYDOUBLEDEST], a
  269. ld a, h
  270. ld [H_VBCOPYDOUBLEDEST + 1], a
  271. ld hl, sp + 0
  272. ld a, l
  273. ld [H_VBCOPYDOUBLESRC], a
  274. ld a, h
  275. ld [H_VBCOPYDOUBLESRC + 1], a
  276. ld a, [H_SPTEMP]
  277. ld h, a
  278. ld a, [H_SPTEMP + 1]
  279. ld l, a
  280. ld sp, hl
  281. ret
  282. VBlankCopy::
  283. ; Copy [H_VBCOPYSIZE] 2bpp tiles (or 16 * [H_VBCOPYSIZE] tile map entries)
  284. ; from H_VBCOPYSRC to H_VBCOPYDEST.
  285. ; Source and destination addresses are updated,
  286. ; so transfer can continue in subsequent calls.
  287. ld a, [H_VBCOPYSIZE]
  288. and a
  289. ret z
  290. ld hl, sp + 0
  291. ld a, h
  292. ld [H_SPTEMP], a
  293. ld a, l
  294. ld [H_SPTEMP + 1], a
  295. ld a, [H_VBCOPYSRC]
  296. ld l, a
  297. ld a, [H_VBCOPYSRC + 1]
  298. ld h, a
  299. ld sp, hl
  300. ld a, [H_VBCOPYDEST]
  301. ld l, a
  302. ld a, [H_VBCOPYDEST + 1]
  303. ld h, a
  304. ld a, [H_VBCOPYSIZE]
  305. ld b, a
  306. xor a ; transferred
  307. ld [H_VBCOPYSIZE], a
  308. .loop
  309. rept 7
  310. pop de
  311. ld [hl], e
  312. inc l
  313. ld [hl], d
  314. inc l
  315. endr
  316. pop de
  317. ld [hl], e
  318. inc l
  319. ld [hl], d
  320. inc hl
  321. dec b
  322. jr nz, .loop
  323. ld a, l
  324. ld [H_VBCOPYDEST], a
  325. ld a, h
  326. ld [H_VBCOPYDEST + 1], a
  327. ld hl, sp + 0
  328. ld a, l
  329. ld [H_VBCOPYSRC], a
  330. ld a, h
  331. ld [H_VBCOPYSRC + 1], a
  332. ld a, [H_SPTEMP]
  333. ld h, a
  334. ld a, [H_SPTEMP + 1]
  335. ld l, a
  336. ld sp, hl
  337. ret
  338. UpdateMovingBgTiles::
  339. ; Animate water and flower
  340. ; tiles in the overworld.
  341. ld a, [hTilesetType]
  342. and a
  343. ret z ; no animations if indoors (or if a menu set this to 0)
  344. ld a, [hMovingBGTilesCounter1]
  345. inc a
  346. ld [hMovingBGTilesCounter1], a
  347. cp 20
  348. ret c
  349. cp 21
  350. jr z, .flower
  351. ; water
  352. ld hl, vTileset + $14 * $10
  353. ld c, $10
  354. ld a, [wMovingBGTilesCounter2]
  355. inc a
  356. and 7
  357. ld [wMovingBGTilesCounter2], a
  358. and 4
  359. jr nz, .left
  360. .right
  361. ld a, [hl]
  362. rrca
  363. ld [hli], a
  364. dec c
  365. jr nz, .right
  366. jr .done
  367. .left
  368. ld a, [hl]
  369. rlca
  370. ld [hli], a
  371. dec c
  372. jr nz, .left
  373. .done
  374. ld a, [hTilesetType]
  375. rrca
  376. ret nc
  377. ; if in a cave, no flower animations
  378. xor a
  379. ld [hMovingBGTilesCounter1], a
  380. ret
  381. .flower
  382. xor a
  383. ld [hMovingBGTilesCounter1], a
  384. ld a, [wMovingBGTilesCounter2]
  385. and 3
  386. cp 2
  387. ld hl, FlowerTile1
  388. jr c, .copy
  389. ld hl, FlowerTile2
  390. jr z, .copy
  391. ld hl, FlowerTile3
  392. .copy
  393. ld de, vTileset + $3 * $10
  394. ld c, $10
  395. .loop
  396. ld a, [hli]
  397. ld [de], a
  398. inc de
  399. dec c
  400. jr nz, .loop
  401. ret
  402. FlowerTile1: INCBIN "gfx/tilesets/flower/flower1.2bpp"
  403. FlowerTile2: INCBIN "gfx/tilesets/flower/flower2.2bpp"
  404. FlowerTile3: INCBIN "gfx/tilesets/flower/flower3.2bpp"