    .ifndef WITH_REFINEMENTS
    .equ WITH_REFINEMENTS,0
    .endif
    .ifndef WITH_PLAN_AIM_HINTS
    .equ WITH_PLAN_AIM_HINTS,1
    .endif
    .equ PLAN_AIM_CLEAR,16
    .equ PLAN_AIM_IMPROVE,8
| Optional Hard CPU shot plan. Native pass/carry/assignment/physics remain active.
| Only own option F810 and bounded transient F840..F847 are written.
    .equ VALUE,0xFFFFF810
    .equ MAGIC,0xFFFFF840
    .equ ACTOR,0xFFFFF842
    .equ AGE,0xFFFFF844
    .equ SIDE,0xFFFFF846
    .equ LIMIT,96
    .text
    .globl _start
_start:
    bra.w commit
    bra.w target
    bra.w decide
    bra.w geometry
    bra.w load
    bra.w put
    bra.w reset
    bra.w clear

| D0 boolean. Internal scratch D1/D2/A0; callers preserve their full contracts.
guard:
    moveq #0,%d0
    cmpi.w #1,VALUE
    bne.w guard_done
    cmpi.w #1,0xFFFFF07A
    bne.w guard_done
    btst #3,0x62(%a3)
    bne.w guard_done
    btst #5,0x62(%a3)
    bne.w guard_done
    move.b 0x63(%a3),%d1
    andi.b #5,%d1
    bne.w guard_done
    tst.w 0x34(%a3)
    ble.w guard_done
    cmpi.w #5,0x34(%a3)
    bhi.w guard_done
    move.b 0xFFFFC2EA,%d1
    andi.b #0x51,%d1
    bne.w guard_done
    move.b 0xFFFFC2EC,%d1
    andi.b #0x81,%d1
    bne.w guard_done
    btst #7,0xFFFFC2EE
    bne.w guard_done
    btst #2,0xFFFFC2F2
    bne.w guard_done
    move.b 0xFFFFC2FA,%d1
    andi.b #5,%d1
    bne.w guard_done
    tst.w 0xFFFFD412
    bne.w guard_done
    move.w 0xFFFFF738,%d1
    subi.w #0xA502,%d1
    cmpi.w #5,%d1
    bcs.w guard_done
    move.w 0x52(%a3),%d1
    cmpi.w #11,%d1
    bhi.w guard_done
    cmp.w 0xFFFFB7AA,%d1
    bne.w guard_done
    lea 0xFFFFC328,%a0
    moveq #3,%d2
human_pad:
    tst.w (%a0)
    beq.s next_pad
    cmp.w -8(%a0),%d1
    beq.s guard_done
next_pad:
    addq.w #2,%a0
    dbra %d2,human_pad
    moveq #1,%d0
guard_done:
    rts

| D0=1 only for a covered ordinary attacking shot in the weak middle distance.
| All other GPR/SP, incoming X/IRQ and native BEDC are preserved; no RNG/writes.
geometry:
    move.w %sr,-(%sp)
    movem.l %d1-%d7/%a0-%a6,-(%sp)
    move.w 0xFFFFBEDC,-(%sp)
    bsr.w guard
    tst.w %d0
    beq.w geometry_no
    btst #1,0x64(%a3)
    bne.w geometry_no
    btst #3,0x64(%a3)
    bne.w geometry_no             | existing native one-timer/buffered paths remain responsible
    move.w 0xFFFFB75E,%d5
    btst #7,0x62(%a3)
    bne.s normalized
    neg.w %d5
normalized:
    cmpi.w #88,%d5
    blt.w geometry_no             | native defensive/neutral clearances remain available
    cmpi.w #264,%d5
    bge.w geometry_no
    move.w 0xFFFFB74A,%d4
    cmpi.w #-160,%d4
    blt.w geometry_no
    cmpi.w #160,%d4
    bgt.w geometry_no
    jsr 0xC566                    | actual native aim; restored before returning
    bsr.w plan_oracle_policy      | local exact native-target geometry, available in legacy ablations
    tst.w %d0
    beq.w geometry_no
    cmpi.w #8,%d1
    bhi.w geometry_no
    cmpi.w #16,%d2
    bcc.w geometry_no             | genuine keeper openings/empty nets retain native shots
    move.w 0xFFFFB75E,%d5        | recompute after native C566; do not assume its scratch ABI
    btst #7,0x62(%a3)
    bne.s selected_depth
    neg.w %d5
