GBA Context: Move logging and option parsing into GBAContext
@@ -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,15 +32,33 @@ mappedMemoryFree(context->cpu, sizeof(struct ARMCore));
} return false; } + 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); } - - GBACreate(context->gba); - ARMSetComponents(context->cpu, &context->gba->d, 0, context->components); - ARMInit(context->cpu); context->gba->sync = 0; return true;@@ -130,6 +152,10 @@ return false;
} 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 @@ while (frameCounter == context->gba->video.frameCounter) {
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); +}
@@ -39,7 +39,6 @@ accelVector accel;
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 @@ sf2d_swapbuffers();
} 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 @@ 0, 0
}; FSUSER_OpenArchive(0, &sdmcArchive); - logFile = VFileOpen("/mgba.log", O_WRONLY | O_CREAT | O_TRUNC); struct GUIFont* font = GUIFontCreate(); if (!font) {@@ -352,17 +342,14 @@ .unpaused = _gameLoaded,
.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 @@ }
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); -}
@@ -158,7 +158,6 @@
GBAContextInit(&context, 0); struct GBAOptions opts = { .useBios = true, - .logLevel = 0, .idleOptimization = IDLE_LOOP_REMOVE }; GBAConfigLoadDefaults(&context.config, &opts);
@@ -100,7 +100,7 @@ .incrementScreenMode = GBAPSP2IncrementScreenMode,
.pollGameInput = GBAPSP2PollInput }; - GBAGUIInit(&runner, 0); + GBAGUIInit(&runner, "psvita"); GBAGUIRunloop(&runner); GBAGUIDeinit(&runner);
@@ -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);
@@ -22,8 +22,6 @@ #include "util/vfs.h"
#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 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 @@ font = GUIFontCreate();
fatInitDefault(); - logfile = fopen("/mgba.log", "w"); - rumble.setRumble = _setRumble; rotation.sample = _sampleRotation;@@ -177,11 +172,10 @@ .paused = _gameUnloaded,
.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 @@
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 @@ GX_LoadProjectionMtx(proj, GX_ORTHOGRAPHIC);
} 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;