PS2 games in the browser: static recompilation to WebAssembly with a native runtime
C++
iop: read the disc image for cdvdman and ioman
Samir Nuri09696e912a02
diff --git a/runtime/include/ps2/disc.hpp b/runtime/include/ps2/disc.hppnew file mode 100644index 0000000..49d7eb4--- /dev/null+++ b/runtime/include/ps2/disc.hpp@@ -0,0 +1,25 @@+#pragma once++#include <cstdint>+#include <optional>+#include <string>+#include <vector>++namespace ps2::disc {++inline constexpr std::uint32_t kSector = 2048;++struct Entry {+ std::uint32_t sector;+ std::uint32_t size;+ bool directory;+};++bool open(const std::string &path);+bool is_open();+bool read(std::uint32_t sector, std::uint32_t count, void *out);+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.cppnew file mode 100644index 0000000..c2d016b--- /dev/null+++ b/runtime/src/disc.cpp@@ -0,0 +1,117 @@+#include "ps2/disc.hpp"++#include <algorithm>+#include <cctype>+#include <cstdio>+#include <cstring>+#include <map>++namespace ps2::disc {++namespace {++std::FILE *g_file = nullptr;+std::uint64_t g_size = 0;+std::uint32_t g_layer1 = 0;+std::map<std::string, Entry> g_index;++std::uint32_t u32(const std::uint8_t *p) {+ std::uint32_t v;+ std::memcpy(&v, p, 4);+ return v;+}++std::string upper(std::string s) {+ for (char &c : s) c = static_cast<char>(std::toupper(static_cast<unsigned char>(c)));+ return s;+}++void walk(std::uint32_t base, std::uint32_t lba, std::uint32_t size, const std::string &prefix, int depth) {+ if (depth > 16) return;+ std::vector<std::uint8_t> data((size + kSector - 1) / kSector * kSector);+ if (!read(base + lba, static_cast<std::uint32_t>(data.size() / kSector), data.data())) return;+ std::size_t off = 0;+ while (off < size) {+ const std::uint8_t length = data[off];+ if (!length) {+ off = (off / kSector + 1) * kSector;+ continue;+ }+ const std::uint8_t *rec = &data[off];+ off += length;+ const std::uint8_t name_len = rec[32];+ std::string name(reinterpret_cast<const char *>(rec + 33), name_len);+ if (name_len == 1 && (name[0] == 0 || name[0] == 1)) continue;+ const auto semi = name.find(';');+ if (semi != std::string::npos) name = name.substr(0, semi);+ const std::string path = prefix + upper(name);+ const bool dir = rec[25] & 2;+ const Entry entry{base + u32(rec + 2), u32(rec + 10), dir};+ g_index[path] = entry;+ if (dir) walk(base, u32(rec + 2), u32(rec + 10), path + "/", depth + 1);+ }+}++void index_layer(std::uint32_t pvd_sector, std::uint32_t base) {+ std::uint8_t pvd[kSector];+ if (!read(pvd_sector, 1, pvd) || std::memcmp(pvd, "\x01" "CD001", 6) != 0) return;+ walk(base, u32(pvd + 158), u32(pvd + 166), "", 0);+}++}++bool open(const std::string &path) {+ g_file = std::fopen(path.c_str(), "rb");+ if (!g_file) return false;+ std::fseek(g_file, 0, SEEK_END);+ g_size = static_cast<std::uint64_t>(std::ftell(g_file));+ g_index.clear();+ index_layer(16, 0);+ std::uint8_t pvd[kSector];+ read(16, 1, pvd);+ const std::uint32_t layer0 = u32(pvd + 80);+ for (std::uint32_t s = layer0; s < layer0 + 64 && static_cast<std::uint64_t>(s) * kSector < g_size; ++s) {+ std::uint8_t probe[kSector];+ if (read(s, 1, probe) && std::memcmp(probe, "\x01" "CD001", 6) == 0) {+ g_layer1 = s - 16;+ index_layer(s, g_layer1);+ break;+ }+ }+ std::fprintf(stderr, "[disc] %s: %zu files, %llu sectors, layer 1 at %u\n", path.c_str(), g_index.size(),+ static_cast<unsigned long long>(g_size / kSector), g_layer1);+ return true;+}++bool is_open() { return g_file != nullptr; }++bool read(std::uint32_t sector, std::uint32_t count, void *out) {+ if (!g_file) return false;+#if defined(_WIN32)+ _fseeki64(g_file, static_cast<long long>(sector) * kSector, SEEK_SET);+#else+ fseeko(g_file, static_cast<off_t>(sector) * kSector, SEEK_SET);+#endif+ const std::size_t want = static_cast<std::size_t>(count) * kSector;+ const std::size_t got = std::fread(out, 1, want, g_file);+ if (got < want) std::memset(static_cast<std::uint8_t *>(out) + got, 0, want - got);+ return got > 0 || !count;+}++std::optional<Entry> find(const std::string &raw) {+ std::string path = raw;+ const auto colon = path.find(':');+ if (colon != std::string::npos) path = path.substr(colon + 1);+ std::replace(path.begin(), path.end(), '\\', '/');+ const auto semi = path.find(';');+ if (semi != std::string::npos) path = path.substr(0, semi);+ while (!path.empty() && path.front() == '/') path.erase(path.begin());+ auto it = g_index.find(upper(path));+ if (it == g_index.end()) return std::nullopt;+ return it->second;+}++std::uint32_t layer1_start() { return g_layer1; }+std::uint64_t sector_count() { return g_size / kSector; }++}diff --git a/runtime/src/iop/cdvd.cpp b/runtime/src/iop/cdvd.cppnew file mode 100644index 0000000..0be316a--- /dev/null+++ b/runtime/src/iop/cdvd.cpp@@ -0,0 +1,180 @@+#include "kernel.hpp"++#include "ps2/disc.hpp"++#include <algorithm>+#include <cstdio>+#include <ctime>+#include <map>+#include <memory>++namespace ps2::iop {++namespace kernel {++namespace {++std::uint32_t arg(const Cpu &c, int i) { return i < 4 ? c.r[4 + i] : read32(c.r[29] + 16u + static_cast<std::uint32_t>(i - 4) * 4u); }++void def(const char *library, std::uint32_t index, const char *name, std::function<void(Cpu &)> body) {+ register_hle(library, index, [library, name, body = std::move(body)](Cpu &c) {+ trace_hle(library, name, c);+ body(c);+ });+}++struct CdState {+ bool busy = false;+ std::uint32_t callback = 0;+ std::uint32_t error = 0;+ std::vector<int> sync_waiters;+} g_cd;++std::uint8_t bcd(int v) { return static_cast<std::uint8_t>(((v / 10) << 4) | (v % 10)); }++void finish_read() {+ g_cd.busy = false;+ for (int id : g_cd.sync_waiters)+ if (Thread *t = thread(id)) wake(*t, 0);+ if (!g_cd.sync_waiters.empty()) reschedule();+ g_cd.sync_waiters.clear();+ if (g_cd.callback) call_guest(g_cd.callback, 1);+}++std::uint64_t read_cycles(std::uint32_t sectors) {+ return usec_to_cycles(400) + static_cast<std::uint64_t>(sectors) * usec_to_cycles(250);+}++bool start_read(std::uint32_t lsn, std::uint32_t sectors, std::uint32_t buffer, std::uint32_t mode, bool ee_memory);++}++std::vector<std::uint8_t> read_disc_file(const std::string &path) {+ const auto entry = disc::find(path);+ if (!entry || entry->directory) return {};+ std::vector<std::uint8_t> data((entry->size + disc::kSector - 1) / disc::kSector * disc::kSector);+ disc::read(entry->sector, static_cast<std::uint32_t>(data.size() / disc::kSector), data.data());+ data.resize(entry->size);+ return data;+}++bool cd_read(std::uint32_t lsn, std::uint32_t sectors, std::uint32_t buffer, std::uint32_t mode, std::function<void()> done);++namespace {++std::function<void()> g_ee_done;++bool start_read(std::uint32_t lsn, std::uint32_t sectors, std::uint32_t buffer, std::uint32_t, bool) {+ if (g_cd.busy) return false;+ g_cd.busy = true;+ g_cd.error = 0;+ std::vector<std::uint8_t> data(static_cast<std::size_t>(sectors) * disc::kSector);+ disc::read(lsn, sectors, data.data());+ for (std::size_t i = 0; i < data.size(); ++i) write8(buffer + static_cast<std::uint32_t>(i), data[i]);+ schedule(now() + read_cycles(sectors), finish_read);+ return true;+}++}++void install_cdvd() {+ g_cd = CdState{};+ register_hle_library("cdvdman", 0x0101);+ def("cdvdman", 4, "sceCdInit", [](Cpu &c) { c.r[2] = 1; });+ def("cdvdman", 5, "sceCdStandby", [](Cpu &c) { c.r[2] = 1; });+ def("cdvdman", 6, "sceCdRead", [](Cpu &c) {+ if (g_trace_hle) std::fprintf(stderr, "[cdvd] read lsn %u count %u to 0x%08X\n", arg(c, 0), arg(c, 1), arg(c, 2));+ c.r[2] = start_read(arg(c, 0), arg(c, 1), arg(c, 2), arg(c, 3), false) ? 1u : 0u;+ });+ def("cdvdman", 7, "sceCdSeek", [](Cpu &c) { c.r[2] = 1; });+ def("cdvdman", 8, "sceCdGetError", [](Cpu &c) { c.r[2] = g_cd.error; });+ def("cdvdman", 10, "sceCdSearchFile", [](Cpu &c) {+ const std::string name = ram_string(arg(c, 1), 256);+ const auto entry = disc::find(name);+ if (!entry) { c.r[2] = 0; return; }+ const std::uint32_t p = arg(c, 0);+ write32(p, entry->sector);+ write32(p + 4, entry->size);+ const std::string base = name.substr(name.find_last_of("\\/") == std::string::npos ? 0 : name.find_last_of("\\/") + 1);+ for (std::uint32_t i = 0; i < 16; ++i) write8(p + 8 + i, i < base.size() ? static_cast<std::uint8_t>(base[i]) : 0);+ c.r[2] = 1;+ });+ def("cdvdman", 11, "sceCdSync", [](Cpu &c) {+ if (arg(c, 0) == 1 || arg(c, 0) == 17) { c.r[2] = g_cd.busy ? 1u : 0u; return; }+ c.r[2] = 0;+ if (!g_cd.busy || !current()) return;+ g_cd.sync_waiters.push_back(current()->id);+ block(c, kWaitHle, 0);+ });+ def("cdvdman", 12, "sceCdGetDiskType", [](Cpu &c) { c.r[2] = 0x14; });+ def("cdvdman", 13, "sceCdDiskReady", [](Cpu &c) { c.r[2] = 2; });+ def("cdvdman", 14, "sceCdTrayReq", [](Cpu &c) { if (arg(c, 1)) write32(arg(c, 1), 0); c.r[2] = 1; });+ def("cdvdman", 15, "sceCdStop", [](Cpu &c) { c.r[2] = 1; });+ def("cdvdman", 22, "sceCdRI", [](Cpu &c) {+ for (std::uint32_t i = 0; i < 8; ++i) write8(arg(c, 0) + i, 0);+ if (arg(c, 1)) write32(arg(c, 1), 0);+ c.r[2] = 1;+ });+ def("cdvdman", 24, "sceCdReadClock", [](Cpu &c) {+ const std::time_t t = std::time(nullptr);+ const std::tm *tm = std::gmtime(&t);+ const std::uint32_t p = arg(c, 0);+ write8(p + 0, 0);+ write8(p + 1, bcd(tm->tm_sec));+ write8(p + 2, bcd(tm->tm_min));+ write8(p + 3, bcd((tm->tm_hour + 9) % 24));+ write8(p + 4, 0);+ write8(p + 5, bcd(tm->tm_mday));+ write8(p + 6, bcd(tm->tm_mon + 1));+ write8(p + 7, bcd(tm->tm_year % 100));+ c.r[2] = 1;+ });+ def("cdvdman", 28, "sceCdStatus", [](Cpu &c) { c.r[2] = g_cd.busy ? 0x06u : 0x0Au; });+ def("cdvdman", 37, "sceCdCallback", [](Cpu &c) { c.r[2] = g_cd.callback; g_cd.callback = arg(c, 0); });+ def("cdvdman", 38, "sceCdPause", [](Cpu &c) { c.r[2] = 1; });+ def("cdvdman", 39, "sceCdBreak", [](Cpu &c) { c.r[2] = 1; });+ def("cdvdman", 74, "sceCdPowerOff", [](Cpu &c) { c.r[2] = 1; });+ def("cdvdman", 75, "sceCdMmode", [](Cpu &c) { c.r[2] = 1; });+ def("cdvdman", 78, "sceCdPOffCallback", [](Cpu &c) { c.r[2] = 0; });+ def("cdvdman", 83, "sceCdReadDvdDualInfo", [](Cpu &c) {+ write32(arg(c, 0), disc::layer1_start() ? 1u : 0u);+ write32(arg(c, 1), disc::layer1_start());+ c.r[2] = 1;+ });+ def("cdvdman", 84, "sceCdLayerSearchFile", [](Cpu &c) {+ const std::string name = ram_string(arg(c, 1), 256);+ const auto entry = disc::find(name);+ if (!entry) { c.r[2] = 0; return; }+ write32(arg(c, 0), entry->sector);+ write32(arg(c, 0) + 4, entry->size);+ c.r[2] = 1;+ });+}++bool cd_busy() { return g_cd.busy; }++bool cd_read(std::uint32_t lsn, std::uint32_t sectors, std::uint32_t buffer, std::uint32_t mode, std::function<void()> done) {+ if (!start_read(lsn, sectors, buffer, mode, false)) return false;+ g_ee_done = std::move(done);+ schedule(now() + read_cycles(sectors) + 1, []() {+ auto d = std::move(g_ee_done);+ g_ee_done = nullptr;+ if (d) d();+ });+ return true;+}++}++std::int32_t load_module(const std::string &path, const std::string &args, std::function<void(int, int)> done) {+ std::vector<std::uint8_t> data;+ std::string lower = path;+ for (char &ch : lower) ch = static_cast<char>(std::tolower(static_cast<unsigned char>(ch)));+ if (lower.rfind("cdrom", 0) == 0) data = kernel::read_disc_file(path);+ if (data.empty() && lower.rfind("rom0:", 0) != 0) {+ std::fprintf(stderr, "[iop] cannot read module %s\n", path.c_str());+ }+ return load_module_buffer(data, path, args, std::move(done));+}++}diff --git a/runtime/src/iop/io.cpp b/runtime/src/iop/io.cppnew file mode 100644index 0000000..9bd1009--- /dev/null+++ b/runtime/src/iop/io.cpp@@ -0,0 +1,171 @@+#include "io.hpp"+#include "kernel.hpp"++#include "ps2/disc.hpp"++#include <algorithm>+#include <cstdio>+#include <map>++namespace ps2::iop::io {++namespace {++constexpr int kNoEntry = -2;+constexpr int kBadFd = -9;+constexpr int kTooMany = -24;++struct File {+ std::uint32_t sector = 0;+ std::uint32_t size = 0;+ std::uint64_t position = 0;+ std::vector<std::uint8_t> memory;+ bool in_memory = false;+};++std::map<int, File> g_files;++std::string device_of(const std::string &path) {+ const auto colon = path.find(':');+ std::string d = colon == std::string::npos ? "" : path.substr(0, colon);+ for (char &ch : d) ch = static_cast<char>(std::tolower(static_cast<unsigned char>(ch)));+ while (!d.empty() && std::isdigit(static_cast<unsigned char>(d.back()))) d.pop_back();+ return d;+}++int allocate() {+ for (int fd = 3; fd < 64; ++fd)+ if (!g_files.count(fd)) return fd;+ return kTooMany;+}++}++int open(const std::string &path, std::uint32_t flags) {+ const std::string device = device_of(path);+ File f;+ if (device == "cdrom") {+ const auto entry = disc::find(path);+ if (!entry || entry->directory) return kNoEntry;+ f.sector = entry->sector;+ f.size = entry->size;+ } else if (device == "rom") {+ std::string name = path.substr(path.find(':') + 1);+ if (name == "ROMVER") {+ const std::string text = "0220EC20060210\n";+ f.memory.assign(text.begin(), text.end());+ } else if (name == "OSDSYS" || name.rfind("rom0:", 0) == 0) {+ return kNoEntry;+ } else {+ return kNoEntry;+ }+ f.in_memory = true;+ f.size = static_cast<std::uint32_t>(f.memory.size());+ } else {+ std::fprintf(stderr, "[io] open %s (flags 0x%X) on unsupported device\n", path.c_str(), flags);+ return kNoEntry;+ }+ const int fd = allocate();+ if (fd < 0) return fd;+ g_files[fd] = std::move(f);+ if (kernel::g_trace_hle) std::fprintf(stderr, "[io] open %s -> %d\n", path.c_str(), fd);+ return fd;+}++int close(int fd) { return g_files.erase(fd) ? 0 : kBadFd; }++int read(int fd, std::uint8_t *out, std::uint32_t size) {+ auto it = g_files.find(fd);+ if (it == g_files.end()) return kBadFd;+ File &f = it->second;+ if (f.position >= f.size) return 0;+ const std::uint32_t n = static_cast<std::uint32_t>(std::min<std::uint64_t>(size, f.size - f.position));+ if (f.in_memory) {+ std::memcpy(out, f.memory.data() + f.position, n);+ } else {+ const std::uint64_t start = f.position, end = f.position + n;+ const std::uint32_t first = static_cast<std::uint32_t>(start / disc::kSector);+ const std::uint32_t last = static_cast<std::uint32_t>((end + disc::kSector - 1) / disc::kSector);+ std::vector<std::uint8_t> buffer(static_cast<std::size_t>(last - first) * disc::kSector);+ disc::read(f.sector + first, last - first, buffer.data());+ std::memcpy(out, buffer.data() + (start - static_cast<std::uint64_t>(first) * disc::kSector), n);+ }+ f.position += n;+ return static_cast<int>(n);+}++int write(int fd, const std::uint8_t *, std::uint32_t size) {+ if (fd == 1 || fd == 2) return static_cast<int>(size);+ return g_files.count(fd) ? static_cast<int>(size) : kBadFd;+}++std::int64_t seek(int fd, std::int64_t offset, int whence) {+ auto it = g_files.find(fd);+ if (it == g_files.end()) return kBadFd;+ File &f = it->second;+ std::int64_t base = whence == 1 ? static_cast<std::int64_t>(f.position) : whence == 2 ? f.size : 0;+ const std::int64_t target = std::clamp<std::int64_t>(base + offset, 0, f.size);+ f.position = static_cast<std::uint64_t>(target);+ return target;+}++bool stat(const std::string &path, std::uint32_t &mode, std::uint32_t &size) {+ if (device_of(path) != "cdrom") return false;+ const auto entry = disc::find(path);+ if (!entry) return false;+ mode = entry->directory ? 0x0027u : 0x0017u;+ size = entry->size;+ return true;+}++void reset() { g_files.clear(); }++}++namespace ps2::iop::kernel {++namespace {++std::uint32_t arg(const Cpu &c, int i) { return i < 4 ? c.r[4 + i] : read32(c.r[29] + 16u + static_cast<std::uint32_t>(i - 4) * 4u); }++void def(const char *library, std::uint32_t index, const char *name, std::function<void(Cpu &)> body) {+ register_hle(library, index, [library, name, body = std::move(body)](Cpu &c) {+ trace_hle(library, name, c);+ body(c);+ });+}++}++void install_io() {+ io::reset();+ register_hle_library("ioman", 0x0104);+ def("ioman", 4, "open", [](Cpu &c) { c.r[2] = static_cast<std::uint32_t>(io::open(ram_string(arg(c, 0)), arg(c, 1))); });+ def("ioman", 5, "close", [](Cpu &c) { c.r[2] = static_cast<std::uint32_t>(io::close(static_cast<int>(arg(c, 0)))); });+ def("ioman", 6, "read", [](Cpu &c) {+ std::vector<std::uint8_t> buffer(arg(c, 2));+ const int n = io::read(static_cast<int>(arg(c, 0)), buffer.data(), arg(c, 2));+ for (int i = 0; i < n; ++i) write8(arg(c, 1) + static_cast<std::uint32_t>(i), buffer[static_cast<std::size_t>(i)]);+ c.r[2] = static_cast<std::uint32_t>(n);+ });+ def("ioman", 7, "write", [](Cpu &c) {+ if (arg(c, 0) == 1 || arg(c, 0) == 2) std::fprintf(stderr, "[iop-tty] %s", ram_string(arg(c, 1), arg(c, 2)).c_str());+ c.r[2] = static_cast<std::uint32_t>(io::write(static_cast<int>(arg(c, 0)), nullptr, arg(c, 2)));+ });+ def("ioman", 8, "lseek", [](Cpu &c) {+ c.r[2] = static_cast<std::uint32_t>(io::seek(static_cast<int>(arg(c, 0)), static_cast<std::int32_t>(arg(c, 1)), static_cast<int>(arg(c, 2))));+ });+ def("ioman", 16, "getstat", [](Cpu &c) {+ std::uint32_t mode = 0, size = 0;+ if (!io::stat(ram_string(arg(c, 0)), mode, size)) { c.r[2] = static_cast<std::uint32_t>(-2); return; }+ const std::uint32_t p = arg(c, 1);+ std::memset(ram_ptr(p), 0, 40);+ write32(p, mode);+ write32(p + 8, size);+ c.r[2] = 0;+ });+ for (std::uint32_t i : {9u, 10u, 11u, 12u, 13u, 14u, 15u, 17u, 20u, 21u, 25u, 27u, 31u})+ def("ioman", i, "ioman", [](Cpu &c) { c.r[2] = static_cast<std::uint32_t>(-1); });+}++}diff --git a/runtime/src/iop/io.hpp b/runtime/src/iop/io.hppnew file mode 100644index 0000000..b2538c6--- /dev/null+++ b/runtime/src/iop/io.hpp@@ -0,0 +1,16 @@+#pragma once++#include <cstdint>+#include <string>++namespace ps2::iop::io {++int open(const std::string &path, std::uint32_t flags);+int close(int fd);+int read(int fd, std::uint8_t *out, std::uint32_t size);+int write(int fd, const std::uint8_t *data, std::uint32_t size);+std::int64_t seek(int fd, std::int64_t offset, int whence);+bool stat(const std::string &path, std::uint32_t &mode, std::uint32_t &size);+void reset();++}