ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/rom_patches.cpp
(Generate patch)

Comparing SheepShaver/src/rom_patches.cpp (file contents):
Revision 1.1.1.1 by cebix, 2002-02-04T16:58:13Z vs.
Revision 1.18 by gbeauche, 2003-12-04T17:26:35Z

# Line 40 | Line 40
40   #include "audio_defs.h"
41   #include "serial.h"
42   #include "macos_util.h"
43 + #include "thunks.h"
44  
45   #define DEBUG 0
46   #include "debug.h"
# Line 58 | Line 59
59  
60  
61   // Other ROM addresses
62 < const uint32 CHECK_LOAD_PATCH_SPACE = 0x2f7f00;
63 < const uint32 PUT_SCRAP_PATCH_SPACE = 0x2f7f80;
64 < const uint32 GET_SCRAP_PATCH_SPACE = 0x2f7fc0;
65 < const uint32 ADDR_MAP_PATCH_SPACE = 0x2f8000;
62 > 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  
67   // Global variables
68   int ROMType;                            // ROM type
# Line 74 | Line 75 | static bool patch_nanokernel(void);
75   static bool patch_68k(void);
76  
77  
78 + // 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 +                uint32 next_offset = ntohl(parcel_data[0]);
129 +                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 +                        uint32 lzss_size = ((uintptr)src + next_offset) - ((uintptr)parcel_data + lzss_offset);
136 +                        decode_lzss((uint8 *)parcel_data + lzss_offset, dest, lzss_size);
137 +                }
138 +                parcel_offset = next_offset;
139 +        }
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   /*
203   *  Search ROM for byte string, return ROM offset (or 0)
204   */
# Line 323 | Line 448 | static const uint8 cdrom_driver[] = {  //
448          0x4e, 0x75                                                      //  rts
449   };
450  
451 < #ifdef __linux__
327 < static uint32 serial_nothing_tvect[2] = {(uint32)SerialNothing, 0};
328 < static uint32 serial_open_tvect[2] = {(uint32)SerialOpen, 0};
329 < static uint32 serial_prime_in_tvect[2] = {(uint32)SerialPrimeIn, 0};
330 < static uint32 serial_prime_out_tvect[2] = {(uint32)SerialPrimeOut, 0};
331 < static uint32 serial_control_tvect[2] = {(uint32)SerialControl, 0};
332 < static uint32 serial_status_tvect[2] = {(uint32)SerialStatus, 0};
333 < static uint32 serial_close_tvect[2] = {(uint32)SerialClose, 0};
334 < #endif
451 > static uint32 long_ptr;
452  
453 < static const uint32 ain_driver[] = {    // .AIn driver header
454 <        0x4d000000, 0x00000000,
455 <        0x00200040, 0x00600080,
456 <        0x00a0042e, 0x41496e00,
457 <        0x00000000, 0x00000000,
458 <        0xaafe0700, 0x00000000,
459 <        0x00000000, 0x00179822,
460 < #ifdef __linux__
461 <        0x00010004, (uint32)serial_nothing_tvect,
462 < #else
463 <        0x00010004, (uint32)SerialNothing,
464 < #endif
465 <        0x00000000, 0x00000000,
466 <        0xaafe0700, 0x00000000,
467 <        0x00000000, 0x00179822,
468 < #ifdef __linux__
469 <        0x00010004, (uint32)serial_prime_in_tvect,
470 < #else
471 <        0x00010004, (uint32)SerialPrimeIn,
472 < #endif
473 <        0x00000000, 0x00000000,
474 <        0xaafe0700, 0x00000000,
475 <        0x00000000, 0x00179822,
476 < #ifdef __linux__
477 <        0x00010004, (uint32)serial_control_tvect,
478 < #else
479 <        0x00010004, (uint32)SerialControl,
480 < #endif
481 <        0x00000000, 0x00000000,
482 <        0xaafe0700, 0x00000000,
483 <        0x00000000, 0x00179822,
484 < #ifdef __linux__
485 <        0x00010004, (uint32)serial_status_tvect,
486 < #else
487 <        0x00010004, (uint32)SerialStatus,
488 < #endif
489 <        0x00000000, 0x00000000,
490 <        0xaafe0700, 0x00000000,
491 <        0x00000000, 0x00179822,
492 < #ifdef __linux__
376 <        0x00010004, (uint32)serial_nothing_tvect,
377 < #else
378 <        0x00010004, (uint32)SerialNothing,
379 < #endif
380 <        0x00000000, 0x00000000,
453 > static void SetLongBase(uint32 addr)
454 > {
455 >        long_ptr = addr;
456 > }
457 >
458 > static void Long(uint32 value)
459 > {
460 >        WriteMacInt32(long_ptr, value);
461 >        long_ptr += 4;
462 > }
463 >
464 > static void gen_ain_driver(uintptr addr)
465 > {
466 >        SetLongBase(addr);
467 >
468 >        // .AIn driver header
469 >        Long(0x4d000000); Long(0x00000000);
470 >        Long(0x00200040); Long(0x00600080);
471 >        Long(0x00a0042e); Long(0x41496e00);
472 >        Long(0x00000000); Long(0x00000000);
473 >        Long(0xaafe0700); Long(0x00000000);
474 >        Long(0x00000000); Long(0x00179822);
475 >        Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_NOTHING));
476 >        Long(0x00000000); Long(0x00000000);
477 >        Long(0xaafe0700); Long(0x00000000);
478 >        Long(0x00000000); Long(0x00179822);
479 >        Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_PRIME_IN));
480 >        Long(0x00000000); Long(0x00000000);
481 >        Long(0xaafe0700); Long(0x00000000);
482 >        Long(0x00000000); Long(0x00179822);
483 >        Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_CONTROL));
484 >        Long(0x00000000); Long(0x00000000);
485 >        Long(0xaafe0700); Long(0x00000000);
486 >        Long(0x00000000); Long(0x00179822);
487 >        Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_STATUS));
488 >        Long(0x00000000); Long(0x00000000);
489 >        Long(0xaafe0700); Long(0x00000000);
490 >        Long(0x00000000); Long(0x00179822);
491 >        Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_NOTHING));
492 >        Long(0x00000000); Long(0x00000000);
493   };
494  
495 < static const uint32 aout_driver[] = {   // .AOut driver header
496 <        0x4d000000, 0x00000000,
497 <        0x00200040, 0x00600080,
498 <        0x00a0052e, 0x414f7574,
499 <        0x00000000, 0x00000000,
500 <        0xaafe0700, 0x00000000,
501 <        0x00000000, 0x00179822,
502 < #ifdef __linux__
503 <        0x00010004, (uint32)serial_open_tvect,
504 < #else
505 <        0x00010004, (uint32)SerialOpen,
506 < #endif
507 <        0x00000000, 0x00000000,
508 <        0xaafe0700, 0x00000000,
509 <        0x00000000, 0x00179822,
510 < #ifdef __linux__
511 <        0x00010004, (uint32)serial_prime_out_tvect,
512 < #else
513 <        0x00010004, (uint32)SerialPrimeOut,
514 < #endif
515 <        0x00000000, 0x00000000,
516 <        0xaafe0700, 0x00000000,
517 <        0x00000000, 0x00179822,
518 < #ifdef __linux__
519 <        0x00010004, (uint32)serial_control_tvect,
520 < #else
521 <        0x00010004, (uint32)SerialControl,
522 < #endif
523 <        0x00000000, 0x00000000,
412 <        0xaafe0700, 0x00000000,
413 <        0x00000000, 0x00179822,
414 < #ifdef __linux__
415 <        0x00010004, (uint32)serial_status_tvect,
416 < #else
417 <        0x00010004, (uint32)SerialStatus,
418 < #endif
419 <        0x00000000, 0x00000000,
420 <        0xaafe0700, 0x00000000,
421 <        0x00000000, 0x00179822,
422 < #ifdef __linux__
423 <        0x00010004, (uint32)serial_close_tvect,
424 < #else
425 <        0x00010004, (uint32)SerialClose,
426 < #endif
427 <        0x00000000, 0x00000000,
495 > static void gen_aout_driver(uintptr addr)
496 > {
497 >        SetLongBase(addr);
498 >
499 >        // .AOut driver header
500 >        Long(0x4d000000); Long(0x00000000);
501 >        Long(0x00200040); Long(0x00600080);
502 >        Long(0x00a0052e); Long(0x414f7574);
503 >        Long(0x00000000); Long(0x00000000);
504 >        Long(0xaafe0700); Long(0x00000000);
505 >        Long(0x00000000); Long(0x00179822);
506 >        Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_OPEN));
507 >        Long(0x00000000); Long(0x00000000);
508 >        Long(0xaafe0700); Long(0x00000000);
509 >        Long(0x00000000); Long(0x00179822);
510 >        Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_PRIME_OUT));
511 >        Long(0x00000000); Long(0x00000000);
512 >        Long(0xaafe0700); Long(0x00000000);
513 >        Long(0x00000000); Long(0x00179822);
514 >        Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_CONTROL));
515 >        Long(0x00000000); Long(0x00000000);
516 >        Long(0xaafe0700); Long(0x00000000);
517 >        Long(0x00000000); Long(0x00179822);
518 >        Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_STATUS));
519 >        Long(0x00000000); Long(0x00000000);
520 >        Long(0xaafe0700); Long(0x00000000);
521 >        Long(0x00000000); Long(0x00179822);
522 >        Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_CLOSE));
523 >        Long(0x00000000); Long(0x00000000);
524   };
525  
526 < static const uint32 bin_driver[] = {    // .BIn driver header
527 <        0x4d000000, 0x00000000,
528 <        0x00200040, 0x00600080,
529 <        0x00a0042e, 0x42496e00,
530 <        0x00000000, 0x00000000,
531 <        0xaafe0700, 0x00000000,
532 <        0x00000000, 0x00179822,
533 < #ifdef __linux__
534 <        0x00010004, (uint32)serial_nothing_tvect,
535 < #else
536 <        0x00010004, (uint32)SerialNothing,
537 < #endif
538 <        0x00000000, 0x00000000,
539 <        0xaafe0700, 0x00000000,
540 <        0x00000000, 0x00179822,
541 < #ifdef __linux__
542 <        0x00010004, (uint32)serial_prime_in_tvect,
543 < #else
544 <        0x00010004, (uint32)SerialPrimeIn,
545 < #endif
546 <        0x00000000, 0x00000000,
547 <        0xaafe0700, 0x00000000,
548 <        0x00000000, 0x00179822,
549 < #ifdef __linux__
550 <        0x00010004, (uint32)serial_control_tvect,
551 < #else
552 <        0x00010004, (uint32)SerialControl,
553 < #endif
554 <        0x00000000, 0x00000000,
459 <        0xaafe0700, 0x00000000,
460 <        0x00000000, 0x00179822,
461 < #ifdef __linux__
462 <        0x00010004, (uint32)serial_status_tvect,
463 < #else
464 <        0x00010004, (uint32)SerialStatus,
465 < #endif
466 <        0x00000000, 0x00000000,
467 <        0xaafe0700, 0x00000000,
468 <        0x00000000, 0x00179822,
469 < #ifdef __linux__
470 <        0x00010004, (uint32)serial_nothing_tvect,
471 < #else
472 <        0x00010004, (uint32)SerialNothing,
473 < #endif
474 <        0x00000000, 0x00000000,
526 > static void gen_bin_driver(uintptr addr)
527 > {
528 >        SetLongBase(addr);
529 >
530 >        // .BIn driver header
531 >        Long(0x4d000000); Long(0x00000000);
532 >        Long(0x00200040); Long(0x00600080);
533 >        Long(0x00a0042e); Long(0x42496e00);
534 >        Long(0x00000000); Long(0x00000000);
535 >        Long(0xaafe0700); Long(0x00000000);
536 >        Long(0x00000000); Long(0x00179822);
537 >        Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_NOTHING));
538 >        Long(0x00000000); Long(0x00000000);
539 >        Long(0xaafe0700); Long(0x00000000);
540 >        Long(0x00000000); Long(0x00179822);
541 >        Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_PRIME_IN));
542 >        Long(0x00000000); Long(0x00000000);
543 >        Long(0xaafe0700); Long(0x00000000);
544 >        Long(0x00000000); Long(0x00179822);
545 >        Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_CONTROL));
546 >        Long(0x00000000); Long(0x00000000);
547 >        Long(0xaafe0700); Long(0x00000000);
548 >        Long(0x00000000); Long(0x00179822);
549 >        Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_STATUS));
550 >        Long(0x00000000); Long(0x00000000);
551 >        Long(0xaafe0700); Long(0x00000000);
552 >        Long(0x00000000); Long(0x00179822);
553 >        Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_NOTHING));
554 >        Long(0x00000000); Long(0x00000000);
555   };
556  
557 < static const uint32 bout_driver[] = {   // .BOut driver header
558 <        0x4d000000, 0x00000000,
559 <        0x00200040, 0x00600080,
560 <        0x00a0052e, 0x424f7574,
561 <        0x00000000, 0x00000000,
562 <        0xaafe0700, 0x00000000,
563 <        0x00000000, 0x00179822,
564 < #ifdef __linux__
565 <        0x00010004, (uint32)serial_open_tvect,
566 < #else
567 <        0x00010004, (uint32)SerialOpen,
568 < #endif
569 <        0x00000000, 0x00000000,
570 <        0xaafe0700, 0x00000000,
571 <        0x00000000, 0x00179822,
572 < #ifdef __linux__
573 <        0x00010004, (uint32)serial_prime_out_tvect,
574 < #else
575 <        0x00010004, (uint32)SerialPrimeOut,
576 < #endif
577 <        0x00000000, 0x00000000,
578 <        0xaafe0700, 0x00000000,
579 <        0x00000000, 0x00179822,
580 < #ifdef __linux__
581 <        0x00010004, (uint32)serial_control_tvect,
582 < #else
583 <        0x00010004, (uint32)SerialControl,
584 < #endif
585 <        0x00000000, 0x00000000,
506 <        0xaafe0700, 0x00000000,
507 <        0x00000000, 0x00179822,
508 < #ifdef __linux__
509 <        0x00010004, (uint32)serial_status_tvect,
510 < #else
511 <        0x00010004, (uint32)SerialStatus,
512 < #endif
513 <        0x00000000, 0x00000000,
514 <        0xaafe0700, 0x00000000,
515 <        0x00000000, 0x00179822,
516 < #ifdef __linux__
517 <        0x00010004, (uint32)serial_close_tvect,
518 < #else
519 <        0x00010004, (uint32)SerialClose,
520 < #endif
521 <        0x00000000, 0x00000000,
557 > static void gen_bout_driver(uintptr addr)
558 > {
559 >        SetLongBase(addr);
560 >
561 >        // .BOut driver header
562 >        Long(0x4d000000); Long(0x00000000);
563 >        Long(0x00200040); Long(0x00600080);
564 >        Long(0x00a0052e); Long(0x424f7574);
565 >        Long(0x00000000); Long(0x00000000);
566 >        Long(0xaafe0700); Long(0x00000000);
567 >        Long(0x00000000); Long(0x00179822);
568 >        Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_OPEN));
569 >        Long(0x00000000); Long(0x00000000);
570 >        Long(0xaafe0700); Long(0x00000000);
571 >        Long(0x00000000); Long(0x00179822);
572 >        Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_PRIME_OUT));
573 >        Long(0x00000000); Long(0x00000000);
574 >        Long(0xaafe0700); Long(0x00000000);
575 >        Long(0x00000000); Long(0x00179822);
576 >        Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_CONTROL));
577 >        Long(0x00000000); Long(0x00000000);
578 >        Long(0xaafe0700); Long(0x00000000);
579 >        Long(0x00000000); Long(0x00179822);
580 >        Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_STATUS));
581 >        Long(0x00000000); Long(0x00000000);
582 >        Long(0xaafe0700); Long(0x00000000);
583 >        Long(0x00000000); Long(0x00179822);
584 >        Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_CLOSE));
585 >        Long(0x00000000); Long(0x00000000);
586   };
587  
588   static const uint8 adbop_patch[] = {    // Call ADBOp() completion procedure
# Line 545 | Line 609 | static const uint8 adbop_patch[] = {   //
609  
610  
611   /*
612 + *  Copy PowerPC code to ROM image and reverse bytes if necessary
613 + */
614 +
615 + static inline void memcpy_powerpc_code(void *dst, const void *src, size_t len)
616 + {
617 + #ifdef WORDS_BIGENDIAN
618 +        (void)memcpy(dst, src, len);
619 + #else
620 +        uint32 *d = (uint32 *)dst;
621 +        uint32 *s = (uint32 *)src;
622 +        for (int i = 0; i < len/4; i++)
623 +                d[i] = htonl(s[i]);
624 + #endif
625 + }
626 +
627 +
628 + /*
629   *  Install ROM patches (RAMBase and KernelDataAddr must be set)
630   */
631  
# Line 567 | Line 648 | bool PatchROM(void)
648                  ROMType = ROMTYPE_ZANZIBAR;
649          else if (!memcmp((void *)(ROM_BASE + 0x30d064), "Boot Gazelle", 12))
650                  ROMType = ROMTYPE_GAZELLE;
651 +        else if (!memcmp((void *)(ROM_BASE + 0x30d064), "Boot Gossamer", 13))
652 +                ROMType = ROMTYPE_GOSSAMER;
653          else if (!memcmp((void *)(ROM_BASE + 0x30d064), "NewWorld", 8))
654                  ROMType = ROMTYPE_NEWWORLD;
655          else
656                  return false;
657  
658 +        // Check that other ROM addresses point to really free regions
659 +        if (ntohl(*(uint32 *)(ROM_BASE + CHECK_LOAD_PATCH_SPACE)) != 0x6b636b63)
660 +                return false;
661 +        if (ntohl(*(uint32 *)(ROM_BASE + PUT_SCRAP_PATCH_SPACE)) != 0x6b636b63)
662 +                return false;
663 +        if (ntohl(*(uint32 *)(ROM_BASE + GET_SCRAP_PATCH_SPACE)) != 0x6b636b63)
664 +                return false;
665 +        if (ntohl(*(uint32 *)(ROM_BASE + ADDR_MAP_PATCH_SPACE)) != 0x6b636b63)
666 +                return false;
667 +
668          // Apply patches
669          if (!patch_nanokernel_boot()) return false;
670          if (!patch_68k_emul()) return false;
# Line 616 | Line 709 | static bool patch_nanokernel_boot(void)
709          lp[0xfd8 >> 2] = htonl(ROM_BASE + 0x2a);                // 68k reset vector
710  
711          // Skip SR/BAT/SDR init
712 <        if (ROMType == ROMTYPE_GAZELLE || ROMType == ROMTYPE_NEWWORLD) {
712 >        if (ROMType == ROMTYPE_GAZELLE || ROMType == ROMTYPE_GOSSAMER || ROMType == ROMTYPE_NEWWORLD) {
713                  lp = (uint32 *)(ROM_BASE + 0x310000);
714                  *lp++ = htonl(POWERPC_NOP);
715                  *lp = htonl(0x38000000);
716          }
717 <        static const uint32 sr_init_loc[] = {0x3101b0, 0x3101b0, 0x3101b0, 0x3101ec, 0x310200};
717 >        static const uint32 sr_init_loc[] = {0x3101b0, 0x3101b0, 0x3101b0, 0x3101ec, 0x3101fc, 0x310200};
718          lp = (uint32 *)(ROM_BASE + 0x310008);
719          *lp = htonl(0x48000000 | (sr_init_loc[ROMType] - 8) & 0xffff);  // b            ROM_BASE+0x3101b0
720          lp = (uint32 *)(ROM_BASE + sr_init_loc[ROMType]);
# Line 631 | Line 724 | static bool patch_nanokernel_boot(void)
724          *lp = htonl(0x3de00010);                // lis  r15,0x0010      (size of kernel memory)
725  
726          // Don't read PVR
727 <        static const uint32 pvr_loc[] = {0x3103b0, 0x3103b4, 0x3103b4, 0x310400, 0x310438};
727 >        static const uint32 pvr_loc[] = {0x3103b0, 0x3103b4, 0x3103b4, 0x310400, 0x310430, 0x310438};
728          lp = (uint32 *)(ROM_BASE + pvr_loc[ROMType]);
729          *lp = htonl(0x81800000 + XLM_PVR);      // lwz  r12,(theoretical PVR)
730  
# Line 779 | Line 872 | static bool patch_nanokernel_boot(void)
872          *lp = htonl(POWERPC_NOP);
873  
874          // Don't read PVR
875 <        static const uint32 pvr_ofs[] = {0x138, 0x138, 0x138, 0x140, 0x148};
875 >        static const uint32 pvr_ofs[] = {0x138, 0x138, 0x138, 0x140, 0x148, 0x148};
876          lp = (uint32 *)(ROM_BASE + loc + pvr_ofs[ROMType]);
877          *lp = htonl(0x82e00000 + XLM_PVR);              // lwz  r23,(theoretical PVR)
878          lp = (uint32 *)(ROM_BASE + loc + 0x170);
879 <        if (ntohl(*lp) == 0x7eff42a6)   // NewWorld ROM
879 >        if (ntohl(*lp) == 0x7eff42a6)   // NewWorld or Gossamer ROM
880                  *lp = htonl(0x82e00000 + XLM_PVR);      // lwz  r23,(theoretical PVR)
881          lp = (uint32 *)(ROM_BASE + 0x313134);
882          if (ntohl(*lp) == 0x7e5f42a6)
# Line 791 | Line 884 | static bool patch_nanokernel_boot(void)
884          lp = (uint32 *)(ROM_BASE + 0x3131f4);
885          if (ntohl(*lp) == 0x7e5f42a6)   // NewWorld ROM
886                  *lp = htonl(0x82400000 + XLM_PVR);      // lwz  r18,(theoretical PVR)
887 +        lp = (uint32 *)(ROM_BASE + 0x314600);
888 +        if (ntohl(*lp) == 0x7d3f42a6)
889 +                *lp = htonl(0x81200000 + XLM_PVR);      // lzw  r9,(theoritical PVR)
890  
891          // Don't read SDR1
892 <        static const uint32 sdr1_ofs[] = {0x174, 0x174, 0x174, 0x17c, 0x19c};
892 >        static const uint32 sdr1_ofs[] = {0x174, 0x174, 0x174, 0x17c, 0x19c, 0x19c};
893          lp = (uint32 *)(ROM_BASE + loc + sdr1_ofs[ROMType]);
894          *lp++ = htonl(0x3d00dead);              // lis  r8,0xdead               (pointer to page table)
895          *lp++ = htonl(0x3ec0001f);              // lis  r22,0x001f      (size of page table)
896          *lp = htonl(POWERPC_NOP);
897  
898          // Don't clear page table
899 <        static const uint32 pgtb_ofs[] = {0x198, 0x198, 0x198, 0x1a0, 0x1c4};
899 >        static const uint32 pgtb_ofs[] = {0x198, 0x198, 0x198, 0x1a0, 0x1c0, 0x1c4};
900          lp = (uint32 *)(ROM_BASE + loc + pgtb_ofs[ROMType]);
901          *lp = htonl(POWERPC_NOP);
902  
903          // Don't invalidate TLB
904 <        static const uint32 tlb_ofs[] = {0x1a0, 0x1a0, 0x1a0, 0x1a8, 0x1cc};
904 >        static const uint32 tlb_ofs[] = {0x1a0, 0x1a0, 0x1a0, 0x1a8, 0x1c8, 0x1cc};
905          lp = (uint32 *)(ROM_BASE + loc + tlb_ofs[ROMType]);
906          *lp = htonl(POWERPC_NOP);
907  
908          // Don't create RAM descriptor table
909 <        static const uint32 desc_ofs[] = {0x350, 0x350, 0x350, 0x358, 0x37c};
909 >        static const uint32 desc_ofs[] = {0x350, 0x350, 0x350, 0x358, 0x378, 0x37c};
910          lp = (uint32 *)(ROM_BASE + loc + desc_ofs[ROMType]);
911          *lp = htonl(POWERPC_NOP);
912  
913          // Don't load SRs and BATs
914 <        static const uint32 sr_ofs[] = {0x3d8, 0x3d8, 0x3d8, 0x3e0, 0x404};
914 >        static const uint32 sr_ofs[] = {0x3d8, 0x3d8, 0x3d8, 0x3e0, 0x400, 0x404};
915          lp = (uint32 *)(ROM_BASE + loc + sr_ofs[ROMType]);
916          *lp = htonl(POWERPC_NOP);
917  
918          // Don't mess with SRs
919 <        static const uint32 sr2_ofs[] = {0x312118, 0x312118, 0x312118, 0x312118, 0x3121b4};
919 >        static const uint32 sr2_ofs[] = {0x312118, 0x312118, 0x312118, 0x312118, 0x312118, 0x3121b4};
920          lp = (uint32 *)(ROM_BASE + sr2_ofs[ROMType]);
921          *lp = htonl(POWERPC_BLR);
922  
923          // Don't check performance monitor
924 <        static const uint32 pm_ofs[] = {0x313148, 0x313148, 0x313148, 0x313148, 0x313218};
924 >        static const uint32 pm_ofs[] = {0x313148, 0x313148, 0x313148, 0x313148, 0x313158, 0x313218};
925          lp = (uint32 *)(ROM_BASE + pm_ofs[ROMType]);
926          while (ntohl(*lp) != 0x7e58eba6) lp++;
927          *lp++ = htonl(POWERPC_NOP);
# Line 861 | Line 957 | static bool patch_nanokernel_boot(void)
957          *lp++ = htonl(POWERPC_NOP);
958  
959          // Jump to 68k emulator
960 <        static const uint32 jump68k_ofs[] = {0x40c, 0x40c, 0x40c, 0x414, 0x438};
960 >        static const uint32 jump68k_ofs[] = {0x40c, 0x40c, 0x40c, 0x414, 0x434, 0x438};
961          lp = (uint32 *)(ROM_BASE + loc + jump68k_ofs[ROMType]);
962          *lp++ = htonl(0x80610634);              // lwz  r3,0x0634(r1)   (pointer to Emulator Data)
963          *lp++ = htonl(0x8081119c);              // lwz  r4,0x119c(r1)   (pointer to opcode table)
# Line 882 | Line 978 | static bool patch_68k_emul(void)
978          uint32 base;
979  
980          // Overwrite twi instructions
981 <        static const uint32 twi_loc[] = {0x36e680, 0x36e6c0, 0x36e6c0, 0x36e6c0, 0x36e740};
981 >        static const uint32 twi_loc[] = {0x36e680, 0x36e6c0, 0x36e6c0, 0x36e6c0, 0x36e740, 0x36e740};
982          base = twi_loc[ROMType];
983          lp = (uint32 *)(ROM_BASE + base);
984          *lp++ = htonl(0x48000000 + 0x36f900 - base);            // b 0x36f900 (Emulator start)
# Line 903 | Line 999 | static bool patch_68k_emul(void)
999          *lp = htonl(POWERPC_ILLEGAL);
1000  
1001   #if EMULATED_PPC
1002 <        // Install EMUL_RETURN, EXEC_RETURN and EMUL_OP opcodes
1002 >        // Install EMUL_RETURN, EXEC_RETURN, EXEC_NATIVE and EMUL_OP opcodes
1003          lp = (uint32 *)(ROM_BASE + 0x380000 + (M68K_EMUL_RETURN << 3));
1004          *lp++ = htonl(POWERPC_EMUL_OP);
1005          *lp++ = htonl(0x4bf66e80);                                                      // b    0x366084
1006          *lp++ = htonl(POWERPC_EMUL_OP | 1);
1007          *lp++ = htonl(0x4bf66e78);                                                      // b    0x366084
1008 +        *lp++ = htonl(POWERPC_EMUL_OP | 2);
1009 +        *lp++ = htonl(0x4bf66e70);                                                      // b    0x366084
1010          for (int i=0; i<OP_MAX; i++) {
1011 <                *lp++ = htonl(POWERPC_EMUL_OP | (i + 2));
1012 <                *lp++ = htonl(0x4bf66e70 - i*8);                        // b    0x366084
1011 >                *lp++ = htonl(POWERPC_EMUL_OP | (i + 3));
1012 >                *lp++ = htonl(0x4bf66e68 - i*8);                                // b    0x366084
1013          }
1014   #else
1015          // Install EMUL_RETURN, EXEC_RETURN and EMUL_OP opcodes
# Line 920 | Line 1018 | static bool patch_68k_emul(void)
1018          *lp++ = htonl(0x4bf705fc);                                                      // b    0x36f800
1019          *lp++ = htonl(0x80000000 + XLM_EXEC_RETURN_PROC);       // lwz  r0,XLM_EXEC_RETURN_PROC
1020          *lp++ = htonl(0x4bf705f4);                                                      // b    0x36f800
1021 +        *lp++ = htonl(0x00dead00);                                                      // Let SheepShaver crash, since
1022 +        *lp++ = htonl(0x00beef00);                                                      // no native opcode is available
1023          for (int i=0; i<OP_MAX; i++) {
1024                  *lp++ = htonl(0x38a00000 + i);                          // li   r5,OP_*
1025 <                *lp++ = htonl(0x4bf705f4 - i*8);                        // b    0x36f808
1025 >                *lp++ = htonl(0x4bf705ec - i*8);                        // b    0x36f808
1026          }
1027  
1028          // Extra routines for EMUL_RETURN/EXEC_RETURN/EMUL_OP
# Line 938 | Line 1038 | static bool patch_68k_emul(void)
1038          // Extra routine for 68k emulator start
1039          lp = (uint32 *)(ROM_BASE + 0x36f900);
1040          *lp++ = htonl(0x7c2903a6);                                      // mtctr        r1
1041 + #if EMULATED_PPC
1042 +        *lp++ = htonl(NativeOpcode(NATIVE_DISABLE_INTERRUPT));
1043 + #else
1044          *lp++ = htonl(0x80200000 + XLM_IRQ_NEST);       // lwz          r1,XLM_IRQ_NEST
1045          *lp++ = htonl(0x38210001);                                      // addi         r1,r1,1
1046          *lp++ = htonl(0x90200000 + XLM_IRQ_NEST);       // stw          r1,XLM_IRQ_NEST
1047 + #endif
1048          *lp++ = htonl(0x80200000 + XLM_KERNEL_DATA);// lwz              r1,XLM_KERNEL_DATA
1049          *lp++ = htonl(0x90c10018);                                      // stw          r6,0x18(r1)
1050          *lp++ = htonl(0x7cc902a6);                                      // mfctr        r6
# Line 968 | Line 1072 | static bool patch_68k_emul(void)
1072          // Extra routine for Mixed Mode
1073          lp = (uint32 *)(ROM_BASE + 0x36fa00);
1074          *lp++ = htonl(0x7c2903a6);                                      // mtctr        r1
1075 + #if EMULATED_PPC
1076 +        *lp++ = htonl(NativeOpcode(NATIVE_DISABLE_INTERRUPT));
1077 + #else
1078          *lp++ = htonl(0x80200000 + XLM_IRQ_NEST);       // lwz          r1,XLM_IRQ_NEST
1079          *lp++ = htonl(0x38210001);                                      // addi         r1,r1,1
1080          *lp++ = htonl(0x90200000 + XLM_IRQ_NEST);       // stw          r1,XLM_IRQ_NEST
1081 + #endif
1082          *lp++ = htonl(0x80200000 + XLM_KERNEL_DATA);// lwz              r1,XLM_KERNEL_DATA
1083          *lp++ = htonl(0x90c10018);                                      // stw          r6,0x18(r1)
1084          *lp++ = htonl(0x7cc902a6);                                      // mfctr        r6
# Line 996 | Line 1104 | static bool patch_68k_emul(void)
1104          *lp = htonl(0x4e800020);                                        // blr
1105  
1106          // Extra routine for Reset/FC1E opcode
1107 <        lp = (uint32 *)(ROM_BASE + 0x36fc00);
1107 >        lp = (uint32 *)(ROM_BASE + 0x36fb00);
1108          *lp++ = htonl(0x7c2903a6);                                      // mtctr        r1
1109 + #if EMULATED_PPC
1110 +        *lp++ = htonl(NativeOpcode(NATIVE_DISABLE_INTERRUPT));
1111 + #else
1112          *lp++ = htonl(0x80200000 + XLM_IRQ_NEST);       // lwz          r1,XLM_IRQ_NEST
1113          *lp++ = htonl(0x38210001);                                      // addi         r1,r1,1
1114          *lp++ = htonl(0x90200000 + XLM_IRQ_NEST);       // stw          r1,XLM_IRQ_NEST
1115 + #endif
1116          *lp++ = htonl(0x80200000 + XLM_KERNEL_DATA);// lwz              r1,XLM_KERNEL_DATA
1117          *lp++ = htonl(0x90c10018);                                      // stw          r6,0x18(r1)
1118          *lp++ = htonl(0x7cc902a6);                                      // mfctr        r6
# Line 1017 | Line 1129 | static bool patch_68k_emul(void)
1129          *lp++ = htonl(0x80e10660);                                      // lwz          r7,$0660(r1)
1130          *lp++ = htonl(0x7d8802a6);                                      // mflr         r12
1131          *lp++ = htonl(0x50e74001);                                      // rlwimi.      r7,r7,8,$80000000
1132 <        *lp++ = htonl(0x814105f4);                                      // lwz          r10,0x05f8(r1)
1132 >        *lp++ = htonl(0x814105f8);                                      // lwz          r10,0x05f8(r1)
1133          *lp++ = htonl(0x7d4803a6);                                      // mtlr         r10
1134          *lp++ = htonl(0x7d8a6378);                                      // mr           r10,r12
1135          *lp++ = htonl(0x3d600002);                                      // lis          r11,0x0002
# Line 1028 | Line 1140 | static bool patch_68k_emul(void)
1140          // Extra routine for FE0A opcode (QuickDraw 3D needs this)
1141          lp = (uint32 *)(ROM_BASE + 0x36fc00);
1142          *lp++ = htonl(0x7c2903a6);                                      // mtctr        r1
1143 + #if EMULATED_PPC
1144 +        *lp++ = htonl(NativeOpcode(NATIVE_DISABLE_INTERRUPT));
1145 + #else
1146          *lp++ = htonl(0x80200000 + XLM_IRQ_NEST);       // lwz          r1,XLM_IRQ_NEST
1147          *lp++ = htonl(0x38210001);                                      // addi         r1,r1,1
1148          *lp++ = htonl(0x90200000 + XLM_IRQ_NEST);       // stw          r1,XLM_IRQ_NEST
1149 + #endif
1150          *lp++ = htonl(0x80200000 + XLM_KERNEL_DATA);// lwz              r1,XLM_KERNEL_DATA
1151          *lp++ = htonl(0x90c10018);                                      // stw          r6,0x18(r1)
1152          *lp++ = htonl(0x7cc902a6);                                      // mfctr        r6
# Line 1047 | Line 1163 | static bool patch_68k_emul(void)
1163          *lp++ = htonl(0x80e10660);                                      // lwz          r7,$0660(r1)
1164          *lp++ = htonl(0x7d8802a6);                                      // mflr         r12
1165          *lp++ = htonl(0x50e74001);                                      // rlwimi.      r7,r7,8,$80000000
1166 <        *lp++ = htonl(0x814105f4);                                      // lwz          r10,0x05fc(r1)
1166 >        *lp++ = htonl(0x814105fc);                                      // lwz          r10,0x05fc(r1)
1167          *lp++ = htonl(0x7d4803a6);                                      // mtlr         r10
1168          *lp++ = htonl(0x7d8a6378);                                      // mr           r10,r12
1169          *lp++ = htonl(0x3d600002);                                      // lis          r11,0x0002
# Line 1165 | Line 1281 | static bool patch_nanokernel(void)
1281          *lp = htonl(POWERPC_BCTR);
1282  
1283          lp = (uint32 *)(ROM_BASE + 0x318000);
1284 + #if EMULATED_PPC
1285 +        *lp++ = htonl(NativeOpcode(NATIVE_ENABLE_INTERRUPT));
1286 +        *lp = htonl(0x48000000 + ((xlp - 0x8004) & 0x03fffffc));        // b            ROM_BASE+0x312c2c
1287 + #else
1288          *lp++ = htonl(0x81400000 + XLM_IRQ_NEST);       // lwz  r10,XLM_IRQ_NEST
1289          *lp++ = htonl(0x394affff);                                      // subi r10,r10,1
1290          *lp++ = htonl(0x91400000 + XLM_IRQ_NEST);       // stw  r10,XLM_IRQ_NEST
1291          *lp = htonl(0x48000000 + ((xlp - 0x800c) & 0x03fffffc));        // b            ROM_BASE+0x312c2c
1292 + #endif
1293 +
1294   /*
1295          // Disable FE0A/FE06 opcodes
1296          lp = (uint32 *)(ROM_BASE + 0x3144ac);
# Line 1214 | Line 1336 | static bool patch_68k(void)
1336          // Patch UniversalInfo
1337          if (ROMType == ROMTYPE_NEWWORLD) {
1338                  static const uint8 univ_info_dat[] = {0x3f, 0xff, 0x04, 0x00};
1339 <                if ((base = find_rom_data(0x14000, 0x16000, univ_info_dat, sizeof(univ_info_dat))) == 0) return false;
1339 >                if ((base = find_rom_data(0x14000, 0x18000, univ_info_dat, sizeof(univ_info_dat))) == 0) return false;
1340                  D(bug("universal_info %08lx\n", base));
1341                  lp = (uint32 *)(ROM_BASE + base - 0x14);
1342                  lp[0x00 >> 2] = htonl(ADDR_MAP_PATCH_SPACE - (base - 0x14));
# Line 1238 | Line 1360 | static bool patch_68k(void)
1360                  lp[0x28 >> 2] = htonl(0x00000861);
1361                  lp[0x58 >> 2] = htonl(0x30200000);
1362                  lp[0x60 >> 2] = htonl(0x0000003d);
1363 +        } else if (ROMType == ROMTYPE_GOSSAMER) {
1364 +                base = 0x12d20;
1365 +                lp = (uint32 *)(ROM_BASE + base - 0x14);
1366 +                lp[0x00 >> 2] = htonl(ADDR_MAP_PATCH_SPACE - (base - 0x14));
1367 +                lp[0x10 >> 2] = htonl(0xcc003d11);              // Make it like the PowerMac 9500 UniversalInfo
1368 +                lp[0x14 >> 2] = htonl(0x3fff0401);
1369 +                lp[0x18 >> 2] = htonl(0x0300001c);
1370 +                lp[0x1c >> 2] = htonl(0x000108c4);
1371 +                lp[0x24 >> 2] = htonl(0xc301bf26);
1372 +                lp[0x28 >> 2] = htonl(0x00000861);
1373 +                lp[0x58 >> 2] = htonl(0x30410000);
1374 +                lp[0x60 >> 2] = htonl(0x0000003d);
1375          }
1376  
1377          // Construct AddrMap for NewWorld ROM
1378 <        if (ROMType == ROMTYPE_NEWWORLD || ROMType == ROMTYPE_ZANZIBAR) {
1378 >        if (ROMType == ROMTYPE_NEWWORLD || ROMType == ROMTYPE_ZANZIBAR || ROMType == ROMTYPE_GOSSAMER) {
1379                  lp = (uint32 *)(ROM_BASE + ADDR_MAP_PATCH_SPACE);
1380                  memset(lp - 10, 0, 0x128);
1381                  lp[-10] = htonl(0x0300001c);
# Line 1367 | Line 1501 | static bool patch_68k(void)
1501                  *wp++ = htons(M68K_EMUL_OP_XPRAM2);
1502                  *wp = htons(0x4ed3);                    // jmp  (a3)
1503  
1504 <                static const uint32 nvram3_loc[] = {0x582f0, 0xa0a0, 0x7e50, 0xa1d0, 0};
1505 <                wp = (uint16 *)(ROM_BASE + nvram3_loc[ROMType]);
1504 >                static const uint8 nvram3_dat[] = {0x4e, 0xd3, 0x06, 0x41, 0x13, 0x00};
1505 >                if ((base = find_rom_data(0x7000, 0xb000, nvram3_dat, sizeof(nvram3_dat))) == 0) return false;
1506 >                D(bug("nvram3 %08lx\n", base));
1507 >                wp = (uint16 *)(ROM_BASE + base + 2);
1508 >                *wp++ = htons(M68K_EMUL_OP_XPRAM3);
1509 >                *wp = htons(0x4ed3);                    // jmp  (a3)
1510 >
1511 >                static const uint32 nvram4_loc[] = {0x582f0, 0xa0a0, 0x7e50, 0xa1d0, 0x538d0, 0};
1512 >                wp = (uint16 *)(ROM_BASE + nvram4_loc[ROMType]);
1513                  *wp++ = htons(0x202f);                  // move.l       4(sp),d0
1514                  *wp++ = htons(0x0004);
1515                  *wp++ = htons(M68K_EMUL_OP_NVRAM1);
# Line 1381 | Line 1522 | static bool patch_68k(void)
1522                          *wp = htons(0x0004);
1523                  }
1524  
1525 <                static const uint32 nvram4_loc[] = {0x58460, 0xa0f0, 0x7f40, 0xa220, 0};
1526 <                wp = (uint16 *)(ROM_BASE + nvram4_loc[ROMType]);
1525 >                static const uint32 nvram5_loc[] = {0x58460, 0xa0f0, 0x7f40, 0xa220, 0x53a20, 0};
1526 >                wp = (uint16 *)(ROM_BASE + nvram5_loc[ROMType]);
1527                  if (ROMType == ROMTYPE_ZANZIBAR || ROMType == ROMTYPE_GAZELLE) {
1528                          *wp++ = htons(0x202f);                  // move.l       4(sp),d0
1529                          *wp++ = htons(0x0004);
# Line 1559 | Line 1700 | static bool patch_68k(void)
1700                  if ((base = find_rom_data(0x13000, 0x20000, gc_mask2_dat, sizeof(gc_mask2_dat))) == 0) return false;
1701                  D(bug("gc_mask2 %08lx\n", base));
1702                  wp = (uint16 *)(ROM_BASE + base);
1703 +                if (ROMType == ROMTYPE_GOSSAMER)
1704 +                        *wp++ = htons(M68K_NOP);
1705                  for (int i=0; i<5; i++) {
1706                          *wp++ = htons(M68K_NOP);
1707                          *wp++ = htons(M68K_NOP);
# Line 1566 | Line 1709 | static bool patch_68k(void)
1709                          *wp++ = htons(M68K_NOP);
1710                          wp += 2;
1711                  }
1712 <                if (ROMType == ROMTYPE_ZANZIBAR) {
1712 >                if (ROMType == ROMTYPE_ZANZIBAR || ROMType == ROMTYPE_GOSSAMER) {
1713                          for (int i=0; i<6; i++) {
1714                                  *wp++ = htons(M68K_NOP);
1715                                  *wp++ = htons(M68K_NOP);
# Line 1592 | Line 1735 | static bool patch_68k(void)
1735  
1736          // Patch GetCPUSpeed (via 0x27a) (some ROMs have two of them)
1737          static const uint8 cpu_speed_dat[] = {0x20, 0x30, 0x81, 0xf2, 0x5f, 0xff, 0xef, 0xd8, 0x00, 0x04, 0x4c, 0x7c};
1738 <        if ((base = find_rom_data(0x6000, 0x9000, cpu_speed_dat, sizeof(cpu_speed_dat))) == 0) return false;
1738 >        if ((base = find_rom_data(0x6000, 0xa000, cpu_speed_dat, sizeof(cpu_speed_dat))) == 0) return false;
1739          D(bug("cpu_speed %08lx\n", base));
1740          wp = (uint16 *)(ROM_BASE + base);
1741          *wp++ = htons(0x203c);                  // move.l       #(MHz<<16)|MHz,d0
1742          *wp++ = htons(CPUClockSpeed / 1000000);
1743          *wp++ = htons(CPUClockSpeed / 1000000);
1744          *wp = htons(M68K_RTS);
1745 <        if ((base = find_rom_data(base, 0x9000, cpu_speed_dat, sizeof(cpu_speed_dat))) != 0) {
1745 >        if ((base = find_rom_data(base, 0xa000, cpu_speed_dat, sizeof(cpu_speed_dat))) != 0) {
1746                  D(bug("cpu_speed2 %08lx\n", base));
1747                  wp = (uint16 *)(ROM_BASE + base);
1748                  *wp++ = htons(0x203c);                  // move.l       #(MHz<<16)|MHz,d0
# Line 1639 | Line 1782 | static bool patch_68k(void)
1782          *wp = htons(M68K_RTS);
1783  
1784          // Don't install Time Manager task for 60Hz interrupt (Enable60HzInts, via 0x2b8)
1785 <        if (ROMType == ROMTYPE_NEWWORLD) {
1785 >        if (ROMType == ROMTYPE_NEWWORLD || ROMType == ROMTYPE_GOSSAMER) {
1786                  static const uint8 tm_task_dat[] = {0x30, 0x3c, 0x4e, 0x2b, 0xa9, 0xc9};
1787 <                if ((base = find_rom_data(0x2e0, 0x320, tm_task_dat, sizeof(tm_task_dat))) == 0) return false;
1787 >                if ((base = find_rom_data(0x2a0, 0x320, tm_task_dat, sizeof(tm_task_dat))) == 0) return false;
1788                  D(bug("tm_task %08lx\n", base));
1789                  wp = (uint16 *)(ROM_BASE + base + 28);
1790                  *wp++ = htons(M68K_NOP);
# Line 1661 | Line 1804 | static bool patch_68k(void)
1804          }
1805  
1806          // Don't read PVR from 0x5fffef80 in DriverServicesLib (via 0x316)
1807 <        if (ROMType != ROMTYPE_NEWWORLD) {
1807 >        if (ROMType != ROMTYPE_NEWWORLD && ROMType != ROMTYPE_GOSSAMER) {
1808                  uint32 dsl_offset = find_rom_resource(FOURCC('n','l','i','b'), -16401);
1809                  if (ROMType == ROMTYPE_ZANZIBAR) {
1810                          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};
# Line 1747 | Line 1890 | static bool patch_68k(void)
1890                          D(bug("scsi_var2 %08lx\n", base));
1891                          wp = (uint16 *)(ROM_BASE + base);
1892                          *wp++ = htons(0x7000);  // moveq #0,d0
1893 <                        *wp = htons(M68K_RTS);  // bra
1893 >                        *wp = htons(M68K_RTS);
1894 >                }
1895 >        }
1896 >        else if (ROMType == ROMTYPE_GOSSAMER) {
1897 >                static const uint8 scsi_var_dat[] = {0x70, 0x01, 0xa0, 0x89, 0x4a, 0x6e, 0xfe, 0xac, 0x4f, 0xef, 0x00, 0x10, 0x66, 0x00};
1898 >                if ((base = find_rom_data(0x1d700, 0x1d800, scsi_var_dat, sizeof(scsi_var_dat))) != 0) {
1899 >                        D(bug("scsi_var %08lx\n", base));
1900 >                        wp = (uint16 *)(ROM_BASE + base + 12);
1901 >                        *wp = htons(0x6000);    // bra
1902 >                }
1903 >
1904 >                static const uint8 scsi_var2_dat[] = {0x4e, 0x56, 0xfc, 0x5a, 0x48, 0xe7, 0x1f, 0x38};
1905 >                if ((base = find_rom_data(0x1d900, 0x1da00, scsi_var2_dat, sizeof(scsi_var2_dat))) != 0) {
1906 >                        D(bug("scsi_var2 %08lx\n", base));
1907 >                        wp = (uint16 *)(ROM_BASE + base);
1908 >                        *wp++ = htons(0x7000);  // moveq #0,d0
1909 >                        *wp = htons(M68K_RTS);
1910                  }
1911          }
1912   #endif
# Line 1803 | Line 1962 | static bool patch_68k(void)
1962          memcpy((void *)(ROM_BASE + sony_offset + 0x200), cdrom_driver, sizeof(cdrom_driver));
1963  
1964          // Install serial drivers
1965 <        memcpy((void *)(ROM_BASE + sony_offset + 0x300), ain_driver, sizeof(ain_driver));
1966 <        memcpy((void *)(ROM_BASE + sony_offset + 0x400), aout_driver, sizeof(aout_driver));
1967 <        memcpy((void *)(ROM_BASE + sony_offset + 0x500), bin_driver, sizeof(bin_driver));
1968 <        memcpy((void *)(ROM_BASE + sony_offset + 0x600), bout_driver, sizeof(bout_driver));
1965 >        gen_ain_driver( ROM_BASE + sony_offset + 0x300);
1966 >        gen_aout_driver(ROM_BASE + sony_offset + 0x400);
1967 >        gen_bin_driver( ROM_BASE + sony_offset + 0x500);
1968 >        gen_bout_driver(ROM_BASE + sony_offset + 0x600);
1969  
1970          // Copy icons to ROM
1971          SonyDiskIconAddr = ROM_BASE + sony_offset + 0x800;
# Line 1827 | Line 1986 | static bool patch_68k(void)
1986          *wp = htons(M68K_RTS);
1987  
1988          // Don't install serial drivers from ROM
1989 <        if (ROMType == ROMTYPE_ZANZIBAR || ROMType == ROMTYPE_NEWWORLD) {
1989 >        if (ROMType == ROMTYPE_ZANZIBAR || ROMType == ROMTYPE_NEWWORLD || ROMType == ROMTYPE_GOSSAMER) {
1990                  wp = (uint16 *)(ROM_BASE + find_rom_resource(FOURCC('S','E','R','D'), 0));
1991                  *wp = htons(M68K_RTS);
1992          } else {
# Line 1886 | Line 2045 | static bool patch_68k(void)
2045          wp = (uint16 *)(ROM_BASE + base);
2046          if (ROMType == ROMTYPE_ZANZIBAR)
2047                  *wp = htons(M68K_RTS);
2048 <        else
2048 >        else if (ntohs(wp[-4]) == 0x61ff)
2049 >                *wp = htons(M68K_RTS);
2050 >        else if (ntohs(wp[-2]) == 0x6700)
2051                  wp[-2] = htons(0x6000); // bra
2052  
2053          // Patch PowerOff()
# Line 1916 | Line 2077 | static bool patch_68k(void)
2077  
2078          if (ROMType == ROMTYPE_NEWWORLD) {
2079                  static const uint8 via_int3_dat[] = {0x48, 0xe7, 0xf0, 0xf0, 0x76, 0x01, 0x60, 0x26};
2080 <                if ((base = find_rom_data(0x15000, 0x18000, via_int3_dat, sizeof(via_int3_dat))) == 0) return false;
2080 >                if ((base = find_rom_data(0x15000, 0x19000, via_int3_dat, sizeof(via_int3_dat))) == 0) return false;
2081                  D(bug("via_int3 %08lx\n", base));
2082                  wp = (uint16 *)(ROM_BASE + base);       // CHRP level 1 handler
2083                  *wp++ = htons(M68K_JMP);
# Line 2006 | Line 2167 | void InstallDrivers(void)
2167   {
2168          D(bug("Installing drivers...\n"));
2169          M68kRegisters r;
2170 <        uint8 pb[SIZEOF_IOParam];
2170 >        SheepArray<SIZEOF_IOParam> pb_var;
2171 >        const uintptr pb = pb_var.addr();
2172 >
2173 >        // Install floppy driver
2174 >        if (ROMType == ROMTYPE_NEWWORLD || ROMType == ROMTYPE_GOSSAMER) {
2175 >
2176 >                // Force installation of floppy driver with NewWorld and Gossamer ROMs
2177 >                r.a[0] = ROM_BASE + sony_offset;
2178 >                r.d[0] = (uint32)SonyRefNum;
2179 >                Execute68kTrap(0xa43d, &r);             // DrvrInstallRsrvMem()
2180 >                r.a[0] = ReadMacInt32(ReadMacInt32(0x11c) + ~SonyRefNum * 4);   // Get driver handle from Unit Table
2181 >                Execute68kTrap(0xa029, &r);             // HLock()
2182 >                uint32 dce = ReadMacInt32(r.a[0]);
2183 >                WriteMacInt32(dce + dCtlDriver, ROM_BASE + sony_offset);
2184 >                WriteMacInt16(dce + dCtlFlags, SonyDriverFlags);
2185 >        }
2186 >
2187 > #if DISABLE_SCSI && 0
2188 >        // Fake SCSIGlobals
2189 >        static const uint8 fake_scsi_globals[32] = {0,};
2190 >        WriteMacInt32(0xc0c, (uint32)fake_scsi_globals);
2191 > #endif
2192  
2193          // Open .Sony driver
2194 <        WriteMacInt8((uint32)pb + ioPermssn, 0);
2195 <        WriteMacInt32((uint32)pb + ioNamePtr, (uint32)"\005.Sony");
2196 <        r.a[0] = (uint32)pb;
2194 >        SheepString sony_str("\005.Sony");
2195 >        WriteMacInt8(pb + ioPermssn, 0);
2196 >        WriteMacInt32(pb + ioNamePtr, sony_str.addr());
2197 >        r.a[0] = pb;
2198          Execute68kTrap(0xa000, &r);             // Open()
2199  
2200          // Install disk driver
# Line 2025 | Line 2208 | void InstallDrivers(void)
2208          WriteMacInt16(dce + dCtlFlags, DiskDriverFlags);
2209  
2210          // Open disk driver
2211 <        WriteMacInt32((uint32)pb + ioNamePtr, (uint32)"\005.Disk");
2212 <        r.a[0] = (uint32)pb;
2211 >        SheepString disk_str("\005.Disk");
2212 >        WriteMacInt32(pb + ioNamePtr, disk_str.addr());
2213 >        r.a[0] = pb;
2214          Execute68kTrap(0xa000, &r);             // Open()
2215  
2216          // Install CD-ROM driver unless nocdrom option given
# Line 2043 | Line 2227 | void InstallDrivers(void)
2227                  WriteMacInt16(dce + dCtlFlags, CDROMDriverFlags);
2228  
2229                  // Open CD-ROM driver
2230 <                WriteMacInt32((uint32)pb + ioNamePtr, (uint32)"\010.AppleCD");
2231 <                r.a[0] = (uint32)pb;
2230 >                SheepString apple_cd("\010.AppleCD");
2231 >                WriteMacInt32(pb + ioNamePtr, apple_cd.addr());
2232 >                r.a[0] = pb;
2233                  Execute68kTrap(0xa000, &r);             // Open()
2234          }
2235  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines