all repos — mgba @ 50402c830729f2ba5a6fc3e6facfd8b258f7f97d

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