# Code patches for Donkey Kong Bananza 1.0.0 main (recomp/patches.py). Each original word is checked against main.

set hidproxy DKB_SKIP_HID_PROXY 1

# al::GameFrameworkOunce::createControllerMgr (main+0x73b300) reaches sub_71008725E0 through main+0x73be90: it makes
# a transfer memory and talks to IHidServer itself (nn::hid::server::CreateHidServerProxy), past the nn::hid calls the
# host takes over, and aborts on a failed Result. The host serves no "hid" IPC service, so the call reports success.
site hidproxy main 73bea0 9404d9d0 2a1f03e0   # bl sub_71008725E0 -> mov w0, wzr

# Experimental host-paced frame timing. The controller's common exit passes the
# measured simulation rate from patch page +12 to the framework. Opt-in until
# physics, camera and cutscene timing have been qualified across frame rates.
# x8 is dead here: 1ead28 calls 1eaf30, whose first instruction writes w8 before
# reading it. Keep the controller's present interval at +0x40 and its original
# load at 1ead2c; nvn_api_obj.c present_period uses the host cap with realtime on.
set realtime TOTK_REALTIME 0
site realtime main 1ead24 aa1303e0 @page:x8 b9400d01 b9005281 b9005681 aa1303e0

# Clock producers divide 60 by the controller's integer FPS. Read the precise
# 60 Hz step at +8 instead. Preserve the address register, stack and flags; the
# original time-scale multipliers, pause checks and seconds conversions remain.
site realtime main 1bc834 1e201820 f81f0fe9 @page:x9 bd400920 f84107e9
site realtime main 2d9fc8 1e201820 f81f0fe9 @page:x9 bd400920 f84107e9  # global and scene step
site realtime main 2da120 1e201820 f81f0fe9 @page:x9 bd400920 f84107e9  # seconds producer divides by 60 next
site realtime main 34e99c 1e201820 f81f0fe9 @page:x9 bd400920 f84107e9  # actor and camera clock
site realtime main 34ec1c 1e201828 f81f0fe9 @page:x9 bd400928 f84107e9  # same clock with game time scale
site realtime main 34ec68 1e201860 f81f0fe9 @page:x9 bd400920 f84107e9  # time-scale transition
site realtime main 69fc60 1e201820 f81f0fe9 @page:x9 bd400920 f84107e9  # secondary scene clock

# The player's damped velocity update (main+1241c50, 3.2.0 main+13fad60) adds its target once per whole 60 Hz frame, so
# above 60 fps the climb velocity decays to 0 and DK stops on walls and ceilings (#201). runtime/frame_rate.c damped_sum
# (patch page +0x260) stands in for the fmov s12, #1.0 that bounds the loop; the three movi that clear the sum on the
# path that skips the loop become nops. patches-dkb458752.txt explains the sites. The BL assumes the patch page is at
# main+0x2450000 (recomp/layout.py), as the mousecamera sites do.
site realtime main 1241c78 1e2e100c 9448397a  # fmov s12, #1.0, the loop bound
site realtime main 1241c8c 2f00e40b d503201f  # movi d11, #0 on the path that skips the loop
site realtime main 1241c90 2f00e40a d503201f  # movi d10, #0
site realtime main 1241c94 2f00e409 d503201f  # movi d9, #0

# Optional Bananza virtual display and camera aspect. Values are supplied by
# runtime/dkb_wide.c at page +0x100 (width), +0x104 (height), +0x108 (aspect),
# +0x10c (aspect / 16:9), +0x110 (render-target pool bytes).
set dkbwide DKB_ULTRAWIDE 0

# al::getVirtualDisplayWidth / Height. The retail base uses 2880 by 1620.
site dkbwide main 5ad9f0 52816800 @page:x0 b9410000
site dkbwide main 5ada00 5280ca80 @page:x0 b9410400

# Framework init (f_7000458a88) sets the 200 MiB render-target pool size, 0xc800000, into a
# field of its init argument. A larger output overflows that pool (issue 152): take the
# size from page +0x110. x9 is overwritten right after it is stored, so it is free scratch.
site dkbwide main 458adc 52a19009 @page:x9 b9411129

