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

# Types

> Public types exported from wires.

All types are exported from `wires`. Import them with `import type`.

## Signal

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

## SignalTransition

```ts theme={null}
type SignalTransition = {
  readonly tick: number;
  readonly wire: string; // wire id: the path of the port that drives it
  readonly previous: Signal;
  readonly value: Signal;
};
```

## ComponentDefinition

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

## ComponentInstance

Returned by `add()` inside a circuit builder.

```ts theme={null}
interface ComponentInstance<I extends string = string, O extends string = string> {
  readonly kind: "instance";
  readonly name: string;
  readonly definition: ComponentDefinition<I, O>;
  readonly inputs: { readonly [K in I]: Port<K, "input", "sink"> };
  readonly outputs: { readonly [K in O]: Port<K, "output", "source"> };
}
```

## Port, SourcePort, SinkPort

```ts theme={null}
type Direction = "input" | "output";
type PortRole = "source" | "sink";

interface Port<Name extends string = string, D extends Direction = Direction, R extends PortRole = PortRole> {
  readonly kind: "port";
  readonly id: string; // e.g. "inputs.a", unique within the owner
  readonly name: Name;
  readonly direction: D;
  readonly role: R;
  readonly owner: ComponentInstance | CircuitScope;
}

type SourcePort = Port<string, Direction, "source">;
type SinkPort = Port<string, Direction, "sink">;

interface CircuitScope {
  readonly kind: "scope"; // the circuit currently being defined
  readonly name: string;
}
```

`SourcePort` is useful for variables that track a signal through a loop, like the carry in a ripple adder. `SinkPort` is what `feed()` accepts as its destination: a part's input.

## InputWiring

The third argument to `add()`: what feeds each input of the part being added. Inputs left out must be wired later with `feed()`.

```ts theme={null}
type InputWiring<I extends string> = { readonly [K in I]?: SourcePort };
```

## ComponentDescription

Returned by `describe()`. Plain, serializable data.

```ts theme={null}
type ComponentDescription = {
  readonly id: string; // "root", "root.not", "root.not.g"
  readonly name: string; // instance name ("root" at the top)
  readonly parent: string | null;
  readonly definition: string;
  readonly kind: "primitive" | "composite";
  readonly ports: readonly PortDescription[];
  readonly components: readonly ComponentDescription[];
  readonly connections: readonly ConnectionDescription[];
};

type PortDescription = { readonly id: string; readonly name: string; readonly direction: Direction };
type ConnectionDescription = { readonly source: string; readonly destination: string };
```

## Spec and builder types

| Type | Used by |
| - | - |
| `GateSpec<I, O>` | The argument to [`gate()`](https://wires.phil.bingo/reference/gate) |
| `CircuitSpec<I, O>` | The first argument to [`circuit()`](https://wires.phil.bingo/reference/circuit) |
| `CircuitBuilder<I, O>` | The builder callback's parameter |

## Simulation types

| Type | Description |
| - | - |
| `SimulationState<I, O>` | An immutable moment of a simulation, returned by [`simulate()`](https://wires.phil.bingo/reference/simulate), `set()`, `step()` and `run()` |
| `SimulationOptions` | `{ maxTicks?, trace? }` |
| `SimulationSnapshot` | `{ tick, stable, pendingEvents, signals, wires }`, returned by `inspect()` |
| `Netlist`, `WireInfo`, `GateInfo`, `Ancestor` | Returned by [`elaborate()`](https://wires.phil.bingo/reference/elaborate) |
| `StabilizationDetails` | The fields of [`StabilizationError`](https://wires.phil.bingo/reference/errors) |
