PS2 games in the browser: static recompilation to WebAssembly with a native runtime
C++
A write watchpoint for porting work
Riccardo Giorato4594bc7221e4
diff --git a/runtime/include/ps2/memory.hpp b/runtime/include/ps2/memory.hppindex 56f61d0..6a919d2 100644--- a/runtime/include/ps2/memory.hpp+++ b/runtime/include/ps2/memory.hpp@@ -14,6 +14,18 @@ inline constexpr std::uint32_t kScratchpadSize = 16u << 10;extern std::uint8_t g_ram[];extern std::uint8_t g_spr[];+extern bool g_watch_on;+extern std::uint32_t g_watch_base, g_watch_end;+void watch_hit(std::uint32_t a, std::uint32_t v, const char *via);+void watch_span(std::uint32_t a, std::uint32_t n, const char *via);+void init_watch();++template <typename T>+inline bool watched(std::uint32_t a, std::uint32_t v) {+ if (g_watch_on && a >= g_watch_base && a < g_watch_end) [[unlikely]] watch_hit(a, v, "store");+ return false;+}+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);@@ -68,26 +80,31 @@ inline void ld128(std::uint32_t a, Reg128 &out) {}inline void st8(std::uint32_t a, std::uint8_t v) {+ watched<std::uint8_t>(a, 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) {+ watched<std::uint16_t>(a, 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) {+ watched<std::uint32_t>(a, 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) {+ watched<std::uint32_t>(a, static_cast<std::uint32_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) {+ watched<std::uint32_t>(a, v.w[0]);if (auto *p = fast<std::uint8_t>(a)) { std::memcpy(p, &v, 16); return; }slow_st128(a, v);}diff --git a/runtime/src/dmac.cpp b/runtime/src/dmac.cppindex 8a52cb0..9d2fdaf 100644--- a/runtime/src/dmac.cpp+++ b/runtime/src/dmac.cpp@@ -118,6 +118,7 @@ void spr_to_ram(Channel &c, std::uint32_t qwc) {for (std::uint32_t i = 0; i < qwc; ++i) {const std::uint32_t at = wrap ? ring(c.madr + i * 16) : c.madr + i * 16;std::memcpy(target(at), g_spr + ((c.sadr + i * 16) & (kScratchpadSize - 1)), 16);+ watch_span(at, 16, "dma:spr_to_ram");}c.sadr = (c.sadr + qwc * 16) & (kScratchpadSize - 1);}@@ -164,6 +165,7 @@ void run_normal(int ch) {for (std::uint32_t i = 0, n = 1; i < c.qwc; i += n, n = 1) {while (i + n < c.qwc && target(c.madr + (i + n) * 16) == target(c.madr + i * 16) + n * 16) ++n;gs::read_image(target(c.madr + i * 16), n);+ watch_span(c.madr + i * 16, n * 16, "dma:gs_read_image");}} else {std::fprintf(stderr, "[dma] channel %d reads from a device (not emulated)\n", ch);@@ -361,6 +363,7 @@ bool ipu_push(const std::uint8_t *qword) {Channel &c = g_ch[3];if (!(c.chcr & 0x100u) || !c.qwc) return false;std::memcpy(target(c.madr), qword, 16);+ watch_span(c.madr, 16, "dma:ipu_push");c.madr += 16;if (!--c.qwc) finish(3);return true;diff --git a/runtime/src/iop/cdvdfsv.cpp b/runtime/src/iop/cdvdfsv.cppindex 28407ca..d0097e8 100644--- a/runtime/src/iop/cdvdfsv.cpp+++ b/runtime/src/iop/cdvdfsv.cpp@@ -29,6 +29,7 @@ void read_to_ee(std::uint32_t sector, std::uint32_t count, std::uint32_t dst) {disc::read(sector, count, data.data());const std::uint32_t at = dst & (ps2::kRamSize - 1);std::memcpy(ps2::g_ram + at, data.data(), std::min<std::size_t>(data.size(), ps2::kRamSize - at));+ ps2::watch_span(at, static_cast<std::uint32_t>(std::min<std::size_t>(data.size(), ps2::kRamSize - at)), "cdvd:read");}}diff --git a/runtime/src/iop/sif.cpp b/runtime/src/iop/sif.cppindex 3b17d58..3666a9f 100644--- a/runtime/src/iop/sif.cpp+++ b/runtime/src/iop/sif.cpp@@ -182,6 +182,7 @@ void other_data(const std::uint8_t *packet) {std::memcpy(&dst, packet + 36, 4);std::memcpy(&size, packet + 40, 4);std::memcpy(g_ram + (dst & (kRamSize - 1)), iop::ram_ptr(src), size);+ watch_span(dst, size, "sif:other_data");Rend r = make_rend(*reinterpret_cast<const Header *>(packet), record, paddr, rpc, receive, 0x8000000Cu);send(&r, sizeof r);}@@ -250,7 +251,7 @@ void command(std::uint32_t src, std::uint32_t size) {default: {auto it = g_cmd_handlers.find(h->cid);if (it == g_cmd_handlers.end()) {- std::fprintf(stderr, "[sif] command 0x%08X has no handler\n", h->cid);+ std::fprintf(stderr, "[sif] command 0x%08X has no handler (from ee buffer 0x%08X)\n", h->cid, src);break;}std::memcpy(iop::ram_ptr(kCmdBuffer), packet, std::min<std::uint32_t>(size, 0x400u));@@ -330,8 +331,10 @@ void rpc_finish(iop::Cpu &c) {const Call call = it->second;g_held_calls.erase(it);const std::uint32_t result = c.r[2];- if (call.recv && result && call.recv_size)+ if (call.recv && result && call.recv_size) {std::memcpy(g_ram + (call.recv & (kRamSize - 1)), iop::ram_ptr(result), (call.recv_size + 3u) & ~3u);+ watch_span(call.recv, (call.recv_size + 3u) & ~3u, "sif:rpc_finish");+ }reply_call(call);}c.pc = g_loop_stub;@@ -358,6 +361,7 @@ void install_iop_side() {iop::read32(src), iop::read32(src + 4), iop::read32(src + 8), iop::read32(src + 12));}std::memcpy(g_ram + (dst & (kRamSize - 1)), iop::ram_ptr(src), std::min(size, kRamSize - (dst & (kRamSize - 1))));+ watch_span(dst, std::min(size, kRamSize - (dst & (kRamSize - 1))), "sif:iop_dma");}c.r[2] = g_iop_dma_id++;if (callback) iop::kernel::schedule(iop::kernel::now() + 32, iop::kernel::Job{iop::kernel::kJobSifDmaCallback, callback, data});@@ -382,7 +386,7 @@ void install_iop_side() {auto send_cmd = [](iop::Cpu &c) {const std::uint32_t cid = arg(c, 0), packet = arg(c, 1), size = arg(c, 2);const std::uint32_t src = arg(c, 3), dst = arg(c, 4), extra = arg(c, 5);- if (extra) std::memcpy(g_ram + (dst & (kRamSize - 1)), iop::ram_ptr(src), extra);+ if (extra) { std::memcpy(g_ram + (dst & (kRamSize - 1)), iop::ram_ptr(src), extra); watch_span(dst, extra, "sif:send_cmd"); }std::vector<std::uint8_t> bytes(size);std::memcpy(bytes.data(), iop::ram_ptr(packet), size);const std::uint32_t words[2] = {(size & 0xFFu) | (extra << 8), dst};@@ -413,7 +417,7 @@ void install_iop_side() {iop::write32(client + 28, arg(c, 7));iop::write32(client + 32, arg(c, 8));const std::uint32_t buff = iop::read32(client + 20) & (kRamSize - 1);- if (send_size && buff) std::memcpy(g_ram + buff, iop::ram_ptr(send_ptr), std::min(send_size, kRamSize - buff));+ if (send_size && buff) { std::memcpy(g_ram + buff, iop::ram_ptr(send_ptr), std::min(send_size, kRamSize - buff)); watch_span(buff, send_size, "sif:call_rpc"); }iop::write32(client + 0, 1);// rmode 1 makes the EE answer with an RPC end command, which is what wakes the caller// and runs its end function. Every call asks for it: with rmode 0 the EE would write@@ -473,7 +477,9 @@ void fileio_reply(std::uint32_t args, std::uint32_t command, const std::vector<sif (g_fileio_result) {const std::uint32_t header[4] = {ee_read<std::uint32_t>(args), command, ee_read<std::uint32_t>(args + 4), ee_read<std::uint32_t>(args + 8)};std::memcpy(g_ram + (g_fileio_result & (kRamSize - 1)), header, 16);+ watch_span(g_fileio_result, 16, "sif:fileio_hdr");std::memcpy(g_ram + (g_fileio_result & (kRamSize - 1)) + 16, body.data(), body.size() * 4);+ watch_span(g_fileio_result + 16, static_cast<std::uint32_t>(body.size() * 4), "sif:fileio_body");}const std::uint32_t packet[4] = {16, 0, 0x80000011u, 0};send(packet, 16);@@ -487,6 +493,7 @@ bool fileio_async(std::uint32_t method, std::uint32_t args, std::uint32_t, std::case 255:g_fileio_result = word(0);std::memcpy(g_ram + ret, g_loadfile_version, 4);+ watch_span(ret, 4, "sif:loadfile_version");return true;case 0: {const std::int32_t fd = iop::io::open(text(20), word(12));@@ -501,7 +508,7 @@ bool fileio_async(std::uint32_t method, std::uint32_t args, std::uint32_t, std::const std::uint32_t size = word(20);std::vector<std::uint8_t> buffer(size);const int n = iop::io::read(static_cast<int>(word(12)), buffer.data(), size);- if (n > 0) std::memcpy(g_ram + (word(16) & (kRamSize - 1)), buffer.data(), static_cast<std::size_t>(n));+ if (n > 0) { std::memcpy(g_ram + (word(16) & (kRamSize - 1)), buffer.data(), static_cast<std::size_t>(n)); watch_span(word(16), static_cast<std::uint32_t>(n), "sif:file_read"); }fileio_reply(args, 2, {static_cast<std::uint32_t>(n), 0, 0, 0});return true;}@@ -600,6 +607,7 @@ void install_servers() {}if (method == 0xFF) {std::memcpy(g_ram + ret, g_loadfile_version, 4);+ watch_span(ret, 4, "sif:loadfile_version");return true;}std::fprintf(stderr, "[loadfile] method %u\n", method);@@ -620,7 +628,7 @@ void install_servers() {case 2: {std::vector<std::uint8_t> buffer(word(2));result = iop::io::read(static_cast<int>(word(0)), buffer.data(), word(2));- if (result > 0) std::memcpy(g_ram + (word(1) & (kRamSize - 1)), buffer.data(), static_cast<std::size_t>(result));+ if (result > 0) { std::memcpy(g_ram + (word(1) & (kRamSize - 1)), buffer.data(), static_cast<std::size_t>(result)); watch_span(word(1), static_cast<std::uint32_t>(result), "sif:file_read"); }break;}case 3: result = static_cast<std::int32_t>(word(2)); break;@@ -759,6 +767,7 @@ void pump() {const std::vector<std::uint8_t> packet = std::move(g_outgoing.front());g_outgoing.pop_front();std::memcpy(g_ram + g_ee_recv, packet.data(), packet.size());+ watch_span(g_ee_recv, static_cast<std::uint32_t>(packet.size()), "sif:pump");g_processed = false;hw::dmac_raise(5);}diff --git a/runtime/src/memory.cpp b/runtime/src/memory.cppindex 7d3e198..7a0699c 100644--- a/runtime/src/memory.cpp+++ b/runtime/src/memory.cpp@@ -4,6 +4,7 @@#include "ps2/system.hpp"#include <cstdio>+#include <cstdlib>#include <cstring>namespace ps2 {@@ -19,6 +20,47 @@ namespace {enum class Region { Ram, Scratchpad, Io, Vu, Rom, None };+}++// A watchpoint for porting work: PS2_WATCH=base,len makes every write into+// that range of main memory print itself, guest store or bulk copy, with the+// cpu state at the time.+bool g_watch_on = false;+std::uint32_t g_watch_base = 0, g_watch_end = 0;++void init_watch() {+ if (const char *spec = std::getenv("PS2_WATCH")) {+ std::uint32_t base = 0, len = 0;+ if (std::sscanf(spec, "%x,%x", &base, &len) == 2 && len) {+ g_watch_base = base & (kRamSize - 1);+ g_watch_end = g_watch_base + len;+ g_watch_on = true;+ std::fprintf(stderr, "[watch] writes to 0x%08X..0x%08X will print\n", g_watch_base, g_watch_end);+ }+ }+}++void watch_hit(std::uint32_t a, std::uint32_t v, const char *via) {+ const std::uint8_t *at = (a < kRamSize) ? g_ram + a : ((a >> 14) == (kScratchpadBase >> 14) ? g_spr + (a & (kScratchpadSize - 1)) : nullptr);+ std::uint32_t was = 0;+ if (at) std::memcpy(&was, at, 4);+ std::fprintf(stderr, "[watch] write %08X = %08X (was %08X) via %s, pc 0x%08X ra 0x%08X\n", a, v, was, via, g_cpu.pc, g_cpu.r[31].w[0]);+}++void watch_span(std::uint32_t a, std::uint32_t n, const char *via) {+ if (!g_watch_on) return;+ const std::uint32_t s = a & (kRamSize - 1), e = s + n;+ if (e <= g_watch_base || s >= g_watch_end) return;+ for (std::uint32_t k = s & ~3u; k < e; k += 4) {+ if (k < g_watch_base || k + 4 > g_watch_end) continue;+ std::uint32_t v;+ std::memcpy(&v, g_ram + k, 4);+ std::fprintf(stderr, "[watch] span from %08X: word %08X = %08X via %s, pc 0x%08X\n", a, k, v, via, g_cpu.pc);+ }+}++namespace {+struct Target {Region region;std::uint8_t *host;@@ -74,6 +116,10 @@ template <typename T>void write_slow(std::uint32_t a, T v) {const Target t = resolve(a);if (t.host) {+ if (g_watch_on && t.region == Region::Ram) {+ const std::uint32_t base = t.address & (kRamSize - 1);+ if (base >= g_watch_base && base < g_watch_end) watch_hit(base, static_cast<std::uint32_t>(v), "store-slow");+ }std::memcpy(t.host, &v, sizeof v);if (t.region == Region::Vu && (t.address & 0x4000u) == 0) vu::code_changed((t.address >> 15) & 1u);return;diff --git a/runtime/src/system.cpp b/runtime/src/system.cppindex 0ace318..3416212 100644--- a/runtime/src/system.cpp+++ b/runtime/src/system.cpp@@ -327,6 +327,7 @@ bool load_elf(const std::string &path, std::uint32_t &entry) {const std::uint32_t offset = u32(ph + 4), vaddr = physical(u32(ph + 8)), filesz = u32(ph + 16), memsz = u32(ph + 20);if (vaddr + memsz > kRamSize || offset + filesz > data.size()) return false;std::memcpy(g_ram + vaddr, data.data() + offset, filesz);+ watch_span(vaddr, filesz, "boot:load_exec");std::memset(g_ram + vaddr + filesz, 0, memsz - filesz);}return true;@@ -346,6 +347,7 @@ void request_iop_sync() {}void boot(std::uint32_t entry, const std::vector<std::string> &args) {+ init_watch();hw::reset();kernel::reset();if (!g_options.disc_path.empty() && !disc::open(g_options.disc_path))