HN
Today

Show HN: PS2 games statically recompiled to WebAssembly

A brilliant technical feat brings PlayStation 2 games to your browser via static recompilation, not emulation. This project translates PS2 machine code directly to WebAssembly, complete with reimplemented OS and hardware. It's a deep dive into low-level systems, appealing to HN's love for innovative engineering and pushing web tech limits.

15
Score
0
Comments
#5
Highest Rank
13h
on Front Page
First Seen
Oct 12, 9:00 AM
Last Seen
Oct 12, 9:00 PM
Rank Over Time
179971113115616161719

The Lowdown

This ambitious project demonstrates running PlayStation 2 games directly in a web browser without traditional emulation. Instead, it statically recompiles the PS2's Emotion Engine machine code into C++, which is then compiled to WebAssembly, linking against a reimplementation of the PS2's operating system and hardware that leverages WebGPU.

  • Core Recompilation: The eerecomp tool reads a game's executable, follows its control flow, and generates C++ code for functions, preserving the R5900's specific quirks like 128-bit registers, multimedia instructions, and unusual floating-point behavior. This avoids a stack per guest thread by using tail calls and host stack unwinding for thread switches.
  • PS2 System Reimplementation: Key PS2 components are reimplemented: the Emotion Engine's operating system (threads, semaphores, interrupt handlers) via high-level emulation, and the I/O processor (IOP) by interpreting its R3000 CPU and reimplementing its kernel and libraries in C++. Disk access is handled via HTTP Range requests.
  • Graphics and Vector Units: PS2's demanding vector units (VU1) are handled by compiling microprograms to SIMD C++ ahead-of-time or on-the-fly to WebAssembly, modeling bit-exact math and pipeline delays. Graphics are rendered using WebGPU compute shaders, which process triangles directly in the GPU with native GS memory layouts, enabling accurate emulation of effects like rendering to textures or palette reinterpretation.
  • Performance Optimization: Significant performance gains were achieved by migrating the Graphics Synthesizer to WebGPU compute shaders and compiling vector unit programs. Optimizations included reducing compute dispatches per frame, implementing a 16MB GPU texture cache, refining shader setup, and leveraging MessageChannel and Web Workers to stabilize frame rates in the browser.
  • Porting Games: The project successfully ported Gran Turismo 4 (a highly demanding title) and God of War II, which helped uncover and fix recompilation bugs, timing issues, and missing IOP features. A port.sh script automates the process, using static analysis and multiple headless runs to identify and translate all reachable game code and microprograms.
  • User Experience & Legal: The browser interface supports keyboard and gamepad inputs, on-screen controls for mobile, resolution scaling, and memory card saving (exportable/importable). It requires WebGPU and a cross-origin isolated environment for SharedArrayBuffer. Crucially, the project only contains original code; generated C++ and WebAssembly are translations of the user's game executable and should remain private to the game owner.

This project represents a truly impressive technical achievement, showcasing how deep understanding of both legacy hardware and modern web technologies can yield groundbreaking results. While only two games are fully supported, the methodology provides a clear path for future development and porting efforts, pushing the boundaries of what's possible in a web browser.