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