GBA Context: Move logging and option parsing into GBAContext

This commit is contained in:
Jeffrey Pfau 2015-09-07 21:46:36 -07:00
parent f25486eca3
commit 8452e880c2
6 changed files with 52 additions and 64 deletions

View File

@ -10,10 +10,14 @@
#include "util/memory.h"
#include "util/vfs.h"
static struct VFile* _logFile = 0;
static void _GBAContextLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args);
bool GBAContextInit(struct GBAContext* context, const char* port) {
context->gba = anonymousMemoryMap(sizeof(struct GBA));
context->cpu = anonymousMemoryMap(sizeof(struct ARMCore));
context->rom = 0;
context->bios = 0;
context->fname = 0;
context->save = 0;
context->renderer = 0;
@ -28,16 +32,34 @@ bool GBAContextInit(struct GBAContext* context, const char* port) {
}
return false;
}
GBAConfigInit(&context->config, port);
if (port) {
GBAConfigLoad(&context->config);
}
GBACreate(context->gba);
ARMSetComponents(context->cpu, &context->gba->d, 0, context->components);
ARMInit(context->cpu);
GBAConfigInit(&context->config, port);
if (port) {
if (!_logFile) {
char logPath[PATH_MAX];
GBAConfigDirectory(logPath, PATH_MAX);
strncat(logPath, PATH_SEP "log", PATH_MAX - strlen(logPath));
_logFile = VFileOpen(logPath, O_WRONLY | O_CREAT | O_TRUNC);
}
context->gba->logHandler = _GBAContextLog;
char biosPath[PATH_MAX];
GBAConfigDirectory(biosPath, PATH_MAX);
strncat(biosPath, PATH_SEP "gba_bios.bin", PATH_MAX - strlen(biosPath));
struct GBAOptions opts = {
.bios = biosPath,
.useBios = true,
.idleOptimization = IDLE_LOOP_DETECT,
.logLevel = GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL | GBA_LOG_STATUS
};
GBAConfigLoadDefaults(&context->config, &opts);
GBAConfigLoad(&context->config);
}
context->gba->sync = 0;
return true;
}
@ -130,6 +152,10 @@ bool GBAContextStart(struct GBAContext* context) {
}
GBAConfigMap(&context->config, &opts);
if (!context->bios && opts.bios) {
GBAContextLoadBIOS(context, opts.bios);
}
if (opts.useBios && context->bios) {
GBALoadBIOS(context->gba, context->bios);
}
@ -166,3 +192,19 @@ void GBAContextFrame(struct GBAContext* context, uint16_t keys) {
ARMRunLoop(context->cpu);
}
}
static void _GBAContextLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args) {
UNUSED(thread);
UNUSED(level);
// TODO: Make this local
if (!_logFile) {
return;
}
char out[256];
size_t len = vsnprintf(out, sizeof(out), format, args);
if (len >= sizeof(out)) {
len = sizeof(out) - 1;
}
out[len] = '\n';
_logFile->write(_logFile, out, len + 1);
}

View File

