@litz-sim/litz 0.1.0 → 0.2.1
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/LICENSE +176 -0
- package/README.md +168 -100
- package/index.d.ts +114 -2
- package/index.js +7 -652
- package/litz_wasm.wasm +0 -0
- package/package.json +4 -2
- package/src/bytes.js +73 -0
- package/src/constants.js +98 -0
- package/src/errors.js +9 -0
- package/src/host.js +423 -0
- package/src/jco.js +257 -0
- package/src/litz.js +261 -0
- package/src/session.js +432 -0
package/index.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export declare class Litz {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
export interface SessionOptions {
|
|
15
|
-
/** The diagram.json text. */
|
|
15
|
+
/** The diagram.json text. The board it names selects the chip. */
|
|
16
16
|
diagram: string;
|
|
17
17
|
/** The application image. */
|
|
18
18
|
app: Uint8Array | ArrayBuffer | ArrayBufferView;
|
|
@@ -22,12 +22,96 @@ export interface SessionOptions {
|
|
|
22
22
|
boot?: Uint8Array | ArrayBuffer | ArrayBufferView | null;
|
|
23
23
|
/** Partition table; requires `boot`. */
|
|
24
24
|
ptable?: Uint8Array | ArrayBuffer | ArrayBufferView | null;
|
|
25
|
+
/** Flash size in bytes; the board manifest's, then 4 MiB. */
|
|
26
|
+
flashSize?: number | null;
|
|
27
|
+
/** The MAC the station reports, e.g. `24:0a:c4:12:34:56`. */
|
|
28
|
+
mac?: string | null;
|
|
29
|
+
/** Solve the analog network instead of treating every net as digital. */
|
|
30
|
+
analog?: boolean;
|
|
31
|
+
/** Catalog manifests as TOML text: custom parts and boards. */
|
|
32
|
+
manifests?: string[];
|
|
33
|
+
/** Wasm part modules: `{ "x.wasm": () => instance }`. */
|
|
34
|
+
parts?: Record<string, PartFactory>;
|
|
35
|
+
/** Wasm chip modules: `{ "x.wasm": (host) => instance }`. */
|
|
36
|
+
boards?: Record<string, ChipFactory>;
|
|
25
37
|
}
|
|
26
38
|
|
|
39
|
+
/** One instantiated `litz:part` component, as the simulator drives it. */
|
|
40
|
+
export interface HostPart {
|
|
41
|
+
setPin(pin: number, level: Level, atNs: number): void;
|
|
42
|
+
advance(toNs: number): void;
|
|
43
|
+
nextEvent(): number | null | undefined;
|
|
44
|
+
drive(pin: number): { level: Level; strength?: "strong" | "weak" } | Level | null;
|
|
45
|
+
state(): { kind: string; value?: number } | null;
|
|
46
|
+
press?(pressed: boolean): boolean;
|
|
47
|
+
setControl?(permille: number): boolean;
|
|
48
|
+
analog?(pin: number): number | null;
|
|
49
|
+
i2cConnect?(address: number, read: boolean): void;
|
|
50
|
+
i2cRead?(): number;
|
|
51
|
+
i2cWrite?(byte: number): boolean;
|
|
52
|
+
i2cStop?(): void;
|
|
53
|
+
spiSelect?(selected: boolean): void;
|
|
54
|
+
spiTransfer?(tx: Uint8Array, rxLen: number): Uint8Array;
|
|
55
|
+
uartReceive?(bytes: Uint8Array, atNs: number): void;
|
|
56
|
+
uartPoll?(): Uint8Array;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** One instantiated `litz:mcu` component. */
|
|
60
|
+
export interface HostChip {
|
|
61
|
+
info(): {
|
|
62
|
+
name: string;
|
|
63
|
+
description?: string;
|
|
64
|
+
cpuHz: number;
|
|
65
|
+
flash: { size: number; bootloader: number; partitionTable: number; app: number };
|
|
66
|
+
};
|
|
67
|
+
loadImage(kind: "rom" | "bootloader" | "partition-table" | "firmware", offset: number, bytes: Uint8Array): void;
|
|
68
|
+
boot(): number;
|
|
69
|
+
setInputs(inputs: Array<{ pin: number; level: Level }>): void;
|
|
70
|
+
run(budget: number): { cycles: number; stop: StopWord };
|
|
71
|
+
takeOutputs(): Array<{ pin: number; level: Level; enabled: boolean }>;
|
|
72
|
+
writeConsole(bytes: Uint8Array): void;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The guest's host imports. */
|
|
76
|
+
export interface ChipHost {
|
|
77
|
+
console(bytes: string | Uint8Array): void;
|
|
78
|
+
fault(detail: string): void;
|
|
79
|
+
/**
|
|
80
|
+
* Answer one `litz:mcu@0.2.0` host call against the circuit. `op` is the
|
|
81
|
+
* contract's camelCase name and `args` its JSON argument object; the result
|
|
82
|
+
* is the call's plain JSON value. A call the circuit refuses throws
|
|
83
|
+
* `LitzError`.
|
|
84
|
+
*/
|
|
85
|
+
call(op: string, args: Record<string, unknown>): unknown;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export type PartFactory = () => HostPart;
|
|
89
|
+
export type ChipFactory = (host: ChipHost) => HostChip;
|
|
90
|
+
|
|
91
|
+
export interface ChipFlash {
|
|
92
|
+
readonly size: number;
|
|
93
|
+
readonly bootloader: number;
|
|
94
|
+
readonly partitionTable: number;
|
|
95
|
+
readonly app: number;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export interface ChipInfo {
|
|
99
|
+
/** `esp32c3`, `esp32s3`, `atmega328p`, `rp2040`, ... */
|
|
100
|
+
readonly name: string;
|
|
101
|
+
readonly cpuHz: number;
|
|
102
|
+
readonly flash: ChipFlash;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** How the last `run` ended. */
|
|
106
|
+
export type StopWord = "budget" | "idle" | "halted" | "trap" | "reset" | "breakpoint";
|
|
107
|
+
|
|
108
|
+
/** A pin level as the board sees it. */
|
|
109
|
+
export type Level = "low" | "high" | "high-z";
|
|
110
|
+
|
|
27
111
|
export interface LoadOptions {
|
|
28
112
|
/** Bytes, a URL, a `Response`, or an already compiled module. */
|
|
29
113
|
wasm?: Uint8Array | ArrayBuffer | ArrayBufferView | string | URL | Response | WebAssembly.Module | null;
|
|
30
|
-
/**
|
|
114
|
+
/** Extra imports, merged alongside this package's `litz_host` bridge. */
|
|
31
115
|
imports?: WebAssembly.Imports;
|
|
32
116
|
}
|
|
33
117
|
|
|
@@ -42,6 +126,18 @@ export declare function loadLitz(options?: LoadOptions): Promise<Litz>;
|
|
|
42
126
|
|
|
43
127
|
export declare class LitzError extends Error {}
|
|
44
128
|
|
|
129
|
+
/** Wrap a jco-transpiled `litz:part` root object. */
|
|
130
|
+
export declare function partFromJco(root: unknown): HostPart;
|
|
131
|
+
/** Wrap a jco-transpiled `litz:mcu` root object. */
|
|
132
|
+
export declare function chipFromJco(root: unknown): HostChip;
|
|
133
|
+
/** The imports a transpiled `litz:part@0.2.0` module expects. */
|
|
134
|
+
export declare function partImports(options?: {
|
|
135
|
+
log?: (message: string) => void;
|
|
136
|
+
rom?: () => Uint8Array;
|
|
137
|
+
}): Record<string, unknown>;
|
|
138
|
+
/** The imports a transpiled `litz:mcu` (0.1 or 0.2) module expects. */
|
|
139
|
+
export declare function chipImports(host: ChipHost): Record<string, unknown>;
|
|
140
|
+
|
|
45
141
|
export type PartStateName =
|
|
46
142
|
| "none"
|
|
47
143
|
| "off"
|
|
@@ -60,6 +156,8 @@ export interface PartState {
|
|
|
60
156
|
id: string;
|
|
61
157
|
/** The ABI's state code: 0 none, 1 off, 2 on, 3 up, 4 down. */
|
|
62
158
|
code: number;
|
|
159
|
+
/** What the part is: `led`, `button`, `switch`, `servo`, `radio`, ... */
|
|
160
|
+
kind: string;
|
|
63
161
|
/** `code` as a word; `null` for a part that reports nothing (a board). */
|
|
64
162
|
state: PartStateName | null;
|
|
65
163
|
/** The number behind the state: servo degrees, a segment mask, a tone rate. */
|
|
@@ -155,6 +253,14 @@ export declare class Session {
|
|
|
155
253
|
advance(ms: number, options?: AdvanceOptions): number;
|
|
156
254
|
/** Simulated milliseconds since reset. */
|
|
157
255
|
readonly ms: number;
|
|
256
|
+
/** The backend the diagram booted, its clock and its flash layout. */
|
|
257
|
+
chip(): ChipInfo;
|
|
258
|
+
/** How the last `run` ended. */
|
|
259
|
+
readonly stop: StopWord;
|
|
260
|
+
/** The backend's explanation for a trap, or `null`. */
|
|
261
|
+
readonly fault: string | null;
|
|
262
|
+
/** The firmware's own free heap in bytes, or `null` when it cannot be asked. */
|
|
263
|
+
heap(): number | null;
|
|
158
264
|
readonly serial: Serial;
|
|
159
265
|
readonly trace: Trace;
|
|
160
266
|
readonly probe: Probes;
|
|
@@ -169,6 +275,12 @@ export declare class Session {
|
|
|
169
275
|
press(id: string, pressed?: boolean): void;
|
|
170
276
|
/** Move a control: 0 to 1000 permille of its travel. */
|
|
171
277
|
setControl(id: string, permille: number): void;
|
|
278
|
+
/** Set a named control such as a sensor's `temperature` or `lux`. */
|
|
279
|
+
setNamedControl(id: string, name: string, value: string | number): void;
|
|
280
|
+
/** The level on one of a part's pins, or `null` when it does not exist. */
|
|
281
|
+
partPin(id: string, pin?: string): Level | null;
|
|
282
|
+
/** The level the chip sees on its own pad, by GPIO number. */
|
|
283
|
+
gpio(pin: number): Level | null;
|
|
172
284
|
/** The panel, or `null` when the circuit has no display. */
|
|
173
285
|
display(): Display | null;
|
|
174
286
|
/** Deliver a touch at panel pixels. False without a panel. */
|