-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdb9.c
333 lines (269 loc) · 7.2 KB
/
db9.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
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
/* Nes/Snes/Genesis/SMS/Atari to USB
* Copyright (C) 2006-2015 Raphaël Assénat
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* The author may be contacted at raph@raphnet.net
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include <string.h>
#include "gamepads.h"
#include "db9.h"
#define REPORT_SIZE 3
#define GAMEPAD_BYTES 3
#define CTL_ID_ATARI 0x00 // up/dn/lf/rt/btn_b
#define CTL_ID_SMS 0x01 // up/dn/lf/rt/btn_b/btn_c
#define CTL_ID_GENESIS3 0x02 // up/dn/lf/rt/btn_a/btn_b/btn_c/start
#define CTL_ID_GENESIS6 0x03 // all bits
#define CTL_ID_PADDLE 0x04 // HPD-200 Japanese
#define isGenesis(a) ( (a) >= CTL_ID_GENESIS3)
static unsigned char cur_id = CTL_ID_GENESIS3;
#ifdef DB9_V2
static inline unsigned char SAMPLE()
{
unsigned char c;
unsigned char b;
unsigned char res;
c = PINC;
b = PINB;
/* Target bits in 'res' are:
*
* 0: Up/Up/Z DB9 pin 1
* 1: Down/Down/Y DB9 pin 2
* 2: Left/0/X DB9 pin 3
* 3: Right/0 DB9 pin 4
* 4: BtnB/BtnA DB9 pin 6
* 5: Btnc/BtnStart DB9 pin 9
* 6: Extra button (for direct wiring)
* 7:
*/
res = 0;
if (b & 0x04) res |= 0x01; // Up
if (b & 0x10) res |= 0x02; // Down
if (c & 0x02) res |= 0x04; // Left
if (c & 0x08) res |= 0x08; // Right
if (b & 0x08) res |= 0x10; // B/A
if (c & 0x04) res |= 0x20; // C/Start
return res;
}
#define SELECT_PORT PORTB
#define SELECT_BIT PORTB5
#define SELECT_DDR DDRB
#else
static inline unsigned char SAMPLE()
{
unsigned char c;
unsigned char b;
unsigned char res;
c = PINC;
b = PINB;
/* Target bits in 'res' are:
*
* 0: Up/Up/Z
* 1: Down/Down/Y
* 2: Left/0/X
* 3: Right/0
* 4: BtnB/BtnA
* 5: Btnc/BtnStart
* 6: Extra button (for direct wiring)
* 7:
*/
res = b & 0x0f;
res |= (c & 0x3) << 4;
return res;
}
#define SELECT_PORT PORTC
#define SELECT_BIT PORTC2
#define SELECT_DDR DDRC
#endif
#define SET_SELECT() SELECT_PORT |= 1<<SELECT_BIT;
#define CLR_SELECT() SELECT_PORT &= ~(1<<SELECT_BIT);
/*********** prototypes *************/
static char db9Init(void);
static char db9Update(void);
#define PADDLE_MAP_HORIZ 0
#define PADDLE_MAP_VERT 1
#define PADDLE_MAP_1BTN 0
#define PADDLE_MAP_2BTN 2
static db9_pad_data last_read_state, last_reported_state;
#define READ_CONTROLLER_SIZE 5
static void readController(unsigned char bits[READ_CONTROLLER_SIZE])
{
unsigned char a,b,c,d,e;
// total delays: 160 microseconds.
/* | 1 | 2 | 3 | 4 | 5 ...
* ___ __ __ __ __
* |__| |__| |__| |__|
* ^ ^ ^ ^ ^
* A B D E C
*
* ABC are used when reading controllers.
* D and E are used for auto-detecting the genesis 6 btn controller.
*
*/
// DDRC |= 0x08;
// PORTC |= 0x08;
/* 1 */
SET_SELECT();
_delay_us(20);
a = SAMPLE();
// PORTC &= ~0x08;
if (cur_id == CTL_ID_ATARI ||
cur_id == CTL_ID_SMS) {
bits[0] = a;
bits[1] = 0xff;
bits[2] = 0xff;
return;
}
CLR_SELECT();
_delay_us(20);
b = SAMPLE();
/* 2 */
SET_SELECT();
_delay_us(20);
CLR_SELECT();
_delay_us(20);
d = SAMPLE();
/* 3 */
SET_SELECT();
_delay_us(20);
CLR_SELECT();
_delay_us(20);
e = SAMPLE();
/* 4 */
SET_SELECT();
_delay_us(20);
c = SAMPLE();
CLR_SELECT();
_delay_us(20);
/* 5 */
SET_SELECT();
bits[0] = a;
bits[1] = b;
bits[2] = c;
bits[3] = d;
bits[4] = e;
}
static char db9Init(void)
{
unsigned char bits[READ_CONTROLLER_SIZE];
SELECT_DDR |= (1<<SELECT_BIT);
SELECT_PORT |= (1<<SELECT_BIT);
#ifdef DB9_V2
DDRB &= ~(0x04 | 0x10 | 0x08);
PORTB |= (0x04 | 0x10 | 0x08);
DDRC &= ~(0x02 | 0x08 | 0x04);
PORTC |= (0x02 | 0x08 | 0x04);
#else
// Directions as input with pull-up
DDRB &= 0xF0;
PORTB |= 0x0F;
// Buttons B/A and C/Start
DDRC &= 0xFC;
PORTC |= 0x03;
#endif
readController(bits);
cur_id = CTL_ID_SMS;
if ((bits[0]&0xf) == 0xf) {
if ((bits[1]&0xf) == 0x3)
{
if ( ((bits[3] & 0xf) != 0x3) ||
((bits[4] & 0xf) != 0x3) ) {
/* 6btn controllers return 0x0 and 0xf here. But
* for greater compatibility, I only test
* if it is different from a 3 button controller. */
cur_id = CTL_ID_GENESIS6;
}
else {
cur_id = CTL_ID_GENESIS3;
}
}
}
/* Force 6 Button genesis controller. Useful if auto-detection
* fails for some reason. */
if (!(bits[1] & 0x20)) { // if start button initially held down
cur_id = CTL_ID_GENESIS6;
}
db9Update();
return 0;
}
static char db9Update(void)
{
unsigned char data[READ_CONTROLLER_SIZE];
/* 0: Up//Z
* 1: Down//Y
* 2: Left//X
* 3: Right//Mode
* 4: Btn B/A
* 5: Btn C/Start/
*/
readController(data);
/* Buttons are active low. Invert the bits
* here to simplify subsequent 'if' statements... */
data[0] = data[0] ^ 0xff;
data[1] = data[1] ^ 0xff;
data[2] = data[2] ^ 0xff;
memset(&last_read_state, 0, sizeof(db9_pad_data));
if (data[0] & 1) { last_read_state.buttons |= DB9_BTN_DPAD_UP; }
if (data[0] & 2) { last_read_state.buttons |= DB9_BTN_DPAD_DOWN; }
if (data[0] & 4) { last_read_state.buttons |= DB9_BTN_DPAD_LEFT; }
if (data[0] & 8) { last_read_state.buttons |= DB9_BTN_DPAD_RIGHT; }
if (isGenesis(cur_id)) {
last_read_state.pad_type = PAD_TYPE_MD;
if (data[1]&0x10) { last_read_state.buttons |= DB9_BTN_A; } // A
if (data[0]&0x10) { last_read_state.buttons |= DB9_BTN_B; } // B
if (data[0]&0x20) { last_read_state.buttons |= DB9_BTN_C; } // C
if (data[1]&0x20) { last_read_state.buttons |= DB9_BTN_START; } // Start
if (cur_id == CTL_ID_GENESIS6) {
if (data[2]&0x04) { last_read_state.buttons |= DB9_BTN_X; } // X
if (data[2]&0x02) { last_read_state.buttons |= DB9_BTN_Y; } // Y
if (data[2]&0x01) { last_read_state.buttons |= DB9_BTN_Z; } // Z
if (data[2]&0x08) { last_read_state.buttons |= DB9_BTN_MODE; } // Mode
}
}
else {
last_read_state.pad_type = PAD_TYPE_SMS;
/* The button IDs for 1 and 2 button joysticks should start
* at '1'. Some Atari emulators don't support button remapping so
* this is pretty important! */
if (data[0]&0x10) { last_read_state.buttons |= DB9_BTN_B; } // Button 1
if (data[0]&0x20) { last_read_state.buttons |= DB9_BTN_C; } // Button 2
}
return 0;
}
static char db9Changed(void)
{
return memcmp(&last_read_state, &last_reported_state, sizeof(db9_pad_data));
}
static void db9Report(gamepad_data *dst)
{
if (dst) {
memcpy(dst, &last_read_state, sizeof(db9_pad_data));
memcpy(&last_reported_state, &last_read_state, sizeof(db9_pad_data));
}
}
static Gamepad db9Gamepad = {
.init = db9Init,
.update = db9Update,
.changed = db9Changed,
.getReport = db9Report,
};
Gamepad *db9GetGamepad(void)
{
return &db9Gamepad;
}