# Code patches for XCX DE 2.0.0 main (recomp/patches.py).
#
# xcx60 (XCX_FPS60, on): the frame rate of the frame limit XCX_FPS_LIMIT (frame_rate.c). The interval and period
# sites are our own; the others are ported from the 60FPS mod for 1.0.1 by Fl4sh_ and StevensND
# (github.com/Fl4sh9174/Switch-Emulator-Ultrawide-FPS-Mods): each 1.0.1 site was found in the 1.0.0 main by what the
# patch replaces, then in 2.0.0 by the surrounding instructions (unique match); the original words below are 2.0.0's. The mod's other sites (constant time scales at
# +0x1e245c..+0x1e24dc and +0xeb108..+0xeb128) have no clear 1.0.0 counterpart and are not ported.

set xcx60 XCX_FPS60 1
set xcx60g XCX_FPS60_GETTER 0
set xcx60m XCX_FPS60_MODE 0

# The game's frame interval in vsyncs, set once at boot (main+0x13db530 calls the setter main+0x13a2fd8 with 2). The
# main loop's limiter (main+0x13a9ac0) waits that many vsyncs, the frame-time getter main+0x13a250c returns
# interval * the vsync period below, and the game asks NVN for the same present interval: 1 vsync per frame.
site xcx60 main 13db540 52800040 52800020
# The game's vsync period, a virtual of the frame pacing object (main+0x13baa30) that returns 1/60 s: the limiter
# waits interval * period and the frame time is the same product. The patch page's +0x40 instead (frame_rate.c, the
# frame limit XCX_FPS_LIMIT: 1 / limit), so one interval is one frame at the limit.
site xcx60 main 13baa34 72a79108 @page:x8 b9404108
# The limiter's wait, interval * that period (main+0x13a9af4, then * 0.998 and compared with the time since the last
# frame): the page's +0x48 instead, the frame time of the limit (2/60 s in cutscenes). The period above follows the
# measured frame time (frame_rate.c, the real-time step); waiting that long would hold every frame to the slowest
# recent one. x8 is reloaded before its next use.
site xcx60 main 13a9af4 1e210800 @page:x8 bd404900
# The game's frame-rate pick, flag ? 60.0 : 30.0, then stored with its reciprocal (1.0.1 +0xa2ee5c): both arms the
# limit, the page's +0x44 (x9 is free here: the next use loads it).
site xcx60 main a1f05c 1e27d001 @page:x9 bd404521
# A 1/30 s time constant (0x3d088889, 1.0.1 +0x569c0; the mod's 1/60): 1 / limit, the page's +0x40.
site xcx60 main 56d40 72a7a108 @page:x8 b9404108
# The UI's frame step (#107): the frame-rate pick above stores the rate and 1 / rate at +0x78 and +0x7c of the UI
# root (main+0xa1f044, reached from the menu manager's setup, not each frame), and its per-frame update (main+0xa1f2f0)
# keeps +0x80 = +0x7c * the speed at +0x94, the step its windows advance by each frame. With the rate fixed at the
# boot value, windows opened at half speed in 30 fps cutscenes (Accept Mission, dialogue choices) and would run fast
# above the boot rate. Each frame the update sets +0x7c to the page's +0x78 (1 / the frame rate shown) and +0x80 to
# that times +0x94, as on the console, where the rate and the frame rate are both 30. x8 is not read again here.
site xcx60 main a1f37c f9402e60 @page:x8 bd407900 bd007e60 bd409661 1e210800 bd008260 f9402e60

# The game's frame update (main+0x1e287c, a virtual reached through main+0x1e2b44) passes its step, one per frame, on
# to the field, battle and event updates. Most turn it into time themselves; three count it as 30 fps frames, so above
# 30 fps they ran fast (battle timing prompts): their step is s8 * the patch page's +0x4c (30 / frame rate), as the
# 1.0.1 mod's constant 0.5 at 60 fps (1.0.1 +0x1e24a0, +0x1e24dc, +0xeb108). x9 is free at each: a call follows.
site xcx60 main 1e28e0 1e204100 @page:x9 bd404d20 1e200900
site xcx60 main 1e291c 1e204100 @page:x9 bd404d20 1e200900
site xcx60 main eb408 1e204100 @page:x9 bd404d20 1e200900

