Skip to content

GaminRick7/RetroGB

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RetroGB: A Game Boy Emulator

A cycle-accurate Game Boy (DMG) emulator written in C++ with modern architecture and comprehensive feature set.

Overview

Zelda Gameplay Running The Legend of Zela: Link's Awakening

This emulator implements a complete Game Boy (DMG) system with cycle-accurate timing for a Z80-like processor, proper interrupt handling, and accurate graphics rendering. The project is built with modern C++17 standards and uses SDL2 for cross-platform graphics and input handling.

Architecture

Core Achievements

The emulator delivers cycle-accurate Game Boy emulation through a carefully designed modular architecture:

Cycle-Accurate CPU Emulation

  • Complete Z80-like instruction set with all 8-bit and 16-bit operations including arithmetic, logical, bit manipulation, and control flow instructions (e.g., ADD, LD, JP, CALL, RST)
  • Precise timing: Every instruction executes in the exact number of cycles as the original hardware (4.19 MHz clock)
  • Advanced interrupt system: V-Blank, LCD Status, Timer, Serial, and Joypad interrupts with proper priority handling and interrupt enable/disable flags
  • Register architecture: Full 8-bit registers (A, F, B, C, D, E, H, L) and 16-bit registers (PC, SP) with proper flag handling (Zero, Subtract, Half-carry, Carry)
  • Memory addressing modes: Direct addressing, register indirect, immediate, and indexed addressing with accurate bus timing
  • Stack operations: Push/pop operations with proper stack pointer management and interrupt handling

Hardware-Accurate Graphics Pipeline

  • 160x144 resolution with pixel-perfect rendering at 456 ticks per line, 154 lines per frame
  • Advanced sprite system: Up to 40 sprites with OAM (Object Attribute Memory) and proper priority handling
  • Pixel FIFO: Hardware-accurate pixel pipeline that mimics the original Game Boy's rendering process
  • VRAM management: 8KB video RAM with proper banking
  • LCD timing: Accurate LCD controller timing and synchronization

Complete Memory System

  • Comprehensive memory mapping: ROM (0x0000-0x7FFF), VRAM (0x8000-0x9FFF), WRAM (0xC000-0xDFFF)
  • MBC1 cartridge support: ROM banking with variable sizes and battery-backed save RAM
  • DMA system: High-speed sprite data transfer with accurate timing and bus arbitration

Multi-Threaded Performance

  • Dual-threaded architecture: Dedicated CPU thread with cycle-accurate timing + UI thread for rendering
  • Thread-safe communication: Atomic variables and mutexes for optimal performance
  • Optimized memory allocation: Efficient memory management for smooth 60 FPS performance

Technical Highlights

Performance: Runs at 60 FPS with <5% CPU usage on modern systems while maintaining cycle-accurate timing at 4.19 MHz CPU clock.

Accuracy: Passes comprehensive test suites including CPU instruction tests, interrupt handling, and memory timing validation.

Modularity: Clean separation of concerns with each component (CPU, PPU, Memory, etc.) as independent modules for easy maintenance and extension.

Cross-Platform: Full support for macOS, Linux, and Windows with SDL2 for graphics and input handling.

Supported Games

The emulator includes a comprehensive test suite with various ROMs:

CPU Instruction Tests

  • 01-special.gb - Special instruction tests
  • 02-interrupts.gb - Interrupt handling tests
  • 03-op sp,hl.gb - Stack pointer operations
  • 04-op r,imm.gb - Register-immediate operations
  • 05-op rp.gb - Register pair operations
  • 06-ld r,r.gb - Register-to-register loads
  • 07-jr,jp,call,ret,rst.gb - Jump and call instructions
  • 08-misc instrs.gb - Miscellaneous instructions
  • 09-op r,r.gb - Register operations
  • 10-bit ops.gb - Bit manipulation instructions
  • 11-op a,(hl).g - Accumulator operations

Additionally, the emulator can run any MBC1-type game. Support for other MBC types may be added later.

Building and Running

Prerequisites

  • CMake 3.16 or higher
  • C++17 compatible compiler
  • SDL2 and SDL2_ttf libraries
  • Check library (for unit tests)

Build Instructions

# Clone the repository
git clone <repository-url>
cd gbemu

# Create build directory
mkdir build && cd build

# Configure with CMake
cmake ..

# Build the project
make

# Run the emulator
cd gbemu && ./gbemu <path-to-rom-file>

Platform Support

  • macOS: Full support with Homebrew SDL2
  • Linux: Full support with system SDL2
  • Windows: Full support with bundled SDL2 libraries

Project Structure

gbemu/
├── include/           # Header files
│   ├── cpu.hpp       # CPU emulation
│   ├── ppu.hpp       # Graphics processing
│   ├── bus.hpp       # Memory bus
│   ├── cart.hpp      # Cartridge system
│   ├── timer.hpp     # Timer system
│   ├── dma.hpp       # Direct memory access
│   ├── joypad.hpp    # Input handling
│   ├── ui.hpp        # User interface
│   └── emu.hpp       # Main emulator
├── lib/              # Implementation files
├── tests/            # Unit tests
├── roms/             # Test ROMs
├── cmake/            # CMake configuration
└── CMakeLists.txt    # Build configuration

Configuration

Debug Mode

Enable debug features by setting DEBUG_MODE in common.hpp:

#define DEBUG_MODE 1  // Enable debug features

Performance Tuning

  • Frame rate: Configurable target FPS (default: 60)
  • Threading: CPU and UI thread synchronization
  • Memory allocation: Optimized memory management

Controls

Default Key Mapping

  • A: Z key
  • B: X key
  • Start: Enter key
  • Select: Shift key
  • Up: Up arrow
  • Down: Down arrow
  • Left: Left arrow
  • Right: Right arrow

Debug Controls

  • Debug Window: F1 key
  • Pause: Space bar
  • Step: F2 key (when paused)

Performance

  • Target: 60 FPS at 4.19 MHz CPU clock
  • Memory usage: ~2MB for typical games
  • CPU usage: <5% on modern systems
  • Accuracy: Cycle-accurate timing

Contributing

This project welcomes contributions! Areas for improvement:

  • Audio emulation: Complete sound system implementation
  • Networking: Link cable emulation
  • Performance: Further optimization opportunities
  • Testing: Additional test ROMs and validation

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Game Boy documentation: Pan Docs and other technical references
  • SDL2: Cross-platform multimedia library
  • Test ROMs: Various Game Boy test suites and commercial games
  • Open source community: For inspiration and technical guidance

This emulator aims for accuracy and performance while maintaining clean, maintainable code. The modular architecture makes it easy to extend and improve individual components.

About

A Game Boy Emulator written entirely in C++

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors