From edaa27796bbd2c991b5c9ccd2af6f43c361715a4 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Wed, 11 Dec 2024 17:24:22 -0800 Subject: [PATCH] GBA: Improve reset behavior of various hardware components --- include/mgba/internal/gba/cart/gpio.h | 1 + include/mgba/internal/gba/savedata.h | 1 + src/gba/cart/gpio.c | 15 +++++++++++++++ src/gba/memory.c | 10 ++++++++++ src/gba/savedata.c | 7 ++++++- 5 files changed, 33 insertions(+), 1 deletion(-) diff --git a/include/mgba/internal/gba/cart/gpio.h b/include/mgba/internal/gba/cart/gpio.h index 77785e612..be3cd9173 100644 --- a/include/mgba/internal/gba/cart/gpio.h +++ b/include/mgba/internal/gba/cart/gpio.h @@ -86,6 +86,7 @@ struct GBACartridgeHardware { }; void GBAHardwareInit(struct GBACartridgeHardware* gpio, uint16_t* gpioBase); +void GBAHardwareReset(struct GBACartridgeHardware* gpio); void GBAHardwareClear(struct GBACartridgeHardware* gpio); void GBAHardwareInitRTC(struct GBACartridgeHardware* gpio); diff --git a/include/mgba/internal/gba/savedata.h b/include/mgba/internal/gba/savedata.h index 274241a76..e69d07834 100644 --- a/include/mgba/internal/gba/savedata.h +++ b/include/mgba/internal/gba/savedata.h @@ -91,6 +91,7 @@ struct GBASavedataRTCBuffer { }; void GBASavedataInit(struct GBASavedata* savedata, struct VFile* vf); +void GBASavedataReset(struct GBASavedata* savedata); void GBASavedataDeinit(struct GBASavedata* savedata); void GBASavedataMask(struct GBASavedata* savedata, struct VFile* vf, bool writeback); diff --git a/src/gba/cart/gpio.c b/src/gba/cart/gpio.c index 48cb8c340..a44bab294 100644 --- a/src/gba/cart/gpio.c +++ b/src/gba/cart/gpio.c @@ -47,6 +47,20 @@ void GBAHardwareInit(struct GBACartridgeHardware* hw, uint16_t* base) { GBAHardwareClear(hw); } +void GBAHardwareReset(struct GBACartridgeHardware* hw) { + hw->readWrite = GPIO_WRITE_ONLY; + hw->pinState = 0; + hw->direction = 0; + hw->lightCounter = 0; + hw->lightEdge = false; + hw->lightSample = 0xFF; + hw->gyroSample = 0; + hw->gyroEdge = 0; + hw->tiltX = 0xFFF; + hw->tiltY = 0xFFF; + hw->tiltState = 0; +} + void GBAHardwareClear(struct GBACartridgeHardware* hw) { hw->devices = HW_NONE | (hw->devices & HW_GB_PLAYER_DETECTION); hw->readWrite = GPIO_WRITE_ONLY; @@ -503,6 +517,7 @@ void GBAHardwareDeserialize(struct GBACartridgeHardware* hw, const struct GBASer hw->devices = state->hw.devices; if ((hw->devices & HW_GPIO) && hw->gpioBase) { + // TODO: This needs to update the pristine state somehow if (hw->readWrite) { STORE_16(hw->pinState, 0, hw->gpioBase); STORE_16(hw->direction, 2, hw->gpioBase); diff --git a/src/gba/memory.c b/src/gba/memory.c index bdaf22c36..4f074657a 100644 --- a/src/gba/memory.c +++ b/src/gba/memory.c @@ -138,6 +138,16 @@ void GBAMemoryReset(struct GBA* gba) { mLOG(GBA_MEM, FATAL, "Could not map memory"); } + if (!gba->memory.rom) { + gba->isPristine = false; + } + + if (gba->memory.hw.devices & HW_GPIO) { + _pristineCow(gba); + } + + GBASavedataReset(&gba->memory.savedata); + GBAHardwareReset(&gba->memory.hw); GBADMAReset(gba); GBAUnlCartReset(gba); memset(&gba->memory.matrix, 0, sizeof(gba->memory.matrix)); diff --git a/src/gba/savedata.c b/src/gba/savedata.c index a9cd682ca..c0d2c889a 100644 --- a/src/gba/savedata.c +++ b/src/gba/savedata.c @@ -45,7 +45,7 @@ static void _ashesToAshes(struct mTiming* timing, void* user, uint32_t cyclesLat void GBASavedataInit(struct GBASavedata* savedata, struct VFile* vf) { savedata->type = GBA_SAVEDATA_AUTODETECT; - savedata->data = 0; + savedata->data = NULL; savedata->command = EEPROM_COMMAND_NULL; savedata->flashState = FLASH_STATE_RAW; savedata->vf = vf; @@ -63,6 +63,11 @@ void GBASavedataInit(struct GBASavedata* savedata, struct VFile* vf) { savedata->dust.callback = _ashesToAshes; } +void GBASavedataReset(struct GBASavedata* savedata) { + savedata->command = EEPROM_COMMAND_NULL; + savedata->flashState = FLASH_STATE_RAW; +} + void GBASavedataDeinit(struct GBASavedata* savedata) { if (savedata->vf) { size_t size = GBASavedataSize(savedata);