2013-04-14 20:21:21 +00:00
|
|
|
#include "gba-bios.h"
|
|
|
|
|
|
|
|
#include "gba.h"
|
2013-04-26 09:00:59 +00:00
|
|
|
#include "gba-io.h"
|
2013-04-14 20:36:32 +00:00
|
|
|
#include "gba-memory.h"
|
|
|
|
|
2014-01-18 08:17:58 +00:00
|
|
|
const uint32_t GBA_BIOS_CHECKSUM = 0xBAAE187F;
|
|
|
|
const uint32_t GBA_DS_BIOS_CHECKSUM = 0xBAAE1880;
|
|
|
|
|
2014-04-20 01:14:17 +00:00
|
|
|
static void _unLz77(struct GBA* gba, uint32_t source, uint8_t* dest);
|
|
|
|
static void _unHuffman(struct GBA* gba, uint32_t source, uint32_t* dest);
|
|
|
|
static void _unRl(struct GBA* gba, uint32_t source, uint8_t* dest);
|
2013-04-20 09:52:10 +00:00
|
|
|
|
2013-04-28 05:54:41 +00:00
|
|
|
static void _RegisterRamReset(struct GBA* gba) {
|
|
|
|
uint32_t registers = gba->cpu.gprs[0];
|
|
|
|
(void)(registers);
|
2013-09-22 22:01:23 +00:00
|
|
|
GBALog(gba, GBA_LOG_STUB, "RegisterRamReset unimplemented");
|
2013-04-28 05:54:41 +00:00
|
|
|
}
|
|
|
|
|
2013-04-14 20:36:32 +00:00
|
|
|
static void _CpuSet(struct GBA* gba) {
|
|
|
|
uint32_t source = gba->cpu.gprs[0];
|
|
|
|
uint32_t dest = gba->cpu.gprs[1];
|
|
|
|
uint32_t mode = gba->cpu.gprs[2];
|
|
|
|
int count = mode & 0x000FFFFF;
|
|
|
|
int fill = mode & 0x01000000;
|
|
|
|
int wordsize = (mode & 0x04000000) ? 4 : 2;
|
|
|
|
int i;
|
|
|
|
if (fill) {
|
|
|
|
if (wordsize == 4) {
|
|
|
|
source &= 0xFFFFFFFC;
|
|
|
|
dest &= 0xFFFFFFFC;
|
2014-04-20 01:14:17 +00:00
|
|
|
int32_t word = gba->cpu.memory.load32(&gba->cpu, source, &gba->cpu.cycles);
|
2013-04-14 20:36:32 +00:00
|
|
|
for (i = 0; i < count; ++i) {
|
2014-04-20 01:14:17 +00:00
|
|
|
gba->cpu.memory.store32(&gba->cpu, dest + (i << 2), word, &gba->cpu.cycles);
|
|
|
|
gba->cpu.board.processEvents(&gba->cpu);
|
2013-04-14 20:36:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
source &= 0xFFFFFFFE;
|
|
|
|
dest &= 0xFFFFFFFE;
|
2014-04-20 01:14:17 +00:00
|
|
|
uint16_t word = gba->cpu.memory.load16(&gba->cpu, source, &gba->cpu.cycles);
|
2013-04-14 20:36:32 +00:00
|
|
|
for (i = 0; i < count; ++i) {
|
2014-04-20 01:14:17 +00:00
|
|
|
gba->cpu.memory.store16(&gba->cpu, dest + (i << 1), word, &gba->cpu.cycles);
|
|
|
|
gba->cpu.board.processEvents(&gba->cpu);
|
2013-04-14 20:36:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (wordsize == 4) {
|
|
|
|
source &= 0xFFFFFFFC;
|
|
|
|
dest &= 0xFFFFFFFC;
|
|
|
|
for (i = 0; i < count; ++i) {
|
2014-04-20 01:14:17 +00:00
|
|
|
int32_t word = gba->cpu.memory.load32(&gba->cpu, source + (i << 2), &gba->cpu.cycles);
|
|
|
|
gba->cpu.memory.store32(&gba->cpu, dest + (i << 2), word, &gba->cpu.cycles);
|
|
|
|
gba->cpu.board.processEvents(&gba->cpu);
|
2013-04-14 20:36:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
source &= 0xFFFFFFFE;
|
|
|
|
dest &= 0xFFFFFFFE;
|
|
|
|
for (i = 0; i < count; ++i) {
|
2014-04-20 01:14:17 +00:00
|
|
|
uint16_t word = gba->cpu.memory.load16(&gba->cpu, source + (i << 1), &gba->cpu.cycles);
|
|
|
|
gba->cpu.memory.store16(&gba->cpu, dest + (i << 1), word, &gba->cpu.cycles);
|
|
|
|
gba->cpu.board.processEvents(&gba->cpu);
|
2013-04-14 20:36:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-14 20:21:21 +00:00
|
|
|
|
2013-04-17 06:52:53 +00:00
|
|
|
static void _FastCpuSet(struct GBA* gba) {
|
|
|
|
uint32_t source = gba->cpu.gprs[0] & 0xFFFFFFFC;
|
|
|
|
uint32_t dest = gba->cpu.gprs[1] & 0xFFFFFFFC;
|
|
|
|
uint32_t mode = gba->cpu.gprs[2];
|
|
|
|
int count = mode & 0x000FFFFF;
|
2014-04-20 01:14:17 +00:00
|
|
|
int storeCycles = gba->cpu.memory.waitMultiple(&gba->cpu, dest, 4);
|
2013-04-17 06:52:53 +00:00
|
|
|
count = ((count + 7) >> 3) << 3;
|
|
|
|
int i;
|
|
|
|
if (mode & 0x01000000) {
|
2014-04-20 01:14:17 +00:00
|
|
|
int32_t word = gba->cpu.memory.load32(&gba->cpu, source, &gba->cpu.cycles);
|
2013-10-09 08:57:53 +00:00
|
|
|
for (i = 0; i < count; i += 4) {
|
2014-04-20 01:14:17 +00:00
|
|
|
gba->cpu.memory.store32(&gba->cpu, dest + ((i + 0) << 2), word, 0);
|
|
|
|
gba->cpu.memory.store32(&gba->cpu, dest + ((i + 1) << 2), word, 0);
|
|
|
|
gba->cpu.memory.store32(&gba->cpu, dest + ((i + 2) << 2), word, 0);
|
|
|
|
gba->cpu.memory.store32(&gba->cpu, dest + ((i + 3) << 2), word, 0);
|
2013-10-09 08:57:53 +00:00
|
|
|
gba->cpu.cycles += storeCycles;
|
2014-04-20 01:14:17 +00:00
|
|
|
gba->cpu.board.processEvents(&gba->cpu);
|
2013-04-17 06:52:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-04-20 01:14:17 +00:00
|
|
|
int loadCycles = gba->cpu.memory.waitMultiple(&gba->cpu, source, 4);
|
2013-10-09 08:57:53 +00:00
|
|
|
for (i = 0; i < count; i += 4) {
|
2014-04-20 01:14:17 +00:00
|
|
|
int32_t word0 = gba->cpu.memory.load32(&gba->cpu, source + ((i + 0) << 2), 0);
|
|
|
|
int32_t word1 = gba->cpu.memory.load32(&gba->cpu, source + ((i + 1) << 2), 0);
|
|
|
|
int32_t word2 = gba->cpu.memory.load32(&gba->cpu, source + ((i + 2) << 2), 0);
|
|
|
|
int32_t word3 = gba->cpu.memory.load32(&gba->cpu, source + ((i + 3) << 2), 0);
|
2013-10-09 08:57:53 +00:00
|
|
|
gba->cpu.cycles += loadCycles;
|
2014-04-20 01:14:17 +00:00
|
|
|
gba->cpu.board.processEvents(&gba->cpu);
|
|
|
|
gba->cpu.memory.store32(&gba->cpu, dest + ((i + 0) << 2), word0, 0);
|
|
|
|
gba->cpu.memory.store32(&gba->cpu, dest + ((i + 1) << 2), word1, 0);
|
|
|
|
gba->cpu.memory.store32(&gba->cpu, dest + ((i + 2) << 2), word2, 0);
|
|
|
|
gba->cpu.memory.store32(&gba->cpu, dest + ((i + 3) << 2), word3, 0);
|
2013-10-09 08:57:53 +00:00
|
|
|
gba->cpu.cycles += storeCycles;
|
2014-04-20 01:14:17 +00:00
|
|
|
gba->cpu.board.processEvents(&gba->cpu);
|
2013-04-17 06:52:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-04 08:20:42 +00:00
|
|
|
static void _BgAffineSet(struct GBA* gba) {
|
|
|
|
int i = gba->cpu.gprs[2];
|
|
|
|
float ox, oy;
|
|
|
|
float cx, cy;
|
|
|
|
float sx, sy;
|
|
|
|
float theta;
|
|
|
|
int offset = gba->cpu.gprs[0];
|
|
|
|
int destination = gba->cpu.gprs[1];
|
|
|
|
int diff = gba->cpu.gprs[3];
|
|
|
|
(void)(diff); // Are we supposed to use this?
|
|
|
|
float a, b, c, d;
|
|
|
|
float rx, ry;
|
|
|
|
while (i--) {
|
|
|
|
// [ sx 0 0 ] [ cos(theta) -sin(theta) 0 ] [ 1 0 cx - ox ] [ A B rx ]
|
|
|
|
// [ 0 sy 0 ] * [ sin(theta) cos(theta) 0 ] * [ 0 1 cy - oy ] = [ C D ry ]
|
|
|
|
// [ 0 0 1 ] [ 0 0 1 ] [ 0 0 1 ] [ 0 0 1 ]
|
2014-04-20 01:14:17 +00:00
|
|
|
ox = gba->cpu.memory.load32(&gba->cpu, offset, 0) / 256.f;
|
|
|
|
oy = gba->cpu.memory.load32(&gba->cpu, offset + 4, 0) / 256.f;
|
|
|
|
cx = gba->cpu.memory.load16(&gba->cpu, offset + 8, 0);
|
|
|
|
cy = gba->cpu.memory.load16(&gba->cpu, offset + 10, 0);
|
|
|
|
sx = gba->cpu.memory.load16(&gba->cpu, offset + 12, 0) / 256.f;
|
|
|
|
sy = gba->cpu.memory.load16(&gba->cpu, offset + 14, 0) / 256.f;
|
|
|
|
theta = (gba->cpu.memory.loadU16(&gba->cpu, offset + 16, 0) >> 8) / 128.f * M_PI;
|
2013-05-04 08:20:42 +00:00
|
|
|
offset += 20;
|
|
|
|
// Rotation
|
|
|
|
a = d = cosf(theta);
|
|
|
|
b = c = sinf(theta);
|
|
|
|
// Scale
|
|
|
|
a *= sx;
|
|
|
|
b *= -sx;
|
|
|
|
c *= sy;
|
|
|
|
d *= sy;
|
|
|
|
// Translate
|
|
|
|
rx = ox - (a * cx + b * cy);
|
|
|
|
ry = oy - (c * cx + d * cy);
|
2014-04-20 01:14:17 +00:00
|
|
|
gba->cpu.memory.store16(&gba->cpu, destination, a * 256, 0);
|
|
|
|
gba->cpu.memory.store16(&gba->cpu, destination + 2, b * 256, 0);
|
|
|
|
gba->cpu.memory.store16(&gba->cpu, destination + 4, c * 256, 0);
|
|
|
|
gba->cpu.memory.store16(&gba->cpu, destination + 6, d * 256, 0);
|
|
|
|
gba->cpu.memory.store32(&gba->cpu, destination + 8, rx * 256, 0);
|
|
|
|
gba->cpu.memory.store32(&gba->cpu, destination + 12, ry * 256, 0);
|
2013-05-04 08:20:42 +00:00
|
|
|
destination += 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 08:43:39 +00:00
|
|
|
static void _ObjAffineSet(struct GBA* gba) {
|
|
|
|
int i = gba->cpu.gprs[2];
|
|
|
|
float sx, sy;
|
|
|
|
float theta;
|
|
|
|
int offset = gba->cpu.gprs[0];
|
|
|
|
int destination = gba->cpu.gprs[1];
|
|
|
|
int diff = gba->cpu.gprs[3];
|
|
|
|
float a, b, c, d;
|
|
|
|
while (i--) {
|
|
|
|
// [ sx 0 ] [ cos(theta) -sin(theta) ] [ A B ]
|
|
|
|
// [ 0 sy ] * [ sin(theta) cos(theta) ] = [ C D ]
|
2014-04-20 01:14:17 +00:00
|
|
|
sx = gba->cpu.memory.load16(&gba->cpu, offset, 0) / 256.f;
|
|
|
|
sy = gba->cpu.memory.load16(&gba->cpu, offset + 2, 0) / 256.f;
|
|
|
|
theta = (gba->cpu.memory.loadU16(&gba->cpu, offset + 4, 0) >> 8) / 128.f * M_PI;
|
2013-05-03 08:43:39 +00:00
|
|
|
offset += 6;
|
|
|
|
// Rotation
|
|
|
|
a = d = cosf(theta);
|
|
|
|
b = c = sinf(theta);
|
|
|
|
// Scale
|
|
|
|
a *= sx;
|
|
|
|
b *= -sx;
|
|
|
|
c *= sy;
|
|
|
|
d *= sy;
|
2014-04-20 01:14:17 +00:00
|
|
|
gba->cpu.memory.store16(&gba->cpu, destination, a * 256, 0);
|
|
|
|
gba->cpu.memory.store16(&gba->cpu, destination + diff, b * 256, 0);
|
|
|
|
gba->cpu.memory.store16(&gba->cpu, destination + diff * 2, c * 256, 0);
|
|
|
|
gba->cpu.memory.store16(&gba->cpu, destination + diff * 3, d * 256, 0);
|
2013-05-03 08:43:39 +00:00
|
|
|
destination += diff * 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-20 10:01:50 +00:00
|
|
|
static void _MidiKey2Freq(struct GBA* gba) {
|
2014-04-20 01:14:17 +00:00
|
|
|
uint32_t key = gba->cpu.memory.load32(&gba->cpu, gba->cpu.gprs[0] + 4, 0);
|
2013-04-20 21:46:53 +00:00
|
|
|
gba->cpu.gprs[0] = key / powf(2, (180.f - gba->cpu.gprs[1] - gba->cpu.gprs[2] / 256.f) / 12.f);
|
2013-04-20 10:01:50 +00:00
|
|
|
}
|
|
|
|
|
2014-04-20 01:14:17 +00:00
|
|
|
void GBASwi16(struct ARMCore* cpu, int immediate) {
|
|
|
|
struct GBA* gba = (struct GBA*) cpu;
|
2013-10-09 05:36:19 +00:00
|
|
|
if (gba->memory.fullBios) {
|
|
|
|
ARMRaiseSWI(&gba->cpu);
|
|
|
|
return;
|
|
|
|
}
|
2013-04-14 20:21:21 +00:00
|
|
|
switch (immediate) {
|
2013-04-28 05:54:41 +00:00
|
|
|
case 0x1:
|
|
|
|
_RegisterRamReset(gba);
|
|
|
|
break;
|
2013-04-17 06:14:16 +00:00
|
|
|
case 0x2:
|
|
|
|
GBAHalt(gba);
|
|
|
|
break;
|
2013-04-26 09:00:59 +00:00
|
|
|
case 0x05:
|
|
|
|
// VBlankIntrWait
|
|
|
|
gba->cpu.gprs[0] = 1;
|
|
|
|
gba->cpu.gprs[1] = 1;
|
|
|
|
// Fall through:
|
|
|
|
case 0x04:
|
|
|
|
// IntrWait
|
|
|
|
gba->memory.io[REG_IME >> 1] = 1;
|
|
|
|
if (!gba->cpu.gprs[0] && gba->memory.io[REG_IF >> 1] & gba->cpu.gprs[1]) {
|
|
|
|
break;
|
|
|
|
}
|
2013-05-06 03:37:35 +00:00
|
|
|
gba->memory.io[REG_IF >> 1] = 0;
|
2013-04-26 09:00:59 +00:00
|
|
|
ARMRaiseSWI(&gba->cpu);
|
|
|
|
break;
|
2013-04-26 09:05:54 +00:00
|
|
|
case 0x6:
|
|
|
|
{
|
|
|
|
div_t result = div(gba->cpu.gprs[0], gba->cpu.gprs[1]);
|
|
|
|
gba->cpu.gprs[0] = result.quot;
|
|
|
|
gba->cpu.gprs[1] = result.rem;
|
2013-05-01 03:54:09 +00:00
|
|
|
gba->cpu.gprs[3] = abs(result.quot);
|
2013-04-26 09:05:54 +00:00
|
|
|
}
|
|
|
|
break;
|
2013-04-28 02:58:21 +00:00
|
|
|
case 0x7:
|
|
|
|
{
|
|
|
|
div_t result = div(gba->cpu.gprs[1], gba->cpu.gprs[0]);
|
|
|
|
gba->cpu.gprs[0] = result.quot;
|
|
|
|
gba->cpu.gprs[1] = result.rem;
|
2013-05-01 03:54:09 +00:00
|
|
|
gba->cpu.gprs[3] = abs(result.quot);
|
2013-04-28 02:58:21 +00:00
|
|
|
}
|
|
|
|
break;
|
2013-05-01 07:27:31 +00:00
|
|
|
case 0x8:
|
|
|
|
gba->cpu.gprs[0] = sqrt(gba->cpu.gprs[0]);
|
|
|
|
break;
|
2013-05-04 22:50:42 +00:00
|
|
|
case 0xA:
|
2013-09-27 16:47:30 +00:00
|
|
|
gba->cpu.gprs[0] = atan2f(gba->cpu.gprs[1] / 16384.f, gba->cpu.gprs[0] / 16384.f) / (2 * M_PI) * 0x10000;
|
2013-05-04 22:50:42 +00:00
|
|
|
break;
|
2013-04-14 20:36:32 +00:00
|
|
|
case 0xB:
|
|
|
|
_CpuSet(gba);
|
|
|
|
break;
|
2013-04-17 06:52:53 +00:00
|
|
|
case 0xC:
|
|
|
|
_FastCpuSet(gba);
|
|
|
|
break;
|
2014-01-18 08:17:58 +00:00
|
|
|
case 0xD:
|
|
|
|
gba->cpu.gprs[0] = GBAChecksum(gba->memory.bios, SIZE_BIOS);
|
2013-05-04 08:20:42 +00:00
|
|
|
case 0xE:
|
|
|
|
_BgAffineSet(gba);
|
|
|
|
break;
|
2013-05-03 08:43:39 +00:00
|
|
|
case 0xF:
|
|
|
|
_ObjAffineSet(gba);
|
|
|
|
break;
|
2013-04-20 09:52:10 +00:00
|
|
|
case 0x11:
|
|
|
|
case 0x12:
|
2013-10-14 03:12:30 +00:00
|
|
|
if (gba->cpu.gprs[0] < BASE_WORKING_RAM) {
|
|
|
|
GBALog(gba, GBA_LOG_GAME_ERROR, "Bad LZ77 source");
|
|
|
|
break;
|
|
|
|
}
|
2013-04-23 05:55:49 +00:00
|
|
|
switch (gba->cpu.gprs[1] >> BASE_OFFSET) {
|
|
|
|
case REGION_WORKING_RAM:
|
2014-04-20 01:14:17 +00:00
|
|
|
_unLz77(gba, gba->cpu.gprs[0], &((uint8_t*) gba->memory.wram)[(gba->cpu.gprs[1] & (SIZE_WORKING_RAM - 1))]);
|
2013-04-23 05:55:49 +00:00
|
|
|
break;
|
2013-05-01 07:24:28 +00:00
|
|
|
case REGION_WORKING_IRAM:
|
2014-04-20 01:14:17 +00:00
|
|
|
_unLz77(gba, gba->cpu.gprs[0], &((uint8_t*) gba->memory.iwram)[(gba->cpu.gprs[1] & (SIZE_WORKING_IRAM - 1))]);
|
2013-05-01 07:24:28 +00:00
|
|
|
break;
|
2013-04-23 05:55:49 +00:00
|
|
|
case REGION_VRAM:
|
2014-04-20 01:14:17 +00:00
|
|
|
_unLz77(gba, gba->cpu.gprs[0], &((uint8_t*) gba->video.renderer->vram)[(gba->cpu.gprs[1] & 0x0001FFFF)]);
|
2013-04-23 05:55:49 +00:00
|
|
|
break;
|
|
|
|
default:
|
2013-10-14 03:12:30 +00:00
|
|
|
GBALog(gba, GBA_LOG_GAME_ERROR, "Bad LZ77 destination");
|
2013-04-23 05:55:49 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-04-20 09:52:10 +00:00
|
|
|
break;
|
2013-10-12 20:32:23 +00:00
|
|
|
case 0x13:
|
2013-10-14 03:12:30 +00:00
|
|
|
if (gba->cpu.gprs[0] < BASE_WORKING_RAM) {
|
|
|
|
GBALog(gba, GBA_LOG_GAME_ERROR, "Bad Huffman source");
|
|
|
|
break;
|
|
|
|
}
|
2013-10-12 20:32:23 +00:00
|
|
|
switch (gba->cpu.gprs[1] >> BASE_OFFSET) {
|
|
|
|
case REGION_WORKING_RAM:
|
2014-04-20 01:14:17 +00:00
|
|
|
_unHuffman(gba, gba->cpu.gprs[0], &((uint32_t*) gba->memory.wram)[(gba->cpu.gprs[1] & (SIZE_WORKING_RAM - 3)) >> 2]);
|
2013-10-12 20:32:23 +00:00
|
|
|
break;
|
|
|
|
case REGION_WORKING_IRAM:
|
2014-04-20 01:14:17 +00:00
|
|
|
_unHuffman(gba, gba->cpu.gprs[0], &((uint32_t*) gba->memory.iwram)[(gba->cpu.gprs[1] & (SIZE_WORKING_IRAM - 3)) >> 2]);
|
2013-10-12 20:32:23 +00:00
|
|
|
break;
|
|
|
|
case REGION_VRAM:
|
2014-04-20 01:14:17 +00:00
|
|
|
_unHuffman(gba, gba->cpu.gprs[0], &((uint32_t*) gba->video.renderer->vram)[(gba->cpu.gprs[1] & 0x0001FFFC) >> 2]);
|
2013-10-12 20:32:23 +00:00
|
|
|
break;
|
|
|
|
default:
|
2013-10-14 03:12:30 +00:00
|
|
|
GBALog(gba, GBA_LOG_GAME_ERROR, "Bad Huffman destination");
|
2013-10-12 20:32:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2013-05-12 01:22:23 +00:00
|
|
|
case 0x14:
|
|
|
|
case 0x15:
|
2013-10-14 03:12:30 +00:00
|
|
|
if (gba->cpu.gprs[0] < BASE_WORKING_RAM) {
|
|
|
|
GBALog(gba, GBA_LOG_GAME_ERROR, "Bad RL source");
|
|
|
|
break;
|
|
|
|
}
|
2013-05-12 01:22:23 +00:00
|
|
|
switch (gba->cpu.gprs[1] >> BASE_OFFSET) {
|
|
|
|
case REGION_WORKING_RAM:
|
2014-04-20 01:14:17 +00:00
|
|
|
_unRl(gba, gba->cpu.gprs[0], &((uint8_t*) gba->memory.wram)[(gba->cpu.gprs[1] & (SIZE_WORKING_RAM - 1))]);
|
2013-05-12 01:22:23 +00:00
|
|
|
break;
|
|
|
|
case REGION_WORKING_IRAM:
|
2014-04-20 01:14:17 +00:00
|
|
|
_unRl(gba, gba->cpu.gprs[0], &((uint8_t*) gba->memory.iwram)[(gba->cpu.gprs[1] & (SIZE_WORKING_IRAM - 1))]);
|
2013-05-12 01:22:23 +00:00
|
|
|
break;
|
|
|
|
case REGION_VRAM:
|
2014-04-20 01:14:17 +00:00
|
|
|
_unRl(gba, gba->cpu.gprs[0], &((uint8_t*) gba->video.renderer->vram)[(gba->cpu.gprs[1] & 0x0001FFFF)]);
|
2013-05-12 01:22:23 +00:00
|
|
|
break;
|
|
|
|
default:
|
2013-10-14 03:12:30 +00:00
|
|
|
GBALog(gba, GBA_LOG_GAME_ERROR, "Bad RL destination");
|
2013-05-12 01:22:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2013-04-20 10:01:50 +00:00
|
|
|
case 0x1F:
|
|
|
|
_MidiKey2Freq(gba);
|
|
|
|
break;
|
2013-04-14 20:21:21 +00:00
|
|
|
default:
|
2013-09-22 22:01:23 +00:00
|
|
|
GBALog(gba, GBA_LOG_STUB, "Stub software interrupt: %02x", immediate);
|
2013-04-14 20:21:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-20 01:14:17 +00:00
|
|
|
void GBASwi32(struct ARMCore* cpu, int immediate) {
|
|
|
|
GBASwi16(cpu, immediate >> 16);
|
2013-04-14 20:21:21 +00:00
|
|
|
}
|
2013-04-20 09:52:10 +00:00
|
|
|
|
2014-01-18 08:17:58 +00:00
|
|
|
uint32_t GBAChecksum(uint32_t* memory, size_t size) {
|
|
|
|
size_t i;
|
|
|
|
uint32_t sum = 0;
|
|
|
|
for (i = 0; i < size; i += 4) {
|
|
|
|
sum += memory[i >> 2];
|
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
2014-04-20 01:14:17 +00:00
|
|
|
static void _unLz77(struct GBA* gba, uint32_t source, uint8_t* dest) {
|
|
|
|
int remaining = (gba->cpu.memory.load32(&gba->cpu, source, 0) & 0xFFFFFF00) >> 8;
|
2013-04-20 09:52:10 +00:00
|
|
|
// We assume the signature byte (0x10) is correct
|
|
|
|
int blockheader;
|
|
|
|
uint32_t sPointer = source + 4;
|
|
|
|
uint8_t* dPointer = dest;
|
|
|
|
int blocksRemaining = 0;
|
|
|
|
int block;
|
|
|
|
uint8_t* disp;
|
|
|
|
int bytes;
|
|
|
|
while (remaining > 0) {
|
|
|
|
if (blocksRemaining) {
|
|
|
|
if (blockheader & 0x80) {
|
|
|
|
// Compressed
|
2014-04-20 01:14:17 +00:00
|
|
|
block = gba->cpu.memory.loadU8(&gba->cpu, sPointer, 0) | (gba->cpu.memory.loadU8(&gba->cpu, sPointer + 1, 0) << 8);
|
2013-04-20 09:52:10 +00:00
|
|
|
sPointer += 2;
|
|
|
|
disp = dPointer - (((block & 0x000F) << 8) | ((block & 0xFF00) >> 8)) - 1;
|
|
|
|
bytes = ((block & 0x00F0) >> 4) + 3;
|
|
|
|
while (bytes-- && remaining) {
|
|
|
|
--remaining;
|
|
|
|
*dPointer = *disp;
|
|
|
|
++disp;
|
|
|
|
++dPointer;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Uncompressed
|
2014-04-20 01:14:17 +00:00
|
|
|
*dPointer = gba->cpu.memory.loadU8(&gba->cpu, sPointer++, 0);
|
2013-04-20 09:52:10 +00:00
|
|
|
++dPointer;
|
|
|
|
--remaining;
|
|
|
|
}
|
|
|
|
blockheader <<= 1;
|
|
|
|
--blocksRemaining;
|
|
|
|
} else {
|
2014-04-20 01:14:17 +00:00
|
|
|
blockheader = gba->cpu.memory.loadU8(&gba->cpu, sPointer++, 0);
|
2013-04-20 09:52:10 +00:00
|
|
|
blocksRemaining = 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-12 01:22:23 +00:00
|
|
|
|
2014-04-20 01:14:17 +00:00
|
|
|
static void _unHuffman(struct GBA* gba, uint32_t source, uint32_t* dest) {
|
2013-10-12 20:32:23 +00:00
|
|
|
source = source & 0xFFFFFFFC;
|
2014-04-20 01:14:17 +00:00
|
|
|
uint32_t header = gba->cpu.memory.load32(&gba->cpu, source, 0);
|
2013-10-12 20:32:23 +00:00
|
|
|
int remaining = header >> 8;
|
|
|
|
int bits = header & 0xF;
|
|
|
|
if (32 % bits) {
|
2014-04-20 01:14:17 +00:00
|
|
|
GBALog(gba, GBA_LOG_STUB, "Unimplemented unaligned Huffman");
|
2013-10-12 20:32:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
int padding = (4 - remaining) & 0x3;
|
|
|
|
remaining &= 0xFFFFFFFC;
|
|
|
|
// We assume the signature byte (0x20) is correct
|
|
|
|
//var tree = [];
|
2014-04-20 01:14:17 +00:00
|
|
|
int treesize = (gba->cpu.memory.loadU8(&gba->cpu, source + 4, 0) << 1) + 1;
|
2013-10-12 20:32:23 +00:00
|
|
|
int block = 0;
|
|
|
|
uint32_t treeBase = source + 5;
|
|
|
|
uint32_t sPointer = source + 5 + treesize;
|
|
|
|
uint32_t* dPointer = dest;
|
|
|
|
uint32_t nPointer = treeBase;
|
|
|
|
union HuffmanNode {
|
|
|
|
struct {
|
|
|
|
unsigned offset : 6;
|
|
|
|
unsigned rTerm : 1;
|
|
|
|
unsigned lTerm : 1;
|
|
|
|
};
|
|
|
|
uint8_t packed;
|
|
|
|
} node;
|
|
|
|
int bitsRemaining;
|
|
|
|
int readBits;
|
|
|
|
int bitsSeen = 0;
|
2014-04-20 01:14:17 +00:00
|
|
|
node.packed = gba->cpu.memory.load8(&gba->cpu, nPointer, 0);
|
2013-10-12 20:32:23 +00:00
|
|
|
while (remaining > 0) {
|
2014-04-20 01:14:17 +00:00
|
|
|
uint32_t bitstream = gba->cpu.memory.load32(&gba->cpu, sPointer, 0);
|
2013-10-12 20:32:23 +00:00
|
|
|
sPointer += 4;
|
|
|
|
for (bitsRemaining = 32; bitsRemaining > 0; --bitsRemaining, bitstream <<= 1) {
|
|
|
|
uint32_t next = (nPointer & ~1) + node.offset * 2 + 2;
|
|
|
|
if (bitstream & 0x80000000) {
|
|
|
|
// Go right
|
|
|
|
if (node.rTerm) {
|
2014-04-20 01:14:17 +00:00
|
|
|
readBits = gba->cpu.memory.load8(&gba->cpu, next + 1, 0);
|
2013-10-12 20:32:23 +00:00
|
|
|
} else {
|
|
|
|
nPointer = next + 1;
|
2014-04-20 01:14:17 +00:00
|
|
|
node.packed = gba->cpu.memory.load8(&gba->cpu, nPointer, 0);
|
2013-10-12 20:32:23 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Go left
|
|
|
|
if (node.lTerm) {
|
2014-04-20 01:14:17 +00:00
|
|
|
readBits = gba->cpu.memory.load8(&gba->cpu, next, 0);
|
2013-10-12 20:32:23 +00:00
|
|
|
} else {
|
|
|
|
nPointer = next;
|
2014-04-20 01:14:17 +00:00
|
|
|
node.packed = gba->cpu.memory.load8(&gba->cpu, nPointer, 0);
|
2013-10-12 20:32:23 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
block |= (readBits & ((1 << bits) - 1)) << bitsSeen;
|
|
|
|
bitsSeen += bits;
|
|
|
|
nPointer = treeBase;
|
2014-04-20 01:14:17 +00:00
|
|
|
node.packed = gba->cpu.memory.load8(&gba->cpu, nPointer, 0);
|
2013-10-12 20:32:23 +00:00
|
|
|
if (bitsSeen == 32) {
|
|
|
|
bitsSeen = 0;
|
|
|
|
*dPointer = block;
|
|
|
|
++dPointer;
|
|
|
|
remaining -= 4;
|
|
|
|
block = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if (padding) {
|
|
|
|
*dPointer = block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-20 01:14:17 +00:00
|
|
|
static void _unRl(struct GBA* gba, uint32_t source, uint8_t* dest) {
|
2013-05-12 01:22:23 +00:00
|
|
|
source = source & 0xFFFFFFFC;
|
2014-04-20 01:14:17 +00:00
|
|
|
int remaining = (gba->cpu.memory.load32(&gba->cpu, source, 0) & 0xFFFFFF00) >> 8;
|
2013-05-12 01:22:23 +00:00
|
|
|
int padding = (4 - remaining) & 0x3;
|
|
|
|
// We assume the signature byte (0x30) is correct
|
|
|
|
int blockheader;
|
|
|
|
int block;
|
|
|
|
uint32_t sPointer = source + 4;
|
|
|
|
uint8_t* dPointer = dest;
|
|
|
|
while (remaining > 0) {
|
2014-04-20 01:14:17 +00:00
|
|
|
blockheader = gba->cpu.memory.loadU8(&gba->cpu, sPointer++, 0);
|
2013-05-12 01:22:23 +00:00
|
|
|
if (blockheader & 0x80) {
|
|
|
|
// Compressed
|
|
|
|
blockheader &= 0x7F;
|
|
|
|
blockheader += 3;
|
2014-04-20 01:14:17 +00:00
|
|
|
block = gba->cpu.memory.loadU8(&gba->cpu, sPointer++, 0);
|
2013-05-12 01:22:23 +00:00
|
|
|
while (blockheader-- && remaining) {
|
|
|
|
--remaining;
|
|
|
|
*dPointer = block;
|
|
|
|
++dPointer;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Uncompressed
|
|
|
|
blockheader++;
|
|
|
|
while (blockheader-- && remaining) {
|
|
|
|
--remaining;
|
2014-04-20 01:14:17 +00:00
|
|
|
*dPointer = gba->cpu.memory.loadU8(&gba->cpu, sPointer++, 0);
|
2013-05-12 01:22:23 +00:00
|
|
|
++dPointer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (padding--) {
|
|
|
|
*dPointer = 0;
|
|
|
|
++dPointer;
|
|
|
|
}
|
|
|
|
}
|