From 11992bde4efb004f2aef84ce8b9cb673dab37475 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sun, 10 Jan 2021 01:43:59 +1000 Subject: [PATCH] TimingEvents: Use function pointers instead of std::function --- src/core/cdrom.cpp | 12 ++++++++---- src/core/dma.cpp | 6 ++++-- src/core/gpu.cpp | 8 ++++++-- src/core/mdec.cpp | 5 +++-- src/core/memory_card.cpp | 6 ++++-- src/core/pad.cpp | 4 +++- src/core/spu.cpp | 12 +++++++----- src/core/timers.cpp | 4 +++- src/core/timing_event.cpp | 15 ++++++++------- src/core/timing_event.h | 19 ++++++++++--------- 10 files changed, 56 insertions(+), 35 deletions(-) diff --git a/src/core/cdrom.cpp b/src/core/cdrom.cpp index c7f981e16..ecfdcedc0 100644 --- a/src/core/cdrom.cpp +++ b/src/core/cdrom.cpp @@ -78,10 +78,14 @@ CDROM::~CDROM() = default; void CDROM::Initialize() { - m_command_event = - TimingEvents::CreateTimingEvent("CDROM Command Event", 1, 1, std::bind(&CDROM::ExecuteCommand, this), false); - m_drive_event = TimingEvents::CreateTimingEvent("CDROM Drive Event", 1, 1, - std::bind(&CDROM::ExecuteDrive, this, std::placeholders::_2), false); + m_command_event = TimingEvents::CreateTimingEvent( + "CDROM Command Event", 1, 1, + [](void* param, TickCount ticks, TickCount ticks_late) { static_cast(param)->ExecuteCommand(); }, this, + false); + m_drive_event = TimingEvents::CreateTimingEvent( + "CDROM Drive Event", 1, 1, + [](void* param, TickCount ticks, TickCount ticks_late) { static_cast(param)->ExecuteDrive(ticks_late); }, + this, false); if (g_settings.cdrom_read_thread) m_reader.StartThread(); diff --git a/src/core/dma.cpp b/src/core/dma.cpp index 3d6b3951c..add0e43fc 100644 --- a/src/core/dma.cpp +++ b/src/core/dma.cpp @@ -29,8 +29,10 @@ void DMA::Initialize() m_halt_ticks = g_settings.dma_halt_ticks; m_transfer_buffer.resize(32); - m_unhalt_event = TimingEvents::CreateTimingEvent("DMA Transfer Unhalt", 1, m_max_slice_ticks, - std::bind(&DMA::UnhaltTransfer, this, std::placeholders::_1), false); + m_unhalt_event = TimingEvents::CreateTimingEvent( + "DMA Transfer Unhalt", 1, m_max_slice_ticks, + [](void* param, TickCount ticks, TickCount ticks_late) { static_cast(param)->UnhaltTransfer(ticks); }, this, + false); Reset(); } diff --git a/src/core/gpu.cpp b/src/core/gpu.cpp index c7d21f5d6..12f045719 100644 --- a/src/core/gpu.cpp +++ b/src/core/gpu.cpp @@ -31,9 +31,13 @@ bool GPU::Initialize(HostDisplay* host_display) m_force_progressive_scan = g_settings.gpu_disable_interlacing; m_force_ntsc_timings = g_settings.gpu_force_ntsc_timings; m_crtc_tick_event = TimingEvents::CreateTimingEvent( - "GPU CRTC Tick", 1, 1, std::bind(&GPU::CRTCTickEvent, this, std::placeholders::_1), true); + "GPU CRTC Tick", 1, 1, + [](void* param, TickCount ticks, TickCount ticks_late) { static_cast(param)->CRTCTickEvent(ticks); }, this, + true); m_command_tick_event = TimingEvents::CreateTimingEvent( - "GPU Command Tick", 1, 1, std::bind(&GPU::CommandTickEvent, this, std::placeholders::_1), true); + "GPU Command Tick", 1, 1, + [](void* param, TickCount ticks, TickCount ticks_late) { static_cast(param)->CommandTickEvent(ticks); }, this, + true); m_fifo_size = g_settings.gpu_fifo_size; m_max_run_ahead = g_settings.gpu_max_run_ahead; m_console_is_pal = System::IsPALRegion(); diff --git a/src/core/mdec.cpp b/src/core/mdec.cpp index 5625f80ac..e6008774a 100644 --- a/src/core/mdec.cpp +++ b/src/core/mdec.cpp @@ -18,8 +18,9 @@ MDEC::~MDEC() = default; void MDEC::Initialize() { - m_block_copy_out_event = - TimingEvents::CreateTimingEvent("MDEC Block Copy Out", 1, 1, std::bind(&MDEC::CopyOutBlock, this), false); + m_block_copy_out_event = TimingEvents::CreateTimingEvent( + "MDEC Block Copy Out", 1, 1, + [](void* param, TickCount ticks, TickCount ticks_late) { static_cast(param)->CopyOutBlock(); }, this, false); m_total_blocks_decoded = 0; Reset(); } diff --git a/src/core/memory_card.cpp b/src/core/memory_card.cpp index 5292dff2a..6fdd3767f 100644 --- a/src/core/memory_card.cpp +++ b/src/core/memory_card.cpp @@ -13,8 +13,10 @@ MemoryCard::MemoryCard() { m_FLAG.no_write_yet = true; - m_save_event = TimingEvents::CreateTimingEvent("Memory Card Host Flush", GetSaveDelayInTicks(), GetSaveDelayInTicks(), - std::bind(&MemoryCard::SaveIfChanged, this, true), false); + m_save_event = TimingEvents::CreateTimingEvent( + "Memory Card Host Flush", GetSaveDelayInTicks(), GetSaveDelayInTicks(), + [](void* param, TickCount ticks, TickCount ticks_late) { static_cast(param)->SaveIfChanged(true); }, + this, false); } MemoryCard::~MemoryCard() diff --git a/src/core/pad.cpp b/src/core/pad.cpp index 467fb0365..1c1cc1c61 100644 --- a/src/core/pad.cpp +++ b/src/core/pad.cpp @@ -17,7 +17,9 @@ Pad::~Pad() = default; void Pad::Initialize() { m_transfer_event = TimingEvents::CreateTimingEvent( - "Pad Serial Transfer", 1, 1, std::bind(&Pad::TransferEvent, this, std::placeholders::_2), false); + "Pad Serial Transfer", 1, 1, + [](void* param, TickCount ticks, TickCount ticks_late) { static_cast(param)->TransferEvent(ticks_late); }, + this, false); Reset(); } diff --git a/src/core/spu.cpp b/src/core/spu.cpp index eab9b4f3c..d1e509ae9 100644 --- a/src/core/spu.cpp +++ b/src/core/spu.cpp @@ -24,11 +24,13 @@ void SPU::Initialize() // (X * D) / N / 768 -> (X * D) / (N * 768) m_cpu_ticks_per_spu_tick = System::ScaleTicksToOverclock(SYSCLK_TICKS_PER_SPU_TICK); m_cpu_tick_divider = static_cast(g_settings.cpu_overclock_numerator * SYSCLK_TICKS_PER_SPU_TICK); - m_tick_event = TimingEvents::CreateTimingEvent("SPU Sample", m_cpu_ticks_per_spu_tick, m_cpu_ticks_per_spu_tick, - std::bind(&SPU::Execute, this, std::placeholders::_1), false); - m_transfer_event = - TimingEvents::CreateTimingEvent("SPU Transfer", TRANSFER_TICKS_PER_HALFWORD, TRANSFER_TICKS_PER_HALFWORD, - std::bind(&SPU::ExecuteTransfer, this, std::placeholders::_1), false); + m_tick_event = TimingEvents::CreateTimingEvent( + "SPU Sample", m_cpu_ticks_per_spu_tick, m_cpu_ticks_per_spu_tick, + [](void* param, TickCount ticks, TickCount ticks_late) { static_cast(param)->Execute(ticks); }, this, false); + m_transfer_event = TimingEvents::CreateTimingEvent( + "SPU Transfer", TRANSFER_TICKS_PER_HALFWORD, TRANSFER_TICKS_PER_HALFWORD, + [](void* param, TickCount ticks, TickCount ticks_late) { static_cast(param)->ExecuteTransfer(ticks); }, this, + false); Reset(); } diff --git a/src/core/timers.cpp b/src/core/timers.cpp index d1936da08..81b7d37b8 100644 --- a/src/core/timers.cpp +++ b/src/core/timers.cpp @@ -18,7 +18,9 @@ Timers::~Timers() = default; void Timers::Initialize() { m_sysclk_event = TimingEvents::CreateTimingEvent( - "Timer SysClk Interrupt", 1, 1, std::bind(&Timers::AddSysClkTicks, this, std::placeholders::_1), false); + "Timer SysClk Interrupt", 1, 1, + [](void* param, TickCount ticks, TickCount ticks_late) { static_cast(param)->AddSysClkTicks(ticks); }, + this, false); Reset(); } diff --git a/src/core/timing_event.cpp b/src/core/timing_event.cpp index 008cba496..93790d22d 100644 --- a/src/core/timing_event.cpp +++ b/src/core/timing_event.cpp @@ -36,10 +36,10 @@ void Shutdown() } std::unique_ptr CreateTimingEvent(std::string name, TickCount period, TickCount interval, - TimingEventCallback callback, bool activate) + TimingEventCallback callback, void* callback_param, bool activate) { std::unique_ptr event = - std::make_unique(std::move(name), period, interval, std::move(callback)); + std::make_unique(std::move(name), period, interval, callback, callback_param); if (activate) event->Activate(); @@ -291,7 +291,7 @@ void RunEvents() event->m_time_since_last_run = 0; // The cycles_late is only an indicator, it doesn't modify the cycles to execute. - event->m_callback(ticks_to_execute, ticks_late); + event->m_callback(event->m_callback_param, ticks_to_execute, ticks_late); if (event->m_active) SortEvent(event); } @@ -369,9 +369,10 @@ bool DoState(StateWrapper& sw) } // namespace TimingEvents -TimingEvent::TimingEvent(std::string name, TickCount period, TickCount interval, TimingEventCallback callback) - : m_downcount(interval), m_time_since_last_run(0), m_period(period), m_interval(interval), - m_callback(std::move(callback)), m_name(std::move(name)), m_active(false) +TimingEvent::TimingEvent(std::string name, TickCount period, TickCount interval, TimingEventCallback callback, + void* callback_param) + : m_downcount(interval), m_time_since_last_run(0), m_period(period), m_interval(interval), m_callback(callback), + m_callback_param(callback_param), m_name(std::move(name)), m_active(false) { } @@ -448,7 +449,7 @@ void TimingEvent::InvokeEarly(bool force /* = false */) m_downcount = pending_ticks + m_interval; m_time_since_last_run -= ticks_to_execute; - m_callback(ticks_to_execute, 0); + m_callback(m_callback_param, ticks_to_execute, 0); // Since we've changed the downcount, we need to re-sort the events. DebugAssert(TimingEvents::s_current_event != this); diff --git a/src/core/timing_event.h b/src/core/timing_event.h index 6d2d4e03a..780e5aa48 100644 --- a/src/core/timing_event.h +++ b/src/core/timing_event.h @@ -9,12 +9,13 @@ class StateWrapper; // Event callback type. Second parameter is the number of cycles the event was executed "late". -using TimingEventCallback = std::function; +using TimingEventCallback = void (*)(void* param, TickCount ticks, TickCount ticks_late); class TimingEvent { public: - TimingEvent(std::string name, TickCount period, TickCount interval, TimingEventCallback callback); + TimingEvent(std::string name, TickCount period, TickCount interval, TimingEventCallback callback, + void* callback_param); ~TimingEvent(); ALWAYS_INLINE const std::string& GetName() const { return m_name; } @@ -59,14 +60,16 @@ public: TimingEvent* prev = nullptr; TimingEvent* next = nullptr; + TimingEventCallback m_callback; + void* m_callback_param; + TickCount m_downcount; TickCount m_time_since_last_run; TickCount m_period; TickCount m_interval; - - TimingEventCallback m_callback; - std::string m_name; bool m_active; + + std::string m_name; }; namespace TimingEvents { @@ -79,7 +82,7 @@ void Shutdown(); /// Creates a new event. std::unique_ptr CreateTimingEvent(std::string name, TickCount period, TickCount interval, - TimingEventCallback callback, bool activate); + TimingEventCallback callback, void* callback_param, bool activate); /// Serialization. bool DoState(StateWrapper& sw); @@ -90,6 +93,4 @@ void UpdateCPUDowncount(); TimingEvent** GetHeadEventPtr(); - - -} // namespace TimingEventManager \ No newline at end of file +} // namespace TimingEvents \ No newline at end of file