1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use core::time::Duration;
use std::{thread::sleep, time::Instant};
use crate::{bus::Bus, device::Device};
/// The system clock.
#[non_exhaustive]
pub struct Clock {
/// Time the next tick is due.
pub next_tick: Instant,
/// Target duration for each tick.
pub tick_duration: Duration,
}
impl Default for Clock {
/// Creates a default [`Clock`] with a nominal frequency of 4MHz.
#[inline]
fn default() -> Self {
Self {
next_tick: Instant::now(),
tick_duration: Duration::from_nanos(250), // 4MHz
}
}
}
impl Device for Clock {
/// Waits until the next tick is due.
///
/// If the projected next tick time overflows `usize`, or we are already past the
/// next tick time, returns immediately.
#[inline]
fn tick(&mut self, _bus: &mut Bus) {
let now = Instant::now();
self.next_tick = self
.next_tick
.checked_add(self.tick_duration)
.unwrap_or(now); // too far in the future
let wait = self.next_tick.saturating_duration_since(now);
if !wait.is_zero() {
sleep(wait);
}
}
}