Skip to main content

gb/mmio/
joypad.rs

1//! Joypad input (`JOYP`).
2
3use bitfield_struct::bitfield;
4use voladdress::{Safe, VolAddress};
5
6/// Joypad register (`JOYP`). Active low: a cleared bit means selected, or the input
7/// pressed.
8///
9/// | Bit | Field | Access | Meaning |
10/// |-----|-------|--------|---------|
11/// | 7-6 | —         |     | Unused. |
12/// | 5   | `buttons` | R/W | Selects the action buttons (A/B/Select/Start). |
13/// | 4   | `dpad`    | R/W | Selects the d-pad. |
14/// | 3   | `p13`     | RO  | Input line P13: Down (d-pad) or Start (buttons). |
15/// | 2   | `p12`     | RO  | Input line P12: Up (d-pad) or Select (buttons). |
16/// | 1   | `p11`     | RO  | Input line P11: Left (d-pad) or B (buttons). |
17/// | 0   | `p10`     | RO  | Input line P10: Right (d-pad) or A (buttons). |
18#[bitfield(u8)]
19#[derive(PartialEq, Eq)]
20pub struct Joypad {
21    /// Input line P10: Right (d-pad) or A (buttons).
22    pub p10: bool,
23    /// Input line P11: Left (d-pad) or B (buttons).
24    pub p11: bool,
25    /// Input line P12: Up (d-pad) or Select (buttons).
26    pub p12: bool,
27    /// Input line P13: Down (d-pad) or Start (buttons).
28    pub p13: bool,
29    /// Selects the d-pad.
30    pub dpad: bool,
31    /// Selects the action buttons (A/B/Select/Start).
32    pub buttons: bool,
33    #[bits(2)]
34    __: u8,
35}
36
37/// Joypad: write selects the button or d-pad group, read returns its state.
38pub const JOYP: VolAddress<Joypad, Safe, Safe> = unsafe { VolAddress::new(0xFF00) };
39
40const _: () = {
41    assert!(Joypad::new().with_buttons(true).into_bits() == 0b0010_0000);
42    assert!(Joypad::new().with_dpad(true).into_bits() == 0b0001_0000);
43    assert!(Joypad::new().with_p10(true).into_bits() == 0b0000_0001);
44};