How copy/v86 works
1. What this repo is
v86 is a browser-first x86 PC emulator. The core job is to boot a guest OS, emulate the CPU and chipset, and expose a set of virtual devices to the guest while the host browser supplies ROMs, disk images, input, display, audio, and network.
2. Top-level execution boundary
The project is split between the guest machine model and host integration. The guest side includes CPU, memory, interrupts, and devices; the host side lives mostly under src/browser/ and adapts browser APIs such as DOM, canvas, workers, fetch/XHR, storage, and networking.
3. Boot and startup flow
The entrypoints (index.html, debug.html, src/main.js, src/browser/main.js, src/browser/starter.js) initialize an emulator instance, load BIOS ROMs from bios/, then attach optional boot media like floppy, CD-ROM, hard disk, or filesystem images. The README’s example reflects the typical flow: BIOS + VGA BIOS + image URLs + autostart.
4. CPU emulation pipeline
The CPU subsystem is the core of the architecture. It includes instruction decode, paging, segmentation, register state, and execution machinery in src/cpu.js, src/paging.js, src/regs.js, and the Rust subtree under src/rust/. The Rust code and gen/ scripts indicate a translated/JIT-oriented execution path, with instruction tables and code generators producing WebAssembly-oriented runtime pieces.
5. Memory and address translation
Memory is virtualized through buffer/page abstractions and paging logic (src/buffer.js, src/page.rs, src/paging.js, src/rust/memory.rs). Guest memory access is also the main point where devices intersect with the CPU via memory-mapped I/O and page faults.
6. System bus and chipset plumbing
src/bus.js, src/io.js, src/dma.js, src/pci.js, and interrupt/timing modules like src/pic.js, src/pit.js, src/rtc.js, src/acpi.js, and src/cpu/apic.rs form the motherboard layer. This is the routing fabric between CPU, RAM, and peripherals, and it is where port I/O, DMA, IRQs, and PCI configuration are coordinated.
7. Storage and boot media
Disk and boot media support is split across src/floppy.js, src/ide.js, src/iso9660.js, src/kernel.js, and filesystem helpers in lib/. The emulator can mount host-provided images and also synthesize media such as ISO9660 CD-ROMs. The 9p filesystem support (lib/9p.js, lib/filesystem.js, docs/filesystem.md) is a separate guest/host file-sharing path, not the same as block-device emulation.
8. Peripheral device model
Device emulation is modular and mostly one-file-per-device: VGA/screen in src/vga.js plus browser screen adapters, PS/2 keyboard and mouse, serial/UART, Sound Blaster 16, and virtio devices (src/virtio*.js). The README’s hardware list matches these modules closely: floppy, PS/2, PIC/PIT/RTC, VGA, PCI, IDE, NE2K, virtio FS/net/balloon, modem, and audio.
9. Browser-specific adapters
The src/browser/ subtree is the host integration layer. It converts emulator events into browser UI and browser APIs: screen rendering, keyboard/mouse capture, serial terminal, speaker/audio output, file storage, and several networking backends (fetch_network, broadcast, inbrowser, wisp, fake). These adapters are the main boundary where guest I/O is mapped onto real host capabilities.
10. Networking architecture
Networking is treated as a pluggable host bridge rather than a single stack. Guest NICs such as ne2k.js and virtio_net.js connect to browser-side network providers in src/browser/network.js and related files. The repo supports fetch-based downloads, peer-to-peer browser communication, and modem-style dial-up emulation, so the network boundary is intentionally abstracted.
11. State, snapshots, and debugging
Emulator state is first-class: src/state.js, src/browser/print_stats.js, src/log.js, src/rust/profiler.rs, and the save/restore example show that complete machine snapshots can be serialized and reloaded. This is important architecturally because it spans CPU state, memory, devices, and timers, not just application data.
12. Build and code generation
The repo uses a mixed JS + Rust + Wasm toolchain. gen/ contains opcode and analyzer/JIT generation scripts; the Rust subtree contains CPU/JIT/Wasm builder code; and the top-level Cargo.toml, package.json, Makefile, and GitHub Actions CI tie the build together. The result is a browser-distributed emulator that can be built in debug or optimized form and packaged as libv86.js/npm.
13. Validation and compatibility harness
Testing is unusually broad and reflects the emulator’s architecture: browser API tests, device tests, JIT/paging tests, NASM instruction fixtures, QEMU/kvm-unit-tests, and full integration boot tests under tests/. This test layout maps directly to the main subsystems above and is useful for drawing subsystem boundaries in an architecture diagram.
Main entry points: bus.js Bus layer · main.js Browser main · cpu.js CPU core · run.js Tests
Components 20
Entrypoints
HTML and JS startup surfaces that launch the emulator.
- Index page · Primary browser entrypoint for running the emulator.
index.html - Debug page · Alternate browser entrypoint with debugging-oriented startup.
debug.html - Main bootstrap · Top-level emulator bootstrap and wiring logic.
src/main.js - Browser main · Browser-side emulator setup and host integration entry.
src/browser/main.js - Starter · Creates and configures emulator instances from host-provided options.
src/browser/starter.js
Guest Machine
Core x86 machine model executed inside the emulator.
- CPU core · Instruction execution, decoding, and CPU state.
src/cpu.js - Paging · Guest address translation and page-fault handling.
src/rust/paging.rs - Registers · Architectural register and mode state.
src/rust/regs.rs - Rust runtime · Rust-side CPU, JIT, and wasm generation runtime.
src/rust/lib.rs - Bus layer · System bus, I/O routing, DMA, PCI, interrupts, and timers.
src/bus.js - Memory layer · Buffer and page abstractions backing guest RAM and MMIO.
src/buffer.js
Host Integration
Browser-facing adapters and UI glue.
- Display & audio · VGA screen path plus speaker and serial-style host outputs.
src/vga.js - Input devices · Keyboard and mouse capture/adapters for browser input.
src/browser/keyboard.js - Network bridge · Guest NICs connected to browser-side network providers.
src/browser/network.js - Storage adapters · Browser file and state persistence helpers.
src/browser/filestorage.js - Snapshots · Machine state serialization and restore logic.
src/state.js
Boot Media
ROMs, disks, images, and filesystem sharing paths.
Connections 29
- Index page → Main bootstrap: loads · Browser loads the main emulator bootstrap.
- Debug page → Main bootstrap: loads · Debug page also enters the shared bootstrap path.
- Main bootstrap → Browser main: delegates · Top-level startup hands off to browser integration.
- Browser main → Starter: configures · Browser main creates and configures emulator instances.
- Starter → CPU core: boots · Startup initializes the CPU and machine execution path.
- Starter → Storage: attaches media · Boot flow attaches BIOS and optional disks or images.
- CPU core → Registers: reads/writes · CPU execution operates on architectural register state.
- CPU core → Paging: translates · CPU consults paging for virtual-to-physical access.
- CPU core → Bus layer: traps io · CPU execution reaches ports, IRQs, and chipset services.
- Bus layer → Memory layer: routes · System bus mediates RAM, MMIO, DMA, and device access.
- Bus layer → Storage: connects · Chipset plumbing exposes block and boot-related devices.
- Bus layer → Display & audio: drives · Motherboard layer connects VGA, audio, and serial peripherals.
- Bus layer → Input devices: handles · Bus routes keyboard and mouse device interrupts/events.
- Bus layer → Network bridge: bridges · Guest NICs connect through host network providers.
- Memory layer → Paging: backs · Guest memory buffers are accessed through paging and MMIO.
- Storage → 9p share: augments · File sharing is a separate host/guest path from block media.
- Browser main → Display & audio: renders · Browser adapters turn emulator display/audio into host UI.
- Browser main → Input devices: captures · Browser adapters capture user input for guest devices.
- Browser main → Storage adapters: persists · Host integration saves files and browser-side state.
- Browser main → Network bridge: provides · Browser integration supplies concrete network backends.
- Snapshots → CPU core: serializes · Snapshots include CPU state and execution context.
- Snapshots → Memory layer: serializes · Snapshots include guest memory contents.
- Snapshots → Bus layer: serializes · Snapshots include devices, timers, and machine wiring.
- Codegen → Rust runtime: generates · Build-time generators produce Rust/JIT execution pieces.
- Tests → CPU core: validates · Test suites exercise instruction execution and CPU behavior.
- Tests → Paging: validates · Tests cover paging and memory access behavior.
- Tests → Bus layer: validates · Device and integration tests cover motherboard plumbing.
- Tests → Network bridge: validates · Network tests exercise host bridge implementations.
- Tests → Snapshots: validates · Snapshot and restore paths are covered by integration tests.