Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/genplus-gx/core/input_hw/gamepad.c
2 views
1
/***************************************************************************************
2
* Genesis Plus
3
* 3-Buttons & 6-Buttons pad support
4
* Support for J-CART & 4-Way Play adapters
5
*
6
* Copyright (C) 2007-2011 Eke-Eke (Genesis Plus GX)
7
*
8
* Redistribution and use of this code or any derivative works are permitted
9
* provided that the following conditions are met:
10
*
11
* - Redistributions may not be sold, nor may they be used in a commercial
12
* product or activity.
13
*
14
* - Redistributions that are modified from the original source must include the
15
* complete source code, including the source code for all components used by a
16
* binary built from the modified sources. However, as a special exception, the
17
* source code distributed need not include anything that is normally distributed
18
* (in either source or binary form) with the major components (compiler, kernel,
19
* and so on) of the operating system on which the executable runs, unless that
20
* component itself accompanies the executable.
21
*
22
* - Redistributions must reproduce the above copyright notice, this list of
23
* conditions and the following disclaimer in the documentation and/or other
24
* materials provided with the distribution.
25
*
26
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36
* POSSIBILITY OF SUCH DAMAGE.
37
*
38
****************************************************************************************/
39
40
#include "shared.h"
41
#include "gamepad.h"
42
43
uint8 pad_index;
44
45
46
void gamepad_reset(int port)
47
{
48
/* default state (Gouketsuji Ichizoku / Power Instinct, Samurai Spirits / Samurai Shodown) */
49
gamepad[port].State = 0x40;
50
gamepad[port].Counter = 0;
51
gamepad[port].Timeout = 0;
52
53
/* reset pad index (4-WayPlay) */
54
pad_index = 0;
55
}
56
57
void gamepad_refresh(int port)
58
{
59
/* 6-buttons pad */
60
if (gamepad[port].Timeout++ > 25)
61
{
62
gamepad[port].Counter = 0;
63
gamepad[port].Timeout = 0;
64
}
65
}
66
67
INLINE unsigned char gamepad_read(int port)
68
{
69
/* bit 7 is latched, returns current TH state */
70
unsigned int data = (gamepad[port].State & 0x40) | 0x3F;
71
72
/* pad value */
73
unsigned int val = input.pad[port];
74
75
/* get current step (TH state) */
76
unsigned int step = gamepad[port].Counter | ((data >> 6) & 1);
77
78
switch (step)
79
{
80
case 1: /*** First High ***/
81
case 3: /*** Second High ***/
82
case 5: /*** Third High ***/
83
{
84
/* TH = 1 : ?1CBRLDU */
85
data &= ~(val & 0x3F);
86
break;
87
}
88
89
case 0: /*** First low ***/
90
case 2: /*** Second low ***/
91
{
92
/* TH = 0 : ?0SA00DU */
93
data &= ~(val & 0x03);
94
data &= ~((val >> 2) & 0x30);
95
data &= ~0x0C;
96
break;
97
}
98
99
/* 6buttons specific (taken from gen-hw.txt) */
100
/* A 6-button gamepad allows the extra buttons to be read based on how */
101
/* many times TH is switched from 1 to 0 (and not 0 to 1). Observe the */
102
/* following sequence */
103
/*
104
TH = 1 : ?1CBRLDU 3-button pad return value
105
TH = 0 : ?0SA00DU 3-button pad return value
106
TH = 1 : ?1CBRLDU 3-button pad return value
107
TH = 0 : ?0SA0000 D3-0 are forced to '0'
108
TH = 1 : ?1CBMXYZ Extra buttons returned in D3-0
109
TH = 0 : ?0SA1111 D3-0 are forced to '1'
110
*/
111
case 4: /*** Third Low ***/
112
{
113
/* TH = 0 : ?0SA0000 D3-0 are forced to '0'*/
114
data &= ~((val >> 2) & 0x30);
115
data &= ~0x0F;
116
break;
117
}
118
119
case 6: /*** Fourth Low ***/
120
{
121
/* TH = 0 : ?0SA1111 D3-0 are forced to '1'*/
122
data &= ~((val >> 2) & 0x30);
123
break;
124
}
125
126
case 7: /*** Fourth High ***/
127
{
128
/* TH = 1 : ?1CBMXYZ Extra buttons returned in D3-0*/
129
data &= ~(val & 0x30);
130
data &= ~((val >> 8) & 0x0F);
131
break;
132
}
133
}
134
135
return data;
136
}
137
138
INLINE void gamepad_write(int port, unsigned char data, unsigned char mask)
139
{
140
/* update bits set as output only */
141
data = (gamepad[port].State & ~mask) | (data & mask);
142
143
if (input.dev[port] == DEVICE_PAD6B)
144
{
145
/* TH=0 to TH=1 transition */
146
if (!(gamepad[port].State & 0x40) && (data & 0x40))
147
{
148
gamepad[port].Counter = (gamepad[port].Counter + 2) & 6;
149
gamepad[port].Timeout = 0;
150
}
151
}
152
153
/* update internal state */
154
gamepad[port].State = data;
155
}
156
157
158
/*--------------------------------------------------------------------------*/
159
/* Default ports handlers */
160
/*--------------------------------------------------------------------------*/
161
162
unsigned char gamepad_1_read(void)
163
{
164
return gamepad_read(0);
165
}
166
167
unsigned char gamepad_2_read(void)
168
{
169
return gamepad_read(4);
170
}
171
172
void gamepad_1_write(unsigned char data, unsigned char mask)
173
{
174
gamepad_write(0, data, mask);
175
}
176
177
void gamepad_2_write(unsigned char data, unsigned char mask)
178
{
179
gamepad_write(4, data, mask);
180
}
181
182
/*--------------------------------------------------------------------------*/
183
/* 4-WayPlay ports handler */
184
/*--------------------------------------------------------------------------*/
185
186
unsigned char wayplay_1_read(void)
187
{
188
if (pad_index < 4)
189
{
190
return gamepad_read(pad_index);
191
}
192
193
/* multitap detection */
194
return 0x70;
195
}
196
197
unsigned char wayplay_2_read(void)
198
{
199
return 0x7F;
200
}
201
202
void wayplay_1_write(unsigned char data, unsigned char mask)
203
{
204
if (pad_index < 4)
205
{
206
gamepad_write(pad_index, data, mask);
207
}
208
}
209
210
void wayplay_2_write(unsigned char data, unsigned char mask)
211
{
212
if ((mask & 0x70) == 0x70)
213
{
214
pad_index = (data & 0x70) >> 4;
215
}
216
}
217
218
219
/*--------------------------------------------------------------------------*/
220
/* J-Cart memory handlers */
221
/*--------------------------------------------------------------------------*/
222
223
unsigned int jcart_read(unsigned int address)
224
{
225
/* TH2 output read is fixed to zero (fixes Micro Machines 2) */
226
return ((gamepad_read(5) & 0x7F) | ((gamepad_read(6) & 0x3F) << 8));
227
}
228
229
void jcart_write(unsigned int address, unsigned int data)
230
{
231
gamepad_write(5, (data & 1) << 6, 0x40);
232
gamepad_write(6, (data & 1) << 6, 0x40);
233
return;
234
}
235
236