logigate/app/lib/logic/scopeLayout.ts

48 lines
1.6 KiB
TypeScript
Raw Normal View History

// The logic analyzer part's box: an input pin per bit down the left, the
// title bar on top, and a lane per channel beside its pins, the channel's
// value now and its wave over the last samples.
import { colKeys, colPinNames } from './benchLayout'
import type { CircuitNode, ScopeCol } from './types'
/** Samples a lane shows, and pixels each. */
export const SCOPE_SAMPLES = 96
export const SCOPE_CELL = 4
const CHAR = 0.4
export interface ScopeLane {
col: ScopeCol
/** First pin row and how many. */
row: number
bits: number
}
export interface ScopeLayout {
w: number
h: number
pins: { key: string; label: string; dy: number }[]
lanes: ScopeLane[]
/** Where the value column and the waves start, in grid units. */
valueX: number
waveX: number
}
export function scopeLayout(node: CircuitNode): ScopeLayout {
const cols = node.scope?.cols ?? []
const pins: ScopeLayout['pins'] = []
const lanes: ScopeLane[] = []
let row = 1
for (const c of cols) {
const names = colPinNames(c)
colKeys(c).forEach((key, i) => pins.push({ key, label: names[i]!, dy: row + i }))
lanes.push({ col: c, row, bits: c.bits })
row += c.bits
}
const lw = pins.length ? Math.max(...pins.map(p => p.label.length)) * CHAR + 0.7 : 0.4
const valueX = lw + 0.2
const valueW = Math.max(2, ...cols.map(c => (c.bits === 1 ? 1 : String(2 ** c.bits - 1).length + Math.ceil(c.bits / 4) + 4))) * CHAR + 0.4
const waveX = valueX + valueW
const w = cols.length ? Math.ceil(waveX + (SCOPE_SAMPLES * SCOPE_CELL) / 16 + 0.4) : 12
return { w, h: Math.max(row + 0.5, 2.5), pins, lanes, valueX, waveX }
}