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

# circuit()

> Define a composite circuit from other components.

```ts theme={null}
function circuit<const I extends readonly string[], const O extends readonly string[]>(
  spec: CircuitSpec<I, O>,
  build: (builder: CircuitBuilder<I[number], O[number]>) => void,
): ComponentDefinition<I[number], O[number]>;
```

## Parameters

**spec.name** `string` *required*

The definition name. Must be an identifier.

**spec.inputs** `readonly string[]` *required*

External input names, inferred as literal types.

**spec.outputs** `readonly string[]` *required*

External output names, inferred as literal types. Must not repeat any input name.

**build** `(builder: CircuitBuilder) =\> void` *required*

A synchronous callback that adds parts and wires them. It runs **immediately**, exactly once, and its result is validated before `circuit()` returns.

## Returns

A frozen [`ComponentDefinition`](https://wires.phil.bingo/reference/types#componentdefinition) with `kind: "composite"`.

## CircuitBuilder

Every wiring call reads **destination ← source**.

**inputs** `\{ [name]: SourcePort \}`

The circuit's own inputs. Use them as sources for the parts you add.

**add** `(definition, name?, inputs?) =\> ComponentInstance`

Adds a part made from any definition, and wires the inputs listed in `inputs`, an object like `{ a: source, b: source }`. Inputs you leave out must be wired later with `feed`. Without `name`, a deterministic name `<Definition>#<n>` is generated, and the inputs object moves to the second argument: `add(NOT, { a: inputs.a })`. The returned part exposes `outputs` (sources) and `inputs` (only for `feed`).

**feed** `(input: SinkPort, source: SourcePort) =\> void`

Wires an input of a part you already added. This is how feedback loops are built, because the source can be a part added after the one it feeds.

**output** `(name: O, source: SourcePort) =\> void`

Drives one of the circuit's own outputs from a source. Each output must be driven exactly once.

## Example

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

const AND = circuit(
  { name: "AND", inputs: ["a", "b"], outputs: ["out"] },
  ({ inputs, add, output }) => {
    const nand = add(NAND, "nand", { a: inputs.a, b: inputs.b });
    const not = add(NOT, "not", { a: nand.outputs.out });
    output("out", not.outputs.out);
  },
);
```

With feedback:

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

const SRLatch = circuit(
  { name: "SRLatch", inputs: ["setN", "resetN"], outputs: ["q", "qBar"] },
  ({ inputs, add, feed, output }) => {
    const top = add(NAND, "top", { a: inputs.setN });
    const bottom = add(NAND, "bottom", { a: inputs.resetN, b: top.outputs.out });
    feed(top.inputs.b, bottom.outputs.out);
    output("q", top.outputs.out);
    output("qBar", bottom.outputs.out);
  },
);
```

## Errors

Every error is a `CircuitError` whose message is prefixed with the circuit name.

| When | Problem |
| - | - |
| `circuit()` call | Invalid or duplicate port names, invalid circuit name, missing builder |
| `add()` | Not a definition, invalid, duplicate or reserved (`inputs`, `outputs`) instance name, or a component that isn't defined yet |
| `add()` inputs | An input name the part doesn't have, or a source that isn't a port, is a part's input, or belongs to another circuit |
| `feed()` | The destination isn't a part's input, the source is invalid, or the input already has a source |
| `output()` | An output name the circuit doesn't have, an invalid source, or an output that's already driven |
| After the builder returns | Child inputs left unconnected, outputs left undriven, or the builder returned a promise |
| Any builder method after the builder returns | The builder has finished and can't be used again |
