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/retune-ddr-dq4-ordered-mask.ts
/** Retune the existing verified DQ4 excursion; no route search or external edits. */
import{readFileSync,writeFileSync,mkdirSync}from'node:fs'
import{resolve}from'node:path'
import{createHash}from'node:crypto'
import{addWestwardTuningExcursion}from'./tune-ddr-data-mask'
import{objectHash,validateDeclaredDdrReplacements}from'./improve-ddr-a9-power-launches'
import{auditCompositeHostTopology}from'./ddr-composite-routing-context'
import{auditDdrTraceJunctions}from'./check-ddr-system-copper'
import{auditRouteAngles}from'./check-route-angles'
import{checkHostTerminalContract}from'./route-ddr-sequential'
import{verifyDdrCapture}from'./ddr-capture-provenance'
const planar=(route:any[])=>route.slice(1).reduce((n,p,i)=>n+(p.route_type==='wire'&&route[i].route_type==='wire'&&p.layer===route[i].layer?Math.hypot(p.x-route[i].x,p.y-route[i].y):0),0)
const dir=resolve(process.argv[2]??'dist/ddr-dq7-ordered-host'),out=resolve(process.argv[3]??'dist/ddr-dq4-ordered-retuned'),target=Number(process.argv[4]??28.423158),capture=resolve('dist/ddr-system/host-taps-54af3e346b5e'),hashes=new Map<string,string>(),fh=(p:string)=>createHash('sha256').update(readFileSync(p)).digest('hex'),read=(p:string)=>{hashes.set(p,fh(p));return JSON.parse(readFileSync(p,'utf8'))},parent=read(`${dir}/candidate.circuit.json`),prior=read(`${dir}/report.json`),host=read(`${dir}/host-bundle.json`),input=read(`${dir}/routing-input.json`),beforeBundle=read(`${capture}/data-mask-tuned/candidate.physical.json`),exact=read(`${capture}/data-exact-width/candidate.physical.json`),tuned=read(`${capture}/data-d4-tuned/candidate.physical.json`),tuning=read(`${capture}/data-d4-tuned/tuning-report.json`),{captureHash}=verifyDdrCapture(capture)
if([prior,host,beforeBundle,exact,tuned,tuning].some(v=>v.captureHash!==captureHash)||objectHash(beforeBundle.traces)!==tuning.tuning.sourceTraceHash)throw Error('Authoritative pre-tuning provenance mismatch')
const baseline=beforeBundle.traces.find((t:any)=>t.connection_name==='source_trace_579'),exactTrace=exact.traces.find((t:any)=>t.connection_name==='source_trace_579'),oldTuned=tuned.traces.find((t:any)=>t.connection_name==='source_trace_579'),before=parent.find((e:any)=>e.type==='pcb_trace'&&e.source_trace_id==='source_trace_579')
if(objectHash(baseline)!==objectHash(exactTrace)||objectHash(before.route)!==objectHash(oldTuned.route))throw Error('Actual DQ4 differs from exact-width/tuned authoritative route')
const recipe=tuning.attempts.find((a:any)=>a.clean),replay=addWestwardTuningExcursion(baseline,recipe.index,recipe.entryX,recipe.exitX,tuning.tuning.addedMm,recipe.sign)
if(objectHash(replay.route)!==objectHash(before.route))throw Error('Old tuning cannot be reproduced')
const connection=input.connections.find((c:any)=>c.name==='source_trace_579'),fixedWitnesses=connection.pointsToConnect.map((p:any)=>{const exit=parent.find((e:any)=>e.type==='pcb_breakout_point'&&e.pcb_breakout_point_id===p.pointId),traces=parent.filter((e:any)=>e.type==='pcb_trace'&&e.source_trace_id===exit?.source_trace_id&&e.route.at(-1)?.layer===p.layer&&Math.hypot(e.route.at(-1).x-p.x,e.route.at(-1).y-p.y)<1e-7);if(traces.length!==1)throw Error('Ambiguous actual source escape');return{exit,trace:traces[0],lengthMm:planar(traces[0].route)}}),fixedMm=fixedWitnesses.reduce((n:number,w:any)=>n+w.lengthMm,0),baseFullMm=fixedMm+planar(baseline.route),beforeFullMm=fixedMm+planar(before.route)
if(Math.abs(baseFullMm-tuning.tuning.beforeMm)>1e-7||Math.abs(beforeFullMm-tuning.tuning.measuredMm)>1e-7||target<=baseFullMm)throw Error('Measured source paths changed or target below baseline')
const after={...structuredClone(before),route:addWestwardTuningExcursion(baseline,recipe.index,recipe.entryX,recipe.exitX,target-baseFullMm,recipe.sign).route},child=parent.map((e:any)=>e===before?after:e),traces=host.traces.map((t:any)=>t.pcb_trace_id===before.pcb_trace_id?after:t),topology=auditCompositeHostTopology(child,traces,input.connections),allAngles=auditRouteAngles(child.filter((e:any)=>e.type==='pcb_trace')),allJunctions=auditDdrTraceJunctions(child,input.connections),terminal=checkHostTerminalContract(traces,input.connections.filter((c:any)=>traces.some((t:any)=>t.connection_name===c.name))),measuredFullMm=fixedMm+planar(after.route),accepted=topology.valid&&allAngles.valid&&allJunctions.valid&&terminal.valid&&Math.abs(measuredFullMm-target)<1e-7
const manifest={captureHash,parentDirectory:dir,parentCircuitSha256:objectHash(parent),candidateCircuitSha256:objectHash(child),authoritativeBaselineTraceSha256:objectHash(baseline),authoritativePreTuningBundleSha256:objectHash(beforeBundle.traces),inputHashes:Object.fromEntries(hashes),fixedWitnesses,replacements:[{id:`pcb_trace:${before.pcb_trace_id}`,before,after,beforeSha256:objectHash(before),afterSha256:objectHash(after),reason:'Reduce existing DQ4 tuning excursion to ordered-DM0 target using the same verified template.'}]},preservation=validateDeclaredDdrReplacements(parent,child,manifest)
for(const[p,h]of hashes)if(fh(p)!==h)throw Error('Input changed during verification')
if(verifyDdrCapture(capture).captureHash!==captureHash)throw Error('Capture changed')
mkdirSync(out,{recursive:true});const write=(n:string,v:any)=>writeFileSync(`${out}/${n}`,JSON.stringify(v,null,2));write('report.json',{captureHash,accepted,beforeFullMm,baseFullMm,targetMm:target,measuredFullMm,topology,allAngles,allJunctions,terminal,preservation,inputHashes:Object.fromEntries(hashes),scope:'Isolated DQ4 template reduction, all current orderedDM0/DQ7 hosts retained; complete byte timing remains unqualified.'});write('original-copper-replacements.json',manifest)
if(accepted){write('candidate.circuit.json',child);write('host-bundle.json',{...host,traces});write('candidate.physical.json',{captureHash,layerSpace:'physical',traces})}
console.log(JSON.stringify({accepted,beforeFullMm,baseFullMm,targetMm:target,measuredFullMm,topology:topology.valid,allAngles:allAngles.valid,allJunctions:allJunctions.valid,terminal:terminal.valid,violations:topology.physical.violations.length,errors:topology.physical.errors,hosts:traces.length,candidateCircuitSha256:objectHash(child)}))