@litz-sim/litz 0.2.1 → 0.3.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # @litz-sim/litz
2
2
 
3
- `@litz-sim/litz` is the JavaScript binding for Litz, a deterministic electronics and firmware simulator. The package loads `litz_wasm.wasm`, opens a session on one board from a `diagram.json` and an application image, and exposes serial output, part state, pins, probes, decoders, displays and electrical conflicts as plain objects. It has no DOM code, no workers and no runtime dependencies, and it advances only when the host calls `run` or `advance`.
3
+ `@litz-sim/litz` is the JavaScript binding for Litz, a deterministic electronics and firmware simulator. The package loads `litz_wasm.wasm`, opens a session over one or more development boards from a `diagram.json` and their application images, and exposes serial output, part state, pins, probes, decoders, displays, electrical conflicts and a RISC-V debugger as plain objects. It has no DOM code, no workers and no runtime dependencies, and it advances only when the host calls `run` or `advance`.
4
4
 
5
5
  ## Install
6
6
 
@@ -12,7 +12,7 @@ The package is an ES module for Node 18 or newer and for browsers. It ships `ind
12
12
 
13
13
  ## First session
14
14
 
15
- `loadLitz` reads, compiles and instantiates the module and returns a `Litz`. `createSession` opens one board, selected by the board named in the diagram. `advance` moves simulated time, `serial.take` drains the console and `close` releases the session. The sample below reads the repository's `blinky` example. In a checkout, `npm run example` runs it from `js/examples/blinky.mjs`, where Node resolves the package name through the enclosing `js/package.json`. In another project, install the package and pass that project's diagram and image.
15
+ `loadLitz` reads, compiles and instantiates the module and returns a `Litz`. `createSession` boots every development board the diagram names, each from its own image (`boardImages`) or from one `app`. `advance` moves simulated time, `serial.take` drains the console and `close` releases the session. The sample below reads the repository's `blinky` example. In a checkout, `npm run example` runs it from `js/examples/blinky.mjs`, where Node resolves the package name through the enclosing `js/package.json`. In another project, install the package and pass that project's diagram and image.
16
16
 
17
17
  ```js
18
18
  import { readFile } from "node:fs/promises";
package/index.d.ts CHANGED
@@ -12,10 +12,12 @@ export declare class Litz {
12
12
  }
13
13
 
