ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/adb.cpp
Revision: 1.11
Committed: 2004-01-10T08:54:14Z (20 years, 4 months ago) by gbeauche
Branch: MAIN
Changes since 1.10: +7 -7 lines
Log Message:
Make sure 68k procedures are stored on 16-bit word boundaries.

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * adb.cpp - ADB emulation (mouse/keyboard)
3     *
4 cebix 1.8 * Basilisk II (C) 1997-2002 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     #define DEBUG 0
38     #include "debug.h"
39    
40    
41     // Global variables
42     static int mouse_x = 0, mouse_y = 0; // Mouse position
43     static int old_mouse_x = 0, old_mouse_y = 0;
44     static bool mouse_button[3] = {false, false, false}; // Mouse button states
45     static bool old_mouse_button[3] = {false, false, false};
46     static bool relative_mouse = false;
47    
48     static uint8 key_states[16]; // Key states (Mac keycodes)
49     #define MATRIX(code) (key_states[code >> 3] & (1 << (~code & 7)))
50    
51     // Keyboard event buffer (Mac keycodes with up/down flag)
52     const int KEY_BUFFER_SIZE = 16;
53     static uint8 key_buffer[KEY_BUFFER_SIZE];
54     static unsigned int key_read_ptr = 0, key_write_ptr = 0;
55    
56     static uint8 mouse_reg_3[2] = {0x63, 0x01}; // Mouse ADB register 3
57    
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 nigel 1.10 static uint8 m_keyboard_type = 0x05;
62    
63 cebix 1.7 // ADB mouse motion lock (for platforms that use separate input thread)
64 cebix 1.6 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 nigel 1.10 m_keyboard_type = (uint8)PrefsFindInt32("keyboardtype");
75     key_reg_3[1] = m_keyboard_type;
76 cebix 1.6 }
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 cebix 1.1
92     /*
93     * ADBOp() replacement
94     */
95    
96     void ADBOp(uint8 op, uint8 *data)
97     {
98     D(bug("ADBOp op %02x, data %02x %02x %02x\n", op, data[0], data[1], data[2]));
99    
100     // ADB reset?
101     if ((op & 0x0f) == 0) {
102     mouse_reg_3[0] = 0x63;
103     mouse_reg_3[1] = 0x01;
104     key_reg_2[0] = 0xff;
105     key_reg_2[1] = 0xff;
106     key_reg_3[0] = 0x62;
107 nigel 1.10 key_reg_3[1] = m_keyboard_type;
108 cebix 1.1 return;
109     }
110    
111     // Cut op into fields
112     uint8 adr = op >> 4;
113     uint8 cmd = (op >> 2) & 3;
114     uint8 reg = op & 3;
115    
116     // Check which device was addressed and act accordingly
117     if (adr == (mouse_reg_3[0] & 0x0f)) {
118    
119     // Mouse
120     if (cmd == 2) {
121    
122     // Listen
123     switch (reg) {
124     case 3: // Address/HandlerID
125     if (data[2] == 0xfe) // Change address
126     mouse_reg_3[0] = (mouse_reg_3[0] & 0xf0) | (data[1] & 0x0f);
127     else if (data[2] == 1 || data[2] == 2 || data[2] == 4) // Change device handler ID
128     mouse_reg_3[1] = data[2];
129     else if (data[2] == 0x00) // Change address and enable bit
130     mouse_reg_3[0] = (mouse_reg_3[0] & 0xd0) | (data[1] & 0x2f);
131     break;
132     }
133    
134     } else if (cmd == 3) {
135    
136     // Talk
137     switch (reg) {
138     case 1: // Extended mouse protocol
139     data[0] = 8;
140     data[1] = 'a'; // Identifier
141     data[2] = 'p';
142     data[3] = 'p';
143     data[4] = 'l';
144     data[5] = 300 >> 8; // Resolution (dpi)
145     data[6] = 300 & 0xff;
146     data[7] = 1; // Class (mouse)
147     data[8] = 3; // Number of buttons
148     break;
149     case 3: // Address/HandlerID
150     data[0] = 2;
151     data[1] = mouse_reg_3[0] & 0xf0 | (rand() & 0x0f);
152     data[2] = mouse_reg_3[1];
153     break;
154     default:
155     data[0] = 0;
156     break;
157     }
158     }
159     D(bug(" mouse reg 3 %02x%02x\n", mouse_reg_3[0], mouse_reg_3[1]));
160    
161     } else if (adr == (key_reg_3[0] & 0x0f)) {
162    
163     // Keyboard
164     if (cmd == 2) {
165    
166     // Listen
167     switch (reg) {
168     case 2: // LEDs/Modifiers
169     key_reg_2[0] = data[1];
170     key_reg_2[1] = data[2];
171     break;
172     case 3: // Address/HandlerID
173     if (data[2] == 0xfe) // Change address
174     key_reg_3[0] = (key_reg_3[0] & 0xf0) | (data[1] & 0x0f);
175     else if (data[2] == 0x00) // Change address and enable bit
176     key_reg_3[0] = (key_reg_3[0] & 0xd0) | (data[1] & 0x2f);
177     break;
178     }
179    
180     } else if (cmd == 3) {
181    
182     // Talk
183     switch (reg) {
184     case 2: { // LEDs/Modifiers
185     uint8 reg2hi = 0xff;
186     uint8 reg2lo = key_reg_2[1] | 0xf8;
187     if (MATRIX(0x6b)) // Scroll Lock
188     reg2lo &= ~0x40;
189     if (MATRIX(0x47)) // Num Lock
190     reg2lo &= ~0x80;
191     if (MATRIX(0x37)) // Command
192     reg2hi &= ~0x01;
193     if (MATRIX(0x3a)) // Option
194     reg2hi &= ~0x02;
195     if (MATRIX(0x38)) // Shift
196     reg2hi &= ~0x04;
197     if (MATRIX(0x36)) // Control
198     reg2hi &= ~0x08;
199     if (MATRIX(0x39)) // Caps Lock
200     reg2hi &= ~0x20;
201     if (MATRIX(0x75)) // Delete
202     reg2hi &= ~0x40;
203     data[0] = 2;
204     data[1] = reg2hi;
205     data[2] = reg2lo;
206     break;
207     }
208     case 3: // Address/HandlerID
209     data[0] = 2;
210     data[1] = key_reg_3[0] & 0xf0 | (rand() & 0x0f);
211     data[2] = key_reg_3[1];
212     break;
213     default:
214     data[0] = 0;
215     break;
216     }
217     }
218     D(bug(" keyboard reg 3 %02x%02x\n", key_reg_3[0], key_reg_3[1]));
219    
220     } else // Unknown address
221     if (cmd == 3)
222     data[0] = 0; // Talk: 0 bytes of data
223     }
224    
225    
226     /*
227 cebix 1.4 * Mouse was moved (x/y are absolute or relative, depending on ADBSetRelMouseMode())
228 cebix 1.1 */
229    
230     void ADBMouseMoved(int x, int y)
231     {
232 cebix 1.6 B2_lock_mutex(mouse_lock);
233 cebix 1.1 if (relative_mouse) {
234     mouse_x += x; mouse_y += y;
235     } else {
236     mouse_x = x; mouse_y = y;
237     }
238 cebix 1.6 B2_unlock_mutex(mouse_lock);
239 cebix 1.7 SetInterruptFlag(INTFLAG_ADB);
240     TriggerInterrupt();
241 cebix 1.1 }
242    
243    
244     /*
245     * Mouse button pressed
246     */
247    
248     void ADBMouseDown(int button)
249     {
250     mouse_button[button] = true;
251 cebix 1.7 SetInterruptFlag(INTFLAG_ADB);
252     TriggerInterrupt();
253 cebix 1.1 }
254    
255    
256     /*
257 cebix 1.7 * Mouse button released
258 cebix 1.1 */
259    
260     void ADBMouseUp(int button)
261     {
262     mouse_button[button] = false;
263 cebix 1.7 SetInterruptFlag(INTFLAG_ADB);
264     TriggerInterrupt();
265 cebix 1.1 }
266    
267    
268     /*
269     * Set mouse mode (absolute or relative)
270     */
271    
272     void ADBSetRelMouseMode(bool relative)
273     {
274 cebix 1.7 if (relative_mouse != relative) {
275     relative_mouse = relative;
276     mouse_x = mouse_y = 0;
277     }
278 cebix 1.1 }
279    
280    
281     /*
282     * Key pressed ("code" is the Mac key code)
283     */
284    
285     void ADBKeyDown(int code)
286     {
287     // Add keycode to buffer
288     key_buffer[key_write_ptr] = code;
289     key_write_ptr = (key_write_ptr + 1) % KEY_BUFFER_SIZE;
290    
291     // Set key in matrix
292     key_states[code >> 3] |= (1 << (~code & 7));
293 cebix 1.7
294     // Trigger interrupt
295     SetInterruptFlag(INTFLAG_ADB);
296     TriggerInterrupt();
297 cebix 1.1 }
298    
299    
300     /*
301     * Key released ("code" is the Mac key code)
302     */
303    
304     void ADBKeyUp(int code)
305     {
306     // Add keycode to buffer
307     key_buffer[key_write_ptr] = code | 0x80; // Key-up flag
308     key_write_ptr = (key_write_ptr + 1) % KEY_BUFFER_SIZE;
309    
310     // Clear key in matrix
311     key_states[code >> 3] &= ~(1 << (~code & 7));
312 cebix 1.7
313     // Trigger interrupt
314     SetInterruptFlag(INTFLAG_ADB);
315     TriggerInterrupt();
316 cebix 1.1 }
317    
318    
319     /*
320     * ADB interrupt function (executed as part of 60Hz interrupt)
321     */
322    
323     void ADBInterrupt(void)
324     {
325     M68kRegisters r;
326    
327     // Return if ADB is not initialized
328     uint32 adb_base = ReadMacInt32(0xcf8);
329     if (!adb_base || adb_base == 0xffffffff)
330     return;
331     uint32 tmp_data = adb_base + 0x163; // Temporary storage for faked ADB data
332    
333 cebix 1.6 // Get mouse state
334     B2_lock_mutex(mouse_lock);
335 cebix 1.1 int mx = mouse_x;
336     int my = mouse_y;
337 cebix 1.6 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 cebix 1.1
342 cebix 1.4 uint32 key_base = adb_base + 4;
343     uint32 mouse_base = adb_base + 16;
344    
345 cebix 1.1 if (relative_mouse) {
346    
347     // Mouse movement (relative) and buttons
348 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]) {
349 cebix 1.1
350     // Call mouse ADB handler
351     if (mouse_reg_3[1] == 4) {
352     // Extended mouse protocol
353 cebix 1.2 WriteMacInt8(tmp_data, 3);
354 cebix 1.6 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 cebix 1.1 } else {
358     // 100/200 dpi mode
359 cebix 1.2 WriteMacInt8(tmp_data, 2);
360 cebix 1.6 WriteMacInt8(tmp_data + 1, (my & 0x7f) | (mb[0] ? 0 : 0x80));
361     WriteMacInt8(tmp_data + 2, (mx & 0x7f) | (mb[1] ? 0 : 0x80));
362 cebix 1.1 }
363     r.a[0] = tmp_data;
364     r.a[1] = ReadMacInt32(mouse_base);
365     r.a[2] = ReadMacInt32(mouse_base + 4);
366     r.a[3] = adb_base;
367     r.d[0] = (mouse_reg_3[0] << 4) | 0x0c; // Talk 0
368     Execute68k(r.a[1], &r);
369    
370 cebix 1.6 old_mouse_button[0] = mb[0];
371     old_mouse_button[1] = mb[1];
372     old_mouse_button[2] = mb[2];
373 cebix 1.1 }
374    
375     } else {
376    
377     // Update mouse position (absolute)
378     if (mx != old_mouse_x || my != old_mouse_y) {
379 cebix 1.4 #ifdef POWERPC_ROM
380 gbeauche 1.11 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 cebix 1.4 };
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 cebix 1.1 WriteMacInt16(0x82a, mx);
394     WriteMacInt16(0x828, my);
395     WriteMacInt16(0x82e, mx);
396     WriteMacInt16(0x82c, my);
397     WriteMacInt8(0x8ce, ReadMacInt8(0x8cf)); // CrsrCouple -> CrsrNew
398 cebix 1.4 #endif
399 cebix 1.1 old_mouse_x = mx;
400     old_mouse_y = my;
401     }
402    
403     // Send mouse button events
404 cebix 1.6 if (mb[0] != old_mouse_button[0] || mb[1] != old_mouse_button[1] || mb[2] != old_mouse_button[2]) {
405 cebix 1.1 uint32 mouse_base = adb_base + 16;
406    
407     // Call mouse ADB handler
408     if (mouse_reg_3[1] == 4) {
409     // Extended mouse protocol
410 cebix 1.2 WriteMacInt8(tmp_data, 3);
411 cebix 1.6 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 cebix 1.1 } else {
415     // 100/200 dpi mode
416 cebix 1.2 WriteMacInt8(tmp_data, 2);
417 cebix 1.6 WriteMacInt8(tmp_data + 1, mb[0] ? 0 : 0x80);
418     WriteMacInt8(tmp_data + 2, mb[1] ? 0 : 0x80);
419 cebix 1.1 }
420     r.a[0] = tmp_data;
421     r.a[1] = ReadMacInt32(mouse_base);
422     r.a[2] = ReadMacInt32(mouse_base + 4);
423     r.a[3] = adb_base;
424     r.d[0] = (mouse_reg_3[0] << 4) | 0x0c; // Talk 0
425     Execute68k(r.a[1], &r);
426    
427 cebix 1.6 old_mouse_button[0] = mb[0];
428     old_mouse_button[1] = mb[1];
429     old_mouse_button[2] = mb[2];
430 cebix 1.1 }
431     }
432    
433     // Process accumulated keyboard events
434     while (key_read_ptr != key_write_ptr) {
435    
436     // Read keyboard event
437     uint8 mac_code = key_buffer[key_read_ptr];
438     key_read_ptr = (key_read_ptr + 1) % KEY_BUFFER_SIZE;
439    
440     // Call keyboard ADB handler
441 cebix 1.2 WriteMacInt8(tmp_data, 2);
442     WriteMacInt8(tmp_data + 1, mac_code);
443     WriteMacInt8(tmp_data + 2, mac_code == 0x7f ? 0x7f : 0xff); // Power key is special
444 cebix 1.1 r.a[0] = tmp_data;
445     r.a[1] = ReadMacInt32(key_base);
446     r.a[2] = ReadMacInt32(key_base + 4);
447     r.a[3] = adb_base;
448     r.d[0] = (key_reg_3[0] << 4) | 0x0c; // Talk 0
449     Execute68k(r.a[1], &r);
450     }
451    
452     // Clear temporary data
453     WriteMacInt32(tmp_data, 0);
454     WriteMacInt32(tmp_data + 4, 0);
455     }