-
Notifications
You must be signed in to change notification settings - Fork 0
/
payload.c
294 lines (246 loc) · 7.77 KB
/
payload.c
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
#include "vbe.h"
#include "drawing.h"
#include "rle.h"
#include "baselib.h"
#include "interrupts.h"
#include "assets/build/assets.h"
#define TICKS_INTERVAL 33 // 16.5 ms = 60.61 Hz (FPS for our purposes)
static uint16_t remaining_ticks = TICKS_INTERVAL;
static uint8_t fade_cc = 0;
static struct PbmImage balloons_image;
static struct PbmImage happy_text_image;
static struct PbmImage birthday_text_image;
static struct PbmImage smile_image;
static struct PbmPalette image_palette;
// 64 KiB maximum size for these buffers.
// The first address is 64 Ki positions below ISRs
static void* balloons_decompress_buf = (void*) 0x6FEF0;
#define BALLOONS_DECOMPRESS_BUF_SIZE 32768
static void* happy_text_decompress_buf = (void*) 0x77EF0;
#define HAPPY_TEXT_DECOMPRESS_BUF_SIZE 4096
static void* birthday_text_decompress_buf = (void*) 0x78EF0;
#define BIRTHDAY_TEXT_DECOMPRESS_BUF_SIZE 4096
static void* smile_decompress_buf = (void*) 0x79EF0;
#define SMILE_DECOMPRESS_BUF_SIZE 512
static void initial_fade(void);
static void happy_text_fade(void);
static void birthday_text_fade(void);
static void random_balloons_color(void);
/*
* Decompresses the specified RLE data to the given buffer, and decodes it as a PBM image.
* If not successful, this function draws error color codes and never returns.
*/
static void decompress_and_decode_pbm(void* data, size_t size, void* buf, size_t buf_size, struct PbmImage* image);
/**
* Entry point of the application. The bootloader will jump to the first instruction
* of this function, at 0x8000.
*/
void start(void) {
// Load images
decompress_and_decode_pbm(
balloons_pbm_stripped_rle, balloons_pbm_stripped_rle_len,
balloons_decompress_buf, BALLOONS_DECOMPRESS_BUF_SIZE, &balloons_image
);
decompress_and_decode_pbm(
happy_text_pbm_stripped_rle, happy_text_pbm_stripped_rle_len,
happy_text_decompress_buf, HAPPY_TEXT_DECOMPRESS_BUF_SIZE, &happy_text_image
);
decompress_and_decode_pbm(
birthday_text_pbm_stripped_rle, birthday_text_pbm_stripped_rle_len,
birthday_text_decompress_buf, BIRTHDAY_TEXT_DECOMPRESS_BUF_SIZE, &birthday_text_image
);
decompress_and_decode_pbm(
smile_pbm_stripped_rle, smile_pbm_stripped_rle_len,
smile_decompress_buf, SMILE_DECOMPRESS_BUF_SIZE, &smile_image
);
// Make sure everything is black
fill(0, 0, modeInfoBlockPtr->XResolution, modeInfoBlockPtr->YResolution, 0, 0, 0);
setup_interrupts(&initial_fade);
// Wait indefinitely until the next tick
while (true) {
halt(false);
}
}
void decompress_and_decode_pbm(void* data, size_t size, void* buf, size_t buf_size, struct PbmImage* image) {
image->width = 0;
image->palette = &image_palette;
size_t rle_decompressed_data_size = decompress(data, size, buf, buf_size);
if (rle_decompressed_data_size == 0) {
fill(0, 0, modeInfoBlockPtr->XResolution, modeInfoBlockPtr->YResolution, 255, 255, 0);
halt(true);
}
decode_pbm(buf, rle_decompressed_data_size, image);
if (image->width == 0) {
fill(0, 0, modeInfoBlockPtr->XResolution, modeInfoBlockPtr->YResolution, 255, 127, 0);
halt(true);
}
}
void initial_fade(void) {
if (--remaining_ticks == 0) {
uint8_t old_fade_cc = fade_cc;
fade_cc += 3; // So fade lasts 1.402 s at 60 FPS
// Replace previous fading color
replace_color(
old_fade_cc, old_fade_cc, old_fade_cc,
fade_cc, fade_cc, fade_cc,
0, 0, modeInfoBlockPtr->XResolution, modeInfoBlockPtr->YResolution
);
if (fade_cc == 3) {
// Draw background image
image_palette.low_r = 0;
image_palette.low_g = 0;
image_palette.low_b = 0;
image_palette.high_r = fade_cc;
image_palette.high_g = fade_cc;
image_palette.high_b = fade_cc;
draw_pbm_image(
&balloons_image,
modeInfoBlockPtr->XResolution / 2 - balloons_image.width,
modeInfoBlockPtr->YResolution / 2 - balloons_image.height / 2,
2
);
}
// Proceed to the next phase
if (fade_cc == 255) {
fade_cc = 252;
image_palette.low_r = fade_cc;
image_palette.low_g = fade_cc;
image_palette.low_b = fade_cc;
image_palette.high_r = 255;
image_palette.high_g = 255;
image_palette.high_b = 255;
draw_pbm_image(
&happy_text_image,
modeInfoBlockPtr->XResolution / 2 - 305,
modeInfoBlockPtr->YResolution / 2 - 228,
2
);
setup_interrupts(&happy_text_fade);
}
remaining_ticks = TICKS_INTERVAL;
}
}
static void happy_text_fade(void) {
if (--remaining_ticks == 0) {
uint8_t old_fade_cc = fade_cc;
fade_cc -= 3;
// Replace previous fading color
replace_color(
old_fade_cc, old_fade_cc, old_fade_cc,
fade_cc, fade_cc, fade_cc,
modeInfoBlockPtr->XResolution / 2 - 305, modeInfoBlockPtr->YResolution / 2 - 228,
happy_text_image.width * 2, happy_text_image.height
);
if (fade_cc == 0) {
fade_cc = 252;
setup_interrupts(&birthday_text_fade);
remaining_ticks = 3000; // 1.5 s for fade start
} else {
remaining_ticks = TICKS_INTERVAL;
}
}
}
void birthday_text_fade(void) {
if (--remaining_ticks == 0) {
uint8_t old_fade_cc = fade_cc;
fade_cc -= 3;
// First tick, draw image
if (old_fade_cc == 252) {
draw_pbm_image(
&birthday_text_image,
modeInfoBlockPtr->XResolution / 2 - 30,
modeInfoBlockPtr->YResolution / 2 + 175,
2
);
}
// Replace previous fading color
replace_color(
old_fade_cc, old_fade_cc, old_fade_cc,
fade_cc, fade_cc, fade_cc,
modeInfoBlockPtr->XResolution / 2 - 30, modeInfoBlockPtr->YResolution / 2 + 175,
birthday_text_image.width * 2, birthday_text_image.height
);
if (fade_cc == 0) {
setup_interrupts(&random_balloons_color);
}
remaining_ticks = TICKS_INTERVAL;
}
}
void random_balloons_color(void) {
static uint8_t previous_r = 0;
static uint8_t previous_g = 0;
static uint8_t previous_b = 0;
static bool smile_not_drawn = true;
if (--remaining_ticks == 0) {
uint8_t new_r = (uint8_t) (rand() % 200);
uint8_t new_g = (uint8_t) (rand() % 200);
uint8_t new_b = (uint8_t) (rand() % 200);
replace_color(
previous_r, previous_g, previous_b,
new_r, new_g, new_b,
modeInfoBlockPtr->XResolution / 2 - 154, modeInfoBlockPtr->YResolution / 2 - 240,
330, 301
);
replace_color(
previous_r, previous_g, previous_b,
new_r, new_g, new_b,
modeInfoBlockPtr->XResolution / 2 - 176, modeInfoBlockPtr->YResolution / 2 - 92,
22, 111
);
replace_color(
previous_r, previous_g, previous_b,
new_r, new_g, new_b,
modeInfoBlockPtr->XResolution / 2 + 31, modeInfoBlockPtr->YResolution / 2 + 61,
72, 25
);
replace_color(
previous_r, previous_g, previous_b,
new_r, new_g, new_b,
modeInfoBlockPtr->XResolution / 2 - 42, modeInfoBlockPtr->YResolution / 2 + 98,
56, 22
);
replace_color(
previous_r, previous_g, previous_b,
new_r, new_g, new_b,
modeInfoBlockPtr->XResolution / 2 + 14, modeInfoBlockPtr->YResolution / 2 + 88,
15, 17
);
replace_color(
previous_r, previous_g, previous_b,
new_r, new_g, new_b,
modeInfoBlockPtr->XResolution / 2 + 28, modeInfoBlockPtr->YResolution / 2 + 82,
4, 6
);
replace_color(
previous_r, previous_g, previous_b,
new_r, new_g, new_b,
modeInfoBlockPtr->XResolution / 2 - 48, modeInfoBlockPtr->YResolution / 2 + 103,
6, 5
);
replace_color(
previous_r, previous_g, previous_b,
new_r, new_g, new_b,
modeInfoBlockPtr->XResolution / 2 - 52, modeInfoBlockPtr->YResolution / 2 + 98,
4, 4
);
if (rand() % 60 == 3 && smile_not_drawn) {
image_palette.low_r = 0;
image_palette.low_g = 0;
image_palette.low_b = 0;
image_palette.high_r = 255;
image_palette.high_g = 255;
image_palette.high_b = 255;
draw_pbm_image(
&smile_image,
modeInfoBlockPtr->XResolution / 2 - 246,
modeInfoBlockPtr->YResolution / 2 + 126,
2
);
smile_not_drawn = false;
}
previous_r = new_r;
previous_g = new_g;
previous_b = new_b;
remaining_ticks = (uint16_t) (rand() % 600) + 400; // 0.5 seconds maximum, 0.2 seconds minimum
}
}