// Analysis on a compiled component: truth table, NAND count, gate delay, // and step-by-step tests (used by challenges). import { compile, type Netlist } from './compile' import { chipPins, type DefLookup } from './geometry' import { Simulator } from './sim' const SETTLE_BUDGET = 2_000_000 export interface TruthRow { ins: number[] outs: number[] /** False when the circuit kept oscillating for this input. */ settled: boolean } export interface TruthTable { inputs: string[] outputs: string[] rows: TruthRow[] } export const MAX_TABLE_INPUTS = 10 export function truthTable(getDef: DefLookup, defId: string): TruthTable | { error: string } { const def = getDef(defId) if (!def) return { error: 'Component not found' } const pins = chipPins(def) const n = pins.inputs.length if (n > MAX_TABLE_INPUTS) return { error: `Too many inputs for a table (${n}, max ${MAX_TABLE_INPUTS})` } if (pins.outputs.length === 0) return { error: 'Add at least one output pin' } const sim = new Simulator(compile(getDef, defId)) sim.run(SETTLE_BUDGET) const rows: TruthRow[] = [] for (let i = 0; i < 1 << n; i++) { const ins = pins.inputs.map((_, k) => (i >> (n - 1 - k)) & 1) pins.inputs.forEach((p, k) => sim.setSource(p.key, ins[k] === 1)) const settled = sim.run(SETTLE_BUDGET) rows.push({ ins, outs: pins.outputs.map(p => sim.pin(p.key, 'a')), settled }) } return { inputs: pins.inputs.map(p => p.label), outputs: pins.outputs.map(p => p.label), rows } } export interface Stats { nands: number /** Has feedback loops, so it can store state (latch, flip-flop, counter). */ sequential: boolean /** Longest path in NAND gates; null when there are loops. */ delay: number | null } export function stats(nl: Netlist): Stats { const gates = nl.gateOut.length const driver = new Int32Array(nl.netCount).fill(-1) for (let g = 0; g < gates; g++) driver[nl.gateOut[g]!] = g const indeg = new Int32Array(gates) for (let g = 0; g < gates; g++) { const a = nl.gateA[g]! const b = nl.gateB[g]! if (driver[a]! >= 0) indeg[g]!++ if (b !== a && driver[b]! >= 0) indeg[g]!++ } const depth = new Int32Array(gates) const queue: number[] = [] for (let g = 0; g < gates; g++) if (indeg[g] === 0) queue.push(g) let done = 0 let delay = 0 while (done < queue.length) { const g = queue[done++]! const d = depth[g]! + 1 delay = Math.max(delay, d) const out = nl.gateOut[g]! for (let j = nl.fanStart[out]!; j < nl.fanStart[out + 1]!; j++) { const r = nl.fanList[j]! depth[r] = Math.max(depth[r]!, d) if (--indeg[r]! === 0) queue.push(r) } } const sequential = done < gates return { nands: gates, sequential, delay: sequential ? null : delay } } /** One test step: set inputs (by pin label), let it settle, check outputs. */ export interface TestStep { set: Record expect: Record } export interface TestResult { pass: boolean message: string failedStep?: number } export function runSteps(getDef: DefLookup, defId: string, steps: TestStep[]): TestResult { const def = getDef(defId) if (!def) return { pass: false, message: 'Component not found' } const pins = chipPins(def) const find = (list: { key: string; label: string }[], label: string) => list.find(p => p.label.toLowerCase() === label.toLowerCase()) for (const step of steps) { for (const l of Object.keys(step.set)) { if (!find(pins.inputs, l)) return { pass: false, message: `Missing input pin "${l}"` } } for (const l of Object.keys(step.expect)) { if (!find(pins.outputs, l)) return { pass: false, message: `Missing output pin "${l}"` } } } const sim = new Simulator(compile(getDef, defId)) sim.run(SETTLE_BUDGET) for (let i = 0; i < steps.length; i++) { const step = steps[i]! for (const [l, v] of Object.entries(step.set)) sim.setSource(find(pins.inputs, l)!.key, v === 1) if (!sim.run(SETTLE_BUDGET)) return { pass: false, message: 'The circuit oscillates and never settles', failedStep: i } for (const [l, want] of Object.entries(step.expect)) { const got = sim.pin(find(pins.outputs, l)!.key, 'a') if (got !== want) { const inputs = Object.entries(step.set).map(([k, v]) => `${k}=${v}`).join(' ') return { pass: false, message: `With ${inputs}: ${l} is ${got}, expected ${want}`, failedStep: i } } } } return { pass: true, message: `All ${steps.length} checks pass` } }