From 5fe3d2ef2e695a9d1353ebbe2022f76ccb0b316f Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Tue, 7 Feb 2023 09:51:11 +1100 Subject: [PATCH] parse cart header --- src/main.rs | 8 ++++---- src/processor/memory/rom.rs | 32 +++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 70b0ee4..29db173 100644 --- a/src/main.rs +++ b/src/main.rs @@ -100,8 +100,11 @@ fn main() { *v = args.verbose; } + let rom: ROM = ROM::load(fs::read(args.rom).expect("Could not load ROM")); + let bootrom: Vec = fs::read(args.bootrom).expect("Could not load BootROM"); + let mut window = Window::new( - "Gameboy", + format!("{}", rom.get_title()).as_str(), WIDTH * FACTOR, HEIGHT * FACTOR, WindowOptions::default(), @@ -114,9 +117,6 @@ fn main() { window.topmost(true); - let rom: ROM = ROM::load(fs::read(args.rom).expect("Could not load ROM")); - let bootrom: Vec = fs::read(args.bootrom).expect("Could not load BootROM"); - let mut cpu = CPU::new(Memory::init(bootrom, args.run_bootrom, rom), window); if !args.run_bootrom { diff --git a/src/processor/memory/rom.rs b/src/processor/memory/rom.rs index 3040ca2..5715854 100644 --- a/src/processor/memory/rom.rs +++ b/src/processor/memory/rom.rs @@ -1,12 +1,42 @@ use crate::processor::memory::Address; +use std::str::from_utf8_unchecked; + +#[derive(Debug)] +enum MBC { + ROM, + MBC1, +} pub struct ROM { + title: String, data: Vec, + mbc: MBC, } impl ROM { pub fn load(data: Vec) -> Self { - Self { data } + let mut title_length = 0x143; + for i in 0x134..0x143 { + title_length = i; + if data[i] == 0x0 { + break; + } + } + let title = unsafe { from_utf8_unchecked(&data[0x134..title_length]).to_string() }; + let _gbc_flag = data[0x143]; + let _sgb_flag = data[0x146]; + let _rom_size = data[0x148]; + let _ram_size = data[0x149]; + let mbc = match data[0x147] { + 0x00 => MBC::ROM, + 0x01 => MBC::MBC1, + _ => panic!("unimplemented mbc: {:#X}", data[0x147]), + }; + Self { title, data, mbc } + } + + pub fn get_title(&self) -> &String { + &self.title } pub(super) fn get(&self, address: Address) -> u8 {