Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

fix wiimote resting acceleration and ir report encoding. also remove pointless tests. fixes ffcc crystal bearers pointer disappearing (the former)

Layle057d60de69e6
diff --git a/crates/gecko/src/hollywood/ipc/usb/wiimote.rs b/crates/gecko/src/hollywood/ipc/usb/wiimote.rs
index ca8edfe..323f357 100644
--- a/crates/gecko/src/hollywood/ipc/usb/wiimote.rs
+++ b/crates/gecko/src/hollywood/ipc/usb/wiimote.rs
@@ -126,9 +126,14 @@ 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 WIIMOTE_ACCEL_ZERO_G: i32 = WIIMOTE_EEPROM_CALIBRATION[0x16] as i32;
+const WIIMOTE_ACCEL_ONE_G: i32 = WIIMOTE_EEPROM_CALIBRATION[0x1A] as i32;
+const WIIMOTE_ACCEL_LSB_PER_G: f32 = (WIIMOTE_ACCEL_ONE_G - WIIMOTE_ACCEL_ZERO_G) as f32;
+const ACCEL_REST: [u8; 3] = [
+ WIIMOTE_ACCEL_ZERO_G as u8,
+ WIIMOTE_ACCEL_ZERO_G as u8,
+ WIIMOTE_ACCEL_ONE_G as u8,
+];
pub const REPORT_HZ: u64 = 200;
@@ -661,23 +666,23 @@ fn pack_ir_basic(objects: &[Option<(u16, u16)>; 4]) -> [u8; 10] {
out
}
-fn split_ir_coords(x: u16, y: u16) -> (u8, u8, u8, u8) {
- (x as u8, ((x >> 8) & 0x03) as u8, y as u8, ((y >> 8) & 0x03) as u8)
+fn split_ir_coords(x: u16, y: u16) -> (u8, u8, u8) {
+ let hi = (((y >> 8) & 0x03) << 2) | ((x >> 8) & 0x03);
+
+ (x as u8, y as u8, hi as u8)
}
fn pack_ir_basic_pair(out: &mut [u8], p1: Option<(u16, u16)>, p2: Option<(u16, u16)>) {
- if let Some((x, y)) = p1 {
- let (x_lo, x_hi, y_lo, y_hi) = self::split_ir_coords(x, y);
- out[0] = x_lo;
- out[1] = y_lo;
- out[2] = (out[2] & 0xF0) | (y_hi << 2) | x_hi;
- }
+ for (base, shift, point) in [(0usize, 4u32, p1), (3, 0, p2)] {
+ let Some((x, y)) = point else {
+ continue;
+ };
+
+ let (x_lo, y_lo, hi) = self::split_ir_coords(x, y);
- if let Some((x, y)) = p2 {
- let (x_lo, x_hi, y_lo, y_hi) = self::split_ir_coords(x, y);
- out[3] = x_lo;
- out[4] = y_lo;
- out[2] = (out[2] & 0x0F) | (y_hi << 6) | (x_hi << 4);
+ out[base] = x_lo;
+ out[base + 1] = y_lo;
+ out[2] = (out[2] & !(0xFu8 << shift)) | (hi << shift);
}
}
@@ -687,10 +692,11 @@ fn pack_ir_extended(objects: &[Option<(u16, u16)>; 4]) -> [u8; 12] {
for (i, slot) in objects.iter().enumerate() {
if let Some((x, y)) = *slot {
let base = i * 3;
- let (x_lo, x_hi, y_lo, y_hi) = self::split_ir_coords(x, y);
+ let (x_lo, y_lo, hi) = self::split_ir_coords(x, y);
+
out[base] = x_lo;
out[base + 1] = y_lo;
- out[base + 2] = (3 << 4) | (y_hi << 2) | x_hi;
+ out[base + 2] = (hi << 4) | 3;
}
}
@@ -714,62 +720,6 @@ fn trivial_ack(button_bytes: [u8; 2], report_id: u8) -> Vec<Vec<u8>> {
]]
}
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn core_button_report_encodes_a_press_and_release() {
- let mut wiimote = WiimoteState::default();
-
- assert!(wiimote.set_buttons(BTN_A));
- assert_eq!(wiimote.make_input_report(), [0xA1, 0x30, 0x00, 0x08]);
-
- assert!(wiimote.set_buttons(0));
- assert_eq!(wiimote.make_input_report(), [0xA1, 0x30, 0x00, 0x00]);
- }
-
- #[test]
- fn data_reporting_mode_selects_extended_minimal_reports() {
- let mut wiimote = WiimoteState::default();
-
- assert!(wiimote.set_buttons(BTN_A));
- let acks = wiimote.handle_output_report(&[0xA2, 0x12, 0x04, 0x31]);
- assert_eq!(acks, vec![vec![0xA1, 0x22, 0x00, 0x08, 0x12, 0x00]]);
- assert!(wiimote.continuous);
- assert_eq!(wiimote.make_input_report(), [0xA1, 0x31, 0x00, 0x08, 0x80, 0x80, 0xB3]);
-
- let acks = wiimote.handle_output_report(&[0xA2, 0x12, 0x00, 0x33]);
- assert_eq!(acks, vec![vec![0xA1, 0x22, 0x00, 0x08, 0x12, 0x00]]);
- let mut expected = vec![0xA1, 0x33, 0x00, 0x08, 0x80, 0x80, 0xB3];
- expected.extend_from_slice(&[0xFF; 12]);
- assert_eq!(wiimote.make_input_report(), expected);
- }
-
- #[test]
- fn read_memory_returns_chunked_calibration_data() {
- let mut wiimote = WiimoteState::default();
- let reports = wiimote.handle_output_report(&[0xA2, 0x17, 0x00, 0x00, 0x00, 0x16, 0x00, 0x10]);
- assert_eq!(reports.len(), 1);
- let r = &reports[0];
- assert_eq!(&r[0..2], &[0xA1, 0x21]);
- assert_eq!(r[4], 0xF0); // 16 bytes - 1 = 0x0F, shifted into upper nibble
- assert_eq!(&r[5..7], &[0x00, 0x16]);
- assert_eq!(&r[7..23], &WIIMOTE_EEPROM_CALIBRATION[22..38]);
- }
-
- #[test]
- fn unsupported_report_mode_is_rejected_and_kept_at_previous() {
- let mut wiimote = WiimoteState::default();
- // Pin a known mode first.
- wiimote.handle_output_report(&[0xA2, 0x12, 0x00, 0x31]);
- assert_eq!(wiimote.report_mode, ReportMode::CoreAccel);
- // Reject unknown mode 0x3D and stay on 0x31.
- wiimote.handle_output_report(&[0xA2, 0x12, 0x00, 0x3D]);
- assert_eq!(wiimote.report_mode, ReportMode::CoreAccel);
- }
-}
-
impl WiimoteState {
pub(super) fn save_state(&self, w: &mut crate::savestate::StateWriter) {
w.u16(self.buttons);