forked from pcsx-redux/nugget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvancedpad.hh
208 lines (181 loc) · 7.77 KB
/
advancedpad.hh
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
MIT License
Copyright (c) 2024 PCSX-Redux authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <EASTL/functional.h>
#include <stdint.h>
namespace psyqo {
/**
* @brief An advanced class to access the pads.
*
* @details This class is meant to be used as a singleton, probably in
* the `Application` derived class. It does not use the BIOS'
* PAD interface. Instead, it uses the SIO interface directly, and
* can therefore support more device types including multitaps.
*/
class AdvancedPad {
public:
enum Pad { Pad1a, Pad1b, Pad1c, Pad1d, Pad2a, Pad2b, Pad2c, Pad2d };
enum Button {
Select = 0,
L3 = 1,
R3 = 2,
Start = 3,
Up = 4,
Right = 5,
Down = 6,
Left = 7,
L2 = 8,
R2 = 9,
L1 = 10,
R1 = 11,
Triangle = 12,
Circle = 13,
Cross = 14,
Square = 15,
};
enum Command : uint8_t {
PadSelect = 0x01,
ReadPad = 0x42, // 'B' Read Buttons AND analog inputs
// Config mode commands
ToggleConfigMode = 0x43, // 'C' Enter/Exit Configuration Mode
SetLED = 0x44, // 'D' Set LED State (analog mode on/off)
GetLED = 0x45, // 'E' Get LED State (and whatever values)
GetMotorInfo = 0x46, // 'F' Allegedly get info about a motor
GetMotorList = 0x47, // 'G' Allegedly get list of motors
GetMotorState = 0x48, // 'H' Allegedly get motor state
GetSupportedModes = 0x4c, // 'L' Allegedly get supported modes
ConfigRequestFormat = 0x4d, // 'M' Allegedly configure poll request format
ConfigResponseFormat = 0x4f, // 'O' Allegedly configure poll response format
};
enum PadType : uint8_t {
Mouse = 0x12, // (two button mouse)
NegCon = 0x23, // (steering twist/wheel/paddle)
KonamiLightgun = 0x31, // (IRQ10-type)
DigitalPad = 0x41, // (or analog pad/stick in digital mode; LED=Off)
AnalogStick = 0x53, // (or analog pad in "flight mode"; LED=Green)
NamcoLightgun = 0x63, // (Cinch-type)
AnalogPad = 0x73, // (in normal analog mode; LED=Red)
Multitap = 0x80, // (multiplayer adaptor) (when activated)
Jogcon = 0xe3, // (steering dial)
ConfigMode = 0xf3, // (when in config mode; see rumble command 43h)
None = 0xff // (no controller connected, pins floating High-Z)
};
struct Event {
enum { PadConnected, PadDisconnected, ButtonPressed, ButtonReleased } type;
Pad pad;
Button button;
};
/**
* @brief Initializes the pads.
*
* @details This method should be called once at the beginning of
* the program, preferably from the `Application::prepare` method.
* The `mode` parameter can be used to indicate whether the ports
* should be polled one at a time, or both at once. The default is
* `PollingMode::Normal`, which will poll one port per frame. The
* `PollingMode::Fast` mode will poll all ports at once each frame,
* which can reduce input lag, but will also increase the CPU usage.
*
* @param mode The polling mode to use.
*/
enum class PollingMode { Normal, Fast };
void initialize(PollingMode mode = PollingMode::Normal);
/**
* @brief Sets the event callback function.
*
* @details The event callback will be called for each pad-related event,
* such as pad connection / disconnection, or button press / release.
* The callback will only be called between frames.
*
* Scenes that are calling `setOnEvent` during their `start` method should
* call `setOnEvent` again in their `teardown` method with the `nullptr`
* value in order to unregister the event callback cleanly.
*
* Only one callback can be registered at a time, so setting a new
* callback will simply remove the previous one.
*
* Careful about what is called from the callback: pushing or popping scenes
* might call into `setOnEvent` as a result, and could end up corrupting
* memory as a result of the callback being deleted while being executed.
*/
void setOnEvent(eastl::function<void(Event)>&& callback) { m_callback = eastl::move(callback); }
/**
* @brief Returns the state of a pad.
*
* @details Returns the state of a pad. The state is a boolean value
* that is `true` if the pad is connected, and `false` otherwise.
*
* @param pad The pad to query.
* @return A boolean value indicating whether the pad is connected.
*/
bool isPadConnected(Pad pad) const { return (m_padData[pad][0] & 0xff) == 0; }
/**
* @brief Returns the state of a button.
*
* @details Returns the state of a button. The state is a boolean value
* that is `true` if the button is pressed, and `false` otherwise.
*
* @param pad The pad to query.
* @param button The button to query.
* @return A boolean value indicating whether the button is pressed.
*/
bool isButtonPressed(Pad pad, Button button) const { return (m_padData[pad][1] & (1 << button)) == 0; }
private:
void busyLoop(unsigned delay) {
unsigned cycles = 0;
while (++cycles < delay) asm("");
};
void flushRxBuffer();
uint8_t outputDefault(unsigned ticks);
uint8_t outputMultitap(unsigned ticks);
void processChanges(Pad pad);
void readPad();
uint8_t transceive(uint8_t data_out);
bool waitForAck(); // true if ack received, false if timeout
uint16_t m_padData[8][4];
eastl::function<void(Event)> m_callback;
bool m_connected[8] = {false, false, false, false, false, false, false, false};
uint16_t m_buttons[8] = {
0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
};
uint8_t m_portToProbe = 0;
uint8_t m_portsToProbeByVSync = 0;
};
// prefix increment operator
inline psyqo::AdvancedPad::Pad& operator++(psyqo::AdvancedPad::Pad& pad) {
return pad = static_cast<psyqo::AdvancedPad::Pad>((static_cast<unsigned>(pad) + 1) & 7);
}
// postfix increment operator
inline psyqo::AdvancedPad::Pad operator++(psyqo::AdvancedPad::Pad& pad, int) {
psyqo::AdvancedPad::Pad copy(pad);
pad = static_cast<psyqo::AdvancedPad::Pad>((static_cast<unsigned>(pad) + 1) & 7);
return copy;
}
// prefix decrement operator
inline psyqo::AdvancedPad::Pad& operator--(psyqo::AdvancedPad::Pad& pad) {
return pad = static_cast<psyqo::AdvancedPad::Pad>((static_cast<unsigned>(pad) - 1) & 7);
}
// postfix decrement operator
inline psyqo::AdvancedPad::Pad operator--(psyqo::AdvancedPad::Pad& pad, int) {
psyqo::AdvancedPad::Pad copy(pad);
pad = static_cast<psyqo::AdvancedPad::Pad>((static_cast<unsigned>(pad) - 1) & 7);
return copy;
}
} // namespace psyqo