From 4fce95c86e12f91e127605d440118e1b6a64208b Mon Sep 17 00:00:00 2001 From: Astatin Date: Thu, 22 May 2025 00:07:30 +0200 Subject: Save wram,vram,io,hram with X button + move big arrays to Heap --- src/io.rs | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) (limited to 'src/io.rs') diff --git a/src/io.rs b/src/io.rs index 3e7330e..b26533a 100644 --- a/src/io.rs +++ b/src/io.rs @@ -1,13 +1,14 @@ use std::time::SystemTime; use std::{thread, time}; -use crate::state::GBState; use crate::consts; +use crate::state::GBState; pub trait Input { fn update_events(&mut self, cycles: u128) -> Option; fn get_action_gamepad_reg(&self) -> u8; fn get_direction_gamepad_reg(&self) -> u8; + fn save_state(&mut self) -> bool; } impl Input for Box { @@ -20,6 +21,9 @@ impl Input for Box { fn get_direction_gamepad_reg(&self) -> u8 { (**self).get_direction_gamepad_reg() } + fn save_state(&mut self) -> bool { + (**self).save_state() + } } pub enum WindowSignal { @@ -45,21 +49,20 @@ pub trait Audio { fn new + Send + 'static>(wave: S) -> Self; } -pub trait LoadSave where Self::Error: std::fmt::Display, Self::Error: std::fmt::Debug { +pub trait LoadSave +where + Self::Error: std::fmt::Display, + Self::Error: std::fmt::Debug, +{ type Error; fn load_bootrom(&self, boot_rom: &mut [u8]) -> Result<(), Self::Error>; fn load_rom(&self, rom: &mut [u8]) -> Result<(), Self::Error>; fn load_external_ram(&self, external_ram: &mut [u8]) -> Result<(), Self::Error>; fn save_external_ram(&self, external_ram: &[u8]) -> Result<(), Self::Error>; + fn save_state(&self, state: &GBState); } -pub struct Gameboy< - I: Input, - W: Window, - S: Serial, - A: Audio, - LS: LoadSave, -> { +pub struct Gameboy { input: I, window: W, speed: f64, @@ -78,7 +81,6 @@ impl Gameboy Gameboy Gameboy= 0.0 || next_precise_gamepad_update.map_or(false, |c| (c >= total_cycle_counter)) { + if nanos_sleep >= 0.0 + || next_precise_gamepad_update.map_or(false, |c| (c >= total_cycle_counter)) + { next_precise_gamepad_update = input.update_events(total_cycle_counter); - let (action_button_reg, direction_button_reg) = ( + let (action_button_reg, direction_button_reg, save_state) = ( input.get_action_gamepad_reg(), input.get_direction_gamepad_reg(), + input.save_state(), ); + if save_state { + load_save.save_state(&state); + } + if state.mem.joypad_is_action - && (action_button_reg & (state.mem.joypad_reg >> 4)) != (state.mem.joypad_reg >> 4) + && (action_button_reg & (state.mem.joypad_reg >> 4)) + != (state.mem.joypad_reg >> 4) || (!state.mem.joypad_is_action && (direction_button_reg & state.mem.joypad_reg & 0b1111) != (state.mem.joypad_reg & 0b1111)) @@ -150,7 +160,6 @@ impl Gameboy 0.0 { if let Some(fb) = state.mem.display.redraw_request { if let Some(WindowSignal::Exit) = window.update(&fb) { @@ -165,7 +174,7 @@ impl Gameboy