PS2 games in the browser: static recompilation to WebAssembly with a native runtime
C++
Judge timer events at their scheduled time and hold the count before an overdue one
Samir Nuri022f0c804774
diff --git a/runtime/include/ps2/system.hpp b/runtime/include/ps2/system.hppindex 49f4272..6c9dc67 100644--- a/runtime/include/ps2/system.hpp+++ b/runtime/include/ps2/system.hpp@@ -60,6 +60,7 @@ std::uint64_t now();std::uint64_t hblank_cycles();void set_budget(Cpu &c, std::int64_t cycles);void schedule(int event, std::uint64_t at, std::function<void()> action);+std::uint64_t scheduled_at(int event);bool preempt(Cpu &c);bool has_code(std::uint32_t pc);diff --git a/runtime/src/hw.cpp b/runtime/src/hw.cppindex cb1fc99..2ccaa1f 100644--- a/runtime/src/hw.cpp+++ b/runtime/src/hw.cpp@@ -62,6 +62,13 @@ std::uint32_t timer_count(int n) {return static_cast<std::uint32_t>(((now() - t.base) / timer_divider(t)) & 0xFFFFu);}+std::uint32_t timer_read(int n) {+ const std::uint64_t due = scheduled_at(kEventTimer0 + n);+ if (due > now()) return timer_count(n);+ const Timer &t = g_timers[n];+ return static_cast<std::uint32_t>(((due - 1 - t.base) / timer_divider(t)) & 0xFFFFu);+}+void timer_set_count(int n, std::uint32_t value) {Timer &t = g_timers[n];t.frozen = value & 0xFFFFu;@@ -70,14 +77,14 @@ void timer_set_count(int n, std::uint32_t value) {void timer_schedule(int n);-void timer_event(int n) {+void timer_event(int n, std::uint64_t at) {Timer &t = g_timers[n];if (!(t.mode & 0x80u)) return;- const std::uint32_t count = timer_count(n);+ const std::uint32_t count = static_cast<std::uint32_t>(((at - t.base) / timer_divider(t)) & 0xFFFFu);if (count == (t.comp & 0xFFFFu)) {t.mode |= 0x400u;if (t.mode & 0x100u) intc_raise(9u + static_cast<unsigned>(n));- if (t.mode & 0x40u) timer_set_count(n, 0);+ if (t.mode & 0x40u) t.base = at;}if (count == 0 && !(t.mode & 0x40u && t.comp == 0)) {t.mode |= 0x800u;@@ -101,7 +108,7 @@ void timer_schedule(int n) {if (!(t.mode & 0x100u)) ticks = to_overflow;const std::uint64_t div = timer_divider(t);const std::uint64_t at = t.base + ((now() - t.base) / div + ticks) * div;- schedule(kEventTimer0 + n, at, [n] { timer_event(n); });+ schedule(kEventTimer0 + n, at, [n, at] { timer_event(n, at); });}int timer_index(std::uint32_t a) { return static_cast<int>((a >> 11) & 3u); }@@ -192,7 +199,7 @@ std::uint32_t read32(std::uint32_t a) {if (is_timer(a)) {const int n = timer_index(a);switch (a & 0x30u) {- case 0x00: return timer_count(n);+ case 0x00: return timer_read(n);case 0x10: return g_timers[n].mode;case 0x20: return g_timers[n].comp;default: return g_timers[n].hold;@@ -286,9 +293,9 @@ void write128(std::uint32_t a, const Reg128 &v) {namespace ps2::hw {-std::function<void()> event_action(int id) {+std::function<void()> event_action(int id, std::uint64_t at) {if (id == kEventVblankEnd) return [] { intc_raise(3); };- if (id >= kEventTimer0 && id <= kEventTimer3) return [n = id - kEventTimer0] { timer_event(n); };+ if (id >= kEventTimer0 && id <= kEventTimer3) return [n = id - kEventTimer0, at] { timer_event(n, at); };return [] {};}diff --git a/runtime/src/hw.hpp b/runtime/src/hw.hppindex 8f215da..40c4798 100644--- a/runtime/src/hw.hpp+++ b/runtime/src/hw.hpp@@ -21,7 +21,7 @@ std::uint32_t physical(std::uint32_t a);namespace hw {void state(ps2::state::Stream &s);-std::function<void()> event_action(int id);+std::function<void()> event_action(int id, std::uint64_t at);enum Event {kEventVblank,diff --git a/runtime/src/system.cpp b/runtime/src/system.cppindex e87ee66..0ace318 100644--- a/runtime/src/system.cpp+++ b/runtime/src/system.cpp@@ -193,6 +193,12 @@ void schedule(int event, std::uint64_t at, std::function<void()> action) {g_events.push_back({event, at, std::move(action)});}+std::uint64_t scheduled_at(int event) {+ for (const Event &e : g_events)+ if (e.id == event) return e.at;+ return UINT64_MAX;+}+bool preempt(Cpu &c) {run_due_events();refill(c);@@ -212,11 +218,11 @@ namespace {constexpr char kStateMagic[8] = {'P', 'S', '2', 'S', 'T', 'A', 'T', 'E'};constexpr std::uint32_t kStateVersion = 2;-std::function<void()> event_action(int id) {+std::function<void()> event_action(int id, std::uint64_t at) {if (id == hw::kEventVblank) return vblank;if (id == hw::kEventVblankEnd + 100) return [] { iop::vblank(false); };if (id >= hw::kEventCount) return kernel::alarm_action(id - hw::kEventCount);- return hw::event_action(id);+ return hw::event_action(id, at);}std::atomic<std::uint32_t> g_vu_fence{0};@@ -248,7 +254,7 @@ void machine_state(state::Stream &s) {s.vec(g_events, [&s](Event &e) {s.pod(e.id);s.pod(e.at);- if (s.loading()) e.action = event_action(e.id);+ if (s.loading()) e.action = event_action(e.id, e.at);});kernel::state(s);hw::state(s);