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/plan-ddr-d1-freed-via.ts

/** Bounded D1 ground launch into the original E3 barrel location; no external writes. */
import {readFileSync,writeFileSync,mkdirSync} from 'node:fs'
import {resolve} from 'node:path'
import {createHash} from 'node:crypto'
import F from '@flatten-js/core'
import {verifyDdrCapture} from './ddr-capture-provenance'
import {verifyDdrSystemCopper,auditDdrTraceJunctions} from './check-ddr-system-copper'
import {auditRouteAngles} from './check-route-angles'
import {refreshDdrPlaneContacts} from './ddr-plane-contact-audit'
const [parentArg,outArg]=process.argv.slice(2),dir=resolve(parentArg!),out=resolve(outArg!),hash=(x:any)=>createHash('sha256').update(JSON.stringify(x)).digest('hex'),bound:Record<string,string>={},read=(p:string)=>{p=resolve(p);bound[p]=createHash('sha256').update(readFileSync(p)).digest('hex');return JSON.parse(readFileSync(p,'utf8'))},parent=read(`${dir}/candidate.circuit.json`),host=read(`${dir}/host-bundle.json`),input=read(`${dir}/routing-input.json`),prior=read(`${dir}/report.json`),{captureHash,snapshot}=verifyDdrCapture(resolve('dist/ddr-system/host-taps-54af3e346b5e'))
if(host.captureHash!==captureHash||host.traces.length!==19)throw Error('Expected frozen nineteen-host parent')
let child=structuredClone(parent);const patches:any[]=[],details:any[]=[],ids=new Set<string>(host.traces.map((t:any)=>t.pcb_trace_id)),poly=(vs:any[])=>new F.Polygon(vs.map(p=>[p.x,p.y]))
function replace(before:any,after:any,reason:string){const id=`${before.type}:${before[`${before.type}_id`]}`,i=child.findIndex((e:any)=>`${e.type}:${e[`${e.type}_id`]}`===id);if(i<0)throw Error('Missing element');child[i]=after;patches.push({id,before,after,beforeSha256:hash(before),afterSha256:hash(after),reason})}
for(const byte of [0,1]){const original=snapshot.pathsByByte[byte].find((p:any)=>p.connection==='U1.ball_E3'),v=original.route.find((p:any)=>p.route_type==='via'),place=snapshot.placements[byte],via={x:place.pcbX-v.x,y:place.pcbY-v.y},before=child.find((e:any)=>e.type==='pcb_trace'&&e.pcb_trace_id===`saved_fanout_pcb_group_${byte?5:3}_3`),start=before.route[0],pad=child.find((e:any)=>e.type==='pcb_smtpad'&&e.pcb_port_id===start.start_pcb_port_id),join=child.find((e:any)=>e.type==='source_trace'&&e.source_trace_id===`planned_ram${byte}_D1_supply`)
 if(!pad||Math.hypot(pad.x-start.x,pad.y-start.y)>1e-8||join.connected_source_net_ids[0]!=='source_net_346')throw Error('D1 source pad/rail mismatch')
 const dy=via.y-start.y,corner={x:via.x+Math.abs(dy),y:start.y},lengthMm=Math.abs(corner.x-start.x)+Math.SQRT2*Math.abs(dy)
 if(lengthMm>1.524+1e-8)throw Error('Ground launch exceeds 1.524 mm')
 const after={...before,connectsTo:before.connectsTo.filter((s:string)=>!s.startsWith('pcb_breakout_point_')),route:[start,{route_type:'wire',...corner,layer:'top',width:.12},{route_type:'wire',...via,layer:'top',width:.12},{route_type:'via',...via,from_layer:'top',to_layer:'inner1',via_diameter:.4572,via_hole_diameter:.254}]};delete after.connection_name;replace(before,after,'D1 short W then NW launch to source-bound former E3 through-via location');ids.add(after.pcb_trace_id);details.push({byte,via,pad:{x:pad.x,y:pad.y},lengthMm,traceId:after.pcb_trace_id,join,retiredBreakout:before.connectsTo.filter((s:string)=>s.startsWith('pcb_breakout_point_'))})}
