ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/rom_patches.cpp
Revision: 1.9
Committed: 1999-10-25T19:01:29Z (24 years, 6 months ago) by cebix
Branch: MAIN
Changes since 1.8: +1 -1 lines
Log Message:
- fixes to audio_oss_esd.cpp from Alexander R. Pruss (8-bit mode)
- added configuration summary to configure script
- prefs_editor_amiga.cpp: output of SCSI prefs was broken

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