"""Extract the firmware image embedded in a wire capture's 860-boot preamble. The dPL3 host boots the render board by STREAMING the whole .MNG over the wire (VR_COMMS.C boot_860): args860("i860 50MHz and kicking"), one 40-byte code860 packet = the 7-word header {csize,dsize,bsize,cstart,dstart,bstart,entry}, then code860/data860/bss860 segment packets. So every capture carries the EXACT firmware build the game ran -- run THAT for a version-matched replay (sda4/RPLIVE/VREND.MNG is a different build: text 0x39ec0 vs cap7's 0x31440). Writes .mng (12-byte header + text + data, emu860.load_mng format) and .bss (the streamed bss-segment content -- NOT all zeros; load at bstart). Usage: python extract_capfw.py """ import sys, os, struct sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, r'C:\VWE\TeslaRel410\dpl3-revive\patha') from emu_replay import parse_capture from vrboard import A cap = sys.argv[1] if len(sys.argv) > 1 else r'C:\VWE\TeslaRel410\dpl3-revive\patha\cap7.raw.bin' out = sys.argv[2] if len(sys.argv) > 2 else os.path.join( os.path.dirname(os.path.abspath(__file__)), 'capfw7') cmds = parse_capture(cap) code, data, bss, hdr = [], [], [], None for a, pl in cmds: if a == A.code860: if hdr is None and len(pl) == 40: hdr = struct.unpack_from('<7I', pl, 0) continue code.append(pl) elif a == A.data860: data.append(pl) elif a == A.bss860: bss.append(pl) csize, dsize, bsize, cstart, dstart, bstart, entry = hdr text, dat, bs = b''.join(code), b''.join(data), b''.join(bss) assert len(text) == csize and len(dat) == dsize and len(bs) == bsize, \ f"stream/header mismatch: {len(text):#x}/{csize:#x} {len(dat):#x}/{dsize:#x} {len(bs):#x}/{bsize:#x}" with open(out + '.mng', 'wb') as f: f.write(struct.pack(' {out}.mng (+ {out}.bss, {sum(1 for b in bs if b)} nonzero bytes)")