ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/emul_op.cpp
Revision: 1.14
Committed: 2000-06-23T14:22:45Z (24 years, 5 months ago) by cebix
Branch: MAIN
CVS Tags: snapshot-13072000
Changes since 1.13: +1 -0 lines
Log Message:
- fixed possible crash when using audio after restarting MacOS

File Contents

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