Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/genplus-gx/core/input_hw/xe_a1p.c
2 views
1
/***************************************************************************************
2
* Genesis Plus
3
* XE-A1P analog controller support
4
*
5
* Copyright (C) 2011-2013 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 Latency;
46
} xe_a1p[2];
47
48
void xe_a1p_reset(int index)
49
{
50
input.analog[index][0] = 128;
51
input.analog[index][1] = 128;
52
input.analog[index+1][0] = 128;
53
index >>= 2;
54
xe_a1p[index].State = 0x40;
55
xe_a1p[index].Counter = 0;
56
xe_a1p[index].Latency = 0;
57
}
58
59
INLINE unsigned char xe_a1p_read(int index)
60
{
61
unsigned int temp = 0x40;
62
unsigned int port = index << 2;
63
64
/* Left Stick X & Y analog values (bidirectional) */
65
int x = input.analog[port][0];
66
int y = input.analog[port][1];
67
68
/* Right Stick X or Y value (unidirectional) */
69
int z = input.analog[port+1][0];
70
71
/* Buttons status (active low) */
72
uint16 pad = ~input.pad[port];
73
74
/* Current internal cycle (0-7) */
75
unsigned int cycle = xe_a1p[index].Counter & 7;
76
77
/* Current 4-bit data cycle */
78
/* There are eight internal data cycle for each 5 acquisition sequence */
79
/* First 4 return the same 4-bit data, next 4 return next 4-bit data */
80
switch (xe_a1p[index].Counter >> 2)
81
{
82
case 0:
83
temp |= ((pad >> 8) & 0x0F); /* E1 E2 Start Select */
84
break;
85
case 1:
86
temp |= ((pad >> 4) & 0x0F); /* A B C D */
87
break;
88
case 2:
89
temp |= ((x >> 4) & 0x0F);
90
break;
91
case 3:
92
temp |= ((y >> 4) & 0x0F);
93
break;
94
case 4:
95
break;
96
case 5:
97
temp |= ((z >> 4) & 0x0F);
98
break;
99
case 6:
100
temp |= (x & 0x0F);
101
break;
102
case 7:
103
temp |= (y & 0x0F);
104
break;
105
case 8:
106
break;
107
case 9:
108
temp |= (z & 0x0F);
109
break;
110
}
111
112
/* TL indicates which part of data is returned (0=1st part, 1=2nd part) */
113
temp |= ((cycle & 4) << 2);
114
115
/* TR indicates if data is ready (0=ready, 1=not ready) */
116
/* Fastest One input routine actually expects this bit to switch between 0 & 1 */
117
/* so we make the first read of a data cycle return 1 then 0 for remaining reads */
118
temp |= (!(cycle & 3) << 5);
119
120
/* Automatically increment data cycle on each read (within current acquisition sequence) */
121
cycle = (cycle + 1) & 7;
122
123
/* Update internal cycle counter */
124
xe_a1p[index].Counter = (xe_a1p[index].Counter & ~7) | cycle;
125
126
/* Update internal latency on each read */
127
xe_a1p[index].Latency++;
128
129
return temp;
130
}
131
132
INLINE void xe_a1p_write(int index, unsigned char data, unsigned char mask)
133
{
134
/* update bits set as output only */
135
data = (xe_a1p[index].State & ~mask) | (data & mask);
136
137
/* look for TH 1->0 transitions */
138
if (!(data & 0x40) && (xe_a1p[index].State & 0x40))
139
{
140
/* reset acquisition cycle */
141
xe_a1p[index].Latency = xe_a1p[index].Counter = 0;
142
}
143
else
144
{
145
/* some games immediately write new data to TH */
146
/* so we make sure first sequence has actually been handled */
147
if (xe_a1p[index].Latency > 2)
148
{
149
/* next acquisition sequence */
150
xe_a1p[index].Counter = (xe_a1p[index].Counter & ~7) + 8;
151
152
/* 5 sequence max with 8 cycles each */
153
if (xe_a1p[index].Counter > 32)
154
{
155
xe_a1p[index].Counter = 32;
156
}
157
}
158
}
159
160
/* update internal state */
161
xe_a1p[index].State = data;
162
}
163
164
unsigned char xe_a1p_1_read(void)
165
{
166
return xe_a1p_read(0);
167
}
168
169
unsigned char xe_a1p_2_read(void)
170
{
171
return xe_a1p_read(1);
172
}
173
174
void xe_a1p_1_write(unsigned char data, unsigned char mask)
175
{
176
xe_a1p_write(0, data, mask);
177
}
178
179
void xe_a1p_2_write(unsigned char data, unsigned char mask)
180
{
181
xe_a1p_write(1, data, mask);
182
}
183
184