PS2 games in the browser: static recompilation to WebAssembly with a native runtime
C++
Translate SN Systems SNR2 relocatable modules
Samir Nuria7f3c5aa3ebc
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 78197fd..11decaf 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -17,7 +17,8 @@ if(NOT EMSCRIPTEN)recomp/r5900.cpprecomp/elf.cpprecomp/analysis.cpp- recomp/codegen.cpp)+ recomp/codegen.cpp+ recomp/snr2.cpp)target_compile_options(eerecomp PRIVATE -Wall -Wextra)add_executable(vujit_dump recomp/vujit_dump.cpp runtime/src/vu_jit.cpp)diff --git a/recomp/analysis.cpp b/recomp/analysis.cppindex 5c47e61..e2f2580 100644--- a/recomp/analysis.cpp+++ b/recomp/analysis.cpp@@ -51,7 +51,7 @@ public:if (!valid_target(pc + 4) || decode(*image_.word(pc + 4), pc + 4).op == Op::Invalid) return false;if (has_delay_slot(decode(*image_.word(pc + 4), pc + 4).op)) return false;seen.insert(pc + 4);- if (f == Flow::Branch || f == Flow::BranchLikely || f == Flow::Jump) {+ if (f == Flow::Branch || f == Flow::BranchLikely || (f == Flow::Jump && !p_.dynamic.count(pc))) {if (!valid_target(insn.branch_target_of())) return false;pending.push_back(insn.branch_target_of());}@@ -79,10 +79,10 @@ private:local(insn.branch_target_of(), pc);break;case Flow::Jump:- local(insn.branch_target_of(), pc);+ if (!p_.dynamic.count(pc)) local(insn.branch_target_of(), pc);return;case Flow::Call:- strong(insn.branch_target_of(), true);+ if (!p_.dynamic.count(pc)) strong(insn.branch_target_of(), true);p_.entries.insert(pc + 8);break;case Flow::CallRegister:@@ -184,8 +184,9 @@ std::vector<std::uint32_t> computed_branches(const Image &image, const Program &}-Program analyse(const Image &image, const std::vector<std::uint32_t> &extra_entries) {+Program analyse(const Image &image, const std::vector<std::uint32_t> &extra_entries, const std::set<std::uint32_t> &dynamic) {Program p;+ p.dynamic = dynamic;p.base = UINT32_MAX;for (const auto &segment : image.segments()) {if (!segment.executable) continue;diff --git a/recomp/analysis.hpp b/recomp/analysis.hppindex fda45a4..a376d0e 100644--- a/recomp/analysis.hpp+++ b/recomp/analysis.hpp@@ -16,12 +16,13 @@ struct Program {std::set<std::uint32_t> functions;std::set<std::uint32_t> entries;std::set<std::uint32_t> rejected;+ std::set<std::uint32_t> dynamic;bool is_code(std::uint32_t address) const {return address >= base && address < end && (address & 3u) == 0 && code[(address - base) >> 2];}};-Program analyse(const Image &image, const std::vector<std::uint32_t> &extra_entries);+Program analyse(const Image &image, const std::vector<std::uint32_t> &extra_entries, const std::set<std::uint32_t> &dynamic = {});}diff --git a/recomp/codegen.cpp b/recomp/codegen.cppindex 7a49f47..3ed6fe4 100644--- a/recomp/codegen.cpp+++ b/recomp/codegen.cpp@@ -6,6 +6,7 @@#include <fstream>#include <iterator>#include <map>+#include <optional>#include <set>#include <sstream>#include <stdexcept>@@ -34,8 +35,6 @@ std::string offset(std::int32_t 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) {@@ -55,8 +54,6 @@ private:std::uint32_t count_{};};-std::string unit_name(std::uint32_t u) { return std::format("unit_{:04X}", u); }-void write_if_changed(const fs::path &path, const std::string &text) {std::ifstream in(path, std::ios::binary);if (in) {@@ -65,29 +62,69 @@ void write_if_changed(const fs::path &path, const std::string &text) {}std::ofstream(path, std::ios::binary) << text;}-std::string table_name(std::uint32_t u) { return std::format("entries_{:04X}", u); }++struct UnitRow {+ std::uint32_t base;+ std::uint32_t size;+ std::string function;+ std::string table;+};class Generator {public:- Generator(const Image &image, Program &program, const CodegenOptions &options)- : image_(image), p_(program), options_(options), layout_(program, options.unit_bits) {}+ Generator(const Translation &t, const CodegenOptions &options)+ : image_(*t.image), p_(*t.program), links_(t.links), prefix_(t.prefix), relocatable_(t.relocatable), options_(options),+ layout_(*t.program, options.unit_bits) {}- void run() {+ std::vector<UnitRow> run() {add_required_entries();assign_ids();- fs::create_directories(options_.output);- write_header();- std::size_t written = 0;+ std::vector<UnitRow> rows;for (std::uint32_t u = 0; u < layout_.count(); ++u)if (has_code_[u]) {write_unit(u);- ++written;+ rows.push_back({layout_.base_of(u), layout_.size(), unit_name(u), table_name(u)});}- write_table();- std::printf("%zu units written to %s\n", written, options_.output.c_str());+ return rows;}private:+ std::string unit_name(std::uint32_t u) const { return std::format("{}unit_{:04X}", prefix_, u); }+ std::string table_name(std::uint32_t u) const { return std::format("{}entries_{:04X}", prefix_, u); }+ std::string pc(std::uint32_t a) const { return relocatable_ ? std::format("(B + {})", hex(a)) : hex(a); }+ std::string rebase() const { return relocatable_ ? "c.base = B; " : ""; }++ const Link *link_at(std::uint32_t a) const {+ if (!links_) return nullptr;+ const auto it = links_->find(a);+ return it == links_->end() ? nullptr : &it->second;+ }++ bool dynamic_jump(std::uint32_t a) const {+ const Link *l = link_at(a);+ return l && l->kind == Fix::Jump && (l->external || !relocatable_);+ }++ std::optional<std::string> field(const Insn &i) const {+ const Link *l = link_at(i.pc);+ if (!l || (l->kind != Fix::High && l->kind != Fix::Low)) return std::nullopt;+ if (l->external || !relocatable_) return std::format("static_cast<std::uint32_t>(ld16({}))", pc(i.pc));+ if (l->kind == Fix::High) return std::format("((B + {}) >> 16)", hex(l->value + 0x8000u));+ return std::format("(B + {})", hex(i.uimm()));+ }++ std::string imm(const Insn &i) const {+ if (const auto e = field(i)) return " + static_cast<std::uint32_t>(static_cast<std::int16_t>(" + *e + "))";+ return offset(i.simm());+ }++ std::string address(const Insn &i) const { return "(" + w(i.rs()) + imm(i) + ")"; }++ std::string uimm(const Insn &i) const {+ if (const auto e = field(i)) return "(static_cast<std::uint64_t>(" + *e + ") & 0xFFFFull)";+ return std::format("{}ull", i.uimm());+ }+Insn at(std::uint32_t a) const { return decode(*image_.word(a), a); }void add_required_entries() {@@ -95,7 +132,7 @@ private: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) {+ if ((fl == Flow::Branch || fl == Flow::BranchLikely || fl == Flow::Jump || i.op == Jal) && !dynamic_jump(a)) {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);}@@ -128,31 +165,6 @@ private:bool in_unit(std::uint32_t a) const { return a >= base_ && a < base_ + layout_.size(); }- void write_header() {- std::ostringstream out;- 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";- write_if_changed(fs::path(options_.output) / "units.hpp", out.str());- }-- void write_table() {- std::ostringstream out;- 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";- write_if_changed(fs::path(options_.output) / "table.cpp", out.str());- }-void write_unit(std::uint32_t u) {base_ = layout_.base_of(u);labels_.clear();@@ -162,7 +174,7 @@ private: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) {+ if ((fl == Flow::Branch || fl == Flow::BranchLikely || fl == Flow::Jump) && !dynamic_jump(a)) {const std::uint32_t t = i.branch_target_of();if (in_unit(t) && p_.is_code(t)) labels_.insert(t);}@@ -177,9 +189,10 @@ private: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";+ if (relocatable_) out_ << " [[maybe_unused]] const std::uint32_t B = c.base;\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";+ out_ << " default: bad_entry(c, " << pc(base_) << "); return;\n }\n";pending_ = 0;bool falls = false, skip = false;@@ -199,7 +212,7 @@ private:flush();out_ << label(a) << ":\n";}- if (options_.probes.count(a)) line(std::format("probe(c, {});", hex(a)));+ if (options_.probes.count(a)) line(std::format("probe(c, {});", pc(a)));const Insn i = at(a);const Flow fl = flow(i.op);if (fl == Flow::None) {@@ -212,7 +225,7 @@ private:falls = false;} else if (fl == Flow::Stop) {flush();- line(std::format("stop(c, {}, \"{}\"); return;", hex(a), name(i.op)));+ line(std::format("stop(c, {}, \"{}\"); return;", pc(a), name(i.op)));falls = false;} else {const Insn ds = p_.is_code(a + 4) ? at(a + 4) : Insn{a + 4, 0, Invalid};@@ -244,28 +257,30 @@ private: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)));+ line(std::format("{}[[clang::musttail]] return {}(c, {}u, depth);", rebase(), unit_name(layout_.unit_of(a)), id_of(a)));} else {- line(std::format("c.pc = {}; [[clang::musttail]] return dispatch(c, 0u, depth);", hex(a)));+ line(std::format("c.pc = {}; [[clang::musttail]] return dispatch(c, 0u, depth);", pc(a)));}}std::string jump(std::uint32_t t, std::uint32_t from) {- std::string check = t <= from ? std::format("if (c.budget < 0 && preempt(c)) {{ c.pc = {} | kUnwind; return; }} ", hex(t)) : "";+ std::string check = t <= from ? std::format("if (c.budget < 0 && preempt(c)) {{ c.pc = {} | kUnwind; return; }} ", pc(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));+ return check + std::format("{}[[clang::musttail]] return {}(c, {}u, depth);", rebase(), unit_name(layout_.unit_of(t)), id_of(t));+ return std::format("c.pc = {}; [[clang::musttail]] return dispatch(c, 0u, depth);", pc(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));+ invoke = relocatable_ ? std::format("if (depth < kMaxDepth) {{ c.base = B; {}(c, {}u, depth + 1u); }} else {{ c.pc = {}; return; }}",+ unit_name(layout_.unit_of(t)), id_of(t), pc(t))+ : std::format("if (depth < kMaxDepth) {}(c, {}u, depth + 1u); else {{ c.pc = {}; return; }}",+ unit_name(layout_.unit_of(t)), id_of(t), pc(t));else- invoke = std::format("call(c, {}, depth + 1u);", hex(t));- return invoke + std::format(" if (c.pc != {}) [[unlikely]] return;", hex(ret));+ invoke = std::format("call(c, {}, depth + 1u);", pc(t));+ return invoke + std::format(" if (c.pc != {}) [[unlikely]] return;", pc(ret));}std::string condition(const Insn &i) {@@ -288,7 +303,7 @@ private: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));+ return std::format("stop(c, {}, \"unsupported delay slot\"); return;", pc(ds.pc));std::string text;capture_ = &text;simple(ds);@@ -301,8 +316,16 @@ private: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::string link = std::format("c.r[31].sd[0] = static_cast<std::int32_t>({});", pc(ret));const std::uint32_t t = i.branch_target_of();+ if (dynamic_jump(a)) {+ const std::string target = std::format("const std::uint32_t t = (({} + 4u) & 0xF0000000u) | ((ld32({}) & 0x03FFFFFFu) << 2);", pc(a), pc(a));+ if (i.op == Jal)+ line(std::format("{{ {} {} {} call(c, t, depth + 1u); if (c.pc != {}) [[unlikely]] return; }}", target, link, slot, pc(ret)));+ else+ line(std::format("{{ {} {} c.pc = t; [[clang::musttail]] return dispatch(c, 0u, depth); }}", target, slot));+ return;+ }switch (i.op) {case Jal:line(std::format("{{ {} {} {} }}", link, slot, call_to(t, ret)));@@ -331,13 +354,13 @@ private: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_)));+ w(i.rs()), slot, pc(base_), layout_.size(), table_name(layout_.unit_of(base_)), pc(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)) : "";+ const std::string set = i.rd() ? std::format("c.r[{}].sd[0] = static_cast<std::int32_t>({});", i.rd(), pc(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)));+ w(i.rs()), set, slot, pc(ret)));break;}default:@@ -395,9 +418,9 @@ private: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)));+ line(std::format("c.pc = {}; syscall(c, {}); if (c.pc != {}) return;", pc(i.pc + 4), pc(i.pc), pc(i.pc + 4)));break;- case Break: line(std::format("breakpoint(c, {});", hex(i.pc))); break;+ case Break: line(std::format("breakpoint(c, {});", pc(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;@@ -436,32 +459,39 @@ private: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 Tge: line(std::format("if ({} >= {}) trap(c, {});", sd(rs), sd(rt), pc(i.pc))); break;+ case Tgeu: line(std::format("if ({} >= {}) trap(c, {});", d(rs), d(rt), pc(i.pc))); break;+ case Tlt: line(std::format("if ({} < {}) trap(c, {});", sd(rs), sd(rt), pc(i.pc))); break;+ case Tltu: line(std::format("if ({} < {}) trap(c, {});", d(rs), d(rt), pc(i.pc))); break;+ case Teq: line(std::format("if ({} == {}) trap(c, {});", d(rs), d(rt), pc(i.pc))); break;+ case Tne: line(std::format("if ({} != {}) trap(c, {});", d(rs), d(rt), pc(i.pc))); break;+ case Tgei: line(std::format("if ({} >= {}ll) trap(c, {});", sd(rs), i.simm(), pc(i.pc))); break;+ case Tgeiu: line(std::format("if ({} >= static_cast<std::uint64_t>({}ll)) trap(c, {});", d(rs), i.simm(), pc(i.pc))); break;+ case Tlti: line(std::format("if ({} < {}ll) trap(c, {});", sd(rs), i.simm(), pc(i.pc))); break;+ case Tltiu: line(std::format("if ({} < static_cast<std::uint64_t>({}ll)) trap(c, {});", d(rs), i.simm(), pc(i.pc))); break;+ case Teqi: line(std::format("if ({} == {}ll) trap(c, {});", sd(rs), i.simm(), pc(i.pc))); break;+ case Tnei: line(std::format("if ({} != {}ll) trap(c, {});", sd(rs), i.simm(), pc(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 Addi: case Addiu: set_w(rt, w(rs) + imm(i)); 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 Andi: set_d(rt, std::format("{} & {}", d(rs), uimm(i))); break;+ case Ori: set_d(rt, std::format("{} | {}", d(rs), uimm(i))); break;+ case Xori: set_d(rt, std::format("{} ^ {}", d(rs), uimm(i))); break;+ case Lui:+ if (!rt) break;+ if (const auto e = field(i)) line(std::format("c.r[{}].sd[0] = static_cast<std::int32_t>({} << 16);", rt, *e));+ else line(std::format("c.r[{}].sd[0] = static_cast<std::int32_t>({});", rt, hex(i.uimm() << 16)));+ break;+ case Daddi: case Daddiu:+ if (const auto e = field(i)) set_d(rt, std::format("{} + static_cast<std::uint64_t>(static_cast<std::int64_t>(static_cast<std::int16_t>({})))", d(rs), *e));+ else 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;@@ -589,7 +619,7 @@ private: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)));+ line(std::format("stop(c, {}, \"{}\");", pc(i.pc), name(i.op)));break;case Mfc1: set_w(rt, f(i.fs())); break;@@ -732,13 +762,16 @@ private: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)));+ line(std::format("stop(c, {}, \"{}\"); return;", pc(i.pc), name(i.op)));break;}}const Image &image_;Program &p_;+ const Links *links_;+ std::string prefix_;+ bool relocatable_;const CodegenOptions &options_;Layout layout_;std::vector<std::map<std::uint32_t, std::uint16_t>> ids_;@@ -752,8 +785,40 @@ private:}-void generate(const Image &image, Program &program, const CodegenOptions &options) {- Generator(image, program, options).run();+void generate(const std::vector<Translation> &translations, const CodegenOptions &options) {+ fs::create_directories(options.output);+ std::ostringstream header, table, modules;+ header << "#pragma once\n\n#include \"ps2/generated.hpp\"\n\nnamespace gen {\n";+ table << "#include \"units.hpp\"\n\nnamespace gen {\n\n";+ std::size_t written = 0, main_units = 0, module_count = 0;+ for (const Translation &t : translations) {+ const std::vector<UnitRow> rows = Generator(t, options).run();+ written += rows.size();+ for (const UnitRow &r : rows) {+ header << "void " << r.function << "(ps2::Cpu &c, std::uint32_t entry, std::uint32_t depth);\n";+ header << "extern const std::uint16_t " << r.table << "[];\n";+ }+ table << "const ps2::UnitInfo " << (t.relocatable ? t.prefix + "units" : "kUnits") << "[] = {\n";+ for (const UnitRow &r : rows) table << std::format(" {{{}, {}u, &{}, {}}},\n", hex(r.base), r.size, r.function, r.table);+ table << "};\n\n";+ if (!t.relocatable) {+ main_units = rows.size();+ continue;+ }+ table << "const std::uint32_t " << t.prefix << "checks[] = {";+ for (const auto &[at, value] : t.checks) table << std::format("{}, {}, ", hex(at), hex(value));+ table << "};\n\n";+ modules << std::format(" {{\"{}\", {}, {}checks, {}u, {}units, {}u}},\n", t.name, hex(t.size), t.prefix, t.checks.size(), t.prefix, rows.size());+ ++module_count;+ }+ header << "}\n";+ if (module_count) table << "const ps2::ModuleInfo kModules[] = {\n" << modules.str() << "};\n\n";+ table << "}\n\nvoid register_generated_code() { ps2::register_units(gen::kUnits, " << main_units << "u);";+ if (module_count) table << " ps2::register_modules(gen::kModules, " << module_count << "u);";+ table << " }\n";+ write_if_changed(fs::path(options.output) / "units.hpp", header.str());+ write_if_changed(fs::path(options.output) / "table.cpp", table.str());+ std::printf("%zu units written to %s\n", written, options.output.c_str());}}diff --git a/recomp/codegen.hpp b/recomp/codegen.hppindex 865e442..26e71b8 100644--- a/recomp/codegen.hpp+++ b/recomp/codegen.hpp@@ -2,10 +2,13 @@#include "analysis.hpp"#include "elf.hpp"+#include "snr2.hpp"#include <cstdint>#include <set>#include <string>+#include <utility>+#include <vector>namespace recomp {@@ -15,6 +18,17 @@ struct CodegenOptions {std::set<std::uint32_t> probes;};-void generate(const Image &image, Program &program, const CodegenOptions &options);+struct Translation {+ const Image *image{};+ Program *program{};+ const Links *links{};+ std::string prefix;+ bool relocatable{};+ std::string name;+ std::uint32_t size{};+ std::vector<std::pair<std::uint32_t, std::uint32_t>> checks;+};++void generate(const std::vector<Translation> &translations, const CodegenOptions &options);}diff --git a/recomp/elf.cpp b/recomp/elf.cppindex 1fc0dcc..d46fd36 100644--- a/recomp/elf.cpp+++ b/recomp/elf.cpp@@ -68,6 +68,13 @@ Image Image::load(const std::string &path) {return image;}+Image Image::raw(std::uint32_t address, std::vector<std::uint8_t> bytes, std::pair<std::uint32_t, std::uint32_t> text) {+ Image image;+ image.segments_.push_back(Segment{address, std::move(bytes), true});+ image.text_ = text;+ return image;+}+std::optional<std::uint32_t> Image::word(std::uint32_t address) const {for (const auto &segment : segments_) {if (address >= segment.address && address - segment.address + 4 <= segment.bytes.size()) {diff --git a/recomp/elf.hpp b/recomp/elf.hppindex 590c5f3..23c398f 100644--- a/recomp/elf.hpp+++ b/recomp/elf.hpp@@ -18,6 +18,7 @@ struct Segment {class Image {public:static Image load(const std::string &path);+ static Image raw(std::uint32_t address, std::vector<std::uint8_t> bytes, std::pair<std::uint32_t, std::uint32_t> text);std::uint32_t entry() const { return entry_; }const std::vector<Segment> &segments() const { return segments_; }diff --git a/recomp/main.cpp b/recomp/main.cppindex 9dac6bb..a448dee 100644--- a/recomp/main.cpp+++ b/recomp/main.cpp@@ -1,11 +1,18 @@#include "analysis.hpp"#include "codegen.hpp"#include "elf.hpp"+#include "snr2.hpp"+#include <algorithm>#include <cstdio>#include <cstdlib>+#include <cstring>+#include <deque>+#include <filesystem>#include <fstream>+#include <iterator>#include <map>+#include <set>#include <string>#include <vector>@@ -13,19 +20,78 @@ using namespace recomp;namespace {-std::vector<std::uint32_t> read_entries(const std::string &path) {- std::vector<std::uint32_t> entries;+struct Entries {+ std::vector<std::uint32_t> main;+ std::map<std::string, std::vector<std::uint32_t>> modules;+};++Entries read_entries(const std::string &path) {+ Entries 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)));+ const auto colon = token.find(':');+ if (colon == std::string::npos)+ entries.main.push_back(static_cast<std::uint32_t>(std::stoul(token, nullptr, 16)));+ else+ entries.modules[token.substr(0, colon)].push_back(static_cast<std::uint32_t>(std::stoul(token.substr(colon + 1), nullptr, 16)));}return entries;}+std::set<std::uint32_t> dynamic_sites(const Links &links) {+ std::set<std::uint32_t> sites;+ for (const auto &[at, link] : links)+ if (link.kind == Fix::Jump && link.external) sites.insert(at);+ return sites;+}++std::size_t code_size(const Program &program) {+ std::size_t code = 0;+ for (std::uint32_t pc = program.base; pc < program.end; pc += 4) code += program.is_code(pc);+ return code;+}++std::uint32_t word_at(const Snr2Module &m, std::uint32_t at) {+ std::uint32_t value;+ std::memcpy(&value, m.bytes.data() + at, 4);+ return value;+}++std::vector<std::pair<std::uint32_t, std::uint32_t>> checks_for(const Snr2Module &m, const Program &program) {+ std::vector<std::uint32_t> words;+ for (std::uint32_t pc = m.code_begin; pc + 4 <= m.code_end; pc += 4)+ if (program.is_code(pc) && !m.links.count(pc) && word_at(m, pc)) words.push_back(pc);+ std::vector<std::pair<std::uint32_t, std::uint32_t>> checks{{0, 0x32524E53u}};+ for (std::size_t k = 0; k < 8 && !words.empty(); ++k) {+ const std::uint32_t pc = words[k * words.size() / 8];+ if (checks.back().first != pc) checks.emplace_back(pc, word_at(m, pc));+ }+ return checks;+}++std::vector<Snr2Module> read_modules(const std::string &dir) {+ std::vector<std::filesystem::path> paths;+ for (const auto &entry : std::filesystem::directory_iterator(dir))+ if (entry.is_regular_file()) paths.push_back(entry.path());+ std::sort(paths.begin(), paths.end());+ std::vector<Snr2Module> modules;+ std::map<std::string, int> names;+ for (const auto &path : paths) {+ std::ifstream file(path, std::ios::binary);+ std::vector<std::uint8_t> bytes{std::istreambuf_iterator<char>(file), {}};+ auto module = read_snr2_module(std::move(bytes));+ if (!module) continue;+ if (std::any_of(modules.begin(), modules.end(), [&](const Snr2Module &m) { return m.bytes == module->bytes; })) continue;+ if (const int n = names[module->name]++) module->name += "_" + std::to_string(n + 1);+ modules.push_back(std::move(*module));+ }+ return modules;+}+int usage() {- std::fprintf(stderr, "usage: eerecomp <elf> [--entries file] [--probes file] [--out dir] [--stats]\n");+ std::fprintf(stderr, "usage: eerecomp <elf> [--entries file] [--modules dir] [--probes file] [--out dir] [--stats]\n");return 2;}@@ -33,7 +99,7 @@ int usage() {int main(int argc, char **argv) {if (argc < 2) return usage();- std::string elf_path = argv[1], entries_path, out, probes_path;+ std::string elf_path = argv[1], entries_path, out, probes_path, modules_dir;bool stats = false;for (int i = 2; i < argc; ++i) {const std::string arg = argv[i];@@ -41,29 +107,47 @@ int main(int argc, char **argv) {else if (arg == "--stats") stats = true;else if (arg == "--out" && i + 1 < argc) out = argv[++i];else if (arg == "--probes" && i + 1 < argc) probes_path = argv[++i];+ else if (arg == "--modules" && i + 1 < argc) modules_dir = 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;+ const Entries entries = entries_path.empty() ? Entries{} : read_entries(entries_path);+ const Links imports = snr2_imports(image);+ Program program = analyse(image, entries.main, dynamic_sites(imports));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];- }+ for (std::uint32_t pc = program.base; pc < program.end; pc += 4)+ if (program.is_code(pc)) ++histogram[decode(*image.word(pc), pc).op];+ const std::size_t code = code_size(program);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));++ std::vector<Translation> translations{{&image, &program, imports.empty() ? nullptr : &imports, "", false, "", 0, {}}};+ const std::vector<Snr2Module> modules = modules_dir.empty() ? std::vector<Snr2Module>{} : read_modules(modules_dir);+ std::deque<Image> images;+ std::deque<Program> programs;+ std::size_t module_code = 0;+ for (const Snr2Module &m : modules) {+ images.push_back(snr2_image(m));+ std::vector<std::uint32_t> seeds = m.seeds;+ if (const auto it = entries.modules.find(m.name); it != entries.modules.end()) seeds.insert(seeds.end(), it->second.begin(), it->second.end());+ programs.push_back(analyse(images.back(), seeds, dynamic_sites(m.links)));+ module_code += code_size(programs.back());+ translations.push_back({&images.back(), &programs.back(), &m.links, "m_" + m.name + "_", true, m.name, m.size, checks_for(m, programs.back())});+ }+ if (!modules.empty())+ std::printf("%zu modules, code %zu instructions (%zu KiB), %zu sites in the program linked at run time\n", modules.size(), module_code,+ module_code * 4 / 1024, imports.size());+if (!out.empty()) {CodegenOptions options;options.output = out;if (!probes_path.empty())- for (std::uint32_t a : read_entries(probes_path)) options.probes.insert(a);- generate(image, program, options);+ for (std::uint32_t a : read_entries(probes_path).main) options.probes.insert(a);+ generate(translations, options);}} catch (const std::exception &error) {std::fprintf(stderr, "eerecomp: %s\n", error.what());diff --git a/recomp/snr2.cpp b/recomp/snr2.cppnew file mode 100644index 0000000..c179274--- /dev/null+++ b/recomp/snr2.cpp@@ -0,0 +1,141 @@+#include "snr2.hpp"++#include <algorithm>+#include <cctype>+#include <cstring>++namespace recomp {++namespace {++constexpr std::uint32_t kMagic = 0x32524E53u;++std::uint32_t u32(const std::vector<std::uint8_t> &b, std::uint32_t o) {+ std::uint32_t v = 0;+ if (o <= b.size() && b.size() - o >= 4) std::memcpy(&v, b.data() + o, 4);+ return v;+}++std::uint16_t u16(const std::vector<std::uint8_t> &b, std::uint32_t o) {+ std::uint16_t v = 0;+ if (o <= b.size() && b.size() - o >= 2) std::memcpy(&v, b.data() + o, 2);+ return v;+}++std::optional<Fix> fix_of(std::uint32_t type) {+ switch (type) {+ case 2: return Fix::Word;+ case 4: return Fix::Jump;+ case 5: return Fix::High;+ case 6: return Fix::Low;+ default: return std::nullopt;+ }+}++std::string module_name(const std::vector<std::uint8_t> &b, std::uint32_t at) {+ std::string name;+ for (std::uint32_t o = at; at && o < b.size() && b[o]; ++o) name += static_cast<char>(b[o]);+ if (const auto slash = name.find_last_of("\\/"); slash != std::string::npos) name = name.substr(slash + 1);+ if (const auto dot = name.find('.'); dot != std::string::npos) name = name.substr(0, dot);+ std::string clean;+ for (char ch : name) clean += std::isalnum(static_cast<unsigned char>(ch)) ? ch : '_';+ return clean.empty() ? "module" : clean;+}++}++std::optional<Snr2Module> read_snr2_module(std::vector<std::uint8_t> bytes) {+ if (bytes.size() < 0x40 || u32(bytes, 0) != kMagic) return std::nullopt;+ Snr2Module m;+ const std::uint32_t rel = u32(bytes, 0x04), nrel = u32(bytes, 0x08), sym = u32(bytes, 0x0C), nsym = u32(bytes, 0x10);+ const std::uint32_t stream = u32(bytes, 0x34);+ m.size = u32(bytes, 0x2C);+ if (m.size > bytes.size() || rel + nrel * 12u > m.size || sym + nsym * 12u > m.size || stream >= m.size) return std::nullopt;+ m.name = module_name(bytes, u32(bytes, 0x14));++ m.code_begin = 0x40;+ while (m.code_begin < m.size && !u32(bytes, m.code_begin)) m.code_begin += 4;+ m.code_end = m.size;+ for (std::uint32_t table : {rel, sym, stream, u32(bytes, 0x14)})+ if (table > m.code_begin) m.code_end = std::min(m.code_end, table);++ for (std::uint32_t entry : {u32(bytes, 0x18), u32(bytes, 0x1C)})+ if (entry) m.seeds.push_back(entry);++ std::vector<std::uint8_t> kinds(nsym);+ for (std::uint32_t k = 0; k < nsym; ++k) kinds[k] = static_cast<std::uint8_t>(u32(bytes, sym + k * 12u + 8u) >> 16);++ std::vector<std::pair<std::uint32_t, std::uint32_t>> steps;+ for (std::uint32_t p = stream, at = 0; stream && p < m.size;) {+ const std::uint8_t b = bytes[p++];+ if (b & 0xFCu) {+ at += b & 0xFCu;+ } else {+ const std::uint32_t word = u32(bytes, p);+ p += 4;+ if (!word) break;+ at = word;+ }+ steps.emplace_back(b & 3u, at);+ }+ for (std::size_t k = 0; k < steps.size(); ++k) {+ const auto [type, at] = steps[k];+ switch (type) {+ case 0:+ m.links[at] = {Fix::Low, false, 0};+ break;+ case 1: {+ const auto low = std::find_if(steps.begin() + static_cast<std::ptrdiff_t>(k) + 1, steps.end(), [](const auto &s) { return s.first == 0; });+ if (low == steps.end()) break;+ const std::uint32_t value = (static_cast<std::uint32_t>(u16(bytes, at)) << 16) ++ static_cast<std::uint32_t>(static_cast<std::int16_t>(u16(bytes, low->second)));+ m.links[at] = {Fix::High, false, value};+ break;+ }+ case 2:+ m.links[at] = {Fix::Jump, false, 0};+ m.seeds.push_back((u32(bytes, at) & 0x03FFFFFFu) << 2);+ break;+ default:+ m.links[at] = {Fix::Word, false, 0};+ break;+ }+ }++ for (std::uint32_t k = 0; k < nrel; ++k) {+ const std::uint32_t at = u32(bytes, rel + k * 12u), info = u32(bytes, rel + k * 12u + 4u);+ const std::uint32_t symbol = info >> 8;+ const auto fix = fix_of(info & 0xFFu);+ if (!fix) continue;+ m.links[at] = {*fix, true, 0};+ if (symbol < nsym && (kinds[symbol] == 0 || kinds[symbol] == 2 || kinds[symbol] == 3) && *fix == Fix::Jump)+ m.seeds.push_back(u32(bytes, sym + symbol * 12u + 4u) + u32(bytes, rel + k * 12u + 8u));+ }+ m.bytes = std::move(bytes);+ m.bytes.resize(m.size);+ return m;+}++Links snr2_imports(const Image &image) {+ Links links;+ for (const auto &segment : image.segments()) {+ for (std::uint32_t o = 0; o + 0x40 <= segment.bytes.size(); o += 4) {+ if (u32(segment.bytes, o) != kMagic) continue;+ const std::uint32_t rel = u32(segment.bytes, o + 0x04), nrel = u32(segment.bytes, o + 0x08);+ if (!nrel || nrel > 0x100000u || !image.word(rel) || !image.word(rel + nrel * 12u - 4u)) continue;+ for (std::uint32_t k = 0; k < nrel; ++k) {+ const std::uint32_t at = *image.word(rel + k * 12u), info = *image.word(rel + k * 12u + 4u);+ const auto fix = fix_of(info & 0xFFu);+ if (fix && *fix != Fix::Word && image.executable(at)) links[at] = {*fix, true, 0};+ }+ return links;+ }+ }+ return links;+}++Image snr2_image(const Snr2Module &module) {+ return Image::raw(0, module.bytes, {module.code_begin, module.code_end});+}++}diff --git a/recomp/snr2.hpp b/recomp/snr2.hppnew file mode 100644index 0000000..c05eb9c--- /dev/null+++ b/recomp/snr2.hpp@@ -0,0 +1,37 @@+#pragma once++#include "elf.hpp"++#include <cstdint>+#include <map>+#include <optional>+#include <string>+#include <vector>++namespace recomp {++enum class Fix : std::uint8_t { Word, Jump, High, Low };++struct Link {+ Fix kind{};+ bool external{};+ std::uint32_t value{};+};++using Links = std::map<std::uint32_t, Link>;++struct Snr2Module {+ std::string name;+ std::vector<std::uint8_t> bytes;+ std::uint32_t size{};+ std::uint32_t code_begin{};+ std::uint32_t code_end{};+ Links links;+ std::vector<std::uint32_t> seeds;+};++std::optional<Snr2Module> read_snr2_module(std::vector<std::uint8_t> bytes);+Links snr2_imports(const Image &image);+Image snr2_image(const Snr2Module &module);++}diff --git a/runtime/include/ps2/cpu.hpp b/runtime/include/ps2/cpu.hppindex 80d3476..bab0b9e 100644--- a/runtime/include/ps2/cpu.hpp+++ b/runtime/include/ps2/cpu.hpp@@ -57,7 +57,7 @@ struct Cpu {std::uint32_t sa{};std::uint32_t pc{};std::int32_t budget{};- std::uint32_t pad0{};+ std::uint32_t base{};std::uint32_t f[32];std::uint32_t facc{};diff --git a/runtime/include/ps2/generated.hpp b/runtime/include/ps2/generated.hppindex 584ba9d..3d473fb 100644--- a/runtime/include/ps2/generated.hpp+++ b/runtime/include/ps2/generated.hpp@@ -21,7 +21,17 @@ struct UnitInfo {const std::uint16_t *entries;};+struct ModuleInfo {+ const char *name;+ std::uint32_t size;+ const std::uint32_t *checks;+ std::uint32_t check_count;+ const UnitInfo *units;+ std::uint32_t count;+};+void register_units(const UnitInfo *units, std::uint32_t count);+void register_modules(const ModuleInfo *modules, 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);diff --git a/runtime/src/dispatch.cpp b/runtime/src/dispatch.cppindex 165d4c1..654a6da 100644--- a/runtime/src/dispatch.cpp+++ b/runtime/src/dispatch.cpp@@ -17,6 +17,55 @@ std::vector<const UnitInfo *> g_index;std::uint32_t g_origin = 0;std::uint32_t g_unit_size = 1;std::set<std::uint32_t> g_misses;+std::set<std::string> g_module_misses;++constexpr std::uint32_t kModuleMagic = 0x32524E53u;++struct Instance {+ std::uint32_t base;+ const ModuleInfo *module;+};++const ModuleInfo *g_modules = nullptr;+std::uint32_t g_module_count = 0;+std::uint32_t g_module_span = 0;+std::vector<Instance> g_instances;++bool loaded_at(const ModuleInfo &m, std::uint32_t base) {+ if (ld32(base + 0x2Cu) != m.size) return false;+ for (std::uint32_t k = 0; k < m.check_count; ++k)+ if (ld32(base + m.checks[2 * k]) != m.checks[2 * k + 1]) return false;+ return true;+}++const Instance *instance_for(std::uint32_t pc) {+ for (std::size_t k = 0; k < g_instances.size(); ++k) {+ const Instance &i = g_instances[k];+ if (pc - i.base >= i.module->size) continue;+ if (loaded_at(*i.module, i.base)) return &i;+ g_instances.erase(g_instances.begin() + static_cast<std::ptrdiff_t>(k));+ break;+ }+ if (!g_module_count || pc >= kRamSize) return nullptr;+ for (std::uint32_t at = pc & ~15u; pc - at < g_module_span; at -= 16) {+ if (ld32(at) == kModuleMagic) {+ for (std::uint32_t k = 0; k < g_module_count; ++k)+ if (pc - at < g_modules[k].size && loaded_at(g_modules[k], at)) {+ g_instances.push_back({at, &g_modules[k]});+ return &g_instances.back();+ }+ return nullptr;+ }+ if (at < 16) break;+ }+ return nullptr;+}++const UnitInfo *module_unit(const ModuleInfo &m, std::uint32_t offset) {+ for (std::uint32_t k = 0; k < m.count; ++k)+ if (offset - m.units[k].base < m.units[k].size) return &m.units[k];+ return nullptr;+}const UnitInfo *unit_for(std::uint32_t pc) {const std::uint32_t slot = (pc - g_origin) / g_unit_size;@@ -25,6 +74,13 @@ const UnitInfo *unit_for(std::uint32_t pc) {}void record_miss(std::uint32_t pc) {+ if (const Instance *m = instance_for(pc)) {+ char token[96];+ std::snprintf(token, sizeof token, "%s:%x", m->module->name, pc - m->base);+ if (!g_module_misses.insert(token).second || g_options.misses_path.empty()) return;+ std::ofstream(g_options.misses_path, std::ios::app) << token << "\n";+ return;+ }if (!g_misses.insert(pc).second || g_options.misses_path.empty()) return;std::ofstream out(g_options.misses_path, std::ios::app);out << std::hex << pc << "\n";@@ -42,9 +98,19 @@ void register_units(const UnitInfo *units, std::uint32_t count) {for (const UnitInfo &u : g_units) g_index[(u.base - g_origin) / g_unit_size] = &u;}+void register_modules(const ModuleInfo *modules, std::uint32_t count) {+ g_modules = modules;+ g_module_count = count;+ g_instances.clear();+ for (std::uint32_t k = 0; k < count; ++k) g_module_span = std::max(g_module_span, modules[k].size);+}+bool has_code(std::uint32_t pc) {const UnitInfo *u = unit_for(pc);- return u && u->entries[(pc - u->base) >> 2] != 0;+ if (u && u->entries[(pc - u->base) >> 2] != 0) return true;+ const Instance *m = instance_for(pc);+ const UnitInfo *mu = m ? module_unit(*m->module, pc - m->base) : nullptr;+ return mu && mu->entries[(pc - m->base - mu->base) >> 2] != 0;}void dispatch(Cpu &c, std::uint32_t, std::uint32_t depth) {@@ -54,6 +120,16 @@ void dispatch(Cpu &c, std::uint32_t, std::uint32_t depth) {const UnitInfo *u = unit_for(pc);const std::uint16_t id = u ? u->entries[(pc - u->base) >> 2] : 0;if (!id) {+ if (const Instance *m = instance_for(pc)) {+ const std::uint32_t base = m->base;+ const UnitInfo *mu = module_unit(*m->module, pc - base);+ const std::uint16_t entry = mu ? mu->entries[(pc - base - mu->base) >> 2] : 0;+ if (entry) {+ c.base = base;+ mu->function(c, entry, depth);+ return;+ }+ }record_miss(pc);char reason[64];std::snprintf(reason, sizeof reason, "missing code at 0x%08X (ra 0x%08X)", pc, c.r[31].w[0]);diff --git a/scripts/disc.py b/scripts/disc.pyindex da9ff8c..474ecef 100644--- a/scripts/disc.py+++ b/scripts/disc.py@@ -109,9 +109,34 @@ def extract(image, destination, patterns, limit):print('%d files extracted to %s' % (count, destination))+def read_at(image, sector, offset, size):+ image.file.seek(sector * SECTOR + offset)+ return image.file.read(size)+++def modules(image, destination):+ found = []+ for path, _, sector, size in image.files():+ head = read_at(image, sector, 0, 8)+ if head[:4] == b'SNR2':+ found.append((path.replace('/', '_'), sector, 0, size))+ elif head[:4] == b'AFS\0':+ count = struct.unpack_from('<I', head, 4)[0]+ table = read_at(image, sector, 8, count * 8)+ for index in range(count):+ offset, length = struct.unpack_from('<II', table, index * 8)+ if offset + 4 <= size and read_at(image, sector, offset, 4) == b'SNR2':+ found.append(('%s_%04d' % (path.replace('/', '_'), index), sector, offset, length))+ os.makedirs(destination, exist_ok=True)+ for name, sector, offset, length in found:+ with open(os.path.join(destination, name + '.snr'), 'wb') as out:+ out.write(read_at(image, sector, offset, length))+ print('%d SNR2 modules extracted to %s' % (len(found), destination))++def main():parser = argparse.ArgumentParser(description='Read files from PS2 CD and DVD images, including both layers of a DVD9.')- parser.add_argument('command', choices=['info', 'list', 'extract'])+ parser.add_argument('command', choices=['info', 'list', 'extract', 'modules'])parser.add_argument('image')parser.add_argument('destination', nargs='?')parser.add_argument('patterns', nargs='*', help='paths or globs to extract, everything below --limit if omitted')@@ -122,6 +147,10 @@ def main():info(image)elif args.command == 'list':listing(image)+ elif args.command == 'modules':+ if not args.destination:+ parser.error('modules needs a destination folder')+ modules(image, args.destination)else:if not args.destination:parser.error('extract needs a destination folder')diff --git a/scripts/port.sh b/scripts/port.shindex e4a85bd..788ba5c 100755--- a/scripts/port.sh+++ b/scripts/port.sh@@ -88,8 +88,13 @@ elif python3 -I "$here/disc.py" list "$iso" | awk '{print $4}' | grep -qx CORE.Gelf="$game/core.elf"fiecho "${elf#$ROOT/}"+modules=()+if grep -q SNR2 "$elf"; then+ python3 -I "$here/disc.py" modules "$iso" "$game/modules"+ modules=(--modules "$game/modules")+fi-translate() { "$tools/eerecomp" "$elf" --entries "$game/entries.txt" --out "$generated" | tail -1; }+translate() { "$tools/eerecomp" "$elf" --entries "$game/entries.txt" "${modules[@]}" --out "$generated" | tail -1; }echo "== [3/6] machine code to C++"touch "$game/entries.txt"@@ -111,12 +116,15 @@ if [ "$explore" = 1 ]; thenadded=$(python3 -I - "$game/entries.txt" "$game/misses.txt" <<'EOF'import os, sysentries, misses = sys.argv[1], sys.argv[2]-known = {int(t, 16) for t in open(entries).read().split()}-found = [int(t, 16) for t in open(misses).read().split()] if os.path.exists(misses) else []-new = sorted({a for a in found if a not in known})+def token(t):+ name, _, at = t.rpartition(':')+ return (name + ':' if name else '') + '%x' % int(at, 16)+known = {token(t) for t in open(entries).read().split()}+found = [token(t) for t in open(misses).read().split()] if os.path.exists(misses) else []+new = sorted({t for t in found if t not in known})with open(entries, 'a') as out:- for a in new:- out.write('%x\n' % a)+ for t in new:+ out.write(t + '\n')print(len(new))EOF)