]> Shamusworld >> Repos - virtualjaguar/blob - src/joystick.cpp
Added another developer key ('t')
[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     if (keystate[SDLK_ESCAPE])
131         finished = true;
132
133         if (keystate[SDLK_TAB])
134         {
135                 if (!GUIKeyHeld)
136                         showGUI = !showGUI, GUIKeyHeld = true;
137         }
138         else
139                 GUIKeyHeld = false;
140
141         if (keystate[SDLK_q])
142                 start_logging = 1;
143         if (keystate[SDLK_w])
144                 gpu_reset_stats();
145 //      if (keystate[SDLK_u])           jaguar_long_write(0xf1c384,jaguar_long_read(0xf1c384)+1);
146         if (keystate[SDLK_d])
147                 DumpMainMemory();
148         if (keystate[SDLK_l])
149                 gpu_start_log = 1;
150         if (keystate[SDLK_o])
151                 op_start_log = 1;
152         if (keystate[SDLK_b])
153                 blit_start_log = 1;
154
155         if (keystate[SDLK_1])
156                 effect_start = 1;
157         if (keystate[SDLK_2])
158                 effect_start2 = 1;
159         if (keystate[SDLK_3])
160                 effect_start3 = 1;
161         if (keystate[SDLK_4])
162                 effect_start4 = 1;
163         if (keystate[SDLK_5])
164                 effect_start5 = 1;
165         if (keystate[SDLK_6])
166                 effect_start6 = 1;
167
168         if (keystate[SDLK_i])
169                 interactiveMode = true;
170
171         if (keystate[SDLK_8] && interactiveMode)
172         {
173                 if (!keyHeld1)
174                         objectPtr--, keyHeld1 = true;
175         }
176         else
177                 keyHeld1 = false;
178
179         if (keystate[SDLK_0] && interactiveMode)
180         {
181                 if (!keyHeld2)
182                         objectPtr++, keyHeld2 = true;
183         }
184         else
185                 keyHeld2 = false;
186
187         if (keystate[SDLK_9] && interactiveMode)
188         {
189                 if (!keyHeld3)
190                         iToggle = !iToggle, keyHeld3 = true;
191         }
192         else
193                 keyHeld3 = false;
194
195         if (keystate[SDLK_e])
196                 startMemLog = true;
197         if (keystate[SDLK_r])
198                 WriteLog("\n--------> MARK!\n\n");
199         if (keystate[SDLK_t])
200                 doDSPDis = true;
201
202
203         // Joystick support [nwagenaar]
204
205     if (vjs.useJoystick)
206     {
207                 extern SDL_Joystick * joystick;
208                 int16 x = SDL_JoystickGetAxis(joystick, 0),
209                         y = SDL_JoystickGetAxis(joystick, 1);
210         
211                 if (x > 16384)
212                         joypad_0_buttons[BUTTON_R] = 0x01;
213                 if (x < -16384)
214                         joypad_0_buttons[BUTTON_L] = 0x01;
215                 if (y > 16384)
216                         joypad_0_buttons[BUTTON_D] = 0x01;
217                 if (y < -16384)
218                         joypad_0_buttons[BUTTON_U] = 0x01;
219         
220                 if (SDL_JoystickGetButton(joystick, 0) == SDL_PRESSED)
221                         joypad_0_buttons[BUTTON_A] = 0x01;
222                 if (SDL_JoystickGetButton(joystick, 1) == SDL_PRESSED)
223                         joypad_0_buttons[BUTTON_B] = 0x01;
224                 if (SDL_JoystickGetButton(joystick, 2) == SDL_PRESSED)
225                         joypad_0_buttons[BUTTON_C] = 0x01;
226         }
227         
228         // Needed to ensure that the events queue is empty [nwagenaar]
229     SDL_PumpEvents();
230 }
231
232 void joystick_reset(void)
233 {
234         memset(joystick_ram, 0x00, 4);
235         memset(joypad_0_buttons, 0, 21);
236         memset(joypad_1_buttons, 0, 21);
237 }
238
239 void joystick_done(void)
240 {
241 }
242
243 uint8 joystick_byte_read(uint32 offset)
244 {
245 //      extern bool hardwareTypeNTSC;
246         offset &= 0x03;
247
248         if (offset == 0)
249         {
250                 uint8 data = 0x00;
251                 int pad0Index = joystick_ram[1] & 0x0F;
252                 int pad1Index = (joystick_ram[1] >> 4) & 0x0F;
253                 
254 // This is bad--we're assuming that a bit is set in the last case. Might not be so!
255                 if (!(pad0Index & 0x01)) 
256                         pad0Index = 0;
257                 else if (!(pad0Index & 0x02)) 
258                         pad0Index = 1;
259                 else if (!(pad0Index & 0x04)) 
260                         pad0Index = 2;
261                 else 
262                         pad0Index = 3;
263                 
264                 if (!(pad1Index & 0x01)) 
265                         pad1Index = 0;
266                 else if (!(pad1Index & 0x02)) 
267                         pad1Index = 1;
268                 else if (!(pad1Index & 0x04)) 
269                         pad1Index = 2;
270                 else
271                         pad1Index = 3;
272
273                 if (joypad_0_buttons[(pad0Index << 2) + 0])     data |= 0x01;
274                 if (joypad_0_buttons[(pad0Index << 2) + 1]) data |= 0x02;
275                 if (joypad_0_buttons[(pad0Index << 2) + 2]) data |= 0x04;
276                 if (joypad_0_buttons[(pad0Index << 2) + 3]) data |= 0x08;
277                 if (joypad_1_buttons[(pad1Index << 2) + 0]) data |= 0x10;
278                 if (joypad_1_buttons[(pad1Index << 2) + 1]) data |= 0x20;
279                 if (joypad_1_buttons[(pad1Index << 2) + 2]) data |= 0x40;
280                 if (joypad_1_buttons[(pad1Index << 2) + 3]) data |= 0x80;
281
282                 return ~data;
283         }
284         else if (offset == 3)
285         {
286                 uint8 data = 0x2F | (vjs.hardwareTypeNTSC ? 0x10 : 0x00);
287                 int pad0Index = joystick_ram[1] & 0x0F;
288 //unused                int pad1Index = (joystick_ram[1] >> 4) & 0x0F;
289                 
290                 if (!(pad0Index & 0x01))
291                 {
292                         if (joypad_0_buttons[BUTTON_PAUSE])
293                                 data ^= 0x01;
294                         if (joypad_0_buttons[BUTTON_A])
295                                 data ^= 0x02;
296                 }
297                 else if (!(pad0Index & 0x02))
298                 {
299                         if (joypad_0_buttons[BUTTON_B])
300                                 data ^= 0x02;
301                 }
302                 else if (!(pad0Index & 0x04))
303                 {
304                         if (joypad_0_buttons[BUTTON_C])
305                                 data ^= 0x02;
306                 }
307                 else
308                 {
309                         if (joypad_0_buttons[BUTTON_OPTION])
310                                 data ^= 0x02;
311                 }               
312                 return data;
313         }
314
315         return joystick_ram[offset];
316 }
317
318 uint16 joystick_word_read(uint32 offset)
319 {
320         return ((uint16)joystick_byte_read((offset+0)&0x03) << 8) | joystick_byte_read((offset+1)&0x03);
321 }
322
323 void joystick_byte_write(uint32 offset, uint8 data)
324 {
325         joystick_ram[offset&0x03] = data;
326 }
327
328 void joystick_word_write(uint32 offset, uint16 data)
329 {
330         offset &= 0x03;
331         joystick_ram[offset+0] = (data >> 8) & 0xFF;
332         joystick_ram[offset+1] = data & 0xFF;
333 }