Skip to content

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

C++

Scripted pad input, and the libpad button order

Samir Nurifdbcb3170df0
diff --git a/host/main.cpp b/host/main.cpp
index 63047ea..615fc2e 100644
--- a/host/main.cpp
+++ b/host/main.cpp
@@ -10,11 +10,41 @@ void register_generated_code();
namespace {
+bool parse_press(const std::string &spec, ps2::Press &press) {
+ static const char *const kNames[16] = {"select", "l3", "r3", "start", "up", "right", "down", "left",
+ "l2", "r2", "l1", "r1", "triangle", "circle", "cross", "square"};
+ const auto colon = spec.find(':');
+ if (colon == std::string::npos) return false;
+ press.frame = std::strtoull(spec.c_str(), nullptr, 10);
+ std::string rest = spec.substr(colon + 1);
+ press.frames = 6;
+ const auto second = rest.find(':');
+ if (second != std::string::npos) {
+ press.frames = static_cast<std::uint32_t>(std::strtoul(rest.c_str() + second + 1, nullptr, 10));
+ rest = rest.substr(0, second);
+ }
+ press.buttons = 0;
+ std::size_t start = 0;
+ while (start <= rest.size()) {
+ const auto plus = rest.find('+', start);
+ const std::string name = rest.substr(start, plus == std::string::npos ? std::string::npos : plus - start);
+ int bit = -1;
+ for (int i = 0; i < 16; ++i)
+ if (name == kNames[i]) bit = i;
+ if (bit < 0) return false;
+ press.buttons |= static_cast<std::uint16_t>(1u << bit);
+ if (plus == std::string::npos) break;
+ start = plus + 1;
+ }
+ return true;
+}
+
int usage() {
std::fprintf(stderr,
"usage: ps2run <elf> [--arg text]... [--frames n] [--ntsc] [--misses file]\n"
" [--disc image.iso] [--trace-syscalls] [--trace-io] [--trace-threads]\n"
- " [--trace-iop] [--trace-iop-hle] [--frames-dir dir] [--frame-every n]\n");
+ " [--trace-iop] [--trace-iop-hle] [--frames-dir dir] [--frame-every n]\n"
+ " [--press frame:button[+button][:frames]]...\n");
return 2;
}
@@ -36,6 +66,11 @@ int main(int argc, char **argv) {
else if (a == "--trace-iop") ps2::g_options.trace_iop_modules = true;
else if (a == "--trace-iop-hle") ps2::g_options.trace_iop_hle = true;
else if (a == "--frames-dir" && i + 1 < argc) ps2::g_options.frames_dir = argv[++i];
+ else if (a == "--press" && i + 1 < argc) {
+ ps2::Press press;
+ if (!parse_press(argv[++i], press)) return usage();
+ ps2::g_options.presses.push_back(press);
+ }
else if (a == "--frame-every" && i + 1 < argc) ps2::g_options.frame_every = static_cast<std::uint32_t>(std::strtoul(argv[++i], nullptr, 10));
else return usage();
}
diff --git a/runtime/include/ps2/system.hpp b/runtime/include/ps2/system.hpp
index e855191..f001a4c 100644
--- a/runtime/include/ps2/system.hpp
+++ b/runtime/include/ps2/system.hpp
@@ -15,6 +15,12 @@ inline constexpr std::uint32_t kIdle = 0x00001010u;
extern Cpu g_cpu;
+struct Press {
+ std::uint64_t frame;
+ std::uint32_t frames;
+ std::uint16_t buttons;
+};
+
struct Options {
bool trace_syscalls = false;
bool trace_io = false;
@@ -27,6 +33,7 @@ struct Options {
bool trace_iop_hle = false;
std::string frames_dir;
std::uint32_t frame_every = 60;
+ std::vector<Press> presses;
};
extern Options g_options;
diff --git a/runtime/src/iop/sio2.cpp b/runtime/src/iop/sio2.cpp
index 729ab1d..dac7e9f 100644
--- a/runtime/src/iop/sio2.cpp
+++ b/runtime/src/iop/sio2.cpp
@@ -53,8 +53,8 @@ void controller(int port, std::size_t at, std::uint32_t out_size) {
put(2, 0x5A);
const std::uint16_t raw = static_cast<std::uint16_t>(~p.buttons);
auto data = [&]() {
- put(3, static_cast<std::uint8_t>(raw >> 8));
- put(4, static_cast<std::uint8_t>(raw));
+ put(3, static_cast<std::uint8_t>(raw));
+ put(4, static_cast<std::uint8_t>(raw >> 8));
if (p.mode != kDigital) {
put(5, p.axes[0]);
put(6, p.axes[1]);
@@ -62,7 +62,7 @@ void controller(int port, std::size_t at, std::uint32_t out_size) {
put(8, p.axes[3]);
}
if (p.mode == kPressure) {
- static const int kBits[12] = {5 + 8, 7 + 8, 4 + 8, 6 + 8, 4, 5, 6, 7, 2, 3, 0, 1};
+ static const int kBits[12] = {5, 7, 4, 6, 12, 13, 14, 15, 10, 11, 8, 9};
for (int i = 0; i < 12; ++i) put(9 + static_cast<std::size_t>(i), (p.buttons >> kBits[i]) & 1u ? 0xFF : 0x00);
}
};
diff --git a/runtime/src/system.cpp b/runtime/src/system.cpp
index 7699460..6919981 100644
--- a/runtime/src/system.cpp
+++ b/runtime/src/system.cpp
@@ -5,6 +5,7 @@
#include "hw.hpp"
#include "iop/iop.hpp"
#include "iop/sif.hpp"
+#include "iop/sio2.hpp"
#include "ps2/disc.hpp"
#include "kernel.hpp"
#include "ps2/generated.hpp"
@@ -84,8 +85,17 @@ void dump_frame() {
std::fclose(f);
}
+void apply_presses() {
+ if (g_options.presses.empty()) return;
+ std::uint16_t mask = 0;
+ for (const Press &p : g_options.presses)
+ if (g_frames >= p.frame && g_frames < p.frame + p.frames) mask |= p.buttons;
+ iop::sio2::set_buttons(0, mask);
+}
+
void vblank() {
++g_frames;
+ apply_presses();
if (g_options.trace_iop_modules && g_frames % 25 == 0) std::fprintf(stderr, "[frame] %llu prims %llu\n", static_cast<unsigned long long>(g_frames), static_cast<unsigned long long>(gs::primitives_drawn()));
if (!g_options.frames_dir.empty() && g_options.frame_every && g_frames % g_options.frame_every == 0) dump_frame();
hw::vblank_start();