# sourmesen/mesen2 architecture

> Architecture diagram of the GitHub repository sourmesen/mesen2, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sourmesen/mesen2
- Repository: https://github.com/sourmesen/mesen2

## Overview

##### Project Type and Purpose
- Mesen is a multi-system emulator that supports a wide variety of target systems (e.g., NES, SNES, Game Boy, Game Boy Advance, PC Engine, SMS/Game Gear, WonderSwan).
- Its purpose is to accurately emulate these systems on Windows, Linux, and macOS while also offering extensive debugging capabilities.
- From the README that native and .NET builds are supported, and the emulator includes both performance-optimized native code and managed UI components.

##### Main Components
a. Emulation Core:
– Represent the “Core” directory as the heart of the project.
– Show sub-components for each emulated system (e.g., Core/NES, Core/SNES, Core/Gameboy, etc.) and shared modules (e.g., Core/Shared, Core/Utilities).
– Highlight that each system module contains CPU emulation, memory management, input handling, audio processing, and video rendering.
– Emphasize that many systems have their own dedicated debuggers and assemblers (e.g., directories like Core/Debugger for each platform).

b. Debugger and Scripting Modules:
– Add a dedicated Debugger component that aggregates the many debugger modules found under Core (and system-specific ones under Core/[System]/Debugger).
– Include the scripting interfaces (notably Lua support under the “Lua” directory) as a way to interact with, script, or extend the debugger and emulator functionality.

c. UI and Interop Layer:
– Represent the “UI” folder as the front-end component.
- This layer contains views, view-models, controls, configuration windows, and debugging windows.
– Show the “InteropDLL” as a bridge between the core (native emulation engine) and the UI. This component allows the UI (and possibly other external tools) to interact seamlessly with the emulator core.
– Include layers representing configuration and third-party libraries (like Avalonia/XAML for UI and even the separate “Sdl” folder for native rendering and input on some platforms).

