Try   HackMD

2020q3 專題: Game Boy 模擬器 + JIT 編譯器

目標

  • 改進給定的 Game Boy 模擬器,排除已知問題,提升其執行效率及其相容性
    • 透過 GBIT 確保程式碼實作的相容性和正確性
  • 量化分析動態編譯器的效率,並尋求效能改進的方案
    • 善用 perf 和 GNU Toolchain

準備工作

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.

給定的 Game Boy 模擬器

  1. 取得 jitboy 原始程式碼:
    ​​​​$ git clone https://github.com/sysprog21/jitboy
    
  2. 安裝 SDL2 套件
  3. 編譯 (目前僅能在 GNU/Linux 平台運作)
    ​​​​$ cd jitboy
    ​​​​$ make
    
  4. 取得 Super Mario Land,解開下載的 ZIP 檔案,將 Super Mario Land (World).gb 重新命名為 mario.gb
  5. 執行 build/jitboy mario.gb

Thread Sanitizer (tsan)

動態編譯器原理

內建 JIT 的 Game Boy 模擬器

Instruction Tester

  • 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.
  • TODO: 整合 GBIT,讓 JIT 編譯後的機械碼得以通過驗證
  • TODO: 量化分析純粹直譯器和動態編譯器的效能表現