-
Notifications
You must be signed in to change notification settings - Fork 6
/
Nintendo.cpp
executable file
·569 lines (466 loc) · 16.9 KB
/
Nintendo.cpp
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
Copyright (c) 2014 NicoHood
See the readme for credit to other people.
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.
*/
#include "Nintendo.h"
//================================================================================
// Gamecube Init
//================================================================================
Gamecube_ Gamecube;
Gamecube_::Gamecube_(void){
// prevents sendget from being called without calling begin()
_bitMask = 0;
// clears the reports
memset(&status, 0x00, sizeof(status));
memset(&report, 0x00, sizeof(report));
}
bool Gamecube_::begin(uint8_t pin){
// Get the port mask and the pointers to the in/out/mode registers
_bitMask = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
_outPort = portOutputRegister(port);
_inPort = portInputRegister(port);
_modePort = portModeRegister(port);
// Initialize the gamecube controller by sending it a null byte.
// This is unnecessary for a standard controller, but is required for the
// Wavebird.
// return status information for optional use
return init();
}
bool Gamecube_::init(void){
// only read values if begin() was called before
if (!_bitMask) return false;
// Initialize the gamecube controller by sending it a null byte.
// This is unnecessary for a standard controller, but is required for the
// Wavebird.
// 1 received bit per byte raw input dump
// for fast measurement
// the dump is used for sending and getting new information in the same array
//uint8_t raw_dump[sizeof(status)* 8] = { 0x00 };
uint8_t raw_dump[sizeof(status)* 8];
uint8_t command[] = { 0x00 };
// don't want interrupts getting in the way
noInterrupts();
// send those byte, read data and dump it into raw_dump
//bool newinput = sendget(raw_dump, 1, sizeof(raw_dump), _modePort, _outPort, _inPort, _bitMask);
// send the command
send(command, sizeof(command), _modePort, _outPort, _bitMask);
// read in data and dump it to raw_dump
bool newinput = get(raw_dump, sizeof(raw_dump), _modePort, _outPort, _inPort, _bitMask);
// end of time sensitive code
interrupts();
// only translate the data if the input was valid
if (newinput){
// translate the data in raw_dump to something useful
translate_raw_data(raw_dump, &status, sizeof(status));
// switch the first two bytes to compare it easy with the documentation
uint8_t temp = status.whole8[0];
status.whole8[0] = status.whole8[1];
status.whole8[1] = temp;
}
// return status information for optional use
return newinput;
}
void Gamecube_::end(void){
// only read values if begin() was called before
if (!_bitMask) return;
// Turns off rumble by sending a normal reading request
// and discards the information
// 1 received bit per byte raw input dump
// for fast measurement
// the dump is used for sending and getting new information in the same array
//uint8_t raw_dump[sizeof(report)* 8] = { 0x40, 0x03, 0x00 };
uint8_t command[] = { 0x40, 0x03, 0x00 };
// don't want interrupts getting in the way
noInterrupts();
// send the command
send(command, sizeof(command), _modePort, _outPort, _bitMask);
// end of time sensitive code
interrupts();
// Stupid routine to wait for the gamecube controller to stop
// sending its response. We don't care what it is, but we
// can't start asking for status if it's still responding
for (int i = 0; i < 10; i++) {
// make sure the line is idle for 10 iterations, should
// be plenty. (about 7uS)
if (!(*_inPort & _bitMask))
i = 0;
}
// reset that you need to call begin() again first
_bitMask = 0;
}
//================================================================================
// Gamecube Read
//================================================================================
bool Gamecube_::read(bool rumble){
// only read values if begin() was called before
if (!_bitMask) return false;
// Command to send to the gamecube
// The last bit is rumble
// 1 received bit per byte raw input dump
// for fast measurement
// the dump is used for sending and getting new information in the same array
//uint8_t raw_dump[sizeof(report)* 8] = { 0x40, 0x03, rumble & 0x01 };
uint8_t raw_dump[sizeof(report)* 8];
uint8_t command[] = { 0x40, 0x03, rumble & 0x01 };
// don't want interrupts getting in the way
noInterrupts();
// send those bytes, read data and dump it into raw_dump
//bool newinput = sendget(raw_dump, 3, sizeof(raw_dump), _modePort, _outPort, _inPort, _bitMask);
// send the command
send(command, sizeof(command), _modePort, _outPort, _bitMask);
// read in data and dump it to raw_dump
bool newinput = get(raw_dump, sizeof(raw_dump), _modePort, _outPort, _inPort, _bitMask);
// end of time sensitive code
interrupts();
// only translate the data if the input was valid
if (newinput){
// translate the data in raw_dump to something useful
translate_raw_data(raw_dump, &report, sizeof(report));
}
// return status information for optional use
return newinput;
}
void Gamecube_::translate_raw_data(uint8_t raw_dumb[], void* data, uint8_t length){
// The get function sloppily dumps its data 1 bit per byte
// into the get_status_extended char array. It's our job to go through
// that and put each piece neatly into the struct
uint8_t* report = (uint8_t*)data;
memset(report, 0, length);
for (int i = 0; i < length * 8; i++){
report[i / 8] |= raw_dumb[i] ? (0x80 >> (i % 8)) : 0;
//raw_dump[i] ? data[i/8] |= (0x80 >> (i%8)) : data[i/8] &= ~(0x80 >> (i%8));
}
}
//================================================================================
// Gamecube i/o functions
//================================================================================
//================================================================================
// Send
//================================================================================
/**
* This sends the given byte sequence to the controller
* length must be at least 1
* Oh, it destroys the buffer passed in as it sends it
* it fills the buff with the answer from the controller
*/
void Gamecube_::send(uint8_t *buffer, uint8_t length,
volatile uint8_t* modePort, volatile uint8_t* outPort, uint8_t bitMask){
// Prepare pin for output and pull it high
*outPort |= bitMask; //HIGH
*modePort |= bitMask; //OUT
*outPort |= bitMask; //HIGH
// This routine is very carefully timed by examining the assembly output.
// Do not change any statements, it could throw the timings off
//
// We get 16 cycles per microsecond, which should be plenty, but we need to
// be conservative. Most assembly ops take 1 cycle, but a few take 2
//
// I use manually constructed for-loops out of gotos so I have more control
// over the outputted assembly. I can insert nops where it was impossible
// with a for loop
// Starting outer for loop
outer_loop:
{
// Starting inner for loop
// Send these bytes
uint8_t bits = 8;
inner_loop:
{
// Starting a bit, set the line low
*outPort &= ~bitMask; //LOW, 5 cycles
// branching
if (*buffer >> 7) { //8 cycles to enter
// Bit is a 1, 2 cycles
// remain low for 1us, then go high for 3us
// nop block 1
asm volatile ("nop\n");
// Setting line to high
*outPort |= bitMask; //HIGH, 5 cycles
// nop block 2
// we'll wait only 2us to sync up with both conditions
// at the bottom of the if statement
asm volatile ("nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
);
}
else {
// Bit is a 0, 3 cycles to enter
// remain low for 3us, then go high for 1us
// nop block 3
asm volatile ("nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\n");
// Setting line to high
*outPort |= bitMask; //HIGH, 5 cycles
// wait for 1us
// end of conditional branch, need to wait 1us more before next bit
}
// end of the if, the line is high and needs to remain
// high for exactly 16 more cycles, regardless of the previous
// branch path
// finishing inner loop body
--bits;
if (bits != 0) {
// nop block 4
// this block is why a for loop was impossible
asm volatile ("nop\nnop\n");
// rotating out bits
*buffer <<= 1;
goto inner_loop;
} // fall out of inner loop
}
// continuing outer loop
// In this case: the inner loop exits and the outer loop iterates,
// there are /exactly/ 16 cycles taken up by the necessary operations.
// So no nops are needed here (that was lucky!)
// Due to the modification the 2 unnecessary cycles somehow disappeared
// and this information might be wrong
--length;
if (length != 0) {
++buffer;
// nop block 5 (added)
//asm volatile ("nop\n");
goto outer_loop;
} // fall out of outer loop
}
// send a single stop (1) bit
// nop block 6
asm volatile ("nop\nnop\nnop\nnop\nnop\n");
*outPort &= ~bitMask; //LOW, 5 cycles
// wait 1 us, 16 cycles, then raise the line
// nop block 7
asm volatile ("nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\n");
*outPort |= bitMask; //HIGH, 5 cycles
}
//================================================================================
// Get
//================================================================================
bool Gamecube_::get(uint8_t *buffer, uint8_t length,
volatile uint8_t* modePort, volatile uint8_t* outPort, volatile uint8_t * inPort, uint8_t bitMask){
// Prepare pin for input with pullup
*modePort &= ~bitMask; //IN
*outPort |= bitMask; //HIGH, 5 cycles
// listen for the expected 8 bytes of data back from the controller and
// blast it out to the raw_dump array, one bit per byte for extra speed.
// Afterwards, call translate_raw_data() to interpret the raw data and pack
// it into the struct.
// Starting to listen
uint8_t timeout;
// Again, using gotos here to make the assembly more predictable and
// optimization easier (please don't kill me)
get_loop:
// the Port thing takes way longer, so timeout is 5 loops
// this means after 4uS it will timeout
timeout = 0x05;
// wait for line to go low
while (*inPort & bitMask) {
timeout--;
if (!timeout)
return false;
}
// wait approx 1,5us and poll the line
asm volatile (
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\n"
);
*buffer = *inPort & bitMask;
++buffer;
--length;
if (length == 0)
return true;
// wait for line to go high again 1,5uS
asm volatile (
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\n"
);
goto get_loop;
}
/**
* This sends the given byte sequence to the controller
* length must be at least 1
* Oh, it destroys the buffer passed in as it sends it
* it fills the buff with the answer from the controller
*/
bool Gamecube_::sendget(uint8_t *buffer, uint8_t sendlength, uint8_t getlength,
volatile uint8_t* modePort, volatile uint8_t* outPort, volatile uint8_t * inPort, uint8_t bitMask){
// only read values if begin() was called before
if (!bitMask) return false;
//================================================================================
// Send
//================================================================================
// We use this buffer for sending and getting the values. We need to copy the
// beginning of the buffer to use it with get after.
// Send + get in one function for more speed + accuracy
uint8_t *sendbuffer = buffer;
// Prepare pin for output and pull it high
*outPort |= bitMask; //HIGH
*modePort |= bitMask; //OUT
*outPort |= bitMask; //HIGH
// This routine is very carefully timed by examining the assembly output.
// Do not change any statements, it could throw the timings off
//
// We get 16 cycles per microsecond, which should be plenty, but we need to
// be conservative. Most assembly ops take 1 cycle, but a few take 2
//
// I use manually constructed for-loops out of gotos so I have more control
// over the outputted assembly. I can insert nops where it was impossible
// with a for loop
// Starting outer for loop
outer_loop:
{
// Starting inner for loop
// Send these bytes
uint8_t bits = 8;
inner_loop:
{
// Starting a bit, set the line low
*outPort &= ~bitMask; //LOW, 5 cycles
// branching
if (*sendbuffer >> 7) { //8 cycles to enter
// Bit is a 1, 2 cycles
// remain low for 1us, then go high for 3us
// nop block 1
asm volatile ("nop\n");
// Setting line to high
*outPort |= bitMask; //HIGH, 5 cycles
// nop block 2
// we'll wait only 2us to sync up with both conditions
// at the bottom of the if statement
asm volatile ("nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
);
}
else {
// Bit is a 0, 3 cycles to enter
// remain low for 3us, then go high for 1us
// nop block 3
asm volatile ("nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\n");
// Setting line to high
*outPort |= bitMask; //HIGH, 5 cycles
// wait for 1us
// end of conditional branch, need to wait 1us more before next bit
}
// end of the if, the line is high and needs to remain
// high for exactly 16 more cycles, regardless of the previous
// branch path
// finishing inner loop body
--bits;
if (bits != 0) {
// nop block 4
// this block is why a for loop was impossible
asm volatile ("nop\nnop\n");
// rotating out bits
*sendbuffer <<= 1;
goto inner_loop;
} // fall out of inner loop
}
// continuing outer loop
// In this case: the inner loop exits and the outer loop iterates,
// there are /exactly/ 16 cycles taken up by the necessary operations.
// So no nops are needed here (that was lucky!)
// Due to the modification the 2 unnecessary cycles somehow disappeared
// and this information might be wrong
--sendlength;
if (sendlength != 0) {
++sendbuffer;
// nop block 5 (added)
//asm volatile ("nop\n");
goto outer_loop;
} // fall out of outer loop
}
// send a single stop (1) bit
// nop block 6
asm volatile ("nop\nnop\nnop\nnop\nnop\n");
*outPort &= ~bitMask; //LOW, 5 cycles
// wait 1 us, 16 cycles, then raise the line
// nop block 7
asm volatile ("nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\n");
*outPort |= bitMask; //HIGH, 5 cycles
//================================================================================
// Get
//================================================================================
// Prepare pin for input with pullup
*modePort &= ~bitMask; //IN
*outPort |= bitMask; //HIGH, 5 cycles
// listen for the expected 8 bytes of data back from the controller and
// blast it out to the raw_dump array, one bit per byte for extra speed.
// Afterwards, call translate_raw_data() to interpret the raw data and pack
// it into the struct.
// Starting to listen
uint8_t timeout;
uint8_t *getbuffer = buffer;
// Again, using gotos here to make the assembly more predictable and
// optimization easier (please don't kill me)
get_loop:
// the Port thing takes way longer, so timeout is 5
// this means after 4uS it will timeout
timeout = 0x05;
// wait for line to go low
while (*inPort & bitMask) {
timeout--;
if (!timeout)
return false;
}
// wait approx 1,5us and poll the line
asm volatile (
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\n"
);
*getbuffer = *inPort & bitMask;
++getbuffer;
--getlength;
if (getlength == 0)
return true;
// wait for line to go high again 1,5uS
asm volatile (
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\n"
);
goto get_loop;
}