diff --git a/src/emulator/instruction.rs b/src/emulator/instruction.rs new file mode 100644 index 0000000..a04a3de --- /dev/null +++ b/src/emulator/instruction.rs @@ -0,0 +1,22 @@ +use std::fmt::Display; + +#[derive(Debug, Clone)] +pub enum Instruction { + HLT, + REG(u8, usize), + ADD(u8, u8, u8), +} + +impl Display for Instruction { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match self { + Instruction::HLT => format!("HLT"), + Instruction::REG(a, b) => format!("REG {a}, {b}"), + Instruction::ADD(a, b, c) => format!("ADD {a}, {b}, {c}"), + } + ) + } +} diff --git a/src/emulator/mod.rs b/src/emulator/mod.rs index 3ca308d..2cfecb8 100644 --- a/src/emulator/mod.rs +++ b/src/emulator/mod.rs @@ -1,16 +1,11 @@ +pub mod instruction; +pub mod registry; + use std::fmt::Display; use osui::state::{DependencyHandler, State}; -#[derive(Debug, Clone)] -pub struct Registry(pub State<[usize; 8]>); - -#[derive(Debug, Clone)] -pub enum Instruction { - HLT, - REG(u8, usize), - ADD(u8, u8, u8), -} +use crate::emulator::{instruction::Instruction, registry::Registry}; pub struct Emulator { pub pc: State, @@ -22,7 +17,7 @@ pub struct Emulator { impl Emulator { pub fn run(&self) { loop { - std::thread::sleep(std::time::Duration::from_millis(300)); + std::thread::sleep(std::time::Duration::from_millis(100)); let mut pc = self.pc.get(); if let Some(inst) = self.instructions.get(**pc) { **self.inst.get() = inst.clone(); @@ -85,17 +80,3 @@ impl DependencyHandler for Registry { self.0.check() } } - -impl Display for Instruction { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{}", - match self { - Instruction::HLT => format!("HLT"), - Instruction::REG(a, b) => format!("REG {a}, {b}"), - Instruction::ADD(a, b, c) => format!("ADD {a}, {b}, {c}"), - } - ) - } -} diff --git a/src/emulator/registry.rs b/src/emulator/registry.rs new file mode 100644 index 0000000..d10246a --- /dev/null +++ b/src/emulator/registry.rs @@ -0,0 +1,4 @@ +use osui::state::State; + +#[derive(Debug, Clone)] +pub struct Registry(pub State<[usize; 8]>); diff --git a/src/main.rs b/src/main.rs index 4130ae1..917a131 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ use std::sync::Arc; use osui::prelude::*; -use crate::emulator::{Instruction, Registry}; +use crate::emulator::{instruction::Instruction, registry::Registry}; fn main() { let screen = Screen::new(); @@ -21,7 +21,7 @@ fn main() { screen.run().unwrap(); } -fn app(_screen: &Arc, instructions: Vec) -> Rsx { +fn app(_screen: &Arc, instructions: Vec) -> Rsx { let pc = use_state::(0); let inst = use_state::(Instruction::HLT); let registers = Registry(use_state([0; 8]));