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
7const uint32_t GBA_BIOS_CHECKSUM = 0xBAAE187F;
8const uint32_t GBA_DS_BIOS_CHECKSUM = 0xBAAE1880;
9
10static void _unLz77(struct GBA* gba, uint32_t source, uint8_t* dest);
11static void _unHuffman(struct GBA* gba, uint32_t source, uint32_t* dest);
12static void _unRl(struct GBA* gba, uint32_t source, uint8_t* dest);
13
14static void _RegisterRamReset(struct GBA* gba) {
15 uint32_t registers = gba->cpu->gprs[0];
16 UNUSED(registers);
17 GBALog(gba, GBA_LOG_STUB, "RegisterRamReset unimplemented");
18}
19
20static void _BgAffineSet(struct GBA* gba) {
21 struct ARMCore* cpu = gba->cpu;
22 int i = cpu->gprs[2];
23 float ox, oy;
24 float cx, cy;
25 float sx, sy;
26 float theta;
27 int offset = cpu->gprs[0];
28 int destination = cpu->gprs[1];
29 float a, b, c, d;
30 float rx, ry;
31 while (i--) {
32 // [ sx 0 0 ] [ cos(theta) -sin(theta) 0 ] [ 1 0 cx - ox ] [ A B rx ]
33 // [ 0 sy 0 ] * [ sin(theta) cos(theta) 0 ] * [ 0 1 cy - oy ] = [ C D ry ]
34 // [ 0 0 1 ] [ 0 0 1 ] [ 0 0 1 ] [ 0 0 1 ]
35 ox = cpu->memory.load32(cpu, offset, 0) / 256.f;
36 oy = cpu->memory.load32(cpu, offset + 4, 0) / 256.f;
37 cx = cpu->memory.load16(cpu, offset + 8, 0);
38 cy = cpu->memory.load16(cpu, offset + 10, 0);
39 sx = cpu->memory.load16(cpu, offset + 12, 0) / 256.f;
40 sy = cpu->memory.load16(cpu, offset + 14, 0) / 256.f;
41 theta = (cpu->memory.loadU16(cpu, offset + 16, 0) >> 8) / 128.f * M_PI;
42 offset += 20;
43 // Rotation
44 a = d = cosf(theta);
45 b = c = sinf(theta);
46 // Scale
47 a *= sx;
48 b *= -sx;
49 c *= sy;
50 d *= sy;
51 // Translate
52 rx = ox - (a * cx + b * cy);
53 ry = oy - (c * cx + d * cy);
54 cpu->memory.store16(cpu, destination, a * 256, 0);
55 cpu->memory.store16(cpu, destination + 2, b * 256, 0);
56 cpu->memory.store16(cpu, destination + 4, c * 256, 0);
57 cpu->memory.store16(cpu, destination + 6, d * 256, 0);
58 cpu->memory.store32(cpu, destination + 8, rx * 256, 0);
59 cpu->memory.store32(cpu, destination + 12, ry * 256, 0);
60 destination += 16;
61 }
62}
63
64static void _ObjAffineSet(struct GBA* gba) {
65 struct ARMCore* cpu = gba->cpu;
66 int i = cpu->gprs[2];
67 float sx, sy;
68 float theta;
69 int offset = cpu->gprs[0];
70 int destination = cpu->gprs[1];
71 int diff = cpu->gprs[3];
72 float a, b, c, d;
73 while (i--) {
74 // [ sx 0 ] [ cos(theta) -sin(theta) ] [ A B ]
75 // [ 0 sy ] * [ sin(theta) cos(theta) ] = [ C D ]
76 sx = cpu->memory.load16(cpu, offset, 0) / 256.f;
77 sy = cpu->memory.load16(cpu, offset + 2, 0) / 256.f;
78 theta = (cpu->memory.loadU16(cpu, offset + 4, 0) >> 8) / 128.f * M_PI;
79 offset += 8;
80 // Rotation
81 a = d = cosf(theta);
82 b = c = sinf(theta);
83 // Scale
84 a *= sx;
85 b *= -sx;
86 c *= sy;
87 d *= sy;
88 cpu->memory.store16(cpu, destination, a * 256, 0);
89 cpu->memory.store16(cpu, destination + diff, b * 256, 0);
90 cpu->memory.store16(cpu, destination + diff * 2, c * 256, 0);
91 cpu->memory.store16(cpu, destination + diff * 3, d * 256, 0);
92 destination += diff * 4;
93 }
94}
95
96static void _MidiKey2Freq(struct GBA* gba) {
97 struct ARMCore* cpu = gba->cpu;
98 uint32_t key = cpu->memory.load32(cpu, cpu->gprs[0] + 4, 0);
99 cpu->gprs[0] = key / powf(2, (180.f - cpu->gprs[1] - cpu->gprs[2] / 256.f) / 12.f);
100}
101
102static void _Div(struct ARMCore* cpu, int32_t num, int32_t denom) {
103 if (denom != 0) {
104 div_t result = div(num, denom);
105 cpu->gprs[0] = result.quot;
106 cpu->gprs[1] = result.rem;
107 cpu->gprs[3] = abs(result.quot);
108 } else {
109 // If abs(num) > 1, this should hang, but that would be painful to
110 // emulate in HLE, and no game will get into a state where it hangs...
111 cpu->gprs[0] = (num < 0) ? -1 : 1;
112 cpu->gprs[1] = num;
113 cpu->gprs[3] = 1;
114 }
115}
116
117void GBASwi16(struct ARMCore* cpu, int immediate) {
118 struct GBA* gba = (struct GBA*) cpu->master;
119 GBALog(gba, GBA_LOG_DEBUG, "SWI: %02x", immediate);
120
121 if (gba->memory.fullBios) {
122 ARMRaiseSWI(cpu);
123 return;
124 }
125 switch (immediate) {
126 case 0x1:
127 _RegisterRamReset(gba);
128 break;
129 case 0x2:
130 GBAHalt(gba);
131 break;
132 case 0x05:
133 // VBlankIntrWait
134 // Fall through:
135 case 0x04:
136 // IntrWait
137 ARMRaiseSWI(cpu);
138 break;
139 case 0x6:
140 _Div(cpu, cpu->gprs[0], cpu->gprs[1]);
141 break;
142 case 0x7:
143 _Div(cpu, cpu->gprs[1], cpu->gprs[0]);
144 break;
145 case 0x8:
146 cpu->gprs[0] = sqrt(cpu->gprs[0]);
147 break;
148 case 0xA:
149 cpu->gprs[0] = atan2f(cpu->gprs[1] / 16384.f, cpu->gprs[0] / 16384.f) / (2 * M_PI) * 0x10000;
150 break;
151 case 0xB:
152 case 0xC:
153 ARMRaiseSWI(cpu);
154 break;
155 case 0xD:
156 cpu->gprs[0] = GBAChecksum(gba->memory.bios, SIZE_BIOS);
157 case 0xE:
158 _BgAffineSet(gba);
159 break;
160 case 0xF:
161 _ObjAffineSet(gba);
162 break;
163 case 0x11:
164 case 0x12:
165 if (cpu->gprs[0] < BASE_WORKING_RAM) {
166 GBALog(gba, GBA_LOG_GAME_ERROR, "Bad LZ77 source");
167 break;
168 }
169 switch (cpu->gprs[1] >> BASE_OFFSET) {
170 case REGION_WORKING_RAM:
171 _unLz77(gba, cpu->gprs[0], &((uint8_t*) gba->memory.wram)[(cpu->gprs[1] & (SIZE_WORKING_RAM - 1))]);
172 break;
173 case REGION_WORKING_IRAM:
174 _unLz77(gba, cpu->gprs[0], &((uint8_t*) gba->memory.iwram)[(cpu->gprs[1] & (SIZE_WORKING_IRAM - 1))]);
175 break;
176 case REGION_VRAM:
177 _unLz77(gba, cpu->gprs[0], &((uint8_t*) gba->video.renderer->vram)[(cpu->gprs[1] & 0x0001FFFF)]);
178 break;
179 default:
180 GBALog(gba, GBA_LOG_GAME_ERROR, "Bad LZ77 destination");
181 break;
182 }
183 break;
184 case 0x13:
185 if (cpu->gprs[0] < BASE_WORKING_RAM) {
186 GBALog(gba, GBA_LOG_GAME_ERROR, "Bad Huffman source");
187 break;
188 }
189 switch (cpu->gprs[1] >> BASE_OFFSET) {
190 case REGION_WORKING_RAM:
191 _unHuffman(gba, cpu->gprs[0], &((uint32_t*) gba->memory.wram)[(cpu->gprs[1] & (SIZE_WORKING_RAM - 3)) >> 2]);
192 break;
193 case REGION_WORKING_IRAM:
194 _unHuffman(gba, cpu->gprs[0], &((uint32_t*) gba->memory.iwram)[(cpu->gprs[1] & (SIZE_WORKING_IRAM - 3)) >> 2]);
195 break;
196 case REGION_VRAM:
197 _unHuffman(gba, cpu->gprs[0], &((uint32_t*) gba->video.renderer->vram)[(cpu->gprs[1] & 0x0001FFFC) >> 2]);
198 break;
199 default:
200 GBALog(gba, GBA_LOG_GAME_ERROR, "Bad Huffman destination");
201 break;
202 }
203 break;
204 case 0x14:
205 case 0x15:
206 if (cpu->gprs[0] < BASE_WORKING_RAM) {
207 GBALog(gba, GBA_LOG_GAME_ERROR, "Bad RL source");
208 break;
209 }
210 switch (cpu->gprs[1] >> BASE_OFFSET) {
211 case REGION_WORKING_RAM:
212 _unRl(gba, cpu->gprs[0], &((uint8_t*) gba->memory.wram)[(cpu->gprs[1] & (SIZE_WORKING_RAM - 1))]);
213 break;
214 case REGION_WORKING_IRAM:
215 _unRl(gba, cpu->gprs[0], &((uint8_t*) gba->memory.iwram)[(cpu->gprs[1] & (SIZE_WORKING_IRAM - 1))]);
216 break;
217 case REGION_VRAM:
218 _unRl(gba, cpu->gprs[0], &((uint8_t*) gba->video.renderer->vram)[(cpu->gprs[1] & 0x0001FFFF)]);
219 break;
220 default:
221 GBALog(gba, GBA_LOG_GAME_ERROR, "Bad RL destination");
222 break;
223 }
224 break;
225 case 0x1F:
226 _MidiKey2Freq(gba);
227 break;
228 default:
229 GBALog(gba, GBA_LOG_STUB, "Stub software interrupt: %02x", immediate);
230 }
231}
232
233void GBASwi32(struct ARMCore* cpu, int immediate) {
234 GBASwi16(cpu, immediate >> 16);
235}
236
237uint32_t GBAChecksum(uint32_t* memory, size_t size) {
238 size_t i;
239 uint32_t sum = 0;
240 for (i = 0; i < size; i += 4) {
241 sum += memory[i >> 2];
242 }
243 return sum;
244}
245
246static void _unLz77(struct GBA* gba, uint32_t source, uint8_t* dest) {
247 struct ARMCore* cpu = gba->cpu;
248 int remaining = (cpu->memory.load32(cpu, source, 0) & 0xFFFFFF00) >> 8;
249 // We assume the signature byte (0x10) is correct
250 int blockheader;
251 uint32_t sPointer = source + 4;
252 uint8_t* dPointer = dest;
253 int blocksRemaining = 0;
254 int block;
255 uint8_t* disp;
256 int bytes;
257 while (remaining > 0) {
258 if (blocksRemaining) {
259 if (blockheader & 0x80) {
260 // Compressed
261 block = cpu->memory.loadU8(cpu, sPointer, 0) | (cpu->memory.loadU8(cpu, sPointer + 1, 0) << 8);
262 sPointer += 2;
263 disp = dPointer - (((block & 0x000F) << 8) | ((block & 0xFF00) >> 8)) - 1;
264 bytes = ((block & 0x00F0) >> 4) + 3;
265 while (bytes-- && remaining) {
266 --remaining;
267 *dPointer = *disp;
268 ++disp;
269 ++dPointer;
270 }
271 } else {
272 // Uncompressed
273 *dPointer = cpu->memory.loadU8(cpu, sPointer++, 0);
274 ++dPointer;
275 --remaining;
276 }
277 blockheader <<= 1;
278 --blocksRemaining;
279 } else {
280 blockheader = cpu->memory.loadU8(cpu, sPointer++, 0);
281 blocksRemaining = 8;
282 }
283 }
284}
285
286static void _unHuffman(struct GBA* gba, uint32_t source, uint32_t* dest) {
287 struct ARMCore* cpu = gba->cpu;
288 source = source & 0xFFFFFFFC;
289 uint32_t header = cpu->memory.load32(cpu, source, 0);
290 int remaining = header >> 8;
291 int bits = header & 0xF;
292 if (32 % bits) {
293 GBALog(gba, GBA_LOG_STUB, "Unimplemented unaligned Huffman");
294 return;
295 }
296 int padding = (4 - remaining) & 0x3;
297 remaining &= 0xFFFFFFFC;
298 // We assume the signature byte (0x20) is correct
299 //var tree = [];
300 int treesize = (cpu->memory.loadU8(cpu, source + 4, 0) << 1) + 1;
301 int block = 0;
302 uint32_t treeBase = source + 5;
303 uint32_t sPointer = source + 5 + treesize;
304 uint32_t* dPointer = dest;
305 uint32_t nPointer = treeBase;
306 union HuffmanNode {
307 struct {
308 unsigned offset : 6;
309 unsigned rTerm : 1;
310 unsigned lTerm : 1;
311 };
312 uint8_t packed;
313 } node;
314 int bitsRemaining;
315 int readBits;
316 int bitsSeen = 0;
317 node.packed = cpu->memory.load8(cpu, nPointer, 0);
318 while (remaining > 0) {
319 uint32_t bitstream = cpu->memory.load32(cpu, sPointer, 0);
320 sPointer += 4;
321 for (bitsRemaining = 32; bitsRemaining > 0; --bitsRemaining, bitstream <<= 1) {
322 uint32_t next = (nPointer & ~1) + node.offset * 2 + 2;
323 if (bitstream & 0x80000000) {
324 // Go right
325 if (node.rTerm) {
326 readBits = cpu->memory.load8(cpu, next + 1, 0);
327 } else {
328 nPointer = next + 1;
329 node.packed = cpu->memory.load8(cpu, nPointer, 0);
330 continue;
331 }
332 } else {
333 // Go left
334 if (node.lTerm) {
335 readBits = cpu->memory.load8(cpu, next, 0);
336 } else {
337 nPointer = next;
338 node.packed = cpu->memory.load8(cpu, nPointer, 0);
339 continue;
340 }
341 }
342
343 block |= (readBits & ((1 << bits) - 1)) << bitsSeen;
344 bitsSeen += bits;
345 nPointer = treeBase;
346 node.packed = cpu->memory.load8(cpu, nPointer, 0);
347 if (bitsSeen == 32) {
348 bitsSeen = 0;
349 *dPointer = block;
350 ++dPointer;
351 remaining -= 4;
352 block = 0;
353 }
354 }
355
356 }
357 if (padding) {
358 *dPointer = block;
359 }
360}
361
362static void _unRl(struct GBA* gba, uint32_t source, uint8_t* dest) {
363 struct ARMCore* cpu = gba->cpu;
364 source = source & 0xFFFFFFFC;
365 int remaining = (cpu->memory.load32(cpu, source, 0) & 0xFFFFFF00) >> 8;
366 int padding = (4 - remaining) & 0x3;
367 // We assume the signature byte (0x30) is correct
368 int blockheader;
369 int block;
370 uint32_t sPointer = source + 4;
371 uint8_t* dPointer = dest;
372 while (remaining > 0) {
373 blockheader = cpu->memory.loadU8(cpu, sPointer++, 0);
374 if (blockheader & 0x80) {
375 // Compressed
376 blockheader &= 0x7F;
377 blockheader += 3;
378 block = cpu->memory.loadU8(cpu, sPointer++, 0);
379 while (blockheader-- && remaining) {
380 --remaining;
381 *dPointer = block;
382 ++dPointer;
383 }
384 } else {
385 // Uncompressed
386 blockheader++;
387 while (blockheader-- && remaining) {
388 --remaining;
389 *dPointer = cpu->memory.loadU8(cpu, sPointer++, 0);
390 ++dPointer;
391 }
392 }
393 }
394 while (padding--) {
395 *dPointer = 0;
396 ++dPointer;
397 }
398}