From 1526b4a09d81940c3445c365dd1f6e05a713f59e Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Sat, 30 Oct 2021 15:48:47 +0900 Subject: [PATCH] fix(cart): improve title parsin Now both Tokimeki Memorial titles are read properly --- src/cartridge.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/cartridge.rs b/src/cartridge.rs index 5d2cacd..0268cb9 100644 --- a/src/cartridge.rs +++ b/src/cartridge.rs @@ -5,7 +5,8 @@ use crate::bus::BusIo; const RAM_SIZE_ADDRESS: usize = 0x0149; const ROM_SIZE_ADDRESS: usize = 0x0148; const MBC_TYPE_ADDRESS: usize = 0x0147; -const ROM_TITLE_RANGE: std::ops::RangeInclusive = 0x0134..=0x0143; +const ROM_TITLE_START: usize = 0x134; +const ROM_TITLE_MAX_SIZE: usize = 16; #[derive(Debug, Default)] pub(crate) struct Cartridge { @@ -59,13 +60,17 @@ impl Cartridge { } fn find_title(memory: &[u8]) -> Option { - let slice = &memory[ROM_TITLE_RANGE]; - let with_nulls = std::str::from_utf8(slice).ok(); - let trimmed = with_nulls.map(|s| s.trim_matches('\0').trim()); + let title_bytes = &memory[ROM_TITLE_START..(ROM_TITLE_START + ROM_TITLE_MAX_SIZE)]; - match trimmed { + // ASCII Byte array purposely does not have null terminator + let ascii = match title_bytes.iter().position(|byte| *byte == 0x00) { + Some(end) => &memory[ROM_TITLE_START..(ROM_TITLE_START + end)], + None => &memory[ROM_TITLE_START..(ROM_TITLE_START + ROM_TITLE_MAX_SIZE - 1)], + }; + + match std::str::from_utf8(ascii).ok() { Some("") | None => None, - Some(_) => trimmed.map(String::from), + Some(title) => Some(String::from(title)), } }