all repos — mgba @ c19d1117f18da6a407ae8332a904004e9edf0677

mGBA Game Boy Advance Emulator

src/gba/gba-bios.c (view raw)

  1#include "gba-bios.h"
  2
  3#include "gba.h"
  4#include "gba-io.h"
  5#include "gba-memory.h"
  6
  7#include <math.h>
  8#include <stdlib.h>
  9
 10static void _unLz77(struct GBAMemory* memory, uint32_t source, uint8_t* dest);
 11static void _unRl(struct GBAMemory* memory, uint32_t source, uint8_t* dest);
 12
 13static void _RegisterRamReset(struct GBA* gba) {
 14	uint32_t registers = gba->cpu.gprs[0];
 15	(void)(registers);
 16	GBALog(gba, GBA_LOG_STUB, "RegisterRamReset unimplemented");
 17}
 18
 19static void _CpuSet(struct GBA* gba) {
 20	uint32_t source = gba->cpu.gprs[0];
 21	uint32_t dest = gba->cpu.gprs[1];
 22	uint32_t mode = gba->cpu.gprs[2];
 23	int count = mode & 0x000FFFFF;
 24	int fill = mode & 0x01000000;
 25	int wordsize = (mode & 0x04000000) ? 4 : 2;
 26	int i;
 27	if (fill) {
 28		if (wordsize == 4) {
 29			source &= 0xFFFFFFFC;
 30			dest &= 0xFFFFFFFC;
 31			int32_t word = gba->memory.d.load32(&gba->memory.d, source, &gba->cpu.cycles);
 32			for (i = 0; i < count; ++i) {
 33				gba->memory.d.store32(&gba->memory.d, dest + (i << 2), word, &gba->cpu.cycles);
 34				gba->board.d.processEvents(&gba->board.d);
 35			}
 36		} else {
 37			source &= 0xFFFFFFFE;
 38			dest &= 0xFFFFFFFE;
 39			uint16_t word = gba->memory.d.load16(&gba->memory.d, source, &gba->cpu.cycles);
 40			for (i = 0; i < count; ++i) {
 41				gba->memory.d.store16(&gba->memory.d, dest + (i << 1), word, &gba->cpu.cycles);
 42				gba->board.d.processEvents(&gba->board.d);
 43			}
 44		}
 45	} else {
 46		if (wordsize == 4) {
 47			source &= 0xFFFFFFFC;
 48			dest &= 0xFFFFFFFC;
 49			for (i = 0; i < count; ++i) {
 50				int32_t word = gba->memory.d.load32(&gba->memory.d, source + (i << 2), &gba->cpu.cycles);
 51				gba->memory.d.store32(&gba->memory.d, dest + (i << 2), word, &gba->cpu.cycles);
 52				gba->board.d.processEvents(&gba->board.d);
 53			}
 54		} else {
 55			source &= 0xFFFFFFFE;
 56			dest &= 0xFFFFFFFE;
 57			for (i = 0; i < count; ++i) {
 58				uint16_t word = gba->memory.d.load16(&gba->memory.d, source + (i << 1), &gba->cpu.cycles);
 59				gba->memory.d.store16(&gba->memory.d, dest + (i << 1), word, &gba->cpu.cycles);
 60				gba->board.d.processEvents(&gba->board.d);
 61			}
 62		}
 63	}
 64}
 65
 66static void _FastCpuSet(struct GBA* gba) {
 67	uint32_t source = gba->cpu.gprs[0] & 0xFFFFFFFC;
 68	uint32_t dest = gba->cpu.gprs[1] & 0xFFFFFFFC;
 69	uint32_t mode = gba->cpu.gprs[2];
 70	int count = mode & 0x000FFFFF;
 71	int storeCycles = gba->memory.d.waitMultiple(&gba->memory.d, dest, 4);
 72	count = ((count + 7) >> 3) << 3;
 73	int i;
 74	if (mode & 0x01000000) {
 75		int32_t word = gba->memory.d.load32(&gba->memory.d, source, &gba->cpu.cycles);
 76		for (i = 0; i < count; i += 4) {
 77			gba->memory.d.store32(&gba->memory.d, dest + ((i + 0) << 2), word, 0);
 78			gba->memory.d.store32(&gba->memory.d, dest + ((i + 1) << 2), word, 0);
 79			gba->memory.d.store32(&gba->memory.d, dest + ((i + 2) << 2), word, 0);
 80			gba->memory.d.store32(&gba->memory.d, dest + ((i + 3) << 2), word, 0);
 81			gba->cpu.cycles += storeCycles;
 82			gba->board.d.processEvents(&gba->board.d);
 83		}
 84	} else {
 85		int loadCycles = gba->memory.d.waitMultiple(&gba->memory.d, source, 4);
 86		for (i = 0; i < count; i += 4) {
 87			int32_t word0 = gba->memory.d.load32(&gba->memory.d, source + ((i + 0) << 2), 0);
 88			int32_t word1 = gba->memory.d.load32(&gba->memory.d, source + ((i + 1) << 2), 0);
 89			int32_t word2 = gba->memory.d.load32(&gba->memory.d, source + ((i + 2) << 2), 0);
 90			int32_t word3 = gba->memory.d.load32(&gba->memory.d, source + ((i + 3) << 2), 0);
 91			gba->cpu.cycles += loadCycles;
 92			gba->board.d.processEvents(&gba->board.d);
 93			gba->memory.d.store32(&gba->memory.d, dest + ((i + 0) << 2), word0, 0);
 94			gba->memory.d.store32(&gba->memory.d, dest + ((i + 1) << 2), word1, 0);
 95			gba->memory.d.store32(&gba->memory.d, dest + ((i + 2) << 2), word2, 0);
 96			gba->memory.d.store32(&gba->memory.d, dest + ((i + 3) << 2), word3, 0);
 97			gba->cpu.cycles += storeCycles;
 98			gba->board.d.processEvents(&gba->board.d);
 99		}
100	}
101}
102
103static void _BgAffineSet(struct GBA* gba) {
104	int i = gba->cpu.gprs[2];
105	float ox, oy;
106	float cx, cy;
107	float sx, sy;
108	float theta;
109	int offset = gba->cpu.gprs[0];
110	int destination = gba->cpu.gprs[1];
111	int diff = gba->cpu.gprs[3];
112	(void)(diff); // Are we supposed to use this?
113	float a, b, c, d;
114	float rx, ry;
115	while (i--) {
116		// [ sx   0  0 ]   [ cos(theta)  -sin(theta)  0 ]   [ 1  0  cx - ox ]   [ A B rx ]
117		// [  0  sy  0 ] * [ sin(theta)   cos(theta)  0 ] * [ 0  1  cy - oy ] = [ C D ry ]
118		// [  0   0  1 ]   [     0            0       1 ]   [ 0  0     1    ]   [ 0 0  1 ]
119		ox = gba->memory.d.load32(&gba->memory.d, offset, 0) / 256.f;
120		oy = gba->memory.d.load32(&gba->memory.d, offset + 4, 0) / 256.f;
121		cx = gba->memory.d.load16(&gba->memory.d, offset + 8, 0);
122		cy = gba->memory.d.load16(&gba->memory.d, offset + 10, 0);
123		sx = gba->memory.d.load16(&gba->memory.d, offset + 12, 0) / 256.f;
124		sy = gba->memory.d.load16(&gba->memory.d, offset + 14, 0) / 256.f;
125		theta = (gba->memory.d.loadU16(&gba->memory.d, offset + 16, 0) >> 8) / 128.f * M_PI;
126		offset += 20;
127		// Rotation
128		a = d = cosf(theta);
129		b = c = sinf(theta);
130		// Scale
131		a *= sx;
132		b *= -sx;
133		c *= sy;
134		d *= sy;
135		// Translate
136		rx = ox - (a * cx + b * cy);
137		ry = oy - (c * cx + d * cy);
138		gba->memory.d.store16(&gba->memory.d, destination, a * 256, 0);
139		gba->memory.d.store16(&gba->memory.d, destination + 2, b * 256, 0);
140		gba->memory.d.store16(&gba->memory.d, destination + 4, c * 256, 0);
141		gba->memory.d.store16(&gba->memory.d, destination + 6, d * 256, 0);
142		gba->memory.d.store32(&gba->memory.d, destination + 8, rx * 256, 0);
143		gba->memory.d.store32(&gba->memory.d, destination + 12, ry * 256, 0);
144		destination += 16;
145	}
146}
147
148static void _ObjAffineSet(struct GBA* gba) {
149	int i = gba->cpu.gprs[2];
150	float sx, sy;
151	float theta;
152	int offset = gba->cpu.gprs[0];
153	int destination = gba->cpu.gprs[1];
154	int diff = gba->cpu.gprs[3];
155	float a, b, c, d;
156	while (i--) {
157		// [ sx   0 ]   [ cos(theta)  -sin(theta) ]   [ A B ]
158		// [  0  sy ] * [ sin(theta)   cos(theta) ] = [ C D ]
159		sx = gba->memory.d.load16(&gba->memory.d, offset, 0) / 256.f;
160		sy = gba->memory.d.load16(&gba->memory.d, offset + 2, 0) / 256.f;
161		theta = (gba->memory.d.loadU16(&gba->memory.d, offset + 4, 0) >> 8) / 128.f * M_PI;
162		offset += 6;
163		// Rotation
164		a = d = cosf(theta);
165		b = c = sinf(theta);
166		// Scale
167		a *= sx;
168		b *= -sx;
169		c *= sy;
170		d *= sy;
171		gba->memory.d.store16(&gba->memory.d, destination, a * 256, 0);
172		gba->memory.d.store16(&gba->memory.d, destination + diff, b * 256, 0);
173		gba->memory.d.store16(&gba->memory.d, destination + diff * 2, c * 256, 0);
174		gba->memory.d.store16(&gba->memory.d, destination + diff * 3, d * 256, 0);
175		destination += diff * 4;
176	}
177}
178
179static void _MidiKey2Freq(struct GBA* gba) {
180	uint32_t key = gba->memory.d.load32(&gba->memory.d, gba->cpu.gprs[0] + 4, 0);
181	gba->cpu.gprs[0] = key / powf(2, (180.f - gba->cpu.gprs[1] - gba->cpu.gprs[2] / 256.f) / 12.f);
182}
183
184void GBASwi16(struct ARMBoard* board, int immediate) {
185	struct GBA* gba = ((struct GBABoard*) board)->p;
186	if (gba->memory.fullBios) {
187		ARMRaiseSWI(&gba->cpu);
188		return;
189	}
190	switch (immediate) {
191	case 0x1:
192		_RegisterRamReset(gba);
193		break;
194	case 0x2:
195		GBAHalt(gba);
196		break;
197	case 0x05:
198		// VBlankIntrWait
199		gba->cpu.gprs[0] = 1;
200		gba->cpu.gprs[1] = 1;
201		// Fall through:
202	case 0x04:
203		// IntrWait
204		gba->memory.io[REG_IME >> 1] = 1;
205		if (!gba->cpu.gprs[0] && gba->memory.io[REG_IF >> 1] & gba->cpu.gprs[1]) {
206			break;
207		}
208		gba->memory.io[REG_IF >> 1] = 0;
209		ARMRaiseSWI(&gba->cpu);
210		break;
211	case 0x6:
212		{
213			div_t result = div(gba->cpu.gprs[0], gba->cpu.gprs[1]);
214			gba->cpu.gprs[0] = result.quot;
215			gba->cpu.gprs[1] = result.rem;
216			gba->cpu.gprs[3] = abs(result.quot);
217		}
218		break;
219	case 0x7:
220		{
221			div_t result = div(gba->cpu.gprs[1], gba->cpu.gprs[0]);
222			gba->cpu.gprs[0] = result.quot;
223			gba->cpu.gprs[1] = result.rem;
224			gba->cpu.gprs[3] = abs(result.quot);
225		}
226		break;
227	case 0x8:
228		gba->cpu.gprs[0] = sqrt(gba->cpu.gprs[0]);
229		break;
230	case 0xA:
231		gba->cpu.gprs[0] = atan2f(gba->cpu.gprs[1] / 16384.f, gba->cpu.gprs[0] / 16384.f) / (2 * M_PI) * 0x10000;
232		break;
233	case 0xB:
234		_CpuSet(gba);
235		break;
236	case 0xC:
237		_FastCpuSet(gba);
238		break;
239	case 0xE:
240		_BgAffineSet(gba);
241		break;
242	case 0xF:
243		_ObjAffineSet(gba);
244		break;
245	case 0x11:
246	case 0x12:
247		switch (gba->cpu.gprs[1] >> BASE_OFFSET) {
248			case REGION_WORKING_RAM:
249				_unLz77(&gba->memory, gba->cpu.gprs[0], &((uint8_t*) gba->memory.wram)[(gba->cpu.gprs[1] & (SIZE_WORKING_RAM - 1))]);
250				break;
251			case REGION_WORKING_IRAM:
252				_unLz77(&gba->memory, gba->cpu.gprs[0], &((uint8_t*) gba->memory.iwram)[(gba->cpu.gprs[1] & (SIZE_WORKING_IRAM - 1))]);
253				break;
254			case REGION_VRAM:
255				_unLz77(&gba->memory, gba->cpu.gprs[0], &((uint8_t*) gba->video.renderer->vram)[(gba->cpu.gprs[1] & 0x0001FFFF)]);
256				break;
257			default:
258				GBALog(gba, GBA_LOG_WARN, "Bad LZ77 destination");
259				break;
260		}
261		break;
262	case 0x14:
263	case 0x15:
264		switch (gba->cpu.gprs[1] >> BASE_OFFSET) {
265			case REGION_WORKING_RAM:
266				_unRl(&gba->memory, gba->cpu.gprs[0], &((uint8_t*) gba->memory.wram)[(gba->cpu.gprs[1] & (SIZE_WORKING_RAM - 1))]);
267				break;
268			case REGION_WORKING_IRAM:
269				_unRl(&gba->memory, gba->cpu.gprs[0], &((uint8_t*) gba->memory.iwram)[(gba->cpu.gprs[1] & (SIZE_WORKING_IRAM - 1))]);
270				break;
271			case REGION_VRAM:
272				_unRl(&gba->memory, gba->cpu.gprs[0], &((uint8_t*) gba->video.renderer->vram)[(gba->cpu.gprs[1] & 0x0001FFFF)]);
273				break;
274			default:
275				GBALog(gba, GBA_LOG_WARN, "Bad RL destination");
276				break;
277		}
278		break;
279	case 0x1F:
280		_MidiKey2Freq(gba);
281		break;
282	default:
283		GBALog(gba, GBA_LOG_STUB, "Stub software interrupt: %02x", immediate);
284	}
285}
286
287void GBASwi32(struct ARMBoard* board, int immediate) {
288	GBASwi16(board, immediate >> 16);
289}
290
291static void _unLz77(struct GBAMemory* memory, uint32_t source, uint8_t* dest) {
292	int remaining = (memory->d.load32(&memory->d, source, 0) & 0xFFFFFF00) >> 8;
293	// We assume the signature byte (0x10) is correct
294	int blockheader;
295	uint32_t sPointer = source + 4;
296	uint8_t* dPointer = dest;
297	int blocksRemaining = 0;
298	int block;
299	uint8_t* disp;
300	int bytes;
301	while (remaining > 0) {
302		if (blocksRemaining) {
303			if (blockheader & 0x80) {
304				// Compressed
305				block = memory->d.loadU8(&memory->d, sPointer, 0) | (memory->d.loadU8(&memory->d, sPointer + 1, 0) << 8);
306				sPointer += 2;
307				disp = dPointer - (((block & 0x000F) << 8) | ((block & 0xFF00) >> 8)) - 1;
308				bytes = ((block & 0x00F0) >> 4) + 3;
309				while (bytes-- && remaining) {
310					--remaining;
311					*dPointer = *disp;
312					++disp;
313					++dPointer;
314				}
315			} else {
316				// Uncompressed
317				*dPointer = memory->d.loadU8(&memory->d, sPointer++, 0);
318				++dPointer;
319				--remaining;
320			}
321			blockheader <<= 1;
322			--blocksRemaining;
323		} else {
324			blockheader = memory->d.loadU8(&memory->d, sPointer++, 0);
325			blocksRemaining = 8;
326		}
327	}
328}
329
330static void _unRl(struct GBAMemory* memory, uint32_t source, uint8_t* dest) {
331	source = source & 0xFFFFFFFC;
332	int remaining = (memory->d.load32(&memory->d, source, 0) & 0xFFFFFF00) >> 8;
333	int padding = (4 - remaining) & 0x3;
334	// We assume the signature byte (0x30) is correct
335	int blockheader;
336	int block;
337	uint32_t sPointer = source + 4;
338	uint8_t* dPointer = dest;
339	while (remaining > 0) {
340		blockheader = memory->d.loadU8(&memory->d, sPointer++, 0);
341		if (blockheader & 0x80) {
342			// Compressed
343			blockheader &= 0x7F;
344			blockheader += 3;
345			block = memory->d.loadU8(&memory->d, sPointer++, 0);
346			while (blockheader-- && remaining) {
347				--remaining;
348				*dPointer = block;
349				++dPointer;
350			}
351		} else {
352			// Uncompressed
353			blockheader++;
354			while (blockheader-- && remaining) {
355				--remaining;
356				*dPointer = memory->d.loadU8(&memory->d, sPointer++, 0);
357				++dPointer;
358			}
359		}
360	}
361	while (padding--) {
362		*dPointer = 0;
363		++dPointer;
364	}
365}