all repos — mgba @ 07098984e4b3ae1a01361d43c93f7b1dcc90dc00

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						--remaining;
541					}
542					if (width == 2) {
543						byte = (int16_t) cpu->memory.load16(cpu, disp & ~1, 0);
544						if (dest & 1) {
545							byte >>= (disp & 1) * 8;
546							halfword |= byte << 8;
547							cpu->memory.store16(cpu, dest ^ 1, halfword, 0);
548						} else {
549							byte >>= (disp & 1) * 8;
550							halfword = byte & 0xFF;
551						}
552					} else {
553						byte = cpu->memory.load8(cpu, disp, 0);
554						cpu->memory.store8(cpu, dest, byte, 0);
555					}
556					++disp;
557					++dest;
558				}
559			} else {
560				// Uncompressed
561				byte = cpu->memory.load8(cpu, source, 0);
562				++source;
563				if (width == 2) {
564					if (dest & 1) {
565						halfword |= byte << 8;
566						cpu->memory.store16(cpu, dest ^ 1, halfword, 0);
567					} else {
568						halfword = byte;
569					}
570				} else {
571					cpu->memory.store8(cpu, dest, byte, 0);
572				}
573				++dest;
574				--remaining;
575			}
576			blockheader <<= 1;
577			--blocksRemaining;
578		} else {
579			blockheader = cpu->memory.load8(cpu, source, 0);
580			++source;
581			blocksRemaining = 8;
582		}
583	}
584	cpu->gprs[0] = source;
585	cpu->gprs[1] = dest;
586	cpu->gprs[3] = 0;
587}
588
589DECL_BITFIELD(HuffmanNode, uint8_t);
590DECL_BITS(HuffmanNode, Offset, 0, 6);
591DECL_BIT(HuffmanNode, RTerm, 6);
592DECL_BIT(HuffmanNode, LTerm, 7);
593
594static void _unHuffman(struct GBA* gba) {
595	struct ARMCore* cpu = gba->cpu;
596	uint32_t source = cpu->gprs[0] & 0xFFFFFFFC;
597	uint32_t dest = cpu->gprs[1];
598	uint32_t header = cpu->memory.load32(cpu, source, 0);
599	int remaining = header >> 8;
600	unsigned bits = header & 0xF;
601	if (bits == 0) {
602		mLOG(GBA_BIOS, GAME_ERROR, "Invalid Huffman bits");
603		bits = 8;
604	}
605	if (32 % bits || bits == 1) {
606		mLOG(GBA_BIOS, STUB, "Unimplemented unaligned Huffman");
607		return;
608	}
609	// We assume the signature byte (0x20) is correct
610	int treesize = (cpu->memory.load8(cpu, source + 4, 0) << 1) + 1;
611	int block = 0;
612	uint32_t treeBase = source + 5;
613	source += 5 + treesize;
614	uint32_t nPointer = treeBase;
615	HuffmanNode node;
616	int bitsRemaining;
617	int readBits;
618	int bitsSeen = 0;
619	node = cpu->memory.load8(cpu, nPointer, 0);
620	while (remaining > 0) {
621		uint32_t bitstream = cpu->memory.load32(cpu, source, 0);
622		source += 4;
623		for (bitsRemaining = 32; bitsRemaining > 0 && remaining > 0; --bitsRemaining, bitstream <<= 1) {
624			uint32_t next = (nPointer & ~1) + HuffmanNodeGetOffset(node) * 2 + 2;
625			if (bitstream & 0x80000000) {
626				// Go right
627				if (HuffmanNodeIsRTerm(node)) {
628					readBits = cpu->memory.load8(cpu, next + 1, 0);
629				} else {
630					nPointer = next + 1;
631					node = cpu->memory.load8(cpu, nPointer, 0);
632					continue;
633				}
634			} else {
635				// Go left
636				if (HuffmanNodeIsLTerm(node)) {
637					readBits = cpu->memory.load8(cpu, next, 0);
638				} else {
639					nPointer = next;
640					node = cpu->memory.load8(cpu, nPointer, 0);
641					continue;
642				}
643			}
644
645			block |= (readBits & ((1 << bits) - 1)) << bitsSeen;
646			bitsSeen += bits;
647			nPointer = treeBase;
648			node = cpu->memory.load8(cpu, nPointer, 0);
649			if (bitsSeen == 32) {
650				bitsSeen = 0;
651				cpu->memory.store32(cpu, dest, block, 0);
652				dest += 4;
653				remaining -= 4;
654				block = 0;
655			}
656		}
657	}
658	cpu->gprs[0] = source;
659	cpu->gprs[1] = dest;
660}
661
662static void _unRl(struct GBA* gba, int width) {
663	struct ARMCore* cpu = gba->cpu;
664	uint32_t source = cpu->gprs[0];
665	int remaining = (cpu->memory.load32(cpu, source & 0xFFFFFFFC, 0) & 0xFFFFFF00) >> 8;
666	int padding = (4 - remaining) & 0x3;
667	// We assume the signature byte (0x30) is correct
668	int blockheader;
669	int block;
670	source += 4;
671	uint32_t dest = cpu->gprs[1];
672	int halfword = 0;
673	while (remaining > 0) {
674		blockheader = cpu->memory.load8(cpu, source, 0);
675		++source;
676		if (blockheader & 0x80) {
677			// Compressed
678			blockheader &= 0x7F;
679			blockheader += 3;
680			block = cpu->memory.load8(cpu, source, 0);
681			++source;
682			while (blockheader-- && remaining) {
683				--remaining;
684				if (width == 2) {
685					if (dest & 1) {
686						halfword |= block << 8;
687						cpu->memory.store16(cpu, dest ^ 1, halfword, 0);
688					} else {
689						halfword = block;
690					}
691				} else {
692					cpu->memory.store8(cpu, dest, block, 0);
693				}
694				++dest;
695			}
696		} else {
697			// Uncompressed
698			blockheader++;
699			while (blockheader-- && remaining) {
700				--remaining;
701				int byte = cpu->memory.load8(cpu, source, 0);
702				++source;
703				if (width == 2) {
704					if (dest & 1) {
705						halfword |= byte << 8;
706						cpu->memory.store16(cpu, dest ^ 1, halfword, 0);
707					} else {
708						halfword = byte;
709					}
710				} else {
711					cpu->memory.store8(cpu, dest, byte, 0);
712				}
713				++dest;
714			}
715		}
716	}
717	if (width == 2) {
718		if (dest & 1) {
719			--padding;
720			++dest;
721		}
722		for (; padding > 0; padding -= 2, dest += 2) {
723			cpu->memory.store16(cpu, dest, 0, 0);
724		}
725	} else {
726		while (padding--) {
727			cpu->memory.store8(cpu, dest, 0, 0);
728			++dest;
729		}
730	}
731	cpu->gprs[0] = source;
732	cpu->gprs[1] = dest;
733}
734
735static void _unFilter(struct GBA* gba, int inwidth, int outwidth) {
736	struct ARMCore* cpu = gba->cpu;
737	uint32_t source = cpu->gprs[0] & 0xFFFFFFFC;
738	uint32_t dest = cpu->gprs[1];
739	uint32_t header = cpu->memory.load32(cpu, source, 0);
740	int remaining = header >> 8;
741	// We assume the signature nybble (0x8) is correct
742	uint16_t halfword = 0;
743	uint16_t old = 0;
744	source += 4;
745	while (remaining > 0) {
746		uint16_t new;
747		if (inwidth == 1) {
748			new = cpu->memory.load8(cpu, source, 0);
749		} else {
750			new = cpu->memory.load16(cpu, source, 0);
751		}
752		new += old;
753		if (outwidth > inwidth) {
754			halfword >>= 8;
755			halfword |= (new << 8);
756			if (source & 1) {
757				cpu->memory.store16(cpu, dest, halfword, 0);
758				dest += outwidth;
759				remaining -= outwidth;
760			}
761		} else if (outwidth == 1) {
762			cpu->memory.store8(cpu, dest, new, 0);
763			dest += outwidth;
764			remaining -= outwidth;
765		} else {
766			cpu->memory.store16(cpu, dest, new, 0);
767			dest += outwidth;
768			remaining -= outwidth;
769		}
770		old = new;
771		source += inwidth;
772	}
773	cpu->gprs[0] = source;
774	cpu->gprs[1] = dest;
775}
776
777static void _unBitPack(struct GBA* gba) {
778	struct ARMCore* cpu = gba->cpu;
779	uint32_t source = cpu->gprs[0];
780	uint32_t dest = cpu->gprs[1];
781	uint32_t info = cpu->gprs[2];
782	unsigned sourceLen = cpu->memory.load16(cpu, info, 0);
783	unsigned sourceWidth = cpu->memory.load8(cpu, info + 2, 0);
784	unsigned destWidth = cpu->memory.load8(cpu, info + 3, 0);
785	switch (sourceWidth) {
786	case 1:
787	case 2:
788	case 4:
789	case 8:
790		break;
791	default:
792		mLOG(GBA_BIOS, GAME_ERROR, "Bad BitUnPack source width: %u", sourceWidth);
793		return;
794	}
795	switch (destWidth) {
796	case 1:
797	case 2:
798	case 4:
799	case 8:
800	case 16:
801	case 32:
802		break;
803	default:
804		mLOG(GBA_BIOS, GAME_ERROR, "Bad BitUnPack destination width: %u", destWidth);
805		return;
806	}
807	uint32_t bias = cpu->memory.load32(cpu, info + 4, 0);
808	uint8_t in = 0;
809	uint32_t out = 0;
810	int bitsRemaining = 0;
811	int bitsEaten = 0;
812	while (sourceLen > 0) {
813		if (!bitsRemaining) {
814			in = cpu->memory.load8(cpu, source, 0);
815			bitsRemaining = 8;
816			++source;
817			--sourceLen;
818		}
819		unsigned scaled = in & ((1 << sourceWidth) - 1);
820		in >>= sourceWidth;
821		if (scaled || bias & 0x80000000) {
822			scaled += bias & 0x7FFFFFFF;
823			scaled &= (1 << destWidth) - 1;
824		}
825		bitsRemaining -= sourceWidth;
826		out |= scaled << bitsEaten;
827		bitsEaten += destWidth;
828		if (bitsEaten == 32) {
829			cpu->memory.store32(cpu, dest, out, 0);
830			bitsEaten = 0;
831			out = 0;
832			dest += 4;
833		}
834	}
835}