GB Memory: Add segment parameter to GBPatch8
Jeffrey Pfau jeffrey@endrift.com
Fri, 16 Sep 2016 18:17:29 -0700
4 files changed,
35 insertions(+),
17 deletions(-)
M
src/gb/cheats.c
→
src/gb/cheats.c
@@ -28,7 +28,7 @@ if (value != patch->oldValue) {
continue; } } - GBPatch8(device->p->cpu, patch->address, patch->newValue, &patch->oldValue); + GBPatch8(device->p->cpu, patch->address, patch->newValue, &patch->oldValue, 0); patch->applied = true; } }@@ -43,7 +43,7 @@ struct GBCheatPatch* patch = GBCheatPatchListGetPointer(&cheats->romPatches, i);
if (!patch->applied) { continue; } - GBPatch8(device->p->cpu, patch->address, patch->oldValue, &patch->newValue); + GBPatch8(device->p->cpu, patch->address, patch->oldValue, &patch->newValue, 0); patch->applied = false; } }
M
src/gb/core.c
→
src/gb/core.c
@@ -385,21 +385,21 @@ }
static void _GBCoreRawWrite8(struct mCore* core, uint32_t address, int segment, uint8_t value) { struct LR35902Core* cpu = core->cpu; - GBPatch8(cpu, address, value, NULL); + GBPatch8(cpu, address, value, NULL, segment); } static void _GBCoreRawWrite16(struct mCore* core, uint32_t address, int segment, uint16_t value) { struct LR35902Core* cpu = core->cpu; - GBPatch8(cpu, address, value, NULL); - GBPatch8(cpu, address + 1, value >> 8, NULL); + GBPatch8(cpu, address, value, NULL, segment); + GBPatch8(cpu, address + 1, value >> 8, NULL, segment); } static void _GBCoreRawWrite32(struct mCore* core, uint32_t address, int segment, uint32_t value) { struct LR35902Core* cpu = core->cpu; - GBPatch8(cpu, address, value, NULL); - GBPatch8(cpu, address + 1, value >> 8, NULL); - GBPatch8(cpu, address + 2, value >> 16, NULL); - GBPatch8(cpu, address + 3, value >> 24, NULL); + GBPatch8(cpu, address, value, NULL, segment); + GBPatch8(cpu, address + 1, value >> 8, NULL, segment); + GBPatch8(cpu, address + 2, value >> 16, NULL, segment); + GBPatch8(cpu, address + 3, value >> 24, NULL, segment); } static bool _GBCoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
M
src/gb/memory.c
→
src/gb/memory.c
@@ -478,7 +478,7 @@ }
GBStore8(cpu, address, value); } -void GBPatch8(struct LR35902Core* cpu, uint16_t address, int8_t value, int8_t* old) { +void GBPatch8(struct LR35902Core* cpu, uint16_t address, int8_t value, int8_t* old, int segment) { struct GB* gb = (struct GB*) cpu->master; struct GBMemory* memory = &gb->memory; int8_t oldValue = -1;@@ -497,13 +497,26 @@ case GB_REGION_CART_BANK1 + 1:
case GB_REGION_CART_BANK1 + 2: case GB_REGION_CART_BANK1 + 3: _pristineCow(gb); - oldValue = memory->romBank[address & (GB_SIZE_CART_BANK0 - 1)]; - memory->romBank[address & (GB_SIZE_CART_BANK0 - 1)] = value; + if (segment < 0) { + oldValue = memory->romBank[address & (GB_SIZE_CART_BANK0 - 1)]; + memory->romBank[address & (GB_SIZE_CART_BANK0 - 1)] = value; + } else { + if ((size_t) segment * GB_SIZE_CART_BANK0 > memory->romSize) { + return; + } + oldValue = memory->rom[(address & (GB_SIZE_CART_BANK0 - 1)) + segment * GB_SIZE_CART_BANK0]; + memory->rom[(address & (GB_SIZE_CART_BANK0 - 1)) + segment * GB_SIZE_CART_BANK0] = value; + } break; case GB_REGION_VRAM: case GB_REGION_VRAM + 1: - oldValue = gb->video.vramBank[address & (GB_SIZE_VRAM_BANK0 - 1)]; - gb->video.vramBank[address & (GB_SIZE_VRAM_BANK0 - 1)] = value; + if (segment < 0) { + oldValue = gb->video.vramBank[address & (GB_SIZE_VRAM_BANK0 - 1)]; + gb->video.vramBank[address & (GB_SIZE_VRAM_BANK0 - 1)] = value; + } else { + oldValue = gb->video.vram[(address & (GB_SIZE_VRAM_BANK0 - 1)) + segment * GB_SIZE_VRAM_BANK0]; + gb->video.vramBank[(address & (GB_SIZE_VRAM_BANK0 - 1)) + segment * GB_SIZE_VRAM_BANK0] = value; + } break; case GB_REGION_EXTERNAL_RAM: case GB_REGION_EXTERNAL_RAM + 1:@@ -515,8 +528,13 @@ oldValue = memory->wram[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)];
memory->wram[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)] = value; break; case GB_REGION_WORKING_RAM_BANK1: - oldValue = memory->wramBank[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)]; - memory->wramBank[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)] = value; + if (segment < 0) { + oldValue = memory->wramBank[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)]; + memory->wramBank[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)] = value; + } else { + oldValue = memory->wram[(address & (GB_SIZE_WORKING_RAM_BANK0 - 1)) + segment * GB_SIZE_WORKING_RAM_BANK0]; + memory->wram[(address & (GB_SIZE_WORKING_RAM_BANK0 - 1)) + segment * GB_SIZE_WORKING_RAM_BANK0] = value; + } break; default: if (address < GB_BASE_OAM) {
M
src/gb/memory.h
→
src/gb/memory.h
@@ -166,7 +166,7 @@
uint8_t GBDMALoad8(struct LR35902Core* cpu, uint16_t address); void GBDMAStore8(struct LR35902Core* cpu, uint16_t address, int8_t value); -void GBPatch8(struct LR35902Core* cpu, uint16_t address, int8_t value, int8_t* old); +void GBPatch8(struct LR35902Core* cpu, uint16_t address, int8_t value, int8_t* old, int segment); struct GBSerializedState; void GBMemorySerialize(const struct GB* gb, struct GBSerializedState* state);