From 239499f6a436c3272492484c51acdf83c9f43c97 Mon Sep 17 00:00:00 2001 From: Arisotura Date: Wed, 27 Oct 2021 12:50:16 +0200 Subject: [PATCH] add loaded ROM to DLDI volume, pass path via argv --- src/FATStorage.cpp | 54 ++++++++++++++++++++++++++++++++++++++++- src/FATStorage.h | 2 ++ src/NDSCart.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++---- src/NDSCart.h | 2 ++ 4 files changed, 113 insertions(+), 5 deletions(-) diff --git a/src/FATStorage.cpp b/src/FATStorage.cpp index 06b4c79f..4ed88644 100644 --- a/src/FATStorage.cpp +++ b/src/FATStorage.cpp @@ -55,6 +55,49 @@ void FATStorage::Close() } +bool FATStorage::InjectFile(std::string path, u8* data, u32 len) +{ + if (!File) return false; + if (FF_File) return false; + + FF_File = File; + FF_FileSize = FileSize; + ff_disk_open(FF_ReadStorage, FF_WriteStorage, (LBA_t)(FileSize>>9)); + + FRESULT res; + FATFS fs; + + res = f_mount(&fs, "0:", 1); + if (res != FR_OK) + { + ff_disk_close(); + FF_File = nullptr; + return false; + } + + path = "0:/" + path; + FF_FIL file; + res = f_open(&file, path.c_str(), FA_CREATE_ALWAYS | FA_WRITE); + if (res != FR_OK) + { + f_unmount("0:"); + ff_disk_close(); + FF_File = nullptr; + return false; + } + + u32 nwrite; + f_write(&file, data, len, &nwrite); + f_close(&file); + printf("burped hard: %d/%d\n", nwrite, len); + + f_unmount("0:"); + ff_disk_close(); + FF_File = nullptr; + return nwrite==len; +} + + u32 FATStorage::ReadSectors(u32 start, u32 num, u8* data) { return ReadSectorsInternal(File, FileSize, start, num, data); @@ -957,7 +1000,16 @@ printf("IMAGE FILE NEW: %d\n", isnew); if (hasdir) { FileSize = GetDirectorySize(sourcedir); - FileSize += 0x4000000ULL; // 64MB leeway + FileSize += 0x8000000ULL; // 128MB leeway + + // make it a power of two + FileSize |= (FileSize >> 1); + FileSize |= (FileSize >> 2); + FileSize |= (FileSize >> 4); + FileSize |= (FileSize >> 8); + FileSize |= (FileSize >> 16); + FileSize |= (FileSize >> 32); + FileSize++; } else FileSize = 0x20000000ULL; // 512MB diff --git a/src/FATStorage.h b/src/FATStorage.h index 11fd9f1b..d421c183 100644 --- a/src/FATStorage.h +++ b/src/FATStorage.h @@ -37,6 +37,8 @@ public: bool Open(); void Close(); + bool InjectFile(std::string path, u8* data, u32 len); + u32 ReadSectors(u32 start, u32 num, u8* data); u32 WriteSectors(u32 start, u32 num, u8* data); diff --git a/src/NDSCart.cpp b/src/NDSCart.cpp index d249050e..dda0b31f 100644 --- a/src/NDSCart.cpp +++ b/src/NDSCart.cpp @@ -52,6 +52,7 @@ u32 TransferDir; u8 TransferCmd[8]; bool CartInserted; +char CartName[256]; u8* CartROM; u32 CartROMSize; u32 CartID; @@ -202,7 +203,7 @@ void CartCommon::SetupDirectBoot() { CmdEncMode = 2; DataEncMode = 2; - DSiMode = false; // TODO!! + DSiMode = IsDSi && NDS::ConsoleType==1; } void CartCommon::DoSavestate(Savestate* file) @@ -1164,12 +1165,16 @@ u8 CartRetailBT::SPIWrite(u8 val, u32 pos, bool last) CartHomebrew::CartHomebrew(u8* rom, u32 len, u32 chipid) : CartCommon(rom, len, chipid) { + ReadOnly = false; // TODO + //if (Config::DLDIEnable) if (true) { - ApplyDLDIPatch(melonDLDI, sizeof(melonDLDI), false); - SD = new FATStorage("melonDLDI.bin", 0, false, "dldi"); + ApplyDLDIPatch(melonDLDI, sizeof(melonDLDI), ReadOnly); + SD = new FATStorage("melonDLDI.bin", 0, ReadOnly, "dldi"); SD->Open(); + + // fat:/rom.nds } else SD = nullptr; @@ -1191,6 +1196,40 @@ void CartHomebrew::Reset() // TODO??? something about the FATStorage thing? } +void CartHomebrew::SetupDirectBoot() +{ + CartCommon::SetupDirectBoot(); + + if (SD) + { + // add the ROM to the SD volume + + if (!SD->InjectFile(CartName, CartROM, CartROMSize)) + return; + + // setup argv command line + + char argv[512] = {0}; + int argvlen; + + strncpy(argv, "fat:/", 511); + strncat(argv, CartName, 511); + argvlen = strlen(argv); + + void (*writefn)(u32,u32) = (NDS::ConsoleType==1) ? DSi::ARM9Write32 : NDS::ARM9Write32; + + u32 argvbase = Header.ARM9RAMAddress + Header.ARM9Size; + argvbase = (argvbase + 0xF) & ~0xF; + + for (u32 i = 0; i <= argvlen; i+=4) + writefn(argvbase+i, *(u32*)&argv[i]); + + writefn(0x02FFFE70, 0x5F617267); + writefn(0x02FFFE74, argvbase); + writefn(0x02FFFE78, argvlen+1); + } +} + void CartHomebrew::DoSavestate(Savestate* file) { CartCommon::DoSavestate(file); @@ -1244,7 +1283,7 @@ void CartHomebrew::ROMCommandFinish(u8* cmd, u8* data, u32 len) case 0xC1: { u32 sector = (cmd[1]<<24) | (cmd[2]<<16) | (cmd[3]<<8) | cmd[4]; - if (SD && true) SD->WriteSectors(sector, len>>9, data); + if (SD && (!ReadOnly)) SD->WriteSectors(sector, len>>9, data); } break; @@ -1675,6 +1714,16 @@ bool LoadROM(const char* path, const char* sram, bool direct) NDS::Reset(); + char* romname = strrchr(path, '/'); + if (!romname) + { + romname = strrchr(path, '\\'); + if (!romname) + romname = (char*)&path[-1]; + } + romname++; + strncpy(CartName, romname, 255); CartName[255] = '\0'; + fseek(f, 0, SEEK_END); u32 len = (u32)ftell(f); @@ -1696,6 +1745,9 @@ bool LoadROM(const u8* romdata, u32 filelength, const char *sram, bool direct) { NDS::Reset(); + // TODO: make it more meaningful? + strncpy(CartName, "rom.nds", 256); + u32 len = filelength; CartROMSize = 0x200; while (CartROMSize < len) diff --git a/src/NDSCart.h b/src/NDSCart.h index 63d2c0e3..9f399889 100644 --- a/src/NDSCart.h +++ b/src/NDSCart.h @@ -172,6 +172,7 @@ public: ~CartHomebrew() override; void Reset() override; + void SetupDirectBoot() override; void DoSavestate(Savestate* file) override; @@ -183,6 +184,7 @@ private: void ReadROM_B7(u32 addr, u32 len, u8* data, u32 offset); FATStorage* SD; + bool ReadOnly; }; extern u16 SPICnt;