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