# al::CameraPoseUpdater constructor, the default perspective aspect at +0x100.
# The following MOVK preserves the original upper packed FOV word.
site dkbwide main 522e44 f2a7fc68 @page:x8 b9410908

# Scene camera initialization replaces the constructor aspect through this setter.
# Scale the supplied aspect so the camera keeps its own framing and split views.
# s8 is restored immediately afterwards; s16 and x8 are caller-saved scratch.
site dkbwide main 51e0c4 bd010008 @page:x8 bd410d10 1e300910 bd010010

# Widen fresh UI projections before DrawInfo::SetProjectionMtx copies them.
# The first matrix row maps authored layout x to clip space. Saved projections
# are restored through the same setter, so changing the setter would repeatedly
# narrow the HUD. These four sites copy a newly computed sead projection into
# temporary storage instead. Keep the other matrix rows and all scratch intact.
# main+ea8a0 also projects local layout captures (dialogue backgrounds, text
# effects and icon shadows). Those textures retain their authored dimensions.
# Only widen its full 1920x1080 canvas: m00 == 2/1920 and m11 == 2/1080.
# Compare both lanes, broadcast their combined mask, and BSL the widened row
# against the original. This leaves capture rows bit-identical, preserves NZCV,
# and restores x8 and q16-q19 from the 80-byte temporary stack frame.
site dkbwide main ea8a0 9400002d adbf47f0 adbf4ff2 f81f0fe8 3dc00030 bd401431 52911128 72a75108 1e270112 4e32e612 52973ac8 72a75e48 1e270113 4e33e633 4e331e52 4e040652 @page:x8 bd410d11 4e040631 6e31fe11 6e701e32 3d800032 f84107e8 acc14ff2 acc147f0 9400002d
site dkbwide main 52047c 97ef2936 adbf47f0 f81f0fe8 @page:x8 3dc00030 bd410d11 4e040631 6e31fe10 3d800030 f84107e8 acc147f0 97ef2936
site dkbwide main 7ec2ec 97e3f99a adbf47f0 f81f0fe8 @page:x8 3dc00030 bd410d11 4e040631 6e31fe10 3d800030 f84107e8 acc147f0 97e3f99a
site dkbwide main 15209d4 97af27e0 adbf47f0 f81f0fe8 @page:x8 3dc00030 bd410d11 4e040631 6e31fe10 3d800030 f84107e8 acc147f0 97af27e0

set mousecamera TOTK_MOUSE_CAMERA 0

# CameraPoserFollowLimit's manual yaw update and CameraAngleVerticalCtrl::exeUserCtrl.
# Keep each original instruction, then call the host adapter in runtime/dkb_camera.c. The five
# dispatch addresses are outside main, at patch page +0x200/+0x210/+0x220/+0x230/+0x240. These functions
# save LR in their prologues and restore it after the sites, so the inserted BL preserves returns.
# The BL immediates below assume the patch page is at main+0x2450000 (recomp/layout.py).
site mousecamera main 41b35c f9405808 f9405808 9480d3a9   # yaw begin: x0 is the follow camera
site mousecamera main 41b4e0 6d42a3e9 6d42a3e9 9480d34c   # yaw end: x19 is the follow camera
site mousecamera main 7f209c d000cdb4 d000cdb4 94717861   # pitch begin: x19 is the vertical controller
site mousecamera main 7f22f8 6d42a3e9 6d42a3e9 947177ce   # pitch end: x19 is the vertical controller

# Let vertical mouse activity interrupt the same waiting states as stick input.
site mousecamera main 5ef070 2d4e0660 2d4e0660 94798474   # Interp manual input check
site mousecamera main 173535c bd407260 bd407260 94346bb9 # automatic pitch state manual input check
site mousecamera main 173550c bd407260 bd407260 94346b4d # automatic pitch blend manual input check
site mousecamera main 17357c0 bd407260 bd407260 94346aa0   # SnapEnd manual input check
# AcceptRequest loads s0 from [x0, #0x70] at 1735878, before the kept mov x19, x0.
# The comparisons at 1735884 and 1735898 consume that value against -0.15 and +0.15
# without reloading it. runtime/dkb_camera.c pitch_activity sets s0=1 only for matching
# vertical mouse activity, taking the native activity branch at 173589c to 17358c8.
site mousecamera main 173587c aa0003f3 aa0003f3 94346a71   # AcceptRequest manual input check


