PS2 games in the browser: static recompilation to WebAssembly with a native runtime
C++
VU1: record subroutine programs, skip identical microcode uploads
Samir Nuri78ec45da7846
diff --git a/runtime/src/vif.cpp b/runtime/src/vif.cppindex 818128a..9a6a1e9 100644--- a/runtime/src/vif.cpp+++ b/runtime/src/vif.cpp@@ -216,9 +216,14 @@ void execute(int unit, std::uint32_t cmd, const std::uint32_t *data) {std::uint8_t *code = unit ? g_vu1_code : g_vu0_code;const std::uint32_t size = unit ? 16384u : 4096u;const std::uint32_t count = num ? num : 256u;- for (std::uint32_t i = 0; i < count; ++i)- std::memcpy(code + ((imm * 8u + i * 8u) & (size - 1)), data + i * 2, 8);- vu::code_changed(unit);+ bool changed = false;+ for (std::uint32_t i = 0; i < count; ++i) {+ std::uint8_t *at = code + ((imm * 8u + i * 8u) & (size - 1));+ if (std::memcmp(at, data + i * 2, 8) == 0) continue;+ std::memcpy(at, data + i * 2, 8);+ changed = true;+ }+ if (changed) vu::code_changed(unit);break;}case 0x50: case 0x51:diff --git a/runtime/src/vu_micro.cpp b/runtime/src/vu_micro.cppindex 7ce38fc..97afc87 100644--- a/runtime/src/vu_micro.cpp+++ b/runtime/src/vu_micro.cpp@@ -798,6 +798,7 @@ void run_fast(int n, std::uint32_t address) {UpperOut out;static std::uint64_t total = 0, calls = 0;static const bool count = std::getenv("PS2_VU_COUNT") != nullptr;+ static const bool record_jumps = std::getenv("PS2_VU_RECORD") != nullptr || static_cast<bool>(g_options.on_vu_program);if (count && ++calls % 20000 == 0) std::fprintf(stderr, "[vu1] %llu calls, %llu instructions\n", (unsigned long long)calls, (unsigned long long)total);for (std::uint32_t steps = 0; steps < 1'000'000; ++steps, ++total) {std::uint32_t lo, hi;@@ -820,6 +821,7 @@ void run_fast(int n, std::uint32_t address) {std::uint32_t target = npc;lower(u, lo, current, target, branch);if (branch) npc = target;+ if (branch && record_jumps && n == 1 && ((lo >> 25) == 0x24u || (lo >> 25) == 0x25u)) lookup(target);}if (out.target > 0) {Reg128 &dst = out.target == kAcc ? r.acc : r.vf[out.target];