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

# Signals

> The three-valued logic that every wire carries.

A signal is a discrete digital value:

```ts theme={null}
type Signal = 0 | 1 | "X";
```

| Value | Meaning |
| - | - |
| `0` | Logic low |
| `1` | Logic high |
| `"X"` | Unknown or uninitialized |

## Unknown values

Every signal in a new simulation starts as `"X"`. External inputs stay unknown until you assign them with `set()`. Primitives decide how unknowns propagate. NAND, for example, follows this table:

| a | b | NAND |
| - | - | - |
| 0 | X | 1 |
| X | 0 | 1 |
| 1 | X | X |
| X | 1 | X |
| X | X | X |

A `0` on either input forces the output high, so the unknown on the other input doesn't matter. In every other unknown case the result is unknown too.

The simulator never resolves `"X"` to `0` or `1` on its own. A latch that has never been set reports `"X"` on both outputs, which is the honest answer.

## Numbers, not booleans

Signals are the numbers `0` and `1`, not JavaScript booleans, and the library never relies on truthiness. Compare with `===`:

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

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

Passing `true`, `2` or `"x"` where a signal is expected fails to type-check, and throws a `SimulationError` at runtime.

## Helpers

`wires` exports a few small helpers for working with signals:

| Export | Description |
| - | - |
| `X` | The constant `"X"` |
| `SIGNALS` | `[0, 1, "X"]`, frozen |
| `isSignal(value)` | Type guard that is true for `0`, `1` and `"X"` |

> [!NOTE]
>
> High-impedance (`Z`) signals and multi-driver buses are out of scope for now. Each destination has exactly one driver.
