diff --git a/src/bus.rs b/src/bus.rs index 61fe178..a8050d6 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -243,7 +243,7 @@ impl Bus { 0xFF00..=0xFF7F => { // IO Registers match addr { - 0xFF00 => self.joypad.status = byte.into(), + 0xFF00 => self.joypad.status.update(byte), 0xFF01 => self.serial.next = byte, 0xFF02 => self.serial.control = byte.into(), 0xFF04 => self.timer.divider = 0x00, diff --git a/src/joypad.rs b/src/joypad.rs index 73a7639..f926172 100644 --- a/src/joypad.rs +++ b/src/joypad.rs @@ -27,6 +27,16 @@ bitfield! { from into ButtonState, right_a, _set_right_a: 0, 0; } +impl JoypadStatus { + pub(crate) fn update(&mut self, byte: u8) { + let action_row = (byte >> 5) & 0x01; + let direction_row = (byte >> 4) & 0x01; + + self.set_action_row(action_row.into()); + self.set_direction_row(direction_row.into()); + } +} + impl JoypadStatus { pub fn set_down_start(&mut self, state: ButtonState, int: &mut bool) { if !(*int) { @@ -74,12 +84,6 @@ impl Clone for JoypadStatus { } } -impl From for JoypadStatus { - fn from(byte: u8) -> Self { - Self(byte) - } -} - impl From for u8 { fn from(status: JoypadStatus) -> Self { status.0