import re,json
d=open('/home/jdc/.config/retroarch/roms/claude/NHL 2026 - 94 Edition v5.1W.bin','rb').read()
def bcd(b): return (b>>4)<10 and (b&15)<10
P=[]; i=0x1d0000
END=0x1f0000
while i<END:
    L=int.from_bytes(d[i:i+2],'big')
    if 6<=L<=40 and L%2==0:
        nm=d[i+2:i+L]; nm2=nm.rstrip(b'\x00')
        if len(nm)-len(nm2)<=1 and len(nm2)>=4 and all(32<=c<127 for c in nm2) and b' ' in nm2 and bcd(d[i+L]):
            P.append(dict(off=i,name=nm2.decode(),jer=f"{d[i+L]:02x}",raw=d[i+L+1:i+L+8].hex(),end=i+L+8))
            i=i+L+8; continue
    i+=2
# group into teams: consecutive players (next starts at prev end)
teams=[];cur=[]
for p in P:
    if cur and p['off']!=cur[-1]['end']: teams.append(cur); cur=[]
    cur.append(p)
teams.append(cur)
out=[]
for t in teams:
    e=t[-1]['end']; tail=d[e:e+40]
    m=re.search(rb'\x00\x02\x00.([A-Za-z .]+)',tail)
    out.append(dict(team=(m.group(1).decode().strip('\x00') if m else '?'),players=t))
json.dump(out,open('romplayers.json','w'),indent=0)
for t in out: print(t['team'],len(t['players']),hex(t['players'][0]['off']))
