Skip to content

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

C++

Analog stick holds for scripted runs, and a halt the page shows

Riccardo Giorato4b9b05a932b0
diff --git a/host/main.cpp b/host/main.cpp
index 4962c70..f998c37 100644
--- a/host/main.cpp
+++ b/host/main.cpp
@@ -57,6 +57,11 @@ int main(int argc, char **argv) {
if (!ps2::parse_press(argv[++i], press)) return usage();
ps2::g_options.presses.push_back(press);
}
+ else if (a == "--axis" && i + 1 < argc) {
+ ps2::AxisPress press;
+ if (!ps2::parse_axis(argv[++i], press)) return usage();
+ ps2::g_options.axis_presses.push_back(press);
+ }
#ifdef PS2_WINDOW
else if (a == "--window") window = true;
else if (a == "--fullscreen") window = fullscreen = true;
diff --git a/host/web.cpp b/host/web.cpp
index 8b5efe7..6e3c0d6 100644
--- a/host/web.cpp
+++ b/host/web.cpp
@@ -309,6 +309,10 @@ void *emulate(void *data) {
}
}
ps2::run();
+ EM_ASM({
+ if (typeof Module.onHalt === 'function')
+ Module.onHalt(UTF8ToString($0));
+ }, ps2::halt_reason().c_str());
return nullptr;
}
diff --git a/runtime/include/ps2/system.hpp b/runtime/include/ps2/system.hpp
index 6c9dc67..0d9e8a3 100644
--- a/runtime/include/ps2/system.hpp
+++ b/runtime/include/ps2/system.hpp
@@ -21,6 +21,13 @@ struct Press {
std::uint16_t buttons;
};
+struct AxisPress {
+ std::uint64_t frame;
+ std::uint32_t frames;
+ int axis;
+ std::uint8_t value;
+};
+
struct Options {
bool trace_syscalls = false;
bool trace_io = false;
@@ -35,6 +42,7 @@ struct Options {
std::string frames_dir;
std::uint32_t frame_every = 60;
std::vector<Press> presses;
+ std::vector<AxisPress> axis_presses;
std::function<void()> on_vblank;
std::function<void(const std::string &name, const std::uint8_t *code, std::size_t size)> on_vu_program;
std::string memcard_path;
@@ -77,6 +85,7 @@ bool save_state(const std::string &path);
bool load_state(const std::string &path);
bool load_state(const std::uint8_t *data, std::size_t size);
bool halted();
+const std::string &halt_reason();
std::uint64_t frame_count();
bool display_frame(std::vector<std::uint32_t> &rgba, std::uint32_t &width, std::uint32_t &height);
@@ -108,6 +117,7 @@ void wait_render_work(int timeout_ms);
void wake_render_thread();
bool copy_frame(std::vector<std::uint32_t> &rgba, std::uint32_t &width, std::uint32_t &height);
bool parse_press(const std::string &spec, Press &press);
+bool parse_axis(const std::string &spec, AxisPress &press);
double gpu_busy_ms();
void browser_yield();
std::uint64_t frames_run();
diff --git a/runtime/src/system.cpp b/runtime/src/system.cpp
index 3416212..fe99fc3 100644
--- a/runtime/src/system.cpp
+++ b/runtime/src/system.cpp
@@ -101,6 +101,13 @@ void dump_frame() {
}
void apply_presses() {
+ if (!g_options.axis_presses.empty()) {
+ // Outside a hold the stick rests; within one it takes the given tilt.
+ std::uint8_t axes[4] = {0x80, 0x80, 0x80, 0x80};
+ for (const AxisPress &p : g_options.axis_presses)
+ if (g_frames >= p.frame && g_frames < p.frame + p.frames) axes[p.axis] = p.value;
+ for (int a = 0; a < 4; ++a) iop::sio2::set_axis(0, a, axes[a]);
+ }
if (g_options.presses.empty()) return;
std::uint16_t mask = 0;
for (const Press &p : g_options.presses)
@@ -212,6 +219,7 @@ void halt(const std::string &reason) {
}
bool halted() { return g_halted; }
+const std::string &halt_reason() { return g_halt_reason; }
namespace {
@@ -511,6 +519,29 @@ bool parse_press(const std::string &spec, Press &press) {
return true;
}
+bool parse_axis(const std::string &spec, AxisPress &press) {
+ static const char *const kNames[4] = {"rx", "ry", "lx", "ly"};
+ std::size_t at = 0;
+ const auto field = [&](std::string &out) {
+ const auto colon = spec.find(':', at);
+ if (colon == std::string::npos) return false;
+ out = spec.substr(at, colon - at);
+ at = colon + 1;
+ return true;
+ };
+ std::string frame, axis, value;
+ if (!field(frame) || !field(axis)) return false;
+ const auto tail = spec.find(':', at);
+ value = spec.substr(at, tail == std::string::npos ? std::string::npos : tail - at);
+ press.frame = std::strtoull(frame.c_str(), nullptr, 10);
+ for (int i = 0; i < 4; ++i)
+ if (axis == kNames[i]) press.axis = i;
+ press.value = static_cast<std::uint8_t>(std::strtoul(value.c_str(), nullptr, 0));
+ press.frames = 6;
+ if (tail != std::string::npos) press.frames = static_cast<std::uint32_t>(std::strtoul(spec.c_str() + tail + 1, nullptr, 10));
+ return press.frame || press.frames;
+}
+
double gpu_busy_ms() { return gs::gpu_busy_ms(); }
#ifdef __EMSCRIPTEN__
diff --git a/web/shell.html b/web/shell.html
index 51db819..4d09f55 100644
--- a/web/shell.html
+++ b/web/shell.html
@@ -555,6 +555,12 @@ var Module = {
noSurface: params.has('nosurface'),
noCard: params.has('nocard'),
presentCopy: params.has('copy'),
+ onHalt: (reason) => {
+ clearTimeout(noteTimer);
+ noteTimer = 0;
+ statusEl.textContent = 'Halted: ' + reason;
+ statusEl.style.color = '#f66';
+ },
print: (text) => console.log(text),
printErr: (text) => console.log(text),
setStatus: (text) => {