Skip to content

Commit

Permalink
[tui] add 'c' key event to continue
Browse files Browse the repository at this point in the history
  • Loading branch information
Clo91eaf committed May 10, 2024
1 parent 6593209 commit 5eb9a2f
Showing 1 changed file with 36 additions and 18 deletions.
54 changes: 36 additions & 18 deletions src/emulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,14 @@ pub struct Emulator {
/// The DUT which is the peripheral devices of this emulator.
pub dut: Dut,

/// UI information
ui_buffer: UIBuffer,

/// The flag to exit the emulator.
exit: bool,

/// UI information
ui_buffer: UIBuffer,
/// The flag to continue the emulator.
cont: bool,
}

impl Emulator {
Expand All @@ -127,8 +130,9 @@ impl Emulator {
Self {
cpu: Cpu::new(),
dut: Dut::new(),
exit: false,
ui_buffer: UIBuffer::new(),
cont: false,
exit: false,
}
}

Expand All @@ -141,6 +145,17 @@ impl Emulator {
self.exit = true;
}

fn quit(&mut self, terminal: &mut tui::Tui) {
while !self.exit {
terminal.draw(|frame| self.render_frame(frame)).unwrap();
self.handle_events().unwrap();
}
}

fn cont(&mut self) {
self.cont = true;
}

/// Set binary data to the beginning of the DRAM from the emulator console.
pub fn initialize_dram(&mut self, data: Vec<u8>) {
self.cpu.bus.initialize_dram(data);
Expand Down Expand Up @@ -232,22 +247,23 @@ impl Emulator {

frame.render_widget(
Paragraph::new(self.ui_buffer.diff.to_string())
.block(
Block::bordered()
.title("Difftest Status")
.title_alignment(Alignment::Center)
.border_type(BorderType::Rounded),
)
.style(Style::default().fg(Color::Cyan))
.centered(),
.block(
Block::bordered()
.title("Difftest Status")
.title_alignment(Alignment::Center)
.border_type(BorderType::Rounded),
)
.style(Style::default().fg(Color::Cyan))
.centered(),
layout_vertical[2],
);
}

fn handle_key_event(&mut self, key_event: KeyEvent) {
match key_event.code {
KeyCode::Char('q') => self.exit(),
KeyCode::Char('Q') => self.exit(),
KeyCode::Char('q') | KeyCode::Char('Q') => self.exit(),
KeyCode::Char('c') | KeyCode::Char('C') => self.cont(),

_ => {}
}
}
Expand Down Expand Up @@ -398,6 +414,9 @@ impl Emulator {
.ui_buffer
.diff
.push(format!("fatal pc: {:#x}, trap {:#?}", self.cpu.pc, trap));

self.quit(terminal);

return;
}
_ => {}
Expand Down Expand Up @@ -480,18 +499,17 @@ impl Emulator {
self.ui_buffer.diff.push(format!("cpu : {}", cpu_diff));
self.ui_buffer.diff.push(format!("dut : {}", dut_diff));

while !self.exit {
terminal.draw(|frame| self.render_frame(frame)).unwrap();
self.handle_events().unwrap();
}
self.quit(terminal);

return;
}
last_diff = cpu_diff;

// tui
terminal.draw(|frame| self.render_frame(frame)).unwrap();
self.handle_events().unwrap();
if !self.cont {
self.handle_events().unwrap();
}
}
}
}

0 comments on commit 5eb9a2f

Please sign in to comment.