2026-10-17 10:49:38 +00:00
|
|
|
// 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]!
|
|
|
|
|
}
|
|
|
|
|
|
A component faces, project library with share links, fine-grained wire editing
- Lamps and hex displays inside a component show on its box with their
labels, lit by that instance's state; Counter 4 example shows its count.
- Several projects per browser; share links at /p/<id> that the creator's
edits save to automatically, others get their own copy. Export, open a
file as project, or add a file's components to the open project.
- Wires: drag any segment or corner, Alt+drag for a new bend, Ctrl+drag to
branch, double-click selects the whole signal, multi-select, arrow keys,
colour tags.
- Share-link API (POST/GET/PUT /api/p) on Nitro fs storage in data/, mounted
as a volume on edgerealm.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-17 11:21:49 +00:00
|
|
|
/** 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]!
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-17 10:49:38 +00:00
|
|
|
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 }
|
|
|
|
|
}
|
|
|
|
|
}
|