Skip to content

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

C++

Porting tools: compiled VU programs proven against the interpreter

Riccardo Giorato2751ff3239da
diff --git a/runtime/src/dispatch.cpp b/runtime/src/dispatch.cpp
index bec4586..14691bc 100644
--- a/runtime/src/dispatch.cpp
+++ b/runtime/src/dispatch.cpp
@@ -24,9 +24,6 @@ constexpr std::uint32_t kModuleMagic = 0x32524E53u;
struct Instance {
std::uint32_t base;
const ModuleInfo *module;
- // One word that tells the image found here is still the one found before;
- // the full checks read too much to pay on every call into the module.
- std::uint32_t spot_at, spot_value;
};
const ModuleInfo *g_modules = nullptr;
@@ -45,13 +42,20 @@ bool loaded_at(const ModuleInfo &m, std::uint32_t base) {
return true;
}
+// The word a call checks turns with each call, so every translated code word
+// is proven again within check_count calls: one word per call keeps calls
+// cheap while even a one word change of the image is caught in time.
+std::uint32_t g_spot_cursor = 0;
+
const Instance *in_place(const Instance &i) {
- return ld32(i.base + i.spot_at) == i.spot_value ? &i : nullptr;
+ if (std::getenv("PS2_WINDOW_FULL")) return loaded_at(*i.module, i.base) ? &i : nullptr;
+ if (!i.module->check_count) return &i;
+ const std::uint32_t c = g_spot_cursor++ % i.module->check_count;
+ return ld32(i.base + i.module->checks[2 * c]) == i.module->checks[2 * c + 1] ? &i : nullptr;
}
Instance make_instance(std::uint32_t base, const ModuleInfo *m) {
- const bool any = m->check_count != 0;
- return {base, m, any ? m->checks[0] : 0, any ? m->checks[1] : 0};
+ return {base, m};
}
const Instance *instance_for(std::uint32_t pc) {
diff --git a/runtime/src/gs/gif.cpp b/runtime/src/gs/gif.cpp
index 74c6c88..ea48474 100644
--- a/runtime/src/gs/gif.cpp
+++ b/runtime/src/gs/gif.cpp
@@ -198,7 +198,10 @@ void write(std::uint32_t a, std::uint32_t v) {
if ((a & 0xF0u) == 0x10u) g_stat = (g_stat & ~1u) | (v & 1u);
}
-void set_path3_masked(bool masked) { g_stat = (g_stat & ~2u) | (masked ? 2u : 0u); }
+void set_path3_masked(bool masked) {
+ g_stat = (g_stat & ~2u) | (masked ? 2u : 0u);
+ if (std::getenv("PS2_TRACE_PATH3")) std::fprintf(stderr, "[gif] path3 %s\n", masked ? "masked" : "unmasked");
+}
}
diff --git a/runtime/src/gs/gs.cpp b/runtime/src/gs/gs.cpp
index 2991477..9f7e1e9 100644
--- a/runtime/src/gs/gs.cpp
+++ b/runtime/src/gs/gs.cpp
@@ -638,6 +638,10 @@ void signal(std::uint32_t reg, std::uint64_t value) {
void write_packed(std::uint32_t reg, const Reg128 &q) {
State &s = g_state;
+ if (const char *seq = std::getenv("PS2_TRACE_GSSEQ")) {
+ static std::uint64_t n = 0, start = std::strtoull(seq, nullptr, 10);
+ if (++n >= start && n < start + 100000) std::fprintf(stderr, "[gs] w %02X %016llX\n", reg, static_cast<unsigned long long>(q.d[0]));
+ }
switch (reg) {
case 0x0: write(0x00, q.d[0] & 0x7FFu); break;
case 0x1: {
diff --git a/runtime/src/iop/cdvdfsv.cpp b/runtime/src/iop/cdvdfsv.cpp
index d0097e8..6487e8d 100644
--- a/runtime/src/iop/cdvdfsv.cpp
+++ b/runtime/src/iop/cdvdfsv.cpp
@@ -38,6 +38,7 @@ void install_cdvdfsv() {
g_streaming = false;
g_stream_pos = 0;
register_job(kJobFsvRead, [](const Job &j) {
+ if (g_trace_hle) std::fprintf(stderr, "[cdvdfsv] stream write lsn %u count %u to 0x%08X\n", j.a, j.b, j.c);
if (!disc::ready(j.a, j.b)) {
schedule(now() + usec_to_cycles(1000), j);
return;
@@ -98,6 +99,7 @@ void install_cdvdfsv() {
});
sif::register_hle_server(0x80000595u, [](std::uint32_t method, std::uint32_t args, std::uint32_t, std::uint32_t ret, std::uint32_t ret_size) {
const std::uint32_t sector = ee32(args), count = ee32(args + 4), dst = ee32(args + 8);
+ if (g_trace_hle) std::fprintf(stderr, "[cdvdfsv] N 0x%X sector %u count %u to 0x%08X\n", method, sector, count, dst);
switch (method) {
case 0x01:
if (ret_size >= 4) set_ee32(ret, 0);
@@ -111,6 +113,7 @@ void install_cdvdfsv() {
return false;
case 0x09: {
const std::uint32_t cmd = ee32(args + 12);
+ if (g_trace_hle) std::fprintf(stderr, "[cdvdfsv] stream cmd %u (pos %u)\n", cmd, g_stream_pos);
switch (cmd) {
case 1: g_stream_pos = sector; g_streaming = true; set_ee32(ret, 1); return true;
case 2: {
diff --git a/runtime/src/vu_micro.cpp b/runtime/src/vu_micro.cpp
index 7c037c4..55f2828 100644
--- a/runtime/src/vu_micro.cpp
+++ b/runtime/src/vu_micro.cpp
@@ -805,6 +805,7 @@ void run_entry(int unit, std::uint32_t pc);
bool g_chaining = false;
std::int64_t g_chained = -1;
+std::uint64_t g_last_key = 0;
void interpret(int unit, std::uint32_t pc) {
if (unit == 1 && g_capture) {
@@ -839,6 +840,49 @@ void run_entry(int unit, std::uint32_t pc) {
++compiled;
const ProgramFn jit = g_entries[(pc & 16383u) >> 3].jit;
static std::uint64_t sampled = 0;
+ static const bool check_vs_interpreter = std::getenv("PS2_VU_CHECK") != nullptr;
+ static bool checking = false;
+ if (check_vs_interpreter && !checking && ++sampled % 64 == 0) {
+ // The interpreter is the reference: run the compiled function
+ // and, from the same state, the interpreter, and report the
+ // first registers or memory they disagree on.
+ static std::uint64_t checked = 0, mismatches = 0;
+ static std::uint8_t before_mem[16384], run_mem[16384];
+ const Vu0Regs before = g_vu1;
+ std::memcpy(before_mem, g_vu1_mem, sizeof before_mem);
+ std::int64_t escape = -1;
+ const std::uint64_t key = program_key(g_vu1_code, reachable(g_vu1_code, 16383u, pc & 16383u), pc & 16383u);
+ g_capture = &escape;
+ fn(g_vu1, g_vu1_mem);
+ g_capture = nullptr;
+ const Vu0Regs after = g_vu1;
+ std::memcpy(run_mem, g_vu1_mem, sizeof run_mem);
+ g_vu1 = before;
+ std::memcpy(g_vu1_mem, before_mem, sizeof g_vu1_mem);
+ checking = true;
+ interpret(1, pc);
+ checking = false;
+ if (escape < 0 && (std::memcmp(&after, &g_vu1, sizeof after) != 0 || std::memcmp(run_mem, g_vu1_mem, sizeof run_mem) != 0)) {
+ if (++mismatches <= 20) {
+ int vf = -1;
+ for (int i = 0; i < 32 && vf < 0; ++i)
+ if (std::memcmp(&after.vf[i], &g_vu1.vf[i], 16) != 0) vf = i;
+ int vi = -1;
+ for (int i = 0; i < 16 && vi < 0; ++i)
+ if (after.vi[i] != g_vu1.vi[i]) vi = i;
+ std::uint32_t word = ~0u;
+ for (int i = 0; i < 16384 && word == ~0u; ++i) word = run_mem[i] ^ g_vu1_mem[i] ? i : ~0u;
+ std::fprintf(stderr, "[vu-check] key %016llX entry %04X call %llu: vf%d vi%d q/p/i/r %d flags %d mem %d\n", static_cast<unsigned long long>(key),
+ pc & 16383u, static_cast<unsigned long long>(checked), vf, vi,
+ after.q != g_vu1.q || after.p != g_vu1.p || after.i != g_vu1.i || after.r != g_vu1.r,
+ after.mac != g_vu1.mac || after.status != g_vu1.status || after.clip != g_vu1.clip, word == ~0u ? -1 : static_cast<int>(word));
+ }
+ }
+ ++checked;
+ g_vu1 = after;
+ std::memcpy(g_vu1_mem, run_mem, sizeof g_vu1_mem);
+ return;
+ }
if (jit && jit != fn && ++sampled % 32 == 0) check_jit(fn, jit);
else fn(g_vu1, g_vu1_mem);
return;
@@ -858,6 +902,7 @@ void run_entry(int unit, std::uint32_t pc) {
ProgramFn lookup(std::uint32_t address) {
EntryCache &e = g_entries[(address & 16383u) >> 3];
+ g_last_key = 0;
if (e.generation == g_generation[1]) return e.function;
e.generation = g_generation[1];
for (const Candidate &c : e.candidates)
@@ -868,6 +913,7 @@ ProgramFn lookup(std::uint32_t address) {
}
const std::vector<std::uint32_t> pcs = reachable(g_vu1_code, 16383u, address);
const std::uint64_t key = program_key(g_vu1_code, pcs, address & 16383u);
+ g_last_key = key;
const auto it = g_programs.find(key);
e.function = it == g_programs.end() ? nullptr : it->second;
e.jit = nullptr;