Skip to content

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

C++

The branch VI delay holds for conditional branches only, not JR and JALR

Samir Nuri18ad888d1af0
diff --git a/recomp/vurecomp.cpp b/recomp/vurecomp.cpp
index 6662fc5..e8b8840 100644
--- a/recomp/vurecomp.cpp
+++ b/recomp/vurecomp.cpp
@@ -550,7 +550,7 @@ std::string generate(const Program &p, std::string &name) {
if (written > 0) body += std::format("next_bk = r.vi[{}]; next_bi = {}; ", written, written);
body += u.pre;
std::string lcode = l.code;
- if (l.branch || l.jump) {
+ if (l.branch) {
for (int reg = 1; reg < 16; ++reg)
replace_all(lcode, std::format("r.vi[{}]", reg), std::format("(bi == {} ? bk : r.vi[{}])", reg, reg));
}
diff --git a/runtime/include/ps2/vu_ops.hpp b/runtime/include/ps2/vu_ops.hpp
index f2a1c51..da92434 100644
--- a/runtime/include/ps2/vu_ops.hpp
+++ b/runtime/include/ps2/vu_ops.hpp
@@ -32,7 +32,7 @@ inline std::uint32_t lower_major(std::uint32_t lo) { return lo >> 25; }
// Flag reads bypass the one-instruction VI delay seen by branch operands.
inline bool branch_delay_exempt(std::uint32_t lo) {
switch (lower_major(lo)) {
- case 0x10: case 0x12: case 0x13: case 0x14: case 0x16: case 0x17: case 0x18: case 0x1A: case 0x1B: case 0x1C: return true;
+ case 0x10: case 0x12: case 0x13: case 0x14: case 0x16: case 0x17: case 0x18: case 0x1A: case 0x1B: case 0x1C: case 0x21: case 0x25: return true;
default: return false;
}
}
diff --git a/runtime/src/vu_jit.cpp b/runtime/src/vu_jit.cpp
index 47626b2..22228d8 100644
--- a/runtime/src/vu_jit.cpp
+++ b/runtime/src/vu_jit.cpp
@@ -443,18 +443,17 @@ private:
}
switch (w >> 25) {
case 0x04: return dest ? it : 0;
- case 0x08: case 0x09: case 0x21: case 0x25: return it;
+ case 0x08: case 0x09: return it;
default: return 0;
}
}
std::uint32_t branch_reads(std::uint32_t pc) {
- const LowerInfo &l = lowers_[pc];
- if (!l.branch && !l.jump) return 0;
+ if (!lowers_[pc].branch) return 0;
const std::uint32_t w = word(pc);
const unsigned it = (w >> 16) & 15u, is = (w >> 11) & 15u;
switch (w >> 25) {
- case 0x24: case 0x25: case 0x2C: case 0x2D: case 0x2E: case 0x2F: return (1u << is) & ~1u;
+ case 0x2C: case 0x2D: case 0x2E: case 0x2F: return (1u << is) & ~1u;
case 0x28: case 0x29: return ((1u << is) | (1u << it)) & ~1u;
default: return 0;
}
@@ -1666,7 +1665,7 @@ bool Translator::lower(std::uint32_t w, std::uint32_t pc) {
break;
case 0x24:
case 0x25:
- branch_vi(is);
+ vi(is);
c_.i32c(8);
c_.op(kOpMul);
c_.i32c(kMask);
diff --git a/runtime/src/vu_micro.cpp b/runtime/src/vu_micro.cpp
index fd27727..fb1a090 100644
--- a/runtime/src/vu_micro.cpp
+++ b/runtime/src/vu_micro.cpp
@@ -671,8 +671,8 @@ void lower(Unit &u, std::uint32_t w, std::uint32_t pc, std::uint32_t &npc, bool
case 0x1C: store_vi(r, it, clip_flags() & 0xFFFu); break;
case 0x20: branch_to(target); break;
case 0x21: store_vi(r, it, (pc + 16u) / 8u); branch_to(target); break;
- case 0x24: branch_to(static_cast<std::uint32_t>(bvi(is)) * 8u); break;
- case 0x25: { const std::uint32_t t = static_cast<std::uint32_t>(bvi(is)) * 8u; store_vi(r, it, (pc + 16u) / 8u); branch_to(t); break; }
+ case 0x24: branch_to(static_cast<std::uint32_t>(r.vi[is]) * 8u); break;
+ case 0x25: { const std::uint32_t t = static_cast<std::uint32_t>(r.vi[is]) * 8u; store_vi(r, it, (pc + 16u) / 8u); branch_to(t); break; }
case 0x28: if (bvi(is) == bvi(it)) branch_to(target); break;
case 0x29: if (bvi(is) != bvi(it)) branch_to(target); break;
case 0x2C: if (static_cast<std::int16_t>(bvi(is)) < 0) branch_to(target); break;
diff --git a/scripts/test_vu_codegen.py b/scripts/test_vu_codegen.py
index eb20f95..e07f8c8 100644
--- a/scripts/test_vu_codegen.py
+++ b/scripts/test_vu_codegen.py
@@ -53,7 +53,7 @@ int main(int argc, char **argv) {
put(56, addi(2, 0, 1), unop | end);
put(72, addi(2, 0, 2), unop | end);
} else if (test >= 6) {
- // JR/JALR use the saved operand; JALR may also overwrite it.
+ // JR/JALR read the operand without the delay; JALR also overwrites it.
put(0, addi(1, 0, 1));
put(8, ((test == 6 ? 0x24u : 0x25u) << 25) | (1u << 11) | (1u << 16), unop | end);
} else {
@@ -72,7 +72,7 @@ int main(int argc, char **argv) {
"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";
native << std::format("vu1_{:016X}(r, mem);\n", test);
- native << (test >= 6 ? "if (r.tpc != 16) return 1;\n" : test == 4 ? "if (r.mac != 0xF0 || r.status != 0x82) return 1;\n" : "if (r.vi[2] != 2) return 1;\n");
+ native << (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(\"8 native VU regressions passed\"); }\n";
@@ -104,7 +104,7 @@ for (let test = 0; test < 8; ++test) {
env.clip = () => 1;
new WebAssembly.Instance(mod, {env}).exports.run(r, 0x4000);
if (test >= 6) {
- if (view.getUint32(r + off.tpc, true) !== 16) throw Error(`Register jump regression ${test}`);
+ if (view.getUint32(r + off.tpc, true) !== 8) throw Error(`Register jump regression ${test}`);
} else if (test === 4) {
if (view.getUint32(r + off.mac, true) !== 0xF0 || view.getUint32(r + off.status, true) !== 0x82)
throw Error('Exit flags were dropped');