gameboy
Game Boy Emulator
A Game Boy emulator written in Crystal, capable of running simple Game Boy ROMs like Tetris.
Requirements
- Crystal (latest version)
- SDL2 library
Building
crystal build src/main.cr -o gameboy --release
Running
./gameboy path/to/rom.gb
Example with included Tetris ROM:
./gameboy resources/tetris_v1.1.gb
Current State
This emulator implements the core Game Boy components:
- CPU: Full Z80-like instruction set with proper timing
- Memory Management Unit (MMU): Complete memory mapping with cartridge support
- Memory Bank Controllers: MBC1, MBC3, and MBC5 implementations for broader ROM compatibility
- Picture Processing Unit (PPU): Background and sprite rendering with accurate timing
- Audio Processing Unit (APU): Accurate sound synthesis with proper timing
- Channel 1: Square wave with frequency sweep, volume envelope, and length counter (fully implemented)
- Channel 2: Square wave with volume envelope and length counter (fully implemented)
- Channel 3: Wave pattern RAM (register handling only, synthesis pending)
- Channel 4: Noise generator (register handling only, synthesis pending)
- Frame sequencer running at 512 Hz for accurate timing (length/envelope/sweep)
- DAC enable/disable handling for clean audio output
- SDL audio output at 48kHz with proper mixing
- Timer: Divider and timer registers with interrupts
- Joypad: Input handling
- Interrupts: VBlank, LCD STAT, Timer, and Joypad interrupts
- Display: SDL-based rendering with accurate 59.73 FPS frame rate limiting
Tested with: Tetris v1.1
Known Limitations
- Audio: Only channels 1 and 2 (square waves) produce sound. Channels 3 (wave) and 4 (noise) need synthesis implementation
- Save states: No save state support
- Battery RAM: No battery-backed RAM persistence (games with saves won't persist between sessions)
- GameBoy Color: No Game Boy Color support
- RTC: Real-time clock in MBC3 not yet ticking (registers implemented but static)
Logical Next Steps
Short Term
- ✅ Remove remaining test files - Completed (no test files found)
- ✅ Frame rate limiting - Completed (accurate 59.73 FPS timing)
- ✅ Memory Bank Controllers - Completed (MBC1, MBC3, MBC5 implemented)
- ✅ APU register handling - Completed (all channels and registers implemented)
- ✅ Basic audio synthesis - Completed (channels 1 & 2 working with SDL audio output)
- ✅ Audio enhancements - Completed (frequency sweep, length counters, volume envelopes, DAC control)
- Complete audio synthesis: Implement channels 3 (wave) and 4 (noise)
- Test with more ROMs: Validate with games beyond Tetris (Super Mario Land, Pokemon, Zelda, etc.)
- Battery-backed RAM: Implement save file persistence for MBC1/MBC3/MBC5
- RTC implementation: Add real-time clock ticking for MBC3 games
Medium Term
- Save state support: Serialize and restore emulator state
- Performance profiling: Identify and optimize CPU hotspots
- Debugger interface: Add breakpoints, memory viewer, and step execution
- Input mapping: Allow keyboard remapping
- Additional MBC types: MBC2, MBC6, MBC7 for even broader compatibility
Long Term
- Game Boy Color support: Extend PPU for color palettes and double-speed mode
- Link cable emulation: Support for multiplayer games
- Enhanced PPU accuracy: Fix any remaining rendering glitches
- Automated testing: ROM test suite integration (Blargg's tests, etc.)
- Cross-platform builds: Package for macOS, Linux, and Windows
Architecture
src/main.cr- Entry pointsrc/emulator.cr- Main emulation loop with frame rate limiting and audio generationsrc/cpu.cr/src/instructions.cr- CPU implementationsrc/ppu.cr- Graphics renderingsrc/mmu.cr- Memory management with cartridge and APU routingsrc/cartridge.cr- Base cartridge interfacesrc/cartridge_none.cr- Simple ROM-only cartridgessrc/cartridge_mbc1.cr- MBC1 implementationsrc/cartridge_mbc3.cr- MBC3 with RTC supportsrc/cartridge_mbc5.cr- MBC5 implementationsrc/apu.cr- Audio Processing Unit (register handling)src/audio_output.cr- Audio synthesis and SDL audio outputsrc/timer.cr- Timer and divider registerssrc/joypad.cr- Input handlingsrc/display.cr- SDL display and renderingsrc/sdl_bindings.cr- SDL2 library bindings (video and audio)src/rom.cr/src/rom_loader.cr- ROM loading and cartridge detection
Testing
To compile and test the emulator:
# Build in release mode
crystal build src/main.cr -o gameboy --release
# Run with Tetris (should have graphics AND sound!)
./gameboy resources/tetris_v1.1.gb
Expected behavior:
- Window opens showing Tetris gameplay
- Sound plays from channels 1 and 2 (square waves)
- Game runs at proper speed (59.73 FPS)
- Input works (arrow keys for movement, Z/X for A/B buttons)
Testing audio specifically:
- Tetris uses channels 1 and 2 extensively for music and sound effects
- You should hear the iconic Tetris theme playing with accurate note lengths and volume fading
- Sound effects should play when moving/rotating pieces and clearing lines
- Audio features working: frequency sweep (pitch bending), volume envelopes (fade in/out), and length counters (note duration)
- If you only see graphics without sound, check that SDL2 audio is working on your system
Resources
- Pan Docs - Comprehensive Game Boy technical reference
resources/gbcpuman.pdf- Game Boy CPU manual
Repository
gameboy
Owner
Statistic
- 1
- 0
- 0
- 0
- 1
- about 10 hours ago
- January 15, 2016
License
MIT License
Links
Synced at
Sun, 09 Nov 2025 10:25:08 GMT
Languages