Timers: Fix sysclk IRQs being delayed by gate changes

SetGate() rescheduled the sysclk event without updating the counters
first when the gated timer was GPU-clocked, so the next IRQ got pushed
back by however long it had been since the last update. Gate changes
that were more frequent than the IRQ stopped it from firing at all.
This commit is contained in:
Stenzek committed 2026-10-15 23:50:33 +10:00
1 parent 22c78a107d
commit 8e7c28987e
5 files changed
+120 -2

No files matched your search

+1
View File
@@ -13,6 +13,7 @@ add_executable(core-tests
negcon_base_tests.cpp
spu_tests.cpp
stub_host.cpp
timers_tests.cpp
)
target_include_directories(core-tests PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/..")
+1
View File
@@ -14,6 +14,7 @@
<ClCompile Include="negcon_base_tests.cpp" />
<ClCompile Include="spu_tests.cpp" />
<ClCompile Include="stub_host.cpp" />
<ClCompile Include="timers_tests.cpp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\dep\googletest\googletest.vcxproj">
@@ -13,6 +13,7 @@
<ClCompile Include="lightgun_controller_tests.cpp" />
<ClCompile Include="negcon_base_tests.cpp" />
<ClCompile Include="mdec_tests.cpp" />
<ClCompile Include="timers_tests.cpp" />
</ItemGroup>
<ItemGroup>
<Text Include="CMakeLists.txt" />
+111
View File
@@ -0,0 +1,111 @@
// SPDX-FileCopyrightText: 2019-2026 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
#include "core/cpu_core.h"
#include "core/interrupt_controller.h"
#include "core/system.h"
#include "core/timers.h"
#include "core/timing_event.h"
#include <gtest/gtest.h>
namespace {
enum : u32
{
TIMER0 = 0x00,
TIMER1 = 0x10,
TIMER2 = 0x20,
REG_COUNTER = 0x00,
REG_MODE = 0x04,
REG_TARGET = 0x08,
MODE_SYNC_ENABLE = (1u << 0),
MODE_RESET_AT_TARGET = (1u << 3),
MODE_IRQ_AT_TARGET = (1u << 4),
MODE_IRQ_ON_OVERFLOW = (1u << 5),
MODE_IRQ_REPEAT = (1u << 6),
MODE_IRQ_TOGGLE = (1u << 7),
MODE_CLOCK_SOURCE_1 = (1u << 8), // dot clock for timer 0, hblank for timer 1
MODE_CLOCK_SOURCE_2 = (1u << 9), // sysclk/8 for timer 2
MODE_IRQ_N = (1u << 10),
MODE_REACHED_TARGET = (1u << 11),
MODE_REACHED_OVERFLOW = (1u << 12),
};
class TimersTest : public ::testing::Test
{
protected:
static void SetUpTestSuite()
{
// The max slice ticks come from System. Updating it schedules the SPU/GPU events too, which we can't run.
TimingEvents::Initialize();
System::UpdateOverclock();
DeactivateAllEvents();
}
void SetUp() override
{
// Interrupts stay disabled in the CPU, the tests look at the interrupt controller instead.
CPU::g_state.cop0_regs.sr.bits = 0;
CPU::g_state.pending_ticks = 0;
InterruptController::Reset();
TimingEvents::Reset();
Timers::Initialize();
}
void TearDown() override { Timers::Shutdown(); }
static void DeactivateAllEvents()
{
while (TimingEvent* event = *TimingEvents::GetHeadEventPtr())
event->Deactivate();
}
/// Runs the CPU for the specified number of ticks, without any register accesses.
static void Run(u32 ticks)
{
for (u32 i = 0; i < ticks; i++)
{
CPU::AddPendingTicks(1);
if (CPU::GetPendingTicks() >= CPU::g_state.downcount)
TimingEvents::RunEvents();
}
}
/// Returns true if the timer has raised an IRQ since the last call.
static bool TakeIRQ(u32 timer)
{
const u32 bit = 1u << (static_cast<u32>(InterruptController::IRQ::TMR0) + timer);
const bool raised = (InterruptController::ReadRegister(0x00) & bit) != 0;
InterruptController::WriteRegister(0x00, ~bit);
return raised;
}
};
} // namespace
// Gate changes on a GPU-clocked timer used to reschedule the sysclk event from counters which weren't up to date,
// pushing the IRQ back each time.
TEST_F(TimersTest, GateChangeDoesNotDelaySysClkIRQ)
{
static constexpr u32 PERIOD = 1000;
Timers::WriteRegister(TIMER2 + REG_TARGET, PERIOD);
Timers::WriteRegister(TIMER2 + REG_MODE, MODE_RESET_AT_TARGET | MODE_IRQ_AT_TARGET | MODE_IRQ_REPEAT);
Timers::WriteRegister(TIMER1 + REG_MODE, MODE_SYNC_ENABLE | MODE_CLOCK_SOURCE_1);
bool gate = false;
for (u32 tick = 1; tick <= PERIOD * 10; tick++)
{
Run(1);
if ((tick % 300) == 0)
{
gate = !gate;
Timers::SetGate(1, gate);
}
ASSERT_EQ(TakeIRQ(2), (tick % PERIOD) == 0) << "at tick " << tick;
}
}
+6 -2
View File
@@ -174,7 +174,8 @@ void Timers::SetGate(u32 timer, bool state)
// Because the gate prevents counting in or outside of the gate, we need a correct counter.
// For reset, we _can_ skip it, until the gate clears.
if (!cs.use_external_clock && (cs.mode.sync_mode != SyncMode::ResetOnGateEnd || !state))
const bool update_sysclk = (!cs.use_external_clock && (cs.mode.sync_mode != SyncMode::ResetOnGateEnd || !state));
if (update_sysclk)
s_state.sysclk_event.InvokeEarly();
switch (cs.mode.sync_mode)
@@ -200,7 +201,10 @@ void Timers::SetGate(u32 timer, bool state)
}
UpdateCountingEnabled(timer, cs);
UpdateSysClkEvent();
// Can't reschedule without updating the counters first, the IRQ would end up being late.
if (update_sysclk)
UpdateSysClkEvent();
}
TickCount Timers::GetTicksUntilIRQ(u32 timer)