-
Notifications
You must be signed in to change notification settings - Fork 3
/
saturn.c
246 lines (189 loc) · 6.28 KB
/
saturn.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
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
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "report.h"
#include "saturn.h"
/* Pin 2 - B5 - D1 (Down)
Pin 3 - B4 - D0 (Up)
Pin 4 - B3 - D6 (TH)
Pin 5 - B2 - D5 (TR)
Pin 6 - B1 - D4 (TL)
Pin 7 - D4 - D3 (Right)
Pin 8 - D0 - D2 (Left)
The analogue pad sets the trigger down bit even in analogue mode. Kept this behaviour for
compatibility with emulators.
*/
extern uchar hat_lut[];
void ReadSaturn(report_t *reportBuffer)
{
uchar temp = 0;
uchar satmode = 0;
DDRB &= ~(SAT_UP|SAT_DN|SAT_TL); // UP/DN/TL as input
DDRB |= (SAT_TH|SAT_TR); // TH/TR as outputs
PORTB |= (SAT_UP|SAT_DN|SAT_TL|SAT_TR|SAT_TH); // Pull-ups, TR/TH high
DDRD &= ~(SAT_RT|SAT_LF); // inputs
PORTD |= (SAT_RT|SAT_LF); // pull-ups
_delay_us(2);
// TH = 1, TR = 1
if (PIND & SAT_LF) // Standard digital pad
{
if (!(PIND & SAT_RT)) reportBuffer->b1 |= (1<<6); // L
// TH = 0, TR = 1
PORTB &= ~(SAT_TH);
_delay_us(2);
if (!(PINB & SAT_UP)) reportBuffer->y = -127; // Up
if (!(PINB & SAT_DN)) reportBuffer->y = 127; // Down
if (!(PIND & SAT_LF)) reportBuffer->x = -127; // Left
if (!(PIND & SAT_RT)) reportBuffer->x = 127; // Right
// TH = 1, TR = 0
PORTB = (PORTB & ~(SAT_TR)) | (SAT_TH);
_delay_us(2);
if (!(PINB & SAT_UP)) reportBuffer->b1 |= (1<<1); // B
if (!(PINB & SAT_DN)) reportBuffer->b1 |= (1<<2); // C
if (!(PIND & SAT_LF)) reportBuffer->b1 |= (1<<0); // A
if (!(PIND & SAT_RT)) reportBuffer->b2 |= (1<<0); // Start
// TH = 0, TR = 0
PORTB &= ~(SAT_TH|SAT_TR);
_delay_us(2);
if (!(PINB & SAT_UP)) reportBuffer->b1 |= (1<<5); // Z
if (!(PINB & SAT_DN)) reportBuffer->b1 |= (1<<4); // Y
if (!(PIND & SAT_LF)) reportBuffer->b1 |= (1<<3); // X
if (!(PIND & SAT_RT)) reportBuffer->b1 |= (1<<7); // R
}
else if (PINB & SAT_UP) // Analogue pad
{
PORTB &= ~(SAT_TH); // 2nd
PORTB &= ~(SAT_TR);
wait_tl_low();
// 3rd
if (PINB & SAT_UP) satmode = 1; // 1 = analogue mode
PORTB |= (SAT_TR);
wait_tl_high();
// 4th
PORTB &= ~(SAT_TR);
wait_tl_low();
// 5th
if (satmode == 0) // digital mode
{
if (!(PINB & SAT_UP)) reportBuffer->y = -127; // Up
if (!(PINB & SAT_DN)) reportBuffer->y = 127; // Down
if (!(PIND & SAT_LF)) reportBuffer->x = -127; // Left
if (!(PIND & SAT_RT)) reportBuffer->x = 127; // Right
}
else
{
if (!(PINB & SAT_UP)) temp |= (1<<0); // Up
if (!(PINB & SAT_DN)) temp |= (1<<1); // Down
if (!(PIND & SAT_LF)) temp |= (1<<2); // Left
if (!(PIND & SAT_RT)) temp |= (1<<3); // Right
reportBuffer->hat = pgm_read_byte(&hat_lut[temp]);
}
PORTB |= (SAT_TR);
wait_tl_high();
// 6th
if (!(PINB & SAT_UP)) reportBuffer->b1 |= (1<<1); // B
if (!(PINB & SAT_DN)) reportBuffer->b1 |= (1<<2); // C
if (!(PIND & SAT_LF)) reportBuffer->b1 |= (1<<0); // A
if (!(PIND & SAT_RT)) reportBuffer->b2 |= (1<<3); // Start
PORTB &= ~(SAT_TR);
wait_tl_low();
// 7th
if (!(PINB & SAT_UP)) reportBuffer->b1 |= (1<<5); // Z
if (!(PINB & SAT_DN)) reportBuffer->b1 |= (1<<4); // Y
if (!(PIND & SAT_LF)) reportBuffer->b1 |= (1<<3); // X
if (!(PIND & SAT_RT)) reportBuffer->b1 |= (1<<7); // R
PORTB |= (SAT_TR);
wait_tl_high();
// 8th
if (!(PIND & SAT_RT)) reportBuffer->b1 |= (1<<6); // L
/* --- Analogue X axis --------------------------------------------------------------*/
PORTB &= ~(SAT_TR);
wait_tl_low();
// 9th
temp = 0;
if (!(PINB & SAT_UP)) temp |= (1<<4); // Up
if (!(PINB & SAT_DN)) temp |= (1<<5); // Down
if (!(PIND & SAT_LF)) temp |= (1<<6); // Left
if (!(PIND & SAT_RT)) temp |= (1<<7); // Right
PORTB |= (SAT_TR);
wait_tl_high();
// 10th
if (!(PINB & SAT_UP)) temp |= (1<<0); // Up
if (!(PINB & SAT_DN)) temp |= (1<<1); // Down
if (!(PIND & SAT_LF)) temp |= (1<<2); // Left
if (!(PIND & SAT_RT)) temp |= (1<<3); // Right
if (satmode == 1) reportBuffer->x = 255 - (temp + 0x80);
/* --- Analogue y axis --------------------------------------------------------------*/
PORTB &= ~(SAT_TR);
wait_tl_low();
// 11th
temp = 0;
if (!(PINB & SAT_UP)) temp |= (1<<4); // Up
if (!(PINB & SAT_DN)) temp |= (1<<5); // Down
if (!(PIND & SAT_LF)) temp |= (1<<6); // Left
if (!(PIND & SAT_RT)) temp |= (1<<7); // Right
PORTB |= (SAT_TR);
wait_tl_high();
// 12th
if (!(PINB & SAT_UP)) temp |= (1<<0); // Up
if (!(PINB & SAT_DN)) temp |= (1<<1); // Down
if (!(PIND & SAT_LF)) temp |= (1<<2); // Left
if (!(PIND & SAT_RT)) temp |= (1<<3); // Right
if (satmode == 1) reportBuffer->y = 255 - (temp + 0x80);
/* --- Right shoulder ---------------------------------------------------------------*/
PORTB &= ~(SAT_TR);
wait_tl_low();
// 11th
temp = 0;
if (!(PINB & SAT_UP)) temp |= (1<<4); // Up
if (!(PINB & SAT_DN)) temp |= (1<<5); // Down
if (!(PIND & SAT_LF)) temp |= (1<<6); // Left
if (!(PIND & SAT_RT)) temp |= (1<<7); // Right
PORTB |= (SAT_TR);
wait_tl_high();
// 12th
if (!(PINB & SAT_UP)) temp |= (1<<0); // Up
if (!(PINB & SAT_DN)) temp |= (1<<1); // Down
if (!(PIND & SAT_LF)) temp |= (1<<2); // Left
if (!(PIND & SAT_RT)) temp |= (1<<3); // Right
if (satmode == 1) reportBuffer->ry = temp - 128;
/* --- Left shoulder ----------------------------------------------------------------*/
PORTB &= ~(SAT_TR);
wait_tl_low();
// 11th
temp = 0;
if (!(PINB & SAT_UP)) temp |= (1<<4); // Up
if (!(PINB & SAT_DN)) temp |= (1<<5); // Down
if (!(PIND & SAT_LF)) temp |= (1<<6); // Left
if (!(PIND & SAT_RT)) temp |= (1<<7); // Right
PORTB |= (SAT_TR);
wait_tl_high();
// 12th
if (!(PINB & SAT_UP)) temp |= (1<<0); // Up
if (!(PINB & SAT_DN)) temp |= (1<<1); // Down
if (!(PIND & SAT_LF)) temp |= (1<<2); // Left
if (!(PIND & SAT_RT)) temp |= (1<<3); // Right
if (satmode == 1) reportBuffer->rx = temp - 128;
/* --- End of data transfer ---------------------------------------------------------*/
PORTB |= (SAT_TR|SAT_TH);
}
}
/* Wait for TL line to change state. Timeout prevents lock-up. */
void wait_tl_low(void)
{
uchar timer = 0;
while (PINB & SAT_TL)
{
timer++;
if (timer == 200) return; // should only ever take max 2us
}
}
void wait_tl_high(void)
{
uchar timer = 0;
while (!(PINB & SAT_TL))
{
timer++;
if (timer == 200) return; // should only ever take max 2us
}
}