Skip to content

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

C++

web: run VIF1 and VU1 on a thread of their own

Samir Nuri4693f1e63220
diff --git a/.gitignore b/.gitignore
index d216e5e..13a4aa6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -15,5 +15,4 @@
*.wav
__pycache__/
-/.claude/
/third_party/wgpu-native/
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2b85ee8..615caa0 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -86,7 +86,7 @@ if(PS2_GENERATED)
set(PS2_WEB_ELF "" CACHE FILEPATH "Game executable preloaded as /game/core.elf")
add_executable(ps2web host/web.cpp)
target_link_libraries(ps2web PRIVATE ps2game ps2runtime)
- target_link_options(ps2web PRIVATE --profiling-funcs -sJSPI -sALLOW_MEMORY_GROWTH=1 -sINITIAL_MEMORY=268435456 -sPTHREAD_POOL_SIZE=1 -sSTACK_SIZE=16777216 -sENVIRONMENT=web
+ target_link_options(ps2web PRIVATE --profiling-funcs -sJSPI -sALLOW_MEMORY_GROWTH=1 -sINITIAL_MEMORY=268435456 -sPTHREAD_POOL_SIZE=2 -sSTACK_SIZE=16777216 -sENVIRONMENT=web
-sEXPORTED_FUNCTIONS=_main,_ps2web_pad,_ps2web_resize,_ps2web_gpu_ms,_ps2web_frames,_ps2web_primitives,_ps2web_audio_ring,_ps2web_audio_queued_ms,_ps2web_idle_ms
-sEXPORTED_RUNTIME_METHODS=callMain,addRunDependency,removeRunDependency,HEAPU8,ENV
--shell-file ${CMAKE_SOURCE_DIR}/web/shell.html --preload-file ${PS2_WEB_ELF}@/game/core.elf)
diff --git a/host/web.cpp b/host/web.cpp
index 1989fb0..9aaf015 100644
--- a/host/web.cpp
+++ b/host/web.cpp
@@ -10,6 +10,7 @@
#include <atomic>
#include <cmath>
#include <cstdio>
+#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
@@ -233,7 +234,10 @@ extern "C" EMSCRIPTEN_KEEPALIVE double ps2web_frames() { return static_cast<doub
extern "C" EMSCRIPTEN_KEEPALIVE double ps2web_primitives() { return static_cast<double>(ps2::primitives_drawn()); }
extern "C" EMSCRIPTEN_KEEPALIVE void *ps2web_audio_ring() { return &g_audio; }
extern "C" EMSCRIPTEN_KEEPALIVE double ps2web_audio_queued_ms() { return audio_queued_ms(); }
-extern "C" EMSCRIPTEN_KEEPALIVE double ps2web_idle_ms(int render) { return render ? g_render_idle.load() : g_emulation_idle.load(); }
+extern "C" EMSCRIPTEN_KEEPALIVE double ps2web_idle_ms(int which) {
+ if (which == 2) return ps2::vu_thread_idle_ms();
+ return which ? g_render_idle.load() : g_emulation_idle.load();
+}
int main(int argc, char **argv) {
static Launch launch;
@@ -272,6 +276,17 @@ int main(int argc, char **argv) {
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, 16u << 20);
+ if (!std::getenv("PS2_NO_VU_THREAD")) {
+ ps2::start_vu_thread();
+ pthread_t vu_thread;
+ if (pthread_create(&vu_thread, &attr, [](void *) -> void * {
+ ps2::run_vu_thread();
+ return nullptr;
+ }, nullptr) != 0) {
+ std::fprintf(stderr, "[web] cannot start the VU thread\n");
+ return 1;
+ }
+ }
pthread_t thread;
if (pthread_create(&thread, &attr, emulate, &launch) != 0) {
std::fprintf(stderr, "[web] cannot start the emulation thread\n");
diff --git a/recomp/vurecomp.cpp b/recomp/vurecomp.cpp
index da48ef0..734820a 100644
--- a/recomp/vurecomp.cpp
+++ b/recomp/vurecomp.cpp
@@ -44,7 +44,6 @@ struct Upper {
std::vector<int> shadow_reads;
};
-// VF0 is hardwired to (0, 0, 0, 1), so its reads become constants the compiler can fold.
std::string vf(unsigned i, int lane_index) {
if (!i) return lane_index == 3 ? "0x3F800000u" : "0u";
return std::format("r.vf[{}].w[{}]", i, lane_index);
@@ -52,7 +51,6 @@ std::string vf(unsigned i, int lane_index) {
std::string vreg(unsigned i) { return i ? std::format("vload(r.vf[{}])", i) : "v4u{0u, 0u, 0u, 0x3F800000u}"; }
-// Arithmetic reads n<reg>, a copy of the register kept normalized, so vfix runs once per write instead of once per read.
std::string shadow(int reg) { return reg == kAcc ? "nacc" : std::format("n{}", reg); }
std::string raw(int reg) { return reg == kAcc ? "r.acc" : std::format("r.vf[{}]", reg); }
diff --git a/runtime/include/ps2/disc.hpp b/runtime/include/ps2/disc.hpp
index 467623a..2af39a0 100644
--- a/runtime/include/ps2/disc.hpp
+++ b/runtime/include/ps2/disc.hpp
@@ -17,7 +17,6 @@ struct Entry {
bool directory;
};
-// Reads up to size bytes at a byte offset of the image and returns how many it got.
using Reader = std::function<std::size_t(std::uint64_t offset, void *out, std::size_t size)>;
bool open(const std::string &path);
diff --git a/runtime/include/ps2/system.hpp b/runtime/include/ps2/system.hpp
index 2a17ed9..3ef0fa6 100644
--- a/runtime/include/ps2/system.hpp
+++ b/runtime/include/ps2/system.hpp
@@ -86,6 +86,9 @@ void resize_window(std::uint32_t width, std::uint32_t height);
bool present_frame();
void init_graphics();
void start_render_thread();
+void start_vu_thread();
+void run_vu_thread();
+double vu_thread_idle_ms();
void post_present();
bool render_pending(bool surface, const std::function<void(const std::uint32_t *rgba, std::uint32_t width, std::uint32_t height)> &copy);
void wait_render_work(int timeout_ms);
diff --git a/runtime/include/ps2/vu_ops.hpp b/runtime/include/ps2/vu_ops.hpp
index 747cc19..f67de0c 100644
--- a/runtime/include/ps2/vu_ops.hpp
+++ b/runtime/include/ps2/vu_ops.hpp
@@ -41,7 +41,6 @@ inline std::uint32_t branch_target(std::uint32_t lo, std::uint32_t pc, std::uint
return (pc + 8u + static_cast<std::uint32_t>(imm11) * 8u) & mask;
}
-// Walks the instructions reachable from an entry; both the runtime and vurecomp key programs by this set.
inline std::vector<std::uint32_t> reachable(const std::uint8_t *code, std::uint32_t mask, std::uint32_t entry) {
std::vector<std::uint8_t> seen((mask + 1) / 8, 0);
std::vector<std::uint32_t> work{entry & mask}, out;
@@ -134,7 +133,6 @@ PS2_VU_INLINE v4u vload(const Reg128 &r) {
PS2_VU_INLINE void vstore(Reg128 &r, v4u v) { std::memcpy(&r, &v, 16); }
PS2_VU_INLINE v4u vsplat(std::uint32_t x) { return v4u{x, x, x, x}; }
PS2_VU_INLINE v4u vsel(v4u old, v4u now, v4u mask) { return (now & mask) | (old & ~mask); }
-// Denormals become signed zero and exponent 255 becomes the largest finite value.
PS2_VU_INLINE v4u vfix(v4u x) {
const v4i clamped = __builtin_elementwise_min((v4i)(x & 0x7FFFFFFFu), (v4i){0x7F7FFFFF, 0x7F7FFFFF, 0x7F7FFFFF, 0x7F7FFFFF});
const v4i normal = (v4f)clamped >= (v4f)(v4i){0x00800000, 0x00800000, 0x00800000, 0x00800000};
@@ -213,7 +211,6 @@ PS2_VU_INLINE void clip(Vu0Regs &r, const Reg128 &s, std::uint32_t w) {
r.clip = ((r.clip << 6) | f) & 0xFFFFFFu;
}
-// MAC, status and clip flags reach the lower pipeline four instructions after the upper op that set them.
struct FlagPipe {
std::uint32_t t = 16;
std::uint32_t mac_ts[8] = {}, mac_v[8] = {}, status_v[8] = {}, clip_ts[8] = {}, clip_v[8] = {};
diff --git a/runtime/src/gs/gif.cpp b/runtime/src/gs/gif.cpp
index 7e7e3ea..6f70d73 100644
--- a/runtime/src/gs/gif.cpp
+++ b/runtime/src/gs/gif.cpp
@@ -167,10 +167,12 @@ void transfer(int path, const std::uint8_t *data, std::uint32_t qwc) {
render(path, data, qwc);
return;
}
- scan(g_scan[path], data, qwc);
+ if (gs::queue::role() != gs::queue::Role::Vu) scan(g_scan[path], data, qwc);
gs::queue::post(gs::queue::kGif, static_cast<std::uint32_t>(path), qwc, data, qwc * 16);
}
+void scan_signals(int path, const std::uint8_t *data, std::uint32_t qwc) { scan(g_scan[path], data, qwc); }
+
bool busy(int path) { return g_paths[path].active; }
void stats() {
diff --git a/runtime/src/gs/gif.hpp b/runtime/src/gs/gif.hpp
index 86492b7..78236d9 100644
--- a/runtime/src/gs/gif.hpp
+++ b/runtime/src/gs/gif.hpp
@@ -9,6 +9,7 @@ void stats();
void transfer(int path, const std::uint8_t *data, std::uint32_t qwc);
void render(int path, const std::uint8_t *data, std::uint32_t qwc);
void render_reset();
+void scan_signals(int path, const std::uint8_t *data, std::uint32_t qwc);
bool busy(int path);
std::uint32_t read(std::uint32_t a);
void write(std::uint32_t a, std::uint32_t v);
diff --git a/runtime/src/gs/gpu/gpu.hpp b/runtime/src/gs/gpu/gpu.hpp
index 0f1e97e..4e4b9c9 100644
--- a/runtime/src/gs/gpu/gpu.hpp
+++ b/runtime/src/gs/gpu/gpu.hpp
@@ -33,7 +33,6 @@ void load_clut(std::uint64_t tex0, std::uint64_t texclut);
void flush();
void download(std::uint8_t *memory);
bool display(const DisplaySource &src, std::vector<std::uint32_t> &rgba);
-// Starts a readback of the frame and returns the newest finished one without waiting; false while none is ready.
bool display_async(const DisplaySource &src, std::vector<std::uint32_t> &rgba, std::uint32_t &width, std::uint32_t &height);
bool attach_surface(const SurfaceTarget &target);
void resize_surface(std::uint32_t width, std::uint32_t height);
diff --git a/runtime/src/gs/gs.cpp b/runtime/src/gs/gs.cpp
index 5e8542a..22262fa 100644
--- a/runtime/src/gs/gs.cpp
+++ b/runtime/src/gs/gs.cpp
@@ -824,7 +824,7 @@ bool copy_frame(const DisplayRegs &r, std::vector<std::uint32_t> &rgba, std::uin
std::atomic<std::uint32_t> g_presented{0};
std::uint32_t g_posted_presents = 0;
-void start_thread() { queue::start(32u << 20); }
+void start_thread() { queue::render().start(32u << 20); }
void post_present() {
const DisplayRegs regs = display_regs();
@@ -834,9 +834,10 @@ void post_present() {
}
bool render_pending(const std::function<void(const DisplayRegs &)> &on_present) {
+ queue::role() = queue::Role::Render;
queue::Header h;
const std::uint8_t *payload = nullptr;
- while (queue::peek(h, payload)) {
+ while (queue::render().peek(h, payload)) {
bool presented = false;
switch (h.type) {
case queue::kGif: gif::render(static_cast<int>(h.arg0), payload, h.arg1); break;
@@ -865,7 +866,7 @@ bool render_pending(const std::function<void(const DisplayRegs &)> &on_present)
}
default: break;
}
- queue::pop(h);
+ queue::render().pop(h);
if (presented) {
g_presented.fetch_add(1);
queue::notify(g_presented);
diff --git a/runtime/src/gs/queue.cpp b/runtime/src/gs/queue.cpp
index a86c50d..61715e9 100644
--- a/runtime/src/gs/queue.cpp
+++ b/runtime/src/gs/queue.cpp
@@ -2,8 +2,8 @@
#include <algorithm>
#include <climits>
+#include <cmath>
#include <cstring>
-#include <memory>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
@@ -17,17 +17,11 @@ namespace ps2::gs::queue {
namespace {
-std::unique_ptr<std::uint8_t[]> g_buffer;
-std::uint64_t g_capacity = 0;
-bool g_active = false;
-alignas(64) std::atomic<std::uint64_t> g_head{0};
-alignas(64) std::atomic<std::uint64_t> g_tail{0};
-alignas(64) std::atomic<std::uint32_t> g_posted{0};
-alignas(64) std::atomic<std::uint32_t> g_freed{0};
-std::atomic<std::uint32_t> g_consumer_waiting{0}, g_producer_waiting{0};
-
constexpr std::uint64_t align16(std::uint64_t n) { return (n + 15) & ~15ull; }
+Ring g_render, g_vu;
+thread_local Role g_role = Role::Emulation;
+
}
#ifdef __EMSCRIPTEN__
@@ -53,77 +47,89 @@ void notify(std::atomic<std::uint32_t> &value) {
#endif
}
-bool active() { return g_active; }
+Ring &render() { return g_render; }
+Ring &vu() { return g_vu; }
+Role &role() { return g_role; }
+
+Ring &outbox() {
+ if (g_role == Role::Emulation && g_vu.active()) return g_vu;
+ return g_render;
+}
+
+bool active() { return g_render.active(); }
+bool vu_active() { return g_vu.active(); }
+
+void post(Type type, std::uint32_t arg0, std::uint32_t arg1, const void *data, std::uint32_t bytes) { outbox().post(type, arg0, arg1, data, bytes); }
-void start(std::size_t capacity) {
- g_capacity = capacity;
- g_buffer = std::make_unique<std::uint8_t[]>(capacity);
- g_active = true;
+void Ring::start(std::size_t capacity) {
+ buffer_ = std::make_unique<std::uint8_t[]>(capacity);
+ capacity_ = capacity;
}
-void post(Type type, std::uint32_t arg0, std::uint32_t arg1, const void *data, std::uint32_t bytes) {
+void Ring::post(Type type, std::uint32_t arg0, std::uint32_t arg1, const void *data, std::uint32_t bytes) {
const std::uint64_t need = sizeof(Header) + align16(bytes);
- std::uint64_t head = g_head.load(std::memory_order_relaxed);
+ std::uint64_t head = head_.load(std::memory_order_relaxed);
for (;;) {
- const std::uint64_t pos = head % g_capacity;
- const std::uint64_t pad = pos + need > g_capacity ? g_capacity - pos : 0;
- const std::uint32_t seen = g_freed.load(std::memory_order_acquire);
- if (head + pad + need - g_tail.load(std::memory_order_acquire) <= g_capacity) {
+ const std::uint64_t pos = head % capacity_;
+ const std::uint64_t pad = pos + need > capacity_ ? capacity_ - pos : 0;
+ const std::uint32_t seen = freed_.load(std::memory_order_acquire);
+ if (head + pad + need - tail_.load(std::memory_order_acquire) <= capacity_) {
if (pad) {
const Header wrap{kWrap, 0, 0, 0};
- std::memcpy(g_buffer.get() + pos, &wrap, sizeof wrap);
+ std::memcpy(buffer_.get() + pos, &wrap, sizeof wrap);
head += pad;
}
break;
}
- g_producer_waiting.store(1);
- if (head + pad + need - g_tail.load() > g_capacity) wait(g_freed, seen);
+ producer_waiting_.store(1);
+ if (head + pad + need - tail_.load() > capacity_) wait(freed_, seen);
}
- std::uint8_t *at = g_buffer.get() + head % g_capacity;
+ std::uint8_t *at = buffer_.get() + head % capacity_;
const Header h{type, bytes, arg0, arg1};
std::memcpy(at, &h, sizeof h);
if (bytes) std::memcpy(at + sizeof h, data, bytes);
- g_head.store(head + need, std::memory_order_release);
- g_posted.fetch_add(1);
- if (g_consumer_waiting.exchange(0)) notify(g_posted);
+ head_.store(head + need, std::memory_order_release);
+ posted_.fetch_add(1);
+ if (consumer_waiting_.exchange(0)) notify(posted_);
}
-bool peek(Header &header, const std::uint8_t *&payload) {
- std::uint64_t tail = g_tail.load(std::memory_order_relaxed);
- const std::uint64_t head = g_head.load(std::memory_order_acquire);
+bool Ring::peek(Header &header, const std::uint8_t *&payload) {
+ std::uint64_t tail = tail_.load(std::memory_order_relaxed);
+ const std::uint64_t head = head_.load(std::memory_order_acquire);
while (tail != head) {
- const std::uint64_t pos = tail % g_capacity;
- std::memcpy(&header, g_buffer.get() + pos, sizeof header);
+ const std::uint64_t pos = tail % capacity_;
+ std::memcpy(&header, buffer_.get() + pos, sizeof header);
if (header.type != kWrap) {
- payload = g_buffer.get() + pos + sizeof header;
+ payload = buffer_.get() + pos + sizeof header;
return true;
}
- tail += g_capacity - pos;
- g_tail.store(tail, std::memory_order_release);
+ tail += capacity_ - pos;
+ tail_.store(tail, std::memory_order_release);
}
return false;
}
-void pop(const Header &header) {
- g_tail.store(g_tail.load(std::memory_order_relaxed) + sizeof(Header) + align16(header.bytes), std::memory_order_release);
- g_freed.fetch_add(1);
- if (g_producer_waiting.exchange(0)) notify(g_freed);
+void Ring::pop(const Header &header) {
+ tail_.store(tail_.load(std::memory_order_relaxed) + sizeof(Header) + align16(header.bytes), std::memory_order_release);
+ freed_.fetch_add(1);
+ if (producer_waiting_.exchange(0)) notify(freed_);
}
-void wake() {
- g_posted.fetch_add(1);
- if (g_consumer_waiting.exchange(0)) notify(g_posted);
+void Ring::wake() {
+ posted_.fetch_add(1);
+ if (consumer_waiting_.exchange(0)) notify(posted_);
}
-void wait_for_work(int timeout_ms) {
- const std::uint32_t seen = g_posted.load();
- g_consumer_waiting.store(1);
- if (g_head.load() != g_tail.load()) {
- g_consumer_waiting.store(0);
+void Ring::wait_for_work(int timeout_ms) {
+ const std::uint32_t seen = posted_.load();
+ consumer_waiting_.store(1);
+ if (head_.load() != tail_.load()) {
+ consumer_waiting_.store(0);
return;
}
#ifdef __EMSCRIPTEN__
- ps2_wait_async(&g_posted, seen, timeout_ms);
+ if (emscripten_is_main_browser_thread()) ps2_wait_async(&posted_, seen, timeout_ms);
+ else emscripten_futex_wait(&posted_, seen, timeout_ms);
#else
(void)seen;
std::this_thread::sleep_for(std::chrono::milliseconds(std::min(timeout_ms, 1)));
diff --git a/runtime/src/gs/queue.hpp b/runtime/src/gs/queue.hpp
index 91a79d4..045cee5 100644
--- a/runtime/src/gs/queue.hpp
+++ b/runtime/src/gs/queue.hpp
@@ -3,24 +3,46 @@
#include <atomic>
#include <cstddef>
#include <cstdint>
+#include <memory>
namespace ps2::gs::queue {
-enum Type : std::uint32_t { kWrap, kGif, kGifReset, kReset, kVertexReset, kVblank, kPresent, kDownload };
+enum Type : std::uint32_t { kWrap, kGif, kGifReset, kReset, kVertexReset, kVblank, kPresent, kDownload, kVif1, kVif1Write, kVuReset };
+
+enum class Role { Emulation, Vu, Render };
struct Header {
std::uint32_t type, bytes, arg0, arg1;
};
-bool active();
-void start(std::size_t capacity);
+class Ring {
+public:
+ void start(std::size_t capacity);
+ bool active() const { return capacity_ != 0; }
+ void post(Type type, std::uint32_t arg0, std::uint32_t arg1, const void *data, std::uint32_t bytes);
+ bool peek(Header &header, const std::uint8_t *&payload);
+ void pop(const Header &header);
+ void wait_for_work(int timeout_ms);
+ void wake();
+
+private:
+ std::unique_ptr<std::uint8_t[]> buffer_;
+ std::uint64_t capacity_ = 0;
+ alignas(64) std::atomic<std::uint64_t> head_{0};
+ alignas(64) std::atomic<std::uint64_t> tail_{0};
+ alignas(64) std::atomic<std::uint32_t> posted_{0};
+ alignas(64) std::atomic<std::uint32_t> freed_{0};
+ std::atomic<std::uint32_t> consumer_waiting_{0}, producer_waiting_{0};
+};
-void post(Type type, std::uint32_t arg0, std::uint32_t arg1, const void *data, std::uint32_t bytes);
+Ring &render();
+Ring &vu();
+Role &role();
+Ring &outbox();
-bool peek(Header &header, const std::uint8_t *&payload);
-void pop(const Header &header);
-void wait_for_work(int timeout_ms);
-void wake();
+bool active();
+bool vu_active();
+void post(Type type, std::uint32_t arg0, std::uint32_t arg1, const void *data, std::uint32_t bytes);
void wait(std::atomic<std::uint32_t> &value, std::uint32_t seen);
void notify(std::atomic<std::uint32_t> &value);
diff --git a/runtime/src/ipu/tables.hpp b/runtime/src/ipu/tables.hpp
index 1b4dfe5..df8593c 100644
--- a/runtime/src/ipu/tables.hpp
+++ b/runtime/src/ipu/tables.hpp
@@ -2,7 +2,7 @@
#include <cstdint>
-// Adapted from Play! (BSD-2-Clause), see NOTICE in this directory.
+// adapted from Play! (BSD-2-Clause), see NOTICE in this directory
namespace ps2::ipu::tables {
struct Vlc {
diff --git a/runtime/src/system.cpp b/runtime/src/system.cpp
index b755edc..0d61add 100644
--- a/runtime/src/system.cpp
+++ b/runtime/src/system.cpp
@@ -1,6 +1,7 @@
#include "dmac.hpp"
#include "gs/gif.hpp"
#include "vif.hpp"
+#include "vu_micro.hpp"
#include "gs/gs.hpp"
#include "gs/queue.hpp"
#include "hw.hpp"
@@ -20,6 +21,8 @@
#endif
#include <algorithm>
+#include <atomic>
+#include <chrono>
#include <cstdio>
#include <cstring>
#include <fstream>
@@ -284,6 +287,35 @@ void resize_window(std::uint32_t width, std::uint32_t height) { gs::resize_windo
bool present_frame() { return gs::present(); }
void init_graphics() { gs::init(); }
void start_render_thread() { gs::start_thread(); }
+void start_vu_thread() { gs::queue::vu().start(32u << 20); }
+
+std::atomic<double> g_vu_idle_ms{0.0};
+
+double vu_thread_idle_ms() { return g_vu_idle_ms.load(); }
+
+void run_vu_thread() {
+ gs::queue::role() = gs::queue::Role::Vu;
+ gs::queue::Ring &in = gs::queue::vu();
+ for (;;) {
+ gs::queue::Header h;
+ const std::uint8_t *payload = nullptr;
+ while (in.peek(h, payload)) {
+ switch (h.type) {
+ case gs::queue::kVif1: vif::transfer(1, payload, h.arg0); break;
+ case gs::queue::kVif1Write: vif::write(1, h.arg0, h.arg1); break;
+ case gs::queue::kVuReset:
+ vif::reset_vu_side();
+ vu::reset();
+ break;
+ default: gs::queue::render().post(static_cast<gs::queue::Type>(h.type), h.arg0, h.arg1, payload, h.bytes); break;
+ }
+ in.pop(h);
+ }
+ const auto start = std::chrono::steady_clock::now();
+ in.wait_for_work(50);
+ g_vu_idle_ms.store(g_vu_idle_ms.load() + std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - start).count());
+ }
+}
void post_present() { gs::post_present(); }
bool render_pending(bool surface, const std::function<void(const std::uint32_t *, std::uint32_t, std::uint32_t)> &copy) {
@@ -298,8 +330,8 @@ bool render_pending(bool surface, const std::function<void(const std::uint32_t *
});
}
-void wait_render_work(int timeout_ms) { gs::queue::wait_for_work(timeout_ms); }
-void wake_render_thread() { gs::queue::wake(); }
+void wait_render_work(int timeout_ms) { gs::queue::render().wait_for_work(timeout_ms); }
+void wake_render_thread() { gs::queue::render().wake(); }
bool copy_frame(std::vector<std::uint32_t> &rgba, std::uint32_t &width, std::uint32_t &height) { return gs::copy_frame(rgba, width, height); }
bool parse_press(const std::string &spec, Press &press) {
diff --git a/runtime/src/vif.cpp b/runtime/src/vif.cpp
index 46f61cd..818128a 100644
--- a/runtime/src/vif.cpp
+++ b/runtime/src/vif.cpp
@@ -2,6 +2,7 @@
#include "dmac.hpp"
#include "gs/gif.hpp"
+#include "gs/queue.hpp"
#include "hw.hpp"
#include "vu_micro.hpp"
@@ -22,7 +23,13 @@ struct Unit {
};
Unit g_units[2];
+Unit g_vu_side;
std::uint64_t g_commands[2][128];
+std::uint8_t g_dry_run[16384];
+
+bool on_vu_thread() { return gs::queue::role() == gs::queue::Role::Vu; }
+bool shadow(int unit) { return unit == 1 && gs::queue::vu_active() && !on_vu_thread(); }
+Unit &state(int unit) { return unit == 1 && on_vu_thread() ? g_vu_side : g_units[unit]; }
std::uint8_t *vu_data(int unit) { return unit ? g_vu1_mem : g_vu0_mem; }
std::uint32_t vu_qwords(int unit) { return unit ? 1024u : 256u; }
@@ -46,12 +53,13 @@ std::uint32_t unpack_words(std::uint32_t cmd, const Unit &u, std::uint32_t num)
}
void run_vu(int unit, std::uint32_t address) {
- Unit &u = g_units[unit];
+ Unit &u = state(unit);
if (unit == 1) {
u.top = u.tops;
u.itop = u.itops;
u.dbf = !u.dbf;
u.tops = u.base + (u.dbf ? u.ofst : 0);
+ if (shadow(unit)) return;
vu::set_tops(u.top, u.itop);
} else {
u.itop = u.itops;
@@ -61,7 +69,12 @@ void run_vu(int unit, std::uint32_t address) {
}
void unpack(int unit, std::uint32_t cmd, const std::uint32_t *data) {
- Unit &u = g_units[unit];
+ Unit &u = state(unit);
+ const bool dry = shadow(unit);
+ if (dry && u.mode != 2) {
+ u.num = 0;
+ return;
+ }
const std::uint32_t vn = (cmd >> 26) & 3u, vl = (cmd >> 24) & 3u;
const bool masked = (cmd >> 28) & 1u;
const bool usn = (cmd >> 14) & 1u;
@@ -83,7 +96,7 @@ void unpack(int unit, std::uint32_t cmd, const std::uint32_t *data) {
return raw;
};
std::uint32_t cycle = 0;
- std::uint8_t *mem = vu_data(unit);
+ std::uint8_t *mem = dry ? g_dry_run : vu_data(unit);
const std::uint32_t wrap = vu_qwords(unit);
if (!masked && u.mode == 0 && !(wl > cl) && !(vn == 3 && vl == 3)) {
const auto *bytes = reinterpret_cast<const std::uint8_t *>(data);
@@ -164,7 +177,7 @@ void unpack(int unit, std::uint32_t cmd, const std::uint32_t *data) {
}
std::uint32_t data_words(int unit, std::uint32_t cmd) {
- const Unit &u = g_units[unit];
+ const Unit &u = state(unit);
const std::uint32_t op = (cmd >> 24) & 0x7Fu;
const std::uint32_t num = (cmd >> 16) & 0xFFu;
switch (op) {
@@ -179,10 +192,10 @@ std::uint32_t data_words(int unit, std::uint32_t cmd) {
}
void execute(int unit, std::uint32_t cmd, const std::uint32_t *data) {
- Unit &u = g_units[unit];
+ Unit &u = state(unit);
const std::uint32_t op = (cmd >> 24) & 0x7Fu, imm = cmd & 0xFFFFu, num = (cmd >> 16) & 0xFFu;
u.code = cmd;
- ++g_commands[unit][op];
+ if (!on_vu_thread()) ++g_commands[unit][op];
switch (op) {
case 0x00: break;
case 0x01: u.cycle = imm; break;
@@ -194,11 +207,12 @@ void execute(int unit, std::uint32_t cmd, const std::uint32_t *data) {
case 0x07: u.mark = imm; break;
case 0x10: case 0x11: case 0x13: break;
case 0x14: case 0x15: run_vu(unit, imm * 8u); break;
- case 0x17: run_vu(unit, vu::tpc(unit)); break;
+ case 0x17: run_vu(unit, shadow(unit) ? 0u : vu::tpc(unit)); break;
case 0x20: u.mask = data[0]; break;
case 0x30: std::memcpy(u.row, data, 16); break;
case 0x31: std::memcpy(u.col, data, 16); break;
case 0x4A: {
+ if (shadow(unit)) break;
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;
@@ -208,7 +222,8 @@ void execute(int unit, std::uint32_t cmd, const std::uint32_t *data) {
break;
}
case 0x50: case 0x51:
- gif::transfer(2, reinterpret_cast<const std::uint8_t *>(data), imm ? imm : 0x10000u);
+ if (shadow(unit)) gif::scan_signals(2, reinterpret_cast<const std::uint8_t *>(data), imm ? imm : 0x10000u);
+ else gif::transfer(2, reinterpret_cast<const std::uint8_t *>(data), imm ? imm : 0x10000u);
break;
default:
if ((op & 0x60u) == 0x60u) unpack(unit, cmd, data);
@@ -218,12 +233,12 @@ void execute(int unit, std::uint32_t cmd, const std::uint32_t *data) {
if ((cmd & 0x80000000u) && !(u.err & 1u)) {
u.stat |= 0xC00u;
u.stalled = true;
- hw::intc_raise(4u + static_cast<unsigned>(unit));
+ if (!on_vu_thread()) hw::intc_raise(4u + static_cast<unsigned>(unit));
}
}
std::size_t run(int unit, const std::uint32_t *words, std::size_t count) {
- Unit &u = g_units[unit];
+ Unit &u = state(unit);
std::size_t pos = 0;
while (pos < count && !u.stalled) {
const std::uint32_t cmd = words[pos];
@@ -237,7 +252,7 @@ std::size_t run(int unit, const std::uint32_t *words, std::size_t count) {
}
void process(int unit) {
- Unit &u = g_units[unit];
+ Unit &u = state(unit);
const std::size_t pos = run(unit, u.pending.data(), u.pending.size());
u.pending.erase(u.pending.begin(), u.pending.begin() + static_cast<std::ptrdiff_t>(pos));
}
@@ -248,6 +263,8 @@ void reset() {
for (Unit &u : g_units) u = Unit{};
}
+void reset_vu_side() { g_vu_side = Unit{}; }
+
void stats() {
for (int u = 0; u < 2; ++u) {
std::fprintf(stderr, "[vif%d]", u);
@@ -258,7 +275,8 @@ void stats() {
}
void transfer(int unit, const std::uint8_t *data, std::uint32_t qwc) {
- Unit &u = g_units[unit];
+ if (shadow(unit)) gs::queue::vu().post(gs::queue::kVif1, qwc, 0, data, qwc * 16);
+ Unit &u = state(unit);
if (u.pending.empty() && !u.stalled) {
const auto *words = reinterpret_cast<const std::uint32_t *>(data);
const std::size_t count = static_cast<std::size_t>(qwc) * 4;
@@ -272,10 +290,10 @@ void transfer(int unit, const std::uint8_t *data, std::uint32_t qwc) {
process(unit);
}
-bool stalled(int unit) { return g_units[unit].stalled; }
+bool stalled(int unit) { return state(unit).stalled; }
std::uint32_t read(int unit, std::uint32_t reg) {
- const Unit &u = g_units[unit];
+ const Unit &u = state(unit);
switch (reg) {
case 0x00: return u.stat | (u.dbf ? 0x80u : 0u);
case 0x20: return u.err;
@@ -299,7 +317,8 @@ std::uint32_t read(int unit, std::uint32_t reg) {
}
void write(int unit, std::uint32_t reg, std::uint32_t value) {
- Unit &u = g_units[unit];
+ if (shadow(unit)) gs::queue::vu().post(gs::queue::kVif1Write, reg, value, nullptr, 0);
+ Unit &u = state(unit);
switch (reg) {
case 0x10:
if (value & 1u) {
@@ -310,7 +329,7 @@ void write(int unit, std::uint32_t reg, std::uint32_t value) {
if (u.stalled) {
u.stalled = false;
process(unit);
- if (!u.stalled) dmac::resume(unit);
+ if (!u.stalled && !on_vu_thread()) dmac::resume(unit);
}
}
break;
diff --git a/runtime/src/vif.hpp b/runtime/src/vif.hpp
index 5b744c9..49802c4 100644
--- a/runtime/src/vif.hpp
+++ b/runtime/src/vif.hpp
@@ -5,6 +5,7 @@
namespace ps2::vif {
void reset();
+void reset_vu_side();
void stats();
void transfer(int unit, const std::uint8_t *data, std::uint32_t qwc);
bool stalled(int unit);
diff --git a/runtime/src/vu_micro.cpp b/runtime/src/vu_micro.cpp
index 368344a..2cfed1a 100644
--- a/runtime/src/vu_micro.cpp
+++ b/runtime/src/vu_micro.cpp
@@ -1,6 +1,7 @@
#include "vu_micro.hpp"
#include "gs/gif.hpp"
+#include "gs/queue.hpp"
#include "hw.hpp"
#include "ps2/fpu.hpp"
#include "ps2/system.hpp"
@@ -611,6 +612,10 @@ void lower(Unit &u, std::uint32_t w, std::uint32_t pc, std::uint32_t &npc, bool
}
void reset() {
+ if (gs::queue::vu_active() && gs::queue::role() != gs::queue::Role::Vu) {
+ gs::queue::vu().post(gs::queue::kVuReset, 0, 0, nullptr, 0);
+ return;
+ }
std::memset(&g_vu1, 0, sizeof g_vu1);
g_vu1.vf[0].w[3] = fp::kOne;
g_top = g_itop = 0;
diff --git a/web/shell.html b/web/shell.html
index 58d95a4..7e06912 100644
--- a/web/shell.html
+++ b/web/shell.html
@@ -363,7 +363,7 @@ async function chooseGpu() {
return 'GPU cannot draw to the page, copying frames';
}
-let frames = 0, last = performance.now(), lastGpu = 0, lastEmuIdle = 0, lastRenderIdle = 0;
+let frames = 0, last = performance.now(), lastGpu = 0, lastEmuIdle = 0, lastRenderIdle = 0, lastVuIdle = 0;
var Module = {
canvas,
arguments: ['/game/core.elf', '--arg', params.get('arg') || 'cdrom0:\\CORE.GT4;1'].concat(params.getAll('press').flatMap((p) => p.split(',').flatMap((x) => ['--press', x]))),
@@ -402,12 +402,14 @@ var Module = {
if (now - last >= 1000) {
const gpu = Module._ps2web_gpu_ms();
const memory = (Module.HEAPU8.length / 1048576).toFixed(0);
- const emuIdle = Module._ps2web_idle_ms(0), renderIdle = Module._ps2web_idle_ms(1), span = now - last;
+ const emuIdle = Module._ps2web_idle_ms(0), renderIdle = Module._ps2web_idle_ms(1), vuIdle = Module._ps2web_idle_ms(2), span = now - last;
statsEl.textContent = 'frame ' + Module._ps2web_frames() + ' ' + (frames * 1000 / span).toFixed(1) + ' fps GPU ' + ((gpu - lastGpu) / span * 100).toFixed(0) +
- '% idle emu ' + ((emuIdle - lastEmuIdle) / span * 100).toFixed(0) + '% gs ' + ((renderIdle - lastRenderIdle) / span * 100).toFixed(0) +
+ '% idle emu ' + ((emuIdle - lastEmuIdle) / span * 100).toFixed(0) + '% vu ' + ((vuIdle - lastVuIdle) / span * 100).toFixed(0) +
+ '% gs ' + ((renderIdle - lastRenderIdle) / span * 100).toFixed(0) +
'% heap ' + memory + ' MB audio ' + Module.audioQueuedMs().toFixed(0) + ' ms';
lastEmuIdle = emuIdle;
lastRenderIdle = renderIdle;
+ lastVuIdle = vuIdle;
lastGpu = gpu;
frames = 0;
last = now;