From 66ae61bd357f8b675be3ec9a965d50b1e39f3e8f Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Thu, 23 Jan 2025 16:15:02 +0100 Subject: xa65: Create hex dumps for both binaries MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows to compare both binaries more easily with tools such as vimdiff and the likes. Signed-off-by: Miquel Sabaté Solà --- crates/xa65/src/main.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'crates/xa65') diff --git a/crates/xa65/src/main.rs b/crates/xa65/src/main.rs index cdfce59..85db01f 100644 --- a/crates/xa65/src/main.rs +++ b/crates/xa65/src/main.rs @@ -1,3 +1,5 @@ +use std::fs::File; +use std::io::prelude::*; use std::path::PathBuf; use std::process::Command; @@ -142,6 +144,50 @@ fn temporary_dir() -> PathBuf { tmp.join(name) } +// Attemps to generate an 'hexdump' with the given `bin` and taking the given +// `src` as an argument for 'hexdump'. The resulting dump will be saved in +// `dst`. +fn hexdump(bin: &PathBuf, src: &PathBuf, dst: &PathBuf) -> bool { + let Ok(nasm) = Command::new(bin).arg("-C").arg(src).output() else { + println!( + "xa65 (warning): could not produce an hexdump of '{}'", + src.display() + ); + return false; + }; + let Ok(mut nasm_file) = File::create(dst) else { + println!( + "xa65 (warning): could not produce an hexdump of '{}'", + src.display() + ); + return false; + }; + if let Err(_) = nasm_file.write_all(nasm.stdout.as_slice()) { + println!( + "xa65 (warning): could not produce an hexdump of '{}'", + src.display() + ); + return false; + } + true +} + +// Attempt to generate 'hexdump' files for both binaries. If this is not +// possible, then it will print a warning and return early. +fn attempt_hexdump(dir: &PathBuf) { + let Some(bin) = find_binary("hexdump") else { + println!( + "xa65 (warning): could not find 'hexdump' in your PATH. \ + A human-readable dump will not be generated" + ); + return; + }; + + if hexdump(&bin, &dir.join("nasm.nes"), &dir.join("nasm.txt")) { + hexdump(&bin, &dir.join("cl65.nes"), &dir.join("cl65.txt")); + } +} + fn main() { // Make sure that the binaries are there. let (nasm, cl65) = match get_binaries() { @@ -214,6 +260,9 @@ fn main() { Ok(diff) => { // If 'diff' failed, show it but don't error out. if !diff.status.success() { + // Try to generate a human-readable diff. + attempt_hexdump(&dir); + println!( "xa65 (error): 'nasm' and 'ca65' have a mismatch. Check the results at {}", dir.display() -- cgit v1.2.3