d. Platform Abstraction and Build/Platform Specific Modules:
– Create separate nodes for “Windows,” “Linux,” and “macOS” directories to show that the emulator supports OS-specific control/input implementations (e.g., Linux/LinuxGameController.cpp, MacOS/MacOSGameController.mm, Windows/* files).
– Also include “SteamOS.md” (and other platform-specific configuration files) to indicate specialized support.
– Illustrate external dependencies (e.g., SDL2, which appears in both the README and Linux/MacOS folders).

e. Utility and Supporting Libraries:
– Represent folders like “Utilities,” “SevenZip,” and “Sdl” which include cross-cutting functionalities like file archiving, video conversions, audio filters, and other helper functions.
– These utilities are used by both the emulator core and possibly by the UI layer for tasks like rendering video frames, processing patches, and handling various media formats.

##### Relationships and Data Flow
- That the Emulation Core distributes responsibilities among system-specific modules, each with their own debugger, audio/video engine, and memory/input systems.
- Point out that the Debugger module aggregates data (e.g., breakpoints, code disassembly, trace logs) from each system emulation core and communicates that to the UI.
- That utility libraries support almost every layer (e.g., audio filtering, video decoding/encoding, file I/O) and are shared resources across the system.

##### Architectural Patterns and Design Principles
- Use a layered architecture: with the lowest/central layer as the Emulation Core, then platform abstraction and utility services, and finally the UI layer on top.
- A modular design where each system (NES, SNES, Gameboy, etc.) is encapsulated as a separate module that plugs into the broader emulator framework.
- Separation of concerns: core emulation logic, debugging/logging, system-specific input/output, and user interface are clearly partitioned.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% UI Layer
    subgraph "UI Layer"
        UI["UI/Presentation Layer"]:::ui
        Interop["Interop Layer"]:::interop
    end
    UI -->|"communicates"| Interop

    %% Emulation Core Layer
    subgraph "Emulation Core"
        ECore["Emulation Core"]:::core
        Nes["NES Module"]:::core
        Gboy["Gameboy Module"]:::core
        GBA["GBA Module"]:::core
        Snes["SNES Module"]:::core
        PCE["PC Engine Module"]:::core
        SMS["SMS/Game Gear Module"]:::core
        WS["WonderSwan Module"]:::core
        Debugger["Core Debugger"]:::debugger

        ECore --> Nes
        ECore --> Gboy
        ECore --> GBA
        ECore --> Snes
        ECore --> PCE
        ECore --> SMS
        ECore --> WS

        Nes -->|"debugs"| Debugger
        Gboy -->|"debugs"| Debugger
        GBA -->|"debugs"| Debugger
        Snes -->|"debugs"| Debugger
        PCE -->|"debugs"| Debugger
        SMS -->|"debugs"| Debugger
        WS -->|"debugs"| Debugger
    end

    Interop -->|"bridges"| ECore
    Debugger -->|"reports"| UI

    %% Scripting Interface
    LuaScripting["Lua Scripting Interface"]:::scripting
    LuaScripting -->|"scripts"| Debugger

    %% Platform Abstraction Layer
    subgraph "Platform Abstraction"
        WindowsOS["Windows"]:::platform
        LinuxOS["Linux"]:::platform
        MacOS["macOS"]:::platform
        SteamOS["SteamOS Config"]:::platform
    end

    ECore -->|"platformsupport"| WindowsOS
    ECore -->|"platformsupport"| LinuxOS
    ECore -->|"platformsupport"| MacOS
    ECore -->|"platformsupport"| SteamOS

    %% Utility & Supporting Libraries
    subgraph "Utility & Supporting Libraries"
        Utils["Utilities"]:::util
        SevenZipLib["SevenZip"]:::util
        SdlLib["SDL"]:::util
    end

    Utils ---|"supports"| ECore
    SevenZipLib ---|"supports"| ECore
    SdlLib ---|"supports"| ECore

    Utils ---|"supports"| UI
    SevenZipLib ---|"supports"| UI
    SdlLib ---|"supports"| UI

    %% Click Events
    click ECore "https://github.com/sourmesen/mesen2/tree/master/Core"
    click Nes "https://github.com/sourmesen/mesen2/tree/master/Core/NES"
    click Gboy "https://github.com/sourmesen/mesen2/tree/master/Core/Gameboy"
    click GBA "https://github.com/sourmesen/mesen2/tree/master/Core/GBA"
    click Snes "https://github.com/sourmesen/mesen2/tree/master/Core/SNES"
    click PCE "https://github.com/sourmesen/mesen2/tree/master/Core/PCE"
    click SMS "https://github.com/sourmesen/mesen2/tree/master/Core/SMS"
    click WS "https://github.com/sourmesen/mesen2/tree/master/Core/WS"
    click Debugger "https://github.com/sourmesen/mesen2/tree/master/Core/Debugger"
    click LuaScripting "https://github.com/sourmesen/mesen2/tree/master/Lua"
    click UI "https://github.com/sourmesen/mesen2/tree/master/UI"
    click Interop "https://github.com/sourmesen/mesen2/tree/master/InteropDLL"
    click WindowsOS "https://github.com/sourmesen/mesen2/tree/master/Windows"
    click LinuxOS "https://github.com/sourmesen/mesen2/tree/master/Linux"
    click MacOS "https://github.com/sourmesen/mesen2/tree/master/MacOS"
    click SteamOS "https://github.com/sourmesen/mesen2/blob/master/SteamOS.md"
    click Utils "https://github.com/sourmesen/mesen2/tree/master/Utilities"
    click SevenZipLib "https://github.com/sourmesen/mesen2/tree/master/SevenZip"
    click SdlLib "https://github.com/sourmesen/mesen2/tree/master/Sdl"

    %% Styles
    classDef core fill:#8ecae6,stroke:#219ebc,stroke-width:2px;
    classDef ui fill:#ffcc99,stroke:#ff9900,stroke-width:2px;
    classDef interop fill:#f5c8d6,stroke:#f72585,stroke-width:2px;
    classDef debugger fill:#ffe5b4,stroke:#ffb703,stroke-width:2px;
    classDef scripting fill:#caffbf,stroke:#57cc99,stroke-width:2px;
    classDef platform fill:#d8f3dc,stroke:#40916c,stroke-width:2px;
    classDef util fill:#e0fbfc,stroke:#98c1d9,stroke-width:2px;
```
