all repos — mgba @ 0298f25ea46b0eb0901becf0e9a6b478223fe39d

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