aboutsummaryrefslogtreecommitdiff
path: root/crates/readrom/src/main.rs
blob: 32e648b462797a3b4f162a0c928b6b7b50ca0146 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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);
}

// Print the given `message` and exit(1).
fn die(message: String) {
    println!("{}", message);
    std::process::exit(1);
}

fn main() {
    let args = Args::parse();
    let Ok(mut input) = File::open(&args.file) else {
        die(format!("failed to open the given file '{}'", &args.file));
        return;
    };

    let mut buf = vec![0u8; 0x10];
    if let Err(e) = input.read_exact(&mut buf) {
        match e.kind() {
            ErrorKind::UnexpectedEof => die("malformed ROM file".to_string()),
            _ => die(e.to_string()),
        }
    }

    let header = match Header::try_from(buf.as_slice()) {
        Ok(h) => h,
        Err(e) => {
            die(e.to_string());
            return;
        }
    };
    print_header(&header);
}