Skip to content

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

C++

HLE RPC servers answer after about an IOP call's latency, not at once

Dusan Bako5a0018e52cc5
diff --git a/runtime/src/iop/kernel.hpp b/runtime/src/iop/kernel.hpp
index 19c105e..396e6f8 100644
--- a/runtime/src/iop/kernel.hpp
+++ b/runtime/src/iop/kernel.hpp
@@ -64,6 +64,7 @@ enum JobKind : std::uint32_t {
kJobFsvComplete,
kJobFsvIopRead,
kJobSifDmaCallback,
+ kJobSifReply,
kJobCount
};
struct Job {
diff --git a/runtime/src/iop/sif.cpp b/runtime/src/iop/sif.cpp
index 3666a9f..6bfddb2 100644
--- a/runtime/src/iop/sif.cpp
+++ b/runtime/src/iop/sif.cpp
@@ -27,6 +27,10 @@ constexpr std::uint32_t kServerXor = 0xACACACACu;
constexpr std::uint32_t kCmdBuffer = 0x0001E000u;
constexpr std::uint32_t kCmdRend = 0x80000008u;
constexpr std::uint32_t kCmdSetSreg = 0x80000001u;
+// An HLE server answers after about the time a call takes on a real IOP. Games
+// depend on it: the calling thread blocks meanwhile and lower priority threads
+// run, as Downhill Domination's loader needs its worker to reach SleepThread.
+constexpr std::uint64_t kHleReplyUsec = 50;
struct Header {
std::uint32_t size_word, dest, cid, opt;
@@ -72,6 +76,7 @@ std::deque<std::vector<std::uint8_t>> g_outgoing;
std::map<std::uint32_t, Server> g_servers;
std::map<std::uint32_t, PendingBind> g_binds;
std::map<std::uint32_t, Call> g_held_calls;
+std::deque<Call> g_hle_replies;
std::map<std::uint32_t, std::deque<Request>> g_requests;
std::map<std::uint32_t, int> g_queue_threads;
std::map<std::uint32_t, CmdHandler> g_cmd_handlers;
@@ -154,10 +159,14 @@ void call(const std::uint8_t *packet) {
return;
}
if (it->second.hle) {
+ // The IOP runs behind the EE by up to a slice; catch it up so the reply
+ // latency counts from the moment of the call.
+ sync_iop();
g_held_calls[server] = c;
if (it->second.invoke(c.rpc_number, g_data_addr, c.send_size, c.recv & (kRamSize - 1), c.recv_size)) {
g_held_calls.erase(server);
- reply_call(c);
+ g_hle_replies.push_back(c);
+ iop::kernel::schedule(iop::kernel::now() + iop::kernel::usec_to_cycles(kHleReplyUsec), iop::kernel::Job{iop::kernel::kJobSifReply});
}
return;
}
@@ -343,6 +352,12 @@ void rpc_finish(iop::Cpu &c) {
void install_iop_side() {
iop::kernel::register_job(iop::kernel::kJobSifDmaCallback, [](const iop::kernel::Job &j) { iop::kernel::call_guest(j.a, j.b); });
+ iop::kernel::register_job(iop::kernel::kJobSifReply, [](const iop::kernel::Job &) {
+ if (g_hle_replies.empty()) return;
+ const Call c = g_hle_replies.front();
+ g_hle_replies.pop_front();
+ reply_call(c);
+ });
g_loop_stub = iop::kernel::hle_stub(rpc_loop);
g_finish_stub = iop::kernel::hle_stub(rpc_finish);
@@ -684,6 +699,7 @@ void reset() {
g_servers.clear();
g_binds.clear();
g_held_calls.clear();
+ g_hle_replies.clear();
g_requests.clear();
g_queue_threads.clear();
g_cmd_handlers.clear();
@@ -862,6 +878,7 @@ void state(ps2::state::Stream &s) {
s.pod(g_ee_dma_id);
s.pod(g_fileio_async);
s.pod(g_fileio_result);
+ if (s.version() >= 4) s.deque(g_hle_replies, [&s](Call &c) { s.pod(c); });
}
}
diff --git a/runtime/src/system.cpp b/runtime/src/system.cpp
index 35802ac..f44c012 100644
--- a/runtime/src/system.cpp
+++ b/runtime/src/system.cpp
@@ -244,7 +244,7 @@ const std::string &halt_reason() { return g_halt_reason; }
namespace {
constexpr char kStateMagic[8] = {'P', 'S', '2', 'S', 'T', 'A', 'T', 'E'};
-constexpr std::uint32_t kStateVersion = 3;
+constexpr std::uint32_t kStateVersion = 4;
std::function<void()> event_action(int id, std::uint64_t at) {
if (id == hw::kEventVblank) return vblank;
@@ -269,7 +269,7 @@ void machine_state(state::Stream &s) {
std::uint32_t version = kStateVersion;
s.pod(version);
if (std::memcmp(magic, kStateMagic, sizeof magic) != 0) throw std::runtime_error("not a state file");
- if (version != kStateVersion && !(s.loading() && version == 2)) throw std::runtime_error("state file from another version");
+ if (version != kStateVersion && !(s.loading() && (version == 2 || version == 3))) throw std::runtime_error("state file from another version");
s.set_version(version);
s.tag("ee");
s.pod(g_cpu);