-
Notifications
You must be signed in to change notification settings - Fork 0
/
pal.s
391 lines (319 loc) · 9.81 KB
/
pal.s
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
/*
* Dragon - Palette render
* Copyright (C) 2016 Sandor Zsuga (Jubatian)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "m74cfg.h"
#include <avr/io.h>
; (Needed only for register names for X, Y and Z)
.section .text
/* Sky coloration variations (14 bytes each) */
sky_data:
.byte (0x2 << 0) + (0x3 << 3) + (0x3 << 6) /* Normal daylight blue */
.byte (0x2 << 0) + (0x4 << 3) + (0x3 << 6)
.byte (0x3 << 0) + (0x4 << 3) + (0x3 << 6)
.byte (0x3 << 0) + (0x5 << 3) + (0x3 << 6)
.byte (0x4 << 0) + (0x5 << 3) + (0x3 << 6)
.byte (0x4 << 0) + (0x6 << 3) + (0x3 << 6)
.byte (0x5 << 0) + (0x6 << 3) + (0x3 << 6)
.byte (0x5 << 0) + (0x7 << 3) + (0x3 << 6)
.byte (0x6 << 0) + (0x7 << 3) + (0x3 << 6)
.byte (0x6 << 0) + (0x7 << 3) + (0x2 << 6)
.byte (0x6 << 0) + (0x6 << 3) + (0x2 << 6)
.byte (0x5 << 0) + (0x6 << 3) + (0x1 << 6)
.byte (0x5 << 0) + (0x5 << 3) + (0x1 << 6)
.byte (0x4 << 0) + (0x5 << 3) + (0x1 << 6)
.byte (0x3 << 0) + (0x2 << 3) + (0x2 << 6) /* Dawn / Dusk */
.byte (0x4 << 0) + (0x2 << 3) + (0x2 << 6)
.byte (0x4 << 0) + (0x3 << 3) + (0x2 << 6)
.byte (0x5 << 0) + (0x3 << 3) + (0x2 << 6)
.byte (0x5 << 0) + (0x4 << 3) + (0x2 << 6)
.byte (0x6 << 0) + (0x4 << 3) + (0x2 << 6)
.byte (0x6 << 0) + (0x5 << 3) + (0x2 << 6)
.byte (0x7 << 0) + (0x5 << 3) + (0x2 << 6)
.byte (0x7 << 0) + (0x6 << 3) + (0x2 << 6)
.byte (0x6 << 0) + (0x5 << 3) + (0x2 << 6)
.byte (0x4 << 0) + (0x4 << 3) + (0x2 << 6)
.byte (0x3 << 0) + (0x3 << 3) + (0x1 << 6)
.byte (0x2 << 0) + (0x2 << 3) + (0x1 << 6)
.byte (0x1 << 0) + (0x1 << 3) + (0x0 << 6)
/*
** Renders the sky. The parameter is the sky type to render.
**
** Inputs:
** r24: sky (type of sky to render)
*/
.global pal_sky_render
pal_sky_render:
ldi r20, 0x00
; Simply fall through with zero for color fading ratio
/*
** Renders the sky with fading. The "sky" parameter is the sky type to render.
** "tg" is the target color to fade towards, "rat" is the ratio; 0: no fading,
** 255: maximal fading.
**
** Inputs:
** r24: sky (type of sky to render)
** r22: target color for fading
** r20: fade ratio
*/
.global pal_sky_render_fade
pal_sky_render_fade:
lds r25, levelscr_y + 0
andi r25, 0x07 ; Low 7 bits of map Y location
ldi XH, hi8(M74_COL0_OFF)
mov XL, r25 ; Start position for 185 rows
subi XL, 0xD0 ; Add 48 for first row (rows 6 - 29 are the game field)
ldi ZH, hi8(sky_data)
ldi ZL, lo8(sky_data)
ldi r25, 14
mul r25, r24 ; Select sky type
add ZL, r0
adc ZH, r1
ldi r24, 13
andi r20, 0xE0 ; Only 8 levels of fading
brne skfade ; Nonzero fade ratio: do fading
lpm r1, Z+
skl0:
mov r0, r1
lpm r1, Z+
st X+, r0
st X+, r0
st X+, r0
st X+, r0
st X+, r1
st X+, r0
st X+, r1
st X+, r0
st X+, r1
st X+, r0
st X+, r1
st X+, r0
st X+, r1
st X+, r1
dec r24
brne skl0
st X+, r1 ; Line 182
st X+, r1 ; Line 183
st X+, r1 ; Line 184
clr r1
ret
; With fading
skfade:
; Calculate target color
mov r21, r22
andi r22, 0xC7 ; Blue and Red of target color
andi r21, 0x38 ; Green of target color
mul r22, r20
movw r22, r0 ; Result Blue and Red of target color
mul r21, r20
mov r21, r1 ; Result Green of target color
; Prepare multiplier and first color
neg r20 ; Multiplier negated for sky colors
lpm r18, Z+
mov r19, r18
andi r18, 0xC7 ; Blue and Red of source color
andi r19, 0x38 ; Green of source color
mul r18, r20
add r0, r22
adc r1, r23 ; Combined Blue and Red
mov r18, r1
andi r18, 0xC7 ; Masked out Blue and Red
mul r19, r20
add r1, r21 ; Combined Green
mov r19, r1
andi r19, 0x38 ; Masked out Green
add r18, r19 ; Combine source & target
; Output loop
skl1:
mov r25, r18
lpm r18, Z+
mov r19, r18
andi r18, 0xC7 ; Blue and Red of source color
andi r19, 0x38 ; Green of source color
mul r18, r20
add r0, r22
adc r1, r23 ; Combined Blue and Red
mov r18, r1
andi r18, 0xC7 ; Masked out Blue and Red
mul r19, r20
add r1, r21 ; Combined Green
mov r19, r1
andi r19, 0x38 ; Masked out Green
add r18, r19 ; Combine source & target
st X+, r25
st X+, r25
st X+, r25
st X+, r25
st X+, r18
st X+, r25
st X+, r18
st X+, r25
st X+, r18
st X+, r25
st X+, r18
st X+, r25
st X+, r18
st X+, r18
dec r24
brne skl1
st X+, r18 ; Line 182
st X+, r18 ; Line 183
st X+, r18 ; Line 184
clr r1
ret
/*
** Renders the top palette.
**
** Inputs:
** r24: top palette type (0: Black & White, 1: Yellow & Red)
*/
.global pal_top_render
pal_top_render:
ldi r20, 0x00
; Simply fall through with zero for color fading ratio
/*
** Renders the top palette with fading. "tg" is the target color to fade
** towards, "rat" is the ratio; 0: no fading, 255: maximal fading.
**
** Inputs:
** r24: top palette type (0: Black & White, 1: Yellow & Red)
** r22: target color for fading
** r20: fade ratio
*/
.global pal_top_render_fade
pal_top_render_fade:
ldi XH, hi8(LOC_TOPPAL_OFF + 1)
ldi XL, lo8(LOC_TOPPAL_OFF + 1)
andi r20, 0xE0 ; Only 8 levels of fading
brne ptfade ; Nonzero fade ratio: do fading
ldi r22, 0xAD ; Y&R Bg. color (10 101 101)
sbrs r24, 0
ldi r22, 0x00 ; B&W Bg. color (00 000 000)
st X+, r22
ldi r22, 0x0B ; Y&R Fg. color (00 001 011)
sbrs r24, 0
ldi r22, 0xFF ; B&W Fg. color (11 111 111)
st X+, r22
ret
ptfade:
; Calculate target color
mov r25, r22
andi r22, 0xC7 ; Blue and Red of target color
andi r25, 0x38 ; Green of target color
mul r22, r20
movw r22, r0 ; Result Blue and Red of target color
mul r25, r20
mov r25, r1 ; Result Green of target color
; Prepare multiplier
neg r20 ; Multiplier negated
; Bg. color
ldi r18, 0x85 ; Y&R Blue and Red of color
sbrs r24, 0
ldi r18, 0x00 ; B&W Blue and Red of color
mul r18, r20
add r0, r22
adc r1, r23 ; Combined Blue and Red
mov r18, r1
andi r18, 0xC7 ; Masked out Blue and Red
ldi r19, 0x28 ; Y&R Green of color
sbrs r24, 0
ldi r19, 0x00 ; B&W Green of color
mul r19, r20
add r1, r25 ; Combined Green
mov r19, r1
andi r19, 0x38 ; Masked out Green
add r18, r19 ; Combine source & target
st X+, r18
; Fg. color
ldi r18, 0x03 ; Y&R Blue and Red of color
sbrs r24, 0
ldi r18, 0xC7 ; B&W Blue and Red of color
mul r18, r20
add r22, r0
adc r23, r1 ; Combined Blue and Red
andi r23, 0xC7 ; Masked out Blue and Red
ldi r19, 0x08 ; Y&R Green of color
sbrs r24, 0
ldi r19, 0x38 ; B&W Green of color
mul r19, r20
add r25, r1 ; Combined Green
andi r25, 0x38 ; Masked out Green
add r25, r23 ; Combine source & target
st X+, r25
clr r1
ret
/*
** Renders the playfield palette.
*/
.global pal_pf_render
pal_pf_render:
ldi r22, 0x00
; Simply fall through with zero for color fading ratio
/*
** Renders the playfield palette with fading. "tg" is the target color to fade
** towards, "rat" is the ratio; 0: no fading, 255: maximal fading.
**
** Inputs:
** r24: target color for fading
** r22: fade ratio
*/
.global pal_pf_render_fade
pal_pf_render_fade:
ldi XH, hi8(LOC_MAINPAL_OFF + 1)
ldi XL, lo8(LOC_MAINPAL_OFF + 1)
ldi ZH, hi8(RES_PAL_00_OFF + 1)
ldi ZL, lo8(RES_PAL_00_OFF + 1)
ldi r18, 15 ; Count of colors to produce
andi r22, 0xE0 ; Only 8 levels of fading
brne pffade ; Nonzero fade ratio: do fading
pfl0:
lpm r0, Z+
st X+, r0
dec r18
brne pfl0
ret
pffade:
; Calculate target color
mov r23, r24
andi r24, 0xC7 ; Blue and Red of target color
andi r23, 0x38 ; Green of target color
mul r24, r22
movw r24, r0 ; Result Blue and Red of target color
mul r23, r22
mov r23, r1 ; Result Green of target color
; Prepare multiplier
neg r22 ; Multiplier negated
; Color generation loop
pfl1:
lpm r20, Z+
mov r21, r20
andi r20, 0xC7 ; Blue and Red of color
mul r20, r22
add r0, r24
adc r1, r25 ; Combined Blue and Red
mov r20, r1
andi r20, 0xC7 ; Masked out Blue and Red
andi r21, 0x38 ; Green of color
mul r21, r22
add r1, r23 ; Combined Green
mov r21, r1
andi r21, 0x38 ; Masked out Green
add r20, r21 ; Combine source & target
st X+, r20
dec r18
brne pfl1
clr r1
ret