CoreTiming: Fix scheduling into the past

ForceExceptionCheck messes up the downcount and slice length if the
callback is scheduled into the past (g_slice_length becomes negative)
This commit is contained in:
EmptyChaos 2016-09-02 02:18:14 +00:00
parent ac63e54473
commit 59465911d7
2 changed files with 9 additions and 1 deletions

View File

@ -282,11 +282,12 @@ void RemoveAllEvents(EventType* event_type)
void ForceExceptionCheck(s64 cycles)
{
cycles = std::max<s64>(0, cycles);
if (DowncountToCycles(PowerPC::ppcState.downcount) > cycles)
{
// downcount is always (much) smaller than MAX_INT so we can safely cast cycles to an int here.
// Account for cycles already executed by adjusting the g_slice_length
g_slice_length -= (DowncountToCycles(PowerPC::ppcState.downcount) - static_cast<int>(cycles));
g_slice_length -= DowncountToCycles(PowerPC::ppcState.downcount) - static_cast<int>(cycles);
PowerPC::ppcState.downcount = CyclesToDowncount(static_cast<int>(cycles));
}
}

View File

@ -241,6 +241,13 @@ TEST(CoreTiming, ScheduleIntoPast)
CoreTiming::g_global_timer += 1000;
Core::DeclareAsCPUThread();
AdvanceAndCheck(1, MAX_SLICE_LENGTH, MAX_SLICE_LENGTH + 1000);
// Schedule directly into the past from the CPU.
// This shouldn't happen in practice, but it's best if we don't mess up the slice length and
// downcount if we do.
CoreTiming::ScheduleEvent(-1000, s_cb_next, CB_IDS[0]);
EXPECT_EQ(0, PowerPC::ppcState.downcount);
AdvanceAndCheck(0, MAX_SLICE_LENGTH, 1000);
}
TEST(CoreTiming, Overclocking)