Skip to content

Silicon Internals ​

This page describes how Silicon's major subsystems fit together. It is intended for advanced users and contributors, so it focuses on behavior, boundaries, and extension points rather than class-by-class API details. For the latter, use the generated class and member reference.

Simulation kernel model ​

Simulation does not run directly on the circuit being edited or serialized. A simulation::Session owns that source circuit, passes it through simulation::CircuitElaborator, and gives the resulting runtime circuit to simulation::Simulator. Primitive components are cloned. SubcircuitComponent instances are recursively replaced by their project definitions; parent interface wires are mapped to the definition's boundary wires and internal wires are cloned per instance. The elaborator only consumes Silicon core circuits and has no frontend or source-language extension point. The source topology and graphical scene remain unchanged. Recursive subcircuit dependencies, missing types, and mismatched interfaces fail during elaboration.

The runtime circuit is compiled into an execution plan. Strongly connected regions are separated from acyclic regions, and the resulting steps are ordered by data flow. Silicon also caches the forward cone reachable from each input wire, so an input change does not require evaluating unrelated components. A topology or port-shape change invalidates these caches: an interactive Circuit recompiles its plan, while the normal UI session rebuilds its elaborated runtime circuit.

The kernel is event driven:

  1. A component reads its input buses and calls simulation::Simulator::updateWire() for its outputs.
  2. A zero-delay combinational update is applied immediately. A delayed update enters a time-ordered event queue at currentTime + delay.
  3. All valid events at one timestamp are applied as a batch, then only their combined forward cone is evaluated. This prevents observers from seeing a half-applied timestamp.
  4. A bounded run(duration) processes queued timestamps within the requested window and advances the clock to the end of the window. runUntilIdle() instead drains all pending delayed events.

Delayed transitions use replacement semantics per source component and output wire. A newly scheduled transition supersedes the previous pending transition for that pair; stale queue entries are ignored. Scheduling the wire's already-current state cancels the pending transition. This models propagation delay without allowing obsolete work to change the circuit later.

Acyclic steps run once in topological order. A cyclic strongly connected region is evaluated repeatedly at the same simulation time until a pass changes no state. These zero-time iterations are delta cycles: they let stable feedback converge without inventing a propagation delay. If the region does not converge within the configured transition limit, simulation throws an unstable-zero-delay-loop error.

Edge-triggered components opt into staged sequential outputs with usesStagedSequentialOutputs(). During a reactive pass their zero-delay writes are collected rather than exposed immediately. The simulator commits them together from the same pre-edge state, then evaluates the downstream cone once more. This prevents one register or flip-flop from observing another component's new state during the same logical edge.

Interactive input toggles call setBus(), evaluate the affected forward cone, and then runUntilIdle() so delayed propagation settles completely. The UI advances one display tick after settling, keeping successive user changes at distinct waveform timestamps. Long-running construction and evaluation accept a cooperative cancellation callback; cancellation is checked between components and delta passes, but an event timestamp is never left half-applied.

Two independent safety limits protect the application:

  • maxTransitionsPerDeltaCycle defaults to 1,000 and bounds convergence of one cyclic region.
  • maxSimulationSteps defaults to 100,000 and bounds the number of queued timestamp batches handled by one run. Reaching it returns StepLimitReached.

Both limits are configurable in the application settings. Increasing them can allow a large, valid circuit to settle, but it does not make an oscillating zero-delay loop stable.

Relevant source: runtime elaboration, simulation sessions, and the event-driven simulator.

Project file format ​

A .sil project is a versioned ZIP archive, not one JSON document. Silicon writes the mimetype entry first and stores it uncompressed with the exact value application/vnd.silicon.project+zip. Readers validate this marker before parsing the manifests.

text
example.sil
├── mimetype
├── metadata.json
├── project.json
├── circuits/
│   ├── untitled.json
│   ├── controller.json
│   └── adder.json
├── code/
│   └── adder.v
└── bin/
    └── program

