joypad: action and direction not mutually exclusive

This commit is contained in:
Alex Janka 2023-02-22 09:48:39 +11:00
parent 5b353364cd
commit 8e910cfec8

View file

@ -2,15 +2,10 @@ use crate::util::{clear_bit, get_bit};
use gilrs::{Button, ConnectedGamepadsIterator}; use gilrs::{Button, ConnectedGamepadsIterator};
use minifb::Key; use minifb::Key;
#[derive(Debug, Clone, Copy, PartialEq)]
enum JoypadBank {
Action,
Direction,
}
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub struct Joypad { pub struct Joypad {
bank_sel: JoypadBank, sel_action: bool,
sel_direction: bool,
down: bool, down: bool,
up: bool, up: bool,
left: bool, left: bool,
@ -24,8 +19,7 @@ pub struct Joypad {
impl Joypad { impl Joypad {
pub fn as_register(&self) -> u8 { pub fn as_register(&self) -> u8 {
let mut reg = 0xFF; let mut reg = 0xFF;
match self.bank_sel { if self.sel_action {
JoypadBank::Action => {
reg = clear_bit(reg, 5); reg = clear_bit(reg, 5);
if self.start { if self.start {
reg = clear_bit(reg, 3); reg = clear_bit(reg, 3);
@ -40,7 +34,7 @@ impl Joypad {
reg = clear_bit(reg, 0); reg = clear_bit(reg, 0);
} }
} }
JoypadBank::Direction => { if self.sel_direction {
reg = clear_bit(reg, 4); reg = clear_bit(reg, 4);
if self.down { if self.down {
reg = clear_bit(reg, 3); reg = clear_bit(reg, 3);
@ -55,17 +49,12 @@ impl Joypad {
reg = clear_bit(reg, 0); reg = clear_bit(reg, 0);
} }
} }
}
reg reg
} }
pub fn mmio_write(&mut self, data: u8) { pub fn mmio_write(&mut self, data: u8) {
if !get_bit(data, 5) { self.sel_action = !get_bit(data, 5);
self.bank_sel = JoypadBank::Action; self.sel_direction = !get_bit(data, 4);
}
if !get_bit(data, 4) {
self.bank_sel = JoypadBank::Direction;
}
} }
pub fn update_pressed_keys( pub fn update_pressed_keys(
@ -75,7 +64,8 @@ impl Joypad {
) -> bool { ) -> bool {
let old = *self; let old = *self;
*self = Joypad { *self = Joypad {
bank_sel: self.bank_sel, sel_action: self.sel_action,
sel_direction: self.sel_direction,
down: false, down: false,
up: false, up: false,
left: false, left: false,
@ -110,7 +100,8 @@ impl Joypad {
impl Default for Joypad { impl Default for Joypad {
fn default() -> Self { fn default() -> Self {
Self { Self {
bank_sel: JoypadBank::Action, sel_action: true,
sel_direction: false,
down: false, down: false,
up: false, up: false,
left: false, left: false,