1 |
|
/* |
2 |
|
* adb.cpp - ADB emulation (mouse/keyboard) |
3 |
|
* |
4 |
< |
* Basilisk II (C) 1997-2000 Christian Bauer |
4 |
> |
* Basilisk II (C) 1997-2004 Christian Bauer |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
* it under the terms of the GNU General Public License as published by |
28 |
|
|
29 |
|
#include "sysdeps.h" |
30 |
|
#include "cpu_emulation.h" |
31 |
+ |
#include "emul_op.h" |
32 |
|
#include "main.h" |
33 |
+ |
#include "prefs.h" |
34 |
|
#include "video.h" |
35 |
|
#include "adb.h" |
36 |
|
|
37 |
+ |
#ifdef POWERPC_ROM |
38 |
+ |
#include "thunks.h" |
39 |
+ |
#endif |
40 |
+ |
|
41 |
|
#define DEBUG 0 |
42 |
|
#include "debug.h" |
43 |
|
|
62 |
|
static uint8 key_reg_2[2] = {0xff, 0xff}; // Keyboard ADB register 2 |
63 |
|
static uint8 key_reg_3[2] = {0x62, 0x05}; // Keyboard ADB register 3 |
64 |
|
|
65 |
+ |
static uint8 m_keyboard_type = 0x05; |
66 |
+ |
|
67 |
+ |
// ADB mouse motion lock (for platforms that use separate input thread) |
68 |
+ |
static B2_mutex *mouse_lock; |
69 |
+ |
|
70 |
+ |
|
71 |
+ |
/* |
72 |
+ |
* Initialize ADB emulation |
73 |
+ |
*/ |
74 |
+ |
|
75 |
+ |
void ADBInit(void) |
76 |
+ |
{ |
77 |
+ |
mouse_lock = B2_create_mutex(); |
78 |
+ |
m_keyboard_type = (uint8)PrefsFindInt32("keyboardtype"); |
79 |
+ |
key_reg_3[1] = m_keyboard_type; |
80 |
+ |
} |
81 |
+ |
|
82 |
+ |
|
83 |
+ |
/* |
84 |
+ |
* Exit ADB emulation |
85 |
+ |
*/ |
86 |
+ |
|
87 |
+ |
void ADBExit(void) |
88 |
+ |
{ |
89 |
+ |
if (mouse_lock) { |
90 |
+ |
B2_delete_mutex(mouse_lock); |
91 |
+ |
mouse_lock = NULL; |
92 |
+ |
} |
93 |
+ |
} |
94 |
+ |
|
95 |
|
|
96 |
|
/* |
97 |
|
* ADBOp() replacement |
108 |
|
key_reg_2[0] = 0xff; |
109 |
|
key_reg_2[1] = 0xff; |
110 |
|
key_reg_3[0] = 0x62; |
111 |
< |
key_reg_3[1] = 0x05; |
111 |
> |
key_reg_3[1] = m_keyboard_type; |
112 |
|
return; |
113 |
|
} |
114 |
|
|
228 |
|
|
229 |
|
|
230 |
|
/* |
231 |
< |
* Mouse was moved (x/y are absolute or relative, depending on ADBSetMouseMode()) |
231 |
> |
* Mouse was moved (x/y are absolute or relative, depending on ADBSetRelMouseMode()) |
232 |
|
*/ |
233 |
|
|
234 |
|
void ADBMouseMoved(int x, int y) |
235 |
|
{ |
236 |
+ |
B2_lock_mutex(mouse_lock); |
237 |
|
if (relative_mouse) { |
238 |
|
mouse_x += x; mouse_y += y; |
239 |
|
} else { |
240 |
|
mouse_x = x; mouse_y = y; |
241 |
|
} |
242 |
+ |
B2_unlock_mutex(mouse_lock); |
243 |
+ |
SetInterruptFlag(INTFLAG_ADB); |
244 |
+ |
TriggerInterrupt(); |
245 |
|
} |
246 |
|
|
247 |
|
|
252 |
|
void ADBMouseDown(int button) |
253 |
|
{ |
254 |
|
mouse_button[button] = true; |
255 |
+ |
SetInterruptFlag(INTFLAG_ADB); |
256 |
+ |
TriggerInterrupt(); |
257 |
|
} |
258 |
|
|
259 |
|
|
260 |
|
/* |
261 |
< |
* First mouse button released |
261 |
> |
* Mouse button released |
262 |
|
*/ |
263 |
|
|
264 |
|
void ADBMouseUp(int button) |
265 |
|
{ |
266 |
|
mouse_button[button] = false; |
267 |
+ |
SetInterruptFlag(INTFLAG_ADB); |
268 |
+ |
TriggerInterrupt(); |
269 |
|
} |
270 |
|
|
271 |
|
|
275 |
|
|
276 |
|
void ADBSetRelMouseMode(bool relative) |
277 |
|
{ |
278 |
< |
relative_mouse = relative; |
278 |
> |
if (relative_mouse != relative) { |
279 |
> |
relative_mouse = relative; |
280 |
> |
mouse_x = mouse_y = 0; |
281 |
> |
} |
282 |
|
} |
283 |
|
|
284 |
|
|
294 |
|
|
295 |
|
// Set key in matrix |
296 |
|
key_states[code >> 3] |= (1 << (~code & 7)); |
297 |
+ |
|
298 |
+ |
// Trigger interrupt |
299 |
+ |
SetInterruptFlag(INTFLAG_ADB); |
300 |
+ |
TriggerInterrupt(); |
301 |
|
} |
302 |
|
|
303 |
|
|
313 |
|
|
314 |
|
// Clear key in matrix |
315 |
|
key_states[code >> 3] &= ~(1 << (~code & 7)); |
316 |
+ |
|
317 |
+ |
// Trigger interrupt |
318 |
+ |
SetInterruptFlag(INTFLAG_ADB); |
319 |
+ |
TriggerInterrupt(); |
320 |
|
} |
321 |
|
|
322 |
|
|
334 |
|
return; |
335 |
|
uint32 tmp_data = adb_base + 0x163; // Temporary storage for faked ADB data |
336 |
|
|
337 |
< |
// Get position so that it won't change during processing |
337 |
> |
// Get mouse state |
338 |
> |
B2_lock_mutex(mouse_lock); |
339 |
|
int mx = mouse_x; |
340 |
|
int my = mouse_y; |
341 |
+ |
if (relative_mouse) |
342 |
+ |
mouse_x = mouse_y = 0; |
343 |
+ |
int mb[3] = {mouse_button[0], mouse_button[1], mouse_button[2]}; |
344 |
+ |
B2_unlock_mutex(mouse_lock); |
345 |
+ |
|
346 |
+ |
uint32 key_base = adb_base + 4; |
347 |
+ |
uint32 mouse_base = adb_base + 16; |
348 |
|
|
349 |
|
if (relative_mouse) { |
350 |
|
|
351 |
|
// Mouse movement (relative) and buttons |
352 |
< |
if (mx != 0 || my != 0 || mouse_button[0] != old_mouse_button[0] || mouse_button[1] != old_mouse_button[1] || mouse_button[2] != old_mouse_button[2]) { |
290 |
< |
uint32 mouse_base = adb_base + 16; |
352 |
> |
if (mx != 0 || my != 0 || mb[0] != old_mouse_button[0] || mb[1] != old_mouse_button[1] || mb[2] != old_mouse_button[2]) { |
353 |
|
|
354 |
|
// Call mouse ADB handler |
355 |
|
if (mouse_reg_3[1] == 4) { |
356 |
|
// Extended mouse protocol |
357 |
|
WriteMacInt8(tmp_data, 3); |
358 |
< |
WriteMacInt8(tmp_data + 1, (my & 0x7f) | (mouse_button[0] ? 0 : 0x80)); |
359 |
< |
WriteMacInt8(tmp_data + 2, (mx & 0x7f) | (mouse_button[1] ? 0 : 0x80)); |
360 |
< |
WriteMacInt8(tmp_data + 3, ((my >> 3) & 0x70) | ((mx >> 7) & 0x07) | (mouse_button[2] ? 0x08 : 0x88)); |
358 |
> |
WriteMacInt8(tmp_data + 1, (my & 0x7f) | (mb[0] ? 0 : 0x80)); |
359 |
> |
WriteMacInt8(tmp_data + 2, (mx & 0x7f) | (mb[1] ? 0 : 0x80)); |
360 |
> |
WriteMacInt8(tmp_data + 3, ((my >> 3) & 0x70) | ((mx >> 7) & 0x07) | (mb[2] ? 0x08 : 0x88)); |
361 |
|
} else { |
362 |
|
// 100/200 dpi mode |
363 |
|
WriteMacInt8(tmp_data, 2); |
364 |
< |
WriteMacInt8(tmp_data + 1, (my & 0x7f) | (mouse_button[0] ? 0 : 0x80)); |
365 |
< |
WriteMacInt8(tmp_data + 2, (mx & 0x7f) | (mouse_button[1] ? 0 : 0x80)); |
364 |
> |
WriteMacInt8(tmp_data + 1, (my & 0x7f) | (mb[0] ? 0 : 0x80)); |
365 |
> |
WriteMacInt8(tmp_data + 2, (mx & 0x7f) | (mb[1] ? 0 : 0x80)); |
366 |
|
} |
367 |
|
r.a[0] = tmp_data; |
368 |
|
r.a[1] = ReadMacInt32(mouse_base); |
371 |
|
r.d[0] = (mouse_reg_3[0] << 4) | 0x0c; // Talk 0 |
372 |
|
Execute68k(r.a[1], &r); |
373 |
|
|
374 |
< |
mouse_x = mouse_y = 0; |
375 |
< |
old_mouse_button[0] = mouse_button[0]; |
376 |
< |
old_mouse_button[1] = mouse_button[1]; |
315 |
< |
old_mouse_button[2] = mouse_button[2]; |
374 |
> |
old_mouse_button[0] = mb[0]; |
375 |
> |
old_mouse_button[1] = mb[1]; |
376 |
> |
old_mouse_button[2] = mb[2]; |
377 |
|
} |
378 |
|
|
379 |
|
} else { |
380 |
|
|
381 |
|
// Update mouse position (absolute) |
382 |
|
if (mx != old_mouse_x || my != old_mouse_y) { |
383 |
+ |
#ifdef POWERPC_ROM |
384 |
+ |
static const uint8 proc_template[] = { |
385 |
+ |
0x2f, 0x08, // move.l a0,-(sp) |
386 |
+ |
0x2f, 0x00, // move.l d0,-(sp) |
387 |
+ |
0x2f, 0x01, // move.l d1,-(sp) |
388 |
+ |
0x70, 0x01, // moveq #1,d0 (MoveTo) |
389 |
+ |
0xaa, 0xdb, // CursorDeviceDispatch |
390 |
+ |
M68K_RTS >> 8, M68K_RTS |
391 |
+ |
}; |
392 |
+ |
BUILD_SHEEPSHAVER_PROCEDURE(proc); |
393 |
+ |
r.a[0] = ReadMacInt32(mouse_base + 4); |
394 |
+ |
r.d[0] = mx; |
395 |
+ |
r.d[1] = my; |
396 |
+ |
Execute68k(proc, &r); |
397 |
+ |
#else |
398 |
|
WriteMacInt16(0x82a, mx); |
399 |
|
WriteMacInt16(0x828, my); |
400 |
|
WriteMacInt16(0x82e, mx); |
401 |
|
WriteMacInt16(0x82c, my); |
402 |
|
WriteMacInt8(0x8ce, ReadMacInt8(0x8cf)); // CrsrCouple -> CrsrNew |
403 |
+ |
#endif |
404 |
|
old_mouse_x = mx; |
405 |
|
old_mouse_y = my; |
406 |
|
} |
407 |
|
|
408 |
|
// Send mouse button events |
409 |
< |
if (mouse_button[0] != old_mouse_button[0]) { |
409 |
> |
if (mb[0] != old_mouse_button[0] || mb[1] != old_mouse_button[1] || mb[2] != old_mouse_button[2]) { |
410 |
|
uint32 mouse_base = adb_base + 16; |
411 |
|
|
412 |
|
// Call mouse ADB handler |
413 |
|
if (mouse_reg_3[1] == 4) { |
414 |
|
// Extended mouse protocol |
415 |
|
WriteMacInt8(tmp_data, 3); |
416 |
< |
WriteMacInt8(tmp_data + 1, mouse_button[0] ? 0 : 0x80); |
417 |
< |
WriteMacInt8(tmp_data + 2, mouse_button[1] ? 0 : 0x80); |
418 |
< |
WriteMacInt8(tmp_data + 3, mouse_button[2] ? 0x08 : 0x88); |
416 |
> |
WriteMacInt8(tmp_data + 1, mb[0] ? 0 : 0x80); |
417 |
> |
WriteMacInt8(tmp_data + 2, mb[1] ? 0 : 0x80); |
418 |
> |
WriteMacInt8(tmp_data + 3, mb[2] ? 0x08 : 0x88); |
419 |
|
} else { |
420 |
|
// 100/200 dpi mode |
421 |
|
WriteMacInt8(tmp_data, 2); |
422 |
< |
WriteMacInt8(tmp_data + 1, mouse_button[0] ? 0 : 0x80); |
423 |
< |
WriteMacInt8(tmp_data + 2, mouse_button[1] ? 0 : 0x80); |
422 |
> |
WriteMacInt8(tmp_data + 1, mb[0] ? 0 : 0x80); |
423 |
> |
WriteMacInt8(tmp_data + 2, mb[1] ? 0 : 0x80); |
424 |
|
} |
425 |
|
r.a[0] = tmp_data; |
426 |
|
r.a[1] = ReadMacInt32(mouse_base); |
429 |
|
r.d[0] = (mouse_reg_3[0] << 4) | 0x0c; // Talk 0 |
430 |
|
Execute68k(r.a[1], &r); |
431 |
|
|
432 |
< |
old_mouse_button[0] = mouse_button[0]; |
433 |
< |
old_mouse_button[1] = mouse_button[1]; |
434 |
< |
old_mouse_button[2] = mouse_button[2]; |
432 |
> |
old_mouse_button[0] = mb[0]; |
433 |
> |
old_mouse_button[1] = mb[1]; |
434 |
> |
old_mouse_button[2] = mb[2]; |
435 |
|
} |
436 |
|
} |
437 |
|
|
438 |
|
// Process accumulated keyboard events |
362 |
– |
uint32 key_base = adb_base + 4; |
439 |
|
while (key_read_ptr != key_write_ptr) { |
440 |
|
|
441 |
|
// Read keyboard event |