metadata.json contains the archive formatVersion, the Silicon version that wrote the file, and UTC creation/modification timestamps. project.json contains the project name and description. There must be at least one circuit, but no circuit has project-level priority. Reusable circuits are flat entries under circuits/, source documents use registered extensions under code/, and raw binaries live under bin/. Absolute paths, traversal segments, reserved entries, unknown document formats, or duplicate paths are rejected.

Inside a running editor, every concrete format is represented by the same silicon::project::Document. Its validated project-relative path is its only identity and determines whether it is a Circuit, Verilog, or RawBinary document. The broad Diagram, Code, or Binary behavior is derived from that concrete type. A document name is derived when needed: circuits/adder.json becomes adder and code/adder.v also becomes adder; it is not stored as independent state. ProjectFile::documents is the authoritative archive collection.

Every open document lives in the ordered DocumentStore. Circuit, code, and binary creation, deletion, activation, tree selection, undo restoration, and saving all use the same canonical path and lifecycle. The store preserves insertion order, including when an undo restores a document at its former index, and emits path-based change events (Added, Updated, Removed, or Reset) so components can refresh a referenced subcircuit without relying on an empty-path sentinel.

ProjectDependencyGraph is a runtime-only index rebuilt from circuit contents; it is never serialized. An edge points from a containing document to the flat circuit document named by a reusable-component slug. Dependency extraction validates malformed subcircuit components, missing targets, and recursion before committing an update. Code documents do not participate in this containment graph.

Each circuit or subcircuit entry is a serialized editor scene. Its two main sections have different responsibilities:

json
{
  "circuit": { "components": ["core topology ..."] },
  "visual": { "components": ["positions ..."], "wires": ["segments ..."] }
}

The circuit object is the canonical logical graph: component type identifiers, declared properties, input/output buses, and shared wire IDs. The visual object stores graphical component positions, rotations, visual type names, and routed wire-segment geometry. A visual component's vertexId associates it with the corresponding core component. Every circuit may also carry reusable graphical boundary/shape metadata. This separation lets simulation and native circuit deserialization remain independent of Qt scene data.

Code documents store their source directly in entries such as code/adder.v. Verilog is the currently registered language. Headless conversion prepares semantic VerilogSource and core Circuit values without depending on Qt. Any Yosys design graph remains a private conversion detail. The UI materializes converted circuits as graphical scenes and owns selection, overwrite confirmation, undo, and activation. Consequently, the same workflow can later host other HDL frontends and binary/memory-file converters without putting editor state in the core.

Converting a circuit to Verilog creates or replaces the corresponding code document. Converting a single-module Verilog file back creates or replaces that circuit directly. For a multi-module file, the editor presents the prepared dependency choices and accepts one or more roots; each selected root and its recursive dependencies are created together as one undoable batch. Module names that cannot be circuit document names, including Yosys-generated parameter variants, receive stable names with conflicting names disambiguated by numeric suffixes. The conversion updates module references to match; existing valid module names are reserved before assigning replacements. Editing a code document hides circuit-only tools and disables simulation.

Circuit documents retain only their canonical path and serialized scene contents. Core consumers parse the logical circuit section headlessly when needed; graphical scene materialization remains a UI responsibility.

Subcircuit placeholders resolve their slug through the current ProjectContext and an explicit CircuitResolver. Reusable-circuit interfaces use named CircuitPort values, keeping each port name attached to its bus for elaboration and hierarchical Yosys export rather than maintaining parallel name arrays. Elaboration operates on circuit documents and never invokes Yosys.

Compatibility is deliberately strict. The archive reader currently accepts only the current metadata.json.formatVersion, and native core circuit JSON must report the current Silicon version. Do not rewrite a .sil archive with a generic ZIP tool and assume entry layout, compression, unknown fields, or future schema versions will remain compatible. Use Silicon to migrate projects, preserve both logical and visual sections, and treat undocumented entries as implementation details.

Relevant source: the project archive contract, core topology serializer, and scene serializer.

Waveform management ​

The simulation controller discovers graphical inputs and outputs, sorts each group by scene position, and registers their buses with the simulator. The component's name property becomes the signal name; unnamed signals receive deterministic names such as input_0 and output_0. Inputs come first and inputCount marks the boundary between the editable input group and the observed output group. Each signal definition also records its bus width.

