Compare commits

..

No commits in common. "61156ca8a818e6b4e8c74a10e11546862b738b79" and "4c657c9832d8ed34e4e97c15af7c90b88ee2f91c" have entirely different histories.

1 changed files with 16 additions and 29 deletions

View File

@ -61,9 +61,8 @@ 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::Mbc3(hw @ Mbc3Hardware::BatteryRam) => Box::new(Mbc3::new(hw, ram_cap)), MbcKind::Mbc4(hw @ Mbc5Hardware::None) => Box::new(Mbc5::new(hw, ram_cap, rom_cap)),
MbcKind::Mbc5(hw @ Mbc5Hardware::None) => Box::new(Mbc5::new(hw, ram_cap, rom_cap)), MbcKind::Mbc4(hw @ Mbc5Hardware::BatteryRam) => {
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),
@ -71,23 +70,21 @@ 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;
// ascii byte slie does not have a null-terminator // byte slice we have here is purposely not null terminated
let ascii = match title_mem.iter().position(|b| *b == 0x00) { let ascii = match title_mem.iter().position(|b| *b == 0x00) {
Some(end) => &title_mem[..end], Some(end) => &title_mem[0..end],
None => &title_mem[..ROM_TITLE_MAX_SIZE], None => &title_mem[0..ROM_TITLE_MAX_SIZE],
}; };
match from_utf8(ascii).map(str::trim).ok() { match std::str::from_utf8(ascii).ok() {
None => match from_utf8(&title_mem[..ALT_TITLE_LEN]).map(str::trim).ok() { None => match std::str::from_utf8(&title_mem[0..ALT_TITLE_LEN]).ok() {
Some("") | None => None, Some("") | None => None,
Some(title) => Some(String::from(title)), Some(title) => Some(String::from(title.trim())),
}, },
Some("") => None, Some("") => None,
Some(title) => Some(String::from(title)), Some(title) => Some(String::from(title.trim())),
} }
} }
@ -108,12 +105,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 => Mbc5(Mbc5Hardware::None), 0x19 => Mbc4(Mbc5Hardware::None),
0x1A => Mbc5(Mbc5Hardware::Ram), 0x1A => Mbc4(Mbc5Hardware::Ram),
0x1B => Mbc5(Mbc5Hardware::BatteryRam), 0x1B => Mbc4(Mbc5Hardware::BatteryRam),
0x1C => Mbc5(Mbc5Hardware::Rumble), 0x1C => Mbc4(Mbc5Hardware::Rumble),
0x1D => Mbc5(Mbc5Hardware::RumbleRam), 0x1D => Mbc4(Mbc5Hardware::RumbleRam),
0x1E => Mbc5(Mbc5Hardware::RumbleBatteryRam), 0x1E => Mbc4(Mbc5Hardware::RumbleBatteryRam),
id => unimplemented!("MBC with code {:#04X} is unsupported", id), id => unimplemented!("MBC with code {:#04X} is unsupported", id),
} }
} }
@ -749,7 +746,7 @@ enum MbcKind {
Mbc1(Mbc1Hardware), Mbc1(Mbc1Hardware),
Mbc2(Mbc2Hardware), Mbc2(Mbc2Hardware),
Mbc3(Mbc3Hardware), Mbc3(Mbc3Hardware),
Mbc5(Mbc5Hardware), Mbc4(Mbc5Hardware),
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
@ -953,14 +950,4 @@ 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));
}
} }