Skip to content

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

C++

vu: FMAC operations targeting VF00 still set the flags

Samir Nuri430f3f0d28f8
diff --git a/recomp/vurecomp.cpp b/recomp/vurecomp.cpp
index 93ae024..77dfe8f 100644
--- a/recomp/vurecomp.cpp
+++ b/recomp/vurecomp.cpp
@@ -128,7 +128,7 @@ Upper upper(std::uint32_t hi, bool flags_live) {
}
unsigned d = dest;
if (kind == Opmula || kind == Opmsub) d = 0xE;
- if (target == 0 || d == 0) return u;
+ if (d == 0 || (target == 0 && !(flags && flags_live))) return u;
const auto b = [&](int i) -> std::string {
switch (src) {
case Vec: return vf(ft, i);
@@ -160,6 +160,11 @@ Upper upper(std::uint32_t hi, bool flags_live) {
default: e = std::format("vabs({})", vs); break;
}
u.pre += std::format("const v4u uv = {}; ", e);
+ if (target == 0) {
+ u.post = std::format("{{ Reg128 uf; vstore(uf, uv); flags(r, uf, {}u); fl.set_mac(r.mac, r.status); }} ", d);
+ u.sets_flags = true;
+ return u;
+ }
if (d == 0xF) {
u.post += std::format("vstore({}, uv); ", dst);
} else {
@@ -200,6 +205,14 @@ Upper upper(std::uint32_t hi, bool flags_live) {
}
u.pre += std::format("const std::uint32_t u{} = {}; ", kLane[i], e);
}
+ if (target == 0) {
+ u.post = "{ Reg128 uf{}; ";
+ for (int i = 0; i < 4; ++i)
+ if (lane(d, i)) u.post += std::format("uf.w[{}] = u{}; ", i, kLane[i]);
+ u.post += std::format("flags(r, uf, {}u); fl.set_mac(r.mac, r.status); }} ", d);
+ u.sets_flags = true;
+ return u;
+ }
for (int i = 0; i < 4; ++i)
if (lane(d, i)) u.post += std::format("{}.w[{}] = u{}; ", dst, i, kLane[i]);
u.sets_flags = flags;
@@ -377,7 +390,7 @@ Lower lower(std::uint32_t w, std::uint32_t pc) {
return l;
}
-bool upper_sets_flags(std::uint32_t hi) { return upper(hi, false).sets_flags; }
+bool upper_sets_flags(std::uint32_t hi) { return upper(hi, true).sets_flags; }
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);
diff --git a/runtime/src/vu_micro.cpp b/runtime/src/vu_micro.cpp
index 248edf0..368344a 100644
--- a/runtime/src/vu_micro.cpp
+++ b/runtime/src/vu_micro.cpp
@@ -374,10 +374,11 @@ void scan_flags(int unit, const std::uint8_t *code, std::uint32_t size) {
void commit(Vu0Regs &r, const UpperOut &o) {
if (o.target < 0) return;
- Reg128 &dst = o.target == kAcc ? r.acc : r.vf[o.target];
- if (o.target == 0) return;
- for (int i = 0; i < 4; ++i)
- if (o.dest & (8u >> i)) dst.w[i] = o.value.w[i];
+ if (o.target > 0) {
+ Reg128 &dst = o.target == kAcc ? r.acc : r.vf[o.target];
+ for (int i = 0; i < 4; ++i)
+ if (o.dest & (8u >> i)) dst.w[i] = o.value.w[i];
+ }
if (o.flags) set_flags(r, o.value, o.dest);
}
@@ -740,10 +741,10 @@ void run_fast(int n, std::uint32_t address) {
Reg128 &dst = out.target == kAcc ? r.acc : r.vf[out.target];
for (int i = 0; i < 4; ++i)
if (out.dest & (8u >> i)) dst.w[i] = out.value.w[i];
- if (out.flags && flags) {
- set_flags(r, out.value, out.dest);
- pipe.set_mac(r.mac, r.status);
- }
+ }
+ if (out.target >= 0 && out.flags && flags) {
+ set_flags(r, out.value, out.dest);
+ pipe.set_mac(r.mac, r.status);
}
if (out.has_clip) {
r.clip = out.clip;
@@ -793,7 +794,7 @@ void run(int n, std::uint32_t address) {
if (branch) npc = target;
}
commit(r, out);
- if (out.target > 0 && out.flags) pipe.set_mac(r.mac, r.status);
+ if (out.target >= 0 && out.flags) pipe.set_mac(r.mac, r.status);
if (out.has_clip) {
r.clip = out.clip;
pipe.set_clip(out.clip, pipe.t);