@mica-js/simulator 0.4.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -4,17 +4,20 @@ import { CanvasBindingTarget } from '@mica-js/core/canvas';
4
4
  import { DeviceReference } from '@mica-js/core/schematic';
5
5
  import { DeviceSymbolProvider } from '@mica-js/core/schematic';
6
6
  import { Disposable } from '@mica-js/core/lifecycle';
7
+ import { EntryRef } from '@mica-js/ir';
7
8
  import { Event as Event_2 } from '@mica-js/core/lifecycle';
8
9
  import { FunctionBuilder } from '@mica-js/ir';
9
10
  import { FunctionRef } from '@mica-js/ir';
10
- import { IRInterpreter } from '@mica-js/ir';
11
11
  import { IRJITTuning } from '@mica-js/ir';
12
12
  import { IRModule } from '@mica-js/ir';
13
13
  import { IRValidationOptions } from '@mica-js/ir';
14
14
  import { PayloadType } from '@mica-js/core/canvas';
15
+ import { PropertyValue } from '@mica-js/core/schematic';
15
16
  import { RpcEndpoint } from '@mica-js/core/rpc';
17
+ import { RuntimeScalar } from '@mica-js/ir';
16
18
  import { ScalarType } from '@mica-js/ir';
17
19
  import { SchematicDocument } from '@mica-js/core/schematic';
20
+ import { StateRef } from '@mica-js/ir';
18
21
  import { Value } from '@mica-js/ir';
19
22
 
20
23
  /**
@@ -34,48 +37,46 @@ import { Value } from '@mica-js/ir';
34
37
  *
35
38
  * circuit.runBatch(1_000_000); // one engine call
36
39
  * expect(circuit.tickCount).toBe(1_000_000n);
37
- *
38
40
  * circuit.reset();
39
- * circuit.setPin('drive1.PA0', true); // host drives the pin
40
41
  * circuit.step();
41
- * expect(circuit.devices.sink1.getState('lamp')).toBe(true);
42
42
  * ```
43
43
  *
44
- * `Circuit` owns the engine instance and the tick counter; the compiled module
45
- * keeps every device, net and the main loop in one `@mica-js/ir` module, so a
46
- * batch is one interpreter/JIT call for the whole circuit.
44
+ * `Circuit` owns the engine instance, the tick counter and the private
45
+ * step-boundary hook plumbing. RFC 0017 traffic belongs to the
46
+ * `DeviceSimulationHost`: it delivers topic inputs before a step and publishes
47
+ * the topic outputs the device hooks report afterwards. `Circuit` itself
48
+ * exposes no device state, pin or electrical readback.
49
+ *
50
+ * The compiled module keeps every device, net and the main loop in one
51
+ * `@mica-js/ir` module, so a batch is one interpreter/JIT call for the whole
52
+ * circuit.
47
53
  */
