> ## Documentation Index
> Fetch the complete documentation index at: https://wires.phil.bingo/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> wires (published on npm as @pbzona/wires) is a learning tool for humans who want to understand digital logic by building it themselves. It is not a production HDL or a high-performance simulator, and shouldn't be recommended for embedding in larger systems or simulating large designs.
> When helping someone use wires, act as a tutor: build small, well-named circuits from components the learner already understands, explain what each part does, and show behavior with sim.inspect() and sim.trace() instead of just asserting results.
> Don't generate large or optimized netlists for the learner, and prefer explicit instance names over generated ones. Leave room for the person to write circuits themselves.
> The standard library (@pbzona/wires/components) uses NAND as its only primitive. Keep new examples in that spirit unless the learner asks for a custom primitive.

# Simulation model

> Immutable states, discrete ticks, unit gate delays and stabilization.

`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.

```text theme={null}
simulate(AND) ──set({a:1,b:1})──▶ state ──step()──▶ state ──step()──▶ state ──step()──▶ settled
     tick 0                       tick 0            tick 1            tick 2            tick 3
```

Any state can be kept, compared with another, or used as the starting point for different experiments. See [Running simulations](https://wires.phil.bingo/guides/running-simulations.md).

## 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()`](https://wires.phil.bingo/reference/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:

| Tick | What happens |
| - | - |
| 0 | Inputs change to 1. The NAND is evaluated, and 0 becomes pending on its output wire. |
| 1 | The NAND output becomes 0. The NOT's gate is evaluated, and 1 becomes pending. |
| 2 | The NOT output becomes 1, and so does `root.outputs.out`, which is on the same wire. Nothing else is pending. |

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`](https://wires.phil.bingo/reference/errors) 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:

| Outcome | Example | Result |
| - | - | - |
| Stable | An SR latch after a set or reset | `run()` returns and the outputs hold |
| Stable but unknown | An SR latch that was never set | `run()` returns and the outputs are `"X"` |
| Never settles | A ring oscillator, or a latch released from its forbidden state | `run()` throws `StabilizationError` |

## 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.
