ps3kit
A game-agnostic toolkit for native PC ports of PS3 games: the executable's
structure, the Cell PPU, a static recompiler from PPU code to C++, and a
runtime that replaces the PS3's system software under the recompiled game.
The same idea as wiikit and
ps2kit,
for the PS3. Each PS3 game has its own engine and formats, but a large part
of every port is the same work: the same executable format, CPU, system
calls, libraries and GPU. ps3kit collects that shared part. It grows inside
the ports: each piece is written because a game needed it, then kept free
of that game's knowledge. Game formats and game fixes live in the ports.
It grew inside its first port, PC-ROA, and moved to its own repository, with
its history, when the second one started. Each port takes it back as a
submodule at ps3kit/.
Ports built on it
| Port |
Game |
What it asked of ps3kit |
| pc-ragnarokodysseyace |
Ragnarok Odyssey ACE (2013) |
everything so far: the descriptor table, the imports, the PPU recompiler, the process start, threads, files, the command buffer to its first flip, the audio ports and their clock, the window, the RSX's draws: the publishers' logos; save data; cellSail and an MPEG-2 decoder: the first movie; the controller: the title screen, the main menu and character creation; the SPU recompiler and a job manager: CRI's sound, heard; cellFont with an OpenType/CFF reader and a rasterizer of its own: character creation's text; system events and the on-screen keyboard: the hero's name |
| pc-3ddotgameheroes |
3D Dot Game Heroes (2009) |
the SPUs: SPU ELFs recompiled at their load address, lv2 SPU thread groups (FMOD's mixer), SPURS task sets, tasks and event flags on a kernel of the runtime's own, spu_printf; switch tables the compiler did not optimise; the I/O map, reports and inline image transfers (the fragment programs); audio notifications; /dev_flash and /dev_hdd1: the publishers' and the middleware's logos; trophies, read from the disc's set and kept; SPU modules recompiled where their loader puts them (FMOD's codec): the title screen, its demo play and FMOD's music; SPURS lock-free queues and queues (Edge zlib, Bullet), position-independent SPU code's jumps, vertex arrays kept while unwritten: the first field, played at 30 frames |
Using it
ps3kit does not decrypt: the input is the decrypted executable (a 64-bit
big-endian ELF), produced from the player's own EBOOT.BIN. The generated
C++ is the game's own code translated, so it belongs in a build directory
and never in a repository.
python -m ps3kit.elf GAME.elf --imports --opd # segments, parameters, imports by name
python -m ps3kit.ppu GAME.elf --at 0x10200 # read code
python -m ps3kit.ppu GAME.elf --mix # the instruction census
python -m ps3kit.spu GAME.elf --at 0xF3D260 --base 0xF3D200 # read SPU code, local-store addresses
python -m ps3kit.spu GAME.elf --mix 0xF3D200:0xFE20 # an SPU program's census
python -m ps3kit.recomp GAME.elf --out build/recomp
cmake -S build/recomp -B build/recomp-build -G Ninja -DCMAKE_CXX_COMPILER=clang++ \
-DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS_RELEASE=-O1
ninja -C build/recomp-build
build/recomp-build/ps3boot GAME.elf --bdvd DISC --hdd0 DIR # DISC holds PS3_GAME
ps3boot opens a window and runs the game behind it. --hdd1 DIR is the
root of the system caches (/dev_hdd1, hdd1 if not given) and --flash DIR is /dev_flash, read-only, for a game whose middleware reads the
system's own files there. --no-video runs the
game alone, --quit-after SECONDS ends the run, and --dump DIR writes
every 60th shown frame as a PNG (--dump-every N for another interval).
Debugging a run: ps3boot reports a host fault with the guest address it
touched (in a library, such as the C runtime's memcpy, with the library's
name and the executable's addresses found on the stack above it, which nm
names), and gdb_trace.py traces any recompiled function
under gdb (arguments, result, the guest call chain) with no debug build:
PS3KIT_TRACE_FUNCS=81BEE8 PS3KIT_TRACE_CHAIN=1 gdb -q -batch -x ps3kit/gdb_trace.py \
--args build/recomp-build/ps3boot GAME.elf --bdvd DISC
PS3KIT_TRACE=1 reports every import call (by default each one's first
three), PS3KIT_FS=1 every file opened, PS3KIT_RSX_TRACE=N every RSX
method of frame N with its argument, PS3KIT_DRAWDUMP=N draw N's vertices,
constants and state, PS3KIT_SHADERDUMP=1 every program pair as GLSL,
PS3KIT_TEXDUMP=DIR every texture as first decoded (PNG), and
PS3KIT_GLDEBUG=1 the OpenGL driver's complaints, PS3KIT_GLSYNC=N each
draw from draw N on, waited for (so that a draw which brings the driver down
is the last one reported), PS3KIT_SAVEDATA=1 each save
data step and callback result, PS3KIT_TROPHY=1 each trophy context and
status reported to the game, PS3KIT_SAIL=1 each movie player call and
event and every 30th frame shown.
PS3KIT_SPU=1 reports each SPU program's first job and how long its jobs
take, PS3KIT_SPU_DMA=1 every DMA transfer an SPU program makes (=2 with
its words), PS3KIT_SPU_TRACE=1 every indirect branch in SPU code, and
PS3KIT_SPU_TRACE=2 every instruction, once the code has been generated with
python -m ps3kit.recomp ... --spu-step. PS3KIT_AUDIODUMP=FILE.wav writes
what the audio output plays, and PS3KIT_AUDIO=1 how many notifications the
audio clock sent and dropped each second.
PS3KIT_LV2=1 reports every lv2 call that has an implementation, with its
arguments and result (the stubs are always reported). PS3KIT_SPURS=1
reports each SPURS task's start, system calls, events and exit, and the raw
block behind each spu_printf it makes.
PS3KIT_PROF=1 reports, every 30 flips, where the wall-clock time went:
the RSX thread idle, on semaphores, capturing draws, waiting for the vblank
or for the renderer; the renderer executing, presenting, idle, compiling
new programs and making new textures; SPURS jobs; with the draws, the bytes
hashed and sent, and the new programs and textures. A frame-rate drop then
names its own cause.
PS3KIT_OSK=1 reports each on-screen keyboard call, and
PS3KIT_OSK_TYPE="Name" answers every keyboard at once with that text,
confirmed.
PS3KIT_PAD_PRESS="30:cross,46:start,52:cross+up" presses the controller's
buttons for a fifth of a second at those times (seconds from the window's
first poll), so that a run nobody plays can still get past a screen.
PS3KIT_FONT=1 reports each font call and every glyph drawn, with where it
lands on the surface.
fonttest FONT SIZE TEXT OUT.pgm (ninja fonttest) lays a string out with
the font reader and the rasterizer alone, so that it can be set beside
another renderer's image of the same string.
mpeg2test MOVIE OUT.yuv [--offset BYTES] (ninja mpeg2test) runs the MPEG-2
decoder alone, on a program stream or an elementary stream, so that its
frames can be compared with another decoder's.
Building needs SDL3, CMake, Ninja and clang (the generated code's tail jumps are
[[clang::musttail]]; clang 22 from MSYS2 is what is used here). The Python
side has no dependencies.
How it works
Functions. A PS3 executable is stripped, but every function whose
address is ever taken has an 8-byte descriptor, {entry, toc}, in one table
that e_entry points into. Those entries, plus every bl target, are the
units; the code ends where the last unit reachable that way ends (the import
stubs). A branch out of a unit's body to a place that is not a unit start
makes an extra entry, as wiikit does.
Code. Each entry becomes void f_XXXXXXXX(PPUContext&). Registers live
in the context; memory is a 4 GiB host reservation indexed by guest address,
big-endian. A branch to another function is a guaranteed tail call, so a
guest loop that crosses a function boundary does not grow the host stack.
Switch tables (lwz of the table from the TOC, lwzx, extsw, add,
bctr) become C++ switches; every other indirect branch goes through a
sorted address table.
Imports. The loader's job, done the loader's way: each slot of the stub
table gets a descriptor whose entry is an address in the runtime's range.
A call through it is dispatched to the C++ function registered under the
import's name (PS3_HLE(name)), or to a stub that reports it. Names come
from nids.txt; a NID is computed from its name, never stored.
System calls. sc dispatches on r11 to LV2_SYSCALL(number, name).
SPU programs. Each one is translated whole into a single function,
compiled for the local-store address it will be loaded at: every
instruction is a label, a direct branch is a goto, and an indirect one (a
return, a computed jump) goes through a switch over every instruction. SPU
code has no call distinct from a branch and returns through registers, so
this needs no discovery of functions at all. A register keeps its bytes in
host order reversed as a whole, which makes the 128-bit value the same
number as on the SPU: element-wise operations run on host vector lanes and
quadword shifts are shifts of an unsigned __int128. SPURS job binaries are
found by the header their job runtime (JOBCRT) carries; they are
position-independent and relocate themselves.
Layers
| Layer |
Question it answers |
Now |
Next |
| 1. Recognise |
What is in this executable? |
elf: segments, process parameters, TLS, the descriptor table, imports by name; SPURS job binaries by their job-runtime header; SPU ELFs by their header and .note.spu_name (recomp.spu.find_elfs) |
task sets, the middleware it links |
| 2. Read code |
What does this function do? |
ppu: the PPU decoder, disassembly, census; spu: the same for the SPU |
cross-references through the TOC |
| 3. Recompile |
The code as C++ |
recomp: discovery, switch tables, the whole user-mode PPU set and the VMX operations games use; each SPU program whole, every SPU instruction but bisled and the double compares by magnitude: SPURS jobs, and SPU ELFs at their load address, dispatching only where an indirect branch can land; SPU modules another SPU program loads (FMOD's codecs and effects), at the bias a run shows (--spu-module-bias) |
|
| 4. Run |
The PS3 under the game |
runtime: memory map, dispatch, loader, imports, lv2 calls, the process start; PPU threads, lightweight and lv2 synchronisation, event queues; the file system (cellFs and sys_fs); cellGame; cellGcmSys and cellResc with a FIFO consumer, labels, reports, the I/O map, flips and a 59.94 Hz vblank; the renderer (SDL3, OpenGL 4.5): a window, render targets on the host GPU, clears, draws with vertex and fragment programs translated to GLSL, textures, blending, flips; video out; cellAudio and the audio output, with the 48 kHz clock that consumes the ports and plays them through SDL3; SPURS job queues with a job manager running recompiled SPU programs: the SPU's channels, its DMA and atomic unit, the lock-line reservation event; lv2 SPU images and thread groups, the threads' mailboxes, event ports and local stores; SPURS instances, task sets, tasks, exit codes, event flags and the PPU's side of lock-free queues, on a kernel of the runtime's own; save data (Auto, ListAuto, Fixed) with PARAM.SFO; trophies (sceNpTrophy) from the disc's TROPHY.TRP, the unlocks kept on /dev_hdd0; SPU modules run as overlays when their loader branches into them; cellSail playing PAMF through the runtime's own MPEG-2 decoder; cellPad from the host's gamepad and keyboard; system events raised through cellSysutilCheckCallback; cellOskDialog typed on the host's keyboard; cellFont over the runtime's own OpenType reader (CFF outlines, name- or CID-keyed) and coverage rasterizer |
the rest of the 3D engine's state (fog, clip planes, two-sided colour, point sprites), TrueType outlines and the system's font sets, the list save dialogs, SPURS queues and barriers, the SPU's side of lock-free queues |
Known approximations
Declared here as they are made, so that the list is the cost:
fres, frsqrte and the VMX estimates return exact values, not the
hardware's estimates; FPSCR exception bits are not kept, and
the saturating VMX operations never set VSCR[SAT].
- The word divides write zero into the undefined high word.
lwarx/stwcx. is a compare-and-swap against the value the reservation
saw: a store of the same value in between goes unnoticed.
- Guest threads are host threads and the host schedules them. Guest
priorities order nothing among themselves; a thread more urgent than the
primary thread runs at the host's above-normal priority. Every
synchronisation object shares one lock and one condition variable; waiters
race rather than queue.
sys_timer_usleep and the system's clocks sleep on a high-resolution
waitable timer on Windows, and the process opts out of the power
throttling that coarsens a background window's timers. With the ordinary
sleep, CRI's 60 Hz server woke up to 12 ms late a few times a second and
the audio it feeds fell a whole port behind.
- The RSX's 3D engine is the registers a draw reads so far: surfaces,
clears, viewport, scissor, blending, colour masks, depth, stencil,
culling, logic op, alpha test, vertex arrays, both programs, textures.
Fog, user clip planes, two-sided colour, flat shading, point sprites,
polygon offset and depth bounds are written and not acted on.
- Fragment programs keep their 16-bit H registers apart from the 32-bit R
registers instead of aliasing two H onto one R, and compute everything at
full precision; only the fixed-point and saturating clamps are applied.