home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 25 / AACD 25.iso / AACD / Utilities / BasiliskII / src / rom_patches.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-10  |  51.5 KB  |  1,676 lines

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