mgba/src/gba/bios.c

685 lines
20 KiB
C
Raw Normal View History

/* Copyright (c) 2013-2015 Jeffrey Pfau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "bios.h"
2013-04-14 20:21:21 +00:00
2016-04-29 06:51:18 +00:00
#include "arm/isa-inlines.h"
#include "arm/macros.h"
#include "gba/gba.h"
#include "gba/io.h"
#include "gba/memory.h"
2013-04-14 20:36:32 +00:00
2014-01-18 08:17:58 +00:00
const uint32_t GBA_BIOS_CHECKSUM = 0xBAAE187F;
const uint32_t GBA_DS_BIOS_CHECKSUM = 0xBAAE1880;
2016-02-08 13:25:46 +00:00
mLOG_DEFINE_CATEGORY(GBA_BIOS, "GBA BIOS");
static void _unLz77(struct GBA* gba, int width);
static void _unHuffman(struct GBA* gba);
static void _unRl(struct GBA* gba, int width);
static void _unFilter(struct GBA* gba, int inwidth, int outwidth);
2013-04-20 09:52:10 +00:00
2015-01-08 06:33:16 +00:00
static void _SoftReset(struct GBA* gba) {
struct ARMCore* cpu = gba->cpu;
ARMSetPrivilegeMode(cpu, MODE_IRQ);
cpu->spsr.packed = 0;
cpu->gprs[ARM_LR] = 0;
cpu->gprs[ARM_SP] = SP_BASE_IRQ;
ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
cpu->spsr.packed = 0;
cpu->gprs[ARM_LR] = 0;
cpu->gprs[ARM_SP] = SP_BASE_SUPERVISOR;
ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
cpu->gprs[ARM_LR] = 0;
cpu->gprs[ARM_SP] = SP_BASE_SYSTEM;
int8_t flag = ((int8_t*) gba->memory.iwram)[0x7FFA];
memset(((int8_t*) gba->memory.iwram) + SIZE_WORKING_IRAM - 0x200, 0, 0x200);
if (flag) {
cpu->gprs[ARM_PC] = BASE_WORKING_RAM;
} else {
cpu->gprs[ARM_PC] = BASE_CART0;
}
_ARMSetMode(cpu, MODE_ARM);
int currentCycles = 0;
ARM_WRITE_PC;
}
2013-04-28 05:54:41 +00:00
static void _RegisterRamReset(struct GBA* gba) {
2014-04-20 07:19:55 +00:00
uint32_t registers = gba->cpu->gprs[0];
struct ARMCore* cpu = gba->cpu;
cpu->memory.store16(cpu, BASE_IO | REG_DISPCNT, 0x0080, 0);
if (registers & 0x01) {
memset(gba->memory.wram, 0, SIZE_WORKING_RAM);
}
if (registers & 0x02) {
memset(gba->memory.iwram, 0, SIZE_WORKING_IRAM - 0x200);
}
if (registers & 0x04) {
memset(gba->video.palette, 0, SIZE_PALETTE_RAM);
}
if (registers & 0x08) {
memset(gba->video.renderer->vram, 0, SIZE_VRAM);
}
if (registers & 0x10) {
memset(gba->video.oam.raw, 0, SIZE_OAM);
}
if (registers & 0x20) {
cpu->memory.store16(cpu, BASE_IO | REG_SIOCNT, 0x0000, 0);
cpu->memory.store16(cpu, BASE_IO | REG_RCNT, RCNT_INITIAL, 0);
cpu->memory.store16(cpu, BASE_IO | REG_SIOMLT_SEND, 0, 0);
cpu->memory.store16(cpu, BASE_IO | REG_JOYCNT, 0, 0);
2015-03-02 05:54:22 +00:00
cpu->memory.store32(cpu, BASE_IO | REG_JOY_RECV_LO, 0, 0);
cpu->memory.store32(cpu, BASE_IO | REG_JOY_TRANS_LO, 0, 0);
}
if (registers & 0x40) {
cpu->memory.store16(cpu, BASE_IO | REG_SOUND1CNT_LO, 0, 0);
cpu->memory.store16(cpu, BASE_IO | REG_SOUND1CNT_HI, 0, 0);
cpu->memory.store16(cpu, BASE_IO | REG_SOUND1CNT_X, 0, 0);
cpu->memory.store16(cpu, BASE_IO | REG_SOUND2CNT_LO, 0, 0);
cpu->memory.store16(cpu, BASE_IO | REG_SOUND2CNT_HI, 0, 0);
cpu->memory.store16(cpu, BASE_IO | REG_SOUND3CNT_LO, 0, 0);
cpu->memory.store16(cpu, BASE_IO | REG_SOUND3CNT_HI, 0, 0);
cpu->memory.store16(cpu, BASE_IO | REG_SOUND3CNT_X, 0, 0);
cpu->memory.store16(cpu, BASE_IO | REG_SOUND4CNT_LO, 0, 0);
cpu->memory.store16(cpu, BASE_IO | REG_SOUND4CNT_HI, 0, 0);
cpu->memory.store16(cpu, BASE_IO | REG_SOUNDCNT_LO, 0, 0);
cpu->memory.store16(cpu, BASE_IO | REG_SOUNDCNT_HI, 0, 0);
cpu->memory.store16(cpu, BASE_IO | REG_SOUNDCNT_X, 0, 0);
cpu->memory.store16(cpu, BASE_IO | REG_SOUNDBIAS, 0x200, 0);
2016-02-14 03:37:34 +00:00
memset(gba->audio.psg.ch3.wavedata32, 0, sizeof(gba->audio.psg.ch3.wavedata32));
}
if (registers & 0x80) {
cpu->memory.store16(cpu, BASE_IO | 0x04, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x06, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x08, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x0A, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x0C, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x0E, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x10, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x12, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x14, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x16, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x18, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x1A, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x1C, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x1E, 0, 0);
cpu->memory.store16(cpu, BASE_IO | REG_BG2PA, 0x100, 0);
cpu->memory.store16(cpu, BASE_IO | REG_BG2PB, 0, 0);
cpu->memory.store16(cpu, BASE_IO | REG_BG2PC, 0, 0);
cpu->memory.store16(cpu, BASE_IO | REG_BG2PD, 0x100, 0);
cpu->memory.store32(cpu, BASE_IO | 0x28, 0, 0);
cpu->memory.store32(cpu, BASE_IO | 0x2C, 0, 0);
cpu->memory.store16(cpu, BASE_IO | REG_BG3PA, 0x100, 0);
cpu->memory.store16(cpu, BASE_IO | REG_BG3PB, 0, 0);
cpu->memory.store16(cpu, BASE_IO | REG_BG3PC, 0, 0);
cpu->memory.store16(cpu, BASE_IO | REG_BG3PD, 0x100, 0);
cpu->memory.store32(cpu, BASE_IO | 0x38, 0, 0);
cpu->memory.store32(cpu, BASE_IO | 0x3C, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x40, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x42, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x44, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x46, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x48, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x4A, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x4C, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x50, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x52, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x54, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0xB0, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0xB2, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0xB4, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0xB6, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0xB8, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0xBA, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0xBC, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0xBE, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0xC0, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0xC2, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0xC4, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0xC6, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0xC8, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0xCA, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0xCC, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0xCE, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0xD0, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0xD2, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0xD4, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0xD6, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0xD8, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0xDA, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0xDC, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0xDE, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x100, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x102, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x104, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x106, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x108, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x10A, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x10C, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x10E, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x200, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x202, 0xFFFF, 0);
cpu->memory.store16(cpu, BASE_IO | 0x204, 0, 0);
cpu->memory.store16(cpu, BASE_IO | 0x208, 0, 0);
}
2013-04-28 05:54:41 +00:00
}
2013-05-04 08:20:42 +00:00
static void _BgAffineSet(struct GBA* gba) {
2014-04-20 07:19:55 +00:00
struct ARMCore* cpu = gba->cpu;
int i = cpu->gprs[2];
2013-05-04 08:20:42 +00:00
float ox, oy;
float cx, cy;
float sx, sy;
float theta;
2014-04-20 07:19:55 +00:00
int offset = cpu->gprs[0];
int destination = cpu->gprs[1];
2013-05-04 08:20:42 +00:00
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 ]
ox = (int32_t) cpu->memory.load32(cpu, offset, 0) / 256.f;
oy = (int32_t) cpu->memory.load32(cpu, offset + 4, 0) / 256.f;
cx = (int16_t) cpu->memory.load16(cpu, offset + 8, 0);
cy = (int16_t) cpu->memory.load16(cpu, offset + 10, 0);
sx = (int16_t) cpu->memory.load16(cpu, offset + 12, 0) / 256.f;
sy = (int16_t) cpu->memory.load16(cpu, offset + 14, 0) / 256.f;
theta = (cpu->memory.load16(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 07:19:55 +00:00
cpu->memory.store16(cpu, destination, a * 256, 0);
cpu->memory.store16(cpu, destination + 2, b * 256, 0);
cpu->memory.store16(cpu, destination + 4, c * 256, 0);
cpu->memory.store16(cpu, destination + 6, d * 256, 0);
cpu->memory.store32(cpu, destination + 8, rx * 256, 0);
cpu->memory.store32(cpu, destination + 12, ry * 256, 0);
2013-05-04 08:20:42 +00:00
destination += 16;
}
}
static void _ObjAffineSet(struct GBA* gba) {
2014-04-20 07:19:55 +00:00
struct ARMCore* cpu = gba->cpu;
int i = cpu->gprs[2];
float sx, sy;
float theta;
2014-04-20 07:19:55 +00:00
int offset = cpu->gprs[0];
int destination = cpu->gprs[1];
int diff = 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 ]
sx = (int16_t) cpu->memory.load16(cpu, offset, 0) / 256.f;
sy = (int16_t) cpu->memory.load16(cpu, offset + 2, 0) / 256.f;
theta = (cpu->memory.load16(cpu, offset + 4, 0) >> 8) / 128.f * M_PI;
2014-07-23 07:54:11 +00:00
offset += 8;
// Rotation
a = d = cosf(theta);
b = c = sinf(theta);
// Scale
a *= sx;
b *= -sx;
c *= sy;
d *= sy;
2014-04-20 07:19:55 +00:00
cpu->memory.store16(cpu, destination, a * 256, 0);
cpu->memory.store16(cpu, destination + diff, b * 256, 0);
cpu->memory.store16(cpu, destination + diff * 2, c * 256, 0);
cpu->memory.store16(cpu, destination + diff * 3, d * 256, 0);
destination += diff * 4;
}
}
2013-04-20 10:01:50 +00:00
static void _MidiKey2Freq(struct GBA* gba) {
2014-04-20 07:19:55 +00:00
struct ARMCore* cpu = gba->cpu;
uint32_t key = cpu->memory.load32(cpu, cpu->gprs[0] + 4, 0);
cpu->gprs[0] = key / powf(2, (180.f - cpu->gprs[1] - cpu->gprs[2] / 256.f) / 12.f);
2013-04-20 10:01:50 +00:00
}
2014-10-23 08:45:32 +00:00
static void _Div(struct GBA* gba, int32_t num, int32_t denom) {
struct ARMCore* cpu = gba->cpu;
if (denom != 0) {
div_t result = div(num, denom);
cpu->gprs[0] = result.quot;
cpu->gprs[1] = result.rem;
cpu->gprs[3] = abs(result.quot);
} else {
2016-02-08 13:25:46 +00:00
mLOG(GBA_BIOS, GAME_ERROR, "Attempting to divide %i by zero!", num);
// If abs(num) > 1, this should hang, but that would be painful to
// emulate in HLE, and no game will get into a state where it hangs...
cpu->gprs[0] = (num < 0) ? -1 : 1;
cpu->gprs[1] = num;
cpu->gprs[3] = 1;
}
}
2014-04-20 01:14:17 +00:00
void GBASwi16(struct ARMCore* cpu, int immediate) {
2014-04-20 07:19:55 +00:00
struct GBA* gba = (struct GBA*) cpu->master;
2016-02-08 13:25:46 +00:00
mLOG(GBA_BIOS, DEBUG, "SWI: %02X r0: %08X r1: %08X r2: %08X r3: %08X",
immediate, cpu->gprs[0], cpu->gprs[1], cpu->gprs[2], cpu->gprs[3]);
2014-07-05 01:17:34 +00:00
2013-10-09 05:36:19 +00:00
if (gba->memory.fullBios) {
2014-04-20 07:19:55 +00:00
ARMRaiseSWI(cpu);
2013-10-09 05:36:19 +00:00
return;
}
2013-04-14 20:21:21 +00:00
switch (immediate) {
2015-01-08 06:33:16 +00:00
case 0x0:
_SoftReset(gba);
break;
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;
2015-07-20 01:12:56 +00:00
case 0x3:
GBAStop(gba);
break;
2013-04-26 09:00:59 +00:00
case 0x05:
// VBlankIntrWait
// Fall through:
2013-04-26 09:00:59 +00:00
case 0x04:
// IntrWait
2014-04-20 07:19:55 +00:00
ARMRaiseSWI(cpu);
2013-04-26 09:00:59 +00:00
break;
2013-04-26 09:05:54 +00:00
case 0x6:
2014-10-23 08:45:32 +00:00
_Div(gba, cpu->gprs[0], cpu->gprs[1]);
2013-04-26 09:05:54 +00:00
break;
2013-04-28 02:58:21 +00:00
case 0x7:
2014-10-23 08:45:32 +00:00
_Div(gba, cpu->gprs[1], cpu->gprs[0]);
2013-04-28 02:58:21 +00:00
break;
2013-05-01 07:27:31 +00:00
case 0x8:
2015-11-18 03:47:05 +00:00
cpu->gprs[0] = sqrt((uint32_t) cpu->gprs[0]);
2013-05-01 07:27:31 +00:00
break;
2013-05-04 22:50:42 +00:00
case 0xA:
cpu->gprs[0] = (uint16_t) (atan2f(cpu->gprs[1] / 16384.f, cpu->gprs[0] / 16384.f) / (2 * M_PI) * 0x10001);
2013-05-04 22:50:42 +00:00
break;
2013-04-14 20:36:32 +00:00
case 0xB:
2013-04-17 06:52:53 +00:00
case 0xC:
if (cpu->gprs[0] >> BASE_OFFSET < REGION_WORKING_RAM) {
2016-02-08 13:25:46 +00:00
mLOG(GBA_BIOS, GAME_ERROR, "Cannot CpuSet from BIOS");
return;
}
2015-11-06 04:57:56 +00:00
if (cpu->gprs[0] & (cpu->gprs[2] & (1 << 26) ? 3 : 1)) {
2016-02-08 13:25:46 +00:00
mLOG(GBA_BIOS, GAME_ERROR, "Misaligned CpuSet source");
2015-11-06 04:57:56 +00:00
}
if (cpu->gprs[1] & (cpu->gprs[2] & (1 << 26) ? 3 : 1)) {
2016-02-08 13:25:46 +00:00
mLOG(GBA_BIOS, GAME_ERROR, "Misaligned CpuSet destination");
2015-11-06 04:57:56 +00:00
}
ARMRaiseSWI(cpu);
2013-04-17 06:52:53 +00:00
break;
2014-01-18 08:17:58 +00:00
case 0xD:
cpu->gprs[0] = GBA_BIOS_CHECKSUM;
cpu->gprs[1] = 1;
cpu->gprs[3] = SIZE_BIOS;
break;
2013-05-04 08:20:42 +00:00
case 0xE:
_BgAffineSet(gba);
break;
case 0xF:
_ObjAffineSet(gba);
break;
2013-04-20 09:52:10 +00:00
case 0x11:
case 0x12:
2014-04-20 07:19:55 +00:00
if (cpu->gprs[0] < BASE_WORKING_RAM) {
2016-02-08 13:25:46 +00:00
mLOG(GBA_BIOS, GAME_ERROR, "Bad LZ77 source");
break;
}
2014-04-20 07:19:55 +00:00
switch (cpu->gprs[1] >> BASE_OFFSET) {
default:
2016-02-08 13:25:46 +00:00
mLOG(GBA_BIOS, GAME_ERROR, "Bad LZ77 destination");
// Fall through
case REGION_WORKING_RAM:
case REGION_WORKING_IRAM:
case REGION_VRAM:
_unLz77(gba, immediate == 0x11 ? 1 : 2);
break;
2013-04-23 05:55:49 +00:00
}
2013-04-20 09:52:10 +00:00
break;
2013-10-12 20:32:23 +00:00
case 0x13:
2014-04-20 07:19:55 +00:00
if (cpu->gprs[0] < BASE_WORKING_RAM) {
2016-02-08 13:25:46 +00:00
mLOG(GBA_BIOS, GAME_ERROR, "Bad Huffman source");
break;
}
2014-04-20 07:19:55 +00:00
switch (cpu->gprs[1] >> BASE_OFFSET) {
default:
2016-02-08 13:25:46 +00:00
mLOG(GBA_BIOS, GAME_ERROR, "Bad Huffman destination");
// Fall through
case REGION_WORKING_RAM:
case REGION_WORKING_IRAM:
case REGION_VRAM:
_unHuffman(gba);
break;
2013-10-12 20:32:23 +00:00
}
break;
2013-05-12 01:22:23 +00:00
case 0x14:
case 0x15:
2014-04-20 07:19:55 +00:00
if (cpu->gprs[0] < BASE_WORKING_RAM) {
2016-02-08 13:25:46 +00:00
mLOG(GBA_BIOS, GAME_ERROR, "Bad RL source");
break;
}
2014-04-20 07:19:55 +00:00
switch (cpu->gprs[1] >> BASE_OFFSET) {
default:
2016-02-08 13:25:46 +00:00
mLOG(GBA_BIOS, GAME_ERROR, "Bad RL destination");
// Fall through
case REGION_WORKING_RAM:
case REGION_WORKING_IRAM:
case REGION_VRAM:
_unRl(gba, immediate == 0x14 ? 1 : 2);
break;
2013-05-12 01:22:23 +00:00
}
break;
case 0x16:
case 0x17:
case 0x18:
if (cpu->gprs[0] < BASE_WORKING_RAM) {
2016-02-08 13:25:46 +00:00
mLOG(GBA_BIOS, GAME_ERROR, "Bad UnFilter source");
break;
}
switch (cpu->gprs[1] >> BASE_OFFSET) {
default:
2016-02-08 13:25:46 +00:00
mLOG(GBA_BIOS, GAME_ERROR, "Bad UnFilter destination");
// Fall through
case REGION_WORKING_RAM:
case REGION_WORKING_IRAM:
case REGION_VRAM:
_unFilter(gba, immediate == 0x18 ? 2 : 1, immediate == 0x16 ? 1 : 2);
break;
}
break;
2015-07-20 01:14:09 +00:00
case 0x19:
// SoundBias is mostly meaningless here
2016-02-08 13:25:46 +00:00
mLOG(GBA_BIOS, STUB, "Stub software interrupt: SoundBias (19)");
2015-07-20 01:14:09 +00:00
break;
2013-04-20 10:01:50 +00:00
case 0x1F:
_MidiKey2Freq(gba);
break;
2013-04-14 20:21:21 +00:00
default:
2016-02-08 13:25:46 +00:00
mLOG(GBA_BIOS, STUB, "Stub software interrupt: %02X", immediate);
2013-04-14 20:21:21 +00:00
}
gba->memory.biosPrefetch = 0xE3A02004;
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;
}
static void _unLz77(struct GBA* gba, int width) {
2014-04-20 07:19:55 +00:00
struct ARMCore* cpu = gba->cpu;
uint32_t source = cpu->gprs[0];
uint32_t dest = cpu->gprs[1];
2014-04-20 07:19:55 +00:00
int remaining = (cpu->memory.load32(cpu, source, 0) & 0xFFFFFF00) >> 8;
2013-04-20 09:52:10 +00:00
// We assume the signature byte (0x10) is correct
2014-12-31 11:11:06 +00:00
int blockheader = 0; // Some compilers warn if this isn't set, even though it's trivially provably always set
source += 4;
2013-04-20 09:52:10 +00:00
int blocksRemaining = 0;
uint32_t disp;
2013-04-20 09:52:10 +00:00
int bytes;
int byte;
int halfword = 0;
2013-04-20 09:52:10 +00:00
while (remaining > 0) {
if (blocksRemaining) {
if (blockheader & 0x80) {
// Compressed
int block = cpu->memory.load8(cpu, source + 1, 0) | (cpu->memory.load8(cpu, source, 0) << 8);
source += 2;
disp = dest - (block & 0x0FFF) - 1;
bytes = (block >> 12) + 3;
2013-04-20 09:52:10 +00:00
while (bytes-- && remaining) {
--remaining;
if (width == 2) {
byte = (int16_t) cpu->memory.load16(cpu, disp & ~1, 0);
if (dest & 1) {
byte >>= (disp & 1) * 8;
halfword |= byte << 8;
cpu->memory.store16(cpu, dest ^ 1, halfword, 0);
} else {
byte >>= (disp & 1) * 8;
halfword = byte & 0xFF;
}
} else {
byte = cpu->memory.load8(cpu, disp, 0);
cpu->memory.store8(cpu, dest, byte, 0);
}
++disp;
++dest;
2013-04-20 09:52:10 +00:00
}
} else {
// Uncompressed
byte = cpu->memory.load8(cpu, source, 0);
++source;
if (width == 2) {
if (dest & 1) {
halfword |= byte << 8;
cpu->memory.store16(cpu, dest ^ 1, halfword, 0);
} else {
halfword = byte;
}
} else {
cpu->memory.store8(cpu, dest, byte, 0);
}
++dest;
2013-04-20 09:52:10 +00:00
--remaining;
}
blockheader <<= 1;
--blocksRemaining;
} else {
blockheader = cpu->memory.load8(cpu, source, 0);
++source;
2013-04-20 09:52:10 +00:00
blocksRemaining = 8;
}
}
cpu->gprs[0] = source;
cpu->gprs[1] = dest;
cpu->gprs[3] = 0;
2013-04-20 09:52:10 +00:00
}
2013-05-12 01:22:23 +00:00
2014-10-09 10:02:35 +00:00
DECL_BITFIELD(HuffmanNode, uint8_t);
DECL_BITS(HuffmanNode, Offset, 0, 6);
DECL_BIT(HuffmanNode, RTerm, 6);
DECL_BIT(HuffmanNode, LTerm, 7);
static void _unHuffman(struct GBA* gba) {
2014-04-20 07:19:55 +00:00
struct ARMCore* cpu = gba->cpu;
uint32_t source = cpu->gprs[0] & 0xFFFFFFFC;
uint32_t dest = cpu->gprs[1];
2014-04-20 07:19:55 +00:00
uint32_t header = cpu->memory.load32(cpu, source, 0);
2013-10-12 20:32:23 +00:00
int remaining = header >> 8;
int bits = header & 0xF;
if (bits == 0) {
2016-02-12 07:50:58 +00:00
mLOG(GBA_BIOS, GAME_ERROR, "Invalid Huffman bits");
bits = 8;
}
if (32 % bits || bits == 1) {
2016-02-12 07:50:58 +00:00
mLOG(GBA_BIOS, STUB, "Unimplemented unaligned Huffman");
2013-10-12 20:32:23 +00:00
return;
}
// We assume the signature byte (0x20) is correct
int treesize = (cpu->memory.load8(cpu, source + 4, 0) << 1) + 1;
2013-10-12 20:32:23 +00:00
int block = 0;
uint32_t treeBase = source + 5;
source += 5 + treesize;
2013-10-12 20:32:23 +00:00
uint32_t nPointer = treeBase;
2014-10-09 10:02:35 +00:00
HuffmanNode node;
2013-10-12 20:32:23 +00:00
int bitsRemaining;
int readBits;
int bitsSeen = 0;
2014-10-09 10:02:35 +00:00
node = cpu->memory.load8(cpu, nPointer, 0);
2013-10-12 20:32:23 +00:00
while (remaining > 0) {
uint32_t bitstream = cpu->memory.load32(cpu, source, 0);
source += 4;
for (bitsRemaining = 32; bitsRemaining > 0 && remaining > 0; --bitsRemaining, bitstream <<= 1) {
2014-10-09 10:02:35 +00:00
uint32_t next = (nPointer & ~1) + HuffmanNodeGetOffset(node) * 2 + 2;
2013-10-12 20:32:23 +00:00
if (bitstream & 0x80000000) {
// Go right
2014-10-09 10:02:35 +00:00
if (HuffmanNodeIsRTerm(node)) {
2014-04-20 07:19:55 +00:00
readBits = cpu->memory.load8(cpu, next + 1, 0);
2013-10-12 20:32:23 +00:00
} else {
nPointer = next + 1;
2014-10-09 10:02:35 +00:00
node = cpu->memory.load8(cpu, nPointer, 0);
2013-10-12 20:32:23 +00:00
continue;
}
} else {
// Go left
2014-10-09 10:02:35 +00:00
if (HuffmanNodeIsLTerm(node)) {
2014-04-20 07:19:55 +00:00
readBits = cpu->memory.load8(cpu, next, 0);
2013-10-12 20:32:23 +00:00
} else {
nPointer = next;
2014-10-09 10:02:35 +00:00
node = cpu->memory.load8(cpu, nPointer, 0);
2013-10-12 20:32:23 +00:00
continue;
}
}
block |= (readBits & ((1 << bits) - 1)) << bitsSeen;
bitsSeen += bits;
nPointer = treeBase;
2014-10-09 10:02:35 +00:00
node = cpu->memory.load8(cpu, nPointer, 0);
2013-10-12 20:32:23 +00:00
if (bitsSeen == 32) {
bitsSeen = 0;
cpu->memory.store32(cpu, dest, block, 0);
dest += 4;
2013-10-12 20:32:23 +00:00
remaining -= 4;
block = 0;
}
}
}
cpu->gprs[0] = source;
cpu->gprs[1] = dest;
2013-10-12 20:32:23 +00:00
}
static void _unRl(struct GBA* gba, int width) {
2014-04-20 07:19:55 +00:00
struct ARMCore* cpu = gba->cpu;
uint32_t source = cpu->gprs[0];
int remaining = (cpu->memory.load32(cpu, source & 0xFFFFFFFC, 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;
source += 4;
uint32_t dest = cpu->gprs[1];
2014-12-31 11:11:06 +00:00
int halfword = 0;
2013-05-12 01:22:23 +00:00
while (remaining > 0) {
blockheader = cpu->memory.load8(cpu, source, 0);
++source;
2013-05-12 01:22:23 +00:00
if (blockheader & 0x80) {
// Compressed
blockheader &= 0x7F;
blockheader += 3;
block = cpu->memory.load8(cpu, source, 0);
++source;
2013-05-12 01:22:23 +00:00
while (blockheader-- && remaining) {
--remaining;
if (width == 2) {
if (dest & 1) {
halfword |= block << 8;
cpu->memory.store16(cpu, dest ^ 1, halfword, 0);
} else {
halfword = block;
}
} else {
cpu->memory.store8(cpu, dest, block, 0);
}
++dest;
2013-05-12 01:22:23 +00:00
}
} else {
// Uncompressed
blockheader++;
while (blockheader-- && remaining) {
--remaining;
int byte = cpu->memory.load8(cpu, source, 0);
++source;
if (width == 2) {
if (dest & 1) {
halfword |= byte << 8;
cpu->memory.store16(cpu, dest ^ 1, halfword, 0);
} else {
halfword = byte;
}
} else {
cpu->memory.store8(cpu, dest, byte, 0);
}
++dest;
2013-05-12 01:22:23 +00:00
}
}
}
if (width == 2) {
if (dest & 1) {
--padding;
++dest;
}
for (; padding > 0; padding -= 2, dest += 2) {
cpu->memory.store16(cpu, dest, 0, 0);
}
} else {
while (padding--) {
cpu->memory.store8(cpu, dest, 0, 0);
++dest;
}
2013-05-12 01:22:23 +00:00
}
cpu->gprs[0] = source;
cpu->gprs[1] = dest;
2013-05-12 01:22:23 +00:00
}
static void _unFilter(struct GBA* gba, int inwidth, int outwidth) {
struct ARMCore* cpu = gba->cpu;
uint32_t source = cpu->gprs[0] & 0xFFFFFFFC;
uint32_t dest = cpu->gprs[1];
uint32_t header = cpu->memory.load32(cpu, source, 0);
int remaining = header >> 8;
// We assume the signature nybble (0x8) is correct
uint16_t halfword = 0;
uint16_t old = 0;
source += 4;
while (remaining > 0) {
uint16_t new;
if (inwidth == 1) {
new = cpu->memory.load8(cpu, source, 0);
} else {
new = cpu->memory.load16(cpu, source, 0);
}
new += old;
if (outwidth > inwidth) {
halfword >>= 8;
halfword |= (new << 8);
if (source & 1) {
cpu->memory.store16(cpu, dest, halfword, 0);
dest += outwidth;
remaining -= outwidth;
}
} else if (outwidth == 1) {
cpu->memory.store8(cpu, dest, new, 0);
dest += outwidth;
remaining -= outwidth;
} else {
cpu->memory.store16(cpu, dest, new, 0);
dest += outwidth;
remaining -= outwidth;
}
old = new;
source += inwidth;
}
cpu->gprs[0] = source;
cpu->gprs[1] = dest;
}