ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/emul_op.cpp
Revision: 1.7
Committed: 1999-10-22T15:08:09Z (25 years ago) by cebix
Branch: MAIN
Changes since 1.6: +4 -0 lines
Log Message:
- added BlockMove() replacement
- extfs.cpp: get_path_for_fsitem(root parent) no longer crashes

File Contents

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