ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/rom_patches.cpp
Revision: 1.14
Committed: 2000-07-22T16:00:33Z (23 years, 9 months ago) by cebix
Branch: MAIN
Changes since 1.13: +2 -2 lines
Log Message:
- small fixes for NetBSD and AmigaOS

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