src/gba/gba-bios.c (view raw)
1/* Copyright (c) 2013-2014 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "gba-bios.h"
7
8#include "gba.h"
9#include "gba-io.h"
10#include "gba-memory.h"
11#include "isa-inlines.h"
12
13const uint32_t GBA_BIOS_CHECKSUM = 0xBAAE187F;
14const uint32_t GBA_DS_BIOS_CHECKSUM = 0xBAAE1880;
15
16static void _unLz77(struct GBA* gba, int width);
17static void _unHuffman(struct GBA* gba);
18static void _unRl(struct GBA* gba, int width);
19static void _unFilter(struct GBA* gba, int inwidth, int outwidth);
20
21static void _SoftReset(struct GBA* gba) {
22 struct ARMCore* cpu = gba->cpu;
23 ARMSetPrivilegeMode(cpu, MODE_IRQ);
24 cpu->spsr.packed = 0;
25 cpu->gprs[ARM_LR] = 0;
26 cpu->gprs[ARM_SP] = SP_BASE_IRQ;
27 ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
28 cpu->spsr.packed = 0;
29 cpu->gprs[ARM_LR] = 0;
30 cpu->gprs[ARM_SP] = SP_BASE_SUPERVISOR;
31 ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
32 cpu->gprs[ARM_LR] = 0;
33 cpu->gprs[ARM_SP] = SP_BASE_SYSTEM;
34 int8_t flag = ((int8_t*) gba->memory.iwram)[0x7FFA];
35 memset(((int8_t*) gba->memory.iwram) + SIZE_WORKING_IRAM - 0x200, 0, 0x200);
36 if (flag) {
37 cpu->gprs[ARM_PC] = BASE_WORKING_RAM;
38 } else {
39 cpu->gprs[ARM_PC] = BASE_CART0;
40 }
41 _ARMSetMode(cpu, MODE_ARM);
42 int currentCycles = 0;
43 ARM_WRITE_PC;
44}
45
46static void _RegisterRamReset(struct GBA* gba) {
47 uint32_t registers = gba->cpu->gprs[0];
48 struct ARMCore* cpu = gba->cpu;
49 cpu->memory.store16(cpu, BASE_IO | REG_DISPCNT, 0x0080, 0);
50 if (registers & 0x01) {
51 memset(gba->memory.wram, 0, SIZE_WORKING_RAM);
52 }
53 if (registers & 0x02) {
54 memset(gba->memory.iwram, 0, SIZE_WORKING_IRAM - 0x200);
55 }
56 if (registers & 0x04) {
57 memset(gba->video.palette, 0, SIZE_PALETTE_RAM);
58 }
59 if (registers & 0x08) {
60 memset(gba->video.renderer->vram, 0, SIZE_VRAM);
61 }
62 if (registers & 0x10) {
63 memset(gba->video.oam.raw, 0, SIZE_OAM);
64 }
65 if (registers & 0x20) {
66 GBALog(gba, GBA_LOG_STUB, "RegisterRamReset on SIO unimplemented");
67 }
68 if (registers & 0x40) {
69 GBALog(gba, GBA_LOG_STUB, "RegisterRamReset on Audio unimplemented");
70 }
71 if (registers & 0x80) {
72 GBALog(gba, GBA_LOG_STUB, "RegisterRamReset on IO unimplemented");
73 }
74}
75
76static void _BgAffineSet(struct GBA* gba) {
77 struct ARMCore* cpu = gba->cpu;
78 int i = cpu->gprs[2];
79 float ox, oy;
80 float cx, cy;
81 float sx, sy;
82 float theta;
83 int offset = cpu->gprs[0];
84 int destination = cpu->gprs[1];
85 float a, b, c, d;
86 float rx, ry;
87 while (i--) {
88 // [ sx 0 0 ] [ cos(theta) -sin(theta) 0 ] [ 1 0 cx - ox ] [ A B rx ]
89 // [ 0 sy 0 ] * [ sin(theta) cos(theta) 0 ] * [ 0 1 cy - oy ] = [ C D ry ]
90 // [ 0 0 1 ] [ 0 0 1 ] [ 0 0 1 ] [ 0 0 1 ]
91 ox = cpu->memory.load32(cpu, offset, 0) / 256.f;
92 oy = cpu->memory.load32(cpu, offset + 4, 0) / 256.f;
93 cx = cpu->memory.load16(cpu, offset + 8, 0);
94 cy = cpu->memory.load16(cpu, offset + 10, 0);
95 sx = cpu->memory.load16(cpu, offset + 12, 0) / 256.f;
96 sy = cpu->memory.load16(cpu, offset + 14, 0) / 256.f;
97 theta = (cpu->memory.load16(cpu, offset + 16, 0) >> 8) / 128.f * M_PI;
98 offset += 20;
99 // Rotation
100 a = d = cosf(theta);
101 b = c = sinf(theta);
102 // Scale
103 a *= sx;
104 b *= -sx;
105 c *= sy;
106 d *= sy;
107 // Translate
108 rx = ox - (a * cx + b * cy);
109 ry = oy - (c * cx + d * cy);
110 cpu->memory.store16(cpu, destination, a * 256, 0);
111 cpu->memory.store16(cpu, destination + 2, b * 256, 0);
112 cpu->memory.store16(cpu, destination + 4, c * 256, 0);
113 cpu->memory.store16(cpu, destination + 6, d * 256, 0);
114 cpu->memory.store32(cpu, destination + 8, rx * 256, 0);
115 cpu->memory.store32(cpu, destination + 12, ry * 256, 0);
116 destination += 16;
117 }
118}
119
120static void _ObjAffineSet(struct GBA* gba) {
121 struct ARMCore* cpu = gba->cpu;
122 int i = cpu->gprs[2];
123 float sx, sy;
124 float theta;
125 int offset = cpu->gprs[0];
126 int destination = cpu->gprs[1];
127 int diff = cpu->gprs[3];
128 float a, b, c, d;
129 while (i--) {
130 // [ sx 0 ] [ cos(theta) -sin(theta) ] [ A B ]
131 // [ 0 sy ] * [ sin(theta) cos(theta) ] = [ C D ]
132 sx = cpu->memory.load16(cpu, offset, 0) / 256.f;
133 sy = cpu->memory.load16(cpu, offset + 2, 0) / 256.f;
134 theta = (cpu->memory.load16(cpu, offset + 4, 0) >> 8) / 128.f * M_PI;
135 offset += 8;
136 // Rotation
137 a = d = cosf(theta);
138 b = c = sinf(theta);
139 // Scale
140 a *= sx;
141 b *= -sx;
142 c *= sy;
143 d *= sy;
144 cpu->memory.store16(cpu, destination, a * 256, 0);
145 cpu->memory.store16(cpu, destination + diff, b * 256, 0);
146 cpu->memory.store16(cpu, destination + diff * 2, c * 256, 0);
147 cpu->memory.store16(cpu, destination + diff * 3, d * 256, 0);
148 destination += diff * 4;
149 }
150}
151
152static void _MidiKey2Freq(struct GBA* gba) {
153 struct ARMCore* cpu = gba->cpu;
154 uint32_t key = cpu->memory.load32(cpu, cpu->gprs[0] + 4, 0);
155 cpu->gprs[0] = key / powf(2, (180.f - cpu->gprs[1] - cpu->gprs[2] / 256.f) / 12.f);
156}
157
158static void _Div(struct GBA* gba, int32_t num, int32_t denom) {
159 struct ARMCore* cpu = gba->cpu;
160 if (denom != 0) {
161 div_t result = div(num, denom);
162 cpu->gprs[0] = result.quot;
163 cpu->gprs[1] = result.rem;
164 cpu->gprs[3] = abs(result.quot);
165 } else {
166 GBALog(gba, GBA_LOG_GAME_ERROR, "Attempting to divide %i by zero!", num);
167 // If abs(num) > 1, this should hang, but that would be painful to
168 // emulate in HLE, and no game will get into a state where it hangs...
169 cpu->gprs[0] = (num < 0) ? -1 : 1;
170 cpu->gprs[1] = num;
171 cpu->gprs[3] = 1;
172 }
173}
174
175void GBASwi16(struct ARMCore* cpu, int immediate) {
176 struct GBA* gba = (struct GBA*) cpu->master;
177 GBALog(gba, GBA_LOG_SWI, "SWI: %02X r0: %08X r1: %08X r2: %08X r3: %08X",
178 immediate, cpu->gprs[0], cpu->gprs[1], cpu->gprs[2], cpu->gprs[3]);
179
180 if (gba->memory.fullBios) {
181 ARMRaiseSWI(cpu);
182 return;
183 }
184 switch (immediate) {
185 case 0x0:
186 _SoftReset(gba);
187 break;
188 case 0x1:
189 _RegisterRamReset(gba);
190 break;
191 case 0x2:
192 GBAHalt(gba);
193 break;
194 case 0x05:
195 // VBlankIntrWait
196 // Fall through:
197 case 0x04:
198 // IntrWait
199 ARMRaiseSWI(cpu);
200 break;
201 case 0x6:
202 _Div(gba, cpu->gprs[0], cpu->gprs[1]);
203 break;
204 case 0x7:
205 _Div(gba, cpu->gprs[1], cpu->gprs[0]);
206 break;
207 case 0x8:
208 cpu->gprs[0] = sqrt(cpu->gprs[0]);
209 break;
210 case 0xA:
211 cpu->gprs[0] = atan2f(cpu->gprs[1] / 16384.f, cpu->gprs[0] / 16384.f) / (2 * M_PI) * 0x10000;
212 break;
213 case 0xB:
214 case 0xC:
215 ARMRaiseSWI(cpu);
216 break;
217 case 0xD:
218 cpu->gprs[0] = GBAChecksum(gba->memory.bios, SIZE_BIOS);
219 case 0xE:
220 _BgAffineSet(gba);
221 break;
222 case 0xF:
223 _ObjAffineSet(gba);
224 break;
225 case 0x11:
226 case 0x12:
227 if (cpu->gprs[0] < BASE_WORKING_RAM) {
228 GBALog(gba, GBA_LOG_GAME_ERROR, "Bad LZ77 source");
229 }
230 switch (cpu->gprs[1] >> BASE_OFFSET) {
231 default:
232 GBALog(gba, GBA_LOG_GAME_ERROR, "Bad LZ77 destination");
233 case REGION_WORKING_RAM:
234 case REGION_WORKING_IRAM:
235 case REGION_VRAM:
236 _unLz77(gba, immediate == 0x11 ? 1 : 2);
237 break;
238 }
239 break;
240 case 0x13:
241 if (cpu->gprs[0] < BASE_WORKING_RAM) {
242 GBALog(gba, GBA_LOG_GAME_ERROR, "Bad Huffman source");
243 }
244 switch (cpu->gprs[1] >> BASE_OFFSET) {
245 default:
246 GBALog(gba, GBA_LOG_GAME_ERROR, "Bad Huffman destination");
247 case REGION_WORKING_RAM:
248 case REGION_WORKING_IRAM:
249 case REGION_VRAM:
250 _unHuffman(gba);
251 break;
252 }
253 break;
254 case 0x14:
255 case 0x15:
256 if (cpu->gprs[0] < BASE_WORKING_RAM) {
257 GBALog(gba, GBA_LOG_GAME_ERROR, "Bad RL source");
258 }
259 switch (cpu->gprs[1] >> BASE_OFFSET) {
260 default:
261 GBALog(gba, GBA_LOG_GAME_ERROR, "Bad RL destination");
262 case REGION_WORKING_RAM:
263 case REGION_WORKING_IRAM:
264 case REGION_VRAM:
265 _unRl(gba, immediate == 0x14 ? 1 : 2);
266 break;
267 }
268 break;
269 case 0x16:
270 case 0x17:
271 case 0x18:
272 if (cpu->gprs[0] < BASE_WORKING_RAM) {
273 GBALog(gba, GBA_LOG_GAME_ERROR, "Bad UnFilter source");
274 }
275 switch (cpu->gprs[1] >> BASE_OFFSET) {
276 default:
277 GBALog(gba, GBA_LOG_GAME_ERROR, "Bad UnFilter destination");
278 case REGION_WORKING_RAM:
279 case REGION_WORKING_IRAM:
280 case REGION_VRAM:
281 _unFilter(gba, immediate == 0x18 ? 2 : 1, immediate == 0x16 ? 1 : 2);
282 break;
283 }
284 break;
285 case 0x1F:
286 _MidiKey2Freq(gba);
287 break;
288 default:
289 GBALog(gba, GBA_LOG_STUB, "Stub software interrupt: %02X", immediate);
290 }
291}
292
293void GBASwi32(struct ARMCore* cpu, int immediate) {
294 GBASwi16(cpu, immediate >> 16);
295}
296
297uint32_t GBAChecksum(uint32_t* memory, size_t size) {
298 size_t i;
299 uint32_t sum = 0;
300 for (i = 0; i < size; i += 4) {
301 sum += memory[i >> 2];
302 }
303 return sum;
304}
305
306static void _unLz77(struct GBA* gba, int width) {
307 struct ARMCore* cpu = gba->cpu;
308 uint32_t source = cpu->gprs[0];
309 uint32_t dest = cpu->gprs[1];
310 int remaining = (cpu->memory.load32(cpu, source, 0) & 0xFFFFFF00) >> 8;
311 // We assume the signature byte (0x10) is correct
312 int blockheader = 0; // Some compilers warn if this isn't set, even though it's trivially provably always set
313 source += 4;
314 int blocksRemaining = 0;
315 uint32_t disp;
316 int bytes;
317 int byte;
318 int halfword;
319 while (remaining > 0) {
320 if (blocksRemaining) {
321 if (blockheader & 0x80) {
322 // Compressed
323 int block = cpu->memory.load8(cpu, source + 1, 0) | (cpu->memory.load8(cpu, source, 0) << 8);
324 source += 2;
325 disp = dest - (block & 0x0FFF) - 1;
326 bytes = (block >> 12) + 3;
327 while (bytes-- && remaining) {
328 --remaining;
329 if (width == 2) {
330 byte = cpu->memory.load16(cpu, disp & ~1, 0);
331 if (dest & 1) {
332 byte >>= (disp & 1) * 8;
333 halfword |= byte << 8;
334 cpu->memory.store16(cpu, dest ^ 1, halfword, 0);
335 } else {
336 byte >>= (disp & 1) * 8;
337 halfword = byte & 0xFF;
338 }
339 } else {
340 byte = cpu->memory.load8(cpu, disp, 0);
341 cpu->memory.store8(cpu, dest, byte, 0);
342 }
343 ++disp;
344 ++dest;
345 }
346 } else {
347 // Uncompressed
348 byte = cpu->memory.load8(cpu, source, 0);
349 ++source;
350 if (width == 2) {
351 if (dest & 1) {
352 halfword |= byte << 8;
353 cpu->memory.store16(cpu, dest ^ 1, halfword, 0);
354 } else {
355 halfword = byte;
356 }
357 } else {
358 cpu->memory.store8(cpu, dest, byte, 0);
359 }
360 ++dest;
361 --remaining;
362 }
363 blockheader <<= 1;
364 --blocksRemaining;
365 } else {
366 blockheader = cpu->memory.load8(cpu, source, 0);
367 ++source;
368 blocksRemaining = 8;
369 }
370 }
371 cpu->gprs[0] = source;
372 cpu->gprs[1] = dest;
373 cpu->gprs[3] = 0;
374}
375
376DECL_BITFIELD(HuffmanNode, uint8_t);
377DECL_BITS(HuffmanNode, Offset, 0, 6);
378DECL_BIT(HuffmanNode, RTerm, 6);
379DECL_BIT(HuffmanNode, LTerm, 7);
380
381static void _unHuffman(struct GBA* gba) {
382 struct ARMCore* cpu = gba->cpu;
383 uint32_t source = cpu->gprs[0] & 0xFFFFFFFC;
384 uint32_t dest = cpu->gprs[1];
385 uint32_t header = cpu->memory.load32(cpu, source, 0);
386 int remaining = header >> 8;
387 int bits = header & 0xF;
388 if (32 % bits) {
389 GBALog(gba, GBA_LOG_STUB, "Unimplemented unaligned Huffman");
390 return;
391 }
392 int padding = (4 - remaining) & 0x3;
393 remaining &= 0xFFFFFFFC;
394 // We assume the signature byte (0x20) is correct
395 int treesize = (cpu->memory.load8(cpu, source + 4, 0) << 1) + 1;
396 int block = 0;
397 uint32_t treeBase = source + 5;
398 source += 5 + treesize;
399 uint32_t nPointer = treeBase;
400 HuffmanNode node;
401 int bitsRemaining;
402 int readBits;
403 int bitsSeen = 0;
404 node = cpu->memory.load8(cpu, nPointer, 0);
405 while (remaining > 0) {
406 uint32_t bitstream = cpu->memory.load32(cpu, source, 0);
407 source += 4;
408 for (bitsRemaining = 32; bitsRemaining > 0 && remaining > 0; --bitsRemaining, bitstream <<= 1) {
409 uint32_t next = (nPointer & ~1) + HuffmanNodeGetOffset(node) * 2 + 2;
410 if (bitstream & 0x80000000) {
411 // Go right
412 if (HuffmanNodeIsRTerm(node)) {
413 readBits = cpu->memory.load8(cpu, next + 1, 0);
414 } else {
415 nPointer = next + 1;
416 node = cpu->memory.load8(cpu, nPointer, 0);
417 continue;
418 }
419 } else {
420 // Go left
421 if (HuffmanNodeIsLTerm(node)) {
422 readBits = cpu->memory.load8(cpu, next, 0);
423 } else {
424 nPointer = next;
425 node = cpu->memory.load8(cpu, nPointer, 0);
426 continue;
427 }
428 }
429
430 block |= (readBits & ((1 << bits) - 1)) << bitsSeen;
431 bitsSeen += bits;
432 nPointer = treeBase;
433 node = cpu->memory.load8(cpu, nPointer, 0);
434 if (bitsSeen == 32) {
435 bitsSeen = 0;
436 cpu->memory.store32(cpu, dest, block, 0);
437 dest += 4;
438 remaining -= 4;
439 block = 0;
440 }
441 }
442
443 }
444 if (padding) {
445 cpu->memory.store32(cpu, dest, block, 0);
446 }
447 cpu->gprs[0] = source;
448 cpu->gprs[1] = dest;
449}
450
451static void _unRl(struct GBA* gba, int width) {
452 struct ARMCore* cpu = gba->cpu;
453 uint32_t source = cpu->gprs[0] & 0xFFFFFFFC;
454 int remaining = (cpu->memory.load32(cpu, source, 0) & 0xFFFFFF00) >> 8;
455 int padding = (4 - remaining) & 0x3;
456 // We assume the signature byte (0x30) is correct
457 int blockheader;
458 int block;
459 source += 4;
460 uint32_t dest = cpu->gprs[1];
461 int halfword = 0;
462 while (remaining > 0) {
463 blockheader = cpu->memory.load8(cpu, source, 0);
464 ++source;
465 if (blockheader & 0x80) {
466 // Compressed
467 blockheader &= 0x7F;
468 blockheader += 3;
469 block = cpu->memory.load8(cpu, source, 0);
470 ++source;
471 while (blockheader-- && remaining) {
472 --remaining;
473 if (width == 2) {
474 if (dest & 1) {
475 halfword |= block << 8;
476 cpu->memory.store16(cpu, dest ^ 1, halfword, 0);
477 } else {
478 halfword = block;
479 }
480 } else {
481 cpu->memory.store8(cpu, dest, block, 0);
482 }
483 ++dest;
484 }
485 } else {
486 // Uncompressed
487 blockheader++;
488 while (blockheader-- && remaining) {
489 --remaining;
490 int byte = cpu->memory.load8(cpu, source, 0);
491 ++source;
492 if (width == 2) {
493 if (dest & 1) {
494 halfword |= byte << 8;
495 cpu->memory.store16(cpu, dest ^ 1, halfword, 0);
496 } else {
497 halfword = byte;
498 }
499 } else {
500 cpu->memory.store8(cpu, dest, byte, 0);
501 }
502 ++dest;
503 }
504 }
505 }
506 if (width == 2) {
507 if (dest & 1) {
508 --padding;
509 ++dest;
510 }
511 for (; padding > 0; padding -= 2, dest += 2) {
512 cpu->memory.store16(cpu, dest, 0, 0);
513 }
514 } else {
515 while (padding--) {
516 cpu->memory.store8(cpu, dest, 0, 0);
517 ++dest;
518 }
519 }
520 cpu->gprs[0] = source;
521 cpu->gprs[1] = dest;
522}
523
524static void _unFilter(struct GBA* gba, int inwidth, int outwidth) {
525 struct ARMCore* cpu = gba->cpu;
526 uint32_t source = cpu->gprs[0] & 0xFFFFFFFC;
527 uint32_t dest = cpu->gprs[1];
528 uint32_t header = cpu->memory.load32(cpu, source, 0);
529 int remaining = header >> 8;
530 // We assume the signature nybble (0x8) is correct
531 uint16_t halfword = 0;
532 uint16_t old = 0;
533 source += 4;
534 while (remaining > 0) {
535 uint16_t new;
536 if (inwidth == 1) {
537 new = cpu->memory.load8(cpu, source, 0);
538 } else {
539 new = cpu->memory.load16(cpu, source, 0);
540 }
541 new += old;
542 if (outwidth > inwidth) {
543 halfword >>= 8;
544 halfword |= (new << 8);
545 if (source & 1) {
546 cpu->memory.store16(cpu, dest, halfword, 0);
547 dest += outwidth;
548 remaining -= outwidth;
549 }
550 } else if (outwidth == 1) {
551 cpu->memory.store8(cpu, dest, new, 0);
552 dest += outwidth;
553 remaining -= outwidth;
554 } else {
555 cpu->memory.store16(cpu, dest, new, 0);
556 dest += outwidth;
557 remaining -= outwidth;
558 }
559 old = new;
560 source += inwidth;
561 }
562 cpu->gprs[0] = source;
563 cpu->gprs[1] = dest;
564}