diff options
author | Jesse Talavera-Greenberg <jesse@jesse.tg> | 2023-11-28 17:16:41 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-28 23:16:41 +0100 |
commit | e973236203292637eb7bd009a4cfbd6fd785181f (patch) | |
tree | 4c348a9927bfa6f8f37cc943291174a1096434b3 /src/SPI.cpp | |
parent | c84cb174628c5a2e8e6cc0179e16de3eab47864a (diff) |
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`
Diffstat (limited to 'src/SPI.cpp')
-rw-r--r-- | src/SPI.cpp | 57 |
1 files changed, 29 insertions, 28 deletions
diff --git a/src/SPI.cpp b/src/SPI.cpp index 3ded7bc..b3e5b4e 100644 --- a/src/SPI.cpp +++ b/src/SPI.cpp @@ -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); } }
\ No newline at end of file |