Skip to content

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

C++

web: collect unrecorded VU1 programs while playing

Samir Nuri8aac181f1ad2
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 615caa0..7ac921f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -87,7 +87,7 @@ if(PS2_GENERATED)
add_executable(ps2web host/web.cpp)
target_link_libraries(ps2web PRIVATE ps2game ps2runtime)
target_link_options(ps2web PRIVATE --profiling-funcs -sJSPI -sALLOW_MEMORY_GROWTH=1 -sINITIAL_MEMORY=268435456 -sPTHREAD_POOL_SIZE=2 -sSTACK_SIZE=16777216 -sENVIRONMENT=web
- -sEXPORTED_FUNCTIONS=_main,_ps2web_pad,_ps2web_resize,_ps2web_gpu_ms,_ps2web_frames,_ps2web_primitives,_ps2web_audio_ring,_ps2web_audio_queued_ms,_ps2web_idle_ms
+ -sEXPORTED_FUNCTIONS=_main,_ps2web_pad,_ps2web_resize,_ps2web_gpu_ms,_ps2web_frames,_ps2web_primitives,_ps2web_audio_ring,_ps2web_audio_queued_ms,_ps2web_idle_ms,_ps2web_vu_interpreted
-sEXPORTED_RUNTIME_METHODS=callMain,addRunDependency,removeRunDependency,HEAPU8,ENV
--shell-file ${CMAKE_SOURCE_DIR}/web/shell.html --preload-file ${PS2_WEB_ELF}@/game/core.elf)
set_target_properties(ps2web PROPERTIES SUFFIX ".html" LINK_DEPENDS ${CMAKE_SOURCE_DIR}/web/shell.html)
diff --git a/host/web.cpp b/host/web.cpp
index 9aaf015..50d46b6 100644
--- a/host/web.cpp
+++ b/host/web.cpp
@@ -42,6 +42,17 @@ EM_JS(void, ps2web_fetch, (const char *url, double offset, int size, void *out,
});
});
+EM_JS(int, ps2web_save_program, (const char *name, const std::uint8_t *code, int size), {
+ try {
+ const xhr = new XMLHttpRequest();
+ xhr.open('PUT', 'vu1/' + UTF8ToString(name), false);
+ xhr.send(HEAPU8.slice(code, code + size));
+ return xhr.status;
+ } catch (e) {
+ return 0;
+ }
+});
+
EM_JS(double, ps2web_disc_size, (), { return Module.discSize || 0; });
EM_JS(char *, ps2web_disc_url, (), { return stringToNewUTF8(new URL(Module.discUrl, location.href).href); });
EM_JS(void, ps2web_frame_done, (), { if (Module.onFrame) Module.onFrame(); });
@@ -234,6 +245,7 @@ extern "C" EMSCRIPTEN_KEEPALIVE double ps2web_frames() { return static_cast<doub
extern "C" EMSCRIPTEN_KEEPALIVE double ps2web_primitives() { return static_cast<double>(ps2::primitives_drawn()); }
extern "C" EMSCRIPTEN_KEEPALIVE void *ps2web_audio_ring() { return &g_audio; }
extern "C" EMSCRIPTEN_KEEPALIVE double ps2web_audio_queued_ms() { return audio_queued_ms(); }
+extern "C" EMSCRIPTEN_KEEPALIVE double ps2web_vu_interpreted() { return static_cast<double>(ps2::vu1_interpreted_calls()); }
extern "C" EMSCRIPTEN_KEEPALIVE double ps2web_idle_ms(int which) {
if (which == 2) return ps2::vu_thread_idle_ms();
return which ? g_render_idle.load() : g_emulation_idle.load();
@@ -253,6 +265,13 @@ int main(int argc, char **argv) {
g_disc_size = static_cast<std::uint64_t>(ps2web_disc_size());
g_disc_url = ps2web_disc_url();
ps2::g_options.on_vblank = on_vblank;
+ ps2::g_options.on_vu_program = [](const std::string &name, const std::uint8_t *code, std::size_t size) {
+ static bool unsupported = false;
+ if (unsupported) return;
+ const int status = ps2web_save_program(name.c_str(), code, static_cast<int>(size));
+ if (status == 200 || status == 201) std::fprintf(stderr, "[web] saved VU1 program %s\n", name.c_str());
+ else unsupported = true;
+ };
ps2::set_audio_sink(push_audio);
register_generated_code();
register_generated_vu();
diff --git a/runtime/include/ps2/system.hpp b/runtime/include/ps2/system.hpp
index 3ef0fa6..7c0e252 100644
--- a/runtime/include/ps2/system.hpp
+++ b/runtime/include/ps2/system.hpp
@@ -36,6 +36,7 @@ struct Options {
std::uint32_t frame_every = 60;
std::vector<Press> 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;
};
extern Options g_options;
@@ -89,6 +90,7 @@ void start_render_thread();
void start_vu_thread();
void run_vu_thread();
double vu_thread_idle_ms();
+std::uint64_t vu1_interpreted_calls();
void post_present();
bool render_pending(bool surface, const std::function<void(const std::uint32_t *rgba, std::uint32_t width, std::uint32_t height)> &copy);
void wait_render_work(int timeout_ms);
diff --git a/runtime/src/system.cpp b/runtime/src/system.cpp
index 0d61add..4cdafe3 100644
--- a/runtime/src/system.cpp
+++ b/runtime/src/system.cpp
@@ -292,6 +292,7 @@ void start_vu_thread() { gs::queue::vu().start(32u << 20); }
std::atomic<double> g_vu_idle_ms{0.0};
double vu_thread_idle_ms() { return g_vu_idle_ms.load(); }
+std::uint64_t vu1_interpreted_calls() { return vu::interpreted_calls(); }
void run_vu_thread() {
gs::queue::role() = gs::queue::Role::Vu;
diff --git a/runtime/src/vu_micro.cpp b/runtime/src/vu_micro.cpp
index 2cfed1a..8078fe4 100644
--- a/runtime/src/vu_micro.cpp
+++ b/runtime/src/vu_micro.cpp
@@ -8,6 +8,7 @@
#include "ps2/vu.hpp"
#include "ps2/vu_ops.hpp"
+#include <atomic>
#include <cmath>
#include <cstdio>
#include <cstdlib>
@@ -16,6 +17,7 @@
#include <fstream>
#include <string>
#include <unordered_map>
+#include <unordered_set>
namespace ps2::vu {
@@ -661,6 +663,10 @@ std::uint32_t itop() { return g_itop; }
ProgramFn lookup(std::uint32_t address);
+std::atomic<std::uint64_t> g_interpreted_calls{0};
+
+std::uint64_t interpreted_calls() { return g_interpreted_calls.load(std::memory_order_relaxed); }
+
void interpret(int unit, std::uint32_t pc) {
static const bool interpret_only = std::getenv("PS2_VU_INTERPRET") != nullptr;
static const bool count = std::getenv("PS2_VU_COUNT") != nullptr;
@@ -673,6 +679,7 @@ void interpret(int unit, std::uint32_t pc) {
return;
}
}
+ if (unit == 1) g_interpreted_calls.fetch_add(1, std::memory_order_relaxed);
if (count && unit == 1) {
++misses[pc];
if (++interpreted % 5000 == 0) {
@@ -693,9 +700,14 @@ ProgramFn lookup(std::uint32_t address) {
const auto it = g_programs.find(key);
e.function = it == g_programs.end() ? nullptr : it->second;
static const char *record = std::getenv("PS2_VU_RECORD");
- if (!e.function && record) {
+ if (!e.function && (record || g_options.on_vu_program)) {
char name[64];
std::snprintf(name, sizeof name, "%016llX_%04X.bin", static_cast<unsigned long long>(key), address & 16383u);
+ if (g_options.on_vu_program) {
+ static std::unordered_set<std::string> reported;
+ if (reported.insert(name).second) g_options.on_vu_program(name, g_vu1_code, 16384);
+ return e.function;
+ }
const std::filesystem::path path = std::filesystem::path(record) / name;
if (!std::filesystem::exists(path)) {
std::filesystem::create_directories(record);
diff --git a/runtime/src/vu_micro.hpp b/runtime/src/vu_micro.hpp
index 0faed9d..65718a8 100644
--- a/runtime/src/vu_micro.hpp
+++ b/runtime/src/vu_micro.hpp
@@ -12,5 +12,6 @@ std::uint32_t tpc(int unit);
void set_tops(std::uint32_t top, std::uint32_t itop);
void code_changed(int unit);
Vu0Regs &regs(int unit);
+std::uint64_t interpreted_calls();
}
diff --git a/scripts/serve.py b/scripts/serve.py
index dd3c502..31f6bd6 100755
--- a/scripts/serve.py
+++ b/scripts/serve.py
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
"""Serves a web build and a disc image with HTTP range requests.
-usage: serve.py <build dir> <disc image> [port]
+usage: serve.py <build dir> <disc image> [port] [--record <dir for unrecorded VU1 programs>]
"""
import http.server
@@ -10,6 +10,11 @@ import re
import shutil
import sys
+RECORD = None
+if '--record' in sys.argv:
+ at = sys.argv.index('--record')
+ RECORD = os.path.abspath(sys.argv[at + 1])
+ del sys.argv[at:at + 2]
BUILD = os.path.abspath(sys.argv[1])
DISC = os.path.abspath(sys.argv[2])
PORT = int(sys.argv[3]) if len(sys.argv) > 3 else 8620
@@ -84,6 +89,23 @@ class Handler(http.server.BaseHTTPRequestHandler):
self.wfile.write(chunk)
left -= len(chunk)
+ def do_PUT(self):
+ match = re.match(r'^/vu1/([0-9A-F]{16}_[0-9A-F]{4}\.bin)$', self.path.split('?', 1)[0])
+ length = int(self.headers.get('Content-Length', '0'))
+ if not RECORD or not match or length != 16384:
+ self.send_error(501 if not RECORD else 400)
+ return
+ data = self.rfile.read(length)
+ target = os.path.join(RECORD, match.group(1))
+ if not os.path.exists(target):
+ os.makedirs(RECORD, exist_ok=True)
+ with open(target, 'wb') as f:
+ f.write(data)
+ print('recorded', match.group(1), flush=True)
+ self.send_response(201)
+ self.send_header('Content-Length', '0')
+ self.end_headers()
+
def log_message(self, fmt, *args):
pass
diff --git a/web/shell.html b/web/shell.html
index 0ce81f6..ea79a8a 100644
--- a/web/shell.html
+++ b/web/shell.html
@@ -363,7 +363,7 @@ async function chooseGpu() {
return 'GPU cannot draw to the page, copying frames';
}
-let frames = 0, last = performance.now(), lastGpu = 0, lastEmuIdle = 0, lastRenderIdle = 0, lastVuIdle = 0;
+let frames = 0, last = performance.now(), lastGpu = 0, lastEmuIdle = 0, lastRenderIdle = 0, lastVuIdle = 0, lastInterpreted = 0;
var Module = {
canvas,
arguments: ['/game/core.elf', '--arg', params.get('arg') || 'cdrom0:\\CORE.GT4;1'].concat(params.getAll('press').flatMap((p) => p.split(',').flatMap((x) => ['--press', x]))),
@@ -407,10 +407,13 @@ var Module = {
const gpu = Module._ps2web_gpu_ms();
const memory = (Module.HEAPU8.length / 1048576).toFixed(0);
const emuIdle = Module._ps2web_idle_ms(0), renderIdle = Module._ps2web_idle_ms(1), vuIdle = Module._ps2web_idle_ms(2), span = now - last;
+ const interpreted = Module._ps2web_vu_interpreted();
statsEl.textContent = 'frame ' + Module._ps2web_frames() + ' ' + (frames * 1000 / span).toFixed(1) + ' fps GPU ' + ((gpu - lastGpu) / span * 100).toFixed(0) +
'% idle emu ' + ((emuIdle - lastEmuIdle) / span * 100).toFixed(0) + '% vu ' + ((vuIdle - lastVuIdle) / span * 100).toFixed(0) +
'% gs ' + ((renderIdle - lastRenderIdle) / span * 100).toFixed(0) +
- '% heap ' + memory + ' MB audio ' + Module.audioQueuedMs().toFixed(0) + ' ms';
+ '% heap ' + memory + ' MB audio ' + Module.audioQueuedMs().toFixed(0) + ' ms' +
+ (interpreted > lastInterpreted ? ' vu1 interpreted ' + Math.round((interpreted - lastInterpreted) * 1000 / span) + '/s' : '');
+ lastInterpreted = interpreted;
lastEmuIdle = emuIdle;
lastRenderIdle = renderIdle;
lastVuIdle = vuIdle;