PS2 games in the browser: static recompilation to WebAssembly with a native runtime
C++
Clip flags count the cycles the VU waits on results
reikop701d64bb34d9
diff --git a/recomp/vurecomp.cpp b/recomp/vurecomp.cppindex 6e461cf..879720e 100644--- a/recomp/vurecomp.cpp+++ b/recomp/vurecomp.cpp@@ -143,7 +143,7 @@ Upper upper(std::uint32_t hi, bool flags_live) {if (kind == Nop) return u;if (kind == Clip) {u.pre = std::format("const std::uint32_t uclip = clip_value(r, r.vf[{}], r.vf[{}].w[3]); ", fs, ft);- u.post = "r.clip = uclip; fl.set_clip(uclip, fl.t); ";+ u.post = "r.clip = uclip; fl.set_clip(uclip, fl.now()); ";return u;}unsigned d = dest;@@ -423,7 +423,7 @@ Lower lower(std::uint32_t w, std::uint32_t pc) {case 0x08: c = std::format("set_vi(r, {}, r.vi[{}] + {}u); ", it, is, imm15); break;case 0x09: c = std::format("set_vi(r, {}, r.vi[{}] - {}u); ", it, is, imm15); break;case 0x10: c = std::format("set_vi(r, 1, (fl.clip() & 0xFFFFFFu) == {}u ? 1u : 0u); ", w & 0xFFFFFFu); break;- case 0x11: c = std::format("r.clip = {}u; fl.set_clip(r.clip, fl.t - 4u); ", w & 0xFFFFFFu); break;+ case 0x11: c = std::format("r.clip = {}u; fl.set_clip(r.clip, fl.now() - 4u); ", w & 0xFFFFFFu); break;case 0x12: c = std::format("set_vi(r, 1, (fl.clip() & {}u) ? 1u : 0u); ", w & 0xFFFFFFu); break;case 0x13: c = std::format("set_vi(r, 1, ((fl.clip() | {}u) & 0xFFFFFFu) == 0xFFFFFFu ? 1u : 0u); ", w & 0xFFFFFFu); break;case 0x14: c = std::format("set_vi(r, {}, (fl.status() & 0xFFFu) == {}u ? 1u : 0u); ", it, imm12); l.reads_flags = l.reads_status = true; break;@@ -488,7 +488,7 @@ std::string generate(const Program &p, std::string &name) {const std::vector<std::uint32_t> pcs = ps2::vu::reachable(p.code.data(), kMask, p.entry);std::set<std::uint32_t> in(pcs.begin(), pcs.end());std::map<std::uint32_t, Lower> lowers;- bool any_status = false, any_flags = false, any_q = false;+ bool any_status = false, any_flags = false, any_q = false, any_clip = false;for (std::uint32_t pc : pcs) {const std::uint32_t lo = word(p.code, pc), hi = word(p.code, pc + 4);Lower l;@@ -498,6 +498,7 @@ std::string generate(const Program &p, std::string &name) {any_status = any_status || l.reads_status;any_flags = any_flags || l.reads_flags;any_q = any_q || l.code.find("fl.end_q(") != std::string::npos;+ any_clip = any_clip || ps2::vu::reads_clip(lo, hi);lowers[pc] = l;}std::map<std::uint32_t, bool> live_in;@@ -595,6 +596,9 @@ std::string generate(const Program &p, std::string &name) {body += u.post;if (!(hi & 0x80000000u) && l.supported) body += l.after_upper;o += std::format("{}: {{ ++fl.t; ", label(pc));+ // waits count toward the clip flags only+ if (any_clip)+ if (const std::uint32_t wait = ps2::vu::stall(p.code.data(), kMask, pc)) o += std::format("fl.stalls += {}u; ", wait);if (any_q && ps2::vu::reads_q(hi)) {const ps2::vu::QRead kind = ps2::vu::q_read_kind(p.code.data(), kMask, pc);if (kind == ps2::vu::kQNew) o += "fl.flush_q(r); ";diff --git a/runtime/include/ps2/vu_ops.hpp b/runtime/include/ps2/vu_ops.hppindex 1da281e..bce419f 100644--- a/runtime/include/ps2/vu_ops.hpp+++ b/runtime/include/ps2/vu_ops.hpp@@ -206,6 +206,33 @@ inline std::uint32_t q_delay(const std::uint8_t *code, std::uint32_t mask, std::return latency;}+// Cycles the pair at pc waits for results of the pairs before it, in a+// straight line as q_delay assumes. A result is waited for at most three+// pairs later, but whether those pairs waited themselves depends on the ones+// before them, so the run starts eight pairs back.+inline std::uint32_t stall(const std::uint8_t *code, std::uint32_t mask, std::uint32_t pc) {+ std::uint32_t ready[32][4] = {}, vi_ready[16] = {};+ std::uint32_t cycle = 0;+ for (int k = -8; k <= 0; ++k) {+ const std::uint32_t at = (pc + static_cast<std::uint32_t>(k) * 8u) & mask;+ std::uint32_t lo, hi;+ std::memcpy(&lo, code + at, 4);+ std::memcpy(&hi, code + at + 4, 4);+ const VfUse u = vf_use(lo, hi);+ const std::uint32_t issued = issue(u, cycle + 1u, ready, vi_ready);+ if (k == 0) return issued - (cycle + 1u);+ cycle = issued;+ retire(u, cycle, ready, vi_ready);+ }+ return 0;+}++// FCEQ, FCAND, FCOR and FCGET read the clip flags+inline bool reads_clip(std::uint32_t lo, std::uint32_t hi) {+ const std::uint32_t op = lower_major(lo);+ return !(hi & 0x80000000u) && (op == 0x10 || op == 0x12 || op == 0x13 || op == 0x1C);+}+inline std::uint32_t q_unit_latency(std::uint32_t lo, std::uint32_t hi) {if ((hi & 0x80000000u) || lower_major(lo) != 0x40 || (lo & 0x3Cu) != 0x3Cu) return 0;const unsigned index = ((lo >> 6) & 31u) * 4u + (lo & 3u);@@ -459,6 +486,9 @@ PS2_VU_INLINE void clip(Vu0Regs &r, const Reg128 &s, std::uint32_t w) {struct FlagPipe {std::uint32_t t = 16;+ // Cycles spent waiting on results (stall()), which the clip flags count+ // and the slot counter t does not.+ std::uint32_t stalls = 0;std::uint32_t mac_ts[8] = {}, mac_v[8] = {}, status_v[8] = {}, clip_ts[8] = {}, clip_v[8] = {};unsigned mh = 0, ch = 0;std::uint32_t q_value = 0, q_ready = 0;@@ -515,10 +545,16 @@ struct FlagPipe {}PS2_VU_INLINE std::uint32_t mac() const { return mac_v[mac_slot()]; }PS2_VU_INLINE std::uint32_t status() const { return status_v[mac_slot()]; }+ PS2_VU_INLINE std::uint32_t now() const { return t + stalls; }+ // Clip flags reach FCAND/FCOR/FCEQ/FCGET four cycles after CLIP, stalls+ // included (clip_ts holds now()). God of War II reads them with an FCGET+ // three instructions after a pair of CLIPs, with an MTIR between that+ // waits one or two cycles for its operand; Metal Gear Solid 2 and 3 read+ // them four to six instructions after three CLIPs in a row, with no wait.PS2_VU_INLINE std::uint32_t clip() const {for (unsigned k = 1; k <= 8; ++k) {const unsigned i = (ch - k) & 7u;- if (clip_ts[i] + 3u <= t) return clip_v[i];+ if (clip_ts[i] + 4u <= now()) return clip_v[i];}return clip_v[(ch - 8) & 7u];}diff --git a/runtime/src/vu_jit.cpp b/runtime/src/vu_jit.cppindex d1f0248..9a55aac 100644--- a/runtime/src/vu_jit.cpp+++ b/runtime/src/vu_jit.cpp@@ -44,7 +44,7 @@ constexpr std::uint32_t kOffQ = offsetof(Vu0Regs, q), kOffP = offsetof(Vu0Regs,constexpr std::uint32_t kOffMac = offsetof(Vu0Regs, mac);constexpr std::uint32_t kOffMh = offsetof(vuops::FlagPipe, mh), kOffCh = offsetof(vuops::FlagPipe, ch), kOffMacTs = offsetof(vuops::FlagPipe, mac_ts),kOffMacV = offsetof(vuops::FlagPipe, mac_v), kOffStatusV = offsetof(vuops::FlagPipe, status_v), kOffClipTs = offsetof(vuops::FlagPipe, clip_ts),- kOffClipV = offsetof(vuops::FlagPipe, clip_v), kOffT = offsetof(vuops::FlagPipe, t), kOffQPending = offsetof(vuops::FlagPipe, q_pending),+ kOffClipV = offsetof(vuops::FlagPipe, clip_v), kOffT = offsetof(vuops::FlagPipe, t), kOffStalls = offsetof(vuops::FlagPipe, stalls), kOffQPending = offsetof(vuops::FlagPipe, q_pending),kOffQValue = offsetof(vuops::FlagPipe, q_value), kOffQReady = offsetof(vuops::FlagPipe, q_ready);constexpr std::uint32_t kOffStatus = offsetof(Vu0Regs, status), kOffClip = offsetof(Vu0Regs, clip), kOffTpc = offsetof(Vu0Regs, tpc);@@ -390,7 +390,7 @@ private:std::set<std::uint32_t> keep_bk_, set_bi_;int delay_mode_ = 0;unsigned delay_reg_ = 0;- bool any_status_ = false, any_flags_ = false;+ bool any_status_ = false, any_flags_ = false, any_clip_ = false;std::uint32_t word(std::uint32_t at) const {std::uint32_t v;@@ -470,6 +470,13 @@ private:c_.get(kT);c_.store(kOffT);}+ // FlagPipe::now(): the clip flags count waits too+ void now() {+ c_.get(kT);+ fl();+ c_.load(kOffStalls);+ c_.op(kOpAdd);+ }void br(std::uint32_t depth) {c_.byte(kOpBr);c_.u32(depth);@@ -1168,7 +1175,7 @@ void Translator::upper_post(const Up &u, bool flags_live) {c_.get(kR);c_.get(kUclip);c_.store(kOffClip);- ring_clip([&] { c_.get(kUclip); }, [&] { c_.get(kT); });+ ring_clip([&] { c_.get(kUclip); }, [&] { now(); });return;}if (!active(u, flags_live)) return;@@ -1557,7 +1564,7 @@ bool Translator::lower(std::uint32_t w, std::uint32_t pc) {c_.store(kOffClip);ring_clip([&] { c_.i32c(imm24); },[&] {- c_.get(kT);+ now();c_.i32c(4);c_.op(kOpSub);});@@ -1713,6 +1720,15 @@ void Translator::instruction(std::uint32_t pc, std::uint32_t following) {c_.i32c(1);c_.op(kOpAdd);c_.set(kT);+ if (any_clip_)+ if (const std::uint32_t wait = stall(code_, kMask, pc)) {+ fl();+ fl();+ c_.load(kOffStalls);+ c_.i32c(wait);+ c_.op(kOpAdd);+ c_.store(kOffStalls);+ }if (reads_q(hi)) {const QRead kind = q_read_kind(code_, kMask, pc);if (kind == kQNew) flush_q();@@ -1848,6 +1864,9 @@ void Translator::prologue() {fl();c_.i32c(0);c_.store8(kOffQPending);+ fl();+ c_.i32c(0);+ c_.store(kOffStalls);c_.i32c(16);c_.set(kT);for (unsigned i = 1; i < 16; ++i) {@@ -1877,6 +1896,7 @@ bool Translator::run(std::vector<std::uint8_t> &out) {if (!(word(pc + 4) & 0x80000000u)) l = analyze_lower(word(pc), pc);any_status_ = any_status_ || l.reads_status;any_flags_ = any_flags_ || l.reads_flags;+ any_clip_ = any_clip_ || reads_clip(word(pc), word(pc + 4));lowers_[pc] = l;}for (std::uint32_t pc : pcs_) live_in_[pc] = false;diff --git a/runtime/src/vu_micro.cpp b/runtime/src/vu_micro.cppindex 71ceebb..45c58b3 100644--- a/runtime/src/vu_micro.cpp+++ b/runtime/src/vu_micro.cpp@@ -55,7 +55,7 @@ struct QDelay {std::uint32_t generation = 0, value = 0;};-QDelay g_q_delay[2][2048], g_q_kind[2][2048];+QDelay g_q_delay[2][2048], g_q_kind[2][2048], g_stall[2][2048];void read_q(const Unit &u, vuops::FlagPipe &pipe, Vu0Regs &r, std::uint32_t pc, std::uint32_t hi) {if (!pipe.q_pending || !reads_q(hi)) return;@@ -79,6 +79,15 @@ std::uint32_t q_delay_of(const Unit &u, std::uint32_t pc, std::uint32_t latency)return d.value;}+std::uint32_t stall_of(const Unit &u, std::uint32_t pc) {+ QDelay &d = g_stall[u.n][(pc & u.code_mask) >> 3];+ if (d.generation != g_generation[u.n]) {+ d.generation = g_generation[u.n];+ d.value = stall(u.code, u.code_mask, pc);+ }+ return d.value;+}+std::uint64_t g_reported = 0;bool g_reference_dry = false;@@ -662,7 +671,7 @@ void lower(Unit &u, std::uint32_t w, std::uint32_t pc, std::uint32_t &npc, boolcase 0x10: store_vi(r, 1, (clip_flags() & 0xFFFFFFu) == (w & 0xFFFFFFu) ? 1u : 0u); break;case 0x11:r.clip = w & 0xFFFFFFu;- if (u.pipe) u.pipe->set_clip(r.clip, u.pipe->t - 4u);+ if (u.pipe) u.pipe->set_clip(r.clip, u.pipe->now() - 4u);break;case 0x12: store_vi(r, 1, (clip_flags() & w & 0xFFFFFFu) ? 1u : 0u); break;case 0x13: store_vi(r, 1, ((clip_flags() | w) & 0xFFFFFFu) == 0xFFFFFFu ? 1u : 0u); break;@@ -1026,6 +1035,7 @@ void run_fast(int n, std::uint32_t address) {pc = npc;npc = (npc + 8u) & u.code_mask;++pipe.t;+ pipe.stalls += stall_of(u, current);read_q(u, pipe, r, current, hi);fast_upper(r, d, out);std::uint16_t was[16];@@ -1058,7 +1068,7 @@ void run_fast(int n, std::uint32_t address) {}if (out.has_clip) {r.clip = out.clip;- pipe.set_clip(out.clip, pipe.t);+ pipe.set_clip(out.clip, pipe.now());}r.vf[0].d[0] = 0;r.vf[0].w[2] = 0;@@ -1096,6 +1106,7 @@ void run(int n, std::uint32_t address) {pc = npc;npc = (npc + 8u) & u.code_mask;++pipe.t;+ pipe.stalls += stall_of(u, current);read_q(u, pipe, r, current, hi);UpperOut out = upper(r, hi);bool branch = false;@@ -1120,7 +1131,7 @@ void run(int n, std::uint32_t address) {}if (out.has_clip) {r.clip = out.clip;- pipe.set_clip(out.clip, pipe.t);+ pipe.set_clip(out.clip, pipe.now());}r.vf[0].d[0] = 0;r.vf[0].w[2] = 0;diff --git a/scripts/test_vu_codegen.py b/scripts/test_vu_codegen.pyindex 20f23db..c002925 100644--- a/scripts/test_vu_codegen.py+++ b/scripts/test_vu_codegen.py@@ -30,7 +30,7 @@ int main(int argc, char **argv) {const auto branch = [](unsigned op, unsigned is, unsigned it, unsigned from, unsigned to) {return (op << 25) | (is << 11) | (it << 16) | (((to - from - 8u) / 8u) & 0x7FFu);};- for (int test = 0; test < 9; ++test) {+ for (int test = 0; test < 10; ++test) {Program p{static_cast<std::uint64_t>(test), 0, std::vector<std::uint8_t>(16384)};const auto put = [&](unsigned pc, std::uint32_t lo, std::uint32_t hi = unop) {std::memcpy(p.code.data() + pc, &lo, 4);@@ -55,6 +55,15 @@ int main(int argc, char **argv) {} else if (test == 8) {// FSSET clears the sticky flags after its upper instruction set them.put(0, 0x15u << 25, (0xFu << 21) | (2u << 16) | (1u << 11) | (3u << 6) | 0x28u | end);+ } else if (test == 9) {+ // Clip flags reach FCGET four cycles after CLIP, waits included: the+ // MTIR waits two cycles for the MFIR, so the FCGET three instructions+ // after the first CLIP sees both (God of War II's culling).+ const auto clip = [](unsigned fs, unsigned ft) { return (0xEu << 21) | (ft << 16) | (fs << 11) | (7u << 6) | 0x3Fu; };+ put(0, (0x40u << 25) | (8u << 21) | (5u << 16) | (15u << 6) | 0x3Du, clip(1, 3));+ put(8, nop, clip(1, 0));+ put(16, (0x40u << 25) | (4u << 16) | (5u << 11) | (15u << 6) | 0x3Cu);+ put(24, (0x1Cu << 25) | (2u << 16), unop | end);} else if (test >= 6) {// JR/JALR read the operand without the delay; JALR also overwrites it.put(0, addi(1, 0, 1));@@ -71,20 +80,26 @@ int main(int argc, char **argv) {std::ofstream(out / std::format("{}.wasm", test), std::ios::binary).write(reinterpret_cast<const char *>(wasm.data()), wasm.size());}native << "int main() {\n";- for (int test = 0; test < 9; ++test) {+ for (int test = 0; test < 10; ++test) {native << "{ Vu0Regs r{}; std::uint8_t mem[16384]{}; r.vi[1]=2; r.vi[3]=2; r.clip=1;""for (int i=0;i<4;++i) r.vf[1].w[i]=0xBF800000; r.vf[0].w[3]=0x3F800000;\n";if (test == 5) native << "r.vi[1]=0xFFFF;\n";+ if (test == 9) native << "r.vf[3].w[3]=0x3F000000;\n";native << std::format("vu1_{:016X}(r, mem);\n", test);- native << (test == 8 ? "if (r.mac != 0xF0 || r.status != 0x02) return 1;\n"+ native << (test == 9 ? "if (r.vi[2] != 0xA80) return 1;\n"+ : test == 8 ? "if (r.mac != 0xF0 || r.status != 0x02) return 1;\n": test >= 6 ? "if (r.tpc != 8) return 1;\n": test == 4 ? "if (r.mac != 0xF0 || r.status != 0x82) return 1;\n": "if (r.vi[2] != 2) return 1;\n");native << "}\n";}- native << "puts(\"9 native VU regressions passed\"); }\n";+ native << "puts(\"10 native VU regressions passed\"); }\n";std::ofstream offsets(out / "offsets.json");- offsets << std::format("{{\"vi\":{},\"vf\":{},\"clip\":{},\"mac\":{},\"status\":{},\"tpc\":{}}}", offsetof(ps2::Vu0Regs, vi), offsetof(ps2::Vu0Regs, vf), offsetof(ps2::Vu0Regs, clip), offsetof(ps2::Vu0Regs, mac), offsetof(ps2::Vu0Regs, status), offsetof(ps2::Vu0Regs, tpc));+ using ps2::vuops::FlagPipe;+ offsets << std::format("{{\"vi\":{},\"vf\":{},\"clip\":{},\"mac\":{},\"status\":{},\"tpc\":{},\"t\":{},\"stalls\":{},\"ch\":{},\"clip_ts\":{},\"clip_v\":{}}}",+ offsetof(ps2::Vu0Regs, vi), offsetof(ps2::Vu0Regs, vf), offsetof(ps2::Vu0Regs, clip), offsetof(ps2::Vu0Regs, mac),+ offsetof(ps2::Vu0Regs, status), offsetof(ps2::Vu0Regs, tpc), offsetof(FlagPipe, t), offsetof(FlagPipe, stalls),+ offsetof(FlagPipe, ch), offsetof(FlagPipe, clip_ts), offsetof(FlagPipe, clip_v));}'''@@ -93,7 +108,7 @@ const fs = require('node:fs');const path = require('node:path');const dir = process.argv[2];const off = JSON.parse(fs.readFileSync(path.join(dir, 'offsets.json')));-for (let test = 0; test < 9; ++test) {+for (let test = 0; test < 10; ++test) {const memory = new WebAssembly.Memory({initial: 1, maximum: 65536, shared: true});const view = new DataView(memory.buffer);const r = 0x3000;@@ -102,17 +117,29 @@ for (let test = 0; test < 9; ++test) {view.setUint32(r + off.clip, 1, true);view.setUint32(r + off.vf + 12, 0x3F800000, true);for (let i = 0; i < 4; ++i) view.setUint32(r + off.vf + 16 + 4*i, 0xBF800000, true);+ if (test === 9) view.setUint32(r + off.vf + 3*16 + 12, 0x3F000000, true);const mod = new WebAssembly.Module(fs.readFileSync(path.join(dir, `${test}.wasm`)));const env = {memory};for (const imp of WebAssembly.Module.imports(mod)) {if (imp.kind !== 'function') continue;env[imp.name] = () => { throw Error(`Unexpected import: ${imp.name}`); };}- env.clip = () => 1;+ // FlagPipe::clip(): the latest clip flags at least four cycles old+ env.clip = (fl) => {+ const now = view.getUint32(fl + off.t, true) + view.getUint32(fl + off.stalls, true);+ const ch = view.getUint32(fl + off.ch, true);+ for (let k = 1; k <= 8; ++k) {+ const i = (ch - k) & 7;+ if (view.getUint32(fl + off.clip_ts + 4*i, true) + 4 <= now) return view.getUint32(fl + off.clip_v + 4*i, true);+ }+ return view.getUint32(fl + off.clip_v + 4*((ch - 8) & 7), true);+ };env.status = () => 0;env.force_mac = () => {};new WebAssembly.Instance(mod, {env}).exports.run(r, 0x4000);- if (test === 8) {+ if (test === 9) {+ if (view.getUint16(r + off.vi + 4, true) !== 0xA80) throw Error('Clip flags ignored the wait before FCGET');+ } else if (test === 8) {if (view.getUint32(r + off.mac, true) !== 0xF0 || view.getUint32(r + off.status, true) !== 0x02)throw Error('FSSET was overwritten by its upper instruction');} else if (test >= 6) {@@ -124,7 +151,7 @@ for (let test = 0; test < 9; ++test) {throw Error(`Branch VI regression ${test}`);}}-console.log('9 Wasm VU regressions passed');+console.log('10 Wasm VU regressions passed');'''