]> Shamusworld >> Repos - virtualjaguar/blob - src/joystick.cpp
Virtual Jaguar 1.0.4 update (Shamus)
[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 //#ifndef __PORT__
10 //#include "include/stdafx.h"
11 //#include <mmsystem.h>
12 //#endif
13 #include <time.h>
14 #include <SDL.h>
15 #include "SDLptc.h"
16 #include "jaguar.h"
17
18 void main_screen_switch(void);
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
38 #define BUTTON_A                16
39 #define BUTTON_B                17
40 #define BUTTON_C                18
41 #define BUTTON_OPTION   19
42 #define BUTTON_PAUSE    20
43
44 static uint8 joystick_ram[4];
45 static uint8 joypad_0_buttons[21];
46 static uint8 joypad_1_buttons[21];
47 extern bool finished;
48 extern int start_logging;
49 int gpu_start_log = 0;
50 int op_start_log = 0;
51 int blit_start_log = 0;
52 int effect_start = 0;
53 bool interactiveMode = false;
54 bool iLeft, iRight, iToggle = false;
55 bool keyHeld1 = false, keyHeld2 = false, keyHeld3 = false;
56 int objectPtr = 0;
57
58 void joystick_init(void)
59 {
60         joystick_reset();
61 }
62
63 void joystick_exec(void)
64 {
65         uint8 * keystate = SDL_GetKeyState(NULL);
66         extern Console console;
67         
68         memset(joypad_0_buttons, 0, 21);
69         memset(joypad_1_buttons, 0, 21);
70         gpu_start_log = 0;                                                      // Only log while key down!
71         effect_start = 0;
72         blit_start_log = 0;
73         iLeft = iRight = false;
74
75         if ((keystate[SDLK_LALT]) & (keystate[SDLK_RETURN]))
76                 main_screen_switch();
77
78         /* Added/Changed by SDLEMU (http://sdlemu.ngemu.com) */
79
80         if (keystate[SDLK_UP])          joypad_0_buttons[BUTTON_U] = 0x01;
81         if (keystate[SDLK_DOWN])        joypad_0_buttons[BUTTON_D] = 0x01;
82         if (keystate[SDLK_LEFT])        joypad_0_buttons[BUTTON_L] = 0x01;
83         if (keystate[SDLK_RIGHT])       joypad_0_buttons[BUTTON_R] = 0x01;
84         // The buttons are labelled C,B,A on the controller (going from left to right)
85         if (keystate[SDLK_z])           joypad_0_buttons[BUTTON_C] = 0x01;
86         if (keystate[SDLK_x])           joypad_0_buttons[BUTTON_B] = 0x01;
87         if (keystate[SDLK_c])           joypad_0_buttons[BUTTON_A] = 0x01;
88         if (keystate[SDLK_TAB])         joypad_0_buttons[BUTTON_OPTION] = 0x01;
89         if (keystate[SDLK_RETURN])      joypad_0_buttons[BUTTON_PAUSE] = 0x01;
90         if (keystate[SDLK_q])
91                 start_logging = 1;
92         if (keystate[SDLK_w])
93                 gpu_reset_stats();
94 //      if (keystate[SDLK_u])           jaguar_long_write(0xf1c384,jaguar_long_read(0xf1c384)+1);
95         if (keystate[SDLK_d])
96                 DumpMainMemory();
97         if (keystate[SDLK_l])
98                 gpu_start_log = 1;
99         if (keystate[SDLK_o])
100                 op_start_log = 1;
101         if (keystate[SDLK_b])
102                 blit_start_log = 1;
103         if (keystate[SDLK_1])
104                 effect_start = 1;
105
106         if (keystate[SDLK_i])
107                 interactiveMode = true;
108
109         if (keystate[SDLK_8] && interactiveMode)
110         {
111                 if (!keyHeld1)
112                         objectPtr--, keyHeld1 = true;
113         }
114         else
115                 keyHeld1 = false;
116
117         if (keystate[SDLK_0] && interactiveMode)
118         {
119                 if (!keyHeld2)
120                         objectPtr++, keyHeld2 = true;
121         }
122         else
123                 keyHeld2 = false;
124
125         if (keystate[SDLK_9] && interactiveMode)
126         {
127                 if (!keyHeld3)
128                         iToggle = !iToggle, keyHeld3 = true;
129         }
130         else
131                 keyHeld3 = false;
132
133         if (keystate[SDLK_KP0])         joypad_0_buttons[BUTTON_0] = 0x01;
134         if (keystate[SDLK_KP1])         joypad_0_buttons[BUTTON_1] = 0x01;
135         if (keystate[SDLK_KP2])         joypad_0_buttons[BUTTON_2] = 0x01;
136         if (keystate[SDLK_KP3])         joypad_0_buttons[BUTTON_3] = 0x01;
137         if (keystate[SDLK_KP4])         joypad_0_buttons[BUTTON_4] = 0x01;
138         if (keystate[SDLK_KP5])         joypad_0_buttons[BUTTON_5] = 0x01;
139         if (keystate[SDLK_KP6])         joypad_0_buttons[BUTTON_6] = 0x01;
140         if (keystate[SDLK_KP7])         joypad_0_buttons[BUTTON_7] = 0x01;
141         if (keystate[SDLK_KP8])         joypad_0_buttons[BUTTON_8] = 0x01;
142         if (keystate[SDLK_KP9])         joypad_0_buttons[BUTTON_9] = 0x01;
143
144     if (keystate[SDLK_ESCAPE])
145         finished = true;
146
147     /* Added/Changed by SDLEMU (http://sdlemu.ngemu.com */
148     /* Joystick support                                 */
149     
150     if (console.JoyEnabled() == 1)
151     {
152                 int16 x = SDL_JoystickGetAxis(console.joystick, 0),
153                         y = SDL_JoystickGetAxis(console.joystick, 1);
154         
155                 if (x > 16384)
156                         joypad_0_buttons[BUTTON_R] = 0x01;
157                 if (x < -16384)
158                         joypad_0_buttons[BUTTON_L] = 0x01;
159                 if (y > 16384)
160                         joypad_0_buttons[BUTTON_D] = 0x01;
161                 if (y < -16384)
162                         joypad_0_buttons[BUTTON_U] = 0x01;
163         
164                 if (SDL_JoystickGetButton(console.joystick, 0) == SDL_PRESSED)
165                         joypad_0_buttons[BUTTON_A] = 0x01;
166                 if (SDL_JoystickGetButton(console.joystick, 1) == SDL_PRESSED)
167                         joypad_0_buttons[BUTTON_B] = 0x01;
168                 if (SDL_JoystickGetButton(console.joystick, 2) == SDL_PRESSED)
169                         joypad_0_buttons[BUTTON_C] = 0x01;
170         }
171         
172         /* ADDED by SDLEMU (http://sdlemu.ngemu.com */
173         /* Needed to make sure that the events queue is empty */
174     SDL_PumpEvents();            
175 }
176
177 void joystick_reset(void)
178 {
179         memset(joystick_ram, 0x00, 4);
180         memset(joypad_0_buttons, 0, 21);
181         memset(joypad_1_buttons, 0, 21);
182 }
183
184 void joystick_done(void)
185 {
186 }
187
188 void joystick_byte_write(uint32 offset, uint8 data)
189 {
190         joystick_ram[offset&0x03] = data;
191 }
192
193 void joystick_word_write(uint32 offset, uint16 data)
194 {
195         offset &= 0x03;
196         joystick_ram[offset+0] = (data >> 8) & 0xFF;
197         joystick_ram[offset+1] = data & 0xFF;
198 }
199
200 uint8 joystick_byte_read(uint32 offset)
201 {
202         extern bool hardwareTypeNTSC;
203         offset &= 0x03;
204
205         if (offset == 0)
206         {
207                 uint8 data = 0x00;
208                 int pad0Index = joystick_ram[1] & 0x0F;
209                 int pad1Index = (joystick_ram[1] >> 4) & 0x0F;
210                 
211 // This is bad--we're assuming that a bit is set in the last case
212                 if (!(pad0Index & 0x01)) 
213                         pad0Index = 0;
214                 else if (!(pad0Index & 0x02)) 
215                         pad0Index = 1;
216                 else if (!(pad0Index & 0x04)) 
217                         pad0Index = 2;
218                 else 
219                         pad0Index = 3;
220                 
221                 if (!(pad1Index & 0x01)) 
222                         pad1Index = 0;
223                 else if (!(pad1Index & 0x02)) 
224                         pad1Index = 1;
225                 else if (!(pad1Index & 0x04)) 
226                         pad1Index = 2;
227                 else
228                         pad1Index = 3;
229
230                 if (joypad_0_buttons[(pad0Index << 2) + 0])     data |= 0x01;
231                 if (joypad_0_buttons[(pad0Index << 2) + 1]) data |= 0x02;
232                 if (joypad_0_buttons[(pad0Index << 2) + 2]) data |= 0x04;
233                 if (joypad_0_buttons[(pad0Index << 2) + 3]) data |= 0x08;
234                 if (joypad_1_buttons[(pad1Index << 2) + 0]) data |= 0x10;
235                 if (joypad_1_buttons[(pad1Index << 2) + 1]) data |= 0x20;
236                 if (joypad_1_buttons[(pad1Index << 2) + 2]) data |= 0x40;
237                 if (joypad_1_buttons[(pad1Index << 2) + 3]) data |= 0x80;
238
239                 return ~data;
240         }
241         else if (offset == 3)
242         {
243 //              uint8 data = ((1 << 5) | (1 << 4) | 0x0F);
244                 uint8 data = 0x2F | (hardwareTypeNTSC ? 0x10 : 0x00);
245                 int pad0Index = joystick_ram[1] & 0x0F;
246 //unused                int pad1Index = (joystick_ram[1] >> 4) & 0x0F;
247                 
248                 if (!(pad0Index & 0x01))
249                 {
250                         if (joypad_0_buttons[BUTTON_PAUSE])
251                                 data ^= 0x01;
252                         if (joypad_0_buttons[BUTTON_A])
253                                 data ^= 0x02;
254                 }
255                 else if (!(pad0Index & 0x02))
256                 {
257                         if (joypad_0_buttons[BUTTON_B])
258                                 data ^= 0x02;
259                 }
260                 else if (!(pad0Index & 0x04))
261                 {
262                         if (joypad_0_buttons[BUTTON_C])
263                                 data ^= 0x02;
264                 }
265                 else
266                 {
267                         if (joypad_0_buttons[BUTTON_OPTION])
268                                 data ^= 0x02;
269                 }               
270                 return data;
271         }
272
273         return joystick_ram[offset];
274 }
275
276 uint16 joystick_word_read(uint32 offset)
277 {
278         return ((uint16)joystick_byte_read((offset+0)&0x03) << 8) | joystick_byte_read((offset+1)&0x03);
279 }