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

# Gates

> NAND, NOT, AND, OR, NOR, XOR and XNOR.

All gates use the inputs `a, b` (or just `a` for `NOT`) and the output `out`.

## NAND

The library's only primitive.

| a | b | out |
| - | - | - |
| 0 | 0 | 1 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
| 0 | X | 1 |
| X | 0 | 1 |
| 1 | X | X |
| X | 1 | X |
| X | X | X |

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

const sim = simulate(NAND).set({ a: 0 }).run(); // b stays "X"
console.log(sim.read("out")); // 1, because a 0 input dominates
```

## NOT

`NAND(a, a)`. Child: `g`.

| a | out |
| - | - |
| 0 | 1 |
| 1 | 0 |
| X | X |

## AND

`NOT(NAND(a, b))`. Children: `nand`, `not`.

| a | b | out |
| - | - | - |
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |

## OR

`NAND(NOT a, NOT b)`. Children: `notA`, `notB`, `nand`.

| a | b | out |
| - | - | - |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |

## NOR

`NOT(OR(a, b))`. Children: `or`, `not`.

| a | b | out |
| - | - | - |
| 0 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 0 |

## XOR

The classic four-NAND network. Children: `n1`–`n4`.

```text theme={null}
n1 = NAND(a, b)
n2 = NAND(a, n1)
n3 = NAND(b, n1)
out = NAND(n2, n3)
```

| a | b | out |
| - | - | - |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |

> [!NOTE]
>
> Changing both inputs in the same tick, for example from `0, 0` to `1, 1`, makes the XOR output glitch briefly before it settles back to `0`. That hazard is real gate-level behavior, and it shows up in `trace()`.

## XNOR

`NOT(XOR(a, b))`. Children: `xor`, `not`.

| a | b | out |
| - | - | - |
| 0 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