# Layer changes (#126). The parallel loop main+0x410680 walks a list of entries, each naming a cell of a 3D grid (a
# 0x4f58-byte object whose address comes from the entry's id through the owner's layer table, owner+0x488), and makes a
# devirtualised call on the cell. While a layer hands over to the next one (Lagoon to Hilltop, Hilltop to Canyon,
# Canyon to Branch, Branch to Forest), dozens of its 4,500 entries and of the cells they name hold reused memory:
# float data as an id (0x3f7fff58), a layer the table does not have, a cell never constructed (vtable 0), a "cell"
# inside main's code, a cell whose own pointers (+0x48, +0x4f10) are garbage. Without these guards the game crashes
# in this loop at every one of those transitions (17 of 17 runs at the first one). Why the memory is reused while the
# list still names it is not known; these are workarounds that keep such an entry out of the loop's work, each through
# a path of the loop itself:
#   410df0: the layer record from the table load at 410dec, before its fields are read -> next entry (410d4c). The
#           loop caches the last entry's id at [x29-0x2c] together with the record in x9; the guard sets that id to -1,
#           so the next entry looks its record up again instead of reusing the bad one.
#   41075c: the cell, before an entry flagged in bit 31 branches to 410eb8 (tbnz at 410764) and before its vtable slot
#           +0x10 is loaded and compared or called at 410768 (only the fall-through reaches it) -> next entry.
#   410ee0: the cell's +0x48 on the bit-31 path; 410edc already sends a null one to 41164c -> 41164c.
#   410794: the cell's element array (+0x4f10, loaded at 410790) -> 410e54, the path of an index past the cell's count.
# Every failed check is logged (runtime/patches.c guard_fail). Opt-out: DKB_FIX_POOL_GUARD=0. Update 3.2.0
# (patches-dkb458752.txt) carries the same guards at main+0x429480.
set poolguard DKB_FIX_POOL_GUARD 1
guard poolguard main 410df0 410d4c ptr x9 f9401927 st32 x29 -2c ffffffff
guard poolguard main 41075c 410d4c vcall x9 b940028a
guard poolguard main 410ee0 41164c ptr x20 aa1403e0
guard poolguard main 410794 410e54 ptr x10 f875794a
# #218 crash 2: f_7000108020 caches up to 256 ids in nodes taken from a free list (owner+0x76b38+0x18, count +0x28). When the
# free list is empty its own code takes a null node and loads its payload at +0x10 (108510). The guard continues the
# loop instead (108468): the id is not cached and may come back on a later pass.
guard poolguard main 108510 108468 ptr x8 f9400909
# #317: the job graph builder f_700011d680 looks each edge's target up in the node table (+0x28); a null slot faulted
# on its pending count (11d8b4, 3.2.0 1116d4). The guard sends a null target to the next edge (11d8dc) before the link
# slot is taken, the path of a full link pool.
guard poolguard main 11d858 11d8dc ptr x12 b82b01ce

# #136: the walk in main+0x1692ac4 passes a null slot to f_7000534324 (the call at 1692c00); the guard sits on the
# bl and skips it to 1692c5c, the loop tail that reloads x8 from [x20] and uses no result of the call.
guard poolguard main 1692c00 1692c5c ptr x0 97ba85c9

# Text box per-character transform setter main+0x84cd40 (issues 157, 206, 173). The layout animation reader
# main+0x82fee0 calls it for each entry of an FLCT section (per-character transform) with x1 = 0 or 1; it stores
# the float in s0 through the object's +0x150 block (at +0 or +4) and sets bit 0x4 of the u16 at +0x114. The block
# is optional: the object's copy routine (main+0x84b530) keeps a null one null. On some players' machines (RX 9070 XT,
# RX 580, RTX 3050; never here) the block is null when a save or the title screen loads, and the store writes address
# 0. Why it is null there is not known. The guards skip the store and the flag (a return), so nothing later reads
# the missing block:
#   84cd50: str s0, [x8] after x8 = [x0+0x150], x1 == 0 -> ret at 84cd60
#   84cd68: str s0, [x8, #4] after x8 = [x0+0x150], x1 == 1 -> ret at 84cd78
# Every failed check is logged (runtime/patches.c guard_fail). Opt-out: DKB_FIX_FLCT_GUARD=0.
set flctguard DKB_FIX_FLCT_GUARD 1
guard flctguard main 84cd50 84cd60 ptr x8 bd000100
guard flctguard main 84cd68 84cd78 ptr x8 bd000500

