From 4bf12db91feb5db98d65b518dbad0c1a8c6748df Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Tue, 6 Oct 2026 17:15:16 +0200 Subject: readrom: add support for default expansion device MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The 15th byte from the NES 2.0 header was wilfully ignored, but it can be relevant to the end user. Hence, add support for it in the 'header' library, and add the frontend code into 'readrom'. The point is not to support every single thing out there, but I have added the stuff that I thought was most relevant, and we can add more items to it whenever the need appears. Signed-off-by: Miquel Sabaté Solà --- lib/header/src/lib.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) (limited to 'lib/header/src/lib.rs') diff --git a/lib/header/src/lib.rs b/lib/header/src/lib.rs index cf0b736..3a62e35 100644 --- a/lib/header/src/lib.rs +++ b/lib/header/src/lib.rs @@ -146,6 +146,48 @@ impl std::fmt::Display for Timing { } } +/// The default expansion device available on the NES 2.0 header. +#[derive(Debug)] +pub enum DefaultExpansionDevice { + Unknown, + StandardControllers, + FourScoreSatellite, + FamicomFourPlayersAdapter, + VsSystem4016, + VsSystem4017, + VsZapper, + Zapper, + TwoZappers, + PowerPadA, + PowerPadB, +} + +impl std::fmt::Display for DefaultExpansionDevice { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + Self::Unknown => write!(f, "Unknown"), + Self::StandardControllers => write!(f, "Standard NES/Famicom controllers"), + Self::FourScoreSatellite => write!( + f, + "NES Four Score/Satellite with two additional standard controllers" + ), + Self::FamicomFourPlayersAdapter => { + write!( + f, + "Famicom Four Players Adapter with two additional standard controllers using the \"simple\" protocol" + ) + } + Self::VsSystem4016 => write!(f, "Vs. System (1P via $4016)"), + Self::VsSystem4017 => write!(f, "Vs. System (1P via $4017)"), + Self::VsZapper => write!(f, "Vs. Zapper"), + Self::Zapper => write!(f, "Zapper ($4017)"), + Self::TwoZappers => write!(f, "Two Zappers"), + Self::PowerPadA => write!(f, "Power Pad Side A"), + Self::PowerPadB => write!(f, "Power Pad Side B"), + } + } +} + /// Information that has been parsed from an NES/Famicom ROM header. The /// `Header` struct implements the `TryFrom` trait. Hence, use /// `Header::try_from` in order to parse a header. @@ -190,6 +232,10 @@ pub struct Header { /// The kind of the header. pub kind: Kind, + + /// The default expansion device that can be specified in the NES 2.0 + /// header. Used to specify the default way to experience a game. + pub default_expansion_device: Option, } impl TryFrom<&[u8]> for Header { @@ -219,11 +265,33 @@ impl TryFrom<&[u8]> for Header { prg_ram_definition: parse_ram_definition(&kind, bytes.get(10), has_persistent_memory), chr_ram_definition: parse_ram_definition(&kind, bytes.get(11), has_persistent_memory), timing: parse_timing(&kind, bytes.get(12)), + default_expansion_device: if matches!(kind, Kind::Nes20) { + Some(parse_default_expansion_device(bytes.get(15).unwrap_or(&0))) + } else { + None + }, kind, }) } } +// Returns the DefaultExpansionDevice for the given byte value. +fn parse_default_expansion_device(byte: &u8) -> DefaultExpansionDevice { + match byte { + 1 => DefaultExpansionDevice::StandardControllers, + 2 => DefaultExpansionDevice::FourScoreSatellite, + 3 => DefaultExpansionDevice::FamicomFourPlayersAdapter, + 4 => DefaultExpansionDevice::VsSystem4016, + 5 => DefaultExpansionDevice::VsSystem4017, + 7 => DefaultExpansionDevice::VsZapper, + 8 => DefaultExpansionDevice::Zapper, + 9 => DefaultExpansionDevice::TwoZappers, + 11 => DefaultExpansionDevice::PowerPadA, + 12 => DefaultExpansionDevice::PowerPadB, + _ => DefaultExpansionDevice::Unknown, + } +} + // Parse the given `byte` by assuming it's a RAM definition. It will just return // `None` if `has_persistent_memory` is false or the `kind` is not NES 2.0. fn parse_ram_definition( -- cgit v1.2.3