chore: follow rust style guides
This commit is contained in:
parent
9b77d6c6c3
commit
823e4b1e0a
|
@ -31,14 +31,14 @@ impl Cartridge {
|
||||||
let ram_byte_count = ram_size.to_byte_count();
|
let ram_byte_count = ram_size.to_byte_count();
|
||||||
|
|
||||||
let mbc = match mbc_kind {
|
let mbc = match mbc_kind {
|
||||||
MBCKind::None => todo!("Handle no MBC Situation"),
|
MbcKind::None => todo!("Handle no MBC Situation"),
|
||||||
MBCKind::MBC1 => MBC1 {
|
MbcKind::MBC1 => Mbc1 {
|
||||||
ram_size,
|
ram_size,
|
||||||
ram: vec![0; ram_byte_count as usize],
|
ram: vec![0; ram_byte_count as usize],
|
||||||
bank_count,
|
bank_count,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
MBCKind::MBC5 => todo!("Implement MBC5"),
|
MbcKind::MBC5 => todo!("Implement MBC5"),
|
||||||
};
|
};
|
||||||
|
|
||||||
Box::new(mbc)
|
Box::new(mbc)
|
||||||
|
@ -54,14 +54,14 @@ impl Cartridge {
|
||||||
id.into()
|
id.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_mbc(memory: &[u8]) -> MBCKind {
|
fn find_mbc(memory: &[u8]) -> MbcKind {
|
||||||
let id = memory[MBC_TYPE_ADDRESS];
|
let id = memory[MBC_TYPE_ADDRESS];
|
||||||
|
|
||||||
// TODO: Refactor this to match the other enums in this module
|
// TODO: Refactor this to match the other enums in this module
|
||||||
match id {
|
match id {
|
||||||
0x00 => MBCKind::None,
|
0x00 => MbcKind::None,
|
||||||
0x01 => MBCKind::MBC1,
|
0x01 => MbcKind::MBC1,
|
||||||
0x19 => MBCKind::MBC5,
|
0x19 => MbcKind::MBC5,
|
||||||
_ => unimplemented!("{} is the id of an unsupported memory bank controller", id),
|
_ => unimplemented!("{} is the id of an unsupported memory bank controller", id),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -70,8 +70,8 @@ impl Cartridge {
|
||||||
impl Cartridge {
|
impl Cartridge {
|
||||||
pub fn read_byte(&self, addr: u16) -> u8 {
|
pub fn read_byte(&self, addr: u16) -> u8 {
|
||||||
match self.mbc.handle_read(addr) {
|
match self.mbc.handle_read(addr) {
|
||||||
MBCResult::Address(addr) => self.memory[addr as usize],
|
MbcResult::Address(addr) => self.memory[addr as usize],
|
||||||
MBCResult::Value(byte) => byte,
|
MbcResult::Value(byte) => byte,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn write_byte(&mut self, addr: u16, byte: u8) {
|
pub fn write_byte(&mut self, addr: u16, byte: u8) {
|
||||||
|
@ -89,7 +89,7 @@ impl Cartridge {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
struct MBC1 {
|
struct Mbc1 {
|
||||||
rom_bank: u8, // 5-bit Number
|
rom_bank: u8, // 5-bit Number
|
||||||
ram_bank: u8, // 2-bit number
|
ram_bank: u8, // 2-bit number
|
||||||
mode: bool,
|
mode: bool,
|
||||||
|
@ -99,7 +99,7 @@ struct MBC1 {
|
||||||
ram_enabled: bool,
|
ram_enabled: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MBC1 {
|
impl Mbc1 {
|
||||||
fn calc_zero_bank_number(&self) -> u8 {
|
fn calc_zero_bank_number(&self) -> u8 {
|
||||||
use BankCount::*;
|
use BankCount::*;
|
||||||
|
|
||||||
|
@ -164,9 +164,9 @@ impl MBC1 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MemoryBankController for MBC1 {
|
impl MemoryBankController for Mbc1 {
|
||||||
fn handle_read(&self, addr: u16) -> MBCResult {
|
fn handle_read(&self, addr: u16) -> MbcResult {
|
||||||
use MBCResult::*;
|
use MbcResult::*;
|
||||||
|
|
||||||
match addr {
|
match addr {
|
||||||
0x0000..=0x3FFF => {
|
0x0000..=0x3FFF => {
|
||||||
|
@ -212,16 +212,16 @@ impl MemoryBankController for MBC1 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
trait MemoryBankController: CloneMBC {
|
trait MemoryBankController: CloneMbc {
|
||||||
fn handle_read(&self, addr: u16) -> MBCResult;
|
fn handle_read(&self, addr: u16) -> MbcResult;
|
||||||
fn handle_write(&mut self, addr: u16, byte: u8);
|
fn handle_write(&mut self, addr: u16, byte: u8);
|
||||||
}
|
}
|
||||||
|
|
||||||
trait CloneMBC {
|
trait CloneMbc {
|
||||||
fn clone_mbc(&self) -> Box<dyn MemoryBankController>;
|
fn clone_mbc(&self) -> Box<dyn MemoryBankController>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> CloneMBC for T
|
impl<T> CloneMbc for T
|
||||||
where
|
where
|
||||||
T: MemoryBankController + Clone + 'static,
|
T: MemoryBankController + Clone + 'static,
|
||||||
{
|
{
|
||||||
|
@ -230,19 +230,19 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum MBCResult {
|
enum MbcResult {
|
||||||
Address(u16),
|
Address(u16),
|
||||||
Value(u8),
|
Value(u8),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
enum MBCKind {
|
enum MbcKind {
|
||||||
None,
|
None,
|
||||||
MBC1,
|
MBC1,
|
||||||
MBC5,
|
MBC5,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for MBCKind {
|
impl Default for MbcKind {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::None
|
Self::None
|
||||||
}
|
}
|
||||||
|
@ -375,6 +375,6 @@ impl std::clone::Clone for Box<dyn MemoryBankController> {
|
||||||
|
|
||||||
impl std::default::Default for Box<dyn MemoryBankController> {
|
impl std::default::Default for Box<dyn MemoryBankController> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Box::new(MBC1::default())
|
Box::new(Mbc1::default())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue