Debian Codegraph @

mednafen/pce_fast/input.cpp

raw symbols SCIP
1 /* Mednafen - Multi-system Emulator
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18 #include "pce.h"
19 #include "input.h"
20 #include "huc.h"
21 #include "../mednafen-endian.h"
22
23 static int InputTypes[5];
24 static uint8 *data_ptr[5];
25
26 static bool AVPad6Which[5]; // Lower(8 buttons) or higher(4 buttons).
27 static bool AVPad6Enabled[5];
28
29 uint16 pce_jp_data[5];
30
31 static int64 mouse_last_meow[5];
32
33 static int32 mouse_x[5], mouse_y[5];
34 static uint16 mouse_rel[5];
35
36 uint8 pce_mouse_button[5];
37 uint8 mouse_index[5];
38
39 static uint8 sel;
40 static uint8 read_index = 0;
41
42 static void SyncSettings(void);
43
44 void PCEINPUT_SettingChanged(const char *name)
45 {
46 SyncSettings();
47 }
48
49 void PCEINPUT_Init(void)
50 {
51 SyncSettings();
52 }
53
54 void PCEINPUT_SetInput(int port, const char *type, void *ptr)
55 {
56 assert(port < 5);
57
58 if(!strcasecmp(type, "gamepad"))
59 InputTypes[port] = 1;
60 else if(!strcasecmp(type, "mouse"))
61 InputTypes[port] = 2;
62 else
63 InputTypes[port] = 0;
64 data_ptr[port] = (uint8 *)ptr;
65 }
66
67 void INPUT_Frame(void)
68 {
69 unsigned x;
70
71 for(x = 0; x < 5; x++)
72 {
73 if(InputTypes[x] == 1)
74 {
75 uint16 new_data = data_ptr[x][0] | (data_ptr[x][1] << 8);
76
77 if((new_data & 0x1000) && !(pce_jp_data[x] & 0x1000))
78 {
79 AVPad6Enabled[x] = !AVPad6Enabled[x];
80 MDFN_DispMessage("%d-button mode selected for pad %d", AVPad6Enabled[x] ? 6 : 2, x + 1);
81 }
82
83 pce_jp_data[x] = new_data;
84 }
85 else if(InputTypes[x] == 2)
86 {
87 mouse_x[x] += (int32)MDFN_de32lsb(data_ptr[x] + 0);
88 mouse_y[x] += (int32)MDFN_de32lsb(data_ptr[x] + 4);
89 pce_mouse_button[x] = *(uint8 *)(data_ptr[x] + 8);
90 }
91 }
92 }
93
94 void INPUT_FixTS(void)
95 {
96 unsigned x;
97
98 for(x = 0; x < 5; x++)
99 {
100 if(InputTypes[x] == 2)
101 mouse_last_meow[x] -= HuCPU.timestamp;
102 }
103 }
104
105 static INLINE bool CheckLM(int n)
106 {
107 if((int64)HuCPU.timestamp - mouse_last_meow[n] > 10000)
108 {
109 mouse_last_meow[n] = HuCPU.timestamp;
110
111 int32 rel_x = (int32)((0-mouse_x[n]));
112 int32 rel_y = (int32)((0-mouse_y[n]));
113
114 if(rel_x < -127) rel_x = -127;
115 if(rel_x > 127) rel_x = 127;
116 if(rel_y < -127) rel_y = -127;
117 if(rel_y > 127) rel_y = 127;
118
119 mouse_rel[n] = ((rel_x & 0xF0) >> 4) | ((rel_x & 0x0F) << 4);
120 mouse_rel[n] |= (((rel_y & 0xF0) >> 4) | ((rel_y & 0x0F) << 4)) << 8;
121
122 mouse_x[n] += (int32)(rel_x);
123 mouse_y[n] += (int32)(rel_y);
124
125 return(1);
126 }
127 return(0);
128 }
129
130 uint8 INPUT_Read(unsigned int A)
131 {
132 uint8 ret = 0xF;
133 int tmp_ri = read_index;
134
135 if(tmp_ri > 4)
136 ret ^= 0xF;
137 else
138 {
139 if(!InputTypes[tmp_ri])
140 ret ^= 0xF;
141 else if(InputTypes[tmp_ri] == 2) // Mouse
142 {
143 if(sel & 1)
144 {
145 CheckLM(tmp_ri);
146 ret ^= 0xF;
147 ret ^= mouse_rel[tmp_ri] & 0xF;
148
149 mouse_rel[tmp_ri] >>= 4;
150 }
151 else
152 {
153 if(pce_mouse_button[tmp_ri] & 1)
154 ret ^= 0x3; //pce_mouse_button[tmp_ri];
155
156 if(pce_mouse_button[tmp_ri] & 0x2)
157 ret ^= 0x8;
158 }
159 }
160 else
161 {
162 if(InputTypes[tmp_ri] == 1) // Gamepad
163 {
164 if(AVPad6Which[tmp_ri] && AVPad6Enabled[tmp_ri])
165 {
166 if(sel & 1)
167 ret ^= 0x0F;
168 else
169 ret ^= (pce_jp_data[tmp_ri] >> 8) & 0x0F;
170 }
171 else
172 {
173 if(sel & 1)
174 ret ^= (pce_jp_data[tmp_ri] >> 4) & 0x0F;
175 else
176 ret ^= pce_jp_data[tmp_ri] & 0x0F;
177 }
178 if(!(sel & 1))
179 AVPad6Which[tmp_ri] = !AVPad6Which[tmp_ri];
180 }
181 }
182 }
183
184 if(!PCE_IsCD)
185 ret |= 0x80; // Set when CDROM is not attached
186
187 //ret |= 0x40; // PC Engine if set, TG16 if clear. Let's leave it clear, PC Engine games don't seem to mind if it's clear, but TG16 games barf if it's set.
188
189 ret |= 0x30; // Always-set?
190
191 return(ret);
192 }
193
194 void INPUT_Write(unsigned int A, uint8 V)
195 {
196 if((V & 1) && !(sel & 2) && (V & 2))
197 read_index = 0;
198 else if((V & 1) && !(sel & 1))
199 {
200 if(read_index < 255)
201 read_index++;
202 }
203 sel = V & 3;
204 }
205
206 int INPUT_StateAction(StateMem *sm, int load, int data_only)
207 {
208 SFORMAT StateRegs[] =
209 {
210 // 0.8.A fix:
211 SFARRAYB(AVPad6Enabled, 5),
212 SFARRAYB(AVPad6Which, 5),
213
214 SFVARN(mouse_last_meow[0], "mlm_0"),
215 SFVARN(mouse_last_meow[1], "mlm_1"),
216 SFVARN(mouse_last_meow[2], "mlm_2"),
217 SFVARN(mouse_last_meow[3], "mlm_3"),
218 SFVARN(mouse_last_meow[4], "mlm_4"),
219
220 SFARRAY32(mouse_x, 5),
221 SFARRAY32(mouse_y, 5),
222 SFARRAY16(mouse_rel, 5),
223 SFARRAY(pce_mouse_button, 5),
224 SFARRAY(mouse_index, 5),
225 // end 0.8.A fix
226
227 SFARRAY16(pce_jp_data, 5),
228 SFVAR(sel),
229 SFVAR(read_index),
230 SFEND
231 };
232 int ret = MDFNSS_StateAction(sm, load, data_only, StateRegs, "JOY");
233
234 return(ret);
235 }
236
237 // GamepadIDII and GamepadIDII_DSR must be EXACTLY the same except for the RUN+SELECT exclusion in the latter.
238 static const InputDeviceInputInfoStruct GamepadIDII[] =
239 {
240 { "i", "I", 12, IDIT_BUTTON_CAN_RAPID, NULL },
241 { "ii", "II", 11, IDIT_BUTTON_CAN_RAPID, NULL },
242 { "select", "SELECT", 4, IDIT_BUTTON, NULL },
243 { "run", "RUN", 5, IDIT_BUTTON, NULL },
244 { "up", "UP ↑", 0, IDIT_BUTTON, "down" },
245 { "right", "RIGHT →", 3, IDIT_BUTTON, "left" },
246 { "down", "DOWN ↓", 1, IDIT_BUTTON, "up" },
247 { "left", "LEFT ←", 2, IDIT_BUTTON, "right" },
248 { "iii", "III", 10, IDIT_BUTTON, NULL },
249 { "iv", "IV", 7, IDIT_BUTTON, NULL },
250 { "v", "V", 8, IDIT_BUTTON, NULL },
251 { "vi", "VI", 9, IDIT_BUTTON, NULL },
252 { "mode_select", "2/6 Mode Select", 6, IDIT_BUTTON, NULL },
253 };
254 static const InputDeviceInputInfoStruct GamepadIDII_DSR[] =
255 {
256 { "i", "I", 12, IDIT_BUTTON_CAN_RAPID, NULL },
257 { "ii", "II", 11, IDIT_BUTTON_CAN_RAPID, NULL },
258 { "select", "SELECT", 4, IDIT_BUTTON, "run" },
259 { "run", "RUN", 5, IDIT_BUTTON, "select" },
260 { "up", "UP ↑", 0, IDIT_BUTTON, "down" },
261 { "right", "RIGHT →", 3, IDIT_BUTTON, "left" },
262 { "down", "DOWN ↓", 1, IDIT_BUTTON, "up" },
263 { "left", "LEFT ←", 2, IDIT_BUTTON, "right" },
264 { "iii", "III", 10, IDIT_BUTTON, NULL },
265 { "iv", "IV", 7, IDIT_BUTTON, NULL },
266 { "v", "V", 8, IDIT_BUTTON, NULL },
267 { "vi", "VI", 9, IDIT_BUTTON, NULL },
268 { "mode_select", "2/6 Mode Select", 6, IDIT_BUTTON, NULL },
269 };
270
271 static const InputDeviceInputInfoStruct MouseIDII[] =
272 {
273 { "x_axis", "X Axis", -1, IDIT_X_AXIS_REL },
274 { "y_axis", "Y Axis", -1, IDIT_Y_AXIS_REL },
275 { "left", "Left Button", 0, IDIT_BUTTON, NULL },
276 { "right", "Right Button", 1, IDIT_BUTTON, NULL },
277 };
278
279 // If we add more devices to this array, REMEMBER TO UPDATE the hackish array indexing in the SyncSettings() function
280 // below.
281 static InputDeviceInfoStruct InputDeviceInfo[] =
282 {
283 // None
284 {
285 "none",
286 "none",
287 NULL,
288 NULL,
289 0,
290 NULL
291 },
292
293 // Gamepad
294 {
295 "gamepad",
296 "Gamepad",
297 NULL,
298 NULL,
299 sizeof(GamepadIDII) / sizeof(InputDeviceInputInfoStruct),
300 GamepadIDII,
301 },
302
303 // Mouse
304 {
305 "mouse",
306 "Mouse",
307 NULL,
308 NULL,
309 sizeof(MouseIDII) / sizeof(InputDeviceInputInfoStruct),
310 MouseIDII,
311 },
312
313 };
314
315 static const InputPortInfoStruct PortInfo[] =
316 {
317 { "port1", "Port 1", sizeof(InputDeviceInfo) / sizeof(InputDeviceInfoStruct), InputDeviceInfo, "gamepad" },
318 { "port2", "Port 2", sizeof(InputDeviceInfo) / sizeof(InputDeviceInfoStruct), InputDeviceInfo, "gamepad" },
319 { "port3", "Port 3", sizeof(InputDeviceInfo) / sizeof(InputDeviceInfoStruct), InputDeviceInfo, "gamepad" },
320 { "port4", "Port 4", sizeof(InputDeviceInfo) / sizeof(InputDeviceInfoStruct), InputDeviceInfo, "gamepad" },
321 { "port5", "Port 5", sizeof(InputDeviceInfo) / sizeof(InputDeviceInfoStruct), InputDeviceInfo, "gamepad" },
322 };
323
324 InputInfoStruct PCEInputInfo =
325 {
326 sizeof(PortInfo) / sizeof(InputPortInfoStruct),
327 PortInfo
328 };
329
330 static void SyncSettings(void)
331 {
332 MDFNGameInfo->mouse_sensitivity = MDFN_GetSettingF("pce_fast.mouse_sensitivity");
333 InputDeviceInfo[1].IDII = MDFN_GetSettingB("pce_fast.disable_softreset") ? GamepadIDII_DSR : GamepadIDII;
334 }