selected_depth:
    lsl.w #2,%d1
    lea 0xC542,%a0
    move.w (%a0,%d1.w),%d0       | actual selected native target post X
    sub.w 0xFFFFB74A,%d0
    move.w #264,%d1
    sub.w %d5,%d1
    muls.w %d0,%d0
    muls.w %d1,%d1
    add.l %d1,%d0
    jsr 0x110BE                   | same native distance square-root as actual C3FC release
    cmpi.w #160,%d0
    bcc.s geometry_no
    cmpi.w #40,%d0
    bls.s geometry_no
    moveq #1,%d0
    bra.s geometry_restore
geometry_no:
    moveq #0,%d0
geometry_restore:
    move.w (%sp)+,0xFFFFBEDC
    movem.l (%sp)+,%d1-%d7/%a0-%a6
    move.w (%sp)+,%sr
    tst.w %d0
    rts

| Pure decision. Bounded observations are counted only by actual target calls.
decide:
    move.w %sr,-(%sp)
    move.l %d1,-(%sp)
    bsr.w geometry
    tst.w %d0
    beq.s decided
    cmpi.w #0x4150,MAGIC
    bne.s decided
    move.w 0x52(%a3),%d1
    cmp.w ACTOR,%d1
    bne.s decided
    cmpi.w #1,SIDE
    beq.s age_check
    cmpi.w #-1,SIDE
    bne.s decided
age_check:
    cmpi.w #LIMIT,AGE
    bne.s decided
    moveq #0,%d0                 | native fallback remains latched for this observed owner
 decided:
    move.l (%sp)+,%d1
    move.w (%sp)+,%sr
    tst.w %d0
    rts

| Actual native shot commitment, before its original caller-return discard.
commit:
    move.w %sr,-(%sp)
    cmpi.w #1,VALUE
    bne.s commit_fast
    movem.l %d0-%d7/%a0-%a6,-(%sp)
    bsr.w decide
    tst.w %d0
    bne.s decline
    movem.l (%sp)+,%d0-%d7/%a0-%a6
    move.w (%sp)+,%sr
    bra.s commit_native
decline:
    movem.l (%sp)+,%d0-%d7/%a0-%a6
    move.w (%sp)+,%sr
    rts                           | genuine original caller continues native pass/carry
commit_fast:
    move.w (%sp)+,%sr
commit_native:
    addq.w #4,%sp                 | displaced original E412
    move.w 0xFFFFB75E,%d0
    jmp 0xE418

| D0=1 only if an actual available opposing SKATER is within24 of the carrier.
| Keeper role0 is excluded; native E12C still does movement/evasion itself.
pressure:
    lea 0xFFFFB04A,%a0
    cmpi.w #6,0x52(%a3)
    bcc.s pressure_base
    adda.w #768,%a0
pressure_base:
    moveq #5,%d7
pressure_next:
    tst.w 0x34(%a0)
    ble.s pressure_skip
    cmpi.w #5,0x34(%a0)
    bhi.s pressure_skip
    btst #2,0x63(%a0)
    bne.s pressure_skip
    move.w (%a0),%d3
    sub.w (%a3),%d3
    cmpi.w #-24,%d3
    blt.s pressure_skip
    cmpi.w #24,%d3
    bgt.s pressure_skip
    move.w 0x14(%a0),%d4
    sub.w 0x14(%a3),%d4
    cmpi.w #-24,%d4
    blt.s pressure_skip
    cmpi.w #24,%d4
    bgt.s pressure_skip
    muls.w %d3,%d3
    muls.w %d4,%d4
    add.l %d4,%d3
    cmpi.l #576,%d3
    bls.s pressured
pressure_skip:
    adda.w #128,%a0
    dbra %d7,pressure_next
    moveq #0,%d0
    rts
