Skip to content

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

C++

Web: disc reads wait in emulated time instead of blocking

Samir Nuri1066c9eea78c
diff --git a/host/web.cpp b/host/web.cpp
index 36dc962..9369ee2 100644
--- a/host/web.cpp
+++ b/host/web.cpp
@@ -183,12 +183,20 @@ Slot *want(std::uint64_t index) {
return victim;
}
-const Slot *chunk(std::uint64_t index) {
- Slot *s = want(index);
- if (index == g_last_chunk + 1) ++g_run;
- else if (index != g_last_chunk) g_run = 0;
+void advance(std::uint64_t index) {
+ if (index <= g_last_chunk && g_last_chunk - index <= kReadAhead * 4) return;
+ g_run = index == g_last_chunk + 1 ? g_run + 1 : 0;
g_last_chunk = index;
+}
+
+void read_ahead(std::uint64_t index) {
for (std::uint64_t k = 1; k <= std::min(g_run * 2, kReadAhead); ++k) want(index + k);
+}
+
+const Slot *chunk(std::uint64_t index) {
+ Slot *s = want(index);
+ advance(index);
+ read_ahead(std::max(index, g_last_chunk));
for (int attempt = 0; s && attempt < 4; ++attempt) {
for (std::uint32_t state = s->state.load(); state != kReady && state != kFailed; state = s->state.load())
emscripten_futex_wait(&s->state, state, INFINITY);
@@ -203,6 +211,23 @@ const Slot *chunk(std::uint64_t index) {
return nullptr;
}
+bool disc_ready(std::uint64_t offset, std::size_t size) {
+ if (!size) return true;
+ const std::uint64_t last = (offset + size - 1) / kChunk;
+ bool ready = true;
+ for (std::uint64_t index = offset / kChunk; index <= last; ++index) {
+ Slot *s = want(index);
+ if (s && s->state.load(std::memory_order_acquire) == kFailed) {
+ s->state.store(kWanted, std::memory_order_release);
+ ps2::wake_render_thread();
+ }
+ ready = ready && s && s->state.load(std::memory_order_acquire) == kReady;
+ advance(index);
+ }
+ read_ahead(std::max(last, g_last_chunk));
+ return ready;
+}
+
std::size_t read_disc(std::uint64_t offset, void *out, std::size_t size) {
std::size_t done = 0;
auto *to = static_cast<std::uint8_t *>(out);
@@ -268,7 +293,7 @@ struct Launch {
void *emulate(void *data) {
const Launch &launch = *static_cast<const Launch *>(data);
- if (g_disc_size && !ps2::disc::open("disc", g_disc_size, read_disc)) std::fprintf(stderr, "[web] cannot read the disc image\n");
+ if (g_disc_size && !ps2::disc::open("disc", g_disc_size, read_disc, disc_ready)) std::fprintf(stderr, "[web] cannot read the disc image\n");
std::uint32_t entry = 0;
if (!ps2::load_elf("/game/core.elf", entry)) {
std::fprintf(stderr, "cannot load the game executable\n");
diff --git a/runtime/include/ps2/disc.hpp b/runtime/include/ps2/disc.hpp
index 2af39a0..0f1af64 100644
--- a/runtime/include/ps2/disc.hpp
+++ b/runtime/include/ps2/disc.hpp
@@ -18,11 +18,13 @@ struct Entry {
};
using Reader = std::function<std::size_t(std::uint64_t offset, void *out, std::size_t size)>;
+using Probe = std::function<bool(std::uint64_t offset, std::size_t size)>;
bool open(const std::string &path);
-bool open(const std::string &name, std::uint64_t size, Reader reader);
+bool open(const std::string &name, std::uint64_t size, Reader reader, Probe probe = {});
bool is_open();
bool read(std::uint32_t sector, std::uint32_t count, void *out);
+bool ready(std::uint32_t sector, std::uint32_t count);
std::optional<Entry> find(const std::string &path);
std::uint32_t layer1_start();
std::uint64_t sector_count();
diff --git a/runtime/src/disc.cpp b/runtime/src/disc.cpp
index 7d1a45c..4491f35 100644
--- a/runtime/src/disc.cpp
+++ b/runtime/src/disc.cpp
@@ -12,6 +12,7 @@ namespace {
std::FILE *g_file = nullptr;
Reader g_reader;
+Probe g_probe;
std::uint64_t g_size = 0;
std::uint32_t g_layer1 = 0;
std::map<std::string, Entry> g_index;
@@ -82,8 +83,9 @@ bool open(const std::string &path) {
});
}
-bool open(const std::string &name, std::uint64_t size, Reader reader) {
+bool open(const std::string &name, std::uint64_t size, Reader reader, Probe probe) {
g_reader = std::move(reader);
+ g_probe = std::move(probe);
g_size = size;
g_index.clear();
index_layer(16, 0);
@@ -113,6 +115,10 @@ bool read(std::uint32_t sector, std::uint32_t count, void *out) {
return got > 0 || !count;
}
+bool ready(std::uint32_t sector, std::uint32_t count) {
+ return !g_probe || g_probe(static_cast<std::uint64_t>(sector) * kSector, static_cast<std::size_t>(count) * kSector);
+}
+
std::optional<Entry> find(const std::string &raw) {
std::string path = raw;
const auto colon = path.find(':');
diff --git a/runtime/src/iop/cdvd.cpp b/runtime/src/iop/cdvd.cpp
index c638ef3..218cbda 100644
--- a/runtime/src/iop/cdvd.cpp
+++ b/runtime/src/iop/cdvd.cpp
@@ -66,6 +66,7 @@ bool start_read(std::uint32_t lsn, std::uint32_t sectors, std::uint32_t buffer,
if (g_cd.busy) return false;
g_cd.busy = true;
g_cd.error = 0;
+ disc::ready(lsn, sectors);
schedule(now() + read_cycles(sectors), Job{kJobCdRead, lsn, sectors, buffer});
return true;
}
@@ -75,6 +76,10 @@ bool start_read(std::uint32_t lsn, std::uint32_t sectors, std::uint32_t buffer,
void install_cdvd() {
g_cd = CdState{};
register_job(kJobCdRead, [](const Job &j) {
+ if (!disc::ready(j.a, j.b)) {
+ schedule(now() + usec_to_cycles(1000), j);
+ return;
+ }
std::vector<std::uint8_t> data(static_cast<std::size_t>(j.b) * disc::kSector);
disc::read(j.a, j.b, data.data());
for (std::size_t i = 0; i < data.size(); ++i) write8(j.c + static_cast<std::uint32_t>(i), data[i]);
diff --git a/runtime/src/iop/cdvdfsv.cpp b/runtime/src/iop/cdvdfsv.cpp
index 66a0d6a..28407ca 100644
--- a/runtime/src/iop/cdvdfsv.cpp
+++ b/runtime/src/iop/cdvdfsv.cpp
@@ -37,11 +37,19 @@ void install_cdvdfsv() {
g_streaming = false;
g_stream_pos = 0;
register_job(kJobFsvRead, [](const Job &j) {
+ if (!disc::ready(j.a, j.b)) {
+ schedule(now() + usec_to_cycles(1000), j);
+ return;
+ }
read_to_ee(j.a, j.b, j.c);
sif::complete_call(0x80000595u);
});
register_job(kJobFsvComplete, [](const Job &) { sif::complete_call(0x80000595u); });
register_job(kJobFsvIopRead, [](const Job &j) {
+ if (!disc::ready(j.a, j.b)) {
+ schedule(now() + usec_to_cycles(1000), j);
+ return;
+ }
std::vector<std::uint8_t> data(static_cast<std::size_t>(j.b) * disc::kSector);
disc::read(j.a, j.b, data.data());
for (std::size_t i = 0; i < data.size(); ++i) write8(j.c + static_cast<std::uint32_t>(i), data[i]);
@@ -92,6 +100,7 @@ void install_cdvdfsv() {
switch (method) {
case 0x01:
if (ret_size >= 4) set_ee32(ret, 0);
+ disc::ready(sector, count);
schedule(now() + delay(count), Job{kJobFsvRead, sector, count, dst});
return false;
case 0x04: set_ee32(ret, 1); return true;
@@ -107,6 +116,7 @@ void install_cdvdfsv() {
const std::uint32_t pos = g_stream_pos;
g_stream_pos += count;
set_ee32(ret, count);
+ disc::ready(pos, count);
schedule(now() + delay(count), Job{kJobFsvRead, pos, count, dst});
return false;
}
@@ -119,6 +129,7 @@ void install_cdvdfsv() {
}
case 0x0D:
if (ret_size >= 4) set_ee32(ret, 0);
+ disc::ready(sector, count);
schedule(now() + delay(count), Job{kJobFsvIopRead, sector, count, dst});
return false;
case 0x0E: set_ee32(ret, 2); return true;