2013-10-16 22:26:13 -04:00
|
|
|
/* Dreamcast to USB : Sega dc controllers to USB adapter
|
|
|
|
* Copyright (C) 2013 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
|
|
|
|
*/
|
2013-10-13 17:01:08 -04:00
|
|
|
#include <avr/io.h>
|
|
|
|
#include <util/delay.h>
|
2013-10-27 18:38:44 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "maplebus.h"
|
2013-10-13 17:01:08 -04:00
|
|
|
|
2013-10-26 16:26:58 -04:00
|
|
|
#undef NOLRC
|
|
|
|
#undef TRACE_RX_START_END
|
|
|
|
#undef TRACE_DECODED
|
|
|
|
#undef TRACE_PIN1_BITS
|
|
|
|
|
2013-10-13 17:01:08 -04:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// PORTC0 : Pin 1
|
|
|
|
// PORTC1 : Pin 5
|
|
|
|
//
|
|
|
|
|
|
|
|
void maple_init(void)
|
|
|
|
{
|
2013-10-26 16:26:58 -04:00
|
|
|
DDRC = 0xFC;
|
|
|
|
PORTC = 0x03;
|
2013-10-13 17:01:08 -04:00
|
|
|
}
|
2013-10-26 16:26:58 -04:00
|
|
|
#define transmitMode() do { PORTC |= 0x03; DDRC |= 0x03; } while(0)
|
|
|
|
#define inputMode() do { PORTC |= 0x03; DDRC &= ~0x03; } while(0)
|
|
|
|
|
2013-10-13 17:01:08 -04:00
|
|
|
|
2013-10-26 16:26:58 -04:00
|
|
|
#define MAPLE_BUF_SIZE 641
|
2013-10-13 17:01:08 -04:00
|
|
|
static unsigned char maplebuf[MAPLE_BUF_SIZE];
|
|
|
|
static unsigned char buf_used;
|
|
|
|
static unsigned char buf_phase;
|
|
|
|
|
|
|
|
#define PIN_1 0x01
|
|
|
|
#define PIN_5 0x02
|
|
|
|
static void buf_reset(void)
|
|
|
|
{
|
|
|
|
buf_used = 0;
|
|
|
|
buf_phase = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void buf_addBit(char value)
|
|
|
|
{
|
2013-10-26 16:26:58 -04:00
|
|
|
// The values in maplebuf will be written
|
|
|
|
// directly to PORTC. Unused bits will be low.
|
2013-10-13 17:01:08 -04:00
|
|
|
if (buf_phase & 0x01) {
|
|
|
|
maplebuf[buf_used] = PIN_5;
|
|
|
|
if (value) {
|
|
|
|
maplebuf[buf_used] |= PIN_1; // prepare data
|
|
|
|
}
|
|
|
|
buf_used++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
maplebuf[buf_used] = PIN_1;
|
|
|
|
if (value) {
|
|
|
|
maplebuf[buf_used] |= PIN_5; // prepare data
|
|
|
|
}
|
|
|
|
buf_used++;
|
|
|
|
}
|
|
|
|
buf_phase ^= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int maplebus_decode(unsigned char *data, unsigned int maxlen)
|
|
|
|
{
|
|
|
|
unsigned char dst_b;
|
|
|
|
unsigned int dst_pos;
|
|
|
|
unsigned char last;
|
|
|
|
unsigned char last_fell;
|
|
|
|
int i;
|
|
|
|
|
2013-10-26 16:26:58 -04:00
|
|
|
#ifdef TRACE_DECODED
|
|
|
|
PORTB |= 0x10;
|
|
|
|
PORTB &= ~0x10;
|
|
|
|
PORTB |= 0x10;
|
|
|
|
PORTB &= ~0x10;
|
|
|
|
PORTB |= 0x10;
|
|
|
|
PORTB &= ~0x10;
|
|
|
|
#endif
|
|
|
|
|
2013-10-13 17:01:08 -04:00
|
|
|
// Look for the initial phase 1 (Pin 1 high, Pin 5 low). This
|
|
|
|
// is to skip what we got of the sync/start of frame sequence.
|
|
|
|
//
|
|
|
|
for (i=0; i<MAPLE_BUF_SIZE; i++) {
|
|
|
|
if ((maplebuf[i]&0x03) == 0x01)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i==MAPLE_BUF_SIZE) {
|
|
|
|
return -1; // timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
dst_pos = 0;
|
|
|
|
data[0] = 0;
|
|
|
|
dst_b = 0x80;
|
|
|
|
last = maplebuf[i] & 0x03;
|
|
|
|
last_fell = 0;
|
|
|
|
for (; i<MAPLE_BUF_SIZE; i++) {
|
|
|
|
unsigned char fell;
|
2013-10-26 16:26:58 -04:00
|
|
|
unsigned char cur = maplebuf[i] & 0x3;
|
|
|
|
|
|
|
|
#ifdef TRACE_PIN1_BITS
|
|
|
|
if (cur & 1) {
|
|
|
|
PORTB |= 0x10;
|
|
|
|
} else {
|
|
|
|
PORTB &= ~0x10;
|
|
|
|
}
|
|
|
|
#endif
|
2013-10-13 17:01:08 -04:00
|
|
|
|
|
|
|
if (cur == last) {
|
|
|
|
continue; // no change
|
|
|
|
}
|
|
|
|
|
|
|
|
fell = last & (cur ^ last);
|
|
|
|
|
|
|
|
if (!fell) {
|
|
|
|
// pin(s) changed, but none fell.
|
|
|
|
last = cur;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fell == last_fell) {
|
|
|
|
// two identical consecutive phases marks the end of the packet.
|
2013-10-26 16:26:58 -04:00
|
|
|
#ifdef TRACE_DECODED
|
|
|
|
PORTB |= 0x10;
|
|
|
|
PORTB &= ~0x10;
|
|
|
|
PORTB |= 0x10;
|
|
|
|
PORTB &= ~0x10;
|
2013-10-13 17:01:08 -04:00
|
|
|
PORTB |= 0x10;
|
|
|
|
PORTB &= ~0x10;
|
2013-10-26 16:26:58 -04:00
|
|
|
#endif
|
2013-10-13 17:01:08 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// when any of the two pins fall, the
|
|
|
|
// other pin is the data.
|
|
|
|
if (fell) {
|
|
|
|
if (fell == 0x03) {
|
|
|
|
// two pins at the same time!
|
|
|
|
PORTB |= 0x10;
|
|
|
|
PORTB &= ~0x10;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cur) {
|
|
|
|
data[dst_pos] |= dst_b;
|
2013-10-26 16:26:58 -04:00
|
|
|
#ifdef TRACE_DECODED
|
|
|
|
PORTB |= 0x10;
|
|
|
|
#endif
|
2013-10-13 17:01:08 -04:00
|
|
|
}
|
|
|
|
else {
|
2013-10-26 16:26:58 -04:00
|
|
|
#ifdef TRACE_DECODED
|
|
|
|
PORTB &= ~0x10;
|
|
|
|
#endif
|
2013-10-13 17:01:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dst_b >>= 1;
|
|
|
|
if (!dst_b) {
|
|
|
|
dst_b = 0x80;
|
|
|
|
dst_pos++;
|
2013-10-27 18:38:44 -04:00
|
|
|
if (dst_pos >= maxlen) {
|
|
|
|
#ifdef TRACE_DECODED
|
|
|
|
PORTB &= ~0x10;
|
|
|
|
#endif
|
|
|
|
return -3;
|
|
|
|
}
|
2013-10-13 17:01:08 -04:00
|
|
|
data[dst_pos] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_fell = fell;
|
|
|
|
last = cur;
|
|
|
|
}
|
2013-10-27 18:38:44 -04:00
|
|
|
|
2013-10-26 16:26:58 -04:00
|
|
|
#ifdef TRACE_DECODED
|
|
|
|
PORTB &= ~0x10;
|
|
|
|
#endif
|
|
|
|
|
2013-10-13 17:01:08 -04:00
|
|
|
return dst_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \param data Destination buffer to store reply (payload + crc + eot)
|
|
|
|
* \param maxlen The length of the destination buffer
|
2013-10-27 18:38:44 -04:00
|
|
|
* \return -1 on timeout, -2 lrc/frame error, -3 too much data. Otherwise the number of bytes received
|
2013-10-13 17:01:08 -04:00
|
|
|
*/
|
2013-10-27 18:38:44 -04:00
|
|
|
int maple_receiveFrame(unsigned char *data, unsigned int maxlen)
|
2013-10-13 17:01:08 -04:00
|
|
|
{
|
|
|
|
unsigned char *tmp = maplebuf;
|
|
|
|
unsigned char lrc;
|
2013-10-14 22:25:39 -04:00
|
|
|
unsigned char timeout;
|
2013-10-13 17:01:08 -04:00
|
|
|
int res, i;
|
|
|
|
|
|
|
|
//
|
|
|
|
// __ _ _ _
|
|
|
|
// |_____| |_| |_| |_
|
|
|
|
// ___ _ _ _
|
|
|
|
// |_| |___| |_| |_
|
|
|
|
// 310022011023102310
|
|
|
|
// ^ ^ ^ ^^ ^^
|
|
|
|
//
|
|
|
|
|
|
|
|
asm volatile(
|
|
|
|
" push r30 \n" // 2
|
|
|
|
" push r31 \n" // 2
|
2013-10-14 22:25:39 -04:00
|
|
|
" clr %1 \n" // 1 (result=0, no timeout)
|
2013-10-13 17:01:08 -04:00
|
|
|
|
2013-10-26 16:26:58 -04:00
|
|
|
// " sbi 0x5, 4 \n" // PB4
|
|
|
|
// " cbi 0x5, 4 \n"
|
2013-10-13 17:01:08 -04:00
|
|
|
|
|
|
|
// Loop until a change is detected.
|
2013-10-14 22:25:39 -04:00
|
|
|
" ldi r18, 255 \n"
|
|
|
|
" in r17, %2 \n"
|
2013-10-13 17:01:08 -04:00
|
|
|
"wait_start: \n"
|
2013-10-14 22:25:39 -04:00
|
|
|
" dec r18 \n"
|
|
|
|
" breq timeout \n"
|
|
|
|
" in r16, %2 \n"
|
2013-10-13 17:01:08 -04:00
|
|
|
" cp r16, r17 \n"
|
|
|
|
" breq wait_start \n"
|
2013-10-14 22:25:39 -04:00
|
|
|
" rjmp start_rx \n"
|
2013-10-13 17:01:08 -04:00
|
|
|
|
2013-10-14 22:25:39 -04:00
|
|
|
"timeout:\n"
|
|
|
|
" inc %1 \n" // 1 for timeout
|
|
|
|
" sbi 0x5, 4 \n" // PB4
|
|
|
|
" cbi 0x5, 4 \n"
|
|
|
|
" jmp done \n"
|
|
|
|
|
|
|
|
"start_rx: \n"
|
2013-10-26 16:26:58 -04:00
|
|
|
#ifdef TRACE_RX_START_END
|
2013-10-13 17:01:08 -04:00
|
|
|
" sbi 0x5, 4 \n" // PB4
|
|
|
|
" cbi 0x5, 4 \n"
|
2013-10-26 16:26:58 -04:00
|
|
|
#endif
|
2013-10-13 17:01:08 -04:00
|
|
|
|
|
|
|
// We will loose the first bit(s), but
|
|
|
|
// it's only the start of frame.
|
|
|
|
#include "rxcode.asm"
|
|
|
|
|
2013-10-14 22:25:39 -04:00
|
|
|
"done:\n"
|
2013-10-26 16:26:58 -04:00
|
|
|
#ifdef TRACE_RX_START_END
|
2013-10-13 17:01:08 -04:00
|
|
|
" sbi 0x5, 4 \n" // PB4
|
|
|
|
" cbi 0x5, 4 \n"
|
2013-10-26 16:26:58 -04:00
|
|
|
#endif
|
2013-10-13 17:01:08 -04:00
|
|
|
" pop r31 \n" // 2
|
|
|
|
" pop r30 \n" // 2
|
2013-10-14 22:25:39 -04:00
|
|
|
: "=z"(tmp), "=r"(timeout)
|
2013-10-13 17:01:08 -04:00
|
|
|
: "I" (_SFR_IO_ADDR(PINC))
|
2013-10-14 22:25:39 -04:00
|
|
|
: "r16","r17","r18") ;
|
|
|
|
|
|
|
|
if (timeout)
|
|
|
|
return -1;
|
2013-10-13 17:01:08 -04:00
|
|
|
|
|
|
|
res = maplebus_decode(data, maxlen);
|
|
|
|
if (res<=0)
|
|
|
|
return res;
|
|
|
|
|
2013-10-26 16:26:58 -04:00
|
|
|
// A packet contains n groups of 4 bytes, plus 1 byte crc.
|
2013-10-13 17:01:08 -04:00
|
|
|
if (((res-1) & 0x3) != 0) {
|
|
|
|
return -2; // frame error
|
|
|
|
}
|
|
|
|
|
2013-10-26 16:26:58 -04:00
|
|
|
#ifndef NOLRC
|
2013-10-13 17:01:08 -04:00
|
|
|
for (lrc=0, i=0; i<res; i++) {
|
|
|
|
lrc ^= data[i];
|
|
|
|
}
|
|
|
|
if (lrc)
|
|
|
|
return -2; // LRC error
|
2013-10-26 16:26:58 -04:00
|
|
|
#endif
|
2013-10-13 17:01:08 -04:00
|
|
|
|
2013-10-14 22:25:39 -04:00
|
|
|
/* Reverse each group of 4 bytes */
|
|
|
|
for (i=0; i<(res-1); i+=4) {
|
|
|
|
unsigned char tmp;
|
|
|
|
|
|
|
|
tmp = data[i+3];
|
|
|
|
data[i+3] = data[i];
|
|
|
|
data[i] = tmp;
|
|
|
|
|
|
|
|
tmp = data[i+2];
|
|
|
|
data[i+2] = data[i+1];
|
|
|
|
data[i+1] = tmp;
|
|
|
|
}
|
|
|
|
|
2013-10-13 17:01:08 -04:00
|
|
|
return res-1; // remove lrc
|
|
|
|
}
|
|
|
|
|
2013-10-27 18:38:44 -04:00
|
|
|
void maple_sendRaw(unsigned char *data, unsigned char len)
|
2013-10-13 17:01:08 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned char b;
|
|
|
|
|
|
|
|
buf_reset();
|
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
for (b=0x80; b; b>>=1)
|
|
|
|
{
|
|
|
|
buf_addBit(data[i] & b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output
|
2013-10-26 16:26:58 -04:00
|
|
|
transmitMode();
|
2013-10-13 17:01:08 -04:00
|
|
|
|
|
|
|
// DC controller pin 1 and pin 5
|
|
|
|
#define SET_1 " sbi %0, 0\n"
|
|
|
|
#define CLR_1 " cbi %0, 0\n"
|
|
|
|
#define SET_5 " sbi %0, 1\n"
|
|
|
|
#define CLR_5 " cbi %0, 1\n"
|
|
|
|
#define DLY_8 " nop\nnop\nnop\nnop\nnop\nnop\nnop\nnop\n"
|
2013-10-27 18:38:44 -04:00
|
|
|
#define DLY_5 " nop\nnop\nnop\nnop\nnop\n"
|
2013-10-13 17:01:08 -04:00
|
|
|
#define DLY_4 " nop\nnop\nnop\nnop\n"
|
2013-10-27 18:38:44 -04:00
|
|
|
#define DLY_3 " nop\nnop\nnop\n"
|
2013-10-13 17:01:08 -04:00
|
|
|
|
|
|
|
asm volatile(
|
|
|
|
"push r31\n"
|
|
|
|
"push r30\n"
|
|
|
|
|
|
|
|
"mov r19, %1 \n" // Length in bytes
|
|
|
|
"ldi r20, 0x01 \n" // phase 1 pin 1 high, pin 5 low
|
|
|
|
"ldi r21, 0x02 \n" // phase 2 pin 1 low, pin 2 high
|
|
|
|
|
|
|
|
"ld r16, z+ \n"
|
|
|
|
|
|
|
|
// Sync
|
2013-10-27 18:38:44 -04:00
|
|
|
SET_1 SET_5 DLY_8
|
2013-10-13 17:01:08 -04:00
|
|
|
|
2013-10-27 18:38:44 -04:00
|
|
|
CLR_1 DLY_4
|
|
|
|
CLR_5 DLY_3
|
|
|
|
|
|
|
|
SET_5 DLY_3
|
|
|
|
CLR_5
|
|
|
|
DLY_3
|
|
|
|
SET_5
|
|
|
|
DLY_3
|
|
|
|
CLR_5
|
|
|
|
DLY_3
|
|
|
|
SET_5
|
|
|
|
DLY_3
|
|
|
|
CLR_5 DLY_3 SET_5
|
|
|
|
DLY_5 SET_1 CLR_5
|
2013-10-13 17:01:08 -04:00
|
|
|
|
2013-10-14 22:25:39 -04:00
|
|
|
// Pin 5 is low, Pin 1 is high. Ready for 1st phase
|
2013-10-13 17:01:08 -04:00
|
|
|
// Note: Coded for 16Mhz (8 cycles = 500ns)
|
|
|
|
"next_byte:\n"
|
|
|
|
|
|
|
|
"out %0, r20 \n" // 1 initial phase 1 state
|
2013-10-27 18:38:44 -04:00
|
|
|
// "nop \n" // 1
|
2013-10-13 17:01:08 -04:00
|
|
|
"out %0, r16 \n" // 1 data
|
|
|
|
"cbi %0, 0 \n" // 1 falling edge on pin 1
|
|
|
|
"ld r16, z+ \n" // 2 load phase 2 data
|
2013-10-27 18:38:44 -04:00
|
|
|
// "nop \n" // 1
|
2013-10-13 17:01:08 -04:00
|
|
|
|
|
|
|
"out %0, r21 \n" // 1 initial phase 2 state
|
2013-10-27 18:38:44 -04:00
|
|
|
// "nop \n"
|
2013-10-13 17:01:08 -04:00
|
|
|
"out %0, r16 \n" // 1 data
|
|
|
|
"cbi %0, 1 \n" // 1 falling edge on pin 5
|
|
|
|
"ld r16, z+ \n" // 2
|
2013-10-14 22:25:39 -04:00
|
|
|
"dec r19 \n" // 1 Decrement counter for brne below
|
2013-10-13 17:01:08 -04:00
|
|
|
"brne next_byte \n" // 2
|
|
|
|
|
|
|
|
// End of transmission
|
2013-10-27 18:38:44 -04:00
|
|
|
SET_1
|
|
|
|
DLY_4
|
|
|
|
|
|
|
|
SET_5 CLR_5 DLY_3
|
|
|
|
|
|
|
|
CLR_1
|
|
|
|
DLY_3
|
|
|
|
SET_1
|
|
|
|
DLY_3
|
|
|
|
CLR_1
|
|
|
|
DLY_3
|
|
|
|
SET_1
|
|
|
|
DLY_3
|
|
|
|
SET_5
|
2013-10-13 17:01:08 -04:00
|
|
|
|
|
|
|
"pop r30 \n"
|
|
|
|
"pop r31 \n"
|
|
|
|
|
|
|
|
|
|
|
|
:
|
|
|
|
: "I" (_SFR_IO_ADDR(PORTC)), "r"(buf_used/2), "z"(maplebuf)
|
|
|
|
: "r1","r16","r17","r18","r19","r20","r21"
|
|
|
|
);
|
|
|
|
|
2013-10-14 22:25:39 -04:00
|
|
|
// back to input to receive the answer
|
2013-10-26 16:26:58 -04:00
|
|
|
inputMode();
|
2013-10-13 17:01:08 -04:00
|
|
|
}
|
|
|
|
|
2013-10-27 18:38:44 -04:00
|
|
|
void maple_sendFrame1W(uint8_t cmd, uint8_t dst_addr, uint8_t src_addr, uint32_t data)
|
|
|
|
{
|
|
|
|
uint8_t tmp[4] = { data, data >> 8, data >> 16, data >> 24 };
|
|
|
|
maple_sendFrame(cmd, dst_addr, src_addr, 4, tmp);
|
|
|
|
}
|
2013-10-13 17:01:08 -04:00
|
|
|
|
2013-10-27 18:38:44 -04:00
|
|
|
/*
|
|
|
|
* data is in bus order
|
|
|
|
*/
|
|
|
|
void maple_sendFrame(uint8_t cmd, uint8_t dst_addr, uint8_t src_addr, int data_len, uint8_t *data)
|
2013-10-13 17:01:08 -04:00
|
|
|
{
|
2013-10-27 18:38:44 -04:00
|
|
|
uint8_t tmp[4 + data_len + 1]; // header, data and LRC
|
|
|
|
//uint8_t *d;
|
2013-10-13 17:01:08 -04:00
|
|
|
uint8_t lrc=0;
|
|
|
|
int i;
|
|
|
|
|
2013-10-27 18:38:44 -04:00
|
|
|
tmp[0] = data_len >> 2;
|
|
|
|
tmp[1] = src_addr;
|
|
|
|
tmp[2] = dst_addr;
|
|
|
|
tmp[3] = cmd;
|
2013-10-13 17:01:08 -04:00
|
|
|
|
2013-10-27 18:38:44 -04:00
|
|
|
if (data_len) {
|
|
|
|
memcpy(tmp + 4, data, data_len);
|
2013-10-13 17:01:08 -04:00
|
|
|
}
|
|
|
|
|
2013-10-27 18:38:44 -04:00
|
|
|
for (lrc=0, i=0; i<data_len+4; i++) {
|
|
|
|
lrc ^= tmp[i];
|
|
|
|
}
|
2013-10-13 17:01:08 -04:00
|
|
|
|
2013-10-27 18:38:44 -04:00
|
|
|
tmp[i] = lrc;
|
|
|
|
|
|
|
|
maple_sendRaw(tmp, sizeof(tmp));
|
|
|
|
}
|
2013-10-13 17:01:08 -04:00
|
|
|
|