From 407048d0037c8d6d26c7e3fe2384e974212f124e Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Wed, 18 Dec 2024 07:46:26 +0100 Subject: Add the readrom binary MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The readrom binary is similar to `readelf` from Linux and it will allow to display information from an NES/Famicom ROM file. For now the information being shown is just the header, but in the future we might also include disassembling parts of the code, or retrieving the "CHARS" section for a given ROM file, and similar. In order to implement the header parsing part a new library has been introduced, simply named "header" which abstracts everything away so you just need to call `Header::try_from("my bytes")` to fetch the actual information. Signed-off-by: Miquel Sabaté Solà --- crates/nasm/src/main.rs | 2 +- crates/readrom/Cargo.toml | 12 ++++++++++ crates/readrom/src/main.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 crates/readrom/Cargo.toml create mode 100644 crates/readrom/src/main.rs (limited to 'crates') diff --git a/crates/nasm/src/main.rs b/crates/nasm/src/main.rs index 9ad710b..105a995 100644 --- a/crates/nasm/src/main.rs +++ b/crates/nasm/src/main.rs @@ -5,7 +5,7 @@ use std::io::{self, Read, Write}; use xixanta::assembler::Assembler; use xixanta::mapping::{Mapping, EMPTY, NROM, NROM65}; -/// Assembler for the 6502 microprocessor that targets the NES. +/// Assembler for the 6502 microprocessor that targets the NES/Famicom. #[derive(ClapParser, Debug)] #[command(version, about, long_about = None)] struct Args { diff --git a/crates/readrom/Cargo.toml b/crates/readrom/Cargo.toml new file mode 100644 index 0000000..98a5106 --- /dev/null +++ b/crates/readrom/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "readrom" +version = "1.0.0" +rust-version.workspace = true +edition.workspace = true +license.workspace = true +authors.workspace = true + +[dependencies] +anyhow = "^1" +clap = { version = "^4", features = ["derive"] } +header.workspace = true diff --git a/crates/readrom/src/main.rs b/crates/readrom/src/main.rs new file mode 100644 index 0000000..56e60e0 --- /dev/null +++ b/crates/readrom/src/main.rs @@ -0,0 +1,55 @@ +use anyhow::{bail, Result}; +use clap::Parser as ClapParser; +use header::{Header, Kind}; +use std::fs::File; +use std::io::{ErrorKind, Read}; + +/// Display information about NES/Famicom ROM files. +#[derive(ClapParser, Debug)] +#[command(version, about, long_about = None)] +struct Args { + /// NES/Famicom ROM file from which to display information. + file: String, +} + +fn print_header(header: &Header) { + println!("Header:"); + + match header.kind { + Kind::INes => println!(" Kind:\t\tiNES"), + Kind::Nes20 => println!(" Kind:\t\tNES 2.0"), + } + + println!( + " PRG ROM size:\t{} bytes ({}KB)", + header.prg_rom_size * 16 * 1024, + header.prg_rom_size * 16 + ); + println!( + " CHR ROM size:\t{} bytes ({}KB)", + header.chr_rom_size * 8 * 1024, + header.chr_rom_size * 8 + ); + println!(" Mapper:\t{}", header.mapper); +} + +fn main() -> Result<()> { + let args = Args::parse(); + let mut input = File::open(args.file)?; + + let mut buf = vec![0u8; 0x10]; + if let Err(e) = input.read_exact(&mut buf) { + match e.kind() { + ErrorKind::UnexpectedEof => bail!("malformed ROM file."), + _ => return Err(e.into()), + } + } + + let header = match Header::try_from(buf.as_slice()) { + Ok(h) => h, + Err(e) => bail!("{}.", e), + }; + print_header(&header); + + Ok(()) +} -- cgit v1.2.3