# Functions this project implements natively, replacing the game's own.
#
# One hex address per line; `#` comments to end of line. Passed to the emitter
# as `--replace @host/replace.txt` by scripts/04-emit-build.sh.
#
# For each address listed here the emitter keeps the translated body but names
# it `psp_func_<addr>__orig`, and leaves the public `psp_func_<addr>` for
# host/replacements.c to define. Every call site in the emitted C -- direct
# `jal`s included, which no run-time hook can reach -- then binds to ours.
#
# ---------------------------------------------------------------------------
# Before adding an address, three things are worth knowing.
#
# 1. Say why. A bare address here is unreadable six months from now, and the
#    reason is the part that cannot be recovered from the binary.
#
# 2. Check for interior entries. Replacing a function does not replace jumps
#    *into* it: the emitter's `psp_at_<addr>` thunks enter the original body
#    directly. If the generated C has psp_at_ thunks inside the function's
#    extent, control can still reach the original past its prologue, and the
#    substitution is not as clean as it looks.
#
#        grep -n 'psp_at_' game/generated/aclr_funcs.c
#
# 3. The differential oracle will report it as a mismatch, and that is correct
#    rather than a bug. scripts/05-oracle.sh compares each psp_func_<addr>
#    against the interpreter running the original instructions; a replaced
#    function is *meant* to behave differently, so it differs by construction.
#    Skip replaced addresses there rather than reading the differ as a codegen
#    fault -- docs/findings/oracle.md is about emitter correctness, and a
#    deliberate replacement is not evidence either way.
# ---------------------------------------------------------------------------
#
# With no addresses listed, the emitter's output is byte-identical to a build
# without this file, so nothing downstream changes.

# The central camera rebuild reads the active render descriptor's dimensions,
# derives width/480, height/272 and width/height, then rebuilds both projection
# and culling state. PSPRECOMP_ASPECT=window temporarily substitutes a virtual
# width derived from the GL drawable while this original body runs, preserving
# the fixed 480x272 guest framebuffer but giving wider windows a horizontal-plus
# view. Unset, the replacement defers without touching guest state. There are
# no interior entry thunks.
000889B4   # camera projection/frustum -- see host/replacements.c

# The lock target's on-screen test: psp_func_001FBDF8(out) projects the lock
# target's position (0x4D4B40; 0x4D4B5C says whether one is held) with the
# render system's screen matrix, converts to pixels
# and returns 1 when the point lies within the render descriptor's rectangle
# (0,0,480,272). The mission loop keeps the lock marker only while it says
# yes, so on a wide window a target in the bands beside the centred 480
# columns lost its marker although the game draws the marker wherever the
# point is. The replacement widens the horizontal bounds by the band width.
# One interior thunk, the function's own return-0 label.
001FBDF8   # lock target on screen -- see host/replacements.c

# The AC's yaw integrator: psp_func_0004F248(ac, accel, max_rate). Called by
# the fourteen movement-state handlers (walk, boost, jump...), it turns the pad
# into a yaw rate at ac+8312 and adds it to the yaw at ac+36. The game's default
# control path reduces the stick to two button bits first -- pressed(2) left,
# pressed(3) right -- so it ramps at 4*accel per frame to the cap and never
# sees how far the stick moved. host/replacements.c uses that magnitude.
#
# Two things the call log (PSPRECOMP_INPUT_LOG) established: every AC in the
# mission runs this, the enemies every frame, so the replacement acts only on
# the player's object; and the player's handler calls it only while the
# thresholded turn bit is set, so lowering that threshold is the other half of
# the job. Interior thunks: 18, all ordinary branch targets. Findings:
# docs/findings/state.md, controls.
0004F248   # yaw integrator -- see host/replacements.c

# The physical PSP-button converter and the held/pressed action queries form
# the modern gamepad's semantic boundary.  SDL's triggers and stick clicks do
# not exist in SceCtrlData, so host/present.c carries all ten modern-pad
# controls in bits the game's twelve-entry converter ignores.  00279A10 strips
# those carrier bits, maps face buttons/bumpers back to PSP buttons outside
# gameplay, and records their edge state.  The two 0005EFxx helpers add the
# corresponding gameplay action for the player's pad only: LT boost, RT right
# arm, LB left arm/event, RB change weapon, L3 extension, R3 OB/EO, A inside,
# B view reset and Y purge modifier.  This reaches actions 12, 15 and 16,
# whose PSP key-assign masks are zero, and keeps menu input unchanged.  The
# first three replacements have no interior entry thunks.  0005F348 has three
# ordinary internal branch targets; every external caller enters at its start.
00279A10   # PSP buttons -> game buttons, plus modern-pad carrier capture
0005EFA0   # action held -- add semantic gamepad actions for the player
0005EFD4   # action pressed -- add semantic gamepad edges for the player
0005F348   # purge-modifier command -- expose it on Y in every saved layout

