Fix for issue #59

This commit is contained in:
SimoneN64
2023-10-25 22:59:23 +02:00
parent 607071853b
commit 8c044b5cd4
3 changed files with 34 additions and 29 deletions

View File

@@ -34,7 +34,7 @@ void Core::LoadROM(const std::string& rom_) {
GameDB::match(cpu->mem); GameDB::match(cpu->mem);
cpu->mem.mmio.vi.isPal = cpu->mem.IsROMPAL(); cpu->mem.mmio.vi.isPal = cpu->mem.IsROMPAL();
cpu->mem.mmio.si.pif.InitDevices(cpu->mem.saveType); cpu->mem.mmio.si.pif.InitDevices(cpu->mem.saveType);
cpu->mem.mmio.si.pif.LoadMempak(rom); cpu->mem.mmio.si.pif.mempakPath = rom;
cpu->mem.mmio.si.pif.LoadEeprom(cpu->mem.saveType, rom); cpu->mem.mmio.si.pif.LoadEeprom(cpu->mem.saveType, rom);
cpu->mem.flash.Load(cpu->mem.saveType, rom); cpu->mem.flash.Load(cpu->mem.saveType, rom);
cpu->mem.LoadSRAM(cpu->mem.saveType, rom); cpu->mem.LoadSRAM(cpu->mem.saveType, rom);

View File

@@ -27,33 +27,36 @@ void PIF::Reset() {
} }
} }
void PIF::LoadMempak(const std::string& path) { void PIF::MaybeLoadMempak() {
mempakPath = fs::path(path).replace_extension(".mempak").string(); if(!mempakOpen) {
std::error_code error; mempakPath = fs::path(mempakPath).replace_extension(".mempak").string();
if (mempak.is_mapped()) { std::error_code error;
mempak.sync(error); if (mempak.is_mapped()) {
if (error) { Util::panic("Could not sync {}", mempakPath); } mempak.sync(error);
mempak.unmap(); if (error) { Util::panic("Could not sync {}", mempakPath); }
} mempak.unmap();
FILE* f = fopen(mempakPath.c_str(), "rb"); }
if (!f) { FILE *f = fopen(mempakPath.c_str(), "rb");
f = fopen(mempakPath.c_str(), "wb"); if (!f) {
u8* dummy = (u8*)calloc(MEMPAK_SIZE, 1); f = fopen(mempakPath.c_str(), "wb");
fwrite(dummy, 1, MEMPAK_SIZE, f); u8 *dummy = (u8 *) calloc(MEMPAK_SIZE, 1);
free(dummy); fwrite(dummy, 1, MEMPAK_SIZE, f);
} free(dummy);
}
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
size_t actualSize = ftell(f); size_t actualSize = ftell(f);
fseek(f, 0, SEEK_SET); fseek(f, 0, SEEK_SET);
if (actualSize != MEMPAK_SIZE) { if (actualSize != MEMPAK_SIZE) {
Util::panic("Corrupt mempak!"); Util::panic("Corrupt mempak!");
} }
fclose(f); fclose(f);
mempak = mio::make_mmap_sink( mempak = mio::make_mmap_sink(
mempakPath, 0, mio::map_entire_file, error); mempakPath, 0, mio::map_entire_file, error);
if (error) { Util::panic("Could not open {}", mempakPath); } if (error) { Util::panic("Could not open {}", mempakPath); }
mempakOpen = true;
}
} }
FORCE_INLINE size_t getSaveSize(SaveType saveType) { FORCE_INLINE size_t getSaveSize(SaveType saveType) {
@@ -247,7 +250,8 @@ void PIF::ProcessCommands(Mem &mem) {
} }
} }
void PIF::MempakRead(const u8* cmd, u8* res) const { void PIF::MempakRead(const u8* cmd, u8* res) {
MaybeLoadMempak();
u16 offset = cmd[3] << 8; u16 offset = cmd[3] << 8;
offset |= cmd[4]; offset |= cmd[4];
@@ -274,6 +278,7 @@ void PIF::MempakRead(const u8* cmd, u8* res) const {
} }
void PIF::MempakWrite(u8* cmd, u8* res) { void PIF::MempakWrite(u8* cmd, u8* res) {
MaybeLoadMempak();
// First two bytes in the command are the offset // First two bytes in the command are the offset
u16 offset = cmd[3] << 8; u16 offset = cmd[3] << 8;
offset |= cmd[4]; offset |= cmd[4];

View File

@@ -107,7 +107,7 @@ struct PIF {
PIF() = default; PIF() = default;
~PIF() = default; ~PIF() = default;
void Reset(); void Reset();
void LoadMempak(const std::string&); void MaybeLoadMempak();
void LoadEeprom(SaveType, const std::string&); void LoadEeprom(SaveType, const std::string&);
void ProcessCommands(Mem&); void ProcessCommands(Mem&);
void InitDevices(SaveType); void InitDevices(SaveType);
@@ -118,12 +118,12 @@ struct PIF {
void UpdateController(); void UpdateController();
bool ReadButtons(u8*) const; bool ReadButtons(u8*) const;
void ControllerID(u8*) const; void ControllerID(u8*) const;
void MempakRead(const u8*, u8*) const; void MempakRead(const u8*, u8*);
void MempakWrite(u8*, u8*); void MempakWrite(u8*, u8*);
void EepromRead(const u8*, u8*, const Mem&) const; void EepromRead(const u8*, u8*, const Mem&) const;
void EepromWrite(const u8*, u8*, const Mem&); void EepromWrite(const u8*, u8*, const Mem&);
bool gamepadConnected = false; bool gamepadConnected = false, mempakOpen = false;
SDL_GameController* gamepad{}; SDL_GameController* gamepad{};
JoybusDevice joybusDevices[6]{}; JoybusDevice joybusDevices[6]{};
u8 bootrom[PIF_BOOTROM_SIZE]{}, ram[PIF_RAM_SIZE]{}; u8 bootrom[PIF_BOOTROM_SIZE]{}, ram[PIF_RAM_SIZE]{};