pressured:
    moveq #1,%d0
    rts

| Actual carrier target boundary. Only D0/D1 lowwords may be overridden.
target:
    move.w %sr,-(%sp)
    cmpi.w #1,VALUE
    bne.w target_fast
    movem.l %d0-%d7/%a0-%a6,-(%sp)
    bsr.w guard
    tst.w %d0
    beq.w target_restore
    bsr.w geometry
    tst.w %d0
    bne.s plan_wanted
    bsr.w clear
    bra.w target_restore
plan_wanted:
    move.w 0x52(%a3),%d1
    cmpi.w #0x4150,MAGIC
    bne.s initialize
    cmp.w ACTOR,%d1
    bne.s initialize
    cmpi.w #LIMIT,AGE
    bhi.s initialize
    cmpi.w #1,SIDE
    beq.s observed
    cmpi.w #-1,SIDE
    beq.s observed
initialize:
    move.w #0x4150,MAGIC
    move.w %d1,ACTOR
    clr.w AGE
    move.w #1,SIDE
    tst.w (%a3)
    bpl.s observed
    move.w #-1,SIDE
observed:
    cmpi.w #LIMIT,AGE
    bcc.s target_restore
    addq.w #1,AGE
    cmpi.w #LIMIT,AGE
    bcc.s target_restore
    bsr.w pressure
    tst.w %d0
    bne.s target_restore
    move.w 0x14(%a3),%d2
    btst #7,0x62(%a3)
    bne.s carrier_depth
    neg.w %d2
carrier_depth:
    move.w #56,%d3
    move.w #220,%d4
    cmpi.w #216,%d2
    blt.s route_ready
    move.w #28,%d3
    move.w #248,%d4
route_ready:
    muls.w SIDE,%d3
    btst #7,0x62(%a3)
    bne.s route_world
    neg.w %d4
route_world:
    move.w %d3,2(%sp)             | D0 lowword in saved full-register frame
    move.w %d4,6(%sp)             | D1 lowword; native upperwords remain exact
target_restore:
    movem.l (%sp)+,%d0-%d7/%a0-%a6
target_fast:
    move.w (%sp)+,%sr
    lea 0xE12C,%a0                | displaced original PC-relative LEA, same callback
    btst #2,0xFFFFC2F2
    jmp 0xE116

clear:
    move.w %sr,-(%sp)
    clr.l MAGIC
    clr.l MAGIC+4
    move.w (%sp)+,%sr
    rts

record:
    lea 0x209A85,%a0              | native/Full/Light/Mini logical4D42..49
    tst.w 0xFFFFF06C
    beq.s record_done
    move.w 0xFFFFF06A,%d0
    subq.w #1,%d0
    cmpi.w #2,%d0
    bhi.s record_done
    addq.w #1,%d0
    lsl.w #2,%d0
    adda.w %d0,%a0
record_done:
    rts
load:
    move.w %sr,-(%sp)
    movem.l %d0-%d1/%a0,-(%sp)
    .if WITH_REFINEMENTS
    move.w #1,VALUE
    .else
    clr.w VALUE
    .endif
    bsr.w clear
    bsr.s record
    moveq #0,%d0
    move.b (%a0),%d0
    move.w %d0,%d1
    andi.w #0xFE,%d1
    cmpi.w #0x76,%d1
    bne.s loaded
    not.b %d0
    cmp.b 2(%a0),%d0
    bne.s loaded
    not.b %d0
    andi.w #1,%d0
    move.w %d0,VALUE
loaded:
    movem.l (%sp)+,%d0-%d1/%a0
    move.w (%sp)+,%sr
    rts
put:
    move.w %sr,-(%sp)
    movem.l %d0-%d1/%a0,-(%sp)
    bsr.w record
    moveq #0x76,%d0
    cmpi.w #1,VALUE
    bne.s write
    addq.w #1,%d0
write:
    move.b %d0,(%a0)
    not.b %d0
    move.b %d0,2(%a0)
    lea 0x208001,%a0
    moveq #0,%d0
    move.w #0x1FFD,%d1
