--- BasiliskII/src/adb.cpp 1999/10/03 14:16:25 1.1.1.1 +++ BasiliskII/src/adb.cpp 2010/02/21 09:55:36 1.17 @@ -1,7 +1,7 @@ /* * adb.cpp - ADB emulation (mouse/keyboard) * - * Basilisk II (C) 1997-1999 Christian Bauer + * Basilisk II (C) Christian Bauer * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -28,10 +28,16 @@ #include "sysdeps.h" #include "cpu_emulation.h" +#include "emul_op.h" #include "main.h" +#include "prefs.h" #include "video.h" #include "adb.h" +#ifdef POWERPC_ROM +#include "thunks.h" +#endif + #define DEBUG 0 #include "debug.h" @@ -56,6 +62,36 @@ static uint8 mouse_reg_3[2] = {0x63, 0x0 static uint8 key_reg_2[2] = {0xff, 0xff}; // Keyboard ADB register 2 static uint8 key_reg_3[2] = {0x62, 0x05}; // Keyboard ADB register 3 +static uint8 m_keyboard_type = 0x05; + +// ADB mouse motion lock (for platforms that use separate input thread) +static B2_mutex *mouse_lock; + + +/* + * Initialize ADB emulation + */ + +void ADBInit(void) +{ + mouse_lock = B2_create_mutex(); + m_keyboard_type = (uint8)PrefsFindInt32("keyboardtype"); + key_reg_3[1] = m_keyboard_type; +} + + +/* + * Exit ADB emulation + */ + +void ADBExit(void) +{ + if (mouse_lock) { + B2_delete_mutex(mouse_lock); + mouse_lock = NULL; + } +} + /* * ADBOp() replacement @@ -72,7 +108,7 @@ void ADBOp(uint8 op, uint8 *data) key_reg_2[0] = 0xff; key_reg_2[1] = 0xff; key_reg_3[0] = 0x62; - key_reg_3[1] = 0x05; + key_reg_3[1] = m_keyboard_type; return; } @@ -192,16 +228,20 @@ void ADBOp(uint8 op, uint8 *data) /* - * Mouse was moved (x/y are absolute or relative, depending on ADBSetMouseMode()) + * Mouse was moved (x/y are absolute or relative, depending on ADBSetRelMouseMode()) */ void ADBMouseMoved(int x, int y) { + B2_lock_mutex(mouse_lock); if (relative_mouse) { mouse_x += x; mouse_y += y; } else { mouse_x = x; mouse_y = y; } + B2_unlock_mutex(mouse_lock); + SetInterruptFlag(INTFLAG_ADB); + TriggerInterrupt(); } @@ -212,16 +252,20 @@ void ADBMouseMoved(int x, int y) void ADBMouseDown(int button) { mouse_button[button] = true; + SetInterruptFlag(INTFLAG_ADB); + TriggerInterrupt(); } /* - * First mouse button released + * Mouse button released */ void ADBMouseUp(int button) { mouse_button[button] = false; + SetInterruptFlag(INTFLAG_ADB); + TriggerInterrupt(); } @@ -231,7 +275,10 @@ void ADBMouseUp(int button) void ADBSetRelMouseMode(bool relative) { - relative_mouse = relative; + if (relative_mouse != relative) { + relative_mouse = relative; + mouse_x = mouse_y = 0; + } } @@ -247,6 +294,10 @@ void ADBKeyDown(int code) // Set key in matrix key_states[code >> 3] |= (1 << (~code & 7)); + + // Trigger interrupt + SetInterruptFlag(INTFLAG_ADB); + TriggerInterrupt(); } @@ -262,6 +313,10 @@ void ADBKeyUp(int code) // Clear key in matrix key_states[code >> 3] &= ~(1 << (~code & 7)); + + // Trigger interrupt + SetInterruptFlag(INTFLAG_ADB); + TriggerInterrupt(); } @@ -279,29 +334,35 @@ void ADBInterrupt(void) return; uint32 tmp_data = adb_base + 0x163; // Temporary storage for faked ADB data - // Get position so that it won't change during processing + // Get mouse state + B2_lock_mutex(mouse_lock); int mx = mouse_x; int my = mouse_y; + if (relative_mouse) + mouse_x = mouse_y = 0; + int mb[3] = {mouse_button[0], mouse_button[1], mouse_button[2]}; + B2_unlock_mutex(mouse_lock); + + uint32 key_base = adb_base + 4; + uint32 mouse_base = adb_base + 16; if (relative_mouse) { // Mouse movement (relative) and buttons - 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]) { - uint32 mouse_base = adb_base + 16; - uint8 *mouse_data = Mac2HostAddr(tmp_data); + if (mx != 0 || my != 0 || mb[0] != old_mouse_button[0] || mb[1] != old_mouse_button[1] || mb[2] != old_mouse_button[2]) { // Call mouse ADB handler if (mouse_reg_3[1] == 4) { // Extended mouse protocol - mouse_data[0] = 3; - mouse_data[1] = (my & 0x7f) | (mouse_button[0] ? 0 : 0x80); - mouse_data[2] = (mx & 0x7f) | (mouse_button[1] ? 0 : 0x80); - mouse_data[3] = ((my >> 3) & 0x70) | ((mx >> 7) & 0x07) | (mouse_button[2] ? 0x08 : 0x88); + WriteMacInt8(tmp_data, 3); + WriteMacInt8(tmp_data + 1, (my & 0x7f) | (mb[0] ? 0 : 0x80)); + WriteMacInt8(tmp_data + 2, (mx & 0x7f) | (mb[1] ? 0 : 0x80)); + WriteMacInt8(tmp_data + 3, ((my >> 3) & 0x70) | ((mx >> 7) & 0x07) | (mb[2] ? 0x08 : 0x88)); } else { // 100/200 dpi mode - mouse_data[0] = 2; - mouse_data[1] = (my & 0x7f) | (mouse_button[0] ? 0 : 0x80); - mouse_data[2] = (mx & 0x7f) | (mouse_button[1] ? 0 : 0x80); + WriteMacInt8(tmp_data, 2); + WriteMacInt8(tmp_data + 1, (my & 0x7f) | (mb[0] ? 0 : 0x80)); + WriteMacInt8(tmp_data + 2, (mx & 0x7f) | (mb[1] ? 0 : 0x80)); } r.a[0] = tmp_data; r.a[1] = ReadMacInt32(mouse_base); @@ -310,42 +371,56 @@ void ADBInterrupt(void) r.d[0] = (mouse_reg_3[0] << 4) | 0x0c; // Talk 0 Execute68k(r.a[1], &r); - mouse_x = mouse_y = 0; - old_mouse_button[0] = mouse_button[0]; - old_mouse_button[1] = mouse_button[1]; - old_mouse_button[2] = mouse_button[2]; + old_mouse_button[0] = mb[0]; + old_mouse_button[1] = mb[1]; + old_mouse_button[2] = mb[2]; } } else { // Update mouse position (absolute) if (mx != old_mouse_x || my != old_mouse_y) { +#ifdef POWERPC_ROM + static const uint8 proc_template[] = { + 0x2f, 0x08, // move.l a0,-(sp) + 0x2f, 0x00, // move.l d0,-(sp) + 0x2f, 0x01, // move.l d1,-(sp) + 0x70, 0x01, // moveq #1,d0 (MoveTo) + 0xaa, 0xdb, // CursorDeviceDispatch + M68K_RTS >> 8, M68K_RTS & 0xff + }; + BUILD_SHEEPSHAVER_PROCEDURE(proc); + r.a[0] = ReadMacInt32(mouse_base + 4); + r.d[0] = mx; + r.d[1] = my; + Execute68k(proc, &r); +#else WriteMacInt16(0x82a, mx); WriteMacInt16(0x828, my); WriteMacInt16(0x82e, mx); WriteMacInt16(0x82c, my); WriteMacInt8(0x8ce, ReadMacInt8(0x8cf)); // CrsrCouple -> CrsrNew +#endif old_mouse_x = mx; old_mouse_y = my; } // Send mouse button events - if (mouse_button[0] != old_mouse_button[0]) { + if (mb[0] != old_mouse_button[0] || mb[1] != old_mouse_button[1] || mb[2] != old_mouse_button[2]) { uint32 mouse_base = adb_base + 16; - uint8 *mouse_data = Mac2HostAddr(tmp_data); // Call mouse ADB handler if (mouse_reg_3[1] == 4) { // Extended mouse protocol - mouse_data[0] = 3; - mouse_data[1] = mouse_button[0] ? 0 : 0x80; - mouse_data[2] = mouse_button[1] ? 0 : 0x80; - mouse_data[3] = mouse_button[2] ? 0x08 : 0x88; + WriteMacInt8(tmp_data, 3); + WriteMacInt8(tmp_data + 1, mb[0] ? 0 : 0x80); + WriteMacInt8(tmp_data + 2, mb[1] ? 0 : 0x80); + WriteMacInt8(tmp_data + 3, mb[2] ? 0x08 : 0x88); } else { // 100/200 dpi mode - mouse_data[0] = 2; - mouse_data[1] = mouse_button[0] ? 0 : 0x80; - mouse_data[2] = mouse_button[1] ? 0 : 0x80; + WriteMacInt8(tmp_data, 2); + WriteMacInt8(tmp_data + 1, mb[0] ? 0 : 0x80); + WriteMacInt8(tmp_data + 2, mb[1] ? 0 : 0x80); } r.a[0] = tmp_data; r.a[1] = ReadMacInt32(mouse_base); @@ -354,15 +429,13 @@ void ADBInterrupt(void) r.d[0] = (mouse_reg_3[0] << 4) | 0x0c; // Talk 0 Execute68k(r.a[1], &r); - old_mouse_button[0] = mouse_button[0]; - old_mouse_button[1] = mouse_button[1]; - old_mouse_button[2] = mouse_button[2]; + old_mouse_button[0] = mb[0]; + old_mouse_button[1] = mb[1]; + old_mouse_button[2] = mb[2]; } } // Process accumulated keyboard events - uint32 key_base = adb_base + 4; - uint8 *key_data = Mac2HostAddr(tmp_data); while (key_read_ptr != key_write_ptr) { // Read keyboard event @@ -370,9 +443,9 @@ void ADBInterrupt(void) key_read_ptr = (key_read_ptr + 1) % KEY_BUFFER_SIZE; // Call keyboard ADB handler - key_data[0] = 2; - key_data[1] = mac_code; - key_data[2] = mac_code == 0x7f ? 0x7f : 0xff; // Power key is special + WriteMacInt8(tmp_data, 2); + WriteMacInt8(tmp_data + 1, mac_code); + WriteMacInt8(tmp_data + 2, mac_code == 0x7f ? 0x7f : 0xff); // Power key is special r.a[0] = tmp_data; r.a[1] = ReadMacInt32(key_base); r.a[2] = ReadMacInt32(key_base + 4);