# Code patches for Donkey Kong Bananza 458752 main (recomp/patches.py), ported from dkb100 by
# tools/port_patches.py. Each original word is checked against main. Offsets inside comments, and
# "[1.0.0 xxx]" tags, are dkb100 addresses. hidproxy is gone: the controller constructor calls
# InitializeNpad directly (main+0x758650, a 458752 address) and the host overrides that.


# 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 204a54 aa1303e0 @page:x8 b9400d01 b9005281 b9005681 aa1303e0  # 1.0.0 1ead24

# 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 1c8c9c 1e201820 f81f0fe9 @page:x9 bd400920 f84107e9  # 1.0.0 1bc834
site realtime main 2f2928 1e201820 f81f0fe9 @page:x9 bd400920 f84107e9  # global and scene step  [1.0.0 2d9fc8]
site realtime main 2f2a80 1e201820 f81f0fe9 @page:x9 bd400920 f84107e9  # seconds producer divides by 60 next  [1.0.0 2da120]
site realtime main 372060 1e201820 f81f0fe9 @page:x9 bd400920 f84107e9  # actor and camera clock  [1.0.0 34e99c]
site realtime main 3722e0 1e201828 f81f0fe9 @page:x9 bd400928 f84107e9  # same clock with game time scale  [1.0.0 34ec1c]
site realtime main 37232c 1e201860 f81f0fe9 @page:x9 bd400920 f84107e9  # time-scale transition  [1.0.0 34ec68]
site realtime main 6b8260 1e201820 f81f0fe9 @page:x9 bd400920 f84107e9  # secondary scene clock  [1.0.0 69fc60]

# The radial blur entries a smash starts (crystal bananas, the intro rock) count their lifetime in whole 60 Hz frames:
# each frame subtracts fcvtzs(step) from [x22-4], the step being the update info's +0x40 or +0x44. A step under 1.0
# (any rate above 60 fps) truncates to 0, so the entry was never released and its blur stayed on screen (#158).
# runtime/frame_rate.c blur_frames (patch page +0x250) keeps the fraction per entry and returns the whole frames in w8.
# The function saves LR in its prologue (main+426e94), so the BL keeps its return.
site realtime main 426f54 1e380008 948fa4bf  # fcvtzs w8, s0 in the blur entry update  [3.2.0 only]

# The player's damped velocity update (main+13fad60; its eight callers are the player's movers, the wall and ceiling
# climbs among them) steps v = r^step * v + sum of r^i * target for i = 1 .. step: it adds the target once per whole
# 60 Hz frame. A step under 1.0 (any rate above 60 fps) adds nothing, so 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: on a fractional step it puts the closed form of the sum in s9..s11 and +inf in s12, so the loop does
# not run; on a whole step it leaves s12 = 1.0 and the game's loop. The three movi that clear s9..s11 on the path that
# skips the loop become nops so the sum reaches the store. At the BL x0, x1 and s8 hold the mover, the target and r; the
# function saves LR in its prologue (main+13fad70), so the BL keeps its return.
site realtime main 13fad88 1e2e100c 94505536  # fmov s12, #1.0, the loop bound  [3.2.0 only]
site realtime main 13fad9c 2f00e40b d503201f  # movi d11, #0 on the path that skips the loop
site realtime main 13fada0 2f00e40a d503201f  # movi d10, #0
site realtime main 13fada4 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 5bf770 52816800 @page:x0 b9410000  # 1.0.0 5ad9f0
site dkbwide main 5bf780 5280ca80 @page:x0 b9410400  # 1.0.0 5ada00

# Framework init 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 the store.
site dkbwide main 53995c 52a19009 @page:x9 b9411129  # 1.0.0 458adc

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

# 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 45d5d4 bd010008 @page:x8 bd410d10 1e300910 bd010010  # 1.0.0 51e0c4

# 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 b1260 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  # 1.0.0 ea8a0
site dkbwide main 45f9dc 97f1464e adbf47f0 f81f0fe8 @page:x8 3dc00030 bd410d11 4e040631 6e31fe10 3d800030 f84107e8 acc147f0 97f1464e  # 1.0.0 52047c
site dkbwide main 803c94 97e2b5a0 adbf47f0 f81f0fe8 @page:x8 3dc00030 bd410d11 4e040631 6e31fe10 3d800030 f84107e8 acc147f0 97e2b5a0  # 1.0.0 7ec2ec
site dkbwide main 170e2f4 97a68c08 adbf47f0 f81f0fe8 @page:x8 3dc00030 bd410d11 4e040631 6e31fe10 3d800030 f84107e8 acc147f0 97a68c08  # 1.0.0 15209d4

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.
# All seven BL immediates below assume the patch page is at main+0x2810000 (recomp/layout.py).
site mousecamera main 4385fc f9405808 f9405808 948f5f01  # yaw begin: x0 is the follow camera  [1.0.0 41b35c]
site mousecamera main 438780 6d42a3e9 6d42a3e9 948f5ea4  # yaw end: x19 is the follow camera  [1.0.0 41b4e0]
site mousecamera main 80a1a0 f000e254 f000e254 94801820  # pitch begin: x19 is the vertical controller  [1.0.0 7f209c]
site mousecamera main 80a3fc 6d42a3e9 6d42a3e9 9480178d  # pitch end: x19 is the vertical controller  [1.0.0 7f22f8]

