Skip to content

PS2 games in the browser: static recompilation to WebAssembly with a native runtime

C++

EE: NTSC HBLANK period

Samir Nuri9913c70036d9
diff --git a/runtime/include/ps2/system.hpp b/runtime/include/ps2/system.hpp
index 91caade..864b223 100644
--- a/runtime/include/ps2/system.hpp
+++ b/runtime/include/ps2/system.hpp
@@ -57,6 +57,7 @@ void save_context(const Cpu &c, ThreadContext &t);
void load_context(Cpu &c, const ThreadContext &t);
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);
bool preempt(Cpu &c);
diff --git a/runtime/src/hw.cpp b/runtime/src/hw.cpp
index 8dce4a7..72062cc 100644
--- a/runtime/src/hw.cpp
+++ b/runtime/src/hw.cpp
@@ -49,7 +49,7 @@ std::uint64_t timer_divider(const Timer &t) {
case 0: return 2;
case 1: return 32;
case 2: return 512;
- default: return 18874;
+ default: return hblank_cycles();
}
}
@@ -123,7 +123,7 @@ void reset() {
void vblank_start() {
intc_raise(2);
gs::vblank(true);
- schedule(kEventVblankEnd, now() + 18874u * 22u, [] { intc_raise(3); });
+ schedule(kEventVblankEnd, now() + hblank_cycles() * 22u, [] { intc_raise(3); });
}
std::uint32_t intc_pending() { return g_intc_stat & g_intc_mask; }
diff --git a/runtime/src/kernel.cpp b/runtime/src/kernel.cpp
index 47de34f..2c61486 100644
--- a/runtime/src/kernel.cpp
+++ b/runtime/src/kernel.cpp
@@ -263,7 +263,7 @@ void run_alarms(Cpu &c) {
if (it == g_alarms.end()) continue;
const Alarm alarm = it->second;
g_alarms.erase(it);
- run_guest(c, alarm.callback, static_cast<std::uint32_t>(id), static_cast<std::uint32_t>(now() / 18874u), alarm.arg, alarm.gp);
+ run_guest(c, alarm.callback, static_cast<std::uint32_t>(id), static_cast<std::uint32_t>(now() / hblank_cycles()), alarm.arg, alarm.gp);
g_reschedule = true;
}
}
@@ -485,7 +485,7 @@ void handle_syscall(Cpu &c, std::uint32_t pc) {
case 0x18: case 0x1E: case 0xFC: case 0xFE: {
const int id = lowest_free(g_alarms, 64);
if (id < 0) { ret(c, -1); break; }
- g_alarms[id] = Alarm{arg(c, 1), arg(c, 2), c.r[28].w[0], now() + static_cast<std::uint64_t>(arg(c, 0) & 0xFFFFu) * 18874u};
+ g_alarms[id] = Alarm{arg(c, 1), arg(c, 2), c.r[28].w[0], now() + static_cast<std::uint64_t>(arg(c, 0) & 0xFFFFu) * hblank_cycles()};
arm_alarm(id);
ret(c, id);
break;
diff --git a/runtime/src/system.cpp b/runtime/src/system.cpp
index e6e6a52..7dcf8ee 100644
--- a/runtime/src/system.cpp
+++ b/runtime/src/system.cpp
@@ -134,7 +134,7 @@ void vblank() {
if (!g_options.frames_dir.empty() && g_options.frame_every && g_frames % g_options.frame_every == 0) dump_frame();
hw::vblank_start();
iop::vblank(true);
- schedule(hw::kEventVblankEnd + 100, now() + 18874u * 22u, [] { iop::vblank(false); });
+ schedule(hw::kEventVblankEnd + 100, now() + hblank_cycles() * 22u, [] { iop::vblank(false); });
kernel::on_vblank_start();
if (g_options.max_frames && g_frames >= g_options.max_frames) halt("frame limit reached");
if (g_options.save_state_frame && g_frames == g_options.save_state_frame) g_state_request = true;
@@ -180,6 +180,8 @@ void load_context(Cpu &c, const ThreadContext &t) {
std::uint64_t now() { return g_base + static_cast<std::uint64_t>(g_given - g_cpu.budget); }
+std::uint64_t hblank_cycles() { return frame_cycles() * 2u / (g_options.pal ? 625u : 525u); }
+
void set_budget(Cpu &c, std::int64_t cycles) {
g_base = now();
g_given = cycles;