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 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,