From c030dd58c1f1fa193f4cb2f4992713da56efeb93 Mon Sep 17 00:00:00 2001 From: Christopher Swenson Date: Mon, 2 Dec 2024 12:59:17 -0800 Subject: [PATCH] Use crossterm instead of console (#42) So that the terminal doesn't get corrupted after running the emulator. Fixes #38 --- Cargo.lock | 180 +++++++++++++++++++++++++++++------ Cargo.toml | 2 +- emulator/app/Cargo.toml | 2 +- emulator/app/src/main.rs | 25 +++-- emulator/caliptra/Cargo.toml | 1 - 5 files changed, 170 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 83b30c3..8bb138e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -606,7 +606,7 @@ dependencies = [ "strsim", "terminal_size", "unicase", - "unicode-width 0.2.0", + "unicode-width", ] [[package]] @@ -664,19 +664,6 @@ dependencies = [ "kernel", ] -[[package]] -name = "console" -version = "0.15.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb" -dependencies = [ - "encode_unicode", - "lazy_static", - "libc", - "unicode-width 0.1.14", - "windows-sys 0.52.0", -] - [[package]] name = "const-oid" version = "0.9.6" @@ -728,6 +715,31 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b" +[[package]] +name = "crossterm" +version = "0.28.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "829d955a0bb380ef178a640b91779e3987da38c9aea133b20614cfed8cdea9c6" +dependencies = [ + "bitflags 2.6.0", + "crossterm_winapi", + "mio", + "parking_lot", + "rustix", + "signal-hook", + "signal-hook-mio", + "winapi", +] + +[[package]] +name = "crossterm_winapi" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acdd7c62a3665c7f6830a51635d9ac9b23ed385797f70a83bb8bafe9c572ab2b" +dependencies = [ + "winapi", +] + [[package]] name = "crypto-bigint" version = "0.5.5" @@ -908,7 +920,7 @@ dependencies = [ "caliptra-emu-cpu", "caliptra-emu-periph", "clap 4.5.21", - "console", + "crossterm", "ctrlc", "elf", "emulator-bus", @@ -942,7 +954,6 @@ dependencies = [ "caliptra-hw-model", "caliptra-registers", "clap 4.5.21", - "console", "ctrlc", "elf", "gdbstub", @@ -1009,12 +1020,6 @@ dependencies = [ name = "emulator-types" version = "0.1.0" -[[package]] -name = "encode_unicode" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" - [[package]] name = "enum_primitive" version = "0.1.0" @@ -1160,6 +1165,12 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "hermit-abi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" + [[package]] name = "hex" version = "0.4.3" @@ -1552,6 +1563,16 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4ce301924b7887e9d637144fdade93f9dfff9b60981d4ac161db09720d39aa5" +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "log" version = "0.4.22" @@ -1588,6 +1609,19 @@ dependencies = [ "autocfg", ] +[[package]] +name = "mio" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" +dependencies = [ + "hermit-abi", + "libc", + "log", + "wasi", + "windows-sys 0.52.0", +] + [[package]] name = "nix" version = "0.26.4" @@ -1726,6 +1760,29 @@ dependencies = [ "sha2", ] +[[package]] +name = "parking_lot" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets", +] + [[package]] name = "paste" version = "1.0.15" @@ -1874,6 +1931,15 @@ dependencies = [ "getrandom", ] +[[package]] +name = "redox_syscall" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f" +dependencies = [ + "bitflags 2.6.0", +] + [[package]] name = "regex" version = "1.11.1" @@ -2015,6 +2081,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[package]] name = "sec1" version = "0.7.3" @@ -2103,6 +2175,36 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "signal-hook" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8621587d4798caf8eb44879d42e56b9a93ea5dcd315a6487c357130095b62801" +dependencies = [ + "libc", + "signal-hook-registry", +] + +[[package]] +name = "signal-hook-mio" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34db1a06d485c9142248b7a054f034b349b212551f3dfd19c94d45a754a217cd" +dependencies = [ + "libc", + "mio", + "signal-hook", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" +dependencies = [ + "libc", +] + [[package]] name = "signature" version = "2.2.0" @@ -2113,6 +2215,12 @@ dependencies = [ "rand_core", ] +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + [[package]] name = "smlang" version = "0.6.0" @@ -2306,12 +2414,6 @@ version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" -[[package]] -name = "unicode-width" -version = "0.1.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" - [[package]] name = "unicode-width" version = "0.2.0" @@ -2421,6 +2523,22 @@ version = "0.2.95" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d" +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + [[package]] name = "winapi-util" version = "0.1.9" @@ -2430,6 +2548,12 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-core" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index a314481..be6f5a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,7 +48,7 @@ caliptra-emu-periph = { git = "https://github.com/chipsalliance/caliptra-sw.git" caliptra-hw-model = { git = "https://github.com/chipsalliance/caliptra-sw.git", rev = "2f6de531e321b7bb24b17b1bd02b43d2854aef3a" } caliptra-registers = { git = "https://github.com/chipsalliance/caliptra-sw.git", rev = "2f6de531e321b7bb24b17b1bd02b43d2854aef3a" } clap_derive = "4.5.11" -console = "0.15.8" +crossterm = "0.28.1" ctrlc = "3.4.5" elf = "0.7.4" emulator-bus = { path = "emulator/bus" } diff --git a/emulator/app/Cargo.toml b/emulator/app/Cargo.toml index be24a38..32cf26f 100644 --- a/emulator/app/Cargo.toml +++ b/emulator/app/Cargo.toml @@ -11,7 +11,7 @@ edition = "2021" caliptra-emu-cpu.workspace = true caliptra-emu-periph.workspace = true clap.workspace = true -console.workspace = true +crossterm.workspace = true ctrlc.workspace = true elf.workspace = true emulator-bus.workspace = true diff --git a/emulator/app/src/main.rs b/emulator/app/src/main.rs index fcde746..d95cf18 100644 --- a/emulator/app/src/main.rs +++ b/emulator/app/src/main.rs @@ -20,7 +20,7 @@ mod gdb; use caliptra_emu_cpu::{Cpu as CaliptraMainCpu, StepAction as CaliptraMainStepAction}; use caliptra_emu_periph::CaliptraRootBus as CaliptraMainRootBus; use clap::Parser; -use console::{Key, Term}; +use crossterm::event::{Event, KeyCode, KeyEvent}; use emulator_bus::{Clock, Timer}; use emulator_caliptra::{start_caliptra, StartCaliptraArgs}; use emulator_cpu::{Cpu, Pic, RvInstr, StepAction}; @@ -96,17 +96,27 @@ fn disassemble(pc: u32, instr: u32) -> String { } fn read_console(running: Arc, stdin_uart: Option>>>) { - let term = Term::stdout(); let mut buffer = vec![]; if let Some(ref stdin_uart) = stdin_uart { while running.load(std::sync::atomic::Ordering::Relaxed) { if buffer.is_empty() { - match term.read_key() { - Ok(Key::Char(ch)) => buffer.extend_from_slice(ch.to_string().as_bytes()), - Ok(Key::Enter) => { + match crossterm::event::read() { + Ok(Event::Key(KeyEvent { + code: KeyCode::Char(ch), + .. + })) => { + buffer.extend_from_slice(ch.to_string().as_bytes()); + } + Ok(Event::Key(KeyEvent { + code: KeyCode::Enter, + .. + })) => { buffer.push(b'\n'); } - Ok(Key::Backspace) => { + Ok(Event::Key(KeyEvent { + code: KeyCode::Backspace, + .. + })) => { if !buffer.is_empty() { buffer.pop(); } else { @@ -237,9 +247,6 @@ fn run(cli: Emulator, capture_uart_output: bool) -> io::Result> { if std::io::stdout().is_terminal() { ctrlc::set_handler(move || { running_clone.store(false, std::sync::atomic::Ordering::Relaxed); - Term::stdout().clear_line().unwrap(); - Term::stdout().show_cursor().unwrap(); - println!("Terminal might be in a bad state. Run \"stty sane && reset\" to fix it."); }) .unwrap(); } diff --git a/emulator/caliptra/Cargo.toml b/emulator/caliptra/Cargo.toml index afbea9d..64da3ed 100644 --- a/emulator/caliptra/Cargo.toml +++ b/emulator/caliptra/Cargo.toml @@ -21,4 +21,3 @@ gdbstub.workspace = true hex.workspace = true tock-registers.workspace = true ctrlc.workspace = true -console.workspace = true