PS2 games in the browser: static recompilation to WebAssembly with a native runtime
C++
recomp: decode R5900 code and discover what is reachable
Samir Nuri67a050989f5b
diff --git a/CMakeLists.txt b/CMakeLists.txtnew file mode 100644index 0000000..d412c8e--- /dev/null+++ b/CMakeLists.txt@@ -0,0 +1,15 @@+cmake_minimum_required(VERSION 3.20)+project(ps2-web-recomp CXX)++set(CMAKE_CXX_STANDARD 20)+set(CMAKE_CXX_STANDARD_REQUIRED ON)++if(NOT EMSCRIPTEN)+ add_executable(eerecomp+ recomp/main.cpp+ recomp/r5900.cpp+ recomp/elf.cpp+ recomp/analysis.cpp+ recomp/codegen.cpp)+ target_compile_options(eerecomp PRIVATE -Wall -Wextra)+endif()diff --git a/recomp/analysis.cpp b/recomp/analysis.cppnew file mode 100644index 0000000..818f785--- /dev/null+++ b/recomp/analysis.cpp@@ -0,0 +1,184 @@+#include "analysis.hpp"++#include <algorithm>+#include <deque>++namespace recomp {++namespace {++constexpr std::size_t kTentativeLimit = 16384;++class Explorer {+public:+ Explorer(const Image &image, Program &program) : image_(image), p_(program) {}++ bool valid_target(std::uint32_t address) const {+ return (address & 3u) == 0 && address >= p_.base && address < p_.end && image_.executable(address);+ }++ void strong(std::uint32_t address, bool function) {+ if (!valid_target(address)) return;+ if (function) p_.functions.insert(address);+ p_.entries.insert(address);+ if (!p_.is_code(address)) queue_.push_back(address);+ }++ void drain() {+ while (!queue_.empty()) {+ const std::uint32_t start = queue_.front();+ queue_.pop_front();+ walk(start);+ }+ }++ bool tentative(std::uint32_t start) {+ std::deque<std::uint32_t> pending{start};+ std::set<std::uint32_t> seen;+ while (!pending.empty()) {+ std::uint32_t pc = pending.front();+ pending.pop_front();+ while (true) {+ if (!valid_target(pc)) return false;+ if (p_.is_code(pc) || seen.count(pc)) break;+ if (seen.size() > kTentativeLimit) return false;+ seen.insert(pc);+ const Insn insn = decode(*image_.word(pc), pc);+ if (insn.op == Op::Invalid) return false;+ const Flow f = flow(insn.op);+ if (f == Flow::Stop) break;+ if (f == Flow::None) { pc += 4; continue; }+ 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 (!valid_target(insn.branch_target_of())) return false;+ pending.push_back(insn.branch_target_of());+ }+ if (f == Flow::Jump || f == Flow::JumpRegister) break;+ pc += 8;+ }+ }+ return true;+ }++private:+ void walk(std::uint32_t pc) {+ while (valid_target(pc) && !p_.is_code(pc)) {+ const Insn insn = decode(*image_.word(pc), pc);+ if (insn.op == Op::Invalid) return;+ mark(pc);+ const Flow f = flow(insn.op);+ if (f == Flow::Stop) return;+ if (insn.op == Op::Syscall) p_.entries.insert(pc + 4);+ if (f == Flow::None) { pc += 4; continue; }+ if (valid_target(pc + 4) && decode(*image_.word(pc + 4), pc + 4).op != Op::Invalid) mark(pc + 4);+ switch (f) {+ case Flow::Branch:+ case Flow::BranchLikely:+ local(insn.branch_target_of(), pc);+ break;+ case Flow::Jump:+ local(insn.branch_target_of(), pc);+ return;+ case Flow::Call:+ strong(insn.branch_target_of(), true);+ p_.entries.insert(pc + 8);+ break;+ case Flow::CallRegister:+ p_.entries.insert(pc + 8);+ break;+ case Flow::JumpRegister:+ return;+ default:+ break;+ }+ pc += 8;+ }+ }++ void local(std::uint32_t target, std::uint32_t from) {+ if (!valid_target(target)) return;+ if (target <= from) p_.entries.insert(target);+ if (!p_.is_code(target)) queue_.push_back(target);+ }++ void mark(std::uint32_t pc) { p_.code[(pc - p_.base) >> 2] = 1; }++ const Image &image_;+ Program &p_;+ std::deque<std::uint32_t> queue_;+};++std::vector<std::uint32_t> address_constants(const Image &image, const Program &p) {+ std::vector<std::uint32_t> found;+ for (std::uint32_t pc = p.base; pc < p.end; pc += 4) {+ if (!p.is_code(pc)) continue;+ const Insn lui = decode(*image.word(pc), pc);+ if (lui.op != Op::Lui) continue;+ for (std::uint32_t next = pc + 4; next < pc + 40 && p.is_code(next); next += 4) {+ const Insn insn = decode(*image.word(next), next);+ if ((insn.op == Op::Addiu || insn.op == Op::Ori) && insn.rs() == lui.rt()) {+ const std::uint32_t high = lui.uimm() << 16;+ found.push_back(insn.op == Op::Addiu ? high + static_cast<std::uint32_t>(insn.simm()) : high | insn.uimm());+ }+ if (insn.rt() == lui.rt() && insn.op == Op::Lui) break;+ }+ }+ return found;+}++}++Program analyse(const Image &image, const std::vector<std::uint32_t> &extra_entries) {+ Program p;+ p.base = UINT32_MAX;+ for (const auto &segment : image.segments()) {+ if (!segment.executable) continue;+ p.base = std::min(p.base, segment.address & ~3u);+ p.end = std::max<std::uint32_t>(p.end, segment.address + static_cast<std::uint32_t>(segment.bytes.size()));+ }+ p.end &= ~3u;+ p.code.assign((p.end - p.base) >> 2, 0);++ Explorer explorer(image, p);+ explorer.strong(image.entry(), true);+ for (const auto &[address, name] : image.symbols()) explorer.strong(address, true);+ for (std::uint32_t address : extra_entries) explorer.strong(address, true);+ explorer.drain();++ std::uint32_t text_begin = p.end, text_end = p.base;+ for (std::uint32_t pc = p.base; pc < p.end; pc += 4) {+ if (p.is_code(pc)) {+ text_begin = std::min(text_begin, pc);+ text_end = pc + 4;+ }+ }++ for (int round = 0; round < 8; ++round) {+ std::vector<std::uint32_t> candidates = address_constants(image, p);+ for (const auto &segment : image.segments())+ for (std::size_t offset = 0; offset + 4 <= segment.bytes.size(); offset += 4)+ candidates.push_back(*image.word(segment.address + static_cast<std::uint32_t>(offset)));+ std::sort(candidates.begin(), candidates.end());+ candidates.erase(std::unique(candidates.begin(), candidates.end()), candidates.end());++ std::size_t added = 0;+ for (std::uint32_t target : candidates) {+ if (target < text_begin || target >= text_end || !explorer.valid_target(target)) continue;+ if (p.entries.count(target) || p.rejected.count(target)) continue;+ if (p.is_code(target) || explorer.tentative(target)) {+ p.entries.insert(target);+ if (!p.is_code(target)) explorer.strong(target, false);+ ++added;+ } else {+ p.rejected.insert(target);+ }+ }+ explorer.drain();+ if (!added) break;+ }+ return p;+}++}diff --git a/recomp/analysis.hpp b/recomp/analysis.hppnew file mode 100644index 0000000..fda45a4--- /dev/null+++ b/recomp/analysis.hpp@@ -0,0 +1,27 @@+#pragma once++#include "elf.hpp"+#include "r5900.hpp"++#include <cstdint>+#include <set>+#include <vector>++namespace recomp {++struct Program {+ std::uint32_t base{};+ std::uint32_t end{};+ std::vector<std::uint8_t> code;+ std::set<std::uint32_t> functions;+ std::set<std::uint32_t> entries;+ std::set<std::uint32_t> rejected;++ 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);++}diff --git a/recomp/elf.cpp b/recomp/elf.cppnew file mode 100644index 0000000..9a55104--- /dev/null+++ b/recomp/elf.cpp@@ -0,0 +1,81 @@+#include "elf.hpp"++#include <cstring>+#include <fstream>+#include <iterator>+#include <stdexcept>++namespace recomp {++namespace {++template <typename T>+T read(const std::vector<std::uint8_t> &data, std::size_t offset) {+ if (offset + sizeof(T) > data.size()) throw std::runtime_error("truncated ELF file");+ T value;+ std::memcpy(&value, data.data() + offset, sizeof(T));+ return value;+}++}++Image Image::load(const std::string &path) {+ std::ifstream file(path, std::ios::binary);+ if (!file) throw std::runtime_error("cannot open " + path);+ const std::vector<std::uint8_t> data{std::istreambuf_iterator<char>(file), {}};+ if (data.size() < 52 || std::memcmp(data.data(), "\x7f" "ELF\x01\x01", 6) != 0)+ throw std::runtime_error(path + " is not a little endian ELF32 file");++ Image image;+ image.entry_ = read<std::uint32_t>(data, 24);+ const auto phoff = read<std::uint32_t>(data, 28);+ const auto shoff = read<std::uint32_t>(data, 32);+ const auto phentsize = read<std::uint16_t>(data, 42), phnum = read<std::uint16_t>(data, 44);+ const auto shentsize = read<std::uint16_t>(data, 46), shnum = read<std::uint16_t>(data, 48);++ for (std::uint32_t i = 0; i < phnum; ++i) {+ const std::size_t ph = phoff + i * phentsize;+ if (read<std::uint32_t>(data, ph) != 1) continue;+ const auto offset = read<std::uint32_t>(data, ph + 4), vaddr = read<std::uint32_t>(data, ph + 8);+ const auto filesz = read<std::uint32_t>(data, ph + 16), memsz = read<std::uint32_t>(data, ph + 20);+ const auto flags = read<std::uint32_t>(data, ph + 24);+ if (offset + filesz > data.size()) throw std::runtime_error("segment outside the file");+ Segment segment{vaddr, std::vector<std::uint8_t>(memsz), (flags & 1u) != 0};+ std::memcpy(segment.bytes.data(), data.data() + offset, filesz);+ image.segments_.push_back(std::move(segment));+ }++ for (std::uint32_t i = 0; shoff && i < shnum; ++i) {+ const std::size_t sh = shoff + i * shentsize;+ if (read<std::uint32_t>(data, sh + 4) != 2) continue;+ const auto symoff = read<std::uint32_t>(data, sh + 16), symsize = read<std::uint32_t>(data, sh + 20);+ const auto link = read<std::uint32_t>(data, sh + 24);+ const auto stroff = read<std::uint32_t>(data, shoff + link * shentsize + 16);+ for (std::size_t s = symoff; s + 16 <= symoff + symsize; s += 16) {+ const auto name = read<std::uint32_t>(data, s), value = read<std::uint32_t>(data, s + 4);+ if ((data[s + 12] & 15u) != 2u || !value) continue;+ image.symbols_[value] = reinterpret_cast<const char *>(data.data() + stroff + name);+ }+ }+ 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()) {+ std::uint32_t value;+ std::memcpy(&value, segment.bytes.data() + (address - segment.address), 4);+ return value;+ }+ }+ return std::nullopt;+}++bool Image::executable(std::uint32_t address) const {+ for (const auto &segment : segments_)+ if (segment.executable && address >= segment.address && address - segment.address < segment.bytes.size())+ return true;+ return false;+}++}diff --git a/recomp/elf.hpp b/recomp/elf.hppnew file mode 100644index 0000000..957b680--- /dev/null+++ b/recomp/elf.hpp@@ -0,0 +1,35 @@+#pragma once++#include <cstdint>+#include <map>+#include <optional>+#include <string>+#include <vector>++namespace recomp {++struct Segment {+ std::uint32_t address{};+ std::vector<std::uint8_t> bytes;+ bool executable{};+};++class Image {+public:+ static Image load(const std::string &path);++ std::uint32_t entry() const { return entry_; }+ const std::vector<Segment> &segments() const { return segments_; }+ const std::map<std::uint32_t, std::string> &symbols() const { return symbols_; }++ std::optional<std::uint32_t> word(std::uint32_t address) const;+ bool executable(std::uint32_t address) const;+ void add_symbol(std::uint32_t address, std::string name) { symbols_[address] = std::move(name); }++private:+ std::uint32_t entry_{};+ std::vector<Segment> segments_;+ std::map<std::uint32_t, std::string> symbols_;+};++}diff --git a/recomp/r5900.cpp b/recomp/r5900.cppnew file mode 100644index 0000000..6978e6d--- /dev/null+++ b/recomp/r5900.cpp@@ -0,0 +1,254 @@+#include "r5900.hpp"++#include <array>+#include <cstdio>++namespace recomp {++namespace {++using enum Op;++constexpr std::array<Op, 64> kPrimary = {+ Invalid, Invalid, J, Jal, Beq, Bne, Blez, Bgtz,+ Addi, Addiu, Slti, Sltiu, Andi, Ori, Xori, Lui,+ Invalid, Invalid, Invalid, Invalid, Beql, Bnel, Blezl, Bgtzl,+ Daddi, Daddiu, Ldl, Ldr, Invalid, Invalid, Lq, Sq,+ Lb, Lh, Lwl, Lw, Lbu, Lhu, Lwr, Lwu,+ Sb, Sh, Swl, Sw, Sdl, Sdr, Swr, Cache,+ Invalid, Lwc1, Invalid, Pref, Invalid, Invalid, Lqc2, Ld,+ Invalid, Swc1, Invalid, Invalid, Invalid, Invalid, Sqc2, Sd,+};++constexpr std::array<Op, 64> kSpecial = {+ Sll, Invalid, Srl, Sra, Sllv, Invalid, Srlv, Srav,+ Jr, Jalr, Movz, Movn, Syscall, Break, Invalid, Sync,+ Mfhi, Mthi, Mflo, Mtlo, Dsllv, Invalid, Dsrlv, Dsrav,+ Mult, Multu, Div, Divu, Invalid, Invalid, Invalid, Invalid,+ Add, Addu, Sub, Subu, And, Or, Xor, Nor,+ Mfsa, Mtsa, Slt, Sltu, Dadd, Daddu, Dsub, Dsubu,+ Tge, Tgeu, Tlt, Tltu, Teq, Invalid, Tne, Invalid,+ Dsll, Invalid, Dsrl, Dsra, Dsll32, Invalid, Dsrl32, Dsra32,+};++constexpr std::array<Op, 32> kRegimm = {+ Bltz, Bgez, Bltzl, Bgezl, Invalid, Invalid, Invalid, Invalid,+ Tgei, Tgeiu, Tlti, Tltiu, Teqi, Invalid, Tnei, Invalid,+ Bltzal, Bgezal, Bltzall, Bgezall, Invalid, Invalid, Invalid, Invalid,+ Mtsab, Mtsah, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid,+};++constexpr std::array<Op, 64> kMmi = {+ Madd, Maddu, Invalid, Invalid, Plzcw, Invalid, Invalid, Invalid,+ Invalid, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid,+ Mfhi1, Mthi1, Mflo1, Mtlo1, Invalid, Invalid, Invalid, Invalid,+ Mult1, Multu1, Div1, Divu1, Invalid, Invalid, Invalid, Invalid,+ Madd1, Maddu1, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid,+ Invalid, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid,+ Pmfhl, Pmthl, Invalid, Invalid, Psllh, Invalid, Psrlh, Psrah,+ Invalid, Invalid, Invalid, Invalid, Psllw, Invalid, Psrlw, Psraw,+};++constexpr std::array<Op, 32> kMmi0 = {+ Paddw, Psubw, Pcgtw, Pmaxw, Paddh, Psubh, Pcgth, Pmaxh,+ Paddb, Psubb, Pcgtb, Invalid, Invalid, Invalid, Invalid, Invalid,+ Paddsw, Psubsw, Pextlw, Ppacw, Paddsh, Psubsh, Pextlh, Ppach,+ Paddsb, Psubsb, Pextlb, Ppacb, Invalid, Invalid, Pext5, Ppac5,+};++constexpr std::array<Op, 32> kMmi1 = {+ Invalid, Pabsw, Pceqw, Pminw, Padsbh, Pabsh, Pceqh, Pminh,+ Invalid, Invalid, Pceqb, Invalid, Invalid, Invalid, Invalid, Invalid,+ Padduw, Psubuw, Pextuw, Invalid, Padduh, Psubuh, Pextuh, Invalid,+ Paddub, Psubub, Pextub, Qfsrv, Invalid, Invalid, Invalid, Invalid,+};++constexpr std::array<Op, 32> kMmi2 = {+ Pmaddw, Invalid, Psllvw, Psrlvw, Pmsubw, Invalid, Invalid, Invalid,+ Pmfhi, Pmflo, Pinth, Invalid, Pmultw, Pdivw, Pcpyld, Invalid,+ Pmaddh, Phmadh, Pand, Pxor, Pmsubh, Phmsbh, Invalid, Invalid,+ Invalid, Invalid, Pexeh, Prevh, Pmulth, Pdivbw, Pexew, Prot3w,+};++constexpr std::array<Op, 32> kMmi3 = {+ Pmadduw, Invalid, Invalid, Psravw, Invalid, Invalid, Invalid, Invalid,+ Pmthi, Pmtlo, Pinteh, Invalid, Pmultuw, Pdivuw, Pcpyud, Invalid,+ Invalid, Invalid, Por, Pnor, Invalid, Invalid, Invalid, Invalid,+ Invalid, Invalid, Pexch, Pcpyh, Invalid, Invalid, Pexcw, Invalid,+};++constexpr std::array<Op, 64> kCop1S = {+ AddS, SubS, MulS, DivS, SqrtS, AbsS, MovS, NegS,+ Invalid, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid,+ Invalid, Invalid, Invalid, Invalid, Invalid, Invalid, RsqrtS, Invalid,+ AddaS, SubaS, MulaS, Invalid, MaddS, MsubS, MaddaS, MsubaS,+ Invalid, Invalid, Invalid, Invalid, CvtWS, Invalid, Invalid, Invalid,+ MaxS, MinS, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid,+ CF, Invalid, CEq, Invalid, CLt, Invalid, CLe, Invalid,+ Invalid, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid,+};++constexpr std::array<Op, 64> kVector = {+ Vaddbc, Vaddbc, Vaddbc, Vaddbc, Vsubbc, Vsubbc, Vsubbc, Vsubbc,+ Vmaddbc, Vmaddbc, Vmaddbc, Vmaddbc, Vmsubbc, Vmsubbc, Vmsubbc, Vmsubbc,+ Vmaxbc, Vmaxbc, Vmaxbc, Vmaxbc, Vminibc, Vminibc, Vminibc, Vminibc,+ Vmulbc, Vmulbc, Vmulbc, Vmulbc, Vmulq, Vmaxi, Vmuli, Vminii,+ Vaddq, Vmaddq, Vaddi, Vmaddi, Vsubq, Vmsubq, Vsubi, Vmsubi,+ Vadd, Vmadd, Vmul, Vmax, Vsub, Vmsub, Vopmsub, Vmini,+ Viadd, Visub, Viaddi, Invalid, Viand, Vior, Invalid, Invalid,+ Vcallms, Vcallmsr, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid,+};++constexpr std::array<std::array<Op, 32>, 4> kVectorX = {{+ {Vaddabc, Vsubabc, Vmaddabc, Vmsubabc, Vitof0, Vftoi0, Vmulabc, Vmulaq,+ Vaddaq, Vsubaq, Vadda, Vsuba, Vmove, Vlqi, Vdiv, Vmtir,+ Vrnext, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid,+ Invalid, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid},+ {Vaddabc, Vsubabc, Vmaddabc, Vmsubabc, Vitof4, Vftoi4, Vmulabc, Vabs,+ Vmaddaq, Vmsubaq, Vmadda, Vmsuba, Vmr32, Vsqi, Vsqrt, Vmfir,+ Vrget, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid,+ Invalid, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid},+ {Vaddabc, Vsubabc, Vmaddabc, Vmsubabc, Vitof12, Vftoi12, Vmulabc, Vmulai,+ Vaddai, Vsubai, Vmula, Vopmula, Invalid, Vlqd, Vrsqrt, Vilwr,+ Vrinit, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid,+ Invalid, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid},+ {Vaddabc, Vsubabc, Vmaddabc, Vmsubabc, Vitof15, Vftoi15, Vmulabc, Vclip,+ Vmaddai, Vmsubai, Invalid, Vnop, Invalid, Vsqd, Vwaitq, Viswr,+ Vrxor, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid,+ Invalid, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid, Invalid},+}};++Op decode_mmi(std::uint32_t word) {+ const std::uint32_t funct = word & 63u, sub = (word >> 6) & 31u;+ switch (funct) {+ case 8: return kMmi0[sub];+ case 40: return kMmi1[sub];+ case 9: return kMmi2[sub];+ case 41: return kMmi3[sub];+ case 48: return sub <= 4 ? Pmfhl : Invalid;+ case 49: return sub == 0 ? Pmthl : Invalid;+ default: return kMmi[funct];+ }+}++Op decode_cop0(std::uint32_t word) {+ const std::uint32_t rs = (word >> 21) & 31u;+ if (rs == 0) return Mfc0;+ if (rs == 4) return Mtc0;+ if (rs == 8) {+ constexpr Op kBc[4] = {Bc0f, Bc0t, Bc0fl, Bc0tl};+ const std::uint32_t rt = (word >> 16) & 31u;+ return rt < 4 ? kBc[rt] : Invalid;+ }+ if (rs == 16) {+ switch (word & 63u) {+ case 1: return Tlbr;+ case 2: return Tlbwi;+ case 6: return Tlbwr;+ case 8: return Tlbp;+ case 24: return Eret;+ case 56: return Ei;+ case 57: return Di;+ default: return Invalid;+ }+ }+ return Invalid;+}++Op decode_cop1(std::uint32_t word) {+ switch ((word >> 21) & 31u) {+ case 0: return Mfc1;+ case 2: return Cfc1;+ case 4: return Mtc1;+ case 6: return Ctc1;+ case 8: {+ constexpr Op kBc[4] = {Bc1f, Bc1t, Bc1fl, Bc1tl};+ const std::uint32_t rt = (word >> 16) & 31u;+ return rt < 4 ? kBc[rt] : Invalid;+ }+ case 16: return kCop1S[word & 63u];+ case 20: return (word & 63u) == 32u ? CvtSW : Invalid;+ default: return Invalid;+ }+}++Op decode_cop2(std::uint32_t word) {+ const std::uint32_t rs = (word >> 21) & 31u;+ if (rs >= 16) {+ const std::uint32_t funct = word & 63u;+ if (funct >= 60) return kVectorX[funct & 3u][(word >> 6) & 31u];+ return kVector[funct];+ }+ switch (rs) {+ case 1: return Qmfc2;+ case 2: return Cfc2;+ case 5: return Qmtc2;+ case 6: return Ctc2;+ case 8: {+ constexpr Op kBc[4] = {Bc2f, Bc2t, Bc2fl, Bc2tl};+ const std::uint32_t rt = (word >> 16) & 31u;+ return rt < 4 ? kBc[rt] : Invalid;+ }+ default: return Invalid;+ }+}++constexpr const char *kNames[] = {+#define X(name) #name,+ R5900_OPS(X)+#undef X+};++}++Insn decode(std::uint32_t word, std::uint32_t pc) {+ Insn insn{pc, word, Invalid};+ switch (word >> 26) {+ case 0: insn.op = kSpecial[word & 63u]; break;+ case 1: insn.op = kRegimm[(word >> 16) & 31u]; break;+ case 16: insn.op = decode_cop0(word); break;+ case 17: insn.op = decode_cop1(word); break;+ case 18: insn.op = decode_cop2(word); break;+ case 28: insn.op = decode_mmi(word); break;+ default: insn.op = kPrimary[word >> 26]; break;+ }+ return insn;+}++std::uint32_t Insn::branch_target_of() const {+ return op == J || op == Jal ? jump_target() : branch_target();+}++const char *name(Op op) { return kNames[static_cast<std::size_t>(op)]; }++Flow flow(Op op) {+ switch (op) {+ case Beq: case Bne: case Blez: case Bgtz: case Bltz: case Bgez:+ case Bc0f: case Bc0t: case Bc1f: case Bc1t: case Bc2f: case Bc2t:+ return Flow::Branch;+ case Beql: case Bnel: case Blezl: case Bgtzl: case Bltzl: case Bgezl:+ case Bc0fl: case Bc0tl: case Bc1fl: case Bc1tl: case Bc2fl: case Bc2tl:+ return Flow::BranchLikely;+ case Bltzal: case Bgezal: case Bltzall: case Bgezall:+ case Jal:+ return Flow::Call;+ case J: return Flow::Jump;+ case Jr: return Flow::JumpRegister;+ case Jalr: return Flow::CallRegister;+ case Eret: case Invalid: return Flow::Stop;+ default: return Flow::None;+ }+}++bool has_delay_slot(Op op) {+ const Flow f = flow(op);+ return f != Flow::None && f != Flow::Stop;+}++std::string disassemble(const Insn &insn) {+ char text[96];+ std::snprintf(text, sizeof text, "%08X: %08X %s", insn.pc, insn.word, name(insn.op));+ return text;+}++}diff --git a/recomp/r5900.hpp b/recomp/r5900.hppnew file mode 100644index 0000000..0004042--- /dev/null+++ b/recomp/r5900.hpp@@ -0,0 +1,86 @@+#pragma once++#include <cstdint>+#include <string>++namespace recomp {++#define R5900_OPS(X) \+ X(Invalid) \+ X(Sll) X(Srl) X(Sra) X(Sllv) X(Srlv) X(Srav) X(Jr) X(Jalr) X(Movz) X(Movn) X(Syscall) X(Break) X(Sync) \+ X(Mfhi) X(Mthi) X(Mflo) X(Mtlo) X(Dsllv) X(Dsrlv) X(Dsrav) X(Mult) X(Multu) X(Div) X(Divu) \+ X(Add) X(Addu) X(Sub) X(Subu) X(And) X(Or) X(Xor) X(Nor) X(Mfsa) X(Mtsa) X(Slt) X(Sltu) \+ X(Dadd) X(Daddu) X(Dsub) X(Dsubu) X(Tge) X(Tgeu) X(Tlt) X(Tltu) X(Teq) X(Tne) \+ X(Dsll) X(Dsrl) X(Dsra) X(Dsll32) X(Dsrl32) X(Dsra32) \+ X(Bltz) X(Bgez) X(Bltzl) X(Bgezl) X(Tgei) X(Tgeiu) X(Tlti) X(Tltiu) X(Teqi) X(Tnei) \+ X(Bltzal) X(Bgezal) X(Bltzall) X(Bgezall) X(Mtsab) X(Mtsah) \+ X(J) X(Jal) X(Beq) X(Bne) X(Blez) X(Bgtz) X(Addi) X(Addiu) X(Slti) X(Sltiu) X(Andi) X(Ori) X(Xori) X(Lui) \+ X(Beql) X(Bnel) X(Blezl) X(Bgtzl) X(Daddi) X(Daddiu) X(Ldl) X(Ldr) X(Lq) X(Sq) \+ X(Lb) X(Lh) X(Lwl) X(Lw) X(Lbu) X(Lhu) X(Lwr) X(Lwu) X(Sb) X(Sh) X(Swl) X(Sw) X(Sdl) X(Sdr) X(Swr) \+ X(Cache) X(Lwc1) X(Pref) X(Lqc2) X(Ld) X(Swc1) X(Sqc2) X(Sd) \+ X(Madd) X(Maddu) X(Plzcw) X(Mfhi1) X(Mthi1) X(Mflo1) X(Mtlo1) X(Mult1) X(Multu1) X(Div1) X(Divu1) \+ X(Madd1) X(Maddu1) X(Pmfhl) X(Pmthl) X(Psllh) X(Psrlh) X(Psrah) X(Psllw) X(Psrlw) X(Psraw) \+ X(Paddw) X(Psubw) X(Pcgtw) X(Pmaxw) X(Paddh) X(Psubh) X(Pcgth) X(Pmaxh) X(Paddb) X(Psubb) X(Pcgtb) \+ X(Paddsw) X(Psubsw) X(Pextlw) X(Ppacw) X(Paddsh) X(Psubsh) X(Pextlh) X(Ppach) \+ X(Paddsb) X(Psubsb) X(Pextlb) X(Ppacb) X(Pext5) X(Ppac5) \+ X(Pabsw) X(Pceqw) X(Pminw) X(Padsbh) X(Pabsh) X(Pceqh) X(Pminh) X(Pceqb) \+ X(Padduw) X(Psubuw) X(Pextuw) X(Padduh) X(Psubuh) X(Pextuh) X(Paddub) X(Psubub) X(Pextub) X(Qfsrv) \+ X(Pmaddw) X(Psllvw) X(Psrlvw) X(Pmsubw) X(Pmfhi) X(Pmflo) X(Pinth) X(Pmultw) X(Pdivw) X(Pcpyld) \+ X(Pmaddh) X(Phmadh) X(Pand) X(Pxor) X(Pmsubh) X(Phmsbh) X(Pexeh) X(Prevh) X(Pmulth) X(Pdivbw) X(Pexew) X(Prot3w) \+ X(Pmadduw) X(Psravw) X(Pmthi) X(Pmtlo) X(Pinteh) X(Pmultuw) X(Pdivuw) X(Pcpyud) X(Por) X(Pnor) \+ X(Pexch) X(Pcpyh) X(Pexcw) \+ X(Mfc0) X(Mtc0) X(Bc0f) X(Bc0t) X(Bc0fl) X(Bc0tl) X(Tlbr) X(Tlbwi) X(Tlbwr) X(Tlbp) X(Eret) X(Ei) X(Di) \+ X(Mfc1) X(Cfc1) X(Mtc1) X(Ctc1) X(Bc1f) X(Bc1t) X(Bc1fl) X(Bc1tl) \+ X(AddS) X(SubS) X(MulS) X(DivS) X(SqrtS) X(AbsS) X(MovS) X(NegS) X(RsqrtS) X(AddaS) X(SubaS) X(MulaS) \+ X(MaddS) X(MsubS) X(MaddaS) X(MsubaS) X(CvtWS) X(MaxS) X(MinS) X(CF) X(CEq) X(CLt) X(CLe) X(CvtSW) \+ X(Qmfc2) X(Cfc2) X(Qmtc2) X(Ctc2) X(Bc2f) X(Bc2t) X(Bc2fl) X(Bc2tl) \+ X(Vaddbc) X(Vsubbc) X(Vmaddbc) X(Vmsubbc) X(Vmaxbc) X(Vminibc) X(Vmulbc) X(Vmulq) X(Vmaxi) X(Vmuli) X(Vminii) \+ X(Vaddq) X(Vmaddq) X(Vaddi) X(Vmaddi) X(Vsubq) X(Vmsubq) X(Vsubi) X(Vmsubi) \+ X(Vadd) X(Vmadd) X(Vmul) X(Vmax) X(Vsub) X(Vmsub) X(Vopmsub) X(Vmini) \+ X(Viadd) X(Visub) X(Viaddi) X(Viand) X(Vior) X(Vcallms) X(Vcallmsr) \+ X(Vaddabc) X(Vsubabc) X(Vmaddabc) X(Vmsubabc) X(Vitof0) X(Vitof4) X(Vitof12) X(Vitof15) \+ X(Vftoi0) X(Vftoi4) X(Vftoi12) X(Vftoi15) X(Vmulabc) X(Vmulaq) X(Vabs) X(Vmulai) X(Vclip) \+ X(Vaddaq) X(Vmaddaq) X(Vaddai) X(Vmaddai) X(Vsubaq) X(Vmsubaq) X(Vsubai) X(Vmsubai) \+ X(Vadda) X(Vmadda) X(Vmula) X(Vsuba) X(Vmsuba) X(Vopmula) X(Vnop) X(Vmove) X(Vmr32) \+ X(Vlqi) X(Vsqi) X(Vlqd) X(Vsqd) X(Vdiv) X(Vsqrt) X(Vrsqrt) X(Vwaitq) X(Vmtir) X(Vmfir) X(Vilwr) X(Viswr) \+ X(Vrnext) X(Vrget) X(Vrinit) X(Vrxor)++enum class Op : std::uint16_t {+#define X(name) name,+ R5900_OPS(X)+#undef X+ Count+};++struct Insn {+ std::uint32_t pc{};+ std::uint32_t word{};+ Op op{Op::Invalid};++ std::uint32_t rs() const { return (word >> 21) & 31u; }+ std::uint32_t rt() const { return (word >> 16) & 31u; }+ std::uint32_t rd() const { return (word >> 11) & 31u; }+ std::uint32_t sa() const { return (word >> 6) & 31u; }+ std::uint32_t fs() const { return rd(); }+ std::uint32_t ft() const { return rt(); }+ std::uint32_t fd() const { return sa(); }+ std::int32_t simm() const { return static_cast<std::int16_t>(word & 0xFFFFu); }+ std::uint32_t uimm() const { return word & 0xFFFFu; }+ std::uint32_t jump_target() const { return ((pc + 4u) & 0xF0000000u) | ((word & 0x03FFFFFFu) << 2); }+ std::uint32_t branch_target() const { return pc + 4u + (static_cast<std::uint32_t>(simm()) << 2); }+ std::uint32_t branch_target_of() const;+ std::uint32_t dest() const { return (word >> 21) & 15u; }+ std::uint32_t bc() const { return word & 3u; }+ std::int32_t imm5() const { return static_cast<std::int32_t>(sa() << 27) >> 27; }+ std::uint32_t imm15() const { return (word >> 6) & 0x7FFFu; }+};++enum class Flow { None, Branch, BranchLikely, Jump, Call, JumpRegister, CallRegister, Stop };++Insn decode(std::uint32_t word, std::uint32_t pc);+const char *name(Op op);+Flow flow(Op op);+bool has_delay_slot(Op op);+std::string disassemble(const Insn &insn);++}