-
Notifications
You must be signed in to change notification settings - Fork 2
/
map_decode.asm
307 lines (256 loc) · 5.85 KB
/
map_decode.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
; asmsyntax=ca65
; == Byte data format ==
; First byte:
;
; 01le tttt
; |||| ++++-- Tile type (0 doesn't have a second byte)
; |||+------- Has this tile been entered/interacted with?
; ||+-------- Has this byte been loaded (child spawn only)?
; ++--------- Always 01
;
; Second byte:
;
; 1ddd dddd
; |+++-++++-- Tile value.
; | For child: if loaded, its index in RAM.
; | if not loaded, its map ID to load.
; | For health: remaining hits to destroy tile
; | For powerup/down: ID of drop
; +---------- Always 1
;
; Tile types:
;
; %0 No brick
; %10 Standard brick (health)
; %110 Child spawn
; %1110 Powerup
; %11110 PowerDown ;; nope
; %11110 Half brick
.import main_Index_Maps
.import child_Index_Maps
; Load a child map into RAM starting at $6000
; Input: A
; Output: $6000
LoadChildMap:
sta IdxD ; Map ID in ROM
asl a
tax
; Save bank we're coming from
lda $8000
sta LastBank
; Swap to data bank
lda #1
jsr MMC1_Select_Page
; Load up a pointer to the map's metadata
lda child_Index_Maps+0, x
sta AddressPointer0+0
lda child_Index_Maps+1, x
sta AddressPointer0+1
jsr map_LoadMetaData
lda NextChildOffset
cmp #102
bcs @done ; too many maps loaded. abort to not break things.
sta ChildId ; update the ID. This will be written to the brick in memory.
inc NextChildOffset
asl a
tax
lda Child_Map_Addresses, x
sta AddressPointer0
lda Child_Map_Addresses+1, x
sta AddressPointer0+1
; Get the flags and rewrite just the health
lda TmpZ
bpl @noGrav
lda #GRAVITY_VALUE
jmp :+
@noGrav:
lda #0
:
ldy #$48
sta (AddressPointer0), y
bit TmpZ
bvc @noRandoDrops
; TODO save it lol
@noRandoDrops:
lda TmpZ
and #$1F
sta TmpZ
lda #72
sta TmpW ; tile count
@loadLoop:
jsr map_ReadTile
jsr map_WriteData
dec TmpW
bne @loadLoop
; Store the child map ID in RAM next to the map data.
lda IdxD
ldy #1
sta (AddressPointer0), y
; TODO: put a return address pointer here too to
; allow nested child maps.
@done:
; Swap back to last bank
lda LastBank
jmp MMC1_Select_Page
map_WriteData:
ldy #0
lda TmpY
beq @noBrick
; Write the first byte
ora #$40
sta (AddressPointer0), y
jsr IncPointer0
; Write the value byte
and #$0F
cmp #1
beq @healthBrick
cmp #4 ; check for half brick
;beq @halfBrick
bne :+
rts
:
ldy IdxB ; load index
inc IdxB ; increment for next read
lda (AddressPointer2), y ; load value
jmp @writeValue
@healthBrick:
lda TmpZ ; health start value is global to map
@writeValue:
dec TmpW
ora #$80
ldy #0
sta (AddressPointer0), y ; write value
jsr IncPointer0
rts
@noBrick:
lda #0
sta (AddressPointer0), y
jsr IncPointer0
rts
map_LoadMetaData:
ldy #0
; Load pointer to tile data
lda (AddressPointer0), y
sta AddressPointer1+0
iny
lda (AddressPointer0), y
sta AddressPointer1+1
; Load pointer to value data
iny
lda (AddressPointer0), y
sta AddressPointer2+0
iny
lda (AddressPointer0), y
sta AddressPointer2+1
; Load the start health value
iny
lda (AddressPointer0), y
sta TmpZ
lda #0
sta IdxB ; Value offset
lda #$FF
sta IdxA ; Data offset
lda #1 ; init to 1 to load first byte
sta TmpX ; Bit counter
rts
; Expects a map Id in A and loads the map with that Id
; into RAM starting at the CurrentMap label
LoadMap:
asl a
tax
; Save bank we're coming from
lda $8000
sta LastBank
; TODO: lookup table for banks?
; Swap to data bank
lda #1
jsr MMC1_Select_Page
; Load up a pointer to the map's metadata
lda main_Index_Maps+0, x
sta AddressPointer0+0
lda main_Index_Maps+1, x
sta AddressPointer0+1
jsr map_LoadMetaData
; Get the flags and rewrite just the health
lda TmpZ
bpl @noGrav
lda #GRAVITY_VALUE
jmp :+
@noGrav:
lda #0
:
sta Gravity_MainMap
bit TmpZ
bvc @noRandoDrops
; TODO save it lol
@noRandoDrops:
lda TmpZ
and #$20
beq @noRandoChildren
; TODO save it lol
@noRandoChildren:
lda TmpZ
and #$1F
sta TmpZ
; Load map dest address in pointer0
lda #<CurrentMap
sta AddressPointer0+0
lda #>CurrentMap
sta AddressPointer0+1
; Main loop to load map
@loadLoop:
; check for end-of-map
lda AddressPointer0+1
cmp Last_Map_Tile_Address+1
bne :+
lda AddressPointer0+0
cmp Last_Map_Tile_Address+0
bcc :+
; we done
jmp @done
:
jsr map_ReadTile
jsr map_WriteData
jmp @loadLoop
@done:
; Swap back to last bank
lda LastBank
jsr MMC1_Select_Page
rts
; Start at the current offset in the encoded data
; and return a single tile type.
map_ReadTile:
; Things to keep track of:
; IdxA - current data offset
; TmpX - number of bits shifted off current byte
; TmpY - output value
; IdxC - current data byte
lda #0
sta TmpY
@top:
dec TmpX ; decrement bit count
bne @shift ; load next byte after eight bits
inc IdxA ; increment data index
lda #8
sta TmpX ; reset bit count
; Load current data byte
ldy IdxA
lda (AddressPointer1), y
sta IdxC
@shift:
; Shift data off until zero
asl IdxC
bcc @done
inc TmpY
jmp @top
@done:
rts
; Last address for the main map
Last_Map_Tile_Address:
.word (BOARD_WIDTH * BOARD_HEIGHT) + CurrentMap
Child_Map_Addresses:
; RAM will not fit any more than 102 decoded child maps
; at 12x6 tiles (72 bytes, rounded up to 80).
.repeat 102, i
.word ($6000 + (80 * i))
.endrepeat