Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

SHAKE IT BABY SHAKE IT (FFCC GOES INGAME PROPERLY) BAZINGAAAAAA

Layledbba7454aabf
diff --git a/crates/backend-wgpu/src/shader_specialization.rs b/crates/backend-wgpu/src/shader_specialization.rs
index cfe5c0f..e4c58bf 100644
--- a/crates/backend-wgpu/src/shader_specialization.rs
+++ b/crates/backend-wgpu/src/shader_specialization.rs
@@ -18,7 +18,6 @@ const CACHE_MAGIC: [u8; 4] = *b"GSKC";
pub(crate) const CACHE_VERSION: u32 = 2;
pub(crate) const SHADER_CACHE_PATH: &str = "cache/shader_keys.bin";
-
#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
pub(crate) struct ShaderKey {
pub num_tev_stages: u8,
diff --git a/crates/gecko/src/hollywood/ipc.rs b/crates/gecko/src/hollywood/ipc.rs
index 228f228..53afce7 100644
--- a/crates/gecko/src/hollywood/ipc.rs
+++ b/crates/gecko/src/hollywood/ipc.rs
@@ -95,6 +95,8 @@ pub trait IosDevice: Send {
false
}
+ fn set_wiimote_shake(&mut self, _active: bool) {}
+
fn set_nunchuk(&mut self, _buttons: u8, _stick_x: u8, _stick_y: u8) -> bool {
false
}
diff --git a/crates/gecko/src/hollywood/ipc/usb.rs b/crates/gecko/src/hollywood/ipc/usb.rs
index 46aca19..30b8e19 100644
--- a/crates/gecko/src/hollywood/ipc/usb.rs
+++ b/crates/gecko/src/hollywood/ipc/usb.rs
@@ -63,6 +63,10 @@ impl IosDevice for Bluetooth {
Bluetooth::set_wiimote_buttons(self, buttons)
}
+ fn set_wiimote_shake(&mut self, active: bool) {
+ Bluetooth::set_wiimote_shake(self, active)
+ }
+
fn set_nunchuk(&mut self, buttons: u8, stick_x: u8, stick_y: u8) -> bool {
Bluetooth::set_nunchuk(self, buttons, stick_x, stick_y)
}
@@ -178,6 +182,16 @@ impl Bluetooth {
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);
+ }
+
fn set_nunchuk(&mut self, buttons: u8, stick_x: u8, stick_y: u8) -> bool {
let changed = self.wiimote.set_nunchuk(buttons, stick_x, stick_y);
diff --git a/crates/gecko/src/hollywood/ipc/usb/wiimote.rs b/crates/gecko/src/hollywood/ipc/usb/wiimote.rs
index d780bb8..217bcbc 100644
--- a/crates/gecko/src/hollywood/ipc/usb/wiimote.rs
+++ b/crates/gecko/src/hollywood/ipc/usb/wiimote.rs
@@ -126,6 +126,19 @@ const NUNCHUK_CALIBRATION: [u8; 16] = [
const NUNCHUK_KEY_REG_BASE: u8 = 0x40;
const NUNCHUK_KEY_LEN: usize = 16;
+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];
+
+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;
+
+#[inline]
+fn encode_wiimote_g(g: f32) -> u8 {
+ (WIIMOTE_ACCEL_ZERO_G + (g * WIIMOTE_ACCEL_LSB_PER_G).round() as i32).clamp(0, 255) as u8
+}
+
#[derive(Debug, Clone)]
pub(super) struct WiimoteState {
buttons: u16,
@@ -144,6 +157,8 @@ pub(super) struct WiimoteState {
nunchuk_key_valid: u16,
nunchuk_cipher: Cipher,
ir_pointer: Option<(u16, u16)>,
+ accel: [u8; 3],
+ shake_phase: f32,
}
impl Default for WiimoteState {
@@ -173,6 +188,8 @@ impl Default for WiimoteState {
nunchuk_key_valid: 0,
nunchuk_cipher: Cipher::IDENTITY,
ir_pointer: None,
+ accel: ACCEL_REST,
+ shake_phase: 0.0,
}
}
}
@@ -193,6 +210,24 @@ impl WiimoteState {
changed
}
+ pub(super) fn tick_shake(&mut self, active: bool) -> bool {
+ if 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
+ } else {
+ self.shake_phase = 0.0;
+ let was_at_rest = self.accel == ACCEL_REST;
+ self.accel = ACCEL_REST;
+ !was_at_rest
+ }
+ }
+
pub(super) fn set_ir_pointer(&mut self, pointer: Option<(u16, u16)>) -> bool {
let changed = self.ir_pointer != pointer;
self.ir_pointer = pointer;
@@ -205,7 +240,7 @@ impl WiimoteState {
pub(super) fn make_input_report(&self) -> Vec<u8> {
let [bb0, bb1] = self.button_bytes();
- let accel = [0x80u8, 0x80, 0xB3]; // X=0G, Y=0G, Z=+1G (gravity)
+ let accel = self.accel;
let mode = self.report_mode;
let mut r = vec![HID_PREFIX_INPUT, mode as u8, bb0, bb1];
diff --git a/crates/gecko/src/input.rs b/crates/gecko/src/input.rs
index fad698e..6eb1784 100644
--- a/crates/gecko/src/input.rs
+++ b/crates/gecko/src/input.rs
@@ -7,6 +7,7 @@ pub enum HostInput {
Gc(pad::PadStatus),
Wii {
wiimote_buttons: u16,
+ wiimote_shake: bool,
nunchuk_buttons: u8,
nunchuk_stick_x: u8,
nunchuk_stick_y: u8,
@@ -25,6 +26,7 @@ impl HostInput {
pub fn wii_neutral() -> Self {
Self::Wii {
wiimote_buttons: 0,
+ wiimote_shake: false,
nunchuk_buttons: 0,
nunchuk_stick_x: usb::NUNCHUK_STICK_CENTER,
nunchuk_stick_y: usb::NUNCHUK_STICK_CENTER,
diff --git a/crates/gecko/src/starlet.rs b/crates/gecko/src/starlet.rs
index b2c9711..a353d5e 100644
--- a/crates/gecko/src/starlet.rs
+++ b/crates/gecko/src/starlet.rs
@@ -6,6 +6,8 @@ use crate::system::SystemId;
use std::collections::{HashMap, VecDeque};
use std::path::PathBuf;
+const WIIMOTE_DEVICE_PATH: &str = "/dev/usb/oh1/57e/305";
+
// As per zayd. Tuned at GC clock (486 MHz, ~20 us).
const FINALIZE_DELAY_US: u64 = 20;
// Give the CPU breathing room between back-to-back IPC IRQs so other interrupt
@@ -107,19 +109,25 @@ impl Starlet {
pub fn set_wiimote_buttons(&mut self, buttons: u16) -> bool {
self.devices
- .get_mut("/dev/usb/oh1/57e/305")
+ .get_mut(WIIMOTE_DEVICE_PATH)
.is_some_and(|dev| dev.set_wiimote_buttons(buttons))
}
+ pub fn set_wiimote_shake(&mut self, active: bool) {
+ if let Some(dev) = self.devices.get_mut(WIIMOTE_DEVICE_PATH) {
+ dev.set_wiimote_shake(active);
+ }
+ }
+
pub fn set_nunchuk(&mut self, buttons: u8, stick_x: u8, stick_y: u8) -> bool {
self.devices
- .get_mut("/dev/usb/oh1/57e/305")
+ .get_mut(WIIMOTE_DEVICE_PATH)
.is_some_and(|dev| dev.set_nunchuk(buttons, stick_x, stick_y))
}
pub fn set_ir_pointer(&mut self, pointer: Option<(u16, u16)>) -> bool {
self.devices
- .get_mut("/dev/usb/oh1/57e/305")
+ .get_mut(WIIMOTE_DEVICE_PATH)
.is_some_and(|dev| dev.set_ir_pointer(pointer))
}
@@ -163,7 +171,7 @@ impl System<{ crate::WII }> {
.register("/dev/di", Box::new(ipc::di::DiskInterface::new()));
self.starlet.register("/dev/es", Box::new(ipc::es::ETicketServices));
self.starlet
- .register("/dev/usb/oh1/57e/305", Box::new(ipc::usb::Bluetooth::new()));
+ .register(WIIMOTE_DEVICE_PATH, Box::new(ipc::usb::Bluetooth::new()));
self.starlet.register("/dev/sdio/slot0", Box::new(ipc::sdio::SdCard));
}
diff --git a/crates/gecko/src/system.rs b/crates/gecko/src/system.rs
index 891973b..652b0a1 100644
--- a/crates/gecko/src/system.rs
+++ b/crates/gecko/src/system.rs
@@ -559,12 +559,14 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
}
HostInput::Wii {
wiimote_buttons,
+ wiimote_shake,
nunchuk_buttons,
nunchuk_stick_x,
nunchuk_stick_y,
ir_pointer,
} if SYSTEM == WII => {
self.starlet.set_wiimote_buttons(*wiimote_buttons);
+ self.starlet.set_wiimote_shake(*wiimote_shake);
self.starlet
.set_nunchuk(*nunchuk_buttons, *nunchuk_stick_x, *nunchuk_stick_y);
self.starlet.set_ir_pointer(*ir_pointer);
diff --git a/crates/mcp/src/server.rs b/crates/mcp/src/server.rs
index 51dafdc..fd26066 100644
--- a/crates/mcp/src/server.rs
+++ b/crates/mcp/src/server.rs
@@ -114,6 +114,10 @@ pub struct SetWiimoteArgs {
description = "Wiimote core button bitmask: A=0x0008, B=0x0004, ONE=0x0002, TWO=0x0001, MINUS=0x0010, HOME=0x0080, PLUS=0x1000, LEFT=0x0100, RIGHT=0x0200, DOWN=0x0400, UP=0x0800"
)]
pub buttons: Option<u16>,
+ #[schemars(
+ description = "Simulate shaking the Wiimote. While true, the Wiimote core accelerometer reports a 2g, 10 Hz sinusoid on all axes (on top of the +1g Z gravity baseline), which games detect as a shake gesture."
+ )]
+ pub shake: Option<bool>,
#[schemars(description = "Nunchuk button bitmask: Z=0x01, C=0x02")]
pub nunchuk_buttons: Option<u8>,
#[schemars(description = "Nunchuk stick X (0..=255, center=0x80, full-left=0x00, full-right=0xFF)")]
@@ -582,6 +586,7 @@ impl McpServer {
require_loaded(&self.shared)?;
let input = gecko::HostInput::Wii {
wiimote_buttons: args.buttons.unwrap_or(0),
+ wiimote_shake: args.shake.unwrap_or(false),
nunchuk_buttons: args.nunchuk_buttons.unwrap_or(0),
nunchuk_stick_x: args
.nunchuk_stick_x
diff --git a/crates/tinyapp/src/app.rs b/crates/tinyapp/src/app.rs
index 63ad786..1cf7ba6 100644
--- a/crates/tinyapp/src/app.rs
+++ b/crates/tinyapp/src/app.rs
@@ -431,12 +431,14 @@ impl ApplicationHandler<crate::UserEvent> for App {
HostInput::Gc(pad) => crate::update_pad(pad, key, pressed),
HostInput::Wii {
wiimote_buttons,
+ wiimote_shake,
nunchuk_buttons,
nunchuk_stick_x,
nunchuk_stick_y,
ir_pointer: _,
} => {
crate::update_wiimote_keys(wiimote_buttons, key, pressed);
+ crate::update_wiimote_motion_keys(wiimote_shake, key, pressed);
crate::update_nunchuk_keys(nunchuk_buttons, nunchuk_stick_x, nunchuk_stick_y, key, pressed);
}
}
diff --git a/crates/tinyapp/src/main.rs b/crates/tinyapp/src/main.rs
index b245ae5..ac42245 100644
--- a/crates/tinyapp/src/main.rs
+++ b/crates/tinyapp/src/main.rs
@@ -525,6 +525,12 @@ pub fn update_wiimote_keys(buttons: &mut u16, key: KeyCode, pressed: bool) {
self::set_bit(buttons, mask, pressed);
}
+pub fn update_wiimote_motion_keys(shake: &mut bool, key: KeyCode, pressed: bool) {
+ if let KeyCode::ShiftLeft = key {
+ *shake = pressed;
+ }
+}
+
pub fn update_nunchuk_keys(buttons: &mut u8, stick_x: &mut u8, stick_y: &mut u8, key: KeyCode, pressed: bool) {
use wiimote::{NUNCHUK_STICK_CENTER as C, NUNCHUK_STICK_MAX as MAX, NUNCHUK_STICK_MIN as MIN};
match key {