From 8e910cfec8146f78ef1fb911e65dc7a7f09a71d7 Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Wed, 22 Feb 2023 09:48:39 +1100 Subject: [PATCH] joypad: action and direction not mutually exclusive --- src/processor/memory/mmio/joypad.rs | 79 +++++++++++++---------------- 1 file changed, 35 insertions(+), 44 deletions(-) diff --git a/src/processor/memory/mmio/joypad.rs b/src/processor/memory/mmio/joypad.rs index 02ed6e7..3438faf 100644 --- a/src/processor/memory/mmio/joypad.rs +++ b/src/processor/memory/mmio/joypad.rs @@ -2,15 +2,10 @@ use crate::util::{clear_bit, get_bit}; use gilrs::{Button, ConnectedGamepadsIterator}; use minifb::Key; -#[derive(Debug, Clone, Copy, PartialEq)] -enum JoypadBank { - Action, - Direction, -} - #[derive(Debug, Clone, Copy, PartialEq)] pub struct Joypad { - bank_sel: JoypadBank, + sel_action: bool, + sel_direction: bool, down: bool, up: bool, left: bool, @@ -24,48 +19,42 @@ pub struct Joypad { 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); - } + if self.sel_action { + reg = clear_bit(reg, 5); + if self.start { + reg = clear_bit(reg, 3); } - 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); - } + if self.select { + reg = clear_bit(reg, 2); + } + if self.b { + reg = clear_bit(reg, 1); + } + if self.a { + reg = clear_bit(reg, 0); + } + } + if self.sel_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 } pub fn mmio_write(&mut self, data: u8) { - if !get_bit(data, 5) { - self.bank_sel = JoypadBank::Action; - } - if !get_bit(data, 4) { - self.bank_sel = JoypadBank::Direction; - } + self.sel_action = !get_bit(data, 5); + self.sel_direction = !get_bit(data, 4); } pub fn update_pressed_keys( @@ -75,7 +64,8 @@ impl Joypad { ) -> bool { let old = *self; *self = Joypad { - bank_sel: self.bank_sel, + sel_action: self.sel_action, + sel_direction: self.sel_direction, down: false, up: false, left: false, @@ -110,7 +100,8 @@ impl Joypad { impl Default for Joypad { fn default() -> Self { Self { - bank_sel: JoypadBank::Action, + sel_action: true, + sel_direction: false, down: false, up: false, left: false,