ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/emul_op.cpp
Revision: 1.8
Committed: 1999-10-25T08:07:45Z (25 years ago) by cebix
Branch: MAIN
Changes since 1.7: +10 -0 lines
Log Message:
- now uses "mon" if present (currently on breakpoints only)

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 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     char *arg[2];
73     arg[0] = "rmon";
74     arg[1] = NULL;
75     mon(1, arg);
76     #endif
77 cebix 1.1 QuitEmulator();
78     break;
79     }
80    
81     case M68K_EMUL_OP_SHUTDOWN: // Quit emulator
82     QuitEmulator();
83     break;
84    
85     case M68K_EMUL_OP_RESET: { // MacOS reset
86     D(bug("*** RESET ***\n"));
87     TimerReset();
88     EtherReset();
89    
90     // Create BootGlobs at top of memory
91     memset((void *)(RAMBaseHost + 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 cebix 1.2 uint8 b = t;
167 cebix 1.1 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     D(bug("Fix boot stack\n"));
201     r->a[1] = RAMBaseMac + RAMSize * 3 / 4;
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 cebix 1.5 r->d[0] = VideoDriverOpen(r->a[0], r->a[1]);
310 cebix 1.1 break;
311    
312     case M68K_EMUL_OP_VIDEO_CONTROL:
313 cebix 1.5 r->d[0] = VideoDriverControl(r->a[0], r->a[1]);
314 cebix 1.1 break;
315    
316     case M68K_EMUL_OP_VIDEO_STATUS:
317 cebix 1.5 r->d[0] = VideoDriverStatus(r->a[0], r->a[1]);
318 cebix 1.1 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_SCSI_DISPATCH: { // SCSIDispatch() replacement
353     uint32 ret = ReadMacInt32(r->a[7]); // Get return address
354     uint16 sel = ReadMacInt16(r->a[7] + 4); // Get selector
355     r->a[7] += 6;
356     int stack = 0;
357     switch (sel) {
358     case 0: // SCSIReset
359     WriteMacInt16(r->a[7], SCSIReset());
360     stack = 0;
361     break;
362     case 1: // SCSIGet
363     WriteMacInt16(r->a[7], SCSIGet());
364     stack = 0;
365     break;
366     case 2: // SCSISelect
367     case 11: // SCSISelAtn
368     WriteMacInt16(r->a[7] + 2, SCSISelect(ReadMacInt16(r->a[7]) & 0xff));
369     stack = 2;
370     break;
371     case 3: // SCSICmd
372     WriteMacInt16(r->a[7] + 6, SCSICmd(ReadMacInt16(r->a[7]), Mac2HostAddr(ReadMacInt32(r->a[7] + 2))));
373     stack = 6;
374     break;
375     case 4: // SCSIComplete
376     WriteMacInt16(r->a[7] + 12, SCSIComplete(ReadMacInt32(r->a[7]), ReadMacInt32(r->a[7] + 4), ReadMacInt32(r->a[7] + 8)));
377     stack = 12;
378     break;
379     case 5: // SCSIRead
380     case 8: // SCSIRBlind
381     WriteMacInt16(r->a[7] + 4, SCSIRead(ReadMacInt32(r->a[7])));
382     stack = 4;
383     break;
384     case 6: // SCSIWrite
385     case 9: // SCSIWBlind
386     WriteMacInt16(r->a[7] + 4, SCSIWrite(ReadMacInt32(r->a[7])));
387     stack = 4;
388     break;
389     case 10: // SCSIStat
390     WriteMacInt16(r->a[7], SCSIStat());
391     stack = 0;
392     break;
393     case 12: // SCSIMsgIn
394     WriteMacInt16(r->a[7] + 4, 0);
395     stack = 4;
396     break;
397     case 13: // SCSIMsgOut
398     WriteMacInt16(r->a[7] + 2, 0);
399     stack = 2;
400     break;
401     case 14: // SCSIMgrBusy
402     WriteMacInt16(r->a[7], SCSIMgrBusy());
403     stack = 0;
404     break;
405     default:
406     printf("FATAL: SCSIDispatch(%d): illegal selector\n", sel);
407     QuitEmulator();
408     break;
409     }
410     r->a[0] = ret; // "rtd" emulation, a0 = return address, a1 = new stack pointer
411     r->a[1] = r->a[7] + stack;
412     break;
413     }
414    
415     case M68K_EMUL_OP_MEMORY_DISPATCH: { // MemoryDispatch() replacement routine
416     int16 sel = r->d[0];
417     D(bug("MemoryDispatch(%d)\n", sel));
418     switch (sel) {
419     case -6: // GetLogicalRAMSize
420     r->d[0] = RAMSize;
421     break;
422     case -3:
423     r->d[0] = 0x1000;
424     break;
425     case 0: // HoldMemory
426     case 1: // UnholdMemory
427     case 2: // LockMemory
428     case 3: // UnlockMemory
429     case 4: // LockMemoryContiguous
430     case 6: // ProtectMemory
431     case 7: // UnprotectMemory
432     r->d[0] = 0;
433     break;
434     default:
435     printf("FATAL: MemoryDispatch(%d): illegal selector\n", sel);
436     r->d[0] = (uint32)-502;
437     break;
438     }
439     break;
440     }
441    
442     case M68K_EMUL_OP_IRQ: // Level 1 interrupt
443     r->d[0] = 0;
444     if (InterruptFlags & INTFLAG_60HZ) {
445     ClearInterruptFlag(INTFLAG_60HZ);
446     if (HasMacStarted()) {
447    
448 cebix 1.3 // Mac has started, execute all 60Hz interrupt functions
449 cebix 1.1 ADBInterrupt();
450     TimerInterrupt();
451     VideoInterrupt();
452 cebix 1.3 SonyInterrupt();
453     DiskInterrupt();
454     CDROMInterrupt();
455 cebix 1.1
456     // Call DoVBLTask(0)
457     if (ROMVersion == ROM_VERSION_32) {
458     M68kRegisters r2;
459     r2.d[0] = 0;
460     Execute68kTrap(0xa072, &r2);
461     }
462    
463     r->d[0] = 1; // Flag: 68k interrupt routine executes VBLTasks etc.
464     }
465     }
466     if (InterruptFlags & INTFLAG_SERIAL) {
467     ClearInterruptFlag(INTFLAG_SERIAL);
468     SerialInterrupt();
469     }
470     if (InterruptFlags & INTFLAG_ETHER) {
471     ClearInterruptFlag(INTFLAG_ETHER);
472     EtherInterrupt();
473     }
474     if (InterruptFlags & INTFLAG_AUDIO) {
475     ClearInterruptFlag(INTFLAG_AUDIO);
476     AudioInterrupt();
477     }
478     break;
479    
480     case M68K_EMUL_OP_PUT_SCRAP: { // PutScrap() patch
481     void *scrap = Mac2HostAddr(ReadMacInt32(r->a[7] + 4));
482     uint32 type = ReadMacInt32(r->a[7] + 8);
483     int32 length = ReadMacInt32(r->a[7] + 12);
484     PutScrap(type, scrap, length);
485     break;
486     }
487    
488     case M68K_EMUL_OP_CHECKLOAD: { // vCheckLoad() patch (resource loader)
489     uint32 type = r->d[1];
490     int16 id = ReadMacInt16(r->a[2]);
491     if (r->a[0] == 0)
492     break;
493     uint32 adr = ReadMacInt32(r->a[0]);
494     if (adr == 0)
495     break;
496     uint8 *p = Mac2HostAddr(adr);
497     uint32 size = ReadMacInt32(adr - 8) & 0xffffff;
498     CheckLoad(type, id, p, size);
499     break;
500     }
501    
502     case M68K_EMUL_OP_AUDIO: // Audio component dispatch function
503     r->d[0] = AudioDispatch(r->a[3], r->a[4]);
504 cebix 1.4 break;
505    
506 cebix 1.6 #if SUPPORTS_EXTFS
507 cebix 1.4 case M68K_EMUL_OP_EXTFS_COMM: // External file system routines
508     WriteMacInt16(r->a[7] + 14, ExtFSComm(ReadMacInt16(r->a[7] + 12), ReadMacInt32(r->a[7] + 8), ReadMacInt32(r->a[7] + 4)));
509     break;
510    
511     case M68K_EMUL_OP_EXTFS_HFS:
512     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)));
513 cebix 1.1 break;
514 cebix 1.6 #endif
515 cebix 1.1
516 cebix 1.7 case M68K_EMUL_OP_BLOCK_MOVE: // BlockMove() replacement
517     memmove(Mac2HostAddr(r->a[1]), Mac2HostAddr(r->a[0]), r->d[0]);
518     break;
519    
520 cebix 1.1 default:
521     printf("FATAL: EMUL_OP called with bogus opcode %08x\n", opcode);
522     printf("d0 %08lx d1 %08lx d2 %08lx d3 %08lx\n"
523     "d4 %08lx d5 %08lx d6 %08lx d7 %08lx\n"
524     "a0 %08lx a1 %08lx a2 %08lx a3 %08lx\n"
525     "a4 %08lx a5 %08lx a6 %08lx a7 %08lx\n"
526     "sr %04x\n",
527     r->d[0], r->d[1], r->d[2], r->d[3], r->d[4], r->d[5], r->d[6], r->d[7],
528     r->a[0], r->a[1], r->a[2], r->a[3], r->a[4], r->a[5], r->a[6], r->a[7],
529     r->sr);
530     QuitEmulator();
531     break;
532     }
533     }