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