gb-emu/src/processor/memory/rom.rs

43 lines
1 KiB
Rust
Raw Normal View History

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