Hook up the GBA slot to the UI

A GBA cartridge may be loaded in the same
way as a DS cartridge. If the extension of
the selected file is "gba", it will be treated
as a GBA file. The system boot logic is still
centered around the DS cartridge, so loading
a GBA file will not start or reset it.
This commit is contained in:
Raphaël Zumer
2019-12-08 15:31:18 -05:00
parent 968768042e
commit 1da9b3806c

View File

@ -105,9 +105,9 @@ int EmuRunning;
volatile int EmuStatus; volatile int EmuStatus;
bool RunningSomething; bool RunningSomething;
char ROMPath[1024]; char ROMPath[2][1024];
char SRAMPath[1024]; char SRAMPath[2][1024];
char PrevSRAMPath[1024]; // for savestate 'undo load' char PrevSRAMPath[2][1024]; // for savestate 'undo load'
bool SavestateLoaded; bool SavestateLoaded;
@ -184,7 +184,7 @@ void SetupScreenRects(int width, int height);
void TogglePause(void* blarg); void TogglePause(void* blarg);
void Reset(void* blarg); void Reset(void* blarg);
void SetupSRAMPath(); void SetupSRAMPath(int slot);
void SaveState(int slot); void SaveState(int slot);
void LoadState(int slot); void LoadState(int slot);
@ -1648,12 +1648,18 @@ void Reset(void* blarg)
SavestateLoaded = false; SavestateLoaded = false;
uiMenuItemDisable(MenuItem_UndoStateLoad); uiMenuItemDisable(MenuItem_UndoStateLoad);
if (ROMPath[0] == '\0') if (ROMPath[0][0] == '\0')
NDS::LoadBIOS(); NDS::LoadBIOS();
else else
{ {
SetupSRAMPath(); SetupSRAMPath(0);
NDS::LoadROM(ROMPath, SRAMPath, Config::DirectBoot); NDS::LoadROM(ROMPath[0], SRAMPath[0], Config::DirectBoot);
}
if (ROMPath[1][0] != '\0')
{
SetupSRAMPath(1);
NDS::LoadGBAROM(ROMPath[1], SRAMPath[1]);
} }
Run(); Run();
@ -1688,41 +1694,53 @@ void Stop(bool internal)
OSD::AddMessage(0xFFC040, "Shutdown"); OSD::AddMessage(0xFFC040, "Shutdown");
} }
void SetupSRAMPath() void SetupSRAMPath(int slot)
{ {
strncpy(SRAMPath, ROMPath, 1023); strncpy(SRAMPath[slot], ROMPath[slot], 1023);
SRAMPath[1023] = '\0'; SRAMPath[slot][1023] = '\0';
strncpy(SRAMPath + strlen(ROMPath) - 3, "sav", 3); strncpy(SRAMPath[slot] + strlen(ROMPath[slot]) - 3, "sav", 3);
} }
void TryLoadROM(char* file, int prevstatus) void TryLoadROM(char* file, int slot, int prevstatus)
{ {
char oldpath[1024]; char oldpath[1024];
char oldsram[1024]; char oldsram[1024];
strncpy(oldpath, ROMPath, 1024); strncpy(oldpath, ROMPath[slot], 1024);
strncpy(oldsram, SRAMPath, 1024); strncpy(oldsram, SRAMPath[slot], 1024);
strncpy(ROMPath, file, 1023); strncpy(ROMPath[slot], file, 1023);
ROMPath[1023] = '\0'; ROMPath[slot][1023] = '\0';
SetupSRAMPath(); SetupSRAMPath(0);
SetupSRAMPath(1);
if (NDS::LoadROM(ROMPath, SRAMPath, Config::DirectBoot)) if (slot == 0 && NDS::LoadROM(ROMPath[slot], SRAMPath[slot], Config::DirectBoot))
{ {
SavestateLoaded = false; SavestateLoaded = false;
uiMenuItemDisable(MenuItem_UndoStateLoad); uiMenuItemDisable(MenuItem_UndoStateLoad);
strncpy(PrevSRAMPath, SRAMPath, 1024); // safety // Reload the inserted GBA cartridge (if any)
if (ROMPath[1][0] != '\0') NDS::LoadGBAROM(ROMPath[1], SRAMPath[1]);
strncpy(PrevSRAMPath[slot], SRAMPath[slot], 1024); // safety
Run(); Run();
} }
else if (slot == 1 && NDS::LoadGBAROM(ROMPath[slot], SRAMPath[slot]))
{
SavestateLoaded = false;
uiMenuItemDisable(MenuItem_UndoStateLoad);
strncpy(PrevSRAMPath[slot], SRAMPath[slot], 1024); // safety
if (RunningSomething) Run(); // do not start just from a GBA cart
}
else else
{ {
uiMsgBoxError(MainWindow, uiMsgBoxError(MainWindow,
"Failed to load the ROM", "Failed to load the ROM",
"Make sure the file can be accessed and isn't opened in another application."); "Make sure the file can be accessed and isn't opened in another application.");
strncpy(ROMPath, oldpath, 1024); strncpy(ROMPath[slot], oldpath, 1024);
strncpy(SRAMPath, oldsram, 1024); strncpy(SRAMPath[slot], oldsram, 1024);
EmuRunning = prevstatus; EmuRunning = prevstatus;
} }
} }
@ -1735,22 +1753,22 @@ void GetSavestateName(int slot, char* filename, int len)
{ {
int pos; int pos;
if (ROMPath[0] == '\0') // running firmware, no ROM if (ROMPath[0][0] == '\0') // running firmware, no ROM
{ {
strcpy(filename, "firmware"); strcpy(filename, "firmware");
pos = 8; pos = 8;
} }
else else
{ {
int l = strlen(ROMPath); int l = strlen(ROMPath[0]);
pos = l; pos = l;
while (ROMPath[pos] != '.' && pos > 0) pos--; while (ROMPath[0][pos] != '.' && pos > 0) pos--;
if (pos == 0) pos = l; if (pos == 0) pos = l;
// avoid buffer overflow. shoddy // avoid buffer overflow. shoddy
if (pos > len-5) pos = len-5; if (pos > len-5) pos = len-5;
strncpy(&filename[0], ROMPath, pos); strncpy(&filename[0], ROMPath[0], pos);
} }
strcpy(&filename[pos], ".ml"); strcpy(&filename[pos], ".ml");
filename[pos+3] = '0'+slot; filename[pos+3] = '0'+slot;
@ -1818,16 +1836,16 @@ void LoadState(int slot)
if (!failed) if (!failed)
{ {
if (Config::SavestateRelocSRAM && ROMPath[0]!='\0') if (Config::SavestateRelocSRAM && ROMPath[0][0]!='\0')
{ {
strncpy(PrevSRAMPath, SRAMPath, 1024); strncpy(PrevSRAMPath[0], SRAMPath[0], 1024);
strncpy(SRAMPath, filename, 1019); strncpy(SRAMPath[0], filename, 1019);
int len = strlen(SRAMPath); int len = strlen(SRAMPath[0]);
strcpy(&SRAMPath[len], ".sav"); strcpy(&SRAMPath[0][len], ".sav");
SRAMPath[len+4] = '\0'; SRAMPath[0][len+4] = '\0';
NDS::RelocateSave(SRAMPath, false); NDS::RelocateSave(SRAMPath[0], false);
} }
char msg[64]; char msg[64];
@ -1883,14 +1901,14 @@ void SaveState(int slot)
if (slot > 0) if (slot > 0)
uiMenuItemEnable(MenuItem_LoadStateSlot[slot-1]); uiMenuItemEnable(MenuItem_LoadStateSlot[slot-1]);
if (Config::SavestateRelocSRAM && ROMPath[0]!='\0') if (Config::SavestateRelocSRAM && ROMPath[0][0]!='\0')
{ {
strncpy(SRAMPath, filename, 1019); strncpy(SRAMPath[0], filename, 1019);
int len = strlen(SRAMPath); int len = strlen(SRAMPath[0]);
strcpy(&SRAMPath[len], ".sav"); strcpy(&SRAMPath[0][len], ".sav");
SRAMPath[len+4] = '\0'; SRAMPath[0][len+4] = '\0';
NDS::RelocateSave(SRAMPath, true); NDS::RelocateSave(SRAMPath[0], true);
} }
} }
@ -1917,10 +1935,10 @@ void UndoStateLoad()
NDS::DoSavestate(backup); NDS::DoSavestate(backup);
delete backup; delete backup;
if (ROMPath[0]!='\0') if (ROMPath[0][0]!='\0')
{ {
strncpy(SRAMPath, PrevSRAMPath, 1024); strncpy(SRAMPath[0], PrevSRAMPath[0], 1024);
NDS::RelocateSave(SRAMPath, false); NDS::RelocateSave(SRAMPath[0], false);
} }
OSD::AddMessage(0, "State load undone"); OSD::AddMessage(0, "State load undone");
@ -1964,7 +1982,11 @@ void OnDropFile(uiWindow* window, char* file, void* blarg)
while (EmuStatus != 2); while (EmuStatus != 2);
} }
TryLoadROM(file, prevstatus); TryLoadROM(file, 0, prevstatus);
}
else if (!strcasecmp(ext, "gba"))
{
TryLoadROM(file, 1, prevstatus);
} }
} }
@ -1995,7 +2017,7 @@ void OnOpenFile(uiMenuItem* item, uiWindow* window, void* blarg)
EmuRunning = 2; EmuRunning = 2;
while (EmuStatus != 2); while (EmuStatus != 2);
char* file = uiOpenFile(window, "DS ROM (*.nds)|*.nds;*.srl|Any file|*.*", Config::LastROMFolder); char* file = uiOpenFile(window, "DS ROM (*.nds)|*.nds;*.srl|GBA ROM (*.gba)|*.gba|Any file|*.*", Config::LastROMFolder);
if (!file) if (!file)
{ {
EmuRunning = prevstatus; EmuRunning = prevstatus;
@ -2006,8 +2028,17 @@ void OnOpenFile(uiMenuItem* item, uiWindow* window, void* blarg)
while (file[pos] != '/' && file[pos] != '\\' && pos > 0) pos--; while (file[pos] != '/' && file[pos] != '\\' && pos > 0) pos--;
strncpy(Config::LastROMFolder, file, pos); strncpy(Config::LastROMFolder, file, pos);
Config::LastROMFolder[pos] = '\0'; Config::LastROMFolder[pos] = '\0';
char* ext = &file[strlen(file)-3];
if (!strcasecmp(ext, "gba"))
{
TryLoadROM(file, 1, prevstatus);
}
else
{
TryLoadROM(file, 0, prevstatus);
}
TryLoadROM(file, prevstatus);
uiFreeText(file); uiFreeText(file);
} }
@ -2032,7 +2063,7 @@ void OnRun(uiMenuItem* item, uiWindow* window, void* blarg)
{ {
if (!RunningSomething) if (!RunningSomething)
{ {
ROMPath[0] = '\0'; ROMPath[0][0] = '\0';
NDS::LoadBIOS(); NDS::LoadBIOS();
} }
@ -2852,14 +2883,30 @@ int main(int argc, char** argv)
if (!strcasecmp(ext, "nds") || !strcasecmp(ext, "srl")) if (!strcasecmp(ext, "nds") || !strcasecmp(ext, "srl"))
{ {
strncpy(ROMPath, file, 1023); strncpy(ROMPath[0], file, 1023);
ROMPath[1023] = '\0'; ROMPath[0][1023] = '\0';
SetupSRAMPath(); SetupSRAMPath(0);
if (NDS::LoadROM(ROMPath, SRAMPath, Config::DirectBoot)) if (NDS::LoadROM(ROMPath[0], SRAMPath[0], Config::DirectBoot))
Run(); Run();
} }
if (argc > 2)
{
file = argv[2];
ext = &file[strlen(file)-3];
if (!strcasecmp(ext, "gba"))
{
strncpy(ROMPath[1], file, 1023);
ROMPath[1][1023] = '\0';
SetupSRAMPath(1);
NDS::LoadGBAROM(ROMPath[1], SRAMPath[1]);
}
}
} }
uiMain(); uiMain();