Try   HackMD

2020q3 專題: Game Boy 模擬器

目標

  • 改進給定的 Game Boy 模擬器,提升其執行效率、程式碼重構 (refactor),及其相容性
    • 透過 GBIT 確保程式碼實作的相容性和正確性
  • 量化分析直譯器效率,並尋求效能改進的方案
    • 善用 perf 和 GNU Toolchain
  • 實作 CPU 除錯器,允許單步執行和分析特定記憶體內容

準備工作

  • 研讀 Gameboy Overview,理解 Game Boy 硬體和相關模擬器的撰寫
  • 研讀 Z80 Emu Evolution 以理解模擬器設計考量。注意: Game Boy CPU 使用 Z80 的客製化版本,和原本的 Z80 不相容
  • 研讀 Game Boy 遊戲軟體發展,理解 Game Boy 產品、硬體資訊、記憶體映射,及 GBDK 流程

Game Boy 硬體資訊

Game Boy 的硬體設計與運作原理

現有的 Game Boy 模擬器

  • binjgb
  • jgbc
    • 實作完整
    • 內建測試
  • SameBoy
    • Supports Game Boy (DMG) and Game Boy Color (CGB) emulation
    • High quality 96KHz audio
    • Battery save support
    • Save states
    • Advanced text-based debugger with an expression evaluator, disassembler, conditional breakpoints, conditional watchpoints, backtracing and other features
    • Several scaling algorithms (Including exclusive algorithms like OmniScale and Anti-aliased Scale2x; Requires OpenGL 3.2 or later or Metal)
  • PlutoBoy
    • 非常完整
  • GBC
    • When the emulator detects unexpected behavior (e.g., accessing an unknown memory region), it will drop into a built-in debugger.
  • Worldwide: GameBoyColor emulator written in Go
    • 具備 hq2x
    • 非常完整
  • dmg
    • 程式碼架構清晰
    • 內建 test suite
  • mgba/src/sm83: mGBA 是 Game Boy Advance 模擬器,儘管主要針對 ARMv4T,但裡頭的 sm83 則是針對 GBZ80,這部分程式碼相當簡潔並附有指令層級除錯器

給定的 Game Boy 模擬器

  1. 取得原始程式碼:
$ git clone https://github.com/sysprog21/gameboy-emu
  1. 安裝 SDL2 套件
  2. 編譯
$ cd gameboy-emu
$ make
  1. 下載 Game Boy ROM 並測試
$ make download_rom
$ build/emu roms/HungryBirds.gb 

預期會見到以下執行畫面:

HungryBirds for Game Boy

Computed Goto

Opcode Generator / Instruction Tester

  • 參照 emu-gameboy,允許由表格來產生 opcode 及其實作程式碼
  • 預期產出
  • GBIT: Game Boy Instruction Tester
    • Tests all instructions of a Game Boy CPU against a known-good implementation to detect implementation bugs.
    • Useful for testing and debugging, especially early on in Game Boy emulator development where test ROMs do not run yet.

研究進度

gameboy-emu

參考資訊