all repos — mgba @ 8ae1a3a3a3d385d416def2e63b356310d63fcf3f

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 GBA* gba, uint32_t source, uint8_t* dest);
 11static void _unHuffman(struct GBA* gba, uint32_t source, uint32_t* dest);
 12static void _unRl(struct GBA* gba, uint32_t source, uint8_t* dest);
 13
 14static void _RegisterRamReset(struct GBA* gba) {
 15	uint32_t registers = gba->cpu->gprs[0];
 16	UNUSED(registers);
 17	GBALog(gba, GBA_LOG_STUB, "RegisterRamReset unimplemented");
 18}
 19
 20static void _BgAffineSet(struct GBA* gba) {
 21	struct ARMCore* cpu = gba->cpu;
 22	int i = cpu->gprs[2];
 23	float ox, oy;
 24	float cx, cy;
 25	float sx, sy;
 26	float theta;
 27	int offset = cpu->gprs[0];
 28	int destination = cpu->gprs[1];
 29	float a, b, c, d;
 30	float rx, ry;
 31	while (i--) {
 32		// [ sx   0  0 ]   [ cos(theta)  -sin(theta)  0 ]   [ 1  0  cx - ox ]   [ A B rx ]
 33		// [  0  sy  0 ] * [ sin(theta)   cos(theta)  0 ] * [ 0  1  cy - oy ] = [ C D ry ]
 34		// [  0   0  1 ]   [     0            0       1 ]   [ 0  0     1    ]   [ 0 0  1 ]
 35		ox = cpu->memory.load32(cpu, offset, 0) / 256.f;
 36		oy = cpu->memory.load32(cpu, offset + 4, 0) / 256.f;
 37		cx = cpu->memory.load16(cpu, offset + 8, 0);
 38		cy = cpu->memory.load16(cpu, offset + 10, 0);
 39		sx = cpu->memory.load16(cpu, offset + 12, 0) / 256.f;
 40		sy = cpu->memory.load16(cpu, offset + 14, 0) / 256.f;
 41		theta = (cpu->memory.loadU16(cpu, offset + 16, 0) >> 8) / 128.f * M_PI;
 42		offset += 20;
 43		// Rotation
 44		a = d = cosf(theta);
 45		b = c = sinf(theta);
 46		// Scale
 47		a *= sx;
 48		b *= -sx;
 49		c *= sy;
 50		d *= sy;
 51		// Translate
 52		rx = ox - (a * cx + b * cy);
 53		ry = oy - (c * cx + d * cy);
 54		cpu->memory.store16(cpu, destination, a * 256, 0);
 55		cpu->memory.store16(cpu, destination + 2, b * 256, 0);
 56		cpu->memory.store16(cpu, destination + 4, c * 256, 0);
 57		cpu->memory.store16(cpu, destination + 6, d * 256, 0);
 58		cpu->memory.store32(cpu, destination + 8, rx * 256, 0);
 59		cpu->memory.store32(cpu, destination + 12, ry * 256, 0);
 60		destination += 16;
 61	}
 62}
 63
 64static void _ObjAffineSet(struct GBA* gba) {
 65	struct ARMCore* cpu = gba->cpu;
 66	int i = cpu->gprs[2];
 67	float sx, sy;
 68	float theta;
 69	int offset = cpu->gprs[0];
 70	int destination = cpu->gprs[1];
 71	int diff = cpu->gprs[3];
 72	float a, b, c, d;
 73	while (i--) {
 74		// [ sx   0 ]   [ cos(theta)  -sin(theta) ]   [ A B ]
 75		// [  0  sy ] * [ sin(theta)   cos(theta) ] = [ C D ]
 76		sx = cpu->memory.load16(cpu, offset, 0) / 256.f;
 77		sy = cpu->memory.load16(cpu, offset + 2, 0) / 256.f;
 78		theta = (cpu->memory.loadU16(cpu, offset + 4, 0) >> 8) / 128.f * M_PI;
 79		offset += 8;
 80		// Rotation
 81		a = d = cosf(theta);
 82		b = c = sinf(theta);
 83		// Scale
 84		a *= sx;
 85		b *= -sx;
 86		c *= sy;
 87		d *= sy;
 88		cpu->memory.store16(cpu, destination, a * 256, 0);
 89		cpu->memory.store16(cpu, destination + diff, b * 256, 0);
 90		cpu->memory.store16(cpu, destination + diff * 2, c * 256, 0);
 91		cpu->memory.store16(cpu, destination + diff * 3, d * 256, 0);
 92		destination += diff * 4;
 93	}
 94}
 95
 96static void _MidiKey2Freq(struct GBA* gba) {
 97	struct ARMCore* cpu = gba->cpu;
 98	uint32_t key = cpu->memory.load32(cpu, cpu->gprs[0] + 4, 0);
 99	cpu->gprs[0] = key / powf(2, (180.f - cpu->gprs[1] - cpu->gprs[2] / 256.f) / 12.f);
100}
101
102static void _Div(struct ARMCore* cpu, int32_t num, int32_t denom) {
103	if (denom != 0) {
104		div_t result = div(num, denom);
105		cpu->gprs[0] = result.quot;
106		cpu->gprs[1] = result.rem;
107		cpu->gprs[3] = abs(result.quot);
108	} else {
109		// If abs(num) > 1, this should hang, but that would be painful to
110		// emulate in HLE, and no game will get into a state where it hangs...
111		cpu->gprs[0] = (num < 0) ? -1 : 1;
112		cpu->gprs[1] = num;
113		cpu->gprs[3] = 1;
114	}
115}
116
117void GBASwi16(struct ARMCore* cpu, int immediate) {
118	struct GBA* gba = (struct GBA*) cpu->master;
119	GBALog(gba, GBA_LOG_DEBUG, "SWI: %02x", immediate);
120
121	if (gba->memory.fullBios) {
122		ARMRaiseSWI(cpu);
123		return;
124	}
125	switch (immediate) {
126	case 0x1:
127		_RegisterRamReset(gba);
128		break;
129	case 0x2:
130		GBAHalt(gba);
131		break;
132	case 0x05:
133		// VBlankIntrWait
134		// Fall through:
135	case 0x04:
136		// IntrWait
137		ARMRaiseSWI(cpu);
138		break;
139	case 0x6:
140		_Div(cpu, cpu->gprs[0], cpu->gprs[1]);
141		break;
142	case 0x7:
143		_Div(cpu, cpu->gprs[1], cpu->gprs[0]);
144		break;
145	case 0x8:
146		cpu->gprs[0] = sqrt(cpu->gprs[0]);
147		break;
148	case 0xA:
149		cpu->gprs[0] = atan2f(cpu->gprs[1] / 16384.f, cpu->gprs[0] / 16384.f) / (2 * M_PI) * 0x10000;
150		break;
151	case 0xB:
152	case 0xC:
153		ARMRaiseSWI(cpu);
154		break;
155	case 0xD:
156		cpu->gprs[0] = GBAChecksum(gba->memory.bios, SIZE_BIOS);
157	case 0xE:
158		_BgAffineSet(gba);
159		break;
160	case 0xF:
161		_ObjAffineSet(gba);
162		break;
163	case 0x11:
164	case 0x12:
165		if (cpu->gprs[0] < BASE_WORKING_RAM) {
166			GBALog(gba, GBA_LOG_GAME_ERROR, "Bad LZ77 source");
167			break;
168		}
169		switch (cpu->gprs[1] >> BASE_OFFSET) {
170			case REGION_WORKING_RAM:
171				_unLz77(gba, cpu->gprs[0], &((uint8_t*) gba->memory.wram)[(cpu->gprs[1] & (SIZE_WORKING_RAM - 1))]);
172				break;
173			case REGION_WORKING_IRAM:
174				_unLz77(gba, cpu->gprs[0], &((uint8_t*) gba->memory.iwram)[(cpu->gprs[1] & (SIZE_WORKING_IRAM - 1))]);
175				break;
176			case REGION_VRAM:
177				_unLz77(gba, cpu->gprs[0], &((uint8_t*) gba->video.renderer->vram)[(cpu->gprs[1] & 0x0001FFFF)]);
178				break;
179			default:
180				GBALog(gba, GBA_LOG_GAME_ERROR, "Bad LZ77 destination");
181				break;
182		}
183		break;
184	case 0x13:
185		if (cpu->gprs[0] < BASE_WORKING_RAM) {
186			GBALog(gba, GBA_LOG_GAME_ERROR, "Bad Huffman source");
187			break;
188		}
189		switch (cpu->gprs[1] >> BASE_OFFSET) {
190			case REGION_WORKING_RAM:
191				_unHuffman(gba, cpu->gprs[0], &((uint32_t*) gba->memory.wram)[(cpu->gprs[1] & (SIZE_WORKING_RAM - 3)) >> 2]);
192				break;
193			case REGION_WORKING_IRAM:
194				_unHuffman(gba, cpu->gprs[0], &((uint32_t*) gba->memory.iwram)[(cpu->gprs[1] & (SIZE_WORKING_IRAM - 3)) >> 2]);
195				break;
196			case REGION_VRAM:
197				_unHuffman(gba, cpu->gprs[0], &((uint32_t*) gba->video.renderer->vram)[(cpu->gprs[1] & 0x0001FFFC) >> 2]);
198				break;
199			default:
200				GBALog(gba, GBA_LOG_GAME_ERROR, "Bad Huffman destination");
201				break;
202		}
203		break;
204	case 0x14:
205	case 0x15:
206		if (cpu->gprs[0] < BASE_WORKING_RAM) {
207			GBALog(gba, GBA_LOG_GAME_ERROR, "Bad RL source");
208			break;
209		}
210		switch (cpu->gprs[1] >> BASE_OFFSET) {
211			case REGION_WORKING_RAM:
212				_unRl(gba, cpu->gprs[0], &((uint8_t*) gba->memory.wram)[(cpu->gprs[1] & (SIZE_WORKING_RAM - 1))]);
213				break;
214			case REGION_WORKING_IRAM:
215				_unRl(gba, cpu->gprs[0], &((uint8_t*) gba->memory.iwram)[(cpu->gprs[1] & (SIZE_WORKING_IRAM - 1))]);
216				break;
217			case REGION_VRAM:
218				_unRl(gba, cpu->gprs[0], &((uint8_t*) gba->video.renderer->vram)[(cpu->gprs[1] & 0x0001FFFF)]);
219				break;
220			default:
221				GBALog(gba, GBA_LOG_GAME_ERROR, "Bad RL destination");
222				break;
223		}
224		break;
225	case 0x1F:
226		_MidiKey2Freq(gba);
227		break;
228	default:
229		GBALog(gba, GBA_LOG_STUB, "Stub software interrupt: %02x", immediate);
230	}
231}
232
233void GBASwi32(struct ARMCore* cpu, int immediate) {
234	GBASwi16(cpu, immediate >> 16);
235}
236
237uint32_t GBAChecksum(uint32_t* memory, size_t size) {
238	size_t i;
239	uint32_t sum = 0;
240	for (i = 0; i < size; i += 4) {
241		sum += memory[i >> 2];
242	}
243	return sum;
244}
245
246static void _unLz77(struct GBA* gba, uint32_t source, uint8_t* dest) {
247	struct ARMCore* cpu = gba->cpu;
248	int remaining = (cpu->memory.load32(cpu, source, 0) & 0xFFFFFF00) >> 8;
249	// We assume the signature byte (0x10) is correct
250	int blockheader;
251	uint32_t sPointer = source + 4;
252	uint8_t* dPointer = dest;
253	int blocksRemaining = 0;
254	int block;
255	uint8_t* disp;
256	int bytes;
257	while (remaining > 0) {
258		if (blocksRemaining) {
259			if (blockheader & 0x80) {
260				// Compressed
261				block = cpu->memory.loadU8(cpu, sPointer, 0) | (cpu->memory.loadU8(cpu, sPointer + 1, 0) << 8);
262				sPointer += 2;
263				disp = dPointer - (((block & 0x000F) << 8) | ((block & 0xFF00) >> 8)) - 1;
264				bytes = ((block & 0x00F0) >> 4) + 3;
265				while (bytes-- && remaining) {
266					--remaining;
267					*dPointer = *disp;
268					++disp;
269					++dPointer;
270				}
271			} else {
272				// Uncompressed
273				*dPointer = cpu->memory.loadU8(cpu, sPointer++, 0);
274				++dPointer;
275				--remaining;
276			}
277			blockheader <<= 1;
278			--blocksRemaining;
279		} else {
280			blockheader = cpu->memory.loadU8(cpu, sPointer++, 0);
281			blocksRemaining = 8;
282		}
283	}
284}
285
286DECL_BITFIELD(HuffmanNode, uint8_t);
287DECL_BITS(HuffmanNode, Offset, 0, 6);
288DECL_BIT(HuffmanNode, RTerm, 6);
289DECL_BIT(HuffmanNode, LTerm, 7);
290
291static void _unHuffman(struct GBA* gba, uint32_t source, uint32_t* dest) {
292	struct ARMCore* cpu = gba->cpu;
293	source = source & 0xFFFFFFFC;
294	uint32_t header = cpu->memory.load32(cpu, source, 0);
295	int remaining = header >> 8;
296	int bits = header & 0xF;
297	if (32 % bits) {
298		GBALog(gba, GBA_LOG_STUB, "Unimplemented unaligned Huffman");
299		return;
300	}
301	int padding = (4 - remaining) & 0x3;
302	remaining &= 0xFFFFFFFC;
303	// We assume the signature byte (0x20) is correct
304	//var tree = [];
305	int treesize = (cpu->memory.loadU8(cpu, source + 4, 0) << 1) + 1;
306	int block = 0;
307	uint32_t treeBase = source + 5;
308	uint32_t sPointer = source + 5 + treesize;
309	uint32_t* dPointer = dest;
310	uint32_t nPointer = treeBase;
311	HuffmanNode node;
312	int bitsRemaining;
313	int readBits;
314	int bitsSeen = 0;
315	node = cpu->memory.load8(cpu, nPointer, 0);
316	while (remaining > 0) {
317		uint32_t bitstream = cpu->memory.load32(cpu, sPointer, 0);
318		sPointer += 4;
319		for (bitsRemaining = 32; bitsRemaining > 0; --bitsRemaining, bitstream <<= 1) {
320			uint32_t next = (nPointer & ~1) + HuffmanNodeGetOffset(node) * 2 + 2;
321			if (bitstream & 0x80000000) {
322				// Go right
323				if (HuffmanNodeIsRTerm(node)) {
324					readBits = cpu->memory.load8(cpu, next + 1, 0);
325				} else {
326					nPointer = next + 1;
327					node = cpu->memory.load8(cpu, nPointer, 0);
328					continue;
329				}
330			} else {
331				// Go left
332				if (HuffmanNodeIsLTerm(node)) {
333					readBits = cpu->memory.load8(cpu, next, 0);
334				} else {
335					nPointer = next;
336					node = cpu->memory.load8(cpu, nPointer, 0);
337					continue;
338				}
339			}
340
341			block |= (readBits & ((1 << bits) - 1)) << bitsSeen;
342			bitsSeen += bits;
343			nPointer = treeBase;
344			node = cpu->memory.load8(cpu, nPointer, 0);
345			if (bitsSeen == 32) {
346				bitsSeen = 0;
347				*dPointer = block;
348				++dPointer;
349				remaining -= 4;
350				block = 0;
351			}
352		}
353
354	}
355	if (padding) {
356		*dPointer = block;
357	}
358}
359
360static void _unRl(struct GBA* gba, uint32_t source, uint8_t* dest) {
361	struct ARMCore* cpu = gba->cpu;
362	source = source & 0xFFFFFFFC;
363	int remaining = (cpu->memory.load32(cpu, source, 0) & 0xFFFFFF00) >> 8;
364	int padding = (4 - remaining) & 0x3;
365	// We assume the signature byte (0x30) is correct
366	int blockheader;
367	int block;
368	uint32_t sPointer = source + 4;
369	uint8_t* dPointer = dest;
370	while (remaining > 0) {
371		blockheader = cpu->memory.loadU8(cpu, sPointer++, 0);
372		if (blockheader & 0x80) {
373			// Compressed
374			blockheader &= 0x7F;
375			blockheader += 3;
376			block = cpu->memory.loadU8(cpu, sPointer++, 0);
377			while (blockheader-- && remaining) {
378				--remaining;
379				*dPointer = block;
380				++dPointer;
381			}
382		} else {
383			// Uncompressed
384			blockheader++;
385			while (blockheader-- && remaining) {
386				--remaining;
387				*dPointer = cpu->memory.loadU8(cpu, sPointer++, 0);
388				++dPointer;
389			}
390		}
391	}
392	while (padding--) {
393		*dPointer = 0;
394		++dPointer;
395	}
396}