From 5fbda03cebded2e1ffba9cdc02dfbe6f79090cfd Mon Sep 17 00:00:00 2001 From: iris Date: Thu, 21 May 2026 17:55:11 +0200 Subject: [PATCH] new idea --- src/backend/Scheduler.cpp | 85 ++++++++++++++++++++++----------------- src/backend/Scheduler.hpp | 49 +++++++++++----------- 2 files changed, 74 insertions(+), 60 deletions(-) diff --git a/src/backend/Scheduler.cpp b/src/backend/Scheduler.cpp index d031a0d..208aedc 100644 --- a/src/backend/Scheduler.cpp +++ b/src/backend/Scheduler.cpp @@ -5,47 +5,60 @@ void Scheduler::EnqueueRelative(const u64 t, const EventType type) { EnqueueAbso void Scheduler::EnqueueAbsolute(const u64 t, const EventType type) { events.push({t, type}); } -u64 Scheduler::Remove(const EventType eventType) const { - for (auto &[time, type] : events) { - if (type == eventType) { - const u64 ret = time - ticks; - type = NONE; - time = ticks; - return ret; +Event *Scheduler::Find(const EventType eventType) const { + for (auto &event : events) { + if (event.type == eventType) { + const u64 ret = event.time - ticks; + return &event; + } } - } - return 0; + return nullptr; +} + +u64 Scheduler::Remove(const EventType eventType) const { + auto event = Find(eventType); + if (!event) + return 0; + + const u64 ret = event->time - ticks; + event->type = NONE; + event->time = ticks; + return ret; } void Scheduler::Tick(const u64 t) { - n64::Mem& mem = n64::Core::GetMem(); - ticks += t; - n64::MI &mi = mem.mmio.mi; - n64::SI &si = mem.mmio.si; - n64::PI &pi = mem.mmio.pi; + n64::Mem &mem = n64::Core::GetMem(); + ticks += t; + n64::MI &mi = mem.mmio.mi; + n64::SI &si = mem.mmio.si; + n64::PI &pi = mem.mmio.pi; - while (ticks >= events.top().time) { - switch (const auto type = events.top().type) { - case SI_DMA: - si.DMA(); - break; - case PI_DMA_COMPLETE: - mi.InterruptRaise(n64::MI::Interrupt::PI); - pi.dmaBusy = false; - break; - case PI_BUS_WRITE_COMPLETE: - pi.ioBusy = false; - break; - case NONE: - break; - case IMPOSSIBLE: - Util::Error::GetInstance().Throw({Util::Error::Severity::UNRECOVERABLE}, {Util::Error::Type::ROM_LOAD_ERROR}, {}, {}, "Unrecognized rom endianness"); - return; - default: - Util::Error::GetInstance().Throw({Util::Error::Severity::UNRECOVERABLE}, {Util::Error::Type::ROM_LOAD_ERROR}, {}, {}, "Unknown scheduler event type {}", static_cast(type)); - return; + while (ticks >= events.top().time) { + switch (const auto type = events.top().type) { + case SI_DMA: + si.DMA(); + break; + case PI_DMA_COMPLETE: + mi.InterruptRaise(n64::MI::Interrupt::PI); + pi.dmaBusy = false; + break; + case PI_BUS_WRITE_COMPLETE: + pi.ioBusy = false; + break; + case NONE: + break; + case IMPOSSIBLE: + Util::Error::GetInstance().Throw({Util::Error::Severity::UNRECOVERABLE}, + {Util::Error::Type::ROM_LOAD_ERROR}, {}, {}, + "Unrecognized rom endianness"); + return; + default: + Util::Error::GetInstance().Throw({Util::Error::Severity::UNRECOVERABLE}, + {Util::Error::Type::ROM_LOAD_ERROR}, {}, {}, + "Unknown scheduler event type {}", static_cast(type)); + return; + } + events.pop(); } - events.pop(); - } } diff --git a/src/backend/Scheduler.hpp b/src/backend/Scheduler.hpp index 5a37a52..d88d501 100644 --- a/src/backend/Scheduler.hpp +++ b/src/backend/Scheduler.hpp @@ -6,41 +6,42 @@ enum EventType { NONE, PI_BUS_WRITE_COMPLETE, PI_DMA_COMPLETE, SI_DMA, IMPOSSIBLE }; struct Event { - u64 time; - EventType type; + u64 time; + EventType type; - friend bool operator<(const Event &rhs, const Event &lhs) { return rhs.time < lhs.time; } + friend bool operator<(const Event &rhs, const Event &lhs) { return rhs.time < lhs.time; } - friend bool operator>(const Event &rhs, const Event &lhs) { return rhs.time > lhs.time; } + friend bool operator>(const Event &rhs, const Event &lhs) { return rhs.time > lhs.time; } - friend bool operator>=(const Event &rhs, const Event &lhs) { return rhs.time >= lhs.time; } + friend bool operator>=(const Event &rhs, const Event &lhs) { return rhs.time >= lhs.time; } }; struct IterableEvents { - std::priority_queue, std::greater<>> events; + std::priority_queue, std::greater<>> events; - explicit IterableEvents() = default; - [[nodiscard]] auto top() const { return events.top(); } - auto pop() { events.pop(); } - [[nodiscard]] auto begin() const { return const_cast(&events.top()); } - [[nodiscard]] auto end() const { return begin() + events.size(); } - auto push(const Event e) { events.push(e); } + explicit IterableEvents() = default; + [[nodiscard]] auto top() const { return events.top(); } + auto pop() { events.pop(); } + [[nodiscard]] auto begin() const { return const_cast(&events.top()); } + [[nodiscard]] auto end() const { return begin() + events.size(); } + auto push(const Event e) { events.push(e); } }; struct Scheduler { - Scheduler() { EnqueueAbsolute(std::numeric_limits::max(), IMPOSSIBLE); } + Scheduler() { EnqueueAbsolute(std::numeric_limits::max(), IMPOSSIBLE); } - static Scheduler &GetInstance() { - static Scheduler instance; - return instance; - } + static Scheduler &GetInstance() { + static Scheduler instance; + return instance; + } - void EnqueueRelative(u64, EventType); - void EnqueueAbsolute(u64, EventType); - [[nodiscard]] u64 Remove(EventType) const; - void Tick(u64 t); + void EnqueueRelative(u64, EventType); + void EnqueueAbsolute(u64, EventType); + [[nodiscard]] u64 Remove(EventType) const; + [[nodiscard]] Event *Find(EventType) const; + void Tick(u64 t); - u8 index = 0; - u64 ticks = 0; - IterableEvents events{}; + u8 index = 0; + u64 ticks = 0; + IterableEvents events{}; };