diff options
author | Oxore <oxore@protonmail.com> | 2020-02-24 15:08:59 +0300 |
---|---|---|
committer | Oxore <oxore@protonmail.com> | 2020-02-24 15:09:40 +0300 |
commit | 730499e00555eddcbae653bee46f82e1f1d8a06f (patch) | |
tree | 42963ac416632a7f373a76d0892ec76a70cef8f2 | |
parent | a3be84cafaec3ef7ede9c142268cedf12d9f744d (diff) |
Add controller fabric and dummy for websocket controller
-rw-r--r-- | src/main.rs | 54 |
1 files changed, 39 insertions, 15 deletions
diff --git a/src/main.rs b/src/main.rs index 1ec5174..9ae0bae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,20 +11,6 @@ mod core; mod rom; use self::core::Core; -fn core_worker(mut core: Core, should_stop: &Mutex<bool>, cvar: &Condvar) { - loop { - while !(*should_stop.lock().unwrap()) { - println!("0x{:08x}: {}", core.pc(), core.op()); - core.step(); - sleep(time::Duration::from_millis(1000)); - } - - while *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(); @@ -45,6 +31,33 @@ fn cli_controller(asyncpair: Arc<(Mutex<bool>, Condvar)>) { } } +fn ws_controller(asyncpair: Arc<(Mutex<bool>, Condvar)>) { + // TODO implement + println!("{:?}", asyncpair); +} + +fn new_controller(name: &str) -> Result<&dyn Fn(Arc<(Mutex<bool>, Condvar)>), i32> { + match name { + "cli" => Ok(&cli_controller), + "ws" => Ok(&ws_controller), + _ => Err(1), + } +} + +fn core_worker(mut core: Core, should_stop: &Mutex<bool>, cvar: &Condvar) { + loop { + while !(*should_stop.lock().unwrap()) { + println!("0x{:08x}: {}", core.pc(), core.op()); + core.step(); + sleep(time::Duration::from_millis(1000)); + } + + while *should_stop.lock().unwrap() { + let _ = cvar.wait(should_stop.lock().unwrap()).unwrap(); + } + } +} + fn main() { let cli_args_matches = clap::App::new("x51emu") .version("0.1.0") @@ -66,6 +79,7 @@ fn main() { .value_name("INTERFACE") .help("Sets a controlling and I/O interface") .takes_value(true) + .default_value("cli") .possible_values(&["cli", "ws"]), ) .get_matches(); @@ -73,6 +87,16 @@ fn main() { let filename = cli_args_matches .value_of("file") .expect("No file name provided"); + let controller = { + let controller_type_name = cli_args_matches + .value_of("interface") + .expect("No interface type specified"); + new_controller(controller_type_name).expect(&format!( + "Unknown controller type \"{}\"", + controller_type_name + )) + }; + let data = fs::read_to_string(filename).unwrap_or_else(|_| panic!("Unable to read file {}", filename)); @@ -92,5 +116,5 @@ fn main() { core_worker(core, should_stop, condvar); }); - cli_controller(asyncpair); + controller(asyncpair); } |