]> Shamusworld >> Repos - virtualjaguar/blob - src/joystick.cpp
14b5ff784a8f737cd49b67daee9c06c36d996595
[virtualjaguar] / src / joystick.cpp
1 //
2 // Joystick handler
3 //
4 // by cal2
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Cleanups/fixes by James Hammons
7 // (C) 2010 Underground Software
8 //
9 // JLH = James Hammons <jlhamm@acm.org>
10 //
11 // Who  When        What
12 // ---  ----------  -------------------------------------------------------------
13 // JLH  01/16/2010  Created this log ;-)
14 //
15
16 #include "joystick.h"
17
18 #include <SDL.h>
19 #include <time.h>
20 #include "gpu.h"
21 #include "jaguar.h"
22 #include "log.h"
23 #include "settings.h"
24
25 // Global vars
26
27 static uint8 joystick_ram[4];
28 uint8 joypad_0_buttons[21];
29 uint8 joypad_1_buttons[21];
30
31 bool keyBuffer[21];
32
33 SDL_Joystick * joystick1;
34
35 //extern bool finished;
36 ////extern bool showGUI;
37 bool GUIKeyHeld = false;
38 extern int start_logging;
39 int gpu_start_log = 0;
40 int op_start_log = 0;
41 int blit_start_log = 0;
42 int effect_start = 0;
43 int effect_start2 = 0, effect_start3 = 0, effect_start4 = 0, effect_start5 = 0, effect_start6 = 0;
44 bool interactiveMode = false;
45 bool iLeft, iRight, iToggle = false;
46 bool keyHeld1 = false, keyHeld2 = false, keyHeld3 = false;
47 int objectPtr = 0;
48 bool startMemLog = false;
49 extern bool doDSPDis, doGPUDis;
50
51 bool blitterSingleStep = false;
52 bool bssGo = false;
53 bool bssHeld = false;
54
55 void JoystickInit(void)
56 {
57         JoystickReset();
58 }
59
60 void JoystickExec(void)
61 {
62 //      uint8 * keystate = SDL_GetKeyState(NULL);
63
64 //      memset(joypad_0_buttons, 0, 21);
65 //      memset(joypad_1_buttons, 0, 21);
66         gpu_start_log = 0;                                                      // Only log while key down!
67         effect_start = 0;
68         effect_start2 = effect_start3 = effect_start4 = effect_start5 = effect_start6 = 0;
69         blit_start_log = 0;
70         iLeft = iRight = false;
71
72 #if 0
73         if ((keystate[SDLK_LALT] || keystate[SDLK_RALT]) & keystate[SDLK_RETURN])
74                 ToggleFullscreen();
75
76         // Keybindings in order of U, D, L, R, C, B, A, Op, Pa, 0-9, #, *
77 //      vjs.p1KeyBindings[0] = sdlemu_getval_int("p1k_up", SDLK_UP);
78
79 #if 0
80         if (keystate[vjs.p1KeyBindings[0]])
81                 joypad_0_buttons[BUTTON_U] = 0x01;
82         if (keystate[vjs.p1KeyBindings[1]])
83                 joypad_0_buttons[BUTTON_D] = 0x01;
84         if (keystate[vjs.p1KeyBindings[2]])
85                 joypad_0_buttons[BUTTON_L] = 0x01;
86         if (keystate[vjs.p1KeyBindings[3]])
87                 joypad_0_buttons[BUTTON_R] = 0x01;
88         // The buttons are labelled C,B,A on the controller (going from left to right)
89         if (keystate[vjs.p1KeyBindings[4]])
90                 joypad_0_buttons[BUTTON_C] = 0x01;
91         if (keystate[vjs.p1KeyBindings[5]])
92                 joypad_0_buttons[BUTTON_B] = 0x01;
93         if (keystate[vjs.p1KeyBindings[6]])
94                 joypad_0_buttons[BUTTON_A] = 0x01;
95 //I may yet move these to O and P...
96         if (keystate[vjs.p1KeyBindings[7]])
97                 joypad_0_buttons[BUTTON_OPTION] = 0x01;
98         if (keystate[vjs.p1KeyBindings[8]])
99                 joypad_0_buttons[BUTTON_PAUSE] = 0x01;
100
101         if (keystate[vjs.p1KeyBindings[9]])
102                 joypad_0_buttons[BUTTON_0] = 0x01;
103         if (keystate[vjs.p1KeyBindings[10]])
104                 joypad_0_buttons[BUTTON_1] = 0x01;
105         if (keystate[vjs.p1KeyBindings[11]])
106                 joypad_0_buttons[BUTTON_2] = 0x01;
107         if (keystate[vjs.p1KeyBindings[12]])
108                 joypad_0_buttons[BUTTON_3] = 0x01;
109         if (keystate[vjs.p1KeyBindings[13]])
110                 joypad_0_buttons[BUTTON_4] = 0x01;
111         if (keystate[vjs.p1KeyBindings[14]])
112                 joypad_0_buttons[BUTTON_5] = 0x01;
113         if (keystate[vjs.p1KeyBindings[15]])
114                 joypad_0_buttons[BUTTON_6] = 0x01;
115         if (keystate[vjs.p1KeyBindings[16]])
116                 joypad_0_buttons[BUTTON_7] = 0x01;
117         if (keystate[vjs.p1KeyBindings[17]])
118                 joypad_0_buttons[BUTTON_8] = 0x01;
119         if (keystate[vjs.p1KeyBindings[18]])
120                 joypad_0_buttons[BUTTON_9] = 0x01;
121         if (keystate[vjs.p1KeyBindings[19]])
122                 joypad_0_buttons[BUTTON_s] = 0x01;
123         if (keystate[vjs.p1KeyBindings[20]])
124                 joypad_0_buttons[BUTTON_d] = 0x01;
125 #else
126         if (keyBuffer[0])
127                 joypad_0_buttons[BUTTON_U] = 0x01;
128         if (keyBuffer[1])
129                 joypad_0_buttons[BUTTON_D] = 0x01;
130         if (keyBuffer[2])
131                 joypad_0_buttons[BUTTON_L] = 0x01;
132         if (keyBuffer[3])
133                 joypad_0_buttons[BUTTON_R] = 0x01;
134         // The buttons are labelled C,B,A on the controller (going from left to right)
135         if (keyBuffer[4])
136                 joypad_0_buttons[BUTTON_C] = 0x01;
137         if (keyBuffer[5])
138                 joypad_0_buttons[BUTTON_B] = 0x01;
139         if (keyBuffer[6])
140                 joypad_0_buttons[BUTTON_A] = 0x01;
141 //I may yet move these to O and P...
142         if (keyBuffer[7])
143                 joypad_0_buttons[BUTTON_OPTION] = 0x01;
144         if (keyBuffer[8])
145                 joypad_0_buttons[BUTTON_PAUSE] = 0x01;
146
147         if (keyBuffer[9])
148                 joypad_0_buttons[BUTTON_0] = 0x01;
149         if (keyBuffer[10])
150                 joypad_0_buttons[BUTTON_1] = 0x01;
151         if (keyBuffer[11])
152                 joypad_0_buttons[BUTTON_2] = 0x01;
153         if (keyBuffer[12])
154                 joypad_0_buttons[BUTTON_3] = 0x01;
155         if (keyBuffer[13])
156                 joypad_0_buttons[BUTTON_4] = 0x01;
157         if (keyBuffer[14])
158                 joypad_0_buttons[BUTTON_5] = 0x01;
159         if (keyBuffer[15])
160                 joypad_0_buttons[BUTTON_6] = 0x01;
161         if (keyBuffer[16])
162                 joypad_0_buttons[BUTTON_7] = 0x01;
163         if (keyBuffer[17])
164                 joypad_0_buttons[BUTTON_8] = 0x01;
165         if (keyBuffer[18])
166                 joypad_0_buttons[BUTTON_9] = 0x01;
167         if (keyBuffer[19])
168                 joypad_0_buttons[BUTTON_s] = 0x01;
169         if (keyBuffer[20])
170                 joypad_0_buttons[BUTTON_d] = 0x01;
171 #endif
172
173 #warning "!!! FIX !!! (debounceRunKey)"
174 //      extern bool debounceRunKey;
175         bool debounceRunKey;
176     if (keystate[SDLK_ESCAPE])
177     {
178                 if (!debounceRunKey)
179 #warning "!!! FIX !!! (finished = true)"
180 ;//             finished = true;
181     }
182     else
183                 debounceRunKey = false;
184
185         if (keystate[SDLK_TAB])
186         {
187                 if (!GUIKeyHeld)
188 #warning "!!! FIX !!! (showGUI = !showGUI, ...)"
189 ;//                     showGUI = !showGUI, GUIKeyHeld = true;
190         }
191         else
192                 GUIKeyHeld = false;
193
194         if (keystate[SDLK_q])
195                 start_logging = 1;
196         if (keystate[SDLK_w])
197                 GPUResetStats();
198 //      if (keystate[SDLK_u])           jaguar_long_write(0xf1c384,jaguar_long_read(0xf1c384)+1);
199         if (keystate[SDLK_d])
200                 DumpMainMemory();
201         if (keystate[SDLK_l])
202                 gpu_start_log = 1;
203         if (keystate[SDLK_o])
204                 op_start_log = 1;
205         if (keystate[SDLK_b])
206                 blit_start_log = 1;
207
208         if (keystate[SDLK_1])
209                 effect_start = 1;
210         if (keystate[SDLK_2])
211                 effect_start2 = 1;
212         if (keystate[SDLK_3])
213                 effect_start3 = 1;
214         if (keystate[SDLK_4])
215                 effect_start4 = 1;
216         if (keystate[SDLK_5])
217                 effect_start5 = 1;
218         if (keystate[SDLK_6])
219                 effect_start6 = 1;
220
221         if (keystate[SDLK_i])
222                 interactiveMode = true;
223
224         if (keystate[SDLK_8] && interactiveMode)
225         {
226                 if (!keyHeld1)
227                         objectPtr--, keyHeld1 = true;
228         }
229         else
230                 keyHeld1 = false;
231
232         if (keystate[SDLK_0] && interactiveMode)
233         {
234                 if (!keyHeld2)
235                         objectPtr++, keyHeld2 = true;
236         }
237         else
238                 keyHeld2 = false;
239
240         if (keystate[SDLK_9] && interactiveMode)
241         {
242                 if (!keyHeld3)
243                         iToggle = !iToggle, keyHeld3 = true;
244         }
245         else
246                 keyHeld3 = false;
247
248         if (keystate[SDLK_e])
249                 startMemLog = true;
250         if (keystate[SDLK_r])
251                 WriteLog("\n--------> MARK!\n\n");
252         if (keystate[SDLK_t])
253                 doDSPDis = true;
254         if (keystate[SDLK_y])
255                 doGPUDis = true;
256
257         // BLITTER single step
258         if (keystate[SDLK_F5])
259                 blitterSingleStep = true;
260
261         if (keystate[SDLK_F6])
262         {
263                 if (!bssHeld)
264                 {
265                         bssHeld = true;
266                         bssGo = true;
267                 }
268         }
269         else
270                 bssHeld = false;
271 #endif
272         // We need to ensure that illegal combinations are not possible.
273         // So, we do a simple minded way here...
274         // It would be better to check to see which one was pressed last
275         // and discard that one, but for now...
276 //This didn't work... Was still able to do bad combination.
277 //It's because the GUI is changing this *after* we fix it here.
278 #if 0
279         if (joypad_0_buttons[BUTTON_R] && joypad_0_buttons[BUTTON_L])
280                 joypad_0_buttons[BUTTON_L] = 0;
281
282         if (joypad_0_buttons[BUTTON_U] && joypad_0_buttons[BUTTON_D])
283                 joypad_0_buttons[BUTTON_D] = 0;
284 #endif
285
286         // Joystick support [nwagenaar]
287
288     if (vjs.useJoystick)
289     {
290                 extern SDL_Joystick * joystick1;
291                 int16 x = SDL_JoystickGetAxis(joystick1, 0),
292                         y = SDL_JoystickGetAxis(joystick1, 1);
293
294                 if (x > 16384)
295                         joypad_0_buttons[BUTTON_R] = 0x01;
296                 if (x < -16384)
297                         joypad_0_buttons[BUTTON_L] = 0x01;
298                 if (y > 16384)
299                         joypad_0_buttons[BUTTON_D] = 0x01;
300                 if (y < -16384)
301                         joypad_0_buttons[BUTTON_U] = 0x01;
302
303                 if (SDL_JoystickGetButton(joystick1, 0) == SDL_PRESSED)
304                         joypad_0_buttons[BUTTON_A] = 0x01;
305                 if (SDL_JoystickGetButton(joystick1, 1) == SDL_PRESSED)
306                         joypad_0_buttons[BUTTON_B] = 0x01;
307                 if (SDL_JoystickGetButton(joystick1, 2) == SDL_PRESSED)
308                         joypad_0_buttons[BUTTON_C] = 0x01;
309         }
310
311         // Needed to ensure that the events queue is empty [nwagenaar]
312     SDL_PumpEvents();
313 }
314
315 void JoystickReset(void)
316 {
317         memset(joystick_ram, 0x00, 4);
318         memset(joypad_0_buttons, 0, 21);
319         memset(joypad_1_buttons, 0, 21);
320 }
321
322 void JoystickDone(void)
323 {
324 }
325
326 uint8 JoystickReadByte(uint32 offset)
327 {
328 // For now, until we can fix the 2nd controller... :-P
329 //memset(joypad_1_buttons, 0, 21);
330
331 #warning "No bounds checking done in JoystickReadByte!"
332 //      extern bool hardwareTypeNTSC;
333         offset &= 0x03;
334
335         if (offset == 0)
336         {
337                 uint8 data = 0x00;
338                 int pad0Index = joystick_ram[1] & 0x0F;
339                 int pad1Index = (joystick_ram[1] >> 4) & 0x0F;
340
341 // This is bad--we're assuming that a bit is set in the last case. Might not be so!
342 // NOTE: values $7, B, D, & E are only legal ones for pad 0, (rows 3 to 0, in both cases)
343 //              $E, D, B, & 7 are only legal ones for pad 1
344 //       So the following code is WRONG! (now fixed! ;-)
345 // Also: we should explicitly check for those bit patterns, as other patterns
346 // are legal and yield other controllers... !!! FIX !!!
347 #warning "!!! Need to explicitly check for the proper bit combinations! !!!"
348
349                 if (!(pad0Index & 0x01))
350                         pad0Index = 0;
351                 else if (!(pad0Index & 0x02))
352                         pad0Index = 1;
353                 else if (!(pad0Index & 0x04))
354                         pad0Index = 2;
355                 else if (!(pad0Index & 0x08))
356                         pad0Index = 3;
357
358                 if (!(pad1Index & 0x01))
359                         pad1Index = 3;
360                 else if (!(pad1Index & 0x02))
361                         pad1Index = 2;
362                 else if (!(pad1Index & 0x04))
363                         pad1Index = 1;
364                 else if (!(pad1Index & 0x08))
365                         pad1Index = 0;
366
367                 if (joypad_0_buttons[(pad0Index << 2) + 0])     data |= 0x01;
368                 if (joypad_0_buttons[(pad0Index << 2) + 1]) data |= 0x02;
369                 if (joypad_0_buttons[(pad0Index << 2) + 2]) data |= 0x04;
370                 if (joypad_0_buttons[(pad0Index << 2) + 3]) data |= 0x08;
371                 if (joypad_1_buttons[(pad1Index << 2) + 0]) data |= 0x10;
372                 if (joypad_1_buttons[(pad1Index << 2) + 1]) data |= 0x20;
373                 if (joypad_1_buttons[(pad1Index << 2) + 2]) data |= 0x40;
374                 if (joypad_1_buttons[(pad1Index << 2) + 3]) data |= 0x80;
375
376                 return ~data;
377         }
378         else if (offset == 3)
379         {
380                 // Hardware ID returns NTSC/PAL identification bit here
381                 uint8 data = 0x2F | (vjs.hardwareTypeNTSC ? 0x10 : 0x00);
382                 int pad0Index = joystick_ram[1] & 0x0F;
383                 int pad1Index = (joystick_ram[1] >> 4) & 0x0F;
384
385 //This is more stuff to add to the button reading, as the preceeding only
386 //yields 16 buttons...
387 #warning "!!! This reports TeamTap incorrectly when PAUSE pressed on controller #1 or #2 !!!"
388                 if (!(pad0Index & 0x01))
389                 {
390                         if (joypad_0_buttons[BUTTON_PAUSE])
391                                 data ^= 0x01;
392                         if (joypad_0_buttons[BUTTON_A])
393                                 data ^= 0x02;
394                 }
395                 else if (!(pad0Index & 0x02))
396                 {
397                         if (joypad_0_buttons[BUTTON_B])
398                                 data ^= 0x02;
399                 }
400                 else if (!(pad0Index & 0x04))
401                 {
402                         if (joypad_0_buttons[BUTTON_C])
403                                 data ^= 0x02;
404                 }
405                 else if (!(pad0Index & 0x08))
406                 {
407                         if (joypad_0_buttons[BUTTON_OPTION])
408                                 data ^= 0x02;
409                 }
410
411                 if (!(pad1Index & 0x08))
412                 {
413                         if (joypad_1_buttons[BUTTON_PAUSE])
414                                 data ^= 0x04;
415                         if (joypad_1_buttons[BUTTON_A])
416                                 data ^= 0x08;
417                 }
418                 else if (!(pad1Index & 0x04))
419                 {
420                         if (joypad_1_buttons[BUTTON_B])
421                                 data ^= 0x08;
422                 }
423                 else if (!(pad1Index & 0x02))
424                 {
425                         if (joypad_1_buttons[BUTTON_C])
426                                 data ^= 0x08;
427                 }
428                 else if (!(pad1Index & 0x01))
429                 {
430                         if (joypad_1_buttons[BUTTON_OPTION])
431                                 data ^= 0x08;
432                 }
433
434                 return data;
435         }
436
437         return joystick_ram[offset];
438 }
439
440 uint16 JoystickReadWord(uint32 offset)
441 {
442         return ((uint16)JoystickReadByte((offset + 0) & 0x03) << 8) | JoystickReadByte((offset + 1) & 0x03);
443 }
444
445 void JoystickWriteByte(uint32 offset, uint8 data)
446 {
447         joystick_ram[offset & 0x03] = data;
448 }
449
450 void JoystickWriteWord(uint32 offset, uint16 data)
451 {
452 #warning "No bounds checking done for JoystickWriteWord!"
453         offset &= 0x03;
454         joystick_ram[offset + 0] = (data >> 8) & 0xFF;
455         joystick_ram[offset + 1] = data & 0xFF;
456 }