# Let vertical mouse activity interrupt the same waiting states as stick input.
site mousecamera main 5e949c 2d4e0660 2d4e0660 94889b69  # Interp manual input check  [1.0.0 5ef070]
site mousecamera main 192b22c bd407260 bd407260 943b9405  # automatic pitch state manual input check  [1.0.0 173535c]
site mousecamera main 192b3dc bd407260 bd407260 943b9399  # automatic pitch blend manual input check  [1.0.0 173550c]
site mousecamera main 192b690 bd407260 bd407260 943b92ec  # SnapEnd manual input check  [1.0.0 17357c0]
# 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 192b74c aa0003f3 aa0003f3 943b92bd  # AcceptRequest manual input check  [1.0.0 173587c]

# 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 160f0 16104   # recursive spin lock
spin main 16480 16494   # recursive spin lock
spin main 13e8f4 13e908   # recursive spin lock
spin main 173b28 173b3c   # recursive spin lock
spin main 174954 174968   # recursive spin lock
spin main 1749b8 1749cc   # recursive spin lock
spin main 174a1c 174a30   # recursive spin lock
spin main 175dc8 175ddc   # recursive spin lock
spin main 175e2c 175e40   # recursive spin lock
spin main 194d14 194d28   # recursive spin lock
spin main 19fbc8 19fbdc   # recursive spin lock
spin main 207588 20759c   # recursive spin lock
spin main 20867c 208690   # recursive spin lock
spin main 2623d0 2623e4   # recursive spin lock
spin main 287870 287884   # recursive spin lock
spin main 287d70 287d84   # recursive spin lock
spin main 2893fc 289410   # recursive spin lock
spin main 28a47c 28a490   # recursive spin lock
spin main 28bfc0 28bfd4   # recursive spin lock
spin main 3dfc08 3dfc1c   # recursive spin lock
spin main 40b508 40b51c   # recursive spin lock
spin main 4a9f2c 4a9f40   # recursive spin lock
spin main 4c0b6c 4c0b80   # recursive spin lock

# Dynamic-resolution ratio store, as in patches-dkb100.txt (1.0.0 3c1624): the page +0x118 ratio replaces the
# controller's quantised ratio. Same instruction words, found by the unique ldr/str/str/fmov sequence.
set dkbscene DKB_SCENE 0
site dkbscene main 38de2c f9407a69 @page:x9 bd411928 f9407a69  # 1.0.0 3c1548, s8
site dkbscene main 38df08 bd400120 @page:x9 bd411920  # 1.0.0 3c1624

# 3D image scale (runtime/dkb_wide.c dkb_wide_scene, scene_res). Each display's 3D image is a scale from the game's
# render parameters (copied into the config at main+58760, read at config +0x1b0/+0x1b4) times the display size: 2/3
# on 3.2.0, which 1.0.0 does not have, and the dynamic-resolution ratio (dkbscene) divides that image. With scene_res
# set, the two loads read a scale of 1.0 from patch page +0x120/+0x124, so the scene is the display divided by the
# ratio, as on 1.0.0. The image is allocated at the larger of the display size and its own (main+958c8), so a scale of
# 1.0 needs no larger image. x10 is free here: nothing reads it before main+79034 writes it.
set render3d DKB_RENDER3D 0
site render3d main 79024 bd41b100 @page:x10 bd412140  # ldr s0, [x10, #0x120] in place of ldr s0, [x8, #0x1b0]
site render3d main 79028 bd41b501 bd412541  # ldr s1, [x10, #0x124] in place of ldr s1, [x8, #0x1b4]

# Text box per-character transform setter main+0x86d848 (1.0.0 main+0x84cd40, same 15 words): the null +0x150
# block guards of patches-dkb100.txt (issues 157, 206, 173), placed by hand because tools/port_patches.py does not
# port guards. Opt-out: DKB_FIX_FLCT_GUARD=0.
set flctguard DKB_FIX_FLCT_GUARD 1
guard flctguard main 86d858 86d868 ptr x8 bd000100
guard flctguard main 86d870 86d880 ptr x8 bd000500

