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/route-ddr-d1-freed-via.ts
/** One bounded existing-winding D1 top launch trial; independent actual-copper predicate. */
import {readFileSync,writeFileSync,mkdirSync} from 'node:fs'
import {resolve} from 'node:path'
import {createHash} from 'node:crypto'
import {supportCopperObstacles} from './ddr-support-routing-context'
import {verifyDdrSystemCopper,auditDdrTraceJunctions} from './check-ddr-system-copper'
import {auditRouteAngles} from './check-route-angles'
const [parentArg,preparedArg,outArg]=process.argv.slice(2),read=(p:string)=>JSON.parse(readFileSync(p,'utf8')),hash=(x:any)=>createHash('sha256').update(JSON.stringify(x)).digest('hex'),parent=read(`${parentArg}/candidate.circuit.json`),prepared=read(`${preparedArg}/candidate.circuit.json`),proof=read(`${preparedArg}/report.json`),input=read(`${parentArg}/routing-input.json`),host=read(`${parentArg}/host-bundle.json`),out=resolve(outArg!)
if(proof.parentCircuitSha256!==hash(parent)||proof.candidateCircuitSha256!==hash(prepared)||host.traces.length!==19)throw Error('Prepared provenance mismatch')
const detail=proof.details[0],template=prepared.find((e:any)=>e.type==='pcb_trace'&&e.pcb_trace_id===detail.traceId),start=template.route[0],via=detail.via,name='D1_ground_launch',sourcePad=parent.find((e:any)=>e.type==='pcb_smtpad'&&e.pcb_port_id===start.start_pcb_port_id),fixed=parent.filter((e:any)=>e.pcb_trace_id!==detail.traceId),sourcePoint={x:start.x,y:start.y,layer:'top',pcb_port_id:start.start_pcb_port_id},target={...via,layer:'top'},connection={name,pointsToConnect:[sourcePoint,target]},obstacles=fixed.filter((e:any)=>['pcb_trace','pcb_via','pcb_smtpad'].includes(e.type)).flatMap((e:any)=>supportCopperObstacles([e]).map((o:any)=>({...o,...((e.type==='pcb_smtpad'&&e.shape==='circle')||e.type==='pcb_via'||o.obstacleId.startsWith('fixed_via_')||o.obstacleId.endsWith('_cap')?{shape:'circle'}:{}),connectedTo:[e===sourcePad?name:`reserved:${e[`${e.type}_id`]}`]}))),sourceObstacle=obstacles.find((o:any)=>o.connectedTo[0]===name)
if(!sourceObstacle)throw Error('Missing source pad selector')
const bounds={minX:via.x-1,maxX:start.x+1,minY:start.y-1.6,maxY:via.y+1.6},preparedConnection={connection,connectionIndex:0,sourcePoint,sourcePointIndex:0,sourceLayer:'top',sourceObstacle,targetPoint:target},bus={busId:name,direction:'left',exitEdge:'left',termination:{type:'boundary'},connections:[preparedConnection],componentId:'D1-actual-pad',componentObstacles:[sourceObstacle],componentBounds:bounds,sharedBoundary:bounds,xCoordinates:[start.x],yCoordinates:[start.y],pitchX:.8,pitchY:.8},params={srj:{...input,bounds,connections:[connection],obstacles,traces:[]},bus,targetLayer:'top',terminals:[{connection:preparedConnection,viaPoint:sourcePoint,exitPoint:target}],acceptedPlans:[],layerNames:['top',...Array.from({length:8},(_,i)=>`inner${i+1}`),'bottom'],traceWidth:.12,viaDiameter:.4572,viaHoleDiameter:.254,clearance:.1016,allowBlindAndBuriedVias:false,allowSourceLayerRouting:true,gridStep:.02,maximumRouteOrderAttempts:1,maximumSearchStates:300000,heuristicWeight:1,sourceEscapePaths:new Map([[0,[sourcePoint,sourcePoint]]])}
const {routeViaMinimalWindingAlternativesSteps}=await import(resolve('node_modules/@tscircuit/fanout-solver/lib/route-via-minimal-winding.ts')),steps=routeViaMinimalWindingAlternativesSteps(params as any,1),started=Date.now(),progress:any[]=[];let results:any[]=[],finished=false
while(Date.now()-started<55000){const s=steps.next();if(s.done){results=s.value;finished=true;break}progress.push({...s.value,visualization:undefined})}if(!finished)steps.return([])
let trial:any,physical:any,angles:any,junctions:any,lengthMm:number|undefined,sameNetContacts:any[]=[];if(results[0]?.length===1){const route=results[0][0].trace.route;if(route.some((p:any)=>p.route_type!=='wire'||p.layer!=='top')||Math.hypot(route[0].x-start.x,route[0].y-start.y)>1e-8||Math.hypot(route.at(-1).x-via.x,route.at(-1).y-via.y)>1e-8)throw Error('Changed terminal/layer');route[0]={...route[0],start_pcb_port_id:start.start_pcb_port_id};lengthMm=route.slice(1).reduce((n:number,p:any,i:number)=>n+Math.hypot(p.x-route[i].x,p.y-route[i].y),0);trial={...template,route:[...route,template.route.at(-1)]};const child=prepared.map((e:any)=>e.pcb_trace_id===detail.traceId&&e.type==='pcb_trace'?trial:e),fresh=[...host.traces,trial],ids=new Set(fresh.map((e:any)=>e.pcb_trace_id));physical=verifyDdrSystemCopper(child.filter((e:any)=>!ids.has(e.pcb_trace_id)),fresh,input.connections,{},50,{reportSameNetContacts:true});sameNetContacts=(physical.sameNetContacts??[]).filter((v:any)=>v.aShape.traceId===trial.pcb_trace_id||v.bShape.traceId===trial.pcb_trace_id);angles=auditRouteAngles([trial]);junctions=auditDdrTraceJunctions(child,input.connections)}
const report={parentCircuitSha256:hash(parent),preparedCircuitSha256:hash(prepared),captureHash:proof.captureHash,detail,finished,elapsedSeconds:(Date.now()-started)/1000,gridStep:.02,maximumSearchStates:300000,bounds,progress,raw:results,lengthMm,physical,angles,junctions,sameNetContacts,status:'diagnostic-only',scope:'One existing winding top-only RAM0 D1 launch trial. All foreign copper and all nineteen hosts reserved. Must be <=1.524mm with pad-only same-net contacts to proceed. RAM1 not routed in this diagnostic.'};mkdirSync(out,{recursive:true});writeFileSync(`${out}/report.json`,JSON.stringify(report,null,2));if(trial)writeFileSync(`${out}/trial-trace.json`,JSON.stringify(trial,null,2));console.log(JSON.stringify({finished,alternatives:results[0]?.length??0,lengthMm,physicalErrors:physical?.errors,violations:physical?.violations.length,angles:angles?.valid,junctions:junctions?.valid}))