14
14
  export interface SessionOptions {
15
- /** The diagram.json text. The board it names selects the chip. */
15
+ /** The diagram.json text. The boards it names select the chips. */
16
16
  diagram: string;
17
- /** The application image. */
18
- app: Uint8Array | ArrayBuffer | ArrayBufferView;
17
+ /** The application image for a single-board diagram. */
18
+ app?: Uint8Array | ArrayBuffer | ArrayBufferView;
19
+ /** One image per development board; replaces `app` in a multi-board diagram. */
20
+ boardImages?: BoardImage[];
19
21
  /** Mask ROM, when the firmware needs one. */
20
22
  rom?: Uint8Array | ArrayBuffer | ArrayBufferView | null;
21
23
  /** Second-stage bootloader; requires `ptable`. */
@@ -36,6 +38,18 @@ export interface SessionOptions {
36
38
  boards?: Record<string, ChipFactory>;
37
39
  }
38
40
 
41
+ /** One board's images in a multi-board session. */
42
+ export interface BoardImage {
43
+ /** The `diagram.json` instance id; omitted entries match boards by position. */
44
+ id?: string;
45
+ app: Uint8Array | ArrayBuffer | ArrayBufferView;
46
+ rom?: Uint8Array | ArrayBuffer | ArrayBufferView | null;
47
+ boot?: Uint8Array | ArrayBuffer | ArrayBufferView | null;
48
+ ptable?: Uint8Array | ArrayBuffer | ArrayBufferView | null;
49
+ flashSize?: number | null;
50
+ mac?: string | null;
51
+ }
52
+
39
53
  /** One instantiated `litz:part` component, as the simulator drives it. */
40
54
  export interface HostPart {
41
55
  setPin(pin: number, level: Level, atNs: number): void;
@@ -174,10 +188,35 @@ export interface Display {
174
188
  }
175
189
 
176
190
  export interface Serial {
177
- /** Drain what the firmware printed since the last call. */
191
+ /** Drain what the boards printed since the last call, merged in board order. */
178
192
  take(): string;
179
- /** Deliver bytes to the board's UART0 receiver. */
193
+ /** Drain one board's console, by list index or board id. */
194
+ takeBoard(board: number | string): string;
195
+ /** Deliver bytes to the first board's UART0 receiver. */
180
196
  write(data: string | Uint8Array | ArrayBuffer | ArrayBufferView): void;
197
+ /** Deliver bytes to one board's UART0 receiver. */
198
+ writeBoard(
199
+ board: number | string,
200
+ data: string | Uint8Array | ArrayBuffer | ArrayBufferView,
201
+ ): void;
202
+ }
203
+
204
+ /** One development board the session booted. */
205
+ export interface BoardInfo extends ChipInfo {
206
+ readonly index: number;
207
+ readonly id: string;
208
+ }
209
+
210
+ /** The browser-facing debugger over one RISC-V board. */
211
+ export interface Debug {
212
+ available(board?: number | string): boolean;
213
+ pc(board?: number | string): number | null;
214
+ registers(board?: number | string): { values: number[]; pc: number } | null;
215
+ memory(board: number | string, addr: number, length: number): Uint8Array;
216
+ disassemble(board: number | string, addr: number, count?: number): string;
217
+ setBreakpoints(board: number | string, pcs: ArrayLike<number>): void;
218
+ step(board?: number | string): StopWord | "unsupported";
219
+ run(board?: number | string, steps?: number): StopWord | "unsupported";
181
220
  }
182
221
 
183
222
  export interface Trace {
@@ -255,6 +294,10 @@ export declare class Session {
255
294
  readonly ms: number;
256
295
  /** The backend the diagram booted, its clock and its flash layout. */
257
296
  chip(): ChipInfo;
297
+ /** Every development board the session booted, in diagram order. */
298
+ boards(): BoardInfo[];
299
+ /** One board by list index or `diagram.json` id. */
300
+ board(board?: number | string): BoardInfo;
258
301
  /** How the last `run` ended. */
259
302
  readonly stop: StopWord;
260
303
  /** The backend's explanation for a trap, or `null`. */
@@ -281,12 +324,18 @@ export declare class Session {
281
324
  partPin(id: string, pin?: string): Level | null;
282
325
  /** The level the chip sees on its own pad, by GPIO number. */
283
326
  gpio(pin: number): Level | null;
327
+ /** The level one board sees on one of its own pads. */
328
+ gpioOn(board: number | string, pin: number): Level | null;
284
329
  /** The panel, or `null` when the circuit has no display. */
285
330
  display(): Display | null;
331
+ /** One board's panel, by list index or board id. */
332
+ displayOn(board?: number | string): Display | null;
286
333
  /** Deliver a touch at panel pixels. False without a panel. */
287
334
  touch(x: number, y: number, down?: boolean): boolean;
288
335
  /** Probes' samples and decoders' frames. */
289
336
  scope(): ScopeDocument;
337
+ /** Registers, memory, breakpoints and single-step. */
338
+ readonly debug: Debug;
290
339
  /** Drop the session. */
291
340
  close(): void;
292
341
  }
package/litz_wasm.wasm CHANGED
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@litz-sim/litz",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Deterministic electronics and firmware simulator in WebAssembly",
5
5
  "keywords": [
6
6
  "simulator",
package/src/constants.js CHANGED
@@ -43,6 +43,23 @@ export const REQUIRED_EXPORTS = [
43
43
  "litz_guest_console",
44
44
  "litz_guest_fault",
45
45
  "litz_guest_call",
46
+ "litz_board_count",
47
+ "litz_board_id",
48
+ "litz_board_chip_info",
49
+ "litz_board_gpio_level",
50
+ "litz_serial_take_board",
51
+ "litz_serial_write_board",
52
+ "litz_board_display_info",
53
+ "litz_board_display_version",
54
+ "litz_board_display_rgba",
55
+ "litz_debug_available",
56
+ "litz_debug_pc",
57
+ "litz_debug_registers",
58
+ "litz_debug_memory",
59
+ "litz_debug_disassemble",
60
+ "litz_debug_breakpoints",
61
+ "litz_debug_step",
62
+ "litz_debug_run",
46
63
  ];
47
64
 
48
65
  // State codes from `litz_part_state`; `-1` means the diagram has no such id.
package/src/litz.js CHANGED
@@ -166,15 +166,27 @@ export class Litz {
166
166
  manifests = [],
167
167
  parts = {},
168
168
  boards = {},
169
+ boardImages = [],
169
170
  } = {}) {
170
171
  this.#assertOpen();
171
172
  if (typeof diagram !== "string" || diagram.length === 0) {
172
173
  throw new LitzError("createSession needs the diagram.json text as `diagram`");
173
174
  }
174
- const appBytes = bytesOf(app, "app");
175
- if (appBytes.length === 0) {
175
+ const fleets = Array.isArray(boardImages) ? boardImages : [];
176
+ if (fleets.length === 0 && (app === undefined || app === null)) {
176
177
  throw new LitzError("createSession needs the application image as `app`");
177
178
  }
179
+ if (
180
+ fleets.some(
181
+ (board) =>
182
+ typeof board !== "object" ||
183
+ board === null ||
184
+ board.app === undefined ||
185
+ board.app === null,
186
+ )
187
+ ) {
188
+ throw new LitzError("every `boardImages` entry needs an `app` image");
189
+ }
178
190
  if (flashSize !== null && !(Number.isInteger(flashSize) && flashSize > 0)) {
179
191
  throw new LitzError("`flashSize` must be a positive whole number of bytes");
180
192
  }
@@ -196,20 +208,41 @@ export class Litz {
196
208
  const romBytes = optionalBytes(rom, "rom");
197
209
  const bootBytes = optionalBytes(boot, "boot");
198
210
  const ptableBytes = optionalBytes(ptable, "ptable");
199
- const buffers = [
200
- hold(io, appBytes),
201
- hold(io, romBytes),
202
- hold(io, bootBytes),
203
- hold(io, ptableBytes),
204
- ];
205
- const [appBuf, romBuf, bootBuf, ptableBuf] = buffers;
206
- const spec = {
207
- diagram,
208
- app: { ptr: appBuf.ptr, len: appBuf.len },
209
- rom: romBuf.len > 0 ? { ptr: romBuf.ptr, len: romBuf.len } : null,
210
- boot: bootBuf.len > 0 ? { ptr: bootBuf.ptr, len: bootBuf.len } : null,
211
- ptable: ptableBuf.len > 0 ? { ptr: ptableBuf.ptr, len: ptableBuf.len } : null,
212
- };
211
+ const spec = { diagram };
212
+ const buffers = [];
213
+ if (fleets.length === 0) {
214
+ const appBytes = bytesOf(app, "app");
215
+ if (appBytes.length === 0) {
216
+ throw new LitzError("createSession needs the application image as `app`");
217
+ }
218
+ const appBuf = hold(io, appBytes);
219
+ const romBuf = hold(io, romBytes);
220
+ const bootBuf = hold(io, bootBytes);
221
+ const ptableBuf = hold(io, ptableBytes);
222
+ buffers.push(appBuf, romBuf, bootBuf, ptableBuf);
223
+ spec.app = { ptr: appBuf.ptr, len: appBuf.len };
224
+ if (romBuf.len > 0) spec.rom = { ptr: romBuf.ptr, len: romBuf.len };
225
+ if (bootBuf.len > 0) spec.boot = { ptr: bootBuf.ptr, len: bootBuf.len };
226
+ if (ptableBuf.len > 0) spec.ptable = { ptr: ptableBuf.ptr, len: ptableBuf.len };
227
+ } else {
228
+ spec.boards = fleets.map((board) => {
229
+ const appBuf = hold(io, bytesOf(board.app, "board.app"));
230
+ const romBuf = hold(io, optionalBytes(board.rom, "board.rom"));
231
+ const bootBuf = hold(io, optionalBytes(board.boot, "board.boot"));
232
+ const ptableBuf = hold(io, optionalBytes(board.ptable, "board.ptable"));
233
+ buffers.push(appBuf, romBuf, bootBuf, ptableBuf);
234
+ const entry = { app: { ptr: appBuf.ptr, len: appBuf.len } };
235
+ if (board.id !== undefined && board.id !== null) entry.id = String(board.id);
236
+ if (romBuf.len > 0) entry.rom = { ptr: romBuf.ptr, len: romBuf.len };
237
+ if (bootBuf.len > 0) entry.boot = { ptr: bootBuf.ptr, len: bootBuf.len };
238
+ if (ptableBuf.len > 0) entry.ptable = { ptr: ptableBuf.ptr, len: ptableBuf.len };
239
+ if (board.flashSize !== undefined && board.flashSize !== null) {
240
+ entry.flashSize = board.flashSize;
241
+ }
242
+ if (board.mac !== undefined && board.mac !== null) entry.mac = String(board.mac);
243
+ return entry;
244
+ });
245
+ }
213
246
  if (flashSize !== null) spec.flashSize = flashSize;
214
247
  if (mac !== null) spec.mac = mac;
215
248
  if (analog) spec.analog = true;
package/src/session.js CHANGED
@@ -29,7 +29,9 @@ export class Session {
29
29
  const session = this;
30
30
  this.#serial = {
31
31
  take: () => session.#takeSerial(),
32
+ takeBoard: (board) => session.#takeBoardSerial(board),
32
33
  write: (data) => session.#writeSerial(data),
34
+ writeBoard: (board, data) => session.#writeBoardSerial(board, data),
33
35
  };
34
36
  this.#trace = Object.freeze({
35
37
  start: () => {
@@ -114,6 +116,52 @@ export class Session {
114
116
  }
115
117
  }
116
118
 
119
+ /** Every development board the session booted, in diagram order. */
120
+ boards() {
121
+ this.#assertOpen();
122
+ const count = this.#x.litz_board_count(this.#handle);
123
+ const out = [];
124
+ for (let index = 0; index < count; index += 1) {
125
+ const id = this.#readText(
126
+ (ptr, cap) => this.#x.litz_board_id(this.#handle, index, ptr, cap),
127
+ 32,
128
+ );
129
+ const info = this.#readText(
130
+ (ptr, cap) => this.#x.litz_board_chip_info(this.#handle, index, ptr, cap),
131
+ 128,
132
+ );
133
+ let chip;
134
+ try {
135
+ chip = JSON.parse(info);
136
+ } catch (error) {
137
+ throw new LitzError("the chip description was not valid JSON", { cause: error });
138
+ }
139
+ out.push({ index, id, ...chip });
140
+ }
141
+ return out;
142
+ }
143
+
144
+ /** One board by list index or `diagram.json` id. */
145
+ board(board = 0) {
146
+ this.#assertOpen();
147
+ if (typeof board === "number") {
148
+ const entry = this.boards()[board];
149
+ if (!entry) throw new LitzError(`there is no board at index ${board}`);
150
+ return entry;
151
+ }
152
+ const entry = this.boards().find((candidate) => candidate.id === board);
153
+ if (!entry) throw new LitzError(`there is no board ${board}`);
154
+ return entry;
155
+ }
156
+
157
+ /** The level one board sees on one of its own pads. */
158
+ gpioOn(board, pin) {
159
+ this.#assertOpen();
160
+ const index = this.#boardIndex(board);
161
+ const code = this.#x.litz_board_gpio_level(this.#handle, index, pin >>> 0);
162
+ return code < 0 ? null : LEVELS[code] ?? null;
163
+ }
164
+
117
165
  /** How the last `run` ended: budget, idle, halted, trap, reset or breakpoint. */
118
166
  get stop() {
119
167
  this.#assertOpen();
@@ -310,6 +358,151 @@ export class Session {
310
358
  return this.#x.litz_touch(this.#handle, x >>> 0, y >>> 0, down ? 1 : 0) === 1;
311
359
  }
312
360
 
361
+ /** One board's display, addressed by list index or board id. */
362
+ displayOn(board = 0) {
363
+ this.#assertOpen();
364
+ const index = this.#boardIndex(board);
365
+ const info = this.#x.litz_board_display_info(this.#handle, index);
366
+ if (info === 0) return null;
367
+ const width = info >>> 16;
368
+ const height = info & 0xffff;
369
+ const session = this;
370
+ return {
371
+ width,
372
+ height,
373
+ get version() {
374
+ session.#assertOpen();
375
+ return session.#x.litz_board_display_version(session.#handle, index);
376
+ },
377
+ pixels() {
378
+ session.#assertOpen();
379
+ const needed = width * height * 4;
380
+ const cap = session.#io.writeOut(needed);
381
+ const written = session.#x.litz_board_display_rgba(
382
+ session.#handle,
383
+ index,
384
+ cap.ptr,
385
+ cap.len,
386
+ );
387
+ if (written !== needed) {
388
+ throw session.#io.error("the display frame could not be read");
389
+ }
390
+ return session.#io.copyOut(written);
391
+ },
392
+ };
393
+ }
394
+
395
+ /** The browser-facing debugger, one board at a time. */
396
+ get debug() {
397
+ const session = this;
398
+ return Object.freeze({
399
+ available: (board = 0) => {
400
+ session.#assertOpen();
401
+ return session.#x.litz_debug_available(session.#handle, session.#boardIndex(board)) === 1;
402
+ },
403
+ pc: (board = 0) => {
404
+ session.#assertOpen();
405
+ const pc = session.#x.litz_debug_pc(session.#handle, session.#boardIndex(board));
406
+ return pc < 0 ? null : pc >>> 0;
407
+ },
408
+ registers: (board = 0) => {
409
+ session.#assertOpen();
410
+ const index = session.#boardIndex(board);
411
+ const cap = session.#io.writeOut(33 * 4);
412
+ const written = session.#x.litz_debug_registers(
413
+ session.#handle,
414
+ index,
415
+ cap.ptr,
416
+ cap.len,
417
+ );
418
+ if (written !== 33 * 4) return null;
419
+ const bytes = session.#io.copyOut(written);
420
+ const values = [];
421
+ for (let offset = 0; offset < written; offset += 4) {
422
+ values.push(
423
+ (bytes[offset] |
424
+ (bytes[offset + 1] << 8) |
425
+ (bytes[offset + 2] << 16) |
426
+ (bytes[offset + 3] << 24)) >>>
427
+ 0,
428
+ );
429
+ }
430
+ return { values: values.slice(0, 32), pc: values[32] };
431
+ },
432
+ memory: (board, addr, length) => {
433
+ session.#assertOpen();
434
+ const index = session.#boardIndex(board);
435
+ const cap = session.#io.writeOut(Math.max(1, length >>> 0));
436
+ const written = session.#x.litz_debug_memory(
437
+ session.#handle,
438
+ index,
439
+ addr >>> 0,
440
+ cap.ptr,
441
+ cap.len,
442
+ );
443
+ return session.#io.copyOut(written | 0);
444
+ },
445
+ disassemble: (board, addr, count = 8) => {
446
+ session.#assertOpen();
447
+ const index = session.#boardIndex(board);
448
+ return session.#readText(
449
+ (ptr, cap) =>
450
+ session.#x.litz_debug_disassemble(
451
+ session.#handle,
452
+ index,
453
+ addr >>> 0,
454
+ count >>> 0,
455
+ ptr,
456
+ cap,
457
+ ),
458
+ 4096,
459
+ );
460
+ },
461
+ setBreakpoints: (board, pcs) => {
462
+ session.#assertOpen();
463
+ const index = session.#boardIndex(board);
464
+ const list = Array.from(pcs, (pc) => pc >>> 0);
465
+ const bytes = new Uint8Array(list.length * 4);
466
+ list.forEach((pc, slot) => {
467
+ bytes[slot * 4] = pc & 0xff;
468
+ bytes[slot * 4 + 1] = (pc >>> 8) & 0xff;
469
+ bytes[slot * 4 + 2] = (pc >>> 16) & 0xff;
470
+ bytes[slot * 4 + 3] = (pc >>> 24) & 0xff;
471
+ });
472
+ const buf = session.#io.writeIn(bytes);
473
+ let code;
474
+ try {
475
+ code = session.#x.litz_debug_breakpoints(
476
+ session.#handle,
477
+ index,
478
+ buf.ptr,
479
+ list.length,
480
+ );
481
+ } finally {
482
+ session.#io.release(buf);
483
+ }
484
+ if (code !== 1) throw session.#io.error("the breakpoints could not be set");
485
+ },
486
+ step: (board = 0) => {
487
+ session.#assertOpen();
488
+ const code = session.#x.litz_debug_step(
489
+ session.#handle,
490
+ session.#boardIndex(board),
491
+ );
492
+ return code < 0 ? "unsupported" : STOPS[code] ?? "unknown";
493
+ },
494
+ run: (board = 0, steps = DEFAULT_STEPS) => {
495
+ session.#assertOpen();
496
+ const code = session.#x.litz_debug_run(
497
+ session.#handle,
498
+ session.#boardIndex(board),
499
+ steps >>> 0,
500
+ );
501
+ return code < 0 ? "unsupported" : STOPS[code] ?? "unknown";
502
+ },
503
+ });
504
+ }
505
+
313
506
  /** The scope document: every probe's samples and every decoder's frames. */