# Recursive spin locks of main (tools/spin_waits.py): a thread that spins here with every virtual core taken starves the
# lock owner, which needs a core to release the lock (heap and RomFS stalls, issue 126).
spin main 185ac 185c0   # recursive spin lock
spin main 1893c 18950   # recursive spin lock
spin main 135b54 135b68   # recursive spin lock
spin main 157908 15791c   # recursive spin lock
spin main 157da8 157dbc   # recursive spin lock
spin main 1580b4 1580c8   # recursive spin lock
spin main 15944c 159460   # recursive spin lock
spin main 1594b0 1594c4   # recursive spin lock
spin main 15ab04 15ab18   # recursive spin lock
spin main 192778 19278c   # recursive spin lock
spin main 195544 195558   # recursive spin lock
spin main 21a83c 21a850   # recursive spin lock
spin main 22a770 22a784   # recursive spin lock
spin main 22c030 22c044   # recursive spin lock
spin main 253968 25397c   # recursive spin lock
spin main 254ce4 254cf8   # recursive spin lock
spin main 375968 37597c   # recursive spin lock
spin main 3a2c08 3a2c1c   # recursive spin lock
spin main 56976c 569780   # recursive spin lock
spin main 5803b4 5803c8   # recursive spin lock

# The game's dynamic-resolution controller (the function at main 3c12b4) steps a ratio of the virtual display
# to the 3D scene size by frame-time load, quantises it against a table (1.375, 1.5, 1.667 ... 3.0) and stores
# it at 3c1628. Under emulation it settles at 1.5, so the 3D scene is 2/3 of the display on each side. Load the
# ratio from page +0x118 instead (runtime/dkb_wide.c, TOTK_SCENE_RES, 1.0 draws the scene at the full display).
# The next instruction overwrites x9, so it is free scratch. Off by default: the game's own control runs.
# The controller also keeps the unquantised ratio in s8, which its hold test (fcmp s8, s9 at 3c154c) and two
# per-view scales (3c1650, 3c172c) read: load the same ratio into s8 at the join 3c1548, whose own load
# overwrites x9 right after, so all of them see the forced ratio.
set dkbscene DKB_SCENE 0
site dkbscene main 3c1548 f9407a69 @page:x9 bd411928 f9407a69
site dkbscene main 3c1624 bd400120 @page:x9 bd411920

# #229: the per-record loop of f_7000a37520 (3.2.0 f_7000a55520, +0x1e000) reads two 32-bit table indices from the
# record (+0x28, +0x2c) and loads 16 bytes from table base + index*32. A record with an index of 0xffffffff (seen with
# the frame rate unlocked, likely a record still being written) sends the load 128 GiB past the table. The guards
# check the address before each load and take the loop's own next-record step (a3a388, also the target of the tbz at
# a37a20), skipping that record for the frame. Opt-out: DKB_FIX_RECORD_GUARD=0.
set recguard DKB_FIX_RECORD_GUARD 1
guard recguard main a37a84 a3a388 ptr x10 3dc00140
guard recguard main a37a9c a3a388 ptr x11 3dc00160

