1 |
cebix |
1.1 |
/* |
2 |
|
|
* adb.cpp - ADB emulation (mouse/keyboard) |
3 |
|
|
* |
4 |
cebix |
1.5 |
* Basilisk II (C) 1997-2001 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 |
|
|
#include "main.h" |
32 |
cebix |
1.4 |
#include "emul_op.h" |
33 |
cebix |
1.1 |
#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 |
cebix |
1.6 |
// ADB mouse input state 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 |
cebix |
1.1 |
|
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 |
cebix |
1.4 |
* Mouse was moved (x/y are absolute or relative, depending on ADBSetRelMouseMode()) |
223 |
cebix |
1.1 |
*/ |
224 |
|
|
|
225 |
|
|
void ADBMouseMoved(int x, int y) |
226 |
|
|
{ |
227 |
cebix |
1.6 |
B2_lock_mutex(mouse_lock); |
228 |
cebix |
1.1 |
if (relative_mouse) { |
229 |
|
|
mouse_x += x; mouse_y += y; |
230 |
|
|
} else { |
231 |
|
|
mouse_x = x; mouse_y = y; |
232 |
|
|
} |
233 |
cebix |
1.6 |
B2_unlock_mutex(mouse_lock); |
234 |
cebix |
1.1 |
} |
235 |
|
|
|
236 |
|
|
|
237 |
|
|
/* |
238 |
|
|
* Mouse button pressed |
239 |
|
|
*/ |
240 |
|
|
|
241 |
|
|
void ADBMouseDown(int button) |
242 |
|
|
{ |
243 |
cebix |
1.6 |
B2_lock_mutex(mouse_lock); |
244 |
cebix |
1.1 |
mouse_button[button] = true; |
245 |
cebix |
1.6 |
B2_unlock_mutex(mouse_lock); |
246 |
cebix |
1.1 |
} |
247 |
|
|
|
248 |
|
|
|
249 |
|
|
/* |
250 |
|
|
* First mouse button released |
251 |
|
|
*/ |
252 |
|
|
|
253 |
|
|
void ADBMouseUp(int button) |
254 |
|
|
{ |
255 |
cebix |
1.6 |
B2_lock_mutex(mouse_lock); |
256 |
cebix |
1.1 |
mouse_button[button] = false; |
257 |
cebix |
1.6 |
B2_unlock_mutex(mouse_lock); |
258 |
cebix |
1.1 |
} |
259 |
|
|
|
260 |
|
|
|
261 |
|
|
/* |
262 |
|
|
* Set mouse mode (absolute or relative) |
263 |
|
|
*/ |
264 |
|
|
|
265 |
|
|
void ADBSetRelMouseMode(bool relative) |
266 |
|
|
{ |
267 |
|
|
relative_mouse = relative; |
268 |
|
|
} |
269 |
|
|
|
270 |
|
|
|
271 |
|
|
/* |
272 |
|
|
* Key pressed ("code" is the Mac key code) |
273 |
|
|
*/ |
274 |
|
|
|
275 |
|
|
void ADBKeyDown(int code) |
276 |
|
|
{ |
277 |
|
|
// Add keycode to buffer |
278 |
|
|
key_buffer[key_write_ptr] = code; |
279 |
|
|
key_write_ptr = (key_write_ptr + 1) % KEY_BUFFER_SIZE; |
280 |
|
|
|
281 |
|
|
// Set key in matrix |
282 |
|
|
key_states[code >> 3] |= (1 << (~code & 7)); |
283 |
|
|
} |
284 |
|
|
|
285 |
|
|
|
286 |
|
|
/* |
287 |
|
|
* Key released ("code" is the Mac key code) |
288 |
|
|
*/ |
289 |
|
|
|
290 |
|
|
void ADBKeyUp(int code) |
291 |
|
|
{ |
292 |
|
|
// Add keycode to buffer |
293 |
|
|
key_buffer[key_write_ptr] = code | 0x80; // Key-up flag |
294 |
|
|
key_write_ptr = (key_write_ptr + 1) % KEY_BUFFER_SIZE; |
295 |
|
|
|
296 |
|
|
// Clear key in matrix |
297 |
|
|
key_states[code >> 3] &= ~(1 << (~code & 7)); |
298 |
|
|
} |
299 |
|
|
|
300 |
|
|
|
301 |
|
|
/* |
302 |
|
|
* ADB interrupt function (executed as part of 60Hz interrupt) |
303 |
|
|
*/ |
304 |
|
|
|
305 |
|
|
void ADBInterrupt(void) |
306 |
|
|
{ |
307 |
|
|
M68kRegisters r; |
308 |
|
|
|
309 |
|
|
// Return if ADB is not initialized |
310 |
|
|
uint32 adb_base = ReadMacInt32(0xcf8); |
311 |
|
|
if (!adb_base || adb_base == 0xffffffff) |
312 |
|
|
return; |
313 |
|
|
uint32 tmp_data = adb_base + 0x163; // Temporary storage for faked ADB data |
314 |
|
|
|
315 |
cebix |
1.6 |
// Get mouse state |
316 |
|
|
B2_lock_mutex(mouse_lock); |
317 |
cebix |
1.1 |
int mx = mouse_x; |
318 |
|
|
int my = mouse_y; |
319 |
cebix |
1.6 |
if (relative_mouse) |
320 |
|
|
mouse_x = mouse_y = 0; |
321 |
|
|
int mb[3] = {mouse_button[0], mouse_button[1], mouse_button[2]}; |
322 |
|
|
B2_unlock_mutex(mouse_lock); |
323 |
cebix |
1.1 |
|
324 |
cebix |
1.4 |
uint32 key_base = adb_base + 4; |
325 |
|
|
uint32 mouse_base = adb_base + 16; |
326 |
|
|
|
327 |
cebix |
1.1 |
if (relative_mouse) { |
328 |
|
|
|
329 |
|
|
// Mouse movement (relative) and buttons |
330 |
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]) { |
331 |
cebix |
1.1 |
|
332 |
|
|
// Call mouse ADB handler |
333 |
|
|
if (mouse_reg_3[1] == 4) { |
334 |
|
|
// Extended mouse protocol |
335 |
cebix |
1.2 |
WriteMacInt8(tmp_data, 3); |
336 |
cebix |
1.6 |
WriteMacInt8(tmp_data + 1, (my & 0x7f) | (mb[0] ? 0 : 0x80)); |
337 |
|
|
WriteMacInt8(tmp_data + 2, (mx & 0x7f) | (mb[1] ? 0 : 0x80)); |
338 |
|
|
WriteMacInt8(tmp_data + 3, ((my >> 3) & 0x70) | ((mx >> 7) & 0x07) | (mb[2] ? 0x08 : 0x88)); |
339 |
cebix |
1.1 |
} else { |
340 |
|
|
// 100/200 dpi mode |
341 |
cebix |
1.2 |
WriteMacInt8(tmp_data, 2); |
342 |
cebix |
1.6 |
WriteMacInt8(tmp_data + 1, (my & 0x7f) | (mb[0] ? 0 : 0x80)); |
343 |
|
|
WriteMacInt8(tmp_data + 2, (mx & 0x7f) | (mb[1] ? 0 : 0x80)); |
344 |
cebix |
1.1 |
} |
345 |
|
|
r.a[0] = tmp_data; |
346 |
|
|
r.a[1] = ReadMacInt32(mouse_base); |
347 |
|
|
r.a[2] = ReadMacInt32(mouse_base + 4); |
348 |
|
|
r.a[3] = adb_base; |
349 |
|
|
r.d[0] = (mouse_reg_3[0] << 4) | 0x0c; // Talk 0 |
350 |
|
|
Execute68k(r.a[1], &r); |
351 |
|
|
|
352 |
cebix |
1.6 |
old_mouse_button[0] = mb[0]; |
353 |
|
|
old_mouse_button[1] = mb[1]; |
354 |
|
|
old_mouse_button[2] = mb[2]; |
355 |
cebix |
1.1 |
} |
356 |
|
|
|
357 |
|
|
} else { |
358 |
|
|
|
359 |
|
|
// Update mouse position (absolute) |
360 |
|
|
if (mx != old_mouse_x || my != old_mouse_y) { |
361 |
cebix |
1.4 |
#ifdef POWERPC_ROM |
362 |
|
|
static const uint16 proc[] = { |
363 |
|
|
0x2f08, // move.l a0,-(sp) |
364 |
|
|
0x2f00, // move.l d0,-(sp) |
365 |
|
|
0x2f01, // move.l d1,-(sp) |
366 |
|
|
0x7001, // moveq #1,d0 (MoveTo) |
367 |
|
|
0xaadb, // CursorDeviceDispatch |
368 |
|
|
M68K_RTS |
369 |
|
|
}; |
370 |
|
|
r.a[0] = ReadMacInt32(mouse_base + 4); |
371 |
|
|
r.d[0] = mx; |
372 |
|
|
r.d[1] = my; |
373 |
|
|
Execute68k((uint32)proc, &r); |
374 |
|
|
#else |
375 |
cebix |
1.1 |
WriteMacInt16(0x82a, mx); |
376 |
|
|
WriteMacInt16(0x828, my); |
377 |
|
|
WriteMacInt16(0x82e, mx); |
378 |
|
|
WriteMacInt16(0x82c, my); |
379 |
|
|
WriteMacInt8(0x8ce, ReadMacInt8(0x8cf)); // CrsrCouple -> CrsrNew |
380 |
cebix |
1.4 |
#endif |
381 |
cebix |
1.1 |
old_mouse_x = mx; |
382 |
|
|
old_mouse_y = my; |
383 |
|
|
} |
384 |
|
|
|
385 |
|
|
// Send mouse button events |
386 |
cebix |
1.6 |
if (mb[0] != old_mouse_button[0] || mb[1] != old_mouse_button[1] || mb[2] != old_mouse_button[2]) { |
387 |
cebix |
1.1 |
uint32 mouse_base = adb_base + 16; |
388 |
|
|
|
389 |
|
|
// Call mouse ADB handler |
390 |
|
|
if (mouse_reg_3[1] == 4) { |
391 |
|
|
// Extended mouse protocol |
392 |
cebix |
1.2 |
WriteMacInt8(tmp_data, 3); |
393 |
cebix |
1.6 |
WriteMacInt8(tmp_data + 1, mb[0] ? 0 : 0x80); |
394 |
|
|
WriteMacInt8(tmp_data + 2, mb[1] ? 0 : 0x80); |
395 |
|
|
WriteMacInt8(tmp_data + 3, mb[2] ? 0x08 : 0x88); |
396 |
cebix |
1.1 |
} else { |
397 |
|
|
// 100/200 dpi mode |
398 |
cebix |
1.2 |
WriteMacInt8(tmp_data, 2); |
399 |
cebix |
1.6 |
WriteMacInt8(tmp_data + 1, mb[0] ? 0 : 0x80); |
400 |
|
|
WriteMacInt8(tmp_data + 2, mb[1] ? 0 : 0x80); |
401 |
cebix |
1.1 |
} |
402 |
|
|
r.a[0] = tmp_data; |
403 |
|
|
r.a[1] = ReadMacInt32(mouse_base); |
404 |
|
|
r.a[2] = ReadMacInt32(mouse_base + 4); |
405 |
|
|
r.a[3] = adb_base; |
406 |
|
|
r.d[0] = (mouse_reg_3[0] << 4) | 0x0c; // Talk 0 |
407 |
|
|
Execute68k(r.a[1], &r); |
408 |
|
|
|
409 |
cebix |
1.6 |
old_mouse_button[0] = mb[0]; |
410 |
|
|
old_mouse_button[1] = mb[1]; |
411 |
|
|
old_mouse_button[2] = mb[2]; |
412 |
cebix |
1.1 |
} |
413 |
|
|
} |
414 |
|
|
|
415 |
|
|
// Process accumulated keyboard events |
416 |
|
|
while (key_read_ptr != key_write_ptr) { |
417 |
|
|
|
418 |
|
|
// Read keyboard event |
419 |
|
|
uint8 mac_code = key_buffer[key_read_ptr]; |
420 |
|
|
key_read_ptr = (key_read_ptr + 1) % KEY_BUFFER_SIZE; |
421 |
|
|
|
422 |
|
|
// Call keyboard ADB handler |
423 |
cebix |
1.2 |
WriteMacInt8(tmp_data, 2); |
424 |
|
|
WriteMacInt8(tmp_data + 1, mac_code); |
425 |
|
|
WriteMacInt8(tmp_data + 2, mac_code == 0x7f ? 0x7f : 0xff); // Power key is special |
426 |
cebix |
1.1 |
r.a[0] = tmp_data; |
427 |
|
|
r.a[1] = ReadMacInt32(key_base); |
428 |
|
|
r.a[2] = ReadMacInt32(key_base + 4); |
429 |
|
|
r.a[3] = adb_base; |
430 |
|
|
r.d[0] = (key_reg_3[0] << 4) | 0x0c; // Talk 0 |
431 |
|
|
Execute68k(r.a[1], &r); |
432 |
|
|
} |
433 |
|
|
|
434 |
|
|
// Clear temporary data |
435 |
|
|
WriteMacInt32(tmp_data, 0); |
436 |
|
|
WriteMacInt32(tmp_data + 4, 0); |
437 |
|
|
} |