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/shorten-ddr-dqs1.ts
/** Reuse the existing coupled-pair template to remove obsolete overlength tuning. */
import{readFileSync,writeFileSync,mkdirSync}from'node:fs'
import{resolve}from'node:path'
import{createHash}from'node:crypto'
import{auditCompositeHostTopology}from'./ddr-composite-routing-context'
import{fitDqsPairMean}from'./tune-ddr-dqs'
import{auditRouteAngles}from'./check-route-angles'
import{auditDdrTraceJunctions}from'./check-ddr-system-copper'
import{checkHostTerminalContract}from'./route-ddr-sequential'
import{objectHash}from'./improve-ddr-a9-power-launches'
import{verifyDdrCapture}from'./ddr-capture-provenance'
const[capture,parent,out,targetArg='27']=process.argv.slice(2);if(!out)throw Error('Usage: capture current-parent output [byte1-target-mm=27]')
const inputHashes:Record<string,string>={},fh=(p:string)=>createHash('sha256').update(readFileSync(p)).digest('hex'),read=(p:string)=>{p=resolve(p);inputHashes[p]=fh(p);return JSON.parse(readFileSync(p,'utf8'))}
const{captureHash}=verifyDdrCapture(capture),candidate0=read(resolve(parent,'candidate.circuit.json')),host0=read(resolve(parent,'host-bundle.json')),input=read(resolve(parent,'routing-input.json')),prior=read(resolve(parent,'report.json')),target=Number(targetArg),nominalMm=27.3001
if(!Number.isFinite(target)||target<=0||target>nominalMm-.05||prior.candidateCircuitSha256!==objectHash(candidate0)||host0.captureHash!==captureHash)throw Error('Invalid parent or byte1 target')
const hostIds=new Set(host0.traces.map((t:any)=>t.pcb_trace_id)),c={candidate:candidate0,host:host0,input,fixed:candidate0.filter((e:any)=>!hostIds.has(e.pcb_trace_id)),evidence:{captureHash,inputHashes},assertUnchanged:()=>{verifyDdrCapture(capture);for(const[p,h]of Object.entries(inputHashes))if(fh(p)!==h)throw Error('Input changed')}}
if(host0.traces.some((t:any)=>objectHash(t)!==objectHash(candidate0.find((e:any)=>e.type==='pcb_trace'&&e.pcb_trace_id===t.pcb_trace_id))))throw Error('Host bundle differs from candidate')
const connections=c.input.connections.filter((x:any)=>x.ramByte===1&&['DQS_P','DQS_N'].includes(x.ramTerminal)),old=c.host.traces.filter((t:any)=>connections.some((x:any)=>x.name===t.connection_name)),oldIds=new Set(old.map((t:any)=>t.pcb_trace_id))
if(old.length!==2||connections.length!==2)throw Error('Missing original pair')
const planar=(route:any[])=>route.reduce((sum:number,b:any,i:number)=>{const a=route[i-1];return sum+(a?.route_type==='wire'&&b.route_type==='wire'&&a.layer===b.layer?Math.hypot(b.x-a.x,b.y-a.y):0)},0)
const escapeLengths=connections.map((x:any)=>x.pointsToConnect.reduce((sum:number,p:any)=>{const e=c.candidate.find((e:any)=>e.pcb_breakout_point_id===p.pointId),trace=c.candidate.find((t:any)=>t.type==='pcb_trace'&&t.source_trace_id===e.source_trace_id&&t.route.at(-1)?.layer===p.layer&&Math.hypot(t.route.at(-1).x-p.x,t.route.at(-1).y-p.y)<1e-7);if(!trace)throw Error('Missing actual saved escape');return sum+planar(trace.route)},0))
const auditPath=resolve('/Users/ankan/Documents/Codex/2026-09-10/what-x20/outputs/mt41k512m8da-107-it-p-fanout/scripts/audit-am3352-dqs-pair.ts'),fileHash=()=>createHash('sha256').update(readFileSync(auditPath)).digest('hex'),auditHash=fileHash(),{verifyHostDqsPair}=await import(auditPath)
mkdirSync(out,{recursive:true});const write=(n:string,v:any)=>writeFileSync(resolve(out,n),JSON.stringify(v,null,2)+'\n'),attempts:any[]=[];let chosen:any
for(const entryX of[1.9,1.7,1.5]){
const fitted=fitDqsPairMean(connections,escapeLengths,target,entryX,9.6),y=fitted.excursionY,raw=fitted.traces
const traces=raw.map((t:any)=>{const before=old.find((e:any)=>e.pcb_trace_id===t.pcb_trace_id);if(!before||objectHash(before.route[0])!==objectHash(t.route[0])||objectHash(before.route.at(-1))!==objectHash(t.route.at(-1)))throw Error('Pair endpoint or identity changed');return{...before,route:t.route}}),candidate=c.candidate.map((e:any)=>traces.find((t:any)=>t.pcb_trace_id===e.pcb_trace_id)??e),host={...c.host,traces:c.host.traces.map((e:any)=>traces.find((t:any)=>t.pcb_trace_id===e.pcb_trace_id)??e)}
if(traces.some((t:any)=>!oldIds.has(t.pcb_trace_id)))throw Error('Pair IDs changed')
const lengths=traces.map((t:any,i:number)=>planar(t.route)+escapeLengths[i]),actualMean=lengths.reduce((a:number,b:number)=>a+b,0)/2,topology=auditCompositeHostTopology(candidate,host.traces,c.input.connections),allAngles=auditRouteAngles(candidate.filter((e:any)=>e.type==='pcb_trace')),allJunctions=auditDdrTraceJunctions(candidate,c.input.connections),terminal=checkHostTerminalContract(host.traces,c.input.connections.filter((x:any)=>host.traces.some((t:any)=>t.connection_name===x.name))),pair=verifyHostDqsPair({...c.input,connections},c.fixed,traces)
const ok=topology.valid&&allAngles.valid&&allJunctions.valid&&terminal.valid&&pair.status==='pair-geometry-pass'&&Math.abs(actualMean-target)<1e-6&&lengths.every((n:number)=>n<nominalMm)&&topology.physical.connectivity.filter((x:any)=>x.connected).length===c.host.traces.length
const summary={entryX,excursionY:y,accepted:ok,targetMm:target,lengthsMm:lengths,meanMm:actualMean,topologyErrors:topology.errors,physicalErrors:topology.physical.errors,violations:topology.physical.violations,angles:allAngles.violations,junctions:allJunctions.joinedBends,pair,terminal};attempts.push(summary)
if(ok){chosen={candidate,host,traces,topology,allAngles,allJunctions,terminal,summary};break}
}
c.assertUnchanged();if(fileHash()!==auditHash)throw Error('Pair auditor changed')
write('report.json',{captureHash,accepted:!!chosen,nominalMm,targetMm:target,inputHashes,parentCandidateSha256:objectHash(c.candidate),contextEvidence:c.evidence,externalPairAuditorSha256:auditHash,attempts,scope:'Coupled DQS1 shortening below byte1 nominal maximum; byte1 data matching remains outstanding. Complete byte timing remains invalid due to other overlong/short/missing data routes.'})
if(chosen){write('candidate.circuit.json',chosen.candidate);write('host-bundle.json',chosen.host);write('replacements.json',old.map((before:any)=>({before,after:chosen.traces.find((t:any)=>t.pcb_trace_id===before.pcb_trace_id)})));write('physical-report.json',chosen.topology);write('routing-input.json',{...c.input,obstacles:undefined,traces:chosen.host.traces});}
console.log(JSON.stringify({accepted:!!chosen,attempts:attempts.map(a=>({entryX:a.entryX,y:a.excursionY,lengths:a.lengthsMm,errors:a.physicalErrors.length,violations:a.violations.length,topology:a.topologyErrors.length,pair:a.pair.status}))}));if(!chosen)process.exitCode=1