# copy/v86 architecture

> Architecture diagram of the GitHub repository copy/v86, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/copy/v86
- Repository: https://github.com/copy/v86

## Overview

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: [`src/bus.js`](https://github.com/copy/v86/blob/master/src/bus.js) (Bus layer), [`src/browser/main.js`](https://github.com/copy/v86/blob/master/src/browser/main.js) (Browser main), [`src/cpu.js`](https://github.com/copy/v86/blob/master/src/cpu.js) (CPU core), [`tests/full/run.js`](https://github.com/copy/v86/blob/master/tests/full/run.js) (Tests)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entrypoints"]
  node_index_html["Index page<br/>entry html<br/>[index.html]"]
  node_debug_html["Debug page<br/>entry html<br/>[debug.html]"]
  node_src_main_js["Main bootstrap<br/>startup js<br/>[main.js]"]
  node_browser_main_js["Browser main<br/>ui bootstrap<br/>[main.js]"]
  node_browser_starter_js["Starter<br/>startup glue<br/>[starter.js]"]
end

subgraph group_guest["Guest Machine"]
  node_cpu_core["CPU core<br/>x86 execution<br/>[cpu.js]"]
  node_paging_mem["Paging<br/>mmu<br/>[paging.rs]"]
  node_regs_state["Registers<br/>cpu state<br/>[regs.rs]"]
  node_rust_runtime["Rust runtime<br/>jit wasm<br/>[lib.rs]"]
  node_bus_chipset["Bus layer<br/>motherboard<br/>[bus.js]"]
  node_memory_layer["Memory layer<br/>guest memory<br/>[buffer.js]"]
end

subgraph group_host["Host Integration"]
  node_display_audio["Display & audio<br/>devices<br/>[vga.js]"]
  node_input_devices["Input devices<br/>host adapters<br/>[keyboard.js]"]
  node_network_bridge["Network bridge<br/>pluggable net<br/>[network.js]"]
  node_browser_storage["Storage adapters<br/>host storage<br/>[filestorage.js]"]
  node_state_snapshots["Snapshots<br/>save restore<br/>[state.js]"]
end

subgraph group_media["Boot Media"]
  node_storage_media["Storage<br/>block media<br/>[ide.js]"]
  node_filesystem_share["9p share<br/>fs bridge<br/>[9p.js]"]
end

subgraph group_tooling["Build & Tests"]
  node_gen_build["Codegen<br/>generator<br/>[generate_jit.js]"]
  node_tests_harness["Tests<br/>validation<br/>[run.js]"]
end

node_index_html -->|"loads"| node_src_main_js
node_debug_html -->|"loads"| node_src_main_js
node_src_main_js -->|"delegates"| node_browser_main_js
node_browser_main_js -->|"configures"| node_browser_starter_js
node_browser_starter_js -->|"boots"| node_cpu_core
node_browser_starter_js -->|"attaches media"| node_storage_media
node_cpu_core -->|"reads/writes"| node_regs_state
node_cpu_core -->|"translates"| node_paging_mem
node_cpu_core -->|"traps io"| node_bus_chipset
node_bus_chipset -->|"routes"| node_memory_layer
node_bus_chipset -->|"connects"| node_storage_media
node_bus_chipset -->|"drives"| node_display_audio
node_bus_chipset -->|"handles"| node_input_devices
node_bus_chipset -->|"bridges"| node_network_bridge
node_memory_layer -->|"backs"| node_paging_mem
node_storage_media -->|"augments"| node_filesystem_share
node_browser_main_js -->|"renders"| node_display_audio
node_browser_main_js -->|"captures"| node_input_devices
node_browser_main_js -->|"persists"| node_browser_storage
node_browser_main_js -->|"provides"| node_network_bridge
node_state_snapshots -->|"serializes"| node_cpu_core
node_state_snapshots -->|"serializes"| node_memory_layer
node_state_snapshots -->|"serializes"| node_bus_chipset
node_gen_build -->|"generates"| node_rust_runtime
node_tests_harness -->|"validates"| node_cpu_core
node_tests_harness -->|"validates"| node_paging_mem
node_tests_harness -->|"validates"| node_bus_chipset
node_tests_harness -->|"validates"| node_network_bridge
node_tests_harness -->|"validates"| node_state_snapshots

click node_index_html "https://github.com/copy/v86/blob/master/index.html"
click node_debug_html "https://github.com/copy/v86/blob/master/debug.html"
click node_src_main_js "https://github.com/copy/v86/blob/master/src/main.js"
click node_browser_main_js "https://github.com/copy/v86/blob/master/src/browser/main.js"
click node_browser_starter_js "https://github.com/copy/v86/blob/master/src/browser/starter.js"
click node_cpu_core "https://github.com/copy/v86/blob/master/src/cpu.js"
click node_paging_mem "https://github.com/copy/v86/blob/master/src/rust/paging.rs"
click node_regs_state "https://github.com/copy/v86/blob/master/src/rust/regs.rs"
click node_rust_runtime "https://github.com/copy/v86/blob/master/src/rust/lib.rs"
click node_bus_chipset "https://github.com/copy/v86/blob/master/src/bus.js"
click node_memory_layer "https://github.com/copy/v86/blob/master/src/buffer.js"
click node_storage_media "https://github.com/copy/v86/blob/master/src/ide.js"
click node_filesystem_share "https://github.com/copy/v86/blob/master/lib/9p.js"
click node_display_audio "https://github.com/copy/v86/blob/master/src/vga.js"
click node_input_devices "https://github.com/copy/v86/blob/master/src/browser/keyboard.js"
click node_network_bridge "https://github.com/copy/v86/blob/master/src/browser/network.js"
click node_browser_storage "https://github.com/copy/v86/blob/master/src/browser/filestorage.js"
click node_state_snapshots "https://github.com/copy/v86/blob/master/src/state.js"
click node_gen_build "https://github.com/copy/v86/blob/master/gen/generate_jit.js"
click node_tests_harness "https://github.com/copy/v86/blob/master/tests/full/run.js"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_index_html,node_debug_html,node_src_main_js,node_browser_main_js,node_browser_starter_js toneBlue
class node_cpu_core,node_paging_mem,node_regs_state,node_rust_runtime,node_bus_chipset,node_memory_layer toneAmber
class node_display_audio,node_input_devices,node_network_bridge,node_browser_storage,node_state_snapshots toneMint
class node_storage_media,node_filesystem_share toneRose
class node_gen_build,node_tests_harness toneIndigo
```

## Components

### Entrypoints

HTML and JS startup surfaces that launch the emulator.

- [Index page](https://github.com/copy/v86/blob/master/index.html): entry html · Primary browser entrypoint for running the emulator. · `index.html`
- [Debug page](https://github.com/copy/v86/blob/master/debug.html): entry html · Alternate browser entrypoint with debugging-oriented startup. · `debug.html`
- [Main bootstrap](https://github.com/copy/v86/blob/master/src/main.js): startup js · Top-level emulator bootstrap and wiring logic. · `src/main.js`
- [Browser main](https://github.com/copy/v86/blob/master/src/browser/main.js): ui bootstrap · Browser-side emulator setup and host integration entry. · `src/browser/main.js`
- [Starter](https://github.com/copy/v86/blob/master/src/browser/starter.js): startup glue · 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](https://github.com/copy/v86/blob/master/src/cpu.js): x86 execution · Instruction execution, decoding, and CPU state. · `src/cpu.js`
- [Paging](https://github.com/copy/v86/blob/master/src/rust/paging.rs): mmu · Guest address translation and page-fault handling. · `src/rust/paging.rs`
- [Registers](https://github.com/copy/v86/blob/master/src/rust/regs.rs): cpu state · Architectural register and mode state. · `src/rust/regs.rs`
- [Rust runtime](https://github.com/copy/v86/blob/master/src/rust/lib.rs): jit wasm · Rust-side CPU, JIT, and wasm generation runtime. · `src/rust/lib.rs`
- [Bus layer](https://github.com/copy/v86/blob/master/src/bus.js): motherboard · System bus, I/O routing, DMA, PCI, interrupts, and timers. · `src/bus.js`
- [Memory layer](https://github.com/copy/v86/blob/master/src/buffer.js): guest memory · Buffer and page abstractions backing guest RAM and MMIO. · `src/buffer.js`

### Host Integration

Browser-facing adapters and UI glue.

- [Display & audio](https://github.com/copy/v86/blob/master/src/vga.js): devices · VGA screen path plus speaker and serial-style host outputs. · `src/vga.js`
- [Input devices](https://github.com/copy/v86/blob/master/src/browser/keyboard.js): host adapters · Keyboard and mouse capture/adapters for browser input. · `src/browser/keyboard.js`
- [Network bridge](https://github.com/copy/v86/blob/master/src/browser/network.js): pluggable net · Guest NICs connected to browser-side network providers. · `src/browser/network.js`
- [Storage adapters](https://github.com/copy/v86/blob/master/src/browser/filestorage.js): host storage · Browser file and state persistence helpers. · `src/browser/filestorage.js`
- [Snapshots](https://github.com/copy/v86/blob/master/src/state.js): save restore · Machine state serialization and restore logic. · `src/state.js`

### Boot Media

ROMs, disks, images, and filesystem sharing paths.

- [Storage](https://github.com/copy/v86/blob/master/src/ide.js): block media · Floppy, IDE, ISO, and kernel/media loading support. · `src/ide.js`
- [9p share](https://github.com/copy/v86/blob/master/lib/9p.js): fs bridge · Guest/host file sharing path separate from block devices. · `lib/9p.js`

### Build & Tests

Code generation, build scripts, and validation harnesses.

- [Codegen](https://github.com/copy/v86/blob/master/gen/generate_jit.js): generator · Opcode tables and analyzer/JIT code generation scripts. · `gen/generate_jit.js`
- [Tests](https://github.com/copy/v86/blob/master/tests/full/run.js): validation · Integration, device, JIT, and compatibility test suites. · `tests/full/run.js`

## Connections

- 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.
