ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/rom_patches.cpp
Revision: 1.16
Committed: 2000-08-20T14:08:40Z (24 years, 2 months ago) by jlachmann
Branch: MAIN
Changes since 1.15: +25 -8 lines
Log Message:
added MacsBug Support -jl-

File Contents

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