A waveform::Trace is a list of signal definitions plus timestamped snapshots. Each snapshot contains the encoded value of every registered bus. Appending another snapshot at the latest timestamp replaces the existing snapshot instead of creating a zero-width display interval. During a worker simulation, snapshots are collected into one batch and delivered to the GUI after the worker joins; the viewer then coalesces rapid arrivals into at most one refresh per frame. These two batching levels avoid an event-loop post and full relayout for every transition.

Edit mode exposes only the leading input signals. It starts with zero-valued boundaries at time zero and at the requested duration. Assigning a value to [start, end) inserts or updates boundary snapshots, changes only the chosen input column inside the interval, and compacts adjacent identical snapshots. Leaving edit mode sends the input-only samples back to the simulation controller.

Replay builds a fresh runtime circuit, maps input columns to ordered input buses, advances the simulator to each sample time, and applies all values for that timestamp together. If a replay payload contains repeated timestamps, later values replace earlier values by column before propagation. Outputs and intermediate delayed events are sampled through the same trace-bus configuration, so the replayed display follows kernel time rather than wall-clock time.

Waveforms can optionally be written as FST for external viewers. Live FST tracing and UI snapshots observe the same named buses and timestamps. The waveform viewer can also export its already-collected snapshots to an FST file; FST is an interchange/output format, not the in-memory editing model.

Relevant source: the waveform data model, simulation/UI bridge, and FST adapter.

Yosys interoperability and the SILICON technology library ​

Yosys JSON remains the netlist transport format. yosys::serialize() and yosys::deserialize() run in process and produce or consume the shape written by Yosys' write_json command. Native builds can additionally launch an external Yosys process. The implementation is isolated under core/serialization/yosys: the netlist layer owns JSON translation, while the tool layer owns process execution, temporary workspaces, resource discovery, and Yosys command pipelines.

Verilog-facing entry points live in the separate verilog adapter. verilog::read() runs the controlled Verilog frontend and returns Yosys JSON; verilog::write() serializes a circuit or JSON design through the Yosys backend and then applies Verilog-only cleanup. Identifier parsing, generated-net inlining, process cleanup, and ANSI port normalization therefore do not leak into the Yosys tool implementation. A future SystemVerilog, VHDL, or other HDL adapter can reuse the netlist and elaboration layers without inheriting Verilog postprocessing. Yosys is invoked directly, without a shell, and is not embedded in SILICON. By default the executable is resolved from PATH; ToolOptions::executable is an explicit override.

Three layers have deliberately different jobs:

  • Yosys JSON transports modules, ports, parameters, cells, and LSB-first connection vectors. It is not itself the source of SILICON component semantics.
  • The SILICON Yosys technology library defines canonical SILICON_* cells for native identities that generic Yosys cells cannot preserve reliably. Black-box synthesis declarations, separate simulation models, and the technology map live in resources/yosys/.
  • The generic Yosys importer remains a compatibility fallback for supported cells such as $and, $add, $mux, and $dff. It reconstructs equivalent native logic, but does not promise the original high-level component identity.

On export, one Silicon Circuit becomes a module. Named input/output components become module ports and port netnames; shared wires receive module-local numeric signal IDs; and each component's serializeYosys() writes through a silicon::yosys::SerializationContext. Parameter strings use Yosys' MSB-first binary encoding, while connection arrays follow SILICON's LSB-first bus order. Simulation-only propagation delays are intentionally omitted. The final source uses ANSI-style module headers, removes Yosys' redundant wire redeclarations for ports, and retains genuine internal or shared nets. Two-input NAND and NOR gates use fused cells so expressions such as assign q = ~(set | nq); do not expose a temporary OR-to-inverter wire.

Technology-cell ABI ​

Technology-cell names, ports, and parameters are a stable interchange contract and do not depend on graphical shapes. The initial ABI is:

