2026-09-12 10:52:17 +08:00

64 lines
2.6 KiB
JavaScript

import { readFileSync } from 'node:fs';
const { compData, wireData, pinData } = JSON.parse(readFileSync('H:/caiic_workspace/mothercup/hardware_design/_dump.json', 'utf8'));
// Build a connectivity graph from wire segments. Snap to ~1 unit grid.
const key = (x,y) => `${Math.round(x)},${Math.round(y)}`;
const graph = new Map(); // node -> {neighbors:Set, label:null}
function node(k) { if(!graph.has(k)) graph.set(k,{n:new Set(),label:null}); return graph.get(k); }
function link(a,b){ const A=node(a),B=node(b); A.n.add(b); B.n.add(a); }
for (const w of wireData) {
const p = Array.isArray(w.line) ? (Array.isArray(w.line[0]) ? w.line.flat() : w.line) : [];
const pts = [];
for (let i=0;i+1<p.length;i+=2) pts.push(key(p[i],p[i+1]));
// connect consecutive points
for (let i=0;i+1<pts.length;i++) link(pts[i],pts[i+1]);
// if a wire has a net label, mark ALL its points with the label
if (w.net) for (const k of pts) node(k).label = w.net;
}
// net flags: components with no designator but a name -> mark nearest node
for (const f of compData.filter(c=>(!c.d||c.d.trim()==='')&&c.n)) {
const k = key(f.x,f.y);
const nd = node(k);
if (!nd.label) nd.label = f.n;
}
// Find connected components and collect labels
const visited = new Set();
function component(start) {
const comp = new Set(); const labels = new Set();
const stack=[start]; visited.add(start);
while(stack.length){
const k=stack.pop(); comp.add(k);
const nd=graph.get(k); if(nd.label) labels.add(nd.label);
for(const nb of nd.n) if(!visited.has(nb)){visited.add(nb);stack.push(nb);}
}
return {comp,labels:[...labels]};
}
const components=[];
for(const k of graph.keys()){ if(!visited.has(k)) components.push(component(k)); }
function compOf(x,y){
const k=key(x,y);
for(const c of components) if(c.comp.has(k)) return c;
// nearest node within 2 units
for(const c of components) for(const kk of c.comp){ const [a,b]=kk.split(',').map(Number); if(Math.abs(a-x)<=2&&Math.abs(b-y)<=2) return c; }
return null;
}
console.log('=== key pin -> net (connectivity) ===');
for (const k of ['SW1','Q1','Q7','Q4','Q2','Q3','D1','U5','U1']) {
const pd=pinData[k]; if(!pd){console.log(k+': n/a');continue;}
console.log(`${k} (${pd.name})`);
for(const p of pd.pins){
const c=compOf(p.x,p.y);
console.log(` pin${p.num} ${p.name||''} @(${p.x.toFixed(0)},${p.y.toFixed(0)}) -> ${c?JSON.stringify(c.labels):'??'} (${c?c.comp.size:0} nodes)`);
}
}
console.log('\n=== labeled nets (component size >= 2) ===');
for(const c of components) if(c.labels.length && c.comp.size>=2) console.log(` ${JSON.stringify(c.labels)} : ${c.comp.size} nodes`);