/** * "Program format, version 3" — the wire format the TS compiler (compile/) * emits and the Rust W1a event kernel reads (core/README.md, "Program * format, version 3"). This file is the TS half of that contract: the * format constants, a small writer used by compile/index.ts, and a decoder * that performs every validation the contract requires of the Rust reader. * * The decoder exists for two reasons: a Float64Array is otherwise opaque to * a test, and it doubles as executable documentation of the exact layout — * the prose in core/README.md and the checks below must never drift, since * the Rust reader is written against the README's table, not against this * file. * * Every value in a program is an f64. Counts, indices and 32-bit hash states * are carried as exact doubles; every one of them this format ever produces * is far below 2^53, so there is no precision loss doing that. * * Layout (core/README.md, "Program format, version 3"): * header MAGIC, VERSION, nodeCount N, deviceCount D, * optionCount O, (key, value) x O, * digitalSlotCount S, channelSlotCount C * devices D records, in component order: * kindCode, componentIndex, pinCount, pin record x pinCount, * paramCount, param x paramCount, blobCount, blob x blobCount * trailer MAGIC * * A pin record is four values: `node, flags, openHash, deadHash` (the * contract's "pin record" bullet). Kind codes and per-kind record shapes * (pin/param counts, blob shape) are NOT owned here — they are the * orchestrator's table in kinds.ts (CORE_KIND_CODES / CORE_RECORD_SHAPES), * re-exported below under their v1 names too, since some call sites still * import them from this file. */ import { type CoreKindCodeName } from "./kinds.js"; /** "BOC1" read as one big-endian u32. Stamped at both the start and the end of every program. */ export declare const CORE_FORMAT_MAGIC = 1112490801; /** * The only version this package's TS and Rust halves understand. Both sides * ship in one npm package, so there is no cross-version compatibility to * preserve: a layout change bumps this constant on both sides in the same * commit, and decodeProgram rejects anything else outright rather than * trying to interpret an old or new layout. W1d bumped it from 2 to 3 * because the 74ls157 and 74ls245 records gained a delay tail * (core/README.md, "Program format, version 3"). */ export declare const CORE_FORMAT_VERSION = 3; /** * Program v4 (W2c, ruling R100 (a)): v3 with a `CARRY` block between the * devices and the trailer, `carryLength L, value x L` (core/README.md, * "Carry"). The writer emits it only for a carrying load * ({@link withCarryBlock}); a cold load stays v3, which every core since W1d * reads. */ export declare const CORE_FORMAT_VERSION_CARRY = 4; /** * v1 export names, kept as aliases now that the kind-code table moved to * kinds.ts (the orchestrator's table, shared with core/src/kinds.rs) — any * code still importing `CORE_KIND_CODE`/`CoreKindName` from format.ts (this * file's own v1-era tests, chiefly) keeps working unchanged. */ export declare const CORE_KIND_CODE: { readonly resistor: 1; readonly voltage_source: 2; readonly capacitor: 3; readonly switch: 4; readonly push_button: 5; readonly spdt_switch: 6; readonly dip_switch: 7; readonly potentiometer: 8; readonly trimmer: 9; readonly fuse: 10; readonly ldr: 11; readonly thermistor: 12; readonly ptc_fuse: 13; readonly battery_pack: 20; readonly bench_psu: 21; readonly clock: 22; readonly clock_gen: 23; readonly pulse_source: 24; readonly pulse_gen: 25; readonly section_anchor: 26; readonly led: 30; readonly rgb_led: 31; readonly bicolor_led: 32; readonly seg7_cc: 33; readonly seg7_ca: 34; readonly ne555: 40; readonly "74ls00": 50; readonly "74ls04": 51; readonly "74ls08": 52; readonly "74ls32": 53; readonly "74ls86": 54; readonly "74ls157": 55; readonly "74ls283": 56; readonly "74hc138": 57; readonly "74hc14": 58; readonly "74ls47": 59; readonly cd4511: 60; readonly "74ls245": 61; readonly "74ls161": 70; readonly "74ls173": 71; readonly "74hc74": 72; readonly "74hc595": 73; readonly "74hc165": 74; readonly cd4017: 75; readonly cd4060: 76; readonly "74ls189": 80; readonly "28c16": 81; readonly "28c256": 82; readonly rating: 90; readonly thermal: 91; }; export type CoreKindName = CoreKindCodeName; /** Reverse lookup: kind code -> kind name. Used by decodeProgram's error messages and by debugging tools. */ export declare const CORE_KIND_NAME_BY_CODE: ReadonlyMap; /** * Option keys, core/README.md "Program format, version 3" > the Options * table. Every key is optional in the wire format itself (each appears at * most once; an absent key means the reader applies the README's Default * column) — ProgramWriter/decodeProgram only validate the keys that ARE * present, they never invent a default value to write. */ export declare const CORE_OPTION_KEYS: { readonly INTEGRATION: 1; readonly NO_SOLUTION_CACHE: 2; readonly NO_RAIL_SHORTCUT: 3; readonly FORCE_STEPPED: 4; readonly AMBIENT_C: 5; readonly FLOATING_MODEL: 6; readonly START_TIME: 7; }; export type CoreOptionKeyName = keyof typeof CORE_OPTION_KEYS; /** * One pin's wire-format record before it is flattened into four raw * Float64Array values (core/README.md's pin-record bullet). `openHash`/ * `deadHash` are the FNV-1a-32 states after hashing * `${componentId}:${pinId}:` and `${componentId}:${pinId}:undefined:` * respectively (compile/topology.ts computes these; format.ts only carries * and validates the finished numbers). */ export interface ProgramPinInput { /** The pin's node index, `< nodeCount`. */ node: number; /** Classic's `_isOpenPin` result for this pin (spec §1.6). */ open: boolean; /** Whether this pin is in classic's `IC_OPEN_HIGH_PINS` (spec §1.5). */ openHigh: boolean; openHash: number; deadHash: number; } /** One decoded pin record — decodeProgram's view of a ProgramPinInput after the round trip. */ export interface DecodedPin { node: number; open: boolean; openHigh: boolean; openHash: number; deadHash: number; } /** One device record's input to ProgramWriter.addDevice, before it is flattened into the Float64Array. */ export interface ProgramDeviceInput { kindCode: number; /** Index into the source circuit's `components` array (contract: "the index in circuit.components"). */ componentIndex: number; /** Pin records, positional or by-id per the kind's "Pins (in order)" column (core/README.md, G7). */ pins: readonly ProgramPinInput[]; /** Resolved parameter values, positional per the kind's "Params" column. */ params: readonly number[]; /** ROM/RAM/pin-map payload. Empty for every W1a kind; later waves carry per-kind blob data. */ blob?: readonly number[]; } /** One `(key, value)` option pair, in the order it appears on the wire. */ export type ProgramOption = readonly [key: number, value: number]; /** * Builds one program: header (including the option block and readout slot * counts), then device records in the order addDevice is called (which must * be component order — the caller's job, not this class's), then trailer. * * `deviceCount`, `digitalSlotCount` and `channelSlotCount` are not known * until every device has been added (compile/index.ts allocates readout * slots WHILE compiling devices), so all three are written as zero * placeholders and patched by `finish()` — the only two-pass part of an * otherwise append-only writer. * * addDevice validates each record against kinds.ts's CORE_RECORD_SHAPES and * against this program's own nodeCount. This is a belt-and-braces check on * the producer side, not a substitute for decodeProgram (or the Rust * reader): the source of truth for "is this program well-formed" is the * reader, because that is what actually has to survive an untrusted or * hand-built Float64Array. */ export declare class ProgramWriter { private readonly values; private readonly nodeCount; private readonly deviceCountIndex; private readonly slotCountsIndex; private deviceCount; private finished; private readonly pendingDigitalSlotChecks; private readonly pendingChannelSlotChecks; constructor(nodeCount: number, options?: readonly ProgramOption[]); addDevice(device: ProgramDeviceInput): void; /** * Patches deviceCount and the readout slot counts, appends the trailer * magic, and returns the finished program. `digitalSlotCount`/ * `channelSlotCount` are the compiler's own slot-table lengths (compile/ * index.ts's SlotAllocator) — this class never allocates slots itself. */ finish(digitalSlotCount: number, channelSlotCount: number): Float64Array; } /** One decoded device record — decodeProgram's view of a ProgramDeviceInput after the round trip. */ export interface DecodedDevice { kindCode: number; componentIndex: number; pins: DecodedPin[]; params: number[]; blob: number[]; } export interface DecodedProgram { version: number; nodeCount: number; options: readonly ProgramOption[]; digitalSlotCount: number; channelSlotCount: number; devices: DecodedDevice[]; /** v4's `CARRY` block, as records (empty for v3). Checked against the devices by the core, not here. */ carry: readonly number[]; } /** `program` (v3, as {@link ProgramWriter} writes it) as v4 with `carry` as its `CARRY` block. */ export declare function withCarryBlock(program: Float64Array, carry: ArrayLike): Float64Array; /** * Reads and fully validates a program, mirroring every check the contract * requires of the Rust reader (core/README.md's "Validation" bullet: magic, * version, counts, each option key known/not repeated/in range, every node * index `< N`, flags with no unknown bit, hash states integral and in * range, known kind codes, the exact pin/param/blob shape per kind, every * param and blob value finite, every slot index in range, the trailer, and * no trailing data). This function IS that sentence, spelled out. * * There is no error-code return here the way there is over the C ABI * (core/README.md's `-1` et al. on the Rust side): on the TS side a * malformed program throws a plain Error whose message names the exact * field and offset that failed, since this function's job is to be a * precise diagnostic for tests and tooling, not a hot solver path. */ export declare function decodeProgram(program: Float64Array): DecodedProgram;