Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/genplus-gx32/core/input_hw/teamplayer.c
2 views
1
/***************************************************************************************
2
* Genesis Plus
3
* Team Player support
4
*
5
* Copyright (C) 2007-2014 Eke-Eke (Genesis Plus GX)
6
*
7
* Redistribution and use of this code or any derivative works are permitted
8
* provided that the following conditions are met:
9
*
10
* - Redistributions may not be sold, nor may they be used in a commercial
11
* product or activity.
12
*
13
* - Redistributions that are modified from the original source must include the
14
* complete source code, including the source code for all components used by a
15
* binary built from the modified sources. However, as a special exception, the
16
* source code distributed need not include anything that is normally distributed
17
* (in either source or binary form) with the major components (compiler, kernel,
18
* and so on) of the operating system on which the executable runs, unless that
19
* component itself accompanies the executable.
20
*
21
* - Redistributions must reproduce the above copyright notice, this list of
22
* conditions and the following disclaimer in the documentation and/or other
23
* materials provided with the distribution.
24
*
25
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
* POSSIBILITY OF SUCH DAMAGE.
36
*
37
****************************************************************************************/
38
39
#include "shared.h"
40
41
struct
42
{
43
uint8 State;
44
uint8 Counter;
45
uint8 Table[12];
46
} teamplayer[2];
47
48
49
void teamplayer_init(int port)
50
{
51
int i,padnum;
52
int index = 0;
53
54
/* this table determines which gamepad input should be returned during acquisition sequence
55
index = teamplayer read table index: 0=1st read, 1=2nd read, ...
56
table = high bits are pad index, low bits are pad input shift: 0=RLDU, 4=SABC, 8=MXYZ
57
*/
58
for (i=0; i<4; i++)
59
{
60
padnum = (4 * port) + i;
61
if (input.dev[padnum] == DEVICE_PAD3B)
62
{
63
padnum = padnum << 4;
64
teamplayer[port].Table[index++] = padnum;
65
teamplayer[port].Table[index++] = padnum | 4;
66
}
67
else
68
{
69
padnum = padnum << 4;
70
teamplayer[port].Table[index++] = padnum;
71
teamplayer[port].Table[index++] = padnum | 4;
72
teamplayer[port].Table[index++] = padnum | 8;
73
}
74
}
75
}
76
77
void teamplayer_reset(int port)
78
{
79
teamplayer[port].State = 0x60; /* TH = 1, TR = 1 */
80
teamplayer[port].Counter = 0;
81
}
82
83
INLINE unsigned int teamplayer_read(int port)
84
{
85
unsigned int counter = teamplayer[port].Counter;
86
87
/* acquisition sequence */
88
switch (counter)
89
{
90
case 0: /* initial state: xxx0011 */
91
{
92
/* TL should match TR */
93
return ((teamplayer[port].State & 0x20) >> 1) | 0x03;
94
}
95
96
case 1: /* start request: xxx1111 */
97
{
98
/* TL should match TR */
99
return ((teamplayer[port].State & 0x20) >> 1) | 0x0F;
100
}
101
102
case 2:
103
case 3: /* ack request: xxx0000 */
104
{
105
/* TL should match TR */
106
return ((teamplayer[port].State & 0x20) >> 1);
107
}
108
109
case 4:
110
case 5:
111
case 6:
112
case 7: /* PAD type: xxx0000 (3B), xxx0001 (6B) or xxx1111 (NC)*/
113
{
114
unsigned int retval = input.dev[(port << 2) + (counter - 4)];
115
116
/* TL should match TR */
117
return (((teamplayer[port].State & 0x20) >> 1) | retval);
118
}
119
120
default: /* PAD status: xxxRLDU -> xxxSACB -> xxxMXYZ */
121
{
122
unsigned int retval = 0x0F;
123
124
/* SEGA teamplayer returns successively PAD1 -> PAD2 -> PAD3 -> PAD4 inputs */
125
unsigned int padnum = teamplayer[port].Table[counter - 8] >> 4;
126
127
/* Each PAD inputs is obtained through 2 or 3 sequential reads: RLDU -> SACB -> MXYZ */
128
retval &= ~(input.pad[padnum] >> (teamplayer[port].Table[counter - 8] & 0x0F));
129
130
/* TL should match TR */
131
return (((teamplayer[port].State & 0x20) >> 1) | retval);
132
}
133
}
134
}
135
136
INLINE void teamplayer_write(int port, unsigned char data, unsigned char mask)
137
{
138
/* update bits set as output only */
139
unsigned int state = (teamplayer[port].State & ~mask) | (data & mask);
140
141
/* check if TH is HIGH */
142
if (state & 0x40)
143
{
144
/* reset counter */
145
teamplayer[port].Counter = 0;
146
}
147
148
/* TH & TR handshaking */
149
else if ((teamplayer[port].State ^ state) & 0x60)
150
{
151
/* increment counter */
152
teamplayer[port].Counter++;
153
}
154
155
/* update internal state */
156
teamplayer[port].State = state;
157
}
158
159
unsigned char teamplayer_1_read(void)
160
{
161
return teamplayer_read(0);
162
}
163
164
unsigned char teamplayer_2_read(void)
165
{
166
return teamplayer_read(1);
167
}
168
169
void teamplayer_1_write(unsigned char data, unsigned char mask)
170
{
171
teamplayer_write(0, data, mask);
172
}
173
174
void teamplayer_2_write(unsigned char data, unsigned char mask)
175
{
176
teamplayer_write(1, data, mask);
177
}
178
179