gb-emu/src/processor/memory/rom.rs
2023-02-07 10:08:34 +11:00

42 lines
1 KiB
Rust

use crate::processor::memory::Address;
use std::str::from_utf8_unchecked;
use self::mbcs::{MBC, MBC1, NONE};
mod mbcs;
pub struct ROM {
title: String,
mbc: Box<dyn MBC>,
}
impl ROM {
pub fn load(data: Vec<u8>) -> Self {
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: Box<dyn MBC> = match data[0x147] {
0x00 => Box::new(NONE { data }),
0x01 => Box::new(MBC1 { data }),
_ => panic!("unimplemented mbc: {:#X}", data[0x147]),
};
Self { title, mbc }
}
pub fn get_title(&self) -> &String {
&self.title
}
pub(super) fn get(&self, address: Address) -> u8 {
self.mbc.get(address)
}
}