ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/adb.cpp
Revision: 1.7
Committed: 2001-07-09T11:21:59Z (23 years, 4 months ago) by cebix
Branch: MAIN
Changes since 1.6: +20 -7 lines
Log Message:
- ADB has its own interrupt flag, INTFLAG_ADB
- ADBMouseMoved(), ADBMouseDown/Up() and ADBKeyDown/Up() trigger the ADB
  interrupt
- ADB mutex is only used for mouse movement (the only input state where it
  matters)
- adb.cpp: toggling relative mouse mode resets mouse_x/y
- PrimeTime(0) schedules a timer task with 0 delay time; this is still not
  the correct implementation, but it makes MacSyndicate work...
- Unix: pthreads are preferred to POSIX.4 timers for 60Hz ticks because the
  timers drift badly under Linux and the thread can compensate for drifting
  well enough
- Unix: moved GetTicks_usec() and Delay_usec() to timer_unix.cpp
- video_x.cpp: X mouse acceleration is disabled in relative mouse mode because
  MacOS does its own acceleration
- video_x.cpp: palette[].pixel and palette[].flags are always preset
- video_x.cpp: decoupled X event handling from 60Hz video refresh cycle by
  using select() with a timeout on the X fd

File Contents

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