all repos — mgba @ 05e04ba76a396c4e54933951bf72f90097865438

mGBA Game Boy Advance Emulator

src/gba/bios.c (view raw)

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