Skip to content

Commit

Permalink
[ui] add tab ui
Browse files Browse the repository at this point in the history
  • Loading branch information
Clo91eaf committed May 11, 2024
1 parent feafae9 commit 2f77cd2
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 116 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ tracing = "0.1.40"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
crossterm = "0.27.0"
ratatui = "0.26.2"
strum = "0.26.2"

[build-dependencies]
verilator = { path = "./dependencies/verilated-rs/verilator", features = ["gen", "module"] }
118 changes: 27 additions & 91 deletions src/emulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ use crate::dut::Dut;
use crate::exception::Trap;
use crate::tui::{Tui, UI};
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind};
use ratatui::{
prelude::*,
widgets::{block::*, *},
};
use std::io;

#[derive(Default)]
Expand Down Expand Up @@ -81,7 +77,9 @@ impl Emulator {

fn quit(&mut self, terminal: &mut Tui) {
while !self.ui.cmd.exit {
terminal.draw(|frame| self.render_frame(frame)).unwrap();
terminal
.draw(|frame| frame.render_widget(&self.ui, frame.size()))
.unwrap();
self.handle_events().unwrap();
}
}
Expand Down Expand Up @@ -123,81 +121,12 @@ impl Emulator {
}
}

fn render_frame(&self, frame: &mut Frame) {
// layout
let layout_vertical = Layout::default()
.direction(Direction::Vertical)
.constraints(vec![
Constraint::Percentage(40),
Constraint::Percentage(40),
Constraint::Percentage(20),
])
.split(frame.size());

let layout_horizontal = Layout::default()
.direction(Direction::Horizontal)
.constraints(vec![Constraint::Percentage(50), Constraint::Percentage(50)])
.split(layout_vertical[1]);

// render
frame.render_widget(
Paragraph::new(self.ui.buffer.inst.to_string())
.block(
Block::bordered()
.title("Instructions")
.title_alignment(Alignment::Left)
.border_type(BorderType::Rounded),
)
.style(Style::default().fg(Color::Cyan))
.left_aligned(),
layout_vertical[0],
);

frame.render_widget(
Paragraph::new(self.ui.buffer.cpu.to_string())
.block(
Block::bordered()
.title("CPU")
.title_alignment(Alignment::Left)
.border_type(BorderType::Rounded),
)
.style(Style::default().fg(Color::Cyan))
.left_aligned(),
layout_horizontal[0],
);

frame.render_widget(
Paragraph::new(self.ui.buffer.dut.to_string())
.block(
Block::bordered()
.title("DUT")
.title_alignment(Alignment::Left)
.border_type(BorderType::Rounded),
)
.style(Style::default().fg(Color::Cyan))
.left_aligned(),
layout_horizontal[1],
);

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(),
layout_vertical[2],
);
}

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