314
507
  scope() {
315
508
  this.#assertOpen();
@@ -352,6 +545,49 @@ export class Session {
352
545
  return { id, code, kind: entry.kind, state: entry.state, value };
353
546
  }
354
547
 
548
+ #boardIndex(board) {
549
+ if (typeof board === "number") {
550
+ if (!Number.isInteger(board) || board < 0) {
551
+ throw new LitzError(`there is no board ${board}`);
552
+ }
553
+ return board;
554
+ }
555
+ return this.board(board).index;
556
+ }
557
+
558
+ #takeBoardSerial(board) {
559
+ this.#assertOpen();
560
+ const index = this.#boardIndex(board);
561
+ const chunks = [];
562
+ for (;;) {
563
+ const cap = this.#io.writeOut(64 * 1024);
564
+ const written = this.#x.litz_serial_take_board(
565
+ this.#handle,
566
+ index,
567
+ cap.ptr,
568
+ cap.len,
569
+ );
570
+ if (written === 0) break;
571
+ chunks.push(this.#io.copyOut(written));
572
+ if (written < cap.len) break;
573
+ }
574
+ return decodeChunks(chunks);
575
+ }
576
+
577
+ #writeBoardSerial(board, data) {
578
+ this.#assertOpen();
579
+ const index = this.#boardIndex(board);
580
+ const bytes = typeof data === "string" ? ENCODER.encode(data) : bytesOf(data, "serial data");
581
+ if (bytes.length === 0) return;
582
+ const buf = this.#io.writeIn(bytes);
583
+ try {
584
+ this.#x.litz_serial_write_board(this.#handle, index, buf.ptr, buf.len);
585
+ } finally {
586
+ this.#io.release(buf);
587
+ }
588
+ this.#throwStored();
589
+ }
590
+
355
591
  #takeSerial() {
356
592
  this.#assertOpen();
357
593
  const chunks = [];