summaryrefslogtreecommitdiff
path: root/home/copy2.asm
blob: 0a0ee545b42e59473b4048fcf98537a1e02f5b02 (plain)
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
INCLUDE "constants.asm"

SECTION "Video Copy functions", ROM0[$0D2A]

FarCopyData: ; d2a (0:d2a)
; Identical to FarCopyBytes except for tail call optimization
; Copy bc 2bpp bytes from a:hl to de.
	ld [wBuffer], a
	ldh a, [hROMBank]
	push af
	ld a, [wBuffer]
	call Bankswitch
	call CopyBytes
	pop af
	call Bankswitch
	ret

FarCopyDataDouble: ; d3e (0:d3e)
; Copy and expand bc 1bpp bytes from a:hl to de.
	ld [wBuffer], a
	ldh a, [hROMBank]
	push af
	ld a, [wBuffer]
	call Bankswitch
	ld a, h
	ld h, d
	ld d, a
	ld a, l
	ld l, e
	ld e, a
	ld a, b
	and a
	jr z, .copy_small
	ld a, c
	and a
	jr z, .next
.copy_small
	inc b
.next
	ld a, [de]
	inc de
	ld [hli], a
	ld [hli], a
	dec c
	jr nz, .next
	dec b
	jr nz, .next
	pop af
	call Bankswitch
	ret

CopyVideoData:: ; d68 (0:d68)
; Wait for the next VBlank, then copy c 2bpp
; tiles from b:de to hl, 8 tiles at a time.
; This takes c/8 frames.
	ldh a, [hBGMapMode]
	push af
	xor a ; disable auto-transfer while copying
	ldh [hBGMapMode], a
	ldh a, [hROMBank]
	push af
	ld a, b
	call Bankswitch
	ld a, e
	ld [wVBCopySrc], a
	ld a, d
	ld [wVBCopySrc + 1], a
	ld a, l
	ld [wVBCopyDst], a
	ld a, h
	ld [wVBCopyDst + 1], a
.loop
	ld a, c
	cp $8
	jr nc, .keepgoing
	ld [wVBCopySize], a
	call DelayFrame
	pop af
	call Bankswitch
	pop af
	ldh [hBGMapMode], a
	ret
.keepgoing
	ld a, $8
	ld [wVBCopySize], a
	call DelayFrame
	ld a, c
	sub $8
	ld c, a
	jr .loop

CopyVideoDataDouble:: ; da6 (0:da6)
; Wait for the next VBlank, then copy c 1bpp
; tiles from b:de to hl, 8 tiles at a time.
; This takes c/8 frames.
	ldh a, [hBGMapMode]
	push af
	xor a
	ldh [hBGMapMode], a
	ldh a, [hROMBank]
	push af
	ld a, b
	call Bankswitch
	ld a, e
	ld [wVBCopyDoubleSrc], a
	ld a, d
	ld [wVBCopyDoubleSrc + 1], a
	ld a, l
	ld [wVBCopyDoubleDst], a
	ld a, h
	ld [wVBCopyDoubleDst + 1], a
.loop
	ld a, c
	cp $8
	jr nc, .keepgoing
	ld [wVBCopyDoubleSize], a
	call DelayFrame
	pop af
	call Bankswitch
	pop af
	ldh [hBGMapMode], a
	ret
.keepgoing
	ld a, $8
	ld [wVBCopyDoubleSize], a
	call DelayFrame
	ld a, c
	sub $8
	ld c, a
	jr .loop

CopyVideoDataOptimized:: ; de4 (0:de4)
; Copy c 2bpp tiles from b:de to hl in VRAM
; using VBlank service or direct copy in
; case LCD is off
	ldh a, [rLCDC]
	bit rLCDC_ENABLE, a
	jp nz, CopyVideoData ; copy video data during vblank while screen is on
	push hl              ; convert to FarCopyData call
	ld h, d
	ld l, e
	pop de
	ld a, b
	push af
	swap c
	ld a, $0f
	and c
	ld b, a
	ld a, $f0
	and c
	ld c, a
	pop af
	jp FarCopyData

CopyVideoDataDoubleOptimized: ; dff (0:dff)
; Copy c 1bpp tiles from b:de to hl in VRAM
; using VBlank service or direct copy in
; case LCD is off
	ldh a, [rLCDC]
	bit rLCDC_ENABLE, a
	jp nz, CopyVideoDataDouble
	push de
	ld d, h
	ld e, l
	ld a, b
	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 FarCopyDataDouble
; 0xe18