Skip to content

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

C++

Boot like the BIOS: cdrom0 argv0, SetupThread args, modules from memory

Samir Nuriebb089dfb387
diff --git a/runtime/src/iop/sif.cpp b/runtime/src/iop/sif.cpp
index fc368f9..f40cf30 100644
--- a/runtime/src/iop/sif.cpp
+++ b/runtime/src/iop/sif.cpp
@@ -533,6 +533,39 @@ void install_servers() {
}
return false;
}
+ if (method == 0x06) {
+ const std::uint32_t ptr = ee_read<std::uint32_t>(args) & (iop::kRamSize - 1);
+ const std::uint32_t arg_len = ee_read<std::uint32_t>(args + 4);
+ const std::string margs(reinterpret_cast<const char *>(g_ram + args + 8 + 252), std::min<std::uint32_t>(arg_len, 252));
+ const std::uint8_t *elf = iop::ram_ptr(ptr);
+ std::uint32_t size = 0;
+ if (std::memcmp(elf, "\x7F" "ELF", 4) == 0) {
+ const auto u16 = [elf](std::uint32_t o) { return static_cast<std::uint32_t>(elf[o] | (elf[o + 1] << 8)); };
+ const auto u32 = [elf](std::uint32_t o) { return static_cast<std::uint32_t>(elf[o] | (elf[o + 1] << 8) | (elf[o + 2] << 16) | (elf[o + 3] << 24)); };
+ size = u32(0x20) + u16(0x30) * u16(0x2E);
+ for (std::uint32_t i = 0; i < u16(0x2C); ++i) {
+ const std::uint32_t ph = u32(0x1C) + i * u16(0x2A);
+ size = std::max(size, u32(ph + 4) + u32(ph + 16));
+ }
+ for (std::uint32_t i = 0; i < u16(0x30); ++i) {
+ const std::uint32_t sh = u32(0x20) + i * u16(0x2E);
+ if (u32(sh + 4) != 8u) size = std::max(size, u32(sh + 16) + u32(sh + 20));
+ }
+ }
+ size = std::min(size, iop::kRamSize - ptr);
+ std::fprintf(stderr, "[loadfile] buffer at 0x%X, %u bytes\n", ptr, size);
+ const std::int32_t id = size ? iop::load_module_buffer(std::vector<std::uint8_t>(elf, elf + size), "buffer", margs, [ret](int module, int result) {
+ store<std::int32_t>(g_ram + ret, module);
+ store<std::int32_t>(g_ram + ret + 4, result);
+ complete_call(0x80000006u);
+ }) : -1;
+ if (id < 0) {
+ store<std::int32_t>(g_ram + ret, id);
+ store<std::int32_t>(g_ram + ret + 4, 0);
+ return true;
+ }
+ return false;
+ }
if (method == 0x09) {
store<std::int32_t>(g_ram + ret, -202);
return true;
diff --git a/runtime/src/kernel.cpp b/runtime/src/kernel.cpp
index de0f007..543069b 100644
--- a/runtime/src/kernel.cpp
+++ b/runtime/src/kernel.cpp
@@ -380,7 +380,11 @@ void reset() {
std::memset(g_ram + kSyscallTable, 0, 0x400);
}
+std::uint32_t g_boot_argc = 0, g_boot_argv = 0;
+
void start_main_thread(std::uint32_t entry, std::uint32_t gp, std::uint32_t argc, std::uint32_t argv) {
+ g_boot_argc = argc;
+ g_boot_argv = argv;
Thread t;
t.id = 1;
t.status = kRun;
@@ -597,6 +601,27 @@ void handle_syscall(Cpu &c, std::uint32_t pc) {
t->stack = top - size;
t->stack_size = size;
t->gp = arg(c, 0);
+ if (const std::uint32_t args = arg(c, 3); args && g_boot_argv) {
+ const std::uint32_t count = std::min<std::uint32_t>(g_boot_argc, 16u);
+ std::uint32_t payload = args + 4u + 16u * 4u;
+ const std::uint32_t end = payload + 256u;
+ std::uint32_t used = 0;
+ for (std::uint32_t i = 0; i < count; ++i) {
+ const std::uint32_t from = ld32(g_boot_argv + 4u * i);
+ st32(args + 4u + 4u * i, payload);
+ std::uint32_t k = 0;
+ for (; payload + k + 1u < end; ++k) {
+ const std::uint8_t ch = ld8(from + k);
+ st8(payload + k, ch);
+ if (!ch) break;
+ }
+ st8(payload + k, 0);
+ payload += k + 1u;
+ ++used;
+ if (payload >= end) break;
+ }
+ st32(args, used);
+ }
ret(c, top - kContextReserve);
break;
}
@@ -658,6 +683,7 @@ void handle_syscall(Cpu &c, std::uint32_t pc) {
case 0x4A: g_osd_config = ld32(arg(c, 0)); ret(c, 0); break;
case 0x4B: st32(arg(c, 0), g_osd_config); ret(c, 0); break;
case 0x64: case 0x68: ret(c, 0); break;
+ case 0x5B: case 0x6B: ret(c, 0); break;
case 0x70: ret(c, static_cast<std::int64_t>(hw::gs_imr())); break;
case 0x71: { const std::uint64_t old = hw::gs_imr(); hw::gs_set_imr(arg(c, 0)); ret(c, static_cast<std::int64_t>(old)); break; }
case 0x73: g_vsync_flag = arg(c, 0); g_vsync_data = arg(c, 1); ret(c, 0); break;
@@ -747,7 +773,9 @@ const char *syscall_name(std::uint32_t n) {
case 0x48: return "iReferSemaStatus";
case 0x4A: return "SetOsdConfigParam";
case 0x4B: return "GetOsdConfigParam";
+ case 0x5B: return "GetEntryAddress";
case 0x64: return "FlushCache";
+ case 0x6B: return "SifStopDma";
case 0x68: return "iFlushCache";
case 0x70: return "GsGetIMR";
case 0x71: return "GsPutIMR";
diff --git a/runtime/src/system.cpp b/runtime/src/system.cpp
index 03b83a0..3f95806 100644
--- a/runtime/src/system.cpp
+++ b/runtime/src/system.cpp
@@ -22,6 +22,7 @@
#include <algorithm>
#include <atomic>
+#include <cctype>
#include <chrono>
#include <cstdio>
#include <cstring>
@@ -241,14 +242,20 @@ void boot(std::uint32_t entry, const std::vector<std::string> &args) {
std::fprintf(stderr, "[ee] cannot open disc image %s\n", g_options.disc_path.c_str());
iop::set_trace(g_options.trace_iop_modules, g_options.trace_iop_hle);
iop::reset();
- std::uint32_t argv = 0x00000800u, payload = argv + 4u * static_cast<std::uint32_t>(args.size() + 1);
- for (std::size_t i = 0; i < args.size(); ++i) {
+ std::vector<std::string> guest_args = args;
+ if (!guest_args.empty() && guest_args[0].find(':') == std::string::npos) {
+ std::string name = guest_args[0].substr(guest_args[0].find_last_of("/\\") + 1);
+ for (char &ch : name) ch = static_cast<char>(std::toupper(static_cast<unsigned char>(ch)));
+ guest_args[0] = "cdrom0:\\" + name + ";1";
+ }
+ std::uint32_t argv = 0x00000800u, payload = argv + 4u * static_cast<std::uint32_t>(guest_args.size() + 1);
+ for (std::size_t i = 0; i < guest_args.size(); ++i) {
store<std::uint32_t>(g_ram + argv + 4 * i, payload);
- std::memcpy(g_ram + payload, args[i].c_str(), args[i].size() + 1);
- payload += static_cast<std::uint32_t>(args[i].size() + 1);
+ std::memcpy(g_ram + payload, guest_args[i].c_str(), guest_args[i].size() + 1);
+ payload += static_cast<std::uint32_t>(guest_args[i].size() + 1);
}
- store<std::uint32_t>(g_ram + argv + 4 * args.size(), 0);
- kernel::start_main_thread(entry, 0, static_cast<std::uint32_t>(args.size()), argv);
+ store<std::uint32_t>(g_ram + argv + 4 * guest_args.size(), 0);
+ kernel::start_main_thread(entry, 0, static_cast<std::uint32_t>(guest_args.size()), argv);
schedule(hw::kEventVblank, frame_cycles(), vblank);
}