# 2020q3 專題: Game Boy 模擬器 + JIT 編譯器 > * [RinHizakura 的工作區](https://hackmd.io/@RinHizakura/SkvY4N9cv) > * [nelsonlai1 的工作區](https://hackmd.io/@nelsonlai1/GBJIT) ## 目標 * 改進[給定的 Game Boy 模擬器](https://github.com/sysprog21/jitboy),排除已知問題,提升其執行效率及其相容性 * 透過 [GBIT](https://github.com/koenk/gbit) 確保程式碼實作的相容性和正確性 * 量化分析動態編譯器的效率,並尋求效能改進的方案 * 善用 [perf](http://www.brendangregg.com/perf.html) 和 GNU Toolchain ## 準備工作 * 觀看 [The Ultimate Game Boy Talk](https://media.ccc.de/v/33c3-8029-the_ultimate_game_boy_talk) 演講錄影,得知 Game Boy 模擬器的運作 * 研讀 [Gameboy Overview](https://thomas.spurden.name/gameboy/),理解 Game Boy 硬體和相關模擬器的撰寫 * 對應的原始程式碼: [gameboy](https://github.com/tcrs/gameboy) * 研讀 [Z80 Emu Evolution](https://floooh.github.io/2017/12/10/z80-emu-evolution.html) 以理解模擬器設計考量。注意: Game Boy CPU 使用 Z80 的客製化版本,和原本的 Z80 不相容 * 研讀 [Game Boy 遊戲軟體發展](http://140.134.131.145/upload/paper_uni/912pdf/910211.pdf),理解 Game Boy 產品、硬體資訊、記憶體映射,及 GBDK 流程 ## Game Boy 硬體和模擬器設計 > [Game Boy 的硬體設計與運作原理](https://hackmd.io/@RinHizakura/BJ6HoW29v) ## 現有的 Game Boy 模擬器 * [binjgb](https://github.com/binji/binjgb) * 可[在網頁瀏覽器中啟動](https://binji.github.io/binjgb/) * Convenient Python test harness using hashes to validate * Debugger with various visualizations * [jgbc](https://github.com/jamie-mh/jgbc) * 實作完整 * 內建測試 * [SameBoy](https://github.com/LIJI32/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](https://sameboy.github.io/scaling/) (Including exclusive algorithms like OmniScale and Anti-aliased Scale2x; Requires OpenGL 3.2 or later or Metal) * [PlutoBoy](https://github.com/RossMeikleham/PlutoBoy) * 非常完整 * [GBC](https://github.com/koenk/gbc) * When the emulator detects unexpected behavior (e.g., accessing an unknown memory region), it will drop into a built-in debugger. ## [給定的 Game Boy 模擬器](https://github.com/sysprog21/jitboy) 1. 取得 `jitboy` 原始程式碼: ```shell $ git clone https://github.com/sysprog21/jitboy ``` 2. 安裝 SDL2 套件 3. 編譯 (目前僅能在 GNU/Linux 平台運作) ```shell $ cd jitboy $ make ``` 4. 取得 [Super Mario Land](https://wowroms.com/en/roms/nintendo-gameboy/super-mario-land-world/10202.html),解開下載的 ZIP 檔案,將 `Super Mario Land (World).gb` 重新命名為 `mario.gb` 6. 執行 `build/jitboy mario.gb` ## Thread Sanitizer (tsan) * 參見 [ThreadSanitizerCppManual](https://github.com/google/sanitizers/wiki/ThreadSanitizerCppManual) 和 [Thread Sanitizer and Static Analysis](https://developer.apple.com/videos/play/wwdc2016/412/) * `jitboy` 已整合 tsan: ```shell make clean make sanitizer ``` * `jitboy` 執行過程中會遇到若干 [data race](https://en.wikipedia.org/wiki/Race_condition#Data_race),應該予以排除 ## 動態編譯器原理 * [Going faster with Just-In-Time compilation](https://github.com/jorgemarsal/jekyll-now/blob/master/_posts/2016-01-14-going-faster-with-just-in-time-compilation.md): 透過一個簡單的案例,說明適度運用 JIT 編譯為何可在執行時期帶來加速 * [Interpreter, Compiler, JIT from scratch](https://www.slideshare.net/jserv/jit-compiler) * [虛擬機器設計與實作](https://hackmd.io/@sysprog/SkBsZoReb) * [Unofficial DynASM Documentation](https://corsix.github.io/dynasm-doc/reference.html) * 使用 [DynASM](https://luajit.org/dynasm.html) 的案例 * [LuaJIT](https://luajit.org/) * [Pyston v2](https://blog.pyston.org/2020/10/28/pyston-v2-20-faster-python/) * [Opcache JIT for PHP](https://github.com/php/php-src/tree/master/ext/opcache/jit) * [PWASM](https://github.com/pablotron/pwasm): WebAssembly * [Wasmer](https://github.com/wasmerio/wasmer): WebAssembly * [libsregex](https://github.com/openresty/sregex): A non-backtracking NFA/DFA-based Perl-compatible regex engine library for matching on large data streams ## 內建 JIT 的 Game Boy 模擬器 * [gbemuc](https://github.com/mkilgore/gbemuc): 使用 libJIT * [GbJit](https://github.com/iburinoc/gbjit) ## Instruction Tester * [GBIT](https://github.com/koenk/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](https://github.com/koenk/gbit),讓 JIT 編譯後的機械碼得以通過驗證 * TODO: 量化分析純粹直譯器和動態編譯器的效能表現