all repos — mgba @ 69aa7ac3aed1b77f214ece118eefbaa25a2545f4

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.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 && (denom != -1 || num != INT32_MIN)) {
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 if (denom == 0) {
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	} else {
272		mLOG(GBA_BIOS, GAME_ERROR, "Attempting to divide INT_MIN by -1!");
273		cpu->gprs[0] = INT32_MIN;
274		cpu->gprs[1] = 0;
275		cpu->gprs[3] = INT32_MIN;
276	}
277}
278
279static int16_t _ArcTan(int32_t i, int32_t* r1, int32_t* r3) {
280	int32_t a = -((i * i) >> 14);
281	int32_t b = ((0xA9 * a) >> 14) + 0x390;
282	b = ((b * a) >> 14) + 0x91C;
283	b = ((b * a) >> 14) + 0xFB6;
284	b = ((b * a) >> 14) + 0x16AA;
285	b = ((b * a) >> 14) + 0x2081;
286	b = ((b * a) >> 14) + 0x3651;
287	b = ((b * a) >> 14) + 0xA2F9;
288	if (r1) {
289		*r1 = a;
290	}
291	if (r3) {
292		*r3 = b;
293	}
294	return (i * b) >> 16;
295}
296
297static int16_t _ArcTan2(int32_t x, int32_t y, int32_t* r1) {
298	if (!y) {
299		if (x >= 0) {
300			return 0;
301		}
302		return 0x8000;
303	}
304	if (!x) {
305		if (y >= 0) {
306			return 0x4000;
307		}
308		return 0xC000;
309	}
310	if (y >= 0) {
311		if (x >= 0) {
312			if (x >= y) {
313				return _ArcTan((y << 14) / x, r1, NULL);
314			}
315		} else if (-x >= y) {
316			return _ArcTan((y << 14) / x, r1, NULL) + 0x8000;
317		}
318		return 0x4000 - _ArcTan((x << 14) / y, r1, NULL);
319	} else {
320		if (x <= 0) {
321			if (-x > -y) {
322				return _ArcTan((y << 14) / x, r1, NULL) + 0x8000;
323			}
324		} else if (x >= -y) {
325			return _ArcTan((y << 14) / x, r1, NULL) + 0x10000;
326		}
327		return 0xC000 - _ArcTan((x << 14) / y, r1, NULL);
328	}
329}
330
331void GBASwi16(struct ARMCore* cpu, int immediate) {
332	struct GBA* gba = (struct GBA*) cpu->master;
333	mLOG(GBA_BIOS, DEBUG, "SWI: %02X r0: %08X r1: %08X r2: %08X r3: %08X",
334	    immediate, cpu->gprs[0], cpu->gprs[1], cpu->gprs[2], cpu->gprs[3]);
335
336	switch (immediate) {
337	case 0xFA:
338		GBAPrintFlush(gba);
339		return;
340	}
341
342	if (gba->memory.fullBios) {
343		ARMRaiseSWI(cpu);
344		return;
345	}
346	switch (immediate) {
347	case 0x0:
348		_SoftReset(gba);
349		break;
350	case 0x1:
351		_RegisterRamReset(gba);
352		break;
353	case 0x2:
354		GBAHalt(gba);
355		break;
356	case 0x3:
357		GBAStop(gba);
358		break;
359	case 0x05:
360	// VBlankIntrWait
361	// Fall through:
362	case 0x04:
363		// IntrWait
364		ARMRaiseSWI(cpu);
365		break;
366	case 0x6:
367		_Div(gba, cpu->gprs[0], cpu->gprs[1]);
368		break;
369	case 0x7:
370		_Div(gba, cpu->gprs[1], cpu->gprs[0]);
371		break;
372	case 0x8:
373		cpu->gprs[0] = sqrt((uint32_t) cpu->gprs[0]);
374		break;
375	case 0x9:
376		cpu->gprs[0] = _ArcTan(cpu->gprs[0], &cpu->gprs[1], &cpu->gprs[3]);
377		break;
378	case 0xA:
379		cpu->gprs[0] = (uint16_t) _ArcTan2(cpu->gprs[0], cpu->gprs[1], &cpu->gprs[1]);
380		cpu->gprs[3] = 0x170;
381		break;
382	case 0xB:
383	case 0xC:
384		if (cpu->gprs[0] >> BASE_OFFSET < REGION_WORKING_RAM) {
385			mLOG(GBA_BIOS, GAME_ERROR, "Cannot CpuSet from BIOS");
386			break;
387		}
388		if (cpu->gprs[0] & (cpu->gprs[2] & (1 << 26) ? 3 : 1)) {
389			mLOG(GBA_BIOS, GAME_ERROR, "Misaligned CpuSet source");
390		}
391		if (cpu->gprs[1] & (cpu->gprs[2] & (1 << 26) ? 3 : 1)) {
392			mLOG(GBA_BIOS, GAME_ERROR, "Misaligned CpuSet destination");
393		}
394		ARMRaiseSWI(cpu);
395		break;
396	case 0xD:
397		cpu->gprs[0] = GBA_BIOS_CHECKSUM;
398		cpu->gprs[1] = 1;
399		cpu->gprs[3] = SIZE_BIOS;
400		break;
401	case 0xE:
402		_BgAffineSet(gba);
403		break;
404	case 0xF:
405		_ObjAffineSet(gba);
406		break;
407	case 0x10:
408		if (cpu->gprs[0] < BASE_WORKING_RAM) {
409			mLOG(GBA_BIOS, GAME_ERROR, "Bad BitUnPack source");
410			break;
411		}
412		switch (cpu->gprs[1] >> BASE_OFFSET) {
413		default:
414			mLOG(GBA_BIOS, GAME_ERROR, "Bad BitUnPack destination");
415		// Fall through
416		case REGION_WORKING_RAM:
417		case REGION_WORKING_IRAM:
418		case REGION_VRAM:
419			_unBitPack(gba);
420			break;
421		}
422		break;
423	case 0x11:
424	case 0x12:
425		if (cpu->gprs[0] < BASE_WORKING_RAM) {
426			mLOG(GBA_BIOS, GAME_ERROR, "Bad LZ77 source");
427			break;
428		}
429		switch (cpu->gprs[1] >> BASE_OFFSET) {
430		default:
431			mLOG(GBA_BIOS, GAME_ERROR, "Bad LZ77 destination");
432		// Fall through
433		case REGION_WORKING_RAM:
434		case REGION_WORKING_IRAM:
435		case REGION_VRAM:
436			_unLz77(gba, immediate == 0x11 ? 1 : 2);
437			break;
438		}
439		break;
440	case 0x13:
441		if (cpu->gprs[0] < BASE_WORKING_RAM) {
442			mLOG(GBA_BIOS, GAME_ERROR, "Bad Huffman source");
443			break;
444		}
445		switch (cpu->gprs[1] >> BASE_OFFSET) {
446		default:
447			mLOG(GBA_BIOS, GAME_ERROR, "Bad Huffman destination");
448		// Fall through
449		case REGION_WORKING_RAM:
450		case REGION_WORKING_IRAM:
451		case REGION_VRAM:
452			_unHuffman(gba);
453			break;
454		}
455		break;
456	case 0x14:
457	case 0x15:
458		if (cpu->gprs[0] < BASE_WORKING_RAM) {
459			mLOG(GBA_BIOS, GAME_ERROR, "Bad RL source");
460			break;
461		}
462		switch (cpu->gprs[1] >> BASE_OFFSET) {
463		default:
464			mLOG(GBA_BIOS, GAME_ERROR, "Bad RL destination");
465		// Fall through
466		case REGION_WORKING_RAM:
467		case REGION_WORKING_IRAM:
468		case REGION_VRAM:
469			_unRl(gba, immediate == 0x14 ? 1 : 2);
470			break;
471		}
472		break;
473	case 0x16:
474	case 0x17:
475	case 0x18:
476		if (cpu->gprs[0] < BASE_WORKING_RAM) {
477			mLOG(GBA_BIOS, GAME_ERROR, "Bad UnFilter source");
478			break;
479		}
480		switch (cpu->gprs[1] >> BASE_OFFSET) {
481		default:
482			mLOG(GBA_BIOS, GAME_ERROR, "Bad UnFilter destination");
483		// Fall through
484		case REGION_WORKING_RAM:
485		case REGION_WORKING_IRAM:
486		case REGION_VRAM:
487			_unFilter(gba, immediate == 0x18 ? 2 : 1, immediate == 0x16 ? 1 : 2);
488			break;
489		}
490		break;
491	case 0x19:
492		// SoundBias is mostly meaningless here
493		mLOG(GBA_BIOS, STUB, "Stub software interrupt: SoundBias (19)");
494		break;
495	case 0x1F:
496		_MidiKey2Freq(gba);
497		break;
498	default:
499		mLOG(GBA_BIOS, STUB, "Stub software interrupt: %02X", immediate);
500	}
501	gba->memory.biosPrefetch = 0xE3A02004;
502}
503
504void GBASwi32(struct ARMCore* cpu, int immediate) {
505	GBASwi16(cpu, immediate >> 16);
506}
507
508uint32_t GBAChecksum(uint32_t* memory, size_t size) {
509	size_t i;
510	uint32_t sum = 0;
511	for (i = 0; i < size; i += 4) {
512		sum += memory[i >> 2];
513	}
514	return sum;
515}
516
517static void _unLz77(struct GBA* gba, int width) {
518	struct ARMCore* cpu = gba->cpu;
519	uint32_t source = cpu->gprs[0];
520	uint32_t dest = cpu->gprs[1];
521	int remaining = (cpu->memory.load32(cpu, source, 0) & 0xFFFFFF00) >> 8;
522	// We assume the signature byte (0x10) is correct
523	int blockheader = 0; // Some compilers warn if this isn't set, even though it's trivially provably always set
524	source += 4;
525	int blocksRemaining = 0;
526	uint32_t disp;
527	int bytes;
528	int byte;
529	int halfword = 0;
530	while (remaining > 0) {
531		if (blocksRemaining) {
532			if (blockheader & 0x80) {
533				// Compressed
534				int block = cpu->memory.load8(cpu, source + 1, 0) | (cpu->memory.load8(cpu, source, 0) << 8);
535				source += 2;
536				disp = dest - (block & 0x0FFF) - 1;
537				bytes = (block >> 12) + 3;
538				while (bytes--) {
539					if (!remaining) {
540						if (gba->hardCrash) {
541							mLOG(GBA_BIOS, FATAL, "Improperly compressed LZ77 data. Real BIOS would hang.");
542						} else {
543							mLOG(GBA_BIOS, GAME_ERROR, "Improperly compressed LZ77 data. Real BIOS would hang.");
544						}
545						break;
546					}
547					--remaining;
548					if (width == 2) {
549						byte = (int16_t) cpu->memory.load16(cpu, disp & ~1, 0);
550						if (dest & 1) {
551							byte >>= (disp & 1) * 8;
552							halfword |= byte << 8;
553							cpu->memory.store16(cpu, dest ^ 1, halfword, 0);
554						} else {
555							byte >>= (disp & 1) * 8;
556							halfword = byte & 0xFF;
557						}
558					} else {
559						byte = cpu->memory.load8(cpu, disp, 0);
560						cpu->memory.store8(cpu, dest, byte, 0);
561					}
562					++disp;
563					++dest;
564				}
565			} else {
566				// Uncompressed
567				byte = cpu->memory.load8(cpu, source, 0);
568				++source;
569				if (width == 2) {
570					if (dest & 1) {
571						halfword |= byte << 8;
572						cpu->memory.store16(cpu, dest ^ 1, halfword, 0);
573					} else {
574						halfword = byte;
575					}
576				} else {
577					cpu->memory.store8(cpu, dest, byte, 0);
578				}
579				++dest;
580				--remaining;
581			}
582			blockheader <<= 1;
583			--blocksRemaining;
584		} else {
585			blockheader = cpu->memory.load8(cpu, source, 0);
586			++source;
587			blocksRemaining = 8;
588		}
589	}
590	cpu->gprs[0] = source;
591	cpu->gprs[1] = dest;
592	cpu->gprs[3] = 0;
593}
594
595DECL_BITFIELD(HuffmanNode, uint8_t);
596DECL_BITS(HuffmanNode, Offset, 0, 6);
597DECL_BIT(HuffmanNode, RTerm, 6);
598DECL_BIT(HuffmanNode, LTerm, 7);
599
600static void _unHuffman(struct GBA* gba) {
601	struct ARMCore* cpu = gba->cpu;
602	uint32_t source = cpu->gprs[0] & 0xFFFFFFFC;
603	uint32_t dest = cpu->gprs[1];
604	uint32_t header = cpu->memory.load32(cpu, source, 0);
605	int remaining = header >> 8;
606	unsigned bits = header & 0xF;
607	if (bits == 0) {
608		mLOG(GBA_BIOS, GAME_ERROR, "Invalid Huffman bits");
609		bits = 8;
610	}
611	if (32 % bits || bits == 1) {
612		mLOG(GBA_BIOS, STUB, "Unimplemented unaligned Huffman");
613		return;
614	}
615	// We assume the signature byte (0x20) is correct
616	int treesize = (cpu->memory.load8(cpu, source + 4, 0) << 1) + 1;
617	int block = 0;
618	uint32_t treeBase = source + 5;
619	source += 5 + treesize;
620	uint32_t nPointer = treeBase;
621	HuffmanNode node;
622	int bitsRemaining;
623	int readBits;
624	int bitsSeen = 0;
625	node = cpu->memory.load8(cpu, nPointer, 0);
626	while (remaining > 0) {
627		uint32_t bitstream = cpu->memory.load32(cpu, source, 0);
628		source += 4;
629		for (bitsRemaining = 32; bitsRemaining > 0 && remaining > 0; --bitsRemaining, bitstream <<= 1) {
630			uint32_t next = (nPointer & ~1) + HuffmanNodeGetOffset(node) * 2 + 2;
631			if (bitstream & 0x80000000) {
632				// Go right
633				if (HuffmanNodeIsRTerm(node)) {
634					readBits = cpu->memory.load8(cpu, next + 1, 0);
635				} else {
636					nPointer = next + 1;
637					node = cpu->memory.load8(cpu, nPointer, 0);
638					continue;
639				}
640			} else {
641				// Go left
642				if (HuffmanNodeIsLTerm(node)) {
643					readBits = cpu->memory.load8(cpu, next, 0);
644				} else {
645					nPointer = next;
646					node = cpu->memory.load8(cpu, nPointer, 0);
647					continue;
648				}
649			}
650
651			block |= (readBits & ((1 << bits) - 1)) << bitsSeen;
652			bitsSeen += bits;
653			nPointer = treeBase;
654			node = cpu->memory.load8(cpu, nPointer, 0);
655			if (bitsSeen == 32) {
656				bitsSeen = 0;
657				cpu->memory.store32(cpu, dest, block, 0);
658				dest += 4;
659				remaining -= 4;
660				block = 0;
661			}
662		}
663	}
664	cpu->gprs[0] = source;
665	cpu->gprs[1] = dest;
666}
667
668static void _unRl(struct GBA* gba, int width) {
669	struct ARMCore* cpu = gba->cpu;
670	uint32_t source = cpu->gprs[0];
671	int remaining = (cpu->memory.load32(cpu, source & 0xFFFFFFFC, 0) & 0xFFFFFF00) >> 8;
672	int padding = (4 - remaining) & 0x3;
673	// We assume the signature byte (0x30) is correct
674	int blockheader;
675	int block;
676	source += 4;
677	uint32_t dest = cpu->gprs[1];
678	int halfword = 0;
679	while (remaining > 0) {
680		blockheader = cpu->memory.load8(cpu, source, 0);
681		++source;
682		if (blockheader & 0x80) {
683			// Compressed
684			blockheader &= 0x7F;
685			blockheader += 3;
686			block = cpu->memory.load8(cpu, source, 0);
687			++source;
688			while (blockheader-- && remaining) {
689				--remaining;
690				if (width == 2) {
691					if (dest & 1) {
692						halfword |= block << 8;
693						cpu->memory.store16(cpu, dest ^ 1, halfword, 0);
694					} else {
695						halfword = block;
696					}
697				} else {
698					cpu->memory.store8(cpu, dest, block, 0);
699				}
700				++dest;
701			}
702		} else {
703			// Uncompressed
704			blockheader++;
705			while (blockheader-- && remaining) {
706				--remaining;
707				int byte = cpu->memory.load8(cpu, source, 0);
708				++source;
709				if (width == 2) {
710					if (dest & 1) {
711						halfword |= byte << 8;
712						cpu->memory.store16(cpu, dest ^ 1, halfword, 0);
713					} else {
714						halfword = byte;
715					}
716				} else {
717					cpu->memory.store8(cpu, dest, byte, 0);
718				}
719				++dest;
720			}
721		}
722	}
723	if (width == 2) {
724		if (dest & 1) {
725			--padding;
726			++dest;
727		}
728		for (; padding > 0; padding -= 2, dest += 2) {
729			cpu->memory.store16(cpu, dest, 0, 0);
730		}
731	} else {
732		while (padding--) {
733			cpu->memory.store8(cpu, dest, 0, 0);
734			++dest;
735		}
736	}
737	cpu->gprs[0] = source;
738	cpu->gprs[1] = dest;
739}
740
741static void _unFilter(struct GBA* gba, int inwidth, int outwidth) {
742	struct ARMCore* cpu = gba->cpu;
743	uint32_t source = cpu->gprs[0] & 0xFFFFFFFC;
744	uint32_t dest = cpu->gprs[1];
745	uint32_t header = cpu->memory.load32(cpu, source, 0);
746	int remaining = header >> 8;
747	// We assume the signature nybble (0x8) is correct
748	uint16_t halfword = 0;
749	uint16_t old = 0;
750	source += 4;
751	while (remaining > 0) {
752		uint16_t new;
753		if (inwidth == 1) {
754			new = cpu->memory.load8(cpu, source, 0);
755		} else {
756			new = cpu->memory.load16(cpu, source, 0);
757		}
758		new += old;
759		if (outwidth > inwidth) {
760			halfword >>= 8;
761			halfword |= (new << 8);
762			if (source & 1) {
763				cpu->memory.store16(cpu, dest, halfword, 0);
764				dest += outwidth;
765				remaining -= outwidth;
766			}
767		} else if (outwidth == 1) {
768			cpu->memory.store8(cpu, dest, new, 0);
769			dest += outwidth;
770			remaining -= outwidth;
771		} else {
772			cpu->memory.store16(cpu, dest, new, 0);
773			dest += outwidth;
774			remaining -= outwidth;
775		}
776		old = new;
777		source += inwidth;
778	}
779	cpu->gprs[0] = source;
780	cpu->gprs[1] = dest;
781}
782
783static void _unBitPack(struct GBA* gba) {
784	struct ARMCore* cpu = gba->cpu;
785	uint32_t source = cpu->gprs[0];
786	uint32_t dest = cpu->gprs[1];
787	uint32_t info = cpu->gprs[2];
788	unsigned sourceLen = cpu->memory.load16(cpu, info, 0);
789	unsigned sourceWidth = cpu->memory.load8(cpu, info + 2, 0);
790	unsigned destWidth = cpu->memory.load8(cpu, info + 3, 0);
791	switch (sourceWidth) {
792	case 1:
793	case 2:
794	case 4:
795	case 8:
796		break;
797	default:
798		mLOG(GBA_BIOS, GAME_ERROR, "Bad BitUnPack source width: %u", sourceWidth);
799		return;
800	}
801	switch (destWidth) {
802	case 1:
803	case 2:
804	case 4:
805	case 8:
806	case 16:
807	case 32:
808		break;
809	default:
810		mLOG(GBA_BIOS, GAME_ERROR, "Bad BitUnPack destination width: %u", destWidth);
811		return;
812	}
813	uint32_t bias = cpu->memory.load32(cpu, info + 4, 0);
814	uint8_t in = 0;
815	uint32_t out = 0;
816	int bitsRemaining = 0;
817	int bitsEaten = 0;
818	while (sourceLen > 0) {
819		if (!bitsRemaining) {
820			in = cpu->memory.load8(cpu, source, 0);
821			bitsRemaining = 8;
822			++source;
823			--sourceLen;
824		}
825		unsigned scaled = in & ((1 << sourceWidth) - 1);
826		in >>= sourceWidth;
827		if (scaled || bias & 0x80000000) {
828			scaled += bias & 0x7FFFFFFF;
829			scaled &= (1 << destWidth) - 1;
830		}
831		bitsRemaining -= sourceWidth;
832		out |= scaled << bitsEaten;
833		bitsEaten += destWidth;
834		if (bitsEaten == 32) {
835			cpu->memory.store32(cpu, dest, out, 0);
836			bitsEaten = 0;
837			out = 0;
838			dest += 4;
839		}
840	}
841}