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/tune-ddr-data-mask.ts
import {readFileSync,writeFileSync,mkdirSync} from 'node:fs'
import {resolve} from 'node:path'
import {pathToFileURL} from 'node:url'
import {verifyDdrCapture} from './ddr-capture-provenance'
import {verifyDdrSystemCopper} from './check-ddr-system-copper'
import {ddrSeedTraceHash} from './ddr-verified-seed'
/** Add a chamfered excursion within an existing westward horizontal segment.
* Endpoints, width, layer and every original via are preserved. */
export function addWestwardTuningExcursion(trace:any,index:number,entryX:number,exitX:number,addedLength:number,sign:number,bevel=.2){
const a=trace.route[index],b=trace.route[index+1]
if(a?.route_type!=='wire'||b?.route_type!=='wire'||a.layer!==b.layer||Math.abs(a.y-b.y)>1e-8||!(a.x>entryX&&entryX>exitX&&exitX>b.x)||entryX-exitX<=4*bevel||addedLength<=0||![-1,1].includes(sign))throw Error('Invalid horizontal tuning interval')
const height=(addedLength+(8-4*Math.SQRT2)*bevel)/2
if(height<=2*bevel)throw Error('Tuning increment too short')
const y=a.y,points=[[entryX,y],[entryX-bevel,y+sign*bevel],[entryX-bevel,y+sign*(height-bevel)],[entryX-2*bevel,y+sign*height],[exitX+2*bevel,y+sign*height],[exitX+bevel,y+sign*(height-bevel)],[exitX+bevel,y+sign*bevel],[exitX,y]].map(([x,y])=>({...a,x,y}))
return {...structuredClone(trace),route:[...structuredClone(trace.route.slice(0,index+1)),...points,...structuredClone(trace.route.slice(index+1))]}
}
if(import.meta.main){
const [captureArg,sourceArg,ramArg,outArg,byteArg,terminalArg]=process.argv.slice(2)
const byte=byteArg===undefined?1:Number(byteArg),terminal=terminalArg??'DM'
if(![0,1].includes(byte)||! /^(DM|DQ[0-7])$/.test(terminal))throw Error('Select byte0/1 and DM or DQ0–DQ7')
if(!captureArg||!sourceArg||!ramArg||!outArg)throw Error('Usage: capture-directory physical-bundle RAM-directory output-directory')
const {captureHash}=verifyDdrCapture(captureArg),read=(p:string)=>JSON.parse(readFileSync(p,'utf8')),source=read(sourceArg)
if(source.captureHash!==captureHash||source.layerSpace!=='physical')throw Error('Mismatched physical source')
const original=read(resolve(captureArg,'unrouted.circuit.json')),connections=read(resolve(captureArg,'routing-input.json')).connections,merged=read(resolve(captureArg,'merged-routing-input.json')).connections
const initialPhysical=verifyDdrSystemCopper(original,source.traces,merged)
if(initialPhysical.errors.length||initialPhysical.violations.length||!initialPhysical.angles.valid||initialPhysical.joinedBends.length)throw Error('Source copper is not physically clean')
const connectedNames=initialPhysical.connectivity.filter(c=>c.connected).map(c=>c.name)
const {auditAM3352HostRoutes}=await import(pathToFileURL(resolve(ramArg,'scripts/audit-am3352-host-routes.ts')).href)
const before=auditAM3352HostRoutes(original,connections,source.traces),mask=before.lengths.find((r:any)=>r.byte===byte&&r.terminal===terminal),pair=before.lengths.filter((r:any)=>r.byte===byte&&['DQS_P','DQS_N'].includes(r.terminal))
if(!mask||pair.length!==2||![mask,...pair].every(r=>Number.isFinite(r.fullPlanarMm)))throw Error('Missing measured mask/strobe paths')
const target=pair.reduce((s:number,r:any)=>s+r.fullPlanarMm,0)/2,delta=target-mask.fullPlanarMm,traceIndex=source.traces.findIndex((t:any)=>t.connection_name===mask.name)
if(delta<=0||traceIndex<0)throw Error('Mask must be shorter than strobe mean')
const trace=source.traces[traceIndex],attempts:any[]=[],out=resolve(outArg);mkdirSync(out,{recursive:true});let accepted:any=null
search:for(let index=0;index<trace.route.length-1;index++)for(const sign of[-1,1])for(const entryX of[-2,-3,-4,-6])for(const exitX of[-5,-7,-10,-14]){
let candidate:any;try{candidate=addWestwardTuningExcursion(trace,index,entryX,exitX,delta,sign)}catch{continue}
const traces=source.traces.map((t:any,i:number)=>i===traceIndex?candidate:t),physical=verifyDdrSystemCopper(original,traces,merged)
const clean=!physical.errors.length&&!physical.violations.length&&physical.angles.valid&&!physical.joinedBends.length&&connectedNames.every(name=>physical.connectivity.some(c=>c.name===name&&c.connected))
attempts.push({index,sign,entryX,exitX,clean,violations:physical.violations.length})
if(clean){
const audit=auditAM3352HostRoutes(original,connections,traces),measured=audit.lengths.find((r:any)=>r.name===mask.name)?.fullPlanarMm
if(!Number.isFinite(measured)||Math.abs(measured-target)>1e-6)throw Error('Extracted length does not match target')
accepted={captureHash,layerSpace:'physical',traces,tuning:{sourceTraceHash:ddrSeedTraceHash(source.traces),signal:terminal==='DM'?`DDR_DQM${byte}`:`DDR_D${byte*8+Number(terminal.slice(2))}`,beforeMm:mask.fullPlanarMm,targetMm:target,measuredMm:measured,addedMm:delta,scope:'Partial planar tuning only; remaining byte data, via delay and SI unverified.'}}
writeFileSync(resolve(out,'candidate.physical.json'),JSON.stringify(accepted,null,2));writeFileSync(resolve(out,'physical-report.json'),JSON.stringify({captureHash,...physical},null,2));writeFileSync(resolve(out,'independent-host-audit.json'),JSON.stringify(audit,null,2));break search
}
}
writeFileSync(resolve(out,'tuning-report.json'),JSON.stringify({captureHash,accepted:!!accepted,attempts,tuning:accepted?.tuning},null,2));console.log(JSON.stringify({accepted:!!accepted,attempts:attempts.length,tuning:accepted?.tuning}));if(!accepted)process.exitCode=1
}