aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/Util_ROM.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/frontend/Util_ROM.cpp')
-rw-r--r--src/frontend/Util_ROM.cpp170
1 files changed, 161 insertions, 9 deletions
diff --git a/src/frontend/Util_ROM.cpp b/src/frontend/Util_ROM.cpp
index 065b319..5da7330 100644
--- a/src/frontend/Util_ROM.cpp
+++ b/src/frontend/Util_ROM.cpp
@@ -19,6 +19,9 @@
#include <stdio.h>
#include <string.h>
+#ifdef ARCHIVE_SUPPORT_ENABLED
+#include "ArchiveUtil.h"
+#endif
#include "FrontendUtil.h"
#include "Config.h"
#include "SharedConfig.h"
@@ -38,6 +41,8 @@ char ROMPath [ROMSlot_MAX][1024];
char SRAMPath [ROMSlot_MAX][1024];
char PrevSRAMPath[ROMSlot_MAX][1024]; // for savestate 'undo load'
+char NDSROMExtension[4];
+
bool SavestateLoaded;
ARCodeFile* CheatFile;
@@ -295,6 +300,84 @@ int LoadBIOS()
return Load_OK;
}
+int LoadROM(const u8 *romdata, u32 romlength, const char *archivefilename, const char *romfilename, const char *sramfilename, int slot)
+{
+ int res;
+ bool directboot = Config::DirectBoot != 0;
+
+ if (Config::ConsoleType == 1 && slot == 1)
+ {
+ // cannot load a GBA ROM into a DSi
+ return Load_ROMLoadError;
+ }
+
+ res = VerifyDSBIOS();
+ if (res != Load_OK) return res;
+
+ if (Config::ConsoleType == 1)
+ {
+ res = VerifyDSiBIOS();
+ if (res != Load_OK) return res;
+
+ res = VerifyDSiFirmware();
+ if (res != Load_OK) return res;
+
+ res = SetupDSiNAND();
+ if (res != Load_OK) return res;
+
+ GBACart::Eject();
+ ROMPath[ROMSlot_GBA][0] = '\0';
+ }
+ else
+ {
+ res = VerifyDSFirmware();
+ if (res != Load_OK)
+ {
+ if (res == Load_FirmwareNotBootable)
+ directboot = true;
+ else
+ return res;
+ }
+ }
+
+ char oldpath[1024];
+ char oldsram[1024];
+ strncpy(oldpath, ROMPath[slot], 1024);
+ strncpy(oldsram, SRAMPath[slot], 1024);
+
+ strncpy(SRAMPath[slot], sramfilename, 1024);
+ strncpy(ROMPath[slot], archivefilename, 1024);
+
+ NDS::SetConsoleType(Config::ConsoleType);
+
+ if (slot == ROMSlot_NDS && NDS::LoadROM(romdata, romlength, SRAMPath[slot], directboot))
+ {
+ SavestateLoaded = false;
+
+ LoadCheats();
+
+ // Reload the inserted GBA cartridge (if any)
+ // TODO: report failure there??
+ //if (ROMPath[ROMSlot_GBA][0] != '\0') NDS::LoadGBAROM(ROMPath[ROMSlot_GBA], SRAMPath[ROMSlot_GBA]);
+
+ strncpy(PrevSRAMPath[slot], SRAMPath[slot], 1024); // safety
+ return Load_OK;
+ }
+ else if (slot == ROMSlot_GBA && NDS::LoadGBAROM(romdata, romlength, romfilename, SRAMPath[slot]))
+ {
+ SavestateLoaded = false; // checkme??
+
+ strncpy(PrevSRAMPath[slot], SRAMPath[slot], 1024); // safety
+ return Load_OK;
+ }
+ else
+ {
+ strncpy(ROMPath[slot], oldpath, 1024);
+ strncpy(SRAMPath[slot], oldsram, 1024);
+ return Load_ROMLoadError;
+ }
+}
+
int LoadROM(const char* file, int slot)
{
DSi::CloseDSiNAND();
@@ -440,16 +523,76 @@ int Reset()
}
else
{
- SetupSRAMPath(0);
- if (!NDS::LoadROM(ROMPath[ROMSlot_NDS], SRAMPath[ROMSlot_NDS], directboot))
- return Load_ROMLoadError;
+ char ext[5] = {0}; int _len = strlen(ROMPath[ROMSlot_NDS]);
+ strncpy(ext, ROMPath[ROMSlot_NDS] + _len - 4, 4);
+
+ if(!strncmp(ext, ".nds", 4) || !strncmp(ext, ".srl", 4) || !strncmp(ext, ".dsi", 4))
+ {
+ SetupSRAMPath(0);
+ if (!NDS::LoadROM(ROMPath[ROMSlot_NDS], SRAMPath[ROMSlot_NDS], directboot))
+ return Load_ROMLoadError;
+ }
+#ifdef ARCHIVE_SUPPORT_ENABLED
+ else
+ {
+ u8 *romdata = nullptr; u32 romlen;
+ char romfilename[1024] = {0}, sramfilename[1024];
+ strncpy(sramfilename, SRAMPath[ROMSlot_NDS], 1024); // Use existing SRAMPath
+
+ int pos = strlen(sramfilename) - 1;
+ while(pos > 0 && sramfilename[pos] != '/' && sramfilename[pos] != '\\')
+ --pos;
+
+ strncpy(romfilename, &sramfilename[pos + 1], 1024);
+ strncpy(&romfilename[strlen(romfilename) - 3], NDSROMExtension, 3); // extension could be nds, srl or dsi
+ printf("RESET loading from archive : %s\n", romfilename);
+ romlen = Archive::ExtractFileFromArchive(ROMPath[ROMSlot_NDS], romfilename, &romdata);
+ if(!romdata)
+ return Load_ROMLoadError;
+
+ bool ok = NDS::LoadROM(romdata, romlen, sramfilename, directboot);
+ delete romdata;
+ if(!ok)
+ return Load_ROMLoadError;
+ }
+#endif
}
if (ROMPath[ROMSlot_GBA][0] != '\0')
{
- SetupSRAMPath(1);
- if (!NDS::LoadGBAROM(ROMPath[ROMSlot_GBA], SRAMPath[ROMSlot_GBA]))
- return Load_ROMLoadError;
+ char ext[5] = {0}; int _len = strlen(ROMPath[ROMSlot_GBA]);
+ strncpy(ext, ROMPath[ROMSlot_NDS] + _len - 4, 4);
+
+ if(!strncmp(ext, ".gba", 4))
+ {
+ SetupSRAMPath(1);
+ if (!NDS::LoadGBAROM(ROMPath[ROMSlot_GBA], SRAMPath[ROMSlot_GBA]))
+ return Load_ROMLoadError;
+ }
+#ifdef ARCHIVE_SUPPORT_ENABLED
+ else
+ {
+ u8 *romdata = nullptr; u32 romlen;
+ char romfilename[1024] = {0}, sramfilename[1024];
+ strncpy(sramfilename, SRAMPath[ROMSlot_GBA], 1024); // Use existing SRAMPath
+
+ int pos = strlen(sramfilename) - 1;
+ while(pos > 0 && sramfilename[pos] != '/' && sramfilename[pos] != '\\')
+ --pos;
+
+ strncpy(romfilename, &sramfilename[pos + 1], 1024);
+ strncpy(&romfilename[strlen(romfilename) - 3], "gba", 3);
+ printf("RESET loading from archive : %s\n", romfilename);
+ romlen = Archive::ExtractFileFromArchive(ROMPath[ROMSlot_GBA], romfilename, &romdata);
+ if(!romdata)
+ return Load_ROMLoadError;
+
+ bool ok = NDS::LoadGBAROM(romdata, romlen, romfilename, SRAMPath[ROMSlot_GBA]);
+ delete romdata;
+ if(!ok)
+ return Load_ROMLoadError;
+ }
+#endif
}
LoadCheats();
@@ -472,15 +615,24 @@ void GetSavestateName(int slot, char* filename, int len)
}
else
{
- int l = strlen(ROMPath[ROMSlot_NDS]);
+ char *rompath;
+ char ext[5] = {0}; int _len = strlen(ROMPath[ROMSlot_NDS]);
+ strncpy(ext, ROMPath[ROMSlot_NDS] + _len - 4, 4);
+
+ if(!strncmp(ext, ".nds", 4) || !strncmp(ext, ".srl", 4) || !strncmp(ext, ".dsi", 4))
+ rompath = ROMPath[ROMSlot_NDS];
+ else
+ rompath = SRAMPath[ROMSlot_NDS]; // If archive, construct ssname from sram file
+
+ int l = strlen(rompath);
pos = l;
- while (ROMPath[ROMSlot_NDS][pos] != '.' && pos > 0) pos--;
+ while (rompath[pos] != '.' && pos > 0) pos--;
if (pos == 0) pos = l;
// avoid buffer overflow. shoddy
if (pos > len-5) pos = len-5;
- strncpy(&filename[0], ROMPath[ROMSlot_NDS], pos);
+ strncpy(&filename[0], rompath, pos);
}
strcpy(&filename[pos], ".ml");
filename[pos+3] = '0'+slot;