summaryrefslogtreecommitdiff
path: root/src/devices/clock.rs
diff options
context:
space:
mode:
authorBen Bridle <bridle.benjamin@gmail.com>2024-10-28 20:25:01 +1300
committerBen Bridle <bridle.benjamin@gmail.com>2024-10-28 20:29:12 +1300
commit1a830a3d1b9d99653322d5ae49ea8165de7ed9d0 (patch)
tree798e77b6fcf2438b1c2538a67efe856a2f7cb979 /src/devices/clock.rs
parent03c4b069e1806af256730639cefdae115b24401a (diff)
downloadbedrock-pc-01ba3901441af778d473f44acaee953d185245e7.zip
Rewrite emulatorv1.0.0-alpha1
This is a complete rewrite and restructure of the entire emulator project, as part of the effort in locking down the Bedrock specification and in creating much better tooling for creating and using Bedrock programs. This commit adds a command-line argument scheme, an embedded assembler, a headless emulator for use in non-graphical environments, deferred window creation for programs that do not access the screen device, and new versions of phosphor and bedrock-core. The new version of phosphor supports multi-window programs, which will make it possible to implement program forking in the system device later on, and the new version of bedrock-core implements the final core specification.
Diffstat (limited to 'src/devices/clock.rs')
-rw-r--r--src/devices/clock.rs124
1 files changed, 0 insertions, 124 deletions
diff --git a/src/devices/clock.rs b/src/devices/clock.rs
deleted file mode 100644
index 60b1cad..0000000
--- a/src/devices/clock.rs
+++ /dev/null
@@ -1,124 +0,0 @@
-use std::time::{Duration, Instant};
-
-macro_rules! to_ticks { ($dur:expr) => {($dur.as_millis() / 4) as u16}; }
-macro_rules! from_ticks { ($ticks:expr) => {Duration::from_millis(($ticks * 4).into())}; }
-macro_rules! now { () => {
- time::OffsetDateTime::now_local().unwrap_or_else(|_| time::OffsetDateTime::now_utc())};}
-
-/// Create a method to set the instant of a timer.
-macro_rules! generate_set_timer_method {
- ($i:tt) => { mini_paste::item!{
- pub fn [< set_timer_ $i >] (&mut self) {
- let ticks = &mut self. [< timer_ $i >];
- let instant = &mut self. [< timer_ $i _end >];
-
- *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) -> u16 {
- let ticks = &mut self. [< timer_ $i >];
- let instant = &mut self. [< timer_ $i _end >];
-
- if *ticks > 0 {
- *ticks = to_ticks!(instant.duration_since(Instant::now()));
- if *ticks == 0 { self.wake_flag = true; }
- }
- return *ticks;
- }
- }};
-}
-
-pub struct ClockDevice {
- pub wake_flag: bool,
-
- pub program_start: Instant,
- pub uptime: u16,
-
- pub timer_1_end: Instant,
- pub timer_2_end: Instant,
- pub timer_3_end: Instant,
- pub timer_4_end: Instant,
- pub timer_1: u16,
- pub timer_2: u16,
- pub timer_3: u16,
- pub timer_4: u16,
-}
-
-impl ClockDevice {
- pub fn new() -> Self {
- Self {
- wake_flag: false,
-
- program_start: Instant::now(),
- uptime: 0,
-
- timer_1_end: Instant::now(),
- timer_2_end: Instant::now(),
- timer_3_end: Instant::now(),
- timer_4_end: Instant::now(),
- timer_1: 0,
- timer_2: 0,
- timer_3: 0,
- timer_4: 0,
- }
- }
-
- generate_set_timer_method!{1}
- generate_set_timer_method!{2}
- generate_set_timer_method!{3}
- generate_set_timer_method!{4}
-
- generate_update_timer_method!{1}
- generate_update_timer_method!{2}
- generate_update_timer_method!{3}
- generate_update_timer_method!{4}
-
- pub fn update_timers(&mut self) {
- self.update_timer_1();
- self.update_timer_2();
- self.update_timer_3();
- self.update_timer_4();
- }
-
- pub fn update_uptime(&mut self) -> u16 {
- self.uptime = to_ticks!(self.program_start.elapsed());
- return self.uptime;
- }
-
- pub fn year(&self) -> u8 {
- now!().year().saturating_sub(2000).try_into().unwrap_or(u8::MAX)
- }
-
- pub fn month(&self) -> u8 {
- now!().month() as u8 - 1
- }
-
- pub fn day(&self) -> u8 {
- now!().day() as u8 - 1
- }
-
- pub fn hour(&self) -> u8 {
- now!().hour()
- }
-
- pub fn minute(&self) -> u8 {
- now!().minute()
- }
-
- pub fn second(&self) -> u8 {
- now!().second()
- }
-
- pub fn time_to_next_wake(&self) -> Option<Duration> {
- [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)))
- }
-}