const fresh=child.filter((e:any)=>e.type==='pcb_trace'&&ids.has(e.pcb_trace_id)),conductor=verifyDdrSystemCopper(child.filter((e:any)=>e.type!=='pcb_copper_pour'&&!ids.has(e.pcb_trace_id)),fresh,input.connections,{},50,{reportSameNetContacts:true}),refills:any[]=[]
// Only remove an entire existing closed void when it contains one declared former E3 site;
// full all-layer clearance below decides whether its other area can safely be filled.
for(const before of child.filter((e:any)=>e.type==='pcb_copper_pour'&&['inner1','inner5'].includes(e.layer))){const removed=before.brep_shape.inner_rings.filter((r:any)=>details.some(d=>poly(r.vertices).contains(new F.Point(d.via.x,d.via.y))));if(removed.length!==2)throw Error('Expected exactly two old E3 ground holes');const after={...before,brep_shape:{...before.brep_shape,inner_rings:before.brep_shape.inner_rings.filter((r:any)=>!removed.includes(r))}};replace(before,after,'Explicit former-E3 ground antipad refill for source-owned D1 ground barrels');refills.push({layer:before.layer,removed})}
const physical=verifyDdrSystemCopper(child.filter((e:any)=>!ids.has(e.pcb_trace_id)),fresh,input.connections,{},50,{reportSameNetContacts:true}),angles=auditRouteAngles(child.filter((e:any)=>e.type==='pcb_trace')),junctions=auditDdrTraceJunctions(child,input.connections),witnesses=structuredClone(prior.planeAudit)
for(const w of witnesses)if(['inner1','inner5'].includes(w.layer)){for(const d of details)w.viaContacts.push({id:`${d.traceId}:via:3`,...d.via,annulusSamplesInPlane:32,total:32});w.uniqueViaCount+=2}
let planeAudit:any,planeError:string|undefined;try{planeAudit=refreshDdrPlaneContacts(witnesses,child.filter((e:any)=>e.type==='pcb_copper_pour'),child)}catch(e){planeError=String(e)}
const everyTrace=child.filter((e:any)=>e.type==='pcb_trace'),everyId=new Set(everyTrace.map((e:any)=>e.pcb_trace_id)),allCopper=verifyDdrSystemCopper(child.filter((e:any)=>!everyId.has(e.pcb_trace_id)),everyTrace,input.connections,{}),planeForeignViolations=allCopper.violations.filter((v:any)=>v.aShape.polygon||v.bShape.polygon)
const accepted=!planeForeignViolations.length&&!conductor.errors.length&&!conductor.violations.length&&!physical.errors.length&&!physical.violations.length&&angles.valid&&junctions.valid&&!planeError,report={...prior,captureHash,parentDirectory:dir,parentCircuitSha256:hash(parent),candidateCircuitSha256:hash(child),sourceHashes:bound,status:accepted?'accepted-isolated-power-geometry':'rejected-isolated-power-geometry',details,refills,planeForeignViolations,conductor,physical,angles,junctions,planeAudit,planeError,hostsPreserved:host.traces.every((t:any)=>child.some((e:any)=>hash(e)===hash(t))),scope:'Only two D1 ground launches and explicit former-E3 ground antipad refills. E1/E2 board profile overrides retained unchanged, all nineteen host traces exact. Power launch/annulus geometry does not qualify complete DDR or PDN.'}
for(const[p,h]of Object.entries(bound))if(createHash('sha256').update(readFileSync(p)).digest('hex')!==h)throw Error('Input changed')
mkdirSync(out,{recursive:true});for(const[n,v]of Object.entries({'candidate.circuit.json':child,'host-bundle.json':host,'routing-input.json':input,'report.json':report,'original-copper-replacements.json':{captureHash,parentCircuitSha256:hash(parent),candidateCircuitSha256:hash(child),replacements:patches}}))writeFileSync(resolve(out,n),JSON.stringify(v,null,2));console.log(JSON.stringify({accepted,details,conductorViolations:conductor.violations.length,physicalErrors:physical.errors,physicalViolations:physical.violations.length,first:physical.violations.slice(0,2),planeError,angles:angles.valid,junctions:junctions.valid}))