]> Shamusworld >> Repos - virtualjaguar/blob - src/joystick.cpp
Extensive changes to remove gcc 4.x warnings, general code cleanup
[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 //
8
9 #include "joystick.h"
10
11 #include <time.h>
12 #include <SDL.h>
13 #include "jaguar.h"
14 #include "video.h"
15 #include "settings.h"
16 #include "gpu.h"
17 #include "log.h"
18
19 #define BUTTON_U                0
20 #define BUTTON_D                1
21 #define BUTTON_L                2
22 #define BUTTON_R                3
23 #define BUTTON_s                4
24 #define BUTTON_7                5
25 #define BUTTON_4                6
26 #define BUTTON_1                7
27 #define BUTTON_0                8
28 #define BUTTON_8                9
29 #define BUTTON_5                10
30 #define BUTTON_2                11
31 #define BUTTON_d                12
32 #define BUTTON_9                13
33 #define BUTTON_6                14
34 #define BUTTON_3                15
35
36 #define BUTTON_A                16
37 #define BUTTON_B                17
38 #define BUTTON_C                18
39 #define BUTTON_OPTION   19
40 #define BUTTON_PAUSE    20
41
42 // Global vars
43
44 static uint8 joystick_ram[4];
45 static uint8 joypad_0_buttons[21];
46 static uint8 joypad_1_buttons[21];
47 extern bool finished;
48 extern bool showGUI;
49 bool GUIKeyHeld = false;
50 extern int start_logging;
51 int gpu_start_log = 0;
52 int op_start_log = 0;
53 int blit_start_log = 0;
54 int effect_start = 0;
55 int effect_start2 = 0, effect_start3 = 0, effect_start4 = 0, effect_start5 = 0, effect_start6 = 0;
56 bool interactiveMode = false;
57 bool iLeft, iRight, iToggle = false;
58 bool keyHeld1 = false, keyHeld2 = false, keyHeld3 = false;
59 int objectPtr = 0;
60 bool startMemLog = false;
61 extern bool doDSPDis, doGPUDis;
62
63 bool blitterSingleStep = false;
64 bool bssGo = false;
65 bool bssHeld = false;
66
67 void joystick_init(void)
68 {
69         joystick_reset();
70 }
71
72 void joystick_exec(void)
73 {
74 //      extern bool useJoystick;
75         uint8 * keystate = SDL_GetKeyState(NULL);
76         
77         memset(joypad_0_buttons, 0, 21);
78         memset(joypad_1_buttons, 0, 21);
79         gpu_start_log = 0;                                                      // Only log while key down!
80         effect_start = 0;
81         effect_start2 = effect_start3 = effect_start4 = effect_start5 = effect_start6 = 0;
82         blit_start_log = 0;
83         iLeft = iRight = false;
84
85         if ((keystate[SDLK_LALT] || keystate[SDLK_RALT]) & keystate[SDLK_RETURN])
86                 ToggleFullscreen();
87
88         // Keybindings in order of U, D, L, R, C, B, A, Op, Pa, 0-9, #, *
89 //      vjs.p1KeyBindings[0] = sdlemu_getval_int("p1k_up", SDLK_UP);
90
91         if (keystate[vjs.p1KeyBindings[0]])
92                 joypad_0_buttons[BUTTON_U] = 0x01;
93         if (keystate[vjs.p1KeyBindings[1]])
94                 joypad_0_buttons[BUTTON_D] = 0x01;
95         if (keystate[vjs.p1KeyBindings[2]])
96                 joypad_0_buttons[BUTTON_L] = 0x01;
97         if (keystate[vjs.p1KeyBindings[3]])
98                 joypad_0_buttons[BUTTON_R] = 0x01;
99         // The buttons are labelled C,B,A on the controller (going from left to right)
100         if (keystate[vjs.p1KeyBindings[4]])
101                 joypad_0_buttons[BUTTON_C] = 0x01;
102         if (keystate[vjs.p1KeyBindings[5]])
103                 joypad_0_buttons[BUTTON_B] = 0x01;
104         if (keystate[vjs.p1KeyBindings[6]])
105                 joypad_0_buttons[BUTTON_A] = 0x01;
106 //I may yet move these to O and P...
107         if (keystate[vjs.p1KeyBindings[7]])
108                 joypad_0_buttons[BUTTON_OPTION] = 0x01;
109         if (keystate[vjs.p1KeyBindings[8]])
110                 joypad_0_buttons[BUTTON_PAUSE] = 0x01;
111
112         if (keystate[vjs.p1KeyBindings[9]])
113                 joypad_0_buttons[BUTTON_0] = 0x01;
114         if (keystate[vjs.p1KeyBindings[10]])
115                 joypad_0_buttons[BUTTON_1] = 0x01;
116         if (keystate[vjs.p1KeyBindings[11]])
117                 joypad_0_buttons[BUTTON_2] = 0x01;
118         if (keystate[vjs.p1KeyBindings[12]])
119                 joypad_0_buttons[BUTTON_3] = 0x01;
120         if (keystate[vjs.p1KeyBindings[13]])
121                 joypad_0_buttons[BUTTON_4] = 0x01;
122         if (keystate[vjs.p1KeyBindings[14]])
123                 joypad_0_buttons[BUTTON_5] = 0x01;
124         if (keystate[vjs.p1KeyBindings[15]])
125                 joypad_0_buttons[BUTTON_6] = 0x01;
126         if (keystate[vjs.p1KeyBindings[16]])
127                 joypad_0_buttons[BUTTON_7] = 0x01;
128         if (keystate[vjs.p1KeyBindings[17]])
129                 joypad_0_buttons[BUTTON_8] = 0x01;
130         if (keystate[vjs.p1KeyBindings[18]])
131                 joypad_0_buttons[BUTTON_9] = 0x01;
132         if (keystate[vjs.p1KeyBindings[19]])
133                 joypad_0_buttons[BUTTON_s] = 0x01;
134         if (keystate[vjs.p1KeyBindings[20]])
135                 joypad_0_buttons[BUTTON_d] = 0x01;
136
137         extern bool debounceRunKey;
138     if (keystate[SDLK_ESCAPE])
139     {
140                 if (!debounceRunKey)
141                 finished = true;
142     }
143     else
144                 debounceRunKey = false;
145
146         if (keystate[SDLK_TAB])
147         {
148                 if (!GUIKeyHeld)
149                         showGUI = !showGUI, GUIKeyHeld = true;
150         }
151         else
152                 GUIKeyHeld = false;
153
154         if (keystate[SDLK_q])
155                 start_logging = 1;
156         if (keystate[SDLK_w])
157                 gpu_reset_stats();
158 //      if (keystate[SDLK_u])           jaguar_long_write(0xf1c384,jaguar_long_read(0xf1c384)+1);
159         if (keystate[SDLK_d])
160                 DumpMainMemory();
161         if (keystate[SDLK_l])
162                 gpu_start_log = 1;
163         if (keystate[SDLK_o])
164                 op_start_log = 1;
165         if (keystate[SDLK_b])
166                 blit_start_log = 1;
167
168         if (keystate[SDLK_1])
169                 effect_start = 1;
170         if (keystate[SDLK_2])
171                 effect_start2 = 1;
172         if (keystate[SDLK_3])
173                 effect_start3 = 1;
174         if (keystate[SDLK_4])
175                 effect_start4 = 1;
176         if (keystate[SDLK_5])
177                 effect_start5 = 1;
178         if (keystate[SDLK_6])
179                 effect_start6 = 1;
180
181         if (keystate[SDLK_i])
182                 interactiveMode = true;
183
184         if (keystate[SDLK_8] && interactiveMode)
185         {
186                 if (!keyHeld1)
187                         objectPtr--, keyHeld1 = true;
188         }
189         else
190                 keyHeld1 = false;
191
192         if (keystate[SDLK_0] && interactiveMode)
193         {
194                 if (!keyHeld2)
195                         objectPtr++, keyHeld2 = true;
196         }
197         else
198                 keyHeld2 = false;
199
200         if (keystate[SDLK_9] && interactiveMode)
201         {
202                 if (!keyHeld3)
203                         iToggle = !iToggle, keyHeld3 = true;
204         }
205         else
206                 keyHeld3 = false;
207
208         if (keystate[SDLK_e])
209                 startMemLog = true;
210         if (keystate[SDLK_r])
211                 WriteLog("\n--------> MARK!\n\n");
212         if (keystate[SDLK_t])
213                 doDSPDis = true;
214         if (keystate[SDLK_y])
215                 doGPUDis = true;
216
217         // BLITTER single step
218         if (keystate[SDLK_F5])
219                 blitterSingleStep = true;
220
221         if (keystate[SDLK_F6])
222         {
223                 if (!bssHeld)
224                 {
225                         bssHeld = true;
226                         bssGo = true;
227                 }
228         }
229         else
230                 bssHeld = false;
231
232         // Joystick support [nwagenaar]
233
234     if (vjs.useJoystick)
235     {
236                 extern SDL_Joystick * joystick;
237                 int16 x = SDL_JoystickGetAxis(joystick, 0),
238                         y = SDL_JoystickGetAxis(joystick, 1);
239         
240                 if (x > 16384)
241                         joypad_0_buttons[BUTTON_R] = 0x01;
242                 if (x < -16384)
243                         joypad_0_buttons[BUTTON_L] = 0x01;
244                 if (y > 16384)
245                         joypad_0_buttons[BUTTON_D] = 0x01;
246                 if (y < -16384)
247                         joypad_0_buttons[BUTTON_U] = 0x01;
248         
249                 if (SDL_JoystickGetButton(joystick, 0) == SDL_PRESSED)
250                         joypad_0_buttons[BUTTON_A] = 0x01;
251                 if (SDL_JoystickGetButton(joystick, 1) == SDL_PRESSED)
252                         joypad_0_buttons[BUTTON_B] = 0x01;
253                 if (SDL_JoystickGetButton(joystick, 2) == SDL_PRESSED)
254                         joypad_0_buttons[BUTTON_C] = 0x01;
255         }
256         
257         // Needed to ensure that the events queue is empty [nwagenaar]
258     SDL_PumpEvents();
259 }
260
261 void joystick_reset(void)
262 {
263         memset(joystick_ram, 0x00, 4);
264         memset(joypad_0_buttons, 0, 21);
265         memset(joypad_1_buttons, 0, 21);
266 }
267
268 void joystick_done(void)
269 {
270 }
271
272 uint8 joystick_byte_read(uint32 offset)
273 {
274 //      extern bool hardwareTypeNTSC;
275         offset &= 0x03;
276
277         if (offset == 0)
278         {
279                 uint8 data = 0x00;
280                 int pad0Index = joystick_ram[1] & 0x0F;
281                 int pad1Index = (joystick_ram[1] >> 4) & 0x0F;
282                 
283 // This is bad--we're assuming that a bit is set in the last case. Might not be so!
284                 if (!(pad0Index & 0x01)) 
285                         pad0Index = 0;
286                 else if (!(pad0Index & 0x02)) 
287                         pad0Index = 1;
288                 else if (!(pad0Index & 0x04)) 
289                         pad0Index = 2;
290                 else 
291                         pad0Index = 3;
292                 
293                 if (!(pad1Index & 0x01)) 
294                         pad1Index = 0;
295                 else if (!(pad1Index & 0x02)) 
296                         pad1Index = 1;
297                 else if (!(pad1Index & 0x04)) 
298                         pad1Index = 2;
299                 else
300                         pad1Index = 3;
301
302                 if (joypad_0_buttons[(pad0Index << 2) + 0])     data |= 0x01;
303                 if (joypad_0_buttons[(pad0Index << 2) + 1]) data |= 0x02;
304                 if (joypad_0_buttons[(pad0Index << 2) + 2]) data |= 0x04;
305                 if (joypad_0_buttons[(pad0Index << 2) + 3]) data |= 0x08;
306                 if (joypad_1_buttons[(pad1Index << 2) + 0]) data |= 0x10;
307                 if (joypad_1_buttons[(pad1Index << 2) + 1]) data |= 0x20;
308                 if (joypad_1_buttons[(pad1Index << 2) + 2]) data |= 0x40;
309                 if (joypad_1_buttons[(pad1Index << 2) + 3]) data |= 0x80;
310
311                 return ~data;
312         }
313         else if (offset == 3)
314         {
315                 uint8 data = 0x2F | (vjs.hardwareTypeNTSC ? 0x10 : 0x00);
316                 int pad0Index = joystick_ram[1] & 0x0F;
317 //unused                int pad1Index = (joystick_ram[1] >> 4) & 0x0F;
318                 
319                 if (!(pad0Index & 0x01))
320                 {
321                         if (joypad_0_buttons[BUTTON_PAUSE])
322                                 data ^= 0x01;
323                         if (joypad_0_buttons[BUTTON_A])
324                                 data ^= 0x02;
325                 }
326                 else if (!(pad0Index & 0x02))
327                 {
328                         if (joypad_0_buttons[BUTTON_B])
329                                 data ^= 0x02;
330                 }
331                 else if (!(pad0Index & 0x04))
332                 {
333                         if (joypad_0_buttons[BUTTON_C])
334                                 data ^= 0x02;
335                 }
336                 else
337                 {
338                         if (joypad_0_buttons[BUTTON_OPTION])
339                                 data ^= 0x02;
340                 }               
341                 return data;
342         }
343
344         return joystick_ram[offset];
345 }
346
347 uint16 joystick_word_read(uint32 offset)
348 {
349         return ((uint16)joystick_byte_read((offset+0)&0x03) << 8) | joystick_byte_read((offset+1)&0x03);
350 }
351
352 void joystick_byte_write(uint32 offset, uint8 data)
353 {
354         joystick_ram[offset&0x03] = data;
355 }
356
357 void joystick_word_write(uint32 offset, uint16 data)
358 {
359         offset &= 0x03;
360         joystick_ram[offset+0] = (data >> 8) & 0xFF;
361         joystick_ram[offset+1] = data & 0xFF;
362 }