-
Notifications
You must be signed in to change notification settings - Fork 4
/
DogeBawx.h
329 lines (300 loc) · 6.51 KB
/
DogeBawx.h
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#pragma once
#include "bawxTypeMacros.h"
#include "bawxBackendMacros.h"
#include "bawxButtonDefines.h"
union Buttons{
u8 arr[10];
struct {
// byte 0
u8 A : 1;
u8 B : 1;
u8 X : 1;
u8 Y : 1;
u8 S : 1;
u8 orig : 1;
u8 errL : 1;
u8 errS : 1;
// byte 1
u8 Dl : 1;
u8 Dr : 1;
u8 Dd : 1;
u8 Du : 1;
u8 Z : 1;
u8 R : 1;
u8 L : 1;
u8 high : 1;
//byte 2-7
u8 Ax : 8;
u8 Ay : 8;
u8 Cx : 8;
u8 Cy : 8;
u8 La : 8;
u8 Ra : 8;
// magic byte 8 & 9 (only used in origin cmd)
// have something to do with rumble motor status???
// ignore these, they are magic numbers needed
// to make a cmd response work
u8 magic1 : 8;
u8 magic2 : 8;
};
}btn;
union ID {
u8 arr3[3] = { 0x09,0x00,0x00 };
struct {
u8 byt1 : 8;
u8 byt2 : 8;
u8 byt3 : 8;
};
}id;
struct CMD {
u8 command : 8;
u8 readmode : 8;
u8 rumbleInfo : 8;
}cmd;
class DogeBawx {
public:
static inline void init()
{
cli();
initCounter();
initBtns();
initLines();
align();
}
static inline void readWrite()
{
readBtns();
readCmd();
write();
}
private:
// sets up cpu cycle counter register
static inline void initCounter()
{
ARM_DEMCR |= ARM_DEMCR_TRCENA;
ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA;
}
static inline void initGroup(u8 group)
{
for (uint8_t i = 0; i < 5; i++) {
pinMode((i+group), INPUT_PULLUP);
}
}
static inline void initBtns()
{
initGroup(GL);
initGroup(GM);
initGroup(GR);
btn.high = 0b1u;
btn.orig = 0b0u;
btn.errL = 0b0u;
btn.errS = 0b0u;
btn.Ax = 0x80u;
btn.Ay = 0x80u;
btn.Cx = 0x80u;
btn.Cy = 0x80u;
btn.magic1 = 0x02u;
btn.magic2 = 0x02u;
pinMode(HP, INPUT_PULLUP);
}
// sets pin modes for gamecube controller data line
// and screen rx/tx
static inline void initLines()
{
pinMode(LINE, INPUT);
//pinMode(STX, OUTPUT);
//pinMode(SRX, INPUT);
}
// reads and returns one bit from LINE
// (assumes LINE is already INPUT)
static inline bool readBit()
{
while (digitalReadFast(LINE)) {
// start of bit starts on falling edge
}
markCycle();// record CPU cycle of falling edge
while (!digitalReadFast(LINE)) {
// bit value is determined by how long LINE is held LOW by the console
}
// 1 if LINE was LOW for < halfBit
// 0 if LINE was LOW for > halfBit
return elapsedCycles() < _halfBit;
}
// writes one bit to LINE
// (assumes LINE is already OUTPUT)
static inline void writeBit(bool bt /* bit to be written */)
{
markCycle();// record CPU cycle of the start of the bit
if (bt) {
digitalWriteFast(LINE, LOW);
while (elapsedCycles() < _quarterBit) {
// hold LINE LOW for 1/4 (1us) of the bit
}
digitalWriteFast(LINE, HIGH);
while (elapsedCycles() < _oneBit) {
// hold LINE HIGH for the rest of the bit
}
return;
}
else {
digitalWriteFast(LINE, LOW);
while (elapsedCycles() < _threeqBit) {
// hold LINE LOW for 3/4 (3us) of the bit
}
digitalWriteFast(LINE, HIGH);
while (elapsedCycles() < _oneBit) {
// hold LINE HIGH for the rest of the bit
}
return;
}
}
// writes one controller stop bit to LINE
// (assumes LINE is already OUTPUT)
static inline void writeStop() {
markCycle();
digitalWriteFast(LINE, LOW);
while (elapsedCycles() < _halfBit) {
// hold LINE LOW for 1/2 (2us) of the bit
}
digitalWriteFast(LINE, HIGH);
while (elapsedCycles() < _halfBit) {
// hold LINE HIGH for 1/2 (2us) of the bit
}
return;
}
// writes one byte to LINE (MSB order)
// (assumes LINE is already OUTPUT)
static inline void writeByte(u8 byt /* byte to be written */)
{
for (uint8_t i = 0; i < 8; i++) {
writeBit(byt & 0x80u);
byt <<= 1;
}
}
// waits for almost 2 init cmds + stop bits to ensure
// the next command is aligned with the first command read
static inline void align()
{
u16 buffer=0xffff;
delay(100);
while (buffer!=0b0000001000000001) {
buffer = (buffer << 1) | readBit();
}
}
public:
// attempts to read a console command from LINE
// one cmd will either be 8bits or 24bits (not including stop bit)
// returns the first byte of the command
// stores entire command in the cmd struct
// sets LINE to pinmode INPUT
static inline void readCmd()
{
pinMode(LINE, INPUT);
for (uint8_t i = 0; i < 8; i++) {
cmd.command = (cmd.command << 1) | readBit();
}
if (cmd.command==0x41||cmd.command==0x00||cmd.command == 0xff) { // stop bit
readBit(); // stop bit
return;
}
else {
for (uint8_t i = 0; i < 8; i++) {
cmd.readmode = (cmd.readmode << 1) | readBit();
}
for (uint8_t i = 0; i < 8; i++) {
cmd.rumbleInfo = (cmd.rumbleInfo << 1) | readBit();
}
readBit(); // stop bit
return;
}
}
// writes a response to LINE based on the contents of cmd
static inline void write()
{
pinMode(LINE, OUTPUT);
// read cmd
if (cmd.command == 0x40) {
for (uint8_t i = 0; i < 8; i++) {
writeByte(btn.arr[i]);
}
writeStop();
}
// origin cmd
else if (cmd.command == 0x41) {
btn.orig = 0b0u;
for (uint8_t i = 0; i < 10; i++) {
writeByte(btn.arr[i]);
}
writeStop();
}
// init / reset cmd
else if (cmd.command == 0x00 || cmd.command == 0xff) {
for (uint8_t i = 0; i < 3; i++) {
writeByte(id.arr3[i]);
}
writeStop();
}
pinMode(LINE, INPUT);
}
// reads button state and stores in btn struct/array
// assumes all button pinmodes are INPUT_PULLUP
static inline void readBtns()
{
// digital buttons
btn.A = readBtn(btn_A);
btn.B = readBtn(btn_B);
btn.X = readBtn(btn_X);
btn.Y = readBtn(btn_Y);
btn.L = readBtn(btn_L);
btn.R = readBtn(btn_R);
btn.Z = readBtn(btn_Z);
btn.S = readBtn(btn_S);
btn.Du = readBtn(btn_Du);
btn.Dd = readBtn(btn_Dd);
btn.Dl = readBtn(btn_Dl);
btn.Dr = readBtn(btn_Dr);
// trigger light press DAC
btn.La = readBtn(btn_La) ? 0x80u : 0x00u;
btn.Ra = readBtn(btn_Ra) ? 0x80u : 0x00u;
// Analog stick X axis
if (readBtn(btn_Al)) {
btn.Ax = readBtn(btn_Ar) ? 0x80u : 0x00u;
}
else if (readBtn(btn_Ar)) {
btn.Ax = 0xffu;
}
else {
btn.Ax = 0x80u;
}
// Analog stick Y axis
if (readBtn(btn_Ad)) {
btn.Ay = readBtn(btn_Au) ? 0x80u : 0x00u;
}
else if (readBtn(btn_Au)) {
btn.Ay = 0xffu;
}
else {
btn.Ay = 0x80u;
}
// C stick X axis
if (readBtn(btn_Cl)) {
btn.Cx = readBtn(btn_Cr) ? 0x80u : 0x00u;
}
else if (readBtn(btn_Cr)) {
btn.Cx = 0xffu;
}
else {
btn.Cx = 0x80u;
}
// C stick Y axis
if (readBtn(btn_Cd)) {
btn.Cy = readBtn(btn_Cu) ? 0x80u : 0x00u;
}
else if (readBtn(btn_Cu)) {
btn.Cy = 0xffu;
}
else {
btn.Cy = 0x80u;
}
}
};