@mica-js/simulator 1.0.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
@@ -8,7 +8,6 @@ import { EntryRef } from '@mica-js/ir';
8
8
  import { Event as Event_2 } from '@mica-js/core/lifecycle';
9
9
  import { FunctionBuilder } from '@mica-js/ir';
10
10
  import { FunctionRef } from '@mica-js/ir';
11
- import { IRInterpreter } from '@mica-js/ir';
12
11
  import { IRJITTuning } from '@mica-js/ir';
13
12
  import { IRModule } from '@mica-js/ir';
14
13
  import { IRValidationOptions } from '@mica-js/ir';
@@ -38,32 +37,31 @@ import { Value } from '@mica-js/ir';
38
37
  *
39
38
  * circuit.runBatch(1_000_000); // one engine call
40
39
  * expect(circuit.tickCount).toBe(1_000_000n);
41
- *
42
40
  * circuit.reset();
43
41
  * circuit.step();
44
- * expect(circuit.devices.sink1.getState('lamp')).toBe(true);
45
42
  * ```
46
43
  *
47
- * `Circuit` owns the engine instance and the tick counter; the compiled module
48
- * keeps every device, net and the main loop in one `@mica-js/ir` module, so a
49
- * 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.
50
53
  */
51
- export declare class Circuit<I extends CircuitCompilationInput = CircuitCompilationInput> {
54
+ export declare class Circuit {
52
55
  #private;
53
- /** Typed per-device accessors, keyed by device id. */
54
- readonly devices: DeviceAccessorsOf<I>;
55
56
  private constructor();
56
57
  /**
57
58
  * Compiles `input`, builds the engine instance and runs the generated
58
59
  * `initialize` entry once.
59
60
  *
60
- * The input is inferred as a `const` type, so a literal device list keeps its
61
- * ids and `circuit.devices.<id>` is typed without a cast.
62
- *
63
61
  * @throws {UnifiedIRCompilerError} when the circuit cannot be compiled.
64
62
  * @throws {CircuitError} for invalid options.
65
63
  */
66
- static create<const I extends CircuitCompilationInput>(input: I, options?: CircuitRuntimeOptions): Promise<Circuit<I>>;
64
+ static create(input: CircuitCompilationInput, options?: CircuitRuntimeOptions): Promise<Circuit>;
67
65
  /**
68
66
  * Runs `count` ticks inside one generated loop — the throughput path.
69
67
  *
@@ -87,32 +85,15 @@ export declare class Circuit<I extends CircuitCompilationInput = CircuitCompilat
87
85
  * exactly like a freshly created one.
88
86
  */
89
87
  reset(): void;
90
- /**
91
- * Reads the level one pin observes.
92
- *
93
- * Requires an explicitly initialized digital input capability and applies
94
- * the circuit interpretation to the solved physical voltage.
95
- *
96
- * @throws {CircuitError} with code `pin-not-found`.
97
- */
98
- getPin(pinName: string): boolean;
99
- /** Physical pin voltage; floating results never contain a synthetic voltage. */
100
- getVoltage(pinName: string): ElectricalReading;
101
- /** Resistor/contact currents follow endpoint order; source and pin contributions flow outward. */
102
- getCurrent(resource: string | ElectricalCurrentTarget): ElectricalReading;
103
- /** Pull-based active diagnostics: one warning per resource; successful recovery removes it. */
104
- getDiagnostics(): readonly ElectricalDiagnostic[];
105
88
  /** Number of ticks completed since creation or the last `reset()`. */
106
89
  get tickCount(): bigint;
107
90
  /** Virtual tick time completed since creation or the last `reset()`. */
108
91
  get virtualTime(): number;
109
- /** Underlying engine instance, e.g. for diagnostics or custom entries. */
110
- get jit(): IRInterpreter;
111
92
  /** Whether `dispose()` already ran. */
112
93
  get disposed(): boolean;
113
94
  /**
114
95
  * Releases the runtime. Idempotent; every later call throws a `CircuitError`
115
- * with code `disposed`, including stale device accessors.
96
+ * with code `disposed`.
116
97
  */
117
98
  dispose(): void;
118
99
  }
@@ -142,21 +123,6 @@ export declare interface CircuitCompilationOptions {
142
123
  };
143
124
  }
144
125
 
145
- export declare interface CircuitDeviceSimulationHostOptions {
146
- /** Circuit the caller already created. */
147
- readonly circuit: Circuit;
148
- /** Whether `dispose()` also disposes the circuit. Defaults to `true`. */
149
- readonly ownsCircuit?: boolean;
150
- /** Schematic the circuit was built from, reported through `getSchematic`. */
151
- readonly schematic?: SchematicDocument;
152
- /** Endpoints the running devices expose. */
153
- readonly topics?: readonly DeviceTopic[];
154
- /** Picoseconds per circuit step. Defaults to 10 ns. */
155
- readonly stepPs?: number;
156
- /** Routes one widget payload into the caller's device layer. */
157
- readonly deliver: (symbol: string, port: string, payload: unknown) => void;
158
- }
159
-
160
126
  /** Electrical-solve options forwarded to `compileCircuit`. */
161
127
  export declare interface CircuitElectricalOptions {
162
128
  readonly sources?: CircuitCompilationOptions['sources'];
@@ -164,6 +130,14 @@ export declare interface CircuitElectricalOptions {
164
130
  readonly floatingSeed?: CircuitCompilationOptions['floatingSeed'];
165
131
  }
166
132
 
133
+ /**
134
+ * Types of the unified circuit runtime (U02).
135
+ *
136
+ * `Circuit` is the host-facing facade over one compiled unified IR module: it
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.
140
+ */
167
141
  /** Execution engine a `Circuit` runs on. */
168
142
  export declare type CircuitEngine = 'jit' | 'reference';
169
143
 
@@ -177,17 +151,15 @@ export declare class CircuitError extends Error {
177
151
  readonly name = "CircuitError";
178
152
  readonly code: CircuitErrorCode;
179
153
  readonly deviceId: string | undefined;
180
- readonly pinName: string | undefined;
181
154
  readonly stateName: string | undefined;
182
155
  constructor(code: CircuitErrorCode, message: string, options?: CircuitErrorOptions);
183
156
  }
184
157
 
185
158
  /** Error codes raised by `CircuitError`. */
186
- export declare type CircuitErrorCode = 'invalid-options' | 'invalid-count' | 'unsupported-operation' | 'resource-not-found' | 'pin-not-found' | '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';
187
160
 
188
161
  export declare interface CircuitErrorOptions {
189
162
  readonly deviceId?: string;
190
- readonly pinName?: string;
191
163
  readonly stateName?: string;
192
164
  readonly cause?: unknown;
193
165
  }
@@ -289,15 +261,6 @@ export declare interface ContactDeclaration extends ResistorDeclaration {
289
261
  readonly initial: 'open' | 'closed';
290
262
  }
291
263
 
292
- /**
293
- * Adapts one already-loaded unified-IR circuit to the RFC 0018 device host.
294
- *
295
- * This is the same-thread adapter for an owner that keeps `Circuit` accessors for itself (a
296
- * diagnostics bench, for example). Application/canvas integration uses
297
- * `createDeviceSimulationHostFromSandbox` from `@mica-js/simulator/sandbox` and never holds a circuit.
298
- */
299
- export declare function createCircuitDeviceSimulationHost({ circuit, ownsCircuit, schematic, topics, stepPs, deliver, }: CircuitDeviceSimulationHostOptions): DeviceSimulationHost;
300
-
301
264
  /**
302
265
  * Compiles a schematic sheet into a circuit.
303
266
  *
@@ -335,8 +298,8 @@ export declare function createModuleValidation(module: IRModule, updateResources
335
298
  /**
336
299
  * Compiles a schematic and reports which circuit device each symbol became.
337
300
  *
338
- * {@link createCircuitFromSchematic} returns only the circuit; hosts that route
339
- * 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.
340
303
  */
341
304
  export declare function createSchematicCircuit(schematic: SchematicDocument, options: SchematicCircuitOptions): Promise<SchematicCircuitResult>;
342
305
 
@@ -354,90 +317,6 @@ export declare function createSchematicDeviceSimulationHost(options: SchematicDe
354
317
  /** Global pin addresses one device may own; enough for the largest packages. */
355
318
  export declare const DEVICE_PIN_STRIDE = 512;
356
319
 
357
- /**
358
- * What every device accessor offers, whatever its type.
359
- *
360
- * `readPin` returns the level the *device* drives onto one of its pins (its
361
- * output latch), while `Circuit.getPin('device.pin')` returns the level the pin
362
- * *observes* after the electrical solve. Host changes use device state; there
363
- * is no direct pin override API.
364
- */
365
- export declare interface DeviceAccessor {
366
- /** Circuit-unique device id. */
367
- readonly deviceId: string;
368
- /** Device type, e.g. `'push-pull-driver'`. */
369
- readonly type: string;
370
- /** Declared pin names, in declaration order. */
371
- readonly pinNames: readonly string[];
372
- /** Declared internal state names, in declaration order. */
373
- readonly stateNames: readonly string[];
374
- /** Reads the level the device drives onto `pinName`. */
375
- readPin(pinName: string): boolean;
376
- /** Writes the level the device drives onto `pinName`. */
377
- /** Reads one declared internal state. */
378
- getState(name: string): unknown;
379
- /** Writes one declared internal state. */
380
- setState(name: string, value: unknown): void;
381
- /** Consumes inputs at a host step boundary, when declared by the paired factory. */
382
- applyInputs?(inputs: readonly DeviceInput[]): void;
383
- /** Publishes outputs after the host finishes evaluating the step. */
384
- flushOutputs?(publish: (port: string, payload: unknown) => void): void;
385
- }
386
-
387
- /**
388
- * Builds the typed accessor of a device after its module is compiled.
389
- *
390
- * A description that supplies `accessor` replaces the generic accessor the
391
- * runtime would otherwise synthesize from `type`. The factory runs once per
392
- * circuit and closes over the layout its IR factory allocated.
393
- */
394
- export declare type DeviceAccessorFactory = (context: DeviceAccessorFactoryContext) => DeviceAccessorSurface;
395
-
396
- /** Host services handed to a device accessor factory at circuit creation. */
397
- export declare interface DeviceAccessorFactoryContext {
398
- readonly deviceId: string;
399
- /** Live engine storage (`IRStorage` in practice). */
400
- readonly storage: unknown;
401
- /** Throws once the owning circuit has been disposed. */
402
- readonly assertActive: () => void;
403
- /** Advances the owning circuit by one tick. */
404
- readonly step: () => void;
405
- /** Advances the owning circuit by `count` ticks. */
406
- readonly runBatch: (count: number) => void;
407
- }
408
-
409
- /** Every accessor of one circuit, keyed by device id. */
410
- export declare type DeviceAccessors = Readonly<Record<string, DeviceAccessor>>;
411
-
412
- /**
413
- * Accessor map derived from a literal compilation input.
414
- *
415
- * `Circuit.create` infers the device ids of a `const` input, so
416
- * `circuit.devices.<id>` is typed without a cast. A device package that wants a
417
- * richer surface builds it through `DeviceDescription.accessor` and casts at
418
- * the call site.
419
- */
420
- export declare type DeviceAccessorsOf<I extends CircuitCompilationInput> = {
421
- readonly [D in I['devices'][number] as D['id']]: DeviceAccessor;
422
- };
423
-
424
- /**
425
- * Structural surface every device accessor exposes.
426
- *
427
- * The compiler keeps this declaration independent from the runtime's
428
- * `DeviceAccessor` so a device kernel can ship its own typed accessor without
429
- * making the compiler depend on the runtime module.
430
- */
431
- declare interface DeviceAccessorSurface {
432
- readonly deviceId: string;
433
- readonly type: string;
434
- readonly pinNames: readonly string[];
435
- readonly stateNames: readonly string[];
436
- readPin(pinName: string): boolean;
437
- getState(name: string): unknown;
438
- setState(name: string, value: unknown): void;
439
- }
440
-
441
320
  /** Build surface handed to one device behavior. */
442
321
  export declare interface DeviceBehaviorApi {
443
322
  readonly deviceId: string;
@@ -508,11 +387,6 @@ export declare interface DeviceDescription extends DeviceBridgeHooks {
508
387
  * the behaviour hooks the compiler should call inside each phase.
509
388
  */
510
389
  readonly kernel?: DeviceKernelFactory;
511
- /**
512
- * Optional typed accessor factory. Kernels with a richer surface than the
513
- * generic state/pin accessor (a register file, a memory map) use it.
514
- */
515
- readonly accessor?: DeviceAccessorFactory;
516
390
  /**
517
391
  * Read-only byte regions the kernel wants host updates for. The compiler
518
392
  * grants `update` access on top of the normal `read` grant so kernels can
@@ -600,8 +474,6 @@ declare interface DeviceMetadata extends DeviceBridgeHooks {
600
474
  readonly initialize: string | undefined;
601
475
  readonly tick: string | undefined;
602
476
  readonly update: string | undefined;
603
- /** Typed accessor factory supplied by the device description, if any. */
604
- readonly accessor: DeviceAccessorFactory | undefined;
605
477
  }
606
478
 
607
479
  export declare interface DeviceOutputIo {
@@ -797,27 +669,6 @@ export declare interface DeviceTopic {
797
669
 
798
670
  export { Disposable }
799
671
 
800
- /**
801
- * Types of the unified circuit runtime (U02).
802
- *
803
- * `Circuit` is the host-facing facade over one compiled unified IR module: it
804
- * owns the engine instance, exposes typed per-device accessors and keeps the
805
- * tick counter. Everything here is transport-neutral; no type mentions the
806
- * device host or the schematic editor.
807
- */
808
- export declare type ElectricalCurrentTarget = {
809
- readonly kind: 'resistor' | 'contact';
810
- readonly device: string;
811
- readonly name: string;
812
- } | {
813
- readonly kind: 'drive' | 'pull-up' | 'pull-down';
814
- readonly device: string;
815
- readonly pin: string;
816
- } | {
817
- readonly kind: 'source';
818
- readonly name: string;
819
- };
820
-
821
672
  export declare class ElectricalDefinitionError extends Error {
822
673
  readonly code: ElectricalDefinitionErrorCode;
823
674
  readonly name = "ElectricalDefinitionError";
@@ -826,23 +677,6 @@ export declare class ElectricalDefinitionError extends Error {
826
677
 
827
678
  export declare type ElectricalDefinitionErrorCode = 'electrical/invalid-definition' | 'electrical/invalid-parameter' | 'electrical/unknown-resource' | 'electrical/duplicate-initialization' | 'electrical/missing-initialization' | 'electrical/factory-closed';
828
679
 
829
- /** Active diagnostics of the last published electrical snapshot, plus any latched solve fault. */
830
- export declare type ElectricalDiagnostic = {
831
- readonly code: 'electrical/overcurrent';
832
- readonly severity: 'warning';
833
- readonly target: ElectricalCurrentTarget;
834
- readonly current: number;
835
- readonly maxAbsCurrent: number;
836
- } | {
837
- readonly code: 'electrical/invalid-parameter' | 'electrical/source-conflict' | 'electrical/numerical-failure';
838
- readonly severity: 'error';
839
- /** Identifiable failed operation or resource; absent for a matrix-level failure. */
840
- readonly target?: ElectricalCurrentTarget;
841
- /** Resources associated with the affected network, not a list of proven culprits. */
842
- readonly resources: readonly ElectricalCurrentTarget[];
843
- readonly pins: readonly string[];
844
- };
845
-
846
680
  export declare interface ElectricalFactoryContext {
847
681
  readonly properties: Readonly<Record<string, PropertyValue>>;
848
682
  readonly electrical: DeviceElectricalInitialization;
@@ -853,15 +687,6 @@ export declare interface ElectricalPinDeclaration {
853
687
  readonly side?: 'left' | 'right' | 'top' | 'bottom';
854
688
  }
855
689
 
856
- declare type ElectricalReading = {
857
- readonly state: 'determinate';
858
- readonly value: number;
859
- } | {
860
- readonly state: 'floating';
861
- } | {
862
- readonly state: 'indeterminate';
863
- };
864
-
865
690
  export declare interface ElectricalReference {
866
691
  readonly source: string;
867
692
  }
@@ -971,16 +796,18 @@ declare interface SchematicCircuitResult {
971
796
  export declare interface SchematicDeviceBinding {
972
797
  /** Device type, e.g. `'counter-ir'`. */
973
798
  readonly type: string;
974
- /** IR description: pins, states and behaviours. */
799
+ /** IR description: pins, states, factory, behaviours and boundary hooks. */
975
800
  readonly description: DeviceDescription;
976
801
  /** Instance configuration reserved for device kernels. */
977
802
  readonly config?: Readonly<Record<string, unknown>>;
978
- /** 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
+ */
979
810
  readonly topics?: readonly SchematicDeviceTopic[];
980
- /** Bridges one input topic payload onto the compiled device accessor. */
981
- deliver?(accessor: DeviceAccessor, port: string, payload: unknown): void;
982
- /** Reads one output topic from the compiled device accessor. */
983
- read?(accessor: DeviceAccessor, port: string): unknown;
984
811
  }
985
812
 
986
813
  /** Reads the bytes of one schematic URI; the caller resolves relative references. */