sum:
    add.b (%a0),%d0
    addq.l #2,%a0
    dbra %d1,sum
    not.b %d0
    move.b %d0,(%a0)
    not.b %d0
    move.b %d0,2(%a0)
    movem.l (%sp)+,%d0-%d1/%a0
    move.w (%sp)+,%sr
    rts
reset:
    move.w %sr,-(%sp)
    move.w 0xFFFFF06C,-(%sp)
    move.w #1,0xFFFFF06C
    .if WITH_REFINEMENTS
    move.w #1,VALUE
    .else
    clr.w VALUE
    .endif
    bsr.w clear
    bsr.w put
    move.w (%sp)+,0xFFFFF06C
    move.w (%sp)+,%sr
    rts

| Geometry copied from the accepted native-target policy, with every label local.
| When native smart aiming is explicitly ablated, keep its actual native post
| instead of evaluating a post that the release would never choose. This helper
| never depends on an optional module being present, and never writes its target.
plan_oracle_guard:
 moveq #0,%d0
 cmpi.w #1,0xFFFFF07A
 bne.w plan_oracle_guard_done
 btst #3,0x62(%a3)
 bne.w plan_oracle_guard_done
 btst #2,0x63(%a3)
 bne.w plan_oracle_guard_done
 tst.w 0x34(%a3)
 ble.w plan_oracle_guard_done
 btst #5,0x62(%a3)
 beq.s plan_oracle_guard_context
 cmpi.w #0x7FC,0x58(%a3)
 beq.s plan_oracle_guard_context
 cmpi.w #0x92E,0x58(%a3)
 bne.w plan_oracle_guard_done
plan_oracle_guard_context:
 btst #0,0xFFFFC2EA
 bne.w plan_oracle_guard_done
 btst #4,0xFFFFC2EA
 bne.w plan_oracle_guard_done
 btst #7,0xFFFFC2EC
 bne.w plan_oracle_guard_done
 btst #7,0xFFFFC2EE
 bne.w plan_oracle_guard_done
 btst #2,0xFFFFC2F2
 bne.w plan_oracle_guard_done
 move.b 0xFFFFC2FA,%d0
 andi.w #5,%d0
 bne.s plan_oracle_guard_no
 move.w 0xFFFFF738,%d0
 subi.w #0xA502,%d0
 cmpi.w #5,%d0
 bcs.s plan_oracle_guard_no                    | native automatic replay can have C2EC bit7 clear
 move.w 0xFFFFB7AA,%d0
 cmpi.w #11,%d0
 bhi.s plan_oracle_guard_no
 cmp.w 0x52(%a3),%d0
 bne.s plan_oracle_guard_no
 moveq #1,%d0
 rts
plan_oracle_guard_no:
 moveq #0,%d0
plan_oracle_guard_done:
 rts

| Native BEDC input; D0 eligible, D1 selected index, D2 selected ray separation.
| D3-D7/A0-A6 preserved. 7FFF means no keeper between puck and goal line.
plan_oracle_policy:
 link.w %a6,#-24
 movem.l %d3-%d7/%a0-%a5,-(%sp)
 moveq #0,%d1
 move.w 0xFFFFBEDC,%d1
 move.w #0x7FFF,%d2
 cmpi.w #8,%d1
 bhi.w plan_oracle_policy_no
 bsr.w plan_oracle_guard
 tst.w %d0
 beq.w plan_oracle_policy_no
 move.w 0xFFFFB74A,-2(%a6)
 move.w 0xFFFFB75E,%d3
 btst #7,0x62(%a3)
 bne.s plan_oracle_puck_depth
 neg.w %d3
plan_oracle_puck_depth:
 move.w %d3,-4(%a6)
 cmpi.w #264,%d3
 bge.w plan_oracle_behind_goal
 cmpi.w #88,%d3
 blt.w plan_oracle_policy_yes                 | retain native clears outside attacking plan_oracle_policy
 move.w #264,%d4
 sub.w %d3,%d4
 move.w %d4,-6(%a6)
 move.w %d1,%d3
 lsl.w #2,%d3
 lea 0xC542,%a0
 move.w 2(%a0,%d3.w),-16(%a6)      | keep the actual native chosen shot height
 lea 0xFFFFB04A,%a0
 cmpi.w #6,0x52(%a3)
 bcc.s plan_oracle_keeper_base
 adda.w #768,%a0
