A GameCube/Wii emulator and debugger written in Rust.
input_sink
Layle691505f0e49c
diff --git a/crates/app/src/player/emu_thread.rs b/crates/app/src/player/emu_thread.rsindex 59ec514..63b6ffe 100644--- a/crates/app/src/player/emu_thread.rs+++ b/crates/app/src/player/emu_thread.rs@@ -15,6 +15,8 @@ pub fn run<const SYSTEM: SystemId>(let sleeper = SpinSleeper::default();let throttle_step = Duration::from_micros(5);+ emulator.set_input_sink(Box::new(input.clone()));+while !shutdown.load(Ordering::Relaxed) {while throttle.load(Ordering::Relaxed) && emulator.audio_sink.should_throttle() {if shutdown.load(Ordering::Relaxed) {diff --git a/crates/gecko/src/hollywood/ipc.rs b/crates/gecko/src/hollywood/ipc.rsindex a2e80a2..74a57ae 100644--- a/crates/gecko/src/hollywood/ipc.rs+++ b/crates/gecko/src/hollywood/ipc.rs@@ -106,6 +106,8 @@ pub trait IosDevice: Send {fn set_ir_pointer(&mut self, _pointer: Option<(u16, u16)>) -> bool {false}++ fn tick_input_report(&mut self) {}}pub struct Ipc {diff --git a/crates/gecko/src/hollywood/ipc/usb.rs b/crates/gecko/src/hollywood/ipc/usb.rsindex 316b30d..6639959 100644--- a/crates/gecko/src/hollywood/ipc/usb.rs+++ b/crates/gecko/src/hollywood/ipc/usb.rs@@ -9,7 +9,7 @@ use std::collections::VecDeque;pub use wiimote::{BTN_A, BTN_B, BTN_DOWN, BTN_HOME, BTN_LEFT, BTN_MINUS, BTN_ONE, BTN_PLUS, BTN_RIGHT, BTN_TWO, BTN_UP,IR_CAMERA_HEIGHT, IR_CAMERA_WIDTH, NUNCHUK_BTN_C, NUNCHUK_BTN_Z, NUNCHUK_STICK_CENTER, NUNCHUK_STICK_MAX,- NUNCHUK_STICK_MIN,+ NUNCHUK_STICK_MIN, REPORT_HZ,};const USB_CTRL: u32 = 0;@@ -74,6 +74,10 @@ impl IosDevice for Bluetooth {fn set_ir_pointer(&mut self, pointer: Option<(u16, u16)>) -> bool {Bluetooth::set_ir_pointer(self, pointer)}++ fn tick_input_report(&mut self) {+ Bluetooth::tick_input_report(self)+ }}impl Bluetooth {@@ -172,24 +176,11 @@ impl Bluetooth {tracing::debug!(buttons = format!("{buttons:#06x}"), "Wiimote button state changed");}- if self.host_hid_interrupt_cid.is_none() {- return changed;- }-- let report = self.wiimote.make_input_report();- self.queue_hid_input_report(report);-changed}fn set_wiimote_shake(&mut self, active: bool) {- let report_needed = self.wiimote.tick_shake(active);- if !report_needed || self.host_hid_interrupt_cid.is_none() {- return;- }-- let report = self.wiimote.make_input_report();- self.queue_hid_input_report(report);+ self.wiimote.set_shake(active);}fn set_nunchuk(&mut self, buttons: u8, stick_x: u8, stick_y: u8) -> bool {@@ -204,13 +195,6 @@ impl Bluetooth {);}- if self.host_hid_interrupt_cid.is_none() {- return changed;- }-- let report = self.wiimote.make_input_report();- self.queue_hid_input_report(report);-changed}@@ -221,14 +205,22 @@ impl Bluetooth {tracing::debug!(pointer = ?pointer, "IR pointer state changed");}+ changed+ }++ fn tick_input_report(&mut self) {if self.host_hid_interrupt_cid.is_none() {- return changed;+ return;+ }++ self.wiimote.tick_motion();++ if !self.wiimote.report_due() {+ return;}let report = self.wiimote.make_input_report();self.queue_hid_input_report(report);-- changed}}diff --git a/crates/gecko/src/hollywood/ipc/usb/wiimote.rs b/crates/gecko/src/hollywood/ipc/usb/wiimote.rsindex 217bcbc..52ff7b1 100644--- a/crates/gecko/src/hollywood/ipc/usb/wiimote.rs+++ b/crates/gecko/src/hollywood/ipc/usb/wiimote.rs@@ -130,9 +130,11 @@ const WIIMOTE_ACCEL_ZERO_G: i32 = 0x80;const WIIMOTE_ACCEL_LSB_PER_G: f32 = 0x1A as f32;const ACCEL_REST: [u8; 3] = [0x80, 0x80, 0xB3];+pub const REPORT_HZ: u64 = 200;+const SHAKE_FREQ_HZ: f32 = 10.0;const SHAKE_AMPLITUDE_G: f32 = 2.0;-const SHAKE_PHASE_STEP: f32 = 2.0 * std::f32::consts::PI * SHAKE_FREQ_HZ / 60.0;+const SHAKE_PHASE_STEP: f32 = 2.0 * std::f32::consts::PI * SHAKE_FREQ_HZ / REPORT_HZ as f32;#[inline]fn encode_wiimote_g(g: f32) -> u8 {@@ -158,7 +160,9 @@ pub(super) struct WiimoteState {nunchuk_cipher: Cipher,ir_pointer: Option<(u16, u16)>,accel: [u8; 3],+ shake_active: bool,shake_phase: f32,+ dirty: bool,}impl Default for WiimoteState {@@ -189,16 +193,19 @@ impl Default for WiimoteState {nunchuk_cipher: Cipher::IDENTITY,ir_pointer: None,accel: ACCEL_REST,+ shake_active: false,shake_phase: 0.0,+ dirty: false,}}}impl WiimoteState {pub(super) fn set_buttons(&mut self, buttons: u16) -> bool {- let old = self.buttons;+ let changed = self.buttons != buttons;self.buttons = buttons;- self.buttons != old+ self.dirty |= changed;+ changed}pub(super) fn set_nunchuk(&mut self, buttons: u8, stick_x: u8, stick_y: u8) -> bool {@@ -207,30 +214,40 @@ impl WiimoteState {self.nunchuk_buttons = buttons;self.nunchuk_stick_x = stick_x;self.nunchuk_stick_y = stick_y;+ self.dirty |= changed;changed}- pub(super) fn tick_shake(&mut self, active: bool) -> bool {- if active {+ pub(super) fn set_shake(&mut self, active: bool) {+ self.shake_active = active;+ }++ pub(super) fn tick_motion(&mut self) {+ if self.shake_active {self.shake_phase = (self.shake_phase + SHAKE_PHASE_STEP) % (2.0 * std::f32::consts::PI);- let lol = SHAKE_AMPLITUDE_G * self.shake_phase.sin();- self.accel = [- encode_wiimote_g(lol),- encode_wiimote_g(lol),- encode_wiimote_g(1.0 + lol),- ];- true+ let g = SHAKE_AMPLITUDE_G * self.shake_phase.sin();+ self.accel = [encode_wiimote_g(g), encode_wiimote_g(g), encode_wiimote_g(1.0 + g)];+ self.dirty = true;} else {self.shake_phase = 0.0;- let was_at_rest = self.accel == ACCEL_REST;- self.accel = ACCEL_REST;- !was_at_rest++ if self.accel != ACCEL_REST {+ self.accel = ACCEL_REST;+ self.dirty = true;+ }}}+ pub(super) fn report_due(&mut self) -> bool {+ let due = self.continuous || self.dirty;+ self.dirty = false;+ due+ }+pub(super) fn set_ir_pointer(&mut self, pointer: Option<(u16, u16)>) -> bool {let changed = self.ir_pointer != pointer;self.ir_pointer = pointer;+ self.dirty |= changed;changed}diff --git a/crates/gecko/src/input.rs b/crates/gecko/src/input.rsindex 6eb1784..772c777 100644--- a/crates/gecko/src/input.rs+++ b/crates/gecko/src/input.rs@@ -1,6 +1,20 @@use crate::flipper::si::pad;use crate::hollywood::ipc::usb;use crate::{GC, SystemId, WII};+use std::sync::{Arc, Mutex};++/// Live host input the emulator pulls from. The Wiimote report tick samples+/// this every report interval, so implementations should return the freshest+/// state available.+pub trait InputSink: Send {+ fn sample(&mut self) -> HostInput;+}++impl InputSink for Arc<Mutex<HostInput>> {+ fn sample(&mut self) -> HostInput {+ *self.lock().unwrap()+ }+}#[derive(Clone, Copy, Debug)]pub enum HostInput {diff --git a/crates/gecko/src/lib.rs b/crates/gecko/src/lib.rsindex 2ea5d77..b527a3e 100644--- a/crates/gecko/src/lib.rs+++ b/crates/gecko/src/lib.rs@@ -20,7 +20,7 @@ pub mod system;pub mod wii;pub use gamecube::GameCube;-pub use input::HostInput;+pub use input::{HostInput, InputSink};pub use system::{ExecutionMode, GC, System, SystemId, WII};pub use wii::Wii;diff --git a/crates/gecko/src/scheduler.rs b/crates/gecko/src/scheduler.rsindex 069f981..d6038b0 100644--- a/crates/gecko/src/scheduler.rs+++ b/crates/gecko/src/scheduler.rs@@ -156,6 +156,12 @@ impl<const SYSTEM: SystemId> Scheduler<SYSTEM> {crate::flipper::cp::PUMP_INTERVAL_CYCLES,crate::flipper::cp::pump_handler::<SYSTEM>,);+ if SYSTEM == system::WII {+ s.schedule_at(+ self::wiimote_report_interval(SYSTEM),+ self::wiimote_report_handler::<SYSTEM>,+ );+ }#[cfg(feature = "fps-counter")]s.schedule_at(self::cpu_clock(SYSTEM), crate::fps::fps_handler::<SYSTEM>);s@@ -186,6 +192,24 @@ pub fn reseed_vsync<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>) {.schedule_in(rate.cycles_per_frame(SYSTEM), self::vsync_handler::<SYSTEM>);}+#[inline(always)]+pub const fn wiimote_report_interval(system: SystemId) -> u64 {+ self::cpu_clock(system) / crate::hollywood::ipc::usb::REPORT_HZ+}++/// Reschedules itself at the Wiimote's 200Hz (TODO VERIFY) report rate. Samples+/// fresh host input and emits one HID input report per tick (continuous+/// mode) or only on change (non continuous).+pub fn wiimote_report_handler<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>) {+ sys.sample_host_input();+ sys.starlet.tick_wiimote();++ sys.scheduler.schedule_in(+ self::wiimote_report_interval(SYSTEM),+ self::wiimote_report_handler::<SYSTEM>,+ );+}+#[inline(always)]pub const fn dsp_batch_interval(system: SystemId) -> u64 {self::cpu_cycles_per_dsp_tick(system) * self::DSP_BATCH_SIZEdiff --git a/crates/gecko/src/starlet.rs b/crates/gecko/src/starlet.rsindex 9ac37c1..b7fc24d 100644--- a/crates/gecko/src/starlet.rs+++ b/crates/gecko/src/starlet.rs@@ -131,6 +131,12 @@ impl Starlet {.is_some_and(|dev| dev.set_ir_pointer(pointer))}+ pub fn tick_wiimote(&mut self) {+ if let Some(dev) = self.devices.get_mut(WIIMOTE_DEVICE_PATH) {+ dev.tick_input_report();+ }+ }+/// Drop an fd. Calls `close` on the underlying device first; for owned/// fds the device is dropped after close.pub fn close_fd(&mut self, fd: i32, ctx: &mut DeviceContext<'_>) -> i32 {diff --git a/crates/gecko/src/system.rs b/crates/gecko/src/system.rsindex 969d070..295c766 100644--- a/crates/gecko/src/system.rs+++ b/crates/gecko/src/system.rs@@ -1,3 +1,4 @@+use crate::input::InputSink;use crate::HostInput;use crate::audio::{AudioSink, EmptyAudioSink};use crate::dvd::DvdInterface;@@ -65,6 +66,9 @@ pub struct System<const SYSTEM: SystemId> {/// AID DMA pushes 8-frame stereo s16 blocks here.pub audio_sink: Box<dyn AudioSink>,+ /// Controller input is sampled from here FOR WII ONLY.+ pub input_sink: Option<Box<dyn InputSink>>,+#[cfg(feature = "hooks")]pub hook_host: Option<Box<dyn Host<SYSTEM> + Send>>,#[cfg(feature = "hooks")]@@ -117,6 +121,7 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {render_sink: Box::new(EmptyRenderSink::default()),audio_sink: Box::new(EmptyAudioSink),+ input_sink: None,#[cfg(feature = "hooks")]hook_host: None,@@ -647,6 +652,20 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {(fmt.columns(), fmt.lines())}+ pub fn set_input_sink(&mut self, sink: Box<dyn InputSink>) {+ self.input_sink = Some(sink);+ }++ pub fn sample_host_input(&mut self) {+ let Some(sink) = self.input_sink.as_mut() else {+ return;+ };++ let input = sink.sample();++ self.apply_host_input(&input);+ }+pub fn apply_host_input(&mut self, input: &HostInput) {match input {HostInput::Gc(pad) if SYSTEM == GC => {diff --git a/crates/tinyapp/src/thread.rs b/crates/tinyapp/src/thread.rsindex 3b69e3e..d1af7b8 100644--- a/crates/tinyapp/src/thread.rs+++ b/crates/tinyapp/src/thread.rs@@ -30,6 +30,8 @@ pub fn emu_thread<const SYSTEM: SystemId>(emulator.gx.recorder = Some(Box::new(FifoRecorder::new()));}+ emulator.set_input_sink(Box::new(input.clone()));+while !shutdown.load(Ordering::Relaxed) {while throttle && emulator.audio_sink.should_throttle() {if shutdown.load(Ordering::Relaxed) {