Skip to main content

sm64_binds/sm64/
game_pad.rs

1use serde::{Deserialize, Serialize};
2
3/// Byte layout of a pad in game memory, mirroring `OSContPad` in
4/// `sm64-port/src/pc/pc_main.c`.
5pub const GAME_PAD_SIZE: usize = 4;
6
7#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
8pub struct GamePad {
9    pub button: u16,
10    pub stick_x: i8,
11    pub stick_y: i8,
12}
13
14impl GamePad {
15    pub const fn new(button: u16, stick_x: i8, stick_y: i8) -> Self {
16        GamePad {
17            button,
18            stick_x,
19            stick_y,
20        }
21    }
22
23    pub fn equals(&self, pad: &GamePad) -> bool {
24        self == pad
25    }
26
27    /// Decodes the 4 byte little endian pad as laid out in game memory.
28    ///
29    /// # Panics
30    ///
31    /// Panics if `data` is shorter than [`GAME_PAD_SIZE`].
32    pub fn from_bytes(data: &[u8; GAME_PAD_SIZE]) -> Self {
33        let button = u16::from_le_bytes(data[0..2].try_into().unwrap());
34        let stick_x = data[2] as i8;
35        let stick_y = data[3] as i8;
36        GamePad::new(button, stick_x, stick_y)
37    }
38
39    pub const fn is_pressed(&self, button_mask: u16) -> bool {
40        (self.button & button_mask) != 0
41    }
42
43    pub fn enable_button(&mut self, button_mask: u16) {
44        self.button |= button_mask;
45    }
46
47    pub fn disable_button(&mut self, button_mask: u16) {
48        self.button &= !button_mask;
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55    use crate::buttons;
56
57    #[test]
58    fn from_bytes_decodes_little_endian_fields() {
59        // A_BUTTON is 0x8000, which is [0x00, 0x80] little endian.
60        let pad = GamePad::from_bytes(&[0x00, 0x80, 0x50, 0xB0]);
61        assert_eq!(pad.button, buttons::A_BUTTON);
62        assert_eq!(pad.stick_x, 80);
63        assert_eq!(pad.stick_y, -80, "stick_y is signed");
64    }
65
66    #[test]
67    fn from_bytes_round_trips() {
68        let pad = GamePad::new(buttons::A_BUTTON | buttons::Z_TRIG, -80, 80);
69        let bytes = [
70            pad.button.to_le_bytes()[0],
71            pad.button.to_le_bytes()[1],
72            pad.stick_x as u8,
73            pad.stick_y as u8,
74        ];
75        assert_eq!(GamePad::from_bytes(&bytes), pad);
76    }
77
78    #[test]
79    fn default_is_a_neutral_pad() {
80        assert_eq!(GamePad::default(), GamePad::new(0, 0, 0));
81    }
82
83    #[test]
84    fn is_pressed_matches_every_single_bit() {
85        // N64 face buttons live in the high byte, triggers and sticks in the low.
86        for mask in [
87            buttons::A_BUTTON,
88            buttons::B_BUTTON,
89            buttons::Z_TRIG,
90            buttons::START_BUTTON,
91            buttons::R_JPAD,
92        ] {
93            let mut pad = GamePad::default();
94            pad.enable_button(mask);
95            assert!(pad.is_pressed(mask), "0x{mask:04x} should be pressed");
96            assert_eq!(
97                pad.is_pressed(!mask),
98                pad.button == 0,
99                "complement of 0x{mask:04x} must not report a stray press"
100            );
101        }
102    }
103
104    #[test]
105    fn is_pressed_is_false_for_unheld_bits() {
106        let pad = GamePad::new(buttons::A_BUTTON, 0, 0);
107        assert!(pad.is_pressed(buttons::A_BUTTON));
108        assert!(!pad.is_pressed(buttons::B_BUTTON));
109    }
110
111    #[test]
112    fn enable_and_disable_button_round_trip() {
113        let mut pad = GamePad::default();
114
115        pad.enable_button(buttons::A_BUTTON);
116        pad.enable_button(buttons::B_BUTTON);
117        assert_eq!(pad.button, buttons::A_BUTTON | buttons::B_BUTTON);
118
119        pad.disable_button(buttons::A_BUTTON);
120        assert_eq!(pad.button, buttons::B_BUTTON);
121
122        pad.disable_button(buttons::B_BUTTON);
123        assert_eq!(pad, GamePad::default());
124    }
125
126    #[test]
127    fn disable_button_leaves_other_buttons_alone() {
128        let mut pad = GamePad::new(buttons::A_BUTTON | buttons::B_BUTTON, 0, 0);
129        pad.disable_button(buttons::B_BUTTON);
130        assert_eq!(pad.button, buttons::A_BUTTON);
131    }
132
133    #[test]
134    fn equals_compares_all_three_fields() {
135        let pad = GamePad::new(buttons::A_BUTTON, 1, 2);
136        assert!(pad.equals(&GamePad::new(buttons::A_BUTTON, 1, 2)));
137        assert!(!pad.equals(&GamePad::new(buttons::B_BUTTON, 1, 2)));
138        assert!(!pad.equals(&GamePad::new(buttons::A_BUTTON, 9, 2)));
139        assert!(!pad.equals(&GamePad::new(buttons::A_BUTTON, 1, 9)));
140    }
141}