1 |
|
/* |
2 |
|
* adb.cpp - ADB emulation (mouse/keyboard) |
3 |
|
* |
4 |
< |
* Basilisk II (C) 1997-1999 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 |
|
|
58 |
|
static uint8 key_reg_2[2] = {0xff, 0xff}; // Keyboard ADB register 2 |
59 |
|
static uint8 key_reg_3[2] = {0x62, 0x05}; // Keyboard ADB register 3 |
60 |
|
|
61 |
+ |
static uint8 m_keyboard_type = 0x05; |
62 |
+ |
|
63 |
+ |
// ADB mouse motion lock (for platforms that use separate input thread) |
64 |
+ |
static B2_mutex *mouse_lock; |
65 |
+ |
|
66 |
+ |
|
67 |
+ |
/* |
68 |
+ |
* Initialize ADB emulation |
69 |
+ |
*/ |
70 |
+ |
|
71 |
+ |
void ADBInit(void) |
72 |
+ |
{ |
73 |
+ |
mouse_lock = B2_create_mutex(); |
74 |
+ |
m_keyboard_type = (uint8)PrefsFindInt32("keyboardtype"); |
75 |
+ |
key_reg_3[1] = m_keyboard_type; |
76 |
+ |
} |
77 |
+ |
|
78 |
+ |
|
79 |
+ |
/* |
80 |
+ |
* Exit ADB emulation |
81 |
+ |
*/ |
82 |
+ |
|
83 |
+ |
void ADBExit(void) |
84 |
+ |
{ |
85 |
+ |
if (mouse_lock) { |
86 |
+ |
B2_delete_mutex(mouse_lock); |
87 |
+ |
mouse_lock = NULL; |
88 |
+ |
} |
89 |
+ |
} |
90 |
+ |
|
91 |
|
|
92 |
|
/* |
93 |
|
* ADBOp() replacement |
104 |
|
key_reg_2[0] = 0xff; |
105 |
|
key_reg_2[1] = 0xff; |
106 |
|
key_reg_3[0] = 0x62; |
107 |
< |
key_reg_3[1] = 0x05; |
107 |
> |
key_reg_3[1] = m_keyboard_type; |
108 |
|
return; |
109 |
|
} |
110 |
|
|
224 |
|
|
225 |
|
|
226 |
|
/* |
227 |
< |
* Mouse was moved (x/y are absolute or relative, depending on ADBSetMouseMode()) |
227 |
> |
* Mouse was moved (x/y are absolute or relative, depending on ADBSetRelMouseMode()) |
228 |
|
*/ |
229 |
|
|
230 |
|
void ADBMouseMoved(int x, int y) |
231 |
|
{ |
232 |
+ |
B2_lock_mutex(mouse_lock); |
233 |
|
if (relative_mouse) { |
234 |
|
mouse_x += x; mouse_y += y; |
235 |
|
} else { |
236 |
|
mouse_x = x; mouse_y = y; |
237 |
|
} |
238 |
+ |
B2_unlock_mutex(mouse_lock); |
239 |
+ |
SetInterruptFlag(INTFLAG_ADB); |
240 |
+ |
TriggerInterrupt(); |
241 |
|
} |
242 |
|
|
243 |
|
|
248 |
|
void ADBMouseDown(int button) |
249 |
|
{ |
250 |
|
mouse_button[button] = true; |
251 |
+ |
SetInterruptFlag(INTFLAG_ADB); |
252 |
+ |
TriggerInterrupt(); |
253 |
|
} |
254 |
|
|
255 |
|
|
256 |
|
/* |
257 |
< |
* First mouse button released |
257 |
> |
* Mouse button released |
258 |
|
*/ |
259 |
|
|
260 |
|
void ADBMouseUp(int button) |
261 |
|
{ |
262 |
|
mouse_button[button] = false; |
263 |
+ |
SetInterruptFlag(INTFLAG_ADB); |
264 |
+ |
TriggerInterrupt(); |
265 |
|
} |
266 |
|
|
267 |
|
|
271 |
|
|
272 |
|
void ADBSetRelMouseMode(bool relative) |
273 |
|
{ |
274 |
< |
relative_mouse = relative; |
274 |
> |
if (relative_mouse != relative) { |
275 |
> |
relative_mouse = relative; |
276 |
> |
mouse_x = mouse_y = 0; |
277 |
> |
} |
278 |
|
} |
279 |
|
|
280 |
|
|
290 |
|
|
291 |
|
// Set key in matrix |
292 |
|
key_states[code >> 3] |= (1 << (~code & 7)); |
293 |
+ |
|
294 |
+ |
// Trigger interrupt |
295 |
+ |
SetInterruptFlag(INTFLAG_ADB); |
296 |
+ |
TriggerInterrupt(); |
297 |
|
} |
298 |
|
|
299 |
|
|
309 |
|
|
310 |
|
// Clear key in matrix |
311 |
|
key_states[code >> 3] &= ~(1 << (~code & 7)); |
312 |
+ |
|
313 |
+ |
// Trigger interrupt |
314 |
+ |
SetInterruptFlag(INTFLAG_ADB); |
315 |
+ |
TriggerInterrupt(); |
316 |
|
} |
317 |
|
|
318 |
|
|
330 |
|
return; |
331 |
|
uint32 tmp_data = adb_base + 0x163; // Temporary storage for faked ADB data |
332 |
|
|
333 |
< |
// Get position so that it won't change during processing |
333 |
> |
// Get mouse state |
334 |
> |
B2_lock_mutex(mouse_lock); |
335 |
|
int mx = mouse_x; |
336 |
|
int my = mouse_y; |
337 |
+ |
if (relative_mouse) |
338 |
+ |
mouse_x = mouse_y = 0; |
339 |
+ |
int mb[3] = {mouse_button[0], mouse_button[1], mouse_button[2]}; |
340 |
+ |
B2_unlock_mutex(mouse_lock); |
341 |
+ |
|
342 |
+ |
uint32 key_base = adb_base + 4; |
343 |
+ |
uint32 mouse_base = adb_base + 16; |
344 |
|
|
345 |
|
if (relative_mouse) { |
346 |
|
|
347 |
|
// Mouse movement (relative) and buttons |
348 |
< |
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; |
348 |
> |
if (mx != 0 || my != 0 || mb[0] != old_mouse_button[0] || mb[1] != old_mouse_button[1] || mb[2] != old_mouse_button[2]) { |
349 |
|
|
350 |
|
// Call mouse ADB handler |
351 |
|
if (mouse_reg_3[1] == 4) { |
352 |
|
// Extended mouse protocol |
353 |
|
WriteMacInt8(tmp_data, 3); |
354 |
< |
WriteMacInt8(tmp_data + 1, (my & 0x7f) | (mouse_button[0] ? 0 : 0x80)); |
355 |
< |
WriteMacInt8(tmp_data + 2, (mx & 0x7f) | (mouse_button[1] ? 0 : 0x80)); |
356 |
< |
WriteMacInt8(tmp_data + 3, ((my >> 3) & 0x70) | ((mx >> 7) & 0x07) | (mouse_button[2] ? 0x08 : 0x88)); |
354 |
> |
WriteMacInt8(tmp_data + 1, (my & 0x7f) | (mb[0] ? 0 : 0x80)); |
355 |
> |
WriteMacInt8(tmp_data + 2, (mx & 0x7f) | (mb[1] ? 0 : 0x80)); |
356 |
> |
WriteMacInt8(tmp_data + 3, ((my >> 3) & 0x70) | ((mx >> 7) & 0x07) | (mb[2] ? 0x08 : 0x88)); |
357 |
|
} else { |
358 |
|
// 100/200 dpi mode |
359 |
|
WriteMacInt8(tmp_data, 2); |
360 |
< |
WriteMacInt8(tmp_data + 1, (my & 0x7f) | (mouse_button[0] ? 0 : 0x80)); |
361 |
< |
WriteMacInt8(tmp_data + 2, (mx & 0x7f) | (mouse_button[1] ? 0 : 0x80)); |
360 |
> |
WriteMacInt8(tmp_data + 1, (my & 0x7f) | (mb[0] ? 0 : 0x80)); |
361 |
> |
WriteMacInt8(tmp_data + 2, (mx & 0x7f) | (mb[1] ? 0 : 0x80)); |
362 |
|
} |
363 |
|
r.a[0] = tmp_data; |
364 |
|
r.a[1] = ReadMacInt32(mouse_base); |
367 |
|
r.d[0] = (mouse_reg_3[0] << 4) | 0x0c; // Talk 0 |
368 |
|
Execute68k(r.a[1], &r); |
369 |
|
|
370 |
< |
mouse_x = mouse_y = 0; |
371 |
< |
old_mouse_button[0] = mouse_button[0]; |
372 |
< |
old_mouse_button[1] = mouse_button[1]; |
315 |
< |
old_mouse_button[2] = mouse_button[2]; |
370 |
> |
old_mouse_button[0] = mb[0]; |
371 |
> |
old_mouse_button[1] = mb[1]; |
372 |
> |
old_mouse_button[2] = mb[2]; |
373 |
|
} |
374 |
|
|
375 |
|
} else { |
376 |
|
|
377 |
|
// Update mouse position (absolute) |
378 |
|
if (mx != old_mouse_x || my != old_mouse_y) { |
379 |
+ |
#ifdef POWERPC_ROM |
380 |
+ |
static const uint16 proc[] = { |
381 |
+ |
PW(0x2f08), // move.l a0,-(sp) |
382 |
+ |
PW(0x2f00), // move.l d0,-(sp) |
383 |
+ |
PW(0x2f01), // move.l d1,-(sp) |
384 |
+ |
PW(0x7001), // moveq #1,d0 (MoveTo) |
385 |
+ |
PW(0xaadb), // CursorDeviceDispatch |
386 |
+ |
PW(M68K_RTS) |
387 |
+ |
}; |
388 |
+ |
r.a[0] = ReadMacInt32(mouse_base + 4); |
389 |
+ |
r.d[0] = mx; |
390 |
+ |
r.d[1] = my; |
391 |
+ |
Execute68k((uint32)proc, &r); |
392 |
+ |
#else |
393 |
|
WriteMacInt16(0x82a, mx); |
394 |
|
WriteMacInt16(0x828, my); |
395 |
|
WriteMacInt16(0x82e, mx); |
396 |
|
WriteMacInt16(0x82c, my); |
397 |
|
WriteMacInt8(0x8ce, ReadMacInt8(0x8cf)); // CrsrCouple -> CrsrNew |
398 |
+ |
#endif |
399 |
|
old_mouse_x = mx; |
400 |
|
old_mouse_y = my; |
401 |
|
} |
402 |
|
|
403 |
|
// Send mouse button events |
404 |
< |
if (mouse_button[0] != old_mouse_button[0]) { |
404 |
> |
if (mb[0] != old_mouse_button[0] || mb[1] != old_mouse_button[1] || mb[2] != old_mouse_button[2]) { |
405 |
|
uint32 mouse_base = adb_base + 16; |
406 |
|
|
407 |
|
// Call mouse ADB handler |
408 |
|
if (mouse_reg_3[1] == 4) { |
409 |
|
// Extended mouse protocol |
410 |
|
WriteMacInt8(tmp_data, 3); |
411 |
< |
WriteMacInt8(tmp_data + 1, mouse_button[0] ? 0 : 0x80); |
412 |
< |
WriteMacInt8(tmp_data + 2, mouse_button[1] ? 0 : 0x80); |
413 |
< |
WriteMacInt8(tmp_data + 3, mouse_button[2] ? 0x08 : 0x88); |
411 |
> |
WriteMacInt8(tmp_data + 1, mb[0] ? 0 : 0x80); |
412 |
> |
WriteMacInt8(tmp_data + 2, mb[1] ? 0 : 0x80); |
413 |
> |
WriteMacInt8(tmp_data + 3, mb[2] ? 0x08 : 0x88); |
414 |
|
} else { |
415 |
|
// 100/200 dpi mode |
416 |
|
WriteMacInt8(tmp_data, 2); |
417 |
< |
WriteMacInt8(tmp_data + 1, mouse_button[0] ? 0 : 0x80); |
418 |
< |
WriteMacInt8(tmp_data + 2, mouse_button[1] ? 0 : 0x80); |
417 |
> |
WriteMacInt8(tmp_data + 1, mb[0] ? 0 : 0x80); |
418 |
> |
WriteMacInt8(tmp_data + 2, mb[1] ? 0 : 0x80); |
419 |
|
} |
420 |
|
r.a[0] = tmp_data; |
421 |
|
r.a[1] = ReadMacInt32(mouse_base); |
424 |
|
r.d[0] = (mouse_reg_3[0] << 4) | 0x0c; // Talk 0 |
425 |
|
Execute68k(r.a[1], &r); |
426 |
|
|
427 |
< |
old_mouse_button[0] = mouse_button[0]; |
428 |
< |
old_mouse_button[1] = mouse_button[1]; |
429 |
< |
old_mouse_button[2] = mouse_button[2]; |
427 |
> |
old_mouse_button[0] = mb[0]; |
428 |
> |
old_mouse_button[1] = mb[1]; |
429 |
> |
old_mouse_button[2] = mb[2]; |
430 |
|
} |
431 |
|
} |
432 |
|
|
433 |
|
// Process accumulated keyboard events |
362 |
– |
uint32 key_base = adb_base + 4; |
434 |
|
while (key_read_ptr != key_write_ptr) { |
435 |
|
|
436 |
|
// Read keyboard event |