So, it's recently been brought to my attention that I have yet to add support for cheat codes. With my new memory mapping design, I have a couple of implementations I could choose, but I'd like some opinions on what would be best from a user perspective.
GameShark codes are very straightforward and don't rely on any particular implementation; they just write specific values to RAM on every frame.
Game Genie codes are where it gets complicated, because they effectively patch the ROM directly. I have a few options here:
Option 1: Bake cheat code information into the ROM metadata at ROM conversion time, and patch or unpatch the ROM directly when enabling and disabling codes.
Pros:
- Cheat codes can be verified to work with the game at conversion time, and be stored in the most optimal way to parse them.
- No runtime performance or memory cost during emulation.
Cons:
- Cheat codes can't be changed without reconverting the ROM.
- Patching the ROM involves unarchiving and rearchiving files, which can cause garbage collects. However, since the patchable areas are known in advance, they can be split into smaller (<=16KB) files to reduce archive load.
- ROMs could end up in an inconsistent state if the patching process is interrupted (e.g. the user refuses a Garbage Collect).
Option 2: Convert cheat files separately, and load patched segments of the ROM into RAM.
Pros:
- Can easily change cheat codes. If it's in a textual format, they could even be edited on-calc with a text editor.
- No archive usage or garbage collection when enabling or disabling codes.
Cons:
- Incurs a performance cost when switching memory banks containing patched data, as well as extra splits in JIT code blocks.
- Number of active codes is limited by available RAM (approximately 256 bytes per code).
- Cheat code parsing on the calculator is more complex, as well as detecting when the file was edited so codes can be disabled.
Option 3: Same as option 2, but load entire 16KB banks into RAM when they're patched. This significantly reduces the number of possible active codes, but has no performance cost.
I'd also like opinions on cheat code persistence between loads of the ROM. I'm leaning towards saving which codes are active (especially if Option 1 is taken), not as part of save states but as either part of the ROM (in Option 1) or the per-game config file (in Option 2/3).