diff options
author | Oxore <oxore@protonmail.com> | 2019-07-14 01:30:57 +0300 |
---|---|---|
committer | Oxore <oxore@protonmail.com> | 2019-07-14 13:59:20 +0300 |
commit | 24e3ef936efd5b9239de76088efa02d06537ceb6 (patch) | |
tree | 2ec8875dcd264e9b145946cdc9f21d86f2da5d27 |
Initial commit
Implement CLI and CPU core skeletons
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Cargo.lock | 6 | ||||
-rw-r--r-- | Cargo.toml | 7 | ||||
-rw-r--r-- | src/core.rs | 14 | ||||
-rw-r--r-- | src/main.rs | 54 |
5 files changed, 83 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..53eaa21 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..f9daf50 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "smemu" +version = "0.1.0" + diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..962e040 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "smemu" +version = "0.1.0" +authors = ["Oxore <oxore@protonmail.com>"] +edition = "2018" + +[dependencies] diff --git a/src/core.rs b/src/core.rs new file mode 100644 index 0000000..0062e1c --- /dev/null +++ b/src/core.rs @@ -0,0 +1,14 @@ +pub struct Core { + pc: u16, +} + +impl Core { + pub fn new() -> Self { + Core { pc: 0 } + } + + pub fn step(&mut self) -> u16 { + self.pc += 1; + self.pc + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..f5af35c --- /dev/null +++ b/src/main.rs @@ -0,0 +1,54 @@ +use std::io; +use std::io::prelude::*; +use std::{thread, time}; +use std::thread::sleep; +use std::sync::{Arc, Mutex, Condvar}; + +mod core; +use self::core::Core; + +fn core_worker(mut core: Core, should_stop: &Mutex<bool>, cvar: &Condvar) { + loop { + while false == *should_stop.lock().unwrap() { + println!("step {}", core.step()); + sleep(time::Duration::from_millis(1000)); + } + + while true == *should_stop.lock().unwrap() { + let _ = cvar.wait(should_stop.lock().unwrap()).unwrap(); + } + } +} + +fn cli_controller(asyncpair: Arc<(Mutex<bool>, Condvar)>) { + let &(ref should_stop, ref cvar) = &*asyncpair; + let stdin = io::stdin(); + for line in stdin.lock().lines() { + let line = line.unwrap(); + match line.as_ref() { + "stop" => { + println!("stop"); + *should_stop.lock().unwrap() = true; + }, + "start" => { + println!("start"); + *should_stop.lock().unwrap() = false; + cvar.notify_one(); + }, + _ => () + } + } +} + +fn main() { + let core = Core::new(); + let asyncpair = Arc::new((Mutex::new(false), Condvar::new())); + let asyncpair2 = asyncpair.clone(); + + thread::spawn(move || { + let (ref should_stop, ref condvar) = *asyncpair2; + core_worker(core, should_stop, condvar); + }); + + cli_controller(asyncpair); +} |