-
Notifications
You must be signed in to change notification settings - Fork 0
/
joypad.rs
177 lines (155 loc) · 5.37 KB
/
joypad.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use gilrs::{Button, Event as GamepadEvent, EventType as GamepadEventType};
use winit::event::{ElementState, KeyboardInput, VirtualKeyCode};
#[derive(Debug)]
pub struct Joypad {
/// 0xFF00 | P1/JOYP - Player 1 Joypad
pub(crate) p1: u8,
ext: JoypadState,
interrupt: bool,
}
impl Default for Joypad {
fn default() -> Self {
Self {
p1: 0xFF,
ext: Default::default(),
interrupt: Default::default(),
}
}
}
impl Joypad {
pub(crate) fn interrupt(&self) -> bool {
self.interrupt
}
pub(crate) fn set_interrupt(&mut self, value: bool) {
self.interrupt = value;
}
pub(crate) fn update(&mut self, byte: u8) {
let direction_row = (byte >> 4) & 0x01 == 0x00;
let action_row = (byte >> 5) & 0x01 == 0x00;
let updated = match (direction_row, action_row) {
(true, false) => (byte & 0x30) | self.ext.as_direction_bits(),
(false, true) => (byte & 0x30) | self.ext.as_action_bits(),
_ => {
// TODO: What if both or no rows are selected?
let merge = self.ext.as_action_bits() | self.ext.as_action_bits();
(byte & 0x30) | merge
}
};
self.p1 = updated
}
}
#[derive(Debug, Default)]
struct JoypadState {
// Direction Row
dpad_down: ButtonEvent,
dpad_up: ButtonEvent,
dpad_left: ButtonEvent,
dpad_right: ButtonEvent,
// Action Row
start: ButtonEvent,
select: ButtonEvent,
south: ButtonEvent,
east: ButtonEvent,
}
impl JoypadState {
fn as_direction_bits(&self) -> u8 {
(self.dpad_down as u8) << 3
| (self.dpad_up as u8) << 2
| (self.dpad_left as u8) << 1
| self.dpad_right as u8
}
fn as_action_bits(&self) -> u8 {
(self.start as u8) << 3
| (self.select as u8) << 2
| (self.south as u8) << 1
| self.east as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
enum ButtonEvent {
Pressed = 0,
Standby = 1,
}
// used in the context of is_pressed: bool
impl From<bool> for ButtonEvent {
fn from(value: bool) -> Self {
match value {
true => Self::Pressed,
false => Self::Standby,
}
}
}
impl Default for ButtonEvent {
fn default() -> Self {
Self::Standby
}
}
impl ButtonEvent {
fn update(&mut self, is_pressed: bool, irq: &mut bool) {
*self = is_pressed.into();
if let ButtonEvent::Pressed = *self {
*irq = true;
}
}
}
#[inline]
pub(crate) fn handle_keyboard_input(pad: &mut Joypad, key: KeyboardInput) {
let state = &mut pad.ext;
let irq = &mut pad.interrupt;
match key.state {
ElementState::Pressed => match key.virtual_keycode {
Some(VirtualKeyCode::Down) => state.dpad_down.update(true, irq),
Some(VirtualKeyCode::Up) => state.dpad_up.update(true, irq),
Some(VirtualKeyCode::Left) => state.dpad_left.update(true, irq),
Some(VirtualKeyCode::Right) => state.dpad_right.update(true, irq),
Some(VirtualKeyCode::Return) => state.start.update(true, irq),
Some(VirtualKeyCode::RShift) => state.select.update(true, irq),
Some(VirtualKeyCode::Z) => state.south.update(true, irq),
Some(VirtualKeyCode::X) => state.east.update(true, irq),
None | Some(_) => {}
},
ElementState::Released => match key.virtual_keycode {
Some(VirtualKeyCode::Down) => state.dpad_down.update(false, irq),
Some(VirtualKeyCode::Up) => state.dpad_up.update(false, irq),
Some(VirtualKeyCode::Left) => state.dpad_left.update(false, irq),
Some(VirtualKeyCode::Right) => state.dpad_right.update(false, irq),
Some(VirtualKeyCode::Return) => state.start.update(false, irq),
Some(VirtualKeyCode::RShift) => state.select.update(false, irq),
Some(VirtualKeyCode::Z) => state.south.update(false, irq),
Some(VirtualKeyCode::X) => state.east.update(false, irq),
None | Some(_) => {}
},
}
}
#[inline]
pub(crate) fn handle_gamepad_input(pad: &mut Joypad, event: GamepadEvent) {
use Button::*;
use GamepadEventType::*;
let state = &mut pad.ext;
let irq = &mut pad.interrupt;
match event.event {
ButtonPressed(btn, _) => match btn {
DPadDown => state.dpad_down.update(true, irq),
DPadUp => state.dpad_up.update(true, irq),
DPadLeft => state.dpad_left.update(true, irq),
DPadRight => state.dpad_right.update(true, irq),
Start => state.start.update(true, irq),
Select => state.select.update(true, irq),
South => state.south.update(true, irq),
East => state.east.update(true, irq),
_ => {}
},
ButtonReleased(btn, _) => match btn {
DPadDown => state.dpad_down.update(false, irq),
DPadUp => state.dpad_up.update(false, irq),
DPadLeft => state.dpad_left.update(false, irq),
DPadRight => state.dpad_right.update(false, irq),
Start => state.start.update(false, irq),
Select => state.select.update(false, irq),
South => state.south.update(false, irq),
East => state.east.update(false, irq),
_ => {}
},
_ => {}
}
}