fix(cartridge): Use default title instead of empty string

This commit is contained in:
Rekai Nyangadzayi Musuka 2021-08-03 14:50:34 -05:00
parent 32b597a328
commit 05d6475015
1 changed files with 8 additions and 7 deletions

View File

@ -7,6 +7,7 @@ use crate::bus::BusIo;
const RAM_SIZE_ADDRESS: usize = 0x0149; const RAM_SIZE_ADDRESS: usize = 0x0149;
const ROM_SIZE_ADDRESS: usize = 0x0148; const ROM_SIZE_ADDRESS: usize = 0x0148;
const MBC_TYPE_ADDRESS: usize = 0x0147; const MBC_TYPE_ADDRESS: usize = 0x0147;
const ROM_TITLE_RANGE: std::ops::RangeInclusive<usize> = 0x0134..=0x0143;
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub(crate) struct Cartridge { pub(crate) struct Cartridge {
@ -88,14 +89,14 @@ impl Cartridge {
} }
fn find_title(memory: &[u8]) -> Option<String> { fn find_title(memory: &[u8]) -> Option<String> {
// FIXME: Get rid of magic values and handle cases let slice = &memory[ROM_TITLE_RANGE];
// where 0x134..0x143 reads past the length of the let with_nulls = std::str::from_utf8(slice).ok();
// string let trimmed = with_nulls.map(|s| s.trim_matches('\0'));
let slice = &memory[0x134..0x143]; match trimmed {
Some("") | None => None,
let str_with_nulls = std::str::from_utf8(slice).ok(); Some(_) => trimmed.map(String::from),
str_with_nulls.map(|s| s.trim_matches('\0').to_string()) }
} }
pub(crate) fn title(&self) -> Option<&str> { pub(crate) fn title(&self) -> Option<&str> {