Architecture
Regenerator 2000 is an interactive disassembler for 8-bit Commodore computers (C64, C128, VIC-20, Plus/4, PET, 1541), written in Rust. It follows a unidirectional data flow architecture where user events modify the application state through commands, triggering re-analysis and re-rendering of the view.
High-Level Overview
flowchart TD
subgraph bin [regenerator2000 Crate - CLI]
Main[main.rs]
end
subgraph tui_crate [regenerator2000-tui Crate - TUI]
Input[User Input]
EventLoop[Event Loop]
Widget[Active Widget<br/>View/Dialog]
Renderer[TUI Renderer]
UIState[UI State]
end
subgraph core_crate [regenerator2000-core Crate - Engine]
Core[Core Hub]
Action[AppAction]
CommandSys[Command System]
AppState[Application State]
CoreViewState[Core View State]
Analyzer[Code Analyzer]
DisasmEngine[Disassembly Engine]
ViceClient[VICE Client]
MCPServer[MCP Server<br/>HTTP/Stdio]
Unpacker[Binary Unpacker]
end
subgraph External [External Interface]
MCPClient[MCP Client / AI Agent]
VICE[VICE Emulator]
end
Main -->|Initializes| Core
Main -->|Initializes| EventLoop
Input -->|Handled by| Widget
EventLoop -->|Drives| Renderer
Widget -->|AppAction| Action
Action -->|apply_action| Core
MCPClient -->|Tools/Resources| MCPServer
MCPServer -->|AppAction| Core
MCPServer -.->|Read State| AppState
Core -->|Dispatch| CommandSys
Core -->|Direct Mutate| CoreViewState
Core -->|UnpackStarted Event| EventLoop
EventLoop -->|Spawns background| Unpacker
Unpacker -.->|Loads unpacked PRG| AppState
CommandSys -->|Apply/Undo| AppState
AppState -->|Requests| DisasmEngine
AppState -->|Triggers| Analyzer
CoreViewState -.->|Embedded via Deref| UIState
UIState -->|Provides Context| Renderer
AppState -->|Provides Data| Renderer
DisasmEngine -->|Generates Lines| Renderer
VICE <-->|Binary Protocol| ViceClient
ViceClient -.-> AppState
Workspace Structure
The project is organized as a Cargo workspace with three primary components:
regenerator2000-core: The head-less engine. Contains all memory management, disassembly logic, CPU tables, analysis heuristics, cross-frontend view state, and the MCP server.regenerator2000-tui: The TUI library. Implements theratatuiwidgets, event loop coordination, and theme system.regenerator2000(root): The binary crate. Provides the CLI entry point, initializes the terminal, and links the core engine with the TUI frontend.
Core Components
1. Application State & Logic (regenerator2000-core/src/state/)
The core engine state, organized across multiple modules:
core.rs: The centralCorehub. Orchestrates persistent state (AppState) and transient view state (CoreViewState). Frontends interact with it viaapply_action().-
app_state.rs: The mainAppStatestruct that holds the runtime data hub. Contains the Undo Stack, Disassembly Cache, scopes, and connection state for VICE. -
view_state.rs: DefinesCoreViewState— the frontend-agnostic representation of cursor positions, selections, and active panes. -
actions.rs: Defines theAppActionenum — semantic actions that any frontend (TUI, GUI, Web, MCP) can produce. -
blocks.rs: Block management logic (Code, Data, Text, etc.) and memory layout queries. -
disassembly.rs: Disassembly orchestration and line-index lookups. -
file_io.rs: Loading and importing of various formats intoAppState. -
navigation.rs: Pure navigation helpers (jumping to addresses, creating save contexts) that operate onAppState+CoreViewState. -
project.rs: TheProjectStatestruct — the persistent part of the state saved to.regen2000projfiles (includes labels, comments, blocks, scopes, and enums). -
settings.rs: Document-level settings (assembler, system, display preferences, fill run threshold). -
search.rs: Centralized search logic (hex, text, PETSCII). -
types.rs: Core type definitions used across the workspace (Addr,System,BlockType,Assembler,LabelType, etc.). event.rs: DefinesCoreEvent(state changes, dialog requests, status messages) andDialogType— the frontend-agnostic event vocabulary returned byCore::apply_action().
2. Disassembly Engine (regenerator2000-core/src/disassembler/)
Responsible for converting raw bytes into human-readable assembly code based on the state.
-
disassembler.rs: The main driver. It iterates through the raw data, respectingBlockTypedefinitions, and produces a list ofDisassemblyLines. -
context.rs: TheDisassemblyContextstruct that bundles all data needed for a disassembly pass (binary data, block types, labels, comments, cross-refs, analysis hints, etc.). -
handlers.rs: Helper functions for disassembling specific block types (e.g., data bytes, words, addresses, text, screencodes). -
formatter.rs: A trait abstracting the differences between assembler syntaxes. -
formatter_acme.rs: ACME assembler implementation. -
formatter_64tass.rs: 64tass assembler implementation. -
formatter_ca65.rs: ca65 (cc65 suite) assembler implementation. -
formatter_kickasm.rs: KickAssembler implementation.
3. CPU Model (regenerator2000-core/src/cpu.rs)
Provides the domain model for the MOS 6502/6510 CPU.
Opcode: Definitions of all supported opcodes, including cycle counts, addressing modes, and descriptions.AddressingMode: Enum defining the different addressing modes (Absolute, ZeroPage, Immediate, etc.). Used by both the Disassembler (to decode instructions) and the Analyzer (to understand control flow).
4. Command System (regenerator2000-core/src/commands.rs)
Implements the Command Pattern. Granular actions (e.g., SetBlockType, SetLabel) are encapsulated as Structs that
know how to:
- Apply: Execute the change on
AppState. - Undo: Revert the change. This enables robust Undo/Redo functionality and ensures state consistency.
5. Analyzer (regenerator2000-core/src/analyzer.rs)
A heuristic engine that runs after state changes. It:
- Traces code paths (following JMPs and branches).
- Identifies referenced addresses.
- Identifies and marks fill sequences based on the "Fill run threshold" setting.
- Auto-generates labels (e.g.,
s_C000,j_0400,zpf_A0) based on usage context (subroutine, branch, jump, pointer, field). See Analysis — Label Prefixes for the complete prefix reference.
6. Parser (regenerator2000-core/src/parser/)
Handles importing various Commodore file formats and label files.
parser.rs: Module re-exports for all parser sub-modules.prg.rs: Parser for standard Commodore PRG files (2-byte load address header). Also parses embedded BASIC SYS addresses to suggest entry points.crt.rs: Parser for Commodore 64 cartridge (.crt) files with multi-bank chip selection.d64.rs: Unified parser for D64 (35/40/42-track), D71 (70/80-track), and D81 disk image files. Supports file extraction from 1541/1571/1581 disk images.t64.rs: Parser for T64 tape archive files.-
dis65.rs: Parser for 6502bench SourceGen (.dis65) project files. -
vice_lbl.rs: Parser for VICE label files (.lbl) for importing debug symbols. -
vice_vsf.rs: Parser for VICE snapshot files (.vsf). Auto-detects the system from the VSF header.
These parsers allow Regenerator 2000 to load programs from multiple source formats (PRG, CRT, D64/D71/D81, T64, VSF, DIS65) and import debugging symbols from VICE emulator sessions.
7. Exporter (regenerator2000-core/src/exporter/)
Handles generation of complete, compilable source code and browsable HTML disassembly files.
-
asm.rs: Exports disassembly as a compilable assembly source file. Supports all four assembler formats (ACME, 64tass, ca65, KickAssembler) via theFormattertrait, and handles external-file (incbin) regions. -
html.rs: Exports disassembly as a self-contained, syntax-highlighted HTML file with clickable cross-reference hyperlinks, light/dark theme toggle, and assembler-specific build instructions in the header.ExternalFileregions are written to separate linked HTML files. -
verify.rs: Export→assemble→diff roundtrip verification. Exports ASM, invokes the real assembler binary, and byte-compares the output against the original binary to confirm disassembly correctness. Supports all four assemblers.
8. Binary Unpacker (regenerator2000-core/src/unpacker.rs)
Provides a accurate 6502 emulation sandbox to automatically decompress packed Commodore 64 programs.
- Two-Phase Emulation: Simulates the packer's decryption stub (Phase 1) and runs the main decompression routine ( Phase 2), tracking memory writes to extract clean, decompressed program data.
- Advanced Sandboxing: Intercepts Kernal and BASIC ROM vectors (such as
GETIN,CHROUT,CLRSCR) to bypass I/O calls, prevent infinite loops, and handle sophisticated decompression stubs natively. - Background Execution: Runs asynchronously on a separate thread with real-time instruction counters sent to the TUI status bar for responsiveness during heavy unpacking jobs.
9. UI Architecture
The UI is built on crossterm and ratatui with a custom Widget trait abstraction.
WidgetTrait (regenerator2000-tui/src/ui/widget.rs): Defines the interface for all UI components (Views, Dialogs, Menu, StatusBar).
pub trait Widget {
fn render(&self, f: &mut Frame, area: Rect, app_state: &AppState, ui_state: &mut UIState);
fn handle_input(&mut self, key: KeyEvent, app_state: &mut AppState, ui_state: &mut UIState) -> WidgetResult;
// Default implementation returns WidgetResult::Ignored
fn handle_mouse(&mut self, mouse: MouseEvent, app_state: &mut AppState, ui_state: &mut UIState) -> WidgetResult;
}
- Core UI Components:
main.rs: Initializes the terminal and event loop.-
events.rs: The primary event loop and rendering coordinator. Synchronizes view states and manages the main application loop. -
events/input.rs: The input router. It determines the active pane and dispatches input events (keyboard and mouse) to the correspondingWidget. ui.rs: The top-level layout engine. It renders the Menu, MinimapBar, StatusBar, and the active Main View.-
minimap_bar.rs: Top-level horizontal memory map overview widget with IDA Pro-style colored blocks. -
statusbar.rs: Bottom status bar showing cursor address, block type, and context info. -
navigable.rs: Shared trait/helpers for views that support cursor-based navigation. -
graphics_common.rs: Shared rendering logic for graphical views (sprites, charset, bitmap). -
Menu System (
regenerator2000-tui/src/ui/menu/): The menu bar is split across several sub-modules: -
mod.rs: TheMenustruct implementingWidget, handling keyboard and mouse interaction with the menu bar and popup menus. -
menu_action.rs: Action dispatch logic — routesAppActionvariants toCommandapplications, dialog creation, and other side effects. -
menu_model.rs: Data model for the menu system:MenuState,MenuCategory, andMenuItemstructs with keyboard shortcut bindings. -
menu_render.rs: Rendering functions for the menu bar and popup menus. -
Main Views (
regenerator2000-tui/src/ui/view_*.rs): -
view_disassembly.rs: The primary disassembly listing view with syntax highlighting and navigation. -
view_hexdump.rs: Hexadecimal dump view with multiple display modes (PETSCII, Screencode). -
view_sprites.rs: Visual sprite editor/viewer for C64 sprite data. -
view_charset.rs: Character set editor/viewer for font data. -
view_bitmap.rs: Bitmap graphics viewer for hires and multicolor bitmaps. -
view_blocks.rs: Block type overview showing the memory layout. -
view_debugger.rs: Live debugging view for VICE integration. -
Dialogs (
regenerator2000-tui/src/ui/dialog_*.rs): -
dialog_about.rs: About/help dialog. -
dialog_apply_enum.rs: Apply a custom enum definition to a data block or instruction operand. -
dialog_bookmarks.rs: Bookmark manager for navigating saved addresses. -
dialog_breakpoint_address.rs: Set breakpoint address for VICE debugging. -
dialog_comment.rs: Add/edit comments. -
dialog_complete_address.rs: Complete missing byte for Hi/Lo or Lo/Hi address packing when only one immediate value is available. -
dialog_confirmation.rs: Generic confirmation dialog. -
dialog_crt_picker.rs: CRT cartridge chip/bank picker for selecting which chip to load from multi-chip cartridges. -
dialog_d64_picker.rs: D64 disk image file picker for loading programs from disk images. -
dialog_document_settings.rs: Project-level settings editor. -
dialog_edit_enum.rs: Create, update, or delete key-value mappings within a custom enum definition. -
dialog_export_as.rs: Export source code dialog (ASM or HTML). -
dialog_export_labels.rs: Export labels to VICE format. -
dialog_find_references.rs: Find cross-references to an address. -
dialog_import_context.rs: Import Context Setup dialog. Shown when loading a raw binary (no PRG header) to let the user configure the target system, binary origin address, entry-point address, and whether to auto-disassemble the code sequence from that entry point. Displays a high-entropy warning when the binary may be packed or compressed. -
dialog_go_to_symbol.rs: Navigate to a label by name. -
dialog_jump_to_address.rs: Jump to a specific memory address. -
dialog_jump_to_line.rs: Jump to a specific line number. -
dialog_keyboard_shortcut.rs: Keyboard shortcuts reference. -
dialog_label.rs: Add/edit labels. -
dialog_manage_enums.rs: Manage the project's custom enum definitions (create, rename, delete). -
dialog_memory_dump_address.rs: Set memory dump address for the VICE debugger panel. -
dialog_open.rs: Open file browser. -
dialog_open_recent.rs: Open recent projects list. -
dialog_origin.rs: Set the load address. -
dialog_save_as.rs: Save project dialog. -
dialog_search.rs: Search for bytes or text. -
dialog_settings.rs: Application-level settings. -
dialog_t64_picker.rs: T64 tape archive file picker for selecting which entry to load. -
dialog_vice_connect.rs: Configures connection to VICE's remote monitor. -
dialog_warning.rs: Generic warning dialog for displaying important messages to the user. -
dialog_watchpoint_address.rs: Set watchpoint address for VICE debugging (memory read/write breakpoints). -
UI State Management (
regenerator2000-tui/src/ui_state.rs) : The TUI-specific interface state. It embeds aCoreViewStatevia the.corefield and usesDeref/DerefMutto allow direct access to core view state (likecursor_index) from the UI layer. - Active Dialog:
Option<Box<dyn Widget>>allowing modal dialogs to take over input and rendering. - Theme: Current color theme for the TUI.
- Layout Areas: Cached rectangles for mouse interaction detection.
- TUI Widgets:
status_bar,menu, and list states for various side-panels.
10. Theme System (regenerator2000-tui/src/theme.rs & [
theme_file.rs](https://github.com/ricardoquesada/regenerator2000/blob/main/crates/regenerator2000-tui/src/theme_file.rs))
Provides customizable color schemes for the UI.
theme.rs: Defines color palettes for different UI elements (dialogs, menus, status bar, syntax highlighting). Loads themes by name.-
theme_file.rs: Parses TOML-based theme definition files (theme-*.toml). Built-in themes are embedded as assets; users can override or create custom themes by placing TOML files in the config directory. - Supports multiple built-in themes (Dracula, Nord, Catppuccin, Gruvbox, etc.).
11. Configuration (regenerator2000-core/src/config.rs)
Manages application-level configuration that persists across sessions.
SystemConfig: User preferences including:- Theme selection
- View synchronization settings (sync hex dump, sprites, charset, bitmap, blocks with disassembly)
- Auto-analyze toggle
- Entropy threshold for analysis
- Recent projects list
- Update checking preference
- Stored as
config.tomlin the OS-specific config directory (migrated from JSON in v0.9.13). Stored separately from project state to maintain user preferences across different projects.
12. Assets (regenerator2000-core/src/assets.rs)
Manages embedded system definition files (system-*.toml) and theme files (theme-*.toml).
System files provide labels, comments, and exclude-address lists for each supported machine.
Theme files define color palettes for the TUI. Both support user overrides from the config directory.
13. MCP Server (regenerator2000-core/src/mcp/)
Implements the Model Context Protocol (MCP) server for programmatic access to Regenerator 2000.
mod.rs: Module definition.-
handler.rs: Core request handler implementing all MCP tools and resources. Routes tool calls to the appropriateAppStatecommands. http.rs: HTTP transport using Server-Sent Events (SSE) on port 3000 for real-time communication.-
stdio.rs: Stdio transport mode for headless subprocess MCP communication (e.g., Claude Desktop, Gemini CLI). -
types.rs: Shared type definitions for MCP request/response structures.
The MCP server exposes tools and resources allowing AI agents to:
- Tools: Manipulate disassembly (set labels, comments, block types), search memory, manage cross-references, save projects, and perform undo/redo operations.
- Resources: Access binary data, disassembly views, hexdump views, and selected regions.
This enables collaborative human-AI workflows where both can work on the same project simultaneously (HTTP mode) or fully automated analysis sessions (stdio mode).
14. VICE Integration (regenerator2000-core/src/vice/)
Provides live debugging integration with the VICE emulator.
-
client.rs:ViceClientthat manages the TCP connection to VICE's remote monitor, sending commands and receiving events. -
protocol.rs: DefinesViceCommandandViceMessagetypes for the VICE binary monitor protocol. -
state.rs:ViceStatetracking the debugger connection status, CPU registers, breakpoints, and run/stop state. -
c64_hardware.rs:Vic2StateandCiaStatestructs for reading and displaying C64 hardware register values during debugging.
15. Utilities (regenerator2000-tui/src/utils.rs & regenerator2000-core/src/utils.rs)
Contains shared helper functions and utilities used across the application, split between TUI and core logic.
Data Flow
- Input: User presses a key (e.g.,
C) or interacts with the mouse. - Dispatch:
regenerator2000-tui/src/events/input.rsroutes the input to the activeWidget. - Action: The Widget processes the input and returns an
AppAction(e.g.,AppAction::Code). - Core Application: The TUI calls
Core::apply_action(action). - Execution:
Coreconverts the action into aCommand(e.g.,SetBlockType), pushes to theUndoStack, and applies it toAppState. - Side Effects:
Coreexecutes the logic, potentially triggeringanalyzer.rsor regenerating the disassembly. - Events:
Core::apply_actionreturns a list ofCoreEvents (e.g.,StateChanged,DialogRequested). - UI Sync: The TUI processes these events, updating
UIState(opening dialogs, syncing cursors, setting status messages). - Render: The main loop calls
ui::draw(), which renders the TUI from the updatedAppStateandUIState.
Persistence
Projects are saved as JSON files (.regen2000proj).
- Structure: Serializes the
ProjectStatestruct. - Efficiency:
- Raw data is gzip-compressed and base64-encoded to reduce file size.
- Block types use run-length encoding to compress long sequences of the same type.
- Scopes: Saved as start/end address pairs to represent named memory regions (.proc, .block, .namespace, etc).
- Portability: Designed to be portable across different machines, storing relative paths where possible.
- Session State: Cursor positions and view settings are saved with the project for seamless session restoration.