# Parallel pool-entry loop main+0x429480 (1.0.0 main+0x410680, the same code shifted by 0x18e00; the words differ
# only in adrp, page offsets and bl targets): the stale layer record and cell guards of patches-dkb100.txt
# (issue 126), placed by hand. The same faults at 429bf0 and 42956c closed the game with code 71 in issue 218.
# Opt-out: DKB_FIX_POOL_GUARD=0.
set poolguard DKB_FIX_POOL_GUARD 1
guard poolguard main 429bf0 429b4c ptr x9 f9401927 st32 x29 -2c ffffffff
guard poolguard main 42955c 429b4c vcall x9 b940028a
guard poolguard main 429ce0 42a44c ptr x20 aa1403e0
guard poolguard main 429594 429c54 ptr x10 f875794a
# #218 crash 2, f_700011a7e0 (1.0.0 f_7000108020, -0x127c0): an empty node free list gives a null node at 11acd0;
# the guard continues the id loop at 11ac28.
guard poolguard main 11acd0 11ac28 ptr x8 f9400909
# #317: the edge loop of the job graph builder f_70001114a0 (1.0.0 f_700011d680, +0xc1e0) looks both ends of each
# edge up in the graph's node table (+0x28) and a null target slot faulted at 1116d4 (its pending count at +0x28). The
# guard checks the target before the link slot is taken (ldadd at 111678) and goes to the loop's next edge (1116fc),
# the path the code already takes when the link pool is full.
guard poolguard main 111678 1116fc ptr x12 b82b01ce

# #136 (1.0.0 main+0x1692c00): the call to f_7000474574 (1.0.0 f_7000534324) in the 3.2.0 walk, skipped to the loop tail at 1887e4c.
guard poolguard main 1887df0 1887e4c ptr x0 97afb1e1

# #229: the per-record table loads of f_7000a55520 (1.0.0 f_7000a37520, -0x1e000): the guards of patches-dkb100.txt.
# Opt-out: DKB_FIX_RECORD_GUARD=0.
set recguard DKB_FIX_RECORD_GUARD 1
guard recguard main a55a84 a58388 ptr x10 3dc00140
guard recguard main a55a9c a58388 ptr x11 3dc00160

