2016-09-02 10:11:08 +00:00
|
|
|
// Copyright 2016 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2016-09-02 10:11:08 +00:00
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <bitset>
|
2017-06-18 14:21:17 +00:00
|
|
|
#include <string>
|
2016-09-02 10:11:08 +00:00
|
|
|
|
2017-07-09 23:17:36 +00:00
|
|
|
#include "Common/Config/Config.h"
|
2017-06-18 14:21:17 +00:00
|
|
|
#include "Common/FileUtil.h"
|
2021-12-25 20:52:50 +00:00
|
|
|
#include "Core/Config/MainSettings.h"
|
2016-09-02 10:11:08 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
|
|
|
#include "Core/Core.h"
|
|
|
|
#include "Core/CoreTiming.h"
|
|
|
|
#include "Core/PowerPC/PowerPC.h"
|
2022-11-23 20:35:21 +00:00
|
|
|
#include "Core/System.h"
|
2017-06-05 02:56:28 +00:00
|
|
|
#include "UICommon/UICommon.h"
|
2016-09-02 10:11:08 +00:00
|
|
|
|
|
|
|
// Numbers are chosen randomly to make sure the correct one is given.
|
|
|
|
static constexpr std::array<u64, 5> CB_IDS{{42, 144, 93, 1026, UINT64_C(0xFFFF7FFFF7FFFF)}};
|
|
|
|
static constexpr int MAX_SLICE_LENGTH = 20000; // Copied from CoreTiming internals
|
|
|
|
|
|
|
|
static std::bitset<CB_IDS.size()> s_callbacks_ran_flags;
|
|
|
|
static u64 s_expected_callback = 0;
|
|
|
|
static s64 s_lateness = 0;
|
|
|
|
|
|
|
|
template <unsigned int IDX>
|
2022-11-06 16:54:58 +00:00
|
|
|
void CallbackTemplate(Core::System& system, u64 userdata, s64 lateness)
|
2016-09-02 10:11:08 +00:00
|
|
|
{
|
|
|
|
static_assert(IDX < CB_IDS.size(), "IDX out of range");
|
|
|
|
s_callbacks_ran_flags.set(IDX);
|
|
|
|
EXPECT_EQ(CB_IDS[IDX], userdata);
|
2016-09-08 09:46:42 +00:00
|
|
|
EXPECT_EQ(CB_IDS[IDX], s_expected_callback);
|
2016-09-02 10:11:08 +00:00
|
|
|
EXPECT_EQ(s_lateness, lateness);
|
|
|
|
}
|
|
|
|
|
|
|
|
class ScopeInit final
|
|
|
|
{
|
|
|
|
public:
|
2023-03-28 18:26:52 +00:00
|
|
|
explicit ScopeInit(Core::System& system) : m_system(system), m_profile_path(File::CreateTempDir())
|
2016-09-02 10:11:08 +00:00
|
|
|
{
|
2021-01-22 19:34:45 +00:00
|
|
|
if (!UserDirectoryExists())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-09-02 10:11:08 +00:00
|
|
|
Core::DeclareAsCPUThread();
|
2017-06-18 14:21:17 +00:00
|
|
|
UICommon::SetUserDirectory(m_profile_path);
|
2017-05-13 21:29:55 +00:00
|
|
|
Config::Init();
|
2016-09-02 10:11:08 +00:00
|
|
|
SConfig::Init();
|
2023-03-28 18:26:52 +00:00
|
|
|
system.GetPowerPC().Init(PowerPC::CPUCore::Interpreter);
|
2022-11-26 08:29:46 +00:00
|
|
|
auto& core_timing = system.GetCoreTiming();
|
|
|
|
core_timing.Init();
|
2016-09-02 10:11:08 +00:00
|
|
|
}
|
|
|
|
~ScopeInit()
|
|
|
|
{
|
2021-01-22 19:34:45 +00:00
|
|
|
if (!UserDirectoryExists())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2023-03-28 18:26:52 +00:00
|
|
|
auto& core_timing = m_system.GetCoreTiming();
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.Shutdown();
|
2023-03-28 18:26:52 +00:00
|
|
|
m_system.GetPowerPC().Shutdown();
|
2016-09-02 10:11:08 +00:00
|
|
|
SConfig::Shutdown();
|
2017-05-13 21:29:55 +00:00
|
|
|
Config::Shutdown();
|
2016-09-02 10:11:08 +00:00
|
|
|
Core::UndeclareAsCPUThread();
|
2017-06-18 14:21:17 +00:00
|
|
|
File::DeleteDirRecursively(m_profile_path);
|
2016-09-02 10:11:08 +00:00
|
|
|
}
|
2021-01-22 19:34:45 +00:00
|
|
|
bool UserDirectoryExists() const { return !m_profile_path.empty(); }
|
2017-07-09 23:17:36 +00:00
|
|
|
|
2017-06-18 14:21:17 +00:00
|
|
|
private:
|
2023-03-28 18:26:52 +00:00
|
|
|
Core::System& m_system;
|
2017-06-18 14:21:17 +00:00
|
|
|
std::string m_profile_path;
|
2016-09-02 10:11:08 +00:00
|
|
|
};
|
|
|
|
|
2023-03-28 22:42:27 +00:00
|
|
|
static void AdvanceAndCheck(Core::System& system, u32 idx, int downcount, int expected_lateness = 0,
|
2017-03-25 04:36:03 +00:00
|
|
|
int cpu_downcount = 0)
|
2016-09-02 10:11:08 +00:00
|
|
|
{
|
|
|
|
s_callbacks_ran_flags = 0;
|
|
|
|
s_expected_callback = CB_IDS[idx];
|
|
|
|
s_lateness = expected_lateness;
|
|
|
|
|
2023-03-28 22:42:27 +00:00
|
|
|
auto& ppc_state = system.GetPPCState();
|
|
|
|
ppc_state.downcount = cpu_downcount; // Pretend we executed X cycles of instructions.
|
2022-11-26 08:29:46 +00:00
|
|
|
auto& core_timing = system.GetCoreTiming();
|
|
|
|
core_timing.Advance();
|
2016-09-02 10:11:08 +00:00
|
|
|
|
|
|
|
EXPECT_EQ(decltype(s_callbacks_ran_flags)().set(idx), s_callbacks_ran_flags);
|
2023-03-28 22:42:27 +00:00
|
|
|
EXPECT_EQ(downcount, ppc_state.downcount);
|
2016-09-02 10:11:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CoreTiming, BasicOrder)
|
|
|
|
{
|
2023-03-28 18:26:52 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
|
|
|
|
ScopeInit guard(system);
|
2021-01-22 19:34:45 +00:00
|
|
|
ASSERT_TRUE(guard.UserDirectoryExists());
|
2016-09-02 10:11:08 +00:00
|
|
|
|
2022-11-26 08:29:46 +00:00
|
|
|
auto& core_timing = system.GetCoreTiming();
|
2023-03-28 22:42:27 +00:00
|
|
|
auto& ppc_state = system.GetPPCState();
|
2022-11-26 08:29:46 +00:00
|
|
|
|
|
|
|
CoreTiming::EventType* cb_a = core_timing.RegisterEvent("callbackA", CallbackTemplate<0>);
|
|
|
|
CoreTiming::EventType* cb_b = core_timing.RegisterEvent("callbackB", CallbackTemplate<1>);
|
|
|
|
CoreTiming::EventType* cb_c = core_timing.RegisterEvent("callbackC", CallbackTemplate<2>);
|
|
|
|
CoreTiming::EventType* cb_d = core_timing.RegisterEvent("callbackD", CallbackTemplate<3>);
|
|
|
|
CoreTiming::EventType* cb_e = core_timing.RegisterEvent("callbackE", CallbackTemplate<4>);
|
2016-09-02 10:11:08 +00:00
|
|
|
|
|
|
|
// Enter slice 0
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.Advance();
|
2016-09-02 10:11:08 +00:00
|
|
|
|
|
|
|
// D -> B -> C -> A -> E
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.ScheduleEvent(1000, cb_a, CB_IDS[0]);
|
2023-03-28 22:42:27 +00:00
|
|
|
EXPECT_EQ(1000, ppc_state.downcount);
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.ScheduleEvent(500, cb_b, CB_IDS[1]);
|
2023-03-28 22:42:27 +00:00
|
|
|
EXPECT_EQ(500, ppc_state.downcount);
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.ScheduleEvent(800, cb_c, CB_IDS[2]);
|
2023-03-28 22:42:27 +00:00
|
|
|
EXPECT_EQ(500, ppc_state.downcount);
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.ScheduleEvent(100, cb_d, CB_IDS[3]);
|
2023-03-28 22:42:27 +00:00
|
|
|
EXPECT_EQ(100, ppc_state.downcount);
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.ScheduleEvent(1200, cb_e, CB_IDS[4]);
|
2023-03-28 22:42:27 +00:00
|
|
|
EXPECT_EQ(100, ppc_state.downcount);
|
2016-09-02 10:11:08 +00:00
|
|
|
|
2023-03-28 22:42:27 +00:00
|
|
|
AdvanceAndCheck(system, 3, 400);
|
|
|
|
AdvanceAndCheck(system, 1, 300);
|
|
|
|
AdvanceAndCheck(system, 2, 200);
|
|
|
|
AdvanceAndCheck(system, 0, 200);
|
|
|
|
AdvanceAndCheck(system, 4, MAX_SLICE_LENGTH);
|
2016-09-02 10:11:08 +00:00
|
|
|
}
|
|
|
|
|
2016-09-08 09:46:42 +00:00
|
|
|
namespace SharedSlotTest
|
|
|
|
{
|
|
|
|
static unsigned int s_counter = 0;
|
|
|
|
|
|
|
|
template <unsigned int ID>
|
2022-11-06 16:54:58 +00:00
|
|
|
void FifoCallback(Core::System& system, u64 userdata, s64 lateness)
|
2016-09-08 09:46:42 +00:00
|
|
|
{
|
|
|
|
static_assert(ID < CB_IDS.size(), "ID out of range");
|
|
|
|
s_callbacks_ran_flags.set(ID);
|
|
|
|
EXPECT_EQ(CB_IDS[ID], userdata);
|
|
|
|
EXPECT_EQ(ID, s_counter);
|
|
|
|
EXPECT_EQ(s_lateness, lateness);
|
|
|
|
++s_counter;
|
|
|
|
}
|
2019-05-05 23:48:12 +00:00
|
|
|
} // namespace SharedSlotTest
|
2016-09-08 09:46:42 +00:00
|
|
|
|
2016-09-02 10:11:08 +00:00
|
|
|
TEST(CoreTiming, SharedSlot)
|
|
|
|
{
|
2016-09-08 09:46:42 +00:00
|
|
|
using namespace SharedSlotTest;
|
|
|
|
|
2023-03-28 18:26:52 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
|
|
|
|
ScopeInit guard(system);
|
2021-01-22 19:34:45 +00:00
|
|
|
ASSERT_TRUE(guard.UserDirectoryExists());
|
2016-09-02 10:11:08 +00:00
|
|
|
|
2022-11-26 08:29:46 +00:00
|
|
|
auto& core_timing = system.GetCoreTiming();
|
2023-03-28 22:42:27 +00:00
|
|
|
auto& ppc_state = system.GetPPCState();
|
2022-11-26 08:29:46 +00:00
|
|
|
|
|
|
|
CoreTiming::EventType* cb_a = core_timing.RegisterEvent("callbackA", FifoCallback<0>);
|
|
|
|
CoreTiming::EventType* cb_b = core_timing.RegisterEvent("callbackB", FifoCallback<1>);
|
|
|
|
CoreTiming::EventType* cb_c = core_timing.RegisterEvent("callbackC", FifoCallback<2>);
|
|
|
|
CoreTiming::EventType* cb_d = core_timing.RegisterEvent("callbackD", FifoCallback<3>);
|
|
|
|
CoreTiming::EventType* cb_e = core_timing.RegisterEvent("callbackE", FifoCallback<4>);
|
2016-09-02 10:11:08 +00:00
|
|
|
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.ScheduleEvent(1000, cb_a, CB_IDS[0]);
|
|
|
|
core_timing.ScheduleEvent(1000, cb_b, CB_IDS[1]);
|
|
|
|
core_timing.ScheduleEvent(1000, cb_c, CB_IDS[2]);
|
|
|
|
core_timing.ScheduleEvent(1000, cb_d, CB_IDS[3]);
|
|
|
|
core_timing.ScheduleEvent(1000, cb_e, CB_IDS[4]);
|
2016-09-02 10:11:08 +00:00
|
|
|
|
|
|
|
// Enter slice 0
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.Advance();
|
2023-03-28 22:42:27 +00:00
|
|
|
EXPECT_EQ(1000, ppc_state.downcount);
|
2016-09-02 10:11:08 +00:00
|
|
|
|
|
|
|
s_callbacks_ran_flags = 0;
|
2016-09-08 09:46:42 +00:00
|
|
|
s_counter = 0;
|
2016-09-02 10:11:08 +00:00
|
|
|
s_lateness = 0;
|
2023-03-28 22:42:27 +00:00
|
|
|
ppc_state.downcount = 0;
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.Advance();
|
2023-03-28 22:42:27 +00:00
|
|
|
EXPECT_EQ(MAX_SLICE_LENGTH, ppc_state.downcount);
|
2016-09-02 10:11:08 +00:00
|
|
|
EXPECT_EQ(0x1FULL, s_callbacks_ran_flags.to_ullong());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CoreTiming, PredictableLateness)
|
|
|
|
{
|
2023-03-28 18:26:52 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
|
|
|
|
ScopeInit guard(system);
|
2021-01-22 19:34:45 +00:00
|
|
|
ASSERT_TRUE(guard.UserDirectoryExists());
|
2016-09-02 10:11:08 +00:00
|
|
|
|
2022-11-26 08:29:46 +00:00
|
|
|
auto& core_timing = system.GetCoreTiming();
|
|
|
|
|
|
|
|
CoreTiming::EventType* cb_a = core_timing.RegisterEvent("callbackA", CallbackTemplate<0>);
|
|
|
|
CoreTiming::EventType* cb_b = core_timing.RegisterEvent("callbackB", CallbackTemplate<1>);
|
2016-09-02 10:11:08 +00:00
|
|
|
|
|
|
|
// Enter slice 0
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.Advance();
|
2016-09-02 10:11:08 +00:00
|
|
|
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.ScheduleEvent(100, cb_a, CB_IDS[0]);
|
|
|
|
core_timing.ScheduleEvent(200, cb_b, CB_IDS[1]);
|
2016-09-02 10:11:08 +00:00
|
|
|
|
2023-03-28 22:42:27 +00:00
|
|
|
AdvanceAndCheck(system, 0, 90, 10, -10); // (100 - 10)
|
|
|
|
AdvanceAndCheck(system, 1, MAX_SLICE_LENGTH, 50, -50);
|
2016-09-02 10:11:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace ChainSchedulingTest
|
|
|
|
{
|
|
|
|
static int s_reschedules = 0;
|
|
|
|
|
2022-11-06 16:54:58 +00:00
|
|
|
static void RescheduleCallback(Core::System& system, u64 userdata, s64 lateness)
|
2016-09-02 10:11:08 +00:00
|
|
|
{
|
|
|
|
--s_reschedules;
|
|
|
|
EXPECT_TRUE(s_reschedules >= 0);
|
|
|
|
EXPECT_EQ(s_lateness, lateness);
|
|
|
|
|
|
|
|
if (s_reschedules > 0)
|
2022-11-26 08:29:46 +00:00
|
|
|
{
|
|
|
|
system.GetCoreTiming().ScheduleEvent(1000, reinterpret_cast<CoreTiming::EventType*>(userdata),
|
|
|
|
userdata);
|
|
|
|
}
|
2016-09-02 10:11:08 +00:00
|
|
|
}
|
2019-05-05 23:48:12 +00:00
|
|
|
} // namespace ChainSchedulingTest
|
2016-09-02 10:11:08 +00:00
|
|
|
|
|
|
|
TEST(CoreTiming, ChainScheduling)
|
|
|
|
{
|
|
|
|
using namespace ChainSchedulingTest;
|
|
|
|
|
2023-03-28 18:26:52 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
|
|
|
|
ScopeInit guard(system);
|
2021-01-22 19:34:45 +00:00
|
|
|
ASSERT_TRUE(guard.UserDirectoryExists());
|
2016-09-02 10:11:08 +00:00
|
|
|
|
2022-11-26 08:29:46 +00:00
|
|
|
auto& core_timing = system.GetCoreTiming();
|
2023-03-28 22:42:27 +00:00
|
|
|
auto& ppc_state = system.GetPPCState();
|
2022-11-26 08:29:46 +00:00
|
|
|
|
|
|
|
CoreTiming::EventType* cb_a = core_timing.RegisterEvent("callbackA", CallbackTemplate<0>);
|
|
|
|
CoreTiming::EventType* cb_b = core_timing.RegisterEvent("callbackB", CallbackTemplate<1>);
|
|
|
|
CoreTiming::EventType* cb_c = core_timing.RegisterEvent("callbackC", CallbackTemplate<2>);
|
2016-09-02 10:11:08 +00:00
|
|
|
CoreTiming::EventType* cb_rs =
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.RegisterEvent("callbackReschedule", RescheduleCallback);
|
2016-09-02 10:11:08 +00:00
|
|
|
|
|
|
|
// Enter slice 0
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.Advance();
|
2016-09-02 10:11:08 +00:00
|
|
|
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.ScheduleEvent(800, cb_a, CB_IDS[0]);
|
|
|
|
core_timing.ScheduleEvent(1000, cb_b, CB_IDS[1]);
|
|
|
|
core_timing.ScheduleEvent(2200, cb_c, CB_IDS[2]);
|
|
|
|
core_timing.ScheduleEvent(1000, cb_rs, reinterpret_cast<u64>(cb_rs));
|
2023-03-28 22:42:27 +00:00
|
|
|
EXPECT_EQ(800, ppc_state.downcount);
|
2016-09-02 10:11:08 +00:00
|
|
|
|
|
|
|
s_reschedules = 3;
|
2023-03-28 22:42:27 +00:00
|
|
|
AdvanceAndCheck(system, 0, 200); // cb_a
|
|
|
|
AdvanceAndCheck(system, 1, 1000); // cb_b, cb_rs
|
2016-09-02 10:11:08 +00:00
|
|
|
EXPECT_EQ(2, s_reschedules);
|
|
|
|
|
2023-03-28 22:42:27 +00:00
|
|
|
ppc_state.downcount = 0;
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.Advance(); // cb_rs
|
2016-09-02 10:11:08 +00:00
|
|
|
EXPECT_EQ(1, s_reschedules);
|
2023-03-28 22:42:27 +00:00
|
|
|
EXPECT_EQ(200, ppc_state.downcount);
|
2016-09-02 10:11:08 +00:00
|
|
|
|
2023-03-28 22:42:27 +00:00
|
|
|
AdvanceAndCheck(system, 2, 800); // cb_c
|
2016-09-02 10:11:08 +00:00
|
|
|
|
2023-03-28 22:42:27 +00:00
|
|
|
ppc_state.downcount = 0;
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.Advance(); // cb_rs
|
2016-09-02 10:11:08 +00:00
|
|
|
EXPECT_EQ(0, s_reschedules);
|
2023-03-28 22:42:27 +00:00
|
|
|
EXPECT_EQ(MAX_SLICE_LENGTH, ppc_state.downcount);
|
2016-09-02 10:11:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace ScheduleIntoPastTest
|
|
|
|
{
|
|
|
|
static CoreTiming::EventType* s_cb_next = nullptr;
|
|
|
|
|
2022-11-06 16:54:58 +00:00
|
|
|
static void ChainCallback(Core::System& system, u64 userdata, s64 lateness)
|
2016-09-02 10:11:08 +00:00
|
|
|
{
|
|
|
|
EXPECT_EQ(CB_IDS[0] + 1, userdata);
|
|
|
|
EXPECT_EQ(0, lateness);
|
|
|
|
|
2022-11-26 08:29:46 +00:00
|
|
|
system.GetCoreTiming().ScheduleEvent(-1000, s_cb_next, userdata - 1);
|
2016-09-02 10:11:08 +00:00
|
|
|
}
|
2019-05-05 23:48:12 +00:00
|
|
|
} // namespace ScheduleIntoPastTest
|
2016-09-02 10:11:08 +00:00
|
|
|
|
|
|
|
// This can happen when scheduling from outside the CPU Thread.
|
|
|
|
// Also, if the callback is very late, it may reschedule itself for the next period which
|
|
|
|
// is also in the past.
|
|
|
|
TEST(CoreTiming, ScheduleIntoPast)
|
|
|
|
{
|
|
|
|
using namespace ScheduleIntoPastTest;
|
|
|
|
|
2023-03-28 18:26:52 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
|
|
|
|
ScopeInit guard(system);
|
2021-01-22 19:34:45 +00:00
|
|
|
ASSERT_TRUE(guard.UserDirectoryExists());
|
2016-09-02 10:11:08 +00:00
|
|
|
|
2022-11-26 08:29:46 +00:00
|
|
|
auto& core_timing = system.GetCoreTiming();
|
2023-03-28 22:42:27 +00:00
|
|
|
auto& ppc_state = system.GetPPCState();
|
2022-11-26 08:29:46 +00:00
|
|
|
|
|
|
|
s_cb_next = core_timing.RegisterEvent("callbackA", CallbackTemplate<0>);
|
|
|
|
CoreTiming::EventType* cb_b = core_timing.RegisterEvent("callbackB", CallbackTemplate<1>);
|
|
|
|
CoreTiming::EventType* cb_chain = core_timing.RegisterEvent("callbackChain", ChainCallback);
|
2016-09-02 10:11:08 +00:00
|
|
|
|
|
|
|
// Enter slice 0
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.Advance();
|
2016-09-02 10:11:08 +00:00
|
|
|
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.ScheduleEvent(1000, cb_chain, CB_IDS[0] + 1);
|
2023-03-28 22:42:27 +00:00
|
|
|
EXPECT_EQ(1000, ppc_state.downcount);
|
2016-09-02 10:11:08 +00:00
|
|
|
|
2023-03-28 22:42:27 +00:00
|
|
|
AdvanceAndCheck(system, 0, MAX_SLICE_LENGTH, 1000); // Run cb_chain into late cb_a
|
2016-09-02 10:11:08 +00:00
|
|
|
|
|
|
|
// Schedule late from wrong thread
|
|
|
|
// The problem with scheduling CPU events from outside the CPU Thread is that g_global_timer
|
|
|
|
// is not reliable outside the CPU Thread. It's possible for the other thread to sample the
|
|
|
|
// global timer right before the timer is updated by Advance() then submit a new event using
|
|
|
|
// the stale value, i.e. effectively half-way through the previous slice.
|
|
|
|
// NOTE: We're only testing that the scheduler doesn't break, not whether this makes sense.
|
|
|
|
Core::UndeclareAsCPUThread();
|
2022-11-26 09:20:32 +00:00
|
|
|
auto& core_timing_globals = core_timing.GetGlobals();
|
2022-11-23 20:35:21 +00:00
|
|
|
core_timing_globals.global_timer -= 1000;
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.ScheduleEvent(0, cb_b, CB_IDS[1], CoreTiming::FromThread::NON_CPU);
|
2022-11-23 20:35:21 +00:00
|
|
|
core_timing_globals.global_timer += 1000;
|
2016-09-02 10:11:08 +00:00
|
|
|
Core::DeclareAsCPUThread();
|
2023-03-28 22:42:27 +00:00
|
|
|
AdvanceAndCheck(system, 1, MAX_SLICE_LENGTH, MAX_SLICE_LENGTH + 1000);
|
2016-09-02 02:18:14 +00:00
|
|
|
|
|
|
|
// 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.
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.ScheduleEvent(-1000, s_cb_next, CB_IDS[0]);
|
2023-03-28 22:42:27 +00:00
|
|
|
EXPECT_EQ(0, ppc_state.downcount);
|
|
|
|
AdvanceAndCheck(system, 0, MAX_SLICE_LENGTH, 1000);
|
2016-09-02 10:11:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CoreTiming, Overclocking)
|
|
|
|
{
|
2023-03-28 18:26:52 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
|
|
|
|
ScopeInit guard(system);
|
2021-01-22 19:34:45 +00:00
|
|
|
ASSERT_TRUE(guard.UserDirectoryExists());
|
2016-09-02 10:11:08 +00:00
|
|
|
|
2022-11-26 08:29:46 +00:00
|
|
|
auto& core_timing = system.GetCoreTiming();
|
2023-03-28 22:42:27 +00:00
|
|
|
auto& ppc_state = system.GetPPCState();
|
2022-11-26 08:29:46 +00:00
|
|
|
|
|
|
|
CoreTiming::EventType* cb_a = core_timing.RegisterEvent("callbackA", CallbackTemplate<0>);
|
|
|
|
CoreTiming::EventType* cb_b = core_timing.RegisterEvent("callbackB", CallbackTemplate<1>);
|
|
|
|
CoreTiming::EventType* cb_c = core_timing.RegisterEvent("callbackC", CallbackTemplate<2>);
|
|
|
|
CoreTiming::EventType* cb_d = core_timing.RegisterEvent("callbackD", CallbackTemplate<3>);
|
|
|
|
CoreTiming::EventType* cb_e = core_timing.RegisterEvent("callbackE", CallbackTemplate<4>);
|
2016-09-02 10:11:08 +00:00
|
|
|
|
|
|
|
// Overclock
|
2021-12-25 20:52:50 +00:00
|
|
|
Config::SetCurrent(Config::MAIN_OVERCLOCK_ENABLE, true);
|
|
|
|
Config::SetCurrent(Config::MAIN_OVERCLOCK, 2.0f);
|
2016-09-02 10:11:08 +00:00
|
|
|
|
|
|
|
// Enter slice 0
|
|
|
|
// Updates s_last_OC_factor.
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.Advance();
|
2016-09-02 10:11:08 +00:00
|
|
|
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.ScheduleEvent(100, cb_a, CB_IDS[0]);
|
|
|
|
core_timing.ScheduleEvent(200, cb_b, CB_IDS[1]);
|
|
|
|
core_timing.ScheduleEvent(400, cb_c, CB_IDS[2]);
|
|
|
|
core_timing.ScheduleEvent(800, cb_d, CB_IDS[3]);
|
|
|
|
core_timing.ScheduleEvent(1600, cb_e, CB_IDS[4]);
|
2023-03-28 22:42:27 +00:00
|
|
|
EXPECT_EQ(200, ppc_state.downcount);
|
2016-09-02 10:11:08 +00:00
|
|
|
|
2023-03-28 22:42:27 +00:00
|
|
|
AdvanceAndCheck(system, 0, 200); // (200 - 100) * 2
|
|
|
|
AdvanceAndCheck(system, 1, 400); // (400 - 200) * 2
|
|
|
|
AdvanceAndCheck(system, 2, 800); // (800 - 400) * 2
|
|
|
|
AdvanceAndCheck(system, 3, 1600); // (1600 - 800) * 2
|
|
|
|
AdvanceAndCheck(system, 4, MAX_SLICE_LENGTH * 2);
|
2016-09-02 10:11:08 +00:00
|
|
|
|
|
|
|
// Underclock
|
2021-12-25 20:52:50 +00:00
|
|
|
Config::SetCurrent(Config::MAIN_OVERCLOCK, 0.5f);
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.Advance();
|
2016-09-02 10:11:08 +00:00
|
|
|
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.ScheduleEvent(100, cb_a, CB_IDS[0]);
|
|
|
|
core_timing.ScheduleEvent(200, cb_b, CB_IDS[1]);
|
|
|
|
core_timing.ScheduleEvent(400, cb_c, CB_IDS[2]);
|
|
|
|
core_timing.ScheduleEvent(800, cb_d, CB_IDS[3]);
|
|
|
|
core_timing.ScheduleEvent(1600, cb_e, CB_IDS[4]);
|
2023-03-28 22:42:27 +00:00
|
|
|
EXPECT_EQ(50, ppc_state.downcount);
|
2016-09-02 10:11:08 +00:00
|
|
|
|
2023-03-28 22:42:27 +00:00
|
|
|
AdvanceAndCheck(system, 0, 50); // (200 - 100) / 2
|
|
|
|
AdvanceAndCheck(system, 1, 100); // (400 - 200) / 2
|
|
|
|
AdvanceAndCheck(system, 2, 200); // (800 - 400) / 2
|
|
|
|
AdvanceAndCheck(system, 3, 400); // (1600 - 800) / 2
|
|
|
|
AdvanceAndCheck(system, 4, MAX_SLICE_LENGTH / 2);
|
2016-09-02 10:11:08 +00:00
|
|
|
|
|
|
|
// Try switching the clock mid-emulation
|
2021-12-25 20:52:50 +00:00
|
|
|
Config::SetCurrent(Config::MAIN_OVERCLOCK, 1.0f);
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.Advance();
|
2016-09-02 10:11:08 +00:00
|
|
|
|
2022-11-26 08:29:46 +00:00
|
|
|
core_timing.ScheduleEvent(100, cb_a, CB_IDS[0]);
|
|
|
|
core_timing.ScheduleEvent(200, cb_b, CB_IDS[1]);
|
|
|
|
core_timing.ScheduleEvent(400, cb_c, CB_IDS[2]);
|
|
|
|
core_timing.ScheduleEvent(800, cb_d, CB_IDS[3]);
|
|
|
|
core_timing.ScheduleEvent(1600, cb_e, CB_IDS[4]);
|
2023-03-28 22:42:27 +00:00
|
|
|
EXPECT_EQ(100, ppc_state.downcount);
|
2016-09-02 10:11:08 +00:00
|
|
|
|
2023-03-28 22:42:27 +00:00
|
|
|
AdvanceAndCheck(system, 0, 100); // (200 - 100)
|
2021-12-25 20:52:50 +00:00
|
|
|
Config::SetCurrent(Config::MAIN_OVERCLOCK, 2.0f);
|
2023-03-28 22:42:27 +00:00
|
|
|
AdvanceAndCheck(system, 1, 400); // (400 - 200) * 2
|
|
|
|
AdvanceAndCheck(system, 2, 800); // (800 - 400) * 2
|
2021-12-25 20:52:50 +00:00
|
|
|
Config::SetCurrent(Config::MAIN_OVERCLOCK, 0.1f);
|
2023-03-28 22:42:27 +00:00
|
|
|
AdvanceAndCheck(system, 3, 80); // (1600 - 800) / 10
|
2021-12-25 20:52:50 +00:00
|
|
|
Config::SetCurrent(Config::MAIN_OVERCLOCK, 1.0f);
|
2023-03-28 22:42:27 +00:00
|
|
|
AdvanceAndCheck(system, 4, MAX_SLICE_LENGTH);
|
2016-09-02 10:11:08 +00:00
|
|
|
}
|