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

46 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;
#[derive(Debug)]
enum MBC {
ROM,
MBC1,
}
2023-02-07 09:19:50 +11:00
pub struct ROM {
2023-02-07 09:51:11 +11:00
title: String,
2023-02-07 09:19:50 +11:00
data: Vec<u8>,
2023-02-07 09:51:11 +11:00
mbc: 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];
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
2023-02-07 09:19:50 +11:00
}
pub(super) fn get(&self, address: Address) -> u8 {
self.data[address as usize]
}
}