§ 1.1. Engine¶
CircuitSim is an event-driven simulator of digital circuits written in C++20. A circuit is a tree of components connected by wires; a change on a wire makes the components it feeds compute again, and their new outputs reach their wires a delay later. This page describes the pieces of that engine. § 1.2 Time and Delay describes the delays.
Values¶
Every signal carries one of four values on each bit:
| Value | Name | Meaning |
|---|---|---|
| 0 | LOW |
Logic low |
| 1 | HIGH |
Logic high |
X |
UNKNOWN |
Not known: never driven yet, conflicting, or the result of an unknown input |
Z |
HIGH_Z |
Floating: nothing drives the bit |
A gate treats Z like X: AND gives 0 when either input is 0 and X when
nothing decides it; OR gives 1 when either input is 1. Wiring parts (splitters,
joiners, Rewire) pass every value on unchanged, Z included. Each page's
Unknown inputs section gives the exact rule of its part.
A value is 1 to 64 bits wide. It is stored packed in two 64-bit words,
value and unknown: bit i of each together holds bit i of the signal (0 as
(0, 0), 1 as (1, 0), X as (0, 1), Z as (1, 1)), so a 32-bit operation
costs about as much as a one-bit one.
Pins and wires¶
A pin belongs to a component and has a name, a direction (input or
output) and a width fixed when the component is made. A wire connects one
output pin (its source) to any number of input pins (its sinks) of the same
width. Pin<W> and Wire<W> are typed handles for a width known at compile
time; PinBase and WireBase serve widths chosen at run time.
Each pin has an external wire, in the scope of the component's parent,
and an internal wire, in the component's own scope. An input pin's
internal wire carries the value into the component's children; an output
pin's internal wire collects it from them. A component may not connect its
own input pin straight to its own output pin; a zero-delay
Rewire bridges the two when a value must pass
through unchanged.
Components¶
Every component derives from Component:
Component
└─ IOComponent has pins; a composite builds its children in buildInternals()
└─ BasicComponent a leaf: has a delay and computes its outputs in evaluate()
A composite (IOComponent) contains other components and wires and has
no behavior of its own: its outputs are whatever its children drive. A
leaf (BasicComponent) computes its outputs from its inputs. The gates
of § 1.3, the wiring parts and constants of § 1.4 and the
clock generator are the leaves every structural circuit ends in;
the memory primitives of chapter 3, which stand for electrical behavior, are
the only other leaves of a structural circuit.
Building a circuit¶
Components are made with Component::create<T>(name, ...), inside a parent
with ComponentBuilder::addNewComponent<T>(name, ...), or by contract with
builder.add(family, name) (below). These run the component's pin
initializer and buildInternals(); constructing a component directly with
std::make_shared would skip both. A structural recipe collects connections
with circuit::detail::StructuralWiring: connect(source, sink) for each
fan-out, then finish() creates one wire per source.
Events and the simulator¶
The simulator keeps one priority queue of events ordered by time, then wire
updates before component evaluations, then the order in which they were
scheduled. A wire update sets a wire's value and schedules an evaluation
of every component it feeds; an evaluation runs a leaf's evaluate(),
which schedules updates of its output wires one delay later. Nothing happens
between events, and there is one global time: every clock is an ordinary
signal in it.
The simulator records the history of every wire and pin change, so a run can be scrubbed backwards and forwards in time and a memory's past contents read without disturbing it. Long headless runs turn the history off.
Structural and compact forms¶
Most parts exist in two forms with the same pins and the same values:
- the structural form, built from smaller parts down to gates; and
- the behavioral (compact) form, one leaf that computes the settled outputs directly and is much faster to simulate.
A family (ComponentFamily) is one public contract: a contract ID such
as logic.mux.2.32, its pins, and at most one factory of each form. A parent
asks for a family, not a class, and one build profile (BuildProfile, an
ordered list of rules, the last match winning) chooses the form at each path
of the tree. A build manifest records what was chosen. A compact form must
never be faster than its gates: it runs on the delays proven on its gate-level
form (§ 1.2). Each compact form is tested against its
gates, including unknown and floating inputs, before the catalog accepts it.
The interactive simulator offers presets of these rules
(gate, logic, visual and compact) and lets the component tree choose
the form of each part: an expanded part is built from its parts, a collapsed
one as one block.
Scenarios and tests¶
One registry of tests (src/tests/TestRegistry.cpp) serves the C++ test
runner, the Python binding and the WebAssembly build. A component test
drives a part through recorded checkpoints in every form it has and compares
them; the example tables on component pages are those checkpoints, recorded
in the unit timing profile. The same registry names the scenarios of the
interactive simulator.