Skip to main content
wires uses a deterministic, discrete-time simulation. A simulation is a sequence of states, and time only moves forward when you ask for a new state with step() or run().

States

simulate(definition) returns the first state. set(), step() and run() each return a new state and never change the one you called them on.
Any state can be kept, compared with another, or used as the starting point for different experiments. See Running simulations.

Wires

Before simulating, the hierarchy is elaborated into a flat graph of primitive gates and wires. A wire is one driver plus every port connected to it. Connections add no delay, so all of those ports always carry the same value. A wire is named after its driver, which is either a root input (root.inputs.a) or a primitive output (root.nand.outputs.out). The hierarchy isn’t lost. Each primitive keeps its full path (root.not.g) and its list of composite ancestors. See elaborate().

Delays

  • Every primitive gate has a delay of one tick.
  • Connections have no delay, including a direct connection from an input to an output.

What happens in a step

1

Apply pending changes

Wire changes waiting for this step are applied, in wire-id order. Inputs from set() are among them.
2

Find affected gates

Gates that read a changed wire are marked for evaluation. The very first step evaluates every gate once, even though no input has changed.
3

Evaluate against one set of values

The marked gates are evaluated. Evaluation never changes a wire directly, so every gate in the step sees the same values.
4

Collect changed outputs

An output whose new value differs from its wire’s current value becomes pending for the next step. Unchanged outputs create no events.
5

Advance time

The new state’s tick is one higher.
Gates and wires are processed in sorted id order, so the result never depends on the order you declared components or connections in.

Example: AND from NAND

With a = 1 and b = 1, the standard AND (a NAND followed by a NOT) settles in three ticks: The settled state’s tick is 3, the next tick to be processed.

Stabilization

A state is stable when no wire changes are pending and every gate has had its first evaluation. run() steps until it reaches a stable state. Each run() call may take at most maxTicks steps (default 10_000). If the circuit still hasn’t settled, run() throws a StabilizationError describing the current tick, how many ticks ran, the most recent changes and the gates involved. An unstable circuit can never hang your process, and the state you called run() on is still there to try again from.

Feedback

The graph can contain cycles, and the simulator doesn’t rely on topological sorting. Feedback circuits end up in one of three outcomes:

Determinism and isolation

  • The same definition, inputs and calls always produce the same states and the same trace.
  • States are frozen. Every simulate() call starts a separate simulation, and simulations share nothing but their (immutable) definition.
  • Reading with read, readAll or inspect never advances time or evaluates anything.