Skip to content

Commit

Permalink
implement margins for scheduler
Browse files Browse the repository at this point in the history
hopefully this does not break anything
  • Loading branch information
RSDuck committed Sep 27, 2021
1 parent 1471c73 commit 737171c
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/NDS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ u64 FrameStartTimestamp;
int CurCPU;

const s32 kMaxIterationCycles = 64;
const s32 kIterationCycleMargin = 8;

u32 ARM9ClockShift;

Expand Down Expand Up @@ -917,22 +918,27 @@ void RelocateSave(const char* path, bool write)

u64 NextTarget()
{
u64 ret = SysTimestamp + kMaxIterationCycles;
u64 minEvent = UINT64_MAX;

u32 mask = SchedListMask;
for (int i = 0; i < Event_MAX; i++)
{
if (!mask) break;
if (mask & 0x1)
{
if (SchedList[i].Timestamp < ret)
ret = SchedList[i].Timestamp;
if (SchedList[i].Timestamp < minEvent)
minEvent = SchedList[i].Timestamp;
}

mask >>= 1;
}

return ret;
u64 max = SysTimestamp + kMaxIterationCycles;

if (minEvent < max + kIterationCycleMargin)
return minEvent;

return max;
}

void RunSystem(u64 timestamp)
Expand Down Expand Up @@ -969,7 +975,6 @@ u32 RunFrame()

while (Running && GPU::TotalScanlines==0)
{
// TODO: give it some margin, so it can directly do 17 cycles instead of 16 then 1
u64 target = NextTarget();
ARM9Target = target << ARM9ClockShift;
CurCPU = 0;
Expand Down

0 comments on commit 737171c

Please sign in to comment.