Skip to content

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

C++

recomp: emit C++ for the analysed code

Samir Nuriafcaf90f32b4
diff --git a/recomp/codegen.cpp b/recomp/codegen.cpp
new file mode 100644
index 0000000..d93fef4
--- /dev/null
+++ b/recomp/codegen.cpp
@@ -0,0 +1,743 @@
+#include "codegen.hpp"
+
+#include <bit>
+#include <filesystem>
+#include <format>
+#include <fstream>
+#include <map>
+#include <set>
+#include <sstream>
+#include <stdexcept>
+#include <vector>
+
+namespace recomp {
+
+namespace {
+
+namespace fs = std::filesystem;
+using enum Op;
+
+std::string hex(std::uint32_t v) { return std::format("0x{:08X}u", v); }
+std::string label(std::uint32_t v) { return std::format("L_{:08X}", v); }
+std::string w(std::uint32_t r) { return r ? std::format("c.r[{}].w[0]", r) : "0u"; }
+std::string d(std::uint32_t r) { return r ? std::format("c.r[{}].d[0]", r) : "0ull"; }
+std::string sd(std::uint32_t r) { return r ? std::format("c.r[{}].sd[0]", r) : "0ll"; }
+std::string reg(std::uint32_t r) { return std::format("c.r[{}]", r); }
+std::string f(std::uint32_t r) { return std::format("c.f[{}]", r); }
+std::string vf(std::uint32_t r) { return std::format("c.vu0.vf[{}]", r); }
+std::string vi(std::uint32_t r) { return std::format("c.vu0.vi[{}]", r); }
+
+std::string offset(std::int32_t imm) {
+ if (imm < 0) return std::format(" - {}u", -static_cast<std::int64_t>(imm));
+ if (imm > 0) return std::format(" + {}u", imm);
+ return "";
+}
+
+std::string address(const Insn &i) { return "(" + w(i.rs()) + offset(i.simm()) + ")"; }
+
+class Layout {
+public:
+ Layout(const Program &p, std::uint32_t bits) : bits_(bits) {
+ origin_ = p.base & ~(size() - 1);
+ count_ = ((p.end - origin_) + size() - 1) >> bits;
+ }
+
+ std::uint32_t size() const { return 1u << bits_; }
+ std::uint32_t origin() const { return origin_; }
+ std::uint32_t count() const { return count_; }
+ std::uint32_t unit_of(std::uint32_t a) const { return (a - origin_) >> bits_; }
+ std::uint32_t base_of(std::uint32_t u) const { return origin_ + (u << bits_); }
+
+private:
+ std::uint32_t bits_;
+ std::uint32_t origin_{};
+ std::uint32_t count_{};
+};
+
+std::string unit_name(std::uint32_t u) { return std::format("unit_{:04X}", u); }
+std::string table_name(std::uint32_t u) { return std::format("entries_{:04X}", u); }
+
+class Generator {
+public:
+ Generator(const Image &image, Program &program, const CodegenOptions &options)
+ : image_(image), p_(program), options_(options), layout_(program, options.unit_bits) {}
+
+ void run() {
+ add_required_entries();
+ assign_ids();
+ fs::create_directories(options_.output);
+ write_header();
+ std::size_t written = 0;
+ for (std::uint32_t u = 0; u < layout_.count(); ++u)
+ if (has_code_[u]) {
+ write_unit(u);
+ ++written;
+ }
+ write_table();
+ std::printf("%zu units written to %s\n", written, options_.output.c_str());
+ }
+
+private:
+ Insn at(std::uint32_t a) const { return decode(*image_.word(a), a); }
+
+ void add_required_entries() {
+ for (std::uint32_t a = p_.base; a < p_.end; a += 4) {
+ if (!p_.is_code(a)) continue;
+ const Insn i = at(a);
+ const Flow fl = flow(i.op);
+ if (fl == Flow::Branch || fl == Flow::BranchLikely || fl == Flow::Jump || i.op == Jal) {
+ const std::uint32_t t = i.branch_target_of();
+ if (p_.is_code(t) && layout_.unit_of(t) != layout_.unit_of(a)) p_.entries.insert(t);
+ }
+ const std::uint32_t next = a + 4;
+ if (layout_.unit_of(next) != layout_.unit_of(a) && p_.is_code(next)) p_.entries.insert(next);
+ if (has_delay_slot(i.op) && layout_.unit_of(a + 8) != layout_.unit_of(a) && p_.is_code(a + 8))
+ p_.entries.insert(a + 8);
+ }
+ }
+
+ void assign_ids() {
+ ids_.assign(layout_.count(), {});
+ has_code_.assign(layout_.count(), false);
+ for (std::uint32_t a = p_.base; a < p_.end; a += 4)
+ if (p_.is_code(a)) has_code_[layout_.unit_of(a)] = true;
+ for (std::uint32_t a : p_.entries) {
+ if (!p_.is_code(a)) continue;
+ auto &unit = ids_[layout_.unit_of(a)];
+ const auto id = static_cast<std::uint16_t>(unit.size() + 1);
+ unit.emplace(a, id);
+ }
+ }
+
+ std::uint16_t id_of(std::uint32_t a) const {
+ const auto &unit = ids_[layout_.unit_of(a)];
+ const auto it = unit.find(a);
+ if (it == unit.end()) throw std::runtime_error(std::format("no entry for {:08X}", a));
+ return it->second;
+ }
+
+ bool in_unit(std::uint32_t a) const { return a >= base_ && a < base_ + layout_.size(); }
+
+ void write_header() {
+ std::ofstream out(fs::path(options_.output) / "units.hpp");
+ out << "#pragma once\n\n#include \"ps2/generated.hpp\"\n\nnamespace gen {\n";
+ for (std::uint32_t u = 0; u < layout_.count(); ++u) {
+ if (!has_code_[u]) continue;
+ out << "void " << unit_name(u) << "(ps2::Cpu &c, std::uint32_t entry, std::uint32_t depth);\n";
+ out << "extern const std::uint16_t " << table_name(u) << "[];\n";
+ }
+ out << "}\n";
+ }
+
+ void write_table() {
+ std::ofstream out(fs::path(options_.output) / "table.cpp");
+ out << "#include \"units.hpp\"\n\nnamespace gen {\n\nconst ps2::UnitInfo kUnits[] = {\n";
+ std::size_t n = 0;
+ for (std::uint32_t u = 0; u < layout_.count(); ++u) {
+ if (!has_code_[u]) continue;
+ out << std::format(" {{{}, {}u, &{}, {}}},\n", hex(layout_.base_of(u)), layout_.size(), unit_name(u), table_name(u));
+ ++n;
+ }
+ out << "};\n\n}\n\nvoid register_generated_code() { ps2::register_units(gen::kUnits, " << n << "u); }\n";
+ }
+
+ void write_unit(std::uint32_t u) {
+ base_ = layout_.base_of(u);
+ labels_.clear();
+ for (const auto &[a, id] : ids_[u]) labels_.insert(a);
+ const std::uint32_t end = base_ + layout_.size();
+ for (std::uint32_t a = base_; a < end; a += 4) {
+ if (!p_.is_code(a)) continue;
+ const Insn i = at(a);
+ const Flow fl = flow(i.op);
+ if (fl == Flow::Branch || fl == Flow::BranchLikely || fl == Flow::Jump) {
+ const std::uint32_t t = i.branch_target_of();
+ if (in_unit(t) && p_.is_code(t)) labels_.insert(t);
+ }
+ if (has_delay_slot(i.op) && labels_.count(a + 4) && in_unit(a + 8)) labels_.insert(a + 8);
+ }
+
+ out_.str({});
+ out_ << "#include \"units.hpp\"\n\nnamespace gen {\n\nusing namespace ps2;\n\n";
+ out_ << "const std::uint16_t " << table_name(u) << "[" << (layout_.size() / 4) << "] = {";
+ std::vector<std::uint16_t> dense(layout_.size() / 4, 0);
+ for (const auto &[a, id] : ids_[u]) dense[(a - base_) >> 2] = id;
+ for (std::size_t k = 0; k < dense.size(); ++k) out_ << (k % 32 ? " " : "\n ") << dense[k] << ",";
+ out_ << "\n};\n\n";
+ out_ << "void " << unit_name(u) << "(Cpu &c, std::uint32_t entry, std::uint32_t depth) {\n";
+ out_ << "dispatch_entry:\n switch (entry) {\n";
+ for (const auto &[a, id] : ids_[u]) out_ << " case " << id << "u: goto " << label(a) << ";\n";
+ out_ << " default: bad_entry(c, " << hex(base_) << "); return;\n }\n";
+
+ pending_ = 0;
+ bool falls = false, skip = false;
+ for (std::uint32_t a = base_; a < end; a += 4) {
+ if (!p_.is_code(a)) {
+ if (falls) exit_to(a);
+ falls = skip = false;
+ continue;
+ }
+ const bool labelled = labels_.count(a) != 0;
+ if (skip && !labelled) {
+ skip = false;
+ continue;
+ }
+ skip = false;
+ if (labelled) {
+ flush();
+ out_ << label(a) << ":\n";
+ }
+ const Insn i = at(a);
+ const Flow fl = flow(i.op);
+ if (fl == Flow::None) {
+ simple(i);
+ ++pending_;
+ falls = true;
+ } else if (fl == Flow::Stop) {
+ flush();
+ line(std::format("stop(c, {}, \"{}\"); return;", hex(a), name(i.op)));
+ falls = false;
+ } else {
+ const Insn ds = p_.is_code(a + 4) ? at(a + 4) : Insn{a + 4, 0, Invalid};
+ control(i, ds);
+ falls = fl != Flow::Jump && fl != Flow::JumpRegister;
+ skip = true;
+ if (falls && labels_.count(a + 4)) {
+ if (in_unit(a + 8)) line("goto " + label(a + 8) + ";");
+ else exit_to(a + 8);
+ falls = false;
+ }
+ }
+ }
+ if (falls) exit_to(end);
+ out_ << "}\n\n}\n";
+ std::ofstream file(fs::path(options_.output) / std::format("{}.cpp", unit_name(u)));
+ file << out_.str();
+ }
+
+ void line(const std::string &text) {
+ if (capture_) *capture_ += text + " ";
+ else out_ << " " << text << "\n";
+ }
+
+ void flush() {
+ if (pending_) line(std::format("c.budget -= {};", pending_));
+ pending_ = 0;
+ }
+
+ void exit_to(std::uint32_t a) {
+ flush();
+ if (p_.is_code(a)) {
+ line(std::format("[[clang::musttail]] return {}(c, {}u, depth);", unit_name(layout_.unit_of(a)), id_of(a)));
+ } else {
+ line(std::format("c.pc = {}; [[clang::musttail]] return dispatch(c, 0u, depth);", hex(a)));
+ }
+ }
+
+ std::string jump(std::uint32_t t, std::uint32_t from) {
+ std::string check = t <= from ? std::format("if (c.budget < 0) {{ c.pc = {} | kUnwind; return; }} ", hex(t)) : "";
+ if (in_unit(t) && p_.is_code(t)) return check + "goto " + label(t) + ";";
+ if (p_.is_code(t))
+ return check + std::format("[[clang::musttail]] return {}(c, {}u, depth);", unit_name(layout_.unit_of(t)), id_of(t));
+ return std::format("c.pc = {}; [[clang::musttail]] return dispatch(c, 0u, depth);", hex(t));
+ }
+
+ std::string call_to(std::uint32_t t, std::uint32_t ret) {
+ std::string invoke;
+ if (p_.is_code(t))
+ invoke = std::format("if (depth < kMaxDepth) {}(c, {}u, depth + 1u); else {{ c.pc = {}; return; }}",
+ unit_name(layout_.unit_of(t)), id_of(t), hex(t));
+ else
+ invoke = std::format("call(c, {}, depth + 1u);", hex(t));
+ return invoke + std::format(" if (c.pc != {}) [[unlikely]] return;", hex(ret));
+ }
+
+ std::string condition(const Insn &i) {
+ switch (i.op) {
+ case Beq: case Beql: return d(i.rs()) + " == " + d(i.rt());
+ case Bne: case Bnel: return d(i.rs()) + " != " + d(i.rt());
+ case Blez: case Blezl: return sd(i.rs()) + " <= 0";
+ case Bgtz: case Bgtzl: return sd(i.rs()) + " > 0";
+ case Bltz: case Bltzl: case Bltzal: case Bltzall: return sd(i.rs()) + " < 0";
+ case Bgez: case Bgezl: case Bgezal: case Bgezall: return sd(i.rs()) + " >= 0";
+ case Bc0f: case Bc0fl: return "!cpcond0(c)";
+ case Bc0t: case Bc0tl: return "cpcond0(c)";
+ case Bc1f: case Bc1fl: return "!(c.fcr31 & kFpuCondition)";
+ case Bc1t: case Bc1tl: return "(c.fcr31 & kFpuCondition) != 0";
+ case Bc2f: case Bc2fl: return "!vu0_busy(c)";
+ case Bc2t: case Bc2tl: return "vu0_busy(c)";
+ default: throw std::runtime_error(std::string("no condition for ") + name(i.op));
+ }
+ }
+
+ std::string delay(const Insn &ds) {
+ if (ds.op == Invalid || has_delay_slot(ds.op) || ds.op == Syscall)
+ return std::format("stop(c, {}, \"unsupported delay slot\"); return;", hex(ds.pc));
+ std::string text;
+ capture_ = &text;
+ simple(ds);
+ capture_ = nullptr;
+ return text;
+ }
+
+ void control(const Insn &i, const Insn &ds) {
+ const std::uint32_t a = i.pc, ret = a + 8;
+ line(std::format("c.budget -= {};", pending_ + 2));
+ pending_ = 0;
+ const std::string slot = delay(ds);
+ const std::string link = std::format("c.r[31].sd[0] = static_cast<std::int32_t>({});", hex(ret));
+ const std::uint32_t t = i.branch_target_of();
+ switch (i.op) {
+ case Jal:
+ line(std::format("{{ {} {} {} }}", link, slot, call_to(t, ret)));
+ return;
+ case Bltzal: case Bgezal:
+ line(std::format("{{ const bool t = {}; {} {} if (t) {{ {} }} }}", condition(i), link, slot, call_to(t, ret)));
+ return;
+ case Bltzall: case Bgezall:
+ line(std::format("{{ const bool t = {}; {} if (t) {{ {} {} }} }}", condition(i), link, slot, call_to(t, ret)));
+ return;
+ default:
+ break;
+ }
+ switch (flow(i.op)) {
+ case Flow::Branch:
+ line(std::format("{{ const bool t = {}; {} if (t) {{ {} }} }}", condition(i), slot, jump(t, a)));
+ break;
+ case Flow::BranchLikely:
+ line(std::format("if ({}) {{ {} {} }}", condition(i), slot, jump(t, a)));
+ break;
+ case Flow::Jump:
+ line(std::format("{{ {} {} }}", slot, jump(t, a)));
+ break;
+ case Flow::JumpRegister:
+ if (i.rs() == 31) {
+ line(std::format("{{ const std::uint32_t t = {}; {} c.pc = t; return; }}", w(31), slot));
+ } else {
+ line(std::format("{{ const std::uint32_t t = {}; {} c.pc = t; if (t - {} < {}u) {{ entry = {}[(t - {}) >> 2]; if (entry) goto dispatch_entry; }} [[clang::musttail]] return dispatch(c, 0u, depth); }}",
+ w(i.rs()), slot, hex(base_), layout_.size(), table_name(layout_.unit_of(base_)), hex(base_)));
+ }
+ break;
+ case Flow::CallRegister: {
+ const std::string set = i.rd() ? std::format("c.r[{}].sd[0] = static_cast<std::int32_t>({});", i.rd(), hex(ret)) : "";
+ line(std::format("{{ const std::uint32_t t = {}; {} {} c.pc = t; call(c, t, depth + 1u); if (c.pc != {}) [[unlikely]] return; }}",
+ w(i.rs()), set, slot, hex(ret)));
+ break;
+ }
+ default:
+ throw std::runtime_error("unexpected control flow");
+ }
+ }
+
+ void set_d(std::uint32_t r, const std::string &value) {
+ if (r) line(std::format("c.r[{}].d[0] = {};", r, value));
+ }
+
+ void set_w(std::uint32_t r, const std::string &value) {
+ if (r) line(std::format("set32(c.r[{}], {});", r, value));
+ }
+
+ void mmi3(const char *op, const Insn &i) {
+ if (i.rd()) line(std::format("mmi::{}({}, {}, {});", op, reg(i.rd()), reg(i.rs()), reg(i.rt())));
+ }
+
+ void mmi2(const char *op, const Insn &i) {
+ if (i.rd()) line(std::format("mmi::{}({}, {});", op, reg(i.rd()), reg(i.rt())));
+ }
+
+ std::string dest_reg(const Insn &i) { return i.rd() ? reg(i.rd()) : "scratch"; }
+
+ void vec(const char *op, const Insn &i) {
+ line(std::format("vu::{}(c.vu0, {}u, {}u, {}u, {}u);", op, i.dest(), i.fd(), i.fs(), i.ft()));
+ }
+
+ void vecbc(const char *op, const Insn &i, const std::string &t) {
+ line(std::format("vu::{}(c.vu0, {}u, {}u, {}u, {});", op, i.dest(), i.fd(), i.fs(), t));
+ }
+
+ void vacc(const char *op, const Insn &i) {
+ line(std::format("vu::{}(c.vu0, {}u, {}u, {}u);", op, i.dest(), i.fs(), i.ft()));
+ }
+
+ void vaccbc(const char *op, const Insn &i, const std::string &t) {
+ line(std::format("vu::{}(c.vu0, {}u, {}u, {});", op, i.dest(), i.fs(), t));
+ }
+
+ std::string bcv(const Insn &i) { return std::format("{}.w[{}]", vf(i.ft()), i.bc()); }
+
+ void simple(const Insn &i) {
+ const std::uint32_t rs = i.rs(), rt = i.rt(), rd = i.rd(), sa = i.sa();
+ const std::uint32_t fsf = (i.word >> 21) & 3u, ftf = (i.word >> 23) & 3u;
+ switch (i.op) {
+ case Sll: if (i.word) set_w(rd, std::format("{} << {}", w(rt), sa)); break;
+ case Srl: set_w(rd, std::format("{} >> {}", w(rt), sa)); break;
+ case Sra: set_w(rd, std::format("static_cast<std::uint32_t>(static_cast<std::int32_t>({}) >> {})", w(rt), sa)); break;
+ case Sllv: set_w(rd, std::format("{} << ({} & 31u)", w(rt), w(rs))); break;
+ case Srlv: set_w(rd, std::format("{} >> ({} & 31u)", w(rt), w(rs))); break;
+ case Srav: set_w(rd, std::format("static_cast<std::uint32_t>(static_cast<std::int32_t>({}) >> ({} & 31u))", w(rt), w(rs))); break;
+ case Movz: if (rd) line(std::format("if ({} == 0) c.r[{}].d[0] = {};", d(rt), rd, d(rs))); break;
+ case Movn: if (rd) line(std::format("if ({} != 0) c.r[{}].d[0] = {};", d(rt), rd, d(rs))); break;
+ case Syscall:
+ flush();
+ line(std::format("c.pc = {}; syscall(c, {}); if (c.pc != {}) return;", hex(i.pc + 4), hex(i.pc), hex(i.pc + 4)));
+ break;
+ case Break: line(std::format("breakpoint(c, {});", hex(i.pc))); break;
+ case Sync: case Cache: case Pref: break;
+ case Mfhi: set_d(rd, "c.hi.d[0]"); break;
+ case Mflo: set_d(rd, "c.lo.d[0]"); break;
+ case Mfhi1: set_d(rd, "c.hi.d[1]"); break;
+ case Mflo1: set_d(rd, "c.lo.d[1]"); break;
+ case Mthi: line("c.hi.d[0] = " + d(rs) + ";"); break;
+ case Mtlo: line("c.lo.d[0] = " + d(rs) + ";"); break;
+ case Mthi1: line("c.hi.d[1] = " + d(rs) + ";"); break;
+ case Mtlo1: line("c.lo.d[1] = " + d(rs) + ";"); break;
+ case Dsllv: set_d(rd, std::format("{} << ({} & 63u)", d(rt), w(rs))); break;
+ case Dsrlv: set_d(rd, std::format("{} >> ({} & 63u)", d(rt), w(rs))); break;
+ case Dsrav: set_d(rd, std::format("static_cast<std::uint64_t>({} >> ({} & 63u))", sd(rt), w(rs))); break;
+ case Mult: case Multu: case Mult1: case Multu1: case Madd: case Maddu: case Madd1: case Maddu1: {
+ const bool one = i.op == Mult1 || i.op == Multu1 || i.op == Madd1 || i.op == Maddu1;
+ const char *fn = (i.op == Mult || i.op == Mult1) ? "mult" : (i.op == Multu || i.op == Multu1) ? "multu"
+ : (i.op == Madd || i.op == Madd1) ? "madd" : "maddu";
+ line(std::format("mmi::{}(c, {}, {}, {});", fn, one ? 1 : 0, w(rs), w(rt)));
+ set_d(rd, std::format("c.lo.d[{}]", one ? 1 : 0));
+ break;
+ }
+ case Div: line(std::format("mmi::div(c, 0, {}, {});", w(rs), w(rt))); break;
+ case Divu: line(std::format("mmi::divu(c, 0, {}, {});", w(rs), w(rt))); break;
+ case Div1: line(std::format("mmi::div(c, 1, {}, {});", w(rs), w(rt))); break;
+ case Divu1: line(std::format("mmi::divu(c, 1, {}, {});", w(rs), w(rt))); break;
+ case Add: case Addu: set_w(rd, w(rs) + " + " + w(rt)); break;
+ case Sub: case Subu: set_w(rd, w(rs) + " - " + w(rt)); break;
+ case And: set_d(rd, d(rs) + " & " + d(rt)); break;
+ case Or: set_d(rd, d(rs) + " | " + d(rt)); break;
+ case Xor: set_d(rd, d(rs) + " ^ " + d(rt)); break;
+ case Nor: set_d(rd, "~(" + d(rs) + " | " + d(rt) + ")"); break;
+ case Mfsa: set_d(rd, "c.sa"); break;
+ case Mtsa: line(std::format("c.sa = {} & 15u;", w(rs))); break;
+ case Mtsab: line(std::format("c.sa = ({} ^ {}u) & 15u;", w(rs), i.uimm())); break;
+ case Mtsah: line(std::format("c.sa = (({} ^ {}u) & 7u) * 2u;", w(rs), i.uimm())); break;
+ case Slt: set_d(rd, std::format("{} < {} ? 1ull : 0ull", sd(rs), sd(rt))); break;
+ case Sltu: set_d(rd, std::format("{} < {} ? 1ull : 0ull", d(rs), d(rt))); break;
+ case Dadd: case Daddu: set_d(rd, d(rs) + " + " + d(rt)); break;
+ case Dsub: case Dsubu: set_d(rd, d(rs) + " - " + d(rt)); break;
+ case Tge: line(std::format("if ({} >= {}) trap(c, {});", sd(rs), sd(rt), hex(i.pc))); break;
+ case Tgeu: line(std::format("if ({} >= {}) trap(c, {});", d(rs), d(rt), hex(i.pc))); break;
+ case Tlt: line(std::format("if ({} < {}) trap(c, {});", sd(rs), sd(rt), hex(i.pc))); break;
+ case Tltu: line(std::format("if ({} < {}) trap(c, {});", d(rs), d(rt), hex(i.pc))); break;
+ case Teq: line(std::format("if ({} == {}) trap(c, {});", d(rs), d(rt), hex(i.pc))); break;
+ case Tne: line(std::format("if ({} != {}) trap(c, {});", d(rs), d(rt), hex(i.pc))); break;
+ case Tgei: line(std::format("if ({} >= {}ll) trap(c, {});", sd(rs), i.simm(), hex(i.pc))); break;
+ case Tgeiu: line(std::format("if ({} >= static_cast<std::uint64_t>({}ll)) trap(c, {});", d(rs), i.simm(), hex(i.pc))); break;
+ case Tlti: line(std::format("if ({} < {}ll) trap(c, {});", sd(rs), i.simm(), hex(i.pc))); break;
+ case Tltiu: line(std::format("if ({} < static_cast<std::uint64_t>({}ll)) trap(c, {});", d(rs), i.simm(), hex(i.pc))); break;
+ case Teqi: line(std::format("if ({} == {}ll) trap(c, {});", sd(rs), i.simm(), hex(i.pc))); break;
+ case Tnei: line(std::format("if ({} != {}ll) trap(c, {});", sd(rs), i.simm(), hex(i.pc))); break;
+ case Dsll: set_d(rd, std::format("{} << {}", d(rt), sa)); break;
+ case Dsrl: set_d(rd, std::format("{} >> {}", d(rt), sa)); break;
+ case Dsra: set_d(rd, std::format("static_cast<std::uint64_t>({} >> {})", sd(rt), sa)); break;
+ case Dsll32: set_d(rd, std::format("{} << {}", d(rt), sa + 32)); break;
+ case Dsrl32: set_d(rd, std::format("{} >> {}", d(rt), sa + 32)); break;
+ case Dsra32: set_d(rd, std::format("static_cast<std::uint64_t>({} >> {})", sd(rt), sa + 32)); break;
+ case Addi: case Addiu: set_w(rt, w(rs) + offset(i.simm())); break;
+ case Slti: set_d(rt, std::format("{} < {}ll ? 1ull : 0ull", sd(rs), i.simm())); break;
+ case Sltiu: set_d(rt, std::format("{} < static_cast<std::uint64_t>({}ll) ? 1ull : 0ull", d(rs), i.simm())); break;
+ case Andi: set_d(rt, std::format("{} & {}ull", d(rs), i.uimm())); break;
+ case Ori: set_d(rt, std::format("{} | {}ull", d(rs), i.uimm())); break;
+ case Xori: set_d(rt, std::format("{} ^ {}ull", d(rs), i.uimm())); break;
+ case Lui: if (rt) line(std::format("c.r[{}].sd[0] = static_cast<std::int32_t>({});", rt, hex(i.uimm() << 16))); break;
+ case Daddi: case Daddiu: set_d(rt, std::format("{} + static_cast<std::uint64_t>({}ll)", d(rs), i.simm())); break;
+ case Ldl: if (rt) line(std::format("ldl({}, {});", reg(rt), address(i))); break;
+ case Ldr: if (rt) line(std::format("ldr({}, {});", reg(rt), address(i))); break;
+ case Lwl: if (rt) line(std::format("lwl({}, {});", reg(rt), address(i))); break;
+ case Lwr: if (rt) line(std::format("lwr({}, {});", reg(rt), address(i))); break;
+ case Sdl: line(std::format("sdl({}, {});", reg(rt), address(i))); break;
+ case Sdr: line(std::format("sdr({}, {});", reg(rt), address(i))); break;
+ case Swl: line(std::format("swl({}, {});", reg(rt), address(i))); break;
+ case Swr: line(std::format("swr({}, {});", reg(rt), address(i))); break;
+ case Lq:
+ if (rt) line(std::format("ld128({} & ~15u, {});", address(i), reg(rt)));
+ else line(std::format("{{ Reg128 scratch; ld128({} & ~15u, scratch); }}", address(i)));
+ break;
+ case Sq: line(std::format("st128({} & ~15u, {});", address(i), reg(rt))); break;
+ case Lb: if (rt) line(std::format("c.r[{}].sd[0] = static_cast<std::int8_t>(ld8({}));", rt, address(i))); else line(std::format("(void)ld8({});", address(i))); break;
+ case Lbu: if (rt) line(std::format("c.r[{}].d[0] = ld8({});", rt, address(i))); else line(std::format("(void)ld8({});", address(i))); break;
+ case Lh: if (rt) line(std::format("c.r[{}].sd[0] = static_cast<std::int16_t>(ld16({}));", rt, address(i))); else line(std::format("(void)ld16({});", address(i))); break;
+ case Lhu: if (rt) line(std::format("c.r[{}].d[0] = ld16({});", rt, address(i))); else line(std::format("(void)ld16({});", address(i))); break;
+ case Lw: if (rt) line(std::format("set32(c.r[{}], ld32({}));", rt, address(i))); else line(std::format("(void)ld32({});", address(i))); break;
+ case Lwu: if (rt) line(std::format("c.r[{}].d[0] = ld32({});", rt, address(i))); else line(std::format("(void)ld32({});", address(i))); break;
+ case Ld: if (rt) line(std::format("c.r[{}].d[0] = ld64({});", rt, address(i))); else line(std::format("(void)ld64({});", address(i))); break;
+ case Sb: line(std::format("st8({}, static_cast<std::uint8_t>({}));", address(i), w(rt))); break;
+ case Sh: line(std::format("st16({}, static_cast<std::uint16_t>({}));", address(i), w(rt))); break;
+ case Sw: line(std::format("st32({}, {});", address(i), w(rt))); break;
+ case Sd: line(std::format("st64({}, {});", address(i), d(rt))); break;
+ case Lwc1: line(std::format("{} = ld32({});", f(i.ft()), address(i))); break;
+ case Swc1: line(std::format("st32({}, {});", address(i), f(i.ft()))); break;
+ case Lqc2:
+ if (i.ft()) line(std::format("ld128({} & ~15u, {});", address(i), vf(i.ft())));
+ else line(std::format("{{ Reg128 scratch; ld128({} & ~15u, scratch); }}", address(i)));
+ break;
+ case Sqc2: line(std::format("st128({} & ~15u, {});", address(i), vf(i.ft()))); break;
+
+ case Plzcw: if (rd) line(std::format("mmi::plzcw({}, {});", reg(rd), reg(rs))); break;
+ case Pmfhl: if (rd) line(std::format("mmi::pmfhl(c, {}, {}u);", reg(rd), sa)); break;
+ case Pmthl: line(std::format("mmi::pmthl(c, {});", reg(rs))); break;
+ case Psllh: if (rd) line(std::format("mmi::psllh({}, {}, {}u);", reg(rd), reg(rt), sa)); break;
+ case Psrlh: if (rd) line(std::format("mmi::psrlh({}, {}, {}u);", reg(rd), reg(rt), sa)); break;
+ case Psrah: if (rd) line(std::format("mmi::psrah({}, {}, {}u);", reg(rd), reg(rt), sa)); break;
+ case Psllw: if (rd) line(std::format("mmi::psllw({}, {}, {}u);", reg(rd), reg(rt), sa)); break;
+ case Psrlw: if (rd) line(std::format("mmi::psrlw({}, {}, {}u);", reg(rd), reg(rt), sa)); break;
+ case Psraw: if (rd) line(std::format("mmi::psraw({}, {}, {}u);", reg(rd), reg(rt), sa)); break;
+ case Paddw: mmi3("paddw", i); break;
+ case Psubw: mmi3("psubw", i); break;
+ case Pcgtw: mmi3("pcgtw", i); break;
+ case Pmaxw: mmi3("pmaxw", i); break;
+ case Paddh: mmi3("paddh", i); break;
+ case Psubh: mmi3("psubh", i); break;
+ case Pcgth: mmi3("pcgth", i); break;
+ case Pmaxh: mmi3("pmaxh", i); break;
+ case Paddb: mmi3("paddb", i); break;
+ case Psubb: mmi3("psubb", i); break;
+ case Pcgtb: mmi3("pcgtb", i); break;
+ case Paddsw: mmi3("paddsw", i); break;
+ case Psubsw: mmi3("psubsw", i); break;
+ case Pextlw: mmi3("pextlw", i); break;
+ case Ppacw: mmi3("ppacw", i); break;
+ case Paddsh: mmi3("paddsh", i); break;
+ case Psubsh: mmi3("psubsh", i); break;
+ case Pextlh: mmi3("pextlh", i); break;
+ case Ppach: mmi3("ppach", i); break;
+ case Paddsb: mmi3("paddsb", i); break;
+ case Psubsb: mmi3("psubsb", i); break;
+ case Pextlb: mmi3("pextlb", i); break;
+ case Ppacb: mmi3("ppacb", i); break;
+ case Pext5: mmi2("pext5", i); break;
+ case Ppac5: mmi2("ppac5", i); break;
+ case Pabsw: mmi2("pabsw", i); break;
+ case Pceqw: mmi3("pceqw", i); break;
+ case Pminw: mmi3("pminw", i); break;
+ case Padsbh: mmi3("padsbh", i); break;
+ case Pabsh: mmi2("pabsh", i); break;
+ case Pceqh: mmi3("pceqh", i); break;
+ case Pminh: mmi3("pminh", i); break;
+ case Pceqb: mmi3("pceqb", i); break;
+ case Padduw: mmi3("padduw", i); break;
+ case Psubuw: mmi3("psubuw", i); break;
+ case Pextuw: mmi3("pextuw", i); break;
+ case Padduh: mmi3("padduh", i); break;
+ case Psubuh: mmi3("psubuh", i); break;
+ case Pextuh: mmi3("pextuh", i); break;
+ case Paddub: mmi3("paddub", i); break;
+ case Psubub: mmi3("psubub", i); break;
+ case Pextub: mmi3("pextub", i); break;
+ case Qfsrv: if (rd) line(std::format("mmi::qfsrv({}, {}, {}, c.sa);", reg(rd), reg(rs), reg(rt))); break;
+ case Psllvw: mmi3("psllvw", i); break;
+ case Psrlvw: mmi3("psrlvw", i); break;
+ case Psravw: mmi3("psravw", i); break;
+ case Pmaddw: case Pmsubw:
+ line(std::format("{{ Reg128 scratch; mmi::pmaddw(c, {}, {}, {}, {}); }}", dest_reg(i), reg(rs), reg(rt), i.op == Pmsubw ? "true" : "false"));
+ break;
+ case Pmadduw: line(std::format("{{ Reg128 scratch; mmi::pmadduw(c, {}, {}, {}); }}", dest_reg(i), reg(rs), reg(rt))); break;
+ case Pmultw: line(std::format("{{ Reg128 scratch; mmi::pmultw(c, {}, {}, {}); }}", dest_reg(i), reg(rs), reg(rt))); break;
+ case Pmultuw: line(std::format("{{ Reg128 scratch; mmi::pmultuw(c, {}, {}, {}); }}", dest_reg(i), reg(rs), reg(rt))); break;
+ case Pmulth: case Pmaddh: case Pmsubh:
+ line(std::format("{{ Reg128 scratch; mmi::pmulth(c, {}, {}, {}, {}); }}", dest_reg(i), reg(rs), reg(rt), i.op == Pmulth ? 0 : i.op == Pmaddh ? 1 : 2));
+ break;
+ case Phmadh: case Phmsbh:
+ line(std::format("{{ Reg128 scratch; mmi::phmadh(c, {}, {}, {}, {}); }}", dest_reg(i), reg(rs), reg(rt), i.op == Phmsbh ? "true" : "false"));
+ break;
+ case Pdivw: line(std::format("mmi::pdivw(c, {}, {});", reg(rs), reg(rt))); break;
+ case Pdivuw: line(std::format("mmi::pdivuw(c, {}, {});", reg(rs), reg(rt))); break;
+ case Pdivbw: line(std::format("mmi::pdivbw(c, {}, {});", reg(rs), reg(rt))); break;
+ case Pmfhi: if (rd) line(std::format("{} = c.hi;", reg(rd))); break;
+ case Pmflo: if (rd) line(std::format("{} = c.lo;", reg(rd))); break;
+ case Pmthi: line(std::format("c.hi = {};", reg(rs))); break;
+ case Pmtlo: line(std::format("c.lo = {};", reg(rs))); break;
+ case Pinth: mmi3("pinth", i); break;
+ case Pinteh: mmi3("pinteh", i); break;
+ case Pcpyld: mmi3("pcpyld", i); break;
+ case Pcpyud: mmi3("pcpyud", i); break;
+ case Pand: mmi3("pand", i); break;
+ case Pxor: mmi3("pxor", i); break;
+ case Por: mmi3("por", i); break;
+ case Pnor: mmi3("pnor", i); break;
+ case Pexeh: mmi2("pexeh", i); break;
+ case Prevh: mmi2("prevh", i); break;
+ case Pexew: mmi2("pexew", i); break;
+ case Prot3w: mmi2("prot3w", i); break;
+ case Pexch: mmi2("pexch", i); break;
+ case Pcpyh: mmi2("pcpyh", i); break;
+ case Pexcw: mmi2("pexcw", i); break;
+
+ case Mfc0: set_w(rt, std::format("mfc0(c, {}u, {}u)", rd, i.word & 7u)); break;
+ case Mtc0: line(std::format("mtc0(c, {}u, {});", rd, w(rt))); break;
+ case Ei: line("set_interrupts(c, true);"); break;
+ case Di: line("set_interrupts(c, false);"); break;
+ case Tlbr: case Tlbwi: case Tlbwr: case Tlbp:
+ line(std::format("stop(c, {}, \"{}\");", hex(i.pc), name(i.op)));
+ break;
+
+ case Mfc1: set_w(rt, f(i.fs())); break;
+ case Mtc1: line(std::format("{} = {};", f(i.fs()), w(rt))); break;
+ case Cfc1: set_w(rt, std::format("cfc1(c, {}u)", i.fs())); break;
+ case Ctc1: line(std::format("ctc1(c, {}u, {});", i.fs(), w(rt))); break;
+ case AddS: line(std::format("{} = fp::add({}, {});", f(i.fd()), f(i.fs()), f(i.ft()))); break;
+ case SubS: line(std::format("{} = fp::sub({}, {});", f(i.fd()), f(i.fs()), f(i.ft()))); break;
+ case MulS: line(std::format("{} = fp::mul({}, {});", f(i.fd()), f(i.fs()), f(i.ft()))); break;
+ case DivS: line(std::format("fpu_div(c, {}u, {}, {});", i.fd(), f(i.fs()), f(i.ft()))); break;
+ case SqrtS: line(std::format("{} = fp::sqrt({});", f(i.fd()), f(i.ft()))); break;
+ case AbsS: line(std::format("{} = {} & fp::kAbs;", f(i.fd()), f(i.fs()))); break;
+ case MovS: line(std::format("{} = {};", f(i.fd()), f(i.fs()))); break;
+ case NegS: line(std::format("{} = {} ^ fp::kSign;", f(i.fd()), f(i.fs()))); break;
+ case RsqrtS: line(std::format("fpu_rsqrt(c, {}u, {}, {});", i.fd(), f(i.fs()), f(i.ft()))); break;
+ case AddaS: line(std::format("c.facc = fp::add({}, {});", f(i.fs()), f(i.ft()))); break;
+ case SubaS: line(std::format("c.facc = fp::sub({}, {});", f(i.fs()), f(i.ft()))); break;
+ case MulaS: line(std::format("c.facc = fp::mul({}, {});", f(i.fs()), f(i.ft()))); break;
+ case MaddS: line(std::format("{} = fp::add(c.facc, fp::mul({}, {}));", f(i.fd()), f(i.fs()), f(i.ft()))); break;
+ case MsubS: line(std::format("{} = fp::sub(c.facc, fp::mul({}, {}));", f(i.fd()), f(i.fs()), f(i.ft()))); break;
+ case MaddaS: line(std::format("c.facc = fp::add(c.facc, fp::mul({}, {}));", f(i.fs()), f(i.ft()))); break;
+ case MsubaS: line(std::format("c.facc = fp::sub(c.facc, fp::mul({}, {}));", f(i.fs()), f(i.ft()))); break;
+ case CvtWS: line(std::format("{} = fp::cvt_w_s({});", f(i.fd()), f(i.fs()))); break;
+ case CvtSW: line(std::format("{} = fp::cvt_s_w({});", f(i.fd()), f(i.fs()))); break;
+ case MaxS: line(std::format("{} = fp::max({}, {});", f(i.fd()), f(i.fs()), f(i.ft()))); break;
+ case MinS: line(std::format("{} = fp::min({}, {});", f(i.fd()), f(i.fs()), f(i.ft()))); break;
+ case CF: line("fpu_compare(c, false);"); break;
+ case CEq: line(std::format("fpu_compare(c, fp::key({}) == fp::key({}));", f(i.fs()), f(i.ft()))); break;
+ case CLt: line(std::format("fpu_compare(c, fp::key({}) < fp::key({}));", f(i.fs()), f(i.ft()))); break;
+ case CLe: line(std::format("fpu_compare(c, fp::key({}) <= fp::key({}));", f(i.fs()), f(i.ft()))); break;
+
+ case Qmfc2:
+ if (i.word & 1u) line("vu0_wait(c);");
+ if (rt) line(std::format("{} = {};", reg(rt), vf(i.fs())));
+ break;
+ case Qmtc2:
+ if (i.word & 1u) line("vu0_wait(c);");
+ if (i.fs()) line(std::format("{} = {};", vf(i.fs()), reg(rt)));
+ break;
+ case Cfc2:
+ if (i.fs() < 16) set_w(rt, vi(i.fs()));
+ else set_w(rt, std::format("cfc2(c, {}u)", i.fs()));
+ break;
+ case Ctc2:
+ if (i.fs() < 16) line(std::format("vu::set_vi(c.vu0, {}u, {});", i.fs(), w(rt)));
+ else line(std::format("ctc2(c, {}u, {});", i.fs(), w(rt)));
+ break;
+ case Vaddbc: vecbc("addbc", i, bcv(i)); break;
+ case Vsubbc: vecbc("subbc", i, bcv(i)); break;
+ case Vmaddbc: vecbc("maddbc", i, bcv(i)); break;
+ case Vmsubbc: vecbc("msubbc", i, bcv(i)); break;
+ case Vmaxbc: vecbc("maxbc", i, bcv(i)); break;
+ case Vminibc: vecbc("minibc", i, bcv(i)); break;
+ case Vmulbc: vecbc("mulbc", i, bcv(i)); break;
+ case Vmulq: vecbc("mulbc", i, "c.vu0.q"); break;
+ case Vmaxi: vecbc("maxbc", i, "c.vu0.i"); break;
+ case Vmuli: vecbc("mulbc", i, "c.vu0.i"); break;
+ case Vminii: vecbc("minibc", i, "c.vu0.i"); break;
+ case Vaddq: vecbc("addbc", i, "c.vu0.q"); break;
+ case Vmaddq: vecbc("maddbc", i, "c.vu0.q"); break;
+ case Vaddi: vecbc("addbc", i, "c.vu0.i"); break;
+ case Vmaddi: vecbc("maddbc", i, "c.vu0.i"); break;
+ case Vsubq: vecbc("subbc", i, "c.vu0.q"); break;
+ case Vmsubq: vecbc("msubbc", i, "c.vu0.q"); break;
+ case Vsubi: vecbc("subbc", i, "c.vu0.i"); break;
+ case Vmsubi: vecbc("msubbc", i, "c.vu0.i"); break;
+ case Vadd: vec("add", i); break;
+ case Vmadd: vec("madd", i); break;
+ case Vmul: vec("mul", i); break;
+ case Vmax: vec("max", i); break;
+ case Vsub: vec("sub", i); break;
+ case Vmsub: vec("msub", i); break;
+ case Vmini: vec("mini", i); break;
+ case Vopmsub: line(std::format("vu::opmsub(c.vu0, {}u, {}u, {}u);", i.fd(), i.fs(), i.ft())); break;
+ case Viadd: line(std::format("vu::set_vi(c.vu0, {}u, {} + {});", i.fd(), vi(i.fs()), vi(i.ft()))); break;
+ case Visub: line(std::format("vu::set_vi(c.vu0, {}u, {} - {});", i.fd(), vi(i.fs()), vi(i.ft()))); break;
+ case Viaddi: line(std::format("vu::set_vi(c.vu0, {}u, {} + {});", i.ft(), vi(i.fs()), i.imm5())); break;
+ case Viand: line(std::format("vu::set_vi(c.vu0, {}u, {} & {});", i.fd(), vi(i.fs()), vi(i.ft()))); break;
+ case Vior: line(std::format("vu::set_vi(c.vu0, {}u, {} | {});", i.fd(), vi(i.fs()), vi(i.ft()))); break;
+ case Vcallms: line(std::format("vcallms(c, {}u);", i.imm15() * 8u)); break;
+ case Vcallmsr: line("vcallms(c, c.vu0.cmsar0 * 8u);"); break;
+ case Vaddabc: vaccbc("addabc", i, bcv(i)); break;
+ case Vsubabc: vaccbc("subabc", i, bcv(i)); break;
+ case Vmaddabc: vaccbc("maddabc", i, bcv(i)); break;
+ case Vmsubabc: vaccbc("msubabc", i, bcv(i)); break;
+ case Vmulabc: vaccbc("mulabc", i, bcv(i)); break;
+ case Vmulaq: vaccbc("mulabc", i, "c.vu0.q"); break;
+ case Vmulai: vaccbc("mulabc", i, "c.vu0.i"); break;
+ case Vaddaq: vaccbc("addabc", i, "c.vu0.q"); break;
+ case Vmaddaq: vaccbc("maddabc", i, "c.vu0.q"); break;
+ case Vaddai: vaccbc("addabc", i, "c.vu0.i"); break;
+ case Vmaddai: vaccbc("maddabc", i, "c.vu0.i"); break;
+ case Vsubaq: vaccbc("subabc", i, "c.vu0.q"); break;
+ case Vmsubaq: vaccbc("msubabc", i, "c.vu0.q"); break;
+ case Vsubai: vaccbc("subabc", i, "c.vu0.i"); break;
+ case Vmsubai: vaccbc("msubabc", i, "c.vu0.i"); break;
+ case Vadda: vacc("adda", i); break;
+ case Vmadda: vacc("madda", i); break;
+ case Vmula: vacc("mula", i); break;
+ case Vsuba: vacc("suba", i); break;
+ case Vmsuba: vacc("msuba", i); break;
+ case Vopmula: line(std::format("vu::opmula(c.vu0, {}u, {}u);", i.fs(), i.ft())); break;
+ case Vitof0: case Vitof4: case Vitof12: case Vitof15: {
+ const int frac = i.op == Vitof0 ? 0 : i.op == Vitof4 ? 4 : i.op == Vitof12 ? 12 : 15;
+ line(std::format("vu::itof(c.vu0, {}u, {}u, {}u, {});", i.dest(), i.ft(), i.fs(), frac));
+ break;
+ }
+ case Vftoi0: case Vftoi4: case Vftoi12: case Vftoi15: {
+ const int frac = i.op == Vftoi0 ? 0 : i.op == Vftoi4 ? 4 : i.op == Vftoi12 ? 12 : 15;
+ line(std::format("vu::ftoi(c.vu0, {}u, {}u, {}u, {});", i.dest(), i.ft(), i.fs(), frac));
+ break;
+ }
+ case Vabs: line(std::format("vu::abs(c.vu0, {}u, {}u, {}u);", i.dest(), i.ft(), i.fs())); break;
+ case Vmove: line(std::format("vu::move(c.vu0, {}u, {}u, {}u);", i.dest(), i.ft(), i.fs())); break;
+ case Vmr32: line(std::format("vu::mr32(c.vu0, {}u, {}u, {}u);", i.dest(), i.ft(), i.fs())); break;
+ case Vclip: line(std::format("vu::clip(c.vu0, {}u, {}u);", i.fs(), i.ft())); break;
+ case Vnop: case Vwaitq: break;
+ case Vlqi: line(std::format("lqc2_vu(c, {}u, {}u, {}); vu::set_vi(c.vu0, {}u, {} + 1u);", i.dest(), i.ft(), vi(i.fs()), i.fs(), vi(i.fs()))); break;
+ case Vsqi: line(std::format("sqc2_vu(c, {}u, {}u, {}); vu::set_vi(c.vu0, {}u, {} + 1u);", i.dest(), i.fs(), vi(i.ft()), i.ft(), vi(i.ft()))); break;
+ case Vlqd: line(std::format("vu::set_vi(c.vu0, {}u, {} - 1u); lqc2_vu(c, {}u, {}u, {});", i.fs(), vi(i.fs()), i.dest(), i.ft(), vi(i.fs()))); break;
+ case Vsqd: line(std::format("vu::set_vi(c.vu0, {}u, {} - 1u); sqc2_vu(c, {}u, {}u, {});", i.ft(), vi(i.ft()), i.dest(), i.fs(), vi(i.ft()))); break;
+ case Vdiv: line(std::format("vu::div(c.vu0, {}.w[{}], {}.w[{}]);", vf(i.fs()), fsf, vf(i.ft()), ftf)); break;
+ case Vsqrt: line(std::format("vu::sqrt(c.vu0, {}.w[{}]);", vf(i.ft()), ftf)); break;
+ case Vrsqrt: line(std::format("vu::rsqrt(c.vu0, {}.w[{}], {}.w[{}]);", vf(i.fs()), fsf, vf(i.ft()), ftf)); break;
+ case Vmtir: line(std::format("vu::set_vi(c.vu0, {}u, {}.w[{}]);", i.ft(), vf(i.fs()), fsf)); break;
+ case Vmfir: line(std::format("vu::mfir(c.vu0, {}u, {}u, {}u);", i.dest(), i.ft(), i.fs())); break;
+ case Vilwr:
+ line(std::format("{{ std::uint16_t v; __builtin_memcpy(&v, vu0_data() + vu0_addr({}) + 4u * {}u, 2); vu::set_vi(c.vu0, {}u, v); }}",
+ vi(i.fs()), std::countl_zero(i.dest() << 28), i.ft()));
+ break;
+ case Viswr:
+ line(std::format("for (int k = 0; k < 4; ++k) if (vu::lane({}u, k)) {{ const std::uint32_t v = {}; __builtin_memcpy(vu0_data() + vu0_addr({}) + 4u * k, &v, 4); }}",
+ i.dest(), vi(i.ft()), vi(i.fs())));
+ break;
+ case Vrnext:
+ line(std::format("c.vu0.r = vu::rand_next(c.vu0.r); vu::apply(c.vu0, {}u, {}u, [&](int) {{ return c.vu0.r | fp::kOne; }});", i.dest(), i.ft()));
+ break;
+ case Vrget: line(std::format("vu::apply(c.vu0, {}u, {}u, [&](int) {{ return c.vu0.r | fp::kOne; }});", i.dest(), i.ft())); break;
+ case Vrinit: line(std::format("c.vu0.r = {}.w[{}] & 0x7FFFFFu;", vf(i.fs()), fsf)); break;
+ case Vrxor: line(std::format("c.vu0.r = (c.vu0.r ^ {}.w[{}]) & 0x7FFFFFu;", vf(i.fs()), fsf)); break;
+
+ default:
+ line(std::format("stop(c, {}, \"{}\"); return;", hex(i.pc), name(i.op)));
+ break;
+ }
+ }
+
+ const Image &image_;
+ Program &p_;
+ const CodegenOptions &options_;
+ Layout layout_;
+ std::vector<std::map<std::uint32_t, std::uint16_t>> ids_;
+ std::vector<bool> has_code_;
+ std::uint32_t base_{};
+ std::set<std::uint32_t> labels_;
+ std::string *capture_{};
+ std::uint32_t pending_{};
+ std::ostringstream out_;
+};
+
+}
+
+void generate(const Image &image, Program &program, const CodegenOptions &options) {
+ Generator(image, program, options).run();
+}
+
+}
diff --git a/recomp/codegen.hpp b/recomp/codegen.hpp
new file mode 100644
index 0000000..ffdffed
--- /dev/null
+++ b/recomp/codegen.hpp
@@ -0,0 +1,18 @@
+#pragma once
+
+#include "analysis.hpp"
+#include "elf.hpp"
+
+#include <cstdint>
+#include <string>
+
+namespace recomp {
+
+struct CodegenOptions {
+ std::string output;
+ std::uint32_t unit_bits = 14;
+};
+
+void generate(const Image &image, Program &program, const CodegenOptions &options);
+
+}
diff --git a/recomp/main.cpp b/recomp/main.cpp
new file mode 100644
index 0000000..0b44b59
--- /dev/null
+++ b/recomp/main.cpp
@@ -0,0 +1,66 @@
+#include "analysis.hpp"
+#include "codegen.hpp"
+#include "elf.hpp"
+
+#include <cstdio>
+#include <cstdlib>
+#include <fstream>
+#include <map>
+#include <string>
+#include <vector>
+
+using namespace recomp;
+
+namespace {
+
+std::vector<std::uint32_t> read_entries(const std::string &path) {
+ std::vector<std::uint32_t> entries;
+ std::ifstream file(path);
+ std::string token;
+ while (file >> token) {
+ if (token[0] == '#') { std::getline(file, token); continue; }
+ entries.push_back(static_cast<std::uint32_t>(std::stoul(token, nullptr, 16)));
+ }
+ return entries;
+}
+
+int usage() {
+ std::fprintf(stderr, "usage: eerecomp <elf> [--entries file] [--out dir] [--stats]\n");
+ return 2;
+}
+
+}
+
+int main(int argc, char **argv) {
+ if (argc < 2) return usage();
+ std::string elf_path = argv[1], entries_path, out;
+ bool stats = false;
+ for (int i = 2; i < argc; ++i) {
+ const std::string arg = argv[i];
+ if (arg == "--entries" && i + 1 < argc) entries_path = argv[++i];
+ else if (arg == "--stats") stats = true;
+ else if (arg == "--out" && i + 1 < argc) out = argv[++i];
+ else return usage();
+ }
+
+ try {
+ const Image image = Image::load(elf_path);
+ Program program = analyse(image, entries_path.empty() ? std::vector<std::uint32_t>{} : read_entries(entries_path));
+ std::size_t code = 0;
+ std::map<Op, std::size_t> histogram;
+ for (std::uint32_t pc = program.base; pc < program.end; pc += 4) {
+ if (!program.is_code(pc)) continue;
+ ++code;
+ ++histogram[decode(*image.word(pc), pc).op];
+ }
+ std::printf("code %zu instructions (%zu KiB), functions %zu, entries %zu, rejected seeds %zu\n",
+ code, code * 4 / 1024, program.functions.size(), program.entries.size(), program.rejected.size());
+ if (stats)
+ for (const auto &[op, count] : histogram) std::printf("%10zu %s\n", count, name(op));
+ if (!out.empty()) generate(image, program, CodegenOptions{out});
+ } catch (const std::exception &error) {
+ std::fprintf(stderr, "eerecomp: %s\n", error.what());
+ return 1;
+ }
+ return 0;
+}
diff --git a/runtime/include/ps2/cpu.hpp b/runtime/include/ps2/cpu.hpp
new file mode 100644
index 0000000..80d3476
--- /dev/null
+++ b/runtime/include/ps2/cpu.hpp
@@ -0,0 +1,73 @@
+#pragma once
+
+#include <cstdint>
+
+namespace ps2 {
+
+using u8x16 = std::uint8_t __attribute__((vector_size(16)));
+using s8x16 = std::int8_t __attribute__((vector_size(16)));
+using u16x8 = std::uint16_t __attribute__((vector_size(16)));
+using s16x8 = std::int16_t __attribute__((vector_size(16)));
+using u32x4 = std::uint32_t __attribute__((vector_size(16)));
+using s32x4 = std::int32_t __attribute__((vector_size(16)));
+using u64x2 = std::uint64_t __attribute__((vector_size(16)));
+using s64x2 = std::int64_t __attribute__((vector_size(16)));
+using f32x4 = float __attribute__((vector_size(16)));
+
+union alignas(16) Reg128 {
+ std::uint64_t d[2];
+ std::int64_t sd[2];
+ std::uint32_t w[4];
+ std::int32_t sw[4];
+ std::uint16_t h[8];
+ std::int16_t sh[8];
+ std::uint8_t b[16];
+ std::int8_t sb[16];
+ float f[4];
+ u8x16 ub16;
+ s8x16 sb16;
+ u16x8 uh8;
+ s16x8 sh8;
+ u32x4 uw4;
+ s32x4 sw4;
+ u64x2 ud2;
+ s64x2 sd2;
+ f32x4 f4;
+};
+
+struct Vu0Regs {
+ Reg128 vf[32];
+ Reg128 acc;
+ std::uint16_t vi[16];
+ std::uint32_t q{};
+ std::uint32_t p{};
+ std::uint32_t i{};
+ std::uint32_t r{};
+ std::uint32_t mac{};
+ std::uint32_t status{};
+ std::uint32_t clip{};
+ std::uint32_t cmsar0{};
+ std::uint32_t tpc{};
+};
+
+struct Cpu {
+ Reg128 r[32];
+ Reg128 hi;
+ Reg128 lo;
+ std::uint32_t sa{};
+ std::uint32_t pc{};
+ std::int32_t budget{};
+ std::uint32_t pad0{};
+
+ std::uint32_t f[32];
+ std::uint32_t facc{};
+ std::uint32_t fcr31{};
+
+ Vu0Regs vu0;
+ std::uint32_t cop0[32];
+};
+
+inline constexpr std::uint32_t kUnwind = 1u;
+inline constexpr std::uint32_t kFpuCondition = 1u << 23;
+
+}
diff --git a/runtime/include/ps2/fpu.hpp b/runtime/include/ps2/fpu.hpp
new file mode 100644
index 0000000..faf3ebe
--- /dev/null
+++ b/runtime/include/ps2/fpu.hpp
@@ -0,0 +1,142 @@
+#pragma once
+
+#include <bit>
+#include <cmath>
+#include <cstdint>
+#include <utility>
+
+namespace ps2::fp {
+
+inline constexpr std::uint32_t kSign = 0x80000000u;
+inline constexpr std::uint32_t kAbs = 0x7FFFFFFFu;
+inline constexpr std::uint32_t kOne = 0x3F800000u;
+
+inline std::uint32_t exponent(std::uint32_t x) { return (x >> 23) & 0xFFu; }
+inline std::uint32_t flush(std::uint32_t x) { return exponent(x) ? x : (x & kSign); }
+inline std::uint32_t mantissa(std::uint32_t x) { return (x & 0x7FFFFFu) | 0x800000u; }
+
+inline std::uint32_t pack(std::uint32_t sign, int e, std::uint32_t m) {
+ if (e > 255) return sign | kAbs;
+ if (e <= 0) return sign;
+ return sign | (static_cast<std::uint32_t>(e) << 23) | (m & 0x7FFFFFu);
+}
+
+inline std::uint32_t add(std::uint32_t a, std::uint32_t b) {
+ a = flush(a);
+ b = flush(b);
+ std::uint32_t aa = a & kAbs, ba = b & kAbs;
+ if (!ba) return aa ? a : (a & b);
+ if (!aa) return b;
+ if (ba > aa) {
+ std::swap(a, b);
+ std::swap(aa, ba);
+ }
+ int e = static_cast<int>(aa >> 23);
+ const int diff = e - static_cast<int>(ba >> 23);
+ std::uint32_t ma = mantissa(aa) << 1;
+ const std::uint32_t mb = diff > 25 ? 0u : (mantissa(ba) << 1) >> diff;
+ if ((a ^ b) & kSign) {
+ ma -= mb;
+ if (!ma) return 0;
+ const int shift = std::countl_zero(ma) - 7;
+ ma <<= shift;
+ e -= shift;
+ } else {
+ ma += mb;
+ if (ma & 0x2000000u) {
+ ma >>= 1;
+ ++e;
+ }
+ }
+ return pack(a & kSign, e, ma >> 1);
+}
+
+inline std::uint32_t sub(std::uint32_t a, std::uint32_t b) { return add(a, b ^ kSign); }
+
+inline std::uint32_t mul(std::uint32_t a, std::uint32_t b) {
+ const std::uint32_t sign = (a ^ b) & kSign;
+ const int ea = static_cast<int>(exponent(a)), eb = static_cast<int>(exponent(b));
+ if (!ea || !eb) return sign;
+ std::uint64_t m = static_cast<std::uint64_t>(mantissa(a)) * mantissa(b);
+ int e = ea + eb - 127;
+ if (m >> 47) {
+ m >>= 24;
+ ++e;
+ } else {
+ m >>= 23;
+ }
+ return pack(sign, e, static_cast<std::uint32_t>(m));
+}
+
+inline double to_double(std::uint32_t x) {
+ if (!exponent(x)) return (x & kSign) ? -0.0 : 0.0;
+ const std::uint64_t bits = (static_cast<std::uint64_t>(x & kSign) << 32) |
+ (static_cast<std::uint64_t>(exponent(x) + 1023 - 127) << 52) |
+ (static_cast<std::uint64_t>(x & 0x7FFFFFu) << 29);
+ return std::bit_cast<double>(bits);
+}
+
+inline std::uint32_t from_double(double d) {
+ const std::uint64_t bits = std::bit_cast<std::uint64_t>(d);
+ const std::uint32_t sign = static_cast<std::uint32_t>(bits >> 32) & kSign;
+ const int e = static_cast<int>((bits >> 52) & 0x7FFu);
+ if (!e) return sign;
+ return pack(sign, e - 1023 + 127, static_cast<std::uint32_t>(bits >> 29));
+}
+
+struct Result {
+ std::uint32_t value;
+ bool invalid;
+};
+
+inline Result div(std::uint32_t a, std::uint32_t b) {
+ a = flush(a);
+ b = flush(b);
+ const std::uint32_t sign = (a ^ b) & kSign;
+ if (!(b & kAbs)) return {sign | kAbs, true};
+ if (!(a & kAbs)) return {sign, false};
+ return {from_double(to_double(a) / to_double(b)), false};
+}
+
+inline std::uint32_t sqrt(std::uint32_t a) {
+ a = flush(a) & kAbs;
+ if (!a) return 0;
+ return from_double(std::sqrt(to_double(a)));
+}
+
+inline Result rsqrt(std::uint32_t a, std::uint32_t b) {
+ a = flush(a);
+ b = flush(b) & kAbs;
+ if (!b) return {(a & kSign) | kAbs, true};
+ if (!(a & kAbs)) return {a & kSign, false};
+ return {from_double(to_double(a) / std::sqrt(to_double(b))), false};
+}
+
+inline std::int32_t key(std::uint32_t x) {
+ x = flush(x);
+ const auto magnitude = static_cast<std::int32_t>(x & kAbs);
+ return (x & kSign) ? -magnitude : magnitude;
+}
+
+inline std::uint32_t max(std::uint32_t a, std::uint32_t b) { return key(a) >= key(b) ? a : b; }
+inline std::uint32_t min(std::uint32_t a, std::uint32_t b) { return key(a) <= key(b) ? a : b; }
+
+inline std::uint32_t to_fixed(std::uint32_t x, int fraction) {
+ const int e = static_cast<int>(exponent(x)) + fraction;
+ if (e <= 126 || !exponent(x)) return 0;
+ const bool negative = x & kSign;
+ if (e >= 158) return negative ? 0x80000000u : 0x7FFFFFFFu;
+ const int shift = e - 150;
+ const std::uint32_t m = mantissa(x);
+ const std::uint32_t v = shift >= 0 ? m << shift : m >> -shift;
+ return negative ? 0u - v : v;
+}
+
+inline std::uint32_t from_fixed(std::int32_t v, int fraction) {
+ return from_double(static_cast<double>(v) / static_cast<double>(1u << fraction));
+}
+
+inline std::uint32_t cvt_w_s(std::uint32_t x) { return to_fixed(x, 0); }
+inline std::uint32_t cvt_s_w(std::uint32_t v) { return from_fixed(static_cast<std::int32_t>(v), 0); }
+
+}
diff --git a/runtime/include/ps2/generated.hpp b/runtime/include/ps2/generated.hpp
new file mode 100644
index 0000000..f41e74d
--- /dev/null
+++ b/runtime/include/ps2/generated.hpp
@@ -0,0 +1,83 @@
+#pragma once
+
+#include "cpu.hpp"
+#include "fpu.hpp"
+#include "memory.hpp"
+#include "mmi.hpp"
+#include "vu.hpp"
+
+#include <cstdint>
+
+namespace ps2 {
+
+using UnitFn = void (*)(Cpu &, std::uint32_t entry, std::uint32_t depth);
+
+inline constexpr std::uint32_t kMaxDepth = 192;
+
+struct UnitInfo {
+ std::uint32_t base;
+ std::uint32_t size;
+ UnitFn function;
+ const std::uint16_t *entries;
+};
+
+void register_units(const UnitInfo *units, std::uint32_t count);
+
+void dispatch(Cpu &c, std::uint32_t entry, std::uint32_t depth);
+void call(Cpu &c, std::uint32_t target, std::uint32_t depth);
+void bad_entry(Cpu &c, std::uint32_t unit_base);
+
+void syscall(Cpu &c, std::uint32_t pc);
+void breakpoint(Cpu &c, std::uint32_t pc);
+void trap(Cpu &c, std::uint32_t pc);
+void stop(Cpu &c, std::uint32_t pc, const char *what);
+
+std::uint32_t mfc0(Cpu &c, std::uint32_t reg, std::uint32_t sel);
+void mtc0(Cpu &c, std::uint32_t reg, std::uint32_t value);
+void set_interrupts(Cpu &c, bool enabled);
+bool cpcond0(Cpu &c);
+
+std::uint32_t cfc1(Cpu &c, std::uint32_t reg);
+void ctc1(Cpu &c, std::uint32_t reg, std::uint32_t value);
+
+std::uint32_t cfc2(Cpu &c, std::uint32_t reg);
+void ctc2(Cpu &c, std::uint32_t reg, std::uint32_t value);
+void vcallms(Cpu &c, std::uint32_t address);
+bool vu0_busy(Cpu &c);
+void vu0_wait(Cpu &c);
+std::uint8_t *vu0_data();
+
+inline void set32(Reg128 &r, std::uint32_t v) { r.sd[0] = static_cast<std::int32_t>(v); }
+inline void set64(Reg128 &r, std::uint64_t v) { r.d[0] = v; }
+
+inline void fpu_compare(Cpu &c, bool condition) {
+ c.fcr31 = condition ? (c.fcr31 | kFpuCondition) : (c.fcr31 & ~kFpuCondition);
+}
+
+inline void fpu_div(Cpu &c, unsigned fd, std::uint32_t a, std::uint32_t b) {
+ const fp::Result r = fp::div(a, b);
+ c.f[fd] = r.value;
+ if (r.invalid) c.fcr31 |= 0x30000u;
+}
+
+inline void fpu_rsqrt(Cpu &c, unsigned fd, std::uint32_t a, std::uint32_t b) {
+ const fp::Result r = fp::rsqrt(a, b);
+ c.f[fd] = r.value;
+ if (r.invalid) c.fcr31 |= 0x30000u;
+}
+
+inline std::uint32_t vu0_addr(std::uint32_t vi) { return (vi & 0xFFu) * 16u; }
+
+inline void lqc2_vu(Cpu &c, unsigned dest, unsigned ft, std::uint32_t vi) {
+ Reg128 data;
+ __builtin_memcpy(&data, vu0_data() + vu0_addr(vi), 16);
+ vu::apply(c.vu0, dest, ft, [&](int i) { return data.w[i]; });
+}
+
+inline void sqc2_vu(Cpu &c, unsigned dest, unsigned fs, std::uint32_t vi) {
+ std::uint8_t *p = vu0_data() + vu0_addr(vi);
+ for (int i = 0; i < 4; ++i)
+ if (vu::lane(dest, i)) __builtin_memcpy(p + i * 4, &c.vu0.vf[fs].w[i], 4);
+}
+
+}
diff --git a/runtime/include/ps2/memory.hpp b/runtime/include/ps2/memory.hpp
new file mode 100644
index 0000000..56f61d0
--- /dev/null
+++ b/runtime/include/ps2/memory.hpp
@@ -0,0 +1,148 @@
+#pragma once
+
+#include "cpu.hpp"
+
+#include <cstdint>
+#include <cstring>
+
+namespace ps2 {
+
+inline constexpr std::uint32_t kRamSize = 32u << 20;
+inline constexpr std::uint32_t kScratchpadBase = 0x70000000u;
+inline constexpr std::uint32_t kScratchpadSize = 16u << 10;
+
+extern std::uint8_t g_ram[];
+extern std::uint8_t g_spr[];
+
+std::uint8_t slow_ld8(std::uint32_t a);
+std::uint16_t slow_ld16(std::uint32_t a);
+std::uint32_t slow_ld32(std::uint32_t a);
+std::uint64_t slow_ld64(std::uint32_t a);
+void slow_ld128(std::uint32_t a, Reg128 &out);
+void slow_st8(std::uint32_t a, std::uint8_t v);
+void slow_st16(std::uint32_t a, std::uint16_t v);
+void slow_st32(std::uint32_t a, std::uint32_t v);
+void slow_st64(std::uint32_t a, std::uint64_t v);
+void slow_st128(std::uint32_t a, const Reg128 &v);
+
+template <typename T>
+inline T *fast(std::uint32_t a) {
+ if (a < kRamSize) [[likely]] return reinterpret_cast<T *>(g_ram + a);
+ if ((a >> 14) == (kScratchpadBase >> 14)) return reinterpret_cast<T *>(g_spr + (a & (kScratchpadSize - 1)));
+ return nullptr;
+}
+
+template <typename T>
+inline T load(const void *p) {
+ T v;
+ std::memcpy(&v, p, sizeof v);
+ return v;
+}
+
+template <typename T>
+inline void store(void *p, T v) { std::memcpy(p, &v, sizeof v); }
+
+inline std::uint8_t ld8(std::uint32_t a) {
+ if (auto *p = fast<std::uint8_t>(a)) return *p;
+ return slow_ld8(a);
+}
+
+inline std::uint16_t ld16(std::uint32_t a) {
+ if (auto *p = fast<std::uint8_t>(a)) return load<std::uint16_t>(p);
+ return slow_ld16(a);
+}
+
+inline std::uint32_t ld32(std::uint32_t a) {
+ if (auto *p = fast<std::uint8_t>(a)) return load<std::uint32_t>(p);
+ return slow_ld32(a);
+}
+
+inline std::uint64_t ld64(std::uint32_t a) {
+ if (auto *p = fast<std::uint8_t>(a)) return load<std::uint64_t>(p);
+ return slow_ld64(a);
+}
+
+inline void ld128(std::uint32_t a, Reg128 &out) {
+ if (auto *p = fast<std::uint8_t>(a)) { std::memcpy(&out, p, 16); return; }
+ slow_ld128(a, out);
+}
+
+inline void st8(std::uint32_t a, std::uint8_t v) {
+ if (auto *p = fast<std::uint8_t>(a)) { *p = v; return; }
+ slow_st8(a, v);
+}
+
+inline void st16(std::uint32_t a, std::uint16_t v) {
+ if (auto *p = fast<std::uint8_t>(a)) { store(p, v); return; }
+ slow_st16(a, v);
+}
+
+inline void st32(std::uint32_t a, std::uint32_t v) {
+ if (auto *p = fast<std::uint8_t>(a)) { store(p, v); return; }
+ slow_st32(a, v);
+}
+
+inline void st64(std::uint32_t a, std::uint64_t v) {
+ if (auto *p = fast<std::uint8_t>(a)) { store(p, v); return; }
+ slow_st64(a, v);
+}
+
+inline void st128(std::uint32_t a, const Reg128 &v) {
+ if (auto *p = fast<std::uint8_t>(a)) { std::memcpy(p, &v, 16); return; }
+ slow_st128(a, v);
+}
+
+inline std::uint64_t mask_below(std::uint32_t bits) { return bits >= 64 ? ~0ull : (1ull << bits) - 1; }
+
+inline void lwl(Reg128 &rt, std::uint32_t a) {
+ const std::uint32_t shift = (3u - (a & 3u)) * 8u;
+ const std::uint32_t mem = ld32(a & ~3u);
+ const std::uint32_t v = (mem << shift) | (rt.w[0] & static_cast<std::uint32_t>(mask_below(shift)));
+ rt.sd[0] = static_cast<std::int32_t>(v);
+}
+
+inline void lwr(Reg128 &rt, std::uint32_t a) {
+ const std::uint32_t shift = (a & 3u) * 8u;
+ const std::uint32_t mem = ld32(a & ~3u);
+ const std::uint32_t v = (mem >> shift) | (rt.w[0] & ~static_cast<std::uint32_t>(mask_below(32 - shift)));
+ if (shift == 0) rt.sd[0] = static_cast<std::int32_t>(v);
+ else rt.w[0] = v;
+}
+
+inline void ldl(Reg128 &rt, std::uint32_t a) {
+ const std::uint32_t shift = (7u - (a & 7u)) * 8u;
+ const std::uint64_t mem = ld64(a & ~7u);
+ rt.d[0] = (mem << shift) | (rt.d[0] & mask_below(shift));
+}
+
+inline void ldr(Reg128 &rt, std::uint32_t a) {
+ const std::uint32_t shift = (a & 7u) * 8u;
+ const std::uint64_t mem = ld64(a & ~7u);
+ rt.d[0] = (mem >> shift) | (rt.d[0] & ~mask_below(64 - shift));
+}
+
+inline void swl(const Reg128 &rt, std::uint32_t a) {
+ const std::uint32_t shift = (3u - (a & 3u)) * 8u;
+ const std::uint32_t mem = ld32(a & ~3u);
+ st32(a & ~3u, (rt.w[0] >> shift) | (mem & ~static_cast<std::uint32_t>(mask_below(32 - shift))));
+}
+
+inline void swr(const Reg128 &rt, std::uint32_t a) {
+ const std::uint32_t shift = (a & 3u) * 8u;
+ const std::uint32_t mem = ld32(a & ~3u);
+ st32(a & ~3u, (rt.w[0] << shift) | (mem & static_cast<std::uint32_t>(mask_below(shift))));
+}
+
+inline void sdl(const Reg128 &rt, std::uint32_t a) {
+ const std::uint32_t shift = (7u - (a & 7u)) * 8u;
+ const std::uint64_t mem = ld64(a & ~7u);
+ st64(a & ~7u, (rt.d[0] >> shift) | (mem & ~mask_below(64 - shift)));
+}
+
+inline void sdr(const Reg128 &rt, std::uint32_t a) {
+ const std::uint32_t shift = (a & 7u) * 8u;
+ const std::uint64_t mem = ld64(a & ~7u);
+ st64(a & ~7u, (rt.d[0] << shift) | (mem & mask_below(shift)));
+}
+
+}
diff --git a/runtime/include/ps2/mmi.hpp b/runtime/include/ps2/mmi.hpp
new file mode 100644
index 0000000..b680549
--- /dev/null
+++ b/runtime/include/ps2/mmi.hpp
@@ -0,0 +1,354 @@
+#pragma once
+
+#include "cpu.hpp"
+
+#include <bit>
+#include <cstdint>
+#include <limits>
+
+namespace ps2::mmi {
+
+template <typename T>
+inline T sat_add(T a, T b) {
+ using W = std::conditional_t<std::is_signed_v<T>, std::int64_t, std::uint64_t>;
+ const W r = static_cast<W>(a) + static_cast<W>(b);
+ if (r > static_cast<W>(std::numeric_limits<T>::max())) return std::numeric_limits<T>::max();
+ if constexpr (std::is_signed_v<T>)
+ if (r < static_cast<W>(std::numeric_limits<T>::min())) return std::numeric_limits<T>::min();
+ return static_cast<T>(r);
+}
+
+template <typename T>
+inline T sat_sub(T a, T b) {
+ if constexpr (std::is_signed_v<T>) {
+ const std::int64_t r = static_cast<std::int64_t>(a) - static_cast<std::int64_t>(b);
+ if (r > std::numeric_limits<T>::max()) return std::numeric_limits<T>::max();
+ if (r < std::numeric_limits<T>::min()) return std::numeric_limits<T>::min();
+ return static_cast<T>(r);
+ } else {
+ return a > b ? static_cast<T>(a - b) : T{0};
+ }
+}
+
+#define PS2_LANES(name, field, count, expr) \
+ inline void name(Reg128 &d, const Reg128 &s, const Reg128 &t) { \
+ Reg128 r; \
+ for (int i = 0; i < count; ++i) r.field[i] = (expr); \
+ d = r; \
+ }
+
+PS2_LANES(paddw, w, 4, s.w[i] + t.w[i])
+PS2_LANES(psubw, w, 4, s.w[i] - t.w[i])
+PS2_LANES(paddh, h, 8, static_cast<std::uint16_t>(s.h[i] + t.h[i]))
+PS2_LANES(psubh, h, 8, static_cast<std::uint16_t>(s.h[i] - t.h[i]))
+PS2_LANES(paddb, b, 16, static_cast<std::uint8_t>(s.b[i] + t.b[i]))
+PS2_LANES(psubb, b, 16, static_cast<std::uint8_t>(s.b[i] - t.b[i]))
+PS2_LANES(paddsw, sw, 4, sat_add(s.sw[i], t.sw[i]))
+PS2_LANES(psubsw, sw, 4, sat_sub(s.sw[i], t.sw[i]))
+PS2_LANES(paddsh, sh, 8, sat_add(s.sh[i], t.sh[i]))
+PS2_LANES(psubsh, sh, 8, sat_sub(s.sh[i], t.sh[i]))
+PS2_LANES(paddsb, sb, 16, sat_add(s.sb[i], t.sb[i]))
+PS2_LANES(psubsb, sb, 16, sat_sub(s.sb[i], t.sb[i]))
+PS2_LANES(padduw, w, 4, sat_add(s.w[i], t.w[i]))
+PS2_LANES(psubuw, w, 4, sat_sub(s.w[i], t.w[i]))
+PS2_LANES(padduh, h, 8, sat_add(s.h[i], t.h[i]))
+PS2_LANES(psubuh, h, 8, sat_sub(s.h[i], t.h[i]))
+PS2_LANES(paddub, b, 16, sat_add(s.b[i], t.b[i]))
+PS2_LANES(psubub, b, 16, sat_sub(s.b[i], t.b[i]))
+PS2_LANES(pcgtw, w, 4, s.sw[i] > t.sw[i] ? 0xFFFFFFFFu : 0u)
+PS2_LANES(pcgth, h, 8, static_cast<std::uint16_t>(s.sh[i] > t.sh[i] ? 0xFFFFu : 0u))
+PS2_LANES(pcgtb, b, 16, static_cast<std::uint8_t>(s.sb[i] > t.sb[i] ? 0xFFu : 0u))
+PS2_LANES(pceqw, w, 4, s.w[i] == t.w[i] ? 0xFFFFFFFFu : 0u)
+PS2_LANES(pceqh, h, 8, static_cast<std::uint16_t>(s.h[i] == t.h[i] ? 0xFFFFu : 0u))
+PS2_LANES(pceqb, b, 16, static_cast<std::uint8_t>(s.b[i] == t.b[i] ? 0xFFu : 0u))
+PS2_LANES(pmaxw, sw, 4, s.sw[i] > t.sw[i] ? s.sw[i] : t.sw[i])
+PS2_LANES(pminw, sw, 4, s.sw[i] < t.sw[i] ? s.sw[i] : t.sw[i])
+PS2_LANES(pmaxh, sh, 8, s.sh[i] > t.sh[i] ? s.sh[i] : t.sh[i])
+PS2_LANES(pminh, sh, 8, s.sh[i] < t.sh[i] ? s.sh[i] : t.sh[i])
+PS2_LANES(padsbh, h, 8, static_cast<std::uint16_t>(i < 4 ? s.h[i] - t.h[i] : s.h[i] + t.h[i]))
+PS2_LANES(pand, d, 2, s.d[i] & t.d[i])
+PS2_LANES(por, d, 2, s.d[i] | t.d[i])
+PS2_LANES(pxor, d, 2, s.d[i] ^ t.d[i])
+PS2_LANES(pnor, d, 2, ~(s.d[i] | t.d[i]))
+PS2_LANES(pextlw, w, 4, (i & 1) ? s.w[i >> 1] : t.w[i >> 1])
+PS2_LANES(pextuw, w, 4, (i & 1) ? s.w[2 + (i >> 1)] : t.w[2 + (i >> 1)])
+PS2_LANES(pextlh, h, 8, (i & 1) ? s.h[i >> 1] : t.h[i >> 1])
+PS2_LANES(pextuh, h, 8, (i & 1) ? s.h[4 + (i >> 1)] : t.h[4 + (i >> 1)])
+PS2_LANES(pextlb, b, 16, (i & 1) ? s.b[i >> 1] : t.b[i >> 1])
+PS2_LANES(pextub, b, 16, (i & 1) ? s.b[8 + (i >> 1)] : t.b[8 + (i >> 1)])
+PS2_LANES(ppacw, w, 4, i < 2 ? t.w[i * 2] : s.w[(i - 2) * 2])
+PS2_LANES(ppach, h, 8, i < 4 ? t.h[i * 2] : s.h[(i - 4) * 2])
+PS2_LANES(ppacb, b, 16, i < 8 ? t.b[i * 2] : s.b[(i - 8) * 2])
+PS2_LANES(pinth, h, 8, (i & 1) ? s.h[4 + (i >> 1)] : t.h[i >> 1])
+PS2_LANES(pinteh, h, 8, (i & 1) ? s.h[i & ~1] : t.h[i & ~1])
+PS2_LANES(pcpyld, d, 2, i ? s.d[0] : t.d[0])
+PS2_LANES(pcpyud, d, 2, i ? t.d[1] : s.d[1])
+
+#undef PS2_LANES
+
+#define PS2_UNARY(name, field, count, expr) \
+ inline void name(Reg128 &d, const Reg128 &t) { \
+ Reg128 r; \
+ for (int i = 0; i < count; ++i) r.field[i] = (expr); \
+ d = r; \
+ }
+
+PS2_UNARY(pabsw, sw, 4, t.sw[i] == INT32_MIN ? INT32_MAX : (t.sw[i] < 0 ? -t.sw[i] : t.sw[i]))
+PS2_UNARY(pabsh, sh, 8, static_cast<std::int16_t>(t.sh[i] == INT16_MIN ? INT16_MAX : (t.sh[i] < 0 ? -t.sh[i] : t.sh[i])))
+PS2_UNARY(pcpyh, h, 8, t.h[i & 4])
+PS2_UNARY(pexch, h, 8, t.h[(i & 4) | (i & 1 ? (i & 2 ? 3 : 2) : (i & 2 ? 1 : 0))])
+PS2_UNARY(pexcw, w, 4, t.w[i == 1 ? 2 : i == 2 ? 1 : i])
+PS2_UNARY(pexeh, h, 8, t.h[(i & 4) | ((i & 3) == 0 ? 2 : (i & 3) == 2 ? 0 : (i & 3))])
+PS2_UNARY(pexew, w, 4, t.w[i == 0 ? 2 : i == 2 ? 0 : i])
+PS2_UNARY(prevh, h, 8, t.h[(i & 4) | (3 - (i & 3))])
+PS2_UNARY(prot3w, w, 4, t.w[i == 0 ? 1 : i == 1 ? 2 : i == 2 ? 0 : 3])
+PS2_UNARY(pext5, w, 4, ((t.w[i] & 0x1Fu) << 3) | (((t.w[i] >> 5) & 0x1Fu) << 11) |
+ (((t.w[i] >> 10) & 0x1Fu) << 19) | (((t.w[i] >> 15) & 1u) << 31))
+PS2_UNARY(ppac5, w, 4, ((t.w[i] >> 3) & 0x1Fu) | (((t.w[i] >> 11) & 0x1Fu) << 5) |
+ (((t.w[i] >> 19) & 0x1Fu) << 10) | (((t.w[i] >> 31) & 1u) << 15))
+
+#undef PS2_UNARY
+
+inline void psllh(Reg128 &d, const Reg128 &t, unsigned sa) { for (int i = 0; i < 8; ++i) d.h[i] = static_cast<std::uint16_t>(t.h[i] << (sa & 15)); }
+inline void psrlh(Reg128 &d, const Reg128 &t, unsigned sa) { for (int i = 0; i < 8; ++i) d.h[i] = static_cast<std::uint16_t>(t.h[i] >> (sa & 15)); }
+inline void psrah(Reg128 &d, const Reg128 &t, unsigned sa) { for (int i = 0; i < 8; ++i) d.sh[i] = static_cast<std::int16_t>(t.sh[i] >> (sa & 15)); }
+inline void psllw(Reg128 &d, const Reg128 &t, unsigned sa) { for (int i = 0; i < 4; ++i) d.w[i] = t.w[i] << (sa & 31); }
+inline void psrlw(Reg128 &d, const Reg128 &t, unsigned sa) { for (int i = 0; i < 4; ++i) d.w[i] = t.w[i] >> (sa & 31); }
+inline void psraw(Reg128 &d, const Reg128 &t, unsigned sa) { for (int i = 0; i < 4; ++i) d.sw[i] = t.sw[i] >> (sa & 31); }
+
+inline void psllvw(Reg128 &d, const Reg128 &s, const Reg128 &t) {
+ const std::int64_t a = static_cast<std::int32_t>(t.w[0] << (s.w[0] & 31)), b = static_cast<std::int32_t>(t.w[2] << (s.w[2] & 31));
+ d.sd[0] = a;
+ d.sd[1] = b;
+}
+
+inline void psrlvw(Reg128 &d, const Reg128 &s, const Reg128 &t) {
+ const std::int64_t a = static_cast<std::int32_t>(t.w[0] >> (s.w[0] & 31)), b = static_cast<std::int32_t>(t.w[2] >> (s.w[2] & 31));
+ d.sd[0] = a;
+ d.sd[1] = b;
+}
+
+inline void psravw(Reg128 &d, const Reg128 &s, const Reg128 &t) {
+ const std::int64_t a = t.sw[0] >> (s.w[0] & 31), b = t.sw[2] >> (s.w[2] & 31);
+ d.sd[0] = a;
+ d.sd[1] = b;
+}
+
+inline std::uint32_t leading_sign_bits(std::uint32_t x) {
+ return static_cast<std::uint32_t>(std::countl_zero(static_cast<std::int32_t>(x) < 0 ? ~x : x)) - 1u;
+}
+
+inline void plzcw(Reg128 &d, const Reg128 &s) {
+ const std::uint32_t a = leading_sign_bits(s.w[0]), b = leading_sign_bits(s.w[1]);
+ d.w[0] = a;
+ d.w[1] = b;
+}
+
+inline void qfsrv(Reg128 &d, const Reg128 &s, const Reg128 &t, std::uint32_t sa) {
+ std::uint8_t bytes[32];
+ __builtin_memcpy(bytes, &t, 16);
+ __builtin_memcpy(bytes + 16, &s, 16);
+ Reg128 r;
+ __builtin_memcpy(&r, bytes + (sa & 15), 16);
+ d = r;
+}
+
+inline std::int64_t sext32(std::uint32_t v) { return static_cast<std::int32_t>(v); }
+
+inline void set_hilo(Cpu &c, int pipe, std::uint64_t value) {
+ c.lo.sd[pipe] = sext32(static_cast<std::uint32_t>(value));
+ c.hi.sd[pipe] = sext32(static_cast<std::uint32_t>(value >> 32));
+}
+
+inline std::uint64_t hilo(const Cpu &c, int pipe) {
+ return (static_cast<std::uint64_t>(c.hi.w[pipe * 2]) << 32) | c.lo.w[pipe * 2];
+}
+
+inline void mult(Cpu &c, int pipe, std::uint32_t s, std::uint32_t t) {
+ set_hilo(c, pipe, static_cast<std::uint64_t>(static_cast<std::int64_t>(static_cast<std::int32_t>(s)) * static_cast<std::int32_t>(t)));
+}
+
+inline void multu(Cpu &c, int pipe, std::uint32_t s, std::uint32_t t) {
+ set_hilo(c, pipe, static_cast<std::uint64_t>(s) * t);
+}
+
+inline void madd(Cpu &c, int pipe, std::uint32_t s, std::uint32_t t) {
+ set_hilo(c, pipe, hilo(c, pipe) + static_cast<std::uint64_t>(static_cast<std::int64_t>(static_cast<std::int32_t>(s)) * static_cast<std::int32_t>(t)));
+}
+
+inline void maddu(Cpu &c, int pipe, std::uint32_t s, std::uint32_t t) {
+ set_hilo(c, pipe, hilo(c, pipe) + static_cast<std::uint64_t>(s) * t);
+}
+
+inline void div(Cpu &c, int pipe, std::uint32_t s, std::uint32_t t) {
+ const auto n = static_cast<std::int32_t>(s), m = static_cast<std::int32_t>(t);
+ if (m == 0) {
+ c.lo.sd[pipe] = n < 0 ? 1 : -1;
+ c.hi.sd[pipe] = n;
+ } else if (n == INT32_MIN && m == -1) {
+ c.lo.sd[pipe] = INT32_MIN;
+ c.hi.sd[pipe] = 0;
+ } else {
+ c.lo.sd[pipe] = n / m;
+ c.hi.sd[pipe] = n % m;
+ }
+}
+
+inline void divu(Cpu &c, int pipe, std::uint32_t s, std::uint32_t t) {
+ if (t == 0) {
+ c.lo.sd[pipe] = -1;
+ c.hi.sd[pipe] = sext32(s);
+ } else {
+ c.lo.sd[pipe] = sext32(s / t);
+ c.hi.sd[pipe] = sext32(s % t);
+ }
+}
+
+inline void pmultw(Cpu &c, Reg128 &d, const Reg128 &s, const Reg128 &t) {
+ Reg128 r;
+ for (int i = 0; i < 2; ++i) {
+ const std::int64_t p = static_cast<std::int64_t>(s.sw[i * 2]) * t.sw[i * 2];
+ r.sd[i] = p;
+ set_hilo(c, i, static_cast<std::uint64_t>(p));
+ }
+ d = r;
+}
+
+inline void pmultuw(Cpu &c, Reg128 &d, const Reg128 &s, const Reg128 &t) {
+ Reg128 r;
+ for (int i = 0; i < 2; ++i) {
+ const std::uint64_t p = static_cast<std::uint64_t>(s.w[i * 2]) * t.w[i * 2];
+ r.d[i] = p;
+ set_hilo(c, i, p);
+ }
+ d = r;
+}
+
+inline void pmaddw(Cpu &c, Reg128 &d, const Reg128 &s, const Reg128 &t, bool subtract) {
+ Reg128 r;
+ for (int i = 0; i < 2; ++i) {
+ const std::int64_t p = static_cast<std::int64_t>(s.sw[i * 2]) * t.sw[i * 2];
+ const std::uint64_t acc = hilo(c, i) + static_cast<std::uint64_t>(subtract ? -p : p);
+ r.d[i] = acc;
+ set_hilo(c, i, acc);
+ }
+ d = r;
+}
+
+inline void pmadduw(Cpu &c, Reg128 &d, const Reg128 &s, const Reg128 &t) {
+ Reg128 r;
+ for (int i = 0; i < 2; ++i) {
+ const std::uint64_t acc = hilo(c, i) + static_cast<std::uint64_t>(s.w[i * 2]) * t.w[i * 2];
+ r.d[i] = acc;
+ set_hilo(c, i, acc);
+ }
+ d = r;
+}
+
+inline void pdivw(Cpu &c, const Reg128 &s, const Reg128 &t) {
+ for (int i = 0; i < 2; ++i) div(c, i, s.w[i * 2], t.w[i * 2]);
+}
+
+inline void pdivuw(Cpu &c, const Reg128 &s, const Reg128 &t) {
+ for (int i = 0; i < 2; ++i) divu(c, i, s.w[i * 2], t.w[i * 2]);
+}
+
+inline void pdivbw(Cpu &c, const Reg128 &s, const Reg128 &t) {
+ const std::int32_t m = t.sh[0];
+ for (int i = 0; i < 4; ++i) {
+ const std::int32_t n = s.sw[i];
+ if (m == 0) {
+ c.lo.sw[i] = n < 0 ? 1 : -1;
+ c.hi.sw[i] = n;
+ } else if (n == INT32_MIN && m == -1) {
+ c.lo.sw[i] = INT32_MIN;
+ c.hi.sw[i] = 0;
+ } else {
+ c.lo.sw[i] = n / m;
+ c.hi.sw[i] = static_cast<std::int16_t>(n % m);
+ }
+ }
+}
+
+inline void pmulth(Cpu &c, Reg128 &d, const Reg128 &s, const Reg128 &t, int mode) {
+ std::int32_t p[8];
+ for (int i = 0; i < 8; ++i) p[i] = static_cast<std::int32_t>(s.sh[i]) * t.sh[i];
+ constexpr int kLo[4] = {0, 1, 4, 5}, kHi[4] = {2, 3, 6, 7};
+ for (int k = 0; k < 4; ++k) {
+ if (mode == 0) {
+ c.lo.sw[k] = p[kLo[k]];
+ c.hi.sw[k] = p[kHi[k]];
+ } else if (mode == 1) {
+ c.lo.sw[k] += p[kLo[k]];
+ c.hi.sw[k] += p[kHi[k]];
+ } else {
+ c.lo.sw[k] -= p[kLo[k]];
+ c.hi.sw[k] -= p[kHi[k]];
+ }
+ }
+ Reg128 r;
+ r.w[0] = c.lo.w[0];
+ r.w[1] = c.hi.w[0];
+ r.w[2] = c.lo.w[2];
+ r.w[3] = c.hi.w[2];
+ d = r;
+}
+
+inline void phmadh(Cpu &c, Reg128 &d, const Reg128 &s, const Reg128 &t, bool subtract) {
+ std::int32_t p[8];
+ for (int i = 0; i < 8; ++i) p[i] = static_cast<std::int32_t>(s.sh[i]) * t.sh[i];
+ const auto pair = [&](int k) { return subtract ? p[k + 1] - p[k] : p[k + 1] + p[k]; };
+ c.lo.sw[0] = pair(0);
+ c.hi.sw[0] = pair(2);
+ c.lo.sw[2] = pair(4);
+ c.hi.sw[2] = pair(6);
+ Reg128 r;
+ r.w[0] = c.lo.w[0];
+ r.w[1] = c.hi.w[0];
+ r.w[2] = c.lo.w[2];
+ r.w[3] = c.hi.w[2];
+ d = r;
+}
+
+inline std::int32_t sat32(std::int64_t v) {
+ return v > INT32_MAX ? INT32_MAX : v < INT32_MIN ? INT32_MIN : static_cast<std::int32_t>(v);
+}
+
+inline std::int16_t sat16(std::int32_t v) {
+ return v > INT16_MAX ? INT16_MAX : v < INT16_MIN ? INT16_MIN : static_cast<std::int16_t>(v);
+}
+
+inline void pmfhl(const Cpu &c, Reg128 &d, unsigned mode) {
+ Reg128 r = d;
+ const Reg128 &lo = c.lo, &hi = c.hi;
+ switch (mode) {
+ case 0:
+ r.w[0] = lo.w[0]; r.w[1] = hi.w[0]; r.w[2] = lo.w[2]; r.w[3] = hi.w[2];
+ break;
+ case 1:
+ r.w[0] = lo.w[1]; r.w[1] = hi.w[1]; r.w[2] = lo.w[3]; r.w[3] = hi.w[3];
+ break;
+ case 2:
+ for (int i = 0; i < 2; ++i)
+ r.sd[i] = sat32(static_cast<std::int64_t>((static_cast<std::uint64_t>(hi.w[i * 2]) << 32) | lo.w[i * 2]));
+ break;
+ case 3:
+ r.h[0] = lo.h[0]; r.h[1] = lo.h[2]; r.h[2] = hi.h[0]; r.h[3] = hi.h[2];
+ r.h[4] = lo.h[4]; r.h[5] = lo.h[6]; r.h[6] = hi.h[4]; r.h[7] = hi.h[6];
+ break;
+ default:
+ r.sh[0] = sat16(lo.sw[0]); r.sh[1] = sat16(lo.sw[1]); r.sh[2] = sat16(hi.sw[0]); r.sh[3] = sat16(hi.sw[1]);
+ r.sh[4] = sat16(lo.sw[2]); r.sh[5] = sat16(lo.sw[3]); r.sh[6] = sat16(hi.sw[2]); r.sh[7] = sat16(hi.sw[3]);
+ break;
+ }
+ d = r;
+}
+
+inline void pmthl(Cpu &c, const Reg128 &s) {
+ c.lo.w[0] = s.w[0];
+ c.hi.w[0] = s.w[1];
+ c.lo.w[2] = s.w[2];
+ c.hi.w[2] = s.w[3];
+}
+
+}
diff --git a/runtime/include/ps2/vu.hpp b/runtime/include/ps2/vu.hpp
new file mode 100644
index 0000000..86ead05
--- /dev/null
+++ b/runtime/include/ps2/vu.hpp
@@ -0,0 +1,172 @@
+#pragma once
+
+#include "cpu.hpp"
+#include "fpu.hpp"
+
+#include <cstdint>
+
+namespace ps2::vu {
+
+inline bool lane(unsigned dest, int i) { return dest & (8u >> i); }
+
+template <typename F>
+inline void apply(Vu0Regs &v, unsigned dest, unsigned fd, F f) {
+ Reg128 r = v.vf[fd];
+ for (int i = 0; i < 4; ++i)
+ if (lane(dest, i)) r.w[i] = f(i);
+ if (fd) v.vf[fd] = r;
+}
+
+template <typename F>
+inline void apply_acc(Vu0Regs &v, unsigned dest, F f) {
+ Reg128 r = v.acc;
+ for (int i = 0; i < 4; ++i)
+ if (lane(dest, i)) r.w[i] = f(i);
+ v.acc = r;
+}
+
+inline void add(Vu0Regs &v, unsigned dest, unsigned fd, unsigned fs, unsigned ft) {
+ apply(v, dest, fd, [&](int i) { return fp::add(v.vf[fs].w[i], v.vf[ft].w[i]); });
+}
+inline void addbc(Vu0Regs &v, unsigned dest, unsigned fd, unsigned fs, std::uint32_t t) {
+ apply(v, dest, fd, [&](int i) { return fp::add(v.vf[fs].w[i], t); });
+}
+inline void sub(Vu0Regs &v, unsigned dest, unsigned fd, unsigned fs, unsigned ft) {
+ apply(v, dest, fd, [&](int i) { return fp::sub(v.vf[fs].w[i], v.vf[ft].w[i]); });
+}
+inline void subbc(Vu0Regs &v, unsigned dest, unsigned fd, unsigned fs, std::uint32_t t) {
+ apply(v, dest, fd, [&](int i) { return fp::sub(v.vf[fs].w[i], t); });
+}
+inline void mul(Vu0Regs &v, unsigned dest, unsigned fd, unsigned fs, unsigned ft) {
+ apply(v, dest, fd, [&](int i) { return fp::mul(v.vf[fs].w[i], v.vf[ft].w[i]); });
+}
+inline void mulbc(Vu0Regs &v, unsigned dest, unsigned fd, unsigned fs, std::uint32_t t) {
+ apply(v, dest, fd, [&](int i) { return fp::mul(v.vf[fs].w[i], t); });
+}
+inline void madd(Vu0Regs &v, unsigned dest, unsigned fd, unsigned fs, unsigned ft) {
+ apply(v, dest, fd, [&](int i) { return fp::add(v.acc.w[i], fp::mul(v.vf[fs].w[i], v.vf[ft].w[i])); });
+}
+inline void maddbc(Vu0Regs &v, unsigned dest, unsigned fd, unsigned fs, std::uint32_t t) {
+ apply(v, dest, fd, [&](int i) { return fp::add(v.acc.w[i], fp::mul(v.vf[fs].w[i], t)); });
+}
+inline void msub(Vu0Regs &v, unsigned dest, unsigned fd, unsigned fs, unsigned ft) {
+ apply(v, dest, fd, [&](int i) { return fp::sub(v.acc.w[i], fp::mul(v.vf[fs].w[i], v.vf[ft].w[i])); });
+}
+inline void msubbc(Vu0Regs &v, unsigned dest, unsigned fd, unsigned fs, std::uint32_t t) {
+ apply(v, dest, fd, [&](int i) { return fp::sub(v.acc.w[i], fp::mul(v.vf[fs].w[i], t)); });
+}
+inline void max(Vu0Regs &v, unsigned dest, unsigned fd, unsigned fs, unsigned ft) {
+ apply(v, dest, fd, [&](int i) { return fp::max(v.vf[fs].w[i], v.vf[ft].w[i]); });
+}
+inline void maxbc(Vu0Regs &v, unsigned dest, unsigned fd, unsigned fs, std::uint32_t t) {
+ apply(v, dest, fd, [&](int i) { return fp::max(v.vf[fs].w[i], t); });
+}
+inline void mini(Vu0Regs &v, unsigned dest, unsigned fd, unsigned fs, unsigned ft) {
+ apply(v, dest, fd, [&](int i) { return fp::min(v.vf[fs].w[i], v.vf[ft].w[i]); });
+}
+inline void minibc(Vu0Regs &v, unsigned dest, unsigned fd, unsigned fs, std::uint32_t t) {
+ apply(v, dest, fd, [&](int i) { return fp::min(v.vf[fs].w[i], t); });
+}
+
+inline void adda(Vu0Regs &v, unsigned dest, unsigned fs, unsigned ft) {
+ apply_acc(v, dest, [&](int i) { return fp::add(v.vf[fs].w[i], v.vf[ft].w[i]); });
+}
+inline void addabc(Vu0Regs &v, unsigned dest, unsigned fs, std::uint32_t t) {
+ apply_acc(v, dest, [&](int i) { return fp::add(v.vf[fs].w[i], t); });
+}
+inline void suba(Vu0Regs &v, unsigned dest, unsigned fs, unsigned ft) {
+ apply_acc(v, dest, [&](int i) { return fp::sub(v.vf[fs].w[i], v.vf[ft].w[i]); });
+}
+inline void subabc(Vu0Regs &v, unsigned dest, unsigned fs, std::uint32_t t) {
+ apply_acc(v, dest, [&](int i) { return fp::sub(v.vf[fs].w[i], t); });
+}
+inline void mula(Vu0Regs &v, unsigned dest, unsigned fs, unsigned ft) {
+ apply_acc(v, dest, [&](int i) { return fp::mul(v.vf[fs].w[i], v.vf[ft].w[i]); });
+}
+inline void mulabc(Vu0Regs &v, unsigned dest, unsigned fs, std::uint32_t t) {
+ apply_acc(v, dest, [&](int i) { return fp::mul(v.vf[fs].w[i], t); });
+}
+inline void madda(Vu0Regs &v, unsigned dest, unsigned fs, unsigned ft) {
+ apply_acc(v, dest, [&](int i) { return fp::add(v.acc.w[i], fp::mul(v.vf[fs].w[i], v.vf[ft].w[i])); });
+}
+inline void maddabc(Vu0Regs &v, unsigned dest, unsigned fs, std::uint32_t t) {
+ apply_acc(v, dest, [&](int i) { return fp::add(v.acc.w[i], fp::mul(v.vf[fs].w[i], t)); });
+}
+inline void msuba(Vu0Regs &v, unsigned dest, unsigned fs, unsigned ft) {
+ apply_acc(v, dest, [&](int i) { return fp::sub(v.acc.w[i], fp::mul(v.vf[fs].w[i], v.vf[ft].w[i])); });
+}
+inline void msubabc(Vu0Regs &v, unsigned dest, unsigned fs, std::uint32_t t) {
+ apply_acc(v, dest, [&](int i) { return fp::sub(v.acc.w[i], fp::mul(v.vf[fs].w[i], t)); });
+}
+
+inline void opmula(Vu0Regs &v, unsigned fs, unsigned ft) {
+ const Reg128 s = v.vf[fs], t = v.vf[ft];
+ v.acc.w[0] = fp::mul(s.w[1], t.w[2]);
+ v.acc.w[1] = fp::mul(s.w[2], t.w[0]);
+ v.acc.w[2] = fp::mul(s.w[0], t.w[1]);
+}
+
+inline void opmsub(Vu0Regs &v, unsigned fd, unsigned fs, unsigned ft) {
+ const Reg128 s = v.vf[fs], t = v.vf[ft];
+ Reg128 r = v.vf[fd];
+ r.w[0] = fp::sub(v.acc.w[0], fp::mul(s.w[1], t.w[2]));
+ r.w[1] = fp::sub(v.acc.w[1], fp::mul(s.w[2], t.w[0]));
+ r.w[2] = fp::sub(v.acc.w[2], fp::mul(s.w[0], t.w[1]));
+ if (fd) v.vf[fd] = r;
+}
+
+inline void abs(Vu0Regs &v, unsigned dest, unsigned ft, unsigned fs) {
+ apply(v, dest, ft, [&](int i) { return v.vf[fs].w[i] & fp::kAbs; });
+}
+inline void move(Vu0Regs &v, unsigned dest, unsigned ft, unsigned fs) {
+ apply(v, dest, ft, [&](int i) { return v.vf[fs].w[i]; });
+}
+inline void mr32(Vu0Regs &v, unsigned dest, unsigned ft, unsigned fs) {
+ const Reg128 s = v.vf[fs];
+ apply(v, dest, ft, [&](int i) { return s.w[(i + 1) & 3]; });
+}
+inline void ftoi(Vu0Regs &v, unsigned dest, unsigned ft, unsigned fs, int fraction) {
+ apply(v, dest, ft, [&](int i) { return fp::to_fixed(v.vf[fs].w[i], fraction); });
+}
+inline void itof(Vu0Regs &v, unsigned dest, unsigned ft, unsigned fs, int fraction) {
+ apply(v, dest, ft, [&](int i) { return fp::from_fixed(v.vf[fs].sw[i], fraction); });
+}
+
+inline void clip(Vu0Regs &v, unsigned fs, unsigned ft) {
+ const std::int32_t w = fp::key(v.vf[ft].w[3] & fp::kAbs);
+ std::uint32_t flags = 0;
+ for (int i = 0; i < 3; ++i) {
+ const std::int32_t x = fp::key(v.vf[fs].w[i]);
+ if (x > w) flags |= 1u << (i * 2);
+ if (x < -w) flags |= 2u << (i * 2);
+ }
+ v.clip = ((v.clip << 6) | flags) & 0xFFFFFFu;
+}
+
+inline void div(Vu0Regs &v, std::uint32_t s, std::uint32_t t) {
+ const fp::Result r = fp::div(s, t);
+ v.q = r.value;
+ v.status = (v.status & ~0x30u) | (r.invalid ? 0x20u : 0u);
+}
+inline void sqrt(Vu0Regs &v, std::uint32_t t) { v.q = fp::sqrt(t); }
+inline void rsqrt(Vu0Regs &v, std::uint32_t s, std::uint32_t t) {
+ const fp::Result r = fp::rsqrt(s, t);
+ v.q = r.value;
+ v.status = (v.status & ~0x30u) | (r.invalid ? 0x20u : 0u);
+}
+
+inline void mfir(Vu0Regs &v, unsigned dest, unsigned ft, unsigned is) {
+ const std::uint32_t value = static_cast<std::uint32_t>(static_cast<std::int32_t>(static_cast<std::int16_t>(v.vi[is])));
+ apply(v, dest, ft, [&](int) { return value; });
+}
+
+inline void set_vi(Vu0Regs &v, unsigned index, std::uint32_t value) {
+ if (index) v.vi[index] = static_cast<std::uint16_t>(value);
+}
+
+inline std::uint32_t rand_next(std::uint32_t r) {
+ const std::uint32_t bit = ((r >> 4) ^ (r >> 22)) & 1u;
+ return ((r << 1) | bit) & 0x7FFFFFu;
+}
+
+}