// Event-driven NAND simulator. A changed net queues the gates that read it; // gates run in FIFO order and see the newest values. That settles latches // and flip-flops built from NANDs deterministically, while a loop with an // odd number of inversions (a ring oscillator) never settles and shows as // "oscillating". import { HIGH, type Netlist } from './compile' export interface SimCarry { gates: Map /** Level of all clocks when they ran in lockstep; per-clock levels win. */ clock: number clocks?: Map } export class Simulator { readonly nl: Netlist readonly values: Uint8Array private queue: Int32Array private head = 0 private count = 0 private queued: Uint8Array clock = 0 /** Increments on every net change, so views know when to redraw. */ changes = 0 constructor(nl: Netlist, carry?: SimCarry) { this.nl = nl this.values = new Uint8Array(nl.netCount) this.values[HIGH] = 1 const gates = nl.gateOut.length this.queue = new Int32Array(Math.max(1, gates)) this.queued = new Uint8Array(gates) if (carry) { for (let i = 0; i < gates; i++) { const v = carry.gates.get(nl.gateKeys[i]!) if (v !== undefined) this.values[nl.gateOut[i]!] = v } this.clock = carry.clock nl.clocks.forEach((c, i) => (this.values[c] = carry.clocks?.get(nl.clockKeys[i]!) ?? carry.clock)) } for (let i = 0; i < gates; i++) this.push(i) } private push(g: number) { if (this.queued[g]) return this.queued[g] = 1 this.queue[(this.head + this.count) % this.queue.length] = g this.count++ } get settled() { return this.count === 0 } get pending() { return this.count } /** Drive a net from outside (switch, button, clock). */ set(net: number, v: number) { if (net <= HIGH || this.values[net] === v) return this.values[net] = v this.changes++ const { fanStart, fanList } = this.nl for (let j = fanStart[net]!; j < fanStart[net + 1]!; j++) this.push(fanList[j]!) } setSource(nodeId: string, on: boolean) { const net = this.nl.sources.get(nodeId) if (net !== undefined) this.set(net, on ? 1 : 0) } /** Set every clock to the same level. */ setClock(v: number) { this.clock = v for (const c of this.nl.clocks) this.set(c, v) } /** Flip one clock; returns its new level. */ toggleClock(i: number): number { const net = this.nl.clocks[i]! const v = this.values[net]! ^ 1 this.set(net, v) return v } /** Evaluate up to `budget` gates. Returns true once nothing is left to do. */ run(budget = 1_000_000): boolean { const { gateA, gateB, gateOut, fanStart, fanList } = this.nl const values = this.values const len = this.queue.length while (this.count > 0 && budget-- > 0) { const g = this.queue[this.head]! this.head = (this.head + 1) % len this.count-- this.queued[g] = 0 const v = (values[gateA[g]!]! & values[gateB[g]!]!) ^ 1 const out = gateOut[g]! if (values[out] !== v) { values[out] = v this.changes++ for (let j = fanStart[out]!; j < fanStart[out + 1]!; j++) this.push(fanList[j]!) } } return this.count === 0 } /** Value at a root-circuit pin, 0 if unknown. */ pin(nodeId: string, pin: string): number { const net = this.nl.pinNet.get(`${nodeId}:${pin}`) return net === undefined ? 0 : this.values[net]! } /** What an output pin of the root circuit puts out (a ChipPins output key), after any bubble on it. */ output(key: string): number { const net = this.nl.outputs.get(key) return net === undefined ? 0 : this.values[net]! } /** Value at a lamp or display input inside one of the root circuit's chips. */ face(chipNode: string, node: string, pin: string): number { const net = this.nl.facePinNet.get(`${chipNode}/${node}:${pin}`) return net === undefined ? 0 : this.values[net]! } carry(): SimCarry { const gates = new Map() const { gateKeys, gateOut } = this.nl for (let i = 0; i < gateKeys.length; i++) gates.set(gateKeys[i]!, this.values[gateOut[i]!]!) const clocks = new Map() this.nl.clocks.forEach((c, i) => clocks.set(this.nl.clockKeys[i]!, this.values[c]!)) return { gates, clock: this.clock, clocks } } }