@ -39,7 +39,6 @@ static struct GBA3DSRotationSource {
angularRate gyro;
} rotation;
static struct VFile* logFile;
static bool hasSound;
// TODO: Move into context
static struct GBAVideoSoftwareRenderer renderer;
@ -50,7 +49,6 @@ static size_t audioPos = 0;
static sf2d_texture* tex;
extern bool allocateRomBuffer(void);
static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args);
static void _postAudioBuffer(struct GBAAVStream* stream, struct GBAAudio* audio);
@ -68,13 +66,6 @@ static void _drawEnd(void) {
}
static void _setup(struct GBAGUIRunner* runner) {
struct GBAOptions opts = {
.useBios = true,
.logLevel = 0,
.idleOptimization = IDLE_LOOP_DETECT
};
GBAConfigLoadDefaults(&runner->context.config, &opts);
runner->context.gba->logHandler = GBA3DSLog;
runner->context.gba->rotationSource = &rotation.d;
if (hasSound) {
runner->context.gba->stream = &stream;
@ -323,7 +314,6 @@ int main() {
};
FSUSER_OpenArchive(0, &sdmcArchive);
logFile = VFileOpen("/mgba.log", O_WRONLY | O_CREAT | O_TRUNC);
struct GUIFont* font = GUIFontCreate();
if (!font) {
@ -352,17 +342,14 @@ int main() {
.incrementScreenMode = _incrementScreenMode,
.pollGameInput = _pollGameInput
};
GBAGUIInit(&runner, 0);
GBAGUIInit(&runner, "3ds");
GBAGUIRunloop(&runner);
GBAGUIDeinit(&runner);
cleanup:
linearFree(renderer.outputBuffer);
if (logFile) {
logFile->close(logFile);
}
sf2d_free_texture(tex);
sf2d_fini();
@ -373,18 +360,3 @@ cleanup:
csndExit();
return 0;
}
static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args) {
UNUSED(thread);
UNUSED(level);
if (!logFile) {
return;
}
char out[256];
size_t len = vsnprintf(out, sizeof(out), format, args);
if (len >= sizeof(out)) {
len = sizeof(out) - 1;
}
out[len] = '\n';
logFile->write(logFile, out, len + 1);
}

View File

@ -158,7 +158,6 @@ void retro_init(void) {
GBAContextInit(&context, 0);
struct GBAOptions opts = {
.useBios = true,
.logLevel = 0,
.idleOptimization = IDLE_LOOP_REMOVE
};
GBAConfigLoadDefaults(&context.config, &opts);

View File

@ -100,7 +100,7 @@ int main() {
.pollGameInput = GBAPSP2PollInput
};
GBAGUIInit(&runner, 0);
GBAGUIInit(&runner, "psvita");
GBAGUIRunloop(&runner);
GBAGUIDeinit(&runner);

View File

@ -139,7 +139,6 @@ void GBAPSP2Setup(struct GBAGUIRunner* runner) {
scePowerSetArmClockFrequency(80);
struct GBAOptions opts = {
.useBios = true,
.logLevel = 0,
.idleOptimization = IDLE_LOOP_DETECT
};
GBAConfigLoadDefaults(&runner->context.config, &opts);

View File

@ -22,8 +22,6 @@
#define SAMPLES 1024
static void GBAWiiLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args);
static void _audioDMA(void);
static void _setRumble(struct GBARumble* rumble, int enable);
static void _sampleRotation(struct GBARotationSource* source);
@ -47,7 +45,6 @@ static uint16_t _pollGameInput(struct GBAGUIRunner* runner);
static struct GBAVideoSoftwareRenderer renderer;
static struct GBARumble rumble;
static struct GBARotationSource rotation;
static FILE* logfile;
static GXRModeObj* mode;
static Mtx model, view, modelview;
static uint16_t* texmem;
@ -148,8 +145,6 @@ int main() {
fatInitDefault();
logfile = fopen("/mgba.log", "w");
rumble.setRumble = _setRumble;
rotation.sample = _sampleRotation;
@ -177,11 +172,10 @@ int main() {
.unpaused = 0,
.pollGameInput = _pollGameInput
};
GBAGUIInit(&runner, 0);
GBAGUIInit(&runner, "wii");
GBAGUIRunloop(&runner);
GBAGUIDeinit(&runner);
fclose(logfile);
free(fifo);
free(renderer.outputBuffer);
@ -190,17 +184,6 @@ int main() {
return 0;
}
void GBAWiiLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args) {
UNUSED(thread);
UNUSED(level);
if (!logfile) {
return;
}
vfprintf(logfile, format, args);
fprintf(logfile, "\n");
fflush(logfile);
}
static void _audioDMA(void) {
if (!audioBufferSize) {
return;
@ -313,13 +296,6 @@ void _guiFinish(void) {
}
void _setup(struct GBAGUIRunner* runner) {
struct GBAOptions opts = {
.useBios = true,
.logLevel = 0,
.idleOptimization = IDLE_LOOP_DETECT
};
GBAConfigLoadDefaults(&runner->context.config, &opts);
runner->context.gba->logHandler = GBAWiiLog;
runner->context.gba->rumble = &rumble;
runner->context.gba->rotationSource = &rotation;