ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/rom_patches.cpp
Revision: 1.1.1.1 (vendor branch)
Committed: 1999-10-03T14:16:25Z (24 years, 7 months ago) by cebix
Branch: cebix
CVS Tags: start
Changes since 1.1: +0 -0 lines
Log Message:
Imported sources

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * rom_patches.cpp - 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    
23     #include "sysdeps.h"
24     #include "cpu_emulation.h"
25     #include "main.h"
26     #include "emul_op.h"
27     #include "macos_util.h"
28     #include "slot_rom.h"
29     #include "sony.h"
30     #include "disk.h"
31     #include "cdrom.h"
32     #include "video.h"
33     #include "prefs.h"
34     #include "rom_patches.h"
35    
36     #define DEBUG 0
37     #include "debug.h"
38    
39    
40     // Breakpoint
41     //#define M68K_BREAKPOINT 0x2310 // CritError
42     //#define M68K_BREAKPOINT 0x1d10 // BootMe
43    
44     // Global variables
45     uint32 UniversalInfo; // ROM offset of UniversalInfo
46     uint32 PutScrapPatch; // Mac address of PutScrap() patch
47    
48     static uint32 sony_offset; // ROM offset of .Sony driver
49     static uint32 serd_offset; // ROM offset of SERD resource (serial drivers)
50     static uint32 microseconds_offset; // ROM offset of Microseconds() replacement routine
51     static uint32 memory_dispatch_offset; // ROM offset of MemoryDispatch() replacement routine
52    
53     // Prototypes
54     uint16 ROMVersion;
55    
56    
57     /*
58     * Search ROM for byte string, return ROM offset (or 0)
59     */
60    
61     static uint32 find_rom_data(uint32 start, uint32 end, const uint8 *data, uint32 data_len)
62     {
63     uint32 ofs = start;
64     while (ofs < end) {
65     if (!memcmp((void *)(ROMBaseHost + ofs), data, data_len))
66     return ofs;
67     ofs++;
68     }
69     return 0;
70     }
71    
72    
73     /*
74     * Search ROM resource by type/ID, return ROM offset of resource data
75     */
76    
77     static uint32 rsrc_ptr = 0;
78    
79     static uint32 find_rom_resource(uint32 s_type, int16 s_id, bool cont = false)
80     {
81     uint32 lp = ROMBaseMac + ReadMacInt32(ROMBaseMac + 0x1a);
82     uint32 x = ReadMacInt32(lp);
83    
84     if (!cont)
85     rsrc_ptr = x;
86    
87     for (;;) {
88     lp = ROMBaseMac + rsrc_ptr;
89     uint32 data = ReadMacInt32(lp + 12);
90     uint32 type = ReadMacInt32(lp + 16);
91     int16 id = ReadMacInt16(lp + 20);
92    
93     if (type == s_type && id == s_id)
94     return data;
95    
96     rsrc_ptr = ReadMacInt32(lp + 8);
97     if (!rsrc_ptr)
98     break;
99     }
100     return 0;
101     }
102    
103    
104     /*
105     * Search offset of A-Trap routine in ROM
106     */
107    
108     static uint32 find_rom_trap(uint16 trap)
109     {
110     uint8 *bp = (uint8 *)(ROMBaseHost + ReadMacInt32(ROMBaseMac + 0x22));
111     uint16 rom_trap = 0xa800;
112     uint32 ofs = 0;
113    
114     again:
115     for (int i=0; i<0x400; i++) {
116     bool unimplemented = false;
117     uint8 b = *bp++;
118     if (b == 0x80) // Unimplemented trap
119     unimplemented = true;
120     else if (b == 0xff) { // Absolute address
121     ofs = (bp[0] << 24) | (bp[1] << 16) | (bp[2] << 8) | bp[3];
122     bp += 4;
123     } else if (b & 0x80) { // 1 byte offset
124     int16 add = (b & 0x7f) << 1;
125     if (!add)
126     return 0;
127     ofs += add;
128     } else { // 2 byte offset
129     int16 add = ((b << 8) | *bp++) << 1;
130     if (!add)
131     return 0;
132     ofs += add;
133     }
134     if (rom_trap == trap)
135     return unimplemented ? 0 : ofs;
136     rom_trap++;
137     }
138     rom_trap = 0xa000;
139     goto again;
140     }
141    
142    
143     /*
144     * Driver stubs
145     */
146    
147     static const uint8 sony_driver[] = { // Replacement for .Sony driver
148     // Driver header
149     SonyDriverFlags >> 8, SonyDriverFlags & 0xff, SonyDriverDelay >> 8, SonyDriverDelay & 0xff, 0x00, 0x00, 0x00, 0x00,
150     0x00, 0x18, // Open() offset
151     0x00, 0x1c, // Prime() offset
152     0x00, 0x20, // Control() offset
153     0x00, 0x2c, // Status() offset
154     0x00, 0x52, // Close() offset
155     0x05, 0x2e, 0x53, 0x6f, 0x6e, 0x79, // ".Sony"
156    
157     // Open()
158     M68K_EMUL_OP_SONY_OPEN >> 8, M68K_EMUL_OP_SONY_OPEN & 0xff,
159     0x4e, 0x75, // rts
160    
161     // Prime()
162     M68K_EMUL_OP_SONY_PRIME >> 8, M68K_EMUL_OP_SONY_PRIME & 0xff,
163     0x60, 0x0e, // bra IOReturn
164    
165     // Control()
166     M68K_EMUL_OP_SONY_CONTROL >> 8, M68K_EMUL_OP_SONY_CONTROL & 0xff,
167     0x0c, 0x68, 0x00, 0x01, 0x00, 0x1a, // cmp.w #1,$1a(a0)
168     0x66, 0x04, // bne IOReturn
169     0x4e, 0x75, // rts
170    
171     // Status()
172     M68K_EMUL_OP_SONY_STATUS >> 8, M68K_EMUL_OP_SONY_STATUS & 0xff,
173    
174     // IOReturn
175     0x32, 0x28, 0x00, 0x06, // move.w 6(a0),d1
176     0x08, 0x01, 0x00, 0x09, // btst #9,d1
177     0x67, 0x0c, // beq 1
178     0x4a, 0x40, // tst.w d0
179     0x6f, 0x02, // ble 2
180     0x42, 0x40, // clr.w d0
181     0x31, 0x40, 0x00, 0x10, //2 move.w d0,$10(a0)
182     0x4e, 0x75, // rts
183     0x4a, 0x40, //1 tst.w d0
184     0x6f, 0x04, // ble 3
185     0x42, 0x40, // clr.w d0
186     0x4e, 0x75, // rts
187     0x2f, 0x38, 0x08, 0xfc, //3 move.l $8fc,-(sp)
188     0x4e, 0x75, // rts
189    
190     // Close()
191     0x70, 0xe8, // moveq #-24,d0
192     0x4e, 0x75 // rts
193     };
194    
195     static const uint8 disk_driver[] = { // Generic disk driver
196     // Driver header
197     DiskDriverFlags >> 8, DiskDriverFlags & 0xff, DiskDriverDelay >> 8, DiskDriverDelay & 0xff, 0x00, 0x00, 0x00, 0x00,
198     0x00, 0x18, // Open() offset
199     0x00, 0x1c, // Prime() offset
200     0x00, 0x20, // Control() offset
201     0x00, 0x2c, // Status() offset
202     0x00, 0x52, // Close() offset
203     0x05, 0x2e, 0x44, 0x69, 0x73, 0x6b, // ".Disk"
204    
205     // Open()
206     M68K_EMUL_OP_DISK_OPEN >> 8, M68K_EMUL_OP_DISK_OPEN & 0xff,
207     0x4e, 0x75, // rts
208    
209     // Prime()
210     M68K_EMUL_OP_DISK_PRIME >> 8, M68K_EMUL_OP_DISK_PRIME & 0xff,
211     0x60, 0x0e, // bra IOReturn
212    
213     // Control()
214     M68K_EMUL_OP_DISK_CONTROL >> 8, M68K_EMUL_OP_DISK_CONTROL & 0xff,
215     0x0c, 0x68, 0x00, 0x01, 0x00, 0x1a, // cmp.w #1,$1a(a0)
216     0x66, 0x04, // bne IOReturn
217     0x4e, 0x75, // rts
218    
219     // Status()
220     M68K_EMUL_OP_DISK_STATUS >> 8, M68K_EMUL_OP_DISK_STATUS & 0xff,
221    
222     // IOReturn
223     0x32, 0x28, 0x00, 0x06, // move.w 6(a0),d1
224     0x08, 0x01, 0x00, 0x09, // btst #9,d1
225     0x67, 0x0c, // beq 1
226     0x4a, 0x40, // tst.w d0
227     0x6f, 0x02, // ble 2
228     0x42, 0x40, // clr.w d0
229     0x31, 0x40, 0x00, 0x10, //2 move.w d0,$10(a0)
230     0x4e, 0x75, // rts
231     0x4a, 0x40, //1 tst.w d0
232     0x6f, 0x04, // ble 3
233     0x42, 0x40, // clr.w d0
234     0x4e, 0x75, // rts
235     0x2f, 0x38, 0x08, 0xfc, //3 move.l $8fc,-(sp)
236     0x4e, 0x75, // rts
237    
238     // Close()
239     0x70, 0xe8, // moveq #-24,d0
240     0x4e, 0x75 // rts
241     };
242    
243     static const uint8 cdrom_driver[] = { // CD-ROM driver
244     // Driver header
245     CDROMDriverFlags >> 8, CDROMDriverFlags & 0xff, CDROMDriverDelay >> 8, CDROMDriverDelay & 0xff, 0x00, 0x00, 0x00, 0x00,
246     0x00, 0x1c, // Open() offset
247     0x00, 0x20, // Prime() offset
248     0x00, 0x24, // Control() offset
249     0x00, 0x30, // Status() offset
250     0x00, 0x56, // Close() offset
251     0x08, 0x2e, 0x41, 0x70, 0x70, 0x6c, 0x65, 0x43, 0x44, 0x00, // ".AppleCD"
252    
253     // Open()
254     M68K_EMUL_OP_CDROM_OPEN >> 8, M68K_EMUL_OP_CDROM_OPEN & 0xff,
255     0x4e, 0x75, // rts
256    
257     // Prime()
258     M68K_EMUL_OP_CDROM_PRIME >> 8, M68K_EMUL_OP_CDROM_PRIME & 0xff,
259     0x60, 0x0e, // bra IOReturn
260    
261     // Control()
262     M68K_EMUL_OP_CDROM_CONTROL >> 8, M68K_EMUL_OP_CDROM_CONTROL & 0xff,
263     0x0c, 0x68, 0x00, 0x01, 0x00, 0x1a, // cmp.w #1,$1a(a0)
264     0x66, 0x04, // bne IOReturn
265     0x4e, 0x75, // rts
266    
267     // Status()
268     M68K_EMUL_OP_CDROM_STATUS >> 8, M68K_EMUL_OP_CDROM_STATUS & 0xff,
269    
270     // IOReturn
271     0x32, 0x28, 0x00, 0x06, // move.w 6(a0),d1
272     0x08, 0x01, 0x00, 0x09, // btst #9,d1
273     0x67, 0x0c, // beq 1
274     0x4a, 0x40, // tst.w d0
275     0x6f, 0x02, // ble 2
276     0x42, 0x40, // clr.w d0
277     0x31, 0x40, 0x00, 0x10, //2 move.w d0,$10(a0)
278     0x4e, 0x75, // rts
279     0x4a, 0x40, //1 tst.w d0
280     0x6f, 0x04, // ble 3
281     0x42, 0x40, // clr.w d0
282     0x4e, 0x75, // rts
283     0x2f, 0x38, 0x08, 0xfc, //3 move.l $8fc,-(sp)
284     0x4e, 0x75, // rts
285    
286     // Close()
287     0x70, 0xe8, // moveq #-24,d0
288     0x4e, 0x75 // rts
289     };
290    
291     static const uint8 ain_driver[] = { // .AIn driver header
292     // Driver header
293     0x4d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
294     0x00, 0x18, // Open() offset
295     0x00, 0x1e, // Prime() offset
296     0x00, 0x24, // Control() offset
297     0x00, 0x32, // Status() offset
298     0x00, 0x38, // Close() offset
299     0x04, 0x2e, 0x41, 0x49, 0x6e, 0x09, // ".AIn",9
300    
301     // Open()
302     0x70, 0x00, // moveq #0,d0
303     M68K_EMUL_OP_SERIAL_OPEN >> 8, M68K_EMUL_OP_SERIAL_OPEN & 0xff,
304     0x4e, 0x75, // rts
305    
306     // Prime()
307     0x70, 0x00, // moveq #0,d0
308     M68K_EMUL_OP_SERIAL_PRIME >> 8, M68K_EMUL_OP_SERIAL_PRIME & 0xff,
309     0x60, 0x1a, // bra IOReturn
310    
311     // Control()
312     0x70, 0x00, // moveq #0,d0
313     M68K_EMUL_OP_SERIAL_CONTROL >> 8, M68K_EMUL_OP_SERIAL_CONTROL & 0xff,
314     0x0c, 0x68, 0x00, 0x01, 0x00, 0x1a, // cmp.w #1,$1a(a0)
315     0x66, 0x0e, // bne IOReturn
316     0x4e, 0x75, // rts
317    
318     // Status()
319     0x70, 0x00, // moveq #0,d0
320     M68K_EMUL_OP_SERIAL_STATUS >> 8, M68K_EMUL_OP_SERIAL_STATUS & 0xff,
321     0x60, 0x06, // bra IOReturn
322    
323     // Close()
324     0x70, 0x00, // moveq #0,d0
325     M68K_EMUL_OP_SERIAL_CLOSE >> 8, M68K_EMUL_OP_SERIAL_CLOSE & 0xff,
326     0x4e, 0x75, // rts
327    
328     // IOReturn
329     0x32, 0x28, 0x00, 0x06, // move.w 6(a0),d1
330     0x08, 0x01, 0x00, 0x09, // btst #9,d1
331     0x67, 0x0c, // beq 1
332     0x4a, 0x40, // tst.w d0
333     0x6f, 0x02, // ble 2
334     0x42, 0x40, // clr.w d0
335     0x31, 0x40, 0x00, 0x10, //2 move.w d0,$10(a0)
336     0x4e, 0x75, // rts
337     0x4a, 0x40, //1 tst.w d0
338     0x6f, 0x04, // ble 3
339     0x42, 0x40, // clr.w d0
340     0x4e, 0x75, // rts
341     0x2f, 0x38, 0x08, 0xfc, //3 move.l $8fc,-(a7)
342     0x4e, 0x75, // rts
343     };
344    
345     static const uint8 aout_driver[] = { // .AOut driver header
346     // Driver header
347     0x4e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
348     0x00, 0x1a, // Open() offset
349     0x00, 0x20, // Prime() offset
350     0x00, 0x26, // Control() offset
351     0x00, 0x34, // Status() offset
352     0x00, 0x3a, // Close() offset
353     0x05, 0x2e, 0x41, 0x4f, 0x75, 0x74, 0x09, 0x00, // ".AOut",9
354    
355     // Open()
356     0x70, 0x01, // moveq #1,d0
357     M68K_EMUL_OP_SERIAL_OPEN >> 8, M68K_EMUL_OP_SERIAL_OPEN & 0xff,
358     0x4e, 0x75, // rts
359    
360     // Prime()
361     0x70, 0x01, // moveq #1,d0
362     M68K_EMUL_OP_SERIAL_PRIME >> 8, M68K_EMUL_OP_SERIAL_PRIME & 0xff,
363     0x60, 0x1a, // bra IOReturn
364    
365     // Control()
366     0x70, 0x01, // moveq #1,d0
367     M68K_EMUL_OP_SERIAL_CONTROL >> 8, M68K_EMUL_OP_SERIAL_CONTROL & 0xff,
368     0x0c, 0x68, 0x00, 0x01, 0x00, 0x1a, // cmp.w #1,$1a(a0)
369     0x66, 0x0e, // bne IOReturn
370     0x4e, 0x75, // rts
371    
372     // Status()
373     0x70, 0x01, // moveq #1,d0
374     M68K_EMUL_OP_SERIAL_STATUS >> 8, M68K_EMUL_OP_SERIAL_STATUS & 0xff,
375     0x60, 0x06, // bra IOReturn
376    
377     // Close()
378     0x70, 0x01, // moveq #1,d0
379     M68K_EMUL_OP_SERIAL_CLOSE >> 8, M68K_EMUL_OP_SERIAL_CLOSE & 0xff,
380     0x4e, 0x75, // rts
381    
382     // IOReturn
383     0x32, 0x28, 0x00, 0x06, // move.w 6(a0),d1
384     0x08, 0x01, 0x00, 0x09, // btst #9,d1
385     0x67, 0x0c, // beq 1
386     0x4a, 0x40, // tst.w d0
387     0x6f, 0x02, // ble 2
388     0x42, 0x40, // clr.w d0
389     0x31, 0x40, 0x00, 0x10, //2 move.w d0,$10(a0)
390     0x4e, 0x75, // rts
391     0x4a, 0x40, //1 tst.w d0
392     0x6f, 0x04, // ble 3
393     0x42, 0x40, // clr.w d0
394     0x4e, 0x75, // rts
395     0x2f, 0x38, 0x08, 0xfc, //3 move.l $8fc,-(a7)
396     0x4e, 0x75, // rts
397     };
398    
399     static const uint8 bin_driver[] = { // .BIn driver header
400     // Driver header
401     0x4d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
402     0x00, 0x18, // Open() offset
403     0x00, 0x1e, // Prime() offset
404     0x00, 0x24, // Control() offset
405     0x00, 0x32, // Status() offset
406     0x00, 0x38, // Close() offset
407     0x04, 0x2e, 0x42, 0x49, 0x6e, 0x09, // ".BIn",9
408    
409     // Open()
410     0x70, 0x02, // moveq #2,d0
411     M68K_EMUL_OP_SERIAL_OPEN >> 8, M68K_EMUL_OP_SERIAL_OPEN & 0xff,
412     0x4e, 0x75, // rts
413    
414     // Prime()
415     0x70, 0x02, // moveq #2,d0
416     M68K_EMUL_OP_SERIAL_PRIME >> 8, M68K_EMUL_OP_SERIAL_PRIME & 0xff,
417     0x60, 0x1a, // bra IOReturn
418    
419     // Control()
420     0x70, 0x02, // moveq #2,d0
421     M68K_EMUL_OP_SERIAL_CONTROL >> 8, M68K_EMUL_OP_SERIAL_CONTROL & 0xff,
422     0x0c, 0x68, 0x00, 0x01, 0x00, 0x1a, // cmp.w #1,$1a(a0)
423     0x66, 0x0e, // bne IOReturn
424     0x4e, 0x75, // rts
425    
426     // Status()
427     0x70, 0x02, // moveq #2,d0
428     M68K_EMUL_OP_SERIAL_STATUS >> 8, M68K_EMUL_OP_SERIAL_STATUS & 0xff,
429     0x60, 0x06, // bra IOReturn
430    
431     // Close()
432     0x70, 0x02, // moveq #2,d0
433     M68K_EMUL_OP_SERIAL_CLOSE >> 8, M68K_EMUL_OP_SERIAL_CLOSE & 0xff,
434     0x4e, 0x75, // rts
435    
436     // IOReturn
437     0x32, 0x28, 0x00, 0x06, // move.w 6(a0),d1
438     0x08, 0x01, 0x00, 0x09, // btst #9,d1
439     0x67, 0x0c, // beq 1
440     0x4a, 0x40, // tst.w d0
441     0x6f, 0x02, // ble 2
442     0x42, 0x40, // clr.w d0
443     0x31, 0x40, 0x00, 0x10, //2 move.w d0,$10(a0)
444     0x4e, 0x75, // rts
445     0x4a, 0x40, //1 tst.w d0
446     0x6f, 0x04, // ble 3
447     0x42, 0x40, // clr.w d0
448     0x4e, 0x75, // rts
449     0x2f, 0x38, 0x08, 0xfc, //3 move.l $8fc,-(a7)
450     0x4e, 0x75, // rts
451     };
452    
453     static const uint8 bout_driver[] = { // .BOut driver header
454     // Driver header
455     0x4e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
456     0x00, 0x1a, // Open() offset
457     0x00, 0x20, // Prime() offset
458     0x00, 0x26, // Control() offset
459     0x00, 0x34, // Status() offset
460     0x00, 0x3a, // Close() offset
461     0x05, 0x2e, 0x42, 0x4f, 0x75, 0x74, 0x09, 0x00, // ".BOut",9
462    
463     // Open()
464     0x70, 0x03, // moveq #3,d0
465     M68K_EMUL_OP_SERIAL_OPEN >> 8, M68K_EMUL_OP_SERIAL_OPEN & 0xff,
466     0x4e, 0x75, // rts
467    
468     // Prime()
469     0x70, 0x03, // moveq #3,d0
470     M68K_EMUL_OP_SERIAL_PRIME >> 8, M68K_EMUL_OP_SERIAL_PRIME & 0xff,
471     0x60, 0x1a, // bra IOReturn
472    
473     // Control()
474     0x70, 0x03, // moveq #3,d0
475     M68K_EMUL_OP_SERIAL_CONTROL >> 8, M68K_EMUL_OP_SERIAL_CONTROL & 0xff,
476     0x0c, 0x68, 0x00, 0x01, 0x00, 0x1a, // cmp.w #1,$1a(a0)
477     0x66, 0x0e, // bne IOReturn
478     0x4e, 0x75, // rts
479    
480     // Status()
481     0x70, 0x03, // moveq #3,d0
482     M68K_EMUL_OP_SERIAL_STATUS >> 8, M68K_EMUL_OP_SERIAL_STATUS & 0xff,
483     0x60, 0x06, // bra IOReturn
484    
485     // Close()
486     0x70, 0x03, // moveq #3,d0
487     M68K_EMUL_OP_SERIAL_CLOSE >> 8, M68K_EMUL_OP_SERIAL_CLOSE & 0xff,
488     0x4e, 0x75, // rts
489    
490     // IOReturn
491     0x32, 0x28, 0x00, 0x06, // move.w 6(a0),d1
492     0x08, 0x01, 0x00, 0x09, // btst #9,d1
493     0x67, 0x0c, // beq 1
494     0x4a, 0x40, // tst.w d0
495     0x6f, 0x02, // ble 2
496     0x42, 0x40, // clr.w d0
497     0x31, 0x40, 0x00, 0x10, //2 move.w d0,$10(a0)
498     0x4e, 0x75, // rts
499     0x4a, 0x40, //1 tst.w d0
500     0x6f, 0x04, // ble 3
501     0x42, 0x40, // clr.w d0
502     0x4e, 0x75, // rts
503     0x2f, 0x38, 0x08, 0xfc, //3 move.l $8fc,-(a7)
504     0x4e, 0x75, // rts
505     };
506    
507    
508     /*
509     * ADBOp() patch
510     */
511    
512     static const uint8 adbop_patch[] = { // Call ADBOp() completion procedure
513     // The completion procedure may call ADBOp() again!
514     0x40, 0xe7, // move sr,-(sp)
515     0x00, 0x7c, 0x07, 0x00, // ori #$0700,sr
516     M68K_EMUL_OP_ADBOP >> 8, M68K_EMUL_OP_ADBOP & 0xff,
517     0x48, 0xe7, 0x70, 0xf0, // movem.l d1-d3/a0-a3,-(sp)
518     0x26, 0x48, // move.l a0,a3
519     0x4a, 0xab, 0x00, 0x04, // tst.l 4(a3)
520     0x67, 0x00, 0x00, 0x18, // beq 1
521     0x20, 0x53, // move.l (a3),a0
522     0x22, 0x6b, 0x00, 0x04, // move.l 4(a3),a1
523     0x24, 0x6b, 0x00, 0x08, // move.l 8(a3),a2
524     0x26, 0x78, 0x0c, 0xf8, // move.l $cf8,a3
525     0x4e, 0x91, // jsr (a1)
526     0x70, 0x00, // moveq #0,d0
527     0x60, 0x00, 0x00, 0x04, // bra 2
528     0x70, 0xff, //1 moveq #-1,d0
529     0x4c, 0xdf, 0x0f, 0x0e, //2 movem.l (sp)+,d1-d3/a0-a3
530     0x46, 0xdf, // move (sp)+,sr
531     0x4e, 0x75 // rts
532     };
533    
534    
535     /*
536     * Install .Sony, disk and CD-ROM drivers
537     */
538    
539     void InstallDrivers(uint32 pb)
540     {
541     D(bug("InstallDrivers\n"));
542     M68kRegisters r;
543    
544     // Install Microseconds() replacement routine
545     r.a[0] = ROMBaseMac + microseconds_offset;
546     r.d[0] = 0xa093;
547     Execute68kTrap(0xa247, &r); // SetOSTrapAddress()
548    
549     // Install disk driver
550     r.a[0] = ROMBaseMac + sony_offset + 0x100;
551     r.d[0] = (uint32)DiskRefNum;
552     Execute68kTrap(0xa43d, &r); // DrvrInstallRsrvMem()
553     r.a[0] = ReadMacInt32(ReadMacInt32(0x11c) + ~DiskRefNum * 4); // Get driver handle from Unit Table
554     Execute68kTrap(0xa029, &r); // HLock()
555     uint32 dce = ReadMacInt32(r.a[0]);
556     WriteMacInt32(dce + dCtlDriver, ROMBaseMac + sony_offset + 0x100);
557     WriteMacInt16(dce + dCtlFlags, DiskDriverFlags);
558     WriteMacInt16(dce + dCtlDelay, DiskDriverDelay);
559    
560     // Open disk driver
561     WriteMacInt32(pb + ioNamePtr, ROMBaseMac + sony_offset + 0x112);
562     r.a[0] = pb;
563     Execute68kTrap(0xa000, &r); // Open()
564    
565     // Install CD-ROM driver unless nocdrom option given
566     if (!PrefsFindBool("nocdrom")) {
567    
568     // Install CD-ROM driver
569     r.a[0] = ROMBaseMac + sony_offset + 0x200;
570     r.d[0] = (uint32)CDROMRefNum;
571     Execute68kTrap(0xa43d, &r); // DrvrInstallRsrvMem()
572     r.a[0] = ReadMacInt32(ReadMacInt32(0x11c) + ~CDROMRefNum * 4); // Get driver handle from Unit Table
573     Execute68kTrap(0xa029, &r); // HLock()
574     dce = ReadMacInt32(r.a[0]);
575     WriteMacInt32(dce + dCtlDriver, ROMBaseMac + sony_offset + 0x200);
576     WriteMacInt16(dce + dCtlFlags, CDROMDriverFlags);
577     WriteMacInt16(dce + dCtlDelay, CDROMDriverDelay);
578    
579     // Open CD-ROM driver
580     WriteMacInt32(pb + ioNamePtr, ROMBaseMac + sony_offset + 0x212);
581     r.a[0] = pb;
582     Execute68kTrap(0xa000, &r); // Open()
583     }
584     }
585    
586    
587     /*
588     * Install serial drivers
589     */
590    
591     void InstallSERD(void)
592     {
593     D(bug("InstallSERD\n"));
594    
595     // All drivers are inside the SERD resource
596     M68kRegisters r;
597    
598     // Install .AIn driver
599     r.d[0] = (uint32)-6;
600     r.a[0] = ROMBaseMac + serd_offset + 0x100;
601     Execute68kTrap(0xa53d, &r); // DrvrInstallRsrvMem()
602     Execute68kTrap(0xa029, &r); // HLock()
603     uint32 drvr_ptr = ReadMacInt32(r.a[0]);
604     WriteMacInt32(drvr_ptr + dCtlDriver, ROMBaseMac + serd_offset + 0x100); // Pointer to driver header
605     WriteMacInt16(drvr_ptr + dCtlFlags, (ain_driver[0] << 8) + ain_driver[1]); // Driver flags
606     WriteMacInt16(drvr_ptr + dCtlQHdr + qFlags, 9); // Version number
607    
608     // Install .AOut driver
609     r.d[0] = (uint32)-7;
610     r.a[0] = ROMBaseMac + serd_offset + 0x200;
611     Execute68kTrap(0xa53d, &r); // DrvrInstallRsrvMem()
612     Execute68kTrap(0xa029, &r); // HLock()
613     drvr_ptr = ReadMacInt32(r.a[0]);
614     WriteMacInt32(drvr_ptr + dCtlDriver, ROMBaseMac + serd_offset + 0x200); // Pointer to driver header
615     WriteMacInt16(drvr_ptr + dCtlFlags, (aout_driver[0] << 8) + aout_driver[1]); // Driver flags
616     WriteMacInt16(drvr_ptr + dCtlQHdr + qFlags, 9); // Version number
617    
618     // Install .BIn driver
619     r.d[0] = (uint32)-8;
620     r.a[0] = ROMBaseMac + serd_offset + 0x300;
621     Execute68kTrap(0xa53d, &r); // DrvrInstallRsrvMem()
622     Execute68kTrap(0xa029, &r); // HLock()
623     drvr_ptr = ReadMacInt32(r.a[0]);
624     WriteMacInt32(drvr_ptr + dCtlDriver, ROMBaseMac + serd_offset + 0x300); // Pointer to driver header
625     WriteMacInt16(drvr_ptr + dCtlFlags, (bin_driver[0] << 8) + bin_driver[1]); // Driver flags
626     WriteMacInt16(drvr_ptr + dCtlQHdr + qFlags, 9); // Version number
627    
628     // Install .BOut driver
629     r.d[0] = (uint32)-9;
630     r.a[0] = ROMBaseMac + serd_offset + 0x400;
631     Execute68kTrap(0xa53d, &r); // DrvrInstallRsrvMem()
632     Execute68kTrap(0xa029, &r); // HLock()
633     drvr_ptr = ReadMacInt32(r.a[0]);
634     WriteMacInt32(drvr_ptr + dCtlDriver, ROMBaseMac + serd_offset + 0x400); // Pointer to driver header
635     WriteMacInt16(drvr_ptr + dCtlFlags, (bout_driver[0] << 8) + bout_driver[1]); // Driver flags
636     WriteMacInt16(drvr_ptr + dCtlQHdr + qFlags, 9); // Version number
637     }
638    
639    
640     /*
641     * Install patches after MacOS startup
642     */
643    
644     void PatchAfterStartup(void)
645     {
646     // Install MemoryDispatch() replacement routine
647     M68kRegisters r;
648     r.a[0] = ROMBaseMac + memory_dispatch_offset;
649     r.d[0] = 0xa05c;
650     Execute68kTrap(0xa247, &r); // SetOSTrapAddress()
651     }
652    
653    
654     /*
655     * Check ROM version, returns false if ROM version is not supported
656     */
657    
658     bool CheckROM(void)
659     {
660     // Read version
661     ROMVersion = ntohs(*(uint16 *)(ROMBaseHost + 8));
662    
663     #if REAL_ADDRESSING
664     // Real addressing mode requires a 32-bit clean ROM
665     return ROMVersion == ROM_VERSION_32;
666     #else
667     // Virtual addressing mode works with 32-bit clean Mac II ROMs and Classic ROMs (experimental)
668     return (ROMVersion == ROM_VERSION_CLASSIC) || (ROMVersion == ROM_VERSION_32);
669     #endif
670     }
671    
672    
673     /*
674     * Install ROM patches, returns false if ROM version is not supported
675     */
676    
677     // ROM patches for Mac Classic/SE ROMs (version $0276)
678     static bool patch_rom_classic(void)
679     {
680     uint16 *wp;
681     uint32 base;
682    
683     // Don't jump into debugger (VIA line)
684     wp = (uint16 *)(ROMBaseHost + 0x1c40);
685     *wp = htons(0x601e);
686    
687     // Don't complain about incorrect ROM checksum
688     wp = (uint16 *)(ROMBaseHost + 0x1c6c);
689     *wp = htons(0x7c00);
690    
691     // Don't initialize IWM
692     wp = (uint16 *)(ROMBaseHost + 0x50);
693     *wp++ = htons(M68K_NOP);
694     *wp = htons(M68K_NOP);
695    
696     // Skip startup sound
697     wp = (uint16 *)(ROMBaseHost + 0x6a);
698     *wp++ = htons(M68K_NOP);
699     *wp = htons(M68K_NOP);
700    
701     // Don't loop in ADB init
702     wp = (uint16 *)(ROMBaseHost + 0x3364);
703     *wp = htons(M68K_NOP);
704    
705     // Patch ClkNoMem
706     wp = (uint16 *)(ROMBaseHost + 0xa2c0);
707     *wp++ = htons(M68K_EMUL_OP_CLKNOMEM);
708     *wp = htons(0x4ed5); // jmp (a5)
709    
710     // Skip main memory test (not that it wouldn't pass, but it's faster that way)
711     wp = (uint16 *)(ROMBaseHost + 0x11e);
712     *wp++ = htons(M68K_NOP);
713     *wp = htons(M68K_NOP);
714    
715     // Install our own drivers
716     wp = (uint16 *)(ROMBaseHost + 0x3f82a);
717     *wp++ = htons(M68K_EMUL_OP_INSTALL_DRIVERS);
718     *wp++ = htons(M68K_NOP);
719     *wp++ = htons(M68K_NOP);
720     *wp = htons(M68K_NOP);
721    
722     #if 1
723     // Don't look for SCSI devices
724     wp = (uint16 *)(ROMBaseHost + 0xd5a);
725     *wp = htons(0x601e);
726     #endif
727    
728     // Replace .Sony driver
729     sony_offset = 0x34680;
730     D(bug("sony %08lx\n", sony_offset));
731     memcpy(ROMBaseHost + sony_offset, sony_driver, sizeof(sony_driver));
732    
733     // Install .Disk and .AppleCD drivers
734     memcpy(ROMBaseHost + sony_offset + 0x100, disk_driver, sizeof(disk_driver));
735     memcpy(ROMBaseHost + sony_offset + 0x200, cdrom_driver, sizeof(cdrom_driver));
736    
737     // Copy icons to ROM
738     SonyDiskIconAddr = ROMBaseMac + sony_offset + 0x400;
739     memcpy(ROMBaseHost + sony_offset + 0x400, SonyDiskIcon, sizeof(SonyDiskIcon));
740     SonyDriveIconAddr = ROMBaseMac + sony_offset + 0x600;
741     memcpy(ROMBaseHost + sony_offset + 0x600, SonyDriveIcon, sizeof(SonyDriveIcon));
742     DiskIconAddr = ROMBaseMac + sony_offset + 0x800;
743     memcpy(ROMBaseHost + sony_offset + 0x800, DiskIcon, sizeof(DiskIcon));
744     CDROMIconAddr = ROMBaseMac + sony_offset + 0xa00;
745     memcpy(ROMBaseHost + sony_offset + 0xa00, CDROMIcon, sizeof(CDROMIcon));
746    
747     // Install SERD patch and serial drivers
748     serd_offset = 0x31bae;
749     D(bug("serd %08lx\n", serd_offset));
750     wp = (uint16 *)(ROMBaseHost + serd_offset + 12);
751     *wp++ = htons(M68K_EMUL_OP_SERD);
752     *wp = htons(M68K_RTS);
753     memcpy(ROMBaseHost + serd_offset + 0x100, ain_driver, sizeof(ain_driver));
754     memcpy(ROMBaseHost + serd_offset + 0x200, aout_driver, sizeof(aout_driver));
755     memcpy(ROMBaseHost + serd_offset + 0x300, bin_driver, sizeof(bin_driver));
756     memcpy(ROMBaseHost + serd_offset + 0x400, bout_driver, sizeof(bout_driver));
757    
758     // Replace ADBOp()
759     memcpy(ROMBaseHost + 0x3880, adbop_patch, sizeof(adbop_patch));
760    
761     // Replace Time Manager
762     wp = (uint16 *)(ROMBaseHost + 0x1a95c);
763     *wp++ = htons(M68K_EMUL_OP_INSTIME);
764     *wp = htons(M68K_RTS);
765     wp = (uint16 *)(ROMBaseHost + 0x1a96a);
766     *wp++ = htons(0x40e7); // move sr,-(sp)
767     *wp++ = htons(0x007c); // ori #$0700,sr
768     *wp++ = htons(0x0700);
769     *wp++ = htons(M68K_EMUL_OP_RMVTIME);
770     *wp++ = htons(0x46df); // move (sp)+,sr
771     *wp = htons(M68K_RTS);
772     wp = (uint16 *)(ROMBaseHost + 0x1a984);
773     *wp++ = htons(0x40e7); // move sr,-(sp)
774     *wp++ = htons(0x007c); // ori #$0700,sr
775     *wp++ = htons(0x0700);
776     *wp++ = htons(M68K_EMUL_OP_PRIMETIME);
777     *wp++ = htons(0x46df); // move (sp)+,sr
778     *wp++ = htons(M68K_RTS);
779     microseconds_offset = (uint8 *)wp - ROMBaseHost;
780     *wp++ = htons(M68K_EMUL_OP_MICROSECONDS);
781     *wp = htons(M68K_RTS);
782    
783     // Replace SCSIDispatch()
784     wp = (uint16 *)(ROMBaseHost + 0x1a206);
785     *wp++ = htons(M68K_EMUL_OP_SCSI_DISPATCH);
786     *wp++ = htons(0x2e49); // move.l a1,a7
787     *wp = htons(M68K_JMP_A0);
788    
789     // Modify vCheckLoad() so we can patch resources
790     wp = (uint16 *)(ROMBaseHost + 0xe740);
791     *wp++ = htons(M68K_JMP);
792     *wp++ = htons((ROMBaseMac + sony_offset + 0x300) >> 16);
793     *wp = htons((ROMBaseMac + sony_offset + 0x300) & 0xffff);
794     wp = (uint16 *)(ROMBaseHost + sony_offset + 0x300);
795     *wp++ = htons(0x2f03); // move.l d3,-(sp) (save type)
796     *wp++ = htons(0x2078); // move.l $07f0,a0
797     *wp++ = htons(0x07f0);
798     *wp++ = htons(M68K_JSR_A0);
799     *wp++ = htons(0x221f); // move.l (sp)+,d1 (restore type)
800     *wp++ = htons(M68K_EMUL_OP_CHECKLOAD);
801     *wp = htons(M68K_RTS);
802    
803     // Install PutScrap() patch for clipboard data exchange (the patch is activated by EMUL_OP_INSTALL_DRIVERS)
804     PutScrapPatch = ROMBaseMac + sony_offset + 0xc00;
805     base = ROMBaseMac + 0x12794;
806     wp = (uint16 *)(ROMBaseHost + sony_offset + 0xc00);
807     *wp++ = htons(M68K_EMUL_OP_PUT_SCRAP);
808     *wp++ = htons(M68K_JMP);
809     *wp++ = htons(base >> 16);
810     *wp = htons(base & 0xffff);
811    
812     #if 0
813     // Boot from internal EDisk
814     wp = (uint16 *)(ROMBaseHost + 0x3f83c);
815     *wp = htons(M68K_NOP);
816     #endif
817    
818     // Patch VIA interrupt handler
819     wp = (uint16 *)(ROMBaseHost + 0x2b3a); // Level 1 handler
820     *wp++ = htons(0x5888); // addq.l #4,a0
821     *wp++ = htons(0x5888); // addq.l #4,a0
822     *wp++ = htons(M68K_NOP);
823     *wp++ = htons(M68K_NOP);
824     *wp++ = htons(M68K_NOP);
825     *wp++ = htons(M68K_NOP);
826     *wp++ = htons(M68K_NOP);
827     *wp++ = htons(M68K_NOP);
828     *wp = htons(M68K_NOP);
829    
830     wp = (uint16 *)(ROMBaseHost + 0x2be8); // 60Hz handler (handles everything)
831     *wp++ = htons(M68K_EMUL_OP_IRQ);
832     *wp++ = htons(0x4a80); // tst.l d0
833     *wp = htons(0x67f4); // beq 0x402be2
834     return true;
835     }
836    
837     // ROM patches for 32-bit clean Mac-II ROMs (version $067c)
838     static bool patch_rom_32(void)
839     {
840     uint32 *lp;
841     uint16 *wp;
842     uint8 *bp;
843     uint32 base;
844    
845     // Find UniversalInfo
846     static const uint8 universal_dat[] = {0xdc, 0x00, 0x05, 0x05, 0x3f, 0xff, 0x01, 0x00};
847     if ((base = find_rom_data(0x3400, 0x3c00, universal_dat, sizeof(universal_dat))) == 0) return false;
848     UniversalInfo = base - 0x10;
849     D(bug("universal %08lx\n", UniversalInfo));
850    
851     // Patch UniversalInfo (disable NuBus slots)
852     bp = ROMBaseHost + UniversalInfo + ReadMacInt32(ROMBaseMac + UniversalInfo + 12); // nuBusInfoPtr
853     bp[0] = 0x03;
854     for (int i=1; i<16; i++)
855     bp[i] = 0x08;
856    
857     // Set model ID from preferences
858     bp = ROMBaseHost + UniversalInfo + 18; // productKind
859     *bp = PrefsFindInt32("modelid");
860    
861     // Make FPU optional
862     if (FPUType == 0) {
863     bp = ROMBaseHost + UniversalInfo + 22; // defaultRSRCs
864     *bp = 4; // FPU optional
865     }
866    
867     // Install special reset opcode and jump (skip hardware detection and tests)
868     wp = (uint16 *)(ROMBaseHost + 0x8c);
869     *wp++ = htons(M68K_EMUL_OP_RESET);
870     *wp++ = htons(M68K_JMP);
871     *wp++ = htons((ROMBaseMac + 0xba) >> 16);
872     *wp = htons((ROMBaseMac + 0xba) & 0xffff);
873    
874     // Don't GetHardwareInfo
875     wp = (uint16 *)(ROMBaseHost + 0xc2);
876     *wp++ = htons(M68K_NOP);
877     *wp = htons(M68K_NOP);
878    
879     // Don't init VIAs
880     wp = (uint16 *)(ROMBaseHost + 0xc6);
881     *wp++ = htons(M68K_NOP);
882     *wp++ = htons(M68K_NOP);
883     *wp++ = htons(M68K_NOP);
884     *wp++ = htons(M68K_NOP);
885     *wp++ = htons(M68K_NOP);
886     *wp++ = htons(M68K_NOP);
887     *wp++ = htons(M68K_NOP);
888     *wp++ = htons(M68K_NOP);
889     *wp++ = htons(M68K_NOP);
890     *wp++ = htons(M68K_NOP);
891     *wp++ = htons(M68K_NOP);
892     *wp++ = htons(M68K_NOP);
893     *wp++ = htons(M68K_NOP);
894     *wp++ = htons(M68K_NOP);
895     *wp = htons(M68K_NOP);
896    
897     // Fake CPU type test
898     wp = (uint16 *)(ROMBaseHost + 0x7c0);
899     *wp++ = htons(0x7e00 + CPUType);
900     *wp = htons(M68K_RTS);
901    
902     // Don't clear end of BootGlobs upto end of RAM (address xxxx0000)
903     static const uint8 clear_globs_dat[] = {0x42, 0x9a, 0x36, 0x0a, 0x66, 0xfa};
904     base = find_rom_data(0xa00, 0xb00, clear_globs_dat, sizeof(clear_globs_dat));
905     D(bug("clear_globs %08lx\n", base));
906     if (base) { // ROM15/20/22/23/26/27/32
907     wp = (uint16 *)(ROMBaseHost + base + 2);
908     *wp++ = htons(M68K_NOP);
909     *wp = htons(M68K_NOP);
910     }
911    
912     // Patch InitMMU (no MMU present, don't choke on unknown CPU types)
913     if (ROMSize <= 0x80000) {
914     static const uint8 init_mmu_dat[] = {0x0c, 0x47, 0x00, 0x03, 0x62, 0x00, 0xfe};
915     if ((base = find_rom_data(0x4000, 0x50000, init_mmu_dat, sizeof(init_mmu_dat))) == 0) return false;
916     } else {
917     static const uint8 init_mmu_dat[] = {0x0c, 0x47, 0x00, 0x04, 0x62, 0x00, 0xfd};
918     if ((base = find_rom_data(0x80000, 0x90000, init_mmu_dat, sizeof(init_mmu_dat))) == 0) return false;
919     }
920     D(bug("init_mmu %08lx\n", base));
921     wp = (uint16 *)(ROMBaseHost + base);
922     *wp++ = htons(M68K_NOP);
923     *wp++ = htons(M68K_NOP);
924     *wp++ = htons(M68K_NOP);
925     *wp++ = htons(M68K_NOP);
926     wp++;
927     *wp++ = htons(0x7000); // moveq #0,d0
928     *wp = htons(M68K_NOP);
929    
930     // Patch InitMMU (no RBV present)
931     static const uint8 init_mmu2_dat[] = {0x08, 0x06, 0x00, 0x0d, 0x67};
932     if (ROMSize <= 0x80000) {
933     base = find_rom_data(0x4000, 0x50000, init_mmu2_dat, sizeof(init_mmu2_dat));
934     } else {
935     base = find_rom_data(0x80000, 0x90000, init_mmu2_dat, sizeof(init_mmu2_dat));
936     }
937     D(bug("init_mmu2 %08lx\n", base));
938     if (base) { // ROM11/10/13/26
939     bp = (uint8 *)(ROMBaseHost + base + 4);
940     *bp = 0x60; // bra
941     }
942    
943     // Patch InitMMU (don't init MMU)
944     static const uint8 init_mmu3_dat[] = {0x0c, 0x2e, 0x00, 0x01, 0xff, 0xe6, 0x66, 0x0c, 0x4c, 0xed, 0x03, 0x87, 0xff, 0xe8};
945     if (ROMSize <= 0x80000) {
946     if ((base = find_rom_data(0x4000, 0x50000, init_mmu3_dat, sizeof(init_mmu3_dat))) == 0) return false;
947     } else {
948     if ((base = find_rom_data(0x80000, 0x90000, init_mmu3_dat, sizeof(init_mmu3_dat))) == 0) return false;
949     }
950     D(bug("init_mmu3 %08lx\n", base));
951     wp = (uint16 *)(ROMBaseHost + base + 6);
952     *wp = htons(M68K_NOP);
953    
954     // Replace XPRAM routines
955     static const uint8 read_xpram_dat[] = {0x26, 0x4e, 0x41, 0xf9, 0x50, 0xf0, 0x00, 0x00, 0x08, 0x90, 0x00, 0x02};
956     base = find_rom_data(0x40000, 0x50000, read_xpram_dat, sizeof(read_xpram_dat));
957     D(bug("read_xpram %08lx\n", base));
958     if (base) { // ROM10
959     wp = (uint16 *)(ROMBaseHost + base);
960     *wp++ = htons(M68K_EMUL_OP_READ_XPRAM);
961     *wp = htons(0x4ed6); // jmp (a6)
962     }
963     static const uint8 read_xpram2_dat[] = {0x26, 0x4e, 0x08, 0x92, 0x00, 0x02, 0xea, 0x59, 0x02, 0x01, 0x00, 0x07, 0x00, 0x01, 0x00, 0xb8};
964     base = find_rom_data(0x40000, 0x50000, read_xpram2_dat, sizeof(read_xpram2_dat));
965     D(bug("read_xpram2 %08lx\n", base));
966     if (base) { // ROM11
967     wp = (uint16 *)(ROMBaseHost + base);
968     *wp++ = htons(M68K_EMUL_OP_READ_XPRAM);
969     *wp = htons(0x4ed6); // jmp (a6)
970     }
971     if (ROMSize > 0x80000) {
972     static const uint8 read_xpram3_dat[] = {0x48, 0xe7, 0xe0, 0x60, 0x02, 0x01, 0x00, 0x70, 0x0c, 0x01, 0x00, 0x20};
973     base = find_rom_data(0x80000, 0x90000, read_xpram3_dat, sizeof(read_xpram3_dat));
974     D(bug("read_xpram3 %08lx\n", base));
975     if (base) { // ROM15
976     wp = (uint16 *)(ROMBaseHost + base);
977     *wp++ = htons(M68K_EMUL_OP_READ_XPRAM2);
978     *wp = htons(M68K_RTS);
979     }
980     }
981    
982     // Patch ClkNoMem
983     base = find_rom_trap(0xa053);
984     wp = (uint16 *)(ROMBaseHost + base);
985     if (ntohs(*wp) == 0x4ed5) { // ROM23/26/27/32
986     static const uint8 clk_no_mem_dat[] = {0x40, 0xc2, 0x00, 0x7c, 0x07, 0x00, 0x48, 0x42};
987     if ((base = find_rom_data(0xb0000, 0xb8000, clk_no_mem_dat, sizeof(clk_no_mem_dat))) == 0) return false;
988     }
989     D(bug("clk_no_mem %08lx\n", base));
990     wp = (uint16 *)(ROMBaseHost + base);
991     *wp++ = htons(M68K_EMUL_OP_CLKNOMEM);
992     *wp = htons(0x4ed5); // jmp (a5)
993    
994     // Patch BootGlobs
995     wp = (uint16 *)(ROMBaseHost + 0x10e);
996     *wp++ = htons(M68K_EMUL_OP_PATCH_BOOT_GLOBS);
997     *wp = htons(M68K_NOP);
998    
999     // Don't init SCC
1000     static const uint8 init_scc_dat[] = {0x08, 0x38, 0x00, 0x01, 0x0d, 0xd1, 0x67, 0x04};
1001     if ((base = find_rom_data(0xa00, 0xa80, init_scc_dat, sizeof(init_scc_dat))) == 0) return false;
1002     D(bug("init_scc %08lx\n", base));
1003     wp = (uint16 *)(ROMBaseHost + base);
1004     *wp = htons(M68K_RTS);
1005    
1006     // Don't access 0x50f1a101
1007     wp = (uint16 *)(ROMBaseHost + 0x4232);
1008     if (ntohs(wp[1]) == 0x50f1 && ntohs(wp[2]) == 0xa101) { // ROM32
1009     *wp++ = htons(M68K_NOP);
1010     *wp++ = htons(M68K_NOP);
1011     *wp++ = htons(M68K_NOP);
1012     *wp++ = htons(M68K_NOP);
1013     *wp = htons(M68K_NOP);
1014     }
1015    
1016     // Don't init IWM
1017     wp = (uint16 *)(ROMBaseHost + 0x9c0);
1018     *wp = htons(M68K_RTS);
1019    
1020     // Don't init SCSI
1021     wp = (uint16 *)(ROMBaseHost + 0x9a0);
1022     *wp = htons(M68K_RTS);
1023    
1024     // Don't init ASC
1025     static const uint8 init_asc_dat[] = {0x26, 0x68, 0x00, 0x30, 0x12, 0x00, 0xeb, 0x01};
1026     base = find_rom_data(0x4000, 0x5000, init_asc_dat, sizeof(init_asc_dat));
1027     D(bug("init_asc %08lx\n", base));
1028     if (base) { // ROM15/22/23/26/27/32
1029     wp = (uint16 *)(ROMBaseHost + base);
1030     *wp = htons(0x4ed6); // jmp (a6)
1031     }
1032    
1033     // Don't EnableExtCache
1034     wp = (uint16 *)(ROMBaseHost + 0x190);
1035     *wp++ = htons(M68K_NOP);
1036     *wp = htons(M68K_NOP);
1037    
1038     // Don't DisableIntSources
1039     wp = (uint16 *)(ROMBaseHost + 0x9f4c);
1040     *wp = htons(M68K_RTS);
1041    
1042     // Fake CPU speed test (SetupTimeK)
1043     wp = (uint16 *)(ROMBaseHost + 0x800);
1044     *wp++ = htons(0x31fc); // move.w #xxx,TimeDBRA
1045     *wp++ = htons(100);
1046     *wp++ = htons(0x0d00);
1047     *wp++ = htons(0x31fc); // move.w #xxx,TimeSCCDBRA
1048     *wp++ = htons(100);
1049     *wp++ = htons(0x0d02);
1050     *wp++ = htons(0x31fc); // move.w #xxx,TimeSCSIDBRA
1051     *wp++ = htons(100);
1052     *wp++ = htons(0x0b24);
1053     *wp++ = htons(0x31fc); // move.w #xxx,TimeRAMDBRA
1054     *wp++ = htons(100);
1055     *wp++ = htons(0x0cea);
1056     *wp = htons(M68K_RTS);
1057    
1058     #if REAL_ADDRESSING
1059     // Move system zone to start of Mac RAM
1060     lp = (uint32 *)(ROMBaseHost + 0x50a);
1061     *lp++ = htonl(RAMBaseMac);
1062     *lp = htonl(RAMBaseMac + 0x1800);
1063     #endif
1064    
1065     #if !ROM_IS_WRITE_PROTECTED
1066     #if defined(AMIGA)
1067     // Set fake handle at 0x0000 to scratch memory area (so broken Mac programs won't write into Mac ROM)
1068     extern uint32 ScratchMem;
1069     wp = (uint16 *)(ROMBaseHost + 0xccaa);
1070     *wp++ = htons(0x203c); // move.l #ScratchMem,d0
1071     *wp++ = htons(ScratchMem >> 16);
1072     *wp = htons(ScratchMem);
1073     #else
1074     #error System specific handling for writable ROM is required here
1075     #endif
1076     #endif
1077    
1078     #if REAL_ADDRESSING && defined(AMIGA)
1079     // Don't overwrite SysBase under AmigaOS
1080     wp = (uint16 *)(ROMBaseHost + 0xccb4);
1081     *wp++ = htons(M68K_NOP);
1082     *wp = htons(M68K_NOP);
1083     #endif
1084    
1085     // Don't write to VIA in InitTimeMgr
1086     wp = (uint16 *)(ROMBaseHost + 0xb0e2);
1087     *wp++ = htons(0x4cdf); // movem.l (sp)+,d0-d5/a0-a4
1088     *wp++ = htons(0x1f3f);
1089     *wp = htons(M68K_RTS);
1090    
1091     // Don't read ModelID from 0x5ffffffc
1092     static const uint8 model_id_dat[] = {0x20, 0x7c, 0x5f, 0xff, 0xff, 0xfc, 0x72, 0x07, 0xc2, 0x90};
1093     base = find_rom_data(0x40000, 0x50000, model_id_dat, sizeof(model_id_dat));
1094     D(bug("model_id %08lx\n", base));
1095     if (base) { // ROM20
1096     wp = (uint16 *)(ROMBaseHost + base + 8);
1097     *wp++ = htons(M68K_NOP);
1098     *wp++ = htons(M68K_NOP);
1099     *wp++ = htons(M68K_NOP);
1100     *wp = htons(M68K_NOP);
1101     }
1102    
1103     // Don't read ModelID from 0x5ffffffc
1104     static const uint8 model_id2_dat[] = {0x45, 0xf9, 0x5f, 0xff, 0xff, 0xfc, 0x20, 0x12};
1105     base = find_rom_data(0x4000, 0x5000, model_id2_dat, sizeof(model_id2_dat));
1106     D(bug("model_id2 %08lx\n", base));
1107     if (base) { // ROM27/32
1108     wp = (uint16 *)(ROMBaseHost + base + 6);
1109     *wp++ = htons(0x7000); // moveq #0,d0
1110     *wp++ = htons(0xb040); // cmp.w d0,d0
1111     *wp = htons(0x4ed6); // jmp (a6)
1112     }
1113    
1114     // Install slot ROM
1115     if (!InstallSlotROM())
1116     return false;
1117    
1118     // Don't probe NuBus slots
1119     static const uint8 nubus_dat[] = {0x45, 0xfa, 0x00, 0x0a, 0x42, 0xa7, 0x10, 0x11};
1120     base = find_rom_data(0x5000, 0x6000, nubus_dat, sizeof(nubus_dat));
1121     D(bug("nubus %08lx\n", base));
1122     if (base) { // ROM10/11
1123     wp = (uint16 *)(ROMBaseHost + base + 6);
1124     *wp++ = htons(M68K_NOP);
1125     *wp++ = htons(M68K_NOP);
1126     *wp = htons(M68K_NOP);
1127     }
1128    
1129     // Don't EnableOneSecInts
1130     static const uint8 lea_dat[] = {0x41, 0xf9};
1131     if ((base = find_rom_data(0x226, 0x22a, lea_dat, sizeof(lea_dat))) == 0) return false;
1132     D(bug("enable_one_sec_ints %08lx\n", base));
1133     wp = (uint16 *)(ROMBaseHost + base);
1134     *wp++ = htons(M68K_NOP);
1135     *wp++ = htons(M68K_NOP);
1136     *wp++ = htons(M68K_NOP);
1137     *wp++ = htons(M68K_NOP);
1138     *wp = htons(M68K_NOP);
1139    
1140     // Don't EnableParityPatch/Enable60HzInts
1141     if ((base = find_rom_data(0x230, 0x234, lea_dat, sizeof(lea_dat))) == 0) {
1142     wp = (uint16 *)(ROMBaseHost + 0x230);
1143     if (ntohs(*wp) == 0x6100) // ROM11
1144     base = 0x230;
1145     else
1146     return false;
1147     }
1148     D(bug("enable_60hz_ints %08lx\n", base));
1149     wp = (uint16 *)(ROMBaseHost + base);
1150     *wp++ = htons(M68K_NOP);
1151     *wp++ = htons(M68K_NOP);
1152     *wp++ = htons(M68K_NOP);
1153     *wp++ = htons(M68K_NOP);
1154     *wp = htons(M68K_NOP);
1155    
1156     // Fix logical/physical RAM size (CompBootStack) (must be done after InitMemMgr!)
1157     static const uint8 fix_memsize_dat[] = {0x4e, 0x75};
1158     if ((base = find_rom_data(0x490, 0x4b0, fix_memsize_dat, sizeof(fix_memsize_dat))) == 0) return false;
1159     D(bug("fix_memsize %08lx\n", base));
1160     wp = (uint16 *)(ROMBaseHost + base);
1161     *wp++ = htons(M68K_EMUL_OP_FIX_MEMSIZE);
1162     *wp = htons(M68K_RTS);
1163    
1164     static const uint8 fix_memsize2_dat[] = {0x22, 0x30, 0x81, 0xe2, 0x0d, 0xdc, 0xff, 0xba, 0xd2, 0xb0, 0x81, 0xe2, 0x0d, 0xdc, 0xff, 0xec, 0x21, 0xc1, 0x1e, 0xf8};
1165     base = find_rom_data(0x4c000, 0x4c080, fix_memsize2_dat, sizeof(fix_memsize2_dat));
1166     D(bug("fix_memsize2 %08lx\n", base));
1167     if (base) { // ROM15/22/23/26/27/32
1168     wp = (uint16 *)(ROMBaseHost + base + 16);
1169     *wp++ = htons(M68K_NOP);
1170     *wp = htons(M68K_NOP);
1171     }
1172    
1173     // Don't open .Sound driver but install our own drivers
1174     wp = (uint16 *)(ROMBaseHost + 0x1142);
1175     *wp = htons(M68K_EMUL_OP_INSTALL_DRIVERS);
1176    
1177     // Don't access SonyVars
1178     wp = (uint16 *)(ROMBaseHost + 0x1144);
1179     *wp++ = htons(M68K_NOP);
1180     *wp++ = htons(M68K_NOP);
1181     *wp++ = htons(M68K_NOP);
1182     *wp++ = htons(M68K_NOP);
1183     wp += 2;
1184     *wp = htons(M68K_NOP);
1185    
1186     // Don't write to VIA in InitADB
1187     wp = (uint16 *)(ROMBaseHost + 0xa8a8);
1188     if (*wp == 0) { // ROM22/23/26/27/32
1189     wp = (uint16 *)(ROMBaseHost + 0xb2c6a);
1190     *wp++ = htons(M68K_NOP);
1191     *wp++ = htons(M68K_NOP);
1192     *wp = htons(M68K_NOP);
1193     wp = (uint16 *)(ROMBaseHost + 0xb2d2e);
1194     *wp++ = htons(M68K_NOP);
1195     *wp++ = htons(M68K_NOP);
1196     *wp++ = htons(M68K_NOP);
1197     *wp++ = htons(M68K_NOP);
1198     *wp++ = htons(M68K_NOP);
1199     *wp++ = htons(M68K_NOP);
1200     *wp++ = htons(M68K_NOP);
1201     *wp++ = htons(M68K_NOP);
1202     *wp++ = htons(M68K_NOP);
1203     *wp++ = htons(M68K_NOP);
1204     *wp++ = htons(M68K_NOP);
1205     *wp++ = htons(M68K_NOP);
1206     wp += 2;
1207     *wp++ = htons(M68K_NOP);
1208     *wp = htons(M68K_NOP);
1209     } else {
1210     *wp++ = htons(M68K_NOP);
1211     *wp++ = htons(M68K_NOP);
1212     *wp = htons(M68K_NOP);
1213     wp = (uint16 *)(ROMBaseHost + 0xa662);
1214     *wp++ = htons(M68K_NOP);
1215     *wp++ = htons(M68K_NOP);
1216     *wp++ = htons(M68K_NOP);
1217     *wp++ = htons(M68K_NOP);
1218     *wp++ = htons(M68K_NOP);
1219     wp += 2;
1220     *wp++ = htons(M68K_NOP);
1221     *wp = htons(M68K_NOP);
1222     }
1223    
1224     // Don't EnableSlotInts
1225     if ((base = find_rom_data(0x2ee, 0x2f2, lea_dat, sizeof(lea_dat))) == 0) return false;
1226     D(bug("enable_slot_ints %08lx\n", base));
1227     wp = (uint16 *)(ROMBaseHost + base);
1228     *wp++ = htons(M68K_NOP);
1229     *wp++ = htons(M68K_NOP);
1230     *wp++ = htons(M68K_NOP);
1231     *wp++ = htons(M68K_NOP);
1232     *wp = htons(M68K_NOP);
1233    
1234     // Don't mangle frame buffer base (GetDevBase)
1235     wp = (uint16 *)(ROMBaseHost + 0x5b78);
1236     *wp++ = htons(M68K_NOP);
1237     *wp++ = htons(M68K_NOP);
1238     *wp++ = htons(0x2401); // move.l d1,d2
1239     *wp = htons(0x605e); // bra 0x40805bde
1240    
1241     // Really don't mangle frame buffer base
1242     if (ROMSize > 0x80000) {
1243     static const uint8 frame_base_dat[] = {0x22, 0x78, 0x0d, 0xd8, 0xd3, 0xe9, 0x00, 0x08};
1244     base = find_rom_data(0x8c000, 0x8d000, frame_base_dat, sizeof(frame_base_dat));
1245     D(bug("frame_base %08lx\n", base));
1246     if (base) { // ROM22/23/26/27/32
1247     wp = (uint16 *)(ROMBaseHost + base);
1248     *wp++ = htons(0x2401); // move.l d1,d2
1249     *wp = htons(M68K_RTS);
1250     }
1251     }
1252    
1253     // Don't write to VIA2
1254     static const uint8 via2_dat[] = {0x20, 0x78, 0x0c, 0xec, 0x11, 0x7c, 0x00, 0x90};
1255     if ((base = find_rom_data(0xa000, 0xa400, via2_dat, sizeof(via2_dat))) == 0) return false;
1256     D(bug("via2 %08lx\n", base));
1257     wp = (uint16 *)(ROMBaseHost + base + 4);
1258     *wp = htons(M68K_RTS);
1259    
1260     // Don't write to VIA2, even on ROM20
1261     static const uint8 via2b_dat[] = {0x20, 0x78, 0x0c, 0xec, 0x11, 0x7c, 0x00, 0x90, 0x00, 0x13, 0x4e, 0x75};
1262     base = find_rom_data(0x40000, 0x44000, via2b_dat, sizeof(via2b_dat));
1263     D(bug("via2b %08lx\n", base));
1264     if (base) { // ROM19/20
1265     wp = (uint16 *)(ROMBaseHost + base + 4);
1266     *wp = htons(M68K_RTS);
1267     }
1268    
1269     // Don't use PTEST instruction on 68040/060
1270     if (ROMSize > 0x80000) {
1271    
1272     // BlockMove()
1273     static const uint8 ptest_dat[] = {0xa0, 0x8d, 0x0c, 0x81, 0x00, 0x00, 0x0c, 0x00, 0x6d, 0x06, 0x4e, 0x71, 0xf4, 0xf8};
1274     base = find_rom_data(0x87000, 0x87800, ptest_dat, sizeof(ptest_dat));
1275     D(bug("ptest %08lx\n", base));
1276     if (base) { // ROM15/22/23/26/27/32
1277     wp = (uint16 *)(ROMBaseHost + base + 8);
1278     *wp = htons(M68K_NOP);
1279     }
1280    
1281     // SANE
1282     static const uint8 ptest2_dat[] = {0x0c, 0x38, 0x00, 0x04, 0x01, 0x2f, 0x6d, 0x54, 0x48, 0xe7, 0xf8, 0x60};
1283     base = find_rom_data(0, ROMSize, ptest2_dat, sizeof(ptest2_dat));
1284     D(bug("ptest2 %08lx\n", base));
1285     if (base) { // ROM15/20/22/23/26/27/32
1286     wp = (uint16 *)(ROMBaseHost + base + 8);
1287     *wp++ = htons(M68K_NOP);
1288     *wp++ = htons(0xf4f8); // cpusha dc/ic
1289     *wp++ = htons(M68K_NOP);
1290     *wp++ = htons(0x7000); // moveq #0,d0
1291     *wp = htons(M68K_RTS);
1292     }
1293     }
1294    
1295     // Patch .EDisk driver (don't scan for EDisks in the area ROMBase..0xe00000)
1296     uint32 edisk_offset = find_rom_resource('DRVR', 51);
1297     if (edisk_offset) {
1298     static const uint8 edisk_dat[] = {0xd5, 0xfc, 0x00, 0x01, 0x00, 0x00, 0xb5, 0xfc, 0x00, 0xe0, 0x00, 0x00};
1299     base = find_rom_data(edisk_offset, edisk_offset + 0x10000, edisk_dat, sizeof(edisk_dat));
1300     D(bug("edisk %08lx\n", base));
1301     if (base) {
1302     wp = (uint16 *)(ROMBaseHost + base + 8);
1303     *wp++ = 0;
1304     *wp = 0;
1305     }
1306     }
1307    
1308     // Replace .Sony driver
1309     sony_offset = find_rom_resource('DRVR', 4);
1310     D(bug("sony %08lx\n", sony_offset));
1311     memcpy(ROMBaseHost + sony_offset, sony_driver, sizeof(sony_driver));
1312    
1313     // Install .Disk and .AppleCD drivers
1314     memcpy(ROMBaseHost + sony_offset + 0x100, disk_driver, sizeof(disk_driver));
1315     memcpy(ROMBaseHost + sony_offset + 0x200, cdrom_driver, sizeof(cdrom_driver));
1316    
1317     // Copy icons to ROM
1318     SonyDiskIconAddr = ROMBaseMac + sony_offset + 0x400;
1319     memcpy(ROMBaseHost + sony_offset + 0x400, SonyDiskIcon, sizeof(SonyDiskIcon));
1320     SonyDriveIconAddr = ROMBaseMac + sony_offset + 0x600;
1321     memcpy(ROMBaseHost + sony_offset + 0x600, SonyDriveIcon, sizeof(SonyDriveIcon));
1322     DiskIconAddr = ROMBaseMac + sony_offset + 0x800;
1323     memcpy(ROMBaseHost + sony_offset + 0x800, DiskIcon, sizeof(DiskIcon));
1324     CDROMIconAddr = ROMBaseMac + sony_offset + 0xa00;
1325     memcpy(ROMBaseHost + sony_offset + 0xa00, CDROMIcon, sizeof(CDROMIcon));
1326    
1327     // Install SERD patch and serial drivers
1328     serd_offset = find_rom_resource('SERD', 0);
1329     D(bug("serd %08lx\n", serd_offset));
1330     wp = (uint16 *)(ROMBaseHost + serd_offset + 12);
1331     *wp++ = htons(M68K_EMUL_OP_SERD);
1332     *wp = htons(M68K_RTS);
1333     memcpy(ROMBaseHost + serd_offset + 0x100, ain_driver, sizeof(ain_driver));
1334     memcpy(ROMBaseHost + serd_offset + 0x200, aout_driver, sizeof(aout_driver));
1335     memcpy(ROMBaseHost + serd_offset + 0x300, bin_driver, sizeof(bin_driver));
1336     memcpy(ROMBaseHost + serd_offset + 0x400, bout_driver, sizeof(bout_driver));
1337    
1338     // Replace ADBOp()
1339     memcpy(ROMBaseHost + find_rom_trap(0xa07c), adbop_patch, sizeof(adbop_patch));
1340    
1341     // Replace Time Manager (the Microseconds patch is activated in InstallDrivers())
1342     wp = (uint16 *)(ROMBaseHost + find_rom_trap(0xa058));
1343     *wp++ = htons(M68K_EMUL_OP_INSTIME);
1344     *wp = htons(M68K_RTS);
1345     wp = (uint16 *)(ROMBaseHost + find_rom_trap(0xa059));
1346     *wp++ = htons(0x40e7); // move sr,-(sp)
1347     *wp++ = htons(0x007c); // ori #$0700,sr
1348     *wp++ = htons(0x0700);
1349     *wp++ = htons(M68K_EMUL_OP_RMVTIME);
1350     *wp++ = htons(0x46df); // move (sp)+,sr
1351     *wp = htons(M68K_RTS);
1352     wp = (uint16 *)(ROMBaseHost + find_rom_trap(0xa05a));
1353     *wp++ = htons(0x40e7); // move sr,-(sp)
1354     *wp++ = htons(0x007c); // ori #$0700,sr
1355     *wp++ = htons(0x0700);
1356     *wp++ = htons(M68K_EMUL_OP_PRIMETIME);
1357     *wp++ = htons(0x46df); // move (sp)+,sr
1358     *wp++ = htons(M68K_RTS);
1359     microseconds_offset = (uint8 *)wp - ROMBaseHost;
1360     *wp++ = htons(M68K_EMUL_OP_MICROSECONDS);
1361     *wp = htons(M68K_RTS);
1362    
1363     // Replace SCSIDispatch()
1364     wp = (uint16 *)(ROMBaseHost + find_rom_trap(0xa815));
1365     *wp++ = htons(M68K_EMUL_OP_SCSI_DISPATCH);
1366     *wp++ = htons(0x2e49); // move.l a1,a7
1367     *wp = htons(M68K_JMP_A0);
1368    
1369     // Modify vCheckLoad() so we can patch resources
1370     wp = (uint16 *)(ROMBaseHost + 0x1b8f4);
1371     *wp++ = htons(M68K_JMP);
1372     *wp++ = htons((ROMBaseMac + sony_offset + 0x300) >> 16);
1373     *wp = htons((ROMBaseMac + sony_offset + 0x300) & 0xffff);
1374     wp = (uint16 *)(ROMBaseHost + sony_offset + 0x300);
1375     *wp++ = htons(0x2f03); // move.l d3,-(sp) (save type)
1376     *wp++ = htons(0x2078); // move.l $07f0,a0
1377     *wp++ = htons(0x07f0);
1378     *wp++ = htons(M68K_JSR_A0);
1379     *wp++ = htons(0x221f); // move.l (sp)+,d1 (restore type)
1380     *wp++ = htons(M68K_EMUL_OP_CHECKLOAD);
1381     *wp = htons(M68K_RTS);
1382    
1383     // Patch PowerOff()
1384     wp = (uint16 *)(ROMBaseHost + find_rom_trap(0xa05b)); // PowerOff()
1385     *wp = htons(M68K_EMUL_OP_SHUTDOWN);
1386    
1387     // Install PutScrap() patch for clipboard data exchange (the patch is activated by EMUL_OP_INSTALL_DRIVERS)
1388     PutScrapPatch = ROMBaseMac + sony_offset + 0xc00;
1389     base = ROMBaseMac + find_rom_trap(0xa9fe);
1390     wp = (uint16 *)(ROMBaseHost + sony_offset + 0xc00);
1391     *wp++ = htons(M68K_EMUL_OP_PUT_SCRAP);
1392     *wp++ = htons(M68K_JMP);
1393     *wp++ = htons(base >> 16);
1394     *wp = htons(base & 0xffff);
1395    
1396     // Install MemoryDispatch() replacement routine (activated in PatchAfterStartup())
1397     memory_dispatch_offset = sony_offset + 0xc20;
1398     wp = (uint16 *)(ROMBaseHost + memory_dispatch_offset);
1399     *wp++ = htons(M68K_EMUL_OP_MEMORY_DISPATCH);
1400     *wp = htons(M68K_RTS);
1401    
1402     // Patch VIA interrupt handler
1403     wp = (uint16 *)(ROMBaseHost + 0x9bc4); // Level 1 handler
1404     *wp++ = htons(0x7002); // moveq #2,d0 (always 60Hz interrupt)
1405     *wp++ = htons(M68K_NOP);
1406     *wp++ = htons(M68K_NOP);
1407     *wp++ = htons(M68K_NOP);
1408     *wp = htons(M68K_NOP);
1409    
1410     wp = (uint16 *)(ROMBaseHost + 0xa29a); // 60Hz handler (handles everything)
1411     *wp++ = htons(M68K_EMUL_OP_IRQ);
1412     *wp++ = htons(0x4a80); // tst.l d0
1413     *wp = htons(0x67f4); // beq 0x4080a294
1414     return true;
1415     }
1416    
1417     bool PatchROM(void)
1418     {
1419     // Print ROM info
1420     D(bug("ROM Info:\n"));
1421     D(bug("Checksum: %08lx\n", ReadMacInt32(ROMBaseMac)));
1422     D(bug("Version: %04x\n", ROMVersion));
1423     D(bug("Sub Version: %04x\n", ReadMacInt16(ROMBaseMac + 18)));
1424    
1425     // Patch ROM depending on version
1426     switch (ROMVersion) {
1427     case ROM_VERSION_CLASSIC:
1428     if (!patch_rom_classic())
1429     return false;
1430     break;
1431     case ROM_VERSION_32:
1432     if (!patch_rom_32())
1433     return false;
1434     break;
1435     default:
1436     return false;
1437     }
1438    
1439     #ifdef M68K_BREAKPOINT
1440     // Install breakpoint
1441     uint16 *wp = (uint16 *)(ROMBaseHost + M68K_BREAKPOINT);
1442     *wp = htons(M68K_EMUL_BREAK);
1443     #endif
1444    
1445     // Clear caches as we loaded and patched code
1446     FlushCodeCache(ROMBaseHost, ROMSize);
1447     return true;
1448     }