--- SheepShaver/src/rom_patches.cpp 2004/11/13 14:09:15 1.36 +++ SheepShaver/src/rom_patches.cpp 2005/12/12 20:46:31 1.42 @@ -1,7 +1,7 @@ /* * rom_patches.cpp - ROM patches * - * SheepShaver (C) 1997-2004 Christian Bauer and Marc Hellwig + * SheepShaver (C) 1997-2005 Christian Bauer and Marc Hellwig * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -224,10 +224,9 @@ static uint32 rsrc_ptr = 0; // id = 4711 means "find any ID" static uint32 find_rom_resource(uint32 s_type, int16 s_id = 4711, bool cont = false) { - uint32 *lp = (uint32 *)(ROMBaseHost + 0x1a); - uint32 x = ntohl(*lp); - uint8 *bp = (uint8 *)(ROMBaseHost + x + 5); - uint32 header_size = *bp; + uint32 lp = ROM_BASE + 0x1a; + uint32 x = ReadMacInt32(lp); + uint32 header_size = ReadMacInt8(ROM_BASE + x + 5); if (!cont) rsrc_ptr = x; @@ -235,17 +234,17 @@ static uint32 find_rom_resource(uint32 s return 0; for (;;) { - lp = (uint32 *)(ROMBaseHost + rsrc_ptr); - rsrc_ptr = ntohl(*lp); + lp = ROM_BASE + rsrc_ptr; + rsrc_ptr = ReadMacInt32(lp); if (rsrc_ptr == 0) break; rsrc_ptr += header_size; - lp = (uint32 *)(ROMBaseHost + rsrc_ptr + 4); - uint32 data = ntohl(*lp); lp++; - uint32 type = ntohl(*lp); lp++; - int16 id = ntohs(*(int16 *)lp); + lp = ROM_BASE + rsrc_ptr + 4; + uint32 data = ReadMacInt32(lp); + uint32 type = ReadMacInt32(lp + 4); + int16 id = ReadMacInt16(lp + 8); if (type == s_type && (id == s_id || s_id == 4711)) return data; } @@ -259,13 +258,12 @@ static uint32 find_rom_resource(uint32 s static uint32 find_rom_trap(uint16 trap) { - uint32 *lp = (uint32 *)(ROMBaseHost + 0x22); - lp = (uint32 *)(ROMBaseHost + ntohl(*lp)); + uint32 lp = ROM_BASE + ReadMacInt32(ROM_BASE + 0x22); if (trap > 0xa800) - return ntohl(lp[trap & 0x3ff]); + return ReadMacInt32(lp + 4 * (trap & 0x3ff)); else - return ntohl(lp[(trap & 0xff) + 0x400]); + return ReadMacInt32(lp + 4 * ((trap & 0xff) + 0x400)); } @@ -1085,7 +1083,7 @@ static bool patch_68k_emul(void) *lp++ = htonl(0x48000000 + 0x36fb00 - base - 8); // b 0x36fb00 (Reset/FC1E opcode) *lp++ = htonl(0x48000000 + 0x36fc00 - base - 12); // FE0A opcode *lp++ = htonl(POWERPC_ILLEGAL); // Interrupt - *lp++ = htonl(POWERPC_ILLEGAL); // ? + *lp++ = htonl(0x48000000 + 0x36fd00 - base - 20); // FE0F opcode *lp++ = htonl(POWERPC_ILLEGAL); *lp++ = htonl(POWERPC_ILLEGAL); *lp++ = htonl(POWERPC_ILLEGAL); @@ -1254,6 +1252,36 @@ static bool patch_68k_emul(void) *lp++ = htonl(0x50e7deb4); // rlwimi r7,r7,27,$00000020 *lp = htonl(0x4e800020); // blr + // Extra routine for FE0F opcode (power management) + lp = (uint32 *)(ROMBaseHost + 0x36fd00); + *lp++ = htonl(0x7c2903a6); // mtctr r1 + *lp++ = htonl(0x80200000 + XLM_IRQ_NEST); // lwz r1,XLM_IRQ_NEST + *lp++ = htonl(0x38210001); // addi r1,r1,1 + *lp++ = htonl(0x90200000 + XLM_IRQ_NEST); // stw r1,XLM_IRQ_NEST + *lp++ = htonl(0x80200000 + XLM_KERNEL_DATA);// lwz r1,XLM_KERNEL_DATA + *lp++ = htonl(0x90c10018); // stw r6,0x18(r1) + *lp++ = htonl(0x7cc902a6); // mfctr r6 + *lp++ = htonl(0x90c10004); // stw r6,$0004(r1) + *lp++ = htonl(0x80c1065c); // lwz r6,$065c(r1) + *lp++ = htonl(0x90e6013c); // stw r7,$013c(r6) + *lp++ = htonl(0x91060144); // stw r8,$0144(r6) + *lp++ = htonl(0x9126014c); // stw r9,$014c(r6) + *lp++ = htonl(0x91460154); // stw r10,$0154(r6) + *lp++ = htonl(0x9166015c); // stw r11,$015c(r6) + *lp++ = htonl(0x91860164); // stw r12,$0164(r6) + *lp++ = htonl(0x91a6016c); // stw r13,$016c(r6) + *lp++ = htonl(0x7da00026); // mfcr r13 + *lp++ = htonl(0x80e10660); // lwz r7,$0660(r1) + *lp++ = htonl(0x7d8802a6); // mflr r12 + *lp++ = htonl(0x50e74001); // rlwimi. r7,r7,8,$80000000 + *lp++ = htonl(0x81410604); // lwz r10,0x0604(r1) + *lp++ = htonl(0x7d4803a6); // mtlr r10 + *lp++ = htonl(0x7d8a6378); // mr r10,r12 + *lp++ = htonl(0x3d600002); // lis r11,0x0002 + *lp++ = htonl(0x616bf072); // ori r11,r11,0xf072 (MSR) + *lp++ = htonl(0x50e7deb4); // rlwimi r7,r7,27,$00000020 + *lp = htonl(0x4e800020); // blr + // Patch DR emulator to jump to right address when an interrupt occurs lp = (uint32 *)(ROMBaseHost + 0x370000); while (lp < (uint32 *)(ROMBaseHost + 0x380000)) { @@ -1266,9 +1294,9 @@ static bool patch_68k_emul(void) dr_found: lp++; loc = (uintptr)lp - (uintptr)ROMBaseHost; - if ((base = rom_powerpc_branch_target(loc)) == 0) base = ROM_BASE + loc; + if ((base = rom_powerpc_branch_target(loc)) == 0) base = loc; static const uint8 dr_ret_dat[] = {0x80, 0xbf, 0x08, 0x14, 0x53, 0x19, 0x4d, 0xac, 0x7c, 0xa8, 0x03, 0xa6}; - if ((base = find_rom_data(base - ROM_BASE, 0x380000, dr_ret_dat, sizeof(dr_ret_dat))) == 0) return false; + if ((base = find_rom_data(base, 0x380000, dr_ret_dat, sizeof(dr_ret_dat))) == 0) return false; D(bug("dr_ret %08lx\n", base)); if (base != loc) { // OldWorld ROMs contain an absolute branch @@ -1382,6 +1410,14 @@ static bool patch_nanokernel(void) *lp++ = htonl(POWERPC_NOP); *lp = htonl(POWERPC_NOP); + // Disable suspend (FE0F opcode) + // TODO: really suspend SheepShaver? + static const uint8 suspend_dat[] = {0x7c, 0x88, 0x68, 0x39, 0x41, 0x9d}; + if ((base = find_rom_data(0x315000, 0x316000, suspend_dat, sizeof(suspend_dat))) == 0) return false; + D(bug("suspend %08lx\n", base)); + lp = (uint32 *)(ROMBaseHost + base + 4); + *lp = htonl((ntohl(*lp) & 0xffff) | 0x48000000); // bgt -> b + // Patch trap return routine static const uint8 trap_return_dat[] = {0x80, 0xc1, 0x00, 0x18, 0x80, 0x21, 0x00, 0x04, 0x4c, 0x00, 0x00, 0x64}; if ((base = find_rom_data(0x312000, 0x320000, trap_return_dat, sizeof(trap_return_dat))) == 0) return false; @@ -1677,11 +1713,11 @@ static bool patch_68k(void) static const uint8 ext_cache_dat[] = {0x4e, 0x7b, 0x00, 0x02}; if ((base = find_rom_data(0x1d0, 0x230, ext_cache_dat, sizeof(ext_cache_dat))) == 0) return false; D(bug("ext_cache %08lx\n", base)); - lp = (uint32 *)(ROMBaseHost + base + 6); - wp = (uint16 *)(ROMBaseHost + ntohl(*lp) + base + 6); + loc = ReadMacInt32(ROM_BASE + base + 6); + wp = (uint16 *)(ROMBaseHost + loc + base + 6); *wp = htons(M68K_RTS); - lp = (uint32 *)(ROMBaseHost + base + 12); - wp = (uint16 *)(ROMBaseHost + ntohl(*lp) + base + 12); + loc = ReadMacInt32(ROM_BASE + base + 12); + wp = (uint16 *)(ROMBaseHost + loc + base + 12); *wp = htons(M68K_RTS); // Fake CPU speed test (SetupTimeK) @@ -2226,9 +2262,8 @@ static bool patch_68k(void) *wp++ = htons(M68K_JMP); *wp++ = htons((ROM_BASE + put_scrap) >> 16); *wp++ = htons((ROM_BASE + put_scrap) & 0xffff); - lp = (uint32 *)(ROMBaseHost + 0x22); - lp = (uint32 *)(ROMBaseHost + ntohl(*lp)); - lp[0xa9fe & 0x3ff] = htonl(PUT_SCRAP_PATCH_SPACE); + base = ROM_BASE + ReadMacInt32(ROM_BASE + 0x22); + WriteMacInt32(base + 4 * (0xa9fe & 0x3ff), PUT_SCRAP_PATCH_SPACE); // Patch GetScrap() for clipboard exchange with host OS uint32 get_scrap = find_rom_trap(0xa9fd); // GetScrap() @@ -2237,9 +2272,8 @@ static bool patch_68k(void) *wp++ = htons(M68K_JMP); *wp++ = htons((ROM_BASE + get_scrap) >> 16); *wp++ = htons((ROM_BASE + get_scrap) & 0xffff); - lp = (uint32 *)(ROMBaseHost + 0x22); - lp = (uint32 *)(ROMBaseHost + ntohl(*lp)); - lp[0xa9fd & 0x3ff] = htonl(GET_SCRAP_PATCH_SPACE); + base = ROM_BASE + ReadMacInt32(ROM_BASE + 0x22); + WriteMacInt32(base + 4 * (0xa9fd & 0x3ff), GET_SCRAP_PATCH_SPACE); // Patch SynchIdleTime() if (PrefsFindBool("idlewait")) { @@ -2306,6 +2340,15 @@ void InstallDrivers(void) SheepArray pb_var; const uintptr pb = pb_var.addr(); +#if DISABLE_SCSI + // Setup fake SCSI Globals + r.d[0] = 0x1000; + Execute68kTrap(0xa71e, &r); // NewPtrSysClear() + uint32 scsi_globals = r.a[0]; + D(bug("Fake SCSI globals at %08lx\n", scsi_globals)); + WriteMacInt32(0xc0c, scsi_globals); // Set SCSIGlobals +#endif + // Install floppy driver if (ROMType == ROMTYPE_NEWWORLD || ROMType == ROMTYPE_GOSSAMER) { @@ -2320,11 +2363,6 @@ void InstallDrivers(void) WriteMacInt16(dce + dCtlFlags, SonyDriverFlags); } -#if DISABLE_SCSI && HAVE_SIGSEGV_SKIP_INSTRUCTION - // Fake SCSIGlobals - WriteMacInt32(0xc0c, SheepMem::ZeroPage()); -#endif - // Open .Sony driver SheepString sony_str("\005.Sony"); WriteMacInt8(pb + ioPermssn, 0);