Compare commits

..

2 Commits

Author SHA1 Message Date
Rekai Nyangadzayi Musuka b829f05a34 chore(cartridge): clean-up code
continuous-integration/drone/push Build is passing Details
2021-08-19 20:39:04 -05:00
Rekai Nyangadzayi Musuka afd2d16371 chore(cartridge): re-rename RamInfo and RomRinfo 2021-08-19 20:05:48 -05:00
1 changed files with 49 additions and 43 deletions

View File

@ -33,23 +33,24 @@ impl Cartridge {
} }
fn detect_mbc(memory: &[u8]) -> Box<dyn MBCIo> { fn detect_mbc(memory: &[u8]) -> Box<dyn MBCIo> {
let ram_info = Self::detect_ram_info(memory); let ram_size = Self::detect_ram_info(memory);
let rom_info = Self::detect_rom_info(memory); let rom_size = Self::detect_rom_info(memory);
let mbc_kind = Self::find_mbc(memory); let mbc_kind = Self::find_mbc(memory);
let ram_size = ram_info.size(); let ram_cap = ram_size.capacity();
let rom_cap = rom_size.capacity();
eprintln!("Cartridge Ram Size: {} bytes", ram_size); eprintln!("Cartridge Ram Size: {} bytes", ram_cap);
eprintln!("Cartridge ROM Size: {} bytes", rom_info.size()); eprintln!("Cartridge ROM Size: {} bytes", rom_size.capacity());
eprintln!("MBC Type: {:?}", mbc_kind); eprintln!("MBC Type: {:?}", mbc_kind);
match mbc_kind { match mbc_kind {
MBCKind::None => Box::new(NoMBC), MBCKind::None => Box::new(NoMBC),
MBCKind::MBC1 => Box::new(MBC1::new(ram_info, rom_info)), MBCKind::MBC1 => Box::new(MBC1::new(ram_size, rom_size)),
MBCKind::MBC1WithBattery => Box::new(MBC1::new(ram_info, rom_info)), // TODO: Implement Saving MBCKind::MBC1WithBattery => Box::new(MBC1::new(ram_size, rom_size)), // TODO: Implement Saving
MBCKind::MBC3 => Box::new(MBC3::new(ram_size)), MBCKind::MBC3 => Box::new(MBC3::new(ram_cap)),
MBCKind::MBC3WithBattery => Box::new(MBC3::new(ram_size)), // TODO: Implement Saving MBCKind::MBC3WithBattery => Box::new(MBC3::new(ram_cap)), // TODO: Implement Saving
MBCKind::MBC5 => Box::new(MBC5::new(ram_size)), MBCKind::MBC5 => Box::new(MBC5::new(ram_cap, rom_cap)),
MBCKind::MBC5WithBattery => Box::new(MBC5::new(ram_size)), // TDO: Implement Saving MBCKind::MBC5WithBattery => Box::new(MBC5::new(ram_cap, rom_cap)), // TDO: Implement Saving
} }
} }
@ -68,12 +69,12 @@ impl Cartridge {
self.title.as_deref() self.title.as_deref()
} }
fn detect_ram_info(memory: &[u8]) -> RamInfo { fn detect_ram_info(memory: &[u8]) -> RamSize {
let id = memory[RAM_SIZE_ADDRESS]; let id = memory[RAM_SIZE_ADDRESS];
id.into() id.into()
} }
fn detect_rom_info(memory: &[u8]) -> RomInfo { fn detect_rom_info(memory: &[u8]) -> RomSize {
let id = dbg!(memory[ROM_SIZE_ADDRESS]); let id = dbg!(memory[ROM_SIZE_ADDRESS]);
id.into() id.into()
} }
@ -114,17 +115,17 @@ struct MBC1 {
/// 2-bit number /// 2-bit number
ram_bank: u8, ram_bank: u8,
mode: bool, mode: bool,
ram_size: RamInfo, ram_size: RamSize,
memory: Vec<u8>, memory: Vec<u8>,
rom_size: RomInfo, rom_size: RomSize,
mem_enabled: bool, mem_enabled: bool,
} }
impl MBC1 { impl MBC1 {
fn new(ram_size: RamInfo, rom_size: RomInfo) -> Self { fn new(ram_size: RamSize, rom_size: RomSize) -> Self {
Self { Self {
rom_bank: 0x01, rom_bank: 0x01,
memory: vec![0; ram_size.size() as usize], memory: vec![0; ram_size.capacity() as usize],
ram_size, ram_size,
rom_size, rom_size,
ram_bank: Default::default(), ram_bank: Default::default(),
@ -134,7 +135,7 @@ impl MBC1 {
} }
fn zero_bank(&self) -> u8 { fn zero_bank(&self) -> u8 {
use RomInfo::*; use RomSize::*;
match self.rom_size { match self.rom_size {
None | Four | Eight | Sixteen | ThirtyTwo => 0x00, None | Four | Eight | Sixteen | ThirtyTwo => 0x00,
@ -145,7 +146,7 @@ impl MBC1 {
} }
fn _mbcm_zero_bank(&self) -> u8 { fn _mbcm_zero_bank(&self) -> u8 {
use RomInfo::*; use RomSize::*;
match self.rom_size { match self.rom_size {
None | Four | Eight | Sixteen | ThirtyTwo => 0x00, None | Four | Eight | Sixteen | ThirtyTwo => 0x00,
@ -156,7 +157,7 @@ impl MBC1 {
} }
fn high_bank(&self) -> u8 { fn high_bank(&self) -> u8 {
use RomInfo::*; use RomSize::*;
let base = self.rom_bank & self.rom_size_mask(); let base = self.rom_bank & self.rom_size_mask();
@ -169,7 +170,7 @@ impl MBC1 {
} }
fn rom_size_mask(&self) -> u8 { fn rom_size_mask(&self) -> u8 {
use RomInfo::*; use RomSize::*;
match self.rom_size { match self.rom_size {
None => 0b00000001, None => 0b00000001,
@ -182,10 +183,10 @@ impl MBC1 {
} }
fn ram_addr(&self, addr: u16) -> u16 { fn ram_addr(&self, addr: u16) -> u16 {
use RamInfo::*; use RamSize::*;
match self.ram_size { match self.ram_size {
Unused | One => (addr - 0xA000) % self.ram_size.size() as u16, Unused | One => (addr - 0xA000) % self.ram_size.capacity() as u16,
Four => { Four => {
if self.mode { if self.mode {
0x2000 * self.ram_bank as u16 + (addr - 0xA000) 0x2000 * self.ram_bank as u16 + (addr - 0xA000)
@ -270,9 +271,9 @@ struct MBC3 {
} }
impl MBC3 { impl MBC3 {
fn new(ram_size: usize) -> Self { fn new(ram_cap: usize) -> Self {
Self { Self {
memory: vec![0; ram_size], memory: vec![0; ram_cap],
rom_bank: Default::default(), rom_bank: Default::default(),
ram_bank: Default::default(), ram_bank: Default::default(),
devs_enabled: Default::default(), devs_enabled: Default::default(),
@ -352,19 +353,26 @@ struct MBC5 {
/// 4-bit number /// 4-bit number
ram_bank: u8, ram_bank: u8,
rom_cap: usize,
memory: Vec<u8>, memory: Vec<u8>,
mem_enabled: bool, mem_enabled: bool,
} }
impl MBC5 { impl MBC5 {
fn new(ram_size: usize) -> Self { fn new(ram_cap: usize, rom_cap: usize) -> Self {
Self { Self {
rom_bank: 0x01, rom_bank: 0x01,
memory: vec![0; ram_size], memory: vec![0; ram_cap],
rom_cap,
ram_bank: Default::default(), ram_bank: Default::default(),
mem_enabled: Default::default(), mem_enabled: Default::default(),
} }
} }
fn bank_addr(&self, addr: u16) -> usize {
(0x4000 * self.rom_bank as usize + (addr as usize - 0x4000)) % self.rom_cap
}
} }
impl MBCIo for MBC5 { impl MBCIo for MBC5 {
@ -373,9 +381,7 @@ impl MBCIo for MBC5 {
match addr { match addr {
0x0000..=0x3FFF => Address(addr as usize), 0x0000..=0x3FFF => Address(addr as usize),
0x4000..=0x7FFF => { 0x4000..=0x7FFF => Address(self.bank_addr(addr)),
Address(0x4000u16.wrapping_mul(self.rom_bank) as usize + (addr as usize - 0x4000))
}
0xA000..=0xBFFF if self.mem_enabled => { 0xA000..=0xBFFF if self.mem_enabled => {
Value(self.memory[0x2000 * self.ram_bank as usize + (addr as usize - 0xA000)]) Value(self.memory[0x2000 * self.ram_bank as usize + (addr as usize - 0xA000)])
} }
@ -441,7 +447,7 @@ impl Default for MBCKind {
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
enum RamInfo { enum RamSize {
None = 0x00, None = 0x00,
Unused = 0x01, Unused = 0x01,
One = 0x02, One = 0x02,
@ -450,9 +456,9 @@ enum RamInfo {
Eight = 0x05, Eight = 0x05,
} }
impl RamInfo { impl RamSize {
fn size(&self) -> usize { fn capacity(&self) -> usize {
use RamInfo::*; use RamSize::*;
match *self { match *self {
None => 0, None => 0,
@ -465,15 +471,15 @@ impl RamInfo {
} }
} }
impl Default for RamInfo { impl Default for RamSize {
fn default() -> Self { fn default() -> Self {
Self::None Self::None
} }
} }
impl From<u8> for RamInfo { impl From<u8> for RamSize {
fn from(byte: u8) -> Self { fn from(byte: u8) -> Self {
use RamInfo::*; use RamSize::*;
match byte { match byte {
0x00 => None, 0x00 => None,
@ -488,7 +494,7 @@ impl From<u8> for RamInfo {
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
enum RomInfo { enum RomSize {
None = 0x00, // 32KB (also called Two) None = 0x00, // 32KB (also called Two)
Four = 0x01, // 64KB Four = 0x01, // 64KB
Eight = 0x02, // 128KB Eight = 0x02, // 128KB
@ -503,10 +509,10 @@ enum RomInfo {
NinetySix = 0x54, // 1.5MB NinetySix = 0x54, // 1.5MB
} }
impl RomInfo { impl RomSize {
// https://hacktix.github.io/GBEDG/mbcs/#rom-size // https://hacktix.github.io/GBEDG/mbcs/#rom-size
fn size(&self) -> u32 { fn capacity(&self) -> usize {
use RomInfo::*; use RomSize::*;
match self { match self {
SeventyTwo => 0x120000, SeventyTwo => 0x120000,
@ -517,9 +523,9 @@ impl RomInfo {
} }
} }
impl From<u8> for RomInfo { impl From<u8> for RomSize {
fn from(byte: u8) -> Self { fn from(byte: u8) -> Self {
use RomInfo::*; use RomSize::*;
match byte { match byte {
0x00 => None, 0x00 => None,