]> Shamusworld >> Repos - virtualjaguar/blob - src/joystick.cpp
Virtual Jaguar GCC/SDL v1.0.3 import.
[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 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_1                4
25 #define BUTTON_4                5
26 #define BUTTON_7                6
27 #define BUTTON_s                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 uint8 finished;
48 extern int start_logging;
49
50
51 void joystick_init(void)
52 {
53         joystick_reset();
54 }
55
56 void joystick_exec(void)
57 {
58         uint8 * keystate = SDL_GetKeyState(NULL);
59         extern Console console;
60         
61         memset(joypad_0_buttons, 0, 21);
62         memset(joypad_1_buttons, 0, 21);
63
64         if ((keystate[SDLK_LALT]) & (keystate[SDLK_RETURN]))
65                 main_screen_switch();
66
67         /* Added/Changed by SDLEMU (http://sdlemu.ngemu.com) */
68
69         if (keystate[SDLK_UP])          joypad_0_buttons[BUTTON_U] = 0x01;
70         if (keystate[SDLK_DOWN])        joypad_0_buttons[BUTTON_D] = 0x01;
71         if (keystate[SDLK_LEFT])        joypad_0_buttons[BUTTON_L] = 0x01;
72         if (keystate[SDLK_RIGHT])       joypad_0_buttons[BUTTON_R] = 0x01;
73         if (keystate[SDLK_z])           joypad_0_buttons[BUTTON_A] = 0x01;
74         if (keystate[SDLK_x])           joypad_0_buttons[BUTTON_B] = 0x01;
75         if (keystate[SDLK_c])           joypad_0_buttons[BUTTON_C] = 0x01;
76         if (keystate[SDLK_TAB])         joypad_0_buttons[BUTTON_OPTION] = 0x01;
77         if (keystate[SDLK_RETURN])      joypad_0_buttons[BUTTON_PAUSE] = 0x01;
78         if (keystate[SDLK_q])
79                 start_logging = 1;
80         if (keystate[SDLK_w])
81                 gpu_reset_stats();
82 //      if (keystate[SDLK_u])           jaguar_long_write(0xf1c384,jaguar_long_read(0xf1c384)+1);
83         if (keystate[SDLK_d])
84                 DumpMainMemory();
85
86         if (keystate[SDLK_KP0])         joypad_0_buttons[BUTTON_0] = 0x01;
87         if (keystate[SDLK_KP1])         joypad_0_buttons[BUTTON_1] = 0x01;
88         if (keystate[SDLK_KP2])         joypad_0_buttons[BUTTON_2] = 0x01;
89         if (keystate[SDLK_KP3])         joypad_0_buttons[BUTTON_3] = 0x01;
90         if (keystate[SDLK_KP4])         joypad_0_buttons[BUTTON_4] = 0x01;
91         if (keystate[SDLK_KP5])         joypad_0_buttons[BUTTON_5] = 0x01;
92         if (keystate[SDLK_KP6])         joypad_0_buttons[BUTTON_6] = 0x01;
93         if (keystate[SDLK_KP7])         joypad_0_buttons[BUTTON_7] = 0x01;
94         if (keystate[SDLK_KP8])         joypad_0_buttons[BUTTON_8] = 0x01;
95         if (keystate[SDLK_KP9])         joypad_0_buttons[BUTTON_9] = 0x01;
96
97     if (keystate[SDLK_ESCAPE])
98         finished = 1;
99
100     /* Added/Changed by SDLEMU (http://sdlemu.ngemu.com */
101     /* Joystick support                                 */
102     
103     if (console.JoyEnabled() == 1)
104     {
105                 int16 x = SDL_JoystickGetAxis(console.joystick, 0),
106                         y = SDL_JoystickGetAxis(console.joystick, 1);
107         
108                 if (x > 16384)
109                         joypad_0_buttons[BUTTON_R] = 0x01;
110                 if (x < -16384)
111                         joypad_0_buttons[BUTTON_L] = 0x01;
112                 if (y > 16384)
113                         joypad_0_buttons[BUTTON_D] = 0x01;
114                 if (y < -16384)
115                         joypad_0_buttons[BUTTON_U] = 0x01;
116         
117                 if (SDL_JoystickGetButton(console.joystick, 0) == SDL_PRESSED)
118                         joypad_0_buttons[BUTTON_A] = 0x01;
119                 if (SDL_JoystickGetButton(console.joystick, 1) == SDL_PRESSED)
120                         joypad_0_buttons[BUTTON_B] = 0x01;
121                 if (SDL_JoystickGetButton(console.joystick, 2) == SDL_PRESSED)
122                         joypad_0_buttons[BUTTON_C] = 0x01;
123         }
124         
125         /* ADDED by SDLEMU (http://sdlemu.ngemu.com */
126         /* Needed to make sure that the events queue is empty */
127     SDL_PumpEvents();            
128 }
129
130 void joystick_reset(void)
131 {
132         memset(joystick_ram, 0x00, 4);
133         memset(joypad_0_buttons, 0, 21);
134         memset(joypad_1_buttons, 0, 21);
135 }
136
137 void joystick_done(void)
138 {
139 }
140
141 void joystick_byte_write(uint32 offset, uint8 data)
142 {
143         joystick_ram[offset&0x03] = data;
144 }
145
146 void joystick_word_write(uint32 offset, uint16 data)
147 {
148         offset &= 0x03;
149         joystick_ram[offset+0] = (data >> 8) & 0xFF;
150         joystick_ram[offset+1] = data & 0xFF;
151 }
152
153 uint8 joystick_byte_read(uint32 offset)
154 {
155         offset &= 0x03;
156
157         if (offset == 0)
158         {
159                 uint8 data = 0x00;
160                 int pad0Index = joystick_ram[1] & 0x0F;
161                 int pad1Index = (joystick_ram[1] >> 4) & 0x0F;
162                 
163                 if (!(pad0Index & 0x01)) 
164                         pad0Index = 0;
165                 else if (!(pad0Index & 0x02)) 
166                         pad0Index = 1;
167                 else if (!(pad0Index & 0x04)) 
168                         pad0Index = 2;
169                 else 
170                         pad0Index = 3;
171                 
172                 if (!(pad1Index & 0x01)) 
173                         pad1Index = 0;
174                 else if (!(pad1Index & 0x02)) 
175                         pad1Index = 1;
176                 else if (!(pad1Index & 0x04)) 
177                         pad1Index = 2;
178                 else
179                         pad1Index = 3;
180
181                 if (joypad_0_buttons[(pad0Index << 2) + 0])     data |= 0x01;
182                 if (joypad_0_buttons[(pad0Index << 2) + 1]) data |= 0x02;
183                 if (joypad_0_buttons[(pad0Index << 2) + 2]) data |= 0x04;
184                 if (joypad_0_buttons[(pad0Index << 2) + 3]) data |= 0x08;
185                 if (joypad_1_buttons[(pad1Index << 2) + 0]) data |= 0x10;
186                 if (joypad_1_buttons[(pad1Index << 2) + 1]) data |= 0x20;
187                 if (joypad_1_buttons[(pad1Index << 2) + 2]) data |= 0x40;
188                 if (joypad_1_buttons[(pad1Index << 2) + 3]) data |= 0x80;
189
190                 return ~data;
191         }
192         else if (offset == 3)
193         {
194                 uint8 data = ((1 << 5) | (1 << 4) | 0x0F);
195                 int pad0Index = joystick_ram[1] & 0x0F;
196 //unused                int pad1Index = (joystick_ram[1] >> 4) & 0x0F;
197                 
198                 if (!(pad0Index & 0x01))
199                 {
200                         if (joypad_0_buttons[BUTTON_PAUSE])
201                                 data ^= 0x01;
202                         if (joypad_0_buttons[BUTTON_A])
203                                 data ^= 0x02;
204                 }
205                 else if (!(pad0Index & 0x02))
206                 {
207                         if (joypad_0_buttons[BUTTON_B])
208                                 data ^= 0x02;
209                 }
210                 else if (!(pad0Index & 0x04))
211                 {
212                         if (joypad_0_buttons[BUTTON_C])
213                                 data ^= 0x02;
214                 }
215                 else
216                 {
217                         if (joypad_0_buttons[BUTTON_OPTION])
218                                 data ^= 0x02;
219                 }               
220                 return data;
221         }
222
223         return joystick_ram[offset];
224 }
225
226 uint16 joystick_word_read(uint32 offset)
227 {
228         return ((uint16)joystick_byte_read((offset+0)&0x03) << 8) | joystick_byte_read((offset+1)&0x03);
229 }