diff options
| author | Miquel Sabaté Solà <mssola@mssola.com> | 2026-07-16 16:52:52 +0200 |
|---|---|---|
| committer | Miquel Sabaté Solà <mssola@mssola.com> | 2026-07-16 16:52:52 +0200 |
| commit | f5549b97bf85c0b169093a3a8478d610ef7c9e2e (patch) | |
| tree | 274a1572f27b07360882dd629b93d422b3335fcb /crates/readrom/src/main.rs | |
| parent | d7b1a895de9e3b78dc4f303de6b5a692849b1cd1 (diff) | |
| download | tools.nes-f5549b97bf85.tar.gz tools.nes-f5549b97bf85.zip | |
readrom: only read the ROM file once
Since the implementation grew from disassembling a single subroutine,
reading the ROM file from inside print_range() made sense. But since we
have disassembling of full segments and files now, this reading would be
triggered multiple times.
Commit d7b1a895de9e ("readrom: add an option to disassemble the full
file") avoided the exhaustion of the input by using seek(), but that's
just a hack and it's hiding the fact that we are constantly reading the
same thing over and over. Constantly reading ROM files isn't that much
of a performance issue given how small they are, but it's embarrasing
anyways.
Fix this by reading the full file once and passing the slice of bytes to
the same functions that used to require the file to be passed.
Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
Diffstat (limited to 'crates/readrom/src/main.rs')
| -rw-r--r-- | crates/readrom/src/main.rs | 29 |
1 files changed, 14 insertions, 15 deletions
diff --git a/crates/readrom/src/main.rs b/crates/readrom/src/main.rs index 7c8d1fb..9207c2a 100644 --- a/crates/readrom/src/main.rs +++ b/crates/readrom/src/main.rs @@ -1,7 +1,7 @@ use header::{Header, Kind}; use std::collections::HashMap; use std::fs::File; -use std::io::{BufRead, BufReader, BufWriter, ErrorKind, Read, Seek, SeekFrom, Write}; +use std::io::{BufRead, BufReader, BufWriter, ErrorKind, Read, Write}; use std::path::PathBuf; use xixanta::mapping::get_mapping_configuration; use xixanta::opcodes::OPCODES; @@ -188,7 +188,7 @@ fn die(message: String) { // 'raw' to true if you want all bytes to be printed directly into the stdout, // otherwise a human-readable format will be used. fn print_range( - mut file: &File, + bytes: &[u8], start: usize, end: Option<usize>, memories: HashMap<usize, String>, @@ -196,11 +196,7 @@ fn print_range( raw: bool, filter: Option<&str>, ) -> Result<(), String> { - let mut bytes = Vec::new(); - file.seek(SeekFrom::Start(0)).map_err(|e| e.to_string())?; - file.read_to_end(&mut bytes).map_err(|e| e.to_string())?; - - // Fetch the bytes to be printed. + // Put a lower and upper bounds to the bytes to be used for printing. // NOTE: minus 0x8000 to account for non-ROM address, plus 0x10 to skip the // header from the file. @@ -338,7 +334,7 @@ fn parse_hex_value(address: &str) -> Option<usize> { } fn do_disassemble( - input: &File, + bytes: &[u8], address: Option<&str>, nasm_path: &Option<String>, mut start: Option<usize>, @@ -399,7 +395,7 @@ fn do_disassemble( if let Some(address) = address && let Some(start) = parse_hex_value(address) { - return print_range(input, start, None, memories, addresses, raw, None); + return print_range(bytes, start, None, memories, addresses, raw, None); } // Otherwise, print the full range if possible. @@ -413,7 +409,7 @@ fn do_disassemble( match start { Some(s) => print_range( - input, + bytes, s, Some(end.unwrap()), memories, @@ -426,9 +422,12 @@ fn do_disassemble( } } -fn handle_disassembling_args(args: &Args, input: &File) -> Result<bool, String> { +fn handle_disassembling_args(args: &Args, mut input: &File) -> Result<bool, String> { + let mut bytes = Vec::new(); + input.read_to_end(&mut bytes).map_err(|e| e.to_string())?; + if let Some(address) = &args.disassemble { - if let Err(e) = do_disassemble(input, Some(address), &args.nasm, None, None, args.raw) { + if let Err(e) = do_disassemble(&bytes, Some(address), &args.nasm, None, None, args.raw) { die(e); } return Ok(true); @@ -455,7 +454,7 @@ fn handle_disassembling_args(args: &Args, input: &File) -> Result<bool, String> .collect::<Vec<_>>() .join(", ") ); - do_disassemble(input, None, &args.nasm, Some(start), Some(end), args.raw)?; + do_disassemble(&bytes, None, &args.nasm, Some(start), Some(end), args.raw)?; } } None => { @@ -484,7 +483,7 @@ fn handle_disassembling_args(args: &Args, input: &File) -> Result<bool, String> .collect::<Vec<_>>() .join(", ") ); - do_disassemble(input, None, &args.nasm, Some(start), Some(end), args.raw)?; + do_disassemble(&bytes, None, &args.nasm, Some(start), Some(end), args.raw)?; return Ok(true); } for segment in &m.segments { @@ -501,7 +500,7 @@ fn handle_disassembling_args(args: &Args, input: &File) -> Result<bool, String> .join(", ") ); do_disassemble( - input, + &bytes, None, &args.nasm, Some(start), |
