From 9b7df3c9102c9a3c7e13e19f0b51b0ca83a31be7 Mon Sep 17 00:00:00 2001 From: Jaby Date: Tue, 11 Jul 2023 20:26:33 +0200 Subject: [PATCH] Present memory usage --- lib/psexe.ld | 2 +- src/Tools/psxreadmap/src/lib.rs | 44 +++++++++++++++++++----- src/Tools/psxreadmap/src/main.rs | 59 ++++++++++++++++++++++++++++---- 3 files changed, 89 insertions(+), 16 deletions(-) diff --git a/lib/psexe.ld b/lib/psexe.ld index 2c1638e6..aa5c6e7d 100644 --- a/lib/psexe.ld +++ b/lib/psexe.ld @@ -237,6 +237,6 @@ SECTIONS { __bss_end = .; __heap_start = __heap_base; - . = ADDR(.text) - 0x800; + /*. = ADDR(.text) - 0x800;*/ __end = .; } diff --git a/src/Tools/psxreadmap/src/lib.rs b/src/Tools/psxreadmap/src/lib.rs index 8ebdc101..3c0cbea9 100644 --- a/src/Tools/psxreadmap/src/lib.rs +++ b/src/Tools/psxreadmap/src/lib.rs @@ -70,6 +70,10 @@ impl ConsoleUI { Event::Input(event) => { match event.code { KeyCode::Char('q') => Ok(UIState::Terminated), + KeyCode::Char('s') => { + self.menu_selection = MenuSelection::Stats; + Ok(UIState::Render) + }, KeyCode::Left => { self.menu_selection = self.menu_selection.prev(); Ok(UIState::Render) @@ -102,7 +106,7 @@ impl ConsoleUI { } } - pub fn render(&mut self) -> Result<(), Error> { + pub fn render(&mut self, data: &ConsoleUIData) -> Result<(), Error> { self.terminal.draw(|frame| { Self::update_sub_frames(&mut self.main_chunks, &mut self.stats_chunks, &mut self.cur_size, frame.size()); @@ -110,7 +114,7 @@ impl ConsoleUI { frame.render_widget(Self::create_menu(&self.menu_selection), self.main_chunks[1]); match self.menu_selection { MenuSelection::Stats => { - Self::render_stats(frame, &self.stats_chunks); + Self::render_stats(frame, &self.stats_chunks, data); }, MenuSelection::Quit => { frame.render_widget(Self::create_exit_message(), self.main_chunks[2]); @@ -176,7 +180,7 @@ impl ConsoleUI { ) } - fn render_stats(frame: &mut ConsoleFrame, frames: &Vec) { + fn render_stats(frame: &mut ConsoleFrame, frames: &Vec, data: &ConsoleUIData) { let content_text = Paragraph::new("") .style(Style::default().fg(Color::White)) .alignment(Alignment::Center) @@ -185,18 +189,18 @@ impl ConsoleUI { .style(Style::default().fg(Color::White)) .border_type(BorderType::Plain) ); - let mem_gauge = Self::create_overall_mem_gauge(); + let mem_gauge = Self::create_overall_mem_gauge(data.highest_adr); frame.render_widget(content_text, frames[0]); - frame.render_widget(mem_gauge, frames[1]); + frame.render_widget(mem_gauge, frames[1]); } - fn create_overall_mem_gauge<'a>() -> LineGauge<'a> { - - LineGauge::default().block(Block::default().borders(Borders::ALL).title("Progress")) + fn create_overall_mem_gauge<'a>(highest_adr: u64) -> LineGauge<'a> { + LineGauge::default().block(Block::default().borders(Borders::ALL) + .title(format!("Memory Usage (0x{:x}/0x{:x})", 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(0.4) + .ratio(highest_adr as f64/ConsoleUIData::HIGHEST_RAM_ADDRESS as f64) } fn create_exit_message<'a>() -> Paragraph<'a> { @@ -211,6 +215,28 @@ impl ConsoleUI { } } +#[derive(Default)] +pub struct ConsoleUIData { + highest_adr: u64 +} + +impl ConsoleUIData { + pub const HIGHEST_RAM_ADDRESS:u64 = 0x80000000 + (2*1024*1024); + + pub fn new(memory_map: &MemoryMapInfo) -> Result { + let mut ui_data = ConsoleUIData::default(); + + ui_data.update(memory_map)?; + Ok(ui_data) + } + + pub fn update(&mut self, memory_map: &MemoryMapInfo) -> Result<(), Error> { + + self.highest_adr = memory_map.highest_address; + Ok(()) + } +} + pub fn load_memory_map(file_path: Option) -> Result { readmap::scan(tool_helper::open_input(file_path)?) } \ No newline at end of file diff --git a/src/Tools/psxreadmap/src/main.rs b/src/Tools/psxreadmap/src/main.rs index bc18d7c4..16ba99bb 100644 --- a/src/Tools/psxreadmap/src/main.rs +++ b/src/Tools/psxreadmap/src/main.rs @@ -1,6 +1,6 @@ use clap::Parser; use crossterm::{event::{self, Event as CEvent, KeyboardEnhancementFlags, KeyEventKind, PushKeyboardEnhancementFlags}, execute, terminal}; -use psxreadmap::{ConsoleUI, Event, EventReceiver, Terminal, load_memory_map, UIState}; +use psxreadmap::{ConsoleUI, ConsoleUIData, Event, EventReceiver, Terminal, load_memory_map, UIState}; use std::{io, path::PathBuf, sync::mpsc, thread, time::{Duration, Instant}}; use tool_helper::{Error, exit_with_error}; use tui::backend::CrosstermBackend; @@ -27,15 +27,16 @@ pub fn main() { } fn run_main(cmd: CommandLine) -> Result<(), Error> { - let _sections = load_memory_map(Some(cmd.input))?; + let memory_map = load_memory_map(Some(cmd.input))?; let rx = start_event_loop(); let terminal = setup_console()?; + let ui_data = ConsoleUIData::new(&memory_map)?; let mut console_ui = ConsoleUI::new(rx, terminal); loop { match console_ui.update()? { UIState::Alive => (), - UIState::Render => {console_ui.render()?;} + UIState::Render => {console_ui.render(&ui_data)?;} UIState::Terminated => break } } @@ -55,7 +56,7 @@ fn start_event_loop() -> EventReceiver { let timeout = tick_rate.checked_sub(last_tick.elapsed()).unwrap_or_else(|| Duration::from_secs(0)); if event::poll(timeout).expect("Event poll working") { if let CEvent::Key(key) = event::read().expect("Can read key input") { - if key.kind == KeyEventKind::Press { + if key.kind == KeyEventKind::Release { tx.send(Event::Input(key)).expect("Can send KeyInput"); } } @@ -79,7 +80,7 @@ fn setup_console() -> Result { let stdout = { let mut stdout = io::stdout(); if let Err(_) = execute!(stdout, PushKeyboardEnhancementFlags(KeyboardEnhancementFlags::REPORT_EVENT_TYPES)) { - // When this fails it fails + // This fails under Windows but is required for Linux } stdout }; @@ -88,4 +89,50 @@ fn setup_console() -> Result { terminal.clear()?; Ok(terminal) -} \ No newline at end of file +} + +use std::{fs::File, io::{BufWriter, Write}}; +use readmap::types::{Content::*, Section}; +fn _write_main(cmd: CommandLine) -> Result<(), Error> { + fn value_to_hex(value: T) -> String { + return format!("0x{:X}", value); + } + + fn option_to_hex(value: Option) -> String { + if let Some(value) = value { + return value_to_hex(value); + } + + else { + return String::from(""); + } + } + + let memory_map = load_memory_map(Some(cmd.input))?; + let mut file = tool_helper::open_output_file(&PathBuf::from("./planschi.d"))?; + + for section in memory_map.sections { + fn print_content(tab_level: usize, file: &mut BufWriter, section: Section) -> Result<(), Error> { + for content in section.content { + match content { + Fill(fill) => { + writeln!(file, "{:>tab_level$}*fill* @{}, {}", ' ', value_to_hex(fill.adr), value_to_hex(fill.size), tab_level=tab_level*4)?; + }, + Section(section) => { + writeln!(file, "{:>tab_level$} {} @{}, {}", ' ', section.name, option_to_hex(section.adr), option_to_hex(section.size), tab_level=tab_level*4)?; + print_content(tab_level + 1, file, section)?; + }, + Symbol(symbol) => { + writeln!(file, "{:>tab_level$}{} @{}", ' ', symbol.name, value_to_hex(symbol.adr), tab_level=tab_level*4)?; + } + } + } + + Ok(()) + } + + writeln!(file, "{}: @{}, {}", section.name, option_to_hex(section.adr), option_to_hex(section.size))?; + print_content(1, &mut file, section)?; + } + Ok(()) +}