summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--Cargo.lock6
-rw-r--r--Cargo.toml7
-rw-r--r--src/core.rs14
-rw-r--r--src/main.rs54
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);
+}