# robalb/x86-64-playground architecture

> Architecture diagram of the GitHub repository robalb/x86-64-playground, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/robalb/x86-64-playground
- Repository: https://github.com/robalb/x86-64-playground

## Overview

This project is an innovative, client‐side x86-64 playground that allows users to write, test, and debug assembly code directly from their browsers. Essentially, it ties together a web-based user interface built with Svelte and Typescript and a heavily modified, WebAssembly port of the Blink x86-64-linux emulator. Its purpose is to provide an interactive, GDB-like debugging experience as well as an interface for testing assembly snippets in a fully-emulated Linux environment—all without relying on a traditional server backend.

##### Main Components
- Web UI / Frontend:
- Located in the “webapp” directory, this single-page application is built using Svelte and Typescript.
- It includes various components such as the Editor, Disassembler, Registers, Hexdump, Terminal, and several control menus (e.g., file menu, share dialog, theme/debug controls).
- Assets like CSS, configuration files (jsconfig.json, vite.config.js), and static resources (index.html, images, fonts) are also part of this layer.
- Emulator / WebAssembly Module:
- Found in the “blink” directory, this is the core x86-64 emulator source code written in C, which is compiled to WebAssembly.
- Integration files include build scripts, Makefiles, and the tool wrapper (in “blink/tool”) that transform the Blink emulator into a C library readily accessible from JavaScript.
- The Typescript wrapper (e.g., in webapp/src/core/blink.ts) exposes the emulator’s API to the web application.
- Build and Tooling Infrastructure:
- Various scripts (compile_blink.sh, compile_demo_programs.sh, etc.) and directories (“blink/tool”, “musl_binutils”) support building both the emulator (Blink) and auxiliary demo programs.
- The “docs” and “field_notes” directories, along with third-party subdirectories, provide design documentation, testing frameworks, and examples demonstrating how everything integrates.
- Demo Programs and Testing:
- The “demo_programs” directory includes sample assembly programs used to validate and showcase the functionality of the emulator.

##### Relationships & Interactions
- User Interaction:
- The user writes or uploads assembly code using the editor in the Web UI.
- From there, the snippet is sent to the integrated emulator via the Typescript API.
- Communication with the Emulator:
- The web application loads the Blink emulator as a WebAssembly module.
- A bridge (the “webapp/src/core” folder with files for managing app state, assembling input, and interfacing with blink.ts) handles the bi-directional communication between the UI and emulator.
- Data Flow:
- Code Input → Compiler/Assembler (via configured assemblers like GNU Binutils, Fasm, Nasm from webapp assets) → WebAssembly module (Blink emulator) → Execution results (registers, memory state, disassembled code) → Rendered on the UI components.
- Offline Deployment:
- Being a static, single-page application hosted on GitHub Pages, the entire product, including the emulator core, runs entirely in the client’s browser.

##### Architectural Patterns
- Single Page Application (SPA): The entire web front end operates as a static SPA.
- WebAssembly Integration: The Blink emulator is encapsulated as a WebAssembly module, exposing its API via a TS wrapper.
- Modular Design: Separation of concerns is evident from distinct directories for UI (webapp), emulator source (blink), build tooling, and third-party libraries.
- Build Pipelines: The presence of Makefiles, shell scripts, and configuration files hints at a multi-step custom build system that compiles C code into the WASM binary, bundles it, and integrates with the web UI.

