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

# gate()

> Define a primitive gate with an explicit evaluation function.

```ts theme={null}
function gate<const I extends readonly string[], const O extends readonly string[]>(
  spec: GateSpec<I, O>,
): ComponentDefinition<I[number], O[number]>;
```

## Parameters

**spec.name** `string` *required*

The definition name. Must be an identifier. It appears in `describe()`, netlists, generated instance names and error messages.

**spec.inputs** `readonly string[]` *required*

Input port names. Inferred as literal types. May be empty.

**spec.outputs** `readonly string[]` *required*

Output port names. Inferred as literal types. Must not repeat any input name.

**spec.evaluate** `(inputs) =\> outputs` *required*

A pure, synchronous function. It receives a frozen object with the current value of every input, and must return an object with a [`Signal`](https://wires.phil.bingo/reference/types#signal) for every output and no other keys.

## Returns

A frozen [`ComponentDefinition`](https://wires.phil.bingo/reference/types#componentdefinition) with `kind: "primitive"`.

## Example

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

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",
  }),
});
```

## Errors

| Thrown | When |
| - | - |
| `CircuitError` (immediately) | The name or a port name isn't an identifier, a port name is repeated, or `evaluate` isn't a function |
| `SimulationError` (during simulation) | `evaluate` throws, returns something other than an object, omits an output, adds an unknown output, or returns a non-signal value |
