ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/emul_op.cpp
Revision: 1.1
Committed: 1999-10-03T14:16:25Z (25 years, 1 month ago) by cebix
Branch: MAIN
Branch point for: cebix
Log Message:
Initial revision

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     #include "emul_op.h"
43    
44     #define DEBUG 0
45     #include "debug.h"
46    
47    
48     /*
49     * Execute EMUL_OP opcode (called by 68k emulator or Illegal Instruction trap handler)
50     */
51    
52     void EmulOp(uint16 opcode, M68kRegisters *r)
53     {
54     D(bug("EmulOp %04x\n", opcode));
55     switch (opcode) {
56     case M68K_EMUL_BREAK: { // Breakpoint
57     printf("*** Breakpoint\n");
58     printf("d0 %08lx d1 %08lx d2 %08lx d3 %08lx\n"
59     "d4 %08lx d5 %08lx d6 %08lx d7 %08lx\n"
60     "a0 %08lx a1 %08lx a2 %08lx a3 %08lx\n"
61     "a4 %08lx a5 %08lx a6 %08lx a7 %08lx\n"
62     "sr %04x\n",
63     r->d[0], r->d[1], r->d[2], r->d[3], r->d[4], r->d[5], r->d[6], r->d[7],
64     r->a[0], r->a[1], r->a[2], r->a[3], r->a[4], r->a[5], r->a[6], r->a[7],
65     r->sr);
66     QuitEmulator();
67     break;
68     }
69    
70     case M68K_EMUL_OP_SHUTDOWN: // Quit emulator
71     QuitEmulator();
72     break;
73    
74     case M68K_EMUL_OP_RESET: { // MacOS reset
75     D(bug("*** RESET ***\n"));
76     TimerReset();
77     EtherReset();
78    
79     // Create BootGlobs at top of memory
80     memset((void *)(RAMBaseHost + RAMSize - 4096), 0, 4096);
81     uint32 boot_globs = RAMBaseMac + RAMSize - 0x1c;
82     WriteMacInt32(boot_globs + 0x00, RAMBaseMac); // First RAM bank
83     WriteMacInt32(boot_globs + 0x04, RAMSize);
84     WriteMacInt32(boot_globs + 0x08, 0xffffffff); // End of bank table
85     WriteMacInt32(boot_globs + 0x0c, 0);
86    
87     // Setup registers for boot routine
88     r->d[0] = ReadMacInt32(ROMBaseMac + UniversalInfo + 0x18); // AddrMapFlags
89     r->d[1] = ReadMacInt32(ROMBaseMac + UniversalInfo + 0x1c); // UnivROMFlags
90     r->d[2] = ReadMacInt32(ROMBaseMac + UniversalInfo + 0x10); // HWCfgFlags/IDs
91     if (FPUType)
92     r->d[2] |= 0x10000000; // Set FPU flag if FPU present
93     else
94     r->d[2] &= 0xefffffff; // Clear FPU flag if no FPU present
95     r->a[0] = ROMBaseMac + UniversalInfo + ReadMacInt32(ROMBaseMac + UniversalInfo);// AddrMap
96     r->a[1] = ROMBaseMac + UniversalInfo; // UniversalInfo
97     r->a[6] = boot_globs; // BootGlobs
98     r->a[7] = RAMBaseMac + 0x10000; // Boot stack
99     break;
100     }
101    
102     case M68K_EMUL_OP_CLKNOMEM: { // Clock/PRAM operations
103     bool is_read = r->d[1] & 0x80;
104     if ((r->d[1] & 0x78) == 0x38) {
105     // XPRAM
106     uint8 reg = (r->d[1] << 5) & 0xe0 | (r->d[1] >> 10) & 0x1f;
107     if (is_read) {
108     r->d[2] = XPRAM[reg];
109     bool localtalk = !(XPRAM[0xe0] || XPRAM[0xe1]); // LocalTalk enabled?
110     switch (reg) {
111     case 0x08:
112     if (ROMVersion != ROM_VERSION_32)
113     r->d[2] &= 0xf8;
114     break;
115     case 0x8a:
116     r->d[2] |= 0x05; // 32bit mode is always enabled
117     break;
118     case 0xe0: // Disable LocalTalk (use EtherTalk instead)
119     if (localtalk)
120     r->d[2] = 0x00;
121     break;
122     case 0xe1:
123     if (localtalk)
124     r->d[2] = 0xf1;
125     break;
126     case 0xe2:
127     if (localtalk)
128     r->d[2] = 0x00;
129     break;
130     case 0xe3:
131     if (localtalk)
132     r->d[2] = 0x0a;
133     break;
134     }
135     D(bug("Read XPRAM %02x->%02lx\n", reg, r->d[2]));
136     } else {
137     D(bug("Write XPRAM %02x<-%02lx\n", reg, r->d[2] & 0xff));
138     if (reg == 0x8a && !TwentyFourBitAddressing)
139     r->d[2] |= 0x05; // 32bit mode is always enabled if possible
140     XPRAM[reg] = r->d[2];
141     }
142     } else {
143     // PRAM, RTC and other clock registers
144     uint8 reg = (r->d[1] >> 2) & 0x1f;
145     if (reg >= 0x10 || (reg >= 0x08 && reg < 0x0c)) {
146     if (is_read) {
147     r->d[2] = XPRAM[reg];
148     D(bug("Read XPRAM %02x->%02x\n", reg, XPRAM[reg]));
149     } else {
150     D(bug("Write PRAM %02x<-%02lx\n", reg, r->d[2]));
151     XPRAM[reg] = r->d[2];
152     }
153     } else if (reg < 0x08 && is_read) {
154     uint32 t = TimerDateTime();
155     uint8 b;
156     switch (reg & 3) {
157     case 0: b = t; break;
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     r->d[0] = VideoOpen(r->a[0], r->a[1]);
300     break;
301    
302     case M68K_EMUL_OP_VIDEO_CONTROL:
303     r->d[0] = VideoControl(r->a[0], r->a[1]);
304     break;
305    
306     case M68K_EMUL_OP_VIDEO_STATUS:
307     r->d[0] = VideoStatus(r->a[0], r->a[1]);
308     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     // Mac has started, execute video, ADB and Time Manager interrupt functions
439     ADBInterrupt();
440     TimerInterrupt();
441     VideoInterrupt();
442    
443     // Call DoVBLTask(0)
444     if (ROMVersion == ROM_VERSION_32) {
445     M68kRegisters r2;
446     r2.d[0] = 0;
447     Execute68kTrap(0xa072, &r2);
448     }
449    
450     r->d[0] = 1; // Flag: 68k interrupt routine executes VBLTasks etc.
451     }
452     }
453     if (InterruptFlags & INTFLAG_SERIAL) {
454     ClearInterruptFlag(INTFLAG_SERIAL);
455     SerialInterrupt();
456     }
457     if (InterruptFlags & INTFLAG_ETHER) {
458     ClearInterruptFlag(INTFLAG_ETHER);
459     EtherInterrupt();
460     }
461     if (InterruptFlags & INTFLAG_AUDIO) {
462     ClearInterruptFlag(INTFLAG_AUDIO);
463     AudioInterrupt();
464     }
465     break;
466    
467     case M68K_EMUL_OP_PUT_SCRAP: { // PutScrap() patch
468     void *scrap = Mac2HostAddr(ReadMacInt32(r->a[7] + 4));
469     uint32 type = ReadMacInt32(r->a[7] + 8);
470     int32 length = ReadMacInt32(r->a[7] + 12);
471     PutScrap(type, scrap, length);
472     break;
473     }
474    
475     case M68K_EMUL_OP_CHECKLOAD: { // vCheckLoad() patch (resource loader)
476     uint32 type = r->d[1];
477     int16 id = ReadMacInt16(r->a[2]);
478     if (r->a[0] == 0)
479     break;
480     uint32 adr = ReadMacInt32(r->a[0]);
481     if (adr == 0)
482     break;
483     uint8 *p = Mac2HostAddr(adr);
484     uint32 size = ReadMacInt32(adr - 8) & 0xffffff;
485     CheckLoad(type, id, p, size);
486     break;
487     }
488    
489     case M68K_EMUL_OP_AUDIO: // Audio component dispatch function
490     r->d[0] = AudioDispatch(r->a[3], r->a[4]);
491     break;
492    
493     default:
494     printf("FATAL: EMUL_OP called with bogus opcode %08x\n", opcode);
495     printf("d0 %08lx d1 %08lx d2 %08lx d3 %08lx\n"
496     "d4 %08lx d5 %08lx d6 %08lx d7 %08lx\n"
497     "a0 %08lx a1 %08lx a2 %08lx a3 %08lx\n"
498     "a4 %08lx a5 %08lx a6 %08lx a7 %08lx\n"
499     "sr %04x\n",
500     r->d[0], r->d[1], r->d[2], r->d[3], r->d[4], r->d[5], r->d[6], r->d[7],
501     r->a[0], r->a[1], r->a[2], r->a[3], r->a[4], r->a[5], r->a[6], r->a[7],
502     r->sr);
503     QuitEmulator();
504     break;
505     }
506     }