# firatkucuk/f6502 architecture

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

- Interactive diagram: https://gitdiagram.com/firatkucuk/f6502
- Repository: https://github.com/firatkucuk/f6502

## Overview

f6502 is a TypeScript browser-based 6502/C64 emulator, with an additional C64Script compiler for creating games. A browser user supplies ROMs and optionally a program, disk, or tape; the player drives the emulated machine and presents video/audio. The machine combines the CPU, memory, and C64 devices, while disk and tape support provide KERNAL-level media access. C64Script follows a source-to-PRG compilation path. Relationships for unsampled modules are limited to README statements and SOURCE INDEX references; the graph omits unverified internal wiring.

Main entry points: [`src/machine/c64.ts`](https://github.com/firatkucuk/f6502/blob/main/src/machine/c64.ts) (C64 machine), [`src/browser/player.ts`](https://github.com/firatkucuk/f6502/blob/main/src/browser/player.ts) (Player), [`src/c64script/index.ts`](https://github.com/firatkucuk/f6502/blob/main/src/c64script/index.ts) (Compiler API), [`src/machine/drive.ts`](https://github.com/firatkucuk/f6502/blob/main/src/machine/drive.ts) (Disk drive)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_browser["Browser runtime"]
  node_player["Player<br/>[player.ts]"]
  node_loader["Source loader<br/>[loader.ts]"]
  node_keys["Keyboard mapping<br/>[key-map.ts]"]
  node_keyqueue["Key queue<br/>[key-queue.ts]"]
  node_clock["Frame clock<br/>[clock.ts]"]
  node_audio["Audio playback<br/>[audio.ts]"]
end

subgraph group_emulator["Emulated machine"]
  node_c64["C64 machine<br/>[c64.ts]"]
  node_cpu["6502 CPU<br/>[cpu.ts]"]
  node_opcodes["Opcode definitions<br/>[opcodes.ts]"]
  node_memory["Memory and ROMs<br/>[memory.ts]"]
  node_cia["CIA devices<br/>[cia.ts]"]
  node_keyboard["C64 keyboard<br/>[keyboard.ts]"]
  node_vic["VIC-II video<br/>[vic.ts]"]
  node_sid["SID sound<br/>[sid.ts]"]
end

subgraph group_media["Media support"]
  node_drive["Disk drive<br/>[drive.ts]"]
  node_d64[("D64 disk image<br/>[d64.ts]")]
  node_sources["Disk source detection<br/>[sources.ts]"]
  node_tape["Datasette<br/>[datasette.ts]"]
  node_kernal["KERNAL disk traps<br/>[kernal-traps.ts]"]
end

subgraph group_compiler["C64Script compiler"]
  node_scriptapi["Compiler API<br/>[index.ts]"]
  node_lexer["Lexer<br/>[lexer.ts]"]
  node_parser["Parser and AST<br/>[parser.ts]"]
  node_ast["Language model<br/>[ast.ts]"]
  node_checker["Type checker<br/>[checker.ts]"]
  node_codegen["6502 code generator<br/>[codegen.ts]"]
  node_linker["Program linker<br/>[linker.ts]"]
  node_prgwriter["PRG writer<br/>[prg-writer.ts]"]
end

node_user(("Browser user"))
node_api["Public API<br/>[index.ts]"]

node_user -->|"starts emulator"| node_player
node_api -->|"exports"| node_player
node_player -->|"loads sources"| node_loader
node_player -->|"runs machine"| node_c64
node_player -->|"maps input"| node_keys
node_player -->|"pumps keys"| node_keyqueue
node_player -->|"schedules frames"| node_clock
node_player -->|"pumps audio"| node_audio
node_keys -->|"targets C64 keys"| node_keyboard
node_keyqueue -->|"dispatches keys"| node_keyboard
node_clock -->|"uses frame timing"| node_vic
node_c64 -->|"integrates CPU"| node_cpu
node_cpu -->|"uses instruction definitions"| node_opcodes
node_c64 -->|"integrates memory"| node_memory
node_c64 -->|"integrates CIAs"| node_cia
node_c64 -->|"integrates keyboard"| node_keyboard
node_c64 -->|"integrates video"| node_vic
node_c64 -->|"integrates sound"| node_sid
node_c64 -->|"provides drive"| node_drive
node_c64 -->|"provides datasette"| node_tape
node_drive -->|"opens disk source"| node_sources
node_drive -->|"uses disk image"| node_d64
node_player -->|"mounts disk source"| node_sources
node_kernal -.->|"serves disk commands"| node_drive
node_api -->|"exports compiler"| node_scriptapi
node_scriptapi -->|"tokenizes source"| node_lexer
node_scriptapi -->|"parses source"| node_parser
node_parser -->|"builds language model"| node_ast
node_scriptapi -->|"checks program"| node_checker
node_checker -->|"checks declarations"| node_ast
node_scriptapi -->|"generates machine code"| node_codegen
node_codegen -->|"generates from model"| node_ast
node_codegen -->|"uses opcode definitions"| node_opcodes
node_scriptapi -->|"links output"| node_linker
node_linker -->|"consumes generated code"| node_codegen
node_scriptapi -->|"writes PRG"| node_prgwriter

click node_api "https://github.com/firatkucuk/f6502/blob/main/src/index.ts"
click node_player "https://github.com/firatkucuk/f6502/blob/main/src/browser/player.ts"
click node_loader "https://github.com/firatkucuk/f6502/blob/main/src/browser/loader.ts"
click node_keys "https://github.com/firatkucuk/f6502/blob/main/src/browser/key-map.ts"
click node_keyqueue "https://github.com/firatkucuk/f6502/blob/main/src/browser/key-queue.ts"
click node_clock "https://github.com/firatkucuk/f6502/blob/main/src/browser/clock.ts"
click node_audio "https://github.com/firatkucuk/f6502/blob/main/src/browser/audio.ts"
click node_c64 "https://github.com/firatkucuk/f6502/blob/main/src/machine/c64.ts"
click node_cpu "https://github.com/firatkucuk/f6502/blob/main/src/core/cpu.ts"
click node_opcodes "https://github.com/firatkucuk/f6502/blob/main/src/core/opcodes.ts"
click node_memory "https://github.com/firatkucuk/f6502/blob/main/src/machine/memory.ts"
click node_cia "https://github.com/firatkucuk/f6502/blob/main/src/machine/cia.ts"
click node_keyboard "https://github.com/firatkucuk/f6502/blob/main/src/machine/keyboard.ts"
click node_vic "https://github.com/firatkucuk/f6502/blob/main/src/machine/vic.ts"
click node_sid "https://github.com/firatkucuk/f6502/blob/main/src/machine/sid.ts"
click node_drive "https://github.com/firatkucuk/f6502/blob/main/src/machine/drive.ts"
click node_d64 "https://github.com/firatkucuk/f6502/blob/main/src/machine/d64.ts"
click node_sources "https://github.com/firatkucuk/f6502/blob/main/src/machine/sources.ts"
click node_tape "https://github.com/firatkucuk/f6502/blob/main/src/machine/datasette.ts"
click node_kernal "https://github.com/firatkucuk/f6502/blob/main/src/machine/kernal-traps.ts"
click node_scriptapi "https://github.com/firatkucuk/f6502/blob/main/src/c64script/index.ts"
click node_lexer "https://github.com/firatkucuk/f6502/blob/main/src/c64script/lexer.ts"
click node_parser "https://github.com/firatkucuk/f6502/blob/main/src/c64script/parser.ts"
click node_ast "https://github.com/firatkucuk/f6502/blob/main/src/c64script/ast.ts"
click node_checker "https://github.com/firatkucuk/f6502/blob/main/src/c64script/checker.ts"
click node_codegen "https://github.com/firatkucuk/f6502/blob/main/src/c64script/codegen.ts"
click node_linker "https://github.com/firatkucuk/f6502/blob/main/src/c64script/linker.ts"
click node_prgwriter "https://github.com/firatkucuk/f6502/blob/main/src/c64script/prg-writer.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_player,node_loader,node_keys,node_keyqueue,node_clock,node_audio,node_user toneBlue
class node_c64,node_cpu,node_opcodes,node_memory,node_cia,node_keyboard,node_vic,node_sid toneAmber
class node_drive,node_d64,node_sources,node_tape,node_kernal,node_api toneMint
class node_scriptapi,node_lexer,node_parser,node_ast,node_checker,node_codegen,node_linker,node_prgwriter toneRose
```

## Components

### Browser runtime

- [Player](https://github.com/firatkucuk/f6502/blob/main/src/browser/player.ts): `src/browser/player.ts`
- [Source loader](https://github.com/firatkucuk/f6502/blob/main/src/browser/loader.ts): `src/browser/loader.ts`
- [Keyboard mapping](https://github.com/firatkucuk/f6502/blob/main/src/browser/key-map.ts): `src/browser/key-map.ts`
- [Key queue](https://github.com/firatkucuk/f6502/blob/main/src/browser/key-queue.ts): `src/browser/key-queue.ts`
- [Frame clock](https://github.com/firatkucuk/f6502/blob/main/src/browser/clock.ts): `src/browser/clock.ts`
- [Audio playback](https://github.com/firatkucuk/f6502/blob/main/src/browser/audio.ts): `src/browser/audio.ts`

### Emulated machine

- [C64 machine](https://github.com/firatkucuk/f6502/blob/main/src/machine/c64.ts): `src/machine/c64.ts`
- [6502 CPU](https://github.com/firatkucuk/f6502/blob/main/src/core/cpu.ts): `src/core/cpu.ts`
- [Opcode definitions](https://github.com/firatkucuk/f6502/blob/main/src/core/opcodes.ts): `src/core/opcodes.ts`
- [Memory and ROMs](https://github.com/firatkucuk/f6502/blob/main/src/machine/memory.ts): `src/machine/memory.ts`
- [CIA devices](https://github.com/firatkucuk/f6502/blob/main/src/machine/cia.ts): `src/machine/cia.ts`
- [C64 keyboard](https://github.com/firatkucuk/f6502/blob/main/src/machine/keyboard.ts): `src/machine/keyboard.ts`
- [VIC-II video](https://github.com/firatkucuk/f6502/blob/main/src/machine/vic.ts): `src/machine/vic.ts`
- [SID sound](https://github.com/firatkucuk/f6502/blob/main/src/machine/sid.ts): `src/machine/sid.ts`

### Media support

- [Disk drive](https://github.com/firatkucuk/f6502/blob/main/src/machine/drive.ts): `src/machine/drive.ts`
- [D64 disk image](https://github.com/firatkucuk/f6502/blob/main/src/machine/d64.ts): `src/machine/d64.ts`
- [Disk source detection](https://github.com/firatkucuk/f6502/blob/main/src/machine/sources.ts): `src/machine/sources.ts`
- [Datasette](https://github.com/firatkucuk/f6502/blob/main/src/machine/datasette.ts): `src/machine/datasette.ts`
- [KERNAL disk traps](https://github.com/firatkucuk/f6502/blob/main/src/machine/kernal-traps.ts): `src/machine/kernal-traps.ts`

### C64Script compiler

- [Compiler API](https://github.com/firatkucuk/f6502/blob/main/src/c64script/index.ts): `src/c64script/index.ts`
- [Lexer](https://github.com/firatkucuk/f6502/blob/main/src/c64script/lexer.ts): `src/c64script/lexer.ts`
- [Parser and AST](https://github.com/firatkucuk/f6502/blob/main/src/c64script/parser.ts): `src/c64script/parser.ts`
- [Language model](https://github.com/firatkucuk/f6502/blob/main/src/c64script/ast.ts): `src/c64script/ast.ts`
- [Type checker](https://github.com/firatkucuk/f6502/blob/main/src/c64script/checker.ts): `src/c64script/checker.ts`
- [6502 code generator](https://github.com/firatkucuk/f6502/blob/main/src/c64script/codegen.ts): `src/c64script/codegen.ts`
- [Program linker](https://github.com/firatkucuk/f6502/blob/main/src/c64script/linker.ts): `src/c64script/linker.ts`
- [PRG writer](https://github.com/firatkucuk/f6502/blob/main/src/c64script/prg-writer.ts): `src/c64script/prg-writer.ts`

### Other components

- **Browser user**
- [Public API](https://github.com/firatkucuk/f6502/blob/main/src/index.ts): `src/index.ts`

## Connections

- Browser user → Player: starts emulator (evidence: `README.md`)
- Public API → Player: exports
- Player → Source loader: loads sources (evidence: `src/browser/player.ts`)
- Player → C64 machine: runs machine (evidence: `src/browser/player.ts`)
- Player → Keyboard mapping: maps input (evidence: `src/browser/player.ts`)
- Player → Key queue: pumps keys (evidence: `src/browser/player.ts`)
- Player → Frame clock: schedules frames (evidence: `src/browser/player.ts`)
- Player → Audio playback: pumps audio (evidence: `src/browser/player.ts`)
- Keyboard mapping → C64 keyboard: targets C64 keys (evidence: `src/browser/key-map.ts`)
- Key queue → C64 keyboard: dispatches keys (evidence: `src/browser/key-queue.ts`)
- Frame clock → VIC-II video: uses frame timing (evidence: `src/browser/clock.ts`)
- C64 machine → 6502 CPU: integrates CPU (evidence: `src/machine/c64.ts`)
- 6502 CPU → Opcode definitions: uses instruction definitions (evidence: `src/core/cpu.ts`)
- C64 machine → Memory and ROMs: integrates memory (evidence: `src/machine/c64.ts`)
- C64 machine → CIA devices: integrates CIAs (evidence: `src/machine/c64.ts`)
- C64 machine → C64 keyboard: integrates keyboard (evidence: `src/machine/c64.ts`)
- C64 machine → VIC-II video: integrates video (evidence: `src/machine/c64.ts`)
- C64 machine → SID sound: integrates sound (evidence: `src/machine/c64.ts`)
- C64 machine → Disk drive: provides drive (evidence: `src/machine/c64.ts`)
- C64 machine → Datasette: provides datasette (evidence: `src/machine/c64.ts`)
- Disk drive → Disk source detection: opens disk source (evidence: `src/machine/drive.ts`)
- Disk drive → D64 disk image: uses disk image (evidence: `src/machine/drive.ts`)
- Player → Disk source detection: mounts disk source (evidence: `src/browser/player.ts`)
- KERNAL disk traps → Disk drive: serves disk commands
- Public API → Compiler API: exports compiler (evidence: `src/index.ts`)
- Compiler API → Lexer: tokenizes source (evidence: `src/c64script/index.ts`)
- Compiler API → Parser and AST: parses source (evidence: `src/c64script/index.ts`)
- Parser and AST → Language model: builds language model (evidence: `src/c64script/parser.ts`)
- Compiler API → Type checker: checks program (evidence: `src/c64script/index.ts`)
- Type checker → Language model: checks declarations (evidence: `src/c64script/checker.ts`)
- Compiler API → 6502 code generator: generates machine code (evidence: `src/c64script/index.ts`)
- 6502 code generator → Language model: generates from model (evidence: `src/c64script/codegen.ts`)
- 6502 code generator → Opcode definitions: uses opcode definitions (evidence: `src/c64script/codegen.ts`)
- Compiler API → Program linker: links output (evidence: `src/c64script/index.ts`)
- Program linker → 6502 code generator: consumes generated code (evidence: `src/c64script/linker.ts`)
- Compiler API → PRG writer: writes PRG (evidence: `src/c64script/index.ts`)
