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

# Components

> Definitions, instances, ports, connections and circuits.

`wires` has one central idea: **a primitive gate and a complex circuit expose the same interface.** Anything you define can be used as a building block inside something else.

## Component definitions

A `ComponentDefinition` is a reusable, immutable description of a component. There are two kinds:

| Kind | Created with | Behavior comes from |
| - | - | - |
| `"primitive"` | [`gate()`](https://wires.phil.bingo/reference/gate) | An explicit `evaluate` function |
| `"composite"` | [`circuit()`](https://wires.phil.bingo/reference/circuit) | Other components wired together |

Both kinds have the same shape:

```ts theme={null}
interface ComponentDefinition<I extends string, O extends string> {
  readonly kind: "primitive" | "composite";
  readonly name: string;
  readonly inputs: readonly I[];
  readonly outputs: readonly O[];
  describe(): ComponentDescription;
}
```

Definitions are frozen and hold no simulation state, so you can share one definition across any number of circuits and simulations.

## Instances

When a circuit calls `add(definition, name?, inputs?)`, it creates a `ComponentInstance`, which is one part made from that definition. Two parts made from the same definition are fully independent. They have different names, different hierarchical paths and separate signal values.

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

const TwoInverters = circuit(
  { name: "TwoInverters", inputs: ["a", "b"], outputs: ["x", "y"] },
  ({ inputs, add, output }) => {
    const first = add(NOT, { a: inputs.a }); // named "NOT#0"
    const second = add(NOT, { a: inputs.b }); // named "NOT#1"
    output("x", first.outputs.out);
    output("y", second.outputs.out);
  },
);
```

## Ports

A port is a named terminal on a component. Each port has:

* an `id` unique within its component, such as `"inputs.a"` or `"outputs.out"`
* a `name` and a `direction` (`"input"` or `"output"`)
* an `owner`, which is either an instance or the circuit being defined
* a `role` that says how the port can be used in the current circuit: `"source"` if it can drive a wire, `"sink"` if it can only be fed

Ports are structural references only. They never hold values.

### Sources

Inside a circuit, signals flow from **sources** to the inputs of parts and to the circuit's own outputs. A source is either:

* one of the circuit's own inputs (`inputs.a`), or
* an output of a part the circuit added (`g.outputs.out`).

A part's inputs (`g.inputs.a`) are never sources. You only ever say what *feeds* them. Using one as a source is a compile-time error, and the same check runs again at runtime.

## Connections

A connection links one source to one input or output. Each wiring call in a builder reads **destination ← source**:

| Call | Connects |
| - | - |
| `add(NOT, "not", { a: nand.outputs.out })` | `not`'s input `a` ← `nand`'s output |
| `feed(top.inputs.b, bottom.outputs.out)` | `top`'s input `b` ← `bottom`'s output |
| `output("out", not.outputs.out)` | the circuit's output `out` ← `not`'s output |

* **Fan-out is allowed.** One source can feed any number of inputs.
* **One driver per input.** Giving an input or output a second source throws a `CircuitError`.
* **Cycles are allowed.** Feedback is how memory works. See [Sequential logic](https://wires.phil.bingo/guides/sequential-logic.md).

## Circuits and hierarchy

A circuit is a hierarchical graph of external ports, child instances and connections. Every element gets a stable hierarchical id rooted at `root`:

```text theme={null}
root                     the circuit being simulated or described
root.inputs.a            one of its input ports
root.not                 a child instance named "not"
root.not.g               a NAND gate inside that child
root.not.g.outputs.out   that gate's output port
```

These ids are identical across runs, and they are the keys used by [`describe()`](https://wires.phil.bingo/guides/introspection), [`elaborate()`](https://wires.phil.bingo/reference/elaborate), `inspect()` and `trace()`.
