-
Notifications
You must be signed in to change notification settings - Fork 1
/
sfc-jamma.ino
334 lines (249 loc) · 8.31 KB
/
sfc-jamma.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
////////////////////////////////////////////////////////////////////////////////
// SFC PROFILE CONFIGURATION
////////////////////////////////////////////////////////////////////////////////
#define BUTTON_MAX (16)
#define PROFILE_MAX (2)
int profile = 0;
unsigned int held_btns = 0;
unsigned long held_time = 0;
bool held_test = false;
////////////////////////////////////////////////////////////////////////////////
// SFC PIN CONFIGURATION
////////////////////////////////////////////////////////////////////////////////
#define DATA_SERIAL (14)
#define DATA_CLOCK (15)
#define DATA_LATCH (16)
////////////////////////////////////////////////////////////////////////////////
// JAMMA BUTTON
////////////////////////////////////////////////////////////////////////////////
#define P1_TEST (2)
#define P1_COIN (3)
#define P1_START (4)
#define P1_UP (5)
#define P1_DOWN (6)
#define P1_LEFT (7)
#define P1_RIGHT (8)
#define P1_LP (9)
#define P1_MP (10)
#define P1_HP (11)
#define P1_LK (17)
#define P1_MK (18)
#define P1_HK (19)
////////////////////////////////////////////////////////////////////////////////
// IBUS CONFIGURATION FOR SENDING BUTTONS BACK TO
// PC OVER SERIAL/USB INTERFACE
////////////////////////////////////////////////////////////////////////////////
#define ibus_baud (115200)
#define ibus_sep (0x40)
#define ibus_channels (3)
#define ibus_len (4 + (ibus_channels * 2))
int ibus_checksum = 0;
////////////////////////////////////////////////////////////////////////////////
// CONSTANT TIME DELAY CODE
// BROOK SNES ADAPTERS ARE UNSTABLE AT 6MS, BUT STABLE AT 7MS
// ADD AN EXTRA 0.5MS TO ENSURE A MARGIN OF ERROR
////////////////////////////////////////////////////////////////////////////////
unsigned long time;
#define time_delay (7500)
////////////////////////////////////////////////////////////////////////////////
// BUTTON MAPPING PROFILES
////////////////////////////////////////////////////////////////////////////////
const byte P1[PROFILE_MAX][BUTTON_MAX] = {
{
P1_LK,
P1_LP,
P1_COIN,
P1_START,
P1_UP,
P1_DOWN,
P1_LEFT,
P1_RIGHT,
P1_MK,
P1_MP,
P1_HP,
P1_HK,
0xFF,
0xFF,
0xFF,
0xFF,
}, {
P1_LK,
P1_LP,
P1_COIN,
P1_START,
P1_UP,
P1_DOWN,
P1_LEFT,
P1_RIGHT,
P1_HK,
P1_HP,
P1_MP,
P1_MK,
0xFF,
0xFF,
0xFF,
0xFF,
}
};
////////////////////////////////////////////////////////////////////////////////
// READ DATA FROM THE SFC SERIAL BUS
////////////////////////////////////////////////////////////////////////////////
unsigned int read_button() {
unsigned int buttons = 0x0000;
// INITIATE A LATCHING SEQUENCE
digitalWrite(DATA_LATCH, HIGH);
delayMicroseconds(12);
digitalWrite(DATA_LATCH, LOW);
// LOOP THROUGH EACH BUTTON AND READ ITS VALUE
for(int i=0; i<BUTTON_MAX; i++){
delayMicroseconds(6);
digitalWrite(DATA_CLOCK, LOW);
delayMicroseconds(6);
buttons |= (!!digitalRead(DATA_SERIAL)) << i;
digitalWrite(DATA_CLOCK, HIGH);
}
// CONTROLLER APPEARS UNPLUGGED
// PROCESS AS NO BUTTONS PRESSED INSTEAD
if (buttons == 0x0000) buttons = 0xFFFF;
// NES CONTROLLER SUPPORT
// UNSET HIGH BITS IF WE DETECT 8-BIT NES CONTROLLERS
if ((buttons & 0xFF00) == 0x0000) {
buttons |= 0xFF00;
}
return buttons;
}
////////////////////////////////////////////////////////////////////////////////
// BEGIN AN IBUS TRANSMISSION
////////////////////////////////////////////////////////////////////////////////
void ibus_start() {
ibus_checksum = 0xffff - ibus_len - ibus_sep;
Serial.write(ibus_len);
Serial.write(ibus_sep);
}
////////////////////////////////////////////////////////////////////////////////
// END AN IBUS TRANSMISSION
////////////////////////////////////////////////////////////////////////////////
void ibus_end() {
Serial.write(ibus_checksum & 0xff);
Serial.write(ibus_checksum >> 8);
}
////////////////////////////////////////////////////////////////////////////////
// SEND AN IBUS MESASGE
////////////////////////////////////////////////////////////////////////////////
void ibus_write(unsigned short data) {
// WRITE FIRST BYTE
byte b = data & 0xff;
Serial.write(b);
ibus_checksum -= b;
// WRITE SECOND BYTE
b = data >> 8;
Serial.write(b);
ibus_checksum -= b;
}
////////////////////////////////////////////////////////////////////////////////
// TOGGLE TEST/SETTINGS BUTTON WHEN START+SELECT HELD FOR 2 SECONDS
////////////////////////////////////////////////////////////////////////////////
void test_button() {
if (micros() - held_time < 2000000) return;
held_btns = 0x0000;
held_test = !held_test;
if (held_test) {
pinMode(P1_TEST, OUTPUT);
digitalWrite(P1_TEST, LOW);
} else {
pinMode(P1_TEST, INPUT_PULLUP);
}
}
////////////////////////////////////////////////////////////////////////////////
// MAIN INITIALIZATION CODE
////////////////////////////////////////////////////////////////////////////////
void setup() {
// initialize pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
// INITIALIZE PIN FOR TEST MODE
pinMode(P1_TEST, INPUT_PULLUP);
// SET ALL JAMMA PINS TO "INPUT" MODE
for (int i=0; i<BUTTON_MAX; i++) {
if (P1[profile][i] == 0xFF) continue;
pinMode(P1[profile][i], INPUT_PULLUP);
}
// SET DATA_CLOCK NORMALLY HIGH
pinMode(DATA_CLOCK, OUTPUT);
digitalWrite(DATA_CLOCK, HIGH);
// SET DATA_LATCH NORMALLY LOW
pinMode(DATA_LATCH, OUTPUT);
digitalWrite(DATA_LATCH, LOW);
// SET DATA_SERIAL NORMALLY HIGH
pinMode(DATA_SERIAL, OUTPUT);
digitalWrite(DATA_SERIAL, HIGH);
pinMode(DATA_SERIAL, INPUT_PULLUP);
// SETUP SERIAL FOR IBUS
Serial.begin(ibus_baud);
// GET INITIAL TIMEFRAME
time = micros();
}
////////////////////////////////////////////////////////////////////////////////
// MAIN PROGRAM LOOP
////////////////////////////////////////////////////////////////////////////////
void loop() {
// READ THE CURRENT BUTTON STATE
unsigned int buttons = read_button();
unsigned int jamma = 0;
unsigned short jamma_x = 0x8000;
unsigned short jamma_y = 0x8000;
// LIGHT UP ARDUINO LED IF ANY SFC BUTTONS ARE PRESSED
digitalWrite(LED_BUILTIN, (buttons != 0xFFFF));
// BUTTON STATE CHANGED FROM LAST POLL TIME
if (held_btns != buttons) {
held_btns = buttons;
held_time = micros();
}
// SWITCH BETWEEN JAMMA BUTTON MAPPING PROFILES
switch (~buttons) {
case 0x0014: profile = 0; break;
case 0x0024: profile = 1; break;
// TEST BUTTON TOGGLE
case 0x000C: test_button(); break;
}
// PROCESS BUTTON DATA
for (int i=0; i<BUTTON_MAX; i++) {
int btn = P1[profile][i];
if (btn == 0xFF) continue;
// SFC BUTTON NOT PRESSED, READ JAMMA BUTTON INSTEAD
if (buttons & (0x01 << i)) {
pinMode(btn, INPUT_PULLUP);
if (digitalRead(btn) == LOW) {
jamma |= (0x01 << i);
// PROCESS ARCADE JOYSTICK
switch (btn) {
case P1_LEFT: jamma_x = 0x0000; break;
case P1_RIGHT: jamma_x = 0xffff; break;
case P1_UP: jamma_y = 0x0000; break;
case P1_DOWN: jamma_y = 0xffff; break;
}
}
// SEND SFC BUTTON PRESS TO JAMMA
} else {
pinMode(btn, OUTPUT);
digitalWrite(btn, LOW);
// PROCESS SFC D-PAD
switch (btn) {
case P1_LEFT: jamma_x = 0x0000; break;
case P1_RIGHT: jamma_x = 0xffff; break;
case P1_UP: jamma_y = 0x0000; break;
case P1_DOWN: jamma_y = 0xffff; break;
}
}
}
// SEND SFC AND JAMMA BUTTON STATUS TO IBUS
ibus_start();
ibus_write(jamma | (~buttons));
ibus_write(jamma_x);
ibus_write(jamma_y);
ibus_end();
// DELAY REQUIRED FOR BROOK ADAPTER COMPATIBILITY
// USE A CONSTANT TIME DELAY REGARDLESS OF PROCESSING ABOVE
while ((micros() - time) < time_delay) {}
time += time_delay;
}