]> Shamusworld >> Repos - virtualjaguar/blob - src/joystick.cpp
Changes to support new video subsystem
[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
14 #define BUTTON_U                0
15 #define BUTTON_D                1
16 #define BUTTON_L                2
17 #define BUTTON_R                3
18 #define BUTTON_s                4
19 #define BUTTON_7                5
20 #define BUTTON_4                6
21 #define BUTTON_1                7
22 #define BUTTON_0                8
23 #define BUTTON_8                9
24 #define BUTTON_5                10
25 #define BUTTON_2                11
26 #define BUTTON_d                12
27 #define BUTTON_9                13
28 #define BUTTON_6                14
29 #define BUTTON_3                15
30
31 #define BUTTON_A                16
32 #define BUTTON_B                17
33 #define BUTTON_C                18
34 #define BUTTON_OPTION   19
35 #define BUTTON_PAUSE    20
36
37 // Private function prototypes
38
39 //void main_screen_switch(void);
40
41 // Global vars
42
43 static uint8 joystick_ram[4];
44 static uint8 joypad_0_buttons[21];
45 static uint8 joypad_1_buttons[21];
46 extern bool finished;
47 extern bool showGUI;
48 bool GUIKeyHeld = false;
49 extern int start_logging;
50 int gpu_start_log = 0;
51 int op_start_log = 0;
52 int blit_start_log = 0;
53 int effect_start = 0;
54 int effect_start2 = 0, effect_start3 = 0, effect_start4 = 0, effect_start5 = 0, effect_start6 = 0;
55 bool interactiveMode = false;
56 bool iLeft, iRight, iToggle = false;
57 bool keyHeld1 = false, keyHeld2 = false, keyHeld3 = false;
58 int objectPtr = 0;
59 bool startMemLog = false;
60
61
62 void joystick_init(void)
63 {
64         joystick_reset();
65 }
66
67 void joystick_exec(void)
68 {
69         extern SDL_Joystick * joystick;
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         /* Added/Changed by SDLEMU (http://sdlemu.ngemu.com) */
85
86         if (keystate[SDLK_UP])          joypad_0_buttons[BUTTON_U] = 0x01;
87         if (keystate[SDLK_DOWN])        joypad_0_buttons[BUTTON_D] = 0x01;
88         if (keystate[SDLK_LEFT])        joypad_0_buttons[BUTTON_L] = 0x01;
89         if (keystate[SDLK_RIGHT])       joypad_0_buttons[BUTTON_R] = 0x01;
90         // The buttons are labelled C,B,A on the controller (going from left to right)
91         if (keystate[SDLK_z])           joypad_0_buttons[BUTTON_C] = 0x01;
92         if (keystate[SDLK_x])           joypad_0_buttons[BUTTON_B] = 0x01;
93         if (keystate[SDLK_c])           joypad_0_buttons[BUTTON_A] = 0x01;
94 //I may yet move these to O and P...
95         if (keystate[SDLK_QUOTE])       joypad_0_buttons[BUTTON_OPTION] = 0x01;
96         if (keystate[SDLK_RETURN])      joypad_0_buttons[BUTTON_PAUSE] = 0x01;
97
98         if (keystate[SDLK_TAB])
99         {
100                 if (!GUIKeyHeld)
101                         showGUI = !showGUI, GUIKeyHeld = true;
102         }
103         else
104                 GUIKeyHeld = false;
105
106         if (keystate[SDLK_q])
107                 start_logging = 1;
108         if (keystate[SDLK_w])
109                 gpu_reset_stats();
110 //      if (keystate[SDLK_u])           jaguar_long_write(0xf1c384,jaguar_long_read(0xf1c384)+1);
111         if (keystate[SDLK_d])
112                 DumpMainMemory();
113         if (keystate[SDLK_l])
114                 gpu_start_log = 1;
115         if (keystate[SDLK_o])
116                 op_start_log = 1;
117         if (keystate[SDLK_b])
118                 blit_start_log = 1;
119
120         if (keystate[SDLK_1])
121                 effect_start = 1;
122         if (keystate[SDLK_2])
123                 effect_start2 = 1;
124         if (keystate[SDLK_3])
125                 effect_start3 = 1;
126         if (keystate[SDLK_4])
127                 effect_start4 = 1;
128         if (keystate[SDLK_5])
129                 effect_start5 = 1;
130         if (keystate[SDLK_6])
131                 effect_start6 = 1;
132
133         if (keystate[SDLK_i])
134                 interactiveMode = true;
135
136         if (keystate[SDLK_8] && interactiveMode)
137         {
138                 if (!keyHeld1)
139                         objectPtr--, keyHeld1 = true;
140         }
141         else
142                 keyHeld1 = false;
143
144         if (keystate[SDLK_0] && interactiveMode)
145         {
146                 if (!keyHeld2)
147                         objectPtr++, keyHeld2 = true;
148         }
149         else
150                 keyHeld2 = false;
151
152         if (keystate[SDLK_9] && interactiveMode)
153         {
154                 if (!keyHeld3)
155                         iToggle = !iToggle, keyHeld3 = true;
156         }
157         else
158                 keyHeld3 = false;
159
160         if (keystate[SDLK_e])
161                 startMemLog = true;
162         if (keystate[SDLK_r])
163                 WriteLog("\n--------> MARK!\n\n");
164
165         if (keystate[SDLK_KP0])         joypad_0_buttons[BUTTON_0] = 0x01;
166         if (keystate[SDLK_KP1])         joypad_0_buttons[BUTTON_1] = 0x01;
167         if (keystate[SDLK_KP2])         joypad_0_buttons[BUTTON_2] = 0x01;
168         if (keystate[SDLK_KP3])         joypad_0_buttons[BUTTON_3] = 0x01;
169         if (keystate[SDLK_KP4])         joypad_0_buttons[BUTTON_4] = 0x01;
170         if (keystate[SDLK_KP5])         joypad_0_buttons[BUTTON_5] = 0x01;
171         if (keystate[SDLK_KP6])         joypad_0_buttons[BUTTON_6] = 0x01;
172         if (keystate[SDLK_KP7])         joypad_0_buttons[BUTTON_7] = 0x01;
173         if (keystate[SDLK_KP8])         joypad_0_buttons[BUTTON_8] = 0x01;
174         if (keystate[SDLK_KP9])         joypad_0_buttons[BUTTON_9] = 0x01;
175
176     if (keystate[SDLK_ESCAPE])
177         finished = true;
178
179     /* Added/Changed by SDLEMU (http://sdlemu.ngemu.com */
180     /* Joystick support                                 */
181     
182     if (useJoystick)
183     {
184                 int16 x = SDL_JoystickGetAxis(joystick, 0),
185                         y = SDL_JoystickGetAxis(joystick, 1);
186         
187                 if (x > 16384)
188                         joypad_0_buttons[BUTTON_R] = 0x01;
189                 if (x < -16384)
190                         joypad_0_buttons[BUTTON_L] = 0x01;
191                 if (y > 16384)
192                         joypad_0_buttons[BUTTON_D] = 0x01;
193                 if (y < -16384)
194                         joypad_0_buttons[BUTTON_U] = 0x01;
195         
196                 if (SDL_JoystickGetButton(joystick, 0) == SDL_PRESSED)
197                         joypad_0_buttons[BUTTON_A] = 0x01;
198                 if (SDL_JoystickGetButton(joystick, 1) == SDL_PRESSED)
199                         joypad_0_buttons[BUTTON_B] = 0x01;
200                 if (SDL_JoystickGetButton(joystick, 2) == SDL_PRESSED)
201                         joypad_0_buttons[BUTTON_C] = 0x01;
202         }
203         
204         /* ADDED by SDLEMU (http://sdlemu.ngemu.com */
205         /* Needed to make sure that the events queue is empty */
206     SDL_PumpEvents();            
207 }
208
209 void joystick_reset(void)
210 {
211         memset(joystick_ram, 0x00, 4);
212         memset(joypad_0_buttons, 0, 21);
213         memset(joypad_1_buttons, 0, 21);
214 }
215
216 void joystick_done(void)
217 {
218 }
219
220 uint8 joystick_byte_read(uint32 offset)
221 {
222         extern bool hardwareTypeNTSC;
223         offset &= 0x03;
224
225         if (offset == 0)
226         {
227                 uint8 data = 0x00;
228                 int pad0Index = joystick_ram[1] & 0x0F;
229                 int pad1Index = (joystick_ram[1] >> 4) & 0x0F;
230                 
231 // This is bad--we're assuming that a bit is set in the last case. Might not be so!
232                 if (!(pad0Index & 0x01)) 
233                         pad0Index = 0;
234                 else if (!(pad0Index & 0x02)) 
235                         pad0Index = 1;
236                 else if (!(pad0Index & 0x04)) 
237                         pad0Index = 2;
238                 else 
239                         pad0Index = 3;
240                 
241                 if (!(pad1Index & 0x01)) 
242                         pad1Index = 0;
243                 else if (!(pad1Index & 0x02)) 
244                         pad1Index = 1;
245                 else if (!(pad1Index & 0x04)) 
246                         pad1Index = 2;
247                 else
248                         pad1Index = 3;
249
250                 if (joypad_0_buttons[(pad0Index << 2) + 0])     data |= 0x01;
251                 if (joypad_0_buttons[(pad0Index << 2) + 1]) data |= 0x02;
252                 if (joypad_0_buttons[(pad0Index << 2) + 2]) data |= 0x04;
253                 if (joypad_0_buttons[(pad0Index << 2) + 3]) data |= 0x08;
254                 if (joypad_1_buttons[(pad1Index << 2) + 0]) data |= 0x10;
255                 if (joypad_1_buttons[(pad1Index << 2) + 1]) data |= 0x20;
256                 if (joypad_1_buttons[(pad1Index << 2) + 2]) data |= 0x40;
257                 if (joypad_1_buttons[(pad1Index << 2) + 3]) data |= 0x80;
258
259                 return ~data;
260         }
261         else if (offset == 3)
262         {
263                 uint8 data = 0x2F | (hardwareTypeNTSC ? 0x10 : 0x00);
264                 int pad0Index = joystick_ram[1] & 0x0F;
265 //unused                int pad1Index = (joystick_ram[1] >> 4) & 0x0F;
266                 
267                 if (!(pad0Index & 0x01))
268                 {
269                         if (joypad_0_buttons[BUTTON_PAUSE])
270                                 data ^= 0x01;
271                         if (joypad_0_buttons[BUTTON_A])
272                                 data ^= 0x02;
273                 }
274                 else if (!(pad0Index & 0x02))
275                 {
276                         if (joypad_0_buttons[BUTTON_B])
277                                 data ^= 0x02;
278                 }
279                 else if (!(pad0Index & 0x04))
280                 {
281                         if (joypad_0_buttons[BUTTON_C])
282                                 data ^= 0x02;
283                 }
284                 else
285                 {
286                         if (joypad_0_buttons[BUTTON_OPTION])
287                                 data ^= 0x02;
288                 }               
289                 return data;
290         }
291
292         return joystick_ram[offset];
293 }
294
295 uint16 joystick_word_read(uint32 offset)
296 {
297         return ((uint16)joystick_byte_read((offset+0)&0x03) << 8) | joystick_byte_read((offset+1)&0x03);
298 }
299
300 void joystick_byte_write(uint32 offset, uint8 data)
301 {
302         joystick_ram[offset&0x03] = data;
303 }
304
305 void joystick_word_write(uint32 offset, uint16 data)
306 {
307         offset &= 0x03;
308         joystick_ram[offset+0] = (data >> 8) & 0xFF;
309         joystick_ram[offset+1] = data & 0xFF;
310 }