mGBA 架构概述

本文档概述了 mGBA 的代码库架构、组件和设计原则。了解架构对于为项目做出贡献或自定义 mGBA 至关重要。

概述

mGBA 主要使用 C 语言编写,部分 C++ 用于平台特定代码和基于 Qt 的 GUI。代码库设计注重准确性、性能和跨多个平台的可移植性。

核心架构

模块化设计

mGBA 使用具有独立组件的模块化架构:

  • 核心: 核心模拟引擎(CPU、内存、硬件组件)
  • 前端: 平台特定的 UI 实现(Qt、SDL 等)
  • 后端: 平台特定的后端(OpenGL、Vulkan、Direct3D、Metal)
  • 支持: 支持库和实用程序

目录结构

mGBA 源代码树中的关键目录:

  • src/: 主要源代码
  • src/core/: 核心模拟引擎
  • src/gb/: Game Boy/Game Boy Color 模拟
  • src/gba/: Game Boy Advance 模拟
  • src/platform/: 平台特定代码
  • src/util/: 实用函数和辅助工具
  • src/debugger/: 调试器功能
  • src/feature/: 功能实现(作弊码、脚本等)

Core Components

CPU Emulation

The CPU core implements ARM7TDMI processor emulation:

  • ARM Mode: 32-bit ARM instruction set
  • THUMB Mode: 16-bit THUMB instruction set
  • Cycle Accuracy: Cycle-accurate instruction timing
  • Pipelining: CPU pipeline simulation

Memory Management

Memory system implements complete GBA memory map:

  • Memory Map: Complete 32-bit address space
  • Memory Regions: WRAM, VRAM, ROM, SRAM, I/O registers
  • DMA: Direct Memory Access transfers
  • Cache: CPU cache simulation

Graphics System

Graphics rendering system:

  • PPU (Pixel Processing Unit): Graphics pipeline emulation
  • Rendering Backends: Software, OpenGL, Vulkan, Direct3D, Metal
  • Layers: Background layers and sprite layer
  • Effects: Scaling, rotation, alpha blending

Audio System

Audio processing system:

  • Sound Channels: PCM, PSG channel emulation
  • Mixing: Audio channel mixing
  • Resampling: Sample rate conversion
  • Output: Platform-specific audio output

Frontend Architecture

Qt Frontend

Qt-based GUI frontend for desktop platforms:

  • Cross-platform GUI framework
  • Native look and feel on each platform
  • Rich widget set for configuration and debugging

SDL Frontend

SDL-based frontend for simpler UI or embedded systems:

  • Lighter weight than Qt frontend
  • Used for embedded ports (3DS, Wii U, Vita)
  • Basic windowing and input handling

Backend Architecture

Rendering Backends

Multiple rendering backends for different platforms:

  • Software: CPU-based rendering (always available)
  • OpenGL: Cross-platform hardware acceleration
  • Vulkan: Modern low-level API (Linux/Windows)
  • Direct3D 11: Windows-specific acceleration
  • Metal: macOS/iOS native acceleration

Design Principles

Accuracy First

mGBA prioritizes accuracy over performance:

  • Cycle-accurate emulation as default
  • Hardware-accurate behavior for all components
  • Performance optimizations must not sacrifice accuracy

Portability

Code is written with portability in mind:

  • Platform abstraction layers for OS-specific code
  • Standard C/C++ where possible
  • Conditional compilation for platform-specific features

Modularity

Components are designed to be modular and independent:

  • Clear separation between core and frontend
  • Pluggable backends for rendering and audio
  • Optional features can be enabled or disabled

Key Components

Core Emulator (mCore)

The core emulator class manages emulation state:

  • Initializes and manages all hardware components
  • Handles ROM loading and game state
  • Coordinates CPU, memory, graphics, and audio
  • Manages save states and battery saves

Game Boy Advance (GBA)

GBA-specific emulation:

  • ARM7TDMI CPU core
  • GBA memory map and hardware
  • GBA-specific features (link cable, RTC, etc.)

Game Boy/Game Boy Color (GB/GBC)

Backward compatibility with GB/GBC games:

  • Separate GB/GBC emulation core
  • Automatic detection of GB/GBC games
  • Same features available for GB/GBC games

Platform Ports

mGBA supports multiple platforms through platform-specific ports:

  • Desktop: Windows, macOS, Linux (Qt frontend)
  • Mobile: Android, iOS (native UI)
  • Embedded: 3DS, Wii U, Vita (SDL frontend)

Feature Modules

Optional Features

Features can be enabled or disabled at compile time:

  • Lua Scripting: Lua 5.4 integration
  • Debugger: Debugging tools and memory inspection
  • Cheat Codes: Cheat code support
  • Link Cable: Multiplayer support
Learn More: For detailed information about specific components, see the source code documentation in the repository or check the code comments.

相关文章

有关 mGBA 开发的更多信息: