Skip to main content
A composite circuit is defined with circuit(): a spec with its name and external ports, plus a builder callback that adds parts and wires them together.
Read it top to bottom. Each line adds one part and says what feeds each of its inputs, so the signal flows down the page: the circuit’s inputs feed nand, nand feeds not, and not drives the output.

The builder context

Every wiring call reads destination ← source, like an assignment:
add doesn’t care whether a definition is primitive or composite, and nesting depth is unlimited.

Sources

A source is anything that can drive a wire:
  • the circuit’s own inputs (inputs.a)
  • the outputs of parts you’ve added (nand.outputs.out)
A part’s inputs are never sources: they receive a signal, they don’t produce one. One source can feed any number of inputs (fan-out), but each input has exactly one source.

Naming parts

Part names are optional:
  • Explicit names must be identifiers and unique within the circuit. inputs and outputs are reserved.
  • Generated names have the form <Definition>#<n>, counted per definition in the order you call add, for example XOR#0 and XOR#1. Leave the name out and pass the inputs second: add(NOT, { a: inputs.a }).
Explicit names make hierarchical paths readable (root.fa3.or rather than root.FullAdder#3.OR#0), so prefer them for anything you plan to inspect.

Feedback

Sometimes a part needs a signal from a part that comes later. Memory works this way. Leave that input out of the add call, then wire it with feed once the source exists:
Because feedback is the only time you need feed, every feedback wire stands out when you read a circuit. Cycles are never rejected just for being cycles. See Sequential logic for how they behave. The one exception is a loop made only of wires, with no gate anywhere in it. That can happen through pass-through composites, and since such a wire has no driver, it is reported when the circuit is elaborated.

Checks

The builder checks your wiring and reports problems as a CircuitError prefixed with the circuit name. Most of them are also compile-time errors. After the builder returns, every part input that was never wired and every output that was never driven is reported together:
Unused circuit inputs and unused part outputs are fine.

When the builder runs

The builder callback runs immediately, inside circuit(), and the result is checked before circuit() returns. A wiring mistake is reported where you define the circuit, not later when you first simulate it. It also means a circuit can only contain parts that already exist. A circuit can never contain itself, so every circuit elaborates into a finite set of gates. Builders must be synchronous, and the builder context can’t be used after the callback returns.