forked from pret/pokecrystal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgfx.asm
392 lines (326 loc) · 5.08 KB
/
gfx.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
DEF TILES_PER_CYCLE EQU 8
DEF MOBILE_TILES_PER_CYCLE EQU 6
Get2bppViaHDMA::
ldh a, [rLCDC]
bit rLCDC_ENABLE, a
jp z, Copy2bpp
homecall HDMATransfer2bpp
ret
Get1bppViaHDMA::
ldh a, [rLCDC]
bit rLCDC_ENABLE, a
jp z, Copy1bpp
homecall HDMATransfer1bpp
ret
FarCopyBytesDouble_DoubleBankSwitch::
ldh [hTempBank], a
ldh a, [hROMBank]
push af
ldh a, [hTempBank]
rst Bankswitch
call FarCopyBytesDouble
pop af
rst Bankswitch
ret
SafeHDMATransfer: ; unreferenced
dec c
ldh a, [hBGMapMode]
push af
xor a
ldh [hBGMapMode], a
ldh a, [hROMBank]
push af
ld a, b
rst Bankswitch
.loop
; load the source and target MSB and LSB
ld a, d
ldh [rHDMA1], a ; source MSB
ld a, e
and $f0
ldh [rHDMA2], a ; source LSB
ld a, h
and $1f
ldh [rHDMA3], a ; target MSB
ld a, l
and $f0
ldh [rHDMA4], a ; target LSB
; stop when c < TILES_PER_CYCLE
ld a, c
cp TILES_PER_CYCLE
jr c, .done
; decrease c by TILES_PER_CYCLE
sub TILES_PER_CYCLE
ld c, a
; DMA transfer state
ld a, $f
ldh [hDMATransfer], a
call DelayFrame
; add $100 to hl and de
ld a, l
add LOW($100)
ld l, a
ld a, h
adc HIGH($100)
ld h, a
ld a, e
add LOW($100)
ld e, a
ld a, d
adc HIGH($100)
ld d, a
jr .loop
.done
ld a, c
and $7f ; pretty silly, considering at most bits 0-2 would be set
ldh [hDMATransfer], a
call DelayFrame
pop af
rst Bankswitch
pop af
ldh [hBGMapMode], a
ret
UpdatePlayerSprite::
farcall _UpdatePlayerSprite
ret
LoadStandardFont::
farcall _LoadStandardFont
ret
LoadFontsBattleExtra::
farcall _LoadFontsBattleExtra
ret
LoadFontsExtra::
farcall _LoadFontsExtra1
farcall _LoadFontsExtra2
ret
LoadFontsExtra2: ; unreferenced
farcall _LoadFontsExtra2
ret
DecompressRequest2bpp::
push de
ld a, BANK(sScratch)
call OpenSRAM
push bc
ld de, sScratch
ld a, b
call FarDecompress
pop bc
pop hl
ld de, sScratch
call Request2bpp
call CloseSRAM
ret
FarCopyBytes::
; copy bc bytes from a:hl to de
ldh [hTempBank], a
ldh a, [hROMBank]
push af
ldh a, [hTempBank]
rst Bankswitch
call CopyBytes
pop af
rst Bankswitch
ret
FarCopyBytesDouble:
; Copy bc bytes from a:hl to bc*2 bytes at de,
; doubling each byte in the process.
ldh [hTempBank], a
ldh a, [hROMBank]
push af
ldh a, [hTempBank]
rst Bankswitch
; switcheroo, de <> hl
ld a, h
ld h, d
ld d, a
ld a, l
ld l, e
ld e, a
inc b
inc c
jr .dec
.loop
ld a, [de]
inc de
ld [hli], a
ld [hli], a
.dec
dec c
jr nz, .loop
dec b
jr nz, .loop
pop af
rst Bankswitch
ret
Request2bpp::
; Load 2bpp at b:de to occupy c tiles of hl.
ldh a, [hBGMapMode]
push af
xor a
ldh [hBGMapMode], a
ldh a, [hROMBank]
push af
ld a, b
rst Bankswitch
ldh a, [hTilesPerCycle]
push af
ld a, TILES_PER_CYCLE
ldh [hTilesPerCycle], a
ld a, [wLinkMode]
cp LINK_MOBILE
jr nz, .NotMobile
ldh a, [hMobile]
and a
jr nz, .NotMobile
ld a, MOBILE_TILES_PER_CYCLE
ldh [hTilesPerCycle], a
.NotMobile:
ld a, e
ld [wRequested2bppSource], a
ld a, d
ld [wRequested2bppSource + 1], a
ld a, l
ld [wRequested2bppDest], a
ld a, h
ld [wRequested2bppDest + 1], a
.loop
ld a, c
ld hl, hTilesPerCycle
cp [hl]
jr nc, .cycle
ld [wRequested2bppSize], a
.wait
call DelayFrame
ld a, [wRequested2bppSize]
and a
jr nz, .wait
pop af
ldh [hTilesPerCycle], a
pop af
rst Bankswitch
pop af
ldh [hBGMapMode], a
ret
.cycle
ldh a, [hTilesPerCycle]
ld [wRequested2bppSize], a
.wait2
call DelayFrame
ld a, [wRequested2bppSize]
and a
jr nz, .wait2
ld a, c
ld hl, hTilesPerCycle
sub [hl]
ld c, a
jr .loop
Request1bpp::
; Load 1bpp at b:de to occupy c tiles of hl.
ldh a, [hBGMapMode]
push af
xor a
ldh [hBGMapMode], a
ldh a, [hROMBank]
push af
ld a, b
rst Bankswitch
ldh a, [hTilesPerCycle]
push af
ld a, TILES_PER_CYCLE
ldh [hTilesPerCycle], a
ld a, [wLinkMode]
cp LINK_MOBILE
jr nz, .NotMobile
ldh a, [hMobile]
and a
jr nz, .NotMobile
ld a, MOBILE_TILES_PER_CYCLE
ldh [hTilesPerCycle], a
.NotMobile:
ld a, e
ld [wRequested1bppSource], a
ld a, d
ld [wRequested1bppSource + 1], a
ld a, l
ld [wRequested1bppDest], a
ld a, h
ld [wRequested1bppDest + 1], a
.loop
ld a, c
ld hl, hTilesPerCycle
cp [hl]
jr nc, .cycle
ld [wRequested1bppSize], a
.wait
call DelayFrame
ld a, [wRequested1bppSize]
and a
jr nz, .wait
pop af
ldh [hTilesPerCycle], a
pop af
rst Bankswitch
pop af
ldh [hBGMapMode], a
ret
.cycle
ldh a, [hTilesPerCycle]
ld [wRequested1bppSize], a
.wait2
call DelayFrame
ld a, [wRequested1bppSize]
and a
jr nz, .wait2
ld a, c
ld hl, hTilesPerCycle
sub [hl]
ld c, a
jr .loop
Get2bpp::
; copy c 2bpp tiles from b:de to hl
ldh a, [rLCDC]
bit rLCDC_ENABLE, a
jp nz, Request2bpp
; fallthrough
Copy2bpp:
push hl
ld h, d
ld l, e
pop de
; bank
ld a, b
; bc = c * LEN_2BPP_TILE
push af
swap c
ld a, $f
and c
ld b, a
ld a, $f0
and c
ld c, a
pop af
jp FarCopyBytes
Get1bpp::
; copy c 1bpp tiles from b:de to hl
ldh a, [rLCDC]
bit rLCDC_ENABLE, a
jp nz, Request1bpp
; fallthrough
Copy1bpp::
push de
ld d, h
ld e, l
; bank
ld a, b
; bc = c * LEN_1BPP_TILE
push af
ld h, 0
ld l, c
add hl, hl
add hl, hl
add hl, hl
ld b, h
ld c, l
pop af
pop hl
jp FarCopyBytesDouble