]> Shamusworld >> Repos - virtualjaguar/blob - src/joystick.cpp
86940fc80dc1ae2d13f933784565fa9cfe6f8a79
[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 Hammons
7 // (C) 2010 Underground Software
8 //
9 // JLH = James Hammons <jlhamm@acm.org>
10 //
11 // Who  When        What
12 // ---  ----------  -------------------------------------------------------------
13 // JLH  01/16/2010  Created this log ;-)
14 //
15
16 #include "joystick.h"
17
18 //#include <SDL.h>
19 //#include <time.h>
20 #include <string.h>                     // For memset()
21 #include "gpu.h"
22 #include "jaguar.h"
23 #include "log.h"
24 #include "settings.h"
25
26 // Global vars
27
28 static uint8_t joystick_ram[4];
29 uint8_t joypad_0_buttons[21];
30 uint8_t joypad_1_buttons[21];
31
32 bool keyBuffer[21];
33
34 //SDL_Joystick * joystick1;
35
36 bool GUIKeyHeld = false;
37 extern int start_logging;
38 int gpu_start_log = 0;
39 int op_start_log = 0;
40 int blit_start_log = 0;
41 int effect_start = 0;
42 int effect_start2 = 0, effect_start3 = 0, effect_start4 = 0, effect_start5 = 0, effect_start6 = 0;
43 bool interactiveMode = false;
44 bool iLeft, iRight, iToggle = false;
45 bool keyHeld1 = false, keyHeld2 = false, keyHeld3 = false;
46 int objectPtr = 0;
47 bool startMemLog = false;
48 extern bool doDSPDis, doGPUDis;
49
50 bool blitterSingleStep = false;
51 bool bssGo = false;
52 bool bssHeld = false;
53
54
55 void JoystickInit(void)
56 {
57         JoystickReset();
58 }
59
60
61 void JoystickExec(void)
62 {
63         gpu_start_log = 0;                                                      // Only log while key down!
64         effect_start = 0;
65         effect_start2 = effect_start3 = effect_start4 = effect_start5 = effect_start6 = 0;
66         blit_start_log = 0;
67         iLeft = iRight = false;
68 }
69
70
71 void JoystickReset(void)
72 {
73         memset(joystick_ram, 0x00, 4);
74         memset(joypad_0_buttons, 0, 21);
75         memset(joypad_1_buttons, 0, 21);
76 }
77
78
79 void JoystickDone(void)
80 {
81 }
82
83
84 uint8_t JoystickReadByte(uint32_t offset)
85 {
86 // For now, until we can fix the 2nd controller... :-P
87 //memset(joypad_1_buttons, 0, 21);
88
89 #warning "No bounds checking done in JoystickReadByte!"
90 //      extern bool hardwareTypeNTSC;
91         offset &= 0x03;
92
93         if (offset == 0)
94         {
95                 uint8_t data = 0x00;
96                 int pad0Index = joystick_ram[1] & 0x0F;
97                 int pad1Index = (joystick_ram[1] >> 4) & 0x0F;
98
99 // This is bad--we're assuming that a bit is set in the last case. Might not be so!
100 // NOTE: values $7, B, D, & E are only legal ones for pad 0, (rows 3 to 0, in both cases)
101 //              $E, D, B, & 7 are only legal ones for pad 1
102 //       So the following code is WRONG! (now fixed! ;-)
103 // Also: we should explicitly check for those bit patterns, as other patterns
104 // are legal and yield other controllers... !!! FIX !!!
105 #warning "!!! Need to explicitly check for the proper bit combinations! !!!"
106
107                 if (!(pad0Index & 0x01))
108                         pad0Index = 0;
109                 else if (!(pad0Index & 0x02))
110                         pad0Index = 1;
111                 else if (!(pad0Index & 0x04))
112                         pad0Index = 2;
113                 else if (!(pad0Index & 0x08))
114                         pad0Index = 3;
115
116                 if (!(pad1Index & 0x01))
117                         pad1Index = 3;
118                 else if (!(pad1Index & 0x02))
119                         pad1Index = 2;
120                 else if (!(pad1Index & 0x04))
121                         pad1Index = 1;
122                 else if (!(pad1Index & 0x08))
123                         pad1Index = 0;
124
125                 if (joypad_0_buttons[(pad0Index << 2) + 0])     data |= 0x01;
126                 if (joypad_0_buttons[(pad0Index << 2) + 1]) data |= 0x02;
127                 if (joypad_0_buttons[(pad0Index << 2) + 2]) data |= 0x04;
128                 if (joypad_0_buttons[(pad0Index << 2) + 3]) data |= 0x08;
129                 if (joypad_1_buttons[(pad1Index << 2) + 0]) data |= 0x10;
130                 if (joypad_1_buttons[(pad1Index << 2) + 1]) data |= 0x20;
131                 if (joypad_1_buttons[(pad1Index << 2) + 2]) data |= 0x40;
132                 if (joypad_1_buttons[(pad1Index << 2) + 3]) data |= 0x80;
133
134                 return ~data;
135         }
136         else if (offset == 3)
137         {
138                 // Hardware ID returns NTSC/PAL identification bit here
139                 uint8_t data = 0x2F | (vjs.hardwareTypeNTSC ? 0x10 : 0x00);
140                 int pad0Index = joystick_ram[1] & 0x0F;
141                 int pad1Index = (joystick_ram[1] >> 4) & 0x0F;
142
143 //This is more stuff to add to the button reading, as the preceeding only
144 //yields 16 buttons...
145 #warning "!!! This reports TeamTap incorrectly when PAUSE pressed on controller #1 or #2 !!!"
146                 if (!(pad0Index & 0x01))
147                 {
148                         if (joypad_0_buttons[BUTTON_PAUSE])
149                                 data ^= 0x01;
150                         if (joypad_0_buttons[BUTTON_A])
151                                 data ^= 0x02;
152                 }
153                 else if (!(pad0Index & 0x02))
154                 {
155                         if (joypad_0_buttons[BUTTON_B])
156                                 data ^= 0x02;
157                 }
158                 else if (!(pad0Index & 0x04))
159                 {
160                         if (joypad_0_buttons[BUTTON_C])
161                                 data ^= 0x02;
162                 }
163                 else if (!(pad0Index & 0x08))
164                 {
165                         if (joypad_0_buttons[BUTTON_OPTION])
166                                 data ^= 0x02;
167                 }
168
169                 if (!(pad1Index & 0x08))
170                 {
171                         if (joypad_1_buttons[BUTTON_PAUSE])
172                                 data ^= 0x04;
173                         if (joypad_1_buttons[BUTTON_A])
174                                 data ^= 0x08;
175                 }
176                 else if (!(pad1Index & 0x04))
177                 {
178                         if (joypad_1_buttons[BUTTON_B])
179                                 data ^= 0x08;
180                 }
181                 else if (!(pad1Index & 0x02))
182                 {
183                         if (joypad_1_buttons[BUTTON_C])
184                                 data ^= 0x08;
185                 }
186                 else if (!(pad1Index & 0x01))
187                 {
188                         if (joypad_1_buttons[BUTTON_OPTION])
189                                 data ^= 0x08;
190                 }
191
192                 return data;
193         }
194
195         return joystick_ram[offset];
196 }
197
198
199 uint16_t JoystickReadWord(uint32_t offset)
200 {
201         return ((uint16_t)JoystickReadByte((offset + 0) & 0x03) << 8) | JoystickReadByte((offset + 1) & 0x03);
202 }
203
204
205 void JoystickWriteByte(uint32_t offset, uint8_t data)
206 {
207         joystick_ram[offset & 0x03] = data;
208 }
209
210
211 void JoystickWriteWord(uint32_t offset, uint16_t data)
212 {
213 #warning "No bounds checking done for JoystickWriteWord!"
214         offset &= 0x03;
215         joystick_ram[offset + 0] = (data >> 8) & 0xFF;
216         joystick_ram[offset + 1] = data & 0xFF;
217 }
218