all repos — mgba @ 775e417cc6781ceb30520c85c968d198efb87429

mGBA Game Boy Advance Emulator

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