-
Notifications
You must be signed in to change notification settings - Fork 3
/
snes2ps.c
494 lines (425 loc) · 11 KB
/
snes2ps.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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
snes2psx: SNES controller to Playstation adapter
Copyright (C) 2012-2014 Raphael Assenat <raph@raphnet.net>
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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define CMD_BEGIN_01 0x01
#define CMD_GET_DATA_42 0x42
#define REP_DATA_START_5A 0x5a
#define DEVICE_ID 0x41 /* digital pad */
#define ST_IDLE 0
#define ST_READY 1
#define ST_SEND_BUF0 2
#define ST_SEND_BUF1 3
#define ST_DONE 4
/******** IO port definitions **************/
#define SNES_LATCH_DDR DDRC
#define SNES_LATCH_PORT PORTC
#define SNES_LATCH_BIT (1<<4)
#define SNES_CLOCK_DDR DDRC
#define SNES_CLOCK_PORT PORTC
#define SNES_CLOCK_BIT (1<<5)
#define SNES_DATA_PORT PORTC
#define SNES_DATA_DDR DDRC
#define SNES_DATA_PIN PINC
#define SNES_DATA_BIT (1<<3)
#define PSX_ACK_PORT PORTC
#define PSX_ACK_DDR DDRC
#define PSX_ACK_PIN PINC
#define PSX_ACK_BIT (1<<0)
/********* IO pins manipulation macros **********/
#define SNES_LATCH_LOW() do { SNES_LATCH_PORT &= ~(SNES_LATCH_BIT); } while(0)
#define SNES_LATCH_HIGH() do { SNES_LATCH_PORT |= SNES_LATCH_BIT; } while(0)
#define SNES_CLOCK_LOW() do { SNES_CLOCK_PORT &= ~(SNES_CLOCK_BIT); } while(0)
#define SNES_CLOCK_HIGH() do { SNES_CLOCK_PORT |= SNES_CLOCK_BIT; } while(0)
#define SNES_GET_DATA() (SNES_DATA_PIN & SNES_DATA_BIT)
/* PSX data : (MSb first)
Left Down Right Up Start 1 1 Select
Square X O Tri. R1 L1 R2 L2
*/
#define PSX_LEFT 0x8000
#define PSX_DOWN 0x4000
#define PSX_RIGHT 0x2000
#define PSX_UP 0x1000
#define PSX_START 0x0800
#define PSX_SELECT 0x0100
#define PSX_SQUARE 0x0080
#define PSX_X 0x0040
#define PSX_O 0x0020
#define PSX_TRIANGLE 0x0010
#define PSX_R1 0x0008
#define PSX_L1 0x0004
#define PSX_R2 0x0002
#define PSX_L2 0x0001
/* SNES data, in the received order.
B Y Select Start
Up Down Left Right
A X L R
1 1 1 1
*/
#define SNES_B 0x8000
#define SNES_Y 0x4000
#define SNES_SELECT 0x2000
#define SNES_START 0x1000
#define SNES_UP 0x0800
#define SNES_DOWN 0x0400
#define SNES_LEFT 0x0200
#define SNES_RIGHT 0x0100
#define SNES_A 0x0080
#define SNES_X 0x0040
#define SNES_L 0x0020
#define SNES_R 0x0010
struct map_ent {
unsigned short s; // Snes bit
unsigned short p; // PSX bit
};
#define ALT_MAPPING_SNES_BIT SNES_SELECT
static struct map_ent type1_mapping[] = {
{ SNES_B, PSX_X },
{ SNES_Y, PSX_SQUARE },
{ SNES_SELECT, PSX_SELECT },
{ SNES_START, PSX_START },
{ SNES_UP, PSX_UP },
{ SNES_DOWN, PSX_DOWN },
{ SNES_LEFT, PSX_LEFT },
{ SNES_RIGHT, PSX_RIGHT },
{ SNES_A, PSX_O },
{ SNES_X, PSX_TRIANGLE },
{ SNES_R, PSX_R1 },
{ SNES_L, PSX_L1 },
{ 0, 0 },
};
static struct map_ent type2_mapping[] = {
{ SNES_B, PSX_O },
{ SNES_Y, PSX_X },
{ SNES_SELECT, PSX_SELECT },
{ SNES_START, PSX_START },
{ SNES_UP, PSX_UP },
{ SNES_DOWN, PSX_DOWN },
{ SNES_LEFT, PSX_LEFT },
{ SNES_RIGHT, PSX_RIGHT },
{ SNES_A, PSX_R2 },
{ SNES_X, PSX_TRIANGLE },
{ SNES_R, PSX_R1 },
{ SNES_L, PSX_SQUARE },
{ 0, 0 },
};
static struct map_ent type3_mapping[] = {
{ SNES_B, PSX_TRIANGLE },
{ SNES_Y, PSX_O },
{ SNES_SELECT, PSX_SELECT },
{ SNES_START, PSX_START },
{ SNES_UP, PSX_UP },
{ SNES_DOWN, PSX_DOWN },
{ SNES_LEFT, PSX_LEFT },
{ SNES_RIGHT, PSX_RIGHT },
{ SNES_A, PSX_X },
{ SNES_X, PSX_SQUARE },
{ SNES_R, PSX_R1 },
{ SNES_L, PSX_L1 },
{ 0, 0 },
};
static struct map_ent type4_mapping[] = {
{ SNES_B, PSX_SQUARE },
{ SNES_Y, PSX_X },
{ SNES_SELECT, PSX_SELECT },
{ SNES_START, PSX_START },
{ SNES_UP, PSX_UP },
{ SNES_DOWN, PSX_DOWN },
{ SNES_LEFT, PSX_LEFT },
{ SNES_RIGHT, PSX_RIGHT },
{ SNES_A, PSX_TRIANGLE },
{ SNES_X, PSX_O },
{ SNES_R, PSX_R1 },
{ SNES_L, PSX_L1 },
{ 0, 0 },
};
static struct map_ent type5_mapping[] = {
{ SNES_B, PSX_O },
{ SNES_Y, PSX_TRIANGLE },
{ SNES_SELECT, PSX_SELECT },
{ SNES_START, PSX_START },
{ SNES_UP, PSX_UP },
{ SNES_DOWN, PSX_DOWN },
{ SNES_LEFT, PSX_LEFT },
{ SNES_RIGHT, PSX_RIGHT },
{ SNES_A, PSX_SQUARE },
{ SNES_X, PSX_X },
{ SNES_R, PSX_L1 }, // L/R swapped
{ SNES_L, PSX_R1 },
{ 0, 0 },
};
static struct map_ent type6_mapping[] = { // Type 1 with L2/R2
{ SNES_B, PSX_X },
{ SNES_Y, PSX_SQUARE },
{ SNES_SELECT, PSX_SELECT },
{ SNES_START, PSX_START },
{ SNES_UP, PSX_UP },
{ SNES_DOWN, PSX_DOWN },
{ SNES_LEFT, PSX_LEFT },
{ SNES_RIGHT, PSX_RIGHT },
{ SNES_A, PSX_O },
{ SNES_X, PSX_TRIANGLE },
{ SNES_R, PSX_R2 },
{ SNES_L, PSX_L2 },
{ 0, 0 },
};
static struct map_ent type7_mapping[] = { // Type 1 with rotated directions for right-hand arcade stick steering
{ SNES_B, PSX_X },
{ SNES_Y, PSX_SQUARE },
{ SNES_SELECT, PSX_SELECT },
{ SNES_START, PSX_START },
{ SNES_UP, PSX_DOWN },
{ SNES_DOWN, PSX_UP },
{ SNES_LEFT, PSX_RIGHT },
{ SNES_RIGHT, PSX_LEFT },
{ SNES_A, PSX_O },
{ SNES_X, PSX_TRIANGLE },
{ SNES_R, PSX_L1 },
{ SNES_L, PSX_R1 },
{ 0, 0 },
};
static struct map_ent *g_cur_map = type1_mapping;
static unsigned char state = ST_IDLE;
static volatile unsigned char psxbuf[2];
static unsigned char snesbuf[2];
#define CHIP_SELECT_ACTIVE() (0 == (PINB & (1<<2)))
static void ack()
{
_delay_us(1);
// pull acknowledge
PSX_ACK_PORT &= ~PSX_ACK_BIT;
PSX_ACK_DDR |= PSX_ACK_BIT;
_delay_us(3);
// release acknowledge
PSX_ACK_DDR &= ~PSX_ACK_BIT;
}
ISR(SPI_STC_vect)
{
unsigned char cmd;
cmd = SPDR;
switch(state)
{
case ST_IDLE: // Expecting 0x01
if (cmd != CMD_BEGIN_01) {
/* First byte is no 0x01? This is not a message for us (probably memory card)
*
* Ignore all other bytes until Slave Select is deasserted.
*/
while (CHIP_SELECT_ACTIVE()) {
// Make sure we dont pull the bus low.
if (SPSR & (1<<SPIF)) {
cmd = SPDR;
SPDR = 0x00; // dont pull the bus low (sends 0xff)
}
}
}
else {
// Prepare the Device id (0x41)
SPDR = 0xff ^ DEVICE_ID;
state = ST_READY;
ack();
}
break;
case ST_READY: // Expecting 0x42
if (cmd == CMD_GET_DATA_42) {
SPDR = 0xff ^ REP_DATA_START_5A;
state = ST_SEND_BUF0;
ack();
}
break;
// Based on Playstation.txt, I initially understood that the Playstation
// would always send 0x00 when reading the button status and wrote code
// that checked the received values.
//
// However, Einhander sends 0x40:
// Game...: 0x01, 0x42, 0x00, 0x40, 0x00
// Adapter: 0xFF, 0x41, 0x5a, dat[0], dat[1]
//
// And rollcage sends a 0x01:
// Game...: 0x01, 0x42, 0x01, 0x00, 0x00
// Adapter: 0xFF, 0x41, 0x5a, dat[0], dat[1]
//
// The unexpected values above prevented the games from working, so
// I now treat the transmitted values during button status as "don't care"
// rather than "expecting 0".
//
// This seems to be working well.
//
case ST_SEND_BUF0: // start of data 0x5a sent
SPDR = 0xff ^ psxbuf[0];
state = ST_SEND_BUF1;
ack();
break;
case ST_SEND_BUF1: // psxbuf[0] sent
SPDR = 0xff ^ psxbuf[1];
state = ST_DONE;
ack();
break;
case ST_DONE: // psxbuf[1] sent
SPDR = 0x00; // dont pull the bus low (send 0xff)
state = ST_IDLE;
break;
}
}
/* update snesbuf[] */
static void snesUpdate(void)
{
int i,j;
unsigned char tmp=0;
SNES_LATCH_HIGH();
_delay_us(12);
SNES_LATCH_LOW();
for (j=0; j<2; j++)
{
for (i=0; i<8; i++)
{
_delay_us(6);
SNES_CLOCK_LOW();
tmp <<= 1;
if (SNES_GET_DATA())
tmp |= 1;
_delay_us(6);
SNES_CLOCK_HIGH();
}
snesbuf[j] = tmp;
}
}
unsigned short snes2psx(unsigned short snesbits)
{
unsigned short psxval;
int i;
struct map_ent *map = g_cur_map;
/* Start with a ALL ones message and
* clear the bits when needed. */
psxval = 0xffff;
for (i=0; map[i].s; i++) {
if (!(snesbits & map[i].s))
psxval &= ~(map[i].p);
}
return psxval;
}
int main(void)
{
/* PORT C
* Name Type
* 0: PSX ACT Emulated OC
* 1: NC OUT 0
* 2: NC OUT 0
* 3: SNES DATA IN - PU
*
* 4: SNES LATCH OUT 0
* 5: SNES CLK OUT 0
* 6: reset
*/
DDRC = 0xF6;
PORTC = 0x08;
/* PORT B
*
* Name Type
* 0, 1, 2: Attention Input (The 3 pins are shorted together)
* 3 : CMD (MOSI) from PSX Input
* 4 : DATA (MISO) to PSX Output 0
* 5 : PSX CLK (SCK) from PSX Input
* 6 : XTAL
* 7 : XTAL
*/
PORTB = 0;
DDRB = 0x10;
/* PORTD
*
* Name Type
* 0: USB OUT 0
* 1: USB OUT 0
* 2: USB OUT 0
* 3: NC OUT 0
* 4: VCC OUT 1
* 5: NC OUT 1
* 6: NC OUT 1
* 7: NC OUT 1
*
*/
PORTD = 0xFF;
DDRD = 0;
/* Enable interrupt (SPIE)
* Enable SPI (SPE)
* Use LSB first transmission (DORD)
* Slave mode (MSTR not set)
* Clock normally high (CPOL)
* Data setup on leading edge (falling in this case) (CPHA)
* */
SPCR = (1<<SPIE) | (1<<SPE) | (1<<DORD) | (1<<CPOL) | (1<<CPHA);
SPDR = 0xff ^ 0xff;
/* configure acknowledge pin. Simulate an open-collector
* by changing it's direction. */
PSX_ACK_PORT &= ~PSX_ACK_BIT;
PSX_ACK_DDR &= ~PSX_ACK_BIT;
// buttons are active low and reserved bits stay high.
psxbuf[0] = 0xff;
psxbuf[1] = 0xff;
// TODO: Snes stuff
//
// clock and latch as output
SNES_LATCH_DDR |= SNES_LATCH_BIT;
SNES_CLOCK_DDR |= SNES_CLOCK_BIT;
// data as input
SNES_DATA_DDR &= ~(SNES_DATA_BIT);
// enable pullup. This should prevent random toggling of pins
// when no controller is connected.
SNES_DATA_PORT |= SNES_DATA_BIT;
// clock is normally high
SNES_CLOCK_PORT |= SNES_CLOCK_BIT;
// LATCH is Active HIGH
SNES_LATCH_PORT &= ~(SNES_LATCH_BIT);
snesUpdate();
switch (0xFFFF ^ (snesbuf[0]<<8 | snesbuf[1]))
{
case SNES_START:
g_cur_map = type1_mapping;
break;
case SNES_SELECT:
g_cur_map = type2_mapping;
break;
case SNES_A:
g_cur_map = type3_mapping;
break;
case SNES_B:
g_cur_map = type4_mapping;
break;
case SNES_X:
g_cur_map = type5_mapping;
break;
case SNES_Y:
g_cur_map = type6_mapping;
break;
case SNES_L:
g_cur_map = type7_mapping;
break;
}
sei();
while(1)
{
unsigned short psxbits;
if (!CHIP_SELECT_ACTIVE()) {
SPDR = 0x00;
state = ST_IDLE;
}
snesUpdate();
psxbits = snes2psx((snesbuf[0]<<8) | snesbuf[1]);
psxbuf[0] = psxbits >> 8;
psxbuf[1] = psxbits & 0xff;
}
}