GB: Add patch support
Jeffrey Pfau jeffrey@endrift.com
Sat, 06 Feb 2016 02:30:58 -0800
2 files changed,
28 insertions(+),
5 deletions(-)
M
src/gb/core.c
→
src/gb/core.c
@@ -9,6 +9,7 @@ #include "core/core.h"
#include "gb/gb.h" #include "gb/renderers/software.h" #include "util/memory.h" +#include "util/patch.h" struct GBCore { struct mCore d;@@ -97,11 +98,15 @@ return GBLoadSave(core->board, vf);
} static bool _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) { - // TODO - UNUSED(core); - UNUSED(vf); - mLOG(GB, STUB, "Patches are not yet supported"); - return false; + if (!vf) { + return false; + } + struct Patch patch; + if (!loadPatch(vf, &patch)) { + return false; + } + GBApplyPatch(core->board, &patch); + return true; } static void _GBCoreUnloadROM(struct mCore* core) {
M
src/gb/gb.c
→
src/gb/gb.c
@@ -126,6 +126,24 @@ }
gb->memory.sram = 0; } +void GBApplyPatch(struct GB* gb, struct Patch* patch) { + size_t patchedSize = patch->outputSize(patch, gb->memory.romSize); + if (!patchedSize) { + return; + } + if (patchedSize > 0x400000) { + patchedSize = 0x400000; + } + gb->memory.rom = anonymousMemoryMap(0x400000); + if (!patch->applyPatch(patch, gb->pristineRom, gb->pristineRomSize, gb->memory.rom, patchedSize)) { + mappedMemoryFree(gb->memory.rom, patchedSize); + gb->memory.rom = gb->pristineRom; + return; + } + gb->memory.romSize = patchedSize; + gb->romCrc32 = doCrc32(gb->memory.rom, gb->memory.romSize); +} + void GBDestroy(struct GB* gb) { GBUnloadROM(gb);