The game was compiled with Sony's PSY-Q: CCPSX runs cpp, cc1 (GCC modified for the
PS1; the game used two versions of it, 2.6.3 and 2.7.2) and ASPSX (the assembler). This
repository uses equivalents that run on Windows:
cpp (GCC 2.6.3) -> cc1 (GCC 2.6.3 or 2.7.2, mips-sony-psx) -> maspsx -> GNU as (mipsel-none-elf)
| Step | Tool | Where it comes from |
|---|---|---|
| Preprocessing, compiling | cpp.exe, cc1.exe (GCC 2.6.3 and 2.7.2) |
Built by tools/gcc-win32/build.sh |
| Standing in for ASPSX | maspsx commit 4ffa8b5 + tools/maspsx-extern.patch |
git clone, git apply |
| Assembling, linking | GNU binutils 2.40 (mipsel-none-elf-*) |
PSn00bSDK v0.24, see the README |
tools/psyq.py chains these steps; tools/build.py uses it for every src/**/*.c.
sh tools/gcc-win32/build.sh D:/FF7/toolchain 2.6.3
sh tools/gcc-win32/build.sh D:/FF7/toolchain 2.7.2The script takes the official GCC sources (ftp.gnu.org/old-gnu/gcc/gcc-2.6.3.tar.bz2,
SHA-256 71f67384…3c140; gcc-2.7.2.tar.gz, SHA-256 7cd8bce5…c5da5) and the PS1
patches of decompals/old-gcc (commit cf70ec5,
following their gcc-<version>-psx.Dockerfile), and builds cc1 and cpp with clang as
32-bit Windows programs (i686-w64-mingw32, part of llvm-mingw). 32 bits are needed:
these GCCs take a host integer (HOST_WIDE_INT) to be wide enough for a pointer, as it
was on the machines PSY-Q ran on. tools/gcc-win32/win32-compat.c and
tools/gcc-win32/include/ supply the BSD string functions and getrusage Windows lacks.
The results pass old-gcc's tests; two builds give identical output.
cc1 needs TMPDIR set to a writable folder (psyq.py sets it).
ASPSX reaches a small extern variable (.extern x, 4, size ≤ -G) through $gp; the
original maspsx ignores .extern lines. tools/maspsx-extern.patch records those
variables (only to choose how to reach them, never to define them). It also adds
--nop-at-expansion: a nop between a load and an instruction expanded through $at
that uses the loaded register, which maspsx otherwise does only for ASPSX below 2.30
but the game's text2.c code has with a newer one. And for ASPSX below 2.30 it waits
after a load when the next instruction only writes the loaded register (lw v0,0(v0); nop; lui v0,..., a dummy read of a port in the libraries), which maspsx did only for a
load followed by another load.
git -C D:/FF7/toolchain/maspsx apply "$PWD/tools/maspsx-extern.patch"Each executable is split into C files by how each part was compiled
(tools/psyq.py, tables FILE_OPTIONS and OVERLAY_OPTIONS):
| File | Range | Compiler | cc1 |
maspsx |
|---|---|---|---|---|
src/main/text.c |
0x800110B8–0x800137FF, game code | GCC 2.6.3 | -O2 -G8 |
-G8, ASPSX 2.21 |
src/main/text1b.c |
0x80013800–0x80013C9B, game code | GCC 2.6.3 | -O2 -G8 |
-G8, ASPSX 2.21 |
src/main/text2.c |
0x80013C9C–0x80016F8F, game code (kernel strings...) | GCC 2.7.2 | -O2 -G8 |
-G8, ASPSX 2.34, --nop-at-expansion |
src/main/text3.c |
0x80016F90–0x8001AEE3, game code | GCC 2.6.3 | -O2 -G8 -ffixed-25 |
-G8, ASPSX 2.21 |
src/main/text3b.c |
0x8001AEE4–0x8001BDAF, game code | GCC 2.6.3 | -O2 -G8 |
-G8, ASPSX 2.21 |
src/main/text4.c |
0x8001BDB0–0x800269FF, game code | GCC 2.7.2 | -O2 -G8 |
-G8, ASPSX 2.21 |
src/main/text5.c |
0x80026A00–0x80029A4F, game code | GCC 2.7.2 | -O2 -G8 |
-G8, ASPSX 2.21 |
src/main/akao.c |
0x80029A50–0x80036297, AKAO sound driver, CD/movie code | GCC 2.6.3 | -O2 -G0 |
-G0, ASPSX 2.21 |
src/main/lib.c |
0x80036298–0x8003DDBB, PSY-Q libraries (libapi, libetc...) | GCC 2.6.3 | -O2 -G0 |
-G0, ASPSX 2.34 |
src/main/lib2.c |
0x8003DDBC–0x800404F3, libcd (from bios.c) |
GCC 2.6.3 | -O2 -G0 |
-G0, ASPSX 2.21 |
src/main/lib3.c |
0x800404F4–0x8004161F, PSY-Q libraries | GCC 2.6.3 | -O2 -G0 |
-G0, ASPSX 2.21 |
src/main/lib3a.c |
0x80041620–0x80042167, PSY-Q libraries (CdRead) | GCC 2.6.3 | -O2 -G0 |
-G0, ASPSX 2.34 |
src/main/lib3b.c |
0x80042168–0x80042C5F, PSY-Q libraries | GCC 2.6.3 | -O2 -G0 |
-G0, ASPSX 2.21 |
src/main/lib3c.c |
0x80042C60–0x80043937, PSY-Q libraries | GCC 2.6.3 | -O2 -G0 |
-G0, ASPSX 2.34 |
src/main/lib4.c |
0x80043938–0x8004602B, libgpu (from sys.c) |
GCC 2.6.3 | -O2 -G0 |
-G0, ASPSX 2.21 |
src/main/lib5.c |
0x8004602C–0x80048D17, libgpu and the rest | GCC 2.6.3 | -O2 -G0 |
-G0, ASPSX 2.21 |
src/battle/text.c |
BATTLE.X up to 0x800B30E4 | GCC 2.7.2 | -O2 -G0 |
-G0, ASPSX 2.34 |
src/battle/text2.c |
BATTLE.X, 0x800B30E4–0x800E5F2F | GCC 2.6.3 | -O2 -G0 |
-G0, ASPSX 2.21 |
src/battle/text3.c |
BATTLE.X from 0x800E5F30 | GCC 2.7.2 | -O2 -G0 |
-G0, ASPSX 2.21 |
src/field/text.c … text5.c |
FIELD.BIN, cut where the game's files began | GCC 2.6.3 | -O2 -G0 |
-G0, ASPSX 2.21 |
src/*menu/text.c |
the 13 menus (MENU/*.MNU) | GCC 2.7.2 | -O2 -G0 |
-G0, ASPSX 2.21 |
src/<overlay>/*.c |
every other overlay | GCC 2.6.3 | -O2 -G0 |
-G0, ASPSX 2.21 |
The game's code was many source files; our files are groups of them, cut where the way they were built changes (or, in FIELD.BIN, where the jump tables show a new file). How the main executable's options were settled:
- ASPSX below 2.30 for most of the game's code: a global plus a register is
lui $at; addiu $at, %lo(x); addu $at, $reg; lw 0($at)(maspsx's form below 2.30), and before an instruction expanded through$atthat uses the register just loaded there is an extranop. 2.21 is one value below 2.30. text2.c, a newer ASPSX: there%lois folded into the load (lw $v0, %lo(x)($at)), as from 2.30 on, andliof an unsigned 16-bit constant is stillori, as up to 2.56; it also has thatnop, which maspsx gives a newer version only with--nop-at-expansion(from its patch). 2.34 is one value in between.- GCC 2.7.2 for
text2.candtext4.c: there a function can endlw $ra; li $v0, 1; jr $ra; addiu $sp(2.6.3 never fills a return's delay slot so), and some registers are allocated as 2.7.2 does (mflo $v1where 2.6.3 picks$v0). Elsewhere functions match only with 2.6.3. The boundaries are where those functions change sides, and where the same variable starts being reached through$gpor withlui(its declaration changed, so the source file did). -G8only for the game's code: no function from 0x80029A50 on uses$gp, not even for 1–4 byte variables right next to it, and neither do the overlays ($gppoints into the main executable's data).- Externs reached with
lui/%lo: when the game reaches a small variable that way (too far from$gp, or declared bigger than-Gin its source: part of an array or a struct),psyq.pydrops the size from its.externline so maspsx does not put it on$gp. Which variables near$gpare reached only that way is read from the game's own code. - AKAO: Square probably built the driver apart, with older tools (ASPSX below 2.30).
- PSY-Q libraries: some of
lib.cwas built with other options (for example without filling a return's delay slot), and the GTE (cop2) and BIOS-call functions were assembly to begin with. Two ASPSX versions show in the libraries: libcd and libgpu have the unfoldedlui; addiu; addu; lw 0($at)of a global plus a register (2.21), the others the folded%lo(x)($at)(2.34).lib2.candlib4.chold the 2.21 ranges as far as the forms show them (the$Id:strings ofbios.candsys.cmark where those libraries' files are), andlib3.cis 2.21 too: none of its functions has a folded form, and func_80040CA8 matches only with 2.21's nop after a global load whose register a store reuses next. The CdRead functions after it (lib3a.c, from 0x80041620) are 2.34: func_800418D8 comes within two lines only without that nop, and every other C function of the two files matches under both (work/optsweep/optsweep.py compiles a draft under each GCC build and ASPSX version; work/split3/optcheck.py lists the C functions of a file that another option breaks). The cuts may move once more of the functions around them are in C.
INCLUDE_ASM uses splat's maspsx_hack style: GCC 2.6.3 puts top-level asm ahead of
the C functions around it, so each INCLUDE_ASM is wrapped in a dummy function that
maspsx unwraps, keeping the order. INCLUDE_RODATA (include/common.h) does the same
for rodata.
Each C file has its own part of .rodata (config: [0x800, .rodata, text], ...), and
splat moves every function's jump tables and strings into its asm file, so a function
written in C brings its own: GCC's .rdata is renamed .rodata. GCC aligns a jump table
on 8 bytes from the start of its file's rodata, and the link packs each file's rodata on
2 bytes (SUBALIGN(2)), so that alignment is relative to the file's start, as it was in
the game. Where one of our files holds several of the game's, its start is not theirs:
its jump tables are aligned on 4 bytes instead (where the game really has padding,
match.py reports a rodata difference), unless the file starts on 8 bytes or is marked
unit_start (it begins where one of the game's files began). The game's files show in
the jump tables: those of one file are all on 8 bytes, or all 4 bytes past them, so a
change marks a new file (that is how FIELD.BIN's were found). A function checked alone
(work.py, the permuter) has its rodata start where the function's own does, which in
such a file may be 4 bytes past the file's 8: match.rodata_pad puts that many bytes
before its object's rodata, so its tables are aligned as in the whole file.
Strings a function uses are string literals in its C. A function may not define tables
of its own (static or global data): GCC puts static data at the end of its file, which
in our larger files is not where the game has it, so the game's tables stay in asm and
the C declares them extern (tools/work.py put refuses such data, keeps a table the C
reads by name in an asm file of its own with an INCLUDE_RODATA, and checks the whole
file). put writes that INCLUDE_RODATA before the function. When the function's own
jump table comes first in the game's rodata, that order is wrong, because GCC emits the
jump table where the function is. Then write the INCLUDE_RODATA after the function by
hand, and keep the table out of migration in config/symbols.us.<target>.txt
(force_not_migration:True) so a split writes it as an asm file of its own (tifa
func_801BCE2C).
C code may name any address as D_80012345 or func_80012345; build.py gives each such
name its address (build/us/<target>.addresses.ld), whether or not splat named it. Any
other name ending in _ and eight hex digits counts too, so one address can have several
names: GCC merges identical calls to one symbol but not to two, and the game's calls to
the entry points of different overlays loaded at the same address stay apart only as
ovl1_801B0040, ovl2_801B0040 ... (battle func_800C679C). A line in
config/symbols.us.<target>.txt does not reach the linker for such names.
Finding where a game's file begins from its jump tables: list the tables of the file's
rodata with their offsets from the rodata start (grep dlabel jtbl_ in the asm folder).
A table not 8-aligned from the current start marks a new file there, and each table must
be 8-aligned from the start of its own file (a 4-byte pad before one that is not). When a
function in C stops fitting because its table needs such a pad that the 4-byte alignment
of a shared file does not give (main func_800230C4: the game pads 4 bytes after the
0x14-byte table of func_80022B5C), split the file before the first function of the game's
file that holds the table, at any function boundary after the previous file's last table:
tools/split_file.py main text4 text4b 8001FCDC 80010314 (rodata at the first table of that
file), give the new file unit_start in tools/psyq.py and run tools/build.py main --split.
When several processes may be putting, run the split under the work lock
(auto_decomp.Turn()).
Re-running splat on a target after puts (tools/build.py <target> --split, needed for
alias symbols) can migrate a table into the function .s that references it once only one
function reads it: a jump table the C already emits (battle D_800A0240, read by one asm
function) then exists twice, the file grows by its size and the sha1 differs at the first
table after it. Pin such a table with NAME = 0xADDR; // force_not_migration:True in
config/symbols.us.<target>.txt and split again; the data/*.rodata.s files splat writes
are not linked (only data.data.o is), so duplicates there are harmless.
tools/match.py FILE.ccompiles a file the waybuild.pydoes and compares every word of each function with the game's, after filling in addresses as the linker would (from the built ELF, so runbuild.pyfirst), and the object's rodata with the game's. From Python,match.check(file, like='src/main/akao.c')compiles with the options of the named file.tools/auto_decomp.py --target main --max 100has m2c draft each function still in asm (smallest first) and keeps the ones that match: the draft, the draft with struct members (M2C_SFIELD) and the draft with neighbouring statements swapped are tried. The function replaces itsINCLUDE_ASMand its declarations, its own prototype among them, go intosrc/<target>/decls.h. When a new prototype replaces a guess m2c made earlier for a caller, every function in C is checked again, and the new one is taken out if a caller stops matching. The rest are listed with the reason inbuild/us/auto_decomp.txt.tools/work.py show NAME,diff FILE NAME,put FILE NAME: the same by hand, from m2c's draft to a function in place (see its help). A work file's own declarations replace decls.h's for the same names.tools/permute.py WORK.c NAMEhands a near miss to decomp-permuter (clone it next to the toolchain,pip install pycparser tomlin the venv): base.c is the work file expanded, and each candidate is compiled as the function's file is built and linked at its game address (tools/permute_cc.py), so only real differences count. A match is written next to the work file forwork.py put;tools/permute_all.py LISTruns a list of them one after the other.tools/split_file.py TARGET FILE NEW ADDRESS [RODATA]splits a C file where the game's source files change (a different compiler, or a variable declared otherwise), andtools/find_units.pylists the rare functions followed by a file's paddingnop. Such a function cannot be put as splat cut it (10 instructions where C gives 9, and the linker does not add the padding): split the file where the next one starts, give the paddingnopa symbol of its own inconfig/symbols.us.TARGET.txt(func_ADDRESS = ADDRESS; // type:func), write the function in C and putINCLUDE_ASM(.., func_ADDRESS);right after it, and runtools/build.py TARGET --split(battle func_800D3520 and func_800D3544). A split does not remove the old asm file of the function whose size changed: overwriteasm/us/TARGET/nonmatchings/FILE/func_X.swith the one splat wrote inasm/us/TARGET/matchings/FILE/, orputandmatch.pykeep expecting the old length for the whole file.tools/work.py put NAME --in-fileskips the check of the function alone and lets the whole-file check decide: GCC'slast_spill_regcarries over between the functions of one cc1 run, so the names of a function's reload registers can depend on the functions before it in its file (it did not place magic_brizag2 func_801B2548).- When done:
tools/build.pymust still match.
What GCC 2.6.3 needs to produce the game's code (learned from functions that match):
while (1)rather thanfor (;;); a value chosen withif/elseinto a temporary and stored once, rather than?:inside the store.- The scheduler may swap a load through a pointer to a struct with a store to a plain
variable at a fixed address (and the reverse), never two accesses of the same kind.
So when loads and stores are out of order, try a struct member
(
D_8009A104[1].unk28 = 0) instead of the field's own name (D_8009A18C = 0), or the other way round, or the statements in the other order. - A fixed address kept in a register comes from a pointer variable in the source
(
p = &x; *p |= ...); for other fields of a constant pointer, GCC goes back to the fixed address (lui/%lo). u16counters giveaddu n, n, 0xFFFF+andi; wheren--sits in the loop body decides the order constants are hoisted out of it.- Register choice follows the order variables are declared and how long they live; reusing a parameter as a variable changes it.
- A
switchtable spans the smallest to the largest case, so empty cases may be needed; case blocks are laid out in source order, and identical tails are merged.
m2c drafts C from asm:
python D:/FF7/toolchain/m2c/m2c.py asm/us/main/nonmatchings/<file>/<function>.s.