CellPortsParametersExact native component
SILICON_DLATCHD, EN, Q, QNEN_POLARITYactive-high scalar DLatch
SILICON_DFFD, CLK, Q, QNCLK_POLARITYDFlipFlop without async controls
SILICON_DFFED, EN, CLK, Q, QNCLK_POLARITY, EN_POLARITYEFlipFlop without async controls
SILICON_DFFSRD, CLK, SET, CLR, Q, QNCLK_POLARITY, SET_POLARITY, CLR_POLARITYDFlipFlop with async controls
SILICON_DFFSRED, EN, CLK, SET, CLR, Q, QNclock, enable, set, and clear polaritiesEFlipFlop with async controls
SILICON_JKFFJ, K, CLK, SET, CLR, Q, QNclock, set, and clear polaritiesJKFlipFlop
SILICON_HALF_ADDERA, B, SUM, COUTnone; all ports are scalarHalfAdder
SILICON_FULL_ADDERA, B, CIN, SUM, COUTnone; all ports are scalarFullAdder
SILICON_ADDERA, B, SUM, COUTWIDTH, A_SIGNED, B_SIGNEDunsigned AdderNBits
SILICON_REGISTERDATA, CLK, EN, CLR, LOAD, OUTWIDTH, parallel/serial mode flags, and control polaritiesall four native Register modes

The native latch is scalar, is transparent while its active-high enable is asserted, and holds its stored state while enable is low. Native flip-flop set, clear, and enable controls are active high; clocks may use either edge. Clear and set are asynchronous, clear-only means zero, set-only means one, and a simultaneous or indeterminate set/clear condition produces UNKNOWN. QN always complements Q, including becoming unknown with it. Register clear has priority over enable and clock, shifting moves toward bit zero, and PISO load has priority over shift. Arithmetic cells are unsigned; SUM has WIDTH bits and COUT is the discarded carry bit. silicon_cells.v is the single source for these interfaces and behaviours. Loaded normally it provides external simulation models; SILICON_BLACKBOX selects synthesis black boxes, and SILICON_EXPORT_MAP selects the native-Yosys export lowering.

The custom-cell importer runs before generic dispatch. It requires the exact parameter and connection sets, validates every width and supported polarity, registers both normal and complementary outputs with the single-driver checker, applies shape properties, and only then constructs the matching native component. An unknown SILICON_* type is an error rather than a placeholder or registry lookup.

Import of generic JSON remains pattern based. The compatibility importer:

  • selects an explicitly requested module, the sole module, or the unique module carrying a nonzero top attribute;
  • maps ports to named scalar or bus input/output components;
  • interns numeric net bits as shared Wire objects and turns supported literals into constant components;
  • raises groups of unsigned $eq cells that compare one selector against distinct constants into a single native Decoder, preserving their result wires as sparse decoder outputs;
  • recognizes a strict set of cell types and validates required ports, widths, parameters, polarity, signedness, and single-driver rules; and
  • reconstructs the closest native component or component network, including properties such as bus size, selection width, trigger edge, and zero imported delay.

Raising equality banks into decoders ​

Yosys lowers a Verilog case selector into $eq cells whose one-bit results control a $mux or $pmux network. Importing every comparison as gates would obscure the original decode structure, so SILICON performs a cell-grouping prepass before generic cell dispatch. For example:

