ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/adb.cpp
Revision: 1.14
Committed: 2004-11-22T22:42:55Z (20 years ago) by gbeauche
Branch: MAIN
Changes since 1.13: +13 -8 lines
Log Message:
Generate CursorDeviceDispatch() call onto the SheepShaver Procedures region

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * adb.cpp - ADB emulation (mouse/keyboard)
3     *
4 cebix 1.12 * Basilisk II (C) 1997-2004 Christian Bauer
5 cebix 1.1 *
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
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20    
21     /*
22     * SEE ALSO
23     * Inside Macintosh: Devices, chapter 5 "ADB Manager"
24     * Technote HW 01: "ADB - The Untold Story: Space Aliens Ate My Mouse"
25     */
26    
27     #include <stdlib.h>
28    
29     #include "sysdeps.h"
30     #include "cpu_emulation.h"
31 nigel 1.10 #include "emul_op.h"
32 cebix 1.1 #include "main.h"
33 nigel 1.10 #include "prefs.h"
34 cebix 1.1 #include "video.h"
35     #include "adb.h"
36    
37 gbeauche 1.14 #ifdef POWERPC_ROM
38     #include "thunks.h"
39     #endif
40    
41 cebix 1.1 #define DEBUG 0
42     #include "debug.h"
43    
44    
45     // Global variables
46     static int mouse_x = 0, mouse_y = 0; // Mouse position
47     static int old_mouse_x = 0, old_mouse_y = 0;
48     static bool mouse_button[3] = {false, false, false}; // Mouse button states
49     static bool old_mouse_button[3] = {false, false, false};
50     static bool relative_mouse = false;
51    
52     static uint8 key_states[16]; // Key states (Mac keycodes)
53     #define MATRIX(code) (key_states[code >> 3] & (1 << (~code & 7)))
54    
55     // Keyboard event buffer (Mac keycodes with up/down flag)
56     const int KEY_BUFFER_SIZE = 16;
57     static uint8 key_buffer[KEY_BUFFER_SIZE];
58     static unsigned int key_read_ptr = 0, key_write_ptr = 0;
59    
60     static uint8 mouse_reg_3[2] = {0x63, 0x01}; // Mouse ADB register 3
61    
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 nigel 1.10 static uint8 m_keyboard_type = 0x05;
66    
67 cebix 1.7 // ADB mouse motion lock (for platforms that use separate input thread)
68 cebix 1.6 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 nigel 1.10 m_keyboard_type = (uint8)PrefsFindInt32("keyboardtype");
79     key_reg_3[1] = m_keyboard_type;
80 cebix 1.6 }
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 cebix 1.1
96     /*
97     * ADBOp() replacement
98     */
99    
100     void ADBOp(uint8 op, uint8 *data)
101     {
102     D(bug("ADBOp op %02x, data %02x %02x %02x\n", op, data[0], data[1], data[2]));
103    
104     // ADB reset?
105     if ((op & 0x0f) == 0) {
106     mouse_reg_3[0] = 0x63;
107     mouse_reg_3[1] = 0x01;
108     key_reg_2[0] = 0xff;
109     key_reg_2[1] = 0xff;
110     key_reg_3[0] = 0x62;
111 nigel 1.10 key_reg_3[1] = m_keyboard_type;
112 cebix 1.1 return;
113     }
114    
115     // Cut op into fields
116     uint8 adr = op >> 4;
117     uint8 cmd = (op >> 2) & 3;
118     uint8 reg = op & 3;
119    
120     // Check which device was addressed and act accordingly
121     if (adr == (mouse_reg_3[0] & 0x0f)) {
122    
123     // Mouse
124     if (cmd == 2) {
125    
126     // Listen
127     switch (reg) {
128     case 3: // Address/HandlerID
129     if (data[2] == 0xfe) // Change address
130     mouse_reg_3[0] = (mouse_reg_3[0] & 0xf0) | (data[1] & 0x0f);
131     else if (data[2] == 1 || data[2] == 2 || data[2] == 4) // Change device handler ID
132     mouse_reg_3[1] = data[2];
133     else if (data[2] == 0x00) // Change address and enable bit
134     mouse_reg_3[0] = (mouse_reg_3[0] & 0xd0) | (data[1] & 0x2f);
135     break;
136     }
137    
138     } else if (cmd == 3) {
139    
140     // Talk
141     switch (reg) {
142     case 1: // Extended mouse protocol
143     data[0] = 8;
144     data[1] = 'a'; // Identifier
145     data[2] = 'p';
146     data[3] = 'p';
147     data[4] = 'l';
148     data[5] = 300 >> 8; // Resolution (dpi)
149     data[6] = 300 & 0xff;
150     data[7] = 1; // Class (mouse)
151     data[8] = 3; // Number of buttons
152     break;
153     case 3: // Address/HandlerID
154     data[0] = 2;
155     data[1] = mouse_reg_3[0] & 0xf0 | (rand() & 0x0f);
156     data[2] = mouse_reg_3[1];
157     break;
158     default:
159     data[0] = 0;
160     break;
161     }
162     }
163     D(bug(" mouse reg 3 %02x%02x\n", mouse_reg_3[0], mouse_reg_3[1]));
164    
165     } else if (adr == (key_reg_3[0] & 0x0f)) {
166    
167     // Keyboard
168     if (cmd == 2) {
169    
170     // Listen
171     switch (reg) {
172     case 2: // LEDs/Modifiers
173     key_reg_2[0] = data[1];
174     key_reg_2[1] = data[2];
175     break;
176     case 3: // Address/HandlerID
177     if (data[2] == 0xfe) // Change address
178     key_reg_3[0] = (key_reg_3[0] & 0xf0) | (data[1] & 0x0f);
179     else if (data[2] == 0x00) // Change address and enable bit
180     key_reg_3[0] = (key_reg_3[0] & 0xd0) | (data[1] & 0x2f);
181     break;
182     }
183    
184     } else if (cmd == 3) {
185    
186     // Talk
187     switch (reg) {
188     case 2: { // LEDs/Modifiers
189     uint8 reg2hi = 0xff;
190     uint8 reg2lo = key_reg_2[1] | 0xf8;
191     if (MATRIX(0x6b)) // Scroll Lock
192     reg2lo &= ~0x40;
193     if (MATRIX(0x47)) // Num Lock
194     reg2lo &= ~0x80;
195     if (MATRIX(0x37)) // Command
196     reg2hi &= ~0x01;
197     if (MATRIX(0x3a)) // Option
198     reg2hi &= ~0x02;
199     if (MATRIX(0x38)) // Shift
200     reg2hi &= ~0x04;
201     if (MATRIX(0x36)) // Control
202     reg2hi &= ~0x08;
203     if (MATRIX(0x39)) // Caps Lock
204     reg2hi &= ~0x20;
205     if (MATRIX(0x75)) // Delete
206     reg2hi &= ~0x40;
207     data[0] = 2;
208     data[1] = reg2hi;
209     data[2] = reg2lo;
210     break;
211     }
212     case 3: // Address/HandlerID
213     data[0] = 2;
214     data[1] = key_reg_3[0] & 0xf0 | (rand() & 0x0f);
215     data[2] = key_reg_3[1];
216     break;
217     default:
218     data[0] = 0;
219     break;
220     }
221     }
222     D(bug(" keyboard reg 3 %02x%02x\n", key_reg_3[0], key_reg_3[1]));
223    
224     } else // Unknown address
225     if (cmd == 3)
226     data[0] = 0; // Talk: 0 bytes of data
227     }
228    
229    
230     /*
231 cebix 1.4 * Mouse was moved (x/y are absolute or relative, depending on ADBSetRelMouseMode())
232 cebix 1.1 */
233    
234     void ADBMouseMoved(int x, int y)
235     {
236 cebix 1.6 B2_lock_mutex(mouse_lock);
237 cebix 1.1 if (relative_mouse) {
238     mouse_x += x; mouse_y += y;
239     } else {
240     mouse_x = x; mouse_y = y;
241     }
242 cebix 1.6 B2_unlock_mutex(mouse_lock);
243 cebix 1.7 SetInterruptFlag(INTFLAG_ADB);
244     TriggerInterrupt();
245 cebix 1.1 }
246    
247    
248     /*
249     * Mouse button pressed
250     */
251    
252     void ADBMouseDown(int button)
253     {
254     mouse_button[button] = true;
255 cebix 1.7 SetInterruptFlag(INTFLAG_ADB);
256     TriggerInterrupt();
257 cebix 1.1 }
258    
259    
260     /*
261 cebix 1.7 * Mouse button released
262 cebix 1.1 */
263    
264     void ADBMouseUp(int button)
265     {
266     mouse_button[button] = false;
267 cebix 1.7 SetInterruptFlag(INTFLAG_ADB);
268     TriggerInterrupt();
269 cebix 1.1 }
270    
271    
272     /*
273     * Set mouse mode (absolute or relative)
274     */
275    
276     void ADBSetRelMouseMode(bool relative)
277     {
278 cebix 1.7 if (relative_mouse != relative) {
279     relative_mouse = relative;
280     mouse_x = mouse_y = 0;
281     }
282 cebix 1.1 }
283    
284    
285     /*
286     * Key pressed ("code" is the Mac key code)
287     */
288    
289     void ADBKeyDown(int code)
290     {
291     // Add keycode to buffer
292     key_buffer[key_write_ptr] = code;
293     key_write_ptr = (key_write_ptr + 1) % KEY_BUFFER_SIZE;
294    
295     // Set key in matrix
296     key_states[code >> 3] |= (1 << (~code & 7));
297 cebix 1.7
298     // Trigger interrupt
299     SetInterruptFlag(INTFLAG_ADB);
300     TriggerInterrupt();
301 cebix 1.1 }
302    
303    
304     /*
305     * Key released ("code" is the Mac key code)
306     */
307    
308     void ADBKeyUp(int code)
309     {
310     // Add keycode to buffer
311     key_buffer[key_write_ptr] = code | 0x80; // Key-up flag
312     key_write_ptr = (key_write_ptr + 1) % KEY_BUFFER_SIZE;
313    
314     // Clear key in matrix
315     key_states[code >> 3] &= ~(1 << (~code & 7));
316 cebix 1.7
317     // Trigger interrupt
318     SetInterruptFlag(INTFLAG_ADB);
319     TriggerInterrupt();
320 cebix 1.1 }
321    
322    
323     /*
324     * ADB interrupt function (executed as part of 60Hz interrupt)
325     */
326    
327     void ADBInterrupt(void)
328     {
329     M68kRegisters r;
330    
331     // Return if ADB is not initialized
332     uint32 adb_base = ReadMacInt32(0xcf8);
333     if (!adb_base || adb_base == 0xffffffff)
334     return;
335     uint32 tmp_data = adb_base + 0x163; // Temporary storage for faked ADB data
336    
337 cebix 1.6 // Get mouse state
338     B2_lock_mutex(mouse_lock);
339 cebix 1.1 int mx = mouse_x;
340     int my = mouse_y;
341 cebix 1.6 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 cebix 1.1
346 cebix 1.4 uint32 key_base = adb_base + 4;
347     uint32 mouse_base = adb_base + 16;
348    
349 cebix 1.1 if (relative_mouse) {
350    
351     // Mouse movement (relative) and buttons
352 cebix 1.6 if (mx != 0 || my != 0 || mb[0] != old_mouse_button[0] || mb[1] != old_mouse_button[1] || mb[2] != old_mouse_button[2]) {
353 cebix 1.1
354     // Call mouse ADB handler
355     if (mouse_reg_3[1] == 4) {
356     // Extended mouse protocol
357 cebix 1.2 WriteMacInt8(tmp_data, 3);
358 cebix 1.6 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 cebix 1.1 } else {
362     // 100/200 dpi mode
363 cebix 1.2 WriteMacInt8(tmp_data, 2);
364 cebix 1.6 WriteMacInt8(tmp_data + 1, (my & 0x7f) | (mb[0] ? 0 : 0x80));
365     WriteMacInt8(tmp_data + 2, (mx & 0x7f) | (mb[1] ? 0 : 0x80));
366 cebix 1.1 }
367     r.a[0] = tmp_data;
368     r.a[1] = ReadMacInt32(mouse_base);
369     r.a[2] = ReadMacInt32(mouse_base + 4);
370     r.a[3] = adb_base;
371     r.d[0] = (mouse_reg_3[0] << 4) | 0x0c; // Talk 0
372     Execute68k(r.a[1], &r);
373    
374 cebix 1.6 old_mouse_button[0] = mb[0];
375     old_mouse_button[1] = mb[1];
376     old_mouse_button[2] = mb[2];
377 cebix 1.1 }
378    
379     } else {
380    
381     // Update mouse position (absolute)
382     if (mx != old_mouse_x || my != old_mouse_y) {
383 cebix 1.4 #ifdef POWERPC_ROM
384 gbeauche 1.14 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 cebix 1.4 };
392 gbeauche 1.14 BUILD_SHEEPSHAVER_PROCEDURE(proc);
393 cebix 1.4 r.a[0] = ReadMacInt32(mouse_base + 4);
394     r.d[0] = mx;
395     r.d[1] = my;
396 gbeauche 1.14 Execute68k(proc, &r);
397 cebix 1.4 #else
398 cebix 1.1 WriteMacInt16(0x82a, mx);
399     WriteMacInt16(0x828, my);
400     WriteMacInt16(0x82e, mx);
401     WriteMacInt16(0x82c, my);
402     WriteMacInt8(0x8ce, ReadMacInt8(0x8cf)); // CrsrCouple -> CrsrNew
403 cebix 1.4 #endif
404 cebix 1.1 old_mouse_x = mx;
405     old_mouse_y = my;
406     }
407    
408     // Send mouse button events
409 cebix 1.6 if (mb[0] != old_mouse_button[0] || mb[1] != old_mouse_button[1] || mb[2] != old_mouse_button[2]) {
410 cebix 1.1 uint32 mouse_base = adb_base + 16;
411    
412     // Call mouse ADB handler
413     if (mouse_reg_3[1] == 4) {
414     // Extended mouse protocol
415 cebix 1.2 WriteMacInt8(tmp_data, 3);
416 cebix 1.6 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 cebix 1.1 } else {
420     // 100/200 dpi mode
421 cebix 1.2 WriteMacInt8(tmp_data, 2);
422 cebix 1.6 WriteMacInt8(tmp_data + 1, mb[0] ? 0 : 0x80);
423     WriteMacInt8(tmp_data + 2, mb[1] ? 0 : 0x80);
424 cebix 1.1 }
425     r.a[0] = tmp_data;
426     r.a[1] = ReadMacInt32(mouse_base);
427     r.a[2] = ReadMacInt32(mouse_base + 4);
428     r.a[3] = adb_base;
429     r.d[0] = (mouse_reg_3[0] << 4) | 0x0c; // Talk 0
430     Execute68k(r.a[1], &r);
431    
432 cebix 1.6 old_mouse_button[0] = mb[0];
433     old_mouse_button[1] = mb[1];
434     old_mouse_button[2] = mb[2];
435 cebix 1.1 }
436     }
437    
438     // Process accumulated keyboard events
439     while (key_read_ptr != key_write_ptr) {
440    
441     // Read keyboard event
442     uint8 mac_code = key_buffer[key_read_ptr];
443     key_read_ptr = (key_read_ptr + 1) % KEY_BUFFER_SIZE;
444    
445     // Call keyboard ADB handler
446 cebix 1.2 WriteMacInt8(tmp_data, 2);
447     WriteMacInt8(tmp_data + 1, mac_code);
448     WriteMacInt8(tmp_data + 2, mac_code == 0x7f ? 0x7f : 0xff); // Power key is special
449 cebix 1.1 r.a[0] = tmp_data;
450     r.a[1] = ReadMacInt32(key_base);
451     r.a[2] = ReadMacInt32(key_base + 4);
452     r.a[3] = adb_base;
453     r.d[0] = (key_reg_3[0] << 4) | 0x0c; // Talk 0
454     Execute68k(r.a[1], &r);
455     }
456    
457     // Clear temporary data
458     WriteMacInt32(tmp_data, 0);
459     WriteMacInt32(tmp_data + 4, 0);
460     }