# kichman/vmjs architecture

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

- Interactive diagram: https://gitdiagram.com/kichman/vmjs
- Repository: https://github.com/kichman/vmjs

## Overview

vmjs is a TypeScript implementation of the Uxn virtual machine. A caller creates a VM with a device, loads program bytes into its RAM, and calls run(); the VM fetches instructions, dispatches base opcodes through a table or immediate opcodes through a special handler, and returns the final program counter when BRK stops execution. Opcode families provide flow, memory, stack, arithmetic, and device operations over shared VM state and stack helpers. The README documents the device contract and DEC/LIT extensions; device-operation wiring is shown only at the contract level because its implementation excerpt was not provided.

Main entry points: [`src/opcodes/index.ts`](https://github.com/kichman/vmjs/blob/main/src/opcodes/index.ts) (Opcode exports), [`src/stack.ts`](https://github.com/kichman/vmjs/blob/main/src/stack.ts) (Stack helpers), [`src/uxn-vm.ts`](https://github.com/kichman/vmjs/blob/main/src/uxn-vm.ts) (Uxn runtime), [`src/index.ts`](https://github.com/kichman/vmjs/blob/main/src/index.ts) (Public API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_execution["Execution core"]
  node_public_api["Public API<br/>[index.ts]"]
  node_vm["Uxn runtime<br/>[uxn-vm.ts]"]
  node_opcode_table["Base opcode table<br/>[opcode-table.ts]"]
end

subgraph group_operations["Opcode operations"]
  node_immediate["LIT and DEC<br/>[immediate-ops.ts]"]
  node_opcode_exports["Opcode exports<br/>[index.ts]"]
  node_flow_ops["Flow operations<br/>[flow-ops.ts]"]
  node_stack_ops["Stack operations<br/>[stack-ops.ts]"]
  node_memory_ops["Memory operations<br/>[mem-ops.ts]"]
  node_algebra_ops["Algebra operations<br/>[algebra-ops.ts]"]
  node_device_ops["Device operations<br/>[dev-ops.ts]"]
end

subgraph group_state["VM state"]
  node_vm_state["VM state contract<br/>[uxn-types.ts]"]
  node_stack_helpers["Stack helpers<br/>[stack.ts]"]
end

subgraph group_devices["Device I/O"]
  node_device_contract["Device contract<br/>[uxn-types.ts]"]
  node_console_device["Console device<br/>[ConsoleDevice.ts]"]
end

node_caller(("Program caller"))
node_external_output(("Console output"))

node_caller -->|"uses"| node_public_api
node_public_api -->|"exports"| node_vm
node_public_api -->|"exports"| node_console_device
node_vm -->|"dispatches base ops"| node_opcode_table
node_vm -->|"dispatches immediate ops"| node_immediate
node_opcode_table -->|"imports handlers"| node_opcode_exports
node_opcode_exports -->|"re-exports"| node_flow_ops
node_opcode_exports -->|"re-exports"| node_stack_ops
node_opcode_exports -->|"re-exports"| node_memory_ops
node_opcode_exports -->|"re-exports"| node_algebra_ops
node_opcode_exports -->|"re-exports"| node_device_ops
node_vm -->|"implements"| node_vm_state
node_flow_ops -->|"uses"| node_stack_helpers
node_stack_ops -->|"uses"| node_stack_helpers
node_memory_ops -->|"uses"| node_stack_helpers
node_algebra_ops -->|"uses"| node_stack_helpers
node_immediate -->|"uses"| node_stack_helpers
node_device_ops -.->|"invokes device"| node_device_contract
node_console_device -->|"writes characters"| node_external_output

click node_public_api "https://github.com/kichman/vmjs/blob/main/src/index.ts"
click node_vm "https://github.com/kichman/vmjs/blob/main/src/uxn-vm.ts"
click node_opcode_table "https://github.com/kichman/vmjs/blob/main/src/opcode-table.ts"
click node_immediate "https://github.com/kichman/vmjs/blob/main/src/opcodes/immediate-ops.ts"
click node_opcode_exports "https://github.com/kichman/vmjs/blob/main/src/opcodes/index.ts"
click node_flow_ops "https://github.com/kichman/vmjs/blob/main/src/opcodes/flow-ops.ts"
click node_stack_ops "https://github.com/kichman/vmjs/blob/main/src/opcodes/stack-ops.ts"
click node_memory_ops "https://github.com/kichman/vmjs/blob/main/src/opcodes/mem-ops.ts"
click node_algebra_ops "https://github.com/kichman/vmjs/blob/main/src/opcodes/algebra-ops.ts"
click node_device_ops "https://github.com/kichman/vmjs/blob/main/src/opcodes/dev-ops.ts"
click node_vm_state "https://github.com/kichman/vmjs/blob/main/src/uxn-types.ts"
click node_stack_helpers "https://github.com/kichman/vmjs/blob/main/src/stack.ts"
click node_device_contract "https://github.com/kichman/vmjs/blob/main/src/uxn-types.ts"
click node_console_device "https://github.com/kichman/vmjs/blob/main/src/ConsoleDevice.ts"

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_public_api,node_vm,node_opcode_table toneBlue
class node_immediate,node_opcode_exports,node_flow_ops,node_stack_ops,node_memory_ops,node_algebra_ops,node_device_ops toneAmber
class node_vm_state,node_stack_helpers toneMint
class node_device_contract,node_console_device toneRose
class node_caller,node_external_output toneIndigo
```

## Components

### Execution core

- [Public API](https://github.com/kichman/vmjs/blob/main/src/index.ts): `src/index.ts`
- [Uxn runtime](https://github.com/kichman/vmjs/blob/main/src/uxn-vm.ts): `src/uxn-vm.ts`
- [Base opcode table](https://github.com/kichman/vmjs/blob/main/src/opcode-table.ts): `src/opcode-table.ts`

### Opcode operations

- [LIT and DEC](https://github.com/kichman/vmjs/blob/main/src/opcodes/immediate-ops.ts): `src/opcodes/immediate-ops.ts`
- [Opcode exports](https://github.com/kichman/vmjs/blob/main/src/opcodes/index.ts): `src/opcodes/index.ts`
- [Flow operations](https://github.com/kichman/vmjs/blob/main/src/opcodes/flow-ops.ts): `src/opcodes/flow-ops.ts`
- [Stack operations](https://github.com/kichman/vmjs/blob/main/src/opcodes/stack-ops.ts): `src/opcodes/stack-ops.ts`
- [Memory operations](https://github.com/kichman/vmjs/blob/main/src/opcodes/mem-ops.ts): `src/opcodes/mem-ops.ts`
- [Algebra operations](https://github.com/kichman/vmjs/blob/main/src/opcodes/algebra-ops.ts): `src/opcodes/algebra-ops.ts`
- [Device operations](https://github.com/kichman/vmjs/blob/main/src/opcodes/dev-ops.ts): `src/opcodes/dev-ops.ts`

### VM state

- [VM state contract](https://github.com/kichman/vmjs/blob/main/src/uxn-types.ts): `src/uxn-types.ts`
- [Stack helpers](https://github.com/kichman/vmjs/blob/main/src/stack.ts): `src/stack.ts`

### Device I/O

- [Device contract](https://github.com/kichman/vmjs/blob/main/src/uxn-types.ts): `src/uxn-types.ts`
- [Console device](https://github.com/kichman/vmjs/blob/main/src/ConsoleDevice.ts): `src/ConsoleDevice.ts`

### Other components

- **Program caller**
- **Console output**

## Connections

- Program caller → Public API: uses (evidence: `README.md`)
- Public API → Uxn runtime: exports (evidence: `src/index.ts`)
- Public API → Console device: exports (evidence: `src/index.ts`)
- Uxn runtime → Base opcode table: dispatches base ops (evidence: `src/uxn-vm.ts`)
- Uxn runtime → LIT and DEC: dispatches immediate ops (evidence: `src/uxn-vm.ts`)
- Base opcode table → Opcode exports: imports handlers (evidence: `src/opcode-table.ts`)
- Opcode exports → Flow operations: re-exports (evidence: `src/opcodes/index.ts`)
- Opcode exports → Stack operations: re-exports (evidence: `src/opcodes/index.ts`)
- Opcode exports → Memory operations: re-exports (evidence: `src/opcodes/index.ts`)
- Opcode exports → Algebra operations: re-exports (evidence: `src/opcodes/index.ts`)
- Opcode exports → Device operations: re-exports (evidence: `src/opcodes/index.ts`)
- Uxn runtime → VM state contract: implements (evidence: `src/uxn-vm.ts`)
- Flow operations → Stack helpers: uses (evidence: `src/opcodes/flow-ops.ts`)
- Stack operations → Stack helpers: uses (evidence: `src/opcodes/stack-ops.ts`)
- Memory operations → Stack helpers: uses (evidence: `src/opcodes/mem-ops.ts`)
- Algebra operations → Stack helpers: uses (evidence: `src/opcodes/algebra-ops.ts`)
- LIT and DEC → Stack helpers: uses (evidence: `src/opcodes/immediate-ops.ts`)
- Device operations → Device contract: invokes device (evidence: `src/opcodes/dev-ops.ts`)
- Console device → Console output: writes characters (evidence: `src/ConsoleDevice.ts`)
