"""Decode verified P2 observations. No hardware timing model is assumed. The manifest is probe_format.json beside this script in the delivery package, or a build's probe_info.json supplied with --manifest. Integer times are FRT /8 ticks; source instructions and measurement overhead remain in the manifest. """ import argparse, hashlib, json, statistics, struct from pathlib import Path from schema import * from codec import capture_crc def u16(b,o):return struct.unpack_from('>H',b,o)[0] def u32(b,o):return struct.unpack_from('>I',b,o)[0] def at(address): if HDR<=address7H',b,4+14*i) item['trials'].append(dict(trigger=trigger,last_busy=busy,first_clear=clear,irq_service=irq, polls=polls,flags=f,irq_status=status,timeout=bool(f&1),missing_irq=bool(f&2), no_transfer=bool(f&4),vblank_intervened=bool(f&8),destination_unchanged=bool(f&16), busy_lower_ticks=None if busy==65535 else (busy-trigger)&65535, clear_upper_ticks=None if f&1 or clear==65535 else (clear-trigger)&65535, irq_delay_ticks=None if f&6 or irq==65535 else (irq-trigger)&65535)) result['dma'].append(item) else: n=u32(b,4);assert n<=64 stamps=[u16(b,16+4*i) for i in range(n)] delta=[(y-x)&65535 for x,y in zip(stamps,stamps[1:])] item.update(count=n,frc=stamps,interrupted_pc_low16=[u16(b,18+4*i) for i in range(n)],deltas=delta, median_ticks=statistics.median(delta) if delta else None, cpu_clocks_per_period=statistics.median(delta)*8 if delta else None) result['frames'].append(item) for i in range(header['live_count']): a=at(LIVE+i*LIVE_SIZE);b=data[a:a+LIVE_SIZE] result['live'].append(dict(frame=u32(b,0),logical_live=u32(b,4),ts=u32(b,8),frc=u16(b,12),ftcsr=b[14],tcr=b[15], native_compare_counter=u32(b,16),stale=u32(b,20),kick=u16(b,24),irq_first=u16(b,26),vblank_end=u16(b,28), sound_duration=u16(b,30),sprite_wait=u16(b,32),irq_duration=u16(b,34),dma_output_units64=u16(b,36), irq_count=b[38],status=b[39],task_entry=[u16(b,40+2*j) for j in range(6)], task_return=[u16(b,52+2*j) for j in range(6)],hashes=[u32(b,64),u32(b,68)],inputs=[u16(b,72+2*j) for j in range(4)])) for i in range(header['event_count']): a=at(EVENTS+i*EVENT_SIZE);packed=u32(data,a+8) result['events'].append(dict(frame=u32(data,a),ts=u32(data,a+4),kind=packed>>24,slot=(packed>>16)&255, saved_sr=packed&65535,continuation=u32(data,a+12))) result['limits']=LIMITS return result def compare(a,b): other={(r['frame'],r['logical_live']):r for r in b['live']};matched=[];mismatch=[];missing=[] for r in a['live']: key=(r['frame'],r['logical_live']);s=other.get(key) if s is None:missing.append(key) elif r['hashes']!=s['hashes'] or r['inputs']!=s['inputs']:mismatch.append(key) else:matched.append(dict(frame=key[0],logical_live=key[1],a_ts=r['ts'],b_ts=s['ts'])) return dict(matched_sampled_states=matched,mismatched_sampled_states=mismatch,missing_in_other=missing, warning='Matched hashes cover only the declared sampled regions. Unmatched records are never aligned by timing or index alone.') def main(): p=argparse.ArgumentParser(description=__doc__);p.add_argument('capture',type=Path);p.add_argument('--manifest',type=Path,default=Path(__file__).with_name('probe_format.json'));p.add_argument('--out',type=Path,required=True);p.add_argument('--compare',type=Path);a=p.parse_args() manifest=json.loads(a.manifest.read_text());report=inspect(a.capture.read_bytes(),manifest) if a.compare:report['comparison']=compare(report,inspect(a.compare.read_bytes(),manifest)) assert not a.out.exists();a.out.write_text(json.dumps(report,indent=2)) print(json.dumps(dict(capture=report['capture_sha256'],error=report['header']['error'],mode=report['header']['trace_policy'], cpu=len(report['cpu']),dma=len(report['dma']),live=len(report['live']),warnings=report['warnings']),indent=2)) if __name__=='__main__':main()