/** * Compiles a SimCircuit into the core's "Program format, version 2" * (format.ts, core/README.md). This is the TS front end's half of the * division of labour described in core/README.md: net building, per-pin * open-flag/hash computation and parameter resolution live here so the Rust * side never has to know what a SimCircuit is, and node ids are exactly * buildNets' so classic and core agree on which physical net is which. * * v2 adds two things beyond W0's program: the option block (integration * method, ambient temperature, and the test-only exactness switches) and the * readout slot tables. "Keys are built in TS; Rust publishes numbers" * (core/README.md, "Readouts") — `SlotAllocator` below is the machinery a * kind's compiler case uses to claim a digital or channel slot. W1c's 11 * combinational ICs are the first to claim a digital slot (one per * output-capable pin — see this file's IC dispatch case, and compile/ics.ts); * W1e's `rgb_led`/`bicolor_led`/`seg7_cc`/`seg7_ca` are the first to claim a * channel slot (compile/leds.ts), consecutively per component, in the same * allocation order. The machinery itself is exercised directly by * compile.test.ts's synthetic slot-table tests. */ import type { Net } from "../../sim/engine/graph.js"; import type { SimCircuit } from "../../sim/engine/sim-engine.js"; import { type LiveParamSpec } from "./passives.js"; import type { ThermalDeviceProfile } from "../../sim/thermal-physics.js"; import { type CompiledBatteryInfo } from "./sources.js"; export { buildCoreNodes, corePinKey } from "./topology.js"; export type { CoreNodeBuild } from "./topology.js"; export type { LiveParamSpec } from "./passives.js"; export type { CompiledBatteryInfo, SectionAnchor } from "./sources.js"; /** * Every kind's live-param spec (owner decision 1, `bo_engine_set_params`), * aggregated across the compiler's per-kind-family modules — the same * "index.ts aggregates, a per-kind-family module owns the actual logic" * split this file already uses for `compileCircuit`'s switch statement * itself. `CoreEngine.setParams` (engine.ts) is the only consumer: it maps a * caller's `(componentId, paramName)` to `(deviceIndex, paramIndex)` through * `compiled.devices[i].kind` -> this map -> `paramNames.indexOf`, then * gates the result against `CORE_RECORD_SHAPES.get(kindCode).liveParams` * before calling `resolveLiveValue`. Grows per sub-wave, alongside * `CORE_RECORD_SHAPES`/`CORE_IMPLEMENTED_KINDS` (kinds.ts) and whichever * compile/*.ts module a later kind's live params live in. */ export declare const LIVE_PARAM_SPECS: ReadonlyMap; /** A `digitalState` key, e.g. `u1/qa`, or a bare `u1` for a counter value (spec §10.9, core/README.md's "Readouts"). */ export type DigitalSlotKey = string; /** `(componentId, channel)` — one `getElementChannelI()` entry (core/README.md's "Readouts"). */ export type ChannelSlotKey = readonly [componentId: string, channel: number]; /** * Assigns sequential slot indices to whatever a kind's compiler case gives * it, in allocation order — the exact machinery `compileCircuit` needs to * turn "kind X's compiler case wants a digital/channel slot for key K" into * the slot index that goes into the kind's blob (core/README.md: "Where a * kind publishes digital or channel readouts, its blob starts with the * index of its first slot of each kind it uses"). * * Generic over the slot's key type so ONE class serves both the digital * table (`DigitalSlotKey`, a string) and the channel table (`ChannelSlotKey`, * a tuple) — the allocation logic itself (sequential indices, nothing more) * does not care which. */ export declare class SlotAllocator { private readonly items; /** Claims the next slot for `value`, returning its index. */ allocate(value: T): number; get count(): number; /** The finished slot table, index === slot number. */ get table(): readonly T[]; } /** * Caller-supplied compile options. `ambientC` is deliberately NOT here: the * contract's `AMBIENT_C` is a fact about the circuit * (`circuit.environment?.temperatureC`, classic's own `_ambientTempC` * default source), not a caller preference, so `compileCircuit` always * derives it from `circuit` itself. */ export interface CompileOptions { /** `INTEGRATION` (default `"be"`, classic's own default — sim-engine.ts's `_integrationMethod`). */ integrationMethod?: "be" | "trap"; /** `NO_SOLUTION_CACHE` — an exactness switch; harness/tests only. */ noSolutionCache?: boolean; /** `NO_RAIL_SHORTCUT` — an exactness switch; harness/tests only. */ noRailShortcut?: boolean; /** `FORCE_STEPPED` — tests only (core/README.md's Options table). */ forceStepped?: boolean; /** * `START_TIME` (default `0`) — the absolute time this program starts at, * core/README.md's Options table key 7: "classic keeps absolute simTime * across a reload, and time-dependent behaviour keys on it". `CoreEngine` * always supplies this itself on every `load()` (engine.ts), set to its own * carried-over facade clock, so Rust's internal clock is absolute and never * needs a TS-side offset layered back on top of it. A caller compiling * directly (tests, tooling) can still set it, but a normal host adapter * never needs to. */ startTimeS?: number; /** * W2c: EEPROM images by component id that replace the authored `contents` * as the ROM blob: what a carrying reload hands on (src/core/carry.ts, * `eepromCarryImages`). An image of the wrong size is ignored. */ eepromImages?: ReadonlyMap; } /** * One device record as the facade names it. `pinIds` is the record's pin * ids in pin-record order (for an IC, `vcc` and `gnd` first), so a * `bo_engine_records` record's `pin` index names classic's `pinId` * (core/README.md, Readouts, W1g). It is absent on the synthetic records, * which have no pins, and on a record with a by-id pin the component does not * declare, which no record ever names. */ export interface CompiledDevice { readonly componentId: string; readonly kind: string; readonly pinIds?: readonly string[]; } /** A component with a code 91 record: what `getThermalState` composes classic's readout from. */ export interface CompiledThermalDevice { readonly profile: ThermalDeviceProfile; readonly catalogUid: string; } export interface CompiledCircuit { program: Float64Array; /** Exactly `buildNets(circuit)` — same ids, same order as the classic engine's `nets`. */ nets: Net[]; nodeOfNet: ReadonlyMap; /** `null` for the synthetic datum when there is no "gnd" net. */ netIdOfNode: readonly (string | null)[]; /** Key `${componentId}\u0000${pinId}`. */ pinNode: ReadonlyMap; /** * One entry per device record, in record order: the components in circuit * order (skipping `ground`), then one `section_anchor` entry per isolated * section, then the W1g `rating` and `thermal` records. None of those is a * component, so each takes a synthetic `componentId` (`section_anchor:`, * `rating:`, `thermal:`, `device` the rated or heated * record's index), which no id-keyed lookup (`setParams`, `elementI`, the * device state) can mistake for the component that owns it; none gets an * `elementI` key (core/README.md, "Section anchor record", "Reader rules for * codes 90 and 91"). */ devices: ReadonlyArray; /** Per `battery_pack` component id: the statics `getBatteryState` adds to the device state (core/README.md, "Readouts"). */ batteries: ReadonlyMap; /** * Per `bench_psu` component id: its record's `iLimit`, 0 for an ideal * source. It decides which device-state layout `getIcState` expects back * (core/README.md, `bo_engine_device_state`: one value, `reg`, when `> 0`; * none otherwise), so the facade reads the value Rust was given rather than * re-deriving it from the circuit. */ benchPsuLimits: ReadonlyMap; /** Per component id with a code 91 record, in record order: its profile and `catalogUid` (core/README.md, Readouts, `getThermalState`). */ thermalDevices: ReadonlyMap; /** `AMBIENT_C`, the option this program was written with. */ ambientC: number; /** Digital slot `i` -> `digitalState` key. Empty unless the circuit contains a kind that claims one — W1c's combinational ICs are the first (compile/ics.ts). */ digitalSlots: readonly DigitalSlotKey[]; /** Channel slot `j` -> `(componentId, channel)`. Empty until a compiled kind claims one (none through W1c). */ channelSlots: readonly ChannelSlotKey[]; /** * The `getElementI()` key set, in component order, by classic's * `_updateElementI` rule (spec §10.5): every component with at least two * pins, whether or not its kind has a current model (classic defaults it * to 0). A circuit whose only node is the datum gives an empty set, because * classic's MNA then has no rows and its `_solve` publishes `elementI = {}`. * A circuit like that can hold no ideal source for the core, which rejects * a shorted source as unsupported topology. */ elementIKeys: readonly string[]; } /** * Classic `_updateElementI`'s key set (spec §10.5): every component with at * least two pins, or none at all when the only node is the datum (classic's * MNA then has no rows and publishes `elementI = {}`). Exported so tests can * pin it against a real classic load on circuits the core cannot compile. */ export declare function classicElementIKeys(circuit: SimCircuit, nodeCount: number): string[]; export type CompileResult = { ok: true; compiled: CompiledCircuit; } | { ok: false; reasons: string[]; }; /** * Compiles `circuit` into a core v2 program, or collects every reason it * can't be compiled and returns them instead. Never throws for a user * circuit — an unsupported kind, a bad parameter, a non-finite * `circuit.environment.temperatureC`, a wire naming a component the * circuit does not hold (an ownerless net, which has no component to own a * section anchor), or a component id two components share is data about the * circuit, not a bug in this function, so all five are reported (sorted, * deduplicated) rather than raised. * * `implementedKinds` defaults to CORE_IMPLEMENTED_KINDS. The parameter * exists so a caller (chiefly tests) can compile against a narrower set — * e.g. to check that a kind this module *could* stamp is still correctly * refused when the caller's kind set does not include it. It is not a way * to ask this function to stamp a kind it has no case for below: a kind * that is "implemented" per the caller's set but unrecognised here is a * kinds.ts/compile.ts drift bug, not a user-circuit error, and is raised * loudly rather than swallowed into `reasons` (see the `default` case). */ export declare function compileCircuit(circuit: SimCircuit, implementedKinds?: ReadonlySet, options?: CompileOptions): CompileResult;