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

# simulate()

> Start a simulation and work with its immutable states.

```ts theme={null}
function simulate<I extends string, O extends string>(
  definition: ComponentDefinition<I, O>,
  options?: SimulationOptions,
): SimulationState<I, O>;
```

Elaborates the definition (cached per definition) and returns the first state of a new simulation: tick 0, every signal `"X"`, and every gate due for its first evaluation.

## Options

Options are fixed when the simulation starts and carry over to every later state.

**maxTicks** `number` (default: 10000)

The maximum number of steps a single `run()` call may take. Must be a positive integer.

**trace** `boolean` (default: false)

Record every wire change, so `trace()` can return it.

## SimulationState

A frozen object describing one moment of the simulation. Methods that move the simulation forward return a **new** state and never change the one they're called on.

### Properties

**tick** `number`

Simulation time: the next tick to be processed.

**stable** `boolean`

True when no wire changes are pending and every gate has had its first evaluation.

**changes** `readonly SignalTransition[]`

The wire changes applied by the step that produced this state, in wire order. Empty for the first state and for states returned by `set()`.

### Moving forward

**set(inputs)** `(inputs: \{ [K in I]?: Signal \}) =\> SimulationState`

A new state with these external inputs pending. They take effect on the next step. Returns the same state if nothing would change. Throws `SimulationError` for unknown names or invalid values, without applying anything.

**step()** `() =\> SimulationState`

A new state, one tick later. It applies the pending changes, evaluates the gates that read a changed wire, and makes their changed outputs pending.

**run()** `() =\> SimulationState`

A new state, stepped until stable. Returns the same state if it's already stable. Throws `StabilizationError` after `maxTicks` steps.

### Reading

**read(output)** `(output: O) =\> Signal`

The current value of one external output.

**readAll()** `() =\> \{ [K in O]: Signal \}`

The current values of all external outputs.

**inspect()** `() =\> SimulationSnapshot`

Plain, serializable data: `{ tick, stable, pendingEvents, signals, wires }`. `signals` maps every hierarchical port id to its value, and `wires` maps every wire id to `{ value, ports }`.

**trace()** `() =\> readonly SignalTransition[]`

A frozen array of every wire change from tick 0 up to this state, ordered by tick and then by wire id. Empty unless `trace: true`.

## Example

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

const start = simulate(AND);
const settled = start.set({ a: 1, b: 1 }).run();

console.log(settled.read("out")); // 1
console.log(settled.tick - start.tick); // 3 ticks to settle
console.log(start.read("out")); // "X": start never changes
```

## Related types

```ts theme={null}
type SimulationOptions = {
  readonly maxTicks?: number;
  readonly trace?: boolean;
};

type SimulationSnapshot = {
  readonly tick: number;
  readonly stable: boolean;
  readonly pendingEvents: number;
  readonly signals: Readonly<Record<string, Signal>>;
  readonly wires: Readonly<Record<string, { readonly value: Signal; readonly ports: readonly string[] }>>;
};
```
