From 0f64f071337374845aa3c8b5aa97813577249359 Mon Sep 17 00:00:00 2001 From: Michel Heily Date: Mon, 12 Sep 2022 00:41:35 +0300 Subject: [PATCH] fixes Former-commit-id: bfa963ac13e76b7e3cf2eb2e23651eb3189d3cbb Former-commit-id: 6602c3b0a22327d5ceb85063c9934b3490e1da01 --- core/src/iodev.rs | 4 -- core/src/keypad.rs | 2 +- platform/rustboyadvance-jni/src/emulator.rs | 7 +-- platform/rustboyadvance-libretro/src/lib.rs | 51 +++++++++++++-------- platform/rustboyadvance-sdl2/src/main.rs | 3 -- 5 files changed, 35 insertions(+), 32 deletions(-) diff --git a/core/src/iodev.rs b/core/src/iodev.rs index 0ccc46a..c30ff3c 100644 --- a/core/src/iodev.rs +++ b/core/src/iodev.rs @@ -76,10 +76,6 @@ impl IoDevices { pub fn set_sysbus_ptr(&mut self, ptr: SysBusPtr) { self.sysbus_ptr = ptr; } - - pub(crate) fn set_key_state(&mut self, state: u16) { - self.keyinput = state; - } } impl InterruptConnect for IoDevices { diff --git a/core/src/keypad.rs b/core/src/keypad.rs index db3a1da..c80fb91 100644 --- a/core/src/keypad.rs +++ b/core/src/keypad.rs @@ -1,4 +1,4 @@ -#[derive(Debug, Primitive, PartialEq, Eq)] +#[derive(Debug, Primitive, PartialEq, Eq, Clone, Copy)] #[repr(u8)] pub enum Keys { ButtonA = 0, diff --git a/platform/rustboyadvance-jni/src/emulator.rs b/platform/rustboyadvance-jni/src/emulator.rs index d54054e..4d0d242 100644 --- a/platform/rustboyadvance-jni/src/emulator.rs +++ b/platform/rustboyadvance-jni/src/emulator.rs @@ -18,7 +18,6 @@ use crate::audio::{self, connector::AudioJNIConnector, thread::AudioThreadComman struct Hardware { sample_rate: i32, audio_producer: Option>, - key_state: u16, } impl AudioInterface for Hardware { @@ -192,9 +191,8 @@ impl EmulatorContext { let hw = Rc::new(RefCell::new(Hardware { sample_rate: audio::util::get_sample_rate(env, audio_player), audio_producer: None, - key_state: 0xffff, })); - let mut gba = GameBoyAdvance::new(bios, gamepak, hw.clone(), hw.clone()); + let mut gba = GameBoyAdvance::new(bios, gamepak, hw.clone()); if skip_bios != 0 { info!("skipping bios"); gba.skip_bios(); @@ -242,9 +240,8 @@ impl EmulatorContext { let hw = Rc::new(RefCell::new(Hardware { sample_rate: audio::util::get_sample_rate(env, audio_player), audio_producer: None, - key_state: 0xffff, })); - let gba = GameBoyAdvance::from_saved_state(&savestate, bios, rom, hw.clone(), hw.clone()) + let gba = GameBoyAdvance::from_saved_state(&savestate, bios, rom, hw.clone()) .map_err(|e| { format!( "failed to create GameBoyAdvance from saved savestate, error {:?}", diff --git a/platform/rustboyadvance-libretro/src/lib.rs b/platform/rustboyadvance-libretro/src/lib.rs index 812d7e4..2dba8e8 100644 --- a/platform/rustboyadvance-libretro/src/lib.rs +++ b/platform/rustboyadvance-libretro/src/lib.rs @@ -11,10 +11,11 @@ use libretro_backend::{ use bit::BitIndex; use unsafe_unwrap::UnsafeUnwrap; -use rustboyadvance_core::keypad::Keys as GbaButton; +use rustboyadvance_core::keypad::Keys as _GbaButton; use rustboyadvance_core::prelude::*; use rustboyadvance_utils::audio::AudioRingBuffer; +use std::ops::Deref; use std::path::Path; use std::cell::RefCell; @@ -42,21 +43,33 @@ struct RustBoyAdvanceCore { audio: Option>>, } -fn set_button_state(key_state: &mut u16, button: JoypadButton, is_pressed: bool) { - let mapped_button = match button { - JoypadButton::A => GbaButton::ButtonA, - JoypadButton::B => GbaButton::ButtonB, - JoypadButton::Start => GbaButton::Start, - JoypadButton::Select => GbaButton::Select, - JoypadButton::Left => GbaButton::Left, - JoypadButton::Up => GbaButton::Up, - JoypadButton::Right => GbaButton::Right, - JoypadButton::Down => GbaButton::Down, - JoypadButton::L1 => GbaButton::ButtonL, - JoypadButton::R1 => GbaButton::ButtonR, - _ => unreachable!(), - }; - key_state.set_bit(mapped_button as usize, !is_pressed); +#[repr(transparent)] +struct GbaButton(_GbaButton); + +impl Deref for GbaButton { + type Target = _GbaButton; + fn deref(&self) -> &Self::Target { + return &self.0 + } +} + +impl From for GbaButton { + fn from(button: JoypadButton) -> Self { + let mapped = match button { + JoypadButton::A => _GbaButton::ButtonA, + JoypadButton::B => _GbaButton::ButtonB, + JoypadButton::Start => _GbaButton::Start, + JoypadButton::Select => _GbaButton::Select, + JoypadButton::Left => _GbaButton::Left, + JoypadButton::Up => _GbaButton::Up, + JoypadButton::Right => _GbaButton::Right, + JoypadButton::Down => _GbaButton::Down, + JoypadButton::L1 => _GbaButton::ButtonL, + JoypadButton::R1 => _GbaButton::ButtonR, + _ => panic!("unimplemented button {:?}", button), + }; + GbaButton(mapped) + } } impl libretro_backend::Core for RustBoyAdvanceCore { @@ -125,17 +138,17 @@ impl libretro_backend::Core for RustBoyAdvanceCore { let gba = unsafe { self.gba.as_mut().unsafe_unwrap() }; let audio = unsafe { self.audio.as_mut().unsafe_unwrap() }; - let key_state = gba.borrow_mut().get_key_state_mut(); + let key_state = gba.get_key_state_mut(); macro_rules! update_controllers { ( $( $button:ident ),+ ) => ( $( - set_button_state(key_state, JoypadButton::$button, handle.is_joypad_button_pressed( joypad_port, JoypadButton::$button ) ); + key_state.set_bit(*GbaButton::from(JoypadButton::$button) as usize, !handle.is_joypad_button_pressed( joypad_port, JoypadButton::$button )); )+ ) } - drop(key_state); update_controllers!(A, B, Start, Select, Left, Up, Right, Down, L1, R1); + drop(key_state); gba.frame(); diff --git a/platform/rustboyadvance-sdl2/src/main.rs b/platform/rustboyadvance-sdl2/src/main.rs index b3c95c6..801a84e 100644 --- a/platform/rustboyadvance-sdl2/src/main.rs +++ b/platform/rustboyadvance-sdl2/src/main.rs @@ -37,7 +37,6 @@ mod input; mod video; use audio::{create_audio_player, create_dummy_player}; -use input; use video::{create_video_interface, SCREEN_HEIGHT, SCREEN_WIDTH}; use rustboyadvance_core::cartridge::BackupType; @@ -189,7 +188,6 @@ fn main() -> Result<(), Box> { } else { Rc::new(RefCell::new(create_audio_player(&sdl_context))) }; - let input = Rc::new(RefCell::new(create_input())); let mut savestate_path = get_savestate_path(&Path::new(&rom_path)); @@ -212,7 +210,6 @@ fn main() -> Result<(), Box> { gamepak, video.clone(), audio.clone(), - input.clone(), ); if skip_bios {