Skip to content

Commit

Permalink
readrom: Print addresses for vectors
Browse files Browse the repository at this point in the history
Signed-off-by: Miquel Sabaté Solà <[email protected]>
  • Loading branch information
mssola committed Jan 14, 2025
1 parent fb836cb commit 4de5d3b
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion crates/readrom/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::{Arg, Command};
use clap::{arg, Arg, Command};
use header::{Header, Kind};
use std::fs::File;
use std::io::{ErrorKind, Read};
Expand All @@ -24,6 +24,22 @@ fn print_header(header: &Header) {
println!(" Mapper:\t{}", header.mapper);
}

fn print_vectors(addrs: &[u8]) {
println!("Vectors:");
println!(
" NMI:\t\t{:#04x}",
u16::from_le_bytes([addrs[0], addrs[1]])
);
println!(
" Reset:\t{:#04x}",
u16::from_le_bytes([addrs[2], addrs[3]])
);
println!(
" IRQ:\t\t{:#04x}",
u16::from_le_bytes([addrs[4], addrs[5]])
);
}

// Print the given `message` and exit(1).
fn die(message: String) {
println!("error: {}", message);
Expand All @@ -35,6 +51,7 @@ fn main() {
.version("0.1.0")
.about("Display information about NES/Famicom ROM files.")
.arg(Arg::new("FILE").required(true).help("ROM file to be read"))
.arg(arg!(-H --header "Just print the ROM header and quit"))
.get_matches();
let file = match args.get_one::<String>("FILE") {
Some(file) => file,
Expand All @@ -49,6 +66,8 @@ fn main() {
return;
};

// Header.

let mut buf = vec![0u8; 0x10];
if let Err(e) = input.read_exact(&mut buf) {
match e.kind() {
Expand All @@ -65,4 +84,23 @@ fn main() {
}
};
print_header(&header);

if *args.get_one::<bool>("header").unwrap() {
std::process::exit(0);
}

// PRG ROM.

buf = vec![0u8; header.prg_rom_size * 16 * 1024];
if let Err(e) = input.read_exact(&mut buf) {
match e.kind() {
ErrorKind::UnexpectedEof => die("could not read advertised PRG ROM space".to_string()),
_ => die(e.to_string()),
}
}

// Vectors.

let vectors = &buf.as_slice()[buf.len() - 6..];
print_vectors(vectors);
}

0 comments on commit 4de5d3b

Please sign in to comment.