PS2 games in the browser: static recompilation to WebAssembly with a native runtime
C++
port.sh: --native-only builds the native runner without Emscripten
Samir Nuri1b4808793a12
diff --git a/README.md b/README.mdindex e72df02..502cf97 100644--- a/README.md+++ b/README.md@@ -50,7 +50,7 @@ Along the way it also needed a boot path like the BIOS gives, a module loaded fr## Port a game of your own-You need git, CMake, Ninja, a C++20 compiler (clang is preferred and picked up automatically), Python 3, and 7-Zip if your disc image is in a `.7z`. Everything has been run on Linux on x86-64. The first run installs the Emscripten SDK into `tools/emsdk`, or uses the one `EMSDK` points to, and downloads wgpu-native for the headless test runner. Expect about 300 MB of disk space per game for the generated code and the builds; the disc image itself is linked, not copied.+You need git, CMake, Ninja, a C++20 compiler (clang is preferred and picked up automatically), Python 3, and 7-Zip if your disc image is in a `.7z`. Everything has been run on Linux on x86-64. The first run installs the Emscripten SDK into `tools/emsdk`, or uses the one `EMSDK` points to, and downloads wgpu-native for the native runner. With `--native-only` the script skips the browser build and never installs the SDK; the native window then needs the SDL2 development files. Expect about 300 MB of disk space per game for the generated code and the builds; the disc image itself is linked, not copied.```bashgit clone https://github.com/snuri00/ps2-web-recomp.git@@ -59,7 +59,7 @@ scripts/port.sh mygame "My Game.iso"python3 scripts/serve.py build/web-mygame games/mygame/disc.iso # http://localhost:8620/```-`port.sh` takes a disc image (an ISO, or a ZIP or 7z archive holding one) and does the whole job. It reads the boot program named in the disc's `SYSTEM.CNF`, translates its machine code to C++, and then runs the game headless a few times. Static analysis cannot see every function a game reaches through pointers, so each run writes down the addresses it could not find, the script adds them as entry points and translates again, until a run gets through without new ones. The same runs record the vector unit microprograms the game uploads, which are compiled ahead of time as well. Last it builds the page into `build/web-mygame`. God of War II goes from ISO to page in three and a half minutes on an 8-core laptop.+`port.sh` takes a disc image (an ISO, or a ZIP or 7z archive holding one) and does the whole job. It reads the boot program named in the disc's `SYSTEM.CNF`, translates its machine code to C++, and then runs the game headless a few times. Static analysis cannot see every function a game reaches through pointers, so each run writes down the addresses it could not find, the script adds them as entry points and translates again, until a run gets through without new ones. The same runs record the vector unit microprograms the game uploads, which are compiled ahead of time as well. Last it builds the page into `build/web-mygame` and the native runner into `build/native-mygame`. God of War II goes from ISO to page in three and a half minutes on an 8-core laptop.The exploring runs press start and then cross every five seconds, which gets most games through their title screens, and put an empty memory card in slot 1 the way the page does, so they follow the path a player takes. `--press 900:cross` replaces that with your own inputs and `--frames` sets how long each run lasts. Frames from the last run land in `games/mygame/shots`, which is the quickest way to see how far a new game gets. `scripts/build_web.sh mygame` rebuilds only the page.@@ -73,7 +73,7 @@ The keyboard maps the face buttons to Z (cross), X (circle), A (square) and S (t## Playing natively-The runner `port.sh` builds for its exploring runs, `build/native-mygame/ps2run`, also plays the game in a window when SDL2 is installed. It draws through wgpu-native on Vulkan and plays sound through SDL. The keyboard uses the page's keys for the buttons, with the arrow keys as both the D-pad and the left stick, and any game controller SDL recognizes works too, the Steam Deck's own controls included, with the same trigger pressure and vibration as in the browser.+The native runner, `build/native-mygame/ps2run`, plays the game in a window when SDL2 is installed; `scripts/port.sh mygame "My Game.iso" --native-only` builds only this one. It draws through wgpu-native on Vulkan and plays sound through SDL. The keyboard uses the page's keys for the buttons, with the arrow keys as both the D-pad and the left stick, and any game controller SDL recognizes works too, the Steam Deck's own controls included, with the same trigger pressure and vibration as in the browser.```bashbuild/native-mygame/ps2run games/mygame/disc/SLUS_123.45 --disc games/mygame/disc.iso --window --memcard mygame.ps2diff --git a/scripts/port.sh b/scripts/port.shindex ad472bd..19920eb 100755--- a/scripts/port.sh+++ b/scripts/port.sh@@ -7,11 +7,11 @@ usage() {cat <<'EOF'usage: scripts/port.sh <name> <game.iso | game.zip | game.7z> [options]-Turns a PS2 disc image you own into a browser build in build/web-<name>. It-reads the boot program named in SYSTEM.CNF, translates its machine code to-C++, runs it headless to find code the static pass missed (adding those entry-points and translating again), compiles the vector unit programs those runs-recorded and builds the page.+Turns a PS2 disc image you own into a browser build in build/web-<name> and a+native one in build/native-<name>. It reads the boot program named in+SYSTEM.CNF, translates its machine code to C++, runs it headless to find code+the static pass missed (adding those entry points and translating again),+compiles the vector unit programs those runs recorded and builds both.options:--frames N frames per exploring run (default 3600)@@ -19,18 +19,20 @@ options:presses start at frame 300 and cross every 300 frames--rounds N exploring runs at most (default 8)--no-explore translate and build without running the game+ --native-only skip the browser build and the Emscripten SDK it needsEOF}[ $# -ge 2 ] || { usage; exit 2; }name="$1"; source_path="$2"; shift 2-frames=3600; rounds=8; explore=1; presses=()+frames=3600; rounds=8; explore=1; web=1; presses=()while [ $# -gt 0 ]; docase "$1" in--frames) frames="$2"; shift ;;--press) presses+=(--press "$2"); shift ;;--rounds) rounds="$2"; shift ;;--no-explore) explore=0 ;;+ --native-only) web=0 ;;-h|--help) usage; exit 0 ;;*) echo "unknown option $1" >&2; exit 2 ;;esac@@ -46,7 +48,9 @@ generated="$ROOT/generated/$name"native="$ROOT/build/native-$name"tools="$ROOT/build/tools"export EMSDK="${EMSDK:-$ROOT/tools/emsdk}"-if [ ! -f "$EMSDK/emsdk_env.sh" ] || [ ! -x "$tools/eerecomp" ] || [ ! -x "$tools/vurecomp" ]; then "$here/setup.sh"; fi+setup=()+[ "$web" = 1 ] || setup=(--native-only)+if { [ "$web" = 1 ] && [ ! -f "$EMSDK/emsdk_env.sh" ]; } || [ ! -x "$tools/eerecomp" ] || [ ! -x "$tools/vurecomp" ]; then "$here/setup.sh" "${setup[@]}"; fimkdir -p "$game/disc" "$game/vu1" "$game/shots" "$generated"echo "== [1/6] disc"@@ -100,15 +104,19 @@ echo "== [3/6] machine code to C++"touch "$game/entries.txt"translate-echo "== [4/6] exploring"-if [ "$explore" = 1 ]; then+build_native() {if [ ! -f "$native/build.ninja" ]; thencompilers=()if command -v clang++ > /dev/null; then compilers=(-DCMAKE_CXX_COMPILER=clang++); ficmake -S "$ROOT" -B "$native" -G Ninja -DCMAKE_BUILD_TYPE=Release "${compilers[@]}" -DPS2_GENERATED="$generated" > /dev/nullfi+ ninja -C "$native" ps2run > "$native/ninja.log" 2>&1 || { tail -30 "$native/ninja.log"; exit 1; }+}++echo "== [4/6] exploring"+if [ "$explore" = 1 ]; thenfor ((round = 1; round <= rounds; round++)); do- ninja -C "$native" ps2run > "$native/ninja.log" 2>&1 || { tail -30 "$native/ninja.log"; exit 1; }+ build_nativerm -f "$game/misses.txt" "$game/explore.ps2"PS2_GPU_LOW_POWER=1 PS2_VU_RECORD="$game/vu1" "$native/ps2run" "$elf" --disc "$iso" --misses "$game/misses.txt" --memcard "$game/explore.ps2" \--frames "$frames" "${presses[@]}" --frames-dir "$game/shots" --frame-every 300 > "$game/explore.log" 2>&1 || true@@ -141,17 +149,17 @@ echo "== [5/6] vector unit programs"if compgen -G "$game/vu1/*.bin" > /dev/null; then"$tools/vurecomp" "$game/vu1" "$generated" | tail -1else- echo "none recorded, the page compiles them on the fly"-fi-if [ -f "$native/build.ninja" ]; then- ninja -C "$native" ps2run > "$native/ninja.log" 2>&1 || { tail -30 "$native/ninja.log"; exit 1; }+ echo "none recorded, the native runner interprets them and the page compiles them on the fly"fi-echo "== [6/6] web build"-"$here/build_web.sh" "$name" "$EMSDK"+echo "== [6/6] builds"+build_native+if [ "$web" = 1 ]; then+ "$here/build_web.sh" "$name" "$EMSDK"+fiecho-printf 'Done. Serve it with: python3 scripts/serve.py %q %q\n' "build/web-$name" "games/$name/disc.iso"-echo "and open http://localhost:8620/"-if [ -x "$native/ps2run" ]; then- printf 'or play it natively: %q %q --disc %q --window\n' "build/native-$name/ps2run" "${elf#$ROOT/}" "games/$name/disc.iso"+if [ "$web" = 1 ]; then+ printf 'Done. Serve it with: python3 scripts/serve.py %q %q\n' "build/web-$name" "games/$name/disc.iso"+ echo "and open http://localhost:8620/"fi+printf 'Play it natively: %q %q --disc %q --window\n' "build/native-$name/ps2run" "${elf#$ROOT/}" "games/$name/disc.iso"diff --git a/scripts/setup.sh b/scripts/setup.shindex 695da0d..f467308 100755--- a/scripts/setup.sh+++ b/scripts/setup.sh@@ -3,8 +3,10 @@ set -euo pipefailROOT="$(cd "$(dirname "$0")/.." && pwd)"EMSDK="${EMSDK:-$ROOT/tools/emsdk}"EMSDK_VERSION="6.0.11"+web=1+[ "${1:-}" = "--native-only" ] && web=0-if [ ! -f "$EMSDK/emsdk_env.sh" ]; then+if [ "$web" = 1 ] && [ ! -f "$EMSDK/emsdk_env.sh" ]; thenecho "== installing the Emscripten SDK $EMSDK_VERSION into $EMSDK"mkdir -p "$(dirname "$EMSDK")"git clone https://github.com/emscripten-core/emsdk.git "$EMSDK"@@ -13,7 +15,7 @@ if [ ! -f "$EMSDK/emsdk_env.sh" ]; thenfiif [ ! -f "$ROOT/third_party/wgpu-native/lib/libwgpu_native.a" ]; then- echo "== fetching wgpu-native for the native test runner"+ echo "== fetching wgpu-native for the native runner""$ROOT/scripts/fetch_wgpu.sh"fi