move DLDI shenanigans to CartHomebrew, too

This commit is contained in:
Arisotura 2021-04-14 23:22:01 +02:00
parent 41230881b2
commit 2c5049d40a
2 changed files with 31 additions and 27 deletions

View File

@ -494,8 +494,6 @@ u32 CartID;
bool CartIsHomebrew; bool CartIsHomebrew;
bool CartIsDSi; bool CartIsDSi;
FILE* CartSD;
CartCommon* Cart; CartCommon* Cart;
u32 Key1_KeyBuf[0x412]; u32 Key1_KeyBuf[0x412];
@ -1336,18 +1334,28 @@ u8 CartRetailNAND::SPIWrite(u8 val, u32 pos, bool last)
CartHomebrew::CartHomebrew(u8* rom, u32 len, u32 chipid) : CartCommon(rom, len, chipid) CartHomebrew::CartHomebrew(u8* rom, u32 len, u32 chipid) : CartCommon(rom, len, chipid)
{ {
// TODO: presumably CartSD loading should go here
if (Config::DLDIEnable) if (Config::DLDIEnable)
{
ApplyDLDIPatch(melonDLDI, sizeof(melonDLDI)); ApplyDLDIPatch(melonDLDI, sizeof(melonDLDI));
SDFile = Platform::OpenLocalFile(Config::DLDISDPath, "r+b");
}
else
SDFile = nullptr;
} }
CartHomebrew::~CartHomebrew() CartHomebrew::~CartHomebrew()
{ {
if (SDFile) fclose(SDFile);
} }
void CartHomebrew::Reset() void CartHomebrew::Reset()
{ {
if (SDFile) fclose(SDFile);
if (Config::DLDIEnable)
SDFile = Platform::OpenLocalFile(Config::DLDISDPath, "r+b");
else
SDFile = nullptr;
} }
void CartHomebrew::DoSavestate(Savestate* file) void CartHomebrew::DoSavestate(Savestate* file)
@ -1380,10 +1388,10 @@ int CartHomebrew::ROMCommandStart(u8* cmd, u8* data, u32 len)
u32 sector = (cmd[1]<<24) | (cmd[2]<<16) | (cmd[3]<<8) | cmd[4]; u32 sector = (cmd[1]<<24) | (cmd[2]<<16) | (cmd[3]<<8) | cmd[4];
u64 addr = sector * 0x200ULL; u64 addr = sector * 0x200ULL;
if (CartSD) if (SDFile)
{ {
fseek(CartSD, addr, SEEK_SET); fseek(SDFile, addr, SEEK_SET);
fread(data, len, 1, CartSD); fread(data, len, 1, SDFile);
} }
} }
return 0; return 0;
@ -1398,6 +1406,8 @@ int CartHomebrew::ROMCommandStart(u8* cmd, u8* data, u32 len)
void CartHomebrew::ROMCommandFinish(u8* cmd, u8* data, u32 len) void CartHomebrew::ROMCommandFinish(u8* cmd, u8* data, u32 len)
{ {
// TODO: delayed SD writing? like we have for SRAM
switch (cmd[0]) switch (cmd[0])
{ {
case 0xC1: case 0xC1:
@ -1405,10 +1415,10 @@ void CartHomebrew::ROMCommandFinish(u8* cmd, u8* data, u32 len)
u32 sector = (cmd[1]<<24) | (cmd[2]<<16) | (cmd[3]<<8) | cmd[4]; u32 sector = (cmd[1]<<24) | (cmd[2]<<16) | (cmd[3]<<8) | cmd[4];
u64 addr = sector * 0x200ULL; u64 addr = sector * 0x200ULL;
if (CartSD) if (SDFile)
{ {
fseek(CartSD, addr, SEEK_SET); fseek(SDFile, addr, SEEK_SET);
fwrite(data, len, 1, CartSD); fwrite(data, len, 1, SDFile);
} }
} }
break; break;
@ -1418,7 +1428,7 @@ void CartHomebrew::ROMCommandFinish(u8* cmd, u8* data, u32 len)
} }
} }
void CartHomebrew::ApplyDLDIPatch(const u8* patch, u32 len) void CartHomebrew::ApplyDLDIPatch(const u8* patch, u32 patchlen)
{ {
u32 offset = *(u32*)&ROM[0x20]; u32 offset = *(u32*)&ROM[0x20];
u32 size = *(u32*)&ROM[0x2C]; u32 size = *(u32*)&ROM[0x2C];
@ -1471,7 +1481,7 @@ void CartHomebrew::ApplyDLDIPatch(const u8* patch, u32 len)
u32 patchsize = 1 << patch[0x0D]; u32 patchsize = 1 << patch[0x0D];
u32 patchend = patchbase + patchsize; u32 patchend = patchbase + patchsize;
memcpy(&binary[dldioffset], patch, len); memcpy(&binary[dldioffset], patch, patchlen);
*(u32*)&binary[dldioffset+0x40] += delta; *(u32*)&binary[dldioffset+0x40] += delta;
*(u32*)&binary[dldioffset+0x44] += delta; *(u32*)&binary[dldioffset+0x44] += delta;
@ -1560,9 +1570,6 @@ bool Init()
if (!NDSCart_SRAM::Init()) return false; if (!NDSCart_SRAM::Init()) return false;
CartROM = nullptr; CartROM = nullptr;
CartSD = nullptr;
Cart = nullptr; Cart = nullptr;
return true; return true;
@ -1571,9 +1578,6 @@ bool Init()
void DeInit() void DeInit()
{ {
if (CartROM) delete[] CartROM; if (CartROM) delete[] CartROM;
if (CartSD) fclose(CartSD);
if (Cart) delete Cart; if (Cart) delete Cart;
NDSCart_SRAM::DeInit(); NDSCart_SRAM::DeInit();
@ -1589,9 +1593,6 @@ void Reset()
CartIsHomebrew = false; CartIsHomebrew = false;
CartIsDSi = false; CartIsDSi = false;
if (CartSD) fclose(CartSD);
CartSD = nullptr;
if (Cart) delete Cart; if (Cart) delete Cart;
Cart = nullptr; Cart = nullptr;
@ -1785,9 +1786,6 @@ bool LoadROMCommon(u32 filelength, const char *sram, bool direct)
// ApplyDLDIPatch(melonDLDI, sizeof(melonDLDI)); // ApplyDLDIPatch(melonDLDI, sizeof(melonDLDI));
} }
if (direct)
NDS::SetupDirectBoot();
CartInserted = true; CartInserted = true;
// TODO: support more fancy cart types (homebrew?, flashcarts, etc) // TODO: support more fancy cart types (homebrew?, flashcarts, etc)
@ -1810,7 +1808,11 @@ bool LoadROMCommon(u32 filelength, const char *sram, bool direct)
if (Cart) if (Cart)
{ {
Cart->Reset(); Cart->Reset();
if (direct) Cart->SetupDirectBoot(); if (direct)
{
NDS::SetupDirectBoot();
Cart->SetupDirectBoot();
}
} }
// encryption // encryption
@ -1821,12 +1823,12 @@ bool LoadROMCommon(u32 filelength, const char *sram, bool direct)
//NDSCart_SRAM::LoadSave(sram, romparams.SaveMemType); //NDSCart_SRAM::LoadSave(sram, romparams.SaveMemType);
if (Cart) Cart->LoadSave(sram, romparams.SaveMemType); if (Cart) Cart->LoadSave(sram, romparams.SaveMemType);
if (CartIsHomebrew && Config::DLDIEnable) /*if (CartIsHomebrew && Config::DLDIEnable)
{ {
CartSD = Platform::OpenLocalFile(Config::DLDISDPath, "r+b"); CartSD = Platform::OpenLocalFile(Config::DLDISDPath, "r+b");
} }
else else
CartSD = NULL; CartSD = NULL;*/
return true; return true;
} }

View File

@ -206,6 +206,8 @@ public:
private: private:
void ApplyDLDIPatch(const u8* patch, u32 len); void ApplyDLDIPatch(const u8* patch, u32 len);
void ReadROM_B7(u32 addr, u32 len, u8* data, u32 offset); void ReadROM_B7(u32 addr, u32 len, u8* data, u32 offset);
FILE* SDFile;
}; };
extern u16 SPICnt; extern u16 SPICnt;