-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN64_To_USB.ino
363 lines (313 loc) · 11.1 KB
/
N64_To_USB.ino
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
/**
* N64 To USB adapter
* by Michele Perla ([email protected])
*/
/**
* Gamecube controller to Nintendo 64 adapter
* by Andrew Brown
* Rewritten for N64 to HID by Peter Den Hartog
*/
/**
* To use, hook up the following to the Teensy 2.0
* Digital I/O 2: N64 serial line
* Ground and power lines
*/
//#include "pins_arduino.h"
#define N64_PIN 0
#define N64_PIN_DIR DDRB
// these two macros set arduino pin 2 to input or output, which with an
// external 1K pull-up resistor to the 3.3V rail, is like pulling it high or
// low. These operations translate to 1 op code, which takes 2 cycles
#define N64_HIGH DDRB &= ~0x01
#define N64_LOW DDRB |= 0x01
#define N64_QUERY (PINB & 0x01)
// 8 bytes of data that we get from the controller
struct state{
char stick_x;
char stick_y;
// bits: 0, 0, 0, start, y, x, b, a
unsigned char data1;
// bits: 1, L, R, Z, Dup, Ddown, Dright, Dleft
unsigned char data2;
} N64_status;
char N64_raw_dump[33]; // 1 received bit per byte
void setup()
{
Serial.begin(115200);
delay(500);
// Communication with gamecube controller on this pin
// Don't remove these lines, we don't want to push +5V to the controller
digitalWrite(N64_PIN, LOW);
pinMode(N64_PIN, INPUT);
}
void translate_raw_data()
{
// The get_N64_status 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 N64_status
memset(&N64_status, 0, sizeof(N64_status));
// line 1
// bits: A, B, Z, Start, Dup, Ddown, Dleft, Dright
// line 2
// bits: 0, 0, L, R, Cup, Cdown, Cleft, Cright
// line 3
// bits: joystick x value
// These are 8 bit values centered at 0x80 (128)
// line 4
// bits: joystick 4 value
// These are 8 bit values centered at 0x80 (128)
for (int i=0; i<8; i++) {
N64_status.data1 |= N64_raw_dump[i] ? (0x80 >> i) : 0;
N64_status.data2 |= N64_raw_dump[8+i] ? (0x80 >> i) : 0;
N64_status.stick_x |= N64_raw_dump[16+i] ? (0x80 >> i) : 0;
N64_status.stick_y |= N64_raw_dump[24+i] ? (0x80 >> i) : 0;
}
}
/**
* This sends the given byte sequence to the controller
* length must be at least 1
* Oh, it destroys the buffer passed in as it writes it
*/
void N64_send(unsigned char *buffer, char length)
{
// Send these bytes
char bits;
bool bit;
// 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
asm volatile (";Starting outer for loop");
outer_loop:
{
asm volatile (";Starting inner for loop");
bits=8;
inner_loop:
{
// Starting a bit, set the line low
asm volatile (";Setting line to low");
N64_LOW; // 1 op, 2 cycles
asm volatile (";branching");
if (*buffer >> 7) {
asm volatile (";Bit is a 1");
// 1 bit
// remain low for 1us, then go high for 3us
// nop block 1
asm volatile ("nop\nnop\nnop\nnop\nnop\n");
asm volatile (";Setting line to high");
N64_HIGH;
// 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 {
asm volatile (";Bit is a 0");
// 0 bit
// 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\nnop\nnop\nnop\n"
"nop\n");
asm volatile (";Setting line to high");
N64_HIGH;
// wait for 1us
asm volatile ("; 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
asm volatile (";finishing inner loop body");
--bits;
if (bits != 0) {
// nop block 4
// this block is why a for loop was impossible
asm volatile ("nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\n");
// rotate bits
asm volatile (";rotating out bits");
*buffer <<= 1;
goto inner_loop;
} // fall out of inner loop
}
asm volatile (";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!)
--length;
if (length != 0) {
++buffer;
goto outer_loop;
} // fall out of outer loop
}
// send a single stop (1) bit
// nop block 5
asm volatile ("nop\nnop\nnop\nnop\n");
N64_LOW;
// wait 1 us, 16 cycles, then raise the line
// 16-2=14
// nop block 6
asm volatile ("nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\n");
N64_HIGH;
}
void N64_get()
{
// listen for the expected 8 bytes of data back from the controller and
// blast it out to the N64_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 N64_status struct.
asm volatile (";Starting to listen");
unsigned char timeout;
char bitcount = 32;
char *bitbin = N64_raw_dump;
// Again, using gotos here to make the assembly more predictable and
// optimization easier (please don't kill me)
read_loop:
timeout = 0x3f;
// wait for line to go low
while (N64_QUERY) {
if (!--timeout)
return;
}
// wait approx 2us 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\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
);
*bitbin = N64_QUERY;
++bitbin;
--bitcount;
if (bitcount == 0)
return;
// wait for line to go high again
// it may already be high, so this should just drop through
timeout = 0x3f;
while (!N64_QUERY) {
if (!--timeout)
return;
}
goto read_loop;
}
void print_N64_status()
{
// bits: A, B, Z, Start, Dup, Ddown, Dleft, Dright
// bits: 0, 0, L, R, Cup, Cdown, Cleft, Cright
Serial.println();
Serial.print("Start: ");
Serial.println(N64_status.data1 & 16 ? 1:0);
Serial.print("Z: ");
Serial.println(N64_status.data1 & 32 ? 1:0);
Serial.print("B: ");
Serial.println(N64_status.data1 & 64 ? 1:0);
Serial.print("A: ");
Serial.println(N64_status.data1 & 128 ? 1:0);
Serial.print("L: ");
Serial.println(N64_status.data2 & 32 ? 1:0);
Serial.print("R: ");
Serial.println(N64_status.data2 & 16 ? 1:0);
Serial.print("Cup: ");
Serial.println(N64_status.data2 & 0x08 ? 1:0);
Serial.print("Cdown: ");
Serial.println(N64_status.data2 & 0x04 ? 1:0);
Serial.print("Cright:");
Serial.println(N64_status.data2 & 0x01 ? 1:0);
Serial.print("Cleft: ");
Serial.println(N64_status.data2 & 0x02 ? 1:0);
Serial.print("Dup: ");
Serial.println(N64_status.data1 & 0x08 ? 1:0);
Serial.print("Ddown: ");
Serial.println(N64_status.data1 & 0x04 ? 1:0);
Serial.print("Dright:");
Serial.println(N64_status.data1 & 0x01 ? 1:0);
Serial.print("Dleft: ");
Serial.println(N64_status.data1 & 0x02 ? 1:0);
Serial.print("Stick X:");
Serial.println(N64_status.stick_x+128, DEC);
Serial.print("Stick X remapped:");
Serial.println(map(N64_status.stick_x+128,46,210,0,1023), DEC);
Serial.print("Stick Y:");
Serial.println(N64_status.stick_y+128, DEC);
Serial.print("Stick Y remapped:");
Serial.println(map(N64_status.stick_y+128,46,210,0,1023), DEC);
}
void send_N64_status()
{
// make usb transmission faster by bundling packets
// one packet sent at end of function
Joystick.useManualSend(true);
// bits: A, B, Z, Start, Dup, Ddown, Dleft, Dright
// bits: 0, 0, L, R, Cup, Cdown, Cleft, Cright
//start
Joystick.button(1, N64_status.data1 & 16 ? 1:0);
//z
Joystick.button(2, N64_status.data1 & 32 ? 1:0);
//b
Joystick.button(3, N64_status.data1 & 64 ? 1:0);
//a
Joystick.button(4, N64_status.data1 & 128 ? 1:0);
//l
Joystick.button(5, N64_status.data2 & 32 ? 1:0);
//r
Joystick.button(6, N64_status.data2 & 16 ? 1:0);
//cup
Joystick.button(7, N64_status.data2 & 0x08 ? 1:0);
//cdown
Joystick.button(8, N64_status.data2 & 0x04 ? 1:0);
//cright
Joystick.button(9, N64_status.data2 & 0x01 ? 1:0);
//cleft
Joystick.button(10, N64_status.data2 & 0x02 ? 1:0);
//dup
Joystick.button(11, N64_status.data1 & 0x08 ? 1:0);
//ddown
Joystick.button(12, N64_status.data1 & 0x04 ? 1:0);
//dright
Joystick.button(13, N64_status.data1 & 0x01 ? 1:0);
//dleft
Joystick.button(14, N64_status.data1 & 0x02 ? 1:0);
//x-axis
Joystick.X(map(N64_status.stick_x+128,46,210,0,1023));
//y-axis
Joystick.Y(map(N64_status.stick_y+128,46,210,0,1023));
Joystick.send_now();
}
void loop()
{
// Command to send to the gamecube
// The last bit is rumble, flip it to rumble
// yes this does need to be inside the loop, the
// array gets mutilated when it goes through N64_send
unsigned char command[] = {0x01};
// don't want interrupts getting in the way
noInterrupts();
// send those 3 bytes
N64_send(command, 1);
// read in data and dump it to N64_raw_dump
N64_get();
interrupts();
translate_raw_data();
//print_N64_status();
send_N64_status();
delay(10);
}