diff options
author | Ben Bridle <bridle.benjamin@gmail.com> | 2024-01-28 14:31:12 +1300 |
---|---|---|
committer | Ben Bridle <bridle.benjamin@gmail.com> | 2024-01-31 07:37:54 +1300 |
commit | 521e525763723cbc5d65b37ff47f34af48f50a2f (patch) | |
tree | aed87ecc93cbbd1d07ea94533bc43ee11fcce731 | |
parent | a4d06fd54ab22bbdd6b0dc9d5af199fc8be13591 (diff) | |
download | bedrock-pc-521e525763723cbc5d65b37ff47f34af48f50a2f.zip |
Refactor code without changing functionality
An effort to make the code shorter and more readable. This mostly
affects the code for the clock device.
-rw-r--r-- | src/devices.rs | 21 | ||||
-rw-r--r-- | src/devices/clock.rs | 51 |
2 files changed, 30 insertions, 42 deletions
diff --git a/src/devices.rs b/src/devices.rs index a17514e..5b274c9 100644 --- a/src/devices.rs +++ b/src/devices.rs @@ -109,12 +109,12 @@ impl DeviceBus for StandardDevices { 0x27 => no_read!(), 0x28 => { self.math.multiply(); read_h!(self.math.product_high) }, 0x29 => { self.math.multiply(); read_l!(self.math.product_high) }, - 0x2A => { self.math.multiply(); read_h!(self.math.product_low) }, - 0x2B => { self.math.multiply(); read_l!(self.math.product_low) }, - 0x2C => { self.math.divide(); read_h!(self.math.quotient) }, - 0x2D => { self.math.divide(); read_l!(self.math.quotient) }, - 0x2E => { self.math.modulo(); read_h!(self.math.remainder) }, - 0x2F => { self.math.modulo(); read_l!(self.math.remainder) }, + 0x2A => { self.math.multiply(); read_h!(self.math.product_low) }, + 0x2B => { self.math.multiply(); read_l!(self.math.product_low) }, + 0x2C => { self.math.divide(); read_h!(self.math.quotient) }, + 0x2D => { self.math.divide(); read_l!(self.math.quotient) }, + 0x2E => { self.math.modulo(); read_h!(self.math.remainder) }, + 0x2F => { self.math.modulo(); read_l!(self.math.remainder) }, // Clock 0x30 => self.clock.year(), 0x31 => self.clock.month(), @@ -122,15 +122,15 @@ impl DeviceBus for StandardDevices { 0x33 => self.clock.hour(), 0x34 => self.clock.minute(), 0x35 => self.clock.second(), - 0x38 => { self.clock.update_timer_1(); read_h!(self.clock.timer_1) } 0x36 => read_h!(self.clock.update_cumulative_timer()), 0x37 => read_l!(self.clock.cumulative_timer), + 0x38 => read_h!(self.clock.update_timer_1()), 0x39 => read_l!(self.clock.timer_1), - 0x3A => { self.clock.update_timer_2(); read_h!(self.clock.timer_2) } + 0x3A => read_h!(self.clock.update_timer_2()), 0x3B => read_l!(self.clock.timer_2), - 0x3C => { self.clock.update_timer_3(); read_h!(self.clock.timer_3) } + 0x3C => read_h!(self.clock.update_timer_3()), 0x3D => read_l!(self.clock.timer_3), - 0x3E => { self.clock.update_timer_4(); read_h!(self.clock.timer_4) } + 0x3E => read_h!(self.clock.update_timer_4()), 0x3F => read_l!(self.clock.timer_4), // Input 0x40 => read_h!(self.input.mouse_position.x), @@ -180,6 +180,7 @@ impl DeviceBus for StandardDevices { 0x8E => read_lh!(self.scratch.max_capacity), 0x8F => read_ll!(self.scratch.max_capacity), // Stream + // File 0xA0 => read_b!(self.file.file.is_some()), 0xA1 => read_b!(self.file.operation_state), diff --git a/src/devices/clock.rs b/src/devices/clock.rs index b974a4a..3faa604 100644 --- a/src/devices/clock.rs +++ b/src/devices/clock.rs @@ -5,36 +5,36 @@ macro_rules! now { () => { .unwrap_or_else(|_| time::OffsetDateTime::now_utc()) };} -macro_rules! time_from_ticks { - ($ticks:expr) => { Instant::now() + Duration::from_millis(($ticks * 4).into()) }; -} macro_rules! to_ticks { ($duration:expr) => {($duration.as_millis() / 4) as u16}; } macro_rules! from_ticks { ($ticks:expr) => {Duration::from_millis(($ticks * 4).into())}; } -macro_rules! ticks_since_time { - ($time:expr) => { ($time.duration_since(Instant::now()).as_millis() / 4) as u16 }; -} /// Create a method to set the instant of a timer. macro_rules! generate_set_timer_method { - ($i:tt) => {mini_paste::item!{ + ($i:tt) => { mini_paste::item!{ pub fn [< set_timer_ $i >] (&mut self) { - self. [< timer_ $i _instant >] = time_from_ticks!(self. [< timer_ $i >]); + let ticks = &mut self. [< timer_ $i >]; + let instant = &mut self. [< timer_ $i _instant >]; + + *instant = Instant::now() + from_ticks!(*ticks); } }}; } /// Create a method to update the value of a timer. macro_rules! generate_update_timer_method { - ($i:tt) => {mini_paste::item!{ - pub fn [< update_timer_ $i >] (&mut self) { - if self. [< timer_ $i >] > 0 { - self. [< timer_ $i >] = ticks_since_time!(self.[< timer_ $i _instant >]); - if self. [< timer_ $i >] == 0 { self.wake_flag = true; } + ($i:tt) => { mini_paste::item!{ + pub fn [< update_timer_ $i >] (&mut self) -> u16 { + let ticks = &mut self. [< timer_ $i >]; + let instant = &mut self. [< timer_ $i _instant >]; + + if *ticks > 0 { + *ticks = to_ticks!(instant.duration_since(Instant::now())); + if *ticks == 0 { self.wake_flag = true; } } - self. [< timer_ $i >] + *ticks } }}; } @@ -114,23 +114,10 @@ impl ClockDevice { } pub fn shortest_active_timer(&self) -> Option<Duration> { - let mut is_some = false; - let mut value = u16::MAX; - macro_rules! contribute_to_min { - ($timer:expr) => { - if $timer > 0 { - is_some = true; - value = std::cmp::min(value, $timer); - } - }; - } - contribute_to_min!(self.timer_1); - contribute_to_min!(self.timer_2); - contribute_to_min!(self.timer_3); - contribute_to_min!(self.timer_4); - match is_some { - true => Some(Duration::from_millis((value*4).into())), - false => None, - } + [self.timer_1, self.timer_2, self.timer_3, self.timer_4] + .iter() + .filter(|t| **t > 0) + .min() + .and_then(|t| Some(from_ticks!(t))) } } |