5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Cleanups/fixes by James L. Hammons
7 // (C) 2010 Underground Software
9 // JLH = James L. Hammons <jlhamm@acm.org>
12 // --- ---------- -------------------------------------------------------------
13 // JLH 01/16/2010 Created this log ;-)
27 static uint8 joystick_ram[4];
28 uint8 joypad_0_buttons[21];
29 uint8 joypad_1_buttons[21];
33 SDL_Joystick * joystick1;
35 //extern bool finished;
36 ////extern bool showGUI;
37 bool GUIKeyHeld = false;
38 extern int start_logging;
39 int gpu_start_log = 0;
41 int blit_start_log = 0;
43 int effect_start2 = 0, effect_start3 = 0, effect_start4 = 0, effect_start5 = 0, effect_start6 = 0;
44 bool interactiveMode = false;
45 bool iLeft, iRight, iToggle = false;
46 bool keyHeld1 = false, keyHeld2 = false, keyHeld3 = false;
48 bool startMemLog = false;
49 extern bool doDSPDis, doGPUDis;
51 bool blitterSingleStep = false;
55 void JoystickInit(void)
60 void JoystickExec(void)
62 // uint8 * keystate = SDL_GetKeyState(NULL);
64 // memset(joypad_0_buttons, 0, 21);
65 // memset(joypad_1_buttons, 0, 21);
66 gpu_start_log = 0; // Only log while key down!
68 effect_start2 = effect_start3 = effect_start4 = effect_start5 = effect_start6 = 0;
70 iLeft = iRight = false;
73 if ((keystate[SDLK_LALT] || keystate[SDLK_RALT]) & keystate[SDLK_RETURN])
76 // Keybindings in order of U, D, L, R, C, B, A, Op, Pa, 0-9, #, *
77 // vjs.p1KeyBindings[0] = sdlemu_getval_int("p1k_up", SDLK_UP);
80 if (keystate[vjs.p1KeyBindings[0]])
81 joypad_0_buttons[BUTTON_U] = 0x01;
82 if (keystate[vjs.p1KeyBindings[1]])
83 joypad_0_buttons[BUTTON_D] = 0x01;
84 if (keystate[vjs.p1KeyBindings[2]])
85 joypad_0_buttons[BUTTON_L] = 0x01;
86 if (keystate[vjs.p1KeyBindings[3]])
87 joypad_0_buttons[BUTTON_R] = 0x01;
88 // The buttons are labelled C,B,A on the controller (going from left to right)
89 if (keystate[vjs.p1KeyBindings[4]])
90 joypad_0_buttons[BUTTON_C] = 0x01;
91 if (keystate[vjs.p1KeyBindings[5]])
92 joypad_0_buttons[BUTTON_B] = 0x01;
93 if (keystate[vjs.p1KeyBindings[6]])
94 joypad_0_buttons[BUTTON_A] = 0x01;
95 //I may yet move these to O and P...
96 if (keystate[vjs.p1KeyBindings[7]])
97 joypad_0_buttons[BUTTON_OPTION] = 0x01;
98 if (keystate[vjs.p1KeyBindings[8]])
99 joypad_0_buttons[BUTTON_PAUSE] = 0x01;
101 if (keystate[vjs.p1KeyBindings[9]])
102 joypad_0_buttons[BUTTON_0] = 0x01;
103 if (keystate[vjs.p1KeyBindings[10]])
104 joypad_0_buttons[BUTTON_1] = 0x01;
105 if (keystate[vjs.p1KeyBindings[11]])
106 joypad_0_buttons[BUTTON_2] = 0x01;
107 if (keystate[vjs.p1KeyBindings[12]])
108 joypad_0_buttons[BUTTON_3] = 0x01;
109 if (keystate[vjs.p1KeyBindings[13]])
110 joypad_0_buttons[BUTTON_4] = 0x01;
111 if (keystate[vjs.p1KeyBindings[14]])
112 joypad_0_buttons[BUTTON_5] = 0x01;
113 if (keystate[vjs.p1KeyBindings[15]])
114 joypad_0_buttons[BUTTON_6] = 0x01;
115 if (keystate[vjs.p1KeyBindings[16]])
116 joypad_0_buttons[BUTTON_7] = 0x01;
117 if (keystate[vjs.p1KeyBindings[17]])
118 joypad_0_buttons[BUTTON_8] = 0x01;
119 if (keystate[vjs.p1KeyBindings[18]])
120 joypad_0_buttons[BUTTON_9] = 0x01;
121 if (keystate[vjs.p1KeyBindings[19]])
122 joypad_0_buttons[BUTTON_s] = 0x01;
123 if (keystate[vjs.p1KeyBindings[20]])
124 joypad_0_buttons[BUTTON_d] = 0x01;
127 joypad_0_buttons[BUTTON_U] = 0x01;
129 joypad_0_buttons[BUTTON_D] = 0x01;
131 joypad_0_buttons[BUTTON_L] = 0x01;
133 joypad_0_buttons[BUTTON_R] = 0x01;
134 // The buttons are labelled C,B,A on the controller (going from left to right)
136 joypad_0_buttons[BUTTON_C] = 0x01;
138 joypad_0_buttons[BUTTON_B] = 0x01;
140 joypad_0_buttons[BUTTON_A] = 0x01;
141 //I may yet move these to O and P...
143 joypad_0_buttons[BUTTON_OPTION] = 0x01;
145 joypad_0_buttons[BUTTON_PAUSE] = 0x01;
148 joypad_0_buttons[BUTTON_0] = 0x01;
150 joypad_0_buttons[BUTTON_1] = 0x01;
152 joypad_0_buttons[BUTTON_2] = 0x01;
154 joypad_0_buttons[BUTTON_3] = 0x01;
156 joypad_0_buttons[BUTTON_4] = 0x01;
158 joypad_0_buttons[BUTTON_5] = 0x01;
160 joypad_0_buttons[BUTTON_6] = 0x01;
162 joypad_0_buttons[BUTTON_7] = 0x01;
164 joypad_0_buttons[BUTTON_8] = 0x01;
166 joypad_0_buttons[BUTTON_9] = 0x01;
168 joypad_0_buttons[BUTTON_s] = 0x01;
170 joypad_0_buttons[BUTTON_d] = 0x01;
173 #warning "!!! FIX !!! (debounceRunKey)"
174 // extern bool debounceRunKey;
176 if (keystate[SDLK_ESCAPE])
179 #warning "!!! FIX !!! (finished = true)"
183 debounceRunKey = false;
185 if (keystate[SDLK_TAB])
188 #warning "!!! FIX !!! (showGUI = !showGUI, ...)"
189 ;// showGUI = !showGUI, GUIKeyHeld = true;
194 if (keystate[SDLK_q])
196 if (keystate[SDLK_w])
198 // if (keystate[SDLK_u]) jaguar_long_write(0xf1c384,jaguar_long_read(0xf1c384)+1);
199 if (keystate[SDLK_d])
201 if (keystate[SDLK_l])
203 if (keystate[SDLK_o])
205 if (keystate[SDLK_b])
208 if (keystate[SDLK_1])
210 if (keystate[SDLK_2])
212 if (keystate[SDLK_3])
214 if (keystate[SDLK_4])
216 if (keystate[SDLK_5])
218 if (keystate[SDLK_6])
221 if (keystate[SDLK_i])
222 interactiveMode = true;
224 if (keystate[SDLK_8] && interactiveMode)
227 objectPtr--, keyHeld1 = true;
232 if (keystate[SDLK_0] && interactiveMode)
235 objectPtr++, keyHeld2 = true;
240 if (keystate[SDLK_9] && interactiveMode)
243 iToggle = !iToggle, keyHeld3 = true;
248 if (keystate[SDLK_e])
250 if (keystate[SDLK_r])
251 WriteLog("\n--------> MARK!\n\n");
252 if (keystate[SDLK_t])
254 if (keystate[SDLK_y])
257 // BLITTER single step
258 if (keystate[SDLK_F5])
259 blitterSingleStep = true;
261 if (keystate[SDLK_F6])
272 // We need to ensure that illegal combinations are not possible.
273 // So, we do a simple minded way here...
274 // It would be better to check to see which one was pressed last
275 // and discard that one, but for now...
276 //This didn't work... Was still able to do bad combination.
277 //It's because the GUI is changing this *after* we fix it here.
279 if (joypad_0_buttons[BUTTON_R] && joypad_0_buttons[BUTTON_L])
280 joypad_0_buttons[BUTTON_L] = 0;
282 if (joypad_0_buttons[BUTTON_U] && joypad_0_buttons[BUTTON_D])
283 joypad_0_buttons[BUTTON_D] = 0;
286 // Joystick support [nwagenaar]
290 extern SDL_Joystick * joystick1;
291 int16 x = SDL_JoystickGetAxis(joystick1, 0),
292 y = SDL_JoystickGetAxis(joystick1, 1);
295 joypad_0_buttons[BUTTON_R] = 0x01;
297 joypad_0_buttons[BUTTON_L] = 0x01;
299 joypad_0_buttons[BUTTON_D] = 0x01;
301 joypad_0_buttons[BUTTON_U] = 0x01;
303 if (SDL_JoystickGetButton(joystick1, 0) == SDL_PRESSED)
304 joypad_0_buttons[BUTTON_A] = 0x01;
305 if (SDL_JoystickGetButton(joystick1, 1) == SDL_PRESSED)
306 joypad_0_buttons[BUTTON_B] = 0x01;
307 if (SDL_JoystickGetButton(joystick1, 2) == SDL_PRESSED)
308 joypad_0_buttons[BUTTON_C] = 0x01;
311 // Needed to ensure that the events queue is empty [nwagenaar]
315 void JoystickReset(void)
317 memset(joystick_ram, 0x00, 4);
318 memset(joypad_0_buttons, 0, 21);
319 memset(joypad_1_buttons, 0, 21);
322 void JoystickDone(void)
326 uint8 JoystickReadByte(uint32 offset)
328 // For now, until we can fix the 2nd controller... :-P
329 //memset(joypad_1_buttons, 0, 21);
331 #warning "No bounds checking done in JoystickReadByte!"
332 // extern bool hardwareTypeNTSC;
338 int pad0Index = joystick_ram[1] & 0x0F;
339 int pad1Index = (joystick_ram[1] >> 4) & 0x0F;
341 // This is bad--we're assuming that a bit is set in the last case. Might not be so!
342 // NOTE: values $7, B, D, & E are only legal ones for pad 0, (rows 3 to 0, in both cases)
343 // $E, D, B, & 7 are only legal ones for pad 1
344 // So the following code is WRONG! (now fixed! ;-)
345 // Also: we should explicitly check for those bit patterns, as other patterns
346 // are legal and yield other controllers... !!! FIX !!!
347 #warning "!!! Need to explicitly check for the proper bit combinations! !!!"
349 if (!(pad0Index & 0x01))
351 else if (!(pad0Index & 0x02))
353 else if (!(pad0Index & 0x04))
355 else if (!(pad0Index & 0x08))
358 if (!(pad1Index & 0x01))
360 else if (!(pad1Index & 0x02))
362 else if (!(pad1Index & 0x04))
364 else if (!(pad1Index & 0x08))
367 if (joypad_0_buttons[(pad0Index << 2) + 0]) data |= 0x01;
368 if (joypad_0_buttons[(pad0Index << 2) + 1]) data |= 0x02;
369 if (joypad_0_buttons[(pad0Index << 2) + 2]) data |= 0x04;
370 if (joypad_0_buttons[(pad0Index << 2) + 3]) data |= 0x08;
371 if (joypad_1_buttons[(pad1Index << 2) + 0]) data |= 0x10;
372 if (joypad_1_buttons[(pad1Index << 2) + 1]) data |= 0x20;
373 if (joypad_1_buttons[(pad1Index << 2) + 2]) data |= 0x40;
374 if (joypad_1_buttons[(pad1Index << 2) + 3]) data |= 0x80;
378 else if (offset == 3)
380 // Hardware ID returns NTSC/PAL identification bit here
381 uint8 data = 0x2F | (vjs.hardwareTypeNTSC ? 0x10 : 0x00);
382 int pad0Index = joystick_ram[1] & 0x0F;
383 int pad1Index = (joystick_ram[1] >> 4) & 0x0F;
385 //This is more stuff to add to the button reading, as the preceeding only
386 //yields 16 buttons...
387 #warning "!!! This reports TeamTap incorrectly when PAUSE pressed on controller #1 or #2 !!!"
388 if (!(pad0Index & 0x01))
390 if (joypad_0_buttons[BUTTON_PAUSE])
392 if (joypad_0_buttons[BUTTON_A])
395 else if (!(pad0Index & 0x02))
397 if (joypad_0_buttons[BUTTON_B])
400 else if (!(pad0Index & 0x04))
402 if (joypad_0_buttons[BUTTON_C])
405 else if (!(pad0Index & 0x08))
407 if (joypad_0_buttons[BUTTON_OPTION])
411 if (!(pad1Index & 0x08))
413 if (joypad_1_buttons[BUTTON_PAUSE])
415 if (joypad_1_buttons[BUTTON_A])
418 else if (!(pad1Index & 0x04))
420 if (joypad_1_buttons[BUTTON_B])
423 else if (!(pad1Index & 0x02))
425 if (joypad_1_buttons[BUTTON_C])
428 else if (!(pad1Index & 0x01))
430 if (joypad_1_buttons[BUTTON_OPTION])
437 return joystick_ram[offset];
440 uint16 JoystickReadWord(uint32 offset)
442 return ((uint16)JoystickReadByte((offset + 0) & 0x03) << 8) | JoystickReadByte((offset + 1) & 0x03);
445 void JoystickWriteByte(uint32 offset, uint8 data)
447 joystick_ram[offset & 0x03] = data;
450 void JoystickWriteWord(uint32 offset, uint16 data)
452 #warning "No bounds checking done for JoystickWriteWord!"
454 joystick_ram[offset + 0] = (data >> 8) & 0xFF;
455 joystick_ram[offset + 1] = data & 0xFF;