56 lines
2.8 KiB
TypeScript
56 lines
2.8 KiB
TypeScript
|
|
// The logic analyzer's signals: wires and cables of a circuit you watch.
|
||
|
|
// Each is a pin of a part there (an output, or an input's wire); a cable
|
||
|
|
// pin reads its whole cable as one number, wire 0 the lowest bit.
|
||
|
|
|
||
|
|
import { cableBits, shapeOf, type DefLookup } from './geometry'
|
||
|
|
import { pinName } from './labels'
|
||
|
|
import type { CircuitNode, ComponentDef, Probe } from './types'
|
||
|
|
|
||
|
|
/** How many bits a signal has: 1 for a wire, the cable's wires for a cable pin. */
|
||
|
|
export function probeBits(def: ComponentDef, p: Probe): number {
|
||
|
|
const n = def.nodes.find(x => x.id === p.node)
|
||
|
|
if ((n?.kind === 'bundle' && p.pin === 'q') || (n?.kind === 'split' && p.pin === 'a')) return cableBits(n)
|
||
|
|
return 1
|
||
|
|
}
|
||
|
|
|
||
|
|
/** A signal's value now, read with \`pin(node, pin)\` (the simulator's). */
|
||
|
|
export function probeValue(def: ComponentDef, p: Probe, pin: (node: string, pin: string) => number): number {
|
||
|
|
const n = def.nodes.find(x => x.id === p.node)
|
||
|
|
if (n && ((n.kind === 'bundle' && p.pin === 'q') || (n.kind === 'split' && p.pin === 'a'))) {
|
||
|
|
// A cable: its wires as they leave the bundle (a split reads the bundle that feeds it).
|
||
|
|
const bundle = n.kind === 'bundle' ? n : def.nodes.find(x => x.id === def.wires.find(w => w.to.node === n.id && w.to.pin === 'a')?.from.node)
|
||
|
|
if (!bundle || bundle.kind !== 'bundle') return 0
|
||
|
|
let v = 0
|
||
|
|
for (let i = 0; i < cableBits(n); i++) if (pin(bundle.id, `b${i}`)) v += 2 ** i
|
||
|
|
return v
|
||
|
|
}
|
||
|
|
return pin(p.node, p.pin)
|
||
|
|
}
|
||
|
|
|
||
|
|
/** What a signal is called: its own name, else the pin's or the part's. */
|
||
|
|
export function probeName(def: ComponentDef, p: Probe, get: DefLookup): string {
|
||
|
|
if (p.label?.trim()) return p.label.trim()
|
||
|
|
const n = def.nodes.find(x => x.id === p.node)
|
||
|
|
if (!n) return '?'
|
||
|
|
const own = n.label?.trim() && !n.autoLabel ? n.label.trim() : ''
|
||
|
|
const pin = pinName(n, p.pin, get)
|
||
|
|
const part = own || (n.kind === 'chip' ? (n.chip && get(n.chip)?.name) || 'Component' : n.kind)
|
||
|
|
if (n.kind === 'bundle' || n.kind === 'split') return own || `Cable ${probeBits(def, p)}`
|
||
|
|
if (['input', 'button', 'clock', 'output', 'lamp'].includes(n.kind)) return own || part
|
||
|
|
return pin ? `${part} ${pin}` : part
|
||
|
|
}
|
||
|
|
|
||
|
|
/** The signal a wire carries: the pin it comes from. */
|
||
|
|
export const probeOfWire = (w: { from: { node: string; pin: string } }): Probe => ({ node: w.from.node, pin: w.from.pin })
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The signal at a pin: an output itself, an input by the wire into it (or,
|
||
|
|
* with none, the pin, which reads 0).
|
||
|
|
*/
|
||
|
|
export function probeAt(def: ComponentDef, node: CircuitNode, pin: string, dir: 'in' | 'out', get: DefLookup): Probe | null {
|
||
|
|
if (dir === 'out') return { node: node.id, pin }
|
||
|
|
const w = def.wires.find(x => x.to.node === node.id && x.to.pin === pin)
|
||
|
|
if (w) return probeOfWire(w)
|
||
|
|
return shapeOf(node, get).inputs.some(p => p.key === pin) ? { node: node.id, pin } : null
|
||
|
|
}
|