-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathGamecube.c
217 lines (189 loc) · 8.13 KB
/
Gamecube.c
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
/*
Copyright (c) 2014-2016 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 "Gamecube.h"
//================================================================================
// Gamecube Controller
//================================================================================
bool gc_init(const uint8_t pin, Gamecube_Status_t* status)
{
// Initialize the gamecube controller by sending it a null byte.
// This is unnecessary for a standard controller, but is required for the
// Wavebird.
uint8_t command[] = { 0x00 };
// Send the command and read in data
uint8_t receivedBytes = gc_n64_send_get(pin, command, sizeof(command), (uint8_t*)status, sizeof(Gamecube_Status_t));
// Return status information for optional use.
// On error the report may have been modified!
return (receivedBytes == sizeof(Gamecube_Status_t));
}
bool gc_origin(const uint8_t pin, Gamecube_Origin_t* origin)
{
// Get the initital controller values when it was plugged in.
// This is used for calibration of the controller.
// If you do not call this function the controller data bit "origin" will be set.
// This results in a not accepting Gamecube host/reinitialization.
uint8_t command[] = { 0x41 };
// Send the command and read in data
uint8_t receivedBytes = gc_n64_send_get(pin, command, sizeof(command), (uint8_t*)origin, sizeof(Gamecube_Origin_t));
// Return status information for optional use.
// On error the report may have been modified!
return (receivedBytes == sizeof(Gamecube_Origin_t));
}
bool gc_read(const uint8_t pin, Gamecube_Report_t* report, const bool rumble)
{
// Command to send to the gamecube, LSB is rumble
uint8_t command[] = { 0x40, 0x03, rumble };
// Send the command and read in data
uint8_t receivedBytes = gc_n64_send_get(pin, command, sizeof(command), (uint8_t*)report, sizeof(Gamecube_Report_t));
// Return status information for optional use.
// On error the report may have been modified!
return (receivedBytes == sizeof(Gamecube_Report_t));
}
//================================================================================
// Gamecube Console
//================================================================================
void gc_report_convert(Gamecube_Report_t* report, Gamecube_Different_Report_t* dif, uint8_t mode)
{
dif->a = report->a;
dif->b = report->b;
dif->x = report->x;
dif->y = report->y;
dif->start = report->start;
dif->dleft = report->dleft;
dif->dright = report->dright;
dif->ddown = report->ddown;
dif->dup = report->dup;
dif->z = report->z;
dif->r = report->r;
dif->l = report->l;
dif->xAxis = report->xAxis;
dif->yAxis = report->yAxis;
if (mode == 0 || mode == 5 || mode == 6 || mode == 7)
{
dif->Mode0.cxAxis = report->cxAxis;
dif->Mode0.cyAxis = report->cyAxis;
dif->Mode0.left = report->left >> 4;
dif->Mode0.right = report->right >> 4;
dif->Mode0.analogA = 0;
dif->Mode0.analogB = 0;
}
else if (mode == 1)
{
dif->Mode1.cxAxis = report->cxAxis >> 4;
dif->Mode1.cyAxis = report->cyAxis >> 4;
dif->Mode1.left = report->left;
dif->Mode1.right = report->right;
dif->Mode1.analogA = 0;
dif->Mode1.analogB = 0;
}
else if (mode == 2)
{
dif->Mode2.cxAxis = report->cxAxis >> 4;
dif->Mode2.cyAxis = report->cyAxis >> 4;
dif->Mode2.left = report->left >> 4;
dif->Mode2.right = report->right >> 4;
dif->Mode2.analogA = 0;
dif->Mode2.analogB = 0;
}
else if (mode == 4)
{
dif->Mode4.cxAxis = report->cxAxis;
dif->Mode4.cyAxis = report->cyAxis;
dif->Mode4.analogA = 0;
dif->Mode4.analogB = 0;
}
}
uint8_t gc_write(const uint8_t pin, Gamecube_Status_t* status, Gamecube_Origin_t* origin, Gamecube_Report_t* report)
{
// 0 = no input/error, 1 = init, 2 = origin, 3 = read, 4 = read with rumble
uint8_t ret = 0;
// Get the port mask and the pointers to the in/out/mode registers
uint8_t bitMask = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
volatile uint8_t* modePort = portModeRegister(port);
volatile uint8_t* outPort = portOutputRegister(port);
volatile uint8_t* inPort = portInputRegister(port);
// Don't want interrupts getting in the way
uint8_t oldSREG = SREG;
cli();
// Read in data from the console
// After receiving the init command you have max 80us to respond (for the data command)!
uint8_t command[3];
uint8_t receivedBytes = gc_n64_get(command, sizeof(command), modePort, outPort, inPort, bitMask);
// Init
if (receivedBytes == 1 && command[0] == 0x00)
{
gc_n64_send(status->raw8, sizeof(Gamecube_Status_t), modePort, outPort, bitMask);
ret = 1;
}
// Get origin
else if (receivedBytes == 1 && command[0] == 0x41)
{
gc_n64_send(origin->raw8, sizeof(Gamecube_Origin_t), modePort, outPort, bitMask);
ret = 2;
}
// Get data. Do not check last byte (command[2]), as the flags are unknown
else if (receivedBytes == 3 && command[0] == 0x40 && command[1] <= 0x07)
{
if (command[1] == 0x03)
{
gc_n64_send(report->raw8, sizeof(Gamecube_Report_t), modePort, outPort, bitMask);
ret = 3;
} else {
Gamecube_Different_Report_t dif;
gc_report_convert(report, &dif, command[1]);
gc_n64_send(dif.raw8, sizeof(Gamecube_Different_Report_t), modePort, outPort, bitMask);
ret = 3;
}
// The first byte probably flags a gamecube reading (0x40), as the same
// protocol is also used for N64. The lower nibble seems to be the mode:
// 0x40 (followed by 2 bytes) reading
// 0x41 get origin (1 byte)
// 0x42 (followed by 2 bytes) seems to force mode 0x02 below
// 0x43 (followed by 2 bytes) seems to force mode 0x02 below
// The 2nd byte (command[1]) seems to request a special data format.
// I've noticed formats that merge the L + R data.
// There seem to be only 4 data formats, the rest is ignore.
// 0x00 First 4 bytes: buttons0+1 + X + Y, C-Stick, L+R minimum of both, 0x00 fixed
// 0x01 First 4 bytes: buttons0+1 + X + Y, C-Stick Horizontal only, R, L, 0x00 fixed
// 0x02 First 4 bytes: buttons0+1 + X + Y, C-Stick Horizontal only, L+R minimum of both, 0x02 fixed, 0x01 fixed
// 0x03 Normal reading
// I've seen 3 last options for the last byte (command[2]):
// 0x00 Normal reading
// 0x01 Enable rumble
// 0x02 Normal reading, rumble was at least called once
// 0x03 ??? - never seen so far
// Rumble: 1, 5, 9, 13, 17, ...
// You can see that only 4 of those requests are possible,
// the gamecube will ignore the left 6 MSB.
if((command[2] % 4) == 0x01){
ret = 4;
}
else if((command[2] % 4) == 0x02){
ret = 5;
}
else if((command[2] % 4) == 0x03){
ret = 6;
}
}
// End of time sensitive code
SREG = oldSREG;
return ret;
}