better logic for gamecard reads

This commit is contained in:
Jaklyy 2024-12-21 13:19:34 -05:00
parent 65e2f64695
commit caa009ca03
4 changed files with 35 additions and 35 deletions

View File

@ -797,7 +797,7 @@ void NDS::SetARM9BIOS(const std::array<u8, ARM9BIOSSize>& 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<<id))
{
u64 curts = CurCPU ? ARM7Timestamp : (std::max(ARM9Timestamp, DMA9Timestamp) >> ARM9ClockShift);
SchedEvent& evt = SchedList[id];
if (evt.Timestamp <= curts)
{
evt.Funcs[evt.FuncID](evt.That, evt.Param);
SchedListMask &= ~(1<<id);
}
}
}
u64 NDS::NextTargetSleep()
{
u64 minEvent = UINT64_MAX;
@ -1824,7 +1839,6 @@ u32 NDS::RunFrame()
}
RunSystem(target);
NDSCartSlot.ROMPrepareData();
if (CPUStop & CPUStop_Sleep)
{

View File

@ -432,6 +432,7 @@ public: // TODO: Encapsulate the rest of these members
void UnregisterEventFuncs(u32 id);
void ScheduleEvent(u32 id, bool periodic, s32 delay, u32 funcid, u32 param);
void CancelEvent(u32 id);
void RunEventManual(u32 id);
void debug(u32 p);

View File

@ -37,7 +37,8 @@ namespace NDSCart
enum
{
ROMTransfer_End = 0
ROMTransfer_PrepareData = 0,
ROMTransfer_End
};
// SRAM TODO: emulate write delays???
@ -1442,6 +1443,7 @@ NDSCartSlot::NDSCartSlot(melonDS::NDS& nds, std::unique_ptr<CartCommon>&& 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)-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)-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)-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;

View File

@ -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;
};