virtual-machine 0.2.2 → 0.3.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/build/index.d.ts CHANGED
@@ -15,17 +15,17 @@ declare class WasmVm {
15
15
  free(): void;
16
16
  [Symbol.dispose](): void;
17
17
  /**
18
- * Enable VirtIO GPU device for graphics rendering.
18
+ * Enable D1 Display device for graphics rendering.
19
19
  *
20
- * The GPU device allows the kernel to render to a framebuffer which can
20
+ * The display device allows the kernel to render to a framebuffer which can
21
21
  * then be displayed in a Canvas element. Use `get_gpu_frame()` to retrieve
22
22
  * the rendered frame pixels.
23
23
  *
24
24
  * # Arguments
25
- * * `width` - Display width in pixels (default 800 if 0)
26
- * * `height` - Display height in pixels (default 600 if 0)
25
+ * * `width` - Display width in pixels (ignored, uses 1024x768)
26
+ * * `height` - Display height in pixels (ignored, uses 1024x768)
27
27
  */
28
- enable_gpu(width: number, height: number): void;
28
+ enable_gpu(_width: number, _height: number): void;
29
29
  /**
30
30
  * Get a byte from the UART output buffer, if available.
31
31
  *
@@ -62,7 +62,7 @@ declare class WasmVm {
62
62
  * Get GPU frame data as RGBA pixels.
63
63
  * Returns a Uint8Array of pixel data, or null if no frame is available.
64
64
  *
65
- * The frame data is in RGBA format with 4 bytes per pixel (800x600 = 1,920,000 bytes).
65
+ * The frame data is in RGBA format with 4 bytes per pixel (1024×768 = 3,145,728 bytes).
66
66
  *
67
67
  * This reads from a fixed framebuffer address in guest memory (0x8100_0000).
68
68
  * The kernel GPU driver writes pixels there, and we read them here.
@@ -101,7 +101,7 @@ declare class WasmVm {
101
101
  get_heap_usage(): Array<any>;
102
102
  /**
103
103
  * Get the current network connection status.
104
- * This checks the actual connection state by seeing if an IP was assigned.
104
+ * This checks the actual connection state by seeing if D1 EMAC is enabled.
105
105
  */
106
106
  network_status(): NetworkStatus;
107
107
  /**
@@ -126,6 +126,29 @@ declare class WasmVm {
126
126
  * Get current memory usage (DRAM size) in bytes.
127
127
  */
128
128
  get_memory_usage(): bigint;
129
+ /**
130
+ * Send a mouse event to the guest.
131
+ *
132
+ * # Arguments
133
+ * * `x` - X position (0-799)
134
+ * * `y` - Y position (0-599)
135
+ * * `buttons` - Button state bitmask (bit 0 = left, bit 1 = right, bit 2 = middle)
136
+ * * `prev_buttons` - Previous button state to detect changes
137
+ *
138
+ * Returns true if the event was sent successfully.
139
+ */
140
+ send_mouse_event(x: number, y: number, buttons: number): boolean;
141
+ /**
142
+ * Send a touch event to the D1 GT911 touchscreen controller.
143
+ *
144
+ * # Arguments
145
+ * * `x` - X position (0 to display width)
146
+ * * `y` - Y position (0 to display height)
147
+ * * `pressed` - true for touch down/move, false for touch up
148
+ *
149
+ * Returns true if the event was sent successfully.
150
+ */
151
+ send_touch_event(x: number, y: number, pressed: boolean): boolean;
129
152
  /**
130
153
  * Get the total disk capacity from attached VirtIO block devices.
131
154
  * Returns total bytes across all block devices.
@@ -136,6 +159,14 @@ declare class WasmVm {
136
159
  * Returns None if not in SMP mode.
137
160
  */
138
161
  get_shared_buffer(): SharedArrayBuffer | undefined;
162
+ /**
163
+ * Send a mouse button event to the guest.
164
+ *
165
+ * # Arguments
166
+ * * `button` - Button number (0 = left, 1 = right, 2 = middle)
167
+ * * `pressed` - true for press, false for release
168
+ */
169
+ send_mouse_button(button: number, pressed: boolean): boolean;
139
170
  /**
140
171
  * Terminate all workers.
141
172
  */
@@ -154,6 +185,22 @@ declare class WasmVm {
154
185
  * Note: Connection is asynchronous. Check network_status() to monitor connection state.
155
186
  */
156
187
  connect_webtransport(url: string, cert_hash?: string | null): void;
188
+ /**
189
+ * Get a direct zero-copy view into the framebuffer in SharedArrayBuffer.
190
+ *
191
+ * This eliminates all memory copies by creating a Uint8Array view directly
192
+ * into the SharedArrayBuffer at the framebuffer offset. The browser can
193
+ * pass this directly to WebGPU's writeTexture for zero-copy rendering.
194
+ *
195
+ * Returns None if SharedArrayBuffer is not available (single-threaded mode).
196
+ */
197
+ get_framebuffer_view(): Uint8Array | undefined;
198
+ /**
199
+ * Get the current frame version from kernel memory.
200
+ * Returns a u32 that increments each time the kernel flushes dirty pixels.
201
+ * Browser can compare this to skip fetching unchanged frames.
202
+ */
203
+ get_gpu_frame_version(): number;
157
204
  /**
158
205
  * Inject a network packet to be received by the guest.
159
206
  * Called from JavaScript when the native WebTransport addon receives a packet.
@@ -250,7 +297,7 @@ declare class WasmVm {
250
297
  */
251
298
  is_halted(): boolean;
252
299
  /**
253
- * Load a disk image and attach it as a VirtIO block device.
300
+ * Load a disk image and attach it as a D1 MMC device.
254
301
  * This should be called before starting execution if the kernel needs a filesystem.
255
302
  */
256
303
  load_disk(disk_image: Uint8Array): void;
@@ -282,10 +329,22 @@ declare class WorkerState {
282
329
  * Get the total step count.
283
330
  */
284
331
  step_count(): bigint;
332
+ /**
333
+ * Check if MSIP is pending for this hart (for debugging).
334
+ */
335
+ is_msip_pending(): boolean;
336
+ /**
337
+ * Check if timer is pending for this hart (for debugging).
338
+ */
339
+ is_timer_pending(): boolean;
285
340
  /**
286
341
  * Create a new worker state for a secondary hart.
287
342
  */
288
343
  constructor(hart_id: number, shared_mem: any, entry_pc: bigint);
344
+ /**
345
+ * Get the a0 register value (for debugging hart ID passing).
346
+ */
347
+ get_a0(): bigint;
289
348
  /**
290
349
  * Get the hart ID.
291
350
  */
@@ -312,6 +371,11 @@ declare enum WorkerStepResult {
312
371
  * Fatal error occurred
313
372
  */
314
373
  Error = 3,
374
+ /**
375
+ * WFI executed - worker should yield to prevent busy loop
376
+ * TypeScript should add a small delay before calling step_batch again
377
+ */
378
+ Wfi = 4,
315
379
  }
316
380
 
317
381
  /**
@@ -350,7 +414,9 @@ interface InitOutput {
350
414
  readonly wasmvm_get_cpu_count: (a: number) => number;
351
415
  readonly wasmvm_get_disk_capacity: (a: number) => bigint;
352
416
  readonly wasmvm_get_disk_usage: (a: number) => any;
417
+ readonly wasmvm_get_framebuffer_view: (a: number) => any;
353
418
  readonly wasmvm_get_gpu_frame: (a: number) => any;
419
+ readonly wasmvm_get_gpu_frame_version: (a: number) => number;
354
420
  readonly wasmvm_get_gpu_size: (a: number) => any;
355
421
  readonly wasmvm_get_heap_usage: (a: number) => any;
356
422
  readonly wasmvm_get_memory_usage: (a: number) => bigint;
@@ -372,6 +438,9 @@ interface InitOutput {
372
438
  readonly wasmvm_print_banner: (a: number) => void;
373
439
  readonly wasmvm_print_status: (a: number, b: number, c: number) => void;
374
440
  readonly wasmvm_send_key_event: (a: number, b: number, c: number) => number;
441
+ readonly wasmvm_send_mouse_button: (a: number, b: number, c: number) => number;
442
+ readonly wasmvm_send_mouse_event: (a: number, b: number, c: number, d: number) => number;
443
+ readonly wasmvm_send_touch_event: (a: number, b: number, c: number, d: number) => number;
375
444
  readonly wasmvm_set_external_network_ip: (a: number, b: any) => number;
376
445
  readonly wasmvm_setup_external_network: (a: number, b: any) => [number, number];
377
446
  readonly wasmvm_start_workers: (a: number, b: number, c: number) => [number, number];
@@ -380,17 +449,16 @@ interface InitOutput {
380
449
  readonly wasmvm_terminate_workers: (a: number) => void;
381
450
  readonly wasmvm_uart_output_pending: (a: number) => number;
382
451
  readonly worker_check_interrupts: (a: number, b: any) => bigint;
383
- readonly worker_entry: (a: number, b: any, c: bigint) => void;
452
+ readonly workerstate_get_a0: (a: number) => bigint;
384
453
  readonly workerstate_hart_id: (a: number) => number;
454
+ readonly workerstate_is_msip_pending: (a: number) => number;
455
+ readonly workerstate_is_timer_pending: (a: number) => number;
385
456
  readonly workerstate_new: (a: number, b: any, c: bigint) => number;
386
457
  readonly workerstate_step_batch: (a: number, b: number) => number;
387
458
  readonly workerstate_step_count: (a: number) => bigint;
388
- readonly wasm_bindgen__convert__closures_____invoke__h74d7f37f0b0d661d: (a: number, b: number, c: any) => void;
389
- readonly wasm_bindgen__closure__destroy__h01c75dc83ec8dfae: (a: number, b: number) => void;
390
- readonly wasm_bindgen__convert__closures_____invoke__h9f0ee31d317d33d7: (a: number, b: number) => void;
391
- readonly wasm_bindgen__convert__closures_____invoke__h260170fb96639bcf: (a: number, b: number, c: any) => void;
392
- readonly wasm_bindgen__closure__destroy__h612f8e24953b61d5: (a: number, b: number) => void;
393
- readonly wasm_bindgen__convert__closures_____invoke__h97b9bec6245a8424: (a: number, b: number) => void;
459
+ readonly worker_entry: (a: number, b: any, c: bigint) => void;
460
+ readonly wasm_bindgen__convert__closures_____invoke__hfbd40044a7a1ae85: (a: number, b: number, c: any) => void;
461
+ readonly wasm_bindgen__closure__destroy__h4b145e497aa7828d: (a: number, b: number) => void;
394
462
  readonly __wbindgen_malloc: (a: number, b: number) => number;
395
463
  readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
396
464
  readonly __wbindgen_exn_store: (a: number) => void;
@@ -513,6 +581,37 @@ interface VmOptions {
513
581
  * @returns WasmVm instance
514
582
  */
515
583
  declare function createVM(kernelData: Uint8Array, options?: VmOptions): Promise<WasmVm>;
584
+ interface SDBootInfo {
585
+ kernelData: Uint8Array;
586
+ sdcardData: Uint8Array;
587
+ fsPartitionStart: number;
588
+ }
589
+ /**
590
+ * Parse an SD card image to extract kernel from FAT32 boot partition.
591
+ *
592
+ * SD Card layout:
593
+ * - MBR with partition table
594
+ * - Partition 1 (FAT32): kernel.bin
595
+ * - Partition 2: SFS filesystem
596
+ */
597
+ declare function parseSDCard(sdcard: Uint8Array): SDBootInfo;
598
+ /**
599
+ * Fetch an SD card image from a URL for browser use.
600
+ *
601
+ * @param url - URL to fetch SD card image from
602
+ * @returns Parsed boot info with kernel and disk data
603
+ */
604
+ declare function loadSDCardFromUrl(url: string): Promise<SDBootInfo>;
605
+ /**
606
+ * Create a VM from an SD card image (fetched from URL).
607
+ *
608
+ * This is the recommended way to boot the VM in browser.
609
+ *
610
+ * @param sdcardUrl - URL to SD card image
611
+ * @param options - VM configuration options
612
+ * @returns WasmVm instance ready to run
613
+ */
614
+ declare function createVMFromSDCard(sdcardUrl: string, options?: VmOptions): Promise<WasmVm>;
516
615
  /**
517
616
  * Run the VM with an output callback for UART data.
518
617
  *
@@ -580,4 +679,4 @@ interface WorkerManager {
580
679
  */
581
680
  declare function createWorkerManager(): WorkerManager;
582
681
 
583
- export { NetworkStatus, REQUIRED_HEADERS, type SharedMemorySupport, type VmOptions, WasmInternal, WasmVm, type WorkerErrorMessage, type WorkerHaltedMessage, type WorkerInitMessage, type WorkerManager, type WorkerOutboundMessage, type WorkerReadyMessage, checkSharedMemorySupport, createVM, createWorkerManager, isCrossOriginIsolated, isHaltRequested, isHalted, requestHalt, runVM };
682
+ export { NetworkStatus, REQUIRED_HEADERS, type SharedMemorySupport, type VmOptions, WasmInternal, WasmVm, type WorkerErrorMessage, type WorkerHaltedMessage, type WorkerInitMessage, type WorkerManager, type WorkerOutboundMessage, type WorkerReadyMessage, checkSharedMemorySupport, createVM, createVMFromSDCard, createWorkerManager, isCrossOriginIsolated, isHaltRequested, isHalted, loadSDCardFromUrl, parseSDCard, requestHalt, runVM };