# #214: text is soft above 1080p. nn::fontll rasterises each glyph at run time from the font's CFF outline, at the
# layout size, so a larger output only magnifies it. Off by default; DKB_FONT_SCALE=1 turns it on: raster at k=2 and
# keep the layout the same. The font cache (f_70049e1d0, filled by f_70006f84a0) holds, per font, a px-to-layout multiplier pair
# at +0x10/+0x14 of its 0x1c-byte entries, and the glyph record GetGlyph (86e6e0) hands out has layout fields
# (+0x48..+0x50, raw pixels times that multiplier) and raw cache fields (+0x44/+0x46 size, +0x40/+0x42 position, in cache
# pixels, which the consumer divides by the cache size at x19+0x28/+0x2c). So: every raster size doubles, every
# multiplier halves, the per-font pixel padding tables double, the ascent doubles and the cache texture doubles.
set fontk DKB_FONT_SCALE 0
# Big fonts stay at k=1. The area titles (fcpx FontHeadline, 180 px) still came out garbled at 2x while the HUD text
# (FontCaption, 48 px) was right, so a font whose size is above 96 px keeps the stock raster and layout. The size is the
# one each site already has: node +0x36 in the cache fill, the size argument (w22 in 86f42c, w20 in 86f65c) in the
# queries. Every site derives its factor from that value with the same flag-free test, so the raster scale, the ascent,
# the multipliers and the pixel tables of a glyph always agree: bit = (96 - size) >> 31 (1 above 96 px) at the
# multiplier and table sites, and its complement, (size - 97) >> 31 (1 at 96 px and below), where SetScale and the
# ascent take k = 2 directly. The entry multipliers and tables stay halved and doubled at init (49ebc8.., 49e75c..), and
# the reading sites undo that above 96 px. The size is a u16 in a 32-bit register: the node field is loaded with ldrh,
# but 86f42c's comes from a 32-bit field (ldr w2, [x, #0x18] at its four callers, 86e1ec..) and 86f65c is only called
# through a vtable, and the stock code uses just the low 16 bits (lsl #16). So 86f42c masks w22 where it is set
# (86f450), and the 86f65c sites mask w20 into their scratch register (w20 itself also goes to the fontll calls).
# SetScale (PLT 1985530) takes the size as 16.16 fixed point: lsl #16 -> lsl #(16 + (size - 97 >> 31)). Cache fill,
# then the width, kerning and advance queries (86f42c, 86f65c), which must measure at the same scale as the multiplier
# they use.
site fontk main 6f85a0 53103d01 51018501 531f7c21 11004021 1ac12101
site fontk main 86f450 2a0203f6 12003c56   # mov w22, w2 -> and w22, w2, #0xffff
site fontk main 86f4c4 53103ec1 510186c1 531f7c21 11004021 1ac122c1
site fontk main 86f7e4 53103e81 12003e81 51018421 531f7c21 11004021 1ac12281
site fontk main 86f860 53103e81 12003e81 51018421 531f7c21 11004021 1ac12281
site fontk main 86f8e8 53103e81 12003e81 51018421 531f7c21 11004021 1ac12281
# Multipliers (str of the file's two floats into the entry): halve. s1 is dead (reloaded at the next 49eb18+ use).
site fontk main 49ebc8 bd001160 1e2c1001 1e210800 bd001160
site fontk main 49ebd8 bd001560 1e2c1001 1e210800 bd001560
# Per-font pixel tables used with glyph widths (1c10, 1c28 of the cache): store them doubled. The registers are dead after.
site fontk main 49e75c 7837796a 531f794a 7837796a
site fontk main 49e794 78377928 531f7908 78377928
# Ascent in pixels, size * ratio + 0.5 (fmadd at 6f85d8): the size read from node +0x36 into s1, shifted left by
# 1 - bit. x9 (consumed at 6f85c4) and x10 are dead until after the call at 6f85e0.
site fontk main 6f85d0 7e21d821 1e260029 5101852a 531f7d4a 1aca2129 1e230121
# Fill: the multiplier pair (ldp at 6f87e8) times 2^bit, by adding bit to the exponents (they are 0.5 to 0.6, normal).
# w9 is rewritten at 6f87ec, x10 at 6f8840/6f88a8/6f8948 before any read.
site fontk main 6f87e8 2d42252a 2d42252a 79406f49 4b0903e9 11018129 531f7d29 53092129 1e26014a 0b09014a 1e27014a 1e26012a 0b09014a 1e270149
# Fill: the pixel tables (1c10 at 6f8844, 6f8860, 6f8950, 6f8970; 1c28 at 6f8890, 6f88ac) shifted right by bit, which is
# exact since init doubled them. The two ldrh sites feed a signed halving of the u16, so they load signed and mask back
# to 16 bits. x11 is not used between 6f8838 and its next write at 6f8a80.
site fontk main 6f8844 78e8794a 78e8794a 79406f4b 4b0b03eb 1101816b 531f7d6b 1acb294a
site fontk main 6f8860 78687929 78e87929 79406f4b 4b0b03eb 1101816b 531f7d6b 1acb2929 12003d29
site fontk main 6f8890 78e87929 78e87929 79406f4b 4b0b03eb 1101816b 531f7d6b 1acb2929
site fontk main 6f88ac 78e87929 78e87929 79406f4b 4b0b03eb 1101816b 531f7d6b 1acb2929
site fontk main 6f8950 78e8794a 78e8794a 79406f4b 4b0b03eb 1101816b 531f7d6b 1acb294a
site fontk main 6f8970 78687929 78e87929 79406f4b 4b0b03eb 1101816b 531f7d6b 1acb2929 12003d29
# Width query (86f42c, size w22): multiplier times (bit ? 2.0 : 1.0) through s1 (rewritten at 86f540, w8 at 86f534),
# table 1c10 shifted right by bit through w10 (unused in the function).
site fontk main 86f530 bd401100 bd401100 4b1603e8 11018108 531f7d08 53092108 32091908 1e270101 1e210800
site fontk main 86f538 78f57929 78f57929 4b1603ea 1101814a 531f7d4a 1aca2929
# Kerning query (86f65c, size w20): the same factor on each multiplier load. x9 is reloaded right after 86f830 and
# 86f8ac, where s1 is free before the call; at 86f94c and 86f9b8 s1 holds the kerning value, so s2 carries the factor.
site fontk main 86f830 bd401128 bd401128 12003e89 4b0903e9 11018129 531f7d29 53092129 32091929 1e270121 1e210908
site fontk main 86f8ac bd401128 bd401128 12003e89 4b0903e9 11018129 531f7d29 53092129 32091929 1e270121 1e210908
site fontk main 86f94c bd401100 bd401100 12003e88 4b0803e8 11018108 531f7d08 53092108 32091908 1e270102 1e220800
site fontk main 86f9b8 bd401100 bd401100 12003e88 4b0803e8 11018108 531f7d08 53092108 32091908 1e270102 1e220800
# Raster work buffer of the engine that fills the cache (ScalableFontEngine::Initialize at 49e594, size from descriptor
# +0x88, which the config default f_70004739e0 sets to 0x80000 and nothing overrides): x4, as a glyph bitmap at 2x is
# four times larger. With 512 KiB AcquireGlyphmap (6f85e0) returns null and the fill reads glyph +0x1e off it. The
# buffer comes from the FontHeap (HeapDef "Font", 30720 KiB); w9 is reloaded at 473d0c. f_70004739e0's two callers are
# the two paths of one engine set-up, f_70004727a4 (called once, from 4712c4): 4733e8 inside f_7000473390, which parses
# a font list (fcpx/ entries) and changes only descriptor +0x90, and 4728e0, the built-in list when that returns 0.
# Both fill the same descriptor (sp + 0x678), so there is one 2 MiB buffer and DKB_FONT_EXTRA_KIB counts it once.
site fontk main 473cf0 52a00109 52a00409
# Glyph cache texture (descriptor +0/+4, stored at cache +0x28/+0x2c): x2 each way, as a 2x glyph covers four times the
# area and the shelf packer (line heights 16..1024 from f_700086fa30, lines up to the cache size) would evict glyphs
# still on screen. Every reader takes the size from +0x28/+0x2c (texture builder 49ed30, packer 86be48, glyph records
# 86e490/86e754). The R8 texture becomes 32 MiB, more than the stock 30 MiB FontHeap (the allocation at 49eebc would
# return null and the memset at 49eecc write to 0): resolution.c grows the HeapDef's "Font" by 28 MiB while fontk is on,
# and turns fontk off when it cannot serve that table.
site fontk main 49e4a0 b9400288 b9400288 531f7908
site fontk main 49e4ac b9400688 b9400688 531f7908
