aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAstatin <[email protected]>2025-03-22 01:31:25 +0900
committerAstatin <[email protected]>2025-03-22 01:32:12 +0900
commit6613a89b4d31e3718bb20d144ae93bedfeb35a78 (patch)
tree427639309824a5b98eb55493b0df169a3f032f41 /src
parentc07b53df795c2c0eadcb4cc19c7bb83f44a10855 (diff)
Add custom DMG bootrom
Diffstat (limited to 'src')
-rw-r--r--src/gamepad.rs18
-rw-r--r--src/main.rs33
2 files changed, 33 insertions, 18 deletions
diff --git a/src/gamepad.rs b/src/gamepad.rs
index c47464b..87c445c 100644
--- a/src/gamepad.rs
+++ b/src/gamepad.rs
@@ -10,7 +10,7 @@ pub struct Gamepad {
}
pub trait Input {
- fn update_events(&mut self, cycles: u128);
+ fn update_events(&mut self, cycles: u128) -> Option<u128>;
fn get_action_gamepad_reg(&self) -> u8;
fn get_direction_gamepad_reg(&self) -> u8;
}
@@ -40,8 +40,9 @@ impl Gamepad {
}
impl Input for Gamepad {
- fn update_events(&mut self, _cycles: u128) {
+ fn update_events(&mut self, _cycles: u128) -> Option<u128> {
while let Some(_) = self.gilrs.next_event() {}
+ None
}
fn get_action_gamepad_reg(&self) -> u8 {
@@ -114,7 +115,7 @@ impl Keyboard {
}
impl Input for Keyboard {
- fn update_events(&mut self, _cycles: u128) {
+ fn update_events(&mut self, _cycles: u128) -> Option<u128> {
let mut res = 0xf;
let keys = self.keys.borrow();
@@ -155,6 +156,8 @@ impl Input for Keyboard {
}
self.direction_reg = res;
+
+ None
}
fn get_action_gamepad_reg(&self) -> u8 {
@@ -185,7 +188,7 @@ impl GamepadRecorder {
}
impl Input for GamepadRecorder {
- fn update_events(&mut self, cycles: u128) {
+ fn update_events(&mut self, cycles: u128) -> Option<u128> {
self.input.update_events(cycles);
let new_action_reg = self.input.get_action_gamepad_reg();
@@ -212,6 +215,7 @@ impl Input for GamepadRecorder {
self.action_reg = new_action_reg;
self.direction_reg = new_direction_reg;
+ None
}
fn get_action_gamepad_reg(&self) -> u8 {
@@ -252,9 +256,9 @@ impl GamepadReplay {
}
impl Input for GamepadReplay {
- fn update_events(&mut self, cycles: u128) {
+ fn update_events(&mut self, cycles: u128) -> Option<u128> {
if let Some(next_cycle_update) = self.next_cycle_update {
- if cycles > next_cycle_update {
+ if cycles >= next_cycle_update {
let mut inputs: [u8; 2] = [0; 2];
self.record_file
@@ -273,6 +277,8 @@ impl Input for GamepadReplay {
};
}
}
+
+ return self.next_cycle_update;
}
fn get_action_gamepad_reg(&self) -> u8 {
diff --git a/src/main.rs b/src/main.rs
index 66b60e5..7798bc3 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -42,6 +42,9 @@ struct Cli {
#[arg(short, long, default_value_t = 1.0)]
speed: f32,
+
+ #[arg(short, long, default_value_t = false)]
+ debug: bool,
}
fn main() {
@@ -85,12 +88,15 @@ fn main() {
gamepad = Box::new(GamepadRecorder::new(gamepad, record_file));
};
- let mut nanos_sleep: i128 = 0;
+ state.is_debug = cli.debug;
+
+ let mut nanos_sleep: f64 = 0.0;
let mut halt_time = 0;
let mut was_previously_halted = false;
let mut last_ram_bank_enabled = false;
let mut now = SystemTime::now();
+ let mut next_precise_gamepad_update: Option<u128> = None;
loop {
if was_previously_halted && !state.mem.halt {
@@ -114,22 +120,16 @@ fn main() {
state.check_interrupts().unwrap();
state.mem.update_serial();
- nanos_sleep += c as i128 * (consts::CPU_CYCLE_LENGTH_NANOS as f32 / cli.speed) as i128;
- if nanos_sleep > 0 {
- gamepad.update_events(total_cycle_counter);
+ nanos_sleep += c as f64 * (consts::CPU_CYCLE_LENGTH_NANOS as f64 / cli.speed as f64) as f64;
+
+ if nanos_sleep >= 0.0 || next_precise_gamepad_update.map_or(false, |c| (c >= total_cycle_counter)) {
+ next_precise_gamepad_update = gamepad.update_events(total_cycle_counter);
let (action_button_reg, direction_button_reg) = (
gamepad.get_action_gamepad_reg(),
gamepad.get_direction_gamepad_reg(),
);
- if let Some(fb) = state.mem.display.redraw_request {
- if let Some(window::WindowSignal::Exit) = window.update(&fb) {
- break;
- }
- }
- // gamepad.check_special_actions(&mut state.is_debug);
-
if state.mem.joypad_is_action
&& (action_button_reg & (state.mem.joypad_reg >> 4)) != (state.mem.joypad_reg >> 4)
|| (!state.mem.joypad_is_action
@@ -140,6 +140,15 @@ fn main() {
}
state.mem.joypad_reg = direction_button_reg | (action_button_reg << 4);
+ }
+
+
+ if nanos_sleep > 0.0 {
+ if let Some(fb) = state.mem.display.redraw_request {
+ if let Some(window::WindowSignal::Exit) = window.update(&fb) {
+ break;
+ }
+ }
if !cli.loop_lock_timing {
thread::sleep(time::Duration::from_nanos(nanos_sleep as u64 / 10));
@@ -152,7 +161,7 @@ fn main() {
}
nanos_sleep =
- nanos_sleep - SystemTime::now().duration_since(now).unwrap().as_nanos() as i128;
+ nanos_sleep - SystemTime::now().duration_since(now).unwrap().as_nanos() as f64;
now = SystemTime::now();
if last_ram_bank_enabled && !state.mem.ram_bank_enabled {