Repository: michelhe/rustboyadvance-ng (language: rust)

Issue:
Panic - attempt to subtract with overflow
Thanks for the emulator! Every game I tried running `rustboyadvance-sdl2` with triggered a subtraction overflow panic. Here's the log for Pokemon Emerald:

```
INFO [rustboyadvance_sdl2] Initializing SDL2 context
INFO [rustboyadvance_sdl2] No game controllers were found
INFO [rustboyadvance_sdl2::audio] Found audio device: AudioSpec { freq: 44100, format: S16LSB, channels: 2, silence: 0, samples: 2048, size: 8192 }
INFO [rustboyadvance_sdl2::audio] ringbuffer size = 8192
INFO [rustboyadvance_core::cartridge::builder] Loaded ROM: CartridgeHeader { game_title: "POKEMON EMER", game_code: "BPEE", maker_code: "01", software_version: 0, checksum: 114 }
INFO [rustboyadvance_core::cartridge::builder] Found game overrides for BPEE: GameOverride {
    force_rtc: true,
    save_type: None,
}
INFO [rustboyadvance_core::cartridge::builder] Detected Backup: Flash1M
INFO [rustboyadvance_core::cartridge::builder] Emulating RTC!
INFO [rustboyadvance_core::gba] Verified bios rom
INFO [rustboyadvance_core::sound] MSE enabled!
INFO [rustboyadvance_core::sound] bias - setting sample frequency to 32768hz
INFO [rustboyadvance_core::sound] MSE disabled!
thread 'main' panicked at core/src/gba.rs:218:25:
attempt to subtract with overflow
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
```

I was able to have the game run, after replacing the subtraction in `core/src/gba.rs` with `.saturating_sub`. I'll do a pull request in a second.


Discussion / hints:

Fix patch:
diff --git a/core/src/gba.rs b/core/src/gba.rs
index 1a5af230..9d67b773 100644
--- a/core/src/gba.rs
+++ b/core/src/gba.rs
@@ -215,7 +215,7 @@ impl GameBoyAdvance {
     pub fn frame(&mut self) {
         static mut OVERSHOOT: usize = 0;
         unsafe {
-            OVERSHOOT = CYCLES_FULL_REFRESH - self.run::<false>(CYCLES_FULL_REFRESH - OVERSHOOT);
+            OVERSHOOT = CYCLES_FULL_REFRESH.saturating_sub(self.run::<false>(CYCLES_FULL_REFRESH - OVERSHOOT));
         }
     }
 
@@ -223,7 +223,7 @@ impl GameBoyAdvance {
     fn frame_interruptible(&mut self) {
         static mut OVERSHOOT: usize = 0;
         unsafe {
-            OVERSHOOT = CYCLES_FULL_REFRESH - self.run::<true>(CYCLES_FULL_REFRESH - OVERSHOOT);
+            OVERSHOOT = CYCLES_FULL_REFRESH.saturating_sub(self.run::<true>(CYCLES_FULL_REFRESH - OVERSHOOT));
         }
     }
 


Test patch: