Compare commits
No commits in common. "1f8fa4816820a899620bf99789ac407f10b7ba24" and "8b78b2943eb3d9a6813ee4730c8f1f3c698f1bf5" have entirely different histories.
1f8fa48168
...
8b78b2943e
|
@ -1,8 +0,0 @@
|
|||
{
|
||||
"recommendations": [
|
||||
"matklad.rust-analyzer",
|
||||
"tamasfe.even-better-toml",
|
||||
"serayuzgur.crates",
|
||||
"vadimcn.vscode-lldb",
|
||||
]
|
||||
}
|
112
src/cartridge.rs
112
src/cartridge.rs
|
@ -33,19 +33,18 @@ impl Cartridge {
|
|||
}
|
||||
|
||||
fn detect_mbc(memory: &[u8]) -> Box<dyn MBCIo> {
|
||||
let ram_info = Self::detect_ram_info(memory);
|
||||
let rom_info = Self::detect_rom_info(memory);
|
||||
let ram_size = Self::detect_ram_size(memory);
|
||||
let rom_size = Self::detect_rom_size(memory);
|
||||
let mbc_kind = Self::find_mbc(memory);
|
||||
let ram_size = ram_info.size();
|
||||
|
||||
eprintln!("Cartridge Ram Size: {} bytes", ram_size);
|
||||
eprintln!("Cartridge ROM Size: {} bytes", rom_info.size());
|
||||
eprintln!("Cartridge Ram Size: {} bytes", ram_size.len());
|
||||
eprintln!("Cartridge ROM Size: {} bytes", rom_size.size());
|
||||
eprintln!("MBC Type: {:?}", mbc_kind);
|
||||
|
||||
match mbc_kind {
|
||||
MBCKind::None => Box::new(NoMBC),
|
||||
MBCKind::MBC1 => Box::new(MBC1::new(ram_info, rom_info)),
|
||||
MBCKind::MBC1WithBattery => Box::new(MBC1::new(ram_info, rom_info)), // TODO: Implement Saving
|
||||
MBCKind::MBC1 => Box::new(MBC1::new(ram_size, rom_size)),
|
||||
MBCKind::MBC1WithBattery => Box::new(MBC1::new(ram_size, rom_size)), // TODO: Implement Saving
|
||||
MBCKind::MBC3 => Box::new(MBC3::new(ram_size)),
|
||||
MBCKind::MBC3WithBattery => Box::new(MBC3::new(ram_size)), // TODO: Implement Saving
|
||||
MBCKind::MBC5 => Box::new(MBC5::new(ram_size)),
|
||||
|
@ -68,13 +67,13 @@ impl Cartridge {
|
|||
self.title.as_deref()
|
||||
}
|
||||
|
||||
fn detect_ram_info(memory: &[u8]) -> RamInfo {
|
||||
fn detect_ram_size(memory: &[u8]) -> RamSize {
|
||||
let id = memory[RAM_SIZE_ADDRESS];
|
||||
id.into()
|
||||
}
|
||||
|
||||
fn detect_rom_info(memory: &[u8]) -> RomInfo {
|
||||
let id = dbg!(memory[ROM_SIZE_ADDRESS]);
|
||||
fn detect_rom_size(memory: &[u8]) -> RomSize {
|
||||
let id = memory[ROM_SIZE_ADDRESS];
|
||||
id.into()
|
||||
}
|
||||
|
||||
|
@ -114,17 +113,17 @@ struct MBC1 {
|
|||
/// 2-bit number
|
||||
ram_bank: u8,
|
||||
mode: bool,
|
||||
ram_size: RamInfo,
|
||||
ram_size: RamSize,
|
||||
memory: Vec<u8>,
|
||||
rom_size: RomInfo,
|
||||
rom_size: RomSize,
|
||||
mem_enabled: bool,
|
||||
}
|
||||
|
||||
impl MBC1 {
|
||||
fn new(ram_size: RamInfo, rom_size: RomInfo) -> Self {
|
||||
fn new(ram_size: RamSize, rom_size: RomSize) -> Self {
|
||||
Self {
|
||||
rom_bank: 0x01,
|
||||
memory: vec![0; ram_size.size() as usize],
|
||||
memory: vec![0; ram_size.len() as usize],
|
||||
ram_size,
|
||||
rom_size,
|
||||
ram_bank: Default::default(),
|
||||
|
@ -134,7 +133,7 @@ impl MBC1 {
|
|||
}
|
||||
|
||||
fn zero_bank(&self) -> u8 {
|
||||
use RomInfo::*;
|
||||
use RomSize::*;
|
||||
|
||||
match self.rom_size {
|
||||
None | Four | Eight | Sixteen | ThirtyTwo => 0x00,
|
||||
|
@ -145,7 +144,7 @@ impl MBC1 {
|
|||
}
|
||||
|
||||
fn _mbcm_zero_bank(&self) -> u8 {
|
||||
use RomInfo::*;
|
||||
use RomSize::*;
|
||||
|
||||
match self.rom_size {
|
||||
None | Four | Eight | Sixteen | ThirtyTwo => 0x00,
|
||||
|
@ -156,7 +155,7 @@ impl MBC1 {
|
|||
}
|
||||
|
||||
fn high_bank(&self) -> u8 {
|
||||
use RomInfo::*;
|
||||
use RomSize::*;
|
||||
|
||||
let base = self.rom_bank & self.rom_size_mask();
|
||||
|
||||
|
@ -169,7 +168,7 @@ impl MBC1 {
|
|||
}
|
||||
|
||||
fn rom_size_mask(&self) -> u8 {
|
||||
use RomInfo::*;
|
||||
use RomSize::*;
|
||||
|
||||
match self.rom_size {
|
||||
None => 0b00000001,
|
||||
|
@ -182,11 +181,11 @@ impl MBC1 {
|
|||
}
|
||||
|
||||
fn ram_addr(&self, addr: u16) -> u16 {
|
||||
use RamInfo::*;
|
||||
use RamSize::*;
|
||||
|
||||
match self.ram_size {
|
||||
Unused | One => (addr - 0xA000) % self.ram_size.size() as u16,
|
||||
Four => {
|
||||
Two | Eight => (addr - 0xA000) % self.ram_size.len() as u16,
|
||||
ThirtyTwo => {
|
||||
if self.mode {
|
||||
0x2000 * self.ram_bank as u16 + (addr - 0xA000)
|
||||
} else {
|
||||
|
@ -270,9 +269,9 @@ struct MBC3 {
|
|||
}
|
||||
|
||||
impl MBC3 {
|
||||
fn new(ram_size: usize) -> Self {
|
||||
fn new(ram_size: RamSize) -> Self {
|
||||
Self {
|
||||
memory: vec![0; ram_size],
|
||||
memory: vec![0; ram_size.len() as usize],
|
||||
rom_bank: Default::default(),
|
||||
ram_bank: Default::default(),
|
||||
devs_enabled: Default::default(),
|
||||
|
@ -357,10 +356,10 @@ struct MBC5 {
|
|||
}
|
||||
|
||||
impl MBC5 {
|
||||
fn new(ram_size: usize) -> Self {
|
||||
fn new(ram_size: RamSize) -> Self {
|
||||
Self {
|
||||
rom_bank: 0x01,
|
||||
memory: vec![0; ram_size],
|
||||
memory: vec![0; ram_size.len() as usize],
|
||||
ram_bank: Default::default(),
|
||||
mem_enabled: Default::default(),
|
||||
}
|
||||
|
@ -441,54 +440,54 @@ impl Default for MBCKind {
|
|||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
enum RamInfo {
|
||||
enum RamSize {
|
||||
None = 0x00,
|
||||
Unused = 0x01,
|
||||
One = 0x02,
|
||||
Four = 0x03,
|
||||
Sixteen = 0x04,
|
||||
Eight = 0x05,
|
||||
Two = 0x01,
|
||||
Eight = 0x02,
|
||||
ThirtyTwo = 0x03, // Split into 4 RAM banks
|
||||
OneHundredTwentyEight = 0x04, // Split into 16 RAM banks
|
||||
SixtyFour = 0x05, // Split into 8 RAm Banks
|
||||
}
|
||||
|
||||
impl RamInfo {
|
||||
fn size(&self) -> usize {
|
||||
use RamInfo::*;
|
||||
impl RamSize {
|
||||
fn len(&self) -> u32 {
|
||||
use RamSize::*;
|
||||
|
||||
match *self {
|
||||
None => 0,
|
||||
Unused => 0x800,
|
||||
One => 0x2000,
|
||||
Four => 0x8000,
|
||||
Sixteen => 0x20000,
|
||||
Eight => 0x10000,
|
||||
Two => 2_048,
|
||||
Eight => 8_192,
|
||||
ThirtyTwo => 32_768,
|
||||
OneHundredTwentyEight => 131_072,
|
||||
SixtyFour => 65_536,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for RamInfo {
|
||||
impl Default for RamSize {
|
||||
fn default() -> Self {
|
||||
Self::None
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u8> for RamInfo {
|
||||
impl From<u8> for RamSize {
|
||||
fn from(byte: u8) -> Self {
|
||||
use RamInfo::*;
|
||||
use RamSize::*;
|
||||
|
||||
match byte {
|
||||
0x00 => None,
|
||||
0x01 => Unused,
|
||||
0x02 => One,
|
||||
0x03 => Four,
|
||||
0x04 => Sixteen,
|
||||
0x05 => Eight,
|
||||
0x01 => Two,
|
||||
0x02 => Eight,
|
||||
0x03 => ThirtyTwo,
|
||||
0x04 => OneHundredTwentyEight,
|
||||
0x05 => SixtyFour,
|
||||
_ => unreachable!("{:#04X} is not a valid value for RAMSize", byte),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
enum RomInfo {
|
||||
enum RomSize {
|
||||
None = 0x00, // 32KB (also called Two)
|
||||
Four = 0x01, // 64KB
|
||||
Eight = 0x02, // 128KB
|
||||
|
@ -503,23 +502,24 @@ enum RomInfo {
|
|||
NinetySix = 0x54, // 1.5MB
|
||||
}
|
||||
|
||||
impl RomInfo {
|
||||
impl RomSize {
|
||||
// https://hacktix.github.io/GBEDG/mbcs/#rom-size
|
||||
fn size(&self) -> u32 {
|
||||
use RomInfo::*;
|
||||
use RomSize::*;
|
||||
|
||||
match self {
|
||||
SeventyTwo => 0x120000,
|
||||
Eighty => 0x140000,
|
||||
NinetySix => 0x180000,
|
||||
_ => 0x8000 << *self as u8,
|
||||
None | Four | Eight | Sixteen | ThirtyTwo | SixtyFour | OneTwentyEight
|
||||
| TwoFiftySix | FiveHundredTwelve => 2 << (14 + *self as u8),
|
||||
SeventyTwo => 1_179_648,
|
||||
Eighty => 1_310_720,
|
||||
NinetySix => 1_572_864,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u8> for RomInfo {
|
||||
impl From<u8> for RomSize {
|
||||
fn from(byte: u8) -> Self {
|
||||
use RomInfo::*;
|
||||
use RomSize::*;
|
||||
|
||||
match byte {
|
||||
0x00 => None,
|
||||
|
|
Loading…
Reference in New Issue