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/audit-current-ram-adapter-compatibility.ts

/** Read current external adapter, classify ten source deltas, and audit an in-memory transplant only. */
import{readFileSync,writeFileSync,mkdirSync}from'node:fs'
import{resolve}from'node:path'
import{createHash}from'node:crypto'
import{verifyDdrCapture}from'./ddr-capture-provenance'
import{verifyDdrSystemCopper}from'./check-ddr-system-copper'
import{auditRouteAngles}from'./check-route-angles'
const[dir,out]=process.argv.slice(2),ram=resolve('/Users/ankan/Documents/Codex/2026-09-10/what-x20/outputs/mt41k512m8da-107-it-p-fanout'),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'))},capture=resolve('dist/ddr-system/host-taps-54af3e346b5e'),{captureHash,snapshot}=verifyDdrCapture(capture),c=read(resolve(dir,'candidate.circuit.json')),host=read(resolve(dir,'host-bundle.json')),input=read(resolve(dir,'routing-input.json')),native=read(resolve(ram,'src/generated/bus_grouped.trace-paths.json'))
for(const f of['src/am3352-host.ts','src/am3352-power-escapes.ts','src/am3352-trunk-taps.ts','src/bus-rules.ts','src/pin-map.ts','src/timing.ts','src/terminals.ts','src/saved-paths.ts','src/index.tsx'])bound[resolve(ram,f)]=createHash('sha256').update(readFileSync(resolve(ram,f))).digest('hex')
const{adaptRamPathsToHost}=await import(resolve(ram,'src/am3352-host.ts')),planar=(r:any[])=>r.slice(1).reduce((n:number,b:any,j:number)=>{const a=r[j];return n+(a.route_type==='wire'&&b.route_type==='wire'&&a.layer===b.layer?Math.hypot(a.x-b.x,a.y-b.y):0)},0),rows:any[]=[],changes:any[]=[]
for(const byte of[0,1]){const after=adaptRamPathsToHost(native,byte?'am3352-byte1':'am3352-byte0'),frozen=snapshot.pathsByByte[byte],placement=snapshot.placements[byte],cache=read(resolve(dir,`ram-byte${byte}-facing-progress.trace-paths.json`));for(let n=0;n<after.length;n++){if(hash(after[n])===hash(frozen[n]))continue;const ball=after[n].connection.slice(3),port=c.find((e:any)=>e.type==='source_port'&&e.name===ball&&e.source_component_id===(byte?'source_component_84':'source_component_83')),exit=c.find((e:any)=>e.type==='pcb_breakout_point'&&e.source_port_id===port?.source_port_id),traces=c.filter((e:any)=>e.type==='pcb_trace'&&e.source_trace_id===exit?.source_trace_id);if(traces.length!==1)throw Error('Ambiguous source-owned trace');const old=traces[0],route=after[n].route.map((p:any)=>({...p,x:placement.pcbX-p.x,y:placement.pcbY-p.y}));route[0]={...route[0],start_pcb_port_id:old.route[0].start_pcb_port_id};const next={...old,route},end=route.at(-1),oldCached=cache.find((p:any)=>p.connection===after[n].connection),connected=input.connections.filter((co:any)=>co.pointsToConnect.some((p:any)=>p.pointId===exit.pcb_breakout_point_id)),hosted=connected.flatMap((co:any)=>host.traces.filter((t:any)=>t.connection_name===co.name)),delta=planar(route)-planar(old.route);changes.push({before:old,after:next,exitBefore:exit,exitAfter:{...exit,x:end.x,y:end.y,layer:end.layer}});rows.push({byte,ball,sourcePortId:port.source_port_id,sourceTraceId:old.source_trace_id,pcbTraceId:old.pcb_trace_id,breakoutId:exit.pcb_breakout_point_id,sharedConnections:connected.map((x:any)=>x.name),hosted:hosted.map((x:any)=>x.connection_name),priorExit:{x:exit.x,y:exit.y,layer:exit.layer},currentAdapterExit:{x:end.x,y:end.y,layer:end.layer},priorVias:old.route.filter((p:any)=>p.route_type==='via'),currentVias:route.filter((p:any)=>p.route_type==='via'),priorMm:planar(old.route),currentMm:planar(route),deltaMm:delta,cacheAlreadyOverridesFrozen:hash(oldCached)!==hash(frozen[n]),cacheEqualsCurrent:hash(oldCached)===hash(after[n]),angles:auditRouteAngles([next])})}}
const ids=new Set(changes.map(x=>x.before.pcb_trace_id)),hostIds=new Set(host.traces.map((x:any)=>x.pcb_trace_id)),fixed=c.filter((e:any)=>!ids.has(e.pcb_trace_id)&&!hostIds.has(e.pcb_trace_id)),connections=input.connections.map((co:any)=>({...co,pointsToConnect:co.pointsToConnect.map((p:any)=>{const r=changes.find(x=>x.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(fixed,[...host.traces,...changes.map(x=>x.after)],connections,{},50,{reportSameNetContacts:true})
const collisions=physical.violations.map(v=>({layer:v.layer,gapMm:v.gapMm,a:v.aShape.traceId??v.aShape.join,b:v.bShape.traceId??v.bShape.join,aPoint:v.aShape.a,bPoint:v.bShape.a})),report={captureHash,parentCandidateSha256:hash(c),sourceHashes:bound,nativeSha256:hash(native),nativeMatchesFrozen:hash(native)===snapshot.sourceSha256,rows,diagnostic:{errors:[...new Set(physical.errors)],violations:collisions,hostConnected:physical.connectivity.filter(x=>x.connected).length,angles:physical.angles,joinedBends:physical.joinedBends},scope:'Read-only external source audit. Ten current adapter traces transplanted only in memory; original emitted vias associated with replaced traces removed from diagnostic fixed set, inline replacements include full barrels. Original planes/support remain untouched. Fresh bottom-layer supply/VREF wires trigger signal-only layer-policy errors; actual reported geometric collisions remain relevant. This is not an accepted update.'}
for(const[p,h]of Object.entries(bound))if(createHash('sha256').update(readFileSync(p)).digest('hex')!==h)throw Error('Source changed during audit');mkdirSync(out,{recursive:true});writeFileSync(resolve(out,'report.json'),JSON.stringify(report,null,2));console.log(JSON.stringify({rows:rows.map(r=>({byte:r.byte,ball:r.ball,trace:r.pcbTraceId,hosted:r.hosted,deltaMm:r.deltaMm,exit:r.currentAdapterExit,cacheOverride:r.cacheAlreadyOverridesFrozen})),errors:report.diagnostic.errors,violations:collisions.length,first:collisions.slice(0,8)}))