Compare commits

..

2 Commits

1 changed files with 29 additions and 16 deletions

View File

@ -61,8 +61,9 @@ impl Cartridge {
MbcKind::Mbc2(hw) => Box::new(Mbc2::new(hw, rom_cap)), MbcKind::Mbc2(hw) => Box::new(Mbc2::new(hw, rom_cap)),
MbcKind::Mbc3(hw @ Mbc3Hardware::Rtc) => Box::new(Mbc3::new(hw, ram_cap)), MbcKind::Mbc3(hw @ Mbc3Hardware::Rtc) => Box::new(Mbc3::new(hw, ram_cap)),
MbcKind::Mbc3(hw @ Mbc3Hardware::RtcBatteryRam) => Box::new(Mbc3::new(hw, ram_cap)), MbcKind::Mbc3(hw @ Mbc3Hardware::RtcBatteryRam) => Box::new(Mbc3::new(hw, ram_cap)),
MbcKind::Mbc4(hw @ Mbc5Hardware::None) => Box::new(Mbc5::new(hw, ram_cap, rom_cap)), MbcKind::Mbc3(hw @ Mbc3Hardware::BatteryRam) => Box::new(Mbc3::new(hw, ram_cap)),
MbcKind::Mbc4(hw @ Mbc5Hardware::BatteryRam) => { MbcKind::Mbc5(hw @ Mbc5Hardware::None) => Box::new(Mbc5::new(hw, ram_cap, rom_cap)),
MbcKind::Mbc5(hw @ Mbc5Hardware::BatteryRam) => {
Box::new(Mbc5::new(hw, ram_cap, rom_cap)) Box::new(Mbc5::new(hw, ram_cap, rom_cap))
} }
kind => todo!("ROMS with {:?} are currently unsupported", kind), kind => todo!("ROMS with {:?} are currently unsupported", kind),
@ -70,21 +71,23 @@ impl Cartridge {
} }
fn detect_title(title_mem: &[u8; ROM_TITLE_MAX_SIZE]) -> Option<String> { fn detect_title(title_mem: &[u8; ROM_TITLE_MAX_SIZE]) -> Option<String> {
use std::str::from_utf8;
const ALT_TITLE_LEN: usize = ROM_MANUFACTURER_START - ROM_TITLE_START; const ALT_TITLE_LEN: usize = ROM_MANUFACTURER_START - ROM_TITLE_START;
// byte slice we have here is purposely not null terminated // ascii byte slie does not have a null-terminator
let ascii = match title_mem.iter().position(|b| *b == 0x00) { let ascii = match title_mem.iter().position(|b| *b == 0x00) {
Some(end) => &title_mem[0..end], Some(end) => &title_mem[..end],
None => &title_mem[0..ROM_TITLE_MAX_SIZE], None => &title_mem[..ROM_TITLE_MAX_SIZE],
}; };
match std::str::from_utf8(ascii).ok() { match from_utf8(ascii).map(str::trim).ok() {
None => match std::str::from_utf8(&title_mem[0..ALT_TITLE_LEN]).ok() { None => match from_utf8(&title_mem[..ALT_TITLE_LEN]).map(str::trim).ok() {
Some("") | None => None, Some("") | None => None,
Some(title) => Some(String::from(title.trim())), Some(title) => Some(String::from(title)),
}, },
Some("") => None, Some("") => None,
Some(title) => Some(String::from(title.trim())), Some(title) => Some(String::from(title)),
} }
} }
@ -105,12 +108,12 @@ impl Cartridge {
0x11 => Mbc3(Mbc3Hardware::None), 0x11 => Mbc3(Mbc3Hardware::None),
0x12 => Mbc3(Mbc3Hardware::Ram), 0x12 => Mbc3(Mbc3Hardware::Ram),
0x13 => Mbc3(Mbc3Hardware::BatteryRam), 0x13 => Mbc3(Mbc3Hardware::BatteryRam),
0x19 => Mbc4(Mbc5Hardware::None), 0x19 => Mbc5(Mbc5Hardware::None),
0x1A => Mbc4(Mbc5Hardware::Ram), 0x1A => Mbc5(Mbc5Hardware::Ram),
0x1B => Mbc4(Mbc5Hardware::BatteryRam), 0x1B => Mbc5(Mbc5Hardware::BatteryRam),
0x1C => Mbc4(Mbc5Hardware::Rumble), 0x1C => Mbc5(Mbc5Hardware::Rumble),
0x1D => Mbc4(Mbc5Hardware::RumbleRam), 0x1D => Mbc5(Mbc5Hardware::RumbleRam),
0x1E => Mbc4(Mbc5Hardware::RumbleBatteryRam), 0x1E => Mbc5(Mbc5Hardware::RumbleBatteryRam),
id => unimplemented!("MBC with code {:#04X} is unsupported", id), id => unimplemented!("MBC with code {:#04X} is unsupported", id),
} }
} }
@ -746,7 +749,7 @@ enum MbcKind {
Mbc1(Mbc1Hardware), Mbc1(Mbc1Hardware),
Mbc2(Mbc2Hardware), Mbc2(Mbc2Hardware),
Mbc3(Mbc3Hardware), Mbc3(Mbc3Hardware),
Mbc4(Mbc5Hardware), Mbc5(Mbc5Hardware),
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
@ -950,4 +953,14 @@ mod tests {
Cartridge::detect_title(&title) Cartridge::detect_title(&title)
); );
} }
#[test]
fn all_whitespace_title() {
let title = [
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00,
];
assert_eq!(None, Cartridge::detect_title(&title));
}
} }