0hmX/am3352

This code suite comprises TypeScript scripts that analyze, verify, and assemble complex DDR memory interface hardware, focusing on physical routing, via and pad placement, electrical clearance, and physical constraints, often involving precise geometric calculations and consistent provenance tracking.

Version
1.0.5
License
unset
Stars
0

scripts/check-ddr-facing-feasibility.ts

/** Independently test one saved planner result on the actual latest composite. */
import {readFileSync,writeFileSync} from 'node:fs'
import {resolve} from 'node:path'
import {createHash} from 'node:crypto'
import {objectHash} from './improve-ddr-a9-power-launches'
import {applyFacingExitReplacements} from './ddr-facing-context'
import {verifyDdrCapture} from './ddr-capture-provenance'
import {verifyDdrSystemCopper,auditDdrTraceJunctions} from './check-ddr-system-copper'
import {auditRouteAngles} from './check-route-angles'
import {explicitFacingViaContinuation} from './ddr-facing-via-continuation'
const [capture,composite,out,mode]=process.argv.slice(2)
if(!capture||!composite||!out)throw Error('Usage: capture latest-composite experiment-output')
if(mode&&mode!=='--invalidate-existing-host')throw Error('Unknown verification mode')
const hashes:Record<string,string>={},hash=(p:string)=>createHash('sha256').update(readFileSync(p)).digest('hex')
const read=(p:string)=>{p=resolve(p);hashes[p]=hash(p);return JSON.parse(readFileSync(p,'utf8'))}
const {captureHash}=verifyDdrCapture(capture),circuit=read(resolve(composite,'candidate.circuit.json')),report=read(resolve(composite,'report.json')),input=read(resolve(composite,'routing-input.json')),host=read(resolve(composite,'host-bundle.json')),evidence=read(resolve(out,'input-evidence.json')),raw=read(resolve(out,'pre-guard-raw.json'))
if(report.captureHash!==captureHash||host.captureHash!==captureHash||evidence.captureHash!==captureHash||report.candidateCircuitSha256!==objectHash(circuit))throw Error('Composite provenance mismatch')
const exit=circuit.find((e:any)=>e.pcb_breakout_point_id===evidence.connection.pointsToConnect[0].pointId)
const before=circuit.find((e:any)=>e.type==='pcb_trace'&&e.source_trace_id===exit.source_trace_id)
const oldBaseline=read(resolve(dirnameOfBaseline(evidence),'candidate.circuit.json')).find((e:any)=>e.pcb_trace_id===before.pcb_trace_id)
if(objectHash(before)!==objectHash(oldBaseline)&&mode!=='--invalidate-existing-host')throw Error('Affected current escape differs from planner baseline')
const viaIndex=before.route.findIndex((p:any)=>p.route_type==='via'),via={...before.route[viaIndex],to_layer:evidence.target.layer}
if(objectHash(before.route.slice(0,viaIndex))!==objectHash(oldBaseline.route.slice(0,viaIndex)))throw Error('Actual pad launch differs from baseline')
const invalidatedHostTraces=host.traces.filter((t:any)=>t.connection_name===evidence.connection.name)
if(mode==='--invalidate-existing-host'&&invalidatedHostTraces.length!==1)throw Error('Expected exactly one explicit host invalidation')
if(mode!=='--invalidate-existing-host'&&invalidatedHostTraces.length)throw Error('Existing affected host was not explicitly invalidated')
const invalidatedIds=new Set(invalidatedHostTraces.map((t:any)=>t.pcb_trace_id))
for(const t of invalidatedHostTraces)if(objectHash(circuit.find((e:any)=>e.pcb_trace_id===t.pcb_trace_id))!==objectHash(t))throw Error('Invalidated host witness differs')
const retainedHostTraces=host.traces.filter((t:any)=>!invalidatedIds.has(t.pcb_trace_id))
const {after,omittedCenterDistanceMm}=explicitFacingViaContinuation(before,raw,evidence.target.layer)
const changes={traces:[{before,after}],breakoutPoints:[{before:exit,after:{...exit,...evidence.target}}]}
const candidate=applyFacingExitReplacements(circuit.filter((e:any)=>!invalidatedIds.has(e.pcb_trace_id)),changes),connections=input.connections.map((c:any)=>({...c,pointsToConnect:c.pointsToConnect.map((p:any)=>p.pointId===exit.pcb_breakout_point_id?{...p,...evidence.target}:p)}))
const fresh=[...retainedHostTraces,after],freshIds=new Set(fresh.map((t:any)=>t.pcb_trace_id)),fixed=candidate.filter((e:any)=>!freshIds.has(e.pcb_trace_id))
const physical=verifyDdrSystemCopper(fixed,fresh,connections,{}),allAngles=auditRouteAngles(candidate.filter((e:any)=>e.type==='pcb_trace')),allJunctions=auditDdrTraceJunctions(candidate)
const planar=(r:any[])=>r.reduce((sum:number,p:any,i:number)=>{const q=r[i-1];return sum+(p.route_type==='wire'&&q?.route_type==='wire'&&p.layer===q.layer?Math.hypot(p.x-q.x,p.y-q.y):0)},0)
const cpuPoint=evidence.connection.pointsToConnect[1],cpuExit=circuit.find((e:any)=>e.pcb_breakout_point_id===cpuPoint.pointId),cpuTrace=circuit.find((e:any)=>e.type==='pcb_trace'&&e.source_trace_id===cpuExit.source_trace_id)
const dx=Math.abs(cpuPoint.x-evidence.target.x),dy=Math.abs(cpuPoint.y-evidence.target.y),dqlm=read(resolve(dirnameOfBaseline(evidence),'dqlm-audit.json'))
if(dqlm.captureHash!==captureHash)throw Error('DQLM provenance mismatch')
const nominalMm=dqlm.bytes.find((b:any)=>b.byte===evidence.connection.ramByte).dqlmPairedDataMaximumMm
const lengthBudget={ramEscapeMm:planar(after.route),cpuEscapeMm:planar(cpuTrace.route),hostFloorMm:Math.max(dx,dy)+(Math.SQRT2-1)*Math.min(dx,dy),nominalMm,optimisticTotalMm:0}
lengthBudget.optimisticTotalMm=lengthBudget.ramEscapeMm+lengthBudget.cpuEscapeMm+lengthBudget.hostFloorMm
const seedPreserved=retainedHostTraces.every((t:any)=>objectHash(candidate.find((e:any)=>e.pcb_trace_id===t.pcb_trace_id))===objectHash(t))
const hostConnected=retainedHostTraces.every((t:any)=>physical.connectivity.some((c:any)=>c.name===t.connection_name&&c.connected))
const accepted=seedPreserved&&hostConnected&&!physical.errors.length&&!physical.violations.length&&physical.angles.valid&&!physical.joinedBends.length&&allAngles.valid&&allJunctions.valid&&lengthBudget.optimisticTotalMm<=nominalMm+1e-7
verifyDdrCapture(capture);for(const[p,h]of Object.entries(hashes))if(hash(p)!==h)throw Error(`Changed input ${p}`)
const result={captureHash,accepted,signal:evidence.signal,target:evidence.target,via,omittedCenterDistanceMm,lengthBudget,seedPreserved,hostConnected,retainedHostCount:retainedHostTraces.length,invalidatedHostTraces,inputFileHashes:hashes,baseline:evidence.parentContext.path,parentCompositeSha256:objectHash(circuit),physical,allAngles,allJunctions,scope:'Local escape feasibility only. Baseline-planned geometry independently checked against latest complete composite; no host continuation, timing matching or SI qualification.'}
writeFileSync(resolve(out,'latest-composite-check.json'),JSON.stringify(result,null,2)+'\n')
writeFileSync(resolve(out,'explicit-via-center-replacement.json'),JSON.stringify(changes,null,2)+'\n')
if(accepted){writeFileSync(resolve(out,'latest-composite-candidate.circuit.json'),JSON.stringify(candidate,null,2)+'\n');writeFileSync(resolve(out,'host-bundle.json'),JSON.stringify({...host,traces:retainedHostTraces},null,2)+'\n')}
console.log(JSON.stringify({accepted,lengthBudget,errors:physical.errors.length,violations:physical.violations.length,angles:allAngles.valid,joins:allJunctions.joinedBends.length}))
function dirnameOfBaseline(e:any){return resolve(e.parentContext.path,'..')}