src/Core/sgb.c

filter: Statementstmt 0.3% 2 / 524 Branchbran 0.2% 1 / 362 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.3% 3 / 886 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/Core/workboy.c ↑ Summary src/Core/rumble.c »
1 #include "gb.h"
2 #include "random.h"
3 #include <math.h>
4 #include <assert.h>
5 #include <string.h>
6  
7 #ifndef M_PI
8 #define M_PI 3.14159265358979323846
9 #endif
10  
11 enum {
12 PAL01 = 0x00,
13 PAL23 = 0x01,
14 PAL03 = 0x02,
15 PAL12 = 0x03,
16 ATTR_BLK = 0x04,
17 ATTR_LIN = 0x05,
18 ATTR_DIV = 0x06,
19 ATTR_CHR = 0x07,
20 PAL_SET = 0x0A,
21 PAL_TRN = 0x0B,
22 DATA_SND = 0x0F,
23 MLT_REQ = 0x11,
24 CHR_TRN = 0x13,
25 PCT_TRN = 0x14,
26 ATTR_TRN = 0x15,
27 ATTR_SET = 0x16,
28 MASK_EN = 0x17,
29 };
30  
31 typedef enum {
32 MASK_DISABLED,
33 MASK_FREEZE,
34 MASK_BLACK,
35 MASK_COLOR_0,
36 } mask_mode_t;
37  
38 typedef enum {
39 TRANSFER_LOW_TILES,
40 TRANSFER_HIGH_TILES,
41 TRANSFER_BORDER_DATA,
42 TRANSFER_PALETTES,
43 TRANSFER_ATTRIBUTES,
44 } transfer_dest_t;
45  
46 #define SGB_PACKET_SIZE 16
47 0 static inline void pal_command(GB_gameboy_t *gb, unsigned first, unsigned second)
48 {
49 0 gb->sgb->effective_palettes[0] = gb->sgb->effective_palettes[4] =
50 0 gb->sgb->effective_palettes[8] = gb->sgb->effective_palettes[12] =
51 0 *(uint16_t *)&gb->sgb->command[1];
52
53 0 ▶ for (unsigned i = 0; i < 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < 3; i++) { 0 0 0
54 0 gb->sgb->effective_palettes[first * 4 + i + 1] = *(uint16_t *)&gb->sgb->command[3 + i * 2];
55 }
56
57 0 ▶ for (unsigned i = 0; i < 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < 3; i++) { 0 0 0
58 0 gb->sgb->effective_palettes[second * 4 + i + 1] = *(uint16_t *)&gb->sgb->command[9 + i * 2];
59 }
60 0 }
61  
62 0 static inline void load_attribute_file(GB_gameboy_t *gb, unsigned file_index)
63 {
64 0 ▶ if (file_index > 0x2C) return;
Branch: 1 branch
BranchTrueFalseTotal
if (file_index > 0x2C) return; 0 0 0
65 0 uint8_t *output = gb->sgb->attribute_map;
66 0 ▶ for (unsigned i = 0; i < 90; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < 90; i++) { 0 0 0
67 0 uint8_t byte = gb->sgb->attribute_files[file_index * 90 + i];
68 0 ▶ for (unsigned j = 4; j--;) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned j = 4; j--;) { 0 0 0
69 0 *(output++) = byte >> 6;
70 0 byte <<= 2;
71 }
72 }
73 }
74  
75 static const uint16_t built_in_palettes[] =
76 {
77 0x67BF, 0x265B, 0x10B5, 0x2866,
78 0x637B, 0x3AD9, 0x0956, 0x0000,
79 0x7F1F, 0x2A7D, 0x30F3, 0x4CE7,
80 0x57FF, 0x2618, 0x001F, 0x006A,
81 0x5B7F, 0x3F0F, 0x222D, 0x10EB,
82 0x7FBB, 0x2A3C, 0x0015, 0x0900,
83 0x2800, 0x7680, 0x01EF, 0x2FFF,
84 0x73BF, 0x46FF, 0x0110, 0x0066,
85 0x533E, 0x2638, 0x01E5, 0x0000,
86 0x7FFF, 0x2BBF, 0x00DF, 0x2C0A,
87 0x7F1F, 0x463D, 0x74CF, 0x4CA5,
88 0x53FF, 0x03E0, 0x00DF, 0x2800,
89 0x433F, 0x72D2, 0x3045, 0x0822,
90 0x7FFA, 0x2A5F, 0x0014, 0x0003,
91 0x1EED, 0x215C, 0x42FC, 0x0060,
92 0x7FFF, 0x5EF7, 0x39CE, 0x0000,
93 0x4F5F, 0x630E, 0x159F, 0x3126,
94 0x637B, 0x121C, 0x0140, 0x0840,
95 0x66BC, 0x3FFF, 0x7EE0, 0x2C84,
96 0x5FFE, 0x3EBC, 0x0321, 0x0000,
97 0x63FF, 0x36DC, 0x11F6, 0x392A,
98 0x65EF, 0x7DBF, 0x035F, 0x2108,
99 0x2B6C, 0x7FFF, 0x1CD9, 0x0007,
100 0x53FC, 0x1F2F, 0x0E29, 0x0061,
101 0x36BE, 0x7EAF, 0x681A, 0x3C00,
102 0x7BBE, 0x329D, 0x1DE8, 0x0423,
103 0x739F, 0x6A9B, 0x7293, 0x0001,
104 0x5FFF, 0x6732, 0x3DA9, 0x2481,
105 0x577F, 0x3EBC, 0x456F, 0x1880,
106 0x6B57, 0x6E1B, 0x5010, 0x0007,
107 0x0F96, 0x2C97, 0x0045, 0x3200,
108 0x67FF, 0x2F17, 0x2230, 0x1548,
109 };
110  
111 static const struct {
112 char name[16];
113 unsigned palette_index;
114 } palette_assignments[] =
115 {
116 {"ZELDA", 5},
117 {"SUPER MARIOLAND", 6},
118 {"MARIOLAND2", 0x14},
119 {"SUPERMARIOLAND3", 2},
120 {"KIRBY DREAM LAND", 0xB},
121 {"HOSHINOKA-BI", 0xB},
122 {"KIRBY'S PINBALL", 3},
123 {"YOSSY NO TAMAGO", 0xC},
124 {"MARIO & YOSHI", 0xC},
125 {"YOSSY NO COOKIE", 4},
126 {"YOSHI'S COOKIE", 4},
127 {"DR.MARIO", 0x12},
128 {"TETRIS", 0x11},
129 {"YAKUMAN", 0x13},
130 {"METROID2", 0x1F},
131 {"KAERUNOTAMENI", 9},
132 {"GOLF", 0x18},
133 {"ALLEY WAY", 0x16},
134 {"BASEBALL", 0xF},
135 {"TENNIS", 0x17},
136 {"F1RACE", 0x1E},
137 {"KID ICARUS", 0xE},
138 {"QIX", 0x19},
139 {"SOLARSTRIKER", 7},
140 {"X", 0x1C},
141 {"GBWARS", 0x15},
142 };
143  
144 0 static void command_ready(GB_gameboy_t *gb)
145 {
146 /* SGB header commands are used to send the contents of the header to the SNES CPU.
147 A header command looks like this:
148 Command ID: 0b1111xxx1, where xxx is the packet index. (e.g. F1 for [0x104, 0x112), F3 for [0x112, 0x120))
149 Checksum: Simple one byte sum for the following content bytes
150 0xE content bytes. The last command, FB, is padded with zeros, so information past the header is not sent. */
151
152 0 ▶ if ((gb->sgb->command[0] & 0xF1) == 0xF1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((gb->sgb->command[0] & 0xF1) == 0xF1) { 0 0 0
153 0 ▶ if (gb->boot_rom_finished) return;
Branch: 1 branch
BranchTrueFalseTotal
if (gb->boot_rom_finished) return; 0 0 0
154
155 0 uint8_t checksum = 0;
156 0 ▶ for (unsigned i = 2; i < 0x10; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 2; i < 0x10; i++) { 0 0 0
157 0 checksum += gb->sgb->command[i];
158 }
159 0 ▶ if (checksum != gb->sgb->command[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (checksum != gb->sgb->command[1]) { 0 0 0
160 0 GB_log(gb, "Failed checksum for SGB header command, disabling SGB features\n");
161 0 gb->sgb->disable_commands = true;
162 0 return;
163 }
164 0 unsigned index = (gb->sgb->command[0] >> 1) & 7;
165 0 ▶ if (index > 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (index > 5) { 0 0 0
166 0 return;
167 }
168 0 memcpy(&gb->sgb->received_header[index * 14], &gb->sgb->command[2], 14);
169 0 ▶ if (gb->sgb->command[0] == 0xFB) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb->command[0] == 0xFB) { 0 0 0
170 0 ▶ if (gb->sgb->received_header[0x42] != 3 || gb->sgb->received_header[0x47] != 0x33) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->sgb->received_header[0x42] != 3 || gb->sgb->received_header[0x47] != 0x33) { 0 0 0
if (gb->sgb->received_header[0x42] != 3 || gb->sgb->received_header[0x47] != 0x33) { 0 0 0
171 0 gb->sgb->disable_commands = true;
172 0 ▶ nounroll for (unsigned i = 0; i < sizeof(palette_assignments) / sizeof(palette_assignments[0]); i++) {
Branch: 1 branch
BranchTrueFalseTotal
nounroll for (unsigned i = 0; i < sizeof(palette_assignments) / sizeof(palette_assignments[0]); i++) { 0 0 0
173 0 ▶ if (memcmp(palette_assignments[i].name, &gb->sgb->received_header[0x30], sizeof(palette_assignments[i].name)) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(palette_assignments[i].name, &gb->sgb->received_header[0x30], sizeof(palette_assignments[i].name)) == 0) { 0 0 0
174 0 gb->sgb->effective_palettes[0] = LE16(built_in_palettes[palette_assignments[i].palette_index * 4 - 4]);
175 0 gb->sgb->effective_palettes[1] = LE16(built_in_palettes[palette_assignments[i].palette_index * 4 + 1 - 4]);
176 0 gb->sgb->effective_palettes[2] = LE16(built_in_palettes[palette_assignments[i].palette_index * 4 + 2 - 4]);
177 0 gb->sgb->effective_palettes[3] = LE16(built_in_palettes[palette_assignments[i].palette_index * 4 + 3 - 4]);
178 0 break;
179 }
180 }
181 }
182 }
183 0 return;
184 }
185
186 /* Ignore malformed commands (0 length)*/
187 0 ▶ if ((gb->sgb->command[0] & 7) == 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if ((gb->sgb->command[0] & 7) == 0) return; 0 0 0
188
189 0 switch (gb->sgb->command[0] >> 3) {
190 0 case PAL01:
191 0 pal_command(gb, 0, 1);
192 0 break;
193 0 case PAL23:
194 0 pal_command(gb, 2, 3);
195 0 break;
196 0 case PAL03:
197 0 pal_command(gb, 0, 3);
198 0 break;
199 0 case PAL12:
200 0 pal_command(gb, 1, 2);
201 0 break;
202 0 case ATTR_BLK: {
203 struct {
204 uint8_t count;
205 struct {
206 uint8_t control;
207 uint8_t palettes;
208 uint8_t left, top, right, bottom;
209 } data[];
210 0 } *command = (void *)(gb->sgb->command + 1);
211 0 ▶ if (command->count > 0x12) return;
Branch: 1 branch
BranchTrueFalseTotal
if (command->count > 0x12) return; 0 0 0
212
213 0 ▶ for (unsigned i = 0; i < command->count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < command->count; i++) { 0 0 0
214 0 bool inside = command->data[i].control & 1;
215 0 bool middle = command->data[i].control & 2;
216 0 bool outside = command->data[i].control & 4;
217 0 uint8_t inside_palette = command->data[i].palettes & 0x3;
218 0 uint8_t middle_palette = (command->data[i].palettes >> 2) & 0x3;
219 0 uint8_t outside_palette = (command->data[i].palettes >> 4) & 0x3;
220
221 0 ▶ if (inside && !middle && !outside) {
Branch: 3 branches
BranchTrueFalseTotal
if (inside && !middle && !outside) { 0 0 0
if (inside && !middle && !outside) { 0 0 0
if (inside && !middle && !outside) { 0 0 0
222 0 middle = true;
223 0 middle_palette = inside_palette;
224 }
225 0 ▶ else if (outside && !middle && !inside) {
Branch: 3 branches
BranchTrueFalseTotal
else if (outside && !middle && !inside) { 0 0 0
else if (outside && !middle && !inside) { 0 0 0
else if (outside && !middle && !inside) { 0 0 0
226 0 middle = true;
227 0 middle_palette = outside_palette;
228 }
229
230 0 command->data[i].left &= 0x1F;
231 0 command->data[i].top &= 0x1F;
232 0 command->data[i].right &= 0x1F;
233 0 command->data[i].bottom &= 0x1F;
234
235 0 ▶ for (unsigned y = 0; y < 18; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned y = 0; y < 18; y++) { 0 0 0
236 0 ▶ for (unsigned x = 0; x < 20; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned x = 0; x < 20; x++) { 0 0 0
237 0 ▶ if (x < command->data[i].left || x > command->data[i].right ||
Branch: 2 branches
BranchTrueFalseTotal
if (x < command->data[i].left || x > command->data[i].right || 0 0 0
if (x < command->data[i].left || x > command->data[i].right || 0 0 0
238 0 ▶ y < command->data[i].top || y > command->data[i].bottom) {
Branch: 2 branches
BranchTrueFalseTotal
y < command->data[i].top || y > command->data[i].bottom) { 0 0 0
y < command->data[i].top || y > command->data[i].bottom) { 0 0 0
239 0 ▶ if (outside) {
Branch: 1 branch
BranchTrueFalseTotal
if (outside) { 0 0 0
240 0 gb->sgb->attribute_map[x + 20 * y] = outside_palette;
241 }
242 }
243 0 ▶ else if (x > command->data[i].left && x < command->data[i].right &&
Branch: 2 branches
BranchTrueFalseTotal
else if (x > command->data[i].left && x < command->data[i].right && 0 0 0
else if (x > command->data[i].left && x < command->data[i].right && 0 0 0
244 0 ▶ y > command->data[i].top && y < command->data[i].bottom) {
Branch: 2 branches
BranchTrueFalseTotal
y > command->data[i].top && y < command->data[i].bottom) { 0 0 0
y > command->data[i].top && y < command->data[i].bottom) { 0 0 0
245 0 ▶ if (inside) {
Branch: 1 branch
BranchTrueFalseTotal
if (inside) { 0 0 0
246 0 gb->sgb->attribute_map[x + 20 * y] = inside_palette;
247 }
248 }
249 0 ▶ else if (middle) {
Branch: 1 branch
BranchTrueFalseTotal
else if (middle) { 0 0 0
250 0 gb->sgb->attribute_map[x + 20 * y] = middle_palette;
251 }
252 }
253 }
254 }
255 0 break;
256 }
257 0 case ATTR_CHR: {
258 struct __attribute__((packed)) {
259 uint8_t x, y;
260 uint16_t length;
261 uint8_t direction;
262 uint8_t data[];
263 0 } *command = (void *)(gb->sgb->command + 1);
264
265 0 uint16_t count = command->length;
266 0 count = LE16(count);
267 0 uint8_t x = command->x;
268 0 uint8_t y = command->y;
269 0 ▶ if (x >= 20 || y >= 18) {
Branch: 2 branches
BranchTrueFalseTotal
if (x >= 20 || y >= 18) { 0 0 0
if (x >= 20 || y >= 18) { 0 0 0
270 /* TODO: Verify with the SFC BIOS */
271 break;
272 }
273  
274 0 ▶ for (unsigned i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < count; i++) { 0 0 0
275 0 uint8_t palette = (command->data[i / 4] >> (((~i) & 3) << 1)) & 3;
276 0 gb->sgb->attribute_map[x + 20 * y] = palette;
277 0 ▶ if (command->direction) {
Branch: 1 branch
BranchTrueFalseTotal
if (command->direction) { 0 0 0
278 0 y++;
279 0 ▶ if (y == 18) {
Branch: 1 branch
BranchTrueFalseTotal
if (y == 18) { 0 0 0
280 0 x++;
281 0 y = 0;
282 0 ▶ if (x == 20) {
Branch: 1 branch
BranchTrueFalseTotal
if (x == 20) { 0 0 0
283 0 break;
284 }
285 }
286 }
287 else {
288 0 x++;
289 0 ▶ if (x == 20) {
Branch: 1 branch
BranchTrueFalseTotal
if (x == 20) { 0 0 0
290 0 y++;
291 0 x = 0;
292 0 ▶ if (y == 18) {
Branch: 1 branch
BranchTrueFalseTotal
if (y == 18) { 0 0 0
293 0 break;
294 }
295 }
296 }
297 }
298
299 0 break;
300 }
301 0 case ATTR_LIN: {
302 struct {
303 uint8_t count;
304 uint8_t data[];
305 0 } *command = (void *)(gb->sgb->command + 1);
306 0 ▶ if (command->count > sizeof(gb->sgb->command) - 2) return;
Branch: 1 branch
BranchTrueFalseTotal
if (command->count > sizeof(gb->sgb->command) - 2) return; 0 0 0
307
308 0 ▶ for (unsigned i = 0; i < command->count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < command->count; i++) { 0 0 0
309 0 bool horizontal = command->data[i] & 0x80;
310 0 uint8_t palette = (command->data[i] >> 5) & 0x3;
311 0 uint8_t line = (command->data[i]) & 0x1F;
312
313 0 ▶ if (horizontal) {
Branch: 1 branch
BranchTrueFalseTotal
if (horizontal) { 0 0 0
314 0 ▶ if (line > 18) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (line > 18) continue; 0 0 0
315 0 ▶ for (unsigned x = 0; x < 20; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned x = 0; x < 20; x++) { 0 0 0
316 0 gb->sgb->attribute_map[x + 20 * line] = palette;
317 }
318 }
319 else {
320 0 ▶ if (line > 20) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (line > 20) continue; 0 0 0
321 0 ▶ for (unsigned y = 0; y < 18; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned y = 0; y < 18; y++) { 0 0 0
322 0 gb->sgb->attribute_map[line + 20 * y] = palette;
323 }
324 }
325 }
326 0 break;
327 }
328 0 case ATTR_DIV: {
329 0 uint8_t high_palette = gb->sgb->command[1] & 3;
330 0 uint8_t low_palette = (gb->sgb->command[1] >> 2) & 3;
331 0 uint8_t middle_palette = (gb->sgb->command[1] >> 4) & 3;
332 0 bool horizontal = gb->sgb->command[1] & 0x40;
333 0 uint8_t line = gb->sgb->command[2] & 0x1F;
334
335 0 ▶ for (unsigned y = 0; y < 18; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned y = 0; y < 18; y++) { 0 0 0
336 0 ▶ for (unsigned x = 0; x < 20; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned x = 0; x < 20; x++) { 0 0 0
337 0 ▶ if ((horizontal? y : x) < line) {
Branch: 2 branches
BranchTrueFalseTotal
if ((horizontal? y : x) < line) { 0 0 0
if ((horizontal? y : x) < line) { 0 0 0
338 0 gb->sgb->attribute_map[x + 20 * y] = low_palette;
339 }
340 0 ▶ else if ((horizontal? y : x) == line) {
Branch: 2 branches
BranchTrueFalseTotal
else if ((horizontal? y : x) == line) { 0 0 0
else if ((horizontal? y : x) == line) { 0 0 0
341 0 gb->sgb->attribute_map[x + 20 * y] = middle_palette;
342 }
343 else {
344 0 gb->sgb->attribute_map[x + 20 * y] = high_palette;
345 }
346 }
347 }
348  
349 0 break;
350 }
351 0 case PAL_SET:
352 0 memcpy(&gb->sgb->effective_palettes[0],
353 0 &gb->sgb->ram_palettes[4 * (gb->sgb->command[1] + (gb->sgb->command[2] & 1) * 0x100)],
354 8);
355 0 memcpy(&gb->sgb->effective_palettes[4],
356 0 &gb->sgb->ram_palettes[4 * (gb->sgb->command[3] + (gb->sgb->command[4] & 1) * 0x100)],
357 8);
358 0 memcpy(&gb->sgb->effective_palettes[8],
359 0 &gb->sgb->ram_palettes[4 * (gb->sgb->command[5] + (gb->sgb->command[6] & 1) * 0x100)],
360 8);
361 0 memcpy(&gb->sgb->effective_palettes[12],
362 0 &gb->sgb->ram_palettes[4 * (gb->sgb->command[7] + (gb->sgb->command[8] & 1) * 0x100)],
363 8);
364
365 0 gb->sgb->effective_palettes[12] = gb->sgb->effective_palettes[8] =
366 0 gb->sgb->effective_palettes[4] = gb->sgb->effective_palettes[0];
367
368 0 ▶ if (gb->sgb->command[9] & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb->command[9] & 0x80) { 0 0 0
369 0 load_attribute_file(gb, gb->sgb->command[9] & 0x3F);
370 }
371
372 0 ▶ if (gb->sgb->command[9] & 0x40) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb->command[9] & 0x40) { 0 0 0
373 0 gb->sgb->mask_mode = MASK_DISABLED;
374 }
375 0 break;
376 0 case PAL_TRN:
377 0 gb->sgb->vram_transfer_countdown = 3;
378 0 gb->sgb->transfer_dest = TRANSFER_PALETTES;
379 0 break;
380 0 case DATA_SND:
381 // Not supported, but used by almost all SGB games for hot patching, so let's mute the warning for this
382 0 break;
383 0 case MLT_REQ:
384 0 gb->sgb->player_count = (gb->sgb->command[1] & 3) + 1; /* Todo: When breaking save state comaptibility,
385 fix this to be 0 based. */
386 0 ▶ if (gb->sgb->player_count == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb->player_count == 3) { 0 0 0
387 0 gb->sgb->player_count++;
388 }
389 0 gb->sgb->current_player &= (gb->sgb->player_count - 1);
390 0 break;
391 0 case CHR_TRN:
392 0 gb->sgb->vram_transfer_countdown = 3;
393 0 gb->sgb->transfer_dest = (gb->sgb->command[1] & 1)? TRANSFER_HIGH_TILES : TRANSFER_LOW_TILES;
394 0 break;
395 0 case PCT_TRN:
396 0 gb->sgb->vram_transfer_countdown = 3;
397 0 gb->sgb->transfer_dest = TRANSFER_BORDER_DATA;
398 0 break;
399 0 case ATTR_TRN:
400 0 gb->sgb->vram_transfer_countdown = 3;
401 0 gb->sgb->transfer_dest = TRANSFER_ATTRIBUTES;
402 0 break;
403 0 case ATTR_SET:
404 0 load_attribute_file(gb, gb->sgb->command[1] & 0x3F);
405
406 0 ▶ if (gb->sgb->command[1] & 0x40) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb->command[1] & 0x40) { 0 0 0
407 0 gb->sgb->mask_mode = MASK_DISABLED;
408 }
409 0 break;
410 0 case MASK_EN:
411 0 gb->sgb->mask_mode = gb->sgb->command[1] & 3;
412 0 break;
413 0 default:
414 0 ▶ if ((gb->sgb->command[0] >> 3) == 8 &&
Branch: 1 branch
BranchTrueFalseTotal
if ((gb->sgb->command[0] >> 3) == 8 && 0 0 0
415 0 ▶ (gb->sgb->command[1] & ~0x80) == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
(gb->sgb->command[1] & ~0x80) == 0 && 0 0 0
416 0 ▶ (gb->sgb->command[2] & ~0x80) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
(gb->sgb->command[2] & ~0x80) == 0) { 0 0 0
417 /* Mute/dummy sound commands, ignore this command as it's used by many games at startup */
418 0 break;
419 }
420 0 GB_log(gb, "Unimplemented SGB command %x: ", gb->sgb->command[0] >> 3);
421 0 ▶ for (unsigned i = 0; i < gb->sgb->command_write_index / 8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < gb->sgb->command_write_index / 8; i++) { 0 0 0
422 0 GB_log(gb, "%02x ", gb->sgb->command[i]);
423 }
424 0 GB_log(gb, "\n");
425 }
426 }
427  
428 5 void GB_sgb_write(GB_gameboy_t *gb, uint8_t value)
429 {
430 5 ▶ if (!GB_is_sgb(gb)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!GB_is_sgb(gb)) return; 5 0 5
431 0 ▶ if (!GB_is_hle_sgb(gb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!GB_is_hle_sgb(gb)) { 0 0 0
432 /* Notify via callback */
433 0 return;
434 }
435 0 ▶ if (gb->sgb->disable_commands) return;
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb->disable_commands) return; 0 0 0
436
437 0 ▶ uint16_t command_size = (gb->sgb->command[0] & 7 ?: 1) * SGB_PACKET_SIZE * 8;
Branch: 1 branch
BranchTrueFalseTotal
uint16_t command_size = (gb->sgb->command[0] & 7 ?: 1) * SGB_PACKET_SIZE * 8; 0 0 0
438 0 ▶ if ((gb->sgb->command[0] & 0xF1) == 0xF1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((gb->sgb->command[0] & 0xF1) == 0xF1) { 0 0 0
439 0 command_size = SGB_PACKET_SIZE * 8;
440 }
441
442 0 ▶ if ((value & 0x20) != 0 && (gb->io_registers[GB_IO_JOYP] & 0x20) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ((value & 0x20) != 0 && (gb->io_registers[GB_IO_JOYP] & 0x20) == 0) { 0 0 0
if ((value & 0x20) != 0 && (gb->io_registers[GB_IO_JOYP] & 0x20) == 0) { 0 0 0
443 0 ▶ if ((gb->sgb->player_count & 1) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((gb->sgb->player_count & 1) == 0) { 0 0 0
444 0 gb->sgb->current_player++;
445 0 gb->sgb->current_player &= (gb->sgb->player_count - 1);
446 }
447 }
448
449 0 switch ((value >> 4) & 3) {
450 0 case 3:
451 0 gb->sgb->ready_for_pulse = true;
452 0 break;
453
454 0 case 2: // Zero
455 0 ▶ if (!gb->sgb->ready_for_pulse || !gb->sgb->ready_for_write) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!gb->sgb->ready_for_pulse || !gb->sgb->ready_for_write) return; 0 0 0
if (!gb->sgb->ready_for_pulse || !gb->sgb->ready_for_write) return; 0 0 0
456 0 ▶ if (gb->sgb->ready_for_stop) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb->ready_for_stop) { 0 0 0
457 0 ▶ if (gb->sgb->command_write_index == command_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb->command_write_index == command_size) { 0 0 0
458 0 command_ready(gb);
459 0 gb->sgb->command_write_index = 0;
460 0 memset(gb->sgb->command, 0, sizeof(gb->sgb->command));
461 }
462 0 gb->sgb->ready_for_pulse = false;
463 0 gb->sgb->ready_for_write = false;
464 0 gb->sgb->ready_for_stop = false;
465 }
466 else {
467 0 ▶ if (gb->sgb->command_write_index < sizeof(gb->sgb->command) * 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb->command_write_index < sizeof(gb->sgb->command) * 8) { 0 0 0
468 0 gb->sgb->command_write_index++;
469 0 gb->sgb->ready_for_pulse = false;
470 0 ▶ if (((gb->sgb->command_write_index) & (SGB_PACKET_SIZE * 8 - 1)) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (((gb->sgb->command_write_index) & (SGB_PACKET_SIZE * 8 - 1)) == 0) { 0 0 0
471 0 gb->sgb->ready_for_stop = true;
472 }
473 }
474 }
475 0 break;
476 0 case 1: // One
477 0 ▶ if (!gb->sgb->ready_for_pulse || !gb->sgb->ready_for_write) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!gb->sgb->ready_for_pulse || !gb->sgb->ready_for_write) return; 0 0 0
if (!gb->sgb->ready_for_pulse || !gb->sgb->ready_for_write) return; 0 0 0
478 0 ▶ if (gb->sgb->ready_for_stop) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb->ready_for_stop) { 0 0 0
479 0 GB_log(gb, "Corrupt SGB command.\n");
480 0 gb->sgb->ready_for_pulse = false;
481 0 gb->sgb->ready_for_write = false;
482 0 gb->sgb->command_write_index = 0;
483 0 memset(gb->sgb->command, 0, sizeof(gb->sgb->command));
484 }
485 else {
486 0 ▶ if (gb->sgb->command_write_index < sizeof(gb->sgb->command) * 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb->command_write_index < sizeof(gb->sgb->command) * 8) { 0 0 0
487 0 gb->sgb->command[gb->sgb->command_write_index / 8] |= 1 << (gb->sgb->command_write_index & 7);
488 0 gb->sgb->command_write_index++;
489 0 gb->sgb->ready_for_pulse = false;
490 0 ▶ if (((gb->sgb->command_write_index) & (SGB_PACKET_SIZE * 8 - 1)) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (((gb->sgb->command_write_index) & (SGB_PACKET_SIZE * 8 - 1)) == 0) { 0 0 0
491 0 gb->sgb->ready_for_stop = true;
492 }
493 }
494 }
495 0 break;
496
497 0 case 0:
498 0 ▶ if (!gb->sgb->ready_for_pulse) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!gb->sgb->ready_for_pulse) return; 0 0 0
499 0 gb->sgb->ready_for_write = true;
500 0 gb->sgb->ready_for_pulse = false;
501 0 ▶ if (((gb->sgb->command_write_index) & (SGB_PACKET_SIZE * 8 - 1)) != 0 ||
Branch: 1 branch
BranchTrueFalseTotal
if (((gb->sgb->command_write_index) & (SGB_PACKET_SIZE * 8 - 1)) != 0 || 0 0 0
502 0 ▶ gb->sgb->command_write_index == 0 ||
Branch: 1 branch
BranchTrueFalseTotal
gb->sgb->command_write_index == 0 || 0 0 0
503 0 ▶ gb->sgb->ready_for_stop) {
Branch: 1 branch
BranchTrueFalseTotal
gb->sgb->ready_for_stop) { 0 0 0
504 0 gb->sgb->command_write_index = 0;
505 0 memset(gb->sgb->command, 0, sizeof(gb->sgb->command));
506 0 gb->sgb->ready_for_stop = false;
507 }
508 0 break;
509
510 0 default:
511 0 break;
512 }
513 }
514  
515 0 static uint32_t convert_rgb15(GB_gameboy_t *gb, uint16_t color)
516 {
517 0 return GB_convert_rgb15(gb, color, false);
518 }
519  
520 0 static uint32_t convert_rgb15_with_fade(GB_gameboy_t *gb, uint16_t color, uint8_t fade)
521 {
522 0 uint8_t r = ((color) & 0x1F) - fade;
523 0 uint8_t g = ((color >> 5) & 0x1F) - fade;
524 0 uint8_t b = ((color >> 10) & 0x1F) - fade;
525
526 0 ▶ if (r >= 0x20) r = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r >= 0x20) r = 0; 0 0 0
527 0 ▶ if (g >= 0x20) g = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (g >= 0x20) g = 0; 0 0 0
528 0 ▶ if (b >= 0x20) b = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (b >= 0x20) b = 0; 0 0 0
529
530 0 color = r | (g << 5) | (b << 10);
531
532 0 return GB_convert_rgb15(gb, color, false);
533 }
534  
535 0 static void render_boot_animation (GB_gameboy_t *gb)
536 {
537 #include "graphics/sgb_animation_logo.inc"
538 0 uint32_t *output = gb->screen;
539 0 ▶ if (gb->border_mode != GB_BORDER_NEVER) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->border_mode != GB_BORDER_NEVER) { 0 0 0
540 0 output += 48 + 40 * 256;
541 }
542 0 uint8_t *input = animation_logo;
543 0 unsigned fade_blue = 0;
544 0 unsigned fade_red = 0;
545 0 ▶ if (gb->sgb->intro_animation < 80 - 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb->intro_animation < 80 - 32) { 0 0 0
546 0 fade_blue = 32;
547 }
548 0 ▶ else if (gb->sgb->intro_animation < 80) {
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->sgb->intro_animation < 80) { 0 0 0
549 0 fade_blue = 80 - gb->sgb->intro_animation;
550 }
551 0 ▶ else if (gb->sgb->intro_animation > GB_SGB_INTRO_ANIMATION_LENGTH - 32) {
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->sgb->intro_animation > GB_SGB_INTRO_ANIMATION_LENGTH - 32) { 0 0 0
552 0 fade_red = fade_blue = gb->sgb->intro_animation - GB_SGB_INTRO_ANIMATION_LENGTH + 32;
553 }
554 0 uint32_t colors[] = {
555 0 convert_rgb15(gb, 0),
556 0 convert_rgb15_with_fade(gb, 0x14A5, fade_blue),
557 0 convert_rgb15_with_fade(gb, 0x54E0, fade_blue),
558 0 convert_rgb15_with_fade(gb, 0x0019, fade_red),
559 0 convert_rgb15(gb, 0x0011),
560 0 convert_rgb15(gb, 0x0009),
561 };
562 0 unsigned y_min = (144 - animation_logo_height) / 2;
563 0 unsigned y_max = y_min + animation_logo_height;
564 0 ▶ for (unsigned y = 0; y < 144; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned y = 0; y < 144; y++) { 0 0 0
565 0 ▶ for (unsigned x = 0; x < 160; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned x = 0; x < 160; x++) { 0 0 0
566 0 ▶ if (y < y_min || y >= y_max) {
Branch: 2 branches
BranchTrueFalseTotal
if (y < y_min || y >= y_max) { 0 0 0
if (y < y_min || y >= y_max) { 0 0 0
567 0 *(output++) = colors[0];
568 }
569 else {
570 0 uint8_t color = *input;
571 0 ▶ if (color >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (color >= 3) { 0 0 0
572 0 ▶ if (color == gb->sgb->intro_animation / 2 - 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (color == gb->sgb->intro_animation / 2 - 3) { 0 0 0
573 0 color = 5;
574 }
575 0 ▶ else if (color == gb->sgb->intro_animation / 2 - 4) {
Branch: 1 branch
BranchTrueFalseTotal
else if (color == gb->sgb->intro_animation / 2 - 4) { 0 0 0
576 0 color = 4;
577 }
578 0 ▶ else if (color < gb->sgb->intro_animation / 2 - 4) {
Branch: 1 branch
BranchTrueFalseTotal
else if (color < gb->sgb->intro_animation / 2 - 4) { 0 0 0
579 0 color = 3;
580 }
581 else {
582 0 color = 0;
583 }
584 }
585 0 *(output++) = colors[color];
586 0 input++;
587 }
588 }
589 0 ▶ if (gb->border_mode != GB_BORDER_NEVER) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->border_mode != GB_BORDER_NEVER) { 0 0 0
590 0 output += 256 - 160;
591 }
592 }
593 0 }
594  
595 static void render_jingle(GB_gameboy_t *gb, size_t count);
596 0 void GB_sgb_render(GB_gameboy_t *gb, bool incomplete)
597 {
598 0 ▶ if (gb->apu_output.sample_rate) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu_output.sample_rate) { 0 0 0
599 0 render_jingle(gb, gb->apu_output.sample_rate / GB_get_usual_frame_rate(gb));
600 }
601
602 0 ▶ if (gb->sgb->intro_animation < GB_SGB_INTRO_ANIMATION_LENGTH) gb->sgb->intro_animation++;
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb->intro_animation < GB_SGB_INTRO_ANIMATION_LENGTH) gb->sgb->intro_animation++; 0 0 0
603
604 0 ▶ if (gb->sgb->vram_transfer_countdown) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb->vram_transfer_countdown) { 0 0 0
605 0 ▶ if (--gb->sgb->vram_transfer_countdown == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--gb->sgb->vram_transfer_countdown == 0) { 0 0 0
606 0 unsigned size = 0;
607 0 uint16_t *data = NULL;
608
609 0 switch (gb->sgb->transfer_dest) {
610 0 case TRANSFER_LOW_TILES:
611 0 size = 0x100;
612 0 data = (uint16_t *)gb->sgb->pending_border.tiles;
613 0 break;
614 0 case TRANSFER_HIGH_TILES:
615 0 size = 0x100;
616 0 data = (uint16_t *)gb->sgb->pending_border.tiles + 0x800;
617 0 break;
618 0 case TRANSFER_PALETTES:
619 0 size = 0x100;
620 0 data = gb->sgb->ram_palettes;
621 0 break;
622 0 case TRANSFER_BORDER_DATA:
623 0 size = 0x88;
624 0 data = gb->sgb->pending_border.raw_data;
625 0 break;
626 0 case TRANSFER_ATTRIBUTES:
627 0 size = 0xFE;
628 0 data = (uint16_t *)gb->sgb->attribute_files;
629 0 break;
630 0 default:
631 0 return; // Corrupt state?
632 }
633
634 0 ▶ for (unsigned tile = 0; tile < size; tile++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned tile = 0; tile < size; tile++) { 0 0 0
635 0 unsigned tile_x = (tile % 20) * 8;
636 0 unsigned tile_y = (tile / 20) * 8;
637 0 ▶ for (unsigned y = 0; y < 0x8; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned y = 0; y < 0x8; y++) { 0 0 0
638 static const uint16_t pixel_to_bits[4] = {0x0000, 0x0080, 0x8000, 0x8080};
639 0 *data = 0;
640 0 ▶ for (unsigned x = 0; x < 8; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned x = 0; x < 8; x++) { 0 0 0
641 0 *data |= pixel_to_bits[gb->sgb->screen_buffer[(tile_x + x) + (tile_y + y) * 160] & 3] >> x;
642 }
643 0 ▶ if (gb->sgb->transfer_dest == TRANSFER_PALETTES || gb->sgb->transfer_dest == TRANSFER_BORDER_DATA) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->sgb->transfer_dest == TRANSFER_PALETTES || gb->sgb->transfer_dest == TRANSFER_BORDER_DATA) { 0 0 0
if (gb->sgb->transfer_dest == TRANSFER_PALETTES || gb->sgb->transfer_dest == TRANSFER_BORDER_DATA) { 0 0 0
644 0 *data = LE16(*data);
645 }
646 0 data++;
647 }
648 }
649 0 ▶ if (gb->sgb->transfer_dest == TRANSFER_BORDER_DATA) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb->transfer_dest == TRANSFER_BORDER_DATA) { 0 0 0
650 0 gb->sgb->border_animation = 105; // Measured on an SGB2, but might be off by ±2
651 }
652 }
653 }
654
655 0 ▶ if (!gb->screen || !gb->rgb_encode_callback || gb->disable_rendering) {
Branch: 3 branches
BranchTrueFalseTotal
if (!gb->screen || !gb->rgb_encode_callback || gb->disable_rendering) { 0 0 0
if (!gb->screen || !gb->rgb_encode_callback || gb->disable_rendering) { 0 0 0
if (!gb->screen || !gb->rgb_encode_callback || gb->disable_rendering) { 0 0 0
656 0 ▶ if (gb->sgb->border_animation > 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb->border_animation > 32) { 0 0 0
657 0 gb->sgb->border_animation--;
658 }
659 0 ▶ else if (gb->sgb->border_animation != 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->sgb->border_animation != 0) { 0 0 0
660 0 gb->sgb->border_animation--;
661 }
662 0 ▶ if (gb->sgb->border_animation == 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb->border_animation == 32) { 0 0 0
663 0 memcpy(&gb->sgb->border, &gb->sgb->pending_border, sizeof(gb->sgb->border));
664 }
665 0 return;
666 }
667  
668 uint32_t colors[4 * 4];
669 0 ▶ for (unsigned i = 0; i < 4 * 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < 4 * 4; i++) { 0 0 0
670 0 colors[i] = convert_rgb15(gb, LE16(gb->sgb->effective_palettes[i]));
671 }
672
673 0 ▶ if (gb->sgb->mask_mode != MASK_FREEZE && !incomplete) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->sgb->mask_mode != MASK_FREEZE && !incomplete) { 0 0 0
if (gb->sgb->mask_mode != MASK_FREEZE && !incomplete) { 0 0 0
674 0 memcpy(gb->sgb->effective_screen_buffer,
675 0 gb->sgb->screen_buffer,
676 sizeof(gb->sgb->effective_screen_buffer));
677 }
678
679 0 ▶ if (gb->sgb->intro_animation < GB_SGB_INTRO_ANIMATION_LENGTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb->intro_animation < GB_SGB_INTRO_ANIMATION_LENGTH) { 0 0 0
680 0 render_boot_animation(gb);
681 }
682 else {
683 0 uint32_t *output = gb->screen;
684 0 ▶ if (gb->border_mode != GB_BORDER_NEVER) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->border_mode != GB_BORDER_NEVER) { 0 0 0
685 0 output += 48 + 40 * 256;
686 }
687 0 uint8_t *input = gb->sgb->effective_screen_buffer;
688 0 switch ((mask_mode_t) gb->sgb->mask_mode) {
689 0 case MASK_DISABLED:
690 case MASK_FREEZE: {
691 0 ▶ for (unsigned y = 0; y < 144; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned y = 0; y < 144; y++) { 0 0 0
692 0 ▶ for (unsigned x = 0; x < 160; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned x = 0; x < 160; x++) { 0 0 0
693 0 uint8_t palette = gb->sgb->attribute_map[x / 8 + y / 8 * 20] & 3;
694 0 *(output++) = colors[(*(input++) & 3) + palette * 4];
695 }
696 0 ▶ if (gb->border_mode != GB_BORDER_NEVER) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->border_mode != GB_BORDER_NEVER) { 0 0 0
697 0 output += 256 - 160;
698 }
699 }
700 0 break;
701 }
702 0 case MASK_BLACK:
703 {
704 0 uint32_t black = convert_rgb15(gb, 0);
705 0 ▶ for (unsigned y = 0; y < 144; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned y = 0; y < 144; y++) { 0 0 0
706 0 ▶ for (unsigned x = 0; x < 160; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned x = 0; x < 160; x++) { 0 0 0
707 0 *(output++) = black;
708 }
709 0 ▶ if (gb->border_mode != GB_BORDER_NEVER) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->border_mode != GB_BORDER_NEVER) { 0 0 0
710 0 output += 256 - 160;
711 }
712 }
713 0 break;
714 }
715 0 case MASK_COLOR_0:
716 {
717 0 ▶ for (unsigned y = 0; y < 144; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned y = 0; y < 144; y++) { 0 0 0
718 0 ▶ for (unsigned x = 0; x < 160; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned x = 0; x < 160; x++) { 0 0 0
719 0 *(output++) = colors[0];
720 }
721 0 ▶ if (gb->border_mode != GB_BORDER_NEVER) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->border_mode != GB_BORDER_NEVER) { 0 0 0
722 0 output += 256 - 160;
723 }
724 }
725 0 break;
726 }
727 }
728 }
729
730 uint32_t border_colors[16 * 4];
731 0 ▶ if (gb->sgb->border_animation == 0 || gb->sgb->border_animation > 64 || gb->sgb->intro_animation < GB_SGB_INTRO_ANIMATION_LENGTH) {
Branch: 3 branches
BranchTrueFalseTotal
if (gb->sgb->border_animation == 0 || gb->sgb->border_animation > 64 || gb->sgb->intro_animation < GB_SGB_INTRO_ANIMATION_LENGTH) { 0 0 0
if (gb->sgb->border_animation == 0 || gb->sgb->border_animation > 64 || gb->sgb->intro_animation < GB_SGB_INTRO_ANIMATION_LENGTH) { 0 0 0
if (gb->sgb->border_animation == 0 || gb->sgb->border_animation > 64 || gb->sgb->intro_animation < GB_SGB_INTRO_ANIMATION_LENGTH) { 0 0 0
732 0 ▶ if (gb->sgb->border_animation != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb->border_animation != 0) { 0 0 0
733 0 gb->sgb->border_animation--;
734 }
735 0 ▶ for (unsigned i = 0; i < 16 * 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < 16 * 4; i++) { 0 0 0
736 0 border_colors[i] = convert_rgb15(gb, LE16(gb->sgb->border.palette[i]));
737 }
738 }
739 0 ▶ else if (gb->sgb->border_animation > 32) {
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->sgb->border_animation > 32) { 0 0 0
740 0 gb->sgb->border_animation--;
741 0 ▶ for (unsigned i = 0; i < 16 * 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < 16 * 4; i++) { 0 0 0
742 0 border_colors[i] = convert_rgb15_with_fade(gb, LE16(gb->sgb->border.palette[i]), 64 - gb->sgb->border_animation);
743 }
744 }
745 else {
746 0 gb->sgb->border_animation--;
747 0 ▶ for (unsigned i = 0; i < 16 * 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < 16 * 4; i++) { 0 0 0
748 0 border_colors[i] = convert_rgb15_with_fade(gb, LE16(gb->sgb->border.palette[i]), gb->sgb->border_animation);
749 }
750 }
751
752
753 0 ▶ if (gb->sgb->border_animation == 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb->border_animation == 32) { 0 0 0
754 0 memcpy(&gb->sgb->border, &gb->sgb->pending_border, sizeof(gb->sgb->border));
755 }
756
757 0 ▶ for (unsigned tile_y = 0; tile_y < 28; tile_y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned tile_y = 0; tile_y < 28; tile_y++) { 0 0 0
758 0 ▶ for (unsigned tile_x = 0; tile_x < 32; tile_x++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned tile_x = 0; tile_x < 32; tile_x++) { 0 0 0
759 0 bool gb_area = false;
760 0 ▶ if (tile_x >= 6 && tile_x < 26 && tile_y >= 5 && tile_y < 23) {
Branch: 4 branches
BranchTrueFalseTotal
if (tile_x >= 6 && tile_x < 26 && tile_y >= 5 && tile_y < 23) { 0 0 0
if (tile_x >= 6 && tile_x < 26 && tile_y >= 5 && tile_y < 23) { 0 0 0
if (tile_x >= 6 && tile_x < 26 && tile_y >= 5 && tile_y < 23) { 0 0 0
if (tile_x >= 6 && tile_x < 26 && tile_y >= 5 && tile_y < 23) { 0 0 0
761 0 gb_area = true;
762 }
763 0 ▶ else if (gb->border_mode == GB_BORDER_NEVER) {
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->border_mode == GB_BORDER_NEVER) { 0 0 0
764 0 continue;
765 }
766 0 uint16_t tile = LE16(gb->sgb->border.map[tile_x + tile_y * 32]);
767 0 ▶ if (tile & 0x300) continue; // Unused tile
Branch: 1 branch
BranchTrueFalseTotal
if (tile & 0x300) continue; // Unused tile 0 0 0
768 0 ▶ uint8_t flip_x = (tile & 0x4000)? 0:7;
Branch: 1 branch
BranchTrueFalseTotal
uint8_t flip_x = (tile & 0x4000)? 0:7; 0 0 0
769 0 ▶ uint8_t flip_y = (tile & 0x8000)? 7:0;
Branch: 1 branch
BranchTrueFalseTotal
uint8_t flip_y = (tile & 0x8000)? 7:0; 0 0 0
770 0 uint8_t palette = (tile >> 10) & 3;
771 0 ▶ for (unsigned y = 0; y < 8; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned y = 0; y < 8; y++) { 0 0 0
772 0 unsigned base = (tile & 0xFF) * 32 + (y ^ flip_y) * 2;
773 0 ▶ for (unsigned x = 0; x < 8; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned x = 0; x < 8; x++) { 0 0 0
774 0 uint8_t bit = 1 << (x ^ flip_x);
775 0 uint8_t color = ((gb->sgb->border.tiles[base] & bit) ? 1: 0) |
776 0 ▶ ((gb->sgb->border.tiles[base + 1] & bit) ? 2: 0) |
Branch: 1 branch
BranchTrueFalseTotal
((gb->sgb->border.tiles[base + 1] & bit) ? 2: 0) | 0 0 0
777 0 ▶ ((gb->sgb->border.tiles[base + 16] & bit) ? 4: 0) |
Branch: 1 branch
BranchTrueFalseTotal
((gb->sgb->border.tiles[base + 16] & bit) ? 4: 0) | 0 0 0
778 0 ▶ ((gb->sgb->border.tiles[base + 17] & bit) ? 8: 0);
Branch: 1 branch
BranchTrueFalseTotal
((gb->sgb->border.tiles[base + 17] & bit) ? 8: 0); 0 0 0
779
780 0 uint32_t *output = gb->screen;
781 0 ▶ if (gb->border_mode == GB_BORDER_NEVER) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->border_mode == GB_BORDER_NEVER) { 0 0 0
782 0 output += (tile_x - 6) * 8 + x + ((tile_y - 5) * 8 + y) * 160;
783 }
784 else {
785 0 output += tile_x * 8 + x + (tile_y * 8 + y) * 256;
786 }
787 0 ▶ if (color == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (color == 0) { 0 0 0
788 0 ▶ if (gb_area) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (gb_area) continue; 0 0 0
789 0 *output = colors[0];
790 }
791 else {
792 0 *output = border_colors[color + palette * 16];
793 }
794 }
795 }
796 }
797 }
798 }
799  
800 0 ▶void GB_sgb_load_default_data(GB_gameboy_t *gb)
Branch: 1 branch
BranchTrueFalseTotal
void GB_sgb_load_default_data(GB_gameboy_t *gb) 0 0 0
801 {
802
803 #include "graphics/sgb_border.inc"
804
805 #ifdef GB_BIG_ENDIAN
806 for (unsigned i = 0; i < sizeof(tilemap) / 2; i++) {
807 gb->sgb->border.map[i] = LE16(tilemap[i]);
808 }
809 for (unsigned i = 0; i < sizeof(palette) / 2; i++) {
810 gb->sgb->border.palette[i] = LE16(palette[i]);
811 }
812 #else
813 0 memcpy(gb->sgb->border.map, tilemap, sizeof(tilemap));
814 0 memcpy(gb->sgb->border.palette, palette, sizeof(palette));
815 #endif
816 0 memcpy(gb->sgb->border.tiles, tiles, sizeof(tiles));
817
818 0 ▶ if (gb->model != GB_MODEL_SGB2) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model != GB_MODEL_SGB2) { 0 0 0
819 /* Delete the "2" */
820 0 gb->sgb->border.map[25 * 32 + 25] = gb->sgb->border.map[25 * 32 + 26] =
821 0 gb->sgb->border.map[26 * 32 + 25] = gb->sgb->border.map[26 * 32 + 26] =
822 0 gb->sgb->border.map[27 * 32 + 25] = gb->sgb->border.map[27 * 32 + 26] =
823 0 gb->sgb->border.map[0];
824
825 /* Re-center */
826 0 memmove(&gb->sgb->border.map[25 * 32 + 1], &gb->sgb->border.map[25 * 32], (32 * 3 - 1) * sizeof(gb->sgb->border.map[0]));
827 }
828 0 gb->sgb->effective_palettes[0] = LE16(built_in_palettes[0]);
829 0 gb->sgb->effective_palettes[1] = LE16(built_in_palettes[1]);
830 0 gb->sgb->effective_palettes[2] = LE16(built_in_palettes[2]);
831 0 gb->sgb->effective_palettes[3] = LE16(built_in_palettes[3]);
832 0 }
833  
834 0 static double fm_sin(double phase)
835 {
836 #define SIN_TABLE_LENGTH 128
837 0 phase /= 2 * M_PI;
838 0 phase = fabs(phase);
839 0 phase -= floor(phase);
840 0 ▶ if (phase > 0.5) {
Branch: 1 branch
BranchTrueFalseTotal
if (phase > 0.5) { 0 0 0
841 0 return -(fm_sin(1 - phase));
842 }
843 0 ▶ if (phase > 0.25) {
Branch: 1 branch
BranchTrueFalseTotal
if (phase > 0.25) { 0 0 0
844 0 return fm_sin(0.5 - phase);
845 }
846
847 static bool once = false;
848 static double table[SIN_TABLE_LENGTH + 1];
849 0 ▶ if (!once) {
Branch: 1 branch
BranchTrueFalseTotal
if (!once) { 0 0 0
850 0 ▶ for (unsigned i = 0; i < SIN_TABLE_LENGTH + 1; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < SIN_TABLE_LENGTH + 1; i++) { 0 0 0
851 0 table[i] = sin(i * M_PI / 2 / SIN_TABLE_LENGTH);
852 }
853 0 once = true;
854 }
855
856 0 phase *= 4 * SIN_TABLE_LENGTH;
857 0 double fraction = phase - floor(phase);
858 0 return table[(unsigned)floor(phase)] * (1 - fraction) + table[(unsigned)ceil(phase)] * (fraction);
859 }
860  
861 0 static double fm_synth(double phase)
862 {
863 0 return (fm_sin(phase * M_PI * 2) +
864 0 fm_sin(phase * M_PI * 2 + fm_sin(phase * M_PI * 2)) +
865 0 fm_sin(phase * M_PI * 2 + fm_sin(phase * M_PI * 3)) +
866 0 fm_sin(phase * M_PI * 2 + fm_sin(phase * M_PI * 4))) / 4;
867 }
868  
869 0 static double fm_sweep(double phase)
870 {
871 0 double ret = 0;
872 0 ▶ for (unsigned i = 0; i < 8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < 8; i++) { 0 0 0
873 0 ret += fm_sin((phase * M_PI * 2 + fm_sin(phase * M_PI * 8) / 4) * pow(1.25, i)) * (8 - i) / 36;
874 }
875 0 return ret;
876 }
877 0 static double random_double(void)
878 {
879 0 return ((signed)(GB_random32() % 0x10001) - 0x8000) / (double) 0x8000;
880 }
881  
882 0 static void render_jingle(GB_gameboy_t *gb, size_t count)
883 {
884 0 const double frequencies[7] = {
885 466.16, // Bb4
886 587.33, // D5
887 698.46, // F5
888 830.61, // Ab5
889 1046.50, // C6
890 1244.51, // Eb6
891 1567.98, // G6
892 };
893
894 assert(gb->apu_output.sample_callback);
895
896 0 ▶ if (gb->sgb->intro_animation < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb->intro_animation < 0) { 0 0 0
897 0 GB_sample_t sample = {0, 0};
898 0 ▶ for (unsigned i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < count; i++) { 0 0 0
899 0 gb->apu_output.sample_callback(gb, &sample);
900 }
901 0 return;
902 }
903
904 0 ▶ if (gb->sgb->intro_animation >= GB_SGB_INTRO_ANIMATION_LENGTH) return;
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb->intro_animation >= GB_SGB_INTRO_ANIMATION_LENGTH) return; 0 0 0
905
906 0 signed jingle_stage = (gb->sgb->intro_animation - 64) / 3;
907 0 double sweep_cutoff_ratio = 2000.0 * pow(2, gb->sgb->intro_animation / 20.0) / gb->apu_output.sample_rate;
908 0 double sweep_phase_shift = 1000.0 * pow(2, gb->sgb->intro_animation / 40.0) / gb->apu_output.sample_rate;
909 0 ▶ if (sweep_cutoff_ratio > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (sweep_cutoff_ratio > 1) { 0 0 0
910 0 sweep_cutoff_ratio = 1;
911 }
912  
913 // Render at a lower resolution if our sample rate is too high
914 0 uint8_t downsample_mask = 0;
915 0 size_t temp_count = count;
916 0 ▶ while (temp_count > 2048) {
Branch: 1 branch
BranchTrueFalseTotal
while (temp_count > 2048) { 0 0 0
917 0 temp_count /= 2;
918 0 downsample_mask <<= 1;
919 0 downsample_mask |= 1;
920 0 sweep_phase_shift *= 2;
921 0 sweep_cutoff_ratio *= 2;
922 }
923
924 0 GB_sample_t stereo = {0, 0};
925 0 ▶ for (unsigned i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < count; i++) { 0 0 0
926 0 ▶ if ((i & downsample_mask)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((i & downsample_mask)) { 0 0 0
927 0 gb->apu_output.sample_callback(gb, &stereo);
928 0 continue;
929 }
930 0 double sample = 0;
931 0 ▶ for (signed f = 0; f < 7 && f < jingle_stage; f++) {
Branch: 2 branches
BranchTrueFalseTotal
for (signed f = 0; f < 7 && f < jingle_stage; f++) { 0 0 0
for (signed f = 0; f < 7 && f < jingle_stage; f++) { 0 0 0
932 0 sample += fm_synth(gb->sgb_intro_jingle_phases[f]) *
933 0 (0.75 * pow(0.5, jingle_stage - f) + 0.25) / 5.0;
934 0 gb->sgb_intro_jingle_phases[f] += (frequencies[f] / gb->apu_output.sample_rate) * (downsample_mask + 1);
935 }
936 0 ▶ if (gb->sgb->intro_animation > 100) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb->intro_animation > 100) { 0 0 0
937 0 sample *= pow((GB_SGB_INTRO_ANIMATION_LENGTH - gb->sgb->intro_animation) / (GB_SGB_INTRO_ANIMATION_LENGTH - 100.0), 3);
938 }
939
940 0 ▶ if (gb->sgb->intro_animation < 120) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb->intro_animation < 120) { 0 0 0
941 0 double next = fm_sweep(gb->sgb_intro_sweep_phase) * 0.3 + random_double() * 0.7;
942 0 gb->sgb_intro_sweep_phase += sweep_phase_shift;
943  
944 0 gb->sgb_intro_sweep_previous_sample = next * (sweep_cutoff_ratio) +
945 0 gb->sgb_intro_sweep_previous_sample * (1 - sweep_cutoff_ratio);
946 0 sample += gb->sgb_intro_sweep_previous_sample * pow((120 - gb->sgb->intro_animation) / 120.0, 2) * 0.8;
947 }
948
949 0 stereo.left = stereo.right = sample * 0x7000;
950 0 gb->apu_output.sample_callback(gb, &stereo);
951 }
952
953 0 return;
954 }
955  
956 0 unsigned GB_get_player_count(GB_gameboy_t *gb)
957 {
958 0 ▶ return GB_is_hle_sgb(gb)? gb->sgb->player_count : 1;
Branch: 1 branch
BranchTrueFalseTotal
return GB_is_hle_sgb(gb)? gb->sgb->player_count : 1; 0 0 0
959 }
« src/Core/workboy.c ↑ Summary src/Core/rumble.c »