1
2//============================================================================
3//
4// SSSS tt lll lll
5// SS SS tt ll ll
6// SS tttttt eeee ll ll aaaa
7// SSSS tt ee ee ll ll aa
8// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
9// SS SS tt ee ll ll aa aa
10// SSSS ttt eeeee llll llll aaaaa
11//
12// Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony
13// and the Stella Team
14//
15// See the file "License.txt" for information on usage and redistribution of
16// this file, and for a DISCLAIMER OF ALL WARRANTIES.
17//============================================================================
18
19#include "Settings.hxx"
20#include "Logger.hxx"
21
22#include "ControllerDetector.hxx"
23
24// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
25Controller::Type ControllerDetector::detectType(const uInt8* image, size_t size,
26 const Controller::Type type, const Controller::Jack port, const Settings& settings)
27{
28 if(type == Controller::Type::Unknown || settings.getBool("rominfo"))
29 {
30 Controller::Type detectedType = autodetectPort(image, size, port, settings);
31
32 if(type != Controller::Type::Unknown && type != detectedType)
33 {
34 cerr << "Controller auto-detection not consistent: "
35 << Controller::getName(type) << ", " << Controller::getName(detectedType) << endl;
36 }
37 Logger::debug(Controller::getName(detectedType) + " detected for " +
38 (port == Controller::Jack::Left ? "left" : "right") + " port");
39 return detectedType;
40 }
41
42 return type;
43}
44
45// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
46string ControllerDetector::detectName(const uInt8* image, size_t size,
47 const Controller::Type controller, const Controller::Jack port,
48 const Settings& settings)
49{
50 return Controller::getName(detectType(image, size, controller, port, settings));
51}
52
53// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
54Controller::Type ControllerDetector::autodetectPort(const uInt8* image, size_t size,
55 Controller::Jack port, const Settings& settings)
56{
57 // default type joystick
58 Controller::Type type = Controller::Type::Joystick;
59
60 if(isProbablySaveKey(image, size, port))
61 type = Controller::Type::SaveKey;
62 else if(usesJoystickButton(image, size, port))
63 {
64 if(isProbablyTrakBall(image, size))
65 type = Controller::Type::TrakBall;
66 else if(isProbablyAtariMouse(image, size))
67 type = Controller::Type::AmigaMouse;
68 else if(isProbablyAmigaMouse(image, size))
69 type = Controller::Type::AmigaMouse;
70 else if(usesKeyboard(image, size, port))
71 type = Controller::Type::Keyboard;
72 else if(usesGenesisButton(image, size, port))
73
74 type = Controller::Type::Genesis;
75 }
76 else
77 {
78 if(usesPaddle(image, size, port, settings))
79 type = Controller::Type::Paddles;
80 }
81 // TODO: BOOSTERGRIP, DRIVING, MINDLINK, ATARIVOX, KIDVID
82 // not detectable: PADDLES_IAXIS, PADDLES_IAXDR
83 return type;
84}
85
86// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
87bool ControllerDetector::searchForBytes(const uInt8* image, size_t imagesize,
88 const uInt8* signature, uInt32 sigsize)
89{
90 if (imagesize >= sigsize)
91 for(uInt32 i = 0; i < imagesize - sigsize; ++i)
92 {
93 uInt32 matches = 0;
94 for(uInt32 j = 0; j < sigsize; ++j)
95 {
96 if(image[i + j] == signature[j])
97 ++matches;
98 else
99 break;
100 }
101 if(matches == sigsize)
102 {
103 return true;
104 }
105 }
106
107 return false;
108}
109
110// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
111bool ControllerDetector::usesJoystickButton(const uInt8* image, size_t size,
112 Controller::Jack port)
113{
114 if(port == Controller::Jack::Left)
115 {
116 // check for INPT4 access
117 const int NUM_SIGS_0 = 23;
118 const int SIG_SIZE_0 = 3;
119 uInt8 signature_0[NUM_SIGS_0][SIG_SIZE_0] = {
120 { 0x24, 0x0c, 0x10 }, // bit INPT4; bpl (joystick games only)
121 { 0x24, 0x0c, 0x30 }, // bit INPT4; bmi (joystick games only)
122 { 0xa5, 0x0c, 0x10 }, // lda INPT4; bpl (joystick games only)
123 { 0xa5, 0x0c, 0x30 }, // lda INPT4; bmi (joystick games only)
124 { 0xb5, 0x0c, 0x10 }, // lda INPT4,x; bpl (joystick games only)
125 { 0xb5, 0x0c, 0x30 }, // lda INPT4,x; bmi (joystick games only)
126 { 0x24, 0x3c, 0x10 }, // bit INPT4|$30; bpl (joystick games + Compumate)
127 { 0x24, 0x3c, 0x30 }, // bit INPT4|$30; bmi (joystick, keyboard and mindlink games)
128 { 0xa5, 0x3c, 0x10 }, // lda INPT4|$30; bpl (joystick and keyboard games)
129 { 0xa5, 0x3c, 0x30 }, // lda INPT4|$30; bmi (joystick, keyboard and mindlink games)
130 { 0xb5, 0x3c, 0x10 }, // lda INPT4|$30,x; bpl (joystick, keyboard and driving games)
131 { 0xb5, 0x3c, 0x30 }, // lda INPT4|$30,x; bmi (joystick and keyboard games)
132 { 0xb4, 0x0c, 0x30 }, // ldy INPT4|$30,x; bmi (joystick games only)
133 { 0xa5, 0x3c, 0x2a }, // ldy INPT4|$30; rol (joystick games only)
134 { 0xa6, 0x3c, 0x8e }, // ldx INPT4|$30; stx (joystick games only)
135 { 0xa4, 0x3c, 0x8c }, // ldy INPT4; sty (joystick games only, Scramble)
136 { 0xa5, 0x0c, 0x8d }, // lda INPT4; sta (joystick games only, Super Cobra Arcade)
137 { 0xa4, 0x0c, 0x30 }, // ldy INPT4|; bmi (only Game of Concentration)
138 { 0xa4, 0x3c, 0x30 }, // ldy INPT4|$30; bmi (only Game of Concentration)
139 { 0xa5, 0x0c, 0x25 }, // lda INPT4; and (joystick games only)
140 { 0xa6, 0x3c, 0x30 }, // ldx INPT4|$30; bmi (joystick games only)
141 { 0xa6, 0x0c, 0x30 }, // ldx INPT4; bmi
142 { 0xa5, 0x0c, 0x0a }, // lda INPT4; asl (joystick games only)
143 };
144 const int NUM_SIGS_1 = 9;
145 const int SIG_SIZE_1 = 4;
146 uInt8 signature_1[NUM_SIGS_1][SIG_SIZE_1] = {
147 { 0xb9, 0x0c, 0x00, 0x10 }, // lda INPT4,y; bpl (joystick games only)
148 { 0xb9, 0x0c, 0x00, 0x30 }, // lda INPT4,y; bmi (joystick games only)
149 { 0xb9, 0x3c, 0x00, 0x10 }, // lda INPT4,y; bpl (joystick games only)
150 { 0xb9, 0x3c, 0x00, 0x30 }, // lda INPT4,y; bmi (joystick games only)
151 { 0xa5, 0x0c, 0x0a, 0xb0 }, // lda INPT4; asl; bcs (joystick games only)
152 { 0xb5, 0x0c, 0x29, 0x80 }, // lda INPT4,x; and #$80 (joystick games only)
153 { 0xb5, 0x3c, 0x29, 0x80 }, // lda INPT4|$30,x; and #$80 (joystick games only)
154 { 0xa5, 0x0c, 0x29, 0x80 }, // lda INPT4; and #$80 (joystick games only)
155 { 0xa5, 0x3c, 0x29, 0x80 }, // lda INPT4|$30; and #$80 (joystick games only)
156 };
157 const int NUM_SIGS_2 = 9;
158 const int SIG_SIZE_2 = 5;
159 uInt8 signature_2[NUM_SIGS_2][SIG_SIZE_2] = {
160 { 0xa5, 0x0c, 0x25, 0x0d, 0x10 }, // lda INPT4; and INPT5; bpl (joystick games only)
161 { 0xa5, 0x0c, 0x25, 0x0d, 0x30 }, // lda INPT4; and INPT5; bmi (joystick games only)
162 { 0xa5, 0x3c, 0x25, 0x3d, 0x10 }, // lda INPT4|$30; and INPT5|$30; bpl (joystick games only)
163 { 0xa5, 0x3c, 0x25, 0x3d, 0x30 }, // lda INPT4|$30; and INPT5|$30; bmi (joystick games only)
164 { 0xb5, 0x38, 0x29, 0x80, 0xd0 }, // lda INPT0|$30,y; and #$80; bne (Basic Programming)
165 { 0xa9, 0x80, 0x24, 0x0c, 0xd0 }, // lda #$80; bit INPT4; bne (bBasic)
166 { 0xa5, 0x0c, 0x29, 0x80, 0xd0 }, // lda INPT4; and #$80; bne (joystick games only)
167 { 0xa5, 0x3c, 0x29, 0x80, 0xd0 }, // lda INPT4|$30; and #$80; bne (joystick games only)
168 { 0xad, 0x0c, 0x00, 0x29, 0x80 }, // lda.w INPT4|$30; and #$80 (joystick games only)
169 };
170
171 for(uInt32 i = 0; i < NUM_SIGS_0; ++i)
172 if(searchForBytes(image, size, signature_0[i], SIG_SIZE_0))
173 return true;
174
175 for(uInt32 i = 0; i < NUM_SIGS_1; ++i)
176 if(searchForBytes(image, size, signature_1[i], SIG_SIZE_1))
177 return true;
178
179 for(uInt32 i = 0; i < NUM_SIGS_2; ++i)
180 if(searchForBytes(image, size, signature_2[i], SIG_SIZE_2))
181 return true;
182 }
183 else if(port == Controller::Jack::Right)
184 {
185 // check for INPT5 and indexed INPT4 access
186 const int NUM_SIGS_0 = 16;
187 const int SIG_SIZE_0 = 3;
188 uInt8 signature_0[NUM_SIGS_0][SIG_SIZE_0] = {
189 { 0x24, 0x0d, 0x10 }, // bit INPT5; bpl (joystick games only)
190 { 0x24, 0x0d, 0x30 }, // bit INPT5; bmi (joystick games only)
191 { 0xa5, 0x0d, 0x10 }, // lda INPT5; bpl (joystick games only)
192 { 0xa5, 0x0d, 0x30 }, // lda INPT5; bmi (joystick games only)
193 { 0xb5, 0x0c, 0x10 }, // lda INPT4,x; bpl (joystick games only)
194 { 0xb5, 0x0c, 0x30 }, // lda INPT4,x; bmi (joystick games only)
195 { 0x24, 0x3d, 0x10 }, // bit INPT5|$30; bpl (joystick games, Compumate)
196 { 0x24, 0x3d, 0x30 }, // bit INPT5|$30; bmi (joystick and keyboard games)
197 { 0xa5, 0x3d, 0x10 }, // lda INPT5|$30; bpl (joystick games only)
198 { 0xa5, 0x3d, 0x30 }, // lda INPT5|$30; bmi (joystick and keyboard games)
199 { 0xb5, 0x3c, 0x10 }, // lda INPT4|$30,x; bpl (joystick, keyboard and driving games)
200 { 0xb5, 0x3c, 0x30 }, // lda INPT4|$30,x; bmi (joystick and keyboard games)
201 { 0xa4, 0x3d, 0x30 }, // ldy INPT5; bmi (only Game of Concentration)
202 { 0xa5, 0x0d, 0x25 }, // lda INPT5; and (joystick games only)
203 { 0xa6, 0x3d, 0x30 }, // ldx INPT5|$30; bmi (joystick games only)
204 { 0xa6, 0x0d, 0x30 }, // ldx INPT5; bmi
205 };
206 const int NUM_SIGS_1 = 7;
207 const int SIG_SIZE_1 = 4;
208 uInt8 signature_1[NUM_SIGS_1][SIG_SIZE_1] = {
209 { 0xb9, 0x0c, 0x00, 0x10 }, // lda INPT4,y; bpl (joystick games only)
210 { 0xb9, 0x0c, 0x00, 0x30 }, // lda INPT4,y; bmi (joystick games only)
211 { 0xb9, 0x3c, 0x00, 0x10 }, // lda INPT4,y; bpl (joystick games only)
212 { 0xb9, 0x3c, 0x00, 0x30 }, // lda INPT4,y; bmi (joystick games only)
213 { 0xb5, 0x0c, 0x29, 0x80 }, // lda INPT4,x; and #$80 (joystick games only)
214 { 0xb5, 0x3c, 0x29, 0x80 }, // lda INPT4|$30,x; and #$80 (joystick games only)
215 { 0xa5, 0x3d, 0x29, 0x80 }, // lda INPT5|$30; and #$80 (joystick games only)
216 };
217 const int NUM_SIGS_2 = 3;
218 const int SIG_SIZE_2 = 5;
219 uInt8 signature_2[NUM_SIGS_2][SIG_SIZE_2] = {
220 { 0xb5, 0x38, 0x29, 0x80, 0xd0 }, // lda INPT0|$30,y; and #$80; bne (Basic Programming)
221 { 0xa9, 0x80, 0x24, 0x0d, 0xd0 }, // lda #$80; bit INPT5; bne (bBasic)
222 { 0xad, 0x0d, 0x00, 0x29, 0x80 }, // lda.w INPT5|$30; and #$80 (joystick games only)
223 };
224
225 for(uInt32 i = 0; i < NUM_SIGS_0; ++i)
226 if(searchForBytes(image, size, signature_0[i], SIG_SIZE_0))
227 return true;
228
229 for(uInt32 i = 0; i < NUM_SIGS_1; ++i)
230 if(searchForBytes(image, size, signature_1[i], SIG_SIZE_1))
231 return true;
232
233 for(uInt32 i = 0; i < NUM_SIGS_2; ++i)
234 if(searchForBytes(image, size, signature_2[i], SIG_SIZE_2))
235 return true;
236 }
237 return false;
238}
239
240// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
241bool ControllerDetector::usesKeyboard(const uInt8* image, size_t size,
242 Controller::Jack port)
243{
244 if(port == Controller::Jack::Left)
245 {
246 // check for INPT0 *AND* INPT1 access
247 const int NUM_SIGS_0_0 = 6;
248 const int SIG_SIZE_0_0 = 3;
249 uInt8 signature_0_0[NUM_SIGS_0_0][SIG_SIZE_0_0] = {
250 { 0x24, 0x38, 0x30 }, // bit INPT0|$30; bmi
251 { 0xa5, 0x38, 0x10 }, // lda INPT0|$30; bpl
252 { 0xa4, 0x38, 0x30 }, // ldy INPT0|$30; bmi
253 { 0xb5, 0x38, 0x30 }, // lda INPT0|$30,x; bmi
254 { 0x24, 0x08, 0x30 }, // bit INPT0; bmi
255 { 0xa6, 0x08, 0x30 }, // ldx INPT0; bmi
256 };
257 const int NUM_SIGS_0_2 = 1;
258 const int SIG_SIZE_0_2 = 5;
259 uInt8 signature_0_2[NUM_SIGS_0_2][SIG_SIZE_0_2] = {
260 { 0xb5, 0x38, 0x29, 0x80, 0xd0 }, // lda INPT0,x; and #80; bne
261 };
262
263 const int NUM_SIGS_1_0 = 7;
264 const int SIG_SIZE_1_0 = 3;
265 uInt8 signature_1_0[NUM_SIGS_1_0][SIG_SIZE_1_0] = {
266 { 0x24, 0x39, 0x10 }, // bit INPT1|$30; bpl
267 { 0x24, 0x39, 0x30 }, // bit INPT1|$30; bmi
268 { 0xa5, 0x39, 0x10 }, // lda INPT1|$30; bpl
269 { 0xa4, 0x39, 0x30 }, // ldy INPT1|$30; bmi
270 { 0xb5, 0x38, 0x30 }, // lda INPT0|$30,x; bmi
271 { 0x24, 0x09, 0x30 }, // bit INPT1; bmi
272 { 0xa6, 0x09, 0x30 }, // ldx INPT1; bmi
273 };
274 const int NUM_SIGS_1_2 = 1;
275 const int SIG_SIZE_1_2 = 5;
276 uInt8 signature_1_2[NUM_SIGS_1_2][SIG_SIZE_1_2] = {
277 { 0xb5, 0x38, 0x29, 0x80, 0xd0 }, // lda INPT0,x; and #80; bne
278 };
279
280 bool found = false;
281
282 for(uInt32 i = 0; i < NUM_SIGS_0_0; ++i)
283 if(searchForBytes(image, size, signature_0_0[i], SIG_SIZE_0_0))
284 {
285 found = true;
286 break;
287 }
288 if(!found)
289 for(uInt32 i = 0; i < NUM_SIGS_0_2; ++i)
290 if(searchForBytes(image, size, signature_0_2[i], SIG_SIZE_0_2))
291 {
292 found = true;
293 break;
294 }
295 if(found)
296 {
297 for(uInt32 j = 0; j < NUM_SIGS_1_0; ++j)
298 if(searchForBytes(image, size, signature_1_0[j], SIG_SIZE_1_0))
299 {
300 return true;
301 }
302
303 for(uInt32 j = 0; j < NUM_SIGS_1_2; ++j)
304 if(searchForBytes(image, size, signature_1_2[j], SIG_SIZE_1_2))
305 {
306 return true;
307 }
308 }
309 }
310 else if(port == Controller::Jack::Right)
311 {
312 // check for INPT2 *AND* INPT3 access
313 const int NUM_SIGS_0_0 = 5;
314 const int SIG_SIZE_0_0 = 3;
315 uInt8 signature_0_0[NUM_SIGS_0_0][SIG_SIZE_0_0] = {
316 { 0x24, 0x3a, 0x30 }, // bit INPT2|$30; bmi
317 { 0xa5, 0x3a, 0x10 }, // lda INPT2|$30; bpl
318 { 0xa4, 0x3a, 0x30 }, // ldy INPT2|$30; bmi
319 { 0x24, 0x0a, 0x30 }, // bit INPT2; bmi
320 { 0xa6, 0x0a, 0x30 }, // ldx INPT2; bmi
321 };
322 const int NUM_SIGS_0_2 = 1;
323 const int SIG_SIZE_0_2 = 5;
324 uInt8 signature_0_2[NUM_SIGS_0_2][SIG_SIZE_0_2] = {
325 { 0xb5, 0x38, 0x29, 0x80, 0xd0 }, // lda INPT2,x; and #80; bne
326 };
327
328 const int NUM_SIGS_1_0 = 5;
329 const int SIG_SIZE_1_0 = 3;
330 uInt8 signature_1_0[NUM_SIGS_1_0][SIG_SIZE_1_0] = {
331 { 0x24, 0x3b, 0x30 }, // bit INPT3|$30; bmi
332 { 0xa5, 0x3b, 0x10 }, // lda INPT3|$30; bpl
333 { 0xa4, 0x3b, 0x30 }, // ldy INPT3|$30; bmi
334 { 0x24, 0x0b, 0x30 }, // bit INPT3; bmi
335 { 0xa6, 0x0b, 0x30 }, // ldx INPT3; bmi
336 };
337 const int NUM_SIGS_1_2 = 1;
338 const int SIG_SIZE_1_2 = 5;
339 uInt8 signature_1_2[NUM_SIGS_1_2][SIG_SIZE_1_2] = {
340 { 0xb5, 0x38, 0x29, 0x80, 0xd0 }, // lda INPT2,x; and #80; bne
341 };
342
343 bool found = false;
344
345 for(uInt32 i = 0; i < NUM_SIGS_0_0; ++i)
346 if(searchForBytes(image, size, signature_0_0[i], SIG_SIZE_0_0))
347 {
348 found = true;
349 break;
350 }
351
352 if(!found)
353 for(uInt32 i = 0; i < NUM_SIGS_0_2; ++i)
354 if(searchForBytes(image, size, signature_0_2[i], SIG_SIZE_0_2))
355 {
356 found = true;
357 break;
358 }
359
360 if(found)
361 {
362 for(uInt32 j = 0; j < NUM_SIGS_1_0; ++j)
363 if(searchForBytes(image, size, signature_1_0[j], SIG_SIZE_1_0))
364 {
365 return true;
366 }
367
368 for(uInt32 j = 0; j < NUM_SIGS_1_2; ++j)
369 if(searchForBytes(image, size, signature_1_2[j], SIG_SIZE_1_2))
370 {
371 return true;
372 }
373 }
374 }
375
376 return false;
377}
378
379// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
380bool ControllerDetector::usesGenesisButton(const uInt8* image, size_t size,
381 Controller::Jack port)
382{
383 if(port == Controller::Jack::Left)
384 {
385 // check for INPT1 access
386 const int NUM_SIGS_0 = 18;
387 const int SIG_SIZE_0 = 3;
388 uInt8 signature_0[NUM_SIGS_0][SIG_SIZE_0] = {
389 { 0x24, 0x09, 0x10 }, // bit INPT1; bpl (Genesis only)
390 { 0x24, 0x09, 0x30 }, // bit INPT1; bmi (paddle ROMS too)
391 { 0xa5, 0x09, 0x10 }, // lda INPT1; bpl (paddle ROMS too)
392 { 0xa5, 0x09, 0x30 }, // lda INPT1; bmi (paddle ROMS too)
393 { 0xa4, 0x09, 0x30 }, // ldy INPT1; bmi (Genesis only)
394 { 0xa6, 0x09, 0x30 }, // ldx INPT1; bmi (Genesis only)
395 { 0x24, 0x39, 0x10 }, // bit INPT1|$30; bpl (keyboard and paddle ROMS too)
396 { 0x24, 0x39, 0x30 }, // bit INPT1|$30; bmi (keyboard and paddle ROMS too)
397 { 0xa5, 0x39, 0x10 }, // lda INPT1|$30; bpl (keyboard ROMS too)
398 { 0xa5, 0x39, 0x30 }, // lda INPT1|$30; bmi (keyboard and paddle ROMS too)
399 { 0xa4, 0x39, 0x30 }, // ldy INPT1|$30; bmi (keyboard ROMS too)
400 { 0xa5, 0x39, 0x6a }, // lda INPT1|$30; ror (Genesis only)
401 { 0xa6, 0x39, 0x8e }, // ldx INPT1|$30; stx (Genesis only)
402 { 0xa4, 0x39, 0x8c }, // ldy INPT1|$30; sty (Genesis only, Scramble)
403 { 0xa5, 0x09, 0x8d }, // lda INPT1; sta (Genesis only, Super Cobra Arcade)
404 { 0xa5, 0x09, 0x29 }, // lda INPT1; and (Genesis only)
405 { 0x25, 0x39, 0x30 }, // and INPT1|$30; bmi (Genesis only)
406 { 0x25, 0x09, 0x10 }, // and INPT1; bpl (Genesis only)
407 };
408 for(uInt32 i = 0; i < NUM_SIGS_0; ++i)
409 if(searchForBytes(image, size, signature_0[i], SIG_SIZE_0))
410 return true;
411 }
412 else if(port == Controller::Jack::Right)
413 {
414 // check for INPT3 access
415 const int NUM_SIGS_0 = 10;
416 const int SIG_SIZE_0 = 3;
417 uInt8 signature_0[NUM_SIGS_0][SIG_SIZE_0] = {
418 { 0x24, 0x0b, 0x10 }, // bit INPT3; bpl
419 { 0x24, 0x0b, 0x30 }, // bit INPT3; bmi
420 { 0xa5, 0x0b, 0x10 }, // lda INPT3; bpl
421 { 0xa5, 0x0b, 0x30 }, // lda INPT3; bmi
422 { 0x24, 0x3b, 0x10 }, // bit INPT3|$30; bpl
423 { 0x24, 0x3b, 0x30 }, // bit INPT3|$30; bmi
424 { 0xa5, 0x3b, 0x10 }, // lda INPT3|$30; bpl
425 { 0xa5, 0x3b, 0x30 }, // lda INPT3|$30; bmi
426 { 0xa6, 0x3b, 0x8e }, // ldx INPT3|$30; stx
427 { 0x25, 0x0b, 0x10 }, // and INPT3; bpl (Genesis only)
428 };
429 for(uInt32 i = 0; i < NUM_SIGS_0; ++i)
430 if(searchForBytes(image, size, signature_0[i], SIG_SIZE_0))
431 return true;
432 }
433 return false;
434}
435
436// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
437bool ControllerDetector::usesPaddle(const uInt8* image, size_t size,
438 Controller::Jack port, const Settings& settings)
439{
440 if(port == Controller::Jack::Left)
441 {
442 // check for INPT0 access
443 const int NUM_SIGS_0 = 12;
444 const int SIG_SIZE_0 = 3;
445 uInt8 signature_0[NUM_SIGS_0][SIG_SIZE_0] = {
446 //{ 0x24, 0x08, 0x10 }, // bit INPT0; bpl (many joystick games too!)
447 //{ 0x24, 0x08, 0x30 }, // bit INPT0; bmi (joystick games: Spike's Peak, Sweat, Turbo!)
448 { 0xa5, 0x08, 0x10 }, // lda INPT0; bpl (no joystick games)
449 { 0xa5, 0x08, 0x30 }, // lda INPT0; bmi (no joystick games)
450 //{ 0xb5, 0x08, 0x10 }, // lda INPT0,x; bpl (Duck Attack (graphics)!, Toyshop Trouble (Easter Egg))
451 { 0xb5, 0x08, 0x30 }, // lda INPT0,x; bmi (no joystick games)
452 { 0x24, 0x38, 0x10 }, // bit INPT0|$30; bpl (no joystick games)
453 { 0x24, 0x38, 0x30 }, // bit INPT0|$30; bmi (no joystick games)
454 { 0xa5, 0x38, 0x10 }, // lda INPT0|$30; bpl (no joystick games)
455 { 0xa5, 0x38, 0x30 }, // lda INPT0|$30; bmi (no joystick games)
456 { 0xb5, 0x38, 0x10 }, // lda INPT0|$30,x; bpl (Circus Atari, old code!)
457 { 0xb5, 0x38, 0x30 }, // lda INPT0|$30,x; bmi (no joystick games)
458 { 0x68, 0x48, 0x10 }, // pla; pha; bpl (i.a. Bachelor Party)
459 { 0xa5, 0x08, 0x4c }, // lda INPT0; jmp (only Backgammon)
460 { 0xa4, 0x38, 0x30 }, // ldy INPT0; bmi (no joystick games)
461 };
462 const int NUM_SIGS_1 = 3;
463 const int SIG_SIZE_1 = 4;
464 uInt8 signature_1[NUM_SIGS_1][SIG_SIZE_1] = {
465 { 0xb9, 0x08, 0x00, 0x30 }, // lda INPT0,y; bmi (i.a. Encounter at L-5)
466 { 0xb9, 0x38, 0x00, 0x30 }, // lda INPT0|$30,y; bmi (i.a. SW-Jedi Arena, Video Olympics)
467 { 0x24, 0x08, 0x30, 0x02 }, // bit INPT0; bmi +2 (Picnic)
468 };
469 const int NUM_SIGS_2 = 4;
470 const int SIG_SIZE_2 = 5;
471 uInt8 signature_2[NUM_SIGS_2][SIG_SIZE_2] = {
472 { 0xb5, 0x38, 0x29, 0x80, 0xd0 }, // lda INPT0|$30,x; and #$80; bne (Basic Programming)
473 { 0x24, 0x38, 0x85, 0x08, 0x10 }, // bit INPT0|$30; sta COLUPF, bpl (Fireball)
474 { 0xb5, 0x38, 0x49, 0xff, 0x0a }, // lda INPT0|$30,x; eor #$ff; asl (Blackjack)
475 { 0xb1, 0xf2, 0x30, 0x02, 0xe6 } // lda ($f2),y; bmi...; inc (Warplock)
476 };
477
478 for(uInt32 i = 0; i < NUM_SIGS_0; ++i)
479 if(searchForBytes(image, size, signature_0[i], SIG_SIZE_0))
480 return true;
481
482 for(uInt32 i = 0; i < NUM_SIGS_1; ++i)
483 if(searchForBytes(image, size, signature_1[i], SIG_SIZE_1))
484 return true;
485
486 for(uInt32 i = 0; i < NUM_SIGS_2; ++i)
487 if(searchForBytes(image, size, signature_2[i], SIG_SIZE_2))
488 return true;
489 }
490 else if(port == Controller::Jack::Right)
491 {
492 // check for INPT2 and indexed INPT0 access
493 const int NUM_SIGS_0 = 18;
494 const int SIG_SIZE_0 = 3;
495 uInt8 signature_0[NUM_SIGS_0][SIG_SIZE_0] = {
496 { 0x24, 0x0a, 0x10 }, // bit INPT2; bpl (no joystick games)
497 { 0x24, 0x0a, 0x30 }, // bit INPT2; bmi (no joystick games)
498 { 0xa5, 0x0a, 0x10 }, // lda INPT2; bpl (no joystick games)
499 { 0xa5, 0x0a, 0x30 }, // lda INPT2; bmi
500 { 0xb5, 0x0a, 0x10 }, // lda INPT2,x; bpl
501 { 0xb5, 0x0a, 0x30 }, // lda INPT2,x; bmi
502 { 0xb5, 0x08, 0x10 }, // lda INPT0,x; bpl (no joystick games)
503 { 0xb5, 0x08, 0x30 }, // lda INPT0,x; bmi (no joystick games)
504 { 0x24, 0x3a, 0x10 }, // bit INPT2|$30; bpl
505 { 0x24, 0x3a, 0x30 }, // bit INPT2|$30; bmi
506 { 0xa5, 0x3a, 0x10 }, // lda INPT2|$30; bpl
507 { 0xa5, 0x3a, 0x30 }, // lda INPT2|$30; bmi
508 { 0xb5, 0x3a, 0x10 }, // lda INPT2|$30,x; bpl
509 { 0xb5, 0x3a, 0x30 }, // lda INPT2|$30,x; bmi
510 { 0xb5, 0x38, 0x10 }, // lda INPT0|$30,x; bpl (Circus Atari, old code!)
511 { 0xb5, 0x38, 0x30 }, // lda INPT0|$30,x; bmi (no joystick games)
512 { 0xa4, 0x3a, 0x30 }, // ldy INPT2|$30; bmi (no joystick games)
513 { 0xa5, 0x3b, 0x30 }, // lda INPT3|$30; bmi (only Tac Scan, ports and paddles swapped)
514 };
515 const int NUM_SIGS_1 = 1;
516 const int SIG_SIZE_1 = 4;
517 uInt8 signature_1[NUM_SIGS_1][SIG_SIZE_1] = {
518 { 0xb9, 0x38, 0x00, 0x30 }, // lda INPT0|$30,y; bmi (Video Olympics)
519 };
520 const int NUM_SIGS_2 = 3;
521 const int SIG_SIZE_2 = 5;
522 uInt8 signature_2[NUM_SIGS_2][SIG_SIZE_2] = {
523 { 0xb5, 0x38, 0x29, 0x80, 0xd0 }, // lda INPT0|$30,x; and #$80; bne (Basic Programming)
524 { 0x24, 0x38, 0x85, 0x08, 0x10 }, // bit INPT2|$30; sta COLUPF, bpl (Fireball, patched at runtime!)
525 { 0xb5, 0x38, 0x49, 0xff, 0x0a } // lda INPT0|$30,x; eor #$ff; asl (Blackjack)
526 };
527
528 for(uInt32 i = 0; i < NUM_SIGS_0; ++i)
529 if(searchForBytes(image, size, signature_0[i], SIG_SIZE_0))
530 return true;
531
532 for(uInt32 i = 0; i < NUM_SIGS_1; ++i)
533 if(searchForBytes(image, size, signature_1[i], SIG_SIZE_1))
534 return true;
535
536 for(uInt32 i = 0; i < NUM_SIGS_2; ++i)
537 if(searchForBytes(image, size, signature_2[i], SIG_SIZE_2))
538 return true;
539 }
540
541 return false;
542}
543
544// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
545bool ControllerDetector::isProbablyTrakBall(const uInt8* image, size_t size)
546{
547 // check for TrakBall tables
548 const int NUM_SIGS = 3;
549 const int SIG_SIZE = 6;
550 uInt8 signature[NUM_SIGS][SIG_SIZE] = {
551 { 0b1010, 0b1000, 0b1000, 0b1010, 0b0010, 0b0000/*, 0b0000, 0b0010*/ }, // NextTrackTbl (T. Jentzsch)
552 { 0x00, 0x07, 0x87, 0x07, 0x88, 0x01/*, 0xff, 0x01*/ }, // .MovementTab_1 (Omegamatrix, SMX7)
553 { 0x00, 0x01, 0x81, 0x01, 0x82, 0x03 }, // .MovementTab_1 (Omegamatrix)
554 }; // all pattern checked, only TrakBall matches
555
556 for(uInt32 i = 0; i < NUM_SIGS; ++i)
557 if(searchForBytes(image, size, signature[i], SIG_SIZE))
558 return true;
559
560 return false;
561}
562
563// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
564bool ControllerDetector::isProbablyAtariMouse(const uInt8* image, size_t size)
565{
566 // check for Atari Mouse tables
567 const int NUM_SIGS = 3;
568 const int SIG_SIZE = 6;
569 uInt8 signature[NUM_SIGS][SIG_SIZE] = {
570 { 0b0101, 0b0111, 0b0100, 0b0110, 0b1101, 0b1111/*, 0b1100, 0b1110*/ }, // NextTrackTbl (T. Jentzsch)
571 { 0x00, 0x87, 0x07, 0x00, 0x08, 0x81/*, 0x7f, 0x08*/ }, // .MovementTab_1 (Omegamatrix, SMX7)
572 { 0x00, 0x81, 0x01, 0x00, 0x02, 0x83 }, // .MovementTab_1 (Omegamatrix)
573 }; // all pattern checked, only Atari Mouse matches
574
575 for(uInt32 i = 0; i < NUM_SIGS; ++i)
576 if(searchForBytes(image, size, signature[i], SIG_SIZE))
577 return true;
578
579 return false;
580}
581
582// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
583bool ControllerDetector::isProbablyAmigaMouse(const uInt8* image, size_t size)
584{
585 // check for Amiga Mouse tables
586 const int NUM_SIGS = 4;
587 const int SIG_SIZE = 6;
588 uInt8 signature[NUM_SIGS][SIG_SIZE] = {
589 { 0b1100, 0b1000, 0b0100, 0b0000, 0b1101, 0b1001/*, 0b0101, 0b0001*/ }, // NextTrackTbl (T. Jentzsch)
590 { 0x00, 0x88, 0x07, 0x01, 0x08, 0x00/*, 0x7f, 0x07*/ }, // .MovementTab_1 (Omegamatrix, SMX7)
591 { 0x00, 0x82, 0x01, 0x03, 0x02, 0x00 }, // .MovementTab_1 (Omegamatrix)
592 { 0b100, 0b000, 0b000, 0b000, 0b101, 0b001} // NextTrackTbl (T. Jentzsch, MCTB)
593 }; // all pattern checked, only Amiga Mouse matches
594
595 for(uInt32 i = 0; i < NUM_SIGS; ++i)
596 if(searchForBytes(image, size, signature[i], SIG_SIZE))
597 return true;
598
599 return false;
600}
601
602// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
603bool ControllerDetector::isProbablySaveKey(const uInt8* image, size_t size,
604 Controller::Jack port)
605{
606 // check for known SaveKey code, only supports right port
607 if(port == Controller::Jack::Right)
608 {
609 const int NUM_SIGS = 4;
610 const int SIG_SIZE = 9;
611 uInt8 signature[NUM_SIGS][SIG_SIZE] = {
612 { // from I2C_START (i2c.inc)
613 0xa9, 0x08, // lda #I2C_SCL_MASK
614 0x8d, 0x80, 0x02, // sta SWCHA
615 0xa9, 0x0c, // lda #I2C_SCL_MASK|I2C_SDA_MASK
616 0x8d, 0x81 // sta SWACNT
617 },
618 { // from I2C_START (i2c_v2.1..3.inc)
619 0xa9, 0x18, // #(I2C_SCL_MASK|I2C_SDA_MASK)*2
620 0x8d, 0x80, 0x02, // sta SWCHA
621 0x4a, // lsr
622 0x8d, 0x81, 0x02 // sta SWACNT
623 },
624 { // from I2C_START (Strat-O-Gems)
625 0xa2, 0x08, // ldx #I2C_SCL_MASK
626 0x8e, 0x80, 0x02, // stx SWCHA
627 0xa2, 0x0c, // ldx #I2C_SCL_MASK|I2C_SDA_MASK
628 0x8e, 0x81 // stx SWACNT
629 },
630 { // from I2C_START (AStar, Fall Down, Go Fish!)
631 0xa9, 0x08, // lda #I2C_SCL_MASK
632 0x8d, 0x80, 0x02, // sta SWCHA
633 0xea, // nop
634 0xa9, 0x0c, // lda #I2C_SCL_MASK|I2C_SDA_MASK
635 0x8d // sta SWACNT
636 }
637 };
638
639 for(uInt32 i = 0; i < NUM_SIGS; ++i)
640 if(searchForBytes(image, size, signature[i], SIG_SIZE))
641 return true;
642 }
643
644 return false;
645}
646