all repos — mgba @ c6f4f233321069a7e705cb78960d986110ae7f32

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_WORKING_RAM) {
224			GBALog(gba, GBA_LOG_GAME_ERROR, "Cannot CpuSet from BIOS");
225			return;
226		}
227		if (cpu->gprs[0] & (cpu->gprs[2] & (1 << 26) ? 3 : 1)) {
228			GBALog(gba, GBA_LOG_GAME_ERROR, "Misaligned CpuSet source");
229		}
230		if (cpu->gprs[1] & (cpu->gprs[2] & (1 << 26) ? 3 : 1)) {
231			GBALog(gba, GBA_LOG_GAME_ERROR, "Misaligned CpuSet destination");
232		}
233		ARMRaiseSWI(cpu);
234		break;
235	case 0xD:
236		cpu->gprs[0] = GBA_BIOS_CHECKSUM;
237		cpu->gprs[1] = 1;
238		cpu->gprs[3] = SIZE_BIOS;
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 (cpu->gprs[0] < BASE_WORKING_RAM) {
249			GBALog(gba, GBA_LOG_GAME_ERROR, "Bad LZ77 source");
250			break;
251		}
252		switch (cpu->gprs[1] >> BASE_OFFSET) {
253		default:
254			GBALog(gba, GBA_LOG_GAME_ERROR, "Bad LZ77 destination");
255		// Fall through
256		case REGION_WORKING_RAM:
257		case REGION_WORKING_IRAM:
258		case REGION_VRAM:
259			_unLz77(gba, immediate == 0x11 ? 1 : 2);
260			break;
261		}
262		break;
263	case 0x13:
264		if (cpu->gprs[0] < BASE_WORKING_RAM) {
265			GBALog(gba, GBA_LOG_GAME_ERROR, "Bad Huffman source");
266			break;
267		}
268		switch (cpu->gprs[1] >> BASE_OFFSET) {
269		default:
270			GBALog(gba, GBA_LOG_GAME_ERROR, "Bad Huffman destination");
271		// Fall through
272		case REGION_WORKING_RAM:
273		case REGION_WORKING_IRAM:
274		case REGION_VRAM:
275			_unHuffman(gba);
276			break;
277		}
278		break;
279	case 0x14:
280	case 0x15:
281		if (cpu->gprs[0] < BASE_WORKING_RAM) {
282			GBALog(gba, GBA_LOG_GAME_ERROR, "Bad RL source");
283			break;
284		}
285		switch (cpu->gprs[1] >> BASE_OFFSET) {
286		default:
287			GBALog(gba, GBA_LOG_GAME_ERROR, "Bad RL destination");
288		// Fall through
289		case REGION_WORKING_RAM:
290		case REGION_WORKING_IRAM:
291		case REGION_VRAM:
292			_unRl(gba, immediate == 0x14 ? 1 : 2);
293			break;
294		}
295		break;
296	case 0x16:
297	case 0x17:
298	case 0x18:
299		if (cpu->gprs[0] < BASE_WORKING_RAM) {
300			GBALog(gba, GBA_LOG_GAME_ERROR, "Bad UnFilter source");
301			break;
302		}
303		switch (cpu->gprs[1] >> BASE_OFFSET) {
304		default:
305			GBALog(gba, GBA_LOG_GAME_ERROR, "Bad UnFilter destination");
306		// Fall through
307		case REGION_WORKING_RAM:
308		case REGION_WORKING_IRAM:
309		case REGION_VRAM:
310			_unFilter(gba, immediate == 0x18 ? 2 : 1, immediate == 0x16 ? 1 : 2);
311			break;
312		}
313		break;
314	case 0x19:
315		// SoundBias is mostly meaningless here
316		GBALog(gba, GBA_LOG_STUB, "Stub software interrupt: SoundBias (19)");
317		break;
318	case 0x1F:
319		_MidiKey2Freq(gba);
320		break;
321	default:
322		GBALog(gba, GBA_LOG_STUB, "Stub software interrupt: %02X", immediate);
323	}
324	gba->memory.biosPrefetch = 0xE3A02004;
325}
326
327void GBASwi32(struct ARMCore* cpu, int immediate) {
328	GBASwi16(cpu, immediate >> 16);
329}
330
331uint32_t GBAChecksum(uint32_t* memory, size_t size) {
332	size_t i;
333	uint32_t sum = 0;
334	for (i = 0; i < size; i += 4) {
335		sum += memory[i >> 2];
336	}
337	return sum;
338}
339
340static void _unLz77(struct GBA* gba, int width) {
341	struct ARMCore* cpu = gba->cpu;
342	uint32_t source = cpu->gprs[0];
343	uint32_t dest = cpu->gprs[1];
344	int remaining = (cpu->memory.load32(cpu, source, 0) & 0xFFFFFF00) >> 8;
345	// We assume the signature byte (0x10) is correct
346	int blockheader = 0; // Some compilers warn if this isn't set, even though it's trivially provably always set
347	source += 4;
348	int blocksRemaining = 0;
349	uint32_t disp;
350	int bytes;
351	int byte;
352	int halfword = 0;
353	while (remaining > 0) {
354		if (blocksRemaining) {
355			if (blockheader & 0x80) {
356				// Compressed
357				int block = cpu->memory.load8(cpu, source + 1, 0) | (cpu->memory.load8(cpu, source, 0) << 8);
358				source += 2;
359				disp = dest - (block & 0x0FFF) - 1;
360				bytes = (block >> 12) + 3;
361				while (bytes-- && remaining) {
362					--remaining;
363					if (width == 2) {
364						byte = (int16_t) cpu->memory.load16(cpu, disp & ~1, 0);
365						if (dest & 1) {
366							byte >>= (disp & 1) * 8;
367							halfword |= byte << 8;
368							cpu->memory.store16(cpu, dest ^ 1, halfword, 0);
369						} else {
370							byte >>= (disp & 1) * 8;
371							halfword = byte & 0xFF;
372						}
373					} else {
374						byte = cpu->memory.load8(cpu, disp, 0);
375						cpu->memory.store8(cpu, dest, byte, 0);
376					}
377					++disp;
378					++dest;
379				}
380			} else {
381				// Uncompressed
382				byte = cpu->memory.load8(cpu, source, 0);
383				++source;
384				if (width == 2) {
385					if (dest & 1) {
386						halfword |= byte << 8;
387						cpu->memory.store16(cpu, dest ^ 1, halfword, 0);
388					} else {
389						halfword = byte;
390					}
391				} else {
392					cpu->memory.store8(cpu, dest, byte, 0);
393				}
394				++dest;
395				--remaining;
396			}
397			blockheader <<= 1;
398			--blocksRemaining;
399		} else {
400			blockheader = cpu->memory.load8(cpu, source, 0);
401			++source;
402			blocksRemaining = 8;
403		}
404	}
405	cpu->gprs[0] = source;
406	cpu->gprs[1] = dest;
407	cpu->gprs[3] = 0;
408}
409
410DECL_BITFIELD(HuffmanNode, uint8_t);
411DECL_BITS(HuffmanNode, Offset, 0, 6);
412DECL_BIT(HuffmanNode, RTerm, 6);
413DECL_BIT(HuffmanNode, LTerm, 7);
414
415static void _unHuffman(struct GBA* gba) {
416	struct ARMCore* cpu = gba->cpu;
417	uint32_t source = cpu->gprs[0] & 0xFFFFFFFC;
418	uint32_t dest = cpu->gprs[1];
419	uint32_t header = cpu->memory.load32(cpu, source, 0);
420	int remaining = header >> 8;
421	int bits = header & 0xF;
422	if (bits == 0) {
423		GBALog(gba, GBA_LOG_GAME_ERROR, "Invalid Huffman bits");
424		bits = 8;
425	}
426	if (32 % bits || bits == 1) {
427		GBALog(gba, GBA_LOG_STUB, "Unimplemented unaligned Huffman");
428		return;
429	}
430	// We assume the signature byte (0x20) is correct
431	int treesize = (cpu->memory.load8(cpu, source + 4, 0) << 1) + 1;
432	int block = 0;
433	uint32_t treeBase = source + 5;
434	source += 5 + treesize;
435	uint32_t nPointer = treeBase;
436	HuffmanNode node;
437	int bitsRemaining;
438	int readBits;
439	int bitsSeen = 0;
440	node = cpu->memory.load8(cpu, nPointer, 0);
441	while (remaining > 0) {
442		uint32_t bitstream = cpu->memory.load32(cpu, source, 0);
443		source += 4;
444		for (bitsRemaining = 32; bitsRemaining > 0 && remaining > 0; --bitsRemaining, bitstream <<= 1) {
445			uint32_t next = (nPointer & ~1) + HuffmanNodeGetOffset(node) * 2 + 2;
446			if (bitstream & 0x80000000) {
447				// Go right
448				if (HuffmanNodeIsRTerm(node)) {
449					readBits = cpu->memory.load8(cpu, next + 1, 0);
450				} else {
451					nPointer = next + 1;
452					node = cpu->memory.load8(cpu, nPointer, 0);
453					continue;
454				}
455			} else {
456				// Go left
457				if (HuffmanNodeIsLTerm(node)) {
458					readBits = cpu->memory.load8(cpu, next, 0);
459				} else {
460					nPointer = next;
461					node = cpu->memory.load8(cpu, nPointer, 0);
462					continue;
463				}
464			}
465
466			block |= (readBits & ((1 << bits) - 1)) << bitsSeen;
467			bitsSeen += bits;
468			nPointer = treeBase;
469			node = cpu->memory.load8(cpu, nPointer, 0);
470			if (bitsSeen == 32) {
471				bitsSeen = 0;
472				cpu->memory.store32(cpu, dest, block, 0);
473				dest += 4;
474				remaining -= 4;
475				block = 0;
476			}
477		}
478	}
479	cpu->gprs[0] = source;
480	cpu->gprs[1] = dest;
481}
482
483static void _unRl(struct GBA* gba, int width) {
484	struct ARMCore* cpu = gba->cpu;
485	uint32_t source = cpu->gprs[0] & 0xFFFFFFFC;
486	int remaining = (cpu->memory.load32(cpu, source, 0) & 0xFFFFFF00) >> 8;
487	int padding = (4 - remaining) & 0x3;
488	// We assume the signature byte (0x30) is correct
489	int blockheader;
490	int block;
491	source += 4;
492	uint32_t dest = cpu->gprs[1];
493	int halfword = 0;
494	while (remaining > 0) {
495		blockheader = cpu->memory.load8(cpu, source, 0);
496		++source;
497		if (blockheader & 0x80) {
498			// Compressed
499			blockheader &= 0x7F;
500			blockheader += 3;
501			block = cpu->memory.load8(cpu, source, 0);
502			++source;
503			while (blockheader-- && remaining) {
504				--remaining;
505				if (width == 2) {
506					if (dest & 1) {
507						halfword |= block << 8;
508						cpu->memory.store16(cpu, dest ^ 1, halfword, 0);
509					} else {
510						halfword = block;
511					}
512				} else {
513					cpu->memory.store8(cpu, dest, block, 0);
514				}
515				++dest;
516			}
517		} else {
518			// Uncompressed
519			blockheader++;
520			while (blockheader-- && remaining) {
521				--remaining;
522				int byte = cpu->memory.load8(cpu, source, 0);
523				++source;
524				if (width == 2) {
525					if (dest & 1) {
526						halfword |= byte << 8;
527						cpu->memory.store16(cpu, dest ^ 1, halfword, 0);
528					} else {
529						halfword = byte;
530					}
531				} else {
532					cpu->memory.store8(cpu, dest, byte, 0);
533				}
534				++dest;
535			}
536		}
537	}
538	if (width == 2) {
539		if (dest & 1) {
540			--padding;
541			++dest;
542		}
543		for (; padding > 0; padding -= 2, dest += 2) {
544			cpu->memory.store16(cpu, dest, 0, 0);
545		}
546	} else {
547		while (padding--) {
548			cpu->memory.store8(cpu, dest, 0, 0);
549			++dest;
550		}
551	}
552	cpu->gprs[0] = source;
553	cpu->gprs[1] = dest;
554}
555
556static void _unFilter(struct GBA* gba, int inwidth, int outwidth) {
557	struct ARMCore* cpu = gba->cpu;
558	uint32_t source = cpu->gprs[0] & 0xFFFFFFFC;
559	uint32_t dest = cpu->gprs[1];
560	uint32_t header = cpu->memory.load32(cpu, source, 0);
561	int remaining = header >> 8;
562	// We assume the signature nybble (0x8) is correct
563	uint16_t halfword = 0;
564	uint16_t old = 0;
565	source += 4;
566	while (remaining > 0) {
567		uint16_t new;
568		if (inwidth == 1) {
569			new = cpu->memory.load8(cpu, source, 0);
570		} else {
571			new = cpu->memory.load16(cpu, source, 0);
572		}
573		new += old;
574		if (outwidth > inwidth) {
575			halfword >>= 8;
576			halfword |= (new << 8);
577			if (source & 1) {
578				cpu->memory.store16(cpu, dest, halfword, 0);
579				dest += outwidth;
580				remaining -= outwidth;
581			}
582		} else if (outwidth == 1) {
583			cpu->memory.store8(cpu, dest, new, 0);
584			dest += outwidth;
585			remaining -= outwidth;
586		} else {
587			cpu->memory.store16(cpu, dest, new, 0);
588			dest += outwidth;
589			remaining -= outwidth;
590		}
591		old = new;
592		source += inwidth;
593	}
594	cpu->gprs[0] = source;
595	cpu->gprs[1] = dest;
596}