--- SheepShaver/src/rom_patches.cpp 2002/04/21 15:07:07 1.2 +++ SheepShaver/src/rom_patches.cpp 2003/12/04 17:26:35 1.18 @@ -40,6 +40,7 @@ #include "audio_defs.h" #include "serial.h" #include "macos_util.h" +#include "thunks.h" #define DEBUG 0 #include "debug.h" @@ -58,10 +59,10 @@ // Other ROM addresses -const uint32 CHECK_LOAD_PATCH_SPACE = 0x2f7f00; -const uint32 PUT_SCRAP_PATCH_SPACE = 0x2f7f80; -const uint32 GET_SCRAP_PATCH_SPACE = 0x2f7fc0; -const uint32 ADDR_MAP_PATCH_SPACE = 0x2f8000; +const uint32 CHECK_LOAD_PATCH_SPACE = 0x2fcf00; +const uint32 PUT_SCRAP_PATCH_SPACE = 0x2fcf80; +const uint32 GET_SCRAP_PATCH_SPACE = 0x2fcfc0; +const uint32 ADDR_MAP_PATCH_SPACE = 0x2fd000; // Global variables int ROMType; // ROM type @@ -124,16 +125,17 @@ void decode_parcels(const uint8 *src, ui D(bug("Offset Type Name\n")); while (parcel_offset != 0) { const uint32 *parcel_data = (uint32 *)(src + parcel_offset); - parcel_offset = ntohl(parcel_data[0]); + uint32 next_offset = ntohl(parcel_data[0]); uint32 parcel_type = ntohl(parcel_data[1]); D(bug("%08x %c%c%c%c %s\n", parcel_offset, (parcel_type >> 24) & 0xff, (parcel_type >> 16) & 0xff, (parcel_type >> 8) & 0xff, parcel_type & 0xff, &parcel_data[6])); if (parcel_type == FOURCC('r','o','m',' ')) { uint32 lzss_offset = ntohl(parcel_data[2]); - uint32 lzss_size = ((uint32)src + parcel_offset) - ((uint32)parcel_data + lzss_offset); + uint32 lzss_size = ((uintptr)src + next_offset) - ((uintptr)parcel_data + lzss_offset); decode_lzss((uint8 *)parcel_data + lzss_offset, dest, lzss_size); } + parcel_offset = next_offset; } } @@ -446,202 +448,141 @@ static const uint8 cdrom_driver[] = { // 0x4e, 0x75 // rts }; -#ifdef __linux__ -static uint32 serial_nothing_tvect[2] = {(uint32)SerialNothing, 0}; -static uint32 serial_open_tvect[2] = {(uint32)SerialOpen, 0}; -static uint32 serial_prime_in_tvect[2] = {(uint32)SerialPrimeIn, 0}; -static uint32 serial_prime_out_tvect[2] = {(uint32)SerialPrimeOut, 0}; -static uint32 serial_control_tvect[2] = {(uint32)SerialControl, 0}; -static uint32 serial_status_tvect[2] = {(uint32)SerialStatus, 0}; -static uint32 serial_close_tvect[2] = {(uint32)SerialClose, 0}; -#endif +static uint32 long_ptr; -static const uint32 ain_driver[] = { // .AIn driver header - 0x4d000000, 0x00000000, - 0x00200040, 0x00600080, - 0x00a0042e, 0x41496e00, - 0x00000000, 0x00000000, - 0xaafe0700, 0x00000000, - 0x00000000, 0x00179822, -#ifdef __linux__ - 0x00010004, (uint32)serial_nothing_tvect, -#else - 0x00010004, (uint32)SerialNothing, -#endif - 0x00000000, 0x00000000, - 0xaafe0700, 0x00000000, - 0x00000000, 0x00179822, -#ifdef __linux__ - 0x00010004, (uint32)serial_prime_in_tvect, -#else - 0x00010004, (uint32)SerialPrimeIn, -#endif - 0x00000000, 0x00000000, - 0xaafe0700, 0x00000000, - 0x00000000, 0x00179822, -#ifdef __linux__ - 0x00010004, (uint32)serial_control_tvect, -#else - 0x00010004, (uint32)SerialControl, -#endif - 0x00000000, 0x00000000, - 0xaafe0700, 0x00000000, - 0x00000000, 0x00179822, -#ifdef __linux__ - 0x00010004, (uint32)serial_status_tvect, -#else - 0x00010004, (uint32)SerialStatus, -#endif - 0x00000000, 0x00000000, - 0xaafe0700, 0x00000000, - 0x00000000, 0x00179822, -#ifdef __linux__ - 0x00010004, (uint32)serial_nothing_tvect, -#else - 0x00010004, (uint32)SerialNothing, -#endif - 0x00000000, 0x00000000, +static void SetLongBase(uint32 addr) +{ + long_ptr = addr; +} + +static void Long(uint32 value) +{ + WriteMacInt32(long_ptr, value); + long_ptr += 4; +} + +static void gen_ain_driver(uintptr addr) +{ + SetLongBase(addr); + + // .AIn driver header + Long(0x4d000000); Long(0x00000000); + Long(0x00200040); Long(0x00600080); + Long(0x00a0042e); Long(0x41496e00); + Long(0x00000000); Long(0x00000000); + Long(0xaafe0700); Long(0x00000000); + Long(0x00000000); Long(0x00179822); + Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_NOTHING)); + Long(0x00000000); Long(0x00000000); + Long(0xaafe0700); Long(0x00000000); + Long(0x00000000); Long(0x00179822); + Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_PRIME_IN)); + Long(0x00000000); Long(0x00000000); + Long(0xaafe0700); Long(0x00000000); + Long(0x00000000); Long(0x00179822); + Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_CONTROL)); + Long(0x00000000); Long(0x00000000); + Long(0xaafe0700); Long(0x00000000); + Long(0x00000000); Long(0x00179822); + Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_STATUS)); + Long(0x00000000); Long(0x00000000); + Long(0xaafe0700); Long(0x00000000); + Long(0x00000000); Long(0x00179822); + Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_NOTHING)); + Long(0x00000000); Long(0x00000000); }; -static const uint32 aout_driver[] = { // .AOut driver header - 0x4d000000, 0x00000000, - 0x00200040, 0x00600080, - 0x00a0052e, 0x414f7574, - 0x00000000, 0x00000000, - 0xaafe0700, 0x00000000, - 0x00000000, 0x00179822, -#ifdef __linux__ - 0x00010004, (uint32)serial_open_tvect, -#else - 0x00010004, (uint32)SerialOpen, -#endif - 0x00000000, 0x00000000, - 0xaafe0700, 0x00000000, - 0x00000000, 0x00179822, -#ifdef __linux__ - 0x00010004, (uint32)serial_prime_out_tvect, -#else - 0x00010004, (uint32)SerialPrimeOut, -#endif - 0x00000000, 0x00000000, - 0xaafe0700, 0x00000000, - 0x00000000, 0x00179822, -#ifdef __linux__ - 0x00010004, (uint32)serial_control_tvect, -#else - 0x00010004, (uint32)SerialControl, -#endif - 0x00000000, 0x00000000, - 0xaafe0700, 0x00000000, - 0x00000000, 0x00179822, -#ifdef __linux__ - 0x00010004, (uint32)serial_status_tvect, -#else - 0x00010004, (uint32)SerialStatus, -#endif - 0x00000000, 0x00000000, - 0xaafe0700, 0x00000000, - 0x00000000, 0x00179822, -#ifdef __linux__ - 0x00010004, (uint32)serial_close_tvect, -#else - 0x00010004, (uint32)SerialClose, -#endif - 0x00000000, 0x00000000, +static void gen_aout_driver(uintptr addr) +{ + SetLongBase(addr); + + // .AOut driver header + Long(0x4d000000); Long(0x00000000); + Long(0x00200040); Long(0x00600080); + Long(0x00a0052e); Long(0x414f7574); + Long(0x00000000); Long(0x00000000); + Long(0xaafe0700); Long(0x00000000); + Long(0x00000000); Long(0x00179822); + Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_OPEN)); + Long(0x00000000); Long(0x00000000); + Long(0xaafe0700); Long(0x00000000); + Long(0x00000000); Long(0x00179822); + Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_PRIME_OUT)); + Long(0x00000000); Long(0x00000000); + Long(0xaafe0700); Long(0x00000000); + Long(0x00000000); Long(0x00179822); + Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_CONTROL)); + Long(0x00000000); Long(0x00000000); + Long(0xaafe0700); Long(0x00000000); + Long(0x00000000); Long(0x00179822); + Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_STATUS)); + Long(0x00000000); Long(0x00000000); + Long(0xaafe0700); Long(0x00000000); + Long(0x00000000); Long(0x00179822); + Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_CLOSE)); + Long(0x00000000); Long(0x00000000); }; -static const uint32 bin_driver[] = { // .BIn driver header - 0x4d000000, 0x00000000, - 0x00200040, 0x00600080, - 0x00a0042e, 0x42496e00, - 0x00000000, 0x00000000, - 0xaafe0700, 0x00000000, - 0x00000000, 0x00179822, -#ifdef __linux__ - 0x00010004, (uint32)serial_nothing_tvect, -#else - 0x00010004, (uint32)SerialNothing, -#endif - 0x00000000, 0x00000000, - 0xaafe0700, 0x00000000, - 0x00000000, 0x00179822, -#ifdef __linux__ - 0x00010004, (uint32)serial_prime_in_tvect, -#else - 0x00010004, (uint32)SerialPrimeIn, -#endif - 0x00000000, 0x00000000, - 0xaafe0700, 0x00000000, - 0x00000000, 0x00179822, -#ifdef __linux__ - 0x00010004, (uint32)serial_control_tvect, -#else - 0x00010004, (uint32)SerialControl, -#endif - 0x00000000, 0x00000000, - 0xaafe0700, 0x00000000, - 0x00000000, 0x00179822, -#ifdef __linux__ - 0x00010004, (uint32)serial_status_tvect, -#else - 0x00010004, (uint32)SerialStatus, -#endif - 0x00000000, 0x00000000, - 0xaafe0700, 0x00000000, - 0x00000000, 0x00179822, -#ifdef __linux__ - 0x00010004, (uint32)serial_nothing_tvect, -#else - 0x00010004, (uint32)SerialNothing, -#endif - 0x00000000, 0x00000000, +static void gen_bin_driver(uintptr addr) +{ + SetLongBase(addr); + + // .BIn driver header + Long(0x4d000000); Long(0x00000000); + Long(0x00200040); Long(0x00600080); + Long(0x00a0042e); Long(0x42496e00); + Long(0x00000000); Long(0x00000000); + Long(0xaafe0700); Long(0x00000000); + Long(0x00000000); Long(0x00179822); + Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_NOTHING)); + Long(0x00000000); Long(0x00000000); + Long(0xaafe0700); Long(0x00000000); + Long(0x00000000); Long(0x00179822); + Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_PRIME_IN)); + Long(0x00000000); Long(0x00000000); + Long(0xaafe0700); Long(0x00000000); + Long(0x00000000); Long(0x00179822); + Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_CONTROL)); + Long(0x00000000); Long(0x00000000); + Long(0xaafe0700); Long(0x00000000); + Long(0x00000000); Long(0x00179822); + Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_STATUS)); + Long(0x00000000); Long(0x00000000); + Long(0xaafe0700); Long(0x00000000); + Long(0x00000000); Long(0x00179822); + Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_NOTHING)); + Long(0x00000000); Long(0x00000000); }; -static const uint32 bout_driver[] = { // .BOut driver header - 0x4d000000, 0x00000000, - 0x00200040, 0x00600080, - 0x00a0052e, 0x424f7574, - 0x00000000, 0x00000000, - 0xaafe0700, 0x00000000, - 0x00000000, 0x00179822, -#ifdef __linux__ - 0x00010004, (uint32)serial_open_tvect, -#else - 0x00010004, (uint32)SerialOpen, -#endif - 0x00000000, 0x00000000, - 0xaafe0700, 0x00000000, - 0x00000000, 0x00179822, -#ifdef __linux__ - 0x00010004, (uint32)serial_prime_out_tvect, -#else - 0x00010004, (uint32)SerialPrimeOut, -#endif - 0x00000000, 0x00000000, - 0xaafe0700, 0x00000000, - 0x00000000, 0x00179822, -#ifdef __linux__ - 0x00010004, (uint32)serial_control_tvect, -#else - 0x00010004, (uint32)SerialControl, -#endif - 0x00000000, 0x00000000, - 0xaafe0700, 0x00000000, - 0x00000000, 0x00179822, -#ifdef __linux__ - 0x00010004, (uint32)serial_status_tvect, -#else - 0x00010004, (uint32)SerialStatus, -#endif - 0x00000000, 0x00000000, - 0xaafe0700, 0x00000000, - 0x00000000, 0x00179822, -#ifdef __linux__ - 0x00010004, (uint32)serial_close_tvect, -#else - 0x00010004, (uint32)SerialClose, -#endif - 0x00000000, 0x00000000, +static void gen_bout_driver(uintptr addr) +{ + SetLongBase(addr); + + // .BOut driver header + Long(0x4d000000); Long(0x00000000); + Long(0x00200040); Long(0x00600080); + Long(0x00a0052e); Long(0x424f7574); + Long(0x00000000); Long(0x00000000); + Long(0xaafe0700); Long(0x00000000); + Long(0x00000000); Long(0x00179822); + Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_OPEN)); + Long(0x00000000); Long(0x00000000); + Long(0xaafe0700); Long(0x00000000); + Long(0x00000000); Long(0x00179822); + Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_PRIME_OUT)); + Long(0x00000000); Long(0x00000000); + Long(0xaafe0700); Long(0x00000000); + Long(0x00000000); Long(0x00179822); + Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_CONTROL)); + Long(0x00000000); Long(0x00000000); + Long(0xaafe0700); Long(0x00000000); + Long(0x00000000); Long(0x00179822); + Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_STATUS)); + Long(0x00000000); Long(0x00000000); + Long(0xaafe0700); Long(0x00000000); + Long(0x00000000); Long(0x00179822); + Long(0x00010004); Long(NativeTVECT(NATIVE_SERIAL_CLOSE)); + Long(0x00000000); Long(0x00000000); }; static const uint8 adbop_patch[] = { // Call ADBOp() completion procedure @@ -668,6 +609,23 @@ static const uint8 adbop_patch[] = { // /* + * Copy PowerPC code to ROM image and reverse bytes if necessary + */ + +static inline void memcpy_powerpc_code(void *dst, const void *src, size_t len) +{ +#ifdef WORDS_BIGENDIAN + (void)memcpy(dst, src, len); +#else + uint32 *d = (uint32 *)dst; + uint32 *s = (uint32 *)src; + for (int i = 0; i < len/4; i++) + d[i] = htonl(s[i]); +#endif +} + + +/* * Install ROM patches (RAMBase and KernelDataAddr must be set) */ @@ -690,11 +648,23 @@ bool PatchROM(void) ROMType = ROMTYPE_ZANZIBAR; else if (!memcmp((void *)(ROM_BASE + 0x30d064), "Boot Gazelle", 12)) ROMType = ROMTYPE_GAZELLE; + else if (!memcmp((void *)(ROM_BASE + 0x30d064), "Boot Gossamer", 13)) + ROMType = ROMTYPE_GOSSAMER; else if (!memcmp((void *)(ROM_BASE + 0x30d064), "NewWorld", 8)) ROMType = ROMTYPE_NEWWORLD; else return false; + // Check that other ROM addresses point to really free regions + if (ntohl(*(uint32 *)(ROM_BASE + CHECK_LOAD_PATCH_SPACE)) != 0x6b636b63) + return false; + if (ntohl(*(uint32 *)(ROM_BASE + PUT_SCRAP_PATCH_SPACE)) != 0x6b636b63) + return false; + if (ntohl(*(uint32 *)(ROM_BASE + GET_SCRAP_PATCH_SPACE)) != 0x6b636b63) + return false; + if (ntohl(*(uint32 *)(ROM_BASE + ADDR_MAP_PATCH_SPACE)) != 0x6b636b63) + return false; + // Apply patches if (!patch_nanokernel_boot()) return false; if (!patch_68k_emul()) return false; @@ -739,12 +709,12 @@ static bool patch_nanokernel_boot(void) lp[0xfd8 >> 2] = htonl(ROM_BASE + 0x2a); // 68k reset vector // Skip SR/BAT/SDR init - if (ROMType == ROMTYPE_GAZELLE || ROMType == ROMTYPE_NEWWORLD) { + if (ROMType == ROMTYPE_GAZELLE || ROMType == ROMTYPE_GOSSAMER || ROMType == ROMTYPE_NEWWORLD) { lp = (uint32 *)(ROM_BASE + 0x310000); *lp++ = htonl(POWERPC_NOP); *lp = htonl(0x38000000); } - static const uint32 sr_init_loc[] = {0x3101b0, 0x3101b0, 0x3101b0, 0x3101ec, 0x310200}; + static const uint32 sr_init_loc[] = {0x3101b0, 0x3101b0, 0x3101b0, 0x3101ec, 0x3101fc, 0x310200}; lp = (uint32 *)(ROM_BASE + 0x310008); *lp = htonl(0x48000000 | (sr_init_loc[ROMType] - 8) & 0xffff); // b ROM_BASE+0x3101b0 lp = (uint32 *)(ROM_BASE + sr_init_loc[ROMType]); @@ -754,7 +724,7 @@ static bool patch_nanokernel_boot(void) *lp = htonl(0x3de00010); // lis r15,0x0010 (size of kernel memory) // Don't read PVR - static const uint32 pvr_loc[] = {0x3103b0, 0x3103b4, 0x3103b4, 0x310400, 0x310438}; + static const uint32 pvr_loc[] = {0x3103b0, 0x3103b4, 0x3103b4, 0x310400, 0x310430, 0x310438}; lp = (uint32 *)(ROM_BASE + pvr_loc[ROMType]); *lp = htonl(0x81800000 + XLM_PVR); // lwz r12,(theoretical PVR) @@ -902,11 +872,11 @@ static bool patch_nanokernel_boot(void) *lp = htonl(POWERPC_NOP); // Don't read PVR - static const uint32 pvr_ofs[] = {0x138, 0x138, 0x138, 0x140, 0x148}; + static const uint32 pvr_ofs[] = {0x138, 0x138, 0x138, 0x140, 0x148, 0x148}; lp = (uint32 *)(ROM_BASE + loc + pvr_ofs[ROMType]); *lp = htonl(0x82e00000 + XLM_PVR); // lwz r23,(theoretical PVR) lp = (uint32 *)(ROM_BASE + loc + 0x170); - if (ntohl(*lp) == 0x7eff42a6) // NewWorld ROM + if (ntohl(*lp) == 0x7eff42a6) // NewWorld or Gossamer ROM *lp = htonl(0x82e00000 + XLM_PVR); // lwz r23,(theoretical PVR) lp = (uint32 *)(ROM_BASE + 0x313134); if (ntohl(*lp) == 0x7e5f42a6) @@ -914,41 +884,44 @@ static bool patch_nanokernel_boot(void) lp = (uint32 *)(ROM_BASE + 0x3131f4); if (ntohl(*lp) == 0x7e5f42a6) // NewWorld ROM *lp = htonl(0x82400000 + XLM_PVR); // lwz r18,(theoretical PVR) + lp = (uint32 *)(ROM_BASE + 0x314600); + if (ntohl(*lp) == 0x7d3f42a6) + *lp = htonl(0x81200000 + XLM_PVR); // lzw r9,(theoritical PVR) // Don't read SDR1 - static const uint32 sdr1_ofs[] = {0x174, 0x174, 0x174, 0x17c, 0x19c}; + static const uint32 sdr1_ofs[] = {0x174, 0x174, 0x174, 0x17c, 0x19c, 0x19c}; lp = (uint32 *)(ROM_BASE + loc + sdr1_ofs[ROMType]); *lp++ = htonl(0x3d00dead); // lis r8,0xdead (pointer to page table) *lp++ = htonl(0x3ec0001f); // lis r22,0x001f (size of page table) *lp = htonl(POWERPC_NOP); // Don't clear page table - static const uint32 pgtb_ofs[] = {0x198, 0x198, 0x198, 0x1a0, 0x1c4}; + static const uint32 pgtb_ofs[] = {0x198, 0x198, 0x198, 0x1a0, 0x1c0, 0x1c4}; lp = (uint32 *)(ROM_BASE + loc + pgtb_ofs[ROMType]); *lp = htonl(POWERPC_NOP); // Don't invalidate TLB - static const uint32 tlb_ofs[] = {0x1a0, 0x1a0, 0x1a0, 0x1a8, 0x1cc}; + static const uint32 tlb_ofs[] = {0x1a0, 0x1a0, 0x1a0, 0x1a8, 0x1c8, 0x1cc}; lp = (uint32 *)(ROM_BASE + loc + tlb_ofs[ROMType]); *lp = htonl(POWERPC_NOP); // Don't create RAM descriptor table - static const uint32 desc_ofs[] = {0x350, 0x350, 0x350, 0x358, 0x37c}; + static const uint32 desc_ofs[] = {0x350, 0x350, 0x350, 0x358, 0x378, 0x37c}; lp = (uint32 *)(ROM_BASE + loc + desc_ofs[ROMType]); *lp = htonl(POWERPC_NOP); // Don't load SRs and BATs - static const uint32 sr_ofs[] = {0x3d8, 0x3d8, 0x3d8, 0x3e0, 0x404}; + static const uint32 sr_ofs[] = {0x3d8, 0x3d8, 0x3d8, 0x3e0, 0x400, 0x404}; lp = (uint32 *)(ROM_BASE + loc + sr_ofs[ROMType]); *lp = htonl(POWERPC_NOP); // Don't mess with SRs - static const uint32 sr2_ofs[] = {0x312118, 0x312118, 0x312118, 0x312118, 0x3121b4}; + static const uint32 sr2_ofs[] = {0x312118, 0x312118, 0x312118, 0x312118, 0x312118, 0x3121b4}; lp = (uint32 *)(ROM_BASE + sr2_ofs[ROMType]); *lp = htonl(POWERPC_BLR); // Don't check performance monitor - static const uint32 pm_ofs[] = {0x313148, 0x313148, 0x313148, 0x313148, 0x313218}; + static const uint32 pm_ofs[] = {0x313148, 0x313148, 0x313148, 0x313148, 0x313158, 0x313218}; lp = (uint32 *)(ROM_BASE + pm_ofs[ROMType]); while (ntohl(*lp) != 0x7e58eba6) lp++; *lp++ = htonl(POWERPC_NOP); @@ -984,7 +957,7 @@ static bool patch_nanokernel_boot(void) *lp++ = htonl(POWERPC_NOP); // Jump to 68k emulator - static const uint32 jump68k_ofs[] = {0x40c, 0x40c, 0x40c, 0x414, 0x438}; + static const uint32 jump68k_ofs[] = {0x40c, 0x40c, 0x40c, 0x414, 0x434, 0x438}; lp = (uint32 *)(ROM_BASE + loc + jump68k_ofs[ROMType]); *lp++ = htonl(0x80610634); // lwz r3,0x0634(r1) (pointer to Emulator Data) *lp++ = htonl(0x8081119c); // lwz r4,0x119c(r1) (pointer to opcode table) @@ -1005,7 +978,7 @@ static bool patch_68k_emul(void) uint32 base; // Overwrite twi instructions - static const uint32 twi_loc[] = {0x36e680, 0x36e6c0, 0x36e6c0, 0x36e6c0, 0x36e740}; + static const uint32 twi_loc[] = {0x36e680, 0x36e6c0, 0x36e6c0, 0x36e6c0, 0x36e740, 0x36e740}; base = twi_loc[ROMType]; lp = (uint32 *)(ROM_BASE + base); *lp++ = htonl(0x48000000 + 0x36f900 - base); // b 0x36f900 (Emulator start) @@ -1026,15 +999,17 @@ static bool patch_68k_emul(void) *lp = htonl(POWERPC_ILLEGAL); #if EMULATED_PPC - // Install EMUL_RETURN, EXEC_RETURN and EMUL_OP opcodes + // Install EMUL_RETURN, EXEC_RETURN, EXEC_NATIVE and EMUL_OP opcodes lp = (uint32 *)(ROM_BASE + 0x380000 + (M68K_EMUL_RETURN << 3)); *lp++ = htonl(POWERPC_EMUL_OP); *lp++ = htonl(0x4bf66e80); // b 0x366084 *lp++ = htonl(POWERPC_EMUL_OP | 1); *lp++ = htonl(0x4bf66e78); // b 0x366084 + *lp++ = htonl(POWERPC_EMUL_OP | 2); + *lp++ = htonl(0x4bf66e70); // b 0x366084 for (int i=0; i> 2] = htonl(ADDR_MAP_PATCH_SPACE - (base - 0x14)); @@ -1361,10 +1360,22 @@ static bool patch_68k(void) lp[0x28 >> 2] = htonl(0x00000861); lp[0x58 >> 2] = htonl(0x30200000); lp[0x60 >> 2] = htonl(0x0000003d); + } else if (ROMType == ROMTYPE_GOSSAMER) { + base = 0x12d20; + lp = (uint32 *)(ROM_BASE + base - 0x14); + lp[0x00 >> 2] = htonl(ADDR_MAP_PATCH_SPACE - (base - 0x14)); + lp[0x10 >> 2] = htonl(0xcc003d11); // Make it like the PowerMac 9500 UniversalInfo + lp[0x14 >> 2] = htonl(0x3fff0401); + lp[0x18 >> 2] = htonl(0x0300001c); + lp[0x1c >> 2] = htonl(0x000108c4); + lp[0x24 >> 2] = htonl(0xc301bf26); + lp[0x28 >> 2] = htonl(0x00000861); + lp[0x58 >> 2] = htonl(0x30410000); + lp[0x60 >> 2] = htonl(0x0000003d); } // Construct AddrMap for NewWorld ROM - if (ROMType == ROMTYPE_NEWWORLD || ROMType == ROMTYPE_ZANZIBAR) { + if (ROMType == ROMTYPE_NEWWORLD || ROMType == ROMTYPE_ZANZIBAR || ROMType == ROMTYPE_GOSSAMER) { lp = (uint32 *)(ROM_BASE + ADDR_MAP_PATCH_SPACE); memset(lp - 10, 0, 0x128); lp[-10] = htonl(0x0300001c); @@ -1490,8 +1501,15 @@ static bool patch_68k(void) *wp++ = htons(M68K_EMUL_OP_XPRAM2); *wp = htons(0x4ed3); // jmp (a3) - static const uint32 nvram3_loc[] = {0x582f0, 0xa0a0, 0x7e50, 0xa1d0, 0}; - wp = (uint16 *)(ROM_BASE + nvram3_loc[ROMType]); + static const uint8 nvram3_dat[] = {0x4e, 0xd3, 0x06, 0x41, 0x13, 0x00}; + if ((base = find_rom_data(0x7000, 0xb000, nvram3_dat, sizeof(nvram3_dat))) == 0) return false; + D(bug("nvram3 %08lx\n", base)); + wp = (uint16 *)(ROM_BASE + base + 2); + *wp++ = htons(M68K_EMUL_OP_XPRAM3); + *wp = htons(0x4ed3); // jmp (a3) + + static const uint32 nvram4_loc[] = {0x582f0, 0xa0a0, 0x7e50, 0xa1d0, 0x538d0, 0}; + wp = (uint16 *)(ROM_BASE + nvram4_loc[ROMType]); *wp++ = htons(0x202f); // move.l 4(sp),d0 *wp++ = htons(0x0004); *wp++ = htons(M68K_EMUL_OP_NVRAM1); @@ -1504,8 +1522,8 @@ static bool patch_68k(void) *wp = htons(0x0004); } - static const uint32 nvram4_loc[] = {0x58460, 0xa0f0, 0x7f40, 0xa220, 0}; - wp = (uint16 *)(ROM_BASE + nvram4_loc[ROMType]); + static const uint32 nvram5_loc[] = {0x58460, 0xa0f0, 0x7f40, 0xa220, 0x53a20, 0}; + wp = (uint16 *)(ROM_BASE + nvram5_loc[ROMType]); if (ROMType == ROMTYPE_ZANZIBAR || ROMType == ROMTYPE_GAZELLE) { *wp++ = htons(0x202f); // move.l 4(sp),d0 *wp++ = htons(0x0004); @@ -1682,6 +1700,8 @@ static bool patch_68k(void) if ((base = find_rom_data(0x13000, 0x20000, gc_mask2_dat, sizeof(gc_mask2_dat))) == 0) return false; D(bug("gc_mask2 %08lx\n", base)); wp = (uint16 *)(ROM_BASE + base); + if (ROMType == ROMTYPE_GOSSAMER) + *wp++ = htons(M68K_NOP); for (int i=0; i<5; i++) { *wp++ = htons(M68K_NOP); *wp++ = htons(M68K_NOP); @@ -1689,7 +1709,7 @@ static bool patch_68k(void) *wp++ = htons(M68K_NOP); wp += 2; } - if (ROMType == ROMTYPE_ZANZIBAR) { + if (ROMType == ROMTYPE_ZANZIBAR || ROMType == ROMTYPE_GOSSAMER) { for (int i=0; i<6; i++) { *wp++ = htons(M68K_NOP); *wp++ = htons(M68K_NOP); @@ -1715,14 +1735,14 @@ static bool patch_68k(void) // Patch GetCPUSpeed (via 0x27a) (some ROMs have two of them) static const uint8 cpu_speed_dat[] = {0x20, 0x30, 0x81, 0xf2, 0x5f, 0xff, 0xef, 0xd8, 0x00, 0x04, 0x4c, 0x7c}; - if ((base = find_rom_data(0x6000, 0x9000, cpu_speed_dat, sizeof(cpu_speed_dat))) == 0) return false; + if ((base = find_rom_data(0x6000, 0xa000, cpu_speed_dat, sizeof(cpu_speed_dat))) == 0) return false; D(bug("cpu_speed %08lx\n", base)); wp = (uint16 *)(ROM_BASE + base); *wp++ = htons(0x203c); // move.l #(MHz<<16)|MHz,d0 *wp++ = htons(CPUClockSpeed / 1000000); *wp++ = htons(CPUClockSpeed / 1000000); *wp = htons(M68K_RTS); - if ((base = find_rom_data(base, 0x9000, cpu_speed_dat, sizeof(cpu_speed_dat))) != 0) { + if ((base = find_rom_data(base, 0xa000, cpu_speed_dat, sizeof(cpu_speed_dat))) != 0) { D(bug("cpu_speed2 %08lx\n", base)); wp = (uint16 *)(ROM_BASE + base); *wp++ = htons(0x203c); // move.l #(MHz<<16)|MHz,d0 @@ -1762,9 +1782,9 @@ static bool patch_68k(void) *wp = htons(M68K_RTS); // Don't install Time Manager task for 60Hz interrupt (Enable60HzInts, via 0x2b8) - if (ROMType == ROMTYPE_NEWWORLD) { + if (ROMType == ROMTYPE_NEWWORLD || ROMType == ROMTYPE_GOSSAMER) { static const uint8 tm_task_dat[] = {0x30, 0x3c, 0x4e, 0x2b, 0xa9, 0xc9}; - if ((base = find_rom_data(0x2e0, 0x320, tm_task_dat, sizeof(tm_task_dat))) == 0) return false; + if ((base = find_rom_data(0x2a0, 0x320, tm_task_dat, sizeof(tm_task_dat))) == 0) return false; D(bug("tm_task %08lx\n", base)); wp = (uint16 *)(ROM_BASE + base + 28); *wp++ = htons(M68K_NOP); @@ -1784,7 +1804,7 @@ static bool patch_68k(void) } // Don't read PVR from 0x5fffef80 in DriverServicesLib (via 0x316) - if (ROMType != ROMTYPE_NEWWORLD) { + if (ROMType != ROMTYPE_NEWWORLD && ROMType != ROMTYPE_GOSSAMER) { uint32 dsl_offset = find_rom_resource(FOURCC('n','l','i','b'), -16401); if (ROMType == ROMTYPE_ZANZIBAR) { 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}; @@ -1870,7 +1890,23 @@ static bool patch_68k(void) D(bug("scsi_var2 %08lx\n", base)); wp = (uint16 *)(ROM_BASE + base); *wp++ = htons(0x7000); // moveq #0,d0 - *wp = htons(M68K_RTS); // bra + *wp = htons(M68K_RTS); + } + } + else if (ROMType == ROMTYPE_GOSSAMER) { + static const uint8 scsi_var_dat[] = {0x70, 0x01, 0xa0, 0x89, 0x4a, 0x6e, 0xfe, 0xac, 0x4f, 0xef, 0x00, 0x10, 0x66, 0x00}; + if ((base = find_rom_data(0x1d700, 0x1d800, scsi_var_dat, sizeof(scsi_var_dat))) != 0) { + D(bug("scsi_var %08lx\n", base)); + wp = (uint16 *)(ROM_BASE + base + 12); + *wp = htons(0x6000); // bra + } + + static const uint8 scsi_var2_dat[] = {0x4e, 0x56, 0xfc, 0x5a, 0x48, 0xe7, 0x1f, 0x38}; + if ((base = find_rom_data(0x1d900, 0x1da00, scsi_var2_dat, sizeof(scsi_var2_dat))) != 0) { + D(bug("scsi_var2 %08lx\n", base)); + wp = (uint16 *)(ROM_BASE + base); + *wp++ = htons(0x7000); // moveq #0,d0 + *wp = htons(M68K_RTS); } } #endif @@ -1926,10 +1962,10 @@ static bool patch_68k(void) memcpy((void *)(ROM_BASE + sony_offset + 0x200), cdrom_driver, sizeof(cdrom_driver)); // Install serial drivers - memcpy((void *)(ROM_BASE + sony_offset + 0x300), ain_driver, sizeof(ain_driver)); - memcpy((void *)(ROM_BASE + sony_offset + 0x400), aout_driver, sizeof(aout_driver)); - memcpy((void *)(ROM_BASE + sony_offset + 0x500), bin_driver, sizeof(bin_driver)); - memcpy((void *)(ROM_BASE + sony_offset + 0x600), bout_driver, sizeof(bout_driver)); + gen_ain_driver( ROM_BASE + sony_offset + 0x300); + gen_aout_driver(ROM_BASE + sony_offset + 0x400); + gen_bin_driver( ROM_BASE + sony_offset + 0x500); + gen_bout_driver(ROM_BASE + sony_offset + 0x600); // Copy icons to ROM SonyDiskIconAddr = ROM_BASE + sony_offset + 0x800; @@ -1950,7 +1986,7 @@ static bool patch_68k(void) *wp = htons(M68K_RTS); // Don't install serial drivers from ROM - if (ROMType == ROMTYPE_ZANZIBAR || ROMType == ROMTYPE_NEWWORLD) { + if (ROMType == ROMTYPE_ZANZIBAR || ROMType == ROMTYPE_NEWWORLD || ROMType == ROMTYPE_GOSSAMER) { wp = (uint16 *)(ROM_BASE + find_rom_resource(FOURCC('S','E','R','D'), 0)); *wp = htons(M68K_RTS); } else { @@ -2009,7 +2045,9 @@ static bool patch_68k(void) wp = (uint16 *)(ROM_BASE + base); if (ROMType == ROMTYPE_ZANZIBAR) *wp = htons(M68K_RTS); - else + else if (ntohs(wp[-4]) == 0x61ff) + *wp = htons(M68K_RTS); + else if (ntohs(wp[-2]) == 0x6700) wp[-2] = htons(0x6000); // bra // Patch PowerOff() @@ -2039,7 +2077,7 @@ static bool patch_68k(void) if (ROMType == ROMTYPE_NEWWORLD) { static const uint8 via_int3_dat[] = {0x48, 0xe7, 0xf0, 0xf0, 0x76, 0x01, 0x60, 0x26}; - if ((base = find_rom_data(0x15000, 0x18000, via_int3_dat, sizeof(via_int3_dat))) == 0) return false; + if ((base = find_rom_data(0x15000, 0x19000, via_int3_dat, sizeof(via_int3_dat))) == 0) return false; D(bug("via_int3 %08lx\n", base)); wp = (uint16 *)(ROM_BASE + base); // CHRP level 1 handler *wp++ = htons(M68K_JMP); @@ -2129,12 +2167,34 @@ void InstallDrivers(void) { D(bug("Installing drivers...\n")); M68kRegisters r; - uint8 pb[SIZEOF_IOParam]; + SheepArray pb_var; + const uintptr pb = pb_var.addr(); + + // Install floppy driver + if (ROMType == ROMTYPE_NEWWORLD || ROMType == ROMTYPE_GOSSAMER) { + + // Force installation of floppy driver with NewWorld and Gossamer ROMs + r.a[0] = ROM_BASE + sony_offset; + r.d[0] = (uint32)SonyRefNum; + Execute68kTrap(0xa43d, &r); // DrvrInstallRsrvMem() + r.a[0] = ReadMacInt32(ReadMacInt32(0x11c) + ~SonyRefNum * 4); // Get driver handle from Unit Table + Execute68kTrap(0xa029, &r); // HLock() + uint32 dce = ReadMacInt32(r.a[0]); + WriteMacInt32(dce + dCtlDriver, ROM_BASE + sony_offset); + WriteMacInt16(dce + dCtlFlags, SonyDriverFlags); + } + +#if DISABLE_SCSI && 0 + // Fake SCSIGlobals + static const uint8 fake_scsi_globals[32] = {0,}; + WriteMacInt32(0xc0c, (uint32)fake_scsi_globals); +#endif // Open .Sony driver - WriteMacInt8((uint32)pb + ioPermssn, 0); - WriteMacInt32((uint32)pb + ioNamePtr, (uint32)"\005.Sony"); - r.a[0] = (uint32)pb; + SheepString sony_str("\005.Sony"); + WriteMacInt8(pb + ioPermssn, 0); + WriteMacInt32(pb + ioNamePtr, sony_str.addr()); + r.a[0] = pb; Execute68kTrap(0xa000, &r); // Open() // Install disk driver @@ -2148,8 +2208,9 @@ void InstallDrivers(void) WriteMacInt16(dce + dCtlFlags, DiskDriverFlags); // Open disk driver - WriteMacInt32((uint32)pb + ioNamePtr, (uint32)"\005.Disk"); - r.a[0] = (uint32)pb; + SheepString disk_str("\005.Disk"); + WriteMacInt32(pb + ioNamePtr, disk_str.addr()); + r.a[0] = pb; Execute68kTrap(0xa000, &r); // Open() // Install CD-ROM driver unless nocdrom option given @@ -2166,8 +2227,9 @@ void InstallDrivers(void) WriteMacInt16(dce + dCtlFlags, CDROMDriverFlags); // Open CD-ROM driver - WriteMacInt32((uint32)pb + ioNamePtr, (uint32)"\010.AppleCD"); - r.a[0] = (uint32)pb; + SheepString apple_cd("\010.AppleCD"); + WriteMacInt32(pb + ioNamePtr, apple_cd.addr()); + r.a[0] = pb; Execute68kTrap(0xa000, &r); // Open() }