/***************************************************************************************1* Genesis Plus2* Team Player support3*4* Copyright (C) 2007-2014 Eke-Eke (Genesis Plus GX)5*6* Redistribution and use of this code or any derivative works are permitted7* provided that the following conditions are met:8*9* - Redistributions may not be sold, nor may they be used in a commercial10* product or activity.11*12* - Redistributions that are modified from the original source must include the13* complete source code, including the source code for all components used by a14* binary built from the modified sources. However, as a special exception, the15* source code distributed need not include anything that is normally distributed16* (in either source or binary form) with the major components (compiler, kernel,17* and so on) of the operating system on which the executable runs, unless that18* component itself accompanies the executable.19*20* - Redistributions must reproduce the above copyright notice, this list of21* conditions and the following disclaimer in the documentation and/or other22* materials provided with the distribution.23*24* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"25* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE26* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE27* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE28* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR29* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF30* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN32* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)33* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE34* POSSIBILITY OF SUCH DAMAGE.35*36****************************************************************************************/3738#include "shared.h"3940struct41{42uint8 State;43uint8 Counter;44uint8 Table[12];45} teamplayer[2];464748void teamplayer_init(int port)49{50int i,padnum;51int index = 0;5253/* this table determines which gamepad input should be returned during acquisition sequence54index = teamplayer read table index: 0=1st read, 1=2nd read, ...55table = high bits are pad index, low bits are pad input shift: 0=RLDU, 4=SABC, 8=MXYZ56*/57for (i=0; i<4; i++)58{59padnum = (4 * port) + i;60if (input.dev[padnum] == DEVICE_PAD3B)61{62padnum = padnum << 4;63teamplayer[port].Table[index++] = padnum;64teamplayer[port].Table[index++] = padnum | 4;65}66else67{68padnum = padnum << 4;69teamplayer[port].Table[index++] = padnum;70teamplayer[port].Table[index++] = padnum | 4;71teamplayer[port].Table[index++] = padnum | 8;72}73}74}7576void teamplayer_reset(int port)77{78teamplayer[port].State = 0x60; /* TH = 1, TR = 1 */79teamplayer[port].Counter = 0;80}8182INLINE unsigned int teamplayer_read(int port)83{84unsigned int counter = teamplayer[port].Counter;8586/* acquisition sequence */87switch (counter)88{89case 0: /* initial state: xxx0011 */90{91/* TL should match TR */92return ((teamplayer[port].State & 0x20) >> 1) | 0x03;93}9495case 1: /* start request: xxx1111 */96{97/* TL should match TR */98return ((teamplayer[port].State & 0x20) >> 1) | 0x0F;99}100101case 2:102case 3: /* ack request: xxx0000 */103{104/* TL should match TR */105return ((teamplayer[port].State & 0x20) >> 1);106}107108case 4:109case 5:110case 6:111case 7: /* PAD type: xxx0000 (3B), xxx0001 (6B) or xxx1111 (NC)*/112{113unsigned int retval = input.dev[(port << 2) + (counter - 4)];114115/* TL should match TR */116return (((teamplayer[port].State & 0x20) >> 1) | retval);117}118119default: /* PAD status: xxxRLDU -> xxxSACB -> xxxMXYZ */120{121unsigned int retval = 0x0F;122123/* SEGA teamplayer returns successively PAD1 -> PAD2 -> PAD3 -> PAD4 inputs */124unsigned int padnum = teamplayer[port].Table[counter - 8] >> 4;125126/* Each PAD inputs is obtained through 2 or 3 sequential reads: RLDU -> SACB -> MXYZ */127retval &= ~(input.pad[padnum] >> (teamplayer[port].Table[counter - 8] & 0x0F));128129/* TL should match TR */130return (((teamplayer[port].State & 0x20) >> 1) | retval);131}132}133}134135INLINE void teamplayer_write(int port, unsigned char data, unsigned char mask)136{137/* update bits set as output only */138unsigned int state = (teamplayer[port].State & ~mask) | (data & mask);139140/* check if TH is HIGH */141if (state & 0x40)142{143/* reset counter */144teamplayer[port].Counter = 0;145}146147/* TH & TR handshaking */148else if ((teamplayer[port].State ^ state) & 0x60)149{150/* increment counter */151teamplayer[port].Counter++;152}153154/* update internal state */155teamplayer[port].State = state;156}157158unsigned char teamplayer_1_read(void)159{160return teamplayer_read(0);161}162163unsigned char teamplayer_2_read(void)164{165return teamplayer_read(1);166}167168void teamplayer_1_write(unsigned char data, unsigned char mask)169{170teamplayer_write(0, data, mask);171}172173void teamplayer_2_write(unsigned char data, unsigned char mask)174{175teamplayer_write(1, data, mask);176}177178179