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
use core::fmt::Debug;
pub mod cursor;
pub mod fuller;
pub mod kempston;
pub mod sinclair;
bitflags! {
#[derive(Default)]
pub struct Directions: u8 {
const UP = 0b0000_0001;
const RIGHT = 0b0000_0010;
const DOWN = 0b0000_0100;
const LEFT = 0b0000_1000;
}
}
pub enum JoyDirection {
Center,
Up,
UpRight,
Right,
DownRight,
Down,
DownLeft,
Left,
UpLeft
}
pub trait JoystickInterface {
fn fire(&mut self, btn: u8, pressed: bool);
fn get_fire(&self, btn: u8) -> bool;
fn set_directions(&mut self, dir: Directions);
fn get_directions(&self) -> Directions;
#[inline]
fn direction(&mut self, dir: JoyDirection) {
self.set_directions(match dir {
JoyDirection::Center => Directions::empty(),
JoyDirection::Up => Directions::UP,
JoyDirection::UpRight => Directions::UP|Directions::RIGHT,
JoyDirection::Right => Directions::RIGHT,
JoyDirection::DownRight => Directions::DOWN|Directions::RIGHT,
JoyDirection::Down => Directions::DOWN,
JoyDirection::DownLeft => Directions::DOWN|Directions::LEFT,
JoyDirection::Left => Directions::LEFT,
JoyDirection::UpLeft => Directions::UP|Directions::LEFT,
})
}
#[inline]
fn center(&mut self) {
self.set_directions(Directions::empty());
}
#[inline]
fn is_up(&self) -> bool {
self.get_directions().intersects(Directions::UP)
}
#[inline]
fn is_right(&self) -> bool {
self.get_directions().intersects(Directions::RIGHT)
}
#[inline]
fn is_left(&self) -> bool {
self.get_directions().intersects(Directions::LEFT)
}
#[inline]
fn is_down(&self) -> bool {
self.get_directions().intersects(Directions::DOWN)
}
#[inline]
fn is_center(&self) -> bool {
self.get_directions().intersects(Directions::empty())
}
}
pub trait JoystickDevice: Debug {
fn port_read(&self, port: u16) -> u8;
fn port_write(&mut self, _port: u16, _data: u8) -> bool { false }
}
#[derive(Clone, Copy, Default, Debug)]
pub struct NullJoystickDevice;
impl JoystickDevice for NullJoystickDevice {
fn port_read(&self, _port: u16) -> u8 {
u8::max_value()
}
}
impl JoystickInterface for NullJoystickDevice {
fn fire(&mut self, _btn: u8, _pressed: bool) {}
fn get_fire(&self, _btn: u8) -> bool { false }
fn set_directions(&mut self, _dir: Directions) {}
fn get_directions(&self) -> Directions {
Directions::empty()
}
}