-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLinkCube.hpp
281 lines (235 loc) · 7.32 KB
/
LinkCube.hpp
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#ifndef LINK_CUBE_H
#define LINK_CUBE_H
// --------------------------------------------------------------------------
// A JOYBUS handler for the Link Port.
// --------------------------------------------------------------------------
// Usage:
// - 1) Include this header in your main.cpp file and add:
// LinkCube* linkCube = new LinkCube();
// - 2) Add the required interrupt service routines: (*)
// irq_init(NULL);
// irq_add(II_SERIAL, LINK_CUBE_ISR_SERIAL);
// - 3) Initialize the library with:
// linkCube->activate();
// - 4) Send 32-bit values:
// linkCube->send(0x12345678);
// // (now linkCube->pendingCount() will be 1 until the value is sent)
// - 5) Read 32-bit values:
// if (linkCube->canRead()) {
// u32 value = linkCube->read();
// // ...
// }
// --------------------------------------------------------------------------
// (*) libtonc's interrupt handler sometimes ignores interrupts due to a bug.
// That causes packet loss. You REALLY want to use libugba's instead.
// (see examples)
// --------------------------------------------------------------------------
#ifndef LINK_DEVELOPMENT
#pragma GCC system_header
#endif
#include "_link_common.hpp"
#ifndef LINK_CUBE_QUEUE_SIZE
/**
* @brief Buffer size (how many incoming and outgoing values the queues can
* store at max). The default value is `10`, which seems fine for most games.
* \warning This affects how much memory is allocated. With the default value,
* it's around `120` bytes. There's a double-buffered pending queue (to avoid
* data races), and 1 outgoing queue.
* \warning You can approximate the usage with `LINK_CUBE_QUEUE_SIZE * 12`.
*/
#define LINK_CUBE_QUEUE_SIZE 10
#endif
static volatile char LINK_CUBE_VERSION[] = "LinkCube/v7.0.1";
#define LINK_CUBE_BARRIER asm volatile("" ::: "memory")
/**
* @brief A JOYBUS handler for the Link Port.
*/
class LinkCube {
private:
using u32 = unsigned int;
using u16 = unsigned short;
using u8 = unsigned char;
using U32Queue = Link::Queue<u32, LINK_CUBE_QUEUE_SIZE>;
static constexpr int BIT_CMD_RESET = 0;
static constexpr int BIT_CMD_RECEIVE = 1;
static constexpr int BIT_CMD_SEND = 2;
static constexpr int BIT_IRQ = 6;
static constexpr int BIT_JOYBUS_HIGH = 14;
static constexpr int BIT_GENERAL_PURPOSE_LOW = 14;
static constexpr int BIT_GENERAL_PURPOSE_HIGH = 15;
public:
/**
* @brief Returns whether the library is active or not.
*/
[[nodiscard]] bool isActive() { return isEnabled; }
/**
* @brief Activates the library.
*/
void activate() {
LINK_CUBE_BARRIER;
isEnabled = false;
LINK_CUBE_BARRIER;
resetState();
stop();
LINK_CUBE_BARRIER;
isEnabled = true;
LINK_CUBE_BARRIER;
start();
}
/**
* @brief Deactivates the library.
*/
void deactivate() {
isEnabled = false;
resetState();
stop();
}
/**
* @brief Waits for data. Returns `true` on success, or `false` on
* JOYBUS reset.
*/
bool wait() {
return wait([]() { return false; });
}
/**
* @brief Waits for data. Returns `true` on success, or `false` on
* JOYBUS reset or cancellation.
* @param cancel A function that will be invoked after every SERIAL interrupt.
* If it returns `true`, the wait be aborted.
* \warning Blocks the system until the next SERIAL interrupt!
*/
template <typename F>
bool wait(F cancel) {
resetFlag = false;
while (!resetFlag && !canRead() && !cancel())
Link::_IntrWait(1, Link::_IRQ_SERIAL);
return canRead();
}
/**
* @brief Returns `true` if there are pending received values to read.
*/
[[nodiscard]] bool canRead() { return !incomingQueue.isEmpty(); }
/**
* @brief Dequeues and returns the next received value.
* \warning If there's no received data, a `0` will be returned.
*/
u32 read() { return incomingQueue.syncPop(); }
/**
* @brief Returns the next received value without dequeuing it.
* \warning If there's no received data, a `0` will be returned.
*/
[[nodiscard]] u32 peek() { return incomingQueue.peek(); }
/**
* @brief Sends 32-bit `data`.
* @param data The value to be sent.
* \warning If the other end asks for data at the same time you call this
* method, a `0x00000000` will be sent.
*/
void send(u32 data) { outgoingQueue.syncPush(data); }
/**
* @brief Returns the number of pending outgoing transfers.
*/
[[nodiscard]] u32 pendingCount() { return outgoingQueue.size(); }
/**
* @brief Returns whether a JOYBUS reset was requested or not. After this
* call, the reset flag is cleared if `clear` is `true` (default behavior).
* @param clear Whether it should clear the reset flag or not.
*/
bool didReset(bool clear = true) {
bool reset = resetFlag;
if (clear)
resetFlag = false;
return reset;
}
/**
* @brief This method is called by the SERIAL interrupt handler.
* \warning This is internal API!
*/
void _onSerial() {
if (!isEnabled)
return;
if (isBitHigh(BIT_CMD_RESET)) {
resetState();
resetFlag = true;
setBitHigh(BIT_CMD_RESET);
}
if (isBitHigh(BIT_CMD_RECEIVE)) {
newIncomingQueue.push(getData());
setBitHigh(BIT_CMD_RECEIVE);
}
if (isBitHigh(BIT_CMD_SEND)) {
setPendingData();
setBitHigh(BIT_CMD_SEND);
}
copyState();
}
private:
U32Queue newIncomingQueue;
U32Queue incomingQueue;
U32Queue outgoingQueue;
volatile bool resetFlag = false;
volatile bool needsClear = false;
volatile bool isEnabled = false;
void copyState() {
if (incomingQueue.isReading())
return;
if (needsClear) {
incomingQueue.clear();
needsClear = false;
}
while (!newIncomingQueue.isEmpty())
incomingQueue.push(newIncomingQueue.pop());
}
void resetState() {
needsClear = false;
newIncomingQueue.clear();
if (incomingQueue.isReading())
needsClear = true;
else
incomingQueue.clear();
outgoingQueue.syncClear();
resetFlag = false;
}
void setPendingData() {
setData(outgoingQueue.isWriting() ? 0 : outgoingQueue.pop());
}
void setData(u32 data) {
Link::_REG_JOY_TRANS_H = msB32(data);
Link::_REG_JOY_TRANS_L = lsB32(data);
}
u32 getData() {
return buildU32(Link::_REG_JOY_RECV_H, Link::_REG_JOY_RECV_L);
}
void stop() {
setInterruptsOff();
setGeneralPurposeMode();
}
void start() {
setJoybusMode();
setInterruptsOn();
}
void setJoybusMode() {
Link::_REG_RCNT = Link::_REG_RCNT | (1 << BIT_JOYBUS_HIGH) |
(1 << BIT_GENERAL_PURPOSE_HIGH);
}
void setGeneralPurposeMode() {
Link::_REG_RCNT = (Link::_REG_RCNT & ~(1 << BIT_GENERAL_PURPOSE_LOW)) |
(1 << BIT_GENERAL_PURPOSE_HIGH);
}
void setInterruptsOn() { setBitHigh(BIT_IRQ); }
void setInterruptsOff() { setBitLow(BIT_IRQ); }
u32 buildU32(u16 msB, u16 lsB) { return (msB << 16) | lsB; }
u16 msB32(u32 value) { return value >> 16; }
u16 lsB32(u32 value) { return value & 0xffff; }
bool isBitHigh(u8 bit) { return (Link::_REG_JOYCNT >> bit) & 1; }
void setBitHigh(u8 bit) { Link::_REG_JOYCNT |= 1 << bit; }
void setBitLow(u8 bit) { Link::_REG_JOYCNT &= ~(1 << bit); }
};
extern LinkCube* linkCube;
/**
* @brief SERIAL interrupt handler.
*/
inline void LINK_CUBE_ISR_SERIAL() {
linkCube->_onSerial();
}
#endif // LINK_CUBE_H