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