From caa009ca033739b08af2794728c5e046ddd451b4 Mon Sep 17 00:00:00 2001 From: Jaklyy <102590697+Jaklyy@users.noreply.github.com> Date: Sat, 21 Dec 2024 13:19:34 -0500 Subject: [PATCH] better logic for gamecard reads --- src/NDS.cpp | 18 ++++++++++++++++-- src/NDS.h | 1 + src/NDSCart.cpp | 46 ++++++++++++++++------------------------------ src/NDSCart.h | 5 ++--- 4 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/NDS.cpp b/src/NDS.cpp index 90008e04..04af0079 100644 --- a/src/NDS.cpp +++ b/src/NDS.cpp @@ -797,7 +797,7 @@ void NDS::SetARM9BIOS(const std::array& bios) noexcept u64 NDS::NextTarget() { - u64 minEvent = std::max(SysTimestamp+1, NDSCartSlot.ROMTransferTime[0]); + u64 minEvent = UINT64_MAX; u32 mask = SchedListMask; for (int i = 0; i < Event_MAX; i++) @@ -845,6 +845,21 @@ void NDS::RunSystem(u64 timestamp) } } +void NDS::RunEventManual(u32 id) +{ + if (SchedListMask & (1<> ARM9ClockShift); + SchedEvent& evt = SchedList[id]; + + if (evt.Timestamp <= curts) + { + evt.Funcs[evt.FuncID](evt.That, evt.Param); + SchedListMask &= ~(1<&& rom) n { NDS.RegisterEventFuncs(Event_ROMTransfer, this, { + MakeEventThunk(NDSCartSlot, ROMPrepareData), MakeEventThunk(NDSCartSlot, ROMEndTransfer) }); NDS.RegisterEventFuncs(Event_ROMSPITransfer, this, {MakeEventThunk(NDSCartSlot, SPITransferDone)}); @@ -1797,8 +1799,7 @@ void NDSCartSlot::ResetCart() noexcept TransferDir = 0; memset(TransferCmd.data(), 0, sizeof(TransferCmd)); TransferCmd[0] = 0xFF; - ROMTransferTime[0] = -1; - ROMTransferTime[1] = -1; + ROMTransferTime = -1; if (Cart) Cart->Reset(); } @@ -1815,14 +1816,8 @@ void NDSCartSlot::ROMEndTransfer(u32 param) noexcept Cart->ROMCommandFinish(TransferCmd.data(), TransferData.data(), TransferLen); } -void NDSCartSlot::ROMPrepareData() noexcept +void NDSCartSlot::ROMPrepareData(u32 param) noexcept { - u64 curts; - if (NDS.ExMemCnt[0] & (1<<11)) curts = NDS.ARM7Timestamp; - else curts = (std::max(NDS.ARM9Timestamp, NDS.DMA9Timestamp) + ((1<> NDS.ARM9ClockShift; - - if (curts < ROMTransferTime[0]) return; - if (TransferDir == 0) { if (TransferPos >= TransferLen) @@ -1833,8 +1828,6 @@ void NDSCartSlot::ROMPrepareData() noexcept TransferPos += 4; } - ROMTransferTime[0] = -1; - ROMCnt |= (1<<23); if (NDS.ExMemCnt[0] & (1<<11)) @@ -1845,7 +1838,7 @@ void NDSCartSlot::ROMPrepareData() noexcept u32 NDSCartSlot::GetROMCnt() noexcept { - ROMPrepareData(); + NDS.RunEventManual(Event_ROMTransfer); return ROMCnt; } @@ -1936,16 +1929,13 @@ void NDSCartSlot::WriteROMCnt(u32 val) noexcept NDS.ScheduleEvent(Event_ROMTransfer, false, xfercycle*cmddelay, ROMTransfer_End, 0); else { + NDS.ScheduleEvent(Event_ROMTransfer, false, xfercycle*(cmddelay+4), ROMTransfer_PrepareData, 0); + u64 curts; if (NDS.ExMemCnt[0] & (1<<11)) curts = NDS.ARM7Timestamp; else curts = (std::max(NDS.ARM9Timestamp, NDS.DMA9Timestamp) + ((1<> NDS.ARM9ClockShift; - ROMTransferTime[0] = (xfercycle*(cmddelay+4)) + curts; - - if ((TransferPos + 4) < TransferLen) - ROMTransferTime[1] = (xfercycle*(cmddelay+8)) + curts; - else - ROMTransferTime[1] = -1; + ROMTransferTime = (xfercycle*(cmddelay+8)) + curts; } } @@ -1966,14 +1956,10 @@ void NDSCartSlot::AdvanceROMTransfer() noexcept u64 curts; if (NDS.ExMemCnt[0] & (1<<11)) curts = NDS.ARM7Timestamp; else curts = (std::max(NDS.ARM9Timestamp, NDS.DMA9Timestamp) + ((1<> NDS.ARM9ClockShift; + + NDS.ScheduleEvent(Event_ROMTransfer, false, ROMTransferTime-curts, ROMTransfer_PrepareData, 0); - ROMTransferTime[0] = ROMTransferTime[1]; - - if ((TransferPos + 4) < TransferLen) - ROMTransferTime[1] = (xfercycle*delay) + std::max(curts, ROMTransferTime[0]); - else - ROMTransferTime[1] = -1; - + ROMTransferTime = (xfercycle*delay) + std::max(curts, ROMTransferTime); } else ROMEndTransfer(0); @@ -1982,8 +1968,8 @@ void NDSCartSlot::AdvanceROMTransfer() noexcept u32 NDSCartSlot::ReadROMData() noexcept { if (ROMCnt & (1<<30)) return 0; - - ROMPrepareData(); + + NDS.RunEventManual(Event_ROMTransfer); if (ROMCnt & (1<<23)) { @@ -1996,8 +1982,8 @@ u32 NDSCartSlot::ReadROMData() noexcept void NDSCartSlot::WriteROMData(u32 val) noexcept { if (!(ROMCnt & (1<<30))) return; - - ROMPrepareData(); + + NDS.RunEventManual(Event_ROMTransfer); ROMData = val; diff --git a/src/NDSCart.h b/src/NDSCart.h index 3db076e4..37bbea27 100644 --- a/src/NDSCart.h +++ b/src/NDSCart.h @@ -410,7 +410,6 @@ public: void WriteROMCnt(u32 val) noexcept; [[nodiscard]] u8 ReadSPIData() const noexcept; void WriteSPIData(u8 val) noexcept; - void ROMPrepareData() noexcept; [[nodiscard]] u8 GetROMCommand(u8 index) const noexcept { return ROMCommand[index]; } void SetROMCommand(u8 index, u8 val) noexcept { ROMCommand[index] = val; } @@ -420,8 +419,6 @@ public: [[nodiscard]] u16 GetSPICnt() const noexcept { return SPICnt; } void SetSPICnt(u16 val) noexcept { SPICnt = val; } - u64 ROMTransferTime[2]; - private: friend class CartCommon; melonDS::NDS& NDS; @@ -446,6 +443,7 @@ private: u64 Key2_X = 0; u64 Key2_Y = 0; + u64 ROMTransferTime; void Key1_Encrypt(u32* data) const noexcept; void Key1_Decrypt(u32* data) const noexcept; @@ -454,6 +452,7 @@ private: void Key1_InitKeycode(bool dsi, u32 idcode, u32 level, u32 mod) noexcept; void Key2_Encrypt(const u8* data, u32 len) noexcept; void ROMEndTransfer(u32 param) noexcept; + void ROMPrepareData(u32 param) noexcept; void AdvanceROMTransfer() noexcept; void SPITransferDone(u32 param) noexcept; };