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