@litz-sim/litz 0.1.0 → 0.2.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/README.md +167 -99
- package/index.d.ts +114 -2
- package/index.js +7 -652
- package/litz_wasm.wasm +0 -0
- package/package.json +3 -1
- 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/src/session.js
ADDED
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
// One running board: the public surface over the ABI.
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
DEFAULT_STEPS,
|
|
5
|
+
DECODER,
|
|
6
|
+
ENCODER,
|
|
7
|
+
LEVELS,
|
|
8
|
+
STATE_CODES,
|
|
9
|
+
STOPS,
|
|
10
|
+
} from "./constants.js";
|
|
11
|
+
import { bytesOf, decodeChunks, hold } from "./bytes.js";
|
|
12
|
+
import { LitzError } from "./errors.js";
|
|
13
|
+
|
|
14
|
+
/** One running board. */
|
|
15
|
+
export class Session {
|
|
16
|
+
#io;
|
|
17
|
+
#handle;
|
|
18
|
+
#closed = false;
|
|
19
|
+
#serial;
|
|
20
|
+
#trace;
|
|
21
|
+
#probe;
|
|
22
|
+
#decode;
|
|
23
|
+
|
|
24
|
+
/** @internal Sessions come from `Litz.createSession`. */
|
|
25
|
+
constructor(io, handle) {
|
|
26
|
+
this.#io = io;
|
|
27
|
+
this.#handle = handle;
|
|
28
|
+
|
|
29
|
+
const session = this;
|
|
30
|
+
this.#serial = {
|
|
31
|
+
take: () => session.#takeSerial(),
|
|
32
|
+
write: (data) => session.#writeSerial(data),
|
|
33
|
+
};
|
|
34
|
+
this.#trace = Object.freeze({
|
|
35
|
+
start: () => {
|
|
36
|
+
session.#assertOpen();
|
|
37
|
+
session.#x.litz_trace_start(session.#handle);
|
|
38
|
+
},
|
|
39
|
+
get transitions() {
|
|
40
|
+
session.#assertOpen();
|
|
41
|
+
return session.#x.litz_trace_len(session.#handle);
|
|
42
|
+
},
|
|
43
|
+
vcd: () =>
|
|
44
|
+
session.#readText(
|
|
45
|
+
(ptr, cap) => session.#x.litz_trace_vcd(session.#handle, ptr, cap),
|
|
46
|
+
64 * 1024,
|
|
47
|
+
),
|
|
48
|
+
});
|
|
49
|
+
this.#probe = Object.freeze({
|
|
50
|
+
add: (name, point) => session.#addProbe(name, point),
|
|
51
|
+
});
|
|
52
|
+
this.#decode = Object.freeze({
|
|
53
|
+
add: (spec) => session.#addDecode(spec),
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The session handle inside the module; 0 is never a session. */
|
|
58
|
+
get handle() {
|
|
59
|
+
return this.#handle;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
get #x() {
|
|
63
|
+
return this.#io.exports;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
#assertOpen() {
|
|
67
|
+
if (this.#closed) throw new LitzError("this session has been closed");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Advance by at most `steps` scheduling steps; returns the simulated ms. */
|
|
71
|
+
run(steps = DEFAULT_STEPS) {
|
|
72
|
+
this.#assertOpen();
|
|
73
|
+
return this.#x.litz_run(this.#handle, steps >>> 0);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Advance until `ms` of simulated time have passed. A scheduling step is not
|
|
78
|
+
* a fixed unit of time, so this loops in chunks until the clock moves.
|
|
79
|
+
*/
|
|
80
|
+
advance(ms, { chunkSteps = 20_000, maxSteps = 200_000_000 } = {}) {
|
|
81
|
+
this.#assertOpen();
|
|
82
|
+
const target = this.ms + Math.max(0, ms | 0);
|
|
83
|
+
let spent = 0;
|
|
84
|
+
while (this.ms < target && spent < maxSteps) {
|
|
85
|
+
this.run(chunkSteps);
|
|
86
|
+
spent += chunkSteps;
|
|
87
|
+
}
|
|
88
|
+
if (this.ms < target) {
|
|
89
|
+
throw new LitzError(`the session did not reach ${target} ms within ${maxSteps} steps`);
|
|
90
|
+
}
|
|
91
|
+
return this.ms;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Simulated milliseconds since reset. */
|
|
95
|
+
get ms() {
|
|
96
|
+
this.#assertOpen();
|
|
97
|
+
return this.#x.litz_ms(this.#handle);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The chip the diagram booted: `{ name, cpuHz, flash: { size, bootloader,
|
|
102
|
+
* partitionTable, app } }`.
|
|
103
|
+
*/
|
|
104
|
+
chip() {
|
|
105
|
+
this.#assertOpen();
|
|
106
|
+
const text = this.#readText(
|
|
107
|
+
(ptr, cap) => this.#x.litz_chip_info(this.#handle, ptr, cap),
|
|
108
|
+
128,
|
|
109
|
+
);
|
|
110
|
+
try {
|
|
111
|
+
return JSON.parse(text);
|
|
112
|
+
} catch (error) {
|
|
113
|
+
throw new LitzError("the chip description was not valid JSON", { cause: error });
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** How the last `run` ended: budget, idle, halted, trap, reset or breakpoint. */
|
|
118
|
+
get stop() {
|
|
119
|
+
this.#assertOpen();
|
|
120
|
+
return STOPS[this.#x.litz_stop(this.#handle)] ?? "unknown";
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** The backend's explanation for a trap, or `null` when there is none. */
|
|
124
|
+
get fault() {
|
|
125
|
+
this.#assertOpen();
|
|
126
|
+
return (
|
|
127
|
+
this.#readText((ptr, cap) => this.#x.litz_fault(this.#handle, ptr, cap), 64) || null
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The firmware's own free heap in bytes, or `null` when it cannot be asked. */
|
|
132
|
+
heap() {
|
|
133
|
+
this.#assertOpen();
|
|
134
|
+
const bytes = this.#x.litz_heap(this.#handle);
|
|
135
|
+
return bytes < 0 ? null : bytes;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** The console. `take()` drains what the firmware printed. */
|
|
139
|
+
get serial() {
|
|
140
|
+
return this.#serial;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Wire recording for VCD export. */
|
|
144
|
+
get trace() {
|
|
145
|
+
return this.#trace;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Probe points the scope document reports. */
|
|
149
|
+
get probe() {
|
|
150
|
+
return this.#probe;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Protocol decoders over the probes. */
|
|
154
|
+
get decode() {
|
|
155
|
+
return this.#decode;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Electrical conflicts: the fault text of each, oldest first. */
|
|
159
|
+
conflicts() {
|
|
160
|
+
this.#assertOpen();
|
|
161
|
+
const count = this.#x.litz_conflict_count(this.#handle);
|
|
162
|
+
const out = [];
|
|
163
|
+
for (let index = 0; index < count; index += 1) {
|
|
164
|
+
out.push(this.#readText((ptr, cap) => this.#x.litz_conflict(this.#handle, index, ptr, cap)));
|
|
165
|
+
}
|
|
166
|
+
return out;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Every part the diagram declares, with its current state. */
|
|
170
|
+
parts() {
|
|
171
|
+
this.#assertOpen();
|
|
172
|
+
const count = this.#x.litz_part_count(this.#handle);
|
|
173
|
+
const out = [];
|
|
174
|
+
for (let index = 0; index < count; index += 1) {
|
|
175
|
+
const id = this.#readText((ptr, cap) => this.#x.litz_part_id(this.#handle, index, ptr, cap));
|
|
176
|
+
// A board instance reports no runtime state of its own; it still belongs
|
|
177
|
+
// in the list, with `state: null`.
|
|
178
|
+
out.push(this.#state(id) ?? { id, code: -1, kind: "unknown", state: null, value: 0 });
|
|
179
|
+
}
|
|
180
|
+
return out;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** One part by id, including board-expanded ids such as `esp/led_pwr`. */
|
|
184
|
+
part(id) {
|
|
185
|
+
this.#assertOpen();
|
|
186
|
+
if (typeof id !== "string" || id.length === 0) {
|
|
187
|
+
throw new LitzError("part() needs a part id");
|
|
188
|
+
}
|
|
189
|
+
return this.#state(id);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* The level on one of a part's pins: `low`, `high` or `high-z`, or `null`
|
|
194
|
+
* when the part or pin is not in the diagram. Omit `pin` for the part's
|
|
195
|
+
* first pin.
|
|
196
|
+
*/
|
|
197
|
+
partPin(id, pin = "") {
|
|
198
|
+
this.#assertOpen();
|
|
199
|
+
const idBuf = hold(this.#io, ENCODER.encode(String(id)));
|
|
200
|
+
const pinBuf = hold(this.#io, ENCODER.encode(String(pin)));
|
|
201
|
+
let code;
|
|
202
|
+
try {
|
|
203
|
+
code = this.#x.litz_part_pin_level(
|
|
204
|
+
this.#handle,
|
|
205
|
+
idBuf.ptr,
|
|
206
|
+
idBuf.len,
|
|
207
|
+
pinBuf.ptr,
|
|
208
|
+
pinBuf.len,
|
|
209
|
+
);
|
|
210
|
+
} finally {
|
|
211
|
+
this.#io.release(idBuf);
|
|
212
|
+
this.#io.release(pinBuf);
|
|
213
|
+
}
|
|
214
|
+
return code < 0 ? null : LEVELS[code] ?? null;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** The level the chip sees on one of its own pads, by GPIO number. */
|
|
218
|
+
gpio(pin) {
|
|
219
|
+
this.#assertOpen();
|
|
220
|
+
const code = this.#x.litz_gpio_level(this.#handle, pin >>> 0);
|
|
221
|
+
return code < 0 ? null : LEVELS[code] ?? null;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Set a named control on a part, such as a sensor's `temperature` or `lux`.
|
|
226
|
+
* Throws when the part has no such control.
|
|
227
|
+
*/
|
|
228
|
+
setNamedControl(id, name, value) {
|
|
229
|
+
this.#assertOpen();
|
|
230
|
+
const idBuf = hold(this.#io, ENCODER.encode(String(id)));
|
|
231
|
+
const nameBuf = hold(this.#io, ENCODER.encode(String(name)));
|
|
232
|
+
const valueBuf = hold(this.#io, ENCODER.encode(String(value)));
|
|
233
|
+
let code;
|
|
234
|
+
try {
|
|
235
|
+
code = this.#x.litz_set_named_control(
|
|
236
|
+
this.#handle,
|
|
237
|
+
idBuf.ptr,
|
|
238
|
+
idBuf.len,
|
|
239
|
+
nameBuf.ptr,
|
|
240
|
+
nameBuf.len,
|
|
241
|
+
valueBuf.ptr,
|
|
242
|
+
valueBuf.len,
|
|
243
|
+
);
|
|
244
|
+
} finally {
|
|
245
|
+
this.#io.release(idBuf);
|
|
246
|
+
this.#io.release(nameBuf);
|
|
247
|
+
this.#io.release(valueBuf);
|
|
248
|
+
}
|
|
249
|
+
if (code !== 1) throw this.#io.error(`part ${id} has no control ${name}`);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** Press or release a pushbutton wired to a GPIO. */
|
|
253
|
+
press(id, pressed = true) {
|
|
254
|
+
this.#assertOpen();
|
|
255
|
+
const buf = this.#io.writeIn(ENCODER.encode(String(id)));
|
|
256
|
+
try {
|
|
257
|
+
this.#x.litz_press(this.#handle, buf.ptr, buf.len, pressed ? 1 : 0);
|
|
258
|
+
} finally {
|
|
259
|
+
this.#io.release(buf);
|
|
260
|
+
}
|
|
261
|
+
this.#throwStored();
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** Move a continuous control: 0 to 1000 permille of its travel. */
|
|
265
|
+
setControl(id, permille) {
|
|
266
|
+
this.#assertOpen();
|
|
267
|
+
const value = Math.round(Number(permille));
|
|
268
|
+
if (!Number.isFinite(value)) throw new LitzError("setControl needs a number");
|
|
269
|
+
const clamped = Math.min(1000, Math.max(0, value));
|
|
270
|
+
const buf = this.#io.writeIn(ENCODER.encode(String(id)));
|
|
271
|
+
try {
|
|
272
|
+
this.#x.litz_set_control(this.#handle, buf.ptr, buf.len, clamped);
|
|
273
|
+
} finally {
|
|
274
|
+
this.#io.release(buf);
|
|
275
|
+
}
|
|
276
|
+
this.#throwStored();
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** The panel size and pixels, or `null` when the circuit has no display. */
|
|
280
|
+
display() {
|
|
281
|
+
this.#assertOpen();
|
|
282
|
+
const info = this.#x.litz_display_info(this.#handle);
|
|
283
|
+
if (info === 0) return null;
|
|
284
|
+
const width = info >>> 16;
|
|
285
|
+
const height = info & 0xffff;
|
|
286
|
+
const session = this;
|
|
287
|
+
return {
|
|
288
|
+
width,
|
|
289
|
+
height,
|
|
290
|
+
get version() {
|
|
291
|
+
session.#assertOpen();
|
|
292
|
+
return session.#x.litz_display_version(session.#handle);
|
|
293
|
+
},
|
|
294
|
+
pixels() {
|
|
295
|
+
session.#assertOpen();
|
|
296
|
+
const needed = width * height * 4;
|
|
297
|
+
const cap = session.#io.writeOut(needed);
|
|
298
|
+
const written = session.#x.litz_display_rgba(session.#handle, cap.ptr, cap.len);
|
|
299
|
+
if (written !== needed) {
|
|
300
|
+
throw session.#io.error("the display frame could not be read");
|
|
301
|
+
}
|
|
302
|
+
return session.#io.copyOut(written);
|
|
303
|
+
},
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** Deliver a touch at panel pixels. False when the board has no panel. */
|
|
308
|
+
touch(x, y, down = true) {
|
|
309
|
+
this.#assertOpen();
|
|
310
|
+
return this.#x.litz_touch(this.#handle, x >>> 0, y >>> 0, down ? 1 : 0) === 1;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** The scope document: every probe's samples and every decoder's frames. */
|
|
314
|
+
scope() {
|
|
315
|
+
this.#assertOpen();
|
|
316
|
+
const length = this.#x.litz_scope_len(this.#handle);
|
|
317
|
+
const text = this.#readText(
|
|
318
|
+
(ptr, cap) => this.#x.litz_scope_json(this.#handle, ptr, cap),
|
|
319
|
+
length,
|
|
320
|
+
);
|
|
321
|
+
try {
|
|
322
|
+
return JSON.parse(text);
|
|
323
|
+
} catch (error) {
|
|
324
|
+
throw new LitzError("the scope document was not valid JSON", { cause: error });
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** Close the session and release its memory in the module. */
|
|
329
|
+
close() {
|
|
330
|
+
if (this.#closed) return;
|
|
331
|
+
this.#closed = true;
|
|
332
|
+
this.#x.litz_drop(this.#handle);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** `using session = ...` support where explicit resource management exists. */
|
|
336
|
+
[Symbol.dispose]() {
|
|
337
|
+
this.close();
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
#state(id) {
|
|
341
|
+
const buf = this.#io.writeIn(ENCODER.encode(id));
|
|
342
|
+
let code;
|
|
343
|
+
let value;
|
|
344
|
+
try {
|
|
345
|
+
code = this.#x.litz_part_state(this.#handle, buf.ptr, buf.len);
|
|
346
|
+
value = this.#x.litz_part_value(this.#handle, buf.ptr, buf.len);
|
|
347
|
+
} finally {
|
|
348
|
+
this.#io.release(buf);
|
|
349
|
+
}
|
|
350
|
+
if (code < 0) return null;
|
|
351
|
+
const entry = STATE_CODES[code] ?? { kind: "unknown", state: "unknown" };
|
|
352
|
+
return { id, code, kind: entry.kind, state: entry.state, value };
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
#takeSerial() {
|
|
356
|
+
this.#assertOpen();
|
|
357
|
+
const chunks = [];
|
|
358
|
+
for (;;) {
|
|
359
|
+
const cap = this.#io.writeOut(64 * 1024);
|
|
360
|
+
const written = this.#x.litz_serial_take(this.#handle, cap.ptr, cap.len);
|
|
361
|
+
if (written === 0) break;
|
|
362
|
+
chunks.push(this.#io.copyOut(written));
|
|
363
|
+
if (written < cap.len) break;
|
|
364
|
+
}
|
|
365
|
+
return decodeChunks(chunks);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
#writeSerial(data) {
|
|
369
|
+
this.#assertOpen();
|
|
370
|
+
const bytes = typeof data === "string" ? ENCODER.encode(data) : bytesOf(data, "serial data");
|
|
371
|
+
if (bytes.length === 0) return;
|
|
372
|
+
const buf = this.#io.writeIn(bytes);
|
|
373
|
+
try {
|
|
374
|
+
this.#x.litz_serial_write(this.#handle, buf.ptr, buf.len);
|
|
375
|
+
} finally {
|
|
376
|
+
this.#io.release(buf);
|
|
377
|
+
}
|
|
378
|
+
this.#throwStored();
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
#addProbe(name, point) {
|
|
382
|
+
this.#assertOpen();
|
|
383
|
+
// Two strings in one call cannot share the reusable input buffer.
|
|
384
|
+
const nameBuf = hold(this.#io, ENCODER.encode(String(name)));
|
|
385
|
+
const pointBuf = hold(this.#io, ENCODER.encode(String(point)));
|
|
386
|
+
let ok;
|
|
387
|
+
try {
|
|
388
|
+
ok = this.#x.litz_probe_add(
|
|
389
|
+
this.#handle,
|
|
390
|
+
nameBuf.ptr,
|
|
391
|
+
nameBuf.len,
|
|
392
|
+
pointBuf.ptr,
|
|
393
|
+
pointBuf.len,
|
|
394
|
+
);
|
|
395
|
+
} finally {
|
|
396
|
+
this.#io.release(nameBuf);
|
|
397
|
+
this.#io.release(pointBuf);
|
|
398
|
+
}
|
|
399
|
+
if (ok !== 1) throw this.#io.error("the probe point could not be added");
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
#addDecode(spec) {
|
|
403
|
+
this.#assertOpen();
|
|
404
|
+
const buf = this.#io.writeIn(ENCODER.encode(String(spec)));
|
|
405
|
+
let ok;
|
|
406
|
+
try {
|
|
407
|
+
ok = this.#x.litz_decode_add(this.#handle, buf.ptr, buf.len);
|
|
408
|
+
} finally {
|
|
409
|
+
this.#io.release(buf);
|
|
410
|
+
}
|
|
411
|
+
if (ok !== 1) throw this.#io.error("the decoder could not be added");
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
#throwStored() {
|
|
415
|
+
const message = this.#io.stored();
|
|
416
|
+
if (message) throw new LitzError(message);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
#readText(fill, initial = 256) {
|
|
420
|
+
let capacity = Math.max(64, initial + 1);
|
|
421
|
+
for (;;) {
|
|
422
|
+
const cap = this.#io.writeOut(capacity);
|
|
423
|
+
const written = fill(cap.ptr, cap.len);
|
|
424
|
+
if (written < capacity) return DECODER.decode(this.#io.copyOut(written));
|
|
425
|
+
capacity *= 2;
|
|
426
|
+
if (capacity > 64 * 1024 * 1024) {
|
|
427
|
+
throw new LitzError("a value from the module did not fit in 64 MiB");
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|