Check potential non-existent global variables found in the DWARF information
[clinton/Virtual-Jaguar-Rx.git] / src / joystick.cpp
1 //
2 // Joystick handler
3 //
4 // by cal2
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Extensive rewrite by James Hammons
7 // (C) 2013 Underground Software
8 //
9 // JLH = James Hammons <jlhamm@acm.org>
10 // JPM = Jean-Paul Mari <djipi.mari@gmail.com>
11 //
12 // Who When What
13 // --- ---------- -------------------------------------------------------------
14 // JLH 01/16/2010 Created this log ;-)
15 // JPM 06/06/2016 Visual Studio support
16 //
17
18 #include "joystick.h"
19 #include <string.h> // For memset()
20 #include "gpu.h"
21 #include "jaguar.h"
22 #include "log.h"
23 #include "settings.h"
24
25 // Global vars
26
27 static uint8_t joystick_ram[4];
28 uint8_t joypad0Buttons[21];
29 uint8_t joypad1Buttons[21];
30 bool audioEnabled = false;
31 bool joysticksEnabled = false;
32
33
34 bool GUIKeyHeld = false;
35 extern int start_logging;
36 int gpu_start_log = 0;
37 int op_start_log = 0;
38 int blit_start_log = 0;
39 int effect_start = 0;
40 int effect_start2 = 0, effect_start3 = 0, effect_start4 = 0, effect_start5 = 0, effect_start6 = 0;
41 bool interactiveMode = false;
42 bool iLeft, iRight, iToggle = false;
43 bool keyHeld1 = false, keyHeld2 = false, keyHeld3 = false;
44 int objectPtr = 0;
45 bool startMemLog = false;
46 extern bool doDSPDis, doGPUDis;
47
48 bool blitterSingleStep = false;
49 bool bssGo = false;
50 bool bssHeld = false;
51
52
53 void JoystickInit(void)
54 {
55 JoystickReset();
56 }
57
58
59 void JoystickExec(void)
60 {
61 gpu_start_log = 0; // Only log while key down!
62 effect_start = 0;
63 effect_start2 = effect_start3 = effect_start4 = effect_start5 = effect_start6 = 0;
64 blit_start_log = 0;
65 iLeft = iRight = false;
66 }
67
68
69 void JoystickReset(void)
70 {
71 memset(joystick_ram, 0x00, 4);
72 memset(joypad0Buttons, 0, 21);
73 memset(joypad1Buttons, 0, 21);
74 }
75
76
77 void JoystickDone(void)
78 {
79 }
80
81
82 uint16_t JoystickReadWord(uint32_t offset)
83 {
84 // E, D, B, 7
85 uint8_t joypad0Offset[16] = {
86 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0C, 0xFF, 0xFF, 0xFF, 0x08, 0xFF, 0x04, 0x00, 0xFF
87 };
88 uint8_t joypad1Offset[16] = {
89 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0x04, 0xFF, 0x08, 0x0C, 0xFF
90 };
91
92 #ifdef _MSC_VER
93 #pragma message("Warning: No bounds checking done in JoystickReadByte!")
94 #else
95 #warning "No bounds checking done in JoystickReadByte!"
96 #endif // _MSC_VER
97 offset &= 0x03;
98
99 if (offset == 0)
100 {
101 if (!joysticksEnabled)
102 return 0xFFFF;
103
104 // Joystick data returns active low for buttons pressed, high for non-
105 // pressed.
106 uint16_t data = 0xFFFF;
107 uint8_t offset0 = joypad0Offset[joystick_ram[1] & 0x0F];
108 uint8_t offset1 = joypad1Offset[(joystick_ram[1] >> 4) & 0x0F];
109
110 if (offset0 != 0xFF)
111 {
112 uint16_t mask[4] = { 0xFEFF, 0xFDFF, 0xFBFF, 0xF7FF };
113 uint16_t msk2[4] = { 0xFFFF, 0xFFFD, 0xFFFB, 0xFFF7 };
114
115 for(uint8_t i=0; i<4; i++)
116 data &= (joypad0Buttons[offset0 + i] ? mask[i] : 0xFFFF);
117
118 data &= msk2[offset0 / 4];
119 }
120
121 if (offset1 != 0xFF)
122 {
123 uint16_t mask[4] = { 0xEFFF, 0xDFFF, 0xBFFF, 0x7FFF };
124 uint16_t msk2[4] = { 0xFF7F, 0xFFBF, 0xFFDF, 0xFFEF };
125
126 for(uint8_t i=0; i<4; i++)
127 data &= (joypad1Buttons[offset1 + i] ? mask[i] : 0xFFFF);
128
129 data &= msk2[offset1 / 4];
130 }
131
132 return data;
133 }
134 else if (offset == 2)
135 {
136 // Hardware ID returns NTSC/PAL identification bit here
137 // N.B.: On real H/W, bit 7 is *always* zero...!
138 uint16_t data = 0xFF6F | (vjs.hardwareTypeNTSC ? 0x10 : 0x00);
139
140 if (!joysticksEnabled)
141 return data;
142
143 // Joystick data returns active low for buttons pressed, high for non-
144 // pressed.
145 uint8_t offset0 = joypad0Offset[joystick_ram[1] & 0x0F];
146 uint8_t offset1 = joypad1Offset[(joystick_ram[1] >> 4) & 0x0F];
147
148 if (offset0 != 0xFF)
149 {
150 offset0 /= 4; // Make index 0, 1, 2, 3 instead of 0, 4, 8, 12
151 uint8_t mask[4][2] = { { BUTTON_A, BUTTON_PAUSE }, { BUTTON_B, 0xFF }, { BUTTON_C, 0xFF }, { BUTTON_OPTION, 0xFF } };
152 data &= (joypad0Buttons[mask[offset0][0]] ? 0xFFFD : 0xFFFF);
153
154 if (mask[offset0][1] != 0xFF)
155 data &= (joypad0Buttons[mask[offset0][1]] ? 0xFFFE : 0xFFFF);
156 }
157
158 if (offset1 != 0xFF)
159 {
160 offset1 /= 4; // Make index 0, 1, 2, 3 instead of 0, 4, 8, 12
161 uint8_t mask[4][2] = { { BUTTON_A, BUTTON_PAUSE }, { BUTTON_B, 0xFF }, { BUTTON_C, 0xFF }, { BUTTON_OPTION, 0xFF } };
162 data &= (joypad1Buttons[mask[offset1][0]] ? 0xFFF7 : 0xFFFF);
163
164 if (mask[offset1][1] != 0xFF)
165 data &= (joypad1Buttons[mask[offset1][1]] ? 0xFFFB : 0xFFFF);
166 }
167
168 return data;
169 }
170
171 return 0xFFFF;
172 }
173
174
175 void JoystickWriteWord(uint32_t offset, uint16_t data)
176 {
177 #ifdef _MSC_VER
178 #pragma message("Warning: No bounds checking done for JoystickWriteWord!")
179 #else
180 #warning "No bounds checking done for JoystickWriteWord!"
181 #endif // _MSC_VER
182 offset &= 0x03;
183 joystick_ram[offset + 0] = (data >> 8) & 0xFF;
184 joystick_ram[offset + 1] = data & 0xFF;
185
186 if (offset == 0)
187 {
188 audioEnabled = (data & 0x0100 ? true : false);
189 joysticksEnabled = (data & 0x8000 ? true : false);
190 }
191 }
192