michelhe / rustboyadvance-ng

RustBoyAdvance-NG is a Nintendo™ Game Boy Advance emulator and debugger, written in the rust programming language.
MIT License
596 stars 23 forks source link

"attempt to subtract with overflow" when rendering second frame #168

Closed SkyLeite closed 2 years ago

SkyLeite commented 2 years ago

I'm using rustboyadvance-ng/core for a personal project. It seems to work fine in release mode (as runtime overflow checks are disabled), but in development I run into this error when attempting to render the second frame. The bios frames and first frame of the game itself render fine.

Reproduction of the issue can be found here: https://github.com/SkyLeite/pokecord/tree/0b69d9ae03491ca2190e6cdcf52fb5710925f197

warning: `pokecord` (bin "pokecord") generated 6 warnings
    Finished dev [unoptimized + debuginfo] target(s) in 0.04s
     Running `target/debug/pokecord`
Rendered!
First frame rendered!
thread 'main' panicked at 'attempt to subtract with overflow', rustboyadvance-ng/core/src/gpu/mod.rs:464:47
stack backtrace:
   0:     0x5579b61468ec - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hb16b437de6ab6276
   1:     0x5579b6171bde - core::fmt::write::h8f8bef23583ee234
   2:     0x5579b614d0c1 - std::io::Write::write_fmt::h8486efe074824056
   3:     0x5579b61544be - std::panicking::default_hook::{{closure}}::hdfa4615b29a34cb8
   4:     0x5579b6154127 - std::panicking::default_hook::h60ffd169fe7df297
   5:     0x5579b61549d1 - std::panicking::rust_panic_with_hook::hf0183b899953e185
   6:     0x5579b6146ea9 - std::panicking::begin_panic_handler::{{closure}}::h93eac1c61f0beba3
   7:     0x5579b6146cc4 - std::sys_common::backtrace::__rust_end_short_backtrace::he4788588c206eaab
   8:     0x5579b6154682 - rust_begin_unwind
   9:     0x5579b5e68813 - core::panicking::panic_fmt::h20a31c8e90ca0d37
  10:     0x5579b5e686dd - core::panicking::panic::h95bb893ef3e7cb7f
  11:     0x5579b5ec92be - rustboyadvance_core::gpu::Gpu::on_event::ha2e8b68811dd5add
                               at /mnt/hdd/projects/pokecord/rustboyadvance-ng/core/src/gpu/mod.rs:464:47
  12:     0x5579b5edd4c6 - rustboyadvance_core::gba::GameBoyAdvance::handle_event::hda667049d6b7e2c7
                               at /mnt/hdd/projects/pokecord/rustboyadvance-ng/core/src/gba.rs:310:38
  13:     0x5579b5edd2b2 - rustboyadvance_core::gba::GameBoyAdvance::run::hc95f87805a33d994
                               at /mnt/hdd/projects/pokecord/rustboyadvance-ng/core/src/gba.rs:288:17
  14:     0x5579b5edcd8a - rustboyadvance_core::gba::GameBoyAdvance::frame::h10956a8c82e3b727
                               at /mnt/hdd/projects/pokecord/rustboyadvance-ng/core/src/gba.rs:222:25
  15:     0x5579b5e6e027 - pokecord::main::hf48fb910a058991b
                               at /mnt/hdd/projects/pokecord/src/main.rs:77:14
  16:     0x5579b5e69f7b - core::ops::function::FnOnce::call_once::h794bd1593b72e1e2
                               at /build/rustc-1.63.0-src/library/core/src/ops/function.rs:248:5
  17:     0x5579b5e68b9e - std::sys_common::backtrace::__rust_begin_short_backtrace::hdded08256cdf329e
                               at /build/rustc-1.63.0-src/library/std/src/sys_common/backtrace.rs:122:18
  18:     0x5579b5e68b61 - std::rt::lang_start::{{closure}}::h010441cf4be51538
                               at /build/rustc-1.63.0-src/library/std/src/rt.rs:145:18
  19:     0x5579b614c9d5 - std::rt::lang_start_internal::h3e617fbe87035bdb
  20:     0x5579b5e68b30 - std::rt::lang_start::hda90f78f082db313
                               at /build/rustc-1.63.0-src/library/std/src/rt.rs:144:17
  21:     0x5579b5e6e0cc - main
  22:     0x7f1956160237 - __libc_start_call_main
  23:     0x7f19561602f5 - __libc_start_main_impl
  24:     0x5579b5e689b1 - _start
                               at /build/glibc-2.34/csu/../sysdeps/x86_64/start.S:116
  25:                0x0 - <unknown>
michelhe commented 2 years ago

Thank you, I think I know whats the issue, working on a fix 🎉

michelhe commented 2 years ago

@SkyLeite Hey, I've pushed some long overdue refactors to the emulation scheduler, your bug should now be fixed (tested against your repo as well).

LMK if you run into more of these, any excuse to drag me back into this project is appreciated :)

SkyLeite commented 2 years ago

Thank you! It works great now :)