##### Relevant Technologies & Frameworks
- Frontend: Svelte, Typescript, Vite (for building the webapp), along with CSS and standard JavaScript.
- Backend Emulation: C source code compiled using Makefiles and shell scripts, and then transformed into a WebAssembly module.
- Supporting Libraries and Tools: Blink itself (originally a separate x86-64 emulator), musl, various third-party projects (e.g., qemu assets, libz, sectorlisp, ltp tests) that support comprehensive testing and functionality.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interaction
    U("User"):::user

    %% Web UI / Frontend Subgraph
    subgraph "Web UI (Svelte Frontend)"
        UI_App("App Shell (App.svelte)"):::frontend
        UI_Editor("Editor Component (Editor.svelte)"):::frontend
        UI_Disassembler("Disassembler Component (Disassembler.svelte)"):::frontend
        UI_Registers("Registers Component (Registers.svelte)"):::frontend
        UI_Hexdump("Hexdump Component (Hexdump.svelte)"):::frontend
        UI_Terminal("Terminal Component (Terminal.svelte)"):::frontend
        UI_ThemeDebug("Theme&Debug (ThemeDebug.svelte)"):::frontend
        UI_CoreBridge("Core Bridge (blink.ts)"):::frontend
    end

    %% WASM Emulator Subgraph
    subgraph "WASM Emulator (Blink)"
        EM_Core("Emulator Core (blink.c)"):::emulator
        EM_Make("Build Config (Makefile)"):::emulator
    end

    %% Build & Tooling Pipeline Subgraph
    subgraph "Build & Tooling Pipeline"
        Build_1("compile_blink.sh"):::build
        Build_2("compile_demo_programs.sh"):::build
        Build_3("compile_musl_binutils.sh"):::build
        Build_Tool("Blink Tools (blink/tool)"):::build
    end

    %% Demo Programs & Testing Subgraph
    subgraph "Demo Programs & Testing"
        Demo_Programs("Demo Programs (demo_programs)"):::demo
        Testing_Suite("Testing Suite (blink/test)"):::demo
    end

    %% Relationships
    U -->|"writesCode"| UI_Editor
    UI_Editor -->|"submitsCode"| UI_CoreBridge
    UI_CoreBridge -->|"callsAPI"| EM_Core
    EM_Core -->|"returnsResults"| UI_Disassembler
    EM_Core -->|"updatesRegisters"| UI_Registers
    EM_Core -->|"outputsTerminal"| UI_Terminal

    Build_1 -->|"builds"| EM_Core
    Build_2 -->|"builds"| EM_Core
    Build_3 -->|"builds"| EM_Core
    Build_Tool -->|"integrates"| EM_Core

    Demo_Programs -->|"demonstrates"| EM_Core
    Testing_Suite -->|"verifies"| EM_Core

    %% Styling Classes
    classDef frontend fill:#FFEECC,stroke:#D59100,stroke-width:2px;
    classDef emulator fill:#CCE5FF,stroke:#0077CC,stroke-width:2px;
    classDef build fill:#DFF0D8,stroke:#4CAF50,stroke-width:2px;
    classDef demo fill:#F8D7DA,stroke:#C82333,stroke-width:2px;
    classDef user fill:#FFF2CC,stroke:#CC9900,stroke-width:2px;

    %% Click Events for Web UI / Frontend
    click UI_App "https://github.com/robalb/x86-64-playground/blob/master/webapp/src/App.svelte"
    click UI_Editor "https://github.com/robalb/x86-64-playground/blob/master/webapp/src/components/Editor.svelte"
    click UI_Disassembler "https://github.com/robalb/x86-64-playground/blob/master/webapp/src/components/Disassembler.svelte"
    click UI_Registers "https://github.com/robalb/x86-64-playground/blob/master/webapp/src/components/Registers.svelte"
    click UI_Hexdump "https://github.com/robalb/x86-64-playground/blob/master/webapp/src/components/Hexdump.svelte"
    click UI_Terminal "https://github.com/robalb/x86-64-playground/blob/master/webapp/src/components/Terminal.svelte"
    click UI_ThemeDebug "https://github.com/robalb/x86-64-playground/blob/master/webapp/src/components/ThemeDebug.svelte"
    click UI_CoreBridge "https://github.com/robalb/x86-64-playground/blob/master/webapp/src/core/blink.ts"

    %% Click Events for Emulator / WASM Module
    click EM_Core "https://github.com/robalb/x86-64-playground/blob/master/blink/blink.c"
    click EM_Make "https://github.com/robalb/x86-64-playground/tree/master/blink/Makefile"

    %% Click Events for Build & Tooling Infrastructure
    click Build_1 "https://github.com/robalb/x86-64-playground/blob/master/compile_blink.sh"
    click Build_2 "https://github.com/robalb/x86-64-playground/blob/master/compile_demo_programs.sh"
    click Build_3 "https://github.com/robalb/x86-64-playground/blob/master/compile_musl_binutils.sh"
    click Build_Tool "https://github.com/robalb/x86-64-playground/tree/master/blink/tool"

    %% Click Events for Demo Programs & Testing
    click Demo_Programs "https://github.com/robalb/x86-64-playground/tree/master/demo_programs"
    click Testing_Suite "https://github.com/robalb/x86-64-playground/tree/master/blink/test"
```
