Skip to content
Snippets Groups Projects
Verified Commit 214ea837 authored by João Magalhães's avatar João Magalhães :rocket:
Browse files

fix: title offset calculation

To avoid 0x0 characters in ROM title.
parent fbeb1b02
No related branches found
Tags 0.8.0
No related merge requests found
Pipeline #2177 passed
......@@ -17,7 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
*
* Bug with ROM title that included 0x0 characters in it
## [0.6.7] - 2023-02-13
......
......@@ -57,6 +57,10 @@ impl Emulator {
"========= Cartridge =========\n{}\n=============================\n",
rom
);
self.graphics
.window_mut()
.set_title(format!("{} - {}", TITLE, rom.title()).as_str())
.unwrap();
}
pub fn run(&mut self) {
......@@ -189,6 +193,8 @@ fn main() {
let mut game_boy = GameBoy::new();
game_boy.load_boot_default();
// creates a new generic emulator structure loads the default
// ROM file and starts running it
let mut emulator = Emulator::new(game_boy, SCREEN_SCALE);
emulator.load_rom("../../res/roms/pocket.gb");
emulator.run();
......
......@@ -3,7 +3,7 @@ use sdl2::{
AudioSubsystem, EventPump, TimerSubsystem, VideoSubsystem,
};
/// Structure that provide the complete set of Graphics
/// Structure that provides the complete set of Graphics
/// and Sound syb-system ready to be used by the overall
/// emulator infrastructure.
pub struct Graphics {
......@@ -67,6 +67,8 @@ impl Graphics {
}
}
/// Creates an SDL2 Surface structure from the provided
/// bytes that represent an image (eg: an PNG image buffer).
pub fn surface_from_bytes(bytes: &[u8]) -> Surface {
unsafe {
let rw_ops = RWops::from_bytes(bytes).unwrap();
......
......@@ -222,6 +222,11 @@ pub struct Cartridge {
/// If the RAM access ia enabled, this flag allows
/// control of memory access to avoid corruption.
ram_enabled: bool,
// The final offset of the last character of the title
// that is considered to be non zero (0x0) so that a
// proper safe conversion to UTF-8 string can be done.
title_offset: usize,
}
impl Cartridge {
......@@ -235,6 +240,7 @@ impl Cartridge {
rom_offset: 0x4000,
ram_offset: 0x0000,
ram_enabled: false,
title_offset: 0x0143,
}
}
......@@ -312,6 +318,7 @@ impl Cartridge {
self.ram_offset = 0x0000;
self.set_mbc();
self.set_computed();
self.set_title_offset();
self.allocate_ram();
self.set_rom_bank(1);
self.set_ram_bank(0);
......@@ -326,6 +333,18 @@ impl Cartridge {
self.ram_bank_count = self.ram_size().ram_banks();
}
pub fn set_title_offset(&mut self) {
let mut offset: usize = 0;
for byte in &self.rom_data[0x0134..0x0143] {
if *byte != 0u8 {
offset += 1;
continue;
}
break;
}
self.title_offset = 0x0134 + offset;
}
fn allocate_ram(&mut self) {
let ram_banks = max(self.ram_size().ram_banks(), 1);
self.ram_data = vec![0u8; ram_banks as usize * RAM_BANK_SIZE];
......@@ -335,7 +354,7 @@ impl Cartridge {
#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl Cartridge {
pub fn title(&self) -> String {
String::from(std::str::from_utf8(&self.rom_data[0x0134..0x0143]).unwrap())
String::from(std::str::from_utf8(&self.rom_data[0x0134..self.title_offset]).unwrap())
}
pub fn rom_type(&self) -> RomType {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment