diff options
author | Ben Bridle <bridle.benjamin@gmail.com> | 2024-01-28 14:20:59 +1300 |
---|---|---|
committer | Ben Bridle <bridle.benjamin@gmail.com> | 2024-01-31 07:37:54 +1300 |
commit | a4d06fd54ab22bbdd6b0dc9d5af199fc8be13591 (patch) | |
tree | 69a939728030f3feeb682bcd1765d9624f6b9a1a | |
parent | b10c26cf6b10eab4bd4847c27c7b4d65001d51b7 (diff) | |
download | bedrock-pc-a4d06fd54ab22bbdd6b0dc9d5af199fc8be13591.zip |
Change clock cumulative timer to use 256th ticks instead of seconds
The "Cumulative seconds" port of the CLOCK device has been changed
to be called "Cumulative timer", with the units changing from seconds
to 1/256 second ticks.
-rw-r--r-- | src/devices.rs | 4 | ||||
-rw-r--r-- | src/devices/clock.rs | 18 |
2 files changed, 14 insertions, 8 deletions
diff --git a/src/devices.rs b/src/devices.rs index 562b664..a17514e 100644 --- a/src/devices.rs +++ b/src/devices.rs @@ -122,9 +122,9 @@ impl DeviceBus for StandardDevices { 0x33 => self.clock.hour(), 0x34 => self.clock.minute(), 0x35 => self.clock.second(), - 0x36 => { self.clock.update_cumulative_seconds(); read_h!(self.clock.cumulative_seconds) }, - 0x37 => read_l!(self.clock.cumulative_seconds), 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), 0x39 => read_l!(self.clock.timer_1), 0x3A => { self.clock.update_timer_2(); read_h!(self.clock.timer_2) } 0x3B => read_l!(self.clock.timer_2), diff --git a/src/devices/clock.rs b/src/devices/clock.rs index 063c67b..b974a4a 100644 --- a/src/devices/clock.rs +++ b/src/devices/clock.rs @@ -8,6 +8,10 @@ macro_rules! now { () => { 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 }; @@ -30,6 +34,7 @@ macro_rules! generate_update_timer_method { self. [< timer_ $i >] = ticks_since_time!(self.[< timer_ $i _instant >]); if self. [< timer_ $i >] == 0 { self.wake_flag = true; } } + self. [< timer_ $i >] } }}; } @@ -38,7 +43,7 @@ pub struct ClockDevice { pub wake_flag: bool, pub boot_time: Instant, - pub cumulative_seconds: u16, + pub cumulative_timer: u16, pub timer_1_instant: Instant, pub timer_2_instant: Instant, @@ -56,7 +61,7 @@ impl ClockDevice { wake_flag: false, boot_time: Instant::now(), - cumulative_seconds: 0, + cumulative_timer: 0, timer_1_instant: Instant::now(), timer_2_instant: Instant::now(), @@ -69,10 +74,6 @@ impl ClockDevice { } } - pub fn update_cumulative_seconds(&mut self) { - self.cumulative_seconds = self.boot_time.elapsed().as_secs() as u16; - } - generate_set_timer_method!{1} generate_set_timer_method!{2} generate_set_timer_method!{3} @@ -83,6 +84,11 @@ impl ClockDevice { generate_update_timer_method!{3} generate_update_timer_method!{4} + pub fn update_cumulative_timer(&mut self) -> u16 { + self.cumulative_timer = to_ticks!(self.boot_time.elapsed()); + return self.cumulative_timer; + } + pub fn year(&self) -> u8 { now!().year().saturating_sub(2000).try_into().unwrap_or(u8::MAX) } |