ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/rom_patches.cpp
Revision: 1.20
Committed: 2003-12-14T14:23:46Z (20 years, 5 months ago) by gbeauche
Branch: MAIN
Changes since 1.19: +227 -133 lines
Log Message:
Generic ROM patches from ROMTYPE_PARCELS experiments, no apparent
regession. There is no improvement either.

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * rom_patches.cpp - ROM patches
3     *
4     * SheepShaver (C) 1997-2002 Christian Bauer and Marc Hellwig
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     /*
22     * TODO:
23     * IRQ_NEST must be handled atomically
24     * Don't use r1 in extra routines
25     */
26    
27     #include <string.h>
28    
29     #include "sysdeps.h"
30     #include "rom_patches.h"
31     #include "main.h"
32     #include "prefs.h"
33     #include "cpu_emulation.h"
34     #include "emul_op.h"
35     #include "xlowmem.h"
36     #include "sony.h"
37     #include "disk.h"
38     #include "cdrom.h"
39     #include "audio.h"
40     #include "audio_defs.h"
41     #include "serial.h"
42     #include "macos_util.h"
43 gbeauche 1.18 #include "thunks.h"
44 cebix 1.1
45     #define DEBUG 0
46     #include "debug.h"
47    
48    
49     // 68k breakpoint address
50     //#define M68K_BREAK_POINT 0x29e0 // BootMe
51     //#define M68K_BREAK_POINT 0x2a1e // Boot block code returned
52     //#define M68K_BREAK_POINT 0x3150 // CritError
53     //#define M68K_BREAK_POINT 0x187ce // Unimplemented trap
54    
55     // PowerPC breakpoint address
56     //#define POWERPC_BREAK_POINT 0x36e6c0 // 68k emulator start
57    
58     #define DISABLE_SCSI 1
59    
60    
61     // Other ROM addresses
62 gbeauche 1.17 const uint32 CHECK_LOAD_PATCH_SPACE = 0x2fcf00;
63     const uint32 PUT_SCRAP_PATCH_SPACE = 0x2fcf80;
64     const uint32 GET_SCRAP_PATCH_SPACE = 0x2fcfc0;
65     const uint32 ADDR_MAP_PATCH_SPACE = 0x2fd000;
66 cebix 1.1
67     // Global variables
68     int ROMType; // ROM type
69     static uint32 sony_offset; // Offset of .Sony driver resource
70    
71     // Prototypes
72     static bool patch_nanokernel_boot(void);
73     static bool patch_68k_emul(void);
74     static bool patch_nanokernel(void);
75     static bool patch_68k(void);
76    
77    
78 gbeauche 1.2 // Decode LZSS data
79     static void decode_lzss(const uint8 *src, uint8 *dest, int size)
80     {
81     char dict[0x1000];
82     int run_mask = 0, dict_idx = 0xfee;
83     for (;;) {
84     if (run_mask < 0x100) {
85     // Start new run
86     if (--size < 0)
87     break;
88     run_mask = *src++ | 0xff00;
89     }
90     bool bit = run_mask & 1;
91     run_mask >>= 1;
92     if (bit) {
93     // Verbatim copy
94     if (--size < 0)
95     break;
96     int c = *src++;
97     dict[dict_idx++] = c;
98     *dest++ = c;
99     dict_idx &= 0xfff;
100     } else {
101     // Copy from dictionary
102     if (--size < 0)
103     break;
104     int idx = *src++;
105     if (--size < 0)
106     break;
107     int cnt = *src++;
108     idx |= (cnt << 4) & 0xf00;
109     cnt = (cnt & 0x0f) + 3;
110     while (cnt--) {
111     char c = dict[idx++];
112     dict[dict_idx++] = c;
113     *dest++ = c;
114     idx &= 0xfff;
115     dict_idx &= 0xfff;
116     }
117     }
118     }
119     }
120    
121     // Decode parcels of ROM image (MacOS 9.X and even earlier)
122     void decode_parcels(const uint8 *src, uint8 *dest, int size)
123     {
124     uint32 parcel_offset = 0x14;
125     D(bug("Offset Type Name\n"));
126     while (parcel_offset != 0) {
127     const uint32 *parcel_data = (uint32 *)(src + parcel_offset);
128 gbeauche 1.3 uint32 next_offset = ntohl(parcel_data[0]);
129 gbeauche 1.2 uint32 parcel_type = ntohl(parcel_data[1]);
130     D(bug("%08x %c%c%c%c %s\n", parcel_offset,
131     (parcel_type >> 24) & 0xff, (parcel_type >> 16) & 0xff,
132     (parcel_type >> 8) & 0xff, parcel_type & 0xff, &parcel_data[6]));
133     if (parcel_type == FOURCC('r','o','m',' ')) {
134     uint32 lzss_offset = ntohl(parcel_data[2]);
135 gbeauche 1.16 uint32 lzss_size = ((uintptr)src + next_offset) - ((uintptr)parcel_data + lzss_offset);
136 gbeauche 1.2 decode_lzss((uint8 *)parcel_data + lzss_offset, dest, lzss_size);
137     }
138 gbeauche 1.3 parcel_offset = next_offset;
139 gbeauche 1.2 }
140     }
141    
142    
143     /*
144     * Decode ROM image, 4 MB plain images or NewWorld images
145     */
146    
147     bool DecodeROM(uint8 *data, uint32 size)
148     {
149     if (size == ROM_SIZE) {
150     // Plain ROM image
151     memcpy((void *)ROM_BASE, data, ROM_SIZE);
152     return true;
153     }
154     else if (strncmp((char *)data, "<CHRP-BOOT>", 11) == 0) {
155     // CHRP compressed ROM image
156     uint32 image_offset, image_size;
157     bool decode_info_ok = false;
158    
159     char *s = strstr((char *)data, "constant lzss-offset");
160     if (s != NULL) {
161     // Probably a plain LZSS compressed ROM image
162     if (sscanf(s - 7, "%06x", &image_offset) == 1) {
163     s = strstr((char *)data, "constant lzss-size");
164     if (s != NULL && (sscanf(s - 7, "%06x", &image_size) == 1))
165     decode_info_ok = true;
166     }
167     }
168     else {
169     // Probably a MacOS 9.2.x ROM image
170     s = strstr((char *)data, "constant parcels-offset");
171     if (s != NULL) {
172     if (sscanf(s - 7, "%06x", &image_offset) == 1) {
173     s = strstr((char *)data, "constant parcels-size");
174     if (s != NULL && (sscanf(s - 7, "%06x", &image_size) == 1))
175     decode_info_ok = true;
176     }
177     }
178     }
179    
180     // No valid information to decode the ROM found?
181     if (!decode_info_ok)
182     return false;
183    
184     // Check signature, this could be a parcels-based ROM image
185     uint32 rom_signature = ntohl(*(uint32 *)(data + image_offset));
186     if (rom_signature == FOURCC('p','r','c','l')) {
187     D(bug("Offset of parcels data: %08x\n", image_offset));
188     D(bug("Size of parcels data: %08x\n", image_size));
189     decode_parcels(data + image_offset, (uint8 *)ROM_BASE, image_size);
190     }
191     else {
192     D(bug("Offset of compressed data: %08x\n", image_offset));
193     D(bug("Size of compressed data: %08x\n", image_size));
194     decode_lzss(data + image_offset, (uint8 *)ROM_BASE, image_size);
195     }
196     return true;
197     }
198     return false;
199     }
200    
201    
202 cebix 1.1 /*
203     * Search ROM for byte string, return ROM offset (or 0)
204     */
205    
206     static uint32 find_rom_data(uint32 start, uint32 end, const uint8 *data, uint32 data_len)
207     {
208     uint32 ofs = start;
209     while (ofs < end) {
210     if (!memcmp((void *)(ROM_BASE + ofs), data, data_len))
211     return ofs;
212     ofs++;
213     }
214     return 0;
215     }
216    
217    
218     /*
219     * Search ROM resource by type/ID, return ROM offset of resource data
220     */
221    
222     static uint32 rsrc_ptr = 0;
223    
224     // id = 4711 means "find any ID"
225     static uint32 find_rom_resource(uint32 s_type, int16 s_id = 4711, bool cont = false)
226     {
227     uint32 *lp = (uint32 *)(ROM_BASE + 0x1a);
228     uint32 x = ntohl(*lp);
229     uint8 *bp = (uint8 *)(ROM_BASE + x + 5);
230     uint32 header_size = *bp;
231    
232     if (!cont)
233     rsrc_ptr = x;
234     else if (rsrc_ptr == 0)
235     return 0;
236    
237     for (;;) {
238     lp = (uint32 *)(ROM_BASE + rsrc_ptr);
239     rsrc_ptr = ntohl(*lp);
240     if (rsrc_ptr == 0)
241     break;
242    
243     rsrc_ptr += header_size;
244    
245     lp = (uint32 *)(ROM_BASE + rsrc_ptr + 4);
246     uint32 data = ntohl(*lp); lp++;
247     uint32 type = ntohl(*lp); lp++;
248     int16 id = ntohs(*(int16 *)lp);
249     if (type == s_type && (id == s_id || s_id == 4711))
250     return data;
251     }
252     return 0;
253     }
254    
255    
256     /*
257     * Search offset of A-Trap routine in ROM
258     */
259    
260     static uint32 find_rom_trap(uint16 trap)
261     {
262     uint32 *lp = (uint32 *)(ROM_BASE + 0x22);
263     lp = (uint32 *)(ROM_BASE + ntohl(*lp));
264    
265     if (trap > 0xa800)
266     return ntohl(lp[trap & 0x3ff]);
267     else
268     return ntohl(lp[(trap & 0xff) + 0x400]);
269     }
270    
271    
272     /*
273 gbeauche 1.20 * Return target of branch instruction specified at ADDR, or 0 if
274     * there is no such instruction
275     */
276    
277     static uint32 powerpc_branch_target(uintptr addr)
278     {
279     uint32 opcode = ntohl(*(uint32 *)addr);
280     uint32 primop = opcode >> 26;
281     uint32 target = 0;
282    
283     if (primop == 18) { // Branch
284     target = opcode & 0x3fffffc;
285     if (target & 0x2000000)
286     target |= 0xfc000000;
287     if ((opcode & 2) == 0)
288     target += addr;
289     }
290     else if (primop == 16) { // Branch Conditional
291     target = (int32)(int16)(opcode & 0xfffc);
292     if ((opcode & 2) == 0)
293     target += addr;
294     }
295     return target;
296     }
297    
298    
299     /*
300     * Search ROM for instruction branching to target address, return 0 if none found
301     */
302    
303     static uint32 find_rom_powerpc_branch(uint32 start, uint32 end, uint32 target)
304     {
305     for (uint32 addr = start; addr < end; addr += 4) {
306     if (powerpc_branch_target(ROM_BASE + addr) == ROM_BASE + target)
307     return addr;
308     }
309     return 0;
310     }
311    
312    
313     /*
314 cebix 1.1 * List of audio sifters installed in ROM and System file
315     */
316    
317     struct sift_entry {
318     uint32 type;
319     int16 id;
320     };
321     static sift_entry sifter_list[32];
322     static int num_sifters;
323    
324     void AddSifter(uint32 type, int16 id)
325     {
326     if (FindSifter(type, id))
327     return;
328     D(bug(" adding sifter type %c%c%c%c (%08x), id %d\n", type >> 24, (type >> 16) & 0xff, (type >> 8) & 0xff, type & 0xff, type, id));
329     sifter_list[num_sifters].type = type;
330     sifter_list[num_sifters].id = id;
331     num_sifters++;
332     }
333    
334     bool FindSifter(uint32 type, int16 id)
335     {
336     for (int i=0; i<num_sifters; i++) {
337     if (sifter_list[i].type == type && sifter_list[i].id == id)
338     return true;
339     }
340     return false;
341     }
342    
343    
344     /*
345     * Driver stubs
346     */
347    
348     static const uint8 sony_driver[] = { // Replacement for .Sony driver
349     // Driver header
350     SonyDriverFlags >> 8, SonyDriverFlags & 0xff, 0, 0, 0, 0, 0, 0,
351     0x00, 0x18, // Open() offset
352     0x00, 0x1c, // Prime() offset
353     0x00, 0x20, // Control() offset
354     0x00, 0x2c, // Status() offset
355     0x00, 0x52, // Close() offset
356     0x05, 0x2e, 0x53, 0x6f, 0x6e, 0x79, // ".Sony"
357    
358     // Open()
359     M68K_EMUL_OP_SONY_OPEN >> 8, M68K_EMUL_OP_SONY_OPEN & 0xff,
360     0x4e, 0x75, // rts
361    
362     // Prime()
363     M68K_EMUL_OP_SONY_PRIME >> 8, M68K_EMUL_OP_SONY_PRIME & 0xff,
364     0x60, 0x0e, // bra IOReturn
365    
366     // Control()
367     M68K_EMUL_OP_SONY_CONTROL >> 8, M68K_EMUL_OP_SONY_CONTROL & 0xff,
368     0x0c, 0x68, 0x00, 0x01, 0x00, 0x1a, // cmp.w #1,$1a(a0)
369     0x66, 0x04, // bne IOReturn
370     0x4e, 0x75, // rts
371    
372     // Status()
373     M68K_EMUL_OP_SONY_STATUS >> 8, M68K_EMUL_OP_SONY_STATUS & 0xff,
374    
375     // IOReturn
376     0x32, 0x28, 0x00, 0x06, // move.w 6(a0),d1
377     0x08, 0x01, 0x00, 0x09, // btst #9,d1
378     0x67, 0x0c, // beq 1
379     0x4a, 0x40, // tst.w d0
380     0x6f, 0x02, // ble 2
381     0x42, 0x40, // clr.w d0
382     0x31, 0x40, 0x00, 0x10, //2 move.w d0,$10(a0)
383     0x4e, 0x75, // rts
384     0x4a, 0x40, //1 tst.w d0
385     0x6f, 0x04, // ble 3
386     0x42, 0x40, // clr.w d0
387     0x4e, 0x75, // rts
388     0x2f, 0x38, 0x08, 0xfc, //3 move.l $8fc,-(sp)
389     0x4e, 0x75, // rts
390    
391     // Close()
392     0x70, 0xe8, // moveq #-24,d0
393     0x4e, 0x75 // rts
394     };
395    
396     static const uint8 disk_driver[] = { // Generic disk driver
397     // Driver header
398     DiskDriverFlags >> 8, DiskDriverFlags & 0xff, 0, 0, 0, 0, 0, 0,
399     0x00, 0x18, // Open() offset
400     0x00, 0x1c, // Prime() offset
401     0x00, 0x20, // Control() offset
402     0x00, 0x2c, // Status() offset
403     0x00, 0x52, // Close() offset
404     0x05, 0x2e, 0x44, 0x69, 0x73, 0x6b, // ".Disk"
405    
406     // Open()
407     M68K_EMUL_OP_DISK_OPEN >> 8, M68K_EMUL_OP_DISK_OPEN & 0xff,
408     0x4e, 0x75, // rts
409    
410     // Prime()
411     M68K_EMUL_OP_DISK_PRIME >> 8, M68K_EMUL_OP_DISK_PRIME & 0xff,
412     0x60, 0x0e, // bra IOReturn
413    
414     // Control()
415     M68K_EMUL_OP_DISK_CONTROL >> 8, M68K_EMUL_OP_DISK_CONTROL & 0xff,
416     0x0c, 0x68, 0x00, 0x01, 0x00, 0x1a, // cmp.w #1,$1a(a0)
417     0x66, 0x04, // bne IOReturn
418     0x4e, 0x75, // rts
419    
420     // Status()
421     M68K_EMUL_OP_DISK_STATUS >> 8, M68K_EMUL_OP_DISK_STATUS & 0xff,
422    
423     // IOReturn
424     0x32, 0x28, 0x00, 0x06, // move.w 6(a0),d1
425     0x08, 0x01, 0x00, 0x09, // btst #9,d1
426     0x67, 0x0c, // beq 1
427     0x4a, 0x40, // tst.w d0
428     0x6f, 0x02, // ble 2
429     0x42, 0x40, // clr.w d0
430     0x31, 0x40, 0x00, 0x10, //2 move.w d0,$10(a0)
431     0x4e, 0x75, // rts
432     0x4a, 0x40, //1 tst.w d0
433     0x6f, 0x04, // ble 3
434     0x42, 0x40, // clr.w d0
435     0x4e, 0x75, // rts
436     0x2f, 0x38, 0x08, 0xfc, //3 move.l $8fc,-(sp)
437     0x4e, 0x75, // rts
438    
439     // Close()
440     0x70, 0xe8, // moveq #-24,d0
441     0x4e, 0x75 // rts
442     };
443    
444     static const uint8 cdrom_driver[] = { // CD-ROM driver
445     // Driver header
446     CDROMDriverFlags >> 8, CDROMDriverFlags & 0xff, 0, 0, 0, 0, 0, 0,
447     0x00, 0x1c, // Open() offset
448     0x00, 0x20, // Prime() offset
449     0x00, 0x24, // Control() offset
450     0x00, 0x30, // Status() offset
451     0x00, 0x56, // Close() offset
452     0x08, 0x2e, 0x41, 0x70, 0x70, 0x6c, 0x65, 0x43, 0x44, 0x00, // ".AppleCD"
453    
454     // Open()
455     M68K_EMUL_OP_CDROM_OPEN >> 8, M68K_EMUL_OP_CDROM_OPEN & 0xff,
456     0x4e, 0x75, // rts
457    
458     // Prime()
459     M68K_EMUL_OP_CDROM_PRIME >> 8, M68K_EMUL_OP_CDROM_PRIME & 0xff,
460     0x60, 0x0e, // bra IOReturn
461    
462     // Control()
463     M68K_EMUL_OP_CDROM_CONTROL >> 8, M68K_EMUL_OP_CDROM_CONTROL & 0xff,
464     0x0c, 0x68, 0x00, 0x01, 0x00, 0x1a, // cmp.w #1,$1a(a0)
465     0x66, 0x04, // bne IOReturn
466     0x4e, 0x75, // rts
467    
468     // Status()
469     M68K_EMUL_OP_CDROM_STATUS >> 8, M68K_EMUL_OP_CDROM_STATUS & 0xff,
470    
471     // IOReturn
472     0x32, 0x28, 0x00, 0x06, // move.w 6(a0),d1
473     0x08, 0x01, 0x00, 0x09, // btst #9,d1
474     0x67, 0x0c, // beq 1
475     0x4a, 0x40, // tst.w d0
476     0x6f, 0x02, // ble 2
477     0x42, 0x40, // clr.w d0
478     0x31, 0x40, 0x00, 0x10, //2 move.w d0,$10(a0)
479     0x4e, 0x75, // rts
480     0x4a, 0x40, //1 tst.w d0
481     0x6f, 0x04, // ble 3
482     0x42, 0x40, // clr.w d0
483     0x4e, 0x75, // rts
484     0x2f, 0x38, 0x08, 0xfc, //3 move.l $8fc,-(sp)
485     0x4e, 0x75, // rts
486    
487     // Close()
488     0x70, 0xe8, // moveq #-24,d0
489     0x4e, 0x75 // rts
490     };
491    
492 gbeauche 1.18 static uint32 long_ptr;
493    
494     static void SetLongBase(uint32 addr)
495     {
496     long_ptr = addr;
497     }
498    
499     static void Long(uint32 value)
500     {
501     WriteMacInt32(long_ptr, value);
502     long_ptr += 4;
503     }
504    
505     static void gen_ain_driver(uintptr addr)
506     {
507     SetLongBase(addr);
508 cebix 1.1
509 gbeauche 1.18 // .AIn driver header
510     Long(0x4d000000); Long(0x00000000);
511     Long(0x00200040); Long(0x00600080);
512     Long(0x00a0042e); Long(0x41496e00);
513     Long(0x00000000); Long(0x00000000);
514     Long(0xaafe0700); Long(0x00000000);
515     Long(0x00000000); Long(0x00179822);
516     Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_NOTHING));
517     Long(0x00000000); Long(0x00000000);
518     Long(0xaafe0700); Long(0x00000000);
519     Long(0x00000000); Long(0x00179822);
520     Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_PRIME_IN));
521     Long(0x00000000); Long(0x00000000);
522     Long(0xaafe0700); Long(0x00000000);
523     Long(0x00000000); Long(0x00179822);
524     Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_CONTROL));
525     Long(0x00000000); Long(0x00000000);
526     Long(0xaafe0700); Long(0x00000000);
527     Long(0x00000000); Long(0x00179822);
528     Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_STATUS));
529     Long(0x00000000); Long(0x00000000);
530     Long(0xaafe0700); Long(0x00000000);
531     Long(0x00000000); Long(0x00179822);
532     Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_NOTHING));
533     Long(0x00000000); Long(0x00000000);
534 cebix 1.1 };
535    
536 gbeauche 1.18 static void gen_aout_driver(uintptr addr)
537     {
538     SetLongBase(addr);
539    
540     // .AOut driver header
541     Long(0x4d000000); Long(0x00000000);
542     Long(0x00200040); Long(0x00600080);
543     Long(0x00a0052e); Long(0x414f7574);
544     Long(0x00000000); Long(0x00000000);
545     Long(0xaafe0700); Long(0x00000000);
546     Long(0x00000000); Long(0x00179822);
547     Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_OPEN));
548     Long(0x00000000); Long(0x00000000);
549     Long(0xaafe0700); Long(0x00000000);
550     Long(0x00000000); Long(0x00179822);
551     Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_PRIME_OUT));
552     Long(0x00000000); Long(0x00000000);
553     Long(0xaafe0700); Long(0x00000000);
554     Long(0x00000000); Long(0x00179822);
555     Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_CONTROL));
556     Long(0x00000000); Long(0x00000000);
557     Long(0xaafe0700); Long(0x00000000);
558     Long(0x00000000); Long(0x00179822);
559     Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_STATUS));
560     Long(0x00000000); Long(0x00000000);
561     Long(0xaafe0700); Long(0x00000000);
562     Long(0x00000000); Long(0x00179822);
563     Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_CLOSE));
564     Long(0x00000000); Long(0x00000000);
565 cebix 1.1 };
566    
567 gbeauche 1.18 static void gen_bin_driver(uintptr addr)
568     {
569     SetLongBase(addr);
570    
571     // .BIn driver header
572     Long(0x4d000000); Long(0x00000000);
573     Long(0x00200040); Long(0x00600080);
574     Long(0x00a0042e); Long(0x42496e00);
575     Long(0x00000000); Long(0x00000000);
576     Long(0xaafe0700); Long(0x00000000);
577     Long(0x00000000); Long(0x00179822);
578     Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_NOTHING));
579     Long(0x00000000); Long(0x00000000);
580     Long(0xaafe0700); Long(0x00000000);
581     Long(0x00000000); Long(0x00179822);
582     Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_PRIME_IN));
583     Long(0x00000000); Long(0x00000000);
584     Long(0xaafe0700); Long(0x00000000);
585     Long(0x00000000); Long(0x00179822);
586     Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_CONTROL));
587     Long(0x00000000); Long(0x00000000);
588     Long(0xaafe0700); Long(0x00000000);
589     Long(0x00000000); Long(0x00179822);
590     Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_STATUS));
591     Long(0x00000000); Long(0x00000000);
592     Long(0xaafe0700); Long(0x00000000);
593     Long(0x00000000); Long(0x00179822);
594     Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_NOTHING));
595     Long(0x00000000); Long(0x00000000);
596 cebix 1.1 };
597    
598 gbeauche 1.18 static void gen_bout_driver(uintptr addr)
599     {
600     SetLongBase(addr);
601    
602     // .BOut driver header
603     Long(0x4d000000); Long(0x00000000);
604     Long(0x00200040); Long(0x00600080);
605     Long(0x00a0052e); Long(0x424f7574);
606     Long(0x00000000); Long(0x00000000);
607     Long(0xaafe0700); Long(0x00000000);
608     Long(0x00000000); Long(0x00179822);
609     Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_OPEN));
610     Long(0x00000000); Long(0x00000000);
611     Long(0xaafe0700); Long(0x00000000);
612     Long(0x00000000); Long(0x00179822);
613     Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_PRIME_OUT));
614     Long(0x00000000); Long(0x00000000);
615     Long(0xaafe0700); Long(0x00000000);
616     Long(0x00000000); Long(0x00179822);
617     Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_CONTROL));
618     Long(0x00000000); Long(0x00000000);
619     Long(0xaafe0700); Long(0x00000000);
620     Long(0x00000000); Long(0x00179822);
621     Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_STATUS));
622     Long(0x00000000); Long(0x00000000);
623     Long(0xaafe0700); Long(0x00000000);
624     Long(0x00000000); Long(0x00179822);
625     Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_CLOSE));
626     Long(0x00000000); Long(0x00000000);
627 cebix 1.1 };
628    
629     static const uint8 adbop_patch[] = { // Call ADBOp() completion procedure
630     // The completion procedure may call ADBOp() again!
631     0x40, 0xe7, // move sr,-(sp)
632     0x00, 0x7c, 0x07, 0x00, // ori #$0700,sr
633     M68K_EMUL_OP_ADBOP >> 8, M68K_EMUL_OP_ADBOP & 0xff,
634     0x48, 0xe7, 0x70, 0xf0, // movem.l d1-d3/a0-a3,-(sp)
635     0x26, 0x48, // move.l a0,a3
636     0x4a, 0xab, 0x00, 0x04, // tst.l 4(a3)
637     0x67, 0x00, 0x00, 0x18, // beq 1
638     0x20, 0x53, // move.l (a3),a0
639     0x22, 0x6b, 0x00, 0x04, // move.l 4(a3),a1
640     0x24, 0x6b, 0x00, 0x08, // move.l 8(a3),a2
641     0x26, 0x78, 0x0c, 0xf8, // move.l $cf8,a3
642     0x4e, 0x91, // jsr (a1)
643     0x70, 0x00, // moveq #0,d0
644     0x60, 0x00, 0x00, 0x04, // bra 2
645     0x70, 0xff, //1 moveq #-1,d0
646     0x4c, 0xdf, 0x0f, 0x0e, //2 movem.l (sp)+,d1-d3/a0-a3
647     0x46, 0xdf, // move (sp)+,sr
648     0x4e, 0x75 // rts
649     };
650    
651    
652     /*
653 gbeauche 1.9 * Copy PowerPC code to ROM image and reverse bytes if necessary
654     */
655    
656     static inline void memcpy_powerpc_code(void *dst, const void *src, size_t len)
657     {
658     #ifdef WORDS_BIGENDIAN
659     (void)memcpy(dst, src, len);
660     #else
661     uint32 *d = (uint32 *)dst;
662     uint32 *s = (uint32 *)src;
663     for (int i = 0; i < len/4; i++)
664     d[i] = htonl(s[i]);
665     #endif
666     }
667    
668    
669     /*
670 cebix 1.1 * Install ROM patches (RAMBase and KernelDataAddr must be set)
671     */
672    
673     bool PatchROM(void)
674     {
675     // Print ROM info
676     D(bug("Checksum: %08lx\n", ntohl(*(uint32 *)ROM_BASE)));
677     D(bug("Version: %04x\n", ntohs(*(uint16 *)(ROM_BASE + 8))));
678     D(bug("Sub Version: %04x\n", ntohs(*(uint16 *)(ROM_BASE + 18))));
679     D(bug("Nanokernel ID: %s\n", (char *)ROM_BASE + 0x30d064));
680     D(bug("Resource Map at %08lx\n", ntohl(*(uint32 *)(ROM_BASE + 26))));
681     D(bug("Trap Tables at %08lx\n\n", ntohl(*(uint32 *)(ROM_BASE + 34))));
682    
683     // Detect ROM type
684     if (!memcmp((void *)(ROM_BASE + 0x30d064), "Boot TNT", 8))
685     ROMType = ROMTYPE_TNT;
686     else if (!memcmp((void *)(ROM_BASE + 0x30d064), "Boot Alchemy", 12))
687     ROMType = ROMTYPE_ALCHEMY;
688     else if (!memcmp((void *)(ROM_BASE + 0x30d064), "Boot Zanzibar", 13))
689     ROMType = ROMTYPE_ZANZIBAR;
690     else if (!memcmp((void *)(ROM_BASE + 0x30d064), "Boot Gazelle", 12))
691     ROMType = ROMTYPE_GAZELLE;
692 gbeauche 1.11 else if (!memcmp((void *)(ROM_BASE + 0x30d064), "Boot Gossamer", 13))
693     ROMType = ROMTYPE_GOSSAMER;
694 cebix 1.1 else if (!memcmp((void *)(ROM_BASE + 0x30d064), "NewWorld", 8))
695     ROMType = ROMTYPE_NEWWORLD;
696     else
697     return false;
698    
699 gbeauche 1.14 // Check that other ROM addresses point to really free regions
700     if (ntohl(*(uint32 *)(ROM_BASE + CHECK_LOAD_PATCH_SPACE)) != 0x6b636b63)
701     return false;
702     if (ntohl(*(uint32 *)(ROM_BASE + PUT_SCRAP_PATCH_SPACE)) != 0x6b636b63)
703     return false;
704     if (ntohl(*(uint32 *)(ROM_BASE + GET_SCRAP_PATCH_SPACE)) != 0x6b636b63)
705     return false;
706     if (ntohl(*(uint32 *)(ROM_BASE + ADDR_MAP_PATCH_SPACE)) != 0x6b636b63)
707     return false;
708    
709 cebix 1.1 // Apply patches
710     if (!patch_nanokernel_boot()) return false;
711     if (!patch_68k_emul()) return false;
712     if (!patch_nanokernel()) return false;
713     if (!patch_68k()) return false;
714    
715     #ifdef M68K_BREAK_POINT
716     // Install 68k breakpoint
717     uint16 *wp = (uint16 *)(ROM_BASE + M68K_BREAK_POINT);
718     *wp++ = htons(M68K_EMUL_BREAK);
719     *wp = htons(M68K_EMUL_RETURN);
720     #endif
721    
722     #ifdef POWERPC_BREAK_POINT
723     // Install PowerPC breakpoint
724     uint32 *lp = (uint32 *)(ROM_BASE + POWERPC_BREAK_POINT);
725     *lp = htonl(0);
726     #endif
727    
728     // Copy 68k emulator to 2MB boundary
729     memcpy((void *)(ROM_BASE + ROM_SIZE), (void *)(ROM_BASE + ROM_SIZE - 0x100000), 0x100000);
730     return true;
731     }
732    
733    
734     /*
735     * Nanokernel boot routine patches
736     */
737    
738     static bool patch_nanokernel_boot(void)
739     {
740     uint32 *lp;
741 gbeauche 1.20 uint32 base, loc;
742 cebix 1.1
743     // ROM boot structure patches
744     lp = (uint32 *)(ROM_BASE + 0x30d000);
745     lp[0x9c >> 2] = htonl(KernelDataAddr); // LA_InfoRecord
746     lp[0xa0 >> 2] = htonl(KernelDataAddr); // LA_KernelData
747     lp[0xa4 >> 2] = htonl(KernelDataAddr + 0x1000); // LA_EmulatorData
748     lp[0xa8 >> 2] = htonl(ROM_BASE + 0x480000); // LA_DispatchTable
749     lp[0xac >> 2] = htonl(ROM_BASE + 0x460000); // LA_EmulatorCode
750     lp[0x360 >> 2] = htonl(0); // Physical RAM base (? on NewWorld ROM, this contains -1)
751     lp[0xfd8 >> 2] = htonl(ROM_BASE + 0x2a); // 68k reset vector
752    
753     // Skip SR/BAT/SDR init
754 gbeauche 1.20 loc = 0x310000;
755 gbeauche 1.11 if (ROMType == ROMTYPE_GAZELLE || ROMType == ROMTYPE_GOSSAMER || ROMType == ROMTYPE_NEWWORLD) {
756 gbeauche 1.20 lp = (uint32 *)(ROM_BASE + loc);
757 cebix 1.1 *lp++ = htonl(POWERPC_NOP);
758     *lp = htonl(0x38000000);
759     }
760 gbeauche 1.20 static const uint8 sr_init_dat[] = {0x35, 0x4a, 0xff, 0xfc, 0x7d, 0x86, 0x50, 0x2e};
761     if ((base = find_rom_data(0x3101b0, 0x3105b0, sr_init_dat, sizeof(sr_init_dat))) == 0) return false;
762     D(bug("sr_init %08lx\n", base));
763     lp = (uint32 *)(ROM_BASE + loc + 8);
764     *lp = htonl(0x48000000 | ((base - loc - 8) & 0x3fffffc)); // b ROM_BASE+0x3101b0
765     lp = (uint32 *)(ROM_BASE + base);
766 cebix 1.1 *lp++ = htonl(0x80200000 + XLM_KERNEL_DATA); // lwz r1,(pointer to Kernel Data)
767     *lp++ = htonl(0x3da0dead); // lis r13,0xdead (start of kernel memory)
768     *lp++ = htonl(0x3dc00010); // lis r14,0x0010 (size of page table)
769     *lp = htonl(0x3de00010); // lis r15,0x0010 (size of kernel memory)
770    
771     // Don't read PVR
772 gbeauche 1.20 static const uint8 pvr_read_dat[] = {0x7d, 0x9f, 0x42, 0xa6};
773     if ((base = find_rom_data(0x3103b0, 0x3108b0, pvr_read_dat, sizeof(pvr_read_dat))) == 0) return false;
774     D(bug("pvr_read %08lx\n", base));
775     lp = (uint32 *)(ROM_BASE + base);
776 cebix 1.1 *lp = htonl(0x81800000 + XLM_PVR); // lwz r12,(theoretical PVR)
777    
778     // Set CPU specific data (even if ROM doesn't have support for that CPU)
779     if (ntohl(lp[6]) != 0x2c0c0001)
780     return false;
781     uint32 ofs = ntohl(lp[7]) & 0xffff;
782     D(bug("ofs %08lx\n", ofs));
783     lp[8] = htonl((ntohl(lp[8]) & 0xffff) | 0x48000000); // beq -> b
784 gbeauche 1.20 loc = (ntohl(lp[8]) & 0xffff) + (uint32)(lp+8) - ROM_BASE;
785 cebix 1.1 D(bug("loc %08lx\n", loc));
786     lp = (uint32 *)(ROM_BASE + ofs + 0x310000);
787     switch (PVR >> 16) {
788     case 1: // 601
789     lp[0] = htonl(0x1000); // Page size
790     lp[1] = htonl(0x8000); // Data cache size
791     lp[2] = htonl(0x8000); // Inst cache size
792     lp[3] = htonl(0x00200020); // Coherency block size/Reservation granule size
793     lp[4] = htonl(0x00010040); // Unified caches/Inst cache line size
794     lp[5] = htonl(0x00400020); // Data cache line size/Data cache block size touch
795     lp[6] = htonl(0x00200020); // Inst cache block size/Data cache block size
796     lp[7] = htonl(0x00080008); // Inst cache assoc/Data cache assoc
797     lp[8] = htonl(0x01000002); // TLB total size/TLB assoc
798     break;
799     case 3: // 603
800     lp[0] = htonl(0x1000); // Page size
801     lp[1] = htonl(0x2000); // Data cache size
802     lp[2] = htonl(0x2000); // Inst cache size
803     lp[3] = htonl(0x00200020); // Coherency block size/Reservation granule size
804     lp[4] = htonl(0x00000020); // Unified caches/Inst cache line size
805     lp[5] = htonl(0x00200020); // Data cache line size/Data cache block size touch
806     lp[6] = htonl(0x00200020); // Inst cache block size/Data cache block size
807     lp[7] = htonl(0x00020002); // Inst cache assoc/Data cache assoc
808     lp[8] = htonl(0x00400002); // TLB total size/TLB assoc
809     break;
810     case 4: // 604
811     lp[0] = htonl(0x1000); // Page size
812     lp[1] = htonl(0x4000); // Data cache size
813     lp[2] = htonl(0x4000); // Inst cache size
814     lp[3] = htonl(0x00200020); // Coherency block size/Reservation granule size
815     lp[4] = htonl(0x00000020); // Unified caches/Inst cache line size
816     lp[5] = htonl(0x00200020); // Data cache line size/Data cache block size touch
817     lp[6] = htonl(0x00200020); // Inst cache block size/Data cache block size
818     lp[7] = htonl(0x00040004); // Inst cache assoc/Data cache assoc
819     lp[8] = htonl(0x00800002); // TLB total size/TLB assoc
820     break;
821     // case 5: // 740?
822     case 6: // 603e
823     case 7: // 603ev
824     lp[0] = htonl(0x1000); // Page size
825     lp[1] = htonl(0x4000); // Data cache size
826     lp[2] = htonl(0x4000); // Inst cache size
827     lp[3] = htonl(0x00200020); // Coherency block size/Reservation granule size
828     lp[4] = htonl(0x00000020); // Unified caches/Inst cache line size
829     lp[5] = htonl(0x00200020); // Data cache line size/Data cache block size touch
830     lp[6] = htonl(0x00200020); // Inst cache block size/Data cache block size
831     lp[7] = htonl(0x00040004); // Inst cache assoc/Data cache assoc
832     lp[8] = htonl(0x00400002); // TLB total size/TLB assoc
833     break;
834     case 8: // 750
835     lp[0] = htonl(0x1000); // Page size
836     lp[1] = htonl(0x8000); // Data cache size
837     lp[2] = htonl(0x8000); // Inst cache size
838     lp[3] = htonl(0x00200020); // Coherency block size/Reservation granule size
839     lp[4] = htonl(0x00000020); // Unified caches/Inst cache line size
840     lp[5] = htonl(0x00200020); // Data cache line size/Data cache block size touch
841     lp[6] = htonl(0x00200020); // Inst cache block size/Data cache block size
842     lp[7] = htonl(0x00080008); // Inst cache assoc/Data cache assoc
843     lp[8] = htonl(0x00800002); // TLB total size/TLB assoc
844     break;
845     case 9: // 604e
846     case 10: // 604ev5
847     lp[0] = htonl(0x1000); // Page size
848     lp[1] = htonl(0x8000); // Data cache size
849     lp[2] = htonl(0x8000); // Inst cache size
850     lp[3] = htonl(0x00200020); // Coherency block size/Reservation granule size
851     lp[4] = htonl(0x00000020); // Unified caches/Inst cache line size
852     lp[5] = htonl(0x00200020); // Data cache line size/Data cache block size touch
853     lp[6] = htonl(0x00200020); // Inst cache block size/Data cache block size
854     lp[7] = htonl(0x00040004); // Inst cache assoc/Data cache assoc
855     lp[8] = htonl(0x00800002); // TLB total size/TLB assoc
856     break;
857     // case 11: // X704?
858     case 12: // ???
859     lp[0] = htonl(0x1000); // Page size
860     lp[1] = htonl(0x8000); // Data cache size
861     lp[2] = htonl(0x8000); // Inst cache size
862     lp[3] = htonl(0x00200020); // Coherency block size/Reservation granule size
863     lp[4] = htonl(0x00000020); // Unified caches/Inst cache line size
864     lp[5] = htonl(0x00200020); // Data cache line size/Data cache block size touch
865     lp[6] = htonl(0x00200020); // Inst cache block size/Data cache block size
866     lp[7] = htonl(0x00080008); // Inst cache assoc/Data cache assoc
867     lp[8] = htonl(0x00800002); // TLB total size/TLB assoc
868     break;
869     case 13: // ???
870     lp[0] = htonl(0x1000); // Page size
871     lp[1] = htonl(0x8000); // Data cache size
872     lp[2] = htonl(0x8000); // Inst cache size
873     lp[3] = htonl(0x00200020); // Coherency block size/Reservation granule size
874     lp[4] = htonl(0x00000020); // Unified caches/Inst cache line size
875     lp[5] = htonl(0x00200020); // Data cache line size/Data cache block size touch
876     lp[6] = htonl(0x00200020); // Inst cache block size/Data cache block size
877     lp[7] = htonl(0x00080008); // Inst cache assoc/Data cache assoc
878     lp[8] = htonl(0x01000004); // TLB total size/TLB assoc
879     break;
880     // case 50: // 821
881     // case 80: // 860
882     case 96: // ???
883     lp[0] = htonl(0x1000); // Page size
884     lp[1] = htonl(0x8000); // Data cache size
885     lp[2] = htonl(0x8000); // Inst cache size
886     lp[3] = htonl(0x00200020); // Coherency block size/Reservation granule size
887     lp[4] = htonl(0x00010020); // Unified caches/Inst cache line size
888     lp[5] = htonl(0x00200020); // Data cache line size/Data cache block size touch
889     lp[6] = htonl(0x00200020); // Inst cache block size/Data cache block size
890     lp[7] = htonl(0x00080008); // Inst cache assoc/Data cache assoc
891     lp[8] = htonl(0x00800004); // TLB total size/TLB assoc
892     break;
893     default:
894     printf("WARNING: Unknown CPU type\n");
895     break;
896     }
897    
898     // Don't set SPRG3, don't test MQ
899 gbeauche 1.20 static const uint8 sprg3_mq_dat[] = {0x7d, 0x13, 0x43, 0xa6, 0x3d, 0x00, 0x00, 0x04, 0x7d, 0x00, 0x03, 0xa6, 0x39, 0x00, 0x00, 0x00, 0x7d, 0x00, 0x02, 0xa6};
900     if ((base = find_rom_data(loc + 0x20, loc + 0x60, sprg3_mq_dat, sizeof(sprg3_mq_dat))) == 0) return false;
901     D(bug("sprg3/mq %08lx\n", base));
902     lp = (uint32 *)(ROM_BASE + base);
903     lp[0] = htonl(POWERPC_NOP);
904     lp[2] = htonl(POWERPC_NOP);
905     lp[4] = htonl(POWERPC_NOP);
906 cebix 1.1
907     // Don't read MSR
908 gbeauche 1.20 static const uint8 msr_dat[] = {0x7d, 0xc0, 0x00, 0xa6};
909     if ((base = find_rom_data(loc + 0x40, loc + 0x80, msr_dat, sizeof(msr_dat))) == 0) return false;
910     D(bug("msr %08lx\n", base));
911     lp = (uint32 *)(ROM_BASE + base);
912 cebix 1.1 *lp = htonl(0x39c00000); // li r14,0
913    
914     // Don't write to DEC
915     lp = (uint32 *)(ROM_BASE + loc + 0x70);
916     *lp++ = htonl(POWERPC_NOP);
917     loc = (ntohl(lp[0]) & 0xffff) + (uint32)lp - ROM_BASE;
918     D(bug("loc %08lx\n", loc));
919    
920     // Don't set SPRG3
921 gbeauche 1.20 static const uint8 sprg3_dat[] = {0x39, 0x21, 0x03, 0x60, 0x7d, 0x33, 0x43, 0xa6, 0x39, 0x01, 0x04, 0x20};
922     if ((base = find_rom_data(0x310000, 0x314000, sprg3_dat, sizeof(sprg3_dat))) == 0) return false;
923     D(bug("sprg3 %08lx\n", base + 4));
924     lp = (uint32 *)(ROM_BASE + base + 4);
925 cebix 1.1 *lp = htonl(POWERPC_NOP);
926    
927     // Don't read PVR
928 gbeauche 1.20 static const uint8 pvr_read2_dat[] = {0x7e, 0xff, 0x42, 0xa6, 0x56, 0xf7, 0x84, 0x3e};
929     if ((base = find_rom_data(0x310000, 0x320000, pvr_read2_dat, sizeof(pvr_read2_dat))) == 0) return false;
930     D(bug("pvr_read2 %08lx\n", base));
931     lp = (uint32 *)(ROM_BASE + base);
932 cebix 1.1 *lp = htonl(0x82e00000 + XLM_PVR); // lwz r23,(theoretical PVR)
933 gbeauche 1.20 if ((base = find_rom_data(base + 4, 0x320000, pvr_read2_dat, sizeof(pvr_read2_dat))) != 0) {
934     D(bug("pvr_read2 %08lx\n", base));
935     lp = (uint32 *)(ROM_BASE + base);
936 cebix 1.1 *lp = htonl(0x82e00000 + XLM_PVR); // lwz r23,(theoretical PVR)
937 gbeauche 1.20 }
938     static const uint8 pvr_read3_dat[] = {0x7e, 0x5f, 0x42, 0xa6, 0x56, 0x52, 0x84, 0x3e};
939     if ((base = find_rom_data(0x310000, 0x320000, pvr_read3_dat, sizeof(pvr_read3_dat))) != 0) {
940     D(bug("pvr_read3 %08lx\n", base));
941     lp = (uint32 *)(ROM_BASE + base);
942 cebix 1.1 *lp = htonl(0x82400000 + XLM_PVR); // lwz r18,(theoretical PVR)
943 gbeauche 1.20 }
944     static const uint8 pvr_read4_dat[] = {0x7d, 0x3f, 0x42, 0xa6, 0x55, 0x29, 0x84, 0x3e};
945     if ((base = find_rom_data(0x310000, 0x320000, pvr_read4_dat, sizeof(pvr_read4_dat))) != 0) {
946     D(bug("pvr_read4 %08lx\n", base));
947     lp = (uint32 *)(ROM_BASE + base);
948 gbeauche 1.4 *lp = htonl(0x81200000 + XLM_PVR); // lzw r9,(theoritical PVR)
949 gbeauche 1.20 }
950 cebix 1.1
951     // Don't read SDR1
952 gbeauche 1.20 static const uint8 sdr1_read_dat[] = {0x7d, 0x19, 0x02, 0xa6, 0x55, 0x16, 0x81, 0xde};
953     if ((base = find_rom_data(0x310000, 0x320000, sdr1_read_dat, sizeof(sdr1_read_dat))) == 0) return false;
954     D(bug("sdr1_read %08lx\n", base));
955     lp = (uint32 *)(ROM_BASE + base);
956 cebix 1.1 *lp++ = htonl(0x3d00dead); // lis r8,0xdead (pointer to page table)
957     *lp++ = htonl(0x3ec0001f); // lis r22,0x001f (size of page table)
958     *lp = htonl(POWERPC_NOP);
959    
960 gbeauche 1.20 // Don't clear page table, don't invalidate TLB
961     static const uint8 pgtb_clear_dat[] = {0x36, 0xd6, 0xff, 0xfc, 0x7e, 0xe8, 0xb1, 0x2e, 0x41, 0x81, 0xff, 0xf8};
962     if ((base = find_rom_data(0x310000, 0x320000, pgtb_clear_dat, sizeof(pgtb_clear_dat))) == 0) return false;
963     D(bug("pgtb_clear %08lx\n", base + 4));
964     lp = (uint32 *)(ROM_BASE + base + 4);
965 cebix 1.1 *lp = htonl(POWERPC_NOP);
966 gbeauche 1.20 D(bug("tblie %08lx\n", base + 12));
967     lp = (uint32 *)(ROM_BASE + base + 12);
968 cebix 1.1 *lp = htonl(POWERPC_NOP);
969    
970     // Don't create RAM descriptor table
971 gbeauche 1.20 static const uint8 desc_create_dat[] = {0x97, 0xfd, 0x00, 0x04, 0x3b, 0xff, 0x10, 0x00, 0x4b, 0xff, 0xff, 0xdc};
972     if ((base = find_rom_data(0x310000, 0x320000, desc_create_dat, sizeof(desc_create_dat))) == 0) return false;
973     D(bug("desc_create %08lx\n", base))
974     lp = (uint32 *)(ROM_BASE + base);
975 cebix 1.1 *lp = htonl(POWERPC_NOP);
976    
977     // Don't load SRs and BATs
978 gbeauche 1.20 static const uint8 sr_load[] = {0x7c, 0x00, 0x04, 0xac, 0x83, 0x9d, 0x00, 0x00, 0x93, 0x81, 0x05, 0xe8};
979     if ((loc = find_rom_data(0x310000, 0x320000, sr_load, sizeof(sr_load))) == 0) return false;
980     static const uint8 sr_load_caller[] = {0x3e, 0xd6, 0xff, 0xff, 0x41, 0x81, 0xff, 0xdc, 0xb2, 0xc8, 0x00, 0x02};
981     if ((base = find_rom_data(0x310000, 0x320000, sr_load_caller, sizeof(sr_load_caller))) == 0) return false;
982     if ((base = find_rom_powerpc_branch(base + 12, 0x320000, loc)) == 0) return false;
983     D(bug("sr_load %08lx, called from %08lx\n", loc, base));
984     lp = (uint32 *)(ROM_BASE + base);
985 cebix 1.1 *lp = htonl(POWERPC_NOP);
986    
987     // Don't mess with SRs
988 gbeauche 1.20 static const uint8 sr_load2_dat[] = {0x83, 0xa1, 0x05, 0xe8, 0x57, 0x7c, 0x3e, 0x78, 0x7f, 0xbd, 0xe0, 0x2e};
989     if ((base = find_rom_data(0x310000, 0x320000, sr_load2_dat, sizeof(sr_load2_dat))) == 0) return false;
990     D(bug("sr_load2 %08lx\n", base));
991     lp = (uint32 *)(ROM_BASE + base);
992 cebix 1.1 *lp = htonl(POWERPC_BLR);
993    
994     // Don't check performance monitor
995 gbeauche 1.20 static const uint8 pm_check_dat[] = {0x7e, 0x58, 0xeb, 0xa6, 0x7e, 0x53, 0x90, 0xf8, 0x7e, 0x78, 0xea, 0xa6};
996     if ((base = find_rom_data(0x310000, 0x320000, pm_check_dat, sizeof(pm_check_dat))) == 0) return false;
997     D(bug("pm_check %08lx\n", base));
998     lp = (uint32 *)(ROM_BASE + base);
999    
1000     static const int spr_check_list[] = {
1001     952 /* mmcr0 */, 953 /* pmc1 */, 954 /* pmc2 */, 955 /* sia */,
1002     956 /* mmcr1 */, 957 /* pmc3 */, 958 /* pmc4 */, 959 /* sda */
1003     };
1004    
1005     for (int i = 0; i < sizeof(spr_check_list)/sizeof(spr_check_list[0]); i++) {
1006     int spr = spr_check_list[i];
1007     uint32 mtspr = 0x7e4003a6 | ((spr & 0x1f) << 16) | ((spr & 0x3e0) << 6);
1008     uint32 mfspr = 0x7e6002a6 | ((spr & 0x1f) << 16) | ((spr & 0x3e0) << 6);
1009     for (int ofs = 0; ofs < 64; ofs++) {
1010     if (ntohl(lp[ofs]) == mtspr) {
1011     if (ntohl(lp[ofs + 2]) != mfspr)
1012     return false;
1013     D(bug(" SPR%d %08lx\n", spr, base + 4*ofs));
1014     lp[ofs] = htonl(POWERPC_NOP);
1015     lp[ofs + 2] = htonl(POWERPC_NOP);
1016     }
1017     }
1018     }
1019 cebix 1.1
1020     // Jump to 68k emulator
1021 gbeauche 1.20 static const uint8 jump68k_dat[] = {0x7d, 0x92, 0x43, 0xa6, 0x7d, 0x5a, 0x03, 0xa6, 0x7d, 0x7b, 0x03, 0xa6};
1022     if ((loc = find_rom_data(0x310000, 0x320000, jump68k_dat, sizeof(jump68k_dat))) == 0) return false;
1023     static const uint8 jump68k_caller_dat[] = {0x85, 0x13, 0x00, 0x08, 0x56, 0xbf, 0x50, 0x3e, 0x63, 0xff, 0x0c, 0x00};
1024     if ((base = find_rom_data(0x310000, 0x320000, jump68k_caller_dat, sizeof(jump68k_caller_dat))) == 0) return false;
1025     if ((base = find_rom_powerpc_branch(base + 12, 0x320000, loc)) == 0) return false;
1026     D(bug("jump68k %08lx, called from %08lx\n", loc, base));
1027     lp = (uint32 *)(ROM_BASE + base);
1028 cebix 1.1 *lp++ = htonl(0x80610634); // lwz r3,0x0634(r1) (pointer to Emulator Data)
1029     *lp++ = htonl(0x8081119c); // lwz r4,0x119c(r1) (pointer to opcode table)
1030     *lp++ = htonl(0x80011184); // lwz r0,0x1184(r1) (pointer to emulator init routine)
1031     *lp++ = htonl(0x7c0903a6); // mtctr r0
1032     *lp = htonl(POWERPC_BCTR);
1033     return true;
1034     }
1035    
1036    
1037     /*
1038     * 68k emulator patches
1039     */
1040    
1041     static bool patch_68k_emul(void)
1042     {
1043     uint32 *lp;
1044     uint32 base;
1045    
1046     // Overwrite twi instructions
1047 gbeauche 1.20 static const uint8 twi_dat[] = {0x0f, 0xff, 0x00, 0x00, 0x0f, 0xff, 0x00, 0x01, 0x0f, 0xff, 0x00, 0x02};
1048     if ((base = find_rom_data(0x36e600, 0x36ea00, twi_dat, sizeof(twi_dat))) == 0) return false;
1049     D(bug("twi %08lx\n", base));
1050 cebix 1.1 lp = (uint32 *)(ROM_BASE + base);
1051     *lp++ = htonl(0x48000000 + 0x36f900 - base); // b 0x36f900 (Emulator start)
1052     *lp++ = htonl(0x48000000 + 0x36fa00 - base - 4); // b 0x36fa00 (Mixed mode)
1053     *lp++ = htonl(0x48000000 + 0x36fb00 - base - 8); // b 0x36fb00 (Reset/FC1E opcode)
1054     *lp++ = htonl(0x48000000 + 0x36fc00 - base - 12); // FE0A opcode
1055     *lp++ = htonl(POWERPC_ILLEGAL); // Interrupt
1056     *lp++ = htonl(POWERPC_ILLEGAL); // ?
1057     *lp++ = htonl(POWERPC_ILLEGAL);
1058     *lp++ = htonl(POWERPC_ILLEGAL);
1059     *lp++ = htonl(POWERPC_ILLEGAL);
1060     *lp++ = htonl(POWERPC_ILLEGAL);
1061     *lp++ = htonl(POWERPC_ILLEGAL);
1062     *lp++ = htonl(POWERPC_ILLEGAL);
1063     *lp++ = htonl(POWERPC_ILLEGAL);
1064     *lp++ = htonl(POWERPC_ILLEGAL);
1065     *lp++ = htonl(POWERPC_ILLEGAL);
1066     *lp = htonl(POWERPC_ILLEGAL);
1067    
1068     #if EMULATED_PPC
1069 gbeauche 1.7 // Install EMUL_RETURN, EXEC_RETURN, EXEC_NATIVE and EMUL_OP opcodes
1070 cebix 1.1 lp = (uint32 *)(ROM_BASE + 0x380000 + (M68K_EMUL_RETURN << 3));
1071     *lp++ = htonl(POWERPC_EMUL_OP);
1072     *lp++ = htonl(0x4bf66e80); // b 0x366084
1073     *lp++ = htonl(POWERPC_EMUL_OP | 1);
1074     *lp++ = htonl(0x4bf66e78); // b 0x366084
1075 gbeauche 1.7 *lp++ = htonl(POWERPC_EMUL_OP | 2);
1076     *lp++ = htonl(0x4bf66e70); // b 0x366084
1077 cebix 1.1 for (int i=0; i<OP_MAX; i++) {
1078 gbeauche 1.7 *lp++ = htonl(POWERPC_EMUL_OP | (i + 3));
1079     *lp++ = htonl(0x4bf66e68 - i*8); // b 0x366084
1080 cebix 1.1 }
1081     #else
1082     // Install EMUL_RETURN, EXEC_RETURN and EMUL_OP opcodes
1083     lp = (uint32 *)(ROM_BASE + 0x380000 + (M68K_EMUL_RETURN << 3));
1084     *lp++ = htonl(0x80000000 + XLM_EMUL_RETURN_PROC); // lwz r0,XLM_EMUL_RETURN_PROC
1085     *lp++ = htonl(0x4bf705fc); // b 0x36f800
1086     *lp++ = htonl(0x80000000 + XLM_EXEC_RETURN_PROC); // lwz r0,XLM_EXEC_RETURN_PROC
1087     *lp++ = htonl(0x4bf705f4); // b 0x36f800
1088 gbeauche 1.7 *lp++ = htonl(0x00dead00); // Let SheepShaver crash, since
1089     *lp++ = htonl(0x00beef00); // no native opcode is available
1090 cebix 1.1 for (int i=0; i<OP_MAX; i++) {
1091     *lp++ = htonl(0x38a00000 + i); // li r5,OP_*
1092 gbeauche 1.7 *lp++ = htonl(0x4bf705ec - i*8); // b 0x36f808
1093 cebix 1.1 }
1094    
1095     // Extra routines for EMUL_RETURN/EXEC_RETURN/EMUL_OP
1096     lp = (uint32 *)(ROM_BASE + 0x36f800);
1097     *lp++ = htonl(0x7c0803a6); // mtlr r0
1098     *lp++ = htonl(0x4e800020); // blr
1099    
1100     *lp++ = htonl(0x80000000 + XLM_EMUL_OP_PROC); // lwz r0,XLM_EMUL_OP_PROC
1101     *lp++ = htonl(0x7c0803a6); // mtlr r0
1102     *lp = htonl(0x4e800020); // blr
1103     #endif
1104    
1105     // Extra routine for 68k emulator start
1106     lp = (uint32 *)(ROM_BASE + 0x36f900);
1107     *lp++ = htonl(0x7c2903a6); // mtctr r1
1108 gbeauche 1.8 #if EMULATED_PPC
1109 gbeauche 1.18 *lp++ = htonl(NativeOpcode(NATIVE_DISABLE_INTERRUPT));
1110 gbeauche 1.8 #else
1111 cebix 1.1 *lp++ = htonl(0x80200000 + XLM_IRQ_NEST); // lwz r1,XLM_IRQ_NEST
1112     *lp++ = htonl(0x38210001); // addi r1,r1,1
1113     *lp++ = htonl(0x90200000 + XLM_IRQ_NEST); // stw r1,XLM_IRQ_NEST
1114 gbeauche 1.8 #endif
1115 cebix 1.1 *lp++ = htonl(0x80200000 + XLM_KERNEL_DATA);// lwz r1,XLM_KERNEL_DATA
1116     *lp++ = htonl(0x90c10018); // stw r6,0x18(r1)
1117     *lp++ = htonl(0x7cc902a6); // mfctr r6
1118     *lp++ = htonl(0x90c10004); // stw r6,$0004(r1)
1119     *lp++ = htonl(0x80c1065c); // lwz r6,$065c(r1)
1120     *lp++ = htonl(0x90e6013c); // stw r7,$013c(r6)
1121     *lp++ = htonl(0x91060144); // stw r8,$0144(r6)
1122     *lp++ = htonl(0x9126014c); // stw r9,$014c(r6)
1123     *lp++ = htonl(0x91460154); // stw r10,$0154(r6)
1124     *lp++ = htonl(0x9166015c); // stw r11,$015c(r6)
1125     *lp++ = htonl(0x91860164); // stw r12,$0164(r6)
1126     *lp++ = htonl(0x91a6016c); // stw r13,$016c(r6)
1127     *lp++ = htonl(0x7da00026); // mfcr r13
1128     *lp++ = htonl(0x80e10660); // lwz r7,$0660(r1)
1129     *lp++ = htonl(0x7d8802a6); // mflr r12
1130     *lp++ = htonl(0x50e74001); // rlwimi. r7,r7,8,$80000000
1131     *lp++ = htonl(0x814105f0); // lwz r10,0x05f0(r1)
1132     *lp++ = htonl(0x7d4803a6); // mtlr r10
1133     *lp++ = htonl(0x7d8a6378); // mr r10,r12
1134     *lp++ = htonl(0x3d600002); // lis r11,0x0002
1135     *lp++ = htonl(0x616bf072); // ori r11,r11,0xf072 (MSR)
1136     *lp++ = htonl(0x50e7deb4); // rlwimi r7,r7,27,$00000020
1137     *lp = htonl(0x4e800020); // blr
1138    
1139     // Extra routine for Mixed Mode
1140     lp = (uint32 *)(ROM_BASE + 0x36fa00);
1141     *lp++ = htonl(0x7c2903a6); // mtctr r1
1142 gbeauche 1.8 #if EMULATED_PPC
1143 gbeauche 1.18 *lp++ = htonl(NativeOpcode(NATIVE_DISABLE_INTERRUPT));
1144 gbeauche 1.8 #else
1145 cebix 1.1 *lp++ = htonl(0x80200000 + XLM_IRQ_NEST); // lwz r1,XLM_IRQ_NEST
1146     *lp++ = htonl(0x38210001); // addi r1,r1,1
1147     *lp++ = htonl(0x90200000 + XLM_IRQ_NEST); // stw r1,XLM_IRQ_NEST
1148 gbeauche 1.8 #endif
1149 cebix 1.1 *lp++ = htonl(0x80200000 + XLM_KERNEL_DATA);// lwz r1,XLM_KERNEL_DATA
1150     *lp++ = htonl(0x90c10018); // stw r6,0x18(r1)
1151     *lp++ = htonl(0x7cc902a6); // mfctr r6
1152     *lp++ = htonl(0x90c10004); // stw r6,$0004(r1)
1153     *lp++ = htonl(0x80c1065c); // lwz r6,$065c(r1)
1154     *lp++ = htonl(0x90e6013c); // stw r7,$013c(r6)
1155     *lp++ = htonl(0x91060144); // stw r8,$0144(r6)
1156     *lp++ = htonl(0x9126014c); // stw r9,$014c(r6)
1157     *lp++ = htonl(0x91460154); // stw r10,$0154(r6)
1158     *lp++ = htonl(0x9166015c); // stw r11,$015c(r6)
1159     *lp++ = htonl(0x91860164); // stw r12,$0164(r6)
1160     *lp++ = htonl(0x91a6016c); // stw r13,$016c(r6)
1161     *lp++ = htonl(0x7da00026); // mfcr r13
1162     *lp++ = htonl(0x80e10660); // lwz r7,$0660(r1)
1163     *lp++ = htonl(0x7d8802a6); // mflr r12
1164     *lp++ = htonl(0x50e74001); // rlwimi. r7,r7,8,$80000000
1165     *lp++ = htonl(0x814105f4); // lwz r10,0x05f4(r1)
1166     *lp++ = htonl(0x7d4803a6); // mtlr r10
1167     *lp++ = htonl(0x7d8a6378); // mr r10,r12
1168     *lp++ = htonl(0x3d600002); // lis r11,0x0002
1169     *lp++ = htonl(0x616bf072); // ori r11,r11,0xf072 (MSR)
1170     *lp++ = htonl(0x50e7deb4); // rlwimi r7,r7,27,$00000020
1171     *lp = htonl(0x4e800020); // blr
1172    
1173     // Extra routine for Reset/FC1E opcode
1174 gbeauche 1.4 lp = (uint32 *)(ROM_BASE + 0x36fb00);
1175 cebix 1.1 *lp++ = htonl(0x7c2903a6); // mtctr r1
1176 gbeauche 1.8 #if EMULATED_PPC
1177 gbeauche 1.18 *lp++ = htonl(NativeOpcode(NATIVE_DISABLE_INTERRUPT));
1178 gbeauche 1.8 #else
1179 cebix 1.1 *lp++ = htonl(0x80200000 + XLM_IRQ_NEST); // lwz r1,XLM_IRQ_NEST
1180     *lp++ = htonl(0x38210001); // addi r1,r1,1
1181     *lp++ = htonl(0x90200000 + XLM_IRQ_NEST); // stw r1,XLM_IRQ_NEST
1182 gbeauche 1.8 #endif
1183 cebix 1.1 *lp++ = htonl(0x80200000 + XLM_KERNEL_DATA);// lwz r1,XLM_KERNEL_DATA
1184     *lp++ = htonl(0x90c10018); // stw r6,0x18(r1)
1185     *lp++ = htonl(0x7cc902a6); // mfctr r6
1186     *lp++ = htonl(0x90c10004); // stw r6,$0004(r1)
1187     *lp++ = htonl(0x80c1065c); // lwz r6,$065c(r1)
1188     *lp++ = htonl(0x90e6013c); // stw r7,$013c(r6)
1189     *lp++ = htonl(0x91060144); // stw r8,$0144(r6)
1190     *lp++ = htonl(0x9126014c); // stw r9,$014c(r6)
1191     *lp++ = htonl(0x91460154); // stw r10,$0154(r6)
1192     *lp++ = htonl(0x9166015c); // stw r11,$015c(r6)
1193     *lp++ = htonl(0x91860164); // stw r12,$0164(r6)
1194     *lp++ = htonl(0x91a6016c); // stw r13,$016c(r6)
1195     *lp++ = htonl(0x7da00026); // mfcr r13
1196     *lp++ = htonl(0x80e10660); // lwz r7,$0660(r1)
1197     *lp++ = htonl(0x7d8802a6); // mflr r12
1198     *lp++ = htonl(0x50e74001); // rlwimi. r7,r7,8,$80000000
1199 gbeauche 1.4 *lp++ = htonl(0x814105f8); // lwz r10,0x05f8(r1)
1200 cebix 1.1 *lp++ = htonl(0x7d4803a6); // mtlr r10
1201     *lp++ = htonl(0x7d8a6378); // mr r10,r12
1202     *lp++ = htonl(0x3d600002); // lis r11,0x0002
1203     *lp++ = htonl(0x616bf072); // ori r11,r11,0xf072 (MSR)
1204     *lp++ = htonl(0x50e7deb4); // rlwimi r7,r7,27,$00000020
1205     *lp = htonl(0x4e800020); // blr
1206    
1207     // Extra routine for FE0A opcode (QuickDraw 3D needs this)
1208     lp = (uint32 *)(ROM_BASE + 0x36fc00);
1209     *lp++ = htonl(0x7c2903a6); // mtctr r1
1210 gbeauche 1.8 #if EMULATED_PPC
1211 gbeauche 1.18 *lp++ = htonl(NativeOpcode(NATIVE_DISABLE_INTERRUPT));
1212 gbeauche 1.8 #else
1213 cebix 1.1 *lp++ = htonl(0x80200000 + XLM_IRQ_NEST); // lwz r1,XLM_IRQ_NEST
1214     *lp++ = htonl(0x38210001); // addi r1,r1,1
1215     *lp++ = htonl(0x90200000 + XLM_IRQ_NEST); // stw r1,XLM_IRQ_NEST
1216 gbeauche 1.8 #endif
1217 cebix 1.1 *lp++ = htonl(0x80200000 + XLM_KERNEL_DATA);// lwz r1,XLM_KERNEL_DATA
1218     *lp++ = htonl(0x90c10018); // stw r6,0x18(r1)
1219     *lp++ = htonl(0x7cc902a6); // mfctr r6
1220     *lp++ = htonl(0x90c10004); // stw r6,$0004(r1)
1221     *lp++ = htonl(0x80c1065c); // lwz r6,$065c(r1)
1222     *lp++ = htonl(0x90e6013c); // stw r7,$013c(r6)
1223     *lp++ = htonl(0x91060144); // stw r8,$0144(r6)
1224     *lp++ = htonl(0x9126014c); // stw r9,$014c(r6)
1225     *lp++ = htonl(0x91460154); // stw r10,$0154(r6)
1226     *lp++ = htonl(0x9166015c); // stw r11,$015c(r6)
1227     *lp++ = htonl(0x91860164); // stw r12,$0164(r6)
1228     *lp++ = htonl(0x91a6016c); // stw r13,$016c(r6)
1229     *lp++ = htonl(0x7da00026); // mfcr r13
1230     *lp++ = htonl(0x80e10660); // lwz r7,$0660(r1)
1231     *lp++ = htonl(0x7d8802a6); // mflr r12
1232     *lp++ = htonl(0x50e74001); // rlwimi. r7,r7,8,$80000000
1233 gbeauche 1.4 *lp++ = htonl(0x814105fc); // lwz r10,0x05fc(r1)
1234 cebix 1.1 *lp++ = htonl(0x7d4803a6); // mtlr r10
1235     *lp++ = htonl(0x7d8a6378); // mr r10,r12
1236     *lp++ = htonl(0x3d600002); // lis r11,0x0002
1237     *lp++ = htonl(0x616bf072); // ori r11,r11,0xf072 (MSR)
1238     *lp++ = htonl(0x50e7deb4); // rlwimi r7,r7,27,$00000020
1239     *lp = htonl(0x4e800020); // blr
1240    
1241     // Patch DR emulator to jump to right address when an interrupt occurs
1242     lp = (uint32 *)(ROM_BASE + 0x370000);
1243     while (lp < (uint32 *)(ROM_BASE + 0x380000)) {
1244     if (ntohl(*lp) == 0x4ca80020) // bclr 5,8
1245     goto dr_found;
1246     lp++;
1247     }
1248     D(bug("DR emulator patch location not found\n"));
1249     return false;
1250     dr_found:
1251     lp++;
1252     *lp = htonl(0x48000000 + 0xf000 - (((uint32)lp - ROM_BASE) & 0xffff)); // b DR_CACHE_BASE+0x1f000
1253     lp = (uint32 *)(ROM_BASE + 0x37f000);
1254     *lp++ = htonl(0x3c000000 + ((ROM_BASE + 0x46d0a4) >> 16)); // lis r0,xxx
1255     *lp++ = htonl(0x60000000 + ((ROM_BASE + 0x46d0a4) & 0xffff)); // ori r0,r0,xxx
1256     *lp++ = htonl(0x7c0903a6); // mtctr r0
1257     *lp = htonl(POWERPC_BCTR); // bctr
1258     return true;
1259     }
1260    
1261    
1262     /*
1263     * Nanokernel patches
1264     */
1265    
1266     static bool patch_nanokernel(void)
1267     {
1268     uint32 *lp;
1269 gbeauche 1.20 uint32 base, loc;
1270 cebix 1.1
1271     // Patch Mixed Mode trap
1272 gbeauche 1.20 static const uint8 virt2phys_dat[] = {0x7d, 0x1b, 0x43, 0x78, 0x3b, 0xa1, 0x03, 0x20};
1273     if ((base = find_rom_data(0x313000, 0x314000, virt2phys_dat, sizeof(virt2phys_dat))) == 0) return false;
1274     D(bug("virt2phys %08lx\n", base + 8));
1275     lp = (uint32 *)(ROM_BASE + base + 8); // Don't translate virtual->physical
1276     lp[0] = htonl(0x7f7fdb78); // mr r31,r27
1277     lp[2] = htonl(POWERPC_NOP);
1278    
1279     static const uint8 ppc_excp_tbl_dat[] = {0x39, 0x01, 0x04, 0x20, 0x7d, 0x13, 0x43, 0xa6};
1280     if ((base = find_rom_data(0x313000, 0x314000, ppc_excp_tbl_dat, sizeof(ppc_excp_tbl_dat))) == 0) return false;
1281     D(bug("ppc_excp_tbl %08lx\n", base));
1282     lp = (uint32 *)(ROM_BASE + base); // Don't activate PPC exception table
1283 cebix 1.1 *lp++ = htonl(0x39000000 + MODE_NATIVE); // li r8,MODE_NATIVE
1284 gbeauche 1.20 *lp = htonl(0x91000000 + XLM_RUN_MODE); // stw r8,XLM_RUN_MODE
1285 cebix 1.1
1286 gbeauche 1.20 static const uint8 save_fpu_dat[] = {0x7d, 0x00, 0x00, 0xa6, 0x61, 0x08, 0x20, 0x00, 0x7d, 0x00, 0x01, 0x24};
1287     if ((base = find_rom_data(0x310000, 0x314000, save_fpu_dat, sizeof(save_fpu_dat))) == 0) return false;
1288     D(bug("save_fpu %08lx\n", base));
1289     lp = (uint32 *)(ROM_BASE + base); // Don't modify MSR to turn on FPU
1290     if (ntohl(lp[4]) != 0x556b04e2) return false;
1291     loc = ROM_BASE + base;
1292     #if 1
1293     // FIXME: is that really intended?
1294 cebix 1.1 *lp++ = htonl(POWERPC_NOP);
1295     lp++;
1296     *lp++ = htonl(POWERPC_NOP);
1297     lp++;
1298     *lp = htonl(POWERPC_NOP);
1299 gbeauche 1.20 #else
1300     lp[0] = htonl(POWERPC_NOP);
1301     lp[1] = htonl(POWERPC_NOP);
1302     lp[2] = htonl(POWERPC_NOP);
1303     lp[3] = htonl(POWERPC_NOP);
1304     #endif
1305 cebix 1.1
1306 gbeauche 1.20 static const uint8 save_fpu_caller_dat[] = {0x93, 0xa6, 0x01, 0xec, 0x93, 0xc6, 0x01, 0xf4, 0x93, 0xe6, 0x01, 0xfc, 0x40};
1307     if ((base = find_rom_data(0x310000, 0x314000, save_fpu_caller_dat, sizeof(save_fpu_caller_dat))) == 0) return false;
1308     D(bug("save_fpu_caller %08lx\n", base + 12));
1309     if (powerpc_branch_target(ROM_BASE + base + 12) != loc) return false;
1310     lp = (uint32 *)(ROM_BASE + base + 12); // Always save FPU state
1311 cebix 1.1 *lp = htonl(0x48000000 | (ntohl(*lp) & 0xffff)); // bl 0x00312e88
1312    
1313 gbeauche 1.20 static const uint8 mdec_dat[] = {0x7f, 0xf6, 0x02, 0xa6, 0x2c, 0x08, 0x00, 0x00, 0x93, 0xe1, 0x06, 0x68, 0x7d, 0x16, 0x03, 0xa6};
1314     if ((base = find_rom_data(0x310000, 0x314000, mdec_dat, sizeof(mdec_dat))) == 0) return false;
1315     D(bug("mdec %08lx\n", base));
1316     lp = (uint32 *)(ROM_BASE + base); // Don't modify DEC
1317     lp[0] = htonl(0x3be00000); // li r31,0
1318 cebix 1.1 #if 1
1319 gbeauche 1.20 lp[3] = htonl(POWERPC_NOP);
1320     lp[4] = htonl(POWERPC_NOP);
1321 cebix 1.1 #else
1322 gbeauche 1.20 lp[3] = htonl(0x39000040); // li r8,0x40
1323     lp[4] = htonl(0x990600e4); // stb r8,0xe4(r6)
1324 cebix 1.1 #endif
1325    
1326 gbeauche 1.20 static const uint8 restore_fpu_caller_dat[] = {0x81, 0x06, 0x00, 0xf4, 0x81, 0x46, 0x00, 0xfc, 0x7d, 0x09, 0x03, 0xa6, 0x40};
1327     if ((base = find_rom_data(0x310000, 0x314000, restore_fpu_caller_dat, sizeof(restore_fpu_caller_dat))) == 0) return false;
1328     D(bug("restore_fpu_caller %08lx\n", base + 12));
1329     lp = (uint32 *)(ROM_BASE + base + 12); // Always restore FPU state
1330 cebix 1.1 *lp = htonl(0x48000000 | (ntohl(*lp) & 0xffff)); // bl 0x00312ddc
1331    
1332 gbeauche 1.20 static const uint8 m68k_excp_tbl_dat[] = {0x81, 0x21, 0x06, 0x58, 0x39, 0x01, 0x03, 0x60, 0x7d, 0x13, 0x43, 0xa6};
1333     if ((base = find_rom_data(0x310000, 0x314000, m68k_excp_tbl_dat, sizeof(m68k_excp_tbl_dat))) == 0) return false;
1334     D(bug("m68k_excp %08lx\n", base + 4));
1335     lp = (uint32 *)(ROM_BASE + base + 4); // Don't activate 68k exception table
1336 cebix 1.1 *lp++ = htonl(0x39000000 + MODE_68K); // li r8,MODE_68K
1337     *lp = htonl(0x91000000 + XLM_RUN_MODE); // stw r8,XLM_RUN_MODE
1338    
1339     // Patch 68k emulator trap routine
1340 gbeauche 1.20 static const uint8 restore_fpu_caller2_dat[] = {0x81, 0x86, 0x00, 0x8c, 0x80, 0x66, 0x00, 0x94, 0x80, 0x86, 0x00, 0x9c, 0x40};
1341     if ((base = find_rom_data(0x310000, 0x314000, restore_fpu_caller2_dat, sizeof(restore_fpu_caller2_dat))) == 0) return false;
1342     D(bug("restore_fpu_caller2 %08lx\n", base + 12));
1343     loc = powerpc_branch_target(ROM_BASE + base + 12) - ROM_BASE;
1344     lp = (uint32 *)(ROM_BASE + base + 12); // Always restore FPU state
1345 cebix 1.1 *lp = htonl(0x48000000 | (ntohl(*lp) & 0xffff)); // bl 0x00312dd4
1346    
1347 gbeauche 1.20 static const uint8 restore_fpu_dat[] = {0x55, 0x68, 0x04, 0xa5, 0x4c, 0x82, 0x00, 0x20, 0x81, 0x06, 0x00, 0xe4};
1348     if ((base = find_rom_data(0x310000, 0x314000, restore_fpu_dat, sizeof(restore_fpu_dat))) == 0) return false;
1349     D(bug("restore_fpu %08lx\n", base));
1350     if (base != loc) return false;
1351     lp = (uint32 *)(ROM_BASE + base + 4); // Don't modify MSR to turn on FPU
1352 cebix 1.1 *lp++ = htonl(POWERPC_NOP);
1353     lp += 2;
1354     *lp++ = htonl(POWERPC_NOP);
1355     lp++;
1356     *lp++ = htonl(POWERPC_NOP);
1357     *lp++ = htonl(POWERPC_NOP);
1358     *lp = htonl(POWERPC_NOP);
1359    
1360     // Patch trap return routine
1361 gbeauche 1.20 static const uint8 trap_return_dat[] = {0x80, 0xc1, 0x00, 0x18, 0x80, 0x21, 0x00, 0x04, 0x4c, 0x00, 0x00, 0x64};
1362     if ((base = find_rom_data(0x312000, 0x320000, trap_return_dat, sizeof(trap_return_dat))) == 0) return false;
1363     D(bug("trap_return %08lx\n", base + 8));
1364     lp = (uint32 *)(ROM_BASE + base + 8); // Replace rfi
1365     *lp = htonl(POWERPC_BCTR);
1366    
1367     while (ntohl(*lp) != 0x7d5a03a6) lp--;
1368 cebix 1.1 *lp++ = htonl(0x7d4903a6); // mtctr r10
1369     *lp++ = htonl(0x7daff120); // mtcr r13
1370 gbeauche 1.20 *lp = htonl(0x48000000 + ((0x318000 - ((uint32)lp - ROM_BASE)) & 0x03fffffc)); // b ROM_BASE+0x318000
1371     uint32 npc = (uint32)(lp + 1) - ROM_BASE;
1372 cebix 1.1
1373     lp = (uint32 *)(ROM_BASE + 0x318000);
1374 gbeauche 1.8 #if EMULATED_PPC
1375 gbeauche 1.18 *lp++ = htonl(NativeOpcode(NATIVE_ENABLE_INTERRUPT));
1376 gbeauche 1.20 *lp = htonl(0x48000000 + ((npc - 0x318004) & 0x03fffffc)); // b ROM_BASE+0x312c2c
1377 gbeauche 1.8 #else
1378 cebix 1.1 *lp++ = htonl(0x81400000 + XLM_IRQ_NEST); // lwz r10,XLM_IRQ_NEST
1379     *lp++ = htonl(0x394affff); // subi r10,r10,1
1380     *lp++ = htonl(0x91400000 + XLM_IRQ_NEST); // stw r10,XLM_IRQ_NEST
1381 gbeauche 1.20 *lp = htonl(0x48000000 + ((npc - 0x31800c) & 0x03fffffc)); // b ROM_BASE+0x312c2c
1382 gbeauche 1.8 #endif
1383    
1384 cebix 1.1 /*
1385     // Disable FE0A/FE06 opcodes
1386     lp = (uint32 *)(ROM_BASE + 0x3144ac);
1387     *lp++ = htonl(POWERPC_NOP);
1388     *lp += 8;
1389     */
1390     return true;
1391     }
1392    
1393    
1394     /*
1395     * 68k boot routine patches
1396     */
1397    
1398     static bool patch_68k(void)
1399     {
1400     uint32 *lp;
1401     uint16 *wp;
1402     uint8 *bp;
1403 gbeauche 1.20 uint32 base, loc;
1404 cebix 1.1
1405     // Remove 68k RESET instruction
1406     static const uint8 reset_dat[] = {0x4e, 0x70};
1407     if ((base = find_rom_data(0xc8, 0x120, reset_dat, sizeof(reset_dat))) == 0) return false;
1408     D(bug("reset %08lx\n", base));
1409     wp = (uint16 *)(ROM_BASE + base);
1410     *wp = htons(M68K_NOP);
1411    
1412     // Fake reading PowerMac ID (via Universal)
1413     static const uint8 powermac_id_dat[] = {0x45, 0xf9, 0x5f, 0xff, 0xff, 0xfc, 0x20, 0x12, 0x72, 0x00};
1414     if ((base = find_rom_data(0xe000, 0x15000, powermac_id_dat, sizeof(powermac_id_dat))) == 0) return false;
1415     D(bug("powermac_id %08lx\n", base));
1416     wp = (uint16 *)(ROM_BASE + base);
1417     *wp++ = htons(0x203c); // move.l #id,d0
1418     *wp++ = htons(0);
1419     // if (ROMType == ROMTYPE_NEWWORLD)
1420     // *wp++ = htons(0x3035); // (PowerMac 9500 ID)
1421     // else
1422     *wp++ = htons(0x3020); // (PowerMac 9500 ID)
1423     *wp++ = htons(0xb040); // cmp.w d0,d0
1424     *wp = htons(0x4ed6); // jmp (a6)
1425    
1426     // Patch UniversalInfo
1427     if (ROMType == ROMTYPE_NEWWORLD) {
1428     static const uint8 univ_info_dat[] = {0x3f, 0xff, 0x04, 0x00};
1429 gbeauche 1.4 if ((base = find_rom_data(0x14000, 0x18000, univ_info_dat, sizeof(univ_info_dat))) == 0) return false;
1430 cebix 1.1 D(bug("universal_info %08lx\n", base));
1431     lp = (uint32 *)(ROM_BASE + base - 0x14);
1432     lp[0x00 >> 2] = htonl(ADDR_MAP_PATCH_SPACE - (base - 0x14));
1433     lp[0x10 >> 2] = htonl(0xcc003d11); // Make it like the PowerMac 9500 UniversalInfo
1434     lp[0x14 >> 2] = htonl(0x3fff0401);
1435     lp[0x18 >> 2] = htonl(0x0300001c);
1436     lp[0x1c >> 2] = htonl(0x000108c4);
1437     lp[0x24 >> 2] = htonl(0xc301bf26);
1438     lp[0x28 >> 2] = htonl(0x00000861);
1439     lp[0x58 >> 2] = htonl(0x30200000);
1440     lp[0x60 >> 2] = htonl(0x0000003d);
1441     } else if (ROMType == ROMTYPE_ZANZIBAR) {
1442     base = 0x12b70;
1443     lp = (uint32 *)(ROM_BASE + base - 0x14);
1444     lp[0x00 >> 2] = htonl(ADDR_MAP_PATCH_SPACE - (base - 0x14));
1445     lp[0x10 >> 2] = htonl(0xcc003d11); // Make it like the PowerMac 9500 UniversalInfo
1446     lp[0x14 >> 2] = htonl(0x3fff0401);
1447     lp[0x18 >> 2] = htonl(0x0300001c);
1448     lp[0x1c >> 2] = htonl(0x000108c4);
1449     lp[0x24 >> 2] = htonl(0xc301bf26);
1450     lp[0x28 >> 2] = htonl(0x00000861);
1451     lp[0x58 >> 2] = htonl(0x30200000);
1452     lp[0x60 >> 2] = htonl(0x0000003d);
1453 gbeauche 1.11 } else if (ROMType == ROMTYPE_GOSSAMER) {
1454     base = 0x12d20;
1455     lp = (uint32 *)(ROM_BASE + base - 0x14);
1456     lp[0x00 >> 2] = htonl(ADDR_MAP_PATCH_SPACE - (base - 0x14));
1457     lp[0x10 >> 2] = htonl(0xcc003d11); // Make it like the PowerMac 9500 UniversalInfo
1458     lp[0x14 >> 2] = htonl(0x3fff0401);
1459     lp[0x18 >> 2] = htonl(0x0300001c);
1460     lp[0x1c >> 2] = htonl(0x000108c4);
1461     lp[0x24 >> 2] = htonl(0xc301bf26);
1462     lp[0x28 >> 2] = htonl(0x00000861);
1463     lp[0x58 >> 2] = htonl(0x30410000);
1464     lp[0x60 >> 2] = htonl(0x0000003d);
1465 cebix 1.1 }
1466    
1467     // Construct AddrMap for NewWorld ROM
1468 gbeauche 1.11 if (ROMType == ROMTYPE_NEWWORLD || ROMType == ROMTYPE_ZANZIBAR || ROMType == ROMTYPE_GOSSAMER) {
1469 cebix 1.1 lp = (uint32 *)(ROM_BASE + ADDR_MAP_PATCH_SPACE);
1470     memset(lp - 10, 0, 0x128);
1471     lp[-10] = htonl(0x0300001c);
1472     lp[-9] = htonl(0x000108c4);
1473     lp[-4] = htonl(0x00300000);
1474     lp[-2] = htonl(0x11010000);
1475     lp[-1] = htonl(0xf8000000);
1476     lp[0] = htonl(0xffc00000);
1477     lp[2] = htonl(0xf3016000);
1478     lp[3] = htonl(0xf3012000);
1479     lp[4] = htonl(0xf3012000);
1480     lp[24] = htonl(0xf3018000);
1481     lp[25] = htonl(0xf3010000);
1482     lp[34] = htonl(0xf3011000);
1483     lp[38] = htonl(0xf3015000);
1484     lp[39] = htonl(0xf3014000);
1485     lp[43] = htonl(0xf3000000);
1486     lp[48] = htonl(0xf8000000);
1487     }
1488    
1489     // Don't initialize VIA (via Universal)
1490     static const uint8 via_init_dat[] = {0x08, 0x00, 0x00, 0x02, 0x67, 0x00, 0x00, 0x2c, 0x24, 0x68, 0x00, 0x08};
1491     if ((base = find_rom_data(0xe000, 0x15000, via_init_dat, sizeof(via_init_dat))) == 0) return false;
1492     D(bug("via_init %08lx\n", base));
1493     wp = (uint16 *)(ROM_BASE + base + 4);
1494     *wp = htons(0x6000); // bra
1495    
1496     static const uint8 via_init2_dat[] = {0x24, 0x68, 0x00, 0x08, 0x00, 0x12, 0x00, 0x30, 0x4e, 0x71};
1497     if ((base = find_rom_data(0xa000, 0x10000, via_init2_dat, sizeof(via_init2_dat))) == 0) return false;
1498     D(bug("via_init2 %08lx\n", base));
1499     wp = (uint16 *)(ROM_BASE + base);
1500     *wp = htons(0x4ed6); // jmp (a6)
1501    
1502     static const uint8 via_init3_dat[] = {0x22, 0x68, 0x00, 0x08, 0x28, 0x3c, 0x20, 0x00, 0x01, 0x00};
1503     if ((base = find_rom_data(0xa000, 0x10000, via_init3_dat, sizeof(via_init3_dat))) == 0) return false;
1504     D(bug("via_init3 %08lx\n", base));
1505     wp = (uint16 *)(ROM_BASE + base);
1506     *wp = htons(0x4ed6); // jmp (a6)
1507    
1508     // Don't RunDiags, get BootGlobs pointer directly
1509     if (ROMType == ROMTYPE_NEWWORLD) {
1510     static const uint8 run_diags_dat[] = {0x60, 0xff, 0x00, 0x0c};
1511     if ((base = find_rom_data(0x110, 0x128, run_diags_dat, sizeof(run_diags_dat))) == 0) return false;
1512     D(bug("run_diags %08lx\n", base));
1513     wp = (uint16 *)(ROM_BASE + base);
1514     *wp++ = htons(0x4df9); // lea xxx,a6
1515     *wp++ = htons((RAMBase + RAMSize - 0x1c) >> 16);
1516     *wp = htons((RAMBase + RAMSize - 0x1c) & 0xffff);
1517     } else {
1518     static const uint8 run_diags_dat[] = {0x74, 0x00, 0x2f, 0x0e};
1519     if ((base = find_rom_data(0xd0, 0xf0, run_diags_dat, sizeof(run_diags_dat))) == 0) return false;
1520     D(bug("run_diags %08lx\n", base));
1521     wp = (uint16 *)(ROM_BASE + base - 6);
1522     *wp++ = htons(0x4df9); // lea xxx,a6
1523     *wp++ = htons((RAMBase + RAMSize - 0x1c) >> 16);
1524     *wp = htons((RAMBase + RAMSize - 0x1c) & 0xffff);
1525     }
1526    
1527     // Replace NVRAM routines
1528     static const uint8 nvram1_dat[] = {0x48, 0xe7, 0x01, 0x0e, 0x24, 0x68, 0x00, 0x08, 0x08, 0x83, 0x00, 0x1f};
1529     if ((base = find_rom_data(0x7000, 0xc000, nvram1_dat, sizeof(nvram1_dat))) == 0) return false;
1530     D(bug("nvram1 %08lx\n", base));
1531     wp = (uint16 *)(ROM_BASE + base);
1532     *wp++ = htons(M68K_EMUL_OP_XPRAM1);
1533     *wp = htons(M68K_RTS);
1534    
1535     if (ROMType == ROMTYPE_NEWWORLD) {
1536     static const uint8 nvram2_dat[] = {0x48, 0xe7, 0x1c, 0xe0, 0x4f, 0xef, 0xff, 0xb4};
1537     if ((base = find_rom_data(0xa000, 0xd000, nvram2_dat, sizeof(nvram2_dat))) == 0) return false;
1538     D(bug("nvram2 %08lx\n", base));
1539     wp = (uint16 *)(ROM_BASE + base);
1540     *wp++ = htons(M68K_EMUL_OP_XPRAM2);
1541     *wp = htons(0x4ed3); // jmp (a3)
1542    
1543     static const uint8 nvram3_dat[] = {0x48, 0xe7, 0xdc, 0xe0, 0x4f, 0xef, 0xff, 0xb4};
1544     if ((base = find_rom_data(0xa000, 0xd000, nvram3_dat, sizeof(nvram3_dat))) == 0) return false;
1545     D(bug("nvram3 %08lx\n", base));
1546     wp = (uint16 *)(ROM_BASE + base);
1547     *wp++ = htons(M68K_EMUL_OP_XPRAM3);
1548     *wp = htons(0x4ed3); // jmp (a3)
1549    
1550     static const uint8 nvram4_dat[] = {0x4e, 0x56, 0xff, 0xa8, 0x48, 0xe7, 0x1f, 0x38, 0x16, 0x2e, 0x00, 0x13};
1551     if ((base = find_rom_data(0xa000, 0xd000, nvram4_dat, sizeof(nvram4_dat))) == 0) return false;
1552     D(bug("nvram4 %08lx\n", base));
1553     wp = (uint16 *)(ROM_BASE + base + 16);
1554     *wp++ = htons(0x1a2e); // move.b ($000f,a6),d5
1555     *wp++ = htons(0x000f);
1556     *wp++ = htons(M68K_EMUL_OP_NVRAM3);
1557     *wp++ = htons(0x4cee); // movem.l ($ff88,a6),d3-d7/a2-a4
1558     *wp++ = htons(0x1cf8);
1559     *wp++ = htons(0xff88);
1560     *wp++ = htons(0x4e5e); // unlk a6
1561     *wp = htons(M68K_RTS);
1562    
1563     static const uint8 nvram5_dat[] = {0x0c, 0x80, 0x03, 0x00, 0x00, 0x00, 0x66, 0x0a, 0x70, 0x00, 0x21, 0xf8, 0x02, 0x0c, 0x01, 0xe4};
1564     if ((base = find_rom_data(0xa000, 0xd000, nvram5_dat, sizeof(nvram5_dat))) == 0) return false;
1565     D(bug("nvram5 %08lx\n", base));
1566     wp = (uint16 *)(ROM_BASE + base + 6);
1567     *wp = htons(M68K_NOP);
1568    
1569     static const uint8 nvram6_dat[] = {0x2f, 0x0a, 0x24, 0x48, 0x4f, 0xef, 0xff, 0xa0, 0x20, 0x0f};
1570     if ((base = find_rom_data(0x9000, 0xb000, nvram6_dat, sizeof(nvram6_dat))) == 0) return false;
1571     D(bug("nvram6 %08lx\n", base));
1572     wp = (uint16 *)(ROM_BASE + base);
1573     *wp++ = htons(0x7000); // moveq #0,d0
1574     *wp++ = htons(0x2080); // move.l d0,(a0)
1575     *wp++ = htons(0x4228); // clr.b 4(a0)
1576     *wp++ = htons(0x0004);
1577     *wp = htons(M68K_RTS);
1578    
1579     static const uint8 nvram7_dat[] = {0x42, 0x2a, 0x00, 0x04, 0x4f, 0xef, 0x00, 0x60, 0x24, 0x5f, 0x4e, 0x75, 0x4f, 0xef, 0xff, 0xa0, 0x20, 0x0f};
1580     base = find_rom_data(0x9000, 0xb000, nvram7_dat, sizeof(nvram7_dat));
1581     if (base) {
1582     D(bug("nvram7 %08lx\n", base));
1583     wp = (uint16 *)(ROM_BASE + base + 12);
1584     *wp = htons(M68K_RTS);
1585     }
1586     } else {
1587     static const uint8 nvram2_dat[] = {0x4e, 0xd6, 0x06, 0x41, 0x13, 0x00};
1588     if ((base = find_rom_data(0x7000, 0xb000, nvram2_dat, sizeof(nvram2_dat))) == 0) return false;
1589     D(bug("nvram2 %08lx\n", base));
1590     wp = (uint16 *)(ROM_BASE + base + 2);
1591     *wp++ = htons(M68K_EMUL_OP_XPRAM2);
1592     *wp = htons(0x4ed3); // jmp (a3)
1593    
1594 gbeauche 1.11 static const uint8 nvram3_dat[] = {0x4e, 0xd3, 0x06, 0x41, 0x13, 0x00};
1595     if ((base = find_rom_data(0x7000, 0xb000, nvram3_dat, sizeof(nvram3_dat))) == 0) return false;
1596     D(bug("nvram3 %08lx\n", base));
1597     wp = (uint16 *)(ROM_BASE + base + 2);
1598     *wp++ = htons(M68K_EMUL_OP_XPRAM3);
1599     *wp = htons(0x4ed3); // jmp (a3)
1600    
1601     static const uint32 nvram4_loc[] = {0x582f0, 0xa0a0, 0x7e50, 0xa1d0, 0x538d0, 0};
1602     wp = (uint16 *)(ROM_BASE + nvram4_loc[ROMType]);
1603 cebix 1.1 *wp++ = htons(0x202f); // move.l 4(sp),d0
1604     *wp++ = htons(0x0004);
1605     *wp++ = htons(M68K_EMUL_OP_NVRAM1);
1606     if (ROMType == ROMTYPE_ZANZIBAR || ROMType == ROMTYPE_GAZELLE)
1607     *wp = htons(M68K_RTS);
1608     else {
1609     *wp++ = htons(0x1f40); // move.b d0,8(sp)
1610     *wp++ = htons(0x0008);
1611     *wp++ = htons(0x4e74); // rtd #4
1612     *wp = htons(0x0004);
1613     }
1614    
1615 gbeauche 1.11 static const uint32 nvram5_loc[] = {0x58460, 0xa0f0, 0x7f40, 0xa220, 0x53a20, 0};
1616     wp = (uint16 *)(ROM_BASE + nvram5_loc[ROMType]);
1617 cebix 1.1 if (ROMType == ROMTYPE_ZANZIBAR || ROMType == ROMTYPE_GAZELLE) {
1618     *wp++ = htons(0x202f); // move.l 4(sp),d0
1619     *wp++ = htons(0x0004);
1620     *wp++ = htons(0x122f); // move.b 11(sp),d1
1621     *wp++ = htons(0x000b);
1622     *wp++ = htons(M68K_EMUL_OP_NVRAM2);
1623     *wp = htons(M68K_RTS);
1624     } else {
1625     *wp++ = htons(0x202f); // move.l 6(sp),d0
1626     *wp++ = htons(0x0006);
1627     *wp++ = htons(0x122f); // move.b 4(sp),d1
1628     *wp++ = htons(0x0004);
1629     *wp++ = htons(M68K_EMUL_OP_NVRAM2);
1630     *wp++ = htons(0x4e74); // rtd #6
1631     *wp = htons(0x0006);
1632     }
1633     }
1634    
1635     // Fix MemTop/BootGlobs during system startup
1636     static const uint8 mem_top_dat[] = {0x2c, 0x6c, 0xff, 0xec, 0x2a, 0x4c, 0xdb, 0xec, 0xff, 0xf4};
1637     if ((base = find_rom_data(0x120, 0x180, mem_top_dat, sizeof(mem_top_dat))) == 0) return false;
1638     D(bug("mem_top %08lx\n", base));
1639     wp = (uint16 *)(ROM_BASE + base);
1640     *wp++ = htons(M68K_EMUL_OP_FIX_MEMTOP);
1641     *wp = htons(M68K_NOP);
1642    
1643     // Don't initialize SCC (via 0x1ac)
1644 gbeauche 1.20 static const uint8 scc_init_caller_dat[] = {0x21, 0xce, 0x01, 0x08, 0x22, 0x78, 0x0d, 0xd8};
1645     if ((base = find_rom_data(0x180, 0x1f0, scc_init_caller_dat, sizeof(scc_init_caller_dat))) == 0) return false;
1646     D(bug("scc_init_caller %08lx\n", base + 12));
1647     wp = (uint16 *)(ROM_BASE + base + 12);
1648     loc = ntohs(wp[1]) + ((uintptr)wp - ROM_BASE) + 2;
1649     static const uint8 scc_init_dat[] = {0x08, 0x38, 0x00, 0x03, 0x0d, 0xd3, 0x67, 0x12, 0x20, 0x78, 0x01, 0xdc, 0x22, 0x78, 0x01, 0xd8};
1650     if ((base = find_rom_data(loc, loc + 0x80, scc_init_dat, sizeof(scc_init_dat))) != loc) return false;
1651 cebix 1.1 D(bug("scc_init %08lx\n", base));
1652 gbeauche 1.20 wp = (uint16 *)(ROM_BASE + base);
1653 cebix 1.1 *wp++ = htons(M68K_EMUL_OP_RESET);
1654     *wp = htons(M68K_RTS);
1655    
1656     // Don't EnableExtCache (via 0x1f6) and don't DisableIntSources(via 0x1fc)
1657     static const uint8 ext_cache_dat[] = {0x4e, 0x7b, 0x00, 0x02};
1658     if ((base = find_rom_data(0x1d0, 0x230, ext_cache_dat, sizeof(ext_cache_dat))) == 0) return false;
1659     D(bug("ext_cache %08lx\n", base));
1660     lp = (uint32 *)(ROM_BASE + base + 6);
1661     wp = (uint16 *)(ROM_BASE + ntohl(*lp) + base + 6);
1662     *wp = htons(M68K_RTS);
1663     lp = (uint32 *)(ROM_BASE + base + 12);
1664     wp = (uint16 *)(ROM_BASE + ntohl(*lp) + base + 12);
1665     *wp = htons(M68K_RTS);
1666    
1667     // Fake CPU speed test (SetupTimeK)
1668     static const uint8 timek_dat[] = {0x0c, 0x38, 0x00, 0x04, 0x01, 0x2f, 0x6d, 0x3c};
1669     if ((base = find_rom_data(0x400, 0x500, timek_dat, sizeof(timek_dat))) == 0) return false;
1670     D(bug("timek %08lx\n", base));
1671     wp = (uint16 *)(ROM_BASE + base);
1672     *wp++ = htons(0x31fc); // move.w #xxx,TimeDBRA
1673     *wp++ = htons(100);
1674     *wp++ = htons(0x0d00);
1675     *wp++ = htons(0x31fc); // move.w #xxx,TimeSCCDBRA
1676     *wp++ = htons(100);
1677     *wp++ = htons(0x0d02);
1678     *wp++ = htons(0x31fc); // move.w #xxx,TimeSCSIDBRA
1679     *wp++ = htons(100);
1680     *wp++ = htons(0x0b24);
1681     *wp++ = htons(0x31fc); // move.w #xxx,TimeRAMDBRA
1682     *wp++ = htons(100);
1683     *wp++ = htons(0x0cea);
1684     *wp = htons(M68K_RTS);
1685    
1686     // Relocate jump tables ($2000..)
1687     static const uint8 jump_tab_dat[] = {0x41, 0xfa, 0x00, 0x0e, 0x21, 0xc8, 0x20, 0x10, 0x4e, 0x75};
1688     if ((base = find_rom_data(0x3000, 0x6000, jump_tab_dat, sizeof(jump_tab_dat))) == 0) return false;
1689     D(bug("jump_tab %08lx\n", base));
1690     lp = (uint32 *)(ROM_BASE + base + 16);
1691     for (;;) {
1692     D(bug(" %08lx\n", (uint32)lp - ROM_BASE));
1693     while ((ntohl(*lp) & 0xff000000) == 0xff000000) {
1694     *lp = htonl((ntohl(*lp) & (ROM_SIZE-1)) + ROM_BASE);
1695     lp++;
1696     }
1697     while (!ntohl(*lp)) lp++;
1698     if (ntohl(*lp) != 0x41fa000e)
1699     break;
1700     lp += 4;
1701     }
1702    
1703     // Create SysZone at start of Mac RAM (SetSysAppZone, via 0x22a)
1704     static const uint8 sys_zone_dat[] = {0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x40, 0x00};
1705     if ((base = find_rom_data(0x600, 0x900, sys_zone_dat, sizeof(sys_zone_dat))) == 0) return false;
1706     D(bug("sys_zone %08lx\n", base));
1707     lp = (uint32 *)(ROM_BASE + base);
1708     *lp++ = htonl(RAMBase ? RAMBase : 0x3000);
1709     *lp = htonl(RAMBase ? RAMBase + 0x1800 : 0x4800);
1710    
1711     // Set boot stack at RAMBase+4MB and fix logical/physical RAM size (CompBootStack)
1712     // The RAM size fix must be done after InitMemMgr!
1713     static const uint8 boot_stack_dat[] = {0x08, 0x38, 0x00, 0x06, 0x24, 0x0b};
1714     if ((base = find_rom_data(0x580, 0x800, boot_stack_dat, sizeof(boot_stack_dat))) == 0) return false;
1715     D(bug("boot_stack %08lx\n", base));
1716     wp = (uint16 *)(ROM_BASE + base);
1717     *wp++ = htons(0x207c); // move.l #RAMBase+0x3ffffe,a0
1718     *wp++ = htons((RAMBase + 0x3ffffe) >> 16);
1719     *wp++ = htons((RAMBase + 0x3ffffe) & 0xffff);
1720     *wp++ = htons(M68K_EMUL_OP_FIX_MEMSIZE);
1721     *wp = htons(M68K_RTS);
1722    
1723     // Get PowerPC page size (InitVMemMgr, via 0x240)
1724     static const uint8 page_size_dat[] = {0x20, 0x30, 0x81, 0xf2, 0x5f, 0xff, 0xef, 0xd8, 0x00, 0x10};
1725     if ((base = find_rom_data(0xb000, 0x12000, page_size_dat, sizeof(page_size_dat))) == 0) return false;
1726     D(bug("page_size %08lx\n", base));
1727     wp = (uint16 *)(ROM_BASE + base);
1728     *wp++ = htons(0x203c); // move.l #$1000,d0
1729     *wp++ = htons(0);
1730     *wp++ = htons(0x1000);
1731     *wp++ = htons(M68K_NOP);
1732     *wp = htons(M68K_NOP);
1733    
1734     // Gestalt PowerPC page size, RAM size (InitGestalt, via 0x25c)
1735     static const uint8 page_size2_dat[] = {0x26, 0x79, 0x5f, 0xff, 0xef, 0xd8, 0x25, 0x6b, 0x00, 0x10, 0x00, 0x1e};
1736     if ((base = find_rom_data(0x50000, 0x70000, page_size2_dat, sizeof(page_size2_dat))) == 0) return false;
1737     D(bug("page_size2 %08lx\n", base));
1738     wp = (uint16 *)(ROM_BASE + base);
1739     *wp++ = htons(0x257c); // move.l #$1000,$1e(a2)
1740     *wp++ = htons(0);
1741     *wp++ = htons(0x1000);
1742     *wp++ = htons(0x001e);
1743     *wp++ = htons(0x157c); // move.b #PVR,$1d(a2)
1744     *wp++ = htons(PVR >> 16);
1745     *wp++ = htons(0x001d);
1746     *wp++ = htons(0x263c); // move.l #RAMSize,d3
1747     *wp++ = htons(RAMSize >> 16);
1748     *wp++ = htons(RAMSize & 0xffff);
1749     *wp++ = htons(M68K_NOP);
1750     *wp++ = htons(M68K_NOP);
1751     *wp = htons(M68K_NOP);
1752     if (ROMType == ROMTYPE_NEWWORLD)
1753     wp = (uint16 *)(ROM_BASE + base + 0x4a);
1754     else
1755     wp = (uint16 *)(ROM_BASE + base + 0x28);
1756     *wp++ = htons(M68K_NOP);
1757     *wp = htons(M68K_NOP);
1758    
1759     // Gestalt CPU/bus clock speed (InitGestalt, via 0x25c)
1760     if (ROMType == ROMTYPE_ZANZIBAR) {
1761     wp = (uint16 *)(ROM_BASE + 0x5d87a);
1762     *wp++ = htons(0x203c); // move.l #Hz,d0
1763     *wp++ = htons(BusClockSpeed >> 16);
1764     *wp++ = htons(BusClockSpeed & 0xffff);
1765     *wp++ = htons(M68K_NOP);
1766     *wp = htons(M68K_NOP);
1767     wp = (uint16 *)(ROM_BASE + 0x5d888);
1768     *wp++ = htons(0x203c); // move.l #Hz,d0
1769     *wp++ = htons(CPUClockSpeed >> 16);
1770     *wp++ = htons(CPUClockSpeed & 0xffff);
1771     *wp++ = htons(M68K_NOP);
1772     *wp = htons(M68K_NOP);
1773     }
1774    
1775     // Don't write to GC interrupt mask register (via 0x262)
1776     if (ROMType != ROMTYPE_NEWWORLD) {
1777     static const uint8 gc_mask_dat[] = {0x83, 0xa8, 0x00, 0x24, 0x4e, 0x71};
1778     if ((base = find_rom_data(0x13000, 0x20000, gc_mask_dat, sizeof(gc_mask_dat))) == 0) return false;
1779     D(bug("gc_mask %08lx\n", base));
1780     wp = (uint16 *)(ROM_BASE + base);
1781     *wp++ = htons(M68K_NOP);
1782     *wp = htons(M68K_NOP);
1783     wp = (uint16 *)(ROM_BASE + base + 0x40);
1784     *wp++ = htons(M68K_NOP);
1785     *wp = htons(M68K_NOP);
1786     wp = (uint16 *)(ROM_BASE + base + 0x78);
1787     *wp++ = htons(M68K_NOP);
1788     *wp = htons(M68K_NOP);
1789     wp = (uint16 *)(ROM_BASE + base + 0x96);
1790     *wp++ = htons(M68K_NOP);
1791     *wp = htons(M68K_NOP);
1792    
1793     static const uint8 gc_mask2_dat[] = {0x02, 0xa8, 0x00, 0x00, 0x00, 0x80, 0x00, 0x24};
1794     if ((base = find_rom_data(0x13000, 0x20000, gc_mask2_dat, sizeof(gc_mask2_dat))) == 0) return false;
1795     D(bug("gc_mask2 %08lx\n", base));
1796     wp = (uint16 *)(ROM_BASE + base);
1797 gbeauche 1.11 if (ROMType == ROMTYPE_GOSSAMER)
1798     *wp++ = htons(M68K_NOP);
1799 cebix 1.1 for (int i=0; i<5; i++) {
1800     *wp++ = htons(M68K_NOP);
1801     *wp++ = htons(M68K_NOP);
1802     *wp++ = htons(M68K_NOP);
1803     *wp++ = htons(M68K_NOP);
1804     wp += 2;
1805     }
1806 gbeauche 1.11 if (ROMType == ROMTYPE_ZANZIBAR || ROMType == ROMTYPE_GOSSAMER) {
1807 cebix 1.1 for (int i=0; i<6; i++) {
1808     *wp++ = htons(M68K_NOP);
1809     *wp++ = htons(M68K_NOP);
1810     *wp++ = htons(M68K_NOP);
1811     *wp++ = htons(M68K_NOP);
1812     wp += 2;
1813     }
1814     }
1815     }
1816    
1817     // Don't initialize Cuda (via 0x274)
1818     static const uint8 cuda_init_dat[] = {0x08, 0xa9, 0x00, 0x04, 0x16, 0x00, 0x4e, 0x71, 0x13, 0x7c, 0x00, 0x84, 0x1c, 0x00, 0x4e, 0x71};
1819     if ((base = find_rom_data(0xa000, 0x12000, cuda_init_dat, sizeof(cuda_init_dat))) == 0) return false;
1820     D(bug("cuda_init %08lx\n", base));
1821     wp = (uint16 *)(ROM_BASE + base);
1822     *wp++ = htons(M68K_NOP);
1823     *wp++ = htons(M68K_NOP);
1824     *wp++ = htons(M68K_NOP);
1825     *wp++ = htons(M68K_NOP);
1826     *wp++ = htons(M68K_NOP);
1827     *wp++ = htons(M68K_NOP);
1828     *wp = htons(M68K_NOP);
1829    
1830     // Patch GetCPUSpeed (via 0x27a) (some ROMs have two of them)
1831     static const uint8 cpu_speed_dat[] = {0x20, 0x30, 0x81, 0xf2, 0x5f, 0xff, 0xef, 0xd8, 0x00, 0x04, 0x4c, 0x7c};
1832 gbeauche 1.4 if ((base = find_rom_data(0x6000, 0xa000, cpu_speed_dat, sizeof(cpu_speed_dat))) == 0) return false;
1833 cebix 1.1 D(bug("cpu_speed %08lx\n", base));
1834     wp = (uint16 *)(ROM_BASE + base);
1835     *wp++ = htons(0x203c); // move.l #(MHz<<16)|MHz,d0
1836     *wp++ = htons(CPUClockSpeed / 1000000);
1837     *wp++ = htons(CPUClockSpeed / 1000000);
1838     *wp = htons(M68K_RTS);
1839 gbeauche 1.4 if ((base = find_rom_data(base, 0xa000, cpu_speed_dat, sizeof(cpu_speed_dat))) != 0) {
1840 cebix 1.1 D(bug("cpu_speed2 %08lx\n", base));
1841     wp = (uint16 *)(ROM_BASE + base);
1842     *wp++ = htons(0x203c); // move.l #(MHz<<16)|MHz,d0
1843     *wp++ = htons(CPUClockSpeed / 1000000);
1844     *wp++ = htons(CPUClockSpeed / 1000000);
1845     *wp = htons(M68K_RTS);
1846     }
1847    
1848     // Don't poke VIA in InitTimeMgr (via 0x298)
1849     static const uint8 time_via_dat[] = {0x40, 0xe7, 0x00, 0x7c, 0x07, 0x00, 0x28, 0x78, 0x01, 0xd4, 0x43, 0xec, 0x10, 0x00};
1850     if ((base = find_rom_data(0x30000, 0x40000, time_via_dat, sizeof(time_via_dat))) == 0) return false;
1851     D(bug("time_via %08lx\n", base));
1852     wp = (uint16 *)(ROM_BASE + base);
1853     *wp++ = htons(0x4cdf); // movem.l (sp)+,d0-d5/a0-a4
1854     *wp++ = htons(0x1f3f);
1855     *wp = htons(M68K_RTS);
1856    
1857     // Don't read from 0xff800000 (Name Registry, Open Firmware?) (via 0x2a2)
1858     // Remove this if FE03 works!!
1859     static const uint8 open_firmware_dat[] = {0x2f, 0x79, 0xff, 0x80, 0x00, 0x00, 0x00, 0xfc};
1860     if ((base = find_rom_data(0x48000, 0x58000, open_firmware_dat, sizeof(open_firmware_dat))) == 0) return false;
1861     D(bug("open_firmware %08lx\n", base));
1862     wp = (uint16 *)(ROM_BASE + base);
1863     *wp++ = htons(0x2f7c); // move.l #deadbeef,0xfc(a7)
1864     *wp++ = htons(0xdead);
1865     *wp++ = htons(0xbeef);
1866     *wp = htons(0x00fc);
1867     wp = (uint16 *)(ROM_BASE + base + 0x1a);
1868     *wp++ = htons(M68K_NOP); // (FE03 opcode, tries to jump to 0xdeadbeef)
1869     *wp = htons(M68K_NOP);
1870    
1871     // Don't EnableExtCache (via 0x2b2)
1872     static const uint8 ext_cache2_dat[] = {0x4f, 0xef, 0xff, 0xec, 0x20, 0x4f, 0x10, 0xbc, 0x00, 0x01, 0x11, 0x7c, 0x00, 0x1b};
1873     if ((base = find_rom_data(0x13000, 0x20000, ext_cache2_dat, sizeof(ext_cache2_dat))) == 0) return false;
1874     D(bug("ext_cache2 %08lx\n", base));
1875     wp = (uint16 *)(ROM_BASE + base);
1876     *wp = htons(M68K_RTS);
1877    
1878     // Don't install Time Manager task for 60Hz interrupt (Enable60HzInts, via 0x2b8)
1879 gbeauche 1.13 if (ROMType == ROMTYPE_NEWWORLD || ROMType == ROMTYPE_GOSSAMER) {
1880 cebix 1.1 static const uint8 tm_task_dat[] = {0x30, 0x3c, 0x4e, 0x2b, 0xa9, 0xc9};
1881 gbeauche 1.13 if ((base = find_rom_data(0x2a0, 0x320, tm_task_dat, sizeof(tm_task_dat))) == 0) return false;
1882 cebix 1.1 D(bug("tm_task %08lx\n", base));
1883 gbeauche 1.13 wp = (uint16 *)(ROM_BASE + base + 28);
1884 cebix 1.1 *wp++ = htons(M68K_NOP);
1885     *wp++ = htons(M68K_NOP);
1886     *wp++ = htons(M68K_NOP);
1887     *wp++ = htons(M68K_NOP);
1888     *wp++ = htons(M68K_NOP);
1889     *wp = htons(M68K_NOP);
1890     } else {
1891     static const uint8 tm_task_dat[] = {0x20, 0x3c, 0x73, 0x79, 0x73, 0x61};
1892     if ((base = find_rom_data(0x280, 0x300, tm_task_dat, sizeof(tm_task_dat))) == 0) return false;
1893     D(bug("tm_task %08lx\n", base));
1894     wp = (uint16 *)(ROM_BASE + base - 6);
1895     *wp++ = htons(M68K_NOP);
1896     *wp++ = htons(M68K_NOP);
1897     *wp = htons(M68K_NOP);
1898     }
1899    
1900     // Don't read PVR from 0x5fffef80 in DriverServicesLib (via 0x316)
1901 gbeauche 1.11 if (ROMType != ROMTYPE_NEWWORLD && ROMType != ROMTYPE_GOSSAMER) {
1902 cebix 1.1 uint32 dsl_offset = find_rom_resource(FOURCC('n','l','i','b'), -16401);
1903     if (ROMType == ROMTYPE_ZANZIBAR) {
1904     static const uint8 dsl_pvr_dat[] = {0x40, 0x82, 0x00, 0x40, 0x38, 0x60, 0xef, 0x80, 0x3c, 0x63, 0x60, 0x00, 0x80, 0x83, 0x00, 0x00, 0x54, 0x84, 0x84, 0x3e};
1905     if ((base = find_rom_data(dsl_offset, dsl_offset + 0x6000, dsl_pvr_dat, sizeof(dsl_pvr_dat))) == 0) return false;
1906     } else {
1907     static const uint8 dsl_pvr_dat[] = {0x3b, 0xc3, 0x00, 0x00, 0x30, 0x84, 0xff, 0xa0, 0x40, 0x82, 0x00, 0x44, 0x80, 0x84, 0xef, 0xe0, 0x54, 0x84, 0x84, 0x3e};
1908     if ((base = find_rom_data(dsl_offset, dsl_offset + 0x6000, dsl_pvr_dat, sizeof(dsl_pvr_dat))) == 0) return false;
1909     }
1910     D(bug("dsl_pvr %08lx\n", base));
1911     lp = (uint32 *)(ROM_BASE + base + 12);
1912     *lp = htonl(0x3c800000 | (PVR >> 16)); // lis r4,PVR
1913    
1914     // Don't read bus clock from 0x5fffef88 in DriverServicesLib (via 0x316)
1915     if (ROMType == ROMTYPE_ZANZIBAR) {
1916     static const uint8 dsl_bus_dat[] = {0x81, 0x07, 0x00, 0x00, 0x39, 0x20, 0x42, 0x40, 0x81, 0x62, 0xff, 0x20};
1917     if ((base = find_rom_data(dsl_offset, dsl_offset + 0x6000, dsl_bus_dat, sizeof(dsl_bus_dat))) == 0) return false;
1918     D(bug("dsl_bus %08lx\n", base));
1919     lp = (uint32 *)(ROM_BASE + base);
1920     *lp = htonl(0x81000000 + XLM_BUS_CLOCK); // lwz r8,(bus clock speed)
1921     } else {
1922     static const uint8 dsl_bus_dat[] = {0x80, 0x83, 0xef, 0xe8, 0x80, 0x62, 0x00, 0x10, 0x7c, 0x04, 0x03, 0x96};
1923     if ((base = find_rom_data(dsl_offset, dsl_offset + 0x6000, dsl_bus_dat, sizeof(dsl_bus_dat))) == 0) return false;
1924     D(bug("dsl_bus %08lx\n", base));
1925     lp = (uint32 *)(ROM_BASE + base);
1926     *lp = htonl(0x80800000 + XLM_BUS_CLOCK); // lwz r4,(bus clock speed)
1927     }
1928     }
1929    
1930     // Don't open InterruptTreeTNT in MotherBoardHAL init in DriverServicesLib init
1931     if (ROMType == ROMTYPE_ZANZIBAR) {
1932     lp = (uint32 *)(ROM_BASE + find_rom_resource(FOURCC('n','l','i','b'), -16408) + 0x16c);
1933     *lp = htonl(0x38600000); // li r3,0
1934     }
1935    
1936 gbeauche 1.19 // FIXME: Fake reading from [HpChk]+4 (the callchain reports some function from DriverServicesLib)
1937     if (1) {
1938     uint32 hpchk_offset = find_rom_resource(FOURCC('n','l','i','b'), 10);
1939     static const uint8 hpchk_dat[] = {0x80, 0x80, 0x03, 0x16, 0x94, 0x21, 0xff, 0xb0, 0x83, 0xc4, 0x00, 0x04};
1940     if ((base = find_rom_data(hpchk_offset, hpchk_offset + 0x3000, hpchk_dat, sizeof(hpchk_dat))) == 0) return false;
1941     D(bug("hpchk %08lx\n", base));
1942     lp = (uint32 *)(ROM_BASE + base);
1943     *lp = htonl(0x80800000 + XLM_ZERO_PAGE); // lwz r4,(zero page)
1944     }
1945    
1946 cebix 1.1 // Patch Name Registry
1947     static const uint8 name_reg_dat[] = {0x70, 0xff, 0xab, 0xeb};
1948     if ((base = find_rom_data(0x300, 0x380, name_reg_dat, sizeof(name_reg_dat))) == 0) return false;
1949     D(bug("name_reg %08lx\n", base));
1950     wp = (uint16 *)(ROM_BASE + base);
1951     *wp = htons(M68K_EMUL_OP_NAME_REGISTRY);
1952    
1953     #if DISABLE_SCSI
1954     // Fake SCSI Manager
1955     // Remove this if SCSI Manager works!!
1956     static const uint8 scsi_mgr_a_dat[] = {0x4e, 0x56, 0x00, 0x00, 0x20, 0x3c, 0x00, 0x00, 0x04, 0x0c, 0xa7, 0x1e};
1957     static const uint8 scsi_mgr_b_dat[] = {0x4e, 0x56, 0x00, 0x00, 0x2f, 0x0c, 0x20, 0x3c, 0x00, 0x00, 0x04, 0x0c, 0xa7, 0x1e};
1958     if ((base = find_rom_data(0x1c000, 0x28000, scsi_mgr_a_dat, sizeof(scsi_mgr_a_dat))) == 0) {
1959     if ((base = find_rom_data(0x1c000, 0x28000, scsi_mgr_b_dat, sizeof(scsi_mgr_b_dat))) == 0) return false;
1960     }
1961     D(bug("scsi_mgr %08lx\n", base));
1962     wp = (uint16 *)(ROM_BASE + base);
1963     *wp++ = htons(0x21fc); // move.l #xxx,0x624 (SCSIAtomic)
1964     *wp++ = htons((ROM_BASE + base + 18) >> 16);
1965     *wp++ = htons((ROM_BASE + base + 18) & 0xffff);
1966     *wp++ = htons(0x0624);
1967     *wp++ = htons(0x21fc); // move.l #xxx,0xe54 (SCSIDispatch)
1968     *wp++ = htons((ROM_BASE + base + 22) >> 16);
1969     *wp++ = htons((ROM_BASE + base + 22) & 0xffff);
1970     *wp++ = htons(0x0e54);
1971     *wp++ = htons(M68K_RTS);
1972     *wp++ = htons(M68K_EMUL_OP_SCSI_ATOMIC);
1973     *wp++ = htons(M68K_RTS);
1974     *wp++ = htons(M68K_EMUL_OP_SCSI_DISPATCH);
1975     *wp = htons(0x4ed0); // jmp (a0)
1976     wp = (uint16 *)(ROM_BASE + base + 0x20);
1977     *wp++ = htons(0x7000); // moveq #0,d0
1978     *wp = htons(M68K_RTS);
1979     #endif
1980    
1981     #if DISABLE_SCSI
1982     // Don't access SCSI variables
1983     // Remove this if SCSI Manager works!!
1984     if (ROMType == ROMTYPE_NEWWORLD) {
1985     static const uint8 scsi_var_dat[] = {0x70, 0x01, 0xa0, 0x89, 0x4a, 0x6e, 0xfe, 0xac, 0x4f, 0xef, 0x00, 0x10, 0x66, 0x00};
1986     if ((base = find_rom_data(0x1f500, 0x1f600, scsi_var_dat, sizeof(scsi_var_dat))) != 0) {
1987     D(bug("scsi_var %08lx\n", base));
1988     wp = (uint16 *)(ROM_BASE + base + 12);
1989     *wp = htons(0x6000); // bra
1990     }
1991    
1992     static const uint8 scsi_var2_dat[] = {0x4e, 0x56, 0xfc, 0x58, 0x48, 0xe7, 0x1f, 0x38};
1993     if ((base = find_rom_data(0x1f700, 0x1f800, scsi_var2_dat, sizeof(scsi_var2_dat))) != 0) {
1994     D(bug("scsi_var2 %08lx\n", base));
1995     wp = (uint16 *)(ROM_BASE + base);
1996     *wp++ = htons(0x7000); // moveq #0,d0
1997 gbeauche 1.11 *wp = htons(M68K_RTS);
1998     }
1999     }
2000     else if (ROMType == ROMTYPE_GOSSAMER) {
2001     static const uint8 scsi_var_dat[] = {0x70, 0x01, 0xa0, 0x89, 0x4a, 0x6e, 0xfe, 0xac, 0x4f, 0xef, 0x00, 0x10, 0x66, 0x00};
2002     if ((base = find_rom_data(0x1d700, 0x1d800, scsi_var_dat, sizeof(scsi_var_dat))) != 0) {
2003     D(bug("scsi_var %08lx\n", base));
2004     wp = (uint16 *)(ROM_BASE + base + 12);
2005     *wp = htons(0x6000); // bra
2006     }
2007    
2008     static const uint8 scsi_var2_dat[] = {0x4e, 0x56, 0xfc, 0x5a, 0x48, 0xe7, 0x1f, 0x38};
2009     if ((base = find_rom_data(0x1d900, 0x1da00, scsi_var2_dat, sizeof(scsi_var2_dat))) != 0) {
2010     D(bug("scsi_var2 %08lx\n", base));
2011     wp = (uint16 *)(ROM_BASE + base);
2012     *wp++ = htons(0x7000); // moveq #0,d0
2013     *wp = htons(M68K_RTS);
2014 cebix 1.1 }
2015     }
2016     #endif
2017    
2018     // Don't wait in ADBInit (via 0x36c)
2019     static const uint8 adb_init_dat[] = {0x08, 0x2b, 0x00, 0x05, 0x01, 0x5d, 0x66, 0xf8};
2020     if ((base = find_rom_data(0x31000, 0x3d000, adb_init_dat, sizeof(adb_init_dat))) == 0) return false;
2021     D(bug("adb_init %08lx\n", base));
2022     wp = (uint16 *)(ROM_BASE + base + 6);
2023     *wp = htons(M68K_NOP);
2024    
2025     // Modify check in InitResources() so that addresses >0x80000000 work
2026     static const uint8 init_res_dat[] = {0x4a, 0xb8, 0x0a, 0x50, 0x6e, 0x20};
2027     if ((base = find_rom_data(0x78000, 0x8c000, init_res_dat, sizeof(init_res_dat))) == 0) return false;
2028     D(bug("init_res %08lx\n", base));
2029     bp = (uint8 *)(ROM_BASE + base + 4);
2030     *bp = 0x66;
2031    
2032     // Modify vCheckLoad() so that we can patch resources (68k Resource Manager)
2033     static const uint8 check_load_dat[] = {0x20, 0x78, 0x07, 0xf0, 0x4e, 0xd0};
2034     if ((base = find_rom_data(0x78000, 0x8c000, check_load_dat, sizeof(check_load_dat))) == 0) return false;
2035     D(bug("check_load %08lx\n", base));
2036     wp = (uint16 *)(ROM_BASE + base);
2037     *wp++ = htons(M68K_JMP);
2038     *wp++ = htons((ROM_BASE + CHECK_LOAD_PATCH_SPACE) >> 16);
2039     *wp = htons((ROM_BASE + CHECK_LOAD_PATCH_SPACE) & 0xffff);
2040     wp = (uint16 *)(ROM_BASE + CHECK_LOAD_PATCH_SPACE);
2041     *wp++ = htons(0x2f03); // move.l d3,-(a7)
2042     *wp++ = htons(0x2078); // move.l $07f0,a0
2043     *wp++ = htons(0x07f0);
2044     *wp++ = htons(M68K_JSR_A0);
2045     *wp++ = htons(M68K_EMUL_OP_CHECKLOAD);
2046     *wp = htons(M68K_RTS);
2047    
2048     // Replace .Sony driver
2049     sony_offset = find_rom_resource(FOURCC('D','R','V','R'), 4);
2050     if (ROMType == ROMTYPE_ZANZIBAR || ROMType == ROMTYPE_NEWWORLD)
2051     sony_offset = find_rom_resource(FOURCC('D','R','V','R'), 4, true); // First DRVR 4 is .MFMFloppy
2052     if (sony_offset == 0) {
2053     sony_offset = find_rom_resource(FOURCC('n','d','r','v'), -20196); // NewWorld 1.6 has "PCFloppy" ndrv
2054     if (sony_offset == 0)
2055     return false;
2056     lp = (uint32 *)(ROM_BASE + rsrc_ptr + 8);
2057     *lp = htonl(FOURCC('D','R','V','R'));
2058     wp = (uint16 *)(ROM_BASE + rsrc_ptr + 12);
2059     *wp = htons(4);
2060     }
2061     D(bug("sony_offset %08lx\n", sony_offset));
2062     memcpy((void *)(ROM_BASE + sony_offset), sony_driver, sizeof(sony_driver));
2063    
2064     // Install .Disk and .AppleCD drivers
2065     memcpy((void *)(ROM_BASE + sony_offset + 0x100), disk_driver, sizeof(disk_driver));
2066     memcpy((void *)(ROM_BASE + sony_offset + 0x200), cdrom_driver, sizeof(cdrom_driver));
2067    
2068     // Install serial drivers
2069 gbeauche 1.18 gen_ain_driver( ROM_BASE + sony_offset + 0x300);
2070     gen_aout_driver(ROM_BASE + sony_offset + 0x400);
2071     gen_bin_driver( ROM_BASE + sony_offset + 0x500);
2072     gen_bout_driver(ROM_BASE + sony_offset + 0x600);
2073 cebix 1.1
2074     // Copy icons to ROM
2075     SonyDiskIconAddr = ROM_BASE + sony_offset + 0x800;
2076     memcpy((void *)(ROM_BASE + sony_offset + 0x800), SonyDiskIcon, sizeof(SonyDiskIcon));
2077     SonyDriveIconAddr = ROM_BASE + sony_offset + 0xa00;
2078     memcpy((void *)(ROM_BASE + sony_offset + 0xa00), SonyDriveIcon, sizeof(SonyDriveIcon));
2079     DiskIconAddr = ROM_BASE + sony_offset + 0xc00;
2080     memcpy((void *)(ROM_BASE + sony_offset + 0xc00), DiskIcon, sizeof(DiskIcon));
2081     CDROMIconAddr = ROM_BASE + sony_offset + 0xe00;
2082     memcpy((void *)(ROM_BASE + sony_offset + 0xe00), CDROMIcon, sizeof(CDROMIcon));
2083    
2084     // Patch driver install routine
2085     static const uint8 drvr_install_dat[] = {0xa7, 0x1e, 0x21, 0xc8, 0x01, 0x1c, 0x4e, 0x75};
2086     if ((base = find_rom_data(0xb00, 0xd00, drvr_install_dat, sizeof(drvr_install_dat))) == 0) return false;
2087     D(bug("drvr_install %08lx\n", base));
2088     wp = (uint16 *)(ROM_BASE + base + 8);
2089     *wp++ = htons(M68K_EMUL_OP_INSTALL_DRIVERS);
2090     *wp = htons(M68K_RTS);
2091    
2092     // Don't install serial drivers from ROM
2093 gbeauche 1.11 if (ROMType == ROMTYPE_ZANZIBAR || ROMType == ROMTYPE_NEWWORLD || ROMType == ROMTYPE_GOSSAMER) {
2094 cebix 1.1 wp = (uint16 *)(ROM_BASE + find_rom_resource(FOURCC('S','E','R','D'), 0));
2095     *wp = htons(M68K_RTS);
2096     } else {
2097     wp = (uint16 *)(ROM_BASE + find_rom_resource(FOURCC('s','l','0','5'), 2) + 0xc4);
2098     *wp++ = htons(M68K_NOP);
2099     *wp++ = htons(M68K_NOP);
2100     *wp++ = htons(M68K_NOP);
2101     *wp++ = htons(M68K_NOP);
2102     *wp = htons(0x7000); // moveq #0,d0
2103     wp = (uint16 *)(ROM_BASE + find_rom_resource(FOURCC('s','l','0','5'), 2) + 0x8ee);
2104     *wp = htons(M68K_NOP);
2105     }
2106     uint32 nsrd_offset = find_rom_resource(FOURCC('n','s','r','d'), 1);
2107     if (nsrd_offset) {
2108     lp = (uint32 *)(ROM_BASE + rsrc_ptr + 8);
2109     *lp = htonl(FOURCC('x','s','r','d'));
2110     }
2111    
2112     // Replace ADBOp()
2113     memcpy((void *)(ROM_BASE + find_rom_trap(0xa07c)), adbop_patch, sizeof(adbop_patch));
2114    
2115     // Replace Time Manager
2116     wp = (uint16 *)(ROM_BASE + find_rom_trap(0xa058));
2117     *wp++ = htons(M68K_EMUL_OP_INSTIME);
2118     *wp = htons(M68K_RTS);
2119     wp = (uint16 *)(ROM_BASE + find_rom_trap(0xa059));
2120     *wp++ = htons(0x40e7); // move sr,-(sp)
2121     *wp++ = htons(0x007c); // ori #$0700,sr
2122     *wp++ = htons(0x0700);
2123     *wp++ = htons(M68K_EMUL_OP_RMVTIME);
2124     *wp++ = htons(0x46df); // move (sp)+,sr
2125     *wp = htons(M68K_RTS);
2126     wp = (uint16 *)(ROM_BASE + find_rom_trap(0xa05a));
2127     *wp++ = htons(0x40e7); // move sr,-(sp)
2128     *wp++ = htons(0x007c); // ori #$0700,sr
2129     *wp++ = htons(0x0700);
2130     *wp++ = htons(M68K_EMUL_OP_PRIMETIME);
2131     *wp++ = htons(0x46df); // move (sp)+,sr
2132     *wp = htons(M68K_RTS);
2133     wp = (uint16 *)(ROM_BASE + find_rom_trap(0xa093));
2134     *wp++ = htons(M68K_EMUL_OP_MICROSECONDS);
2135     *wp = htons(M68K_RTS);
2136    
2137     // Disable Egret Manager
2138     static const uint8 egret_dat[] = {0x2f, 0x30, 0x81, 0xe2, 0x20, 0x10, 0x00, 0x18};
2139     if ((base = find_rom_data(0xa000, 0x10000, egret_dat, sizeof(egret_dat))) == 0) return false;
2140     D(bug("egret %08lx\n", base));
2141     wp = (uint16 *)(ROM_BASE + base);
2142     *wp++ = htons(0x7000);
2143     *wp = htons(M68K_RTS);
2144    
2145     // Don't call FE0A opcode in Shutdown Manager
2146     static const uint8 shutdown_dat[] = {0x40, 0xe7, 0x00, 0x7c, 0x07, 0x00, 0x48, 0xe7, 0x3f, 0x00, 0x2c, 0x00, 0x2e, 0x01};
2147     if ((base = find_rom_data(0x30000, 0x40000, shutdown_dat, sizeof(shutdown_dat))) == 0) return false;
2148     D(bug("shutdown %08lx\n", base));
2149     wp = (uint16 *)(ROM_BASE + base);
2150     if (ROMType == ROMTYPE_ZANZIBAR)
2151     *wp = htons(M68K_RTS);
2152 gbeauche 1.6 else if (ntohs(wp[-4]) == 0x61ff)
2153     *wp = htons(M68K_RTS);
2154     else if (ntohs(wp[-2]) == 0x6700)
2155 cebix 1.1 wp[-2] = htons(0x6000); // bra
2156    
2157     // Patch PowerOff()
2158     wp = (uint16 *)(ROM_BASE + find_rom_trap(0xa05b)); // PowerOff()
2159     *wp = htons(M68K_EMUL_RETURN);
2160    
2161     // Patch VIA interrupt handler
2162     static const uint8 via_int_dat[] = {0x70, 0x7f, 0xc0, 0x29, 0x1a, 0x00, 0xc0, 0x29, 0x1c, 0x00};
2163     if ((base = find_rom_data(0x13000, 0x1c000, via_int_dat, sizeof(via_int_dat))) == 0) return false;
2164     D(bug("via_int %08lx\n", base));
2165     uint32 level1_int = ROM_BASE + base;
2166     wp = (uint16 *)level1_int; // Level 1 handler
2167     *wp++ = htons(0x7002); // moveq #2,d0 (60Hz interrupt)
2168     *wp++ = htons(M68K_NOP);
2169     *wp++ = htons(M68K_NOP);
2170     *wp++ = htons(M68K_NOP);
2171     *wp = htons(M68K_NOP);
2172    
2173     static const uint8 via_int2_dat[] = {0x13, 0x7c, 0x00, 0x02, 0x1a, 0x00, 0x4e, 0x71, 0x52, 0xb8, 0x01, 0x6a};
2174     if ((base = find_rom_data(0x10000, 0x18000, via_int2_dat, sizeof(via_int2_dat))) == 0) return false;
2175     D(bug("via_int2 %08lx\n", base));
2176     wp = (uint16 *)(ROM_BASE + base); // 60Hz handler
2177     *wp++ = htons(M68K_EMUL_OP_IRQ);
2178     *wp++ = htons(0x4a80); // tst.l d0
2179     *wp++ = htons(0x6700); // beq xxx
2180     *wp = htons(0xffe8);
2181    
2182     if (ROMType == ROMTYPE_NEWWORLD) {
2183     static const uint8 via_int3_dat[] = {0x48, 0xe7, 0xf0, 0xf0, 0x76, 0x01, 0x60, 0x26};
2184 gbeauche 1.4 if ((base = find_rom_data(0x15000, 0x19000, via_int3_dat, sizeof(via_int3_dat))) == 0) return false;
2185 cebix 1.1 D(bug("via_int3 %08lx\n", base));
2186     wp = (uint16 *)(ROM_BASE + base); // CHRP level 1 handler
2187     *wp++ = htons(M68K_JMP);
2188     *wp++ = htons((level1_int - 12) >> 16);
2189     *wp = htons((level1_int - 12) & 0xffff);
2190     }
2191    
2192     // Patch PutScrap() for clipboard exchange with host OS
2193     uint32 put_scrap = find_rom_trap(0xa9fe); // PutScrap()
2194     wp = (uint16 *)(ROM_BASE + PUT_SCRAP_PATCH_SPACE);
2195     *wp++ = htons(M68K_EMUL_OP_PUT_SCRAP);
2196     *wp++ = htons(M68K_JMP);
2197     *wp++ = htons((ROM_BASE + put_scrap) >> 16);
2198     *wp++ = htons((ROM_BASE + put_scrap) & 0xffff);
2199     lp = (uint32 *)(ROM_BASE + 0x22);
2200     lp = (uint32 *)(ROM_BASE + ntohl(*lp));
2201     lp[0xa9fe & 0x3ff] = htonl(PUT_SCRAP_PATCH_SPACE);
2202    
2203     // Patch GetScrap() for clipboard exchange with host OS
2204     uint32 get_scrap = find_rom_trap(0xa9fd); // GetScrap()
2205     wp = (uint16 *)(ROM_BASE + GET_SCRAP_PATCH_SPACE);
2206     *wp++ = htons(M68K_EMUL_OP_GET_SCRAP);
2207     *wp++ = htons(M68K_JMP);
2208     *wp++ = htons((ROM_BASE + get_scrap) >> 16);
2209     *wp++ = htons((ROM_BASE + get_scrap) & 0xffff);
2210     lp = (uint32 *)(ROM_BASE + 0x22);
2211     lp = (uint32 *)(ROM_BASE + ntohl(*lp));
2212     lp[0xa9fd & 0x3ff] = htonl(GET_SCRAP_PATCH_SPACE);
2213    
2214     #if __BEOS__
2215     // Patch SynchIdleTime()
2216     if (PrefsFindBool("idlewait")) {
2217     wp = (uint16 *)(ROM_BASE + find_rom_trap(0xabf7) + 4); // SynchIdleTime()
2218     D(bug("SynchIdleTime at %08lx\n", wp));
2219     if (ntohs(*wp) == 0x2078) {
2220     *wp++ = htons(M68K_EMUL_OP_IDLE_TIME);
2221     *wp = htons(M68K_NOP);
2222     } else {
2223     D(bug("SynchIdleTime patch not installed\n"));
2224     }
2225     }
2226     #endif
2227    
2228     // Construct list of all sifters used by sound components in ROM
2229     D(bug("Searching for sound components with type sdev in ROM\n"));
2230     uint32 thing = find_rom_resource(FOURCC('t','h','n','g'));
2231     while (thing) {
2232     thing += ROM_BASE;
2233     D(bug(" found %c%c%c%c %c%c%c%c\n", ReadMacInt8(thing), ReadMacInt8(thing + 1), ReadMacInt8(thing + 2), ReadMacInt8(thing + 3), ReadMacInt8(thing + 4), ReadMacInt8(thing + 5), ReadMacInt8(thing + 6), ReadMacInt8(thing + 7)));
2234     if (ReadMacInt32(thing) == FOURCC('s','d','e','v') && ReadMacInt32(thing + 4) == FOURCC('s','i','n','g')) {
2235     WriteMacInt32(thing + 4, FOURCC('a','w','g','c'));
2236     D(bug(" found sdev component at offset %08x in ROM\n", thing));
2237     AddSifter(ReadMacInt32(thing + componentResType), ReadMacInt16(thing + componentResID));
2238     if (ReadMacInt32(thing + componentPFCount))
2239     AddSifter(ReadMacInt32(thing + componentPFResType), ReadMacInt16(thing + componentPFResID));
2240     }
2241     thing = find_rom_resource(FOURCC('t','h','n','g'), 4711, true);
2242     }
2243    
2244     // Patch component code
2245     D(bug("Patching sifters in ROM\n"));
2246     for (int i=0; i<num_sifters; i++) {
2247     if ((thing = find_rom_resource(sifter_list[i].type, sifter_list[i].id)) != 0) {
2248     D(bug(" patching type %08x, id %d\n", sifter_list[i].type, sifter_list[i].id));
2249     // Install 68k glue code
2250     uint16 *wp = (uint16 *)(ROM_BASE + thing);
2251     *wp++ = htons(0x4e56); *wp++ = htons(0x0000); // link a6,#0
2252     *wp++ = htons(0x48e7); *wp++ = htons(0x8018); // movem.l d0/a3-a4,-(a7)
2253     *wp++ = htons(0x266e); *wp++ = htons(0x000c); // movea.l $c(a6),a3
2254     *wp++ = htons(0x286e); *wp++ = htons(0x0008); // movea.l $8(a6),a4
2255     *wp++ = htons(M68K_EMUL_OP_AUDIO_DISPATCH);
2256     *wp++ = htons(0x2d40); *wp++ = htons(0x0010); // move.l d0,$10(a6)
2257     *wp++ = htons(0x4cdf); *wp++ = htons(0x1801); // movem.l (a7)+,d0/a3-a4
2258     *wp++ = htons(0x4e5e); // unlk a6
2259     *wp++ = htons(0x4e74); *wp++ = htons(0x0008); // rtd #8
2260     }
2261     }
2262     return true;
2263     }
2264    
2265    
2266     /*
2267     * Install .Sony, disk and CD-ROM drivers
2268     */
2269    
2270     void InstallDrivers(void)
2271     {
2272     D(bug("Installing drivers...\n"));
2273     M68kRegisters r;
2274 gbeauche 1.18 SheepArray<SIZEOF_IOParam> pb_var;
2275     const uintptr pb = pb_var.addr();
2276 gbeauche 1.7
2277 gbeauche 1.5 // Install floppy driver
2278 gbeauche 1.14 if (ROMType == ROMTYPE_NEWWORLD || ROMType == ROMTYPE_GOSSAMER) {
2279 gbeauche 1.5
2280 gbeauche 1.14 // Force installation of floppy driver with NewWorld and Gossamer ROMs
2281 gbeauche 1.5 r.a[0] = ROM_BASE + sony_offset;
2282     r.d[0] = (uint32)SonyRefNum;
2283     Execute68kTrap(0xa43d, &r); // DrvrInstallRsrvMem()
2284     r.a[0] = ReadMacInt32(ReadMacInt32(0x11c) + ~SonyRefNum * 4); // Get driver handle from Unit Table
2285     Execute68kTrap(0xa029, &r); // HLock()
2286     uint32 dce = ReadMacInt32(r.a[0]);
2287     WriteMacInt32(dce + dCtlDriver, ROM_BASE + sony_offset);
2288     WriteMacInt16(dce + dCtlFlags, SonyDriverFlags);
2289     }
2290 gbeauche 1.8
2291     #if DISABLE_SCSI && 0
2292     // Fake SCSIGlobals
2293 gbeauche 1.19 WriteMacInt32(0xc0c, SheepMem::ZeroPage());
2294 gbeauche 1.8 #endif
2295 gbeauche 1.5
2296 cebix 1.1 // Open .Sony driver
2297 gbeauche 1.18 SheepString sony_str("\005.Sony");
2298     WriteMacInt8(pb + ioPermssn, 0);
2299     WriteMacInt32(pb + ioNamePtr, sony_str.addr());
2300     r.a[0] = pb;
2301 cebix 1.1 Execute68kTrap(0xa000, &r); // Open()
2302    
2303     // Install disk driver
2304     r.a[0] = ROM_BASE + sony_offset + 0x100;
2305     r.d[0] = (uint32)DiskRefNum;
2306     Execute68kTrap(0xa43d, &r); // DrvrInstallRsrvMem()
2307     r.a[0] = ReadMacInt32(ReadMacInt32(0x11c) + ~DiskRefNum * 4); // Get driver handle from Unit Table
2308     Execute68kTrap(0xa029, &r); // HLock()
2309     uint32 dce = ReadMacInt32(r.a[0]);
2310     WriteMacInt32(dce + dCtlDriver, ROM_BASE + sony_offset + 0x100);
2311     WriteMacInt16(dce + dCtlFlags, DiskDriverFlags);
2312    
2313     // Open disk driver
2314 gbeauche 1.18 SheepString disk_str("\005.Disk");
2315     WriteMacInt32(pb + ioNamePtr, disk_str.addr());
2316     r.a[0] = pb;
2317 cebix 1.1 Execute68kTrap(0xa000, &r); // Open()
2318    
2319     // Install CD-ROM driver unless nocdrom option given
2320     if (!PrefsFindBool("nocdrom")) {
2321    
2322     // Install CD-ROM driver
2323     r.a[0] = ROM_BASE + sony_offset + 0x200;
2324     r.d[0] = (uint32)CDROMRefNum;
2325     Execute68kTrap(0xa43d, &r); // DrvrInstallRsrvMem()
2326     r.a[0] = ReadMacInt32(ReadMacInt32(0x11c) + ~CDROMRefNum * 4); // Get driver handle from Unit Table
2327     Execute68kTrap(0xa029, &r); // HLock()
2328     dce = ReadMacInt32(r.a[0]);
2329     WriteMacInt32(dce + dCtlDriver, ROM_BASE + sony_offset + 0x200);
2330     WriteMacInt16(dce + dCtlFlags, CDROMDriverFlags);
2331    
2332     // Open CD-ROM driver
2333 gbeauche 1.18 SheepString apple_cd("\010.AppleCD");
2334     WriteMacInt32(pb + ioNamePtr, apple_cd.addr());
2335     r.a[0] = pb;
2336 cebix 1.1 Execute68kTrap(0xa000, &r); // Open()
2337     }
2338    
2339     // Install serial drivers
2340     r.a[0] = ROM_BASE + sony_offset + 0x300;
2341     r.d[0] = (uint32)-6;
2342     Execute68kTrap(0xa43d, &r); // DrvrInstallRsrvMem()
2343     r.a[0] = ReadMacInt32(ReadMacInt32(0x11c) + ~(-6) * 4); // Get driver handle from Unit Table
2344     Execute68kTrap(0xa029, &r); // HLock()
2345     dce = ReadMacInt32(r.a[0]);
2346     WriteMacInt32(dce + dCtlDriver, ROM_BASE + sony_offset + 0x300);
2347     WriteMacInt16(dce + dCtlFlags, 0x4d00);
2348    
2349     r.a[0] = ROM_BASE + sony_offset + 0x400;
2350     r.d[0] = (uint32)-7;
2351     Execute68kTrap(0xa43d, &r); // DrvrInstallRsrvMem()
2352     r.a[0] = ReadMacInt32(ReadMacInt32(0x11c) + ~(-7) * 4); // Get driver handle from Unit Table
2353     Execute68kTrap(0xa029, &r); // HLock()
2354     dce = ReadMacInt32(r.a[0]);
2355     WriteMacInt32(dce + dCtlDriver, ROM_BASE + sony_offset + 0x400);
2356     WriteMacInt16(dce + dCtlFlags, 0x4e00);
2357    
2358     r.a[0] = ROM_BASE + sony_offset + 0x500;
2359     r.d[0] = (uint32)-8;
2360     Execute68kTrap(0xa43d, &r); // DrvrInstallRsrvMem()
2361     r.a[0] = ReadMacInt32(ReadMacInt32(0x11c) + ~(-8) * 4); // Get driver handle from Unit Table
2362     Execute68kTrap(0xa029, &r); // HLock()
2363     dce = ReadMacInt32(r.a[0]);
2364     WriteMacInt32(dce + dCtlDriver, ROM_BASE + sony_offset + 0x500);
2365     WriteMacInt16(dce + dCtlFlags, 0x4d00);
2366    
2367     r.a[0] = ROM_BASE + sony_offset + 0x600;
2368     r.d[0] = (uint32)-9;
2369     Execute68kTrap(0xa43d, &r); // DrvrInstallRsrvMem()
2370     r.a[0] = ReadMacInt32(ReadMacInt32(0x11c) + ~(-9) * 4); // Get driver handle from Unit Table
2371     Execute68kTrap(0xa029, &r); // HLock()
2372     dce = ReadMacInt32(r.a[0]);
2373     WriteMacInt32(dce + dCtlDriver, ROM_BASE + sony_offset + 0x600);
2374     WriteMacInt16(dce + dCtlFlags, 0x4e00);
2375     }