]> Shamusworld >> Repos - virtualjaguar/blob - src/joystick.cpp
aa81c0ff3ebc107412ba38e365a80dd6735bd759
[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 <SDL.h>
12 #include <time.h>
13 #include "gpu.h"
14 #include "gui.h"
15 #include "jaguar.h"
16 #include "log.h"
17 #include "settings.h"
18 #include "video.h"
19
20 #define BUTTON_U                0
21 #define BUTTON_D                1
22 #define BUTTON_L                2
23 #define BUTTON_R                3
24 #define BUTTON_s                4
25 #define BUTTON_7                5
26 #define BUTTON_4                6
27 #define BUTTON_1                7
28 #define BUTTON_0                8
29 #define BUTTON_8                9
30 #define BUTTON_5                10
31 #define BUTTON_2                11
32 #define BUTTON_d                12
33 #define BUTTON_9                13
34 #define BUTTON_6                14
35 #define BUTTON_3                15
36
37 #define BUTTON_A                16
38 #define BUTTON_B                17
39 #define BUTTON_C                18
40 #define BUTTON_OPTION   19
41 #define BUTTON_PAUSE    20
42
43 // Global vars
44
45 static uint8 joystick_ram[4];
46 static uint8 joypad_0_buttons[21];
47 static uint8 joypad_1_buttons[21];
48 //extern bool finished;
49 ////extern bool showGUI;
50 bool GUIKeyHeld = false;
51 extern int start_logging;
52 int gpu_start_log = 0;
53 int op_start_log = 0;
54 int blit_start_log = 0;
55 int effect_start = 0;
56 int effect_start2 = 0, effect_start3 = 0, effect_start4 = 0, effect_start5 = 0, effect_start6 = 0;
57 bool interactiveMode = false;
58 bool iLeft, iRight, iToggle = false;
59 bool keyHeld1 = false, keyHeld2 = false, keyHeld3 = false;
60 int objectPtr = 0;
61 bool startMemLog = false;
62 extern bool doDSPDis, doGPUDis;
63
64 bool blitterSingleStep = false;
65 bool bssGo = false;
66 bool bssHeld = false;
67
68 void JoystickInit(void)
69 {
70         JoystickReset();
71 }
72
73 void JoystickExec(void)
74 {
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                 GPUResetStats();
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 JoystickReset(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 JoystickDone(void)
269 {
270 }
271
272 uint8 JoystickReadByte(uint32 offset)
273 {
274 #warning No bounds checking done in JoystickReadByte!
275 //      extern bool hardwareTypeNTSC;
276         offset &= 0x03;
277
278         if (offset == 0)
279         {
280                 uint8 data = 0x00;
281                 int pad0Index = joystick_ram[1] & 0x0F;
282                 int pad1Index = (joystick_ram[1] >> 4) & 0x0F;
283
284 // This is bad--we're assuming that a bit is set in the last case. Might not be so!
285                 if (!(pad0Index & 0x01))
286                         pad0Index = 0;
287                 else if (!(pad0Index & 0x02))
288                         pad0Index = 1;
289                 else if (!(pad0Index & 0x04))
290                         pad0Index = 2;
291                 else
292                         pad0Index = 3;
293
294                 if (!(pad1Index & 0x01))
295                         pad1Index = 0;
296                 else if (!(pad1Index & 0x02))
297                         pad1Index = 1;
298                 else if (!(pad1Index & 0x04))
299                         pad1Index = 2;
300                 else
301                         pad1Index = 3;
302
303                 if (joypad_0_buttons[(pad0Index << 2) + 0])     data |= 0x01;
304                 if (joypad_0_buttons[(pad0Index << 2) + 1]) data |= 0x02;
305                 if (joypad_0_buttons[(pad0Index << 2) + 2]) data |= 0x04;
306                 if (joypad_0_buttons[(pad0Index << 2) + 3]) data |= 0x08;
307                 if (joypad_1_buttons[(pad1Index << 2) + 0]) data |= 0x10;
308                 if (joypad_1_buttons[(pad1Index << 2) + 1]) data |= 0x20;
309                 if (joypad_1_buttons[(pad1Index << 2) + 2]) data |= 0x40;
310                 if (joypad_1_buttons[(pad1Index << 2) + 3]) data |= 0x80;
311
312                 return ~data;
313         }
314         else if (offset == 3)
315         {
316                 // Hardware ID returns NTSC/PAL identification bit here
317                 uint8 data = 0x2F | (vjs.hardwareTypeNTSC ? 0x10 : 0x00);
318                 int pad0Index = joystick_ram[1] & 0x0F;
319 //unused                int pad1Index = (joystick_ram[1] >> 4) & 0x0F;
320
321                 if (!(pad0Index & 0x01))
322                 {
323                         if (joypad_0_buttons[BUTTON_PAUSE])
324                                 data ^= 0x01;
325                         if (joypad_0_buttons[BUTTON_A])
326                                 data ^= 0x02;
327                 }
328                 else if (!(pad0Index & 0x02))
329                 {
330                         if (joypad_0_buttons[BUTTON_B])
331                                 data ^= 0x02;
332                 }
333                 else if (!(pad0Index & 0x04))
334                 {
335                         if (joypad_0_buttons[BUTTON_C])
336                                 data ^= 0x02;
337                 }
338                 else
339                 {
340                         if (joypad_0_buttons[BUTTON_OPTION])
341                                 data ^= 0x02;
342                 }
343                 return data;
344         }
345
346         return joystick_ram[offset];
347 }
348
349 uint16 JoystickReadWord(uint32 offset)
350 {
351         return ((uint16)JoystickReadByte((offset + 0) & 0x03) << 8) | JoystickReadByte((offset + 1) & 0x03);
352 }
353
354 void JoystickWriteByte(uint32 offset, uint8 data)
355 {
356         joystick_ram[offset & 0x03] = data;
357 }
358
359 void JoystickWriteWord(uint32 offset, uint16 data)
360 {
361 #warning No bounds checking done for JoystickWriteWord!
362         offset &= 0x03;
363         joystick_ram[offset + 0] = (data >> 8) & 0xFF;
364         joystick_ram[offset + 1] = data & 0xFF;
365 }