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

# Quickstart

> Install wires, define a gate, compose a circuit and run a simulation.

## Install

```bash pnpm theme={null}
pnpm add @pbzona/wires
```

```bash npm theme={null}
npm install @pbzona/wires
```

```bash bun theme={null}
bun add @pbzona/wires
```

`wires` is published on npm as `@pbzona/wires`. It needs Node.js 24, or 26 and later (no end-of-life releases), Bun, or any modern browser or bundler, and it has no runtime dependencies. The package has two entry points:

| Import | Contents |
| - | - |
| `@pbzona/wires` | The engine: `gate`, `circuit`, `simulate`, `elaborate`, errors and types |
| `@pbzona/wires/components` | The standard library: `NAND`, `NOT`, `AND`, `OR`, `NOR`, `XOR`, `XNOR`, `HalfAdder`, `FullAdder`, `Mux`, `Demux`, `SRLatch` |

## Build and simulate a circuit

**1. Define a primitive gate**

A primitive has an explicit evaluation function. Here is NAND, the only gate you need to build everything else.

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

export const NAND = gate({
  name: "NAND",
  inputs: ["a", "b"],
  outputs: ["out"],
  evaluate: ({ a, b }) => ({
    out: a === 0 || b === 0 ? 1 : a === 1 && b === 1 ? 0 : "X",
  }),
});
```

`@pbzona/wires/components` exports this exact gate, so the next steps import it from there.

**2. Compose it into a circuit**

A composite circuit adds parts one at a time, saying what feeds each part's inputs, then says what drives its outputs.

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

export 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(NAND, "not", { a: nand.outputs.out, b: nand.outputs.out });
    output("out", not.outputs.out);
  },
);
```

Read it top to bottom: the inputs feed `nand`, `nand` feeds `not` (a NAND with both inputs tied together is a NOT), and `not` drives the output.

**3. Simulate it**

A simulation is a sequence of states. `simulate()` gives you the first one, and `set()` and `run()` each return a new state. The old state never changes.

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

let sim = simulate(AND);

sim = sim.set({ a: 1, b: 1 }).run();
console.log(sim.read("out")); // 1

sim = sim.set({ a: 0 }).run();
console.log(sim.read("out")); // 0
```

Time only moves when you call `run()` (or `step()`, which moves one tick).

**4. Look inside**

Every state can report the value of every port in the hierarchy, and with tracing on, every change that led to it.

```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.inspect().signals["root.nand.outputs.out"]); // 0
console.log(settled.trace().length); // 4 wire changes
```

> [!TIP]
>
> Port names are inferred from your definitions. `settled.read("nope")` or `add(NAND, "nand", { c: inputs.a })` fails at compile time, not at runtime.

## Next steps

> [card] [Composing circuits](https://wires.phil.bingo/guides/composing-circuits.md)
>
> Builder rules, naming, fan-out and feedback.

> [card] [Simulation model](https://wires.phil.bingo/concepts/simulation-model.md)
>
> How ticks, delays and unknown values work.