# The Soul Challenge ring (the timing prompt in battle): its update (around main+0x5c626c) takes a fixed amount per
# frame, +0x120 of the prompt, off the time left at +0x11c, so above 30 fps the ring shrank fast. The amount is
# scaled by the patch page's +0x4c (30 / frame rate), as the 1.0.1 mod's halving at 60 fps (its cave at 1.0.1
# +0x2a02ac, called from +0x5c5874). x8 and s2 are free: every path from here calls before reading them.
site xcx60 main 5c6284 1e213800 @page:x8 bd404d02 1e220821 1e213800
# Right-stick camera turn (issue #19): the camera's rotation update (main+0xab514) turns the stick, after its response
# curve, into a pitch (main+0xab74c) and a yaw (main+0xabab8) per frame, both times the turn speed it loads here from
# the camera parameters (+0xb4); no frame time is applied, so above 30 fps the camera turned faster. The speed is scaled
# by the patch page's +0x4c (30 / frame rate) where it is loaded, so the pitch limits still apply to the scaled step.
# x8 is rewritten next (main+0xab704); s16 is written before it is read.
site xcx60 main ab700 bd40b6a8 @page:x8 bd40b6a8 bd404d10 1e300908
# The salvage hold (issue #19): its progress is a float at +0xc8 of the hold, clamped to the total at +0xcc
# (main+0xe5300, which adds a step and clamps). While A is held the step is a data rate times a data amount
# (main+0xe5e8c); after A is released the progress drains by +0xd0 of the hold each frame (main+0xe5860). Both steps
# are per frame, so above 30 fps salvaging filled faster. Both are scaled by the patch page's +0x4c (30 / frame rate).
# x0 is reloaded right after the first, x8 is free at the second, and s1 is caller-saved at both (a call follows).
site xcx60 main e5e8c 1e280800 @page:x0 bd404c01 1e280800 1e210800
site xcx60 main e5860 1e214000 @page:x8 bd404d01 1e214000 1e210800
# The hold's tick sound (main+0xe5ee8): a timer at +0x48 of the hold loses 1.0 a frame and, at 0, is reset to 10.0 and
# plays the tick (main+0x35e6b0), so the tick came every 10 frames, 4 times as often at 120 fps. The step is the patch
# page's +0x4c (30 / frame rate) instead: fmov s1, #-1.0 becomes its load and the fadd a fsub. x8 is rewritten at
# main+0xe5f00 before its next use.
site xcx60 main e5eec 1e3e1001 @page:x8 bd404d01
site xcx60 main e5ef0 1e212800 1e213800

# The small or big field jump (issue #64), decided by main+0x23ee90: big when the jump's hold time (VF_JumpInputDuration,
# which counts from the takeoff) reaches min(VF_HighJumpInputFrame / 30, the PROG_SmallOrBigJump frame / 30 +
# CF_NormalTakeOffEnd / 30) - 1e-6 on a frame where the jump animation has not yet passed that PROG frame. The hold time
# leads the animation by the takeoff plus the part of a frame the takeoff overshoots, and the animation's last frame
# before the PROG frame falls up to a frame short of it, so held jumps turned small when the two frame boundaries fell
# unevenly, which steady 30 or 60 fps frames avoid. The threshold is lowered by the patch page's +0x40 (one frame)
# instead of 1e-6, so a held jump is big at any frame time and a tap stays small. x8 is free until main+0x23f008.
site xcx60 main 23efe8 5286f7a8 @page:x8
site xcx60 main 23efec 72b6b0c8 d503201f
site xcx60 main 23eff8 1e270101 bd404101
site xcx60 main 23effc 1e212800 1e213800

# The menus' frame step (issue #63): main+0x927e10 fills the menu manager's frame info each frame from the game's frame
# interval in vsyncs (main+0x1d943a0): +0x8fc and +0x900 = interval / 60 s, +0x904 = 30 / (60 / interval). Menu models
# (Ground Gear and the others, through main+0x6916cc and the model's step setter main+0x185d48) and several menu timers
# advance by +0x900, so with one vsync per frame they stepped 1/60 s per frame and ran fast above 60 fps. The step is
# the patch page's +0x40 (the measured frame time) times the interval instead, so cutscenes (interval 2, period 1/60 s)
# keep 2/60 s, and +0x904 is 30 times that step. x9 is unused in the function, s3 is caller-saved, w0 is the interval.
site xcx60 main 927e4c 1e201821 @page:x9 bd404121 1e220003 1e230821
site xcx60 main 927e50 1e201840 1e210840