text
$eq(selector, 4'b0001) ── select_1
$eq(selector, 4'b0110) ── select_6
$eq(selector, 4'b1101) ── select_13

becomes one native four-to-sixteen Decoder. Its enable input is tied HIGH, its selection input uses the shared selector bus, and outputs 1, 6, and 13 reuse the original $eq result wires. Unmatched decoder outputs receive internal wires, allowing sparse case values without inventing extra comparisons. The mux cells that consume the result wires are then imported normally as native Multiplexer components.

A group is eligible only when:

  • at least two $eq cells use the identical selector connection;
  • exactly one operand of each comparison is a fully defined binary constant;
  • operands are unsigned, equally wide, and no wider than the native decoder's 15-bit selection limit;
  • every comparison has a one-bit result; and
  • constants are distinct within the group.

The constant may appear on either side of $eq; connection arrays are decoded in Yosys' least-significant-bit-first order. Eligible comparisons are marked as consumed so they are not dispatched again, and the native decoder uses zero delay to preserve imported netlist timing. Ineligible or isolated $eq cells remain in generic dispatch and are rejected while standalone equality comparison is unsupported.

External Verilog pipelines ​

Verilog import reads silicon_cells.v with SILICON_BLACKBOX as a library, elaborates and flattens the selected user top, preserves word-level arithmetic, extracts recognizable half/full adder cones, applies silicon_techmap.v, and writes JSON. The map covers supported scalar active-high $dlatch, $dff, $dffe, $dffsr, and $dffsre shapes, equal-width unsigned $add, and the $fa cells produced by extract_fa. A constant-zero carry input identifies a half adder; otherwise the extracted cell becomes a full adder. Unmatched cells continue to the generic importer.

Positive- and negative-edge D storage, enabled D storage, supported asynchronous set/reset forms, equal-width unsigned addition, and extracted half/full adders therefore map to custom cells. A JK flip-flop is preserved only when SILICON_JKFF was already present, such as an explicit Verilog instance or SILICON export. Arbitrary next-state logic feeding a D flip-flop is never called a JK flip-flop: equivalent logic does not prove the original intent.

The default import script does not run ABC. Gate-level remapping would discard the word-level and educational structure this interface is intended to preserve, and no later optimization pass is allowed to decompose SILICON_* cells. Conversely, equivalent arbitrary Verilog cannot always be raised back to a particular native component; only the documented deterministic mappings make that promise.

Verilog export first loads silicon_cells.v in black-box mode, reads the circuit JSON, and applies the same file in SILICON_EXPORT_MAP mode. This lowers technology cells to native Yosys RTL cells so the final HDL uses ordinary expressions and always blocks without SILICON_* dependencies. The exporter then derives descriptive names for internal signals, sanitizes escaped identifiers, and writes attribute-free Verilog without renaming those signals to numeric temporaries. Parameters use decimal notation where possible. A guarded post-processing step folds complete port declarations into ANSI module headers; it leaves a module untouched if the emitted declarations do not match the header exactly. When yosys-config is available at build time, SILICON also builds and packages the silicon_bmux_case Yosys plugin. The export script loads this plugin to raise $bmux cells into combinational RTLIL switch processes, which the Verilog backend emits as readable case statements.

Every external invocation sends captured standard output and standard error to the yosys logger. User-facing failures identify the failed phase and direct the user to those logs, keeping command transcripts available without duplicating them in dialogs.

Resource lookup checks an explicit ToolOptions::technologyLibraryDirectory override, then compile-time build-tree and configured installation locations, followed by paths relative to the running executable (including a macOS bundle Resources candidate). CMake installs the three files under ${CMAKE_INSTALL_DATADIR}/silicon/yosys; paths are normalized and quoted before being passed through direct process arguments. Technology resource lookup is independent of Yosys executable lookup, does not depend on the current working directory, and supports relocated Windows, Linux, and macOS installations.

Emscripten builds cannot launch an external Yosys process, so runScript(), verilog::read(), and verilog::write() report a platform-availability error. In-process JSON import/export, including exact SILICON_* round trips, remains available.

Project subcircuits are hierarchical in both directions. On export, each definition is emitted once as a module and instances become module-typed cells. On multi-module import, SILICON lowers the complete design without flattening, derives module dependencies from module-typed cells, and reconstructs those cells as project subcircuit instances. The UI creates selected roots and their dependency closure in dependency-first order, preserving shared definitions as one project document. Recursive definitions and duplicate module names fail.

Unknown cells are never silently converted to black boxes. An unsupported cell, memory, inout port, high-impedance literal, malformed connection set, ambiguous module choice, or unsupported cell variant aborts the whole import with a contextual error. Adding a type to ComponentRegistry alone therefore cannot make it Yosys-importable.

Relevant source: serialization context and module builder, native component lowering, and the strict cell-pattern importer.

How to add a native component ​

Use Multiplexer and GraphicalMultiplexer as a concrete reference. They exercise validated properties, changing bus shapes, changing graphical ports, native persistence, and Yosys import/export without requiring a large subsystem.

1. Implement the core Component ​

Add the header and implementation under src/core/ or src/extraComponents/, then add every new .cpp file to the appropriate source list in the root CMakeLists.txt.

Your type needs:

  • a stable serialized identifier such as Multiplexer::Type, returned by typeName();
  • nonempty ComponentMetadata (display name, description, and catalog category);
  • a public default constructor, because ComponentRegistry constructs an empty instance before native JSON reconnects its buses;
  • clearly indexed input/output buses, preferably using scoped Inputs and Outputs enums;
  • defineProperty() calls for configurable int, bool, or std::string values;
  • validation/property callbacks that reject invalid values and reshape core buses when a property changes topology; and
  • simulate() logic that reads component state and writes through simulation::Simulator::updateWire(), including the component as the source and any intended propagation delay.

The essential shape looks like this:

cpp
class ExampleComponent : public Component {
public:
  static constexpr std::string_view Type = "ExampleComponent";

  std::string_view typeName() const override { return Type; }
  ComponentMetadata metadata() const override {
    return {"Example", "Explains what the component does.",
            ComponentCategory::Utils};
  }

  ExampleComponent();
  void simulate(simulation::Simulator& engine) override;
  void serializeYosys(silicon::yosys::SerializationContext& context) const override;
};

Multiplexer declares selectionSize, busSize, and delay. Its callbacks validate the candidate value and call setSelectionSize()/setBusSize() so serialized property application and direct property edits produce the same bus topology. Keep this invariant: properties, buses, and behavior must agree even when no GUI exists.

ComponentCategory controls catalog grouping; it does not define a module interface. A component that represents a public subcircuit boundary must additionally set ComponentMetadata::portRole to PortRole::Input or PortRole::Output. Keep the default PortRole::None for ordinary components, including sources such as constants. This core metadata is intentionally distinct from the UI-only ItemCategory flags on graphical items. Circuit::getInputPorts() and Circuit::getOutputPorts() use those boundary components as the named circuit interface. Names come from the component name property. Each direction independently falls back to the topological circuit interface, with deterministic names, when it has no declared boundary components. Circuit::getInputs() and getOutputs() expose the corresponding buses.

2. Register native construction ​

Include the type and add registerComponent<ExampleComponent>(registry) in registerAllComponents(). Registration verifies default constructibility and catalog metadata. It also makes the type available to the catalog's core metadata lookup and, more importantly, lets Circuit::deserialize() recreate a component from its native type field before applying properties and reconnecting serialized buses.

Do not change an existing Type string casually. It is part of saved native circuit data, so renaming it requires an explicit compatibility strategy.

3. Implement and register the graphical component ​

Create a GraphicalLogicComponent subclass under src/ui/circuit/components/ and register its .cpp file in UI_SOURCE_FILES. Its constructor should create the core component, install a shape (an existing Qt resource, SVG, or programmatic QGraphicsItem), and declare input/output PortPairs that match the core buses. Override type() with a distinct value from SiliconTypes for Qt item classification.

For a fixed component, the base class keeps port widths synchronized through the core I/O listener. For a component like GraphicalMultiplexer, property changes can alter the number, position, or meaning of ports as well as their width. Its graphical callbacks delegate to the core setSelectionSize()/setBusSize() methods and then rebuild the shape and ports. Reinstall those callbacks in setComponent() because scene deserialization replaces the constructor-created core instance with the restored one.

Finally, add the type to registerAllGUIComponents():

cpp
reg(std::string(ExampleComponent::Type),
    [](QGraphicsItem* parent) {
      return std::make_unique<GraphicalExampleComponent>(parent);
    });

Normally the scene's visual type equals the core Type, and reg() records that one-to-one mapping. When they differ, use regMapped(guiType, coreType, factory). Graphical inputs demonstrate this: the persisted GUI type Input maps to the core type DummyInputComponent. The mapping lets the catalog ask the core registry for the right metadata and lets scene deserialization construct both halves correctly.

4. Let declared properties flow through the UI and native JSON ​

No property-dock widget or per-property JSON code is required for ordinary properties. The dock visits every declared int, bool, and std::string property and creates a spin box, check box, line edit, or constrained string selector. Circuit::serialize() writes the same property map, and native deserialization calls setProperty() so type checks, allowed string values, validation, and callbacks still apply.

Add a graphical property callback only when the graphical representation must change: shape bounds, labels, port count, port placement, or a special GUI-to-core port mapping. That callback must preserve core validation and reshaping, usually by delegating to a core mutator as GraphicalMultiplexer does. A purely behavioral property such as a fixed-shape component's delay should remain a core-only callback.

5. Make three independent Yosys interoperability decisions ​

Adding a native component requires three separate decisions. None implies either of the others:

  1. Decide whether native export needs a canonical SILICON_* technology cell to retain identity or semantics. If so, extend all three library files and the strict custom importer.
  2. Decide whether one or more arbitrary generic Yosys cell shapes can be imported as the component. Only do this when their parameters and ports prove the native semantics.
  3. Decide whether Verilog synthesis can map or extract the component deterministically. Add a technology-map or extraction rule only when it cannot mislabel equivalent logic.

The two directions use different extension points:

  • Export is component-driven: a native component implements serializeYosys() and emits one canonical technology cell or a useful generic network.
  • Custom import is ABI-driven: SILICON_* dispatch validates the documented contract and reconstructs one native component directly.
  • Generic import is pattern-driven: generic dispatch recognizes exact supported Yosys shapes and may construct an equivalent component network.

Export: validate the native component, then lower it to cells ​

1. Add the export override ​

Declare serializeYosys() in the component header. The base implementation throws, so a component without an override fails export instead of silently disappearing from the netlist. Per-component implementations normally live in src/core/serialization/yosys/components.cpp, alongside the shared helpers from yosys/helpers.hpp.

2. Choose the Yosys representation ​

First decide whether a generic representation is sufficient. Use a SILICON_* cell when native class identity or semantics could not be recovered from the generic network. Otherwise preserve behavior, bus width, signedness, edge polarity, enable/reset semantics, and truncation rules with generic cells. Propagation delays remain simulation metadata and are intentionally omitted.

3. Validate the component's shape ​

Before emitting anything, validate the component's actual buses against its properties. requireBus() handles a required nonempty bus, but component-specific invariants still need explicit checks. For example, a multi-bus Multiplexer with selection width N must expose exactly 2^N equally sized data buses followed by the selection bus. Export must reject a malformed shape rather than generate internally inconsistent JSON.

4. Emit cells with SerializationContext ​

Use SerializationContext as follows:

  • bits(bus, nullValue) returns the module signal numbers or constant literals for an existing bus. Connection arrays are LSB-first, exactly like Bus.
  • allocateBits(width) reserves module-local temporary signals for a multi-cell lowering.
  • concatenate(vectors) appends already LSB-first vectors in the supplied lane order. It does not reverse individual vectors.
  • parameter(value, width) produces the fixed-width, MSB-first binary strings used by write_json parameters. Do not feed connection-order strings into it.
  • addCell(suffix, type, parameters, portDirections, connections) inserts a uniquely named cell in the current module. Every connection and direction key must match the chosen Yosys cell contract.
  • addPort() is reserved for components that represent module boundaries; addSubcircuitInstance() emits a hierarchical module instance.

For example, the central part of the Multiplexer lowering maps its buses to $bmux:

cpp
context.addCell(
    "mux", "$bmux",
    Json{{"WIDTH", SerializationContext::parameter(busWidth)},
         {"S_WIDTH", SerializationContext::parameter(selectionWidth)}},
    directions({{"A", "input"}, {"S", "input"}, {"Y", "output"}}),
    Json{{"A", SerializationContext::concatenate(lanes)},
         {"S", context.bits(requireBus(*this, true, selectionIndex))},
         {"Y", context.bits(requireBus(*this, false, 0))}});

Here lanes is ordered by selection index. For a one-bit mux it contains the existing packed data bus; for a wider mux it contains one busWidth vector per selectable input. The resulting $bmux.A connection is therefore lane 0 bits, lane 1 bits, and so on, without changing the bit order inside a lane.

5. Compose generic cells only when native identity is not required ​

If the native behavior has no direct cell, compose it from supported cells. Use allocateBits() for intermediate results and give each addCell() call a stable suffix such as fold_1 or invert. Cell names are generated deterministically from the circuit vertex, component type, and suffix, which makes output reproducible and diagnostics readable.

Import: recognize and validate explicit cell patterns ​

Yosys deserialization is deliberately ABI- and cell-pattern based. Native project deserialization uses ComponentRegistry; Yosys deserialization never consults it to infer external semantics. Dispatch known SILICON_* types before generic cells. Unknown types and unsupported variants must reach the contextual failure path rather than becoming placeholders.

1. Wire in a focused importer ​

Add the component header to yosys_deserialization.cpp, implement a focused importer method, and dispatch to it from importCell(). Do not add a registry lookup: an arbitrary Yosys cell name is not proof that it has the semantics of a same-named Silicon class.

2. Validate first, construct last ​

An importer method should perform these steps in order:

  1. Call requireConnectionSet() with the exact accepted port names. This rejects both missing and unexpected connections.
  2. Read parameters with checkedWidth(), parameterFlag(), or parseUnsigned() as appropriate. These helpers accept the supported write_json integer/binary forms and reject zero, oversized, non-binary, or non-Boolean values where required.
  3. Read inputs with input()/inputBit() and outputs with output()/outputBit(). Inputs may contain supported constants; driven outputs must be numeric. Output reads also register drivers, so a later duplicate driver fails the whole import.
  4. Cross-check every declared width against its connection length and enforce semantic restrictions such as unsigned-only arithmetic, supported polarities, or scalar-only storage cells.
  5. Construct the native component, set the properties that define its shape, then attach the validated input and output buses. Push it into components only after the pattern is known to be valid.
3. Define every accepted shape explicitly ​

For muxes the importer accepts two related patterns. $mux requires equally wide A, B, and Y plus a one-bit S, and becomes a Multiplexer with selectionSize == 1. $bmux requires A.size() == WIDTH * 2^S_WIDTH, S.size() == S_WIDTH, and Y.size() == WIDTH; it splits A into LSB-first lanes and restores selectionSize and busSize. Width-one lanes use Silicon's packed-data representation, while wider lanes become separate input buses.

4. Set the correct round-trip expectation ​

Be explicit about how exact a round trip can be. Multiplexer exports as $bmux and can be reconstructed as Multiplexer, but a NandGate exports as $and followed by $not and imports as those two native components. The required contract is equivalent supported logic and connectivity, not preservation of the original class or graphical layout.

5. Decide how hierarchy is handled ​

Finally, decide whether the new cell is valid inside a selected flat module only or also needs hierarchy support. The current importer treats a module-instance cell type as unsupported; adding a native component branch does not reconstruct project subcircuits. Designs containing hierarchy must be externally flattened/lowered unless dedicated hierarchy import is implemented as a separate feature.

This import work is mandatory: implementing export alone does not make the component round-trip through Yosys.

6. Test every boundary ​

Add focused tests for:

  • simulation behavior, including UNKNOWN/ERROR, delay, edge, or cyclic behavior that is relevant to the component;
  • property defaults, validation failures, and every bus/port-resizing transition;
  • core registry creation, catalog metadata, unknown/duplicate registration, and a native Circuit::serialize() → Circuit::deserialize() round trip;
  • GUI factory creation, visual/core type mapping, restored setComponent() behavior, and shape/port updates after property edits;
  • Yosys export cell types and parameters, import reconstruction, and a Yosys round trip; and
  • malformed native or Yosys input: missing ports, inconsistent widths, unsupported parameter variants, and invalid properties must fail rather than produce a partially valid component.

The existing multiplexer tests, registry tests, and Yosys tests provide starting patterns. Keep tests focused on observable contracts rather than private helper functions.

For exact declarations while implementing the checklist, return to the Doxygen reference.