#!/usr/bin/env python3
# Static 6502 code discovery for an overlay image: follows jsr/jmp/branches
# from the given entry points (plus optional executed-PC seeds) and prints
# code ranges, data gaps and the out-of-window call targets.
# usage: codeMap.py image.bin base_hex entry_hex[,entry_hex...] [pcs.txt]
import sys
img=open(sys.argv[1],'rb').read(); base=int(sys.argv[2],16)
entries=[int(x,16) for x in sys.argv[3].split(',')]
seeds=set()
if len(sys.argv)>4:
for line in open(sys.argv[4]):
v=int(line.strip().lstrip('$'),16)
if base<=v treat as data
starts.add(pc)
for i in range(n): code.add(pc+i)
if op in br:
t=pc+2+((rd(pc+1)^0x80)-0x80); work.append(t); pc+=2; continue
if op==0x20 or op==0x4C:
t=rd(pc+1)|(rd(pc+2)<<8)
if base<=tprev: gaps.append((prev,a-1))
prev=b+1
if prev=0))
print('external targets:',' '.join('%04X'%t for t in sorted(ext)))