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/assemble-ddr-clock-e7.ts
/** Bind a verified clock/E7/host/plane proposal to complete board-specific caches. */
import { readFileSync,writeFileSync,mkdirSync,copyFileSync,existsSync } from 'node:fs'
import { resolve } from 'node:path'
import { objectHash as hash } from './improve-ddr-a9-power-launches'
import { verifyDdrCapture } from './ddr-capture-provenance'
import { fanoutTracePath } from '@tscircuit/props'
import { auditCompositeHostTopology } from './ddr-composite-routing-context'
import { auditRouteAngles } from './check-route-angles'
import { auditDdrTraceJunctions,verifyDdrSystemCopper } from './check-ddr-system-copper'
import { finalizeDdrEvidenceMetadata } from './finalize-ddr-evidence-metadata'
import { refreshDdrPlaneContacts } from './ddr-plane-contact-audit'
const [proposalArg,outArg]=process.argv.slice(2);if(!outArg)throw Error('Usage: verified-clock-plane-proposal output')
const parent=resolve('dist/ddr-twenty-one-power59-current'),proposal=resolve(proposalArg),out=resolve(outArg),capture=resolve('dist/ddr-system/host-taps-54af3e346b5e'),read=(p:string)=>JSON.parse(readFileSync(p,'utf8')),{snapshot,captureHash}=verifyDdrCapture(capture),old=read(parent+'/candidate.circuit.json'),prior=read(parent+'/report.json'),oldHost=read(parent+'/host-bundle.json'),oldInput=read(parent+'/routing-input.json'),candidate=read(proposal+'/candidate.circuit.json'),proposedReport=read(proposal+'/report.json'),planeManifest=read(proposal+'/original-copper-replacements.json'),comp=read('dist/ddr-clock-e7-compensated/host-replacement.json'),clock=read('dist/ddr-clock-e7-local/input-evidence.json'),e7=read('dist/ddr-clock-e7-local/e7-replacement.json')
if(existsSync(out))throw Error('Output must be new')
if(hash(old)!==prior.candidateCircuitSha256||hash(candidate)!==proposedReport.candidateCircuitSha256||!proposedReport.accepted)throw Error('Proposal must be physically accepted and hash bound')
const id=(e:any)=>`${e.type}:${e[`${e.type}_id`]}`
if(planeManifest.parentCircuitSha256!==hash(old)||planeManifest.candidateCircuitSha256!==hash(candidate))throw Error('Manifest parent/child mismatch')
const declared=[...planeManifest.replacements,...planeManifest.removals];if(new Set(declared.map((r:any)=>r.id)).size!==declared.length)throw Error('Duplicate declared change')
for(const r of declared){const e=old.find((e:any)=>id(e)===r.id);if(!e||hash(e)!==hash(r.before)||hash(e)!==r.beforeSha256)throw Error('Declared before mismatch');if(r.after&&hash(r.after)!==r.afterSha256)throw Error('Declared after mismatch')}
const replay=old.filter((e:any)=>!planeManifest.removals.some((r:any)=>r.id===id(e))).map((e:any)=>planeManifest.replacements.find((r:any)=>r.id===id(e))?.after??e);if(hash(replay)!==hash(candidate))throw Error('Undeclared candidate mutation')
const preservation={valid:true,replacements:planeManifest.replacements.length,removals:planeManifest.removals.length,allOtherElementsExact:true},hosts={...oldHost,traces:oldHost.traces.map((t:any)=>{if(t.pcb_trace_id!==comp.before.pcb_trace_id)return t;if(hash(t)!==hash(comp.before))throw Error('Host before mismatch');return comp.after})},ids=new Set(hosts.traces.map((t:any)=>t.pcb_trace_id))
if(hash(candidate.filter((t:any)=>t.type==='pcb_trace'&&ids.has(t.pcb_trace_id)).sort((a:any,b:any)=>a.pcb_trace_id.localeCompare(b.pcb_trace_id)))!==hash([...hosts.traces].sort((a:any,b:any)=>a.pcb_trace_id.localeCompare(b.pcb_trace_id))))throw Error('Host membership or exact geometry mismatch')
const connections=oldInput.connections.map((n:any)=>({...n,pointsToConnect:n.pointsToConnect.map((p:any)=>{const e=candidate.find((e:any)=>e.type==='pcb_breakout_point'&&e.pcb_breakout_point_id===p.pointId);return e?{...p,x:e.x,y:e.y,layer:e.layer}:p})})),topology=auditCompositeHostTopology(candidate,hosts.traces,connections),angles=auditRouteAngles(candidate.filter((e:any)=>e.type==='pcb_trace')),joins=auditDdrTraceJunctions(candidate,connections),fresh=[...hosts.traces,...clock.clockChanges.map((r:any)=>r.after),e7.after],freshIds=new Set(fresh.map((t:any)=>t.pcb_trace_id)),physical=verifyDdrSystemCopper(candidate.filter((e:any)=>!freshIds.has(e.pcb_trace_id)),fresh,connections,{},20),planeAudit=refreshDdrPlaneContacts(prior.planeAudit,candidate.filter((e:any)=>e.type==='pcb_copper_pour'),candidate)
if(!topology.valid||!angles.valid||!joins.valid||physical.errors.length||physical.violations.length)throw Error('Final actual copper gate failed')
const caches=[0,1].map(byte=>{const cache=read(parent+`/ram-byte${byte}-facing-progress.trace-paths.json`),previous=structuredClone(cache),contract=read(parent+'/'+(byte?'ram-byte1-exit-contract.json':'exit-contract.json')),placement=snapshot.placements[byte],changed=[...clock.clockChanges.filter((r:any)=>r.before.pcb_trace_id.includes(`pcb_group_${byte?5:3}_`)),...(byte===0?[e7]:[])],exits=[...contract.exits]
if(cache.length!==72||placement.pcbRotation!==180)throw Error('Unsupported cache/placement')
for(const change of changed){const trace=candidate.find((e:any)=>e.type==='pcb_trace'&&e.pcb_trace_id===change.after.pcb_trace_id);if(hash(trace)!==hash(change.after))throw Error('Local trace differs from proposal');const sourcePort=candidate.find((e:any)=>e.type==='pcb_port'&&e.pcb_port_id===trace.route[0].start_pcb_port_id),port=candidate.find((e:any)=>e.type==='source_port'&&e.source_port_id===sourcePort?.source_port_id),ball=port?.name?.replace('ball_','');if(!['E7','F7','G7'].includes(ball))throw Error('Source-owned cache ball missing');const index=cache.findIndex((p:any)=>p.connection===`U1.ball_${ball}`);if(index<0)throw Error('Cache path missing');cache[index]={...cache[index],route:trace.route.map((p:any)=>p.route_type==='wire'?{route_type:'wire',x:placement.pcbX-p.x,y:placement.pcbY-p.y,layer:p.layer,width:p.width}:{route_type:'via',x:placement.pcbX-p.x,y:placement.pcbY-p.y,from_layer:p.from_layer,to_layer:p.to_layer,via_diameter:p.via_diameter,via_hole_diameter:p.via_hole_diameter})};fanoutTracePath.parse(cache[index]);const end=trace.route.at(-1),prev=[...trace.route].reverse().find((p:any)=>p.route_type==='wire'&&p.layer===end.layer&&Math.hypot(p.x-end.x,p.y-end.y)>1e-7),d=Math.hypot(end.x-prev.x,end.y-prev.y),tx=(end.x-prev.x)/d,ty=(end.y-prev.y)/d,entry={ball,terminal:ball==='F7'?'CK_P':ball==='G7'?'CK_N':'DQ7',worldSide:ball==='E7'?'right':'interior',localSide:ball==='E7'?'left':'interior',worldExit:{x:end.x,y:end.y,layer:end.layer},localExit:{x:placement.pcbX-end.x,y:placement.pcbY-end.y,layer:end.layer},tangent:{world:{x:tx,y:ty},local:{x:-tx,y:-ty}},tangentDegrees:{world:Math.atan2(ty,tx)*180/Math.PI,local:Math.atan2(-ty,-tx)*180/Math.PI},logicalViaTransition:`top->${end.layer}`,physicalViaSpan:'all ten layers',traceWidthMm:.12,change:ball==='E7'?'Local E7 avoids moved CKP barrel; exact D7 host compensation.':'Board-specific diagonal clock tap; common northbound trunk compatible, trunk not routed.'};const existing=exits.findIndex((x:any)=>x.ball===ball);if(existing<0)exits.push(entry);else exits[existing]=entry}
const changedBalls=cache.filter((p:any,i:number)=>hash(p)!==hash(snapshot.pathsByByte[byte][i])).map((p:any)=>p.connection.slice('U1.ball_'.length));return {byte,cache,previous,contract:{...contract,exits,changedBalls,unchangedPaths:72-changedBalls.length,candidateCircuitSha256:hash(candidate),ramCacheSha256:hash(cache),[byte?'ramByte1CacheSha256':'ramCacheSha256']:hash(cache),scope:`Board-specific RAM byte${byte}: ${changedBalls.length} paths changed from frozen capture, ${72-changedBalls.length} retained. Clock taps are direction-compatible local proposals; no complete clock trunk or electrical qualification.`}}})
mkdirSync(out,{recursive:true});const write=(n:string,v:any)=>writeFileSync(out+'/'+n,JSON.stringify(v,null,2)+'\n');write('candidate.circuit.json',candidate);write('host-bundle.json',hosts);write('routing-input.json',{...oldInput,connections,traces:hosts.traces,obstacles:undefined});write('original-copper-replacements.json',planeManifest);write('parent-original-copper-replacements.json',read(parent+'/original-copper-replacements.json'));write('parent-report.json',prior);write('proposal-report.json',proposedReport);copyFileSync(proposal+'/measured-clock-plane-webs.json',out+'/measured-clock-plane-webs.json');write('source-provenance.json',{captureHash,parentSourceProvenance:read(parent+'/source-provenance.json'),parentDirectory:parent,parentCandidateSha256:hash(old),proposalDirectory:proposal,proposalReportSha256:hash(proposedReport),localInputEvidence:clock});for(const e of caches){write(`ram-byte${e.byte}-facing-progress.trace-paths.json`,e.cache);write(e.byte?'ram-byte1-exit-contract.json':'exit-contract.json',e.contract)}
let report={...prior,accepted:true,candidateCircuitSha256:hash(candidate),supportCircuitSha256:hash(candidate),hostTracesSha256:hash(hosts.traces),hostBundleSha256:hash(hosts),originalCopperReplacementsSha256:hash(planeManifest),physical,topology,fullCandidateAngles:angles,fixedJunctionAudit:joins,planeAudit,ramCacheSha256:hash(caches[0].cache),ramByte1CacheSha256:hash(caches[1].cache),clockLocalProposal:{proposalDirectory:proposal,proposalReportSha256:hash(proposedReport),preservation,compensationMm:read('dist/ddr-clock-e7-compensated/report.json').deltaMm,minimumCopperWebMm:.127,clearanceMm:.107,scope:'Explicit provisional local plane-web assumption; fabrication approval outstanding. Common northbound clock trunk direction is compatible with both branch taps; trunk routing remains outstanding.'},scope:'21 of50 hosts retained; byte0 E7/D7 length compensated. Four clock local taps use compatible strict45 branch directions. Actual coupled clock trunk and full DDR qualification remain outstanding.'};write('report.json',report)
for(const script of ['audit-ddr-global-power-launches.ts','audit-ddr-host-reference-coverage.ts']){const p=Bun.spawn(['bun','scripts/'+script,out,capture],{stdout:'inherit',stderr:'inherit'});if(await p.exited)throw Error(script+' failed')}
const power=read(out+'/ram-power-launch-audit.json'),oldPower=read(parent+'/ram-power-launch-audit.json');if(power.launchQualified<oldPower.launchQualified)throw Error('Power regression');report={...report,launchQualified:power.launchQualified,launchAuditSha256:hash(power)};write('report.json',report)
await finalizeDdrEvidenceMetadata(out,capture)
const byte0=read(out+'/current-byte-budgets.json').bytes.find((b:any)=>b.byte===0),strobes=byte0.members.filter((m:any)=>['DQS_P','DQS_N'].includes(m.terminal)),target=strobes.reduce((sum:number,m:any)=>sum+m.measuredTotalPlanarMm,0)/2
if(strobes.length!==2||!Number.isFinite(target)||byte0.members.filter((m:any)=>m.terminal==='DM'||/^DQ[0-7]$/.test(m.terminal)).some((m:any)=>!m.connected||Math.abs(m.measuredTotalPlanarMm-target)>1e-7))throw Error('Actual byte0 matching regressed')
if(hash(read(parent+'/candidate.circuit.json'))!==hash(old)||hash(read(proposal+'/candidate.circuit.json'))!==hash(candidate))throw Error('Input changed');console.log(JSON.stringify({out,candidateHash:hash(candidate),hosts:hosts.traces.length,power:power.launchQualified}))