Skip to content

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

C++

FSSET takes effect after the upper instruction of its pair

reikopb7371e86bee5
diff --git a/recomp/vurecomp.cpp b/recomp/vurecomp.cpp
index a68401f..998ecbd 100644
--- a/recomp/vurecomp.cpp
+++ b/recomp/vurecomp.cpp
@@ -282,6 +282,8 @@ std::string lanes_store(unsigned dest, const std::string &qword_expr, const std:
struct Lower {
std::string code;
+ // Runs after the upper instruction of the pair has committed its flags (FSSET)
+ std::string after_upper;
int writes = -1;
bool reads_flags = false, reads_status = false;
bool branch = false, jump = false;
@@ -425,7 +427,7 @@ Lower lower(std::uint32_t w, std::uint32_t pc) {
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;
- case 0x15: c = std::format("r.status = (fl.status() & 0x3Fu) | ({}u & 0xFC0u); fl.force_mac(r.mac, r.status); ", imm12); l.reads_flags = l.reads_status = true; break;
+ case 0x15: l.after_upper = std::format("r.status = (r.status & 0x3Fu) | {}u; fl.force_mac(r.mac, r.status); ", imm12 & 0xFC0u); l.reads_flags = l.reads_status = true; break;
case 0x16: c = std::format("set_vi(r, {}, fl.status() & {}u); ", it, imm12); l.reads_flags = l.reads_status = true; break;
case 0x17: c = std::format("set_vi(r, {}, ((fl.status() | {}u) & 0xFFFu) == 0xFFFu ? 1u : 0u); ", it, imm12); l.reads_flags = l.reads_status = true; break;
case 0x18: c = std::format("set_vi(r, {}, (fl.mac() & 0xFFFFu) == r.vi[{}] ? 1u : 0u); ", it, is); l.reads_flags = true; break;
@@ -591,6 +593,7 @@ std::string generate(const Program &p, std::string &name) {
if (keep_bk.count(pc)) body += "bk = next_bk; ";
if (set_bi.count(pc)) body += std::format("bi = {}u; ", std::max(def[pc], 0));
body += u.post;
+ if (!(hi & 0x80000000u) && l.supported) body += l.after_upper;
o += std::format("{}: {{ ++fl.t; ", label(pc));
if (any_q && ps2::vu::reads_q(hi)) {
const ps2::vu::QRead kind = ps2::vu::q_read_kind(p.code.data(), kMask, pc);
diff --git a/runtime/src/vu_jit.cpp b/runtime/src/vu_jit.cpp
index b447891..d1f0248 100644
--- a/runtime/src/vu_jit.cpp
+++ b/runtime/src/vu_jit.cpp
@@ -921,6 +921,7 @@ private:
void upper_pre(const Up &u, bool flags_live);
void upper_post(const Up &u, bool flags_live);
bool lower(std::uint32_t w, std::uint32_t pc);
+ void fsset(std::uint32_t w);
void instruction(std::uint32_t pc, std::uint32_t following);
void prologue();
};
@@ -1590,18 +1591,7 @@ bool Translator::lower(std::uint32_t w, std::uint32_t pc) {
c_.op(kOpEq);
});
break;
- case 0x15:
- c_.get(kR);
- status();
- c_.i32c(0x3Fu);
- c_.op(kOpAnd);
- c_.i32c(imm12 & 0xFC0u);
- c_.op(kOpOr);
- c_.store(kOffStatus);
- fl();
- c_.get(kR);
- c_.call(kForceMac);
- break;
+ case 0x15: break; // fsset(), after the upper instruction
case 0x16:
set_vi(it, [&] {
status();
@@ -1696,6 +1686,23 @@ bool Translator::lower(std::uint32_t w, std::uint32_t pc) {
return true;
}
+// FSSET waits for the upper instruction of its pair, whose status it replaces
+// the sticky flags of (as in the interpreter)
+void Translator::fsset(std::uint32_t w) {
+ const std::uint32_t imm12 = ((w >> 10) & 0x800u) | (w & 0x7FFu);
+ c_.get(kR);
+ c_.get(kR);
+ c_.load(kOffStatus);
+ c_.i32c(0x3Fu);
+ c_.op(kOpAnd);
+ c_.i32c(imm12 & 0xFC0u);
+ c_.op(kOpOr);
+ c_.store(kOffStatus);
+ fl();
+ c_.get(kR);
+ c_.call(kForceMac);
+}
+
void Translator::instruction(std::uint32_t pc, std::uint32_t following) {
const std::uint32_t lo = word(pc), hi = word(pc + 4);
const Up u = decode_upper(hi);
@@ -1720,12 +1727,15 @@ void Translator::instruction(std::uint32_t pc, std::uint32_t following) {
const auto delay = delay_.find(pc);
delay_mode_ = delay == delay_.end() ? 0 : delay->second;
delay_reg_ = delay_mode_ == 1 ? vi_def_[(pc - 8u) & kMask] : 0;
+ bool fsset_after = false;
if (hi & 0x80000000u) {
c_.get(kR);
c_.i32c(lo);
c_.store(kOffI);
} else if (!li.supported || !lower(lo, pc)) {
leave(pc);
+ } else {
+ fsset_after = (lo >> 25) == 0x15;
}
delay_mode_ = 0;
if (keep_bk_.count(pc)) {
@@ -1737,6 +1747,7 @@ void Translator::instruction(std::uint32_t pc, std::uint32_t following) {
c_.set(kBi);
}
upper_post(u, flags_live);
+ if (fsset_after) fsset(lo);
const int written = lower_write(pc);
const std::uint64_t live = shadow_out_[pc >> 3];
if (written >= 0 && shadow_.count(written) && ((live >> written) & 1u)) {
diff --git a/runtime/src/vu_micro.cpp b/runtime/src/vu_micro.cpp
index fb1a090..967f158 100644
--- a/runtime/src/vu_micro.cpp
+++ b/runtime/src/vu_micro.cpp
@@ -37,6 +37,10 @@ struct Unit {
// What a branch reads of the integer registers: the values from before the
// previous instruction when that one wrote them (see BranchView).
const std::uint16_t *branch_vi = nullptr;
+ // FSSET waits for the upper instruction of its pair: the sticky flags it
+ // sets replace those of the status that instruction writes (MGS3 clears
+ // them next to an OPMSUB before testing its collision triangles)
+ std::int32_t fsset = -1;
};
Vu0Regs g_vu1;
@@ -660,8 +664,11 @@ void lower(Unit &u, std::uint32_t w, std::uint32_t pc, std::uint32_t &npc, bool
case 0x13: store_vi(r, 1, ((clip_flags() | w) & 0xFFFFFFu) == 0xFFFFFFu ? 1u : 0u); break;
case 0x14: store_vi(r, it, (status_flags() & 0xFFFu) == ((((w >> 10) & 0x800u) | (w & 0x7FFu))) ? 1u : 0u); break;
case 0x15:
+ if (u.pipe) {
+ u.fsset = static_cast<std::int32_t>((((w >> 10) & 0x800u) | (w & 0x7FFu)) & 0xFC0u);
+ break;
+ }
r.status = (status_flags() & 0x3Fu) | ((((w >> 10) & 0x800u) | (w & 0x7FFu)) & 0xFC0u);
- if (u.pipe) u.pipe->force_mac(r.mac, r.status);
break;
case 0x16: store_vi(r, it, status_flags() & (((w >> 10) & 0x800u) | (w & 0x7FFu))); break;
case 0x17: store_vi(r, it, ((status_flags() | (((w >> 10) & 0x800u) | (w & 0x7FFu))) & 0xFFFu) == 0xFFFu ? 1u : 0u); break;
@@ -1039,6 +1046,11 @@ void run_fast(int n, std::uint32_t address) {
set_flags(r, out.value, out.dest);
pipe.set_mac(r.mac, r.status);
}
+ if (u.fsset >= 0) {
+ r.status = (r.status & 0x3Fu) | static_cast<std::uint32_t>(u.fsset);
+ pipe.force_mac(r.mac, r.status);
+ u.fsset = -1;
+ }
if (out.has_clip) {
r.clip = out.clip;
pipe.set_clip(out.clip, pipe.t);
@@ -1096,6 +1108,11 @@ void run(int n, std::uint32_t address) {
view.commit(was, lo, !(hi & 0x80000000u));
commit(r, out);
if (out.target >= 0 && out.flags) pipe.set_mac(r.mac, r.status);
+ if (u.fsset >= 0) {
+ r.status = (r.status & 0x3Fu) | static_cast<std::uint32_t>(u.fsset);
+ pipe.force_mac(r.mac, r.status);
+ u.fsset = -1;
+ }
if (out.has_clip) {
r.clip = out.clip;
pipe.set_clip(out.clip, pipe.t);
diff --git a/scripts/test_vu_codegen.py b/scripts/test_vu_codegen.py
index 3cca261..20f23db 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 < 8; ++test) {
+ for (int test = 0; test < 9; ++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);
@@ -52,6 +52,9 @@ int main(int argc, char **argv) {
put(40, branch(0x28, 1, 3, 40, 72));
put(56, addi(2, 0, 1), unop | end);
put(72, addi(2, 0, 2), unop | end);
+ } 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 >= 6) {
// JR/JALR read the operand without the delay; JALR also overwrites it.
put(0, addi(1, 0, 1));
@@ -68,15 +71,18 @@ 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 < 8; ++test) {
+ for (int test = 0; test < 9; ++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";
native << std::format("vu1_{:016X}(r, mem);\n", test);
- 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 << (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(\"8 native VU regressions passed\"); }\n";
+ native << "puts(\"9 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));
}
@@ -87,7 +93,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 < 8; ++test) {
+for (let test = 0; test < 9; ++test) {
const memory = new WebAssembly.Memory({initial: 1, maximum: 65536, shared: true});
const view = new DataView(memory.buffer);
const r = 0x3000;
@@ -104,8 +110,12 @@ for (let test = 0; test < 8; ++test) {
}
env.clip = () => 1;
env.status = () => 0;
+ env.force_mac = () => {};
new WebAssembly.Instance(mod, {env}).exports.run(r, 0x4000);
- if (test >= 6) {
+ 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) {
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)
@@ -114,7 +124,7 @@ for (let test = 0; test < 8; ++test) {
throw Error(`Branch VI regression ${test}`);
}
}
-console.log('8 Wasm VU regressions passed');
+console.log('9 Wasm VU regressions passed');
'''