2013-04-05 09:17:22 +00:00
|
|
|
#include "gba.h"
|
|
|
|
|
2013-04-14 20:21:21 +00:00
|
|
|
#include "gba-bios.h"
|
2013-04-16 14:42:20 +00:00
|
|
|
#include "gba-io.h"
|
2013-09-30 08:42:31 +00:00
|
|
|
#include "gba-thread.h"
|
2014-01-15 08:24:06 +00:00
|
|
|
#include "memory.h"
|
2013-04-14 20:21:21 +00:00
|
|
|
|
2013-04-13 20:50:41 +00:00
|
|
|
#include "debugger.h"
|
|
|
|
|
2013-04-16 05:18:28 +00:00
|
|
|
#include <limits.h>
|
2013-04-11 05:52:46 +00:00
|
|
|
#include <stdio.h>
|
2013-04-13 20:50:41 +00:00
|
|
|
#include <stdlib.h>
|
2013-04-20 05:26:44 +00:00
|
|
|
#include <string.h>
|
2013-04-28 03:25:31 +00:00
|
|
|
#include <sys/stat.h>
|
2013-04-14 11:08:06 +00:00
|
|
|
|
2013-10-02 06:56:10 +00:00
|
|
|
const uint32_t GBA_ARM7TDMI_FREQUENCY = 0x1000000;
|
|
|
|
|
2013-04-14 20:21:21 +00:00
|
|
|
enum {
|
|
|
|
SP_BASE_SYSTEM = 0x03FFFF00,
|
|
|
|
SP_BASE_IRQ = 0x03FFFFA0,
|
|
|
|
SP_BASE_SUPERVISOR = 0x03FFFFE0
|
|
|
|
};
|
2013-04-14 18:57:39 +00:00
|
|
|
|
2013-10-21 01:08:18 +00:00
|
|
|
struct GBACartridgeOverride {
|
2014-01-31 10:05:01 +00:00
|
|
|
const char id[4];
|
2013-10-21 01:08:18 +00:00
|
|
|
enum SavedataType type;
|
|
|
|
int gpio;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct GBACartridgeOverride _overrides[] = {
|
2013-10-22 04:42:28 +00:00
|
|
|
// Boktai: The Sun is in Your Hand
|
2014-01-31 10:05:01 +00:00
|
|
|
{ "U3IE", SAVEDATA_EEPROM, GPIO_RTC | GPIO_LIGHT_SENSOR },
|
|
|
|
{ "U3IP", SAVEDATA_EEPROM, GPIO_RTC | GPIO_LIGHT_SENSOR },
|
2013-10-22 04:42:28 +00:00
|
|
|
|
|
|
|
// Boktai 2: Solar Boy Django
|
2014-01-31 10:05:01 +00:00
|
|
|
{ "U32E", SAVEDATA_EEPROM, GPIO_RTC | GPIO_LIGHT_SENSOR },
|
|
|
|
{ "U32P", SAVEDATA_EEPROM, GPIO_RTC | GPIO_LIGHT_SENSOR },
|
2013-10-22 04:42:28 +00:00
|
|
|
|
2013-10-22 07:18:56 +00:00
|
|
|
// Drill Dozer
|
2014-01-31 10:05:01 +00:00
|
|
|
{ "V49J", SAVEDATA_SRAM, GPIO_RUMBLE },
|
|
|
|
{ "V49E", SAVEDATA_SRAM, GPIO_RUMBLE },
|
2013-10-22 07:18:56 +00:00
|
|
|
|
2013-10-22 04:42:28 +00:00
|
|
|
// Pokemon Ruby
|
2014-01-31 10:05:01 +00:00
|
|
|
{ "AXVJ", SAVEDATA_FLASH1M, GPIO_RTC },
|
|
|
|
{ "AXVE", SAVEDATA_FLASH1M, GPIO_RTC },
|
|
|
|
{ "AXVP", SAVEDATA_FLASH1M, GPIO_RTC },
|
|
|
|
{ "AXVI", SAVEDATA_FLASH1M, GPIO_RTC },
|
|
|
|
{ "AXVS", SAVEDATA_FLASH1M, GPIO_RTC },
|
|
|
|
{ "AXVD", SAVEDATA_FLASH1M, GPIO_RTC },
|
|
|
|
{ "AXVF", SAVEDATA_FLASH1M, GPIO_RTC },
|
2013-10-22 04:42:28 +00:00
|
|
|
|
|
|
|
// Pokemon Sapphire
|
2014-01-31 10:05:01 +00:00
|
|
|
{ "AXPJ", SAVEDATA_FLASH1M, GPIO_RTC },
|
|
|
|
{ "AXPE", SAVEDATA_FLASH1M, GPIO_RTC },
|
|
|
|
{ "AXPP", SAVEDATA_FLASH1M, GPIO_RTC },
|
|
|
|
{ "AXPI", SAVEDATA_FLASH1M, GPIO_RTC },
|
|
|
|
{ "AXPS", SAVEDATA_FLASH1M, GPIO_RTC },
|
|
|
|
{ "AXPD", SAVEDATA_FLASH1M, GPIO_RTC },
|
|
|
|
{ "AXPF", SAVEDATA_FLASH1M, GPIO_RTC },
|
2013-10-22 04:42:28 +00:00
|
|
|
|
|
|
|
// Pokemon Emerald
|
2014-01-31 10:05:01 +00:00
|
|
|
{ "BPEJ", SAVEDATA_FLASH1M, GPIO_RTC },
|
|
|
|
{ "BPEE", SAVEDATA_FLASH1M, GPIO_RTC },
|
|
|
|
{ "BPEP", SAVEDATA_FLASH1M, GPIO_RTC },
|
|
|
|
{ "BPEI", SAVEDATA_FLASH1M, GPIO_RTC },
|
|
|
|
{ "BPES", SAVEDATA_FLASH1M, GPIO_RTC },
|
|
|
|
{ "BPED", SAVEDATA_FLASH1M, GPIO_RTC },
|
|
|
|
{ "BPEF", SAVEDATA_FLASH1M, GPIO_RTC },
|
2013-10-22 04:42:28 +00:00
|
|
|
|
2013-11-18 08:34:54 +00:00
|
|
|
// Pokemon FireRed
|
2014-01-31 10:05:01 +00:00
|
|
|
{ "BPRJ", SAVEDATA_FLASH1M, GPIO_NONE },
|
|
|
|
{ "BPRE", SAVEDATA_FLASH1M, GPIO_NONE },
|
|
|
|
{ "BPRP", SAVEDATA_FLASH1M, GPIO_NONE },
|
2013-11-18 08:34:54 +00:00
|
|
|
|
|
|
|
// Pokemon LeafGreen
|
2014-01-31 10:05:01 +00:00
|
|
|
{ "BPGJ", SAVEDATA_FLASH1M, GPIO_NONE },
|
|
|
|
{ "BPGE", SAVEDATA_FLASH1M, GPIO_NONE },
|
|
|
|
{ "BPGP", SAVEDATA_FLASH1M, GPIO_NONE },
|
2013-11-18 08:34:54 +00:00
|
|
|
|
2013-10-22 04:42:28 +00:00
|
|
|
// RockMan EXE 4.5 - Real Operation
|
2014-01-31 10:05:01 +00:00
|
|
|
{ "BR4J", SAVEDATA_FLASH512, GPIO_RTC },
|
2013-10-22 04:42:28 +00:00
|
|
|
|
|
|
|
// Super Mario Advance 4
|
2014-01-31 10:05:01 +00:00
|
|
|
{ "AX4J", SAVEDATA_FLASH1M, GPIO_NONE },
|
|
|
|
{ "AX4E", SAVEDATA_FLASH1M, GPIO_NONE },
|
|
|
|
{ "AX4P", SAVEDATA_FLASH1M, GPIO_NONE },
|
2013-10-22 04:42:28 +00:00
|
|
|
|
|
|
|
// Wario Ware Twisted
|
2014-01-31 10:05:01 +00:00
|
|
|
{ "RWZJ", SAVEDATA_SRAM, GPIO_RUMBLE | GPIO_GYRO },
|
|
|
|
{ "RWZE", SAVEDATA_SRAM, GPIO_RUMBLE | GPIO_GYRO },
|
|
|
|
{ "RWZP", SAVEDATA_SRAM, GPIO_RUMBLE | GPIO_GYRO },
|
2013-10-22 04:42:28 +00:00
|
|
|
|
2014-01-31 10:05:01 +00:00
|
|
|
{ { 0, 0, 0, 0 }, 0, 0 }
|
2013-10-12 05:03:27 +00:00
|
|
|
};
|
|
|
|
|
2013-04-16 05:18:28 +00:00
|
|
|
static void GBAProcessEvents(struct ARMBoard* board);
|
2013-04-20 05:26:44 +00:00
|
|
|
static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles);
|
2013-04-11 05:52:46 +00:00
|
|
|
static void GBAHitStub(struct ARMBoard* board, uint32_t opcode);
|
2014-01-18 08:39:51 +00:00
|
|
|
static void GBAIllegal(struct ARMBoard* board, uint32_t opcode);
|
2013-04-10 05:20:35 +00:00
|
|
|
|
2013-10-12 05:03:27 +00:00
|
|
|
static void _checkOverrides(struct GBA* gba, uint32_t code);
|
|
|
|
|
2013-04-05 09:17:22 +00:00
|
|
|
void GBAInit(struct GBA* gba) {
|
2013-10-12 07:13:11 +00:00
|
|
|
gba->debugger = 0;
|
2013-10-18 15:36:05 +00:00
|
|
|
gba->savefile = 0;
|
2013-04-06 11:34:19 +00:00
|
|
|
|
2013-04-05 09:17:22 +00:00
|
|
|
ARMInit(&gba->cpu);
|
2013-04-06 11:34:19 +00:00
|
|
|
|
2013-04-06 11:20:44 +00:00
|
|
|
gba->memory.p = gba;
|
2013-04-05 09:17:22 +00:00
|
|
|
GBAMemoryInit(&gba->memory);
|
2013-04-07 08:46:28 +00:00
|
|
|
ARMAssociateMemory(&gba->cpu, &gba->memory.d);
|
2013-04-07 20:25:45 +00:00
|
|
|
|
2013-04-13 20:56:29 +00:00
|
|
|
gba->board.p = gba;
|
2013-04-07 20:25:45 +00:00
|
|
|
GBABoardInit(&gba->board);
|
|
|
|
ARMAssociateBoard(&gba->cpu, &gba->board.d);
|
|
|
|
|
2013-04-16 06:01:40 +00:00
|
|
|
gba->video.p = gba;
|
|
|
|
GBAVideoInit(&gba->video);
|
|
|
|
|
2013-09-30 08:23:58 +00:00
|
|
|
gba->audio.p = gba;
|
|
|
|
GBAAudioInit(&gba->audio);
|
|
|
|
|
2013-04-19 07:04:50 +00:00
|
|
|
GBAIOInit(gba);
|
|
|
|
|
2013-10-18 15:42:15 +00:00
|
|
|
gba->timersEnabled = 0;
|
2013-04-20 05:26:44 +00:00
|
|
|
memset(gba->timers, 0, sizeof(gba->timers));
|
|
|
|
|
2013-04-17 02:41:09 +00:00
|
|
|
gba->springIRQ = 0;
|
2013-04-21 07:35:21 +00:00
|
|
|
gba->keySource = 0;
|
2013-10-21 09:54:52 +00:00
|
|
|
gba->rotationSource = 0;
|
2013-10-22 07:26:09 +00:00
|
|
|
gba->rumble = 0;
|
2013-04-17 02:41:09 +00:00
|
|
|
|
2014-01-30 06:44:40 +00:00
|
|
|
gba->logLevel = GBA_LOG_INFO | GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL;
|
2013-09-22 22:01:23 +00:00
|
|
|
|
2014-01-21 01:18:12 +00:00
|
|
|
gba->biosChecksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
|
|
|
|
|
2013-04-07 20:25:45 +00:00
|
|
|
ARMReset(&gba->cpu);
|
2013-04-05 09:17:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GBADeinit(struct GBA* gba) {
|
|
|
|
GBAMemoryDeinit(&gba->memory);
|
2013-05-07 08:00:18 +00:00
|
|
|
GBAVideoDeinit(&gba->video);
|
2013-10-09 07:44:31 +00:00
|
|
|
GBAAudioDeinit(&gba->audio);
|
2013-04-05 09:17:22 +00:00
|
|
|
}
|
|
|
|
|
2013-04-07 20:25:45 +00:00
|
|
|
void GBABoardInit(struct GBABoard* board) {
|
|
|
|
board->d.reset = GBABoardReset;
|
2013-04-16 05:18:28 +00:00
|
|
|
board->d.processEvents = GBAProcessEvents;
|
2013-04-14 18:57:39 +00:00
|
|
|
board->d.swi16 = GBASwi16;
|
|
|
|
board->d.swi32 = GBASwi32;
|
2014-01-18 08:39:51 +00:00
|
|
|
board->d.hitIllegal = GBAIllegal;
|
2013-10-09 08:56:59 +00:00
|
|
|
board->d.readCPSR = GBATestIRQ;
|
2013-04-11 05:52:46 +00:00
|
|
|
board->d.hitStub = GBAHitStub;
|
2013-04-07 20:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GBABoardReset(struct ARMBoard* board) {
|
|
|
|
struct ARMCore* cpu = board->cpu;
|
|
|
|
ARMSetPrivilegeMode(cpu, MODE_IRQ);
|
|
|
|
cpu->gprs[ARM_SP] = SP_BASE_IRQ;
|
|
|
|
ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
|
|
|
|
cpu->gprs[ARM_SP] = SP_BASE_SUPERVISOR;
|
|
|
|
ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
|
|
|
|
cpu->gprs[ARM_SP] = SP_BASE_SYSTEM;
|
|
|
|
}
|
|
|
|
|
2013-04-16 05:18:28 +00:00
|
|
|
static void GBAProcessEvents(struct ARMBoard* board) {
|
2014-01-22 05:07:15 +00:00
|
|
|
do {
|
|
|
|
struct GBABoard* gbaBoard = (struct GBABoard*) board;
|
|
|
|
int32_t cycles = board->cpu->cycles;
|
|
|
|
int32_t nextEvent = INT_MAX;
|
|
|
|
int32_t testEvent;
|
|
|
|
|
|
|
|
if (gbaBoard->p->springIRQ) {
|
|
|
|
ARMRaiseIRQ(&gbaBoard->p->cpu);
|
|
|
|
gbaBoard->p->springIRQ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
testEvent = GBAVideoProcessEvents(&gbaBoard->p->video, cycles);
|
|
|
|
if (testEvent < nextEvent) {
|
|
|
|
nextEvent = testEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
testEvent = GBAAudioProcessEvents(&gbaBoard->p->audio, cycles);
|
|
|
|
if (testEvent < nextEvent) {
|
|
|
|
nextEvent = testEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
testEvent = GBATimersProcessEvents(gbaBoard->p, cycles);
|
|
|
|
if (testEvent < nextEvent) {
|
|
|
|
nextEvent = testEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
testEvent = GBAMemoryRunDMAs(&gbaBoard->p->memory, cycles);
|
|
|
|
if (testEvent < nextEvent) {
|
|
|
|
nextEvent = testEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
board->cpu->cycles -= cycles;
|
|
|
|
board->cpu->nextEvent = nextEvent;
|
|
|
|
} while (board->cpu->cycles >= board->cpu->nextEvent);
|
2013-04-16 05:18:28 +00:00
|
|
|
}
|
|
|
|
|
2013-04-20 05:26:44 +00:00
|
|
|
static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles) {
|
|
|
|
int32_t nextEvent = INT_MAX;
|
|
|
|
if (gba->timersEnabled) {
|
|
|
|
struct GBATimer* timer;
|
|
|
|
struct GBATimer* nextTimer;
|
|
|
|
|
|
|
|
timer = &gba->timers[0];
|
|
|
|
if (timer->enable) {
|
|
|
|
timer->nextEvent -= cycles;
|
2013-04-20 06:01:04 +00:00
|
|
|
timer->lastEvent -= cycles;
|
2013-04-20 05:26:44 +00:00
|
|
|
if (timer->nextEvent <= 0) {
|
2013-04-20 06:01:04 +00:00
|
|
|
timer->lastEvent = timer->nextEvent;
|
2013-04-20 05:26:44 +00:00
|
|
|
timer->nextEvent += timer->overflowInterval;
|
|
|
|
gba->memory.io[REG_TM0CNT_LO >> 1] = timer->reload;
|
|
|
|
timer->oldReload = timer->reload;
|
|
|
|
|
|
|
|
if (timer->doIrq) {
|
|
|
|
GBARaiseIRQ(gba, IRQ_TIMER0);
|
|
|
|
}
|
|
|
|
|
2013-10-02 07:46:51 +00:00
|
|
|
if (gba->audio.enable) {
|
|
|
|
if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == 0) {
|
2014-01-22 05:07:15 +00:00
|
|
|
GBAAudioSampleFIFO(&gba->audio, 0, timer->lastEvent);
|
2013-10-02 07:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == 0) {
|
2014-01-22 05:07:15 +00:00
|
|
|
GBAAudioSampleFIFO(&gba->audio, 1, timer->lastEvent);
|
2013-10-02 07:46:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-20 05:26:44 +00:00
|
|
|
nextTimer = &gba->timers[1];
|
|
|
|
if (nextTimer->countUp) {
|
|
|
|
++gba->memory.io[REG_TM1CNT_LO >> 1];
|
|
|
|
if (!gba->memory.io[REG_TM1CNT_LO >> 1]) {
|
|
|
|
nextTimer->nextEvent = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nextEvent = timer->nextEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
timer = &gba->timers[1];
|
|
|
|
if (timer->enable) {
|
|
|
|
timer->nextEvent -= cycles;
|
2013-04-20 06:01:04 +00:00
|
|
|
timer->lastEvent -= cycles;
|
2013-04-20 05:26:44 +00:00
|
|
|
if (timer->nextEvent <= 0) {
|
2013-04-20 06:01:04 +00:00
|
|
|
timer->lastEvent = timer->nextEvent;
|
2013-04-20 05:26:44 +00:00
|
|
|
timer->nextEvent += timer->overflowInterval;
|
|
|
|
gba->memory.io[REG_TM1CNT_LO >> 1] = timer->reload;
|
|
|
|
timer->oldReload = timer->reload;
|
|
|
|
|
|
|
|
if (timer->doIrq) {
|
|
|
|
GBARaiseIRQ(gba, IRQ_TIMER1);
|
|
|
|
}
|
|
|
|
|
2013-10-02 07:46:51 +00:00
|
|
|
if (gba->audio.enable) {
|
|
|
|
if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == 1) {
|
2014-01-22 05:07:15 +00:00
|
|
|
GBAAudioSampleFIFO(&gba->audio, 0, timer->lastEvent);
|
2013-10-02 07:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == 1) {
|
2014-01-22 05:07:15 +00:00
|
|
|
GBAAudioSampleFIFO(&gba->audio, 1, timer->lastEvent);
|
2013-10-02 07:46:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-20 05:26:44 +00:00
|
|
|
if (timer->countUp) {
|
|
|
|
timer->nextEvent = INT_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
nextTimer = &gba->timers[2];
|
|
|
|
if (nextTimer->countUp) {
|
|
|
|
++gba->memory.io[REG_TM2CNT_LO >> 1];
|
|
|
|
if (!gba->memory.io[REG_TM2CNT_LO >> 1]) {
|
|
|
|
nextTimer->nextEvent = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (timer->nextEvent < nextEvent) {
|
|
|
|
nextEvent = timer->nextEvent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
timer = &gba->timers[2];
|
|
|
|
if (timer->enable) {
|
|
|
|
timer->nextEvent -= cycles;
|
2013-04-20 06:01:04 +00:00
|
|
|
timer->lastEvent -= cycles;
|
2013-04-20 05:26:44 +00:00
|
|
|
nextEvent = timer->nextEvent;
|
|
|
|
if (timer->nextEvent <= 0) {
|
2013-04-20 06:01:04 +00:00
|
|
|
timer->lastEvent = timer->nextEvent;
|
2013-04-20 05:26:44 +00:00
|
|
|
timer->nextEvent += timer->overflowInterval;
|
|
|
|
gba->memory.io[REG_TM2CNT_LO >> 1] = timer->reload;
|
|
|
|
timer->oldReload = timer->reload;
|
|
|
|
|
|
|
|
if (timer->doIrq) {
|
|
|
|
GBARaiseIRQ(gba, IRQ_TIMER2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timer->countUp) {
|
|
|
|
timer->nextEvent = INT_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
nextTimer = &gba->timers[3];
|
|
|
|
if (nextTimer->countUp) {
|
|
|
|
++gba->memory.io[REG_TM3CNT_LO >> 1];
|
|
|
|
if (!gba->memory.io[REG_TM3CNT_LO >> 1]) {
|
|
|
|
nextTimer->nextEvent = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (timer->nextEvent < nextEvent) {
|
|
|
|
nextEvent = timer->nextEvent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
timer = &gba->timers[3];
|
|
|
|
if (timer->enable) {
|
|
|
|
timer->nextEvent -= cycles;
|
2013-04-20 06:01:04 +00:00
|
|
|
timer->lastEvent -= cycles;
|
2013-04-20 05:26:44 +00:00
|
|
|
nextEvent = timer->nextEvent;
|
|
|
|
if (timer->nextEvent <= 0) {
|
2013-04-20 06:01:04 +00:00
|
|
|
timer->lastEvent = timer->nextEvent;
|
2013-04-20 05:26:44 +00:00
|
|
|
timer->nextEvent += timer->overflowInterval;
|
|
|
|
gba->memory.io[REG_TM3CNT_LO >> 1] = timer->reload;
|
|
|
|
timer->oldReload = timer->reload;
|
|
|
|
|
|
|
|
if (timer->doIrq) {
|
|
|
|
GBARaiseIRQ(gba, IRQ_TIMER3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timer->countUp) {
|
|
|
|
timer->nextEvent = INT_MAX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (timer->nextEvent < nextEvent) {
|
|
|
|
nextEvent = timer->nextEvent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nextEvent;
|
|
|
|
}
|
|
|
|
|
2013-04-13 20:50:41 +00:00
|
|
|
void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger) {
|
|
|
|
ARMDebuggerInit(debugger, &gba->cpu);
|
|
|
|
gba->debugger = debugger;
|
|
|
|
}
|
|
|
|
|
2014-02-02 04:24:38 +00:00
|
|
|
void GBADetachDebugger(struct GBA* gba) {
|
|
|
|
ARMDebuggerDeinit(gba->debugger);
|
|
|
|
gba->debugger = 0;
|
|
|
|
}
|
|
|
|
|
2013-09-22 23:45:19 +00:00
|
|
|
void GBALoadROM(struct GBA* gba, int fd, const char* fname) {
|
2013-04-28 03:25:31 +00:00
|
|
|
struct stat info;
|
2014-01-15 08:24:06 +00:00
|
|
|
gba->memory.rom = fileMemoryMap(fd, SIZE_CART0, MEMORY_READ);
|
2013-09-22 23:45:19 +00:00
|
|
|
gba->activeFile = fname;
|
2013-04-28 03:25:31 +00:00
|
|
|
fstat(fd, &info);
|
|
|
|
gba->memory.romSize = info.st_size;
|
2013-10-18 15:36:05 +00:00
|
|
|
if (gba->savefile) {
|
|
|
|
GBASavedataInit(&gba->memory.savedata, gba->savefile);
|
|
|
|
}
|
2013-10-21 01:08:18 +00:00
|
|
|
GBAGPIOInit(&gba->memory.gpio, &((uint16_t*) gba->memory.rom)[GPIO_REG_DATA >> 1]);
|
2013-10-12 05:03:27 +00:00
|
|
|
_checkOverrides(gba, ((struct GBACartridge*) gba->memory.rom)->id);
|
2013-04-07 08:46:28 +00:00
|
|
|
// TODO: error check
|
|
|
|
}
|
|
|
|
|
2013-10-09 05:36:19 +00:00
|
|
|
void GBALoadBIOS(struct GBA* gba, int fd) {
|
2014-01-15 08:24:06 +00:00
|
|
|
gba->memory.bios = fileMemoryMap(fd, SIZE_BIOS, MEMORY_READ);
|
2013-10-09 05:36:19 +00:00
|
|
|
gba->memory.fullBios = 1;
|
2014-01-18 08:17:58 +00:00
|
|
|
uint32_t checksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
|
|
|
|
GBALog(gba, GBA_LOG_DEBUG, "BIOS Checksum: 0x%X", checksum);
|
|
|
|
if (checksum == GBA_BIOS_CHECKSUM) {
|
|
|
|
GBALog(gba, GBA_LOG_INFO, "Official GBA BIOS detected");
|
|
|
|
} else if (checksum == GBA_DS_BIOS_CHECKSUM) {
|
|
|
|
GBALog(gba, GBA_LOG_INFO, "Official GBA (DS) BIOS detected");
|
|
|
|
} else {
|
|
|
|
GBALog(gba, GBA_LOG_WARN, "BIOS checksum incorrect");
|
|
|
|
}
|
2014-01-21 01:18:12 +00:00
|
|
|
gba->biosChecksum = checksum;
|
2013-10-09 05:36:19 +00:00
|
|
|
if ((gba->cpu.gprs[ARM_PC] >> BASE_OFFSET) == BASE_BIOS) {
|
|
|
|
gba->memory.d.setActiveRegion(&gba->memory.d, gba->cpu.gprs[ARM_PC]);
|
|
|
|
}
|
|
|
|
// TODO: error check
|
|
|
|
}
|
|
|
|
|
2013-04-20 06:01:04 +00:00
|
|
|
void GBATimerUpdateRegister(struct GBA* gba, int timer) {
|
|
|
|
struct GBATimer* currentTimer = &gba->timers[timer];
|
|
|
|
if (currentTimer->enable && !currentTimer->countUp) {
|
|
|
|
gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu.cycles - currentTimer->lastEvent) >> currentTimer->prescaleBits);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t reload) {
|
|
|
|
gba->timers[timer].reload = reload;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t control) {
|
|
|
|
struct GBATimer* currentTimer = &gba->timers[timer];
|
|
|
|
GBATimerUpdateRegister(gba, timer);
|
|
|
|
|
|
|
|
int oldPrescale = currentTimer->prescaleBits;
|
|
|
|
switch (control & 0x0003) {
|
|
|
|
case 0x0000:
|
|
|
|
currentTimer->prescaleBits = 0;
|
|
|
|
break;
|
|
|
|
case 0x0001:
|
|
|
|
currentTimer->prescaleBits = 6;
|
|
|
|
break;
|
|
|
|
case 0x0002:
|
|
|
|
currentTimer->prescaleBits = 8;
|
|
|
|
break;
|
|
|
|
case 0x0003:
|
|
|
|
currentTimer->prescaleBits = 10;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
currentTimer->countUp = !!(control & 0x0004);
|
|
|
|
currentTimer->doIrq = !!(control & 0x0040);
|
|
|
|
currentTimer->overflowInterval = (0x10000 - currentTimer->reload) << currentTimer->prescaleBits;
|
|
|
|
int wasEnabled = currentTimer->enable;
|
|
|
|
currentTimer->enable = !!(control & 0x0080);
|
|
|
|
if (!wasEnabled && currentTimer->enable) {
|
|
|
|
if (!currentTimer->countUp) {
|
|
|
|
currentTimer->nextEvent = gba->cpu.cycles + currentTimer->overflowInterval;
|
|
|
|
} else {
|
|
|
|
currentTimer->nextEvent = INT_MAX;
|
|
|
|
}
|
|
|
|
gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->reload;
|
|
|
|
currentTimer->oldReload = currentTimer->reload;
|
|
|
|
gba->timersEnabled |= 1 << timer;
|
|
|
|
} else if (wasEnabled && !currentTimer->enable) {
|
|
|
|
if (!currentTimer->countUp) {
|
|
|
|
gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu.cycles - currentTimer->lastEvent) >> oldPrescale);
|
|
|
|
}
|
|
|
|
gba->timersEnabled &= ~(1 << timer);
|
|
|
|
} else if (currentTimer->prescaleBits != oldPrescale && !currentTimer->countUp) {
|
|
|
|
// FIXME: this might be before present
|
|
|
|
currentTimer->nextEvent = currentTimer->lastEvent + currentTimer->overflowInterval;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentTimer->nextEvent < gba->cpu.nextEvent) {
|
|
|
|
gba->cpu.nextEvent = currentTimer->nextEvent;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-04-16 14:42:20 +00:00
|
|
|
void GBAWriteIE(struct GBA* gba, uint16_t value) {
|
|
|
|
if (value & (1 << IRQ_SIO)) {
|
2013-09-22 22:01:23 +00:00
|
|
|
GBALog(gba, GBA_LOG_STUB, "SIO interrupts not implemented");
|
2013-04-16 14:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value & (1 << IRQ_KEYPAD)) {
|
2013-09-22 22:01:23 +00:00
|
|
|
GBALog(gba, GBA_LOG_STUB, "Keypad interrupts not implemented");
|
2013-04-16 14:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value & (1 << IRQ_GAMEPAK)) {
|
2013-09-22 22:01:23 +00:00
|
|
|
GBALog(gba, GBA_LOG_STUB, "Gamepak interrupts not implemented");
|
2013-04-16 14:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (gba->memory.io[REG_IME >> 1] && value & gba->memory.io[REG_IF >> 1]) {
|
2013-04-16 14:50:34 +00:00
|
|
|
ARMRaiseIRQ(&gba->cpu);
|
2013-04-16 14:42:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBAWriteIME(struct GBA* gba, uint16_t value) {
|
|
|
|
if (value && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
|
2013-04-16 14:50:34 +00:00
|
|
|
ARMRaiseIRQ(&gba->cpu);
|
2013-04-16 14:42:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-16 14:18:25 +00:00
|
|
|
void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq) {
|
2013-04-16 14:42:20 +00:00
|
|
|
gba->memory.io[REG_IF >> 1] |= 1 << irq;
|
|
|
|
|
|
|
|
if (gba->memory.io[REG_IME >> 1] && (gba->memory.io[REG_IE >> 1] & 1 << irq)) {
|
2013-04-16 14:50:34 +00:00
|
|
|
ARMRaiseIRQ(&gba->cpu);
|
2013-04-16 14:42:20 +00:00
|
|
|
}
|
2013-04-16 14:18:25 +00:00
|
|
|
}
|
|
|
|
|
2013-10-09 08:56:59 +00:00
|
|
|
void GBATestIRQ(struct ARMBoard* board) {
|
|
|
|
struct GBABoard* gbaBoard = (struct GBABoard*) board;
|
|
|
|
struct GBA* gba = gbaBoard->p;
|
2013-04-17 02:41:09 +00:00
|
|
|
if (gba->memory.io[REG_IME >> 1] && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
|
|
|
|
gba->springIRQ = 1;
|
2013-10-09 08:56:59 +00:00
|
|
|
gba->cpu.nextEvent = 0;
|
2013-04-17 02:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-15 07:16:34 +00:00
|
|
|
int GBAWaitForIRQ(struct GBA* gba) {
|
|
|
|
int irqs = gba->memory.io[REG_IF >> 1];
|
|
|
|
int newIRQs = 0;
|
|
|
|
gba->memory.io[REG_IF >> 1] = 0;
|
|
|
|
while (1) {
|
|
|
|
if (gba->cpu.nextEvent == INT_MAX) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
gba->cpu.cycles = gba->cpu.nextEvent;
|
|
|
|
GBAProcessEvents(&gba->board.d);
|
|
|
|
if (gba->memory.io[REG_IF >> 1]) {
|
|
|
|
newIRQs = gba->memory.io[REG_IF >> 1];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gba->memory.io[REG_IF >> 1] = newIRQs | irqs;
|
|
|
|
return newIRQs;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GBAHalt(struct GBA* gba) {
|
|
|
|
return GBAWaitForIRQ(gba);
|
2013-04-17 06:14:16 +00:00
|
|
|
}
|
|
|
|
|
2014-02-02 00:13:00 +00:00
|
|
|
static void _GBAVLog(struct GBA* gba, enum GBALogLevel level, const char* format, va_list args) {
|
2013-09-30 08:42:31 +00:00
|
|
|
if (!gba) {
|
|
|
|
struct GBAThread* threadContext = GBAThreadGetContext();
|
|
|
|
if (threadContext) {
|
|
|
|
gba = threadContext->gba;
|
|
|
|
}
|
|
|
|
}
|
2014-01-30 06:40:13 +00:00
|
|
|
|
|
|
|
if (gba && gba->logHandler) {
|
|
|
|
gba->logHandler(gba, level, format, args);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-30 06:44:40 +00:00
|
|
|
if (gba && !(level & gba->logLevel) && level != GBA_LOG_FATAL) {
|
2013-09-22 22:01:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-01-30 06:40:13 +00:00
|
|
|
|
2013-04-11 05:52:46 +00:00
|
|
|
vprintf(format, args);
|
|
|
|
printf("\n");
|
2014-01-30 06:44:40 +00:00
|
|
|
|
|
|
|
if (level == GBA_LOG_FATAL) {
|
|
|
|
abort();
|
|
|
|
}
|
2013-04-11 05:52:46 +00:00
|
|
|
}
|
|
|
|
|
2014-02-02 00:13:00 +00:00
|
|
|
void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
_GBAVLog(gba, level, format, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBADebuggerLogShim(struct ARMDebugger* debugger, enum DebuggerLogLevel level, const char* format, ...) {
|
2014-02-02 00:27:27 +00:00
|
|
|
struct GBABoard* gbaBoard = 0;
|
|
|
|
if (debugger->cpu && debugger->cpu->board) {
|
|
|
|
gbaBoard = (struct GBABoard*) debugger->cpu->board;
|
|
|
|
}
|
2014-02-02 00:13:00 +00:00
|
|
|
|
|
|
|
enum GBALogLevel gbaLevel;
|
|
|
|
switch (level) {
|
|
|
|
case DEBUGGER_LOG_DEBUG:
|
|
|
|
gbaLevel = GBA_LOG_DEBUG;
|
|
|
|
break;
|
|
|
|
case DEBUGGER_LOG_INFO:
|
|
|
|
gbaLevel = GBA_LOG_INFO;
|
|
|
|
break;
|
|
|
|
case DEBUGGER_LOG_WARN:
|
|
|
|
gbaLevel = GBA_LOG_WARN;
|
|
|
|
break;
|
|
|
|
case DEBUGGER_LOG_ERROR:
|
|
|
|
gbaLevel = GBA_LOG_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
2014-02-02 00:27:27 +00:00
|
|
|
_GBAVLog(gbaBoard ? gbaBoard->p : 0, gbaLevel, format, args);
|
2014-02-02 00:13:00 +00:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-11 05:52:46 +00:00
|
|
|
void GBAHitStub(struct ARMBoard* board, uint32_t opcode) {
|
2013-04-13 20:56:29 +00:00
|
|
|
struct GBABoard* gbaBoard = (struct GBABoard*) board;
|
2014-01-30 06:44:40 +00:00
|
|
|
enum GBALogLevel level = GBA_LOG_FATAL;
|
|
|
|
if (gbaBoard->p->debugger) {
|
|
|
|
level = GBA_LOG_STUB;
|
2014-02-01 08:27:53 +00:00
|
|
|
ARMDebuggerEnter(gbaBoard->p->debugger, DEBUGGER_ENTER_ILLEGAL_OP);
|
2013-04-13 20:56:29 +00:00
|
|
|
}
|
2014-01-30 06:44:40 +00:00
|
|
|
GBALog(gbaBoard->p, level, "Stub opcode: %08x", opcode);
|
2014-01-18 08:39:51 +00:00
|
|
|
}
|
2013-10-12 07:13:11 +00:00
|
|
|
|
2014-01-18 08:39:51 +00:00
|
|
|
void GBAIllegal(struct ARMBoard* board, uint32_t opcode) {
|
|
|
|
struct GBABoard* gbaBoard = (struct GBABoard*) board;
|
|
|
|
GBALog(gbaBoard->p, GBA_LOG_WARN, "Illegal opcode: %08x", opcode);
|
|
|
|
if (gbaBoard->p->debugger) {
|
2014-02-01 08:27:53 +00:00
|
|
|
ARMDebuggerEnter(gbaBoard->p->debugger, DEBUGGER_ENTER_ILLEGAL_OP);
|
2014-01-18 08:39:51 +00:00
|
|
|
}
|
2013-04-11 05:52:46 +00:00
|
|
|
}
|
2013-10-12 05:03:27 +00:00
|
|
|
|
|
|
|
void _checkOverrides(struct GBA* gba, uint32_t id) {
|
|
|
|
int i;
|
2014-01-31 10:05:01 +00:00
|
|
|
for (i = 0; _overrides[i].id[0]; ++i) {
|
2014-01-31 10:29:27 +00:00
|
|
|
const uint32_t* overrideId = (const uint32_t*) _overrides[i].id;
|
|
|
|
if (*overrideId == id) {
|
2013-10-21 01:08:18 +00:00
|
|
|
switch (_overrides[i].type) {
|
2013-10-12 05:03:27 +00:00
|
|
|
case SAVEDATA_FLASH512:
|
|
|
|
case SAVEDATA_FLASH1M:
|
2013-10-21 08:28:41 +00:00
|
|
|
gba->memory.savedata.type = _overrides[i].type;
|
2013-10-12 05:03:27 +00:00
|
|
|
GBASavedataInitFlash(&gba->memory.savedata);
|
|
|
|
break;
|
|
|
|
case SAVEDATA_EEPROM:
|
|
|
|
GBASavedataInitEEPROM(&gba->memory.savedata);
|
|
|
|
break;
|
|
|
|
case SAVEDATA_SRAM:
|
|
|
|
GBASavedataInitSRAM(&gba->memory.savedata);
|
|
|
|
break;
|
|
|
|
case SAVEDATA_NONE:
|
|
|
|
break;
|
|
|
|
}
|
2013-10-21 01:08:18 +00:00
|
|
|
|
|
|
|
if (_overrides[i].gpio & GPIO_RTC) {
|
|
|
|
GBAGPIOInitRTC(&gba->memory.gpio);
|
|
|
|
}
|
2013-10-21 09:54:52 +00:00
|
|
|
|
|
|
|
if (_overrides[i].gpio & GPIO_GYRO) {
|
|
|
|
GBAGPIOInitGyro(&gba->memory.gpio);
|
|
|
|
}
|
2013-10-22 04:50:29 +00:00
|
|
|
|
|
|
|
if (_overrides[i].gpio & GPIO_RUMBLE) {
|
|
|
|
GBAGPIOInitRumble(&gba->memory.gpio);
|
|
|
|
}
|
2013-10-21 04:39:47 +00:00
|
|
|
return;
|
2013-10-12 05:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|