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/experiment-ddr-clock-e7-local.ts

/** Fixed clock branch-direction proposal plus one existing-winding E7 reroute. */
import{readFileSync,writeFileSync,mkdirSync}from'node:fs';import{resolve}from'node:path';import{objectHash as hash}from'./improve-ddr-a9-power-launches';import{supportCopperObstacles}from'./ddr-support-routing-context';import{explicitFacingViaContinuation}from'./ddr-facing-via-continuation';import{auditCompositeHostTopology}from'./ddr-composite-routing-context';import{auditRouteAngles}from'./check-route-angles';import{verifyDdrSystemCopper,auditDdrTraceJunctions}from'./check-ddr-system-copper';import{trunkHeadingOptions}from'./audit-ddr-trunk-directions';import{planDdrGroundAntipads}from'./ddr-ground-plane-antipads';
const parent=resolve('dist/ddr-twenty-one-power58-current'),out=resolve('dist/ddr-clock-e7-local'),read=(p:string)=>JSON.parse(readFileSync(p,'utf8')),c=read(parent+'/candidate.circuit.json'),h=read(parent+'/host-bundle.json'),input=read(parent+'/routing-input.json'),prior=read(parent+'/report.json'),preflight=read('dist/ddr-current-clock-preflight/report.json');if(hash(c)!==prior.candidateCircuitSha256||hash(c)!==preflight.parentCandidateSha256||h.traces.length!==21)throw Error('Parent mismatch');mkdirSync(out,{recursive:true});const write=(n:string,v:any)=>writeFileSync(out+'/'+n,JSON.stringify(v,null,2)+'\n')
const clocks=preflight.changes.map((r:any)=>{const after=structuredClone(r.after);after.route.at(-1).y-=.24;const end=after.route.at(-1),v=after.route.find((p:any)=>p.route_type==='via'),angle=(Math.atan2(v.y-end.y,v.x-end.x)*180/Math.PI+360)%360;return{...r,after,exitAfter:{...r.exitAfter,y:end.y},branchDegrees:angle,options:trunkHeadingOptions(angle)}});if(clocks.some((r:any)=>!r.options.some((o:any)=>o.incomingTravelDegrees===90&&o.outgoingTravelDegrees===90)))throw Error('Clock branch cannot support CPU-driven north trunk')
const clockIds=new Set(clocks.map((r:any)=>r.before.pcb_trace_id)),removedVias=c.filter((e:any)=>e.type==='pcb_via'&&clockIds.has(e.pcb_trace_id)),clockCandidate=c.filter((e:any)=>!removedVias.includes(e)).map((e:any)=>clocks.find((r:any)=>hash(r.before)===hash(e))?.after??clocks.find((r:any)=>hash(r.exitBefore)===hash(e))?.exitAfter??e),old=c.find((t:any)=>t.type==='pcb_trace'&&t.source_trace_id==='source_trace_530'),connection=input.connections.find((n:any)=>n.pointsToConnect.some((p:any)=>p.pointId==='pcb_breakout_point_352')),vi=old.route.findIndex((p:any)=>p.route_type==='via'),via=old.route[vi],source={...old.route[0],pointId:'pcb_breakout_point_352'},target={...connection.pointsToConnect[0]},hostIds=new Set(h.traces.map((t:any)=>t.pcb_trace_id)),fixed=clockCandidate.filter((e:any)=>!hostIds.has(e.pcb_trace_id)),obstacles=fixed.filter((e:any)=>['pcb_trace','pcb_via','pcb_smtpad'].includes(e.type)).flatMap((e:any)=>{const own=e.type==='pcb_trace'&&e.pcb_trace_id===old.pcb_trace_id,element=own?{...e,route:e.route.slice(0,vi+1)}:e,isOwn=own||(e.type==='pcb_via'&&e.pcb_trace_id===old.pcb_trace_id)||(e.type==='pcb_smtpad'&&e.pcb_port_id===old.route[0].start_pcb_port_id);return supportCopperObstacles([element]).map(o=>({...o,...(o.obstacleId.endsWith('_cap')||o.obstacleId.startsWith('fixed_via_')||e.shape==='circle'?{shape:'circle'}:{}),connectedTo:[isOwn?connection.name:`reserved:${o.obstacleId}`]}))}),sourceObstacle=obstacles.find((o:any)=>o.connectedTo.includes(connection.name)&&o.layers.includes('top')&&Math.abs(o.center.x-source.x)<=o.width/2+1e-7&&Math.abs(o.center.y-source.y)<=o.height/2+1e-7);if(!sourceObstacle)throw Error('Source obstacle missing')
const prepared:any={connection:{...connection,pointsToConnect:[source,target]},connectionIndex:0,sourcePoint:source,sourcePointIndex:0,sourceLayer:'top',sourceObstacle,targetPoint:target},bounds={minX:-18,maxX:target.x,minY:-13,maxY:0},bus:any={busId:'clock-E7',direction:'right',exitEdge:'right',termination:{type:'boundary'},connections:[prepared],componentId:'RAM0',componentObstacles:[sourceObstacle],componentBounds:bounds,sharedBoundary:bounds,xCoordinates:[via.x],yCoordinates:[via.y],pitchX:.8,pitchY:.8},params:any={srj:{...input,obstacles,traces:h.traces},bus,targetLayer:'inner4',terminals:[{connection:prepared,viaPoint:via,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:.1,maximumRouteOrderAttempts:1,maximumSearchStates:300000,heuristicWeight:1.2,sourceEscapePaths:new Map([[0,old.route.slice(0,vi).map((p:any)=>({x:p.x,y:p.y}))]])};write('input-evidence.json',{parentCandidateSha256:hash(c),clockChanges:clocks,removedVias,source,target,via,bounds,retainedHostsSha256:hash(h.traces)})
const{routeViaMinimalWindingAlternativesSteps}=await import(resolve('node_modules/@tscircuit/fanout-solver/lib/route-via-minimal-winding.ts')),steps=routeViaMinimalWindingAlternativesSteps(params,1),started=Date.now(),progress:any[]=[];let result:any[]=[],finished=false;while(Date.now()-started<55000){const n=steps.next();if(n.done){result=n.value;finished=true;break}progress.push({...n.value,visualization:undefined});write('progress.json',progress)}if(!finished)steps.return([])
let evidence:any={accepted:false,finished,alternatives:result.length};if(result[0]?.[0]?.trace){const after=explicitFacingViaContinuation(old,result[0][0].trace,'inner4').after,candidate=clockCandidate.map((e:any)=>hash(e)===hash(old)?after:e),fresh=[...h.traces,...clocks.map((r:any)=>r.after),after],ids=new Set(fresh.map((t:any)=>t.pcb_trace_id));let planes=c.filter((e:any)=>e.type==='pcb_copper_pour'),planeError=null;try{planes=planDdrGroundAntipads(planes,clocks.filter((r:any)=>Math.hypot(r.before.route[2].x-r.after.route[2].x,r.before.route[2].y-r.after.route[2].y)>1e-7).map((r:any)=>({...r.after.route[2],net:'CK'}))).planes}catch(error){planeError=String(error)}const final=candidate.map((e:any)=>planes.find((p:any)=>p.pcb_copper_pour_id===e.pcb_copper_pour_id)??e),connections=input.connections.map((n:any)=>({...n,pointsToConnect:n.pointsToConnect.map((p:any)=>{const r=clocks.find((r:any)=>r.exitBefore.pcb_breakout_point_id===p.pointId);return r?{...p,x:r.exitAfter.x,y:r.exitAfter.y,layer:r.exitAfter.layer}:p})})),physical=verifyDdrSystemCopper(final.filter((e:any)=>!ids.has(e.pcb_trace_id)),fresh,connections,{},100),angles=auditRouteAngles(final.filter((e:any)=>e.type==='pcb_trace')),joins=auditDdrTraceJunctions(final,connections),planar=(r:any[])=>r.slice(1).reduce((s:number,b:any,i:number)=>{const a=r[i];return s+(a.route_type==='wire'&&b.route_type==='wire'&&a.layer===b.layer?Math.hypot(a.x-b.x,a.y-b.y):0)},0),delta=planar(after.route)-planar(old.route);evidence={...evidence,planeError,physical,angles,joins,e7BeforeMm:planar(old.route),e7AfterMm:planar(after.route),requiredHostCompensationMm:-delta,clockStubMm:clocks.map((r:any)=>planar(r.after.route)),clockDirections:clocks.map((r:any)=>({angle:r.branchDegrees,options:r.options})),scope:'Local geometry proposal only. Host compensation, plane clearance/web, actual coupled trunk and complete board verification are required; no acceptance.'};write('e7-replacement.json',{before:old,after});write('candidate.diagnostic.circuit.json',final)}write('report.json',{...evidence,parentCandidateSha256:hash(c)});console.log(JSON.stringify({alternatives:result.length,finished,planeError:evidence.planeError,delta:evidence.e7AfterMm-evidence.e7BeforeMm,violations:evidence.physical?.violations.map((v:any)=>({layer:v.layer,gap:v.gapMm,a:v.aShape.traceId,b:v.bShape.traceId}))}));if(hash(read(parent+'/candidate.circuit.json'))!==hash(c))throw Error('Parent changed')