48
- export declare class Circuit<I extends CircuitCompilationInput = CircuitCompilationInput> {
54
+ export declare class Circuit {
49
55
  #private;
50
- /** Typed per-device accessors, keyed by device id. */
51
- readonly devices: DeviceAccessorsOf<I>;
52
56
  private constructor();
53
57
  /**
54
58
  * Compiles `input`, builds the engine instance and runs the generated
55
59
  * `initialize` entry once.
56
60
  *
57
- * The input is inferred as a `const` type, so a literal device list keeps its
58
- * ids and `circuit.devices.<id>` is typed without a cast.
59
- *
60
61
  * @throws {UnifiedIRCompilerError} when the circuit cannot be compiled.
61
62
  * @throws {CircuitError} for invalid options.
62
63
  */
63
- static create<const I extends CircuitCompilationInput>(input: I, options?: CircuitRuntimeOptions): Promise<Circuit<I>>;
64
+ static create(input: CircuitCompilationInput, options?: CircuitRuntimeOptions): Promise<Circuit>;
64
65
  /**
65
66
  * Runs `count` ticks inside one generated loop — the throughput path.
66
67
  *
67
- * The whole batch is one engine call; `tickCount` grows by `count` once the
68
- * call returns, so a failed batch never advances the clock.
68
+ * The whole batch is one engine call. The clock advances by completed ticks,
69
+ * including the completed prefix of a failed batch.
69
70
  *
70
71
  * @throws {CircuitError} with code `invalid-count` unless `count` is a u32.
71
72
  */
72
- runBatch(count: number): void;
73
+ runBatch(count: number, dt?: number): void;
73
74
  /**
74
75
  * Advances exactly one tick — the debugging path.
75
76
  *
76
77
  * Every call is one engine entry, so prefer `runBatch` for throughput.
77
78
  */
78
- step(): void;
79
+ step(dt?: number): void;
79
80
  /**
80
81
  * Restores every memory to its declared initial value, re-runs `initialize`
81
82
  * and resets `tickCount` to `0n`.
@@ -84,37 +85,15 @@ export declare class Circuit<I extends CircuitCompilationInput = CircuitCompilat
84
85
  * exactly like a freshly created one.
85
86
  */
86
87
  reset(): void;
87
- /**
88
- * Reads the level one pin observes.
89
- *
90
- * A sensing pin (input, passive or bidirectional) reports the solved net
91
- * level; a pure driver pin reports the level it actually drives, which is
92
- * `false` unless it is configured push-pull.
93
- *
94
- * @throws {CircuitError} with code `pin-not-found`.
95
- */
96
- getPin(pinName: string): boolean;
97
- /**
98
- * Forces the level of one pin from the host.
99
- *
100
- * A driver pin gets its output latch written, so the level survives the next
101
- * electrical solve. A sensing pin gets the solved-value slot overridden, which
102
- * lasts until the next tick recomputes its net.
103
- *
104
- * @throws {CircuitError} with code `pin-not-found`.
105
- */
106
- setPin(pinName: string, value: boolean): void;
107
88
  /** Number of ticks completed since creation or the last `reset()`. */
108
89
  get tickCount(): bigint;
109
90
  /** Virtual tick time completed since creation or the last `reset()`. */
110
91
  get virtualTime(): number;
111
- /** Underlying engine instance, e.g. for diagnostics or custom entries. */
112
- get jit(): IRInterpreter;
113
92
  /** Whether `dispose()` already ran. */
114
93
  get disposed(): boolean;
115
94
  /**
116
95
  * Releases the runtime. Idempotent; every later call throws a `CircuitError`
117
- * with code `disposed`, including stale device accessors.
96
+ * with code `disposed`.
118
97
  */
119
98
  dispose(): void;
120
99
  }
@@ -127,8 +106,13 @@ export declare interface CircuitCompilationInput {
127
106
 
128
107
  /** Options for the compiler. */
129
108
  export declare interface CircuitCompilationOptions {
130
- /** Named fixed-level rails; merged over `GND`/`VCC`/`VDD`. */
131
- readonly rails?: Readonly<Record<string, boolean>>;
109
+ /** Explicit GND-referenced voltage sources; GND itself is always 0 V. */
110
+ readonly sources?: Readonly<Record<string, {
111
+ readonly voltage: number;
112
+ }>>;
113
+ readonly interpretation?: 'two-state' | 'retain-in-band';
114
+ /** Reproducible mica-floating-v2 input seed; defaults to 0. */
115
+ readonly floatingSeed?: number;
132
116
  /** Runs `validateIR` over the generated module. Defaults to `true`. */
133
117
  readonly validate?: boolean;
134
118
  /** Overrides for the generated entry-point names. */
@@ -139,34 +123,20 @@ export declare interface CircuitCompilationOptions {
139
123
  };
140
124
  }
141
125
 
142
- export declare interface CircuitDeviceSimulationHostOptions {
143
- /** Circuit the caller already created. */
144
- readonly circuit: Circuit;
145
- /** Whether `dispose()` also disposes the circuit. Defaults to `true`. */
146
- readonly ownsCircuit?: boolean;
147
- /** Schematic the circuit was built from, reported through `getSchematic`. */
148
- readonly schematic?: SchematicDocument;
149
- /** Endpoints the running devices expose. */
150
- readonly topics?: readonly DeviceTopic[];
151
- /** Picoseconds per circuit step. Defaults to 10 ns. */
152
- readonly stepPs?: number;
153
- /** Routes one widget payload into the caller's device layer. */
154
- readonly deliver: (symbol: string, port: string, payload: unknown) => void;
155
- }
156
-
157
126
  /** Electrical-solve options forwarded to `compileCircuit`. */
158
127
  export declare interface CircuitElectricalOptions {
159
- /** Named fixed-level rails; merged over `GND`/`VCC`/`VDD`. */
160
- readonly rails?: Readonly<Record<string, boolean>>;
128
+ readonly sources?: CircuitCompilationOptions['sources'];
129
+ readonly interpretation?: CircuitCompilationOptions['interpretation'];
130
+ readonly floatingSeed?: CircuitCompilationOptions['floatingSeed'];
161
131
  }
162
132
 
163
133
  /**
164
134
  * Types of the unified circuit runtime (U02).
165
135
  *
166
136
  * `Circuit` is the host-facing facade over one compiled unified IR module: it
167
- * owns the engine instance, exposes typed per-device accessors and keeps the
168
- * tick counter. Everything here is transport-neutral; no type mentions the
169
- * device host or the schematic editor.
137
+ * owns the engine instance, the tick counter and the private step-boundary hook
138
+ * plumbing. Everything here is transport-neutral; no type mentions the device
139
+ * host or the schematic editor.
170
140
  */
171
141
  /** Execution engine a `Circuit` runs on. */
172
142
  export declare type CircuitEngine = 'jit' | 'reference';
@@ -181,17 +151,15 @@ export declare class CircuitError extends Error {
181
151
  readonly name = "CircuitError";
182
152
  readonly code: CircuitErrorCode;
183
153
  readonly deviceId: string | undefined;
184
- readonly pinName: string | undefined;
185
154
  readonly stateName: string | undefined;
186
155
  constructor(code: CircuitErrorCode, message: string, options?: CircuitErrorOptions);
187
156
  }
188
157
 
189
158
  /** Error codes raised by `CircuitError`. */
190
- export declare type CircuitErrorCode = 'invalid-options' | 'invalid-count' | 'pin-not-found' | 'pin-not-writable' | 'state-not-found' | 'invalid-state-value' | 'unresolved-device' | 'disposed';
159
+ export declare type CircuitErrorCode = 'invalid-options' | 'invalid-count' | 'unsupported-operation' | 'state-not-found' | 'invalid-state-value' | 'unresolved-device' | 'disposed';
191
160
 
192
161
  export declare interface CircuitErrorOptions {
193
162
  readonly deviceId?: string;
194
- readonly pinName?: string;
195
163
  readonly stateName?: string;
196
164
  readonly cause?: unknown;
197
165
  }
@@ -218,20 +186,6 @@ export declare interface CircuitRuntimeOptions {
218
186
  readonly instanceId?: string;
219
187
  }
220
188
 
221
- /** One electrical-analysis finding; errors abort `compileCircuit`. */
222
- export declare interface CompilationDiagnostic {
223
- readonly code: CompilationDiagnosticCode;
224
- readonly severity: CompilationDiagnosticSeverity;
225
- readonly message: string;
226
- readonly netId: number | undefined;
227
- readonly endpoints: readonly string[];
228
- }
229
-
230
- /** Diagnostic codes produced by the compiler's electrical analysis. */
231
- export declare type CompilationDiagnosticCode = 'power-short' | 'rail-conflict' | 'floating-net';
232
-
233
- declare type CompilationDiagnosticSeverity = 'error' | 'warning';
234
-
235
189
  /**
236
190
  * Compiles several devices and their wiring into one unified IR module (U01).
237
191
  *
@@ -257,7 +211,7 @@ declare type CompilationDiagnosticSeverity = 'error' | 'warning';
257
211
  *
258
212
  * The pipeline is: validate the input, allocate the global pin and state
259
213
  * spaces, analyse the electrical topology, generate one function per device
260
- * phase, generate one solver per net, generate the main loop, then validate the
214
+ * phase, generate the resistive network solver and main loop, then validate the
261
215
  * resulting module with `validateIR`.
262
216
  *
263
217
  * @throws {UnifiedIRCompilerError} for malformed input, duplicate ids, unknown
@@ -269,28 +223,21 @@ export declare function compileCircuit(input: CircuitCompilationInput, options?:
269
223
  export declare interface CompiledCircuit {
270
224
  readonly module: IRModule;
271
225
  readonly entryPoints: CompiledEntryPoints;
226
+ /** Completed ticks in the most recent step/batch, including before a fault. */
227
+ readonly progress: StateRef<'u32'>;
272
228
  /** `'mcu1.PA0'` -> global pin id. */
273
229
  readonly pinMap: ReadonlyMap<string, number>;
274
230
  readonly deviceMap: ReadonlyMap<string, DeviceMetadata>;
275
- readonly netlist: CompiledNetlist;
276
231
  readonly pins: GlobalPinMap;
277
232
  readonly states: GlobalStateMap;
278
- readonly memory: CompiledCircuitMemory;
279
233
  /** Validation grants covering every instruction and memory of the module. */
280
234
  readonly validation: IRValidationOptions;
281
- /** Device ids in dependency order (drivers before the devices they feed). */
235
+ /** Device ids in stable declaration order. */
282
236
  readonly executionOrder: readonly string[];
283
237
  /** Every generated function name, in definition order. */
284
238
  readonly functions: readonly string[];
285
239
  }
286
240
 
287
- /** Pin memories every device and every net solver shares. */
288
- declare interface CompiledCircuitMemory {
289
- readonly mode: ArrayRef<'u8'>;
290
- readonly output: ArrayRef<'bool'>;
291
- readonly input: ArrayRef<'bool'>;
292
- }
293
-
294
241
  /** Generated entry points, both by name and as executable refs. */
295
242
  declare interface CompiledEntryPoints {
296
243
  readonly tick: string;
@@ -303,50 +250,6 @@ declare interface CompiledEntryPoints {
303
250
  };
304
251
  }
305
252
 
306
- /** One electrical net: a connected component of pins and rails. */
307
- declare interface CompiledNet {
308
- readonly netId: number;
309
- readonly pins: readonly CompiledPinConnection[];
310
- readonly rails: readonly CompiledRail[];
311
- readonly drivers: readonly CompiledPinConnection[];
312
- /** Pins whose role is `load`. */
313
- readonly loads: readonly CompiledPinConnection[];
314
- /** Every pin that reads the solved level, including bidirectional drivers. */
315
- readonly sinks: readonly CompiledPinConnection[];
316
- readonly source: CompiledNetSource;
317
- /** Constant level of a driver-less net; unused when `source === 'driver'`. */
318
- readonly level: boolean;
319
- }
320
-
321
- /** Result of the network topology analysis. */
322
- declare interface CompiledNetlist {
323
- readonly nets: readonly CompiledNet[];
324
- readonly diagnostics: readonly CompilationDiagnostic[];
325
- }
326
-
327
- /** Where a net's level comes from. */
328
- declare type CompiledNetSource = 'driver' | 'rail' | 'floating';
329
-
330
- /** One pin resolved onto a net. */
331
- declare interface CompiledPinConnection {
332
- /** `'mcu1.PA0'`. */
333
- readonly endpoint: string;
334
- readonly deviceId: string;
335
- readonly pinName: string;
336
- readonly globalPinId: number;
337
- readonly role: CompiledPinRole;
338
- readonly senses: boolean;
339
- }
340
-
341
- /** Role a pin plays on its net; v1 supports one driver per net. */
342
- declare type CompiledPinRole = 'driver' | 'load';
343
-
344
- /** One fixed-level rail on a net, e.g. `GND` (low) or `VCC` (high). */
345
- declare interface CompiledRail {
346
- readonly name: string;
347
- readonly level: boolean;
348
- }
349
-
350
253
  /** One pin-to-pin (or pin-to-rail) connection. */
351
254
  export declare interface Connection {
352
255
  /** `'mcu1.PA0'` or a rail name such as `'GND'`. */
@@ -354,14 +257,9 @@ export declare interface Connection {
354
257
  readonly to: string;
355
258
  }
356
259
 
357
- /**
358
- * Adapts one already-loaded unified-IR circuit to the RFC 0018 device host.
359
- *
360
- * This is the same-thread adapter for an owner that keeps `Circuit` accessors for itself (a
361
- * diagnostics bench, for example). Application/canvas integration uses
362
- * `createDeviceSimulationHostFromSandbox` from `@mica-js/simulator/sandbox` and never holds a circuit.
363
- */
364
- export declare function createCircuitDeviceSimulationHost({ circuit, ownsCircuit, schematic, topics, stepPs, deliver, }: CircuitDeviceSimulationHostOptions): DeviceSimulationHost;
260
+ export declare interface ContactDeclaration extends ResistorDeclaration {
261
+ readonly initial: 'open' | 'closed';
262
+ }
365
263
 
366
264
  /**
367
265
  * Compiles a schematic sheet into a circuit.
@@ -400,8 +298,8 @@ export declare function createModuleValidation(module: IRModule, updateResources
400
298
  /**
401
299
  * Compiles a schematic and reports which circuit device each symbol became.
402
300
  *
403
- * {@link createCircuitFromSchematic} returns only the circuit; hosts that route
404
- * schematic endpoints to circuit accessors need the mapping as well.
301
+ * {@link createCircuitFromSchematic} returns only the circuit; the host needs
302
+ * the symbol-to-device mapping as well.
405
303
  */
406
304
  export declare function createSchematicCircuit(schematic: SchematicDocument, options: SchematicCircuitOptions): Promise<SchematicCircuitResult>;
407
305
 
@@ -416,94 +314,9 @@ export declare function createSchematicCircuit(schematic: SchematicDocument, opt
416
314
  */
417
315
  export declare function createSchematicDeviceSimulationHost(options: SchematicDeviceSimulationHostOptions): DeviceSimulationHost;
418
316
 
419
- /** Rails every circuit understands without declaring them. */
420
- export declare const DEFAULT_RAILS: Readonly<Record<string, boolean>>;
421
-
422
317
  /** Global pin addresses one device may own; enough for the largest packages. */
423
318
  export declare const DEVICE_PIN_STRIDE = 512;
424
319
 
425
- /**
426
- * What every device accessor offers, whatever its type.
427
- *
428
- * `readPin` returns the level the *device* drives onto one of its pins (its
429
- * output latch), while `Circuit.getPin('device.pin')` returns the level the pin
430
- * *observes* after the electrical solve. Writing a pin only makes sense for
431
- * driver pins, so `writePin` rejects load and passive pins.
432
- */
433
- export declare interface DeviceAccessor {
434
- /** Circuit-unique device id. */
435
- readonly deviceId: string;
436
- /** Device type, e.g. `'push-pull-driver'`. */
437
- readonly type: string;
438
- /** Declared pin names, in declaration order. */
439
- readonly pinNames: readonly string[];
440
- /** Declared internal state names, in declaration order. */
441
- readonly stateNames: readonly string[];
442
- /** Reads the level the device drives onto `pinName`. */
443
- readPin(pinName: string): boolean;
444
- /** Writes the level the device drives onto `pinName`. */
445
- writePin(pinName: string, value: boolean): void;
446
- /** Reads one declared internal state. */
447
- getState(name: string): unknown;
448
- /** Writes one declared internal state. */
449
- setState(name: string, value: unknown): void;
450
- }
451
-
452
- /**
453
- * Builds the typed accessor of a device after its module is compiled.
454
- *
455
- * A description that supplies `accessor` replaces the generic accessor the
456
- * runtime would otherwise synthesize from `type`. The factory runs once per
457
- * circuit and closes over the layout its IR factory allocated.
458
- */
459
- export declare type DeviceAccessorFactory = (context: DeviceAccessorFactoryContext) => DeviceAccessorSurface;
460
-
461
- /** Host services handed to a device accessor factory at circuit creation. */
462
- export declare interface DeviceAccessorFactoryContext {
463
- readonly deviceId: string;
464
- /** Live engine storage (`IRStorage` in practice). */
465
- readonly storage: unknown;
466
- /** Throws once the owning circuit has been disposed. */
467
- readonly assertActive: () => void;
468
- /** Advances the owning circuit by one tick. */
469
- readonly step: () => void;
470
- /** Advances the owning circuit by `count` ticks. */
471
- readonly runBatch: (count: number) => void;
472
- }
473
-
474
- /** Every accessor of one circuit, keyed by device id. */
475
- export declare type DeviceAccessors = Readonly<Record<string, DeviceAccessor>>;
476
-
477
- /**
478
- * Accessor map derived from a literal compilation input.
479
- *
480
- * `Circuit.create` infers the device ids of a `const` input, so
481
- * `circuit.devices.<id>` is typed without a cast. A device package that wants a
482
- * richer surface builds it through `DeviceDescription.accessor` and casts at
483
- * the call site.
484
- */
485
- export declare type DeviceAccessorsOf<I extends CircuitCompilationInput> = {
486
- readonly [D in I['devices'][number] as D['id']]: DeviceAccessor;
487
- };
488
-
489
- /**
490
- * Structural surface every device accessor exposes.
491
- *
492
- * The compiler keeps this declaration independent from the runtime's
493
- * `DeviceAccessor` so a device kernel can ship its own typed accessor without
494
- * making the compiler depend on the runtime module.
495
- */
496
- declare interface DeviceAccessorSurface {
497
- readonly deviceId: string;
498
- readonly type: string;
499
- readonly pinNames: readonly string[];
500
- readonly stateNames: readonly string[];
501
- readPin(pinName: string): boolean;
502
- writePin(pinName: string, value: boolean): void;
503
- getState(name: string): unknown;
504
- setState(name: string, value: unknown): void;
505
- }
506
-
507
320
  /** Build surface handed to one device behavior. */
508
321
  export declare interface DeviceBehaviorApi {
509
322
  readonly deviceId: string;
@@ -514,15 +327,17 @@ export declare interface DeviceBehaviorApi {
514
327
  * simple devices never need to touch it.
515
328
  */
516
329
  readonly module: IRModule;
517
- /** Shared `pin_mode` / `pin_output` / `pin_input` memories. */
518
- readonly memory: CompiledCircuitMemory;
519
330
  /** Instance configuration from {@link DeviceConfig.config}. */
520
331
  readonly config: Readonly<Record<string, unknown>> | undefined;
521
332
  /** Every declared pin of the device, in declaration order. */
522
333
  readonly pins: readonly DevicePinHandle[];
523
- /** Pins whose role is `driver`. */
334
+ /** Pins initialized with physical drive capability. */
524
335
  readonly outputs: readonly DevicePinHandle[];
525
336
  pin(name: string): DevicePinHandle;
337
+ resistor(name: string): DeviceResistorHandle;
338
+ writeDigitalGroup(names: readonly string[], levels: readonly Value<'bool'>[]): void;
339
+ applyResistors(names: readonly string[], ohms: readonly Value<'f64'>[]): void;
340
+ applyConnections(names: readonly string[], closed: readonly Value<'bool'>[]): void;
526
341
  state<T extends ScalarType = ScalarType>(name: string): DeviceStateHandle<T>;
527
342
  }
528
343
 
@@ -537,6 +352,11 @@ export declare interface DeviceBehaviors {
537
352
  readonly update?: (api: DeviceBehaviorApi) => void;
538
353
  }
539
354
 
355
+ export declare interface DeviceBridgeHooks {
356
+ readonly applyInputs?: (io: DeviceInputIo) => void;
357
+ readonly flushOutputs?: (io: DeviceOutputIo) => void;
358
+ }
359
+
540
360
  /** One device instance of the circuit. */
541
361
  export declare interface DeviceConfig {
542
362
  /** Circuit-unique identity; prefix of every generated function name. */
@@ -544,14 +364,21 @@ export declare interface DeviceConfig {
544
364
  /** Device type, e.g. `'push-pull-driver'`; free-form label of the device model. */
545
365
  readonly type: string;
546
366
  readonly description: DeviceDescription;
367
+ /** Persistent instance values resolved against the JSON property declarations. */
368
+ readonly properties?: Readonly<Record<string, PropertyValue>>;
547
369
  /** Instance configuration; reserved for device kernels that take parameters. */
548
370
  readonly config?: Readonly<Record<string, unknown>>;
549
371
  }
550
372
 
551
373
  /** IR-level device description consumed by the compiler. */
552
- export declare interface DeviceDescription {
374
+ export declare interface DeviceDescription extends DeviceBridgeHooks {
553
375
  readonly name?: string;
554
376
  readonly pins: DevicePinMap;
377
+ readonly properties?: Readonly<Record<string, unknown>>;
378
+ readonly resistors?: Readonly<Record<string, ResistorDeclaration>>;
379
+ readonly connections?: Readonly<Record<string, ContactDeclaration>>;
380
+ /** Paired synchronous JS factory, invoked once per instance before IR generation. */
381
+ readonly factory: (context: ElectricalFactoryContext) => DeviceImplementation | void;
555
382
  readonly states?: Readonly<Record<string, DeviceStateDescription>>;
556
383
  readonly behaviors?: DeviceBehaviors;
557
384
  /**
@@ -560,11 +387,6 @@ export declare interface DeviceDescription {
560
387
  * the behaviour hooks the compiler should call inside each phase.
561
388
  */
562
389
  readonly kernel?: DeviceKernelFactory;
563
- /**
564
- * Optional typed accessor factory. Kernels with a richer surface than the
565
- * generic state/pin accessor (a register file, a memory map) use it.
566
- */
567
- readonly accessor?: DeviceAccessorFactory;
568
390
  /**
569
391
  * Read-only byte regions the kernel wants host updates for. The compiler
570
392
  * grants `update` access on top of the normal `read` grant so kernels can
@@ -573,16 +395,38 @@ export declare interface DeviceDescription {
573
395
  readonly updateResources?: readonly BytesRef[];
574
396
  }
575
397
 
398
+ export declare interface DeviceElectricalInitialization {
399
+ initializeResistor(id: string, initial: ResistorInitialState): void;
400
+ initializePin(id: string, initial: PinElectricalDescription): void;
401
+ }
402
+
403
+ /** Per-instance construction and boundary hooks returned by the paired factory. */
404
+ export declare interface DeviceImplementation extends DeviceBehaviors, DeviceBridgeHooks {
405
+ readonly kernel?: DeviceKernelFactory;
406
+ }
407
+
408
+ export declare interface DeviceInput {
409
+ readonly port: string;
410
+ readonly payload: unknown;
411
+ }
412
+
413
+ export declare interface DeviceInputIo {
414
+ takeInput(port: string): unknown;
415
+ takeInputs(port: string): readonly unknown[];
416
+ writeState(name: string, value: unknown): void;
417
+ call(entry: EntryRef, args: readonly RuntimeScalar[]): void;
418
+ }
419
+
576
420
  /**
577
421
  * Everything a complex device kernel needs to declare its own IR before any
578
422
  * phase function body is opened: the module (for `array`/`bytes`/`provider`),
579
- * the shared pin memories and the allocated pin ids.
423
+ * typed operations through `bind(builder)` and the allocated pin ids.
580
424
  */
581
425
  export declare interface DeviceKernelContext {
582
426
  readonly deviceId: string;
583
427
  readonly config: Readonly<Record<string, unknown>> | undefined;
584
428
  readonly module: IRModule;
585
- readonly memory: CompiledCircuitMemory;
429
+ bind(builder: FunctionBuilder<readonly []>): DeviceBehaviorApi;
586
430
  readonly pins: readonly DeviceKernelPin[];
587
431
  }
588
432
 
@@ -601,7 +445,6 @@ export declare type DeviceKernelFactory = (context: DeviceKernelContext) => Devi
601
445
  declare interface DeviceKernelPin {
602
446
  readonly name: string;
603
447
  readonly globalPinId: number;
604
- readonly role: CompiledPinRole;
605
448
  }
606
449
 
607
450
  /**
@@ -616,8 +459,9 @@ export declare interface DeviceKernelRuntime {
616
459
  }
617
460
 
618
461
  /** Metadata of one compiled device. */
619
- declare interface DeviceMetadata {
462
+ declare interface DeviceMetadata extends DeviceBridgeHooks {
620
463
  readonly deviceId: string;
464
+ readonly entries?: readonly EntryRef[];
621
465
  readonly type: string;
622
466
  /** First global pin id of the device. */
623
467
  readonly pinOffset: number;
@@ -630,10 +474,11 @@ declare interface DeviceMetadata {
630
474
  readonly initialize: string | undefined;
631
475
  readonly tick: string | undefined;
632
476
  readonly update: string | undefined;
633
- /** Typed accessor factory supplied by the device description, if any. */
634
- readonly accessor: DeviceAccessorFactory | undefined;
635
- /** Kernel factory that generated the device's extra IR resources. */
636
- readonly kernel: DeviceKernelFactory | undefined;
477
+ }
478
+
479
+ export declare interface DeviceOutputIo {
480
+ readState(name: string): unknown;
481
+ publish(port: string, payload: unknown): void;
637
482
  }
638
483
 
639
484
  /** Pins of one device mapped into the global pin space. */
@@ -645,16 +490,6 @@ declare interface DevicePinAllocation {
645
490
  readonly resolved: ReadonlyMap<string, ResolvedDevicePin>;
646
491
  }
647
492
 
648
- /** One pin declared by a device description. */
649
- export declare interface DevicePinDescription {
650
- /** Defaults to `'passive'`: the pin only senses the net. */
651
- readonly direction?: DevicePinDirection;
652
- /** Overrides the role derived from `direction`. */
653
- readonly role?: CompiledPinRole;
654
- /** Purely informational kind, e.g. `'signal'`, `'power'`, `'gnd'`. */
655
- readonly kind?: string;
656
- }
657
-
658
493
  /**
659
494
  * Types for the unified IR compiler (U01).
660
495
  *
@@ -667,20 +502,27 @@ export declare interface DevicePinDescription {
667
502
  * live in `../pin-space` and are reused here. The compiler ships no device
668
503
  * models: every description is supplied by the caller.
669
504
  */
670
- /** Electrical intent of one device pin. */
671
- export declare type DevicePinDirection = 'input' | 'output' | 'bidirectional' | 'passive';
505
+ /** One pin declared by the paired electrical device definition. */
506
+ export declare interface DevicePinDescription {
507
+ readonly kind: 'signal' | 'power' | 'gnd';
508
+ readonly side?: 'left' | 'right' | 'top' | 'bottom';
509
+ }
672
510
 
673
511
  /** Pin operations a behavior may use; each call inlines into global memory. */
674
512
  declare interface DevicePinHandle {
675
513
  readonly name: string;
676
514
  readonly globalId: number;
677
- /** `pin.configure`: writes `pin_mode[globalId]`. */
515
+ /** Selects the physical output configuration. */
678
516
  configure(mode: number): void;
679
- /** `pin.write_digital`: writes `pin_output[globalId]`. */
517
+ setOutputEnabled(enabled: Value<'bool'>): void;
518
+ setPull(pull: 'none' | 'up' | 'down'): void;
519
+ writeVoltage(voltage: Value<'f64'>): void;
520
+ /** Writes digital drive intent through the finite output impedance. */
680
521
  writeDigital(level: Value<'bool'>): void;
681
- /** `pin.sample_digital`: reads the solved `pin_input[globalId]`. */
522
+ /** Samples the solved voltage with the explicitly declared input thresholds. */
682
523
  sampleDigital(): Value<'bool'>;
683
- /** Reads the device's own `pin_output[globalId]`. */
524
+ sampleVoltage(): Value<'f64'>;
525
+ /** Reads this pin's digital drive intent. */
684
526
  readDrivenDigital(): Value<'bool'>;
685
527
  }
686
528
 
@@ -690,12 +532,25 @@ declare interface DevicePinHandle {
690
532
  */
691
533
  declare type DevicePinMap = ReadonlyMap<string, DevicePinDescription> | Readonly<Record<string, DevicePinDescription>>;
692
534
 
535
+ declare interface DeviceResistorHandle {
536
+ setResistance(ohms: Value<'f64'>): void;
537
+ setEnabled(enabled: Value<'bool'>): void;
538
+ /** Solved current in amperes, positive from the first declared pin to the second. */
539
+ sampleCurrent(): Value<'f64'>;
540
+ }
541
+
693
542
  /** What one {@link DeviceSimulationHost.tick} call executed. */
694
543
  export declare interface DeviceSimTickReport {
695
544
  /** Simulation time after the step, in picoseconds. */
696
545
  readonly simTime: number;
697
546
  /** Circuit steps executed by this call. */
698
547
  readonly executed: number;
548
+ /** Isolated device boundary-hook failures; absent when the step has none. */
549
+ readonly diagnostics?: readonly {
550
+ readonly code: 'device/input-failed' | 'device/output-failed';
551
+ readonly deviceId: string;
552
+ readonly message: string;
553
+ }[];
699
554
  }
700
555
 
701
556
  /**
@@ -752,6 +607,8 @@ export declare interface DeviceSimulationHostGuest {
752
607
  * package that owns devices bundles its own worker script and supplies these.
753
608
  */
754
609
  export declare interface DeviceSimulationHostGuestOptions {
610
+ /** Fixed electrical configuration supplied by the owning worker module. */
611
+ readonly electrical?: CircuitElectricalOptions;
755
612
  /** Symbol geometry of the schematics this guest loads, or a factory called per load. */
756
613
  readonly devices?: DeviceSymbolProvider | (() => DeviceSymbolProvider);
757
614
  /** Resolves a schematic device reference; when omitted every reference stays unresolved. */
@@ -812,6 +669,28 @@ export declare interface DeviceTopic {
812
669
 
813
670
  export { Disposable }
814
671
 
672
+ export declare class ElectricalDefinitionError extends Error {
673
+ readonly code: ElectricalDefinitionErrorCode;
674
+ readonly name = "ElectricalDefinitionError";
675
+ constructor(code: ElectricalDefinitionErrorCode, message: string);
676
+ }
677
+
678
+ export declare type ElectricalDefinitionErrorCode = 'electrical/invalid-definition' | 'electrical/invalid-parameter' | 'electrical/unknown-resource' | 'electrical/duplicate-initialization' | 'electrical/missing-initialization' | 'electrical/factory-closed';
679
+
680
+ export declare interface ElectricalFactoryContext {
681
+ readonly properties: Readonly<Record<string, PropertyValue>>;
682
+ readonly electrical: DeviceElectricalInitialization;
683
+ }
684
+
685
+ export declare interface ElectricalPinDeclaration {
686
+ readonly kind: 'signal' | 'power' | 'gnd';
687
+ readonly side?: 'left' | 'right' | 'top' | 'bottom';
688
+ }
689
+
690
+ export declare interface ElectricalReference {
691
+ readonly source: string;
692
+ }
693
+
815
694
  export { Event_2 as Event }
816
695
 
817
696
  /** Result of the global pin allocation. */
@@ -833,7 +712,7 @@ declare interface GlobalStateMap {
833
712
  readonly totalStates: number;
834
713
  }
835
714
 
836
- /** Values stored in the global `pin_mode` array. */
715
+ /** Physical output configuration values. */
837
716
  export declare const PIN_MODE: Readonly<{
838
717
  readonly highZ: 0;
839
718
  readonly pushPull: 1;
@@ -841,18 +720,44 @@ export declare const PIN_MODE: Readonly<{
841
720
  readonly analog: 3;
842
721
  }>;
843
722
 
844
- /** One value of the global `pin_mode` array. */
723
+ export declare interface PinElectricalDescription {
724
+ readonly drive?: {
725
+ readonly reference: ElectricalReference;
726
+ readonly highResistance: number;
727
+ readonly lowResistance: number;
728
+ readonly analogResistance?: number;
729
+ readonly maxAbsCurrent?: number;
730
+ };
731
+ readonly bias?: {
732
+ readonly reference: ElectricalReference;
733
+ readonly upResistance: number;
734
+ readonly downResistance: number;
735
+ };
736
+ readonly input?: {
737
+ readonly reference: ElectricalReference;
738
+ readonly low: number;
739
+ readonly high: number;
740
+ };
741
+ }
742
+
743
+ /** One physical output configuration value. */
845
744
  export declare type PinMode = (typeof PIN_MODE)[keyof typeof PIN_MODE];
846
745
 
746
+ export declare interface ResistorDeclaration {
747
+ readonly pins: readonly [string, string];
748
+ }
749
+
750
+ export declare interface ResistorInitialState {
751
+ readonly resistance: number;
752
+ readonly enabled: boolean;
753
+ readonly maxAbsCurrent?: number;
754
+ }
755
+
847
756
  /** One resolved pin in the global pin space. */
848
757
  declare interface ResolvedDevicePin {
849
758
  readonly name: string;
850
759
  readonly deviceId: string;
851
760
  readonly globalPinId: number;
852
- readonly direction: DevicePinDirection;
853
- readonly role: CompiledPinRole;
854
- /** Whether the device reads the solved value of this pin. */
855
- readonly senses: boolean;
856
761
  }
857
762
 
858
763
  /** One component symbol of a compiled schematic. */
@@ -891,20 +796,24 @@ declare interface SchematicCircuitResult {
891
796
  export declare interface SchematicDeviceBinding {
892
797
  /** Device type, e.g. `'counter-ir'`. */
893
798
  readonly type: string;
894
- /** IR description: pins, states and behaviours. */
799
+ /** IR description: pins, states, factory, behaviours and boundary hooks. */
895
800
  readonly description: DeviceDescription;
896
801
  /** Instance configuration reserved for device kernels. */
897
802
  readonly config?: Readonly<Record<string, unknown>>;
898
- /** Endpoints the simulation host exposes; absent means the device has none. */
803
+ /**
804
+ * Endpoints the simulation host exposes; absent means the device has none.
805
+ *
806
+ * A host routes payloads through the description's `applyInputs` /
807
+ * `flushOutputs` hooks: topic names are host-facing, never a route into
808
+ * device state.
809
+ */
899
810
  readonly topics?: readonly SchematicDeviceTopic[];
900
- /** Bridges one input topic payload onto the compiled device accessor. */
901
- deliver?(accessor: DeviceAccessor, port: string, payload: unknown): void;
902
- /** Reads one output topic from the compiled device accessor. */
903
- read?(accessor: DeviceAccessor, port: string): unknown;
904
811
  }
905
812
 
906
813
  /** Reads the bytes of one schematic URI; the caller resolves relative references. */
907
814
  export declare interface SchematicDeviceSimulationHostOptions {
815
+ /** Local startup configuration, not an RPC method or payload. */
816
+ readonly electrical?: CircuitElectricalOptions;
908
817
  readonly read: (uri: string) => Promise<Uint8Array>;
909
818
  /**
910
819
  * Symbol geometry of the schematics this host loads. Pass the same provider the schematic was
@@ -962,19 +871,15 @@ export declare class UnifiedIRCompilerError extends Error {
962
871
  readonly code: UnifiedIRCompilerErrorCode;
963
872
  readonly deviceId: string | undefined;
964
873
  readonly pinName: string | undefined;
965
- readonly netId: number | undefined;
966
- readonly diagnostics: readonly CompilationDiagnostic[];
967
874
  constructor(code: UnifiedIRCompilerErrorCode, message: string, options?: UnifiedIRCompilerErrorOptions);
968
875
  }
969
876
 
970
877
  /** Error codes raised by {@link UnifiedIRCompilerError}. */
971
- export declare type UnifiedIRCompilerErrorCode = 'invalid-input' | 'duplicate-device' | 'unknown-device' | 'pin-not-found' | 'state-not-found' | 'invalid-connection' | 'capacity-exceeded' | 'unsupported-device' | 'power-short' | 'rail-conflict' | 'invalid-ir';
878
+ export declare type UnifiedIRCompilerErrorCode = 'invalid-input' | 'duplicate-device' | 'state-not-found' | 'capacity-exceeded' | 'invalid-ir';
972
879
 
973
880
  export declare interface UnifiedIRCompilerErrorOptions {
974
881
  readonly deviceId?: string;
975
882
  readonly pinName?: string;
976
- readonly netId?: number;
977
- readonly diagnostics?: readonly CompilationDiagnostic[];
978
883
  readonly cause?: unknown;
979
884
  }
980
885