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
|
; reads structs:
; x (1 byte), y (1 byte), data (n bytes), $00
; x (1 byte), y (1 byte), data (n bytes), $00
; ...
; $ff
; for each struct, writes data to BGMap0-translated x,y
WriteDataBlocksToBGMap0:
call WriteDataBlockToBGMap0
bit 7, [hl] ; check for $ff
jr z, WriteDataBlocksToBGMap0
ret
; reads struct:
; x (1 byte), y (1 byte), data (n bytes), $00
; writes data to BGMap0-translated x,y
WriteDataBlockToBGMap0:
ld b, [hl]
inc hl
ld c, [hl]
inc hl
push hl ; hl = addr of data
push bc ; b,c = x,y
ld b, -1
.find_zero_loop
inc b
ld a, [hli]
or a
jr nz, .find_zero_loop
ld a, b ; length of data
pop bc ; x,y
push af
call BCCoordToBGMap0Address
pop af
ld b, a ; length of data
pop hl ; addr of data
or a
jr z, .move_to_next
push bc
push hl
call SafeCopyDataHLtoDE ; copy data to de (BGMap0 translated x,y)
pop hl
pop bc
.move_to_next
inc b ; length of data += 1 (to account for the last $0)
ld c, b
ld b, 0
add hl, bc ; point to next structure
ret
; writes a to [v*BGMap0 + BG_MAP_WIDTH * c + b]
WriteByteToBGMap0:
push af
ld a, [wLCDC]
rla
jr c, .lcd_on
pop af
push hl
push de
push bc
push af
call BCCoordToBGMap0Address
pop af
ld [de], a
pop bc
pop de
pop hl
ret
.lcd_on
pop af
; fallthrough
; writes a to [v*BGMap0 + BG_MAP_WIDTH * c + b] during hblank
HblankWriteByteToBGMap0:
push hl
push de
push bc
ld hl, wTempByte
push hl
ld [hl], a
call BCCoordToBGMap0Address
pop hl
ld b, 1
call HblankCopyDataHLtoDE
pop bc
pop de
pop hl
ret
; copy a bytes of data from hl to vBGMap0 address pointed to by coord at bc
CopyDataToBGMap0:
push bc
push hl
push af
call BCCoordToBGMap0Address
pop af
ld b, a
pop hl
call SafeCopyDataHLtoDE
pop bc
ret
; copy b bytes of data from hl to de
; if LCD on, copy during h-blank only
SafeCopyDataHLtoDE: ; 6fc (0:6fc)
ld a, [wLCDC]
rla
jr c, JPHblankCopyDataHLtoDE
.lcd_off_loop
ld a, [hli]
ld [de], a
inc de
dec b
jr nz, .lcd_off_loop
ret
JPHblankCopyDataHLtoDE:
jp HblankCopyDataHLtoDE
|