all repos — mgba @ c9d411a762ec6ef13c3108d0479408f8326df442

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