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