# The sky's cloud turn and fade (issue #89): main+0xa16eec sets the render scene's step in 30 fps frames each frame
# (+0x894 of the scene, 1.0, or 0.5 in the Switch 2 Edition's 60 fps mode). The sky update advances its cloud layers'
# turn at +0x1c and +0x20 by speed * 1/30 s * step (main+0xa1116c and main+0xa11180) and its fade at +0x4c by
# 1/30 * step (main+0xa1126c), so above 30 fps the clouds ran fast. Each load of the step is multiplied by the patch
# page's +0x4c (30 / frame rate). The step itself stays 1.0: with it scaled, the water slowed down as the frame rate
# rose, so some of its other readers already follow the frame time. x11 and s5 are unused in the rest of
# main+0xa1116c's function; x8 and s2 are set again after main+0xa1126c.
site xcx60 main a1116c bd489501 bd489501 @page:x11 bd404d65 1e250821
site xcx60 main a11180 bd489501 bd489501 @page:x11 bd404d65 1e250821
site xcx60 main a1126c bd489500 bd489500 @page:x8 bd404d02 1e220800

# The mod's getter that returns false on Switch (1.0.1 +0x13eb280): true. Off: in 2.0.0 it crashes the title screen's
# rendering (main+0x9530b0 reads through a bad pointer).
site xcx60g main 13db618 2a1f03e0 52800020

# A flag at +0x301 of the object at main+0x1df6020, read by the getter main+0x13a288c: its callers halve time steps
# and scales (main+0x289748, +0x3122e4, +0xa16ef4) and pick 60 at the fps pick above. Both returns of the getter give
# true. Off: on its own it does not change the frame rate, and what it halves is untested.
site xcx60m main 13a28a4 1a9f07e0 52800020
site xcx60m main 13a28ac 2a1f03e0 52800020

# resbuf (XCX_RES_BUFFER set, resolution.c): main+0x9ccce0 sizes two buffers from a width it loads and the docked
# height of the resolution table; both loads read the patch page's +0x50 and +0x54 instead, as the 1.0.1 resolution
# mods (1.0.1 +0x9dcaec, +0x9dcb00) put sizes of their own there. x8 is reloaded or unused after each.
set resbuf XCX_RES_BUFFER 0
site resbuf main 9cccec b9400114 @page:x8 b9405114
site resbuf main 9ccd00 b9400115 @page:x8 b9405515

# The 3D render scale (issue #33). The game runs dynamic resolution: each frame main+0x97730c compares the time from
# main+0x13a28dc with a target and steps a level from 0 to 10, which moves the scale of its record (renderer +0x528) from
# a maximum (+0x14, +0x18) to a minimum (+0x1c, +0x20), all read from /monolib/shader/lib_nx.ini. Then the renderer
# (main+0x971c00) has main+0x9dc89c take the current scale, the record's floats at +0x24 (width) and +0x28 (height), cut
# to whole percents; with scaling off in the record it is 1, with main+0x9dc980 as the exit. The scene draws in that part
# of its full-size targets, and the game's FSR pass scales it up.
# scene: both pointers go to the patch page's +0x58, one float, the scale XCX_RENDER_SCALE asks for. x8 and x9 are
# what the next instructions load through, so nothing else reads them.
# scenefull: the enable byte reads as 0, the game's own unscaled path. 100 % goes this way because main+0x9dc9f4 rounds
# a scaled size up to a multiple of 8, past the target at a height of 900 or 1800.
set scene XCX_RENDER_SCALE 0
site scene main 9dc8c0 91009028 @page:x8 91016108
site scene main 9dc8c4 9100a029 91000109
set scenefull XCX_RENDER_SCALE 0
site scenefull main 9dc8b4 39400028 2a1f03e8
# sceneread: the page's +0x60 and +0x64 keep the scale in use, for the overlay (resolution.c res_sizes). Every path of
# main+0x9dc89c arrives here with the scale main+0x9dc9f4 sizes the scene by in s1 (width) and s0 (height); the percents
# stored after this are cut once more and can be one lower. x9 is written by the next instruction anyway.
set sceneread XCX_SCENE_READ 1
site sceneread main 9dc9a8 52a85909 @page:x9 2d0c0121 52a85909