plan_oracle_keeper_base:
 moveq #5,%d7
plan_oracle_keeper_scan:
 tst.w 0x34(%a0)
 bne.s plan_oracle_keeper_next
 btst #2,0x63(%a0)
 bne.s plan_oracle_keeper_next
 moveq #0,%d3
 move.b 0x28(%a0),%d3
 ext.w %d3
 asr.w #1,%d3
 add.w (%a0),%d3
 move.w %d3,-8(%a6)
 moveq #0,%d4
 move.b 0x2A(%a0),%d4
 ext.w %d4
 asr.w #1,%d4
 add.w 0x14(%a0),%d4
 btst #7,0x62(%a3)
 bne.s plan_oracle_keeper_depth
 neg.w %d4
plan_oracle_keeper_depth:
 sub.w -4(%a6),%d4
 ble.w plan_oracle_policy_yes
 cmp.w -6(%a6),%d4
 bhi.w plan_oracle_policy_yes
 move.w %d4,-10(%a6)
 bra.s plan_oracle_evaluate
plan_oracle_keeper_next:
 adda.w #128,%a0
 dbra %d7,plan_oracle_keeper_scan
 bra.w plan_oracle_policy_yes
plan_oracle_evaluate:
 move.w %d1,%d3
 lsl.w #2,%d3
 lea 0xC542,%a0
 move.w (%a0,%d3.w),%d3
 bsr.w plan_oracle_ray_gap
 move.w %d3,%d2                  | actual native target's current opening
 move.w %d2,%d6                  | best target, retaining native on ties
 move.w %d1,%d7
    .if WITH_PLAN_AIM_HINTS
 moveq #-16,%d5
 moveq #1,%d4
plan_oracle_post:
 move.w %d5,%d3
 bsr.w plan_oracle_ray_gap
 cmpi.w #PLAN_AIM_CLEAR,%d3
 blt.s plan_oracle_post_next
 cmp.w %d6,%d3
 ble.s plan_oracle_post_next
 move.w %d3,%d6
 moveq #3,%d7                    | right low native target
 tst.w %d5
 bpl.s plan_oracle_post_height
 moveq #5,%d7                    | left low native target
plan_oracle_post_height:
 cmpi.w #6,-16(%a6)
 bne.s plan_oracle_post_high
 moveq #2,%d7
 tst.w %d5
 bpl.s plan_oracle_post_next
 moveq #6,%d7
 bra.s plan_oracle_post_next
plan_oracle_post_high:
 cmpi.w #12,-16(%a6)
 bne.s plan_oracle_post_next
 moveq #1,%d7
 tst.w %d5
 bpl.s plan_oracle_post_next
 moveq #7,%d7
plan_oracle_post_next:
 neg.w %d5
 dbra %d4,plan_oracle_post
 move.w %d2,%d3
 addi.w #PLAN_AIM_IMPROVE,%d3
 cmp.w %d3,%d6
 blt.s plan_oracle_policy_yes
 move.w %d7,%d1
 move.w %d6,%d2
    .endif
plan_oracle_policy_yes:
 moveq #1,%d0
 bra.s plan_oracle_policy_return
plan_oracle_behind_goal:
 moveq #0,%d2
 bra.s plan_oracle_policy_yes
plan_oracle_policy_no:
 moveq #0,%d0
plan_oracle_policy_return:
 movem.l (%sp)+,%d3-%d7/%a0-%a5
 unlk %a6
 rts

plan_oracle_ray_gap:
 sub.w -2(%a6),%d3
 muls.w -10(%a6),%d3
 divs.w -6(%a6),%d3
 add.w -2(%a6),%d3
 sub.w -8(%a6),%d3
 bpl.s plan_oracle_gap_return
 neg.w %d3
plan_oracle_gap_return:
 rts
