    .ifndef WITH_REFINEMENTS
    .equ WITH_REFINEMENTS,0
    .endif
| Optional painted-dot alignment after the native stoppage point policy.
| Runtime writes only BF94/BF96; option F848, own preference4D4A..4D51.
    .equ VALUE,0xFFFFF848
    .text
    .globl _start
_start:
    bra.w commit
    bra.w geometry
    bra.w load
    bra.w put
    bra.w reset

| Displaced native1255A: BSR1257E; CLR.WC3E6. Native corrections run first.
| All native GPR/SP/X/IRQ contracts survive; final CLR supplies native NZVC.
commit:
    jsr 0x1257E
    cmpi.w #1,VALUE
    bne.s committed
    tst.w 0xFFFFF090             | actual tied-overtime shootout state
    bne.s committed
    btst #0,0xFFFFC2FA           | native standalone shootout
    bne.s committed
    btst #2,0xFFFFC2FA           | native special attempt (F2A8 sets bit2)
    bne.s committed
    movem.l %d0-%d1,-(%sp)
    move.w 0xFFFFBF94,%d0
    move.w 0xFFFFBF96,%d1
    bsr.w geometry
    move.w %d0,0xFFFFBF94
    move.w %d1,0xFFFFBF96
    movem.l (%sp)+,%d0-%d1
committed:
    clr.w 0xFFFFC3E6
    rts

| Pure signed lowword X/Y -> nearest painted point in native chosen region.
| Upperwords, all other GPR/SP/SR and all RAM/RNG are preserved.
geometry:
    move.w %sr,-(%sp)
    movem.l %d2-%d7/%a0-%a1,-(%sp)
    cmpi.w #-70,%d0
    blt.w geometry_done
    cmpi.w #70,%d0
    bgt.w geometry_done
    cmpi.w #-206,%d1
    blt.w geometry_done
    cmpi.w #206,%d1
    bgt.w geometry_done
    lea neutral(%pc),%a0
    moveq #4,%d6
    cmpi.w #166,%d1
    bge.s end_positive
    cmpi.w #-166,%d1
    bgt.s region_ready
    lea negative_end(%pc),%a0
    moveq #1,%d6
    bra.s region_ready
end_positive:
    lea positive_end(%pc),%a0
    moveq #1,%d6
region_ready:
    move.w %d0,%d2
    move.w %d1,%d3
    movea.l %a0,%a1
    move.l #0x7FFFFFFF,%d7
candidate:
    move.w (%a0),%d4
    sub.w %d2,%d4
    muls.w %d4,%d4
    move.w 2(%a0),%d5
    sub.w %d3,%d5
    muls.w %d5,%d5
    add.l %d5,%d4
    cmp.l %d7,%d4
    bcc.s next_candidate
    move.l %d4,%d7
    movea.l %a0,%a1
next_candidate:
    addq.l #4,%a0
    dbra %d6,candidate
    move.w (%a1),%d0
    move.w 2(%a1),%d1
geometry_done:
    movem.l (%sp)+,%d2-%d7/%a0-%a1
    move.w (%sp)+,%sr
    rts

| Native end region stays in that end; neutral policy uses five neutral dots.
| Equal distances keep first entry: centre, then +Y/right/left,-Y/right/left.
neutral:
    .word 0,0,70,65,-70,65,70,-65,-70,-65
positive_end:
    .word 70,206,-70,206
negative_end:
    .word 70,-206,-70,-206

record:
    lea 0x209A95,%a0              | native/Full/Light/Mini logical4D4A..51
    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.s record
    moveq #0,%d0
    move.b (%a0),%d0
    move.w %d0,%d1
    andi.w #0xFE,%d1
    cmpi.w #0x78,%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:
    .if WITH_REFINEMENTS
    move.w #1,VALUE                   | permanent user-selected rule; old saved Off cannot disable it
    .endif
    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 #0x78,%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 put
    move.w (%sp)+,0xFFFFF06C
    move.w (%sp)+,%sr
    rts
