From 9ce63952fbc0f8c940207f1f8a191989505da0b8 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Tue, 24 Mar 2020 00:20:45 +1000 Subject: [PATCH] TimingEvent: Fix events losing time when rescheduling outside handler --- src/core/cdrom.cpp | 2 +- src/core/timing_event.cpp | 35 ++++++++++------------------------- src/core/timing_event.h | 3 --- 3 files changed, 11 insertions(+), 29 deletions(-) diff --git a/src/core/cdrom.cpp b/src/core/cdrom.cpp index 4c0f0284f..d05eb1a03 100644 --- a/src/core/cdrom.cpp +++ b/src/core/cdrom.cpp @@ -523,7 +523,7 @@ TickCount CDROM::GetTicksForSeek() const void CDROM::BeginCommand(Command command) { m_command = command; - m_command_event->SetDowncount(GetAckDelayForCommand()); + m_command_event->Schedule(GetAckDelayForCommand()); UpdateCommandEvent(); UpdateStatusRegister(); } diff --git a/src/core/timing_event.cpp b/src/core/timing_event.cpp index a45c443c4..ed3000b3b 100644 --- a/src/core/timing_event.cpp +++ b/src/core/timing_event.cpp @@ -28,26 +28,21 @@ TickCount TimingEvent::GetTicksUntilNextExecution() const void TimingEvent::Schedule(TickCount ticks) { - m_downcount = ticks; - m_time_since_last_run = 0; + const TickCount pending_ticks = m_system->m_cpu->GetPendingTicks(); + m_downcount = pending_ticks + ticks; - // Factor in partial time if this was rescheduled outside of an event handler. Say, an MMIO write. - if (!m_system->m_running_events) + if (!m_active) { - const TickCount pending_ticks = m_system->m_cpu->GetPendingTicks(); - m_downcount += pending_ticks; - m_time_since_last_run -= pending_ticks; - } - - if (m_active) - { - // If this is a call from an IO handler for example, re-sort the event queue. - m_system->SortEvents(); + // Event is going active, so we want it to only execute ticks from the current timestamp. + m_time_since_last_run = -pending_ticks; + m_active = true; + m_system->AddActiveEvent(this); } else { - m_active = true; - m_system->AddActiveEvent(this); + // Event is already active, so we leave the time since last run alone, and just modify the downcount. + // If this is a call from an IO handler for example, re-sort the event queue. + m_system->SortEvents(); } } @@ -118,13 +113,3 @@ void TimingEvent::Deactivate() m_active = false; m_system->RemoveActiveEvent(this); } - -void TimingEvent::SetDowncount(TickCount downcount) -{ - const TickCount pending_ticks = m_system->m_running_events ? 0 : m_system->m_cpu->GetPendingTicks(); - m_downcount = downcount + pending_ticks; - m_time_since_last_run = -pending_ticks; - - if (m_active) - m_system->SortEvents(); -} diff --git a/src/core/timing_event.h b/src/core/timing_event.h index 252753c0d..b7070c77b 100644 --- a/src/core/timing_event.h +++ b/src/core/timing_event.h @@ -57,9 +57,6 @@ public: Deactivate(); } - // Directly alters the downcount of the event. - void SetDowncount(TickCount downcount); - // Directly alters the interval of the event. void SetInterval(TickCount interval) { m_interval = interval; } void SetPeriod(TickCount period) { m_period = period; }