# uifit (XCX_UI_FIT, resolution.c): on a frame wider than 16:9 the runtime narrows the interface about the screen centre
# (nvn_api_cmd.c ui_fit), which would pull anything placed from a 3D position, such as enemy labels, towards the centre.
# Two functions project those positions into the canvas and end with x = (1 - ndc) * width / 2: main+0x560800 (the labels)
# and main+0xa17454 (only caller main+0x402e64). ndc is multiplied by the patch page's +0x68, 1 / the narrowing, so the
# narrowing lands them on their targets. x9 and s5 are free at both: neither is read again before it is written.
set uifit XCX_UI_FIT 1
site uifit main 56094c 1e323a23 @page:x9 bd406925 1e250a52 1e323a23
site uifit main a17580 1e213801 @page:x9 bd406925 1e250821 1e213801

# The narrowed canvas leaves room at both sides, but the interface draws only what overlaps the canvas, so the map (#90)
# and the Affinity Chart (#91) stopped at its 16:9 edges. Two tests decide that:
# - The map (main+0x7a7c60) and the chart (main+0x87f0d0) give their tile and portrait object a visible rect, the canvas
#   centre plus and minus half the canvas over the zoom, through main+0x67f620. The half width there is copied by adding a
#   register that was just zeroed (main+0x7a7d1c, main+0x87f144); the copy becomes the half width times +0x68, 1 / the
#   narrowing, so the rect covers the whole frame. x8 and s6 are free: neither is read again before it is written.
# - The interface engine skips an element whose bounds (element +0x1c0 max x, +0x1c8 min x, in canvas widths) lie wholly
#   left of 0 or right of 1. Each copy of the test compares max x with 0 and min x with 1 (or with s8 holding 1); they
#   compare with +0x70 and +0x74 instead, the canvas range the frame shows (resolution.c ui_fit_init). The y tests stay.
#   x17 and s31 are free: no function in main+0xa20000..0xa48000 uses x17 near these tests or v31 at all.
site uifit main 7a7d1c 1e222984 @page:x8 bd406904 1e240984
site uifit main 87f144 1e232821 @page:x8 bd406906 1e260821
site uifit main a256a4 1e202008 @page:x17 bd40723f 1e3f2000
site uifit main a2f5c8 1e202008 @page:x17 bd40723f 1e3f2000
site uifit main a32280 1e202008 @page:x17 bd40723f 1e3f2000
site uifit main a3479c 1e202008 @page:x17 bd40723f 1e3f2000
site uifit main a35900 1e202008 @page:x17 bd40723f 1e3f2000
site uifit main a3608c 1e202008 @page:x17 bd40723f 1e3f2000
site uifit main a361e4 1e202008 @page:x17 bd40723f 1e3f2000
site uifit main a3cbd0 1e202008 @page:x17 bd40723f 1e3f2000
site uifit main a3ccd8 1e202008 @page:x17 bd40723f 1e3f2000
site uifit main a3ce30 1e202008 @page:x17 bd40723f 1e3f2000
site uifit main a256c0 1e202020 @page:x17 bd40763f 1e3f2020
site uifit main a2f5e4 1e202020 @page:x17 bd40763f 1e3f2020
site uifit main a3229c 1e202020 @page:x17 bd40763f 1e3f2020
site uifit main a347b8 1e202020 @page:x17 bd40763f 1e3f2020
site uifit main a3591c 1e202020 @page:x17 bd40763f 1e3f2020
site uifit main a360a8 1e202020 @page:x17 bd40763f 1e3f2020
site uifit main a36200 1e202020 @page:x17 bd40763f 1e3f2020
site uifit main a3cbe8 1e282000 @page:x17 bd40763f 1e3f2000
site uifit main a3ccf0 1e282000 @page:x17 bd40763f 1e3f2000
site uifit main a3ce48 1e282000 @page:x17 bd40763f 1e3f2000

# viewsize (on above the shipped resolution, resolution.c): a menu with a 3D model asks for a second view, and the view
# setup main+0x972ed0 takes that view's size from the request at +0x1a0, 1920x1080 for the menus, instead of the main
# view's size at renderer +0x3c8 (issue #40). Its targets stayed 1920x1080 and the result was stretched over the frame.
# A request for exactly that size means a whole frame, so it gets the main view's size; other sizes stay. The lifter takes
# no flags in a site, so the choice is arithmetic: clz of the difference, shifted, is 1 on a match and 0 otherwise, and it
# scales the step from the request to the main size. x11 and x12 are free: main+0x972f94 writes x11 next and the function
# does not read x12.
set viewsize XCX_VIEW_SIZE 0
site viewsize main 972f84 b941a2a9 b941a2a9 511e012b 5ac0116b 53057d6b b943ca6c 4b09018c 1b0b7d8c 0b0c0129
site viewsize main 972f8c 910692aa 910692aa b940014b 5110e16b 5ac0116b 53057d6b 910f326c cb0a018c 9b0b7d8c 8b0c014a

