// Where everything on a test bench's box goes: check pins down the left, // drive pins down the right, the title bar with its run button, and the // table between them. The shape (pins) and the drawing both use this. import type { BenchCol, CircuitNode } from './types' /** Pin keys of a column, bit 0 first. */ export const colKeys = (c: Pick) => Array.from({ length: c.bits }, (_, i) => `${c.id}.${i}`) /** A column's pin names: its name, numbered when it has several bits. */ export const colPinNames = (c: Pick) => (c.bits === 1 ? [c.name] : Array.from({ length: c.bits }, (_, i) => `${c.name}${i}`)) /** Rows the box shows; more are in the dialog. */ export const BENCH_ROWS_SHOWN = 24 const CHAR = 0.4 // grid units per character at the table's size const TITLE_H = 1.4 export interface BenchCell { key: string /** Column id, '#' for the row number, 'ticks', or 'state'. */ col: string x: number w: number head: string drive?: boolean } export interface BenchLayout { w: number h: number /** Pin rows on each side: key and name. */ left: { key: string; label: string }[] right: { key: string; label: string }[] /** Table: left edge, top of the header row, its columns, rows shown, rows not shown. */ x: number top: number cols: BenchCell[] shown: number more: number tableW: number } export function benchLayout(node: CircuitNode): BenchLayout { const b = node.bench const cols = b?.cols ?? [] const rows = b?.rows ?? [] const checks = cols.filter(c => c.dir === 'check') const drives = cols.filter(c => c.dir === 'drive') const left = checks.flatMap(c => colKeys(c).map((key, i) => ({ key, label: colPinNames(c)[i]! }))) const right = drives.flatMap(c => colKeys(c).map((key, i) => ({ key, label: colPinNames(c)[i]! }))) const labelW = (list: { label: string }[]) => (list.length ? Math.max(...list.map(p => p.label.length)) * CHAR + 0.7 : 0.3) const lw = labelW(left) const rw = labelW(right) const shown = Math.min(rows.length, BENCH_ROWS_SHOWN) const width = (head: string, cells: string[]) => Math.max(head.length, 1, ...cells.slice(0, shown).map(s => s.length)) * CHAR + 0.6 const table: Omit[] = [{ key: '#', col: '#', w: width('#', [String(shown)]), head: '#' }] for (const c of [...drives, ...checks]) { const cells = rows.map(r => (c.dir === 'drive' ? r.set?.[c.id] : r.expect?.[c.id]) ?? '') table.push({ key: c.id, col: c.id, w: width(c.name, cells), head: c.name, drive: c.dir === 'drive' }) } if (rows.some(r => r.ticks)) table.push({ key: 'ticks', col: 'ticks', w: width('Ticks', rows.map(r => r.ticks ?? '')), head: 'Ticks' }) table.push({ key: 'state', col: 'state', w: 1, head: '' }) let x = lw + 0.2 const placed = table.map(c => { const out = { ...c, x } x += c.w return out }) const tableW = x - lw - 0.2 const w = Math.ceil(Math.max(lw + 0.2 + Math.max(tableW, 9) + 0.2 + rw, 10)) const more = rows.length - shown const tableH = cols.length ? 1 + shown + (more ? 1 : 0) : 1.2 const h = Math.ceil(Math.max(left.length, right.length, TITLE_H + tableH + 0.4)) return { w, h, left, right, x: lw + 0.2, top: -0.5 + TITLE_H, cols: placed, shown, more, tableW } }