all repos — mgba @ d8778cf516ba8095f346de7a4977f96a654df432

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
  7#include <math.h>
  8#include <stdlib.h>
  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 0xE:
241		_BgAffineSet(gba);
242		break;
243	case 0xF:
244		_ObjAffineSet(gba);
245		break;
246	case 0x11:
247	case 0x12:
248		switch (gba->cpu.gprs[1] >> BASE_OFFSET) {
249			case REGION_WORKING_RAM:
250				_unLz77(&gba->memory, gba->cpu.gprs[0], &((uint8_t*) gba->memory.wram)[(gba->cpu.gprs[1] & (SIZE_WORKING_RAM - 1))]);
251				break;
252			case REGION_WORKING_IRAM:
253				_unLz77(&gba->memory, gba->cpu.gprs[0], &((uint8_t*) gba->memory.iwram)[(gba->cpu.gprs[1] & (SIZE_WORKING_IRAM - 1))]);
254				break;
255			case REGION_VRAM:
256				_unLz77(&gba->memory, gba->cpu.gprs[0], &((uint8_t*) gba->video.renderer->vram)[(gba->cpu.gprs[1] & 0x0001FFFF)]);
257				break;
258			default:
259				GBALog(gba, GBA_LOG_WARN, "Bad LZ77 destination");
260				break;
261		}
262		break;
263	case 0x13:
264		switch (gba->cpu.gprs[1] >> BASE_OFFSET) {
265			case REGION_WORKING_RAM:
266				_unHuffman(&gba->memory, gba->cpu.gprs[0], &((uint32_t*) gba->memory.wram)[(gba->cpu.gprs[1] & (SIZE_WORKING_RAM - 3))]);
267				break;
268			case REGION_WORKING_IRAM:
269				_unHuffman(&gba->memory, gba->cpu.gprs[0], &((uint32_t*) gba->memory.iwram)[(gba->cpu.gprs[1] & (SIZE_WORKING_IRAM - 3))]);
270				break;
271			case REGION_VRAM:
272				_unHuffman(&gba->memory, gba->cpu.gprs[0], &((uint32_t*) gba->video.renderer->vram)[(gba->cpu.gprs[1] & 0x0001FFFC)]);
273				break;
274			default:
275				GBALog(gba, GBA_LOG_WARN, "Bad Huffman destination");
276				break;
277		}
278		break;
279	case 0x14:
280	case 0x15:
281		switch (gba->cpu.gprs[1] >> BASE_OFFSET) {
282			case REGION_WORKING_RAM:
283				_unRl(&gba->memory, gba->cpu.gprs[0], &((uint8_t*) gba->memory.wram)[(gba->cpu.gprs[1] & (SIZE_WORKING_RAM - 1))]);
284				break;
285			case REGION_WORKING_IRAM:
286				_unRl(&gba->memory, gba->cpu.gprs[0], &((uint8_t*) gba->memory.iwram)[(gba->cpu.gprs[1] & (SIZE_WORKING_IRAM - 1))]);
287				break;
288			case REGION_VRAM:
289				_unRl(&gba->memory, gba->cpu.gprs[0], &((uint8_t*) gba->video.renderer->vram)[(gba->cpu.gprs[1] & 0x0001FFFF)]);
290				break;
291			default:
292				GBALog(gba, GBA_LOG_WARN, "Bad RL destination");
293				break;
294		}
295		break;
296	case 0x1F:
297		_MidiKey2Freq(gba);
298		break;
299	default:
300		GBALog(gba, GBA_LOG_STUB, "Stub software interrupt: %02x", immediate);
301	}
302}
303
304void GBASwi32(struct ARMBoard* board, int immediate) {
305	GBASwi16(board, immediate >> 16);
306}
307
308static void _unLz77(struct GBAMemory* memory, uint32_t source, uint8_t* dest) {
309	int remaining = (memory->d.load32(&memory->d, source, 0) & 0xFFFFFF00) >> 8;
310	// We assume the signature byte (0x10) is correct
311	int blockheader;
312	uint32_t sPointer = source + 4;
313	uint8_t* dPointer = dest;
314	int blocksRemaining = 0;
315	int block;
316	uint8_t* disp;
317	int bytes;
318	while (remaining > 0) {
319		if (blocksRemaining) {
320			if (blockheader & 0x80) {
321				// Compressed
322				block = memory->d.loadU8(&memory->d, sPointer, 0) | (memory->d.loadU8(&memory->d, sPointer + 1, 0) << 8);
323				sPointer += 2;
324				disp = dPointer - (((block & 0x000F) << 8) | ((block & 0xFF00) >> 8)) - 1;
325				bytes = ((block & 0x00F0) >> 4) + 3;
326				while (bytes-- && remaining) {
327					--remaining;
328					*dPointer = *disp;
329					++disp;
330					++dPointer;
331				}
332			} else {
333				// Uncompressed
334				*dPointer = memory->d.loadU8(&memory->d, sPointer++, 0);
335				++dPointer;
336				--remaining;
337			}
338			blockheader <<= 1;
339			--blocksRemaining;
340		} else {
341			blockheader = memory->d.loadU8(&memory->d, sPointer++, 0);
342			blocksRemaining = 8;
343		}
344	}
345}
346
347static void _unHuffman(struct GBAMemory* memory, uint32_t source, uint32_t* dest) {
348	source = source & 0xFFFFFFFC;
349	uint32_t header = memory->d.load32(&memory->d, source, 0);
350	int remaining = header >> 8;
351	int bits = header & 0xF;
352	if (32 % bits) {
353		GBALog(memory->p, GBA_LOG_STUB, "Unimplemented unaligned Huffman");
354		return;
355	}
356	int padding = (4 - remaining) & 0x3;
357	remaining &= 0xFFFFFFFC;
358	// We assume the signature byte (0x20) is correct
359	//var tree = [];
360	int treesize = (memory->d.load8(&memory->d, source + 4, 0) << 1) + 1;
361	int block = 0;
362	uint32_t treeBase = source + 5;
363	uint32_t sPointer = source + 5 + treesize;
364	uint32_t* dPointer = dest;
365	uint32_t nPointer = treeBase;
366	union HuffmanNode {
367		struct {
368			unsigned offset : 6;
369			unsigned rTerm : 1;
370			unsigned lTerm : 1;
371		};
372		uint8_t packed;
373	} node;
374	int bitsRemaining;
375	int readBits;
376	int bitsSeen = 0;
377	node.packed = memory->d.load8(&memory->d, nPointer, 0);
378	while (remaining > 0) {
379		uint32_t bitstream = memory->d.load32(&memory->d, sPointer, 0);
380		sPointer += 4;
381		for (bitsRemaining = 32; bitsRemaining > 0; --bitsRemaining, bitstream <<= 1) {
382			uint32_t next = (nPointer & ~1) + node.offset * 2 + 2;
383			if (bitstream & 0x80000000) {
384				// Go right
385				if (node.rTerm) {
386					readBits = memory->d.load8(&memory->d, next + 1, 0);
387				} else {
388					nPointer = next + 1;
389					node.packed = memory->d.load8(&memory->d, nPointer, 0);
390					continue;
391				}
392			} else {
393				// Go left
394				if (node.lTerm) {
395					readBits = memory->d.load8(&memory->d, next, 0);
396				} else {
397					nPointer = next;
398					node.packed = memory->d.load8(&memory->d, nPointer, 0);
399					continue;
400				}
401			}
402
403			block |= (readBits & ((1 << bits) - 1)) << bitsSeen;
404			bitsSeen += bits;
405			nPointer = treeBase;
406			node.packed = memory->d.load8(&memory->d, nPointer, 0);
407			if (bitsSeen == 32) {
408				bitsSeen = 0;
409				*dPointer = block;
410				++dPointer;
411				remaining -= 4;
412				block = 0;
413			}
414		}
415
416	}
417	if (padding) {
418		*dPointer = block;
419	}
420}
421
422static void _unRl(struct GBAMemory* memory, uint32_t source, uint8_t* dest) {
423	source = source & 0xFFFFFFFC;
424	int remaining = (memory->d.load32(&memory->d, source, 0) & 0xFFFFFF00) >> 8;
425	int padding = (4 - remaining) & 0x3;
426	// We assume the signature byte (0x30) is correct
427	int blockheader;
428	int block;
429	uint32_t sPointer = source + 4;
430	uint8_t* dPointer = dest;
431	while (remaining > 0) {
432		blockheader = memory->d.loadU8(&memory->d, sPointer++, 0);
433		if (blockheader & 0x80) {
434			// Compressed
435			blockheader &= 0x7F;
436			blockheader += 3;
437			block = memory->d.loadU8(&memory->d, sPointer++, 0);
438			while (blockheader-- && remaining) {
439				--remaining;
440				*dPointer = block;
441				++dPointer;
442			}
443		} else {
444			// Uncompressed
445			blockheader++;
446			while (blockheader-- && remaining) {
447				--remaining;
448				*dPointer = memory->d.loadU8(&memory->d, sPointer++, 0);
449				++dPointer;
450			}
451		}
452	}
453	while (padding--) {
454		*dPointer = 0;
455		++dPointer;
456	}
457}