Refactor NDS and DSi to be objects (#1893)

* First crack at refactoring NDS and DSi into objects

- Remove all global/`static` variables in `NDS` and related classes
- Rely more on virtual dispatch when we need to pick methods at runtime
- Pass `NDS&` or `DSi&` to its constituent components where necessary
- Introduce some headers or move some definitions to break `#include` cycles

* Refactor the frontend to accommodate the core's changes

* Move up `SchedList`'s declaration

- Move it to before the components are initialized so the `map`s inside are initialized
- Fields in C++ are initialized in the order they're declared

* Fix a crash when allocating memory

* Fix JIT-free builds

* Fix GDB-free builds

* Fix Linux builds

- Explicitly qualify some member types in NDS, since they share the same name as their classes

* Remove an unnecessary template argument

- This was causing the build to fail on macOS

* Fix ARM and Android builds

* Rename `Constants.h` to `MemConstants.h`

* Add `NDS::IsRunning()`

* Use an `#include` guard instead of `#pragma once`
This commit is contained in:
Jesse Talavera-Greenberg
2023-11-28 17:16:41 -05:00
committed by GitHub
parent c84cb17462
commit e973236203
73 changed files with 3537 additions and 3176 deletions

View File

@ -65,7 +65,7 @@ bool FirmwareMem::VerifyCRC16(u32 start, u32 offset, u32 len, u32 crcoffset)
}
FirmwareMem::FirmwareMem(SPIHost* host) : SPIDevice(host)
FirmwareMem::FirmwareMem(melonDS::NDS& nds) : SPIDevice(nds)
{
}
@ -79,7 +79,7 @@ void FirmwareMem::Reset()
if (!Firmware)
{
Log(LogLevel::Warn, "SPI firmware: no firmware loaded! Using default\n");
Firmware = std::make_unique<class Firmware>(NDS::ConsoleType);
Firmware = std::make_unique<class Firmware>(NDS.ConsoleType);
}
// fix touchscreen coords
@ -134,31 +134,32 @@ void FirmwareMem::DoSavestate(Savestate* file)
file->Var32(&Addr);
}
void FirmwareMem::SetupDirectBoot(bool dsi)
void FirmwareMem::SetupDirectBoot()
{
const auto& header = Firmware->GetHeader();
const auto& userdata = Firmware->GetEffectiveUserData();
if (dsi)
if (NDS.ConsoleType == 1)
{
// The ARMWrite methods are virtual, they'll delegate to DSi if necessary
for (u32 i = 0; i < 6; i += 2)
DSi::ARM9Write16(0x02FFFCF4, *(u16*)&header.MacAddr[i]); // MAC address
NDS.ARM9Write16(0x02FFFCF4, *(u16*)&header.MacAddr[i]); // MAC address
// checkme
DSi::ARM9Write16(0x02FFFCFA, header.EnabledChannels); // enabled channels
NDS.ARM9Write16(0x02FFFCFA, header.EnabledChannels); // enabled channels
for (u32 i = 0; i < 0x70; i += 4)
DSi::ARM9Write32(0x02FFFC80+i, *(u32*)&userdata.Bytes[i]);
NDS.ARM9Write32(0x02FFFC80+i, *(u32*)&userdata.Bytes[i]);
}
else
{
NDS::ARM9Write32(0x027FF864, 0);
NDS::ARM9Write32(0x027FF868, header.UserSettingsOffset << 3); // user settings offset
NDS.ARM9Write32(0x027FF864, 0);
NDS.ARM9Write32(0x027FF868, header.UserSettingsOffset << 3); // user settings offset
NDS::ARM9Write16(0x027FF874, header.DataGfxChecksum); // CRC16 for data/gfx
NDS::ARM9Write16(0x027FF876, header.GUIWifiCodeChecksum); // CRC16 for GUI/wifi code
NDS.ARM9Write16(0x027FF874, header.DataGfxChecksum); // CRC16 for data/gfx
NDS.ARM9Write16(0x027FF876, header.GUIWifiCodeChecksum); // CRC16 for GUI/wifi code
for (u32 i = 0; i < 0x70; i += 4)
NDS::ARM9Write32(0x027FFC80+i, *(u32*)&userdata.Bytes[i]);
NDS.ARM9Write32(0x027FFC80+i, *(u32*)&userdata.Bytes[i]);
}
}
@ -326,7 +327,7 @@ void FirmwareMem::Release()
PowerMan::PowerMan(SPIHost* host) : SPIDevice(host)
PowerMan::PowerMan(melonDS::NDS& nds) : SPIDevice(nds)
{
}
@ -395,7 +396,7 @@ void PowerMan::Write(u8 val)
switch (regid)
{
case 0:
if (val & 0x40) NDS::Stop(StopReason::PowerOff); // shutdown
if (val & 0x40) NDS.Stop(StopReason::PowerOff); // shutdown
//printf("power %02X\n", val);
break;
case 4:
@ -410,7 +411,7 @@ void PowerMan::Write(u8 val)
TSC::TSC(SPIHost* host) : SPIDevice(host)
TSC::TSC(melonDS::NDS& nds) : SPIDevice(nds)
{
}
@ -452,13 +453,13 @@ void TSC::SetTouchCoords(u16 x, u16 y)
if (y == 0xFFF)
{
// released
NDS::KeyInput |= (1 << (16+6));
NDS.KeyInput |= (1 << (16+6));
return;
}
TouchX <<= 4;
TouchY <<= 4;
NDS::KeyInput &= ~(1 << (16+6));
NDS.KeyInput &= ~(1 << (16+6));
}
void TSC::MicInputFrame(s16* data, int samples)
@ -500,7 +501,7 @@ void TSC::Write(u8 val)
else
{
// 560190 cycles per frame
u32 cyclepos = (u32)NDS::GetSysClockCycles(2);
u32 cyclepos = (u32)NDS.GetSysClockCycles(2);
u32 samplepos = (cyclepos * MicBufferLen) / 560190;
if (samplepos >= MicBufferLen) samplepos = MicBufferLen-1;
s16 sample = MicBuffer[samplepos];
@ -529,17 +530,17 @@ void TSC::Write(u8 val)
SPIHost::SPIHost()
SPIHost::SPIHost(melonDS::NDS& nds) : NDS(nds)
{
NDS::RegisterEventFunc(NDS::Event_SPITransfer, 0, MemberEventFunc(SPIHost, TransferDone));
NDS.RegisterEventFunc(Event_SPITransfer, 0, MemberEventFunc(SPIHost, TransferDone));
Devices[SPIDevice_FirmwareMem] = new FirmwareMem(this);
Devices[SPIDevice_PowerMan] = new PowerMan(this);
Devices[SPIDevice_FirmwareMem] = new FirmwareMem(NDS);
Devices[SPIDevice_PowerMan] = new PowerMan(NDS);
if (NDS::ConsoleType == 1)
Devices[SPIDevice_TSC] = new DSi_TSC(this);
if (NDS.ConsoleType == 1)
Devices[SPIDevice_TSC] = new DSi_TSC(static_cast<DSi&>(NDS));
else
Devices[SPIDevice_TSC] = new TSC(this);
Devices[SPIDevice_TSC] = new TSC(NDS);
}
SPIHost::~SPIHost()
@ -552,7 +553,7 @@ SPIHost::~SPIHost()
Devices[i] = nullptr;
}
NDS::UnregisterEventFunc(NDS::Event_SPITransfer, 0);
NDS.UnregisterEventFunc(Event_SPITransfer, 0);
}
void SPIHost::Reset()
@ -603,7 +604,7 @@ void SPIHost::TransferDone(u32 param)
Cnt &= ~(1<<7);
if (Cnt & (1<<14))
NDS::SetIRQ(1, NDS::IRQ_SPI);
NDS.SetIRQ(1, IRQ_SPI);
}
u8 SPIHost::ReadData()
@ -641,7 +642,7 @@ void SPIHost::WriteData(u8 val)
// SPI transfers one bit per cycle -> 8 cycles per byte
u32 delay = 8 * (8 << (Cnt & 0x3));
NDS::ScheduleEvent(NDS::Event_SPITransfer, false, delay, 0, 0);
NDS.ScheduleEvent(Event_SPITransfer, false, delay, 0, 0);
}
}