diff --git a/src/main.rs b/src/main.rs index 9f2ee17..9ce4fa5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use clap::{ArgGroup, Parser}; use gilrs::Gilrs; use minifb::{Window, WindowOptions}; use processor::{ - memory::{rom::Rom, Memory}, + memory::{Memory, Rom}, Cpu, }; use std::{ diff --git a/src/processor/memory.rs b/src/processor/memory.rs index 1741309..25d4380 100644 --- a/src/processor/memory.rs +++ b/src/processor/memory.rs @@ -1,90 +1,18 @@ -use self::rom::Rom; +use self::mmio::{Joypad, JoypadBank}; +pub use self::rom::Rom; use crate::{ - processor::{clear_bit, get_bit, SplitRegister}, + processor::{get_bit, SplitRegister}, verbose_println, }; use gilrs::{Button, ConnectedGamepadsIterator}; use minifb::Key; use std::io::{stdout, Write}; +mod mmio; pub(crate) mod rom; pub(crate) type Address = u16; -#[derive(Debug, Clone, Copy, PartialEq)] -enum JoypadBank { - Action, - Direction, -} - -#[derive(Debug, Clone, Copy, PartialEq)] -struct Joypad { - bank_sel: JoypadBank, - down: bool, - up: bool, - left: bool, - right: bool, - start: bool, - select: bool, - b: bool, - a: bool, -} - -impl Joypad { - fn as_register(&self) -> u8 { - let mut reg = 0xFF; - match self.bank_sel { - JoypadBank::Action => { - reg = clear_bit(reg, 5); - if self.start { - reg = clear_bit(reg, 3); - } - if self.select { - reg = clear_bit(reg, 2); - } - if self.b { - reg = clear_bit(reg, 1); - } - if self.a { - reg = clear_bit(reg, 0); - } - } - JoypadBank::Direction => { - reg = clear_bit(reg, 4); - if self.down { - reg = clear_bit(reg, 3); - } - if self.up { - reg = clear_bit(reg, 2); - } - if self.left { - reg = clear_bit(reg, 1); - } - if self.right { - reg = clear_bit(reg, 0); - } - } - } - reg - } -} - -impl Default for Joypad { - fn default() -> Self { - Self { - bank_sel: JoypadBank::Action, - down: false, - up: false, - left: false, - right: false, - start: false, - select: false, - b: false, - a: false, - } - } -} - #[allow(dead_code)] pub struct Memory { bootrom: Vec, diff --git a/src/processor/memory/mmio/joypad.rs b/src/processor/memory/mmio/joypad.rs new file mode 100644 index 0000000..801ed0d --- /dev/null +++ b/src/processor/memory/mmio/joypad.rs @@ -0,0 +1,75 @@ +use crate::util::clear_bit; + +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum JoypadBank { + Action, + Direction, +} + +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Joypad { + pub bank_sel: JoypadBank, + pub down: bool, + pub up: bool, + pub left: bool, + pub right: bool, + pub start: bool, + pub select: bool, + pub b: bool, + pub a: bool, +} + +impl Joypad { + pub fn as_register(&self) -> u8 { + let mut reg = 0xFF; + match self.bank_sel { + JoypadBank::Action => { + reg = clear_bit(reg, 5); + if self.start { + reg = clear_bit(reg, 3); + } + if self.select { + reg = clear_bit(reg, 2); + } + if self.b { + reg = clear_bit(reg, 1); + } + if self.a { + reg = clear_bit(reg, 0); + } + } + JoypadBank::Direction => { + reg = clear_bit(reg, 4); + if self.down { + reg = clear_bit(reg, 3); + } + if self.up { + reg = clear_bit(reg, 2); + } + if self.left { + reg = clear_bit(reg, 1); + } + if self.right { + reg = clear_bit(reg, 0); + } + } + } + reg + } +} + +impl Default for Joypad { + fn default() -> Self { + Self { + bank_sel: JoypadBank::Action, + down: false, + up: false, + left: false, + right: false, + start: false, + select: false, + b: false, + a: false, + } + } +} diff --git a/src/processor/memory/mmio/mod.rs b/src/processor/memory/mmio/mod.rs new file mode 100644 index 0000000..db3b2c8 --- /dev/null +++ b/src/processor/memory/mmio/mod.rs @@ -0,0 +1,2 @@ +mod joypad; +pub use joypad::{Joypad, JoypadBank};