Skip to content

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

C++

VU: delay Q by the divider latency, counting pipeline stalls

Samir Nurie045476498e3
diff --git a/recomp/vurecomp.cpp b/recomp/vurecomp.cpp
index 6c651ee..d96cc5f 100644
--- a/recomp/vurecomp.cpp
+++ b/recomp/vurecomp.cpp
@@ -341,10 +341,10 @@ Lower lower(std::uint32_t w, std::uint32_t pc) {
c = std::format("{{ set_vi(r, {}, r.vi[{}] - 1u); std::uint8_t *q = qword(mem, r.vi[{}]); ", it, it, it) +
lanes_store(dest, "q", std::format("r.vf[{}]", fs)) + "} ";
break;
- case 0x38: c = std::format("vu::div(r, r.vf[{}].w[{}], r.vf[{}].w[{}]); ", fs, fsf, ft, ftf); break;
- case 0x39: c = std::format("vu::sqrt(r, r.vf[{}].w[{}]); ", ft, ftf); break;
- case 0x3A: c = std::format("vu::rsqrt(r, r.vf[{}].w[{}], r.vf[{}].w[{}]); ", fs, fsf, ft, ftf); break;
- case 0x3B: break;
+ case 0x38: c = std::format("{{ const std::uint32_t qb = fl.begin_q(r); vu::div(r, r.vf[{}].w[{}], r.vf[{}].w[{}]); fl.end_q(r, qb, 7u); }} ", fs, fsf, ft, ftf); break;
+ case 0x39: c = std::format("{{ const std::uint32_t qb = fl.begin_q(r); vu::sqrt(r, r.vf[{}].w[{}]); fl.end_q(r, qb, 7u); }} ", ft, ftf); break;
+ case 0x3A: c = std::format("{{ const std::uint32_t qb = fl.begin_q(r); vu::rsqrt(r, r.vf[{}].w[{}], r.vf[{}].w[{}]); fl.end_q(r, qb, 13u); }} ", fs, fsf, ft, ftf); break;
+ case 0x3B: c = "fl.flush_q(r); "; break;
case 0x3C: c = std::format("set_vi(r, {}, r.vf[{}].w[{}]); ", it, fs, fsf); break;
case 0x3D:
if (ft) l.writes = static_cast<int>(ft);
@@ -477,13 +477,16 @@ 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;
+ bool any_status = false, any_flags = false, any_q = false;
for (std::uint32_t pc : pcs) {
const std::uint32_t lo = word(p.code, pc), hi = word(p.code, pc + 4);
Lower l;
if (!(hi & 0x80000000u)) l = lower(lo, pc);
+ for (const std::uint32_t latency : {7u, 13u})
+ replace_all(l.code, std::format("fl.end_q(r, qb, {}u)", latency), std::format("fl.end_q(r, qb, {}u)", ps2::vu::q_delay(p.code.data(), kMask, pc, latency)));
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;
lowers[pc] = l;
}
std::map<std::uint32_t, bool> live_in;
@@ -533,12 +536,18 @@ std::string generate(const Program &p, std::string &name) {
for (std::int64_t s : successors(pc)) out_live = out_live || s < 0 || !in.count(static_cast<std::uint32_t>(s)) || live_in[static_cast<std::uint32_t>(s)];
const Upper u = upper(hi, any_status ? true : (any_flags && out_live));
const Lower &l = lowers[pc];
+ std::string body = u.pre;
+ if (hi & 0x80000000u) body += std::format("r.i = {}u; ", lo);
+ else if (!l.supported) body += std::format("vu::interpret(1, {}u); return; ", pc);
+ else body += l.code;
+ body += u.post;
o += std::format("{}: {{ ++fl.t; ", label(pc));
- o += u.pre;
- if (hi & 0x80000000u) o += std::format("r.i = {}u; ", lo);
- else if (!l.supported) o += std::format("vu::interpret(1, {}u); return; ", pc);
- else o += l.code;
- o += u.post;
+ 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); ";
+ else if (kind == ps2::vu::kQDynamic) o += "fl.sync_q(r); ";
+ }
+ o += body;
const int lower_write = (hi & 0x80000000u) || !l.supported ? -1 : l.writes;
if (lower_write >= 0 && shadowed.count(lower_write)) o += std::format("{} = vfix(vload({})); ", shadow(lower_write), raw(lower_write));
if (u.target >= 0 && u.target != lower_write && shadowed.count(u.target)) {
@@ -564,6 +573,10 @@ std::string generate(const Program &p, std::string &name) {
if (!in.count(next)) o += std::format(" vu::interpret(1, {}u);\n return;\n", next);
}
o += "dispatch:\n vu::interpret(1, jpc);\n return;\n}\n\n";
+ if (any_q) {
+ replace_all(o, "vu::interpret(", "fl.flush_q(r); vu::interpret(");
+ replace_all(o, "r.tpc = ", "fl.flush_q(r); r.tpc = ");
+ }
return local_vi(o);
}
diff --git a/runtime/include/ps2/vu_ops.hpp b/runtime/include/ps2/vu_ops.hpp
index c6fabf1..a088624 100644
--- a/runtime/include/ps2/vu_ops.hpp
+++ b/runtime/include/ps2/vu_ops.hpp
@@ -71,6 +71,226 @@ inline std::vector<std::uint32_t> reachable(const std::uint8_t *code, std::uint3
return sorted;
}
+struct VfUse {
+ std::uint8_t reg[4] = {}, mask[4] = {}, wreg[2] = {}, wmask[2] = {};
+ unsigned n = 0, wn = 0;
+ std::uint16_t vi_reads = 0;
+ unsigned vi_load = 0;
+ void read_vi(unsigned r) { vi_reads = static_cast<std::uint16_t>(vi_reads | ((1u << (r & 15u)) & ~1u)); }
+ void read(unsigned r, unsigned m) {
+ if (r && m) {
+ reg[n] = static_cast<std::uint8_t>(r);
+ mask[n++] = static_cast<std::uint8_t>(m);
+ }
+ }
+ void write(unsigned r, unsigned m) {
+ if (r && m) {
+ wreg[wn] = static_cast<std::uint8_t>(r);
+ wmask[wn++] = static_cast<std::uint8_t>(m);
+ }
+ }
+};
+
+inline VfUse vf_use(std::uint32_t lo, std::uint32_t hi) {
+ VfUse u;
+ const unsigned dest = (hi >> 21) & 15u, ft = (hi >> 16) & 31u, fs = (hi >> 11) & 31u, fd = (hi >> 6) & 31u, bc = 8u >> (hi & 3u), funct = hi & 63u;
+ if (funct < 0x3C) {
+ if (funct < 0x1C) {
+ u.read(fs, dest);
+ u.read(ft, bc);
+ } else if (funct < 0x28) {
+ u.read(fs, dest);
+ } else if (funct == 0x2E) {
+ u.read(fs, 0xE);
+ u.read(ft, 0xE);
+ } else {
+ u.read(fs, dest);
+ u.read(ft, dest);
+ }
+ u.write(fd, funct == 0x2E ? 0xE : dest);
+ } else {
+ const unsigned index = ((hi >> 6) & 31u) * 4u + (funct & 3u);
+ if (index < 0x10 || (index >= 0x18 && index < 0x1C)) {
+ u.read(fs, dest);
+ u.read(ft, bc);
+ } else if (index < 0x18 || index == 0x1D) {
+ u.read(fs, dest);
+ u.write(ft, dest);
+ } else if (index == 0x1F) {
+ u.read(fs, 0xE);
+ u.read(ft, 1);
+ } else if (index < 0x28) {
+ u.read(fs, dest);
+ } else if (index == 0x2E) {
+ u.read(fs, 0xE);
+ u.read(ft, 0xE);
+ } else if (index < 0x2F) {
+ u.read(fs, dest);
+ u.read(ft, dest);
+ }
+ }
+ if (hi & 0x80000000u) return u;
+ const unsigned ldest = (lo >> 21) & 15u, lft = (lo >> 16) & 31u, lfs = (lo >> 11) & 31u, fsf = 8u >> ((lo >> 21) & 3u), ftf = 8u >> ((lo >> 23) & 3u);
+ const unsigned it = (lo >> 16) & 15u, is = (lo >> 11) & 15u, major = lower_major(lo);
+ if (major == 0x00 || major == 0x08 || major == 0x09 || major == 0x18 || major == 0x1A || major == 0x1B || major == 0x24 || major == 0x25 || (major >= 0x2C && major <= 0x2F)) u.read_vi(is);
+ if (major == 0x01) u.read_vi(it);
+ if (major == 0x04 || major == 0x05 || major == 0x28 || major == 0x29) u.read_vi(is);
+ if (major == 0x05 || major == 0x28 || major == 0x29) u.read_vi(it);
+ if (major == 0x04) u.vi_load = it;
+ if (major == 0x40 && (lo & 0x3Cu) != 0x3Cu) {
+ const unsigned funct = lo & 0x3Fu;
+ if (funct == 0x30 || funct == 0x31 || funct == 0x32 || funct == 0x34 || funct == 0x35) u.read_vi(is);
+ if (funct == 0x30 || funct == 0x31 || funct == 0x34 || funct == 0x35) u.read_vi(it);
+ }
+ if (major == 0x40 && (lo & 0x3Cu) == 0x3Cu) {
+ const unsigned index = ((lo >> 6) & 31u) * 4u + (lo & 3u);
+ if (index == 0x34 || index == 0x36 || index == 0x3D || index == 0x3E || index == 0x3F || index == 0x6C) u.read_vi(is);
+ if (index == 0x35 || index == 0x37 || index == 0x3F) u.read_vi(it);
+ if (index == 0x3E) u.vi_load = it;
+ }
+ if (major == 0x00) u.write(lft, ldest);
+ else if (major == 0x01) u.read(lfs, ldest);
+ else if (major == 0x40 && (lo & 0x3Cu) == 0x3Cu) {
+ const unsigned index = ((lo >> 6) & 31u) * 4u + (lo & 3u);
+ switch (index) {
+ case 0x30: u.read(lfs, ldest); u.write(lft, ldest); break;
+ case 0x31: u.read(lfs, ((ldest >> 1) | (ldest << 3)) & 15u); u.write(lft, ldest); break;
+ case 0x34: case 0x36: case 0x3D: case 0x40: case 0x41: case 0x64: u.write(lft, ldest); break;
+ case 0x35: case 0x37: u.read(lfs, ldest); break;
+ case 0x38: case 0x3A: u.read(lfs, fsf); u.read(lft, ftf); break;
+ case 0x39: u.read(lft, ftf); break;
+ case 0x3C: case 0x42: case 0x43: u.read(lfs, fsf); break;
+ default: if (index >= 0x70) u.read(lfs, 0xF); break;
+ }
+ }
+ return u;
+}
+
+inline std::uint32_t issue(const VfUse &u, std::uint32_t cycle, const std::uint32_t (&ready)[32][4], const std::uint32_t (&vi_ready)[16]) {
+ for (unsigned i = 0; i < u.n; ++i)
+ for (unsigned lane = 0; lane < 4; ++lane)
+ if (u.mask[i] & (8u >> lane)) cycle = std::max(cycle, ready[u.reg[i]][lane]);
+ for (unsigned r = 1; r < 16; ++r)
+ if (u.vi_reads & (1u << r)) cycle = std::max(cycle, vi_ready[r]);
+ return cycle;
+}
+
+inline void retire(const VfUse &u, std::uint32_t cycle, std::uint32_t (&ready)[32][4], std::uint32_t (&vi_ready)[16]) {
+ for (unsigned i = 0; i < u.wn; ++i)
+ for (unsigned lane = 0; lane < 4; ++lane)
+ if (u.wmask[i] & (8u >> lane)) ready[u.wreg[i]][lane] = cycle + 4u;
+ if (u.vi_load) vi_ready[u.vi_load] = cycle + 4u;
+}
+
+inline std::uint32_t q_delay(const std::uint8_t *code, std::uint32_t mask, std::uint32_t pc, std::uint32_t latency) {
+ std::uint32_t ready[32][4] = {}, vi_ready[16] = {};
+ std::uint32_t cycle = 0, issued = 0;
+ for (int k = -3; k <= static_cast<int>(latency); ++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);
+ cycle = issue(u, cycle + 1u, ready, vi_ready);
+ if (k == 0) issued = cycle;
+ else if (k > 0 && cycle >= issued + latency) return static_cast<std::uint32_t>(k);
+ retire(u, cycle, ready, vi_ready);
+ }
+ return latency;
+}
+
+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);
+ return index == 0x38 || index == 0x39 ? 7u : index == 0x3A ? 13u : 0u;
+}
+
+inline bool waits_q(std::uint32_t lo, std::uint32_t hi) {
+ return !(hi & 0x80000000u) && lower_major(lo) == 0x40 && (lo & 0x3Fu) == 0x3Fu && ((lo >> 6) & 31u) == 0x0Eu;
+}
+
+inline bool reads_q(std::uint32_t hi) {
+ const unsigned funct = hi & 63u, op = funct < 0x3C ? funct : ((hi >> 6) & 31u) * 4u + (funct & 3u);
+ return op == 0x1C || op == 0x20 || op == 0x21 || op == 0x24 || op == 0x25;
+}
+
+enum QRead : std::uint8_t { kQDynamic, kQNew, kQOld };
+
+inline QRead q_read_kind(const std::uint8_t *code, std::uint32_t mask, std::uint32_t pc) {
+ const auto load = [&](std::uint32_t a, std::uint32_t &lo, std::uint32_t &hi) {
+ std::memcpy(&lo, code + (a & mask), 4);
+ std::memcpy(&hi, code + (a & mask) + 4, 4);
+ };
+ {
+ std::uint32_t lo, hi;
+ load(pc, lo, hi);
+ if (waits_q(lo, hi) || q_unit_latency(lo, hi)) return kQNew;
+ }
+ std::vector<std::pair<std::uint32_t, std::uint32_t>> jumps;
+ for (std::uint32_t a = 0; a <= mask; a += 8) {
+ std::uint32_t lo, hi;
+ load(a, lo, hi);
+ if (!(hi & 0x80000000u) && is_branch(lo) && !is_jump_register(lo)) jumps.emplace_back(branch_target(lo, a, mask), (a + 8u) & mask);
+ }
+ const auto preds = [&](std::uint32_t y) {
+ std::vector<std::uint32_t> out;
+ std::uint32_t lo, hi;
+ load(y - 16u, lo, hi);
+ const bool jumped = !(hi & 0x80000000u) && is_branch(lo) && (is_unconditional(lo) || is_jump_register(lo));
+ if (!jumped && !(hi & 0x40000000u)) out.push_back((y - 8u) & mask);
+ for (const auto &[target, slot] : jumps)
+ if (target == (y & mask)) out.push_back(slot);
+ return out;
+ };
+ const auto complete = [&](const std::vector<std::uint32_t> &back, std::uint32_t latency) {
+ std::vector<std::uint32_t> order;
+ const std::uint32_t div = back.back();
+ for (std::uint32_t k = 3; k > 0; --k) order.push_back((div - 8u * k) & mask);
+ order.insert(order.end(), back.rbegin(), back.rend());
+ std::uint32_t ready[32][4] = {}, vi_ready[16] = {};
+ std::uint32_t cycle = 0, issued = 0;
+ for (std::size_t i = 0; i < order.size(); ++i) {
+ std::uint32_t lo, hi;
+ load(order[i], lo, hi);
+ const VfUse u = vf_use(lo, hi);
+ cycle = issue(u, cycle + 1u, ready, vi_ready);
+ if (i == 3) issued = cycle;
+ retire(u, cycle, ready, vi_ready);
+ }
+ return cycle >= issued + latency;
+ };
+ bool any_new = false, any_old = false;
+ unsigned budget = 4096;
+ std::vector<std::vector<std::uint32_t>> stack{{pc & mask}};
+ while (!stack.empty()) {
+ if (!--budget) return kQDynamic;
+ const std::vector<std::uint32_t> path = std::move(stack.back());
+ stack.pop_back();
+ const std::uint32_t at = path.back();
+ if (path.size() > 1) {
+ std::uint32_t lo, hi;
+ load(at, lo, hi);
+ if (const std::uint32_t latency = q_unit_latency(lo, hi)) {
+ (complete(path, latency) ? any_new : any_old) = true;
+ continue;
+ }
+ if (waits_q(lo, hi) || path.size() > 15) {
+ any_new = true;
+ continue;
+ }
+ }
+ const std::vector<std::uint32_t> from = preds(at);
+ if (from.empty()) any_new = true;
+ for (std::uint32_t p : from) {
+ std::vector<std::uint32_t> next = path;
+ next.push_back(p);
+ stack.push_back(std::move(next));
+ }
+ if (any_new && any_old) return kQDynamic;
+ }
+ return any_old ? kQOld : kQNew;
+}
+
inline std::uint64_t program_key(const std::uint8_t *code, const std::vector<std::uint32_t> &pcs, std::uint32_t entry) {
std::uint64_t h = 0xCBF29CE484222325ull ^ entry;
const auto mix = [&](std::uint32_t v) {
@@ -234,6 +454,27 @@ struct FlagPipe {
std::uint32_t t = 16;
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;
+ bool q_pending = false;
+
+ PS2_VU_INLINE std::uint32_t begin_q(Vu0Regs &r) {
+ flush_q(r);
+ return r.q;
+ }
+ PS2_VU_INLINE void end_q(Vu0Regs &r, std::uint32_t before, std::uint32_t latency) {
+ q_value = r.q;
+ r.q = before;
+ q_ready = t + latency;
+ q_pending = true;
+ }
+ PS2_VU_INLINE void sync_q(Vu0Regs &r) {
+ if (q_pending && t >= q_ready) flush_q(r);
+ }
+ PS2_VU_INLINE void flush_q(Vu0Regs &r) {
+ if (!q_pending) return;
+ r.q = q_value;
+ q_pending = false;
+ }
void start(const Vu0Regs &r) {
for (int i = 0; i < 8; ++i) {
diff --git a/runtime/src/vu_jit.cpp b/runtime/src/vu_jit.cpp
index bc386f2..398ac4d 100644
--- a/runtime/src/vu_jit.cpp
+++ b/runtime/src/vu_jit.cpp
@@ -11,7 +11,7 @@ namespace ps2::vu::jit {
const char *const kImportNames[kImports] = {
"clip_value", "clip_post", "set_clip", "start", "mac", "status", "clip", "force_mac", "mul", "msub", "to_fixed", "from_fixed",
- "div", "sqrt", "rsqrt", "efu", "kick", "top", "itop", "rand_next", "interpret",
+ "div", "sqrt", "rsqrt", "efu", "kick", "top", "itop", "rand_next", "interpret", "sync_q", "flush_q",
};
namespace {
@@ -26,7 +26,7 @@ struct ImportType {
constexpr ImportType kImportTypes[kImports] = {
{3, true}, {3, false}, {3, false}, {2, false}, {1, true}, {1, true}, {1, true}, {2, false}, {2, true}, {3, true}, {2, true}, {2, true},
- {3, false}, {2, false}, {3, false}, {4, false}, {1, false}, {0, true}, {0, true}, {1, true}, {1, false},
+ {4, false}, {3, false}, {4, false}, {4, false}, {1, false}, {0, true}, {0, true}, {1, true}, {2, false}, {1, false}, {1, false},
};
enum Local : std::uint32_t {
@@ -899,20 +899,26 @@ bool Translator::lower(std::uint32_t w, std::uint32_t pc) {
c_.get(kR);
mem_lane(fs, fsf);
mem_lane(ft, ftf);
+ c_.i32c(q_delay(code_, kMask, pc, 7));
c_.call(kDiv);
break;
case 0x39:
c_.get(kR);
mem_lane(ft, ftf);
+ c_.i32c(q_delay(code_, kMask, pc, 7));
c_.call(kSqrt);
break;
case 0x3A:
c_.get(kR);
mem_lane(fs, fsf);
mem_lane(ft, ftf);
+ c_.i32c(q_delay(code_, kMask, pc, 13));
c_.call(kRsqrt);
break;
- case 0x3B: break;
+ case 0x3B:
+ c_.get(kR);
+ c_.call(kFlushQ);
+ break;
case 0x3C: set_vi(it, [&] { mem_lane(fs, fsf); }); break;
case 0x3D:
if (ft) {
@@ -1320,12 +1326,20 @@ bool Translator::run(std::vector<std::uint8_t> &out) {
c_.i32c(1);
c_.op(kOpAdd);
c_.store(0);
+ if (reads_q(hi)) {
+ const QRead kind = q_read_kind(code_, kMask, pc);
+ if (kind != kQOld) {
+ c_.get(kR);
+ c_.call(kind == kQNew ? kFlushQ : kSyncQ);
+ }
+ }
upper_pre(u, flags_live);
if (hi & 0x80000000u) {
c_.get(kR);
c_.i32c(lo);
c_.store(kOffI);
} else if (!li.supported || !lower(lo, pc)) {
+ c_.get(kR);
c_.i32c(pc);
c_.tail(kInterpret);
}
@@ -1366,6 +1380,8 @@ bool Translator::run(std::vector<std::uint8_t> &out) {
c_.get(kEnding);
c_.open_if();
c_.get(kR);
+ c_.call(kFlushQ);
+ c_.get(kR);
if (pl.branch || pl.jump) {
if (pl.branch) c_.i32c(pl.target & kMask);
else c_.get(kJpc);
@@ -1406,11 +1422,13 @@ bool Translator::run(std::vector<std::uint8_t> &out) {
}
const std::uint32_t next = (pc + 8u) & kMask;
if (!in_.count(next)) {
+ c_.get(kR);
c_.i32c(next);
c_.tail(kInterpret);
}
}
c_.byte(0x0B);
+ c_.get(kR);
c_.get(kPcw);
c_.i32c(3);
c_.op(kOpShl);
diff --git a/runtime/src/vu_jit.hpp b/runtime/src/vu_jit.hpp
index b53ded9..f5d070f 100644
--- a/runtime/src/vu_jit.hpp
+++ b/runtime/src/vu_jit.hpp
@@ -12,7 +12,7 @@ struct Layout {
enum Import : std::uint32_t {
kClipValue, kClipPost, kSetClip, kStart, kMac, kStatus, kClip, kForceMac, kMul, kMsub, kToFixed, kFromFixed,
- kDiv, kSqrt, kRsqrt, kEfu, kKick, kTop, kItop, kRandNext, kInterpret, kImports
+ kDiv, kSqrt, kRsqrt, kEfu, kKick, kTop, kItop, kRandNext, kInterpret, kSyncQ, kFlushQ, kImports
};
extern const char *const kImportNames[kImports];
diff --git a/runtime/src/vu_jit_web.cpp b/runtime/src/vu_jit_web.cpp
index 52f6e2b..7d1c8cd 100644
--- a/runtime/src/vu_jit_web.cpp
+++ b/runtime/src/vu_jit_web.cpp
@@ -101,9 +101,23 @@ EMSCRIPTEN_KEEPALIVE std::uint32_t ps2jit_mul(std::uint32_t a, std::uint32_t b)
EMSCRIPTEN_KEEPALIVE std::uint32_t ps2jit_msub(std::uint32_t acc, std::uint32_t a, std::uint32_t b) { return vuops::msub(acc, a, b); }
EMSCRIPTEN_KEEPALIVE std::uint32_t ps2jit_to_fixed(std::uint32_t x, int frac) { return fp::to_fixed(x, frac); }
EMSCRIPTEN_KEEPALIVE std::uint32_t ps2jit_from_fixed(std::int32_t x, int frac) { return fp::from_fixed(x, frac); }
-EMSCRIPTEN_KEEPALIVE void ps2jit_div(Vu0Regs *r, std::uint32_t s, std::uint32_t t) { vu::div(*r, s, t); }
-EMSCRIPTEN_KEEPALIVE void ps2jit_sqrt(Vu0Regs *r, std::uint32_t t) { vu::sqrt(*r, t); }
-EMSCRIPTEN_KEEPALIVE void ps2jit_rsqrt(Vu0Regs *r, std::uint32_t s, std::uint32_t t) { vu::rsqrt(*r, s, t); }
+EMSCRIPTEN_KEEPALIVE void ps2jit_div(Vu0Regs *r, std::uint32_t s, std::uint32_t t, std::uint32_t latency) {
+ const std::uint32_t before = vu::jit::g_state.fl.begin_q(*r);
+ vu::div(*r, s, t);
+ vu::jit::g_state.fl.end_q(*r, before, latency);
+}
+EMSCRIPTEN_KEEPALIVE void ps2jit_sqrt(Vu0Regs *r, std::uint32_t t, std::uint32_t latency) {
+ const std::uint32_t before = vu::jit::g_state.fl.begin_q(*r);
+ vu::sqrt(*r, t);
+ vu::jit::g_state.fl.end_q(*r, before, latency);
+}
+EMSCRIPTEN_KEEPALIVE void ps2jit_rsqrt(Vu0Regs *r, std::uint32_t s, std::uint32_t t, std::uint32_t latency) {
+ const std::uint32_t before = vu::jit::g_state.fl.begin_q(*r);
+ vu::rsqrt(*r, s, t);
+ vu::jit::g_state.fl.end_q(*r, before, latency);
+}
+EMSCRIPTEN_KEEPALIVE void ps2jit_sync_q(Vu0Regs *r) { vu::jit::g_state.fl.sync_q(*r); }
+EMSCRIPTEN_KEEPALIVE void ps2jit_flush_q(Vu0Regs *r) { vu::jit::g_state.fl.flush_q(*r); }
EMSCRIPTEN_KEEPALIVE void ps2jit_efu(Vu0Regs *r, unsigned index, unsigned fs, unsigned fsf) { vuops::efu(*r, index, fs, fsf); }
EMSCRIPTEN_KEEPALIVE void ps2jit_kick(std::uint32_t address) {
if (vu::jit::g_dry) {
@@ -115,7 +129,8 @@ EMSCRIPTEN_KEEPALIVE void ps2jit_kick(std::uint32_t address) {
EMSCRIPTEN_KEEPALIVE std::uint32_t ps2jit_top() { return vu::top(); }
EMSCRIPTEN_KEEPALIVE std::uint32_t ps2jit_itop() { return vu::itop(); }
EMSCRIPTEN_KEEPALIVE std::uint32_t ps2jit_rand_next(std::uint32_t x) { return vu::rand_next(x); }
-EMSCRIPTEN_KEEPALIVE void ps2jit_interpret(std::uint32_t pc) {
+EMSCRIPTEN_KEEPALIVE void ps2jit_interpret(Vu0Regs *r, std::uint32_t pc) {
+ vu::jit::g_state.fl.flush_q(*r);
if (vu::jit::g_dry) {
vu::jit::g_escape = pc;
return;
diff --git a/runtime/src/vu_micro.cpp b/runtime/src/vu_micro.cpp
index bb82081..7c037c4 100644
--- a/runtime/src/vu_micro.cpp
+++ b/runtime/src/vu_micro.cpp
@@ -37,7 +37,37 @@ struct Unit {
};
Vu0Regs g_vu1;
+std::uint32_t g_generation[2] = {1, 1};
std::uint32_t g_top = 0, g_itop = 0;
+
+struct QDelay {
+ std::uint32_t generation = 0, value = 0;
+};
+
+QDelay g_q_delay[2][2048], g_q_kind[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;
+ const int n = u.code == g_vu1_code ? 1 : 0;
+ QDelay &d = g_q_kind[n][(pc & u.code_mask) >> 3];
+ if (d.generation != g_generation[n]) {
+ d.generation = g_generation[n];
+ d.value = q_read_kind(u.code, u.code_mask, pc);
+ }
+ if (d.value == kQNew) pipe.flush_q(r);
+ else if (d.value == kQDynamic) pipe.sync_q(r);
+}
+
+std::uint32_t q_delay_of(const Unit &u, std::uint32_t pc, std::uint32_t latency) {
+ const int n = u.code == g_vu1_code ? 1 : 0;
+ QDelay &d = g_q_delay[n][(pc & u.code_mask) >> 3];
+ if (d.generation != g_generation[n]) {
+ d.generation = g_generation[n];
+ d.value = q_delay(u.code, u.code_mask, pc, latency);
+ }
+ return d.value;
+}
+
std::uint64_t g_reported = 0;
Unit unit_of(int n) {
@@ -489,10 +519,25 @@ void lower(Unit &u, std::uint32_t w, std::uint32_t pc, std::uint32_t &npc, bool
for (int i = 0; i < 4; ++i) if (dest & (8u >> i)) std::memcpy(q + i * 4, &r.vf[fs].w[i], 4);
break;
}
- case 0x38: vu::div(r, r.vf[fs].w[fsf], r.vf[ft].w[ftf]); break;
- case 0x39: vu::sqrt(r, r.vf[ft].w[ftf]); break;
- case 0x3A: vu::rsqrt(r, r.vf[fs].w[fsf], r.vf[ft].w[ftf]); break;
- case 0x3B: break;
+ case 0x38: {
+ const std::uint32_t before = u.pipe ? u.pipe->begin_q(r) : r.q;
+ vu::div(r, r.vf[fs].w[fsf], r.vf[ft].w[ftf]);
+ if (u.pipe) u.pipe->end_q(r, before, q_delay_of(u, pc, 7));
+ break;
+ }
+ case 0x39: {
+ const std::uint32_t before = u.pipe ? u.pipe->begin_q(r) : r.q;
+ vu::sqrt(r, r.vf[ft].w[ftf]);
+ if (u.pipe) u.pipe->end_q(r, before, q_delay_of(u, pc, 7));
+ break;
+ }
+ case 0x3A: {
+ const std::uint32_t before = u.pipe ? u.pipe->begin_q(r) : r.q;
+ vu::rsqrt(r, r.vf[fs].w[fsf], r.vf[ft].w[ftf]);
+ if (u.pipe) u.pipe->end_q(r, before, q_delay_of(u, pc, 13));
+ break;
+ }
+ case 0x3B: if (u.pipe) u.pipe->flush_q(r); break;
case 0x3C: store_vi(r, it, r.vf[fs].w[fsf]); break;
case 0x3D: if (ft) { const std::uint32_t v = static_cast<std::uint32_t>(static_cast<std::int32_t>(static_cast<std::int16_t>(r.vi[is]))); for (int i = 0; i < 4; ++i) if (dest & (8u >> i)) r.vf[ft].w[i] = v; } break;
case 0x3E: {
@@ -638,7 +683,6 @@ void set_tops(std::uint32_t top, std::uint32_t itop) {
void run_fast(int n, std::uint32_t address);
std::unordered_map<std::uint64_t, ProgramFn> g_programs;
-std::uint32_t g_generation[2] = {1, 1};
struct Candidate {
std::vector<std::uint32_t> runs;
@@ -882,6 +926,7 @@ void run_fast(int n, std::uint32_t address) {
pc = npc;
npc = (npc + 8u) & u.code_mask;
++pipe.t;
+ read_q(u, pipe, r, current, hi);
fast_upper(r, d, out);
if (hi & 0x80000000u) {
r.i = lo;
@@ -910,6 +955,7 @@ void run_fast(int n, std::uint32_t address) {
r.vf[0].w[3] = fp::kOne;
r.vi[0] = 0;
if (ending) {
+ pipe.flush_q(r);
r.tpc = pc;
return;
}
@@ -939,6 +985,7 @@ void run(int n, std::uint32_t address) {
pc = npc;
npc = (npc + 8u) & u.code_mask;
++pipe.t;
+ read_q(u, pipe, r, current, hi);
UpperOut out = upper(r, hi);
bool branch = false;
if (hi & 0x80000000u) {
@@ -959,6 +1006,7 @@ void run(int n, std::uint32_t address) {
r.vf[0].w[3] = fp::kOne;
r.vi[0] = 0;
if (ending) {
+ pipe.flush_q(r);
r.tpc = pc;
return;
}