use std::env; use std::fs; use std::io; use std::io::prelude::*; use std::{thread, time}; use std::thread::sleep; use std::sync::{Arc, Mutex, Condvar}; use std::string::String; mod core; mod ram; use self::core::Core; fn core_worker(mut core: Core, should_stop: &Mutex, cvar: &Condvar) { loop { while false == *should_stop.lock().unwrap() { core.step(); println!("{}", core.op()); 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, 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 args: Vec<_> = env::args().collect(); if args.len() < 2 { println!("Please, specify ihex file"); return; } let data = fs::read_to_string(&args[1]) .expect(&format!("Unable to read file {}", &args[1])); let asyncpair = Arc::new((Mutex::new(false), Condvar::new())); let asyncpair2 = asyncpair.clone(); let core = match Core::with_ram_from_hex(String::from(data)) { Ok(value) => value, Err(err_string) => { println!("{}", err_string); return; }, }; println!("{}", core.op()); thread::spawn(move || { let (ref should_stop, ref condvar) = *asyncpair2; core_worker(core, should_stop, condvar); }); cli_controller(asyncpair); }