# #214 fontk (1.0.0 sites in brackets): the text raster at 2x, as in patches-dkb100.txt, which explains the method.
# Placed by hand: the cache class gained a second glyph engine at +0x1ed0 in 3.2.0, so the fill, the width query and
# the cache init are restructured (register allocation, merged blocks) and only the kerning query and the descriptor
# default keep the 1.0.0 words. The +0x1ed0 engine is the descriptor's +0x8c8, which the default (f_7000554b80) clears
# and neither of its callers (553a40, 554578) sets, so only the nn::fontll paths are patched; where a block is shared
# with the +0x1ed0 path (the ascent at 747d70, the fill's multiplier and table loads, the width tail at 886ffc) that
# path is not taken. Off by default; DKB_FONT_SCALE=1 turns it on.
set fontk DKB_FONT_SCALE 0
# SetScale (PLT 1b7b460), lsl #(16 + (size - 97 >> 31)): cache fill (size ldrh'd from node +0x36 into w8) [6f85a0].
site fontk main 747d10 53103d01 51018501 531f7c21 11004021 1ac12101
# Width query f_7000886f00 [86f42c]: the size w22 masked where it is set [86f450], then SetScale [86f4c4].
site fontk main 886f2c 2a0203f6 12003c56   # mov w22, w2 -> and w22, w2, #0xffff
site fontk main 887060 53103ec1 510186c1 531f7c21 11004021 1ac122c1
# Kerning query f_700088724c [86f658], the same words (size w20, masked into the scratch register).
site fontk main 8873d8 53103e81 12003e81 51018421 531f7c21 11004021 1ac12281   # [86f7e4]
site fontk main 887454 53103e81 12003e81 51018421 531f7c21 11004021 1ac12281   # [86f860]
site fontk main 8874dc 53103e81 12003e81 51018421 531f7c21 11004021 1ac12281   # [86f8e8]
# Init f_7000581590 [49e1d0], fontll path: multipliers halved (s1 dead, reloaded at 582018 or written before any read).
site fontk main 5820c0 bd001140 1e2c1001 1e210800 bd001140   # [49ebc8]
site fontk main 5820d0 bd001540 1e2c1001 1e210800 bd001540   # [49ebd8]
# Pixel tables 1c10 and 1c28 stored doubled; w10 and w8 are rewritten by the next instruction.
site fontk main 581c8c 7839796a 531f794a 7839796a   # [49e75c]
site fontk main 581cc0 78397928 531f7908 78397928   # [49e794]
# Ascent (ucvtf of the size ldr'd from node +0x36): shifted left by (size - 97 >> 31). x10 holds the entry here, so
# the scratch is w9 (last read at 747d60, next written at 747d98) and w12 (read at 747d6c, next written at 747da8).
site fontk main 747d70 7e21d821 1e260029 5101852c 531f7d8c 1acc2129 1e230121   # [6f85d0]
# Fill: the multiplier pair ldp s10, s8 times 2^bit through w9 (rewritten at 747f70) and w10 (dead after 747f68).
site fontk main 747f6c 2d42212a 2d42212a 79406f89 4b0903e9 11018129 531f7d29 53092129 1e26014a 0b09014a 1e27014a 1e26010a 0b09014a 1e270148   # [6f87e8]
# Fill: pixel tables shifted right by bit through w11 (not read in the function before its write at 74835c). 748048
# is the 1c10 load of the path that 1.0.0 sends through 6f8950 and 3.2.0 sends past 748120 to 748128.
site fontk main 747fc8 78e8794a 78e8794a 79406f8b 4b0b03eb 1101816b 531f7d6b 1acb294a   # [6f8844]
site fontk main 747fe4 78687929 78e87929 79406f8b 4b0b03eb 1101816b 531f7d6b 1acb2929 12003d29   # [6f8860]
site fontk main 748004 78e87929 78e87929 79406f8b 4b0b03eb 1101816b 531f7d6b 1acb2929   # [6f8890]
site fontk main 748020 78e87929 78e87929 79406f8b 4b0b03eb 1101816b 531f7d6b 1acb2929   # [6f88ac]
site fontk main 748048 78e8794a 78e8794a 79406f8b 4b0b03eb 1101816b 531f7d6b 1acb294a   # [6f8950, other path]
site fontk main 748120 78e8794a 78e8794a 79406f8b 4b0b03eb 1101816b 531f7d6b 1acb294a   # [6f8950]
site fontk main 748140 78687929 78e87929 79406f8b 4b0b03eb 1101816b 531f7d6b 1acb2929 12003d29   # [6f8970]
# Width query: the multiplier times (bit ? 2.0 : 1.0) through w10 (dead after 887004; w8 is live here, unlike 1.0.0)
# and s1 (written at 887018), the 1c10 table shifted right by bit through w10 [86f530, 86f538].
site fontk main 887008 bd401120 bd401120 4b1603ea 1101814a 531f7d4a 5309214a 3209194a 1e270141 1e210800
site fontk main 887010 78f47929 78f47929 4b1603ea 1101814a 531f7d4a 1aca2929
# Kerning query multipliers [86f830, 86f8ac, 86f94c, 86f9b8].
site fontk main 887424 bd401128 bd401128 12003e89 4b0903e9 11018129 531f7d29 53092129 32091929 1e270121 1e210908
site fontk main 8874a0 bd401128 bd401128 12003e89 4b0903e9 11018129 531f7d29 53092129 32091929 1e270121 1e210908
site fontk main 887540 bd401100 bd401100 12003e88 4b0803e8 11018108 531f7d08 53092108 32091908 1e270102 1e220800
site fontk main 8875ac bd401100 bd401100 12003e88 4b0803e8 11018108 531f7d08 53092108 32091908 1e270102 1e220800
# Raster work buffer x4 in the descriptor default [473cf0]: one engine, as on 1.0.0. f_7000554b80's callers are the
# two paths of one builder: 553a40 in it and 554578 in the font-list parser f_7000554520 it calls.
site fontk main 554e90 52a00109 52a00409
# Glyph cache texture x2 each way [49e4a0, 49e4ac]; w9 is stored to +0x28/+0x2c and rewritten.
site fontk main 5818b8 b9400369 b9400369 531f7929
site fontk main 5818c8 b9400769 b9400769 531f7929

# Native anti-aliasing (docs/dkb-testing.md, scene_res): the game's upscaler level, in its settings at +0x10, is 0
# while the 3D is drawn at the output size, and the upscale step (f_7000325a80, called from the frame step
# f_7000323504 every frame) then skips its dispatch; at a scaled 3D the level is 2. Read as 2 at both of the step's
# loads, the dispatch runs at full size too, from the 3D to an image of the same size, and a temporal upscaler takes it
# as anti-aliasing alone. The dispatch is issued by f_7000325f30, which runtime/dkb_wide.c dkb_native_aa_issued_by
# names so that vk_exec_draw.c game_upscaler takes a same-size dispatch from there and from nowhere else. The runtime
# turns the set on (dkb_native_aa_live) with FSR 3.1, FSR 4 or DLSS chosen and scene_res = full. 3.2.0 addresses only.
set dkbnative DKB_NATIVE_AA 0
site dkbnative main 325bb0 b94012c8 52800048  # ldr w8, [x22, #0x10] -> mov w8, #2
site dkbnative main 325d10 b9401289 52800049  # ldr w9, [x20, #0x10] -> mov w9, #2
