Skip to content

Commit

Permalink
maple: do a full save of the vmu after loading a state
Browse files Browse the repository at this point in the history
A partial save might corrupt the vmu file system.
  • Loading branch information
flyinghead committed Nov 4, 2024
1 parent 5fc84ac commit fc6142b
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions core/hw/maple/maple_devs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,11 @@ u8 vmu_default[] = {

struct maple_sega_vmu: maple_base
{
FILE* file;
FILE *file = nullptr;
u8 flash_data[128_KB];
u8 lcd_data[192];
u8 lcd_data_decoded[48*32];
bool fullSaveNeeded = false;

MapleDeviceType get_device_type() override
{
Expand All @@ -361,6 +362,7 @@ struct maple_sega_vmu: maple_base
config->SetImage(lcd_data_decoded);
break;
}
fullSaveNeeded = true;
}

bool fullSave()
Expand All @@ -375,6 +377,7 @@ struct maple_sega_vmu: maple_base
ERROR_LOG(MAPLE, "Failed to write the VMU %s to disk", logical_port);
return false;
}
fullSaveNeeded = false;
return true;
}

Expand Down Expand Up @@ -434,6 +437,7 @@ struct maple_sega_vmu: maple_base
if (sum == 0)
// This means the existing VMU file is completely empty and needs to be recreated
initializeVmu();
fullSaveNeeded = false;
}

~maple_sega_vmu() override
Expand Down Expand Up @@ -659,17 +663,19 @@ struct maple_sega_vmu: maple_base

if (file != nullptr)
{
if (std::fseek(file, write_adr, SEEK_SET) != 0
if (fullSaveNeeded) {
if (!fullSave())
return MDRE_FileError;
}
else if (std::fseek(file, write_adr, SEEK_SET) != 0
|| std::fwrite(&flash_data[write_adr], write_len, 1, file) != 1)
{
WARN_LOG(MAPLE, "Failed to save VMU %s: I/O error", logical_port);
ERROR_LOG(MAPLE, "Failed to save VMU %s: I/O error", logical_port);
return MDRE_FileError; // I/O error
}
std::fflush(file);
}
else
{
INFO_LOG(MAPLE, "Failed to save VMU %s data", logical_port);
else {
WARN_LOG(MAPLE, "Failed to save VMU %s data", logical_port);
}
return MDRS_DeviceReply;
}
Expand Down

0 comments on commit fc6142b

Please sign in to comment.