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'
|
A memory that stays: circuits keep their bits across visits and saves; Power off and on
What latches, flip-flops, registers and RAM built from NANDs hold is no
longer lost when you go into a component and come back, reload the page or
open the project on another computer. Leaving a circuit puts its bits in
the project, and opening it starts from them.
Only the gates on feedback loops can hold a bit, so only those that are 1
are kept, plus the clocks that are high: each node id once, each gate as
the path levels it shares with the gate before plus the rest. 1,024
flip-flops take about 30 KB. Restoring runs the gates in the order signals
flow, so the gates in front of a latch are right before it reads them;
gates that no longer exist after an edit are ignored.
The bits go with every save, file, share link and account upload. Between
saves (a clock running, a button pressed) they are put in this browser's
copy every 30 seconds and when the page is left, without counting as a
change: no sync, savegame or undo step of their own, and never over a save
made in another tab. Projects without them start from all zeros as before.
"Power off and on" in the menu on the circuit's name, or with Space,
starts the open circuit again from all zeros.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 17:09:18 +00:00
|
|
|
import { flow } from './state'
|
2026-10-17 10:49:38 +00:00
|
|
|
|
A memory that stays: circuits keep their bits across visits and saves; Power off and on
What latches, flip-flops, registers and RAM built from NANDs hold is no
longer lost when you go into a component and come back, reload the page or
open the project on another computer. Leaving a circuit puts its bits in
the project, and opening it starts from them.
Only the gates on feedback loops can hold a bit, so only those that are 1
are kept, plus the clocks that are high: each node id once, each gate as
the path levels it shares with the gate before plus the rest. 1,024
flip-flops take about 30 KB. Restoring runs the gates in the order signals
flow, so the gates in front of a latch are right before it reads them;
gates that no longer exist after an edit are ignored.
The bits go with every save, file, share link and account upload. Between
saves (a clock running, a button pressed) they are put in this browser's
copy every 30 seconds and when the page is left, without counting as a
change: no sync, savegame or undo step of their own, and never over a save
made in another tab. Projects without them start from all zeros as before.
"Power off and on" in the menu on the circuit's name, or with Space,
starts the open circuit again from all zeros.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 17:09:18 +00:00
|
|
|
/** Gate outputs and clock levels to start from, by key (see state.ts for what is saved). */
|
2026-10-17 10:49:38 +00:00
|
|
|
export interface SimCarry {
|
|
|
|
|
gates: Map<string, number>
|
A settings for every part, overlap-free wiring with crossing styles, accounts with sync
- Clicking anything opens its settings: swap parts in place (wires stay on
matching pins), clock frequency per clock, keyboard keys for switches and
buttons, lamp/display colours, note sizes, component names on the canvas,
position, unlink all wires, unpack a component, make an independent copy.
- Wires of different signals never share a line: auto routes pick free
columns or detours, remaining clashes go on separate lanes. Crossings drawn
as bow, triangle, bridge, gap or plain.
- Accounts (email + scrypt password, httpOnly session) with two-way project
sync and conflict copies; per-account limits; cross-site writes refused.
- Compatibility suite: a project and both browser-storage layouts from
release a241adc must load and compute identically.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-20 11:09:37 +00:00
|
|
|
/** Level of all clocks when they ran in lockstep; per-clock levels win. */
|
2026-10-17 10:49:38 +00:00
|
|
|
clock: number
|
A settings for every part, overlap-free wiring with crossing styles, accounts with sync
- Clicking anything opens its settings: swap parts in place (wires stay on
matching pins), clock frequency per clock, keyboard keys for switches and
buttons, lamp/display colours, note sizes, component names on the canvas,
position, unlink all wires, unpack a component, make an independent copy.
- Wires of different signals never share a line: auto routes pick free
columns or detours, remaining clashes go on separate lanes. Crossings drawn
as bow, triangle, bridge, gap or plain.
- Accounts (email + scrypt password, httpOnly session) with two-way project
sync and conflict copies; per-account limits; cross-site writes refused.
- Compatibility suite: a project and both browser-storage layouts from
release a241adc must load and compute identically.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-20 11:09:37 +00:00
|
|
|
clocks?: Map<string, number>
|
2026-10-17 10:49:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
A settings for every part, overlap-free wiring with crossing styles, accounts with sync
- Clicking anything opens its settings: swap parts in place (wires stay on
matching pins), clock frequency per clock, keyboard keys for switches and
buttons, lamp/display colours, note sizes, component names on the canvas,
position, unlink all wires, unpack a component, make an independent copy.
- Wires of different signals never share a line: auto routes pick free
columns or detours, remaining clashes go on separate lanes. Crossings drawn
as bow, triangle, bridge, gap or plain.
- Accounts (email + scrypt password, httpOnly session) with two-way project
sync and conflict copies; per-account limits; cross-site writes refused.
- Compatibility suite: a project and both browser-storage layouts from
release a241adc must load and compute identically.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-20 11:09:37 +00:00
|
|
|
nl.clocks.forEach((c, i) => (this.values[c] = carry.clocks?.get(nl.clockKeys[i]!) ?? carry.clock))
|
A memory that stays: circuits keep their bits across visits and saves; Power off and on
What latches, flip-flops, registers and RAM built from NANDs hold is no
longer lost when you go into a component and come back, reload the page or
open the project on another computer. Leaving a circuit puts its bits in
the project, and opening it starts from them.
Only the gates on feedback loops can hold a bit, so only those that are 1
are kept, plus the clocks that are high: each node id once, each gate as
the path levels it shares with the gate before plus the rest. 1,024
flip-flops take about 30 KB. Restoring runs the gates in the order signals
flow, so the gates in front of a latch are right before it reads them;
gates that no longer exist after an edit are ignored.
The bits go with every save, file, share link and account upload. Between
saves (a clock running, a button pressed) they are put in this browser's
copy every 30 seconds and when the page is left, without counting as a
change: no sync, savegame or undo step of their own, and never over a save
made in another tab. Projects without them start from all zeros as before.
"Power off and on" in the menu on the circuit's name, or with Space,
starts the open circuit again from all zeros.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 17:09:18 +00:00
|
|
|
// Gates run in the order signals flow, so a latch reads its inputs only once they are right and keeps its bit.
|
|
|
|
|
for (const g of flow(nl).order) this.push(g)
|
|
|
|
|
} else for (let i = 0; i < gates; i++) this.push(i)
|
2026-10-17 10:49:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
A settings for every part, overlap-free wiring with crossing styles, accounts with sync
- Clicking anything opens its settings: swap parts in place (wires stay on
matching pins), clock frequency per clock, keyboard keys for switches and
buttons, lamp/display colours, note sizes, component names on the canvas,
position, unlink all wires, unpack a component, make an independent copy.
- Wires of different signals never share a line: auto routes pick free
columns or detours, remaining clashes go on separate lanes. Crossings drawn
as bow, triangle, bridge, gap or plain.
- Accounts (email + scrypt password, httpOnly session) with two-way project
sync and conflict copies; per-account limits; cross-site writes refused.
- Compatibility suite: a project and both browser-storage layouts from
release a241adc must load and compute identically.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-20 11:09:37 +00:00
|
|
|
/** Set every clock to the same level. */
|
2026-10-17 10:49:38 +00:00
|
|
|
setClock(v: number) {
|
|
|
|
|
this.clock = v
|
|
|
|
|
for (const c of this.nl.clocks) this.set(c, v)
|
|
|
|
|
}
|
|
|
|
|
|
A settings for every part, overlap-free wiring with crossing styles, accounts with sync
- Clicking anything opens its settings: swap parts in place (wires stay on
matching pins), clock frequency per clock, keyboard keys for switches and
buttons, lamp/display colours, note sizes, component names on the canvas,
position, unlink all wires, unpack a component, make an independent copy.
- Wires of different signals never share a line: auto routes pick free
columns or detours, remaining clashes go on separate lanes. Crossings drawn
as bow, triangle, bridge, gap or plain.
- Accounts (email + scrypt password, httpOnly session) with two-way project
sync and conflict copies; per-account limits; cross-site writes refused.
- Compatibility suite: a project and both browser-storage layouts from
release a241adc must load and compute identically.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-20 11:09:37 +00:00
|
|
|
/** 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
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-17 10:49:38 +00:00
|
|
|
/** 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]!
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-23 10:27:12 +00:00
|
|
|
/** 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]!
|
|
|
|
|
}
|
|
|
|
|
|
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]!]!)
|
A settings for every part, overlap-free wiring with crossing styles, accounts with sync
- Clicking anything opens its settings: swap parts in place (wires stay on
matching pins), clock frequency per clock, keyboard keys for switches and
buttons, lamp/display colours, note sizes, component names on the canvas,
position, unlink all wires, unpack a component, make an independent copy.
- Wires of different signals never share a line: auto routes pick free
columns or detours, remaining clashes go on separate lanes. Crossings drawn
as bow, triangle, bridge, gap or plain.
- Accounts (email + scrypt password, httpOnly session) with two-way project
sync and conflict copies; per-account limits; cross-site writes refused.
- Compatibility suite: a project and both browser-storage layouts from
release a241adc must load and compute identically.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-20 11:09:37 +00:00
|
|
|
const clocks = new Map<string, number>()
|
|
|
|
|
this.nl.clocks.forEach((c, i) => clocks.set(this.nl.clockKeys[i]!, this.values[c]!))
|
|
|
|
|
return { gates, clock: this.clock, clocks }
|
2026-10-17 10:49:38 +00:00
|
|
|
}
|
|
|
|
|
}
|