# Events count their time in 30 fps frames: each update adds a constant 1.0 where a step belongs, so above 30 fps
# (Mixed or no cutscene limit) event cameras and actors ran fast and then jumped to the timeline's keyframes, and
# subtitles and voice lines ended early (#106). Each site takes the patch page's +0x4c (30 / frame rate) instead:
# the event manager's update and late update hand 1.0 to every actor and to the camera system (main+0x2d0010,
# main+0x2cfc40); the camera system forces 1.0 while an event runs (main+0x2aadc8); the event clock adds its rate,
# 1.0 frame, per update (main+0x2dca18, read at +0x3c); a message's remaining time and a voice's start delay count
# down by 1.0 (main+0x2e5d20, main+0x304dd0). x9 is free at each; at main+0x2dcb14 s4 is too.
site xcx60 main 2d00b4 1e2e1008 @page:x9 bd404d28
site xcx60 main 2d00f0 1e2e1000 @page:x9 bd404d20
site xcx60 main 2cfcac 1e2e1008 @page:x9 bd404d28
site xcx60 main 2cfcf0 1e2e1000 @page:x9 bd404d20
site xcx60 main 2cfd60 1e2e1008 @page:x9 bd404d28
site xcx60 main 2aae10 1e2e1000 @page:x9 bd404d20
site xcx60 main 2dcb14 2d478e62 @page:x9 2d478e62 bd404d24 1e240842
site xcx60 main 2e5d58 1e3e1001 @page:x9 bd404d21 1e214021
site xcx60 main 304dd0 1e3e1001 @page:x9 bd404d21 1e214021
# Event actors also use the step they are handed as their animations' play speed: main+0x2e9d0 sets each animation
# slot's rate to its base rate times that value (through main+0x3f620). The animations already advance by the real
# frame time, so with the step above at 30 / frame rate they played at half speed at 60 and a quarter at 120; in the
# first NLA scene the camera then lost the Skell it rides with in the close-up (#106). main+0x2e9d0 now divides
# the speed by the patch page's +0x4c, so only a speed the event sets itself remains. x9 is free at its entry.
site xcx60 main 2e9ec 1e204008 @page:x9 bd404d28 1e281808

# The HUD's text windows (#116: the yellow tips at the lower left, among others): opening one (main+0x5d870c,
# main+0x5d8ebc, main+0x5d966c) stores its data row's DispTime column, a count of 30 fps frames, at +0x2a of the window,
# and each update (main+0x5d88c8, main+0x5d9078, main+0x5d9828) takes 1 off it and closes the window at 0. Above 30 fps
# the window closed early, less than a second at 150 fps. The count is divided by the patch page's +0x4c (30 / frame
# rate) where it is stored, so it lasts DispTime / 30 s at any frame rate. x9, w0 and s0-s1 are free: x0 is reloaded and
# a call follows at each.
site xcx60 main 5d87a8 79005680 @page:x9 1e230000 bd404d21 1e211800 1e250000 79005680
site xcx60 main 5d8f58 79005680 @page:x9 1e230000 bd404d21 1e211800 1e250000 79005680
site xcx60 main 5d9708 79005680 @page:x9 1e230000 bd404d21 1e211800 1e250000 79005680
# The field's enemy talk box (#116: "Tyrant Info" and the other lines enemies call out, at the lower left): the talk
# manager's update (main+0x7fc330) opens the box, sets its hold to 300 frames at +0x80 (main+0x7fc8d0) and takes 1 off it
# each update (main+0x7fc6dc), closing the box when it runs out. That is 10 s at the console's 30 fps but 5 s at 60 and
# 2.5 s at 120. The 300 is divided by the patch page's +0x4c (30 / frame rate) instead, so the box holds 10 s at any
# frame rate. x9 is set here anyway and s0-s1 are free: calls come just before and the function returns after.
site xcx60 main 7fc8d0 52802589 @page:x9 bd404d21 52802589 1e230120 1e211800 1e250009
