ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/rom_patches.cpp
Revision: 1.11
Committed: 1999-10-27T16:59:38Z (24 years, 6 months ago) by cebix
Branch: MAIN
Changes since 1.10: +162 -10 lines
Log Message:
- imported fixed UAE FPU from Lauri
- extfs.cpp: fixed bug with fsResolveWDCB in fs_get_wd_info()
- ExtFS: MAX_PATH_LENGTH is global, removed third parameter to
  add_path_component()
- rom_patches.cpp: added print_rom_info()
- Unix: added "-rominfo" command line argument
- extfs_unix.cpp: supports finder info and resource forks
- prefs_editor_gtk.cpp: tab widget is no longer scrollable

File Contents

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