#!/usr/bin/env python3 """Audit actual fitted timing paths without treating empty scopes as passes.""" import argparse,hashlib,json,re,subprocess from pathlib import Path from timing_coverage import parse_ucp,classify ROOT=Path(__file__).resolve().parents[1] AUDITS=('bh_mpi_timing','nmi_timing_paths','uart_timing_paths','reset_cpu_timing_paths','video_timing_paths','ssc_integrated_timing','external_io_timing') def sha(p):return hashlib.sha256(p.read_bytes()).hexdigest() def main(): p=argparse.ArgumentParser(description=__doc__) p.add_argument('--quartus',type=Path,required=True,help='quartus_sta 17.0.2') p.add_argument('--build',type=Path,default=ROOT/'build/fpga') p.add_argument('--output',type=Path,default=ROOT/'build/timing') p.add_argument('--resume',action='store_true',help='Reuse complete native logs; recheck all report hashes/paths') a=p.parse_args();a.build=a.build.resolve();a.output=a.output.resolve() if not a.output.is_relative_to(ROOT/'build'):p.error('Use an output inside build/') build=json.loads((a.build/'build.json').read_text());project=a.build/'project' if not build.get('passed'):raise RuntimeError('Full fit with nonnegative summary required') for n,h in build['inputs'].items(): if sha(project/n)!=h or sha(ROOT/n)!=h:raise RuntimeError('Fitted/current input differs: '+n) identity=dict(rbf_sha256=build['rbf_sha256'],inputs=build['inputs'], fitted_sof_sha256=sha(project/'output_files/CoCo3.sof'), scripts={name:sha(ROOT/'scripts'/(name+'.tcl')) for name in AUDITS}) if a.output.exists() and not a.resume:raise RuntimeError('Preserve earlier diagnostics; choose a fresh output') a.output.mkdir(parents=True,exist_ok=True) identity_file=a.output/'audit-input.json' if a.resume: if not identity_file.is_file() or json.loads(identity_file.read_text())!=identity:raise RuntimeError('Resumed audit does not match the fitted build and original scripts') else:identity_file.write_text(json.dumps(identity,indent=2)+'\n') results={} for script in AUDITS: out=a.output/script;log=a.output/(script+'.log') print('AUDIT '+script,flush=True) if not a.resume: if log.exists() or out.exists():raise RuntimeError('Existing partial audit: '+script) with log.open('w') as f:subprocess.run([str(a.quartus.resolve()),'-t',str(ROOT/'scripts'/(script+'.tcl')),str(project/'CoCo3'),'CoCo3',str(out)],stdout=f,stderr=subprocess.STDOUT,cwd=ROOT,check=True) text=log.read_text(errors='replace') if 'Quartus Prime TimeQuest Timing Analyzer was successful. 0 errors' not in text:raise RuntimeError('Incomplete native audit: '+script) reports={};empty=[] for path in sorted(out.glob('*.rpt')): name=path.name if not re.search(r'-(setup|hold|recovery|removal)\.rpt$',name):continue if script=='external_io_timing': expected=['sdram_dq-in','sdram_dq-out','sdram_command-out','hdmi-scaled','hdmi-direct','hdmi_audio-out','mcp_i2c-in','mcp_i2c-out','pixel-in','pixel-out','global'] if not any(name.startswith(n+'-') for n in expected):continue values=[float(x) for x in re.findall(r'Worst case slack is ([-\d.]+)',path.read_text(errors='replace'))] if any(x<0 for x in values):raise RuntimeError('Negative fitted timing: '+script+'/'+name) if not values: # Only asynchronous checks on synchronous-only subscopes may be empty. if name.startswith('global-') or not re.search(r'-(recovery|removal)\.rpt$',name):raise RuntimeError('Empty substantive timing scope: '+script+'/'+name) empty.append(name) reports[name]=dict(slack_ns=min(values) if values else None,sha256=sha(path)) if script=='bh_mpi_timing' and len(reports)!=34:raise RuntimeError('MPI report inventory incomplete') if script=='external_io_timing' and len(reports)!=48:raise RuntimeError('External report inventory incomplete') if script=='ssc_integrated_timing' and len(reports)!=144:raise RuntimeError('SSC report inventory incomplete') if script=='video_timing_paths' and len(reports)!=24:raise RuntimeError('Video report inventory incomplete') if not reports:raise RuntimeError('No substantive report inventory: '+script) results[script]=dict(reports=reports,empty_synchronous_async_checks=empty,log_sha256=sha(log),script_sha256=sha(ROOT/'scripts'/(script+'.tcl'))) if sha(project/'output_files/CoCo3.rbf')!=build['rbf_sha256']:raise RuntimeError('RBF changed during audit') ucp=a.output/'external_io_timing/unconstrained.rpt' counts,inventory=parse_ucp(ucp) contracts={direction:{name:classify(name) for name in pins} for direction,pins in inventory.items()} if any('UNCLASSIFIED' in value for pins in contracts.values() for value in pins.values()):raise RuntimeError('Unclassified remaining pin contract') receipt=dict(audit_identity=identity,unconstrained_counts=counts,remaining_pin_contracts=contracts,coverage_sha256=sha(ucp),passed=True,rbf_sha256=build['rbf_sha256'],inputs=build['inputs'],audits=results, scope='Both-corner fitted path audits under conditional SDRAM/PCB profile; not complete physical-board qualification',whole_board_electrically_qualified=False) (a.output/'result.json').write_text(json.dumps(receipt,indent=2)+'\n') print('PASS '+str(sum(len(v['reports']) for v in results.values()))+' fitted report scopes') if __name__=='__main__':main()