Skip to content

Commit

Permalink
Use crossterm instead of console (#42)
Browse files Browse the repository at this point in the history
So that the terminal doesn't get corrupted after running the emulator.

Fixes #38
  • Loading branch information
swenson authored Dec 2, 2024
1 parent 5a0e41c commit c030dd5
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 40 deletions.
180 changes: 152 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 1 addition & 1 deletion emulator/app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 16 additions & 9 deletions emulator/app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -96,17 +96,27 @@ fn disassemble(pc: u32, instr: u32) -> String {
}

fn read_console(running: Arc<AtomicBool>, stdin_uart: Option<Arc<Mutex<Option<u8>>>>) {
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 {
Expand Down Expand Up @@ -237,9 +247,6 @@ fn run(cli: Emulator, capture_uart_output: bool) -> io::Result<Vec<u8>> {
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();
}
Expand Down
1 change: 0 additions & 1 deletion emulator/caliptra/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ gdbstub.workspace = true
hex.workspace = true
tock-registers.workspace = true
ctrlc.workspace = true
console.workspace = true

0 comments on commit c030dd5

Please sign in to comment.