| Existing signed Momentum values: one24x3 bar BELOW each native team name.
| Native score box grows one row upward; labels/goals stay native. Reuse six
| retired crowd tiles7E5..7EA and192B of the original in-play crowd bufferE900.
| F8E0/2 hold frozen away/home keys; no additional RAM/VRAM allocation.
 .equ FLAGS,0xFFFFF8DE
 .equ KEYS,0xFFFFF8E0
 .equ BUFFER,0xFFFFE900
 .equ SIDEA,0xFFFFF72E
 .equ SIDEH,0xFFFFF72C
 .equ VDATA,0xC00000
 .equ VCTRL,0xC00004
 .text
 .globl _start
_start:
 bra.w draw
 bra.w clear
 bra.w score_available

draw:
 move.w %sr,-(%sp)
 movem.l %d0-%d7/%a0-%a2,-(%sp)
 ori.w #0x700,%sr
 move.b 0xFFFFC2EC,%d0
 andi.b #0x81,%d0                  | native pause/foreign view owns its menu buffer
 bne.w foreign
 btst #4,0xFFFFC2EA                | native between-period highlights show other teams
 bne.w foreign
 cmpi.w #1,0xFFFFF84A
 beq.w disable
 cmpi.w #0x4352,0xFFFFF700
 bne.w foreign
 move.w 0xFFFFF738,%d0
 subi.w #0xA502,%d0
 cmpi.w #4,%d0
 bls.w replay
 cmpi.w #5,%d0
 beq.w foreign
 bsr.w score_available
 tst.w %d0
 beq.w foreign
 btst #1,FLAGS+1
 beq.s compose_live
 move.w SIDEA,%d0
 cmp.w KEYS,%d0
 bne.s compose_live
 move.w SIDEH,%d0
 cmp.w KEYS+2,%d0
 beq.w publish
compose_live:
 move.w SIDEA,KEYS
 move.w SIDEH,KEYS+2
 bra.s compose
replay:
 btst #0,FLAGS+1                    | preserve actual pre-replay values
 beq.w foreign
 bsr.w score_available
 tst.w %d0
 beq.w foreign
 | Replay/menu decompression may reuse E900. Recompose from dedicated frozen
 | keys each replay frame instead of trusting the transient old crowd buffer.
compose:
 lea BUFFER,%a0
 move.w KEYS,%d6
 bsr.w compose_team
 move.w KEYS+2,%d6
 bsr.w compose_team
 move.w #1,FLAGS
publish:
 move.l #0x00FFE900,(%a5)+
 move.w #96,(%a5)+
 move.w #0xFCA0,(%a5)+
 move.w #0x8F02,VCTRL
 move.l #0x6C420003,VCTRL            | away: row24 cols33..35 below row23 name
 move.w #0xA7E5,VDATA
 move.w #0xA7E6,VDATA
 move.w #0xA7E7,VDATA
 move.l #0x6D420003,VCTRL            | home: row26 cols33..35 below row25 name
 move.w #0xA7E8,VDATA
 move.w #0xA7E9,VDATA
 move.w #0xA7EA,VDATA
 bset #1,FLAGS+1
 bra.s done
disable:
 bsr.w clear_owned
 bra.s done
foreign:
 clr.w FLAGS                       | force fresh composition after any native foreign view
done:
 movem.l (%sp)+,%d0-%d7/%a0-%a2
 move.w (%sp)+,%sr
 rts

compose_team:
 moveq #0,%d4
compose_tile:
 move.l #0x77777777,(%a0)+
 move.l #0x77777777,(%a0)+
 move.w %d6,%d0
 bsr.s bar_row
 move.l %d0,(%a0)+
 move.l %d0,(%a0)+
 move.l %d0,(%a0)+
 move.l #0x77777777,(%a0)+
 move.l #0x77777777,(%a0)+
 move.l #0x77777777,(%a0)+
 addq.w #8,%d4
 cmpi.w #24,%d4
 bcs.s compose_tile
 rts

| D0 signed momentum, D4 tile x0/8/16 -> packed8pixel row. Existing neutral-half
| range and negative-red/nonnegative-white encoding; no event/scaling changes.
bar_row:
 moveq #1,%d3
 tst.w %d0
 bpl.s colour_ready
 moveq #10,%d3
colour_ready:
 cmpi.w #-1000,%d0
 bge.s lower_bound
 move.w #-1000,%d0
lower_bound:
 cmpi.w #1000,%d0
 ble.s upper_bound
 move.w #1000,%d0
upper_bound:
 addi.w #1000,%d0
 mulu.w #22,%d0
 divu.w #2000,%d0
 move.w %d0,%d2
 move.w %d4,%d1
 moveq #7,%d7
 moveq #0,%d0
pixel:
 lsl.l #4,%d0
 cmpi.w #1,%d1
 blo.s background
 cmp.w %d2,%d1
 bhi.s background
 or.b %d3,%d0
 bra.s next_pixel
background:
 ori.b #7,%d0
next_pixel:
 addq.w #1,%d1
 dbra %d7,pixel
 rts

| The entire native top/bottom frame must own this area before publishing.
| Ticker/pause/Stars windows are foreign, even if their cells happen to be blank.
score_available:
 move.w #0x8F02,VCTRL
 lea score_frame(%pc),%a0
 move.l #0x2B400003,VCTRL
 moveq #7,%d1
score_top:
 move.w VDATA,%d2
 cmp.w (%a0)+,%d2
 bne.s score_foreign
 dbra %d1,score_top
 move.l #0x2DC00003,VCTRL
 moveq #7,%d1
score_bottom:
 move.w VDATA,%d2
 cmp.w (%a0)+,%d2
 bne.s score_foreign
 dbra %d1,score_bottom
 moveq #1,%d0
 rts
score_foreign:
 moveq #0,%d0
 rts
score_frame:
 .word 0xA3FF,0xA400,0xA400,0xA400,0xA400,0xA400,0xA400,0xA401
 .word 0xA405,0xA406,0xA406,0xA406,0xA406,0xA406,0xA406,0xA407

clear:
 move.w %sr,-(%sp)
 movem.l %d0-%d3/%a0-%a1,-(%sp)
 ori.w #0x700,%sr
 bsr.s clear_owned
 movem.l (%sp)+,%d0-%d3/%a0-%a1
 move.w (%sp)+,%sr
 rts
clear_owned:
 btst #1,FLAGS+1
 beq.w clear_done
 move.w #0x8F02,VCTRL
 move.l #0x2C420003,VCTRL
 move.w #0xA7E5,%d1
 moveq #2,%d3
clear_check_away:
 move.w VDATA,%d0
 cmp.w %d1,%d0
 bne.s clear_done
 addq.w #1,%d1
 dbra %d3,clear_check_away
 move.l #0x2D420003,VCTRL
 moveq #2,%d3
clear_check_home:
 move.w VDATA,%d0
 cmp.w %d1,%d0
 bne.s clear_done
 addq.w #1,%d1
 dbra %d3,clear_check_home
 move.l #0x6C420003,VCTRL
 move.w #0xA403,VDATA
 move.w #0xA403,VDATA
 move.w #0xA403,VDATA
 move.l #0x6D420003,VCTRL
 move.w #0xA403,VDATA
 move.w #0xA403,VDATA
 move.w #0xA403,VDATA
clear_done:
 clr.w FLAGS
 rts
