all repos — mgba @ aab6231b681b25db70035d45aec6e7403d5d5e62

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 <mgba/internal/gba/bios.h>
  7
  8#include <mgba/internal/arm/isa-inlines.h>
  9#include <mgba/internal/arm/macros.h>
 10#include <mgba/internal/gba/gba.h>
 11#include <mgba/internal/gba/io.h>
 12#include <mgba/internal/gba/memory.h>
 13#include <mgba-util/math.h>
 14
 15const uint32_t GBA_BIOS_CHECKSUM = 0xBAAE187F;
 16const uint32_t GBA_DS_BIOS_CHECKSUM = 0xBAAE1880;
 17
 18mLOG_DEFINE_CATEGORY(GBA_BIOS, "GBA BIOS", "gba.bios");
 19
 20static void _unLz77(struct GBA* gba, int width);
 21static void _unHuffman(struct GBA* gba);
 22static void _unRl(struct GBA* gba, int width);
 23static void _unFilter(struct GBA* gba, int inwidth, int outwidth);
 24static void _unBitPack(struct GBA* gba);
 25
 26static int _mulWait(int32_t r) {
 27	if ((r & 0xFFFFFF00) == 0xFFFFFF00 || !(r & 0xFFFFFF00)) {
 28		return 1;
 29	} else if ((r & 0xFFFF0000) == 0xFFFF0000 || !(r & 0xFFFF0000)) {
 30		return 2;
 31	} else if ((r & 0xFF000000) == 0xFF000000 || !(r & 0xFF000000)) {
 32		return 3;
 33	} else {
 34		return 4;
 35	}
 36}
 37
 38static void _SoftReset(struct GBA* gba) {
 39	struct ARMCore* cpu = gba->cpu;
 40	ARMSetPrivilegeMode(cpu, MODE_IRQ);
 41	cpu->spsr.packed = 0;
 42	cpu->gprs[ARM_LR] = 0;
 43	cpu->gprs[ARM_SP] = SP_BASE_IRQ;
 44	ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
 45	cpu->spsr.packed = 0;
 46	cpu->gprs[ARM_LR] = 0;
 47	cpu->gprs[ARM_SP] = SP_BASE_SUPERVISOR;
 48	ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
 49	cpu->gprs[ARM_LR] = 0;
 50	cpu->gprs[ARM_SP] = SP_BASE_SYSTEM;
 51	int8_t flag = ((int8_t*) gba->memory.iwram)[0x7FFA];
 52	memset(((int8_t*) gba->memory.iwram) + SIZE_WORKING_IRAM - 0x200, 0, 0x200);
 53	if (flag) {
 54		cpu->gprs[ARM_PC] = BASE_WORKING_RAM;
 55	} else {
 56		cpu->gprs[ARM_PC] = BASE_CART0;
 57	}
 58	_ARMSetMode(cpu, MODE_ARM);
 59	ARMWritePC(cpu);
 60}
 61
 62static void _RegisterRamReset(struct GBA* gba) {
 63	uint32_t registers = gba->cpu->gprs[0];
 64	struct ARMCore* cpu = gba->cpu;
 65	cpu->memory.store16(cpu, BASE_IO | REG_DISPCNT, 0x0080, 0);
 66	if (registers & 0x01) {
 67		memset(gba->memory.wram, 0, SIZE_WORKING_RAM);
 68	}
 69	if (registers & 0x02) {
 70		memset(gba->memory.iwram, 0, SIZE_WORKING_IRAM - 0x200);
 71	}
 72	if (registers & 0x04) {
 73		memset(gba->video.palette, 0, SIZE_PALETTE_RAM);
 74	}
 75	if (registers & 0x08) {
 76		memset(gba->video.vram, 0, SIZE_VRAM);
 77	}
 78	if (registers & 0x10) {
 79		memset(gba->video.oam.raw, 0, SIZE_OAM);
 80	}
 81	if (registers & 0x20) {
 82		cpu->memory.store16(cpu, BASE_IO | REG_SIOCNT, 0x0000, 0);
 83		cpu->memory.store16(cpu, BASE_IO | REG_RCNT, RCNT_INITIAL, 0);
 84		cpu->memory.store16(cpu, BASE_IO | REG_SIOMLT_SEND, 0, 0);
 85		cpu->memory.store16(cpu, BASE_IO | REG_JOYCNT, 0, 0);
 86		cpu->memory.store32(cpu, BASE_IO | REG_JOY_RECV_LO, 0, 0);
 87		cpu->memory.store32(cpu, BASE_IO | REG_JOY_TRANS_LO, 0, 0);
 88	}
 89	if (registers & 0x40) {
 90		cpu->memory.store16(cpu, BASE_IO | REG_SOUND1CNT_LO, 0, 0);
 91		cpu->memory.store16(cpu, BASE_IO | REG_SOUND1CNT_HI, 0, 0);
 92		cpu->memory.store16(cpu, BASE_IO | REG_SOUND1CNT_X, 0, 0);
 93		cpu->memory.store16(cpu, BASE_IO | REG_SOUND2CNT_LO, 0, 0);
 94		cpu->memory.store16(cpu, BASE_IO | REG_SOUND2CNT_HI, 0, 0);
 95		cpu->memory.store16(cpu, BASE_IO | REG_SOUND3CNT_LO, 0, 0);
 96		cpu->memory.store16(cpu, BASE_IO | REG_SOUND3CNT_HI, 0, 0);
 97		cpu->memory.store16(cpu, BASE_IO | REG_SOUND3CNT_X, 0, 0);
 98		cpu->memory.store16(cpu, BASE_IO | REG_SOUND4CNT_LO, 0, 0);
 99		cpu->memory.store16(cpu, BASE_IO | REG_SOUND4CNT_HI, 0, 0);
100		cpu->memory.store16(cpu, BASE_IO | REG_SOUNDCNT_LO, 0, 0);
101		cpu->memory.store16(cpu, BASE_IO | REG_SOUNDCNT_HI, 0, 0);
102		cpu->memory.store16(cpu, BASE_IO | REG_SOUNDCNT_X, 0, 0);
103		cpu->memory.store16(cpu, BASE_IO | REG_SOUNDBIAS, 0x200, 0);
104		memset(gba->audio.psg.ch3.wavedata32, 0, sizeof(gba->audio.psg.ch3.wavedata32));
105	}
106	if (registers & 0x80) {
107		cpu->memory.store16(cpu, BASE_IO | 0x04, 0, 0);
108		cpu->memory.store16(cpu, BASE_IO | 0x06, 0, 0);
109		cpu->memory.store16(cpu, BASE_IO | 0x08, 0, 0);
110		cpu->memory.store16(cpu, BASE_IO | 0x0A, 0, 0);
111		cpu->memory.store16(cpu, BASE_IO | 0x0C, 0, 0);
112		cpu->memory.store16(cpu, BASE_IO | 0x0E, 0, 0);
113		cpu->memory.store16(cpu, BASE_IO | 0x10, 0, 0);
114		cpu->memory.store16(cpu, BASE_IO | 0x12, 0, 0);
115		cpu->memory.store16(cpu, BASE_IO | 0x14, 0, 0);
116		cpu->memory.store16(cpu, BASE_IO | 0x16, 0, 0);
117		cpu->memory.store16(cpu, BASE_IO | 0x18, 0, 0);
118		cpu->memory.store16(cpu, BASE_IO | 0x1A, 0, 0);
119		cpu->memory.store16(cpu, BASE_IO | 0x1C, 0, 0);
120		cpu->memory.store16(cpu, BASE_IO | 0x1E, 0, 0);
121		cpu->memory.store16(cpu, BASE_IO | REG_BG2PA, 0x100, 0);
122		cpu->memory.store16(cpu, BASE_IO | REG_BG2PB, 0, 0);
123		cpu->memory.store16(cpu, BASE_IO | REG_BG2PC, 0, 0);
124		cpu->memory.store16(cpu, BASE_IO | REG_BG2PD, 0x100, 0);
125		cpu->memory.store32(cpu, BASE_IO | 0x28, 0, 0);
126		cpu->memory.store32(cpu, BASE_IO | 0x2C, 0, 0);
127		cpu->memory.store16(cpu, BASE_IO | REG_BG3PA, 0x100, 0);
128		cpu->memory.store16(cpu, BASE_IO | REG_BG3PB, 0, 0);
129		cpu->memory.store16(cpu, BASE_IO | REG_BG3PC, 0, 0);
130		cpu->memory.store16(cpu, BASE_IO | REG_BG3PD, 0x100, 0);
131		cpu->memory.store32(cpu, BASE_IO | 0x38, 0, 0);
132		cpu->memory.store32(cpu, BASE_IO | 0x3C, 0, 0);
133		cpu->memory.store16(cpu, BASE_IO | 0x40, 0, 0);
134		cpu->memory.store16(cpu, BASE_IO | 0x42, 0, 0);
135		cpu->memory.store16(cpu, BASE_IO | 0x44, 0, 0);
136		cpu->memory.store16(cpu, BASE_IO | 0x46, 0, 0);
137		cpu->memory.store16(cpu, BASE_IO | 0x48, 0, 0);
138		cpu->memory.store16(cpu, BASE_IO | 0x4A, 0, 0);
139		cpu->memory.store16(cpu, BASE_IO | 0x4C, 0, 0);
140		cpu->memory.store16(cpu, BASE_IO | 0x50, 0, 0);
141		cpu->memory.store16(cpu, BASE_IO | 0x52, 0, 0);
142		cpu->memory.store16(cpu, BASE_IO | 0x54, 0, 0);
143		cpu->memory.store16(cpu, BASE_IO | 0xB0, 0, 0);
144		cpu->memory.store16(cpu, BASE_IO | 0xB2, 0, 0);
145		cpu->memory.store16(cpu, BASE_IO | 0xB4, 0, 0);
146		cpu->memory.store16(cpu, BASE_IO | 0xB6, 0, 0);
147		cpu->memory.store16(cpu, BASE_IO | 0xB8, 0, 0);
148		cpu->memory.store16(cpu, BASE_IO | 0xBA, 0, 0);
149		cpu->memory.store16(cpu, BASE_IO | 0xBC, 0, 0);
150		cpu->memory.store16(cpu, BASE_IO | 0xBE, 0, 0);
151		cpu->memory.store16(cpu, BASE_IO | 0xC0, 0, 0);
152		cpu->memory.store16(cpu, BASE_IO | 0xC2, 0, 0);
153		cpu->memory.store16(cpu, BASE_IO | 0xC4, 0, 0);
154		cpu->memory.store16(cpu, BASE_IO | 0xC6, 0, 0);
155		cpu->memory.store16(cpu, BASE_IO | 0xC8, 0, 0);
156		cpu->memory.store16(cpu, BASE_IO | 0xCA, 0, 0);
157		cpu->memory.store16(cpu, BASE_IO | 0xCC, 0, 0);
158		cpu->memory.store16(cpu, BASE_IO | 0xCE, 0, 0);
159		cpu->memory.store16(cpu, BASE_IO | 0xD0, 0, 0);
160		cpu->memory.store16(cpu, BASE_IO | 0xD2, 0, 0);
161		cpu->memory.store16(cpu, BASE_IO | 0xD4, 0, 0);
162		cpu->memory.store16(cpu, BASE_IO | 0xD6, 0, 0);
163		cpu->memory.store16(cpu, BASE_IO | 0xD8, 0, 0);
164		cpu->memory.store16(cpu, BASE_IO | 0xDA, 0, 0);
165		cpu->memory.store16(cpu, BASE_IO | 0xDC, 0, 0);
166		cpu->memory.store16(cpu, BASE_IO | 0xDE, 0, 0);
167		cpu->memory.store16(cpu, BASE_IO | 0x100, 0, 0);
168		cpu->memory.store16(cpu, BASE_IO | 0x102, 0, 0);
169		cpu->memory.store16(cpu, BASE_IO | 0x104, 0, 0);
170		cpu->memory.store16(cpu, BASE_IO | 0x106, 0, 0);
171		cpu->memory.store16(cpu, BASE_IO | 0x108, 0, 0);
172		cpu->memory.store16(cpu, BASE_IO | 0x10A, 0, 0);
173		cpu->memory.store16(cpu, BASE_IO | 0x10C, 0, 0);
174		cpu->memory.store16(cpu, BASE_IO | 0x10E, 0, 0);
175		cpu->memory.store16(cpu, BASE_IO | 0x200, 0, 0);
176		cpu->memory.store16(cpu, BASE_IO | 0x202, 0xFFFF, 0);
177		cpu->memory.store16(cpu, BASE_IO | 0x204, 0, 0);
178		cpu->memory.store16(cpu, BASE_IO | 0x208, 0, 0);
179	}
180	if (registers & 0x9C) {
181		gba->video.renderer->reset(gba->video.renderer);
182	}
183}
184
185static void _BgAffineSet(struct GBA* gba) {
186	struct ARMCore* cpu = gba->cpu;
187	int i = cpu->gprs[2];
188	float ox, oy;
189	float cx, cy;
190	float sx, sy;
191	float theta;
192	int offset = cpu->gprs[0];
193	int destination = cpu->gprs[1];
194	float a, b, c, d;
195	float rx, ry;
196	while (i--) {
197		// [ sx   0  0 ]   [ cos(theta)  -sin(theta)  0 ]   [ 1  0  cx - ox ]   [ A B rx ]
198		// [  0  sy  0 ] * [ sin(theta)   cos(theta)  0 ] * [ 0  1  cy - oy ] = [ C D ry ]
199		// [  0   0  1 ]   [     0            0       1 ]   [ 0  0     1    ]   [ 0 0  1 ]
200		ox = (int32_t) cpu->memory.load32(cpu, offset, 0) / 256.f;
201		oy = (int32_t) cpu->memory.load32(cpu, offset + 4, 0) / 256.f;
202		cx = (int16_t) cpu->memory.load16(cpu, offset + 8, 0);
203		cy = (int16_t) cpu->memory.load16(cpu, offset + 10, 0);
204		sx = (int16_t) cpu->memory.load16(cpu, offset + 12, 0) / 256.f;
205		sy = (int16_t) cpu->memory.load16(cpu, offset + 14, 0) / 256.f;
206		theta = (cpu->memory.load16(cpu, offset + 16, 0) >> 8) / 128.f * M_PI;
207		offset += 20;
208		// Rotation
209		a = d = cosf(theta);
210		b = c = sinf(theta);
211		// Scale
212		a *= sx;
213		b *= -sx;
214		c *= sy;
215		d *= sy;
216		// Translate
217		rx = ox - (a * cx + b * cy);
218		ry = oy - (c * cx + d * cy);
219		cpu->memory.store16(cpu, destination, a * 256, 0);
220		cpu->memory.store16(cpu, destination + 2, b * 256, 0);
221		cpu->memory.store16(cpu, destination + 4, c * 256, 0);
222		cpu->memory.store16(cpu, destination + 6, d * 256, 0);
223		cpu->memory.store32(cpu, destination + 8, rx * 256, 0);
224		cpu->memory.store32(cpu, destination + 12, ry * 256, 0);
225		destination += 16;
226	}
227}
228
229static void _ObjAffineSet(struct GBA* gba) {
230	struct ARMCore* cpu = gba->cpu;
231	int i = cpu->gprs[2];
232	float sx, sy;
233	float theta;
234	int offset = cpu->gprs[0];
235	int destination = cpu->gprs[1];
236	int diff = cpu->gprs[3];
237	float a, b, c, d;
238	while (i--) {
239		// [ sx   0 ]   [ cos(theta)  -sin(theta) ]   [ A B ]
240		// [  0  sy ] * [ sin(theta)   cos(theta) ] = [ C D ]
241		sx = (int16_t) cpu->memory.load16(cpu, offset, 0) / 256.f;
242		sy = (int16_t) cpu->memory.load16(cpu, offset + 2, 0) / 256.f;
243		theta = (cpu->memory.load16(cpu, offset + 4, 0) >> 8) / 128.f * M_PI;
244		offset += 8;
245		// Rotation
246		a = d = cosf(theta);
247		b = c = sinf(theta);
248		// Scale
249		a *= sx;
250		b *= -sx;
251		c *= sy;
252		d *= sy;
253		cpu->memory.store16(cpu, destination, a * 256, 0);
254		cpu->memory.store16(cpu, destination + diff, b * 256, 0);
255		cpu->memory.store16(cpu, destination + diff * 2, c * 256, 0);
256		cpu->memory.store16(cpu, destination + diff * 3, d * 256, 0);
257		destination += diff * 4;
258	}
259}
260
261static void _MidiKey2Freq(struct GBA* gba) {
262	struct ARMCore* cpu = gba->cpu;
263
264	int oldRegion = gba->memory.activeRegion;
265	gba->memory.activeRegion = REGION_BIOS;
266	uint32_t key = cpu->memory.load32(cpu, cpu->gprs[0] + 4, 0);
267	gba->memory.activeRegion = oldRegion;
268
269	cpu->gprs[0] = key / exp2f((180.f - cpu->gprs[1] - cpu->gprs[2] / 256.f) / 12.f);
270}
271
272static void _Div(struct GBA* gba, int32_t num, int32_t denom) {
273	struct ARMCore* cpu = gba->cpu;
274	if (denom != 0 && (denom != -1 || num != INT32_MIN)) {
275		div_t result = div(num, denom);
276		cpu->gprs[0] = result.quot;
277		cpu->gprs[1] = result.rem;
278		cpu->gprs[3] = abs(result.quot);
279	} else if (denom == 0) {
280		mLOG(GBA_BIOS, GAME_ERROR, "Attempting to divide %i by zero!", num);
281		// If abs(num) > 1, this should hang, but that would be painful to
282		// emulate in HLE, and no game will get into a state where it hangs...
283		cpu->gprs[0] = (num < 0) ? -1 : 1;
284		cpu->gprs[1] = num;
285		cpu->gprs[3] = 1;
286	} else {
287		mLOG(GBA_BIOS, GAME_ERROR, "Attempting to divide INT_MIN by -1!");
288		cpu->gprs[0] = INT32_MIN;
289		cpu->gprs[1] = 0;
290		cpu->gprs[3] = INT32_MIN;
291	}
292	int loops = clz32(denom) - clz32(num);
293	if (loops < 1) {
294		loops = 1;
295	}
296	cpu->cycles += 4 /* prologue */ + 13 * loops + 7 /* epilogue */;
297}
298
299static int16_t _ArcTan(int32_t i, int32_t* r1, int32_t* r3, int32_t* cycles) {
300	int currentCycles = 37;
301	currentCycles += _mulWait(i * i);
302	int32_t a = -((i * i) >> 14);
303	currentCycles += _mulWait(0xA9 * a);
304	int32_t b = ((0xA9 * a) >> 14) + 0x390;
305	currentCycles += _mulWait(b * a);
306	b = ((b * a) >> 14) + 0x91C;
307	currentCycles += _mulWait(b * a);
308	b = ((b * a) >> 14) + 0xFB6;
309	currentCycles += _mulWait(b * a);
310	b = ((b * a) >> 14) + 0x16AA;
311	currentCycles += _mulWait(b * a);
312	b = ((b * a) >> 14) + 0x2081;
313	currentCycles += _mulWait(b * a);
314	b = ((b * a) >> 14) + 0x3651;
315	currentCycles += _mulWait(b * a);
316	b = ((b * a) >> 14) + 0xA2F9;
317	if (r1) {
318		*r1 = a;
319	}
320	if (r3) {
321		*r3 = b;
322	}
323	*cycles += currentCycles;
324	return (i * b) >> 16;
325}
326
327static int16_t _ArcTan2(int32_t x, int32_t y, int32_t* r1, int32_t* cycles) {
328	if (!y) {
329		if (x >= 0) {
330			return 0;
331		}
332		return 0x8000;
333	}
334	if (!x) {
335		if (y >= 0) {
336			return 0x4000;
337		}
338		return 0xC000;
339	}
340	if (y >= 0) {
341		if (x >= 0) {
342			if (x >= y) {
343				return _ArcTan((y << 14) / x, r1, NULL, cycles);
344			}
345		} else if (-x >= y) {
346			return _ArcTan((y << 14) / x, r1, NULL, cycles) + 0x8000;
347		}
348		return 0x4000 - _ArcTan((x << 14) / y, r1, NULL, cycles);
349	} else {
350		if (x <= 0) {
351			if (-x > -y) {
352				return _ArcTan((y << 14) / x, r1, NULL, cycles) + 0x8000;
353			}
354		} else if (x >= -y) {
355			return _ArcTan((y << 14) / x, r1, NULL, cycles) + 0x10000;
356		}
357		return 0xC000 - _ArcTan((x << 14) / y, r1, NULL, cycles);
358	}
359}
360
361static int32_t _Sqrt(uint32_t x, int32_t* cycles) {
362	if (!x) {
363		*cycles += 53;
364		return 0;
365	}
366	int32_t currentCycles = 15;
367	uint32_t lower;
368	uint32_t upper = x;
369	uint32_t bound = 1;
370	while (bound < upper) {
371		upper >>= 1;
372		bound <<= 1;
373		currentCycles += 6;
374	}
375	while (true) {
376		currentCycles += 6;
377		upper = x;
378		uint32_t accum = 0;
379		lower = bound;
380		while (true) {
381			currentCycles += 5;
382			uint32_t oldLower = lower;
383			if (lower <= upper >> 1) {
384				lower <<= 1;
385			}
386			if (oldLower >= upper >> 1) {
387				break;
388			}
389		}
390		while (true) {
391			currentCycles += 8;
392			accum <<= 1;
393			if (upper >= lower) {
394				++accum;
395				upper -= lower;
396			}
397			if (lower == bound) {
398				break;
399			}
400			lower >>= 1;
401		}
402		uint32_t oldBound = bound;
403		bound += accum;
404		bound >>= 1;
405		if (bound >= oldBound) {
406			bound = oldBound;
407			break;
408		}
409	}
410	*cycles += currentCycles;
411	return bound;
412}
413
414void GBASwi16(struct ARMCore* cpu, int immediate) {
415	struct GBA* gba = (struct GBA*) cpu->master;
416	mLOG(GBA_BIOS, DEBUG, "SWI: %02X r0: %08X r1: %08X r2: %08X r3: %08X",
417	    immediate, cpu->gprs[0], cpu->gprs[1], cpu->gprs[2], cpu->gprs[3]);
418
419	switch (immediate) {
420	case 0xFA:
421		GBAPrintFlush(gba);
422		return;
423	}
424
425	if (gba->memory.fullBios) {
426		ARMRaiseSWI(cpu);
427		return;
428	}
429	switch (immediate) {
430	case 0x0:
431		_SoftReset(gba);
432		break;
433	case 0x1:
434		_RegisterRamReset(gba);
435		break;
436	case 0x2:
437		GBAHalt(gba);
438		break;
439	case 0x3:
440		GBAStop(gba);
441		break;
442	case 0x05:
443	// VBlankIntrWait
444	// Fall through:
445	case 0x04:
446		// IntrWait
447		ARMRaiseSWI(cpu);
448		break;
449	case 0x6:
450		_Div(gba, cpu->gprs[0], cpu->gprs[1]);
451		break;
452	case 0x7:
453		_Div(gba, cpu->gprs[1], cpu->gprs[0]);
454		break;
455	case 0x8:
456		cpu->gprs[0] = _Sqrt(cpu->gprs[0], &cpu->cycles);
457		break;
458	case 0x9:
459		cpu->gprs[0] = _ArcTan(cpu->gprs[0], &cpu->gprs[1], &cpu->gprs[3], &cpu->cycles);
460		break;
461	case 0xA:
462		cpu->gprs[0] = (uint16_t) _ArcTan2(cpu->gprs[0], cpu->gprs[1], &cpu->gprs[1], &cpu->cycles);
463		cpu->gprs[3] = 0x170;
464		break;
465	case 0xB:
466	case 0xC:
467		if (cpu->gprs[0] >> BASE_OFFSET < REGION_WORKING_RAM) {
468			mLOG(GBA_BIOS, GAME_ERROR, "Cannot CpuSet from BIOS");
469			break;
470		}
471		if (cpu->gprs[0] & (cpu->gprs[2] & (1 << 26) ? 3 : 1)) {
472			mLOG(GBA_BIOS, GAME_ERROR, "Misaligned CpuSet source");
473		}
474		if (cpu->gprs[1] & (cpu->gprs[2] & (1 << 26) ? 3 : 1)) {
475			mLOG(GBA_BIOS, GAME_ERROR, "Misaligned CpuSet destination");
476		}
477		ARMRaiseSWI(cpu);
478		break;
479	case 0xD:
480		cpu->gprs[0] = GBA_BIOS_CHECKSUM;
481		cpu->gprs[1] = 1;
482		cpu->gprs[3] = SIZE_BIOS;
483		break;
484	case 0xE:
485		_BgAffineSet(gba);
486		break;
487	case 0xF:
488		_ObjAffineSet(gba);
489		break;
490	case 0x10:
491		if (cpu->gprs[0] < BASE_WORKING_RAM) {
492			mLOG(GBA_BIOS, GAME_ERROR, "Bad BitUnPack source");
493			break;
494		}
495		switch (cpu->gprs[1] >> BASE_OFFSET) {
496		default:
497			mLOG(GBA_BIOS, GAME_ERROR, "Bad BitUnPack destination");
498		// Fall through
499		case REGION_WORKING_RAM:
500		case REGION_WORKING_IRAM:
501		case REGION_VRAM:
502			_unBitPack(gba);
503			break;
504		}
505		break;
506	case 0x11:
507	case 0x12:
508		if (cpu->gprs[0] < BASE_WORKING_RAM) {
509			mLOG(GBA_BIOS, GAME_ERROR, "Bad LZ77 source");
510			break;
511		}
512		switch (cpu->gprs[1] >> BASE_OFFSET) {
513		default:
514			mLOG(GBA_BIOS, GAME_ERROR, "Bad LZ77 destination");
515		// Fall through
516		case REGION_WORKING_RAM:
517		case REGION_WORKING_IRAM:
518		case REGION_VRAM:
519			_unLz77(gba, immediate == 0x11 ? 1 : 2);
520			break;
521		}
522		break;
523	case 0x13:
524		if (cpu->gprs[0] < BASE_WORKING_RAM) {
525			mLOG(GBA_BIOS, GAME_ERROR, "Bad Huffman source");
526			break;
527		}
528		switch (cpu->gprs[1] >> BASE_OFFSET) {
529		default:
530			mLOG(GBA_BIOS, GAME_ERROR, "Bad Huffman destination");
531		// Fall through
532		case REGION_WORKING_RAM:
533		case REGION_WORKING_IRAM:
534		case REGION_VRAM:
535			_unHuffman(gba);
536			break;
537		}
538		break;
539	case 0x14:
540	case 0x15:
541		if (cpu->gprs[0] < BASE_WORKING_RAM) {
542			mLOG(GBA_BIOS, GAME_ERROR, "Bad RL source");
543			break;
544		}
545		switch (cpu->gprs[1] >> BASE_OFFSET) {
546		default:
547			mLOG(GBA_BIOS, GAME_ERROR, "Bad RL destination");
548		// Fall through
549		case REGION_WORKING_RAM:
550		case REGION_WORKING_IRAM:
551		case REGION_VRAM:
552			_unRl(gba, immediate == 0x14 ? 1 : 2);
553			break;
554		}
555		break;
556	case 0x16:
557	case 0x17:
558	case 0x18:
559		if (cpu->gprs[0] < BASE_WORKING_RAM) {
560			mLOG(GBA_BIOS, GAME_ERROR, "Bad UnFilter source");
561			break;
562		}
563		switch (cpu->gprs[1] >> BASE_OFFSET) {
564		default:
565			mLOG(GBA_BIOS, GAME_ERROR, "Bad UnFilter destination");
566		// Fall through
567		case REGION_WORKING_RAM:
568		case REGION_WORKING_IRAM:
569		case REGION_VRAM:
570			_unFilter(gba, immediate == 0x18 ? 2 : 1, immediate == 0x16 ? 1 : 2);
571			break;
572		}
573		break;
574	case 0x19:
575		// SoundBias is mostly meaningless here
576		mLOG(GBA_BIOS, STUB, "Stub software interrupt: SoundBias (19)");
577		break;
578	case 0x1F:
579		_MidiKey2Freq(gba);
580		break;
581	default:
582		mLOG(GBA_BIOS, STUB, "Stub software interrupt: %02X", immediate);
583	}
584	gba->cpu->cycles += 45 + cpu->memory.activeNonseqCycles16 /* 8 bit load for SWI # */;
585	// Return cycles
586	if (gba->cpu->executionMode == MODE_ARM) {
587		gba->cpu->cycles += cpu->memory.activeNonseqCycles32 + cpu->memory.activeSeqCycles32;
588	} else {
589		gba->cpu->cycles += cpu->memory.activeNonseqCycles16 + cpu->memory.activeSeqCycles16;
590	}
591	gba->memory.biosPrefetch = 0xE3A02004;
592}
593
594void GBASwi32(struct ARMCore* cpu, int immediate) {
595	GBASwi16(cpu, immediate >> 16);
596}
597
598uint32_t GBAChecksum(uint32_t* memory, size_t size) {
599	size_t i;
600	uint32_t sum = 0;
601	for (i = 0; i < size; i += 4) {
602		sum += memory[i >> 2];
603	}
604	return sum;
605}
606
607static void _unLz77(struct GBA* gba, int width) {
608	struct ARMCore* cpu = gba->cpu;
609	uint32_t source = cpu->gprs[0];
610	uint32_t dest = cpu->gprs[1];
611	int remaining = (cpu->memory.load32(cpu, source, 0) & 0xFFFFFF00) >> 8;
612	// We assume the signature byte (0x10) is correct
613	int blockheader = 0; // Some compilers warn if this isn't set, even though it's trivially provably always set
614	source += 4;
615	int blocksRemaining = 0;
616	uint32_t disp;
617	int bytes;
618	int byte;
619	int halfword = 0;
620	while (remaining > 0) {
621		if (blocksRemaining) {
622			if (blockheader & 0x80) {
623				// Compressed
624				int block = cpu->memory.load8(cpu, source + 1, 0) | (cpu->memory.load8(cpu, source, 0) << 8);
625				source += 2;
626				disp = dest - (block & 0x0FFF) - 1;
627				bytes = (block >> 12) + 3;
628				while (bytes--) {
629					if (remaining) {
630						--remaining;
631					}
632					if (width == 2) {
633						byte = (int16_t) cpu->memory.load16(cpu, disp & ~1, 0);
634						if (dest & 1) {
635							byte >>= (disp & 1) * 8;
636							halfword |= byte << 8;
637							cpu->memory.store16(cpu, dest ^ 1, halfword, 0);
638						} else {
639							byte >>= (disp & 1) * 8;
640							halfword = byte & 0xFF;
641						}
642					} else {
643						byte = cpu->memory.load8(cpu, disp, 0);
644						cpu->memory.store8(cpu, dest, byte, 0);
645					}
646					++disp;
647					++dest;
648				}
649			} else {
650				// Uncompressed
651				byte = cpu->memory.load8(cpu, source, 0);
652				++source;
653				if (width == 2) {
654					if (dest & 1) {
655						halfword |= byte << 8;
656						cpu->memory.store16(cpu, dest ^ 1, halfword, 0);
657					} else {
658						halfword = byte;
659					}
660				} else {
661					cpu->memory.store8(cpu, dest, byte, 0);
662				}
663				++dest;
664				--remaining;
665			}
666			blockheader <<= 1;
667			--blocksRemaining;
668		} else {
669			blockheader = cpu->memory.load8(cpu, source, 0);
670			++source;
671			blocksRemaining = 8;
672		}
673	}
674	cpu->gprs[0] = source;
675	cpu->gprs[1] = dest;
676	cpu->gprs[3] = 0;
677}
678
679DECL_BITFIELD(HuffmanNode, uint8_t);
680DECL_BITS(HuffmanNode, Offset, 0, 6);
681DECL_BIT(HuffmanNode, RTerm, 6);
682DECL_BIT(HuffmanNode, LTerm, 7);
683
684static void _unHuffman(struct GBA* gba) {
685	struct ARMCore* cpu = gba->cpu;
686	uint32_t source = cpu->gprs[0] & 0xFFFFFFFC;
687	uint32_t dest = cpu->gprs[1];
688	uint32_t header = cpu->memory.load32(cpu, source, 0);
689	int remaining = header >> 8;
690	unsigned bits = header & 0xF;
691	if (bits == 0) {
692		mLOG(GBA_BIOS, GAME_ERROR, "Invalid Huffman bits");
693		bits = 8;
694	}
695	if (32 % bits || bits == 1) {
696		mLOG(GBA_BIOS, STUB, "Unimplemented unaligned Huffman");
697		return;
698	}
699	// We assume the signature byte (0x20) is correct
700	int treesize = (cpu->memory.load8(cpu, source + 4, 0) << 1) + 1;
701	int block = 0;
702	uint32_t treeBase = source + 5;
703	source += 5 + treesize;
704	uint32_t nPointer = treeBase;
705	HuffmanNode node;
706	int bitsRemaining;
707	int readBits;
708	int bitsSeen = 0;
709	node = cpu->memory.load8(cpu, nPointer, 0);
710	while (remaining > 0) {
711		uint32_t bitstream = cpu->memory.load32(cpu, source, 0);
712		source += 4;
713		for (bitsRemaining = 32; bitsRemaining > 0 && remaining > 0; --bitsRemaining, bitstream <<= 1) {
714			uint32_t next = (nPointer & ~1) + HuffmanNodeGetOffset(node) * 2 + 2;
715			if (bitstream & 0x80000000) {
716				// Go right
717				if (HuffmanNodeIsRTerm(node)) {
718					readBits = cpu->memory.load8(cpu, next + 1, 0);
719				} else {
720					nPointer = next + 1;
721					node = cpu->memory.load8(cpu, nPointer, 0);
722					continue;
723				}
724			} else {
725				// Go left
726				if (HuffmanNodeIsLTerm(node)) {
727					readBits = cpu->memory.load8(cpu, next, 0);
728				} else {
729					nPointer = next;
730					node = cpu->memory.load8(cpu, nPointer, 0);
731					continue;
732				}
733			}
734
735			block |= (readBits & ((1 << bits) - 1)) << bitsSeen;
736			bitsSeen += bits;
737			nPointer = treeBase;
738			node = cpu->memory.load8(cpu, nPointer, 0);
739			if (bitsSeen == 32) {
740				bitsSeen = 0;
741				cpu->memory.store32(cpu, dest, block, 0);
742				dest += 4;
743				remaining -= 4;
744				block = 0;
745			}
746		}
747	}
748	cpu->gprs[0] = source;
749	cpu->gprs[1] = dest;
750}
751
752static void _unRl(struct GBA* gba, int width) {
753	struct ARMCore* cpu = gba->cpu;
754	uint32_t source = cpu->gprs[0];
755	int remaining = (cpu->memory.load32(cpu, source & 0xFFFFFFFC, 0) & 0xFFFFFF00) >> 8;
756	int padding = (4 - remaining) & 0x3;
757	// We assume the signature byte (0x30) is correct
758	int blockheader;
759	int block;
760	source += 4;
761	uint32_t dest = cpu->gprs[1];
762	int halfword = 0;
763	while (remaining > 0) {
764		blockheader = cpu->memory.load8(cpu, source, 0);
765		++source;
766		if (blockheader & 0x80) {
767			// Compressed
768			blockheader &= 0x7F;
769			blockheader += 3;
770			block = cpu->memory.load8(cpu, source, 0);
771			++source;
772			while (blockheader-- && remaining) {
773				--remaining;
774				if (width == 2) {
775					if (dest & 1) {
776						halfword |= block << 8;
777						cpu->memory.store16(cpu, dest ^ 1, halfword, 0);
778					} else {
779						halfword = block;
780					}
781				} else {
782					cpu->memory.store8(cpu, dest, block, 0);
783				}
784				++dest;
785			}
786		} else {
787			// Uncompressed
788			blockheader++;
789			while (blockheader-- && remaining) {
790				--remaining;
791				int byte = cpu->memory.load8(cpu, source, 0);
792				++source;
793				if (width == 2) {
794					if (dest & 1) {
795						halfword |= byte << 8;
796						cpu->memory.store16(cpu, dest ^ 1, halfword, 0);
797					} else {
798						halfword = byte;
799					}
800				} else {
801					cpu->memory.store8(cpu, dest, byte, 0);
802				}
803				++dest;
804			}
805		}
806	}
807	if (width == 2) {
808		if (dest & 1) {
809			--padding;
810			++dest;
811		}
812		for (; padding > 0; padding -= 2, dest += 2) {
813			cpu->memory.store16(cpu, dest, 0, 0);
814		}
815	} else {
816		while (padding--) {
817			cpu->memory.store8(cpu, dest, 0, 0);
818			++dest;
819		}
820	}
821	cpu->gprs[0] = source;
822	cpu->gprs[1] = dest;
823}
824
825static void _unFilter(struct GBA* gba, int inwidth, int outwidth) {
826	struct ARMCore* cpu = gba->cpu;
827	uint32_t source = cpu->gprs[0] & 0xFFFFFFFC;
828	uint32_t dest = cpu->gprs[1];
829	uint32_t header = cpu->memory.load32(cpu, source, 0);
830	int remaining = header >> 8;
831	// We assume the signature nybble (0x8) is correct
832	uint16_t halfword = 0;
833	uint16_t old = 0;
834	source += 4;
835	while (remaining > 0) {
836		uint16_t new;
837		if (inwidth == 1) {
838			new = cpu->memory.load8(cpu, source, 0);
839		} else {
840			new = cpu->memory.load16(cpu, source, 0);
841		}
842		new += old;
843		if (outwidth > inwidth) {
844			halfword >>= 8;
845			halfword |= (new << 8);
846			if (source & 1) {
847				cpu->memory.store16(cpu, dest, halfword, 0);
848				dest += outwidth;
849				remaining -= outwidth;
850			}
851		} else if (outwidth == 1) {
852			cpu->memory.store8(cpu, dest, new, 0);
853			dest += outwidth;
854			remaining -= outwidth;
855		} else {
856			cpu->memory.store16(cpu, dest, new, 0);
857			dest += outwidth;
858			remaining -= outwidth;
859		}
860		old = new;
861		source += inwidth;
862	}
863	cpu->gprs[0] = source;
864	cpu->gprs[1] = dest;
865}
866
867static void _unBitPack(struct GBA* gba) {
868	struct ARMCore* cpu = gba->cpu;
869	uint32_t source = cpu->gprs[0];
870	uint32_t dest = cpu->gprs[1];
871	uint32_t info = cpu->gprs[2];
872	unsigned sourceLen = cpu->memory.load16(cpu, info, 0);
873	unsigned sourceWidth = cpu->memory.load8(cpu, info + 2, 0);
874	unsigned destWidth = cpu->memory.load8(cpu, info + 3, 0);
875	switch (sourceWidth) {
876	case 1:
877	case 2:
878	case 4:
879	case 8:
880		break;
881	default:
882		mLOG(GBA_BIOS, GAME_ERROR, "Bad BitUnPack source width: %u", sourceWidth);
883		return;
884	}
885	switch (destWidth) {
886	case 1:
887	case 2:
888	case 4:
889	case 8:
890	case 16:
891	case 32:
892		break;
893	default:
894		mLOG(GBA_BIOS, GAME_ERROR, "Bad BitUnPack destination width: %u", destWidth);
895		return;
896	}
897	uint32_t bias = cpu->memory.load32(cpu, info + 4, 0);
898	uint8_t in = 0;
899	uint32_t out = 0;
900	int bitsRemaining = 0;
901	int bitsEaten = 0;
902	while (sourceLen > 0 || bitsRemaining) {
903		if (!bitsRemaining) {
904			in = cpu->memory.load8(cpu, source, 0);
905			bitsRemaining = 8;
906			++source;
907			--sourceLen;
908		}
909		unsigned scaled = in & ((1 << sourceWidth) - 1);
910		in >>= sourceWidth;
911		if (scaled || bias & 0x80000000) {
912			scaled += bias & 0x7FFFFFFF;
913		}
914		bitsRemaining -= sourceWidth;
915		out |= scaled << bitsEaten;
916		bitsEaten += destWidth;
917		if (bitsEaten == 32) {
918			cpu->memory.store32(cpu, dest, out, 0);
919			bitsEaten = 0;
920			out = 0;
921			dest += 4;
922		}
923	}
924	cpu->gprs[0] = source;
925	cpu->gprs[1] = dest;
926}