make things work, sorta kinda (I lost track)

This commit is contained in:
Arisotura 2022-01-02 23:46:58 +01:00
parent 18c7629451
commit cdff61c209
12 changed files with 172 additions and 106 deletions

View File

@ -47,8 +47,6 @@
namespace DSi
{
u32 BootAddr[2];
u16 SCFG_BIOS;
u16 SCFG_Clock9;
u16 SCFG_Clock7;
@ -79,14 +77,9 @@ DSi_NDMA* NDMAs[8];
DSi_SDHost* SDMMC;
DSi_SDHost* SDIO;
FILE* SDMMCFile = nullptr;
u64 ConsoleID;
u8 eMMC_CID[16];
u8 ITCMInit[0x8000];
u8 ARM7Init[0x3C00];
void Set_SCFG_Clock9(u16 val);
@ -134,8 +127,6 @@ void DeInit()
delete SDMMC;
delete SDIO;
CloseDSiNAND();
}
void Reset()
@ -144,21 +135,24 @@ void Reset()
//NDS::ARM9->CP15Write(0x911, 0x00000020);
//NDS::ARM9->CP15Write(0x100, NDS::ARM9->CP15Read(0x100) | 0x00050000);
NDS::ARM9->JumpTo(BootAddr[0]);
NDS::ARM7->JumpTo(BootAddr[1]);
NDS::MapSharedWRAM(3);
NDMACnt[0] = 0; NDMACnt[1] = 0;
for (int i = 0; i < 8; i++) NDMAs[i]->Reset();
memcpy(NDS::ARM9->ITCM, ITCMInit, 0x8000);
DSi_I2C::Reset();
DSi_AES::Reset();
DSi_DSP::Reset();
SDMMC->CloseHandles();
SDIO->CloseHandles();
LoadNAND();
SDMMC->Reset();
SDIO->Reset();
DSi_AES::Reset();
SCFG_BIOS = 0x0101; // TODO: should be zero when booting from BIOS
SCFG_Clock9 = 0x0187; // CHECKME
SCFG_Clock7 = 0x0187;
@ -172,22 +166,6 @@ void Reset()
// LCD init flag
GPU::DispStat[0] |= (1<<6);
GPU::DispStat[1] |= (1<<6);
NDS::MapSharedWRAM(3);
for (u32 i = 0; i < 0x3C00; i+=4)
ARM7Write32(0x03FFC400+i, *(u32*)&ARM7Init[i]);
u32 eaddr = 0x03FFE6E4;
ARM7Write32(eaddr+0x00, *(u32*)&eMMC_CID[0]);
ARM7Write32(eaddr+0x04, *(u32*)&eMMC_CID[4]);
ARM7Write32(eaddr+0x08, *(u32*)&eMMC_CID[8]);
ARM7Write32(eaddr+0x0C, *(u32*)&eMMC_CID[12]);
ARM7Write16(eaddr+0x2C, 0x0001);
ARM7Write16(eaddr+0x2E, 0x0001);
ARM7Write16(eaddr+0x3C, 0x0100);
ARM7Write16(eaddr+0x3E, 0x40E0);
ARM7Write16(eaddr+0x42, 0x0001);
}
void DecryptModcryptArea(u32 offset, u32 size, u8* iv)
@ -445,24 +423,30 @@ void SetupDirectBoot()
ARM9Write32(0x02FFE000+i, tmp);
}
if (DSi_NAND::Init(SDMMCFile, &DSi::ARM7iBIOS[0x8308]))
FILE* nand = Platform::OpenLocalFile(Platform::GetConfigString(Platform::DSi_NANDPath), "r+b");
if (nand)
{
u8 userdata[0x1B0];
DSi_NAND::ReadUserData(userdata);
for (u32 i = 0; i < 0x128; i+=4)
ARM9Write32(0x02000400+i, *(u32*)&userdata[0x88+i]);
if (DSi_NAND::Init(nand, &DSi::ARM7iBIOS[0x8308]))
{
u8 userdata[0x1B0];
DSi_NAND::ReadUserData(userdata);
for (u32 i = 0; i < 0x128; i+=4)
ARM9Write32(0x02000400+i, *(u32*)&userdata[0x88+i]);
u8 hwinfoS[0xA4];
u8 hwinfoN[0x9C];
DSi_NAND::ReadHardwareInfo(hwinfoS, hwinfoN);
u8 hwinfoS[0xA4];
u8 hwinfoN[0x9C];
DSi_NAND::ReadHardwareInfo(hwinfoS, hwinfoN);
for (u32 i = 0; i < 0x14; i+=4)
ARM9Write32(0x02000600+i, *(u32*)&hwinfoN[0x88+i]);
for (u32 i = 0; i < 0x14; i+=4)
ARM9Write32(0x02000600+i, *(u32*)&hwinfoN[0x88+i]);
for (u32 i = 0; i < 0x18; i+=4)
ARM9Write32(0x02FFFD68+i, *(u32*)&hwinfoS[0x88+i]);
for (u32 i = 0; i < 0x18; i+=4)
ARM9Write32(0x02FFFD68+i, *(u32*)&hwinfoS[0x88+i]);
DSi_NAND::DeInit();
DSi_NAND::DeInit();
}
fclose(nand);
}
u8 nwifiver = SPI_Firmware::GetNWifiVersion();
@ -544,21 +528,28 @@ void SoftReset()
NDS::ARM9->CP15Reset();
memcpy(NDS::ARM9->ITCM, ITCMInit, 0x8000);
NDS::MapSharedWRAM(3);
// FIXME!!!!!
//memcpy(NDS::ARM9->ITCM, ITCMInit, 0x8000);
DSi_AES::Reset();
// TODO: does the DSP get reset? NWRAM doesn't, so I'm assuming no
// *HOWEVER*, the bootrom (which does get rerun) does remap NWRAM, and thus
// the DSP most likely gets reset
DSi_DSP::Reset();
SDMMC->CloseHandles();
SDIO->CloseHandles();
LoadNAND();
SDMMC->Reset();
SDIO->Reset();
NDS::ARM9->JumpTo(BootAddr[0]);
NDS::ARM7->JumpTo(BootAddr[1]);
DSi_AES::Reset();
//NDS::ARM9->JumpTo(BootAddr[0]);
//NDS::ARM7->JumpTo(BootAddr[1]);
SCFG_BIOS = 0x0101; // TODO: should be zero when booting from BIOS
SCFG_Clock9 = 0x0187; // CHECKME
@ -575,10 +566,8 @@ void SoftReset()
GPU::DispStat[0] |= (1<<6);
GPU::DispStat[1] |= (1<<6);
NDS::MapSharedWRAM(3);
for (u32 i = 0; i < 0x3C00; i+=4)
ARM7Write32(0x03FFC400+i, *(u32*)&ARM7Init[i]);
//for (u32 i = 0; i < 0x3C00; i+=4)
// ARM7Write32(0x03FFC400+i, *(u32*)&ARM7Init[i]);
u32 eaddr = 0x03FFE6E4;
ARM7Write32(eaddr+0x00, *(u32*)&eMMC_CID[0]);
@ -650,7 +639,14 @@ bool LoadNAND()
{
printf("Loading DSi NAND\n");
if (!DSi_NAND::Init(SDMMCFile, &DSi::ARM7iBIOS[0x8308]))
FILE* nand = Platform::OpenLocalFile(Platform::GetConfigString(Platform::DSi_NANDPath), "r+b");
if (!nand)
{
printf("Failed to open DSi NAND\n");
return false;
}
if (!DSi_NAND::Init(nand, &DSi::ARM7iBIOS[0x8308]))
{
printf("Failed to load DSi NAND\n");
return false;
@ -676,8 +672,8 @@ bool LoadNAND()
memset(NWRAMMask, 0, sizeof(NWRAMMask));
u32 bootparams[8];
fseek(SDMMCFile, 0x220, SEEK_SET);
fread(bootparams, 4, 8, SDMMCFile);
fseek(nand, 0x220, SEEK_SET);
fread(bootparams, 4, 8, nand);
printf("ARM9: offset=%08X size=%08X RAM=%08X size_aligned=%08X\n",
bootparams[0], bootparams[1], bootparams[2], bootparams[3]);
@ -690,8 +686,8 @@ bool LoadNAND()
MBK[1][8] = 0;
u32 mbk[12];
fseek(SDMMCFile, 0x380, SEEK_SET);
fread(mbk, 4, 12, SDMMCFile);
fseek(nand, 0x380, SEEK_SET);
fread(mbk, 4, 12, nand);
MapNWRAM_A(0, mbk[0] & 0xFF);
MapNWRAM_A(1, (mbk[0] >> 8) & 0xFF);
@ -745,12 +741,12 @@ bool LoadNAND()
AES_init_ctx_iv(&ctx, boot2key, boot2iv);
fseek(SDMMCFile, bootparams[0], SEEK_SET);
fseek(nand, bootparams[0], SEEK_SET);
dstaddr = bootparams[2];
for (u32 i = 0; i < bootparams[3]; i += 16)
{
u8 data[16];
fread(data, 16, 1, SDMMCFile);
fread(data, 16, 1, nand);
for (int j = 0; j < 16; j++) tmp[j] = data[15-j];
AES_CTR_xcrypt_buffer(&ctx, tmp, 16);
@ -770,12 +766,12 @@ bool LoadNAND()
AES_init_ctx_iv(&ctx, boot2key, boot2iv);
fseek(SDMMCFile, bootparams[4], SEEK_SET);
fseek(nand, bootparams[4], SEEK_SET);
dstaddr = bootparams[6];
for (u32 i = 0; i < bootparams[7]; i += 16)
{
u8 data[16];
fread(data, 16, 1, SDMMCFile);
fread(data, 16, 1, nand);
for (int j = 0; j < 16; j++) tmp[j] = data[15-j];
AES_CTR_xcrypt_buffer(&ctx, tmp, 16);
@ -787,11 +783,6 @@ bool LoadNAND()
ARM7Write32(dstaddr, *(u32*)&data[12]); dstaddr += 4;
}
// repoint the CPUs to the boot2 binaries
BootAddr[0] = bootparams[2];
BootAddr[1] = bootparams[6];
#define printhex(str, size) { for (int z = 0; z < (size); z++) printf("%02X", (str)[z]); printf("\n"); }
#define printhex_rev(str, size) { for (int z = (size)-1; z >= 0; z--) printf("%02X", (str)[z]); printf("\n"); }
@ -800,18 +791,36 @@ bool LoadNAND()
printf("eMMC CID: "); printhex(eMMC_CID, 16);
printf("Console ID: %" PRIx64 "\n", ConsoleID);
memset(ITCMInit, 0, 0x8000);
memcpy(&ITCMInit[0x4400], &ARM9iBIOS[0x87F4], 0x400);
memcpy(&ITCMInit[0x4800], &ARM9iBIOS[0x9920], 0x80);
memcpy(&ITCMInit[0x4894], &ARM9iBIOS[0x99A0], 0x1048);
memcpy(&ITCMInit[0x58DC], &ARM9iBIOS[0xA9E8], 0x1048);
u32 eaddr = 0x03FFE6E4;
ARM7Write32(eaddr+0x00, *(u32*)&eMMC_CID[0]);
ARM7Write32(eaddr+0x04, *(u32*)&eMMC_CID[4]);
ARM7Write32(eaddr+0x08, *(u32*)&eMMC_CID[8]);
ARM7Write32(eaddr+0x0C, *(u32*)&eMMC_CID[12]);
ARM7Write16(eaddr+0x2C, 0x0001);
ARM7Write16(eaddr+0x2E, 0x0001);
ARM7Write16(eaddr+0x3C, 0x0100);
ARM7Write16(eaddr+0x3E, 0x40E0);
ARM7Write16(eaddr+0x42, 0x0001);
memcpy(&NDS::ARM9->ITCM[0x4400], &ARM9iBIOS[0x87F4], 0x400);
memcpy(&NDS::ARM9->ITCM[0x4800], &ARM9iBIOS[0x9920], 0x80);
memcpy(&NDS::ARM9->ITCM[0x4894], &ARM9iBIOS[0x99A0], 0x1048);
memcpy(&NDS::ARM9->ITCM[0x58DC], &ARM9iBIOS[0xA9E8], 0x1048);
u8 ARM7Init[0x3C00];
memset(ARM7Init, 0, 0x3C00);
memcpy(&ARM7Init[0x0000], &ARM7iBIOS[0x8188], 0x200);
memcpy(&ARM7Init[0x0200], &ARM7iBIOS[0xB5D8], 0x40);
memcpy(&ARM7Init[0x0254], &ARM7iBIOS[0xC6D0], 0x1048);
memcpy(&ARM7Init[0x129C], &ARM7iBIOS[0xD718], 0x1048);
for (u32 i = 0; i < 0x3C00; i+=4)
ARM7Write32(0x03FFC400+i, *(u32*)&ARM7Init[i]);
// repoint the CPUs to the boot2 binaries
NDS::ARM9->JumpTo(bootparams[2]);
NDS::ARM7->JumpTo(bootparams[6]);
DSi_NAND::PatchUserData();
DSi_NAND::DeInit();
@ -819,12 +828,6 @@ bool LoadNAND()
return true;
}
void CloseDSiNAND()
{
if (DSi::SDMMCFile)
fclose(DSi::SDMMCFile);
DSi::SDMMCFile = nullptr;
}
void RunNDMAs(u32 cpu)
{

View File

@ -39,8 +39,6 @@ extern u64 ConsoleID;
extern DSi_SDHost* SDMMC;
extern DSi_SDHost* SDIO;
extern FILE* SDMMCFile;
const u32 NWRAMSize = 0x40000;
extern u8* NWRAM_A;
@ -65,8 +63,6 @@ void SoftReset();
bool LoadBIOS();
bool LoadNAND();
void CloseDSiNAND();
void RunNDMAs(u32 cpu);
void StallNDMAs();
bool NDMAsInMode(u32 cpu, u32 mode);

View File

@ -61,6 +61,14 @@ DSi_SDHost::~DSi_SDHost()
if (Ports[1]) delete Ports[1];
}
void DSi_SDHost::CloseHandles()
{
if (Ports[0]) delete Ports[0];
if (Ports[1]) delete Ports[1];
Ports[0] = nullptr;
Ports[1] = nullptr;
}
void DSi_SDHost::Reset()
{
if (Num == 0)
@ -101,10 +109,7 @@ void DSi_SDHost::Reset()
TXReq = false;
if (Ports[0]) delete Ports[0];
if (Ports[1]) delete Ports[1];
Ports[0] = nullptr;
Ports[1] = nullptr;
CloseHandles();
if (Num == 0)
{
@ -131,7 +136,7 @@ void DSi_SDHost::Reset()
else
sd = nullptr;
mmc = new DSi_MMCStorage(this, true, DSi::SDMMCFile);
mmc = new DSi_MMCStorage(this, true, Platform::GetConfigString(Platform::DSi_NANDPath));
mmc->SetCID(DSi::eMMC_CID);
Ports[0] = sd;
@ -727,12 +732,15 @@ void DSi_SDHost::CheckSwapFIFO()
#define MMC_DESC (Internal?"NAND":"SDcard")
DSi_MMCStorage::DSi_MMCStorage(DSi_SDHost* host, bool internal, FILE* file)
DSi_MMCStorage::DSi_MMCStorage(DSi_SDHost* host, bool internal, std::string filename)
: DSi_SDDevice(host)
{
Internal = internal;
File = file;
File = Platform::OpenLocalFile(filename, "r+b");
printf("BLAGFARTED: %s -> %p\n", filename.c_str(), File);
SD = nullptr;
ReadOnly = false;
}
DSi_MMCStorage::DSi_MMCStorage(DSi_SDHost* host, bool internal, std::string filename, u64 size, bool readonly, std::string sourcedir)
@ -754,6 +762,10 @@ DSi_MMCStorage::~DSi_MMCStorage()
SD->Close();
delete SD;
}
if (File)
{
fclose(File);
}
}
void DSi_MMCStorage::Reset()

View File

@ -33,6 +33,7 @@ public:
DSi_SDHost(u32 num);
~DSi_SDHost();
void CloseHandles();
void Reset();
void DoSavestate(Savestate* file);
@ -122,7 +123,7 @@ protected:
class DSi_MMCStorage : public DSi_SDDevice
{
public:
DSi_MMCStorage(DSi_SDHost* host, bool internal, FILE* file);
DSi_MMCStorage(DSi_SDHost* host, bool internal, std::string filename);
DSi_MMCStorage(DSi_SDHost* host, bool internal, std::string filename, u64 size, bool readonly, std::string sourcedir);
~DSi_MMCStorage();

View File

@ -565,7 +565,6 @@ void Reset()
if (ConsoleType == 1)
{
DSi::LoadBIOS();
DSi::LoadNAND();
ARM9ClockShift = 2;
MainRAMMask = 0xFFFFFF;
@ -932,6 +931,11 @@ void EjectCart()
NDSCart::EjectCart();
}
bool CartInserted()
{
return NDSCart::CartInserted;
}
bool LoadGBACart(const u8* romdata, u32 romlen, const u8* savedata, u32 savelen)
{
if (!GBACart::LoadROM(romdata, romlen))

View File

@ -240,6 +240,7 @@ void LoadBIOS();
bool LoadCart(const u8* romdata, u32 romlen, const u8* savedata, u32 savelen);
void EjectCart();
bool CartInserted();
bool NeedsDirectBoot();
void SetupDirectBoot(std::string romname);

View File

@ -1489,6 +1489,7 @@ void CartHomebrew::ReadROM_B7(u32 addr, u32 len, u8* data, u32 offset)
bool Init()
{
CartInserted = false;
CartROM = nullptr;
Cart = nullptr;
@ -1503,14 +1504,14 @@ void DeInit()
void Reset()
{
if (Cart) delete Cart;
/*if (Cart) delete Cart;
Cart = nullptr;
CartInserted = false;
if (CartROM) delete[] CartROM;
CartROM = nullptr;
CartROMSize = 0;
CartID = 0;
CartID = 0;*/
ResetCart();
}
@ -1716,7 +1717,7 @@ bool LoadROM(const u8* romdata, u32 romlen)
Key1_Encrypt((u32*)&CartROM[arm9base]);
}
}
printf("FAZIL");
CartInserted = true;
u32 irversion = 0;
@ -1738,7 +1739,7 @@ printf("FAZIL");
Cart = new CartRetailBT(CartROM, CartROMSize, CartID);
else
Cart = new CartRetail(CartROM, CartROMSize, CartID);
printf("PFART\n");
if (Cart)
Cart->Reset();
/*{
@ -1759,14 +1760,14 @@ printf("PFART\n");
//if (Cart) Cart->LoadSave(sram, romparams.SaveMemType);
if (Cart && romparams.SaveMemType > 0)
Cart->SetupSave(romparams.SaveMemType);
printf("PROEUPRAOUTTE\n");
return true;
}
void LoadSave(const u8* savedata, u32 savelen)
{printf("CARTSAVE\n");
{
if (Cart)
Cart->LoadSave(savedata, savelen);printf("SAVE FARTED\n");
Cart->LoadSave(savedata, savelen);
}
/*bool LoadROM(const char* path, const char* sram, bool direct)

View File

@ -196,6 +196,7 @@ extern u32 ROMCnt;
extern u8 ROMCommand[8];
extern bool CartInserted;
extern u8* CartROM;
extern u32 CartROMSize;

View File

@ -220,7 +220,7 @@ int SetupDSiNAND()
// TODO: some basic checks
// check that it has the nocash footer, and all
DSi::SDMMCFile = f;
//DSi::SDMMCFile = f;
return Load_OK;
}
@ -253,7 +253,7 @@ void LoadCheats()
int LoadBIOS()
{
DSi::CloseDSiNAND();
//DSi::CloseDSiNAND();
int res;
@ -373,7 +373,7 @@ int LoadROM(const u8 *romdata, u32 romlength, const char *archivefilename, const
int LoadROM(const char* file, int slot)
{
DSi::CloseDSiNAND();
//DSi::CloseDSiNAND();
int res;
bool directboot = Config::DirectBoot != 0;
@ -530,12 +530,12 @@ void UnloadROM(int slot)
ROMPath[slot] = "";
DSi::CloseDSiNAND();
// DSi::CloseDSiNAND();
}
int Reset()
{
DSi::CloseDSiNAND();
// DSi::CloseDSiNAND();
int res;
bool directboot = Config::DirectBoot != 0;

View File

@ -42,6 +42,7 @@ namespace ROMLoader
std::string FullROMPath;
std::string BaseROMDir;
std::string BaseROMName;
std::string BaseAssetName;
int LastSep(std::string path)
@ -75,7 +76,7 @@ std::string GetAssetPath(std::string configpath, std::string ext)
if (!configpath.empty())
configpath += "/";
return configpath + BaseROMName + ext;
return configpath + BaseAssetName + ext;
}
@ -257,12 +258,15 @@ QString VerifySetup()
bool LoadBIOS()
{
NDS::SetConsoleType(Config::ConsoleType);
if (NDS::NeedsDirectBoot())
return false;
FullROMPath = "";
BaseROMDir = "";
BaseROMName = "";
BaseAssetName = "";
NDS::Reset();
return true;
@ -343,10 +347,12 @@ bool LoadROM(QStringList filepath, bool reset)
FullROMPath = fullpath;
BaseROMDir = basepath;
BaseROMName = romname.substr(0, romname.rfind('.'));
BaseROMName = romname;
BaseAssetName = romname.substr(0, romname.rfind('.'));
if (reset)
{
NDS::SetConsoleType(Config::ConsoleType);
NDS::Reset();
}
@ -367,7 +373,7 @@ bool LoadROM(QStringList filepath, bool reset)
}
bool res = NDS::LoadCart(filedata, filelen, savedata, savelen);
if (res)
if (res && reset)
{
if (Config::DirectBoot || NDS::NeedsDirectBoot())
{
@ -387,6 +393,30 @@ void EjectCart()
FullROMPath = "";
BaseROMDir = "";
BaseROMName = "";
BaseAssetName = "";
}
QString CartLabel()
{
if (BaseROMName.empty())
return "(none)";
QString ret = QString::fromStdString(BaseROMName);
int maxlen = 32;
if (ret.length() > maxlen)
ret = ret.left(maxlen-6) + "..." + ret.right(3);
return ret;
}
// PLACEHOLDER
QString GBACartLabel()
{
return "(none)";
}

View File

@ -31,6 +31,11 @@ QString VerifySetup();
bool LoadBIOS();
bool LoadROM(QStringList filepath, bool reset);
void EjectCart();
QString CartLabel();
bool LoadGBAROM(QStringList filepath, bool reset);
void EjectGBACart();
QString GBACartLabel();
enum
{

View File

@ -1308,7 +1308,7 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
menu->addSeparator();
actCurrentCart = menu->addAction("Slot 1: princessbourf.nds");
actCurrentCart = menu->addAction("DS slot: " + ROMLoader::CartLabel());
actCurrentCart->setEnabled(false);
actInsertCart = menu->addAction("Insert cart...");
@ -1319,7 +1319,7 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
menu->addSeparator();
actCurrentGBACart = menu->addAction("Slot 2: Fartslapper Mk. II");
actCurrentGBACart = menu->addAction("GBA slot: " + ROMLoader::GBACartLabel());
actCurrentGBACart->setEnabled(false);
actInsertGBACart = menu->addAction("Insert ROM cart...");
@ -1614,6 +1614,9 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
createScreenPanel();
actEjectCart->setEnabled(false);
actEjectGBACart->setEnabled(false);
for (int i = 0; i < 9; i++)
{
actSaveState[i]->setEnabled(false);
@ -2149,6 +2152,9 @@ void MainWindow::onOpenFile()
NDS::Start();
emuThread->emuRun();
actCurrentCart->setText("DS slot: " + ROMLoader::CartLabel());
actEjectCart->setEnabled(true);
}
/*QString MainWindow::pickAndExtractFileFromArchive(QString archiveFileName, QByteArray *romBuffer)
@ -2333,6 +2339,9 @@ void MainWindow::onInsertCart()
}
emuThread->emuUnpause();
actCurrentCart->setText("DS slot: " + ROMLoader::CartLabel());
actEjectCart->setEnabled(true);
}
void MainWindow::onEjectCart()
@ -2342,6 +2351,9 @@ void MainWindow::onEjectCart()
ROMLoader::EjectCart();
emuThread->emuUnpause();
actCurrentCart->setText("DS slot: " + ROMLoader::CartLabel());
actEjectCart->setEnabled(false);
}
void MainWindow::onSaveState()