KeyCode::Char('h') | KeyCode::Left => self.ui.previous_tab(),
KeyCode::Char('l') | KeyCode::Right => self.ui.next_tab(),
_ => {}
}
}
Expand Down Expand Up @@ -251,7 +180,7 @@ impl Emulator {
match self.cpu.gpr.record {
Some((wnum, wdata)) => {
cpu_diff = DebugInfo::new(true, pc, wnum, wdata);
info!("[cpu] record: true, pc: {:#x}, inst: {}", pc, self.cpu.inst);
info!("[cpu] record: true, pc: {:#x}, inst: {}", pc, self.cpu.inst);
break;
}
None => {
Expand Down Expand Up @@ -329,7 +258,7 @@ impl Emulator {

/// Start executing the emulator with difftest and tui.
pub fn start_diff_tui(&mut self, terminal: &mut Tui) {
self.ui.buffer.diff.push("running".to_string());
self.ui.selected_tab.ui_buffer.diff.push("running".to_string());
let mut last_diff = DebugInfo::default();

while !self.ui.cmd.exit {
Expand All @@ -341,15 +270,17 @@ impl Emulator {

self
.ui
.buffer
.selected_tab
.ui_buffer
.inst
.push(format!("pc: {:#x}, inst: {}", pc, self.cpu.inst));

match trap {
Trap::Fatal => {
self
.ui
.buffer
.selected_tab
.ui_buffer
.diff
.push(format!("fatal pc: {:#x}, trap {:#?}", self.cpu.pc, trap));

Expand All @@ -365,15 +296,17 @@ impl Emulator {
cpu_diff = DebugInfo::new(true, pc, wnum, wdata);
self
.ui
.buffer
.selected_tab
.ui_buffer
.cpu
.push(format!("record: true, pc: {:#x}, inst: {}", pc, self.cpu.inst));
break;
}
None => {
self
.ui
.buffer
.selected_tab
.ui_buffer
.cpu
.push(format!("record: false, pc: {:#x}, inst: {}", pc, self.cpu.inst));
}
Expand All @@ -396,7 +329,7 @@ impl Emulator {
// should be `Exception::InstructionAccessFault`.
dut.data = self.cpu.bus.read(p_addr, crate::cpu::DOUBLEWORD).unwrap();

self.ui.buffer.dut.push(format!(
self.ui.selected_tab.ui_buffer.dut.push(format!(
"{}, data_sram: addr: {:#x}, data: {:#018x}",
dut.ticks, data_sram.addr, dut.data
))
Expand All @@ -412,7 +345,7 @@ impl Emulator {
// should be `Exception::InstructionAccessFault`.
dut.inst = self.cpu.bus.read(p_pc, crate::cpu::WORD).unwrap() as u32;

self.ui.buffer.dut.push(format!(
self.ui.selected_tab.ui_buffer.dut.push(format!(
"{}, inst_sram: pc: {:#x}, inst: {:#010x}",
dut.ticks, inst_sram.addr, dut.inst
))
Expand All @@ -423,7 +356,7 @@ impl Emulator {
break;
}
}
self.ui.buffer.dut.push(format!(
self.ui.selected_tab.ui_buffer.dut.push(format!(
"{}, pc: {:#010x} wnum: {} wdata: {:#018x}",
dut.ticks,
dut.top.debug_pc(),
Expand All @@ -433,16 +366,17 @@ impl Emulator {

// ==================== diff ====================
if cpu_diff != dut_diff {
self.ui.buffer.diff.clear();
self.ui.selected_tab.ui_buffer.diff.clear();

self
.ui
.buffer
.selected_tab
.ui_buffer
.diff
.push("difftest failed. press 'q' or 'Q' to quit. ".to_string());
self.ui.buffer.diff.push(format!("last: {}", last_diff));
self.ui.buffer.diff.push(format!("cpu : {}", cpu_diff));
self.ui.buffer.diff.push(format!("dut : {}", dut_diff));
self.ui.selected_tab.ui_buffer.diff.push(format!("last: {}", last_diff));
self.ui.selected_tab.ui_buffer.diff.push(format!("cpu : {}", cpu_diff));
self.ui.selected_tab.ui_buffer.diff.push(format!("dut : {}", dut_diff));

self.quit(terminal);

Expand All @@ -451,7 +385,9 @@ impl Emulator {
last_diff = cpu_diff;

// tui
terminal.draw(|frame| self.render_frame(frame)).unwrap();
terminal
.draw(|frame| frame.render_widget(&self.ui, frame.size()))
.unwrap();
if !self.ui.cmd.r#continue {
self.handle_events().unwrap();
}
Expand Down
10 changes: 5 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ fn main() -> anyhow::Result<()> {
emu.initialize_disk(img_data);
emu.initialize_pc(DRAM_BASE);

let mut terminal = tui::init()?;

match (args.diff, args.tui) {
(true, true) => emu.start_diff_tui(&mut terminal),
(true, true) => {
let mut terminal = tui::init()?;
emu.start_diff_tui(&mut terminal);
tui::restore()?;
}
(true, false) => emu.start_diff(),
(false, false) => emu.start(),
_ => panic!("Tui without difftest is not supported yet."),
}

tui::restore()?;

Ok(())
}
Loading

0 comments on commit 2f77cd2

Please sign in to comment.