Core: Clean up ROM loading
Jeffrey Pfau jeffrey@endrift.com
Wed, 03 Feb 2016 20:56:08 -0800
7 files changed,
59 insertions(+),
33 deletions(-)
M
src/core/core.h
→
src/core/core.h
@@ -8,6 +8,10 @@ #define M_CORE_H
#include "util/common.h" +#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2 +#include "core/directories.h" +#endif + struct VFile; struct mRTCSource;@@ -23,6 +27,10 @@ struct mCoreSync;
struct mCore { void* cpu; void* board; + +#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2 + struct mDirectorySet dirs; +#endif bool (*init)(struct mCore*); void (*deinit)(struct mCore*);@@ -32,8 +40,9 @@
void (*desiredVideoDimensions)(struct mCore*, unsigned* width, unsigned* height); void (*setVideoBuffer)(struct mCore*, color_t* buffer, size_t stride); - bool (*isROM)(struct mCore*, struct VFile* vf); - bool (*loadROM)(struct mCore*, struct VFile* vf, struct VFile* save, const char* fname); + bool (*isROM)(struct VFile* vf); + bool (*loadROM)(struct mCore*, struct VFile* vf); + bool (*loadSave)(struct mCore*, struct VFile* vf); void (*unloadROM)(struct mCore*); bool (*loadBIOS)(struct mCore*, struct VFile* vf, int biosID);@@ -54,5 +63,8 @@ int32_t (*frequency)(struct mCore*);
void (*setRTC)(struct mCore*, struct mRTCSource*); }; + +bool mCoreLoadFile(struct mCore* core, const char* path); +bool mCoreAutoloadSave(struct mCore* core); #endif
M
src/core/directories.c
→
src/core/directories.c
@@ -99,7 +99,18 @@ file->close(file);
file = 0; } } + if (file) { + char dirname[PATH_MAX]; + separatePath(path, dirname, dirs->baseName, 0); + mDirectorySetAttachBase(dirs, VDirOpen(dirname)); + } return file; +} + +struct VFile* mDirectorySetOpenSuffix(struct mDirectorySet* dirs, const char* suffix, int mode) { + char name[PATH_MAX]; + snprintf(name, sizeof(name), "%s%s", dirs->baseName, suffix); + return dirs->base->openFile(dirs->base, name, mode); } void mDirectorySetMapOptions(struct mDirectorySet* dirs, const struct GBAOptions* opts) {
M
src/core/directories.h
→
src/core/directories.h
@@ -12,6 +12,7 @@ #if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
struct VDir; struct mDirectorySet { + char baseName[PATH_MAX]; struct VDir* base; struct VDir* archive; struct VDir* save;@@ -27,6 +28,7 @@ void mDirectorySetAttachBase(struct mDirectorySet* dirs, struct VDir* base);
void mDirectorySetDetachBase(struct mDirectorySet* dirs); struct VFile* mDirectorySetOpenPath(struct mDirectorySet* dirs, const char* path, bool (*filter)(struct VFile*)); +struct VFile* mDirectorySetOpenSuffix(struct mDirectorySet* dirs, const char* suffix, int mode); struct GBAOptions; void mDirectorySetMapOptions(struct mDirectorySet* dirs, const struct GBAOptions* opts);
M
src/gb/core.c
→
src/gb/core.c
@@ -37,6 +37,10 @@ GBVideoSoftwareRendererCreate(&gbcore->renderer);
GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d); gb->keySource = &gbcore->keys; + +#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2 + mDirectorySetInit(&core->dirs); +#endif return true; }@@ -46,6 +50,9 @@ LR35902Deinit(core->cpu);
GBDestroy(core->board); mappedMemoryFree(core->cpu, sizeof(struct LR35902Core)); mappedMemoryFree(core->board, sizeof(struct GB)); +#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2 + mDirectorySetDeinit(&core->dirs); +#endif } static void _GBCoreSetSync(struct mCore* core, struct mCoreSync* sync) {@@ -65,13 +72,12 @@ gbcore->renderer.outputBuffer = buffer;
gbcore->renderer.outputBufferStride = stride; } -static bool _GBCoreLoadROM(struct mCore* core, struct VFile* vf, struct VFile* save, const char* fname) { - return GBLoadROM(core->board, vf, save, fname); +static bool _GBCoreLoadROM(struct mCore* core, struct VFile* vf) { + return GBLoadROM(core->board, vf); } -static bool _GBCoreIsROM(struct mCore* core, struct VFile* vf) { - UNUSED(core); - return GBIsROM(vf); +static bool _GBCoreLoadSave(struct mCore* core, struct VFile* vf) { + return GBLoadSave(core->board, vf); } static void _GBCoreUnloadROM(struct mCore* core) {@@ -144,8 +150,9 @@ core->deinit = _GBCoreDeinit;
core->setSync = _GBCoreSetSync; core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions; core->setVideoBuffer = _GBCoreSetVideoBuffer; - core->isROM = _GBCoreIsROM; + core->isROM = GBIsROM; core->loadROM = _GBCoreLoadROM; + core->loadSave = _GBCoreLoadSave; core->unloadROM = _GBCoreUnloadROM; core->reset = _GBCoreReset; core->runFrame = _GBCoreRunFrame;
M
src/gb/gb.c
→
src/gb/gb.c
@@ -60,7 +60,7 @@
gb->diPending = false; } -bool GBLoadROM(struct GB* gb, struct VFile* vf, struct VFile* sav, const char* fname) { +bool GBLoadROM(struct GB* gb, struct VFile* vf) { GBUnloadROM(gb); gb->romVf = vf; gb->pristineRomSize = vf->size(vf);@@ -79,21 +79,25 @@ return false;
} gb->yankedRomSize = 0; gb->memory.rom = gb->pristineRom; - gb->activeFile = fname; gb->memory.romSize = gb->pristineRomSize; gb->romCrc32 = doCrc32(gb->memory.rom, gb->memory.romSize); - gb->sramVf = sav; - if (sav) { + + // TODO: error check + return true; +} + +bool GBLoadSave(struct GB* gb, struct VFile* vf) { + gb->sramVf = vf; + if (vf) { // TODO: Do this in bank-switching code - if (sav->size(sav) < 0x20000) { - sav->truncate(sav, 0x20000); + if (vf->size(vf) < 0x20000) { + vf->truncate(vf, 0x20000); } - gb->memory.sram = sav->map(sav, 0x20000, MAP_WRITE); + gb->memory.sram = vf->map(vf, 0x20000, MAP_WRITE); } else { gb->memory.sram = anonymousMemoryMap(0x20000); } - return true; - // TODO: error check + return gb->memory.sram; } void GBUnloadROM(struct GB* gb) {
M
src/gb/gb.h
→
src/gb/gb.h
@@ -61,8 +61,6 @@ uint32_t romCrc32;
struct VFile* romVf; struct VFile* sramVf; - const char* activeFile; - bool diPending; };@@ -100,7 +98,8 @@ void GBHalt(struct LR35902Core* cpu);
void GBStop(struct LR35902Core* cpu); struct VFile; -bool GBLoadROM(struct GB* gb, struct VFile* vf, struct VFile* sav, const char* fname); +bool GBLoadROM(struct GB* gb, struct VFile* vf); +bool GBLoadSave(struct GB* gb, struct VFile* vf); void GBYankROM(struct GB* gb); void GBUnloadROM(struct GB* gb);
M
src/platform/sdl/main.c
→
src/platform/sdl/main.c
@@ -284,8 +284,10 @@ .sync = {
.audioWait = true } }; - struct VFile* vf = VFileOpen(args->fname, O_RDONLY); - struct VFile* savVf = 0; + if (!mCoreLoadFile(renderer->core, args->fname)) { + return 1; + } + mCoreAutoloadSave(renderer->core); renderer->audio.samples = 1024; renderer->audio.sampleRate = 44100;@@ -293,22 +295,11 @@
GBSDLInitAudio(&renderer->audio, 0); renderer->audio.sync = &thread.sync; - { - char savepath[PATH_MAX]; - char dirname[PATH_MAX]; - char basename[PATH_MAX]; - separatePath(args->fname, dirname, basename, 0); - snprintf(savepath, sizeof(savepath), "%s" PATH_SEP "%s.sav", dirname, basename); - savVf = VFileOpen(savepath, O_RDWR | O_CREAT); - } - - renderer->core->loadROM(renderer->core, vf, savVf, args->fname); mCoreThreadStart(&thread); renderer->audio.psg = 0; GBSDLResumeAudio(&renderer->audio); renderer->runloop(renderer, &thread); renderer->core->unloadROM(renderer->core); - vf->close(vf); return 0; } #endif