logigate/app/lib/logic/sim.ts

111 lines
3.2 KiB
TypeScript
Raw Normal View History

// 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<string, number>
clock: number
}
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
for (const c of nl.clocks) this.values[c] = 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)
}
setClock(v: number) {
this.clock = v
for (const c of this.nl.clocks) this.set(c, 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]!
}
carry(): SimCarry {
const gates = new Map<string, number>()
const { gateKeys, gateOut } = this.nl
for (let i = 0; i < gateKeys.length; i++) gates.set(gateKeys[i]!, this.values[gateOut[i]!]!)
return { gates, clock: this.clock }
}
}