ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/emul_op.cpp
Revision: 1.30
Committed: 2004-01-12T15:29:21Z (20 years, 10 months ago) by cebix
Branch: MAIN
CVS Tags: nigel-build-16, nigel-build-15
Changes since 1.29: +1 -1 lines
Log Message:
Happy New Year! :)

File Contents

# Content
1 /*
2 * emul_op.cpp - 68k opcodes for ROM patches
3 *
4 * Basilisk II (C) 1997-2004 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 #include <string.h>
22 #include <stdio.h>
23
24 #include "sysdeps.h"
25 #include "cpu_emulation.h"
26 #include "main.h"
27 #include "macos_util.h"
28 #include "rom_patches.h"
29 #include "rsrc_patches.h"
30 #include "xpram.h"
31 #include "adb.h"
32 #include "timer.h"
33 #include "clip.h"
34 #include "serial.h"
35 #include "sony.h"
36 #include "disk.h"
37 #include "cdrom.h"
38 #include "scsi.h"
39 #include "video.h"
40 #include "audio.h"
41 #include "ether.h"
42 #include "extfs.h"
43 #include "emul_op.h"
44
45 #ifdef ENABLE_MON
46 #include "mon.h"
47 #endif
48
49 #define DEBUG 0
50 #include "debug.h"
51
52
53 /*
54 * Execute EMUL_OP opcode (called by 68k emulator or Illegal Instruction trap handler)
55 */
56
57 void EmulOp(uint16 opcode, M68kRegisters *r)
58 {
59 D(bug("EmulOp %04x\n", opcode));
60 switch (opcode) {
61 case M68K_EMUL_BREAK: { // Breakpoint
62 printf("*** Breakpoint\n");
63 printf("d0 %08x d1 %08x d2 %08x d3 %08x\n"
64 "d4 %08x d5 %08x d6 %08x d7 %08x\n"
65 "a0 %08x a1 %08x a2 %08x a3 %08x\n"
66 "a4 %08x a5 %08x a6 %08x a7 %08x\n"
67 "sr %04x\n",
68 r->d[0], r->d[1], r->d[2], r->d[3], r->d[4], r->d[5], r->d[6], r->d[7],
69 r->a[0], r->a[1], r->a[2], r->a[3], r->a[4], r->a[5], r->a[6], r->a[7],
70 r->sr);
71 VideoQuitFullScreen();
72 #ifdef ENABLE_MON
73 char *arg[4] = {"mon", "-m", "-r", NULL};
74 mon(3, arg);
75 #endif
76 QuitEmulator();
77 break;
78 }
79
80 case M68K_EMUL_OP_SHUTDOWN: // Quit emulator
81 QuitEmulator();
82 break;
83
84 case M68K_EMUL_OP_RESET: { // MacOS reset
85 D(bug("*** RESET ***\n"));
86 TimerReset();
87 EtherReset();
88 AudioReset();
89
90 // Create BootGlobs at top of memory
91 Mac_memset(RAMBaseMac + RAMSize - 4096, 0, 4096);
92 uint32 boot_globs = RAMBaseMac + RAMSize - 0x1c;
93 WriteMacInt32(boot_globs + 0x00, RAMBaseMac); // First RAM bank
94 WriteMacInt32(boot_globs + 0x04, RAMSize);
95 WriteMacInt32(boot_globs + 0x08, 0xffffffff); // End of bank table
96 WriteMacInt32(boot_globs + 0x0c, 0);
97
98 // Setup registers for boot routine
99 r->d[0] = ReadMacInt32(ROMBaseMac + UniversalInfo + 0x18); // AddrMapFlags
100 r->d[1] = ReadMacInt32(ROMBaseMac + UniversalInfo + 0x1c); // UnivROMFlags
101 r->d[2] = ReadMacInt32(ROMBaseMac + UniversalInfo + 0x10); // HWCfgFlags/IDs
102 if (FPUType)
103 r->d[2] |= 0x10000000; // Set FPU flag if FPU present
104 else
105 r->d[2] &= 0xefffffff; // Clear FPU flag if no FPU present
106 r->a[0] = ROMBaseMac + UniversalInfo + ReadMacInt32(ROMBaseMac + UniversalInfo);// AddrMap
107 r->a[1] = ROMBaseMac + UniversalInfo; // UniversalInfo
108 r->a[6] = boot_globs; // BootGlobs
109 r->a[7] = RAMBaseMac + 0x10000; // Boot stack
110 break;
111 }
112
113 case M68K_EMUL_OP_CLKNOMEM: { // Clock/PRAM operations
114 bool is_read = r->d[1] & 0x80;
115 if ((r->d[1] & 0x78) == 0x38) {
116 // XPRAM
117 uint8 reg = (r->d[1] << 5) & 0xe0 | (r->d[1] >> 10) & 0x1f;
118 if (is_read) {
119 r->d[2] = XPRAM[reg];
120 bool localtalk = !(XPRAM[0xe0] || XPRAM[0xe1]); // LocalTalk enabled?
121 switch (reg) {
122 case 0x08:
123 if (ROMVersion != ROM_VERSION_32)
124 r->d[2] &= 0xf8;
125 break;
126 case 0x8a:
127 r->d[2] |= 0x05; // 32bit mode is always enabled
128 break;
129 case 0xe0: // Disable LocalTalk (use EtherTalk instead)
130 if (localtalk)
131 r->d[2] = 0x00;
132 break;
133 case 0xe1:
134 if (localtalk)
135 r->d[2] = 0xf1;
136 break;
137 case 0xe2:
138 if (localtalk)
139 r->d[2] = 0x00;
140 break;
141 case 0xe3:
142 if (localtalk)
143 r->d[2] = 0x0a;
144 break;
145 }
146 D(bug("Read XPRAM %02x->%02lx\n", reg, r->d[2]));
147 } else {
148 D(bug("Write XPRAM %02x<-%02lx\n", reg, r->d[2] & 0xff));
149 if (reg == 0x8a && !TwentyFourBitAddressing)
150 r->d[2] |= 0x05; // 32bit mode is always enabled if possible
151 XPRAM[reg] = r->d[2];
152 }
153 } else {
154 // PRAM, RTC and other clock registers
155 uint8 reg = (r->d[1] >> 2) & 0x1f;
156 if (reg >= 0x10 || (reg >= 0x08 && reg < 0x0c)) {
157 if (is_read) {
158 r->d[2] = XPRAM[reg];
159 D(bug("Read XPRAM %02x->%02x\n", reg, XPRAM[reg]));
160 } else {
161 D(bug("Write PRAM %02x<-%02lx\n", reg, r->d[2]));
162 XPRAM[reg] = r->d[2];
163 }
164 } else if (reg < 0x08 && is_read) {
165 uint32 t = TimerDateTime();
166 uint8 b = t;
167 switch (reg & 3) {
168 case 1: b = t >> 8; break;
169 case 2: b = t >> 16; break;
170 case 3: b = t >> 24; break;
171 }
172 r->d[2] = b;
173 } else
174 D(bug("RTC %s op %d, d1 %08lx d2 %08lx\n", is_read ? "read" : "write", reg, r->d[1], r->d[2]));
175 }
176 r->d[0] = 0;
177 r->d[1] = r->d[2];
178 break;
179 }
180
181 case M68K_EMUL_OP_READ_XPRAM: // Read from XPRAM (ROM10/11)
182 D(bug("Read XPRAM %02lx\n", r->d[1]));
183 r->d[1] = XPRAM[r->d[1] & 0xff];
184 break;
185
186 case M68K_EMUL_OP_READ_XPRAM2: // Read from XPRAM (ROM15)
187 D(bug("Read XPRAM %02lx\n", r->d[0]));
188 r->d[0] = XPRAM[r->d[0] & 0xff];
189 break;
190
191 case M68K_EMUL_OP_PATCH_BOOT_GLOBS: // Patch BootGlobs at startup
192 D(bug("Patch BootGlobs\n"));
193 WriteMacInt32(r->a[4] - 20, RAMBaseMac + RAMSize); // MemTop
194 WriteMacInt8(r->a[4] - 26, 0); // No MMU
195 WriteMacInt8(r->a[4] - 25, ReadMacInt8(r->a[4] - 25) | 1); // No MMU
196 r->a[6] = RAMBaseMac + RAMSize;
197 break;
198
199 case M68K_EMUL_OP_FIX_BOOTSTACK: // Set boot stack to 3/4 of RAM (7.5)
200 r->a[1] = RAMBaseMac + RAMSize * 3 / 4;
201 D(bug("Fix boot stack %08x\n", r->a[1]));
202 break;
203
204 case M68K_EMUL_OP_FIX_MEMSIZE: { // Set correct logical and physical memory size
205 D(bug("Fix MemSize\n"));
206 uint32 diff = ReadMacInt32(0x1ef8) - ReadMacInt32(0x1ef4); // Difference between logical and physical size
207 WriteMacInt32(0x1ef8, RAMSize); // Physical RAM size
208 WriteMacInt32(0x1ef4, RAMSize - diff); // Logical RAM size
209 break;
210 }
211
212 case M68K_EMUL_OP_ADBOP: // ADBOp() replacement
213 ADBOp(r->d[0], Mac2HostAddr(ReadMacInt32(r->a[0])));
214 break;
215
216 case M68K_EMUL_OP_INSTIME: // InsTime() replacement
217 r->d[0] = InsTime(r->a[0], r->d[1]);
218 break;
219
220 case M68K_EMUL_OP_RMVTIME: // RmvTime() replacement
221 r->d[0] = RmvTime(r->a[0]);
222 break;
223
224 case M68K_EMUL_OP_PRIMETIME: // PrimeTime() replacement
225 r->d[0] = PrimeTime(r->a[0], r->d[0]);
226 break;
227
228 case M68K_EMUL_OP_MICROSECONDS: // Microseconds() replacement
229 Microseconds(r->a[0], r->d[0]);
230 break;
231
232 case M68K_EMUL_OP_INSTALL_DRIVERS: {// Patch to install our own drivers during startup
233 // Install drivers
234 D(bug("InstallDrivers\n"));
235 InstallDrivers(r->a[0]);
236
237 // Install PutScrap() patch
238 M68kRegisters r;
239 r.d[0] = 0xa9fe;
240 r.a[0] = PutScrapPatch;
241 Execute68kTrap(0xa647, &r); // SetToolTrap()
242
243 // Setup fake ASC registers
244 if (ROMVersion == ROM_VERSION_32) {
245 r.d[0] = 0x1000;
246 Execute68kTrap(0xa71e, &r); // NewPtrSysClear()
247 uint32 asc_regs = r.a[0];
248 D(bug("ASC registers at %08lx\n", asc_regs));
249 WriteMacInt8(asc_regs + 0x800, 0x0f); // Set ASC version number
250 WriteMacInt32(0xcc0, asc_regs); // Set ASCBase
251 }
252 break;
253 }
254
255 case M68K_EMUL_OP_SERD: // Install serial drivers
256 D(bug("InstallSERD\n"));
257 InstallSERD();
258 break;
259
260 case M68K_EMUL_OP_SONY_OPEN: // Floppy driver functions
261 r->d[0] = SonyOpen(r->a[0], r->a[1]);
262 break;
263
264 case M68K_EMUL_OP_SONY_PRIME:
265 r->d[0] = SonyPrime(r->a[0], r->a[1]);
266 break;
267
268 case M68K_EMUL_OP_SONY_CONTROL:
269 r->d[0] = SonyControl(r->a[0], r->a[1]);
270 break;
271
272 case M68K_EMUL_OP_SONY_STATUS:
273 r->d[0] = SonyStatus(r->a[0], r->a[1]);
274 break;
275
276 case M68K_EMUL_OP_DISK_OPEN: // Disk driver functions
277 r->d[0] = DiskOpen(r->a[0], r->a[1]);
278 break;
279
280 case M68K_EMUL_OP_DISK_PRIME:
281 r->d[0] = DiskPrime(r->a[0], r->a[1]);
282 break;
283
284 case M68K_EMUL_OP_DISK_CONTROL:
285 r->d[0] = DiskControl(r->a[0], r->a[1]);
286 break;
287
288 case M68K_EMUL_OP_DISK_STATUS:
289 r->d[0] = DiskStatus(r->a[0], r->a[1]);
290 break;
291
292 case M68K_EMUL_OP_CDROM_OPEN: // CD-ROM driver functions
293 r->d[0] = CDROMOpen(r->a[0], r->a[1]);
294 break;
295
296 case M68K_EMUL_OP_CDROM_PRIME:
297 r->d[0] = CDROMPrime(r->a[0], r->a[1]);
298 break;
299
300 case M68K_EMUL_OP_CDROM_CONTROL:
301 r->d[0] = CDROMControl(r->a[0], r->a[1]);
302 break;
303
304 case M68K_EMUL_OP_CDROM_STATUS:
305 r->d[0] = CDROMStatus(r->a[0], r->a[1]);
306 break;
307
308 case M68K_EMUL_OP_VIDEO_OPEN: // Video driver functions
309 r->d[0] = VideoDriverOpen(r->a[0], r->a[1]);
310 break;
311
312 case M68K_EMUL_OP_VIDEO_CONTROL:
313 r->d[0] = VideoDriverControl(r->a[0], r->a[1]);
314 break;
315
316 case M68K_EMUL_OP_VIDEO_STATUS:
317 r->d[0] = VideoDriverStatus(r->a[0], r->a[1]);
318 break;
319
320 case M68K_EMUL_OP_SERIAL_OPEN: // Serial driver functions
321 r->d[0] = SerialOpen(r->a[0], r->a[1], r->d[0]);
322 break;
323
324 case M68K_EMUL_OP_SERIAL_PRIME:
325 r->d[0] = SerialPrime(r->a[0], r->a[1], r->d[0]);
326 break;
327
328 case M68K_EMUL_OP_SERIAL_CONTROL:
329 r->d[0] = SerialControl(r->a[0], r->a[1], r->d[0]);
330 break;
331
332 case M68K_EMUL_OP_SERIAL_STATUS:
333 r->d[0] = SerialStatus(r->a[0], r->a[1], r->d[0]);
334 break;
335
336 case M68K_EMUL_OP_SERIAL_CLOSE:
337 r->d[0] = SerialClose(r->a[0], r->a[1], r->d[0]);
338 break;
339
340 case M68K_EMUL_OP_ETHER_OPEN: // Ethernet driver functions
341 r->d[0] = EtherOpen(r->a[0], r->a[1]);
342 break;
343
344 case M68K_EMUL_OP_ETHER_CONTROL:
345 r->d[0] = EtherControl(r->a[0], r->a[1]);
346 break;
347
348 case M68K_EMUL_OP_ETHER_READ_PACKET:
349 EtherReadPacket((uint8 **)&r->a[0], r->a[3], r->d[3], r->d[1]);
350 break;
351
352 case M68K_EMUL_OP_SOUNDIN_OPEN: // Sound input driver functions
353 r->d[0] = SoundInOpen(r->a[0], r->a[1]);
354 break;
355
356 case M68K_EMUL_OP_SOUNDIN_PRIME:
357 r->d[0] = SoundInPrime(r->a[0], r->a[1]);
358 break;
359
360 case M68K_EMUL_OP_SOUNDIN_CONTROL:
361 r->d[0] = SoundInControl(r->a[0], r->a[1]);
362 break;
363
364 case M68K_EMUL_OP_SOUNDIN_STATUS:
365 r->d[0] = SoundInStatus(r->a[0], r->a[1]);
366 break;
367
368 case M68K_EMUL_OP_SOUNDIN_CLOSE:
369 r->d[0] = SoundInClose(r->a[0], r->a[1]);
370 break;
371
372 case M68K_EMUL_OP_SCSI_DISPATCH: { // SCSIDispatch() replacement
373 uint32 ret = ReadMacInt32(r->a[7]); // Get return address
374 uint16 sel = ReadMacInt16(r->a[7] + 4); // Get selector
375 r->a[7] += 6;
376 int stack = 0;
377 switch (sel) {
378 case 0: // SCSIReset
379 WriteMacInt16(r->a[7], SCSIReset());
380 stack = 0;
381 break;
382 case 1: // SCSIGet
383 WriteMacInt16(r->a[7], SCSIGet());
384 stack = 0;
385 break;
386 case 2: // SCSISelect
387 case 11: // SCSISelAtn
388 WriteMacInt16(r->a[7] + 2, SCSISelect(ReadMacInt16(r->a[7]) & 0xff));
389 stack = 2;
390 break;
391 case 3: // SCSICmd
392 WriteMacInt16(r->a[7] + 6, SCSICmd(ReadMacInt16(r->a[7]), Mac2HostAddr(ReadMacInt32(r->a[7] + 2))));
393 stack = 6;
394 break;
395 case 4: // SCSIComplete
396 WriteMacInt16(r->a[7] + 12, SCSIComplete(ReadMacInt32(r->a[7]), ReadMacInt32(r->a[7] + 4), ReadMacInt32(r->a[7] + 8)));
397 stack = 12;
398 break;
399 case 5: // SCSIRead
400 case 8: // SCSIRBlind
401 WriteMacInt16(r->a[7] + 4, SCSIRead(ReadMacInt32(r->a[7])));
402 stack = 4;
403 break;
404 case 6: // SCSIWrite
405 case 9: // SCSIWBlind
406 WriteMacInt16(r->a[7] + 4, SCSIWrite(ReadMacInt32(r->a[7])));
407 stack = 4;
408 break;
409 case 10: // SCSIStat
410 WriteMacInt16(r->a[7], SCSIStat());
411 stack = 0;
412 break;
413 case 12: // SCSIMsgIn
414 WriteMacInt16(r->a[7] + 4, 0);
415 stack = 4;
416 break;
417 case 13: // SCSIMsgOut
418 WriteMacInt16(r->a[7] + 2, 0);
419 stack = 2;
420 break;
421 case 14: // SCSIMgrBusy
422 WriteMacInt16(r->a[7], SCSIMgrBusy());
423 stack = 0;
424 break;
425 default:
426 printf("FATAL: SCSIDispatch(%d): illegal selector\n", sel);
427 QuitEmulator();
428 break;
429 }
430 r->a[0] = ret; // "rtd" emulation, a0 = return address, a1 = new stack pointer
431 r->a[1] = r->a[7] + stack;
432 break;
433 }
434
435 case M68K_EMUL_OP_IRQ: // Level 1 interrupt
436 r->d[0] = 0;
437
438 if (InterruptFlags & INTFLAG_60HZ) {
439 ClearInterruptFlag(INTFLAG_60HZ);
440
441 // Increment Ticks variable
442 WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1);
443
444 if (HasMacStarted()) {
445
446 // Mac has started, execute all 60Hz interrupt functions
447 TimerInterrupt();
448 VideoInterrupt();
449
450 // Call DoVBLTask(0)
451 if (ROMVersion == ROM_VERSION_32) {
452 M68kRegisters r2;
453 r2.d[0] = 0;
454 Execute68kTrap(0xa072, &r2);
455 }
456
457 r->d[0] = 1; // Flag: 68k interrupt routine executes VBLTasks etc.
458 }
459 }
460
461 if (InterruptFlags & INTFLAG_1HZ) {
462 ClearInterruptFlag(INTFLAG_1HZ);
463 if (HasMacStarted()) {
464 SonyInterrupt();
465 DiskInterrupt();
466 CDROMInterrupt();
467 }
468 }
469
470 if (InterruptFlags & INTFLAG_SERIAL) {
471 ClearInterruptFlag(INTFLAG_SERIAL);
472 SerialInterrupt();
473 }
474
475 if (InterruptFlags & INTFLAG_ETHER) {
476 ClearInterruptFlag(INTFLAG_ETHER);
477 EtherInterrupt();
478 }
479
480 if (InterruptFlags & INTFLAG_AUDIO) {
481 ClearInterruptFlag(INTFLAG_AUDIO);
482 AudioInterrupt();
483 }
484
485 if (InterruptFlags & INTFLAG_ADB) {
486 ClearInterruptFlag(INTFLAG_ADB);
487 if (HasMacStarted())
488 ADBInterrupt();
489 }
490
491 if (InterruptFlags & INTFLAG_NMI) {
492 ClearInterruptFlag(INTFLAG_NMI);
493 if (HasMacStarted())
494 TriggerNMI();
495 }
496 break;
497
498 case M68K_EMUL_OP_PUT_SCRAP: { // PutScrap() patch
499 void *scrap = Mac2HostAddr(ReadMacInt32(r->a[7] + 4));
500 uint32 type = ReadMacInt32(r->a[7] + 8);
501 int32 length = ReadMacInt32(r->a[7] + 12);
502 PutScrap(type, scrap, length);
503 break;
504 }
505
506 case M68K_EMUL_OP_CHECKLOAD: { // vCheckLoad() patch (resource loader)
507 uint32 type = r->d[1];
508 int16 id = ReadMacInt16(r->a[2]);
509 if (r->a[0] == 0)
510 break;
511 uint32 adr = ReadMacInt32(r->a[0]);
512 if (adr == 0)
513 break;
514 uint8 *p = Mac2HostAddr(adr);
515 uint32 size = ReadMacInt32(adr - 8) & 0xffffff;
516 CheckLoad(type, id, p, size);
517 break;
518 }
519
520 case M68K_EMUL_OP_AUDIO: // Audio component dispatch function
521 r->d[0] = AudioDispatch(r->a[3], r->a[4]);
522 break;
523
524 #if SUPPORTS_EXTFS
525 case M68K_EMUL_OP_EXTFS_COMM: // External file system routines
526 WriteMacInt16(r->a[7] + 14, ExtFSComm(ReadMacInt16(r->a[7] + 12), ReadMacInt32(r->a[7] + 8), ReadMacInt32(r->a[7] + 4)));
527 break;
528
529 case M68K_EMUL_OP_EXTFS_HFS:
530 WriteMacInt16(r->a[7] + 20, ExtFSHFS(ReadMacInt32(r->a[7] + 16), ReadMacInt16(r->a[7] + 14), ReadMacInt32(r->a[7] + 10), ReadMacInt32(r->a[7] + 6), ReadMacInt16(r->a[7] + 4)));
531 break;
532 #endif
533
534 case M68K_EMUL_OP_BLOCK_MOVE: // BlockMove() cache flushing
535 FlushCodeCache(Mac2HostAddr(r->a[0]), r->a[1]);
536 break;
537
538 case M68K_EMUL_OP_DEBUGUTIL:
539 // printf("DebugUtil d0=%08lx a5=%08lx\n", r->d[0], r->a[5]);
540 r->d[0] = DebugUtil(r->d[0]);
541 break;
542
543 default:
544 printf("FATAL: EMUL_OP called with bogus opcode %08x\n", opcode);
545 printf("d0 %08x d1 %08x d2 %08x d3 %08x\n"
546 "d4 %08x d5 %08x d6 %08x d7 %08x\n"
547 "a0 %08x a1 %08x a2 %08x a3 %08x\n"
548 "a4 %08x a5 %08x a6 %08x a7 %08x\n"
549 "sr %04x\n",
550 r->d[0], r->d[1], r->d[2], r->d[3], r->d[4], r->d[5], r->d[6], r->d[7],
551 r->a[0], r->a[1], r->a[2], r->a[3], r->a[4], r->a[5], r->a[6], r->a[7],
552 r->sr);
553 #ifdef ENABLE_MON
554 char *arg[4] = {"mon", "-m", "-r", NULL};
555 mon(3, arg);
556 #endif
557 QuitEmulator();
558 break;
559 }
560 }