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

# Introspection

> Topology, wires, live state and traces as plain data.

Introspection is a first-class feature. There are four views, each serializable with `JSON.stringify`, and together they hold everything a frontend needs to draw a circuit and animate its signals.

| View | Call | Contains |
| - | - | - |
| Topology | `definition.describe()` | The hierarchical structure: components, ports and connections |
| Netlist | `elaborate(definition)` | The flattened graph of primitives and the wires between them |
| State | `state.inspect()` | The current value of every port and wire, plus timing |
| Trace | `state.trace()` | Every wire change with its tick |

## Topology

`describe()` returns a `ComponentDescription` tree rooted at `"root"`:

```ts theme={null}
import { AND } from "@pbzona/wires/components";

const d = AND.describe();
console.log(d.components.map((c) => `${c.id} (${c.definition}, ${c.kind})`));
// ["root.nand (NAND, primitive)", "root.not (NOT, composite)"]
console.log(d.connections[0]);
// { source: "root.inputs.a", destination: "root.nand.inputs.a" }
```

Each node has an `id` (its hierarchical path), its `name`, the `parent` id, the `definition` name, its `kind`, its `ports`, child `components` and internal `connections`. Primitives have empty `components` and `connections`. See [`ComponentDescription`](https://wires.phil.bingo/reference/types#componentdescription).

## Wires

A **wire** is one driver plus every port connected to it. The driver is either one of the circuit's inputs or a gate's output. Connections add no delay, so every port on a wire always has the same value.

A wire is named after its driver. In `AND`, the wire leaving the inner NAND is called `root.nand.outputs.out`, and it also reaches `root.not.inputs.a` and both inputs of the NOT's gate. That's why the trace and the netlist talk about wires, while `signals` lists every port separately.

## Netlist

[`elaborate()`](https://wires.phil.bingo/reference/elaborate) returns the flattened graph that the simulator actually runs:

```ts theme={null}
import { elaborate } from "@pbzona/wires";
import { AND } from "@pbzona/wires/components";

const netlist = elaborate(AND);
console.log(netlist.gates.map((g) => g.path)); // ["root.nand", "root.not.g"]
console.log(netlist.gates[1]?.ancestors);
// [{ id: "root", definition: "AND" }, { id: "root.not", definition: "NOT" }]
console.log(netlist.portWires["root.outputs.out"]); // "root.not.g.outputs.out"
```

Each wire lists its driver, every port on it, and the gates that read it. That's useful for highlighting a whole wire when one port is hovered.

## State

`inspect()` turns a simulation state into plain data:

```ts theme={null}
import { simulate } from "@pbzona/wires";
import { AND } from "@pbzona/wires/components";

const settled = simulate(AND).set({ a: 1, b: 1 }).run();
console.log(settled.inspect());
```

```ts theme={null}
{
  tick: 3,
  stable: true,
  pendingEvents: 0,
  signals: {
    "root.inputs.a": 1,
    "root.inputs.b": 1,
    "root.outputs.out": 1,
    "root.nand.inputs.a": 1,
    "root.nand.inputs.b": 1,
    "root.nand.outputs.out": 0,
    "root.not.inputs.a": 0,
    "root.not.outputs.out": 1,
    "root.not.g.inputs.a": 0,
    "root.not.g.inputs.b": 0,
    "root.not.g.outputs.out": 1
  },
  wires: {
    "root.inputs.a": { value: 1, ports: ["root.inputs.a", "root.nand.inputs.a"] },
    "root.inputs.b": { value: 1, ports: ["root.inputs.b", "root.nand.inputs.b"] },
    "root.nand.outputs.out": {
      value: 0,
      ports: ["root.nand.outputs.out", "root.not.inputs.a", "root.not.g.inputs.a", "root.not.g.inputs.b"]
    },
    "root.not.g.outputs.out": {
      value: 1,
      ports: ["root.outputs.out", "root.not.outputs.out", "root.not.g.outputs.out"]
    }
  }
}
```

* `signals` has an entry for **every port in the hierarchy**, in hierarchical order, so any port in a diagram can be colored directly.
* `wires` groups those ports by wire, so you can see that a value really is the same wire.

The state itself is immutable, and each snapshot is a fresh plain object.

## Trace

Enable tracing with `simulate(definition, { trace: true })`. It's off by default to save memory.

```ts theme={null}
import { simulate } from "@pbzona/wires";
import { AND } from "@pbzona/wires/components";

const settled = simulate(AND, { trace: true }).set({ a: 1, b: 1 }).run();
console.log(settled.trace());
// [
//   { tick: 0, wire: "root.inputs.a", previous: "X", value: 1 },
//   { tick: 0, wire: "root.inputs.b", previous: "X", value: 1 },
//   { tick: 1, wire: "root.nand.outputs.out", previous: "X", value: 0 },
//   { tick: 2, wire: "root.not.g.outputs.out", previous: "X", value: 1 },
// ]
```

* Entries are ordered by tick, and by wire id within a tick, so traces are deterministic.
* `trace()` returns everything from tick 0 up to the state you call it on. Branches each have their own trace, and they share their common history.
* Each state also has `changes`: just the entries added by the step that produced it. That's handy when stepping with `step()`.

Traces are the raw material for timing diagrams and waveform views.
