src/gba/gba-bios.c (view raw)
1#include "gba-bios.h"
2
3#include "gba.h"
4#include "gba-io.h"
5#include "gba-memory.h"
6
7#include <math.h>
8#include <stdlib.h>
9
10static void _unLz77(struct GBAMemory* memory, uint32_t source, uint8_t* dest);
11static void _unHuffman(struct GBAMemory* memory, uint32_t source, uint32_t* dest);
12static void _unRl(struct GBAMemory* memory, uint32_t source, uint8_t* dest);
13
14static void _RegisterRamReset(struct GBA* gba) {
15 uint32_t registers = gba->cpu.gprs[0];
16 (void)(registers);
17 GBALog(gba, GBA_LOG_STUB, "RegisterRamReset unimplemented");
18}
19
20static void _CpuSet(struct GBA* gba) {
21 uint32_t source = gba->cpu.gprs[0];
22 uint32_t dest = gba->cpu.gprs[1];
23 uint32_t mode = gba->cpu.gprs[2];
24 int count = mode & 0x000FFFFF;
25 int fill = mode & 0x01000000;
26 int wordsize = (mode & 0x04000000) ? 4 : 2;
27 int i;
28 if (fill) {
29 if (wordsize == 4) {
30 source &= 0xFFFFFFFC;
31 dest &= 0xFFFFFFFC;
32 int32_t word = gba->memory.d.load32(&gba->memory.d, source, &gba->cpu.cycles);
33 for (i = 0; i < count; ++i) {
34 gba->memory.d.store32(&gba->memory.d, dest + (i << 2), word, &gba->cpu.cycles);
35 gba->board.d.processEvents(&gba->board.d);
36 }
37 } else {
38 source &= 0xFFFFFFFE;
39 dest &= 0xFFFFFFFE;
40 uint16_t word = gba->memory.d.load16(&gba->memory.d, source, &gba->cpu.cycles);
41 for (i = 0; i < count; ++i) {
42 gba->memory.d.store16(&gba->memory.d, dest + (i << 1), word, &gba->cpu.cycles);
43 gba->board.d.processEvents(&gba->board.d);
44 }
45 }
46 } else {
47 if (wordsize == 4) {
48 source &= 0xFFFFFFFC;
49 dest &= 0xFFFFFFFC;
50 for (i = 0; i < count; ++i) {
51 int32_t word = gba->memory.d.load32(&gba->memory.d, source + (i << 2), &gba->cpu.cycles);
52 gba->memory.d.store32(&gba->memory.d, dest + (i << 2), word, &gba->cpu.cycles);
53 gba->board.d.processEvents(&gba->board.d);
54 }
55 } else {
56 source &= 0xFFFFFFFE;
57 dest &= 0xFFFFFFFE;
58 for (i = 0; i < count; ++i) {
59 uint16_t word = gba->memory.d.load16(&gba->memory.d, source + (i << 1), &gba->cpu.cycles);
60 gba->memory.d.store16(&gba->memory.d, dest + (i << 1), word, &gba->cpu.cycles);
61 gba->board.d.processEvents(&gba->board.d);
62 }
63 }
64 }
65}
66
67static void _FastCpuSet(struct GBA* gba) {
68 uint32_t source = gba->cpu.gprs[0] & 0xFFFFFFFC;
69 uint32_t dest = gba->cpu.gprs[1] & 0xFFFFFFFC;
70 uint32_t mode = gba->cpu.gprs[2];
71 int count = mode & 0x000FFFFF;
72 int storeCycles = gba->memory.d.waitMultiple(&gba->memory.d, dest, 4);
73 count = ((count + 7) >> 3) << 3;
74 int i;
75 if (mode & 0x01000000) {
76 int32_t word = gba->memory.d.load32(&gba->memory.d, source, &gba->cpu.cycles);
77 for (i = 0; i < count; i += 4) {
78 gba->memory.d.store32(&gba->memory.d, dest + ((i + 0) << 2), word, 0);
79 gba->memory.d.store32(&gba->memory.d, dest + ((i + 1) << 2), word, 0);
80 gba->memory.d.store32(&gba->memory.d, dest + ((i + 2) << 2), word, 0);
81 gba->memory.d.store32(&gba->memory.d, dest + ((i + 3) << 2), word, 0);
82 gba->cpu.cycles += storeCycles;
83 gba->board.d.processEvents(&gba->board.d);
84 }
85 } else {
86 int loadCycles = gba->memory.d.waitMultiple(&gba->memory.d, source, 4);
87 for (i = 0; i < count; i += 4) {
88 int32_t word0 = gba->memory.d.load32(&gba->memory.d, source + ((i + 0) << 2), 0);
89 int32_t word1 = gba->memory.d.load32(&gba->memory.d, source + ((i + 1) << 2), 0);
90 int32_t word2 = gba->memory.d.load32(&gba->memory.d, source + ((i + 2) << 2), 0);
91 int32_t word3 = gba->memory.d.load32(&gba->memory.d, source + ((i + 3) << 2), 0);
92 gba->cpu.cycles += loadCycles;
93 gba->board.d.processEvents(&gba->board.d);
94 gba->memory.d.store32(&gba->memory.d, dest + ((i + 0) << 2), word0, 0);
95 gba->memory.d.store32(&gba->memory.d, dest + ((i + 1) << 2), word1, 0);
96 gba->memory.d.store32(&gba->memory.d, dest + ((i + 2) << 2), word2, 0);
97 gba->memory.d.store32(&gba->memory.d, dest + ((i + 3) << 2), word3, 0);
98 gba->cpu.cycles += storeCycles;
99 gba->board.d.processEvents(&gba->board.d);
100 }
101 }
102}
103
104static void _BgAffineSet(struct GBA* gba) {
105 int i = gba->cpu.gprs[2];
106 float ox, oy;
107 float cx, cy;
108 float sx, sy;
109 float theta;
110 int offset = gba->cpu.gprs[0];
111 int destination = gba->cpu.gprs[1];
112 int diff = gba->cpu.gprs[3];
113 (void)(diff); // Are we supposed to use this?
114 float a, b, c, d;
115 float rx, ry;
116 while (i--) {
117 // [ sx 0 0 ] [ cos(theta) -sin(theta) 0 ] [ 1 0 cx - ox ] [ A B rx ]
118 // [ 0 sy 0 ] * [ sin(theta) cos(theta) 0 ] * [ 0 1 cy - oy ] = [ C D ry ]
119 // [ 0 0 1 ] [ 0 0 1 ] [ 0 0 1 ] [ 0 0 1 ]
120 ox = gba->memory.d.load32(&gba->memory.d, offset, 0) / 256.f;
121 oy = gba->memory.d.load32(&gba->memory.d, offset + 4, 0) / 256.f;
122 cx = gba->memory.d.load16(&gba->memory.d, offset + 8, 0);
123 cy = gba->memory.d.load16(&gba->memory.d, offset + 10, 0);
124 sx = gba->memory.d.load16(&gba->memory.d, offset + 12, 0) / 256.f;
125 sy = gba->memory.d.load16(&gba->memory.d, offset + 14, 0) / 256.f;
126 theta = (gba->memory.d.loadU16(&gba->memory.d, offset + 16, 0) >> 8) / 128.f * M_PI;
127 offset += 20;
128 // Rotation
129 a = d = cosf(theta);
130 b = c = sinf(theta);
131 // Scale
132 a *= sx;
133 b *= -sx;
134 c *= sy;
135 d *= sy;
136 // Translate
137 rx = ox - (a * cx + b * cy);
138 ry = oy - (c * cx + d * cy);
139 gba->memory.d.store16(&gba->memory.d, destination, a * 256, 0);
140 gba->memory.d.store16(&gba->memory.d, destination + 2, b * 256, 0);
141 gba->memory.d.store16(&gba->memory.d, destination + 4, c * 256, 0);
142 gba->memory.d.store16(&gba->memory.d, destination + 6, d * 256, 0);
143 gba->memory.d.store32(&gba->memory.d, destination + 8, rx * 256, 0);
144 gba->memory.d.store32(&gba->memory.d, destination + 12, ry * 256, 0);
145 destination += 16;
146 }
147}
148
149static void _ObjAffineSet(struct GBA* gba) {
150 int i = gba->cpu.gprs[2];
151 float sx, sy;
152 float theta;
153 int offset = gba->cpu.gprs[0];
154 int destination = gba->cpu.gprs[1];
155 int diff = gba->cpu.gprs[3];
156 float a, b, c, d;
157 while (i--) {
158 // [ sx 0 ] [ cos(theta) -sin(theta) ] [ A B ]
159 // [ 0 sy ] * [ sin(theta) cos(theta) ] = [ C D ]
160 sx = gba->memory.d.load16(&gba->memory.d, offset, 0) / 256.f;
161 sy = gba->memory.d.load16(&gba->memory.d, offset + 2, 0) / 256.f;
162 theta = (gba->memory.d.loadU16(&gba->memory.d, offset + 4, 0) >> 8) / 128.f * M_PI;
163 offset += 6;
164 // Rotation
165 a = d = cosf(theta);
166 b = c = sinf(theta);
167 // Scale
168 a *= sx;
169 b *= -sx;
170 c *= sy;
171 d *= sy;
172 gba->memory.d.store16(&gba->memory.d, destination, a * 256, 0);
173 gba->memory.d.store16(&gba->memory.d, destination + diff, b * 256, 0);
174 gba->memory.d.store16(&gba->memory.d, destination + diff * 2, c * 256, 0);
175 gba->memory.d.store16(&gba->memory.d, destination + diff * 3, d * 256, 0);
176 destination += diff * 4;
177 }
178}
179
180static void _MidiKey2Freq(struct GBA* gba) {
181 uint32_t key = gba->memory.d.load32(&gba->memory.d, gba->cpu.gprs[0] + 4, 0);
182 gba->cpu.gprs[0] = key / powf(2, (180.f - gba->cpu.gprs[1] - gba->cpu.gprs[2] / 256.f) / 12.f);
183}
184
185void GBASwi16(struct ARMBoard* board, int immediate) {
186 struct GBA* gba = ((struct GBABoard*) board)->p;
187 if (gba->memory.fullBios) {
188 ARMRaiseSWI(&gba->cpu);
189 return;
190 }
191 switch (immediate) {
192 case 0x1:
193 _RegisterRamReset(gba);
194 break;
195 case 0x2:
196 GBAHalt(gba);
197 break;
198 case 0x05:
199 // VBlankIntrWait
200 gba->cpu.gprs[0] = 1;
201 gba->cpu.gprs[1] = 1;
202 // Fall through:
203 case 0x04:
204 // IntrWait
205 gba->memory.io[REG_IME >> 1] = 1;
206 if (!gba->cpu.gprs[0] && gba->memory.io[REG_IF >> 1] & gba->cpu.gprs[1]) {
207 break;
208 }
209 gba->memory.io[REG_IF >> 1] = 0;
210 ARMRaiseSWI(&gba->cpu);
211 break;
212 case 0x6:
213 {
214 div_t result = div(gba->cpu.gprs[0], gba->cpu.gprs[1]);
215 gba->cpu.gprs[0] = result.quot;
216 gba->cpu.gprs[1] = result.rem;
217 gba->cpu.gprs[3] = abs(result.quot);
218 }
219 break;
220 case 0x7:
221 {
222 div_t result = div(gba->cpu.gprs[1], gba->cpu.gprs[0]);
223 gba->cpu.gprs[0] = result.quot;
224 gba->cpu.gprs[1] = result.rem;
225 gba->cpu.gprs[3] = abs(result.quot);
226 }
227 break;
228 case 0x8:
229 gba->cpu.gprs[0] = sqrt(gba->cpu.gprs[0]);
230 break;
231 case 0xA:
232 gba->cpu.gprs[0] = atan2f(gba->cpu.gprs[1] / 16384.f, gba->cpu.gprs[0] / 16384.f) / (2 * M_PI) * 0x10000;
233 break;
234 case 0xB:
235 _CpuSet(gba);
236 break;
237 case 0xC:
238 _FastCpuSet(gba);
239 break;
240 case 0xE:
241 _BgAffineSet(gba);
242 break;
243 case 0xF:
244 _ObjAffineSet(gba);
245 break;
246 case 0x11:
247 case 0x12:
248 if (gba->cpu.gprs[0] < BASE_WORKING_RAM) {
249 GBALog(gba, GBA_LOG_GAME_ERROR, "Bad LZ77 source");
250 break;
251 }
252 switch (gba->cpu.gprs[1] >> BASE_OFFSET) {
253 case REGION_WORKING_RAM:
254 _unLz77(&gba->memory, gba->cpu.gprs[0], &((uint8_t*) gba->memory.wram)[(gba->cpu.gprs[1] & (SIZE_WORKING_RAM - 1))]);
255 break;
256 case REGION_WORKING_IRAM:
257 _unLz77(&gba->memory, gba->cpu.gprs[0], &((uint8_t*) gba->memory.iwram)[(gba->cpu.gprs[1] & (SIZE_WORKING_IRAM - 1))]);
258 break;
259 case REGION_VRAM:
260 _unLz77(&gba->memory, gba->cpu.gprs[0], &((uint8_t*) gba->video.renderer->vram)[(gba->cpu.gprs[1] & 0x0001FFFF)]);
261 break;
262 default:
263 GBALog(gba, GBA_LOG_GAME_ERROR, "Bad LZ77 destination");
264 break;
265 }
266 break;
267 case 0x13:
268 if (gba->cpu.gprs[0] < BASE_WORKING_RAM) {
269 GBALog(gba, GBA_LOG_GAME_ERROR, "Bad Huffman source");
270 break;
271 }
272 switch (gba->cpu.gprs[1] >> BASE_OFFSET) {
273 case REGION_WORKING_RAM:
274 _unHuffman(&gba->memory, gba->cpu.gprs[0], &((uint32_t*) gba->memory.wram)[(gba->cpu.gprs[1] & (SIZE_WORKING_RAM - 3)) >> 2]);
275 break;
276 case REGION_WORKING_IRAM:
277 _unHuffman(&gba->memory, gba->cpu.gprs[0], &((uint32_t*) gba->memory.iwram)[(gba->cpu.gprs[1] & (SIZE_WORKING_IRAM - 3)) >> 2]);
278 break;
279 case REGION_VRAM:
280 _unHuffman(&gba->memory, gba->cpu.gprs[0], &((uint32_t*) gba->video.renderer->vram)[(gba->cpu.gprs[1] & 0x0001FFFC) >> 2]);
281 break;
282 default:
283 GBALog(gba, GBA_LOG_GAME_ERROR, "Bad Huffman destination");
284 break;
285 }
286 break;
287 case 0x14:
288 case 0x15:
289 if (gba->cpu.gprs[0] < BASE_WORKING_RAM) {
290 GBALog(gba, GBA_LOG_GAME_ERROR, "Bad RL source");
291 break;
292 }
293 switch (gba->cpu.gprs[1] >> BASE_OFFSET) {
294 case REGION_WORKING_RAM:
295 _unRl(&gba->memory, gba->cpu.gprs[0], &((uint8_t*) gba->memory.wram)[(gba->cpu.gprs[1] & (SIZE_WORKING_RAM - 1))]);
296 break;
297 case REGION_WORKING_IRAM:
298 _unRl(&gba->memory, gba->cpu.gprs[0], &((uint8_t*) gba->memory.iwram)[(gba->cpu.gprs[1] & (SIZE_WORKING_IRAM - 1))]);
299 break;
300 case REGION_VRAM:
301 _unRl(&gba->memory, gba->cpu.gprs[0], &((uint8_t*) gba->video.renderer->vram)[(gba->cpu.gprs[1] & 0x0001FFFF)]);
302 break;
303 default:
304 GBALog(gba, GBA_LOG_GAME_ERROR, "Bad RL destination");
305 break;
306 }
307 break;
308 case 0x1F:
309 _MidiKey2Freq(gba);
310 break;
311 default:
312 GBALog(gba, GBA_LOG_STUB, "Stub software interrupt: %02x", immediate);
313 }
314}
315
316void GBASwi32(struct ARMBoard* board, int immediate) {
317 GBASwi16(board, immediate >> 16);
318}
319
320static void _unLz77(struct GBAMemory* memory, uint32_t source, uint8_t* dest) {
321 int remaining = (memory->d.load32(&memory->d, source, 0) & 0xFFFFFF00) >> 8;
322 // We assume the signature byte (0x10) is correct
323 int blockheader;
324 uint32_t sPointer = source + 4;
325 uint8_t* dPointer = dest;
326 int blocksRemaining = 0;
327 int block;
328 uint8_t* disp;
329 int bytes;
330 while (remaining > 0) {
331 if (blocksRemaining) {
332 if (blockheader & 0x80) {
333 // Compressed
334 block = memory->d.loadU8(&memory->d, sPointer, 0) | (memory->d.loadU8(&memory->d, sPointer + 1, 0) << 8);
335 sPointer += 2;
336 disp = dPointer - (((block & 0x000F) << 8) | ((block & 0xFF00) >> 8)) - 1;
337 bytes = ((block & 0x00F0) >> 4) + 3;
338 while (bytes-- && remaining) {
339 --remaining;
340 *dPointer = *disp;
341 ++disp;
342 ++dPointer;
343 }
344 } else {
345 // Uncompressed
346 *dPointer = memory->d.loadU8(&memory->d, sPointer++, 0);
347 ++dPointer;
348 --remaining;
349 }
350 blockheader <<= 1;
351 --blocksRemaining;
352 } else {
353 blockheader = memory->d.loadU8(&memory->d, sPointer++, 0);
354 blocksRemaining = 8;
355 }
356 }
357}
358
359static void _unHuffman(struct GBAMemory* memory, uint32_t source, uint32_t* dest) {
360 source = source & 0xFFFFFFFC;
361 uint32_t header = memory->d.load32(&memory->d, source, 0);
362 int remaining = header >> 8;
363 int bits = header & 0xF;
364 if (32 % bits) {
365 GBALog(memory->p, GBA_LOG_STUB, "Unimplemented unaligned Huffman");
366 return;
367 }
368 int padding = (4 - remaining) & 0x3;
369 remaining &= 0xFFFFFFFC;
370 // We assume the signature byte (0x20) is correct
371 //var tree = [];
372 int treesize = (memory->d.loadU8(&memory->d, source + 4, 0) << 1) + 1;
373 int block = 0;
374 uint32_t treeBase = source + 5;
375 uint32_t sPointer = source + 5 + treesize;
376 uint32_t* dPointer = dest;
377 uint32_t nPointer = treeBase;
378 union HuffmanNode {
379 struct {
380 unsigned offset : 6;
381 unsigned rTerm : 1;
382 unsigned lTerm : 1;
383 };
384 uint8_t packed;
385 } node;
386 int bitsRemaining;
387 int readBits;
388 int bitsSeen = 0;
389 node.packed = memory->d.load8(&memory->d, nPointer, 0);
390 while (remaining > 0) {
391 uint32_t bitstream = memory->d.load32(&memory->d, sPointer, 0);
392 sPointer += 4;
393 for (bitsRemaining = 32; bitsRemaining > 0; --bitsRemaining, bitstream <<= 1) {
394 uint32_t next = (nPointer & ~1) + node.offset * 2 + 2;
395 if (bitstream & 0x80000000) {
396 // Go right
397 if (node.rTerm) {
398 readBits = memory->d.load8(&memory->d, next + 1, 0);
399 } else {
400 nPointer = next + 1;
401 node.packed = memory->d.load8(&memory->d, nPointer, 0);
402 continue;
403 }
404 } else {
405 // Go left
406 if (node.lTerm) {
407 readBits = memory->d.load8(&memory->d, next, 0);
408 } else {
409 nPointer = next;
410 node.packed = memory->d.load8(&memory->d, nPointer, 0);
411 continue;
412 }
413 }
414
415 block |= (readBits & ((1 << bits) - 1)) << bitsSeen;
416 bitsSeen += bits;
417 nPointer = treeBase;
418 node.packed = memory->d.load8(&memory->d, nPointer, 0);
419 if (bitsSeen == 32) {
420 bitsSeen = 0;
421 *dPointer = block;
422 ++dPointer;
423 remaining -= 4;
424 block = 0;
425 }
426 }
427
428 }
429 if (padding) {
430 *dPointer = block;
431 }
432}
433
434static void _unRl(struct GBAMemory* memory, uint32_t source, uint8_t* dest) {
435 source = source & 0xFFFFFFFC;
436 int remaining = (memory->d.load32(&memory->d, source, 0) & 0xFFFFFF00) >> 8;
437 int padding = (4 - remaining) & 0x3;
438 // We assume the signature byte (0x30) is correct
439 int blockheader;
440 int block;
441 uint32_t sPointer = source + 4;
442 uint8_t* dPointer = dest;
443 while (remaining > 0) {
444 blockheader = memory->d.loadU8(&memory->d, sPointer++, 0);
445 if (blockheader & 0x80) {
446 // Compressed
447 blockheader &= 0x7F;
448 blockheader += 3;
449 block = memory->d.loadU8(&memory->d, sPointer++, 0);
450 while (blockheader-- && remaining) {
451 --remaining;
452 *dPointer = block;
453 ++dPointer;
454 }
455 } else {
456 // Uncompressed
457 blockheader++;
458 while (blockheader-- && remaining) {
459 --remaining;
460 *dPointer = memory->d.loadU8(&memory->d, sPointer++, 0);
461 ++dPointer;
462 }
463 }
464 }
465 while (padding--) {
466 *dPointer = 0;
467 ++dPointer;
468 }
469}