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/merge-ddr-f8-power-void.ts
/** Explicit local Boolean void merge for the RAM1 F8 supply-plane antipad. */
import F from '@flatten-js/core'
import{readFileSync,writeFileSync,mkdirSync,copyFileSync}from'node:fs'
import{resolve}from'node:path'
import{createHash}from'node:crypto'
import{refreshDdrPlaneContacts}from'./ddr-plane-contact-audit'
import{auditCompositeHostTopology}from'./ddr-composite-routing-context'
import{verifyDdrSystemCopper,auditDdrTraceJunctions}from'./check-ddr-system-copper'
import{auditRouteAngles}from'./check-route-angles'
import{planeCoverageIntervals}from'./audit-ddr-host-reference-coverage'
import{spawnSync}from'node:child_process'
const provisional=process.argv.includes('--provisional'),clearance=provisional?.107:.1016,minimumWeb=provisional?.127:.1524;const[parentArg,proposalArg,outArg]=process.argv.slice(2),dir=resolve(parentArg!),proposal=resolve(proposalArg!),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`),prior=read(`${dir}/report.json`),host=read(`${dir}/host-bundle.json`),input=read(`${dir}/routing-input.json`),trial=read(`${proposal}/report.json`),candidate=read(`${proposal}/candidate.circuit.json`),manifest=read(`${proposal}/original-copper-replacements.json`),poly=(vs:any[])=>{const p=new F.Polygon(vs.map(p=>[p.x,p.y]));if([...p.faces][0]!.orientation()===1)p.reverse();return p},vertices=(f:any)=>[...f.edges].map((e:any)=>({x:e.start.x,y:e.start.y}))
const busPath=resolve('/Users/ankan/Documents/Codex/2026-09-10/what-x20/outputs/mt41k512m8da-107-it-p-fanout/src/bus-rules.ts');bound[busPath]=createHash('sha256').update(readFileSync(busPath)).digest('hex');if((await import(busPath)).BUS_MANUFACTURING.referencePourClearanceMm!==.107)throw Error('External pour policy changed');
if(trial.parentCircuitSha256!==hash(parent)||trial.candidateCircuitSha256!==hash(candidate)||host.traces.length!==20||manifest.replacements.length!==3)throw Error('Expected exact twenty-host three-copper proposal')
let replay=structuredClone(parent);for(const r of manifest.replacements){const i=replay.findIndex((e:any)=>`${e.type}:${e[`${e.type}_id`]}`===r.id);if(i<0||hash(replay[i])!==r.beforeSha256||hash(r.after)!==r.afterSha256)throw Error('Replacement witness mismatch');replay[i]=r.after}if(hash(replay)!==hash(candidate))throw Error('Proposal contains undeclared changes')
const before=parent.find((e:any)=>e.type==='pcb_copper_pour'&&e.layer==='inner3'),via=candidate.find((e:any)=>e.type==='pcb_via'&&e.pcb_via_id==='pcb_via_234'),radius=(.2286+clearance)/Math.cos(Math.PI/64),newHole=Array.from({length:64},(_,i)=>({x:via.x+radius*Math.cos(i*Math.PI/32),y:via.y+radius*Math.sin(i*Math.PI/32)})),oldHoles=before.brep_shape.inner_rings,near=oldHoles.map((h:any)=>({h,d:poly(h.vertices).distanceTo(poly(newHole))[0]})).filter((h:any)=>h.d<.1524-1e-7),bridges:any[]=[]
if(near.length!==2)throw Error(`Expected two neighboring voids, got ${near.length}`)
let region=poly(before.brep_shape.outer_ring.vertices);for(const h of oldHoles){const points=h.vertices.map((p:any)=>new F.Point(p.x,p.y));if([...new F.Polygon(points).faces][0]!.orientation()!==1)points.reverse();region.addFace(points)}region=F.BooleanOperations.subtract(region,poly(newHole))
for(const {h,d}of (provisional?[]:near)){const center={x:h.vertices.reduce((n:number,p:any)=>n+p.x,0)/h.vertices.length,y:h.vertices.reduce((n:number,p:any)=>n+p.y,0)/h.vertices.length},len=Math.hypot(center.x-via.x,center.y-via.y),nx=-(center.y-via.y)/len,ny=(center.x-via.x)/len,half=.1524/2,bridge=[{x:via.x+nx*half,y:via.y+ny*half},{x:center.x+nx*half,y:center.y+ny*half},{x:center.x-nx*half,y:center.y-ny*half},{x:via.x-nx*half,y:via.y-ny*half}];if(!poly(h.vertices).contains(new F.Point(center.x,center.y)))throw Error('Bridge target outside existing void');region=F.BooleanOperations.subtract(region,poly(bridge));bridges.push({oldGapMm:d,center,bridge,widthMm:.1524})}
const faces=[...region.faces],outers=faces.filter(f=>f.orientation()===-1),holes=faces.filter(f=>f.orientation()===1).map(f=>({vertices:vertices(f)}));if(outers.length!==1)throw Error('Plane fragmentation');for(const h of oldHoles)if(F.BooleanOperations.intersect(region,poly(h.vertices)).area()>1e-9)throw Error('Old void refilled');const affected=holes.filter(h=>poly(h.vertices).contains(new F.Point(via.x,via.y)));if(affected.length!==1)throw Error('Missing merged void');const webs=holes.filter(h=>h!==affected[0]).map(h=>poly(affected[0].vertices).distanceTo(poly(h.vertices))[0]);webs.push(...[...poly(before.brep_shape.outer_ring.vertices).edges].map(e=>poly(affected[0].vertices).distanceTo(e.shape)[0]));const minimumWebMm=Math.min(...webs);if(minimumWebMm<minimumWeb-1e-7){mkdirSync(out,{recursive:true});writeFileSync(`${out}/preflight-rejection.json`,JSON.stringify({parentCircuitSha256:hash(parent),minimumWebMm,requiredWebMm:minimumWeb,bridges,reason:'Merged boundary reaches inherited neighboring web below unchanged default'},null,2));throw Error(`Local web ${minimumWebMm} below ${minimumWeb}`)}
const after={...before,brep_shape:{outer_ring:before.brep_shape.outer_ring,inner_rings:holes}},child=candidate.map((e:any)=>e.type==='pcb_copper_pour'&&e.pcb_copper_pour_id===before.pcb_copper_pour_id?after:e),newCoverage:any[]=[]
for(const t of child.filter((e:any)=>e.type==='pcb_trace'))for(let i=1;i<t.route.length;i++){const a=t.route[i-1],b=t.route[i];if(a.route_type!=='wire'||b.route_type!=='wire'||a.layer!==b.layer||!['inner2','inner4'].includes(a.layer)||Math.hypot(b.x-a.x,b.y-a.y)<1e-8)continue;const missed=(p:any)=>planeCoverageIntervals(a,b,p.brep_shape).filter((v:any)=>!v.covered).reduce((n:number,v:any)=>n+v.lengthMm,0),old=missed(before),now=missed(after);if(now-old>1e-7)newCoverage.push({traceId:t.pcb_trace_id,sourceTraceId:t.source_trace_id,segment:i,a,b,addedUncoveredMm:now-old})}
const planeAudit=refreshDdrPlaneContacts(trial.planeAudit,child.filter((e:any)=>e.type==='pcb_copper_pour'),child),topology=auditCompositeHostTopology(child,host.traces,input.connections),angles=auditRouteAngles(child.filter((e:any)=>e.type==='pcb_trace')),junctions=auditDdrTraceJunctions(child,input.connections),all=child.filter((e:any)=>e.type==='pcb_trace'),allIds=new Set(all.map((e:any)=>e.pcb_trace_id)),allCheck=verifyDdrSystemCopper(child,all,input.connections,{}),planeForeignViolations=allCheck.violations.filter((v:any)=>v.aShape.polygon||v.bShape.polygon),accepted=!newCoverage.length&&!planeForeignViolations.length&&topology.valid&&angles.valid&&junctions.valid,patches=[...manifest.replacements,{id:`pcb_copper_pour:${before.pcb_copper_pour_id}`,before,after,beforeSha256:hash(before),afterSha256:hash(after)}],report={...prior,candidateCircuitSha256:hash(child),supportCircuitSha256:hash(child),accepted,captureHash:prior.captureHash,parentCircuitSha256:hash(parent),sourceHashes:bound,planeAudit,topology,physical:topology.physical,angles,junctions,f8Repair:{via:{x:via.x,y:via.y},radius,bridges,minimumWebMm,oldVoidsPreserved:true,outerRingExact:true,outerComponents:1,newCoverage,planeForeignViolations,policy:{clearanceMm:clearance,minimumWebMm:minimumWeb,provisional,scope:provisional?'Explicit F8-only provisional5mil web assumption, external .107mm pour clearance; fabrication approval outstanding':'Unchanged default6mil web policy'}},scope:'Isolated RAM1 F8 relocation and E9 two-pad G8 reuse. Explicit source-owned via move and local Boolean supply void merge. Reject any new inner2/inner4 reference gap or source-contact regression.'}
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,'report.json':report,'original-copper-replacements.json':{captureHash:prior.captureHash,parentDirectory:dir,parentCircuitSha256:hash(parent),candidateCircuitSha256:hash(child),replacements:patches},'host-bundle.json':host,'routing-input.json':input}))writeFileSync(`${out}/${n}`,JSON.stringify(v,null,2));const audit=spawnSync('bun',['scripts/audit-ddr-global-power-launches.ts',out,'dist/ddr-system/host-taps-54af3e346b5e'],{encoding:'utf8'});writeFileSync(`${out}/power-audit-output.txt`,audit.stdout+audit.stderr);if(audit.status!==0)throw Error('Fresh power audit failed');const beforePower=read(`${dir}/ram-power-launch-audit.json`),afterPower=read(`${out}/ram-power-launch-audit.json`),regressions=beforePower.rows.filter((r:any)=>r.launchWithin1524Mm&&!afterPower.rows.find((a:any)=>a.byte===r.byte&&a.ball===r.ball)?.launchWithin1524Mm),newE9=afterPower.rows.find((r:any)=>r.byte===1&&r.ball==='E9'),shared=afterPower.rows.filter((r:any)=>r.connectionVia&&newE9.connectionVia&&Math.hypot(r.connectionVia.x-newE9.connectionVia.x,r.connectionVia.y-newE9.connectionVia.y)<1e-7);if(regressions.length||afterPower.total!==60||!newE9.launchWithin1524Mm||shared.length!==2||!shared.every((r:any)=>r.byte===1&&['E9','G8'].includes(r.ball)))throw Error('Power ownership/launch regression');Object.assign(report,{powerNoRegression:{before:beforePower.launchQualified,after:afterPower.launchQualified,total:60,regressions,sharedViaPads:shared.map((r:any)=>({byte:r.byte,ball:r.ball,lengthMm:r.actualPadToContactViaMm})),allPriorQualifiedRetained:true}});writeFileSync(`${out}/report.json`,JSON.stringify(report,null,2));console.log(JSON.stringify({accepted,minimumWebMm,newCoverage,planeForeignViolations:planeForeignViolations.length,topology:topology.valid,angles:angles.valid,junctions:junctions.valid,power:JSON.parse(readFileSync(`${out}/ram-power-launch-audit.json`,'utf8')).launchQualified}))