# The stick-to-buttons converter: psp_func_00279A50(state, ax, ay), from the
# pad adaptor psp_func_002797D4. The PSP stick, after a circular deadzone of
# radius 30, becomes four virtual bits -- 0x8000 right, 0x4000 left, 0x1000
# forward, 0x2000 back -- when an axis passes the threshold at 0x0042B784
# (100 of 127). Physical buttons occupy bits 0-11, so these four are the whole
# of what the game ever learns about the stick's direction, and the
# key-assign rows bind actions 0-3 to exactly them. The movement state machine
# keys on these bits, which is why the yaw integrator above is never even
# called below the threshold. Interior thunks: 5, all ordinary branch targets.
00279A50   # stick -> virtual buttons -- see host/replacements.c

# The look (pitch) integrator: psp_func_00053234(ac), once per frame from
# psp_func_0004CF74 for the AC under control, after the auto-face pass
# (psp_func_00053048). Its state is a struct at ac+8160 -- lockout counter at
# +4, pitch angle at +16 (ac+8176, radians, up positive), pitch rate at +32 --
# and its constants six floats at 0x0030ADC0: accel 0.00136, max rate 0.027,
# clamp +/-1.1781 (67.5 deg). The shipped path is digital: triangle
# (action 10) ramps the rate by 4*accel per frame to 2*max = 0.054 rad/frame,
# 3.09 deg/frame against yaw's 2.10, and release stops it dead. That is why
# mouse look-up/down felt both faster than left/right and uncontrollable: any
# travel was a full-rate press. host/replacements.c makes it proportional
# under `dual`. Interior thunks: 20, all ordinary branch targets.
00053234   # look (pitch) integrator -- see host/replacements.c

# The push: psp_func_0004F06C(ac, dir, accel, cap), the primitive every
# moving state uses -- walk, jump, boost. `dir` indexes an eight-entry table
# of unit vectors at 0x0030AEB0 (x LEFT, z back); the vector is scaled by
# `accel`, rotated by the AC's yaw through the VFPU helpers
# psp_func_002B007C/00255BBC into the scratch at 0x0030AF00, and
# psp_func_0004EE9C adds four times it to the velocity at ac+80/88 and clamps
# the speed to `cap`. The player's walk reaches it through
# psp_func_00054894 (from the handlers 000655D4/00065824/00065EB0/00066100),
# which passes the legs' own accel and cap, halved for the start, scaled for
# a back walk -- which is why the walk was two-state: the stick chose one of
# eight directions and the cap was always the full one. host/replacements.c
# gives the player the stick's true direction and a cap scaled by its
# magnitude, and leaves the game's accel, clamp and overspeed decay alone. No
# interior thunks.
0004F06C   # the push (walk/strafe/boost direction and speed) -- see host/replacements.c

# The model's animation updater: psp_func_001DFC80(model, layers), once a
# frame per model from psp_func_001DF1D0/001DF208 (and registered for
# indirect calls). Each of the model's slots (count at model+116, base at
# model+120, 128 bytes) holds an anim index (+73), a frame (+76) and frame
# count (+78), a loop byte (+75) and a per-frame step (+92); this adds the
# step to the frame, wraps or holds at the count, and samples the pose at
# 2*frame in 60 Hz ticks. The walk cycle's step is always 1 -- the play call
# (psp_func_001DFA58) writes it every frame -- so the cycle plays at one
# speed however fast the AC walks. host/replacements.c holds the looping
# slots' frames on the frames an accumulator fed by the stick says to, for
# the one call, and restores the game's bytes after. Interior thunks: 46,
# all ordinary branch targets (the function is a loop over slots with
# branch-likely pairs).
001DFC80   # model animation updater (walk cadence) -- see host/replacements.c

# Optional mission rendering at higher FPS. Original functions remain available;
# the loop adapter is generated from the user's module by scripts/fps-loop.py.
00102018   # mission loop
00259250   # original scanline pacer (bypassed only inside the enhanced loop)
00045DAC   # skeletal world matrices
000FF280   # per-view camera update
0022E800   # HUD update/draw split

# The chase camera's two blends: psp_func_0007467C(ideal, current, r), a
# general vector lerp -- current + (ideal - current) * (1 - r) -- with eight
# callers, and psp_func_000746D4(ideal, current, rx, ry, rz), its per-axis
# twin, called only by the chase-camera handler psp_func_000757E8 for the
# eye. The handler reads r from the camera struct (0x0043C080: +104 for the
# eye = 0.83, +108 for the target = 0.8309) and the 17%-a-frame approach is
# the camera lag every yaw measurement showed. host/replacements.c
# substitutes PSPRECOMP_CAMERA_LAG for r when the `current` argument is
# camera 0's eye or target and the mode is not classic; every other call
# is the original. No interior thunks in either.
0007467C   # vector blend -- the chase camera's target lag -- see host/replacements.c
000746D4   # per-axis vector blend -- the chase camera's eye lag -- see host/replacements.c
