diff options
| author | Hannes Schulze <haschu0103@outlook.de> | 2025-01-29 14:49:20 +0100 |
|---|---|---|
| committer | Hannes Schulze <haschu0103@outlook.de> | 2025-01-29 14:49:20 +0100 |
| commit | 5959349f341a2380c0c3f41ec40a1e8e0d969d00 (patch) | |
| tree | 8b36ba3f8cade68562af1dfdcea8b42160619975 /lib/nes/sys/nes.cc | |
| parent | 1d32f7561305d6c86b1c80858bc73d55b1c6c402 (diff) | |
refactor: move emulation logic into sys
Diffstat (limited to 'lib/nes/sys/nes.cc')
| -rw-r--r-- | lib/nes/sys/nes.cc | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/lib/nes/sys/nes.cc b/lib/nes/sys/nes.cc new file mode 100644 index 0000000..f922b7d --- /dev/null +++ b/lib/nes/sys/nes.cc @@ -0,0 +1,65 @@ +#include "nes/sys/nes.hh" +#include "nes/sys/types/snapshot.hh" +#include "nes/common/display.hh" +#include "nes/common/rgb.hh" + +namespace nes +{ + nes::nes(cartridge cartridge, display& display) + : cartridge_{ std::move(cartridge) } + , display_{ display } + , ppu_{ cpu_, cartridge_, display_ } + , cpu_{ ppu_, cartridge_, controller_1_, controller_2_ } + , status_{ cartridge_.get_status() } + { + display_.clear(rgb{}); + display_.switch_buffers(); + display_.clear(rgb{}); + } + + auto nes::step(std::chrono::microseconds const delta) -> void + { + step(cycle_count::from_duration(delta)); + } + + auto nes::step() -> void + { + if (get_status() != status::success) { return; } + + status_ = cpu_.step(); + while (ppu_.get_cycles() < cpu_.get_cycles()) + { + ppu_.step(); + } + } + + auto nes::step(cycle_count const delta) -> void + { + current_cycles_ += delta; + while (cpu_.get_cycles() < current_cycles_ && get_status() == status::success) + { + step(); + } + } + + auto nes::step_to_nmi() -> void + { + while (cpu_.is_nmi_pending()) + { + step(); + } + while (!cpu_.is_nmi_pending()) + { + step(); + } + } + + auto nes::get_snapshot() -> snapshot + { + auto res = snapshot{}; + res.sram = std::vector(cartridge_.get_ram(), cartridge_.get_ram() + cartridge_.get_ram_length()); + cpu_.build_snapshot(res); + ppu_.build_snapshot(res); + return res; + } +} // namespace nes
\ No newline at end of file |
