From 4fd3a1ee6410329fb7c743a1355b32073a28da01 Mon Sep 17 00:00:00 2001 From: Jaby Date: Wed, 26 Jul 2023 21:49:47 +0200 Subject: [PATCH] Display memory usage --- src/Tools/Tools.code-workspace | 2 +- src/Tools/psxreadmap/src/lib.rs | 23 ++++++++++++++++++----- src/Tools/psxreadmap/src/main.rs | 16 ++++++++++++++-- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/Tools/Tools.code-workspace b/src/Tools/Tools.code-workspace index e3442ac0..2fb9ea1b 100644 --- a/src/Tools/Tools.code-workspace +++ b/src/Tools/Tools.code-workspace @@ -82,7 +82,7 @@ "", "--help", "--list -o ../Tests/Test_Planschbecken psx bin-cue ../Tests/ISO_Planschbecken.xml", - "--wsl ../../../examples/PoolBox/application/bin/PSX-release/PoolBox.elf" + "--wsl -o Planschbecken.bin ../../../examples/PoolBox/application/bin/PSX-release/PoolBox.elf" ], "default": "", "description": "Argument options to pass to cargo run" diff --git a/src/Tools/psxreadmap/src/lib.rs b/src/Tools/psxreadmap/src/lib.rs index ae0b58fe..a9fdb518 100644 --- a/src/Tools/psxreadmap/src/lib.rs +++ b/src/Tools/psxreadmap/src/lib.rs @@ -197,11 +197,16 @@ impl ConsoleUI { } fn create_overall_mem_gauge<'a>(highest_adr: u64) -> LineGauge<'a> { + const RAM_TOP:u64 = ConsoleUIData::HIGHEST_RAM_ADDRESS & !ConsoleUIData::RAM_BASE_ADDRESS; + + let highest_adr_masked = highest_adr & !ConsoleUIData::RAM_BASE_ADDRESS; + let adr_ratio = (highest_adr_masked as f64)/RAM_TOP as f64; + LineGauge::default().block(Block::default().borders(Borders::ALL) - .title(format!("Memory Usage (0x{:x}/0x{:x})", highest_adr, ConsoleUIData::HIGHEST_RAM_ADDRESS))) + .title(format!("Memory Usage: {}% [0x{:x}/0x{:x}]", adr_ratio*100.0f64, highest_adr, ConsoleUIData::HIGHEST_RAM_ADDRESS))) .gauge_style(Style::default().fg(Color::White).bg(Color::Black).add_modifier(Modifier::BOLD)) .line_set(symbols::line::THICK) - .ratio(highest_adr as f64/ConsoleUIData::HIGHEST_RAM_ADDRESS as f64) + .ratio(adr_ratio) } fn create_exit_message<'a>() -> Paragraph<'a> { @@ -222,7 +227,8 @@ pub struct ConsoleUIData { } impl ConsoleUIData { - pub const HIGHEST_RAM_ADDRESS:u64 = 0x80000000 + (2*1024*1024); + pub const RAM_BASE_ADDRESS:u64 = 0x80000000; + pub const HIGHEST_RAM_ADDRESS:u64 = Self::RAM_BASE_ADDRESS + (2*1024*1024); pub fn new(memory_map: &MemoryMap) -> Result { let mut ui_data = ConsoleUIData::default(); @@ -231,9 +237,16 @@ impl ConsoleUIData { Ok(ui_data) } - pub fn update(&mut self, _memory_map: &MemoryMap) -> Result<(), Error> { + pub fn update(&mut self, memory_map: &MemoryMap) -> Result<(), Error> { + fn get_last_section_end_adr(memory_map: &MemoryMap) -> u64 { + if let Some(section) = memory_map.sections.last() { + return section.adr + section.size as u64; + } - self.highest_adr = 0; + 0u64 + } + + self.highest_adr = get_last_section_end_adr(memory_map); Ok(()) } } diff --git a/src/Tools/psxreadmap/src/main.rs b/src/Tools/psxreadmap/src/main.rs index 089a10a0..e2667df5 100644 --- a/src/Tools/psxreadmap/src/main.rs +++ b/src/Tools/psxreadmap/src/main.rs @@ -11,7 +11,9 @@ struct CommandLine { #[clap(value_parser, help="Input MAP file for scannning")] input: PathBuf, #[clap(long="wsl", default_value_t=false)] - use_wsl: bool + use_wsl: bool, + #[clap(short='o')] + output: Option } pub fn main() { @@ -29,7 +31,7 @@ pub fn main() { } fn run_main(cmd: CommandLine) -> Result<(), Error> { - let memory_map = load_memory_map(cmd.use_wsl, cmd.input)?; + let memory_map = load_memory_map(cmd.use_wsl, cmd.input)?; dump_memory_map(cmd.output, &memory_map)?; let rx = start_event_loop(); let terminal = setup_console()?; let ui_data = ConsoleUIData::new(&memory_map)?; @@ -46,6 +48,16 @@ fn run_main(cmd: CommandLine) -> Result<(), Error> { console_ui.close() } +fn dump_memory_map(output: Option, memory_map: &readmap::types::MemoryMap) -> Result<(), Error> { + if let Some(output) = output { + let output = tool_helper::open_output(Some(output))?; + + readmap::dump::write(output, &memory_map)?; + } + + Ok(()) +} + fn start_event_loop() -> EventReceiver { // Set up a mpsc (multiproducer, single consumer) channel to communicate between the input handler and the rendering loop. let (tx, rx) = mpsc::channel();