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

Comparing SheepShaver/src/BeOS/main_beos.cpp (file contents):
Revision 1.2 by gbeauche, 2002-04-21T15:07:08Z vs.
Revision 1.14 by gbeauche, 2004-11-13T14:09:15Z

# Line 1 | Line 1
1   /*
2   *  main_beos.cpp - Emulation core, BeOS implementation
3   *
4 < *  SheepShaver (C) 1997-2002 Christian Bauer and Marc Hellwig
4 > *  SheepShaver (C) 1997-2004 Christian Bauer and Marc Hellwig
5   *
6   *  This program is free software; you can redistribute it and/or modify
7   *  it under the terms of the GNU General Public License as published by
# Line 88 | Line 88
88   #include "macos_util.h"
89   #include "rom_patches.h"
90   #include "user_strings.h"
91 + #include "thunks.h"
92  
93   #include "sheep_driver.h"
94  
# Line 116 | Line 117 | const char KERNEL_AREA2_NAME[] = "Macint
117   const char RAM_AREA_NAME[] = "Macintosh RAM";
118   const char ROM_AREA_NAME[] = "Macintosh ROM";
119   const char DR_CACHE_AREA_NAME[] = "Macintosh DR Cache";
120 <
121 < const uint32 ROM_AREA_SIZE = 0x500000;          // Size of ROM area
121 <
122 < const uint32 KERNEL_DATA_BASE = 0x68ffe000;     // Address of Kernel Data
123 < const uint32 KERNEL_DATA2_BASE = 0x5fffe000;// Alternate address of Kernel Data
124 < const uint32 KERNEL_AREA_SIZE = 0x2000;         // Size of Kernel Data area
120 > const char DR_EMULATOR_AREA_NAME[] = "Macintosh DR Emulator";
121 > const char SHEEP_AREA_NAME[] = "SheepShaver Virtual Stack";
122  
123   const uint32 SIG_STACK_SIZE = 8192;                     // Size of signal stack
124  
125   const uint32 MSG_START = 'strt';                        // Emulator start message
126  
127  
131 // Emulator Data
132 struct EmulatorData {
133        uint32 v[0x400];        
134 };
135
136
137 // Kernel Data
138 struct KernelData {
139        uint32 v[0x400];
140        EmulatorData ed;
141 };
142
143
128   // Application object
129   class SheepShaver : public BApplication {
130   public:
# Line 159 | Line 143 | public:
143                  // Initialize other variables
144                  sheep_fd = -1;
145                  emulator_data = NULL;
146 <                kernel_area = kernel_area2 = rom_area = ram_area = dr_cache_area = -1;
146 >                kernel_area = kernel_area2 = rom_area = ram_area = dr_cache_area = dr_emulator_area = -1;
147                  emul_thread = nvram_thread = tick_thread = -1;
148                  ReadyForSignals = false;
149                  AllowQuitting = true;
# Line 207 | Line 191 | private:
191          area_id rom_area;               // ROM area ID
192          area_id ram_area;               // RAM area ID
193          area_id dr_cache_area;  // DR Cache area ID
194 +        area_id dr_emulator_area;       // DR Emulator area ID
195  
196          struct sigaction sigusr1_action;        // Interrupt signal (of emulator thread)
197          struct sigaction sigsegv_action;        // Data access exception signal (of emulator thread)
# Line 230 | Line 215 | uint32 RAMSize;                        // Size of Mac RAM
215   uint32 KernelDataAddr;  // Address of Kernel Data
216   uint32 BootGlobsAddr;   // Address of BootGlobs structure at top of Mac RAM
217   uint32 DRCacheAddr;             // Address of DR Cache
218 + uint32 DREmulatorAddr;  // Address of DR Emulator
219   uint32 PVR;                             // Theoretical PVR
220   int64 CPUClockSpeed;    // Processor clock speed (Hz)
221   int64 BusClockSpeed;    // Bus clock speed (Hz)
222 + int64 TimebaseSpeed;    // Timebase clock speed (Hz)
223   system_info SysInfo;    // System information
224 + uint8 *RAMBaseHost;             // Base address of Mac RAM (host address space)
225 + uint8 *ROMBaseHost;             // Base address of Mac ROM (host address space)
226  
227   static void *sig_stack = NULL;          // Stack for signal handlers
228   static void *extra_stack = NULL;        // Stack for SIGSEGV inside interrupt handler
229 + uint32  SheepMem::page_size;            // Size of a native page
230 + uintptr SheepMem::zero_page = 0;        // Address of ro page filled in with zeros
231 + uintptr SheepMem::base;                         // Address of SheepShaver data
232 + uintptr SheepMem::top;                          // Top of SheepShaver data (stack like storage)
233 + static area_id SheepMemArea;            // SheepShaver data area ID
234  
235  
236   // Prototypes
# Line 308 | Line 302 | void SheepShaver::ReadyToRun(void)
302          }
303          CPUClockSpeed = SysInfo.cpu_clock_speed;
304          BusClockSpeed = SysInfo.bus_clock_speed;
305 +        TimebaseSpeed = BusClockSpeed / 4;
306  
307          // Delete old areas
308          area_id old_kernel_area = find_area(KERNEL_AREA_NAME);
# Line 325 | Line 320 | void SheepShaver::ReadyToRun(void)
320          area_id old_dr_cache_area = find_area(DR_CACHE_AREA_NAME);
321          if (old_dr_cache_area > 0)
322                  delete_area(old_dr_cache_area);
323 +        area_id old_dr_emulator_area = find_area(DR_EMULATOR_AREA_NAME);
324 +        if (old_dr_emulator_area > 0)
325 +                delete_area(old_dr_emulator_area);
326  
327          // Read preferences
328          int argc = 0;
# Line 372 | Line 370 | void SheepShaver::MessageReceived(BMessa
370   void SheepShaver::StartEmulator(void)
371   {
372          char str[256];
373 +        int16 i16;
374  
375          // Open sheep driver and remap low memory
376          sheep_fd = open("/dev/sheep", 0);
# Line 412 | Line 411 | void SheepShaver::StartEmulator(void)
411          }
412          D(bug("Kernel Data 2 area %ld at %p\n", kernel_area2, kernel_data2));
413  
414 +        // Create area for SheepShaver data
415 +        if (!SheepMem::Init()) {
416 +                sprintf(str, GetString(STR_NO_SHEEP_MEM_AREA_ERR), strerror(SheepMemArea), SheepMemArea);
417 +                ErrorAlert(str);
418 +                PostMessage(B_QUIT_REQUESTED);
419 +                return;
420 +        }
421 +        
422          // Create area for Mac RAM
423          RAMSize = PrefsFindInt32("ramsize") & 0xfff00000;       // Round down to 1MB boundary
424          if (RAMSize < 8*1024*1024) {
# Line 427 | Line 434 | void SheepShaver::StartEmulator(void)
434                  PostMessage(B_QUIT_REQUESTED);
435                  return;
436          }
437 <        D(bug("RAM area %ld at %p\n", ram_area, RAMBase));
437 >        RAMBaseHost = (uint8 *)RAMBase
438 >        D(bug("RAM area %ld at %p\n", ram_area, RAMBaseHost));
439  
440          // Create area and load Mac ROM
441          try {
# Line 461 | Line 469 | void SheepShaver::StartEmulator(void)
469          }
470          D(bug("DR Cache area %ld at %p\n", dr_cache_area, DRCacheAddr));
471  
472 +        // Create area for DR Emulator
473 +        DREmulatorAddr = DR_EMULATOR_BASE;
474 +        dr_emulator_area = create_area(DR_EMULATOR_AREA_NAME, (void **)&DREmulatorAddr, B_EXACT_ADDRESS, DR_EMULATOR_SIZE, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA);
475 +        if (dr_emulator_area < 0) {
476 +                sprintf(str, GetString(STR_NO_KERNEL_DATA_ERR), strerror(dr_emulator_area), dr_emulator_area);
477 +                ErrorAlert(str);
478 +                PostMessage(B_QUIT_REQUESTED);
479 +                return;
480 +        }
481 +        D(bug("DR Emulator area %ld at %p\n", dr_emulator_area, DREmulatorAddr));
482 +
483          // Load NVRAM
484          XPRAMInit();
485  
486          // Set boot volume
487 <        drive = PrefsFindInt32("bootdrive");
487 >        i16 = PrefsFindInt32("bootdrive");
488          XPRAM[0x1378] = i16 >> 8;
489          XPRAM[0x1379] = i16 & 0xff;
490 <        driver = PrefsFindInt32("bootdriver");
490 >        i16 = PrefsFindInt32("bootdriver");
491          XPRAM[0x137a] = i16 >> 8;
492          XPRAM[0x137b] = i16 & 0xff;
493  
# Line 481 | Line 500 | void SheepShaver::StartEmulator(void)
500          boot_globs[1] = htonl(RAMSize);
501          boot_globs[2] = htonl((uint32)-1);                              // End of bank table
502  
503 +        // Init thunks
504 +        if (!InitThunks()) {
505 +                PostMessage(B_QUIT_REQUESTED);
506 +                return;
507 +        }
508 +
509          // Init drivers
510          SonyInit();
511          DiskInit();
# Line 520 | Line 545 | void SheepShaver::StartEmulator(void)
545  
546          // Clear caches (as we loaded and patched code) and write protect ROM
547   #if !EMULATED_PPC
548 <        clear_caches((void *)ROM_BASE, ROM_AREA_SIZE, B_INVALIDATE_ICACHE | B_FLUSH_DCACHE);
548 >        clear_caches(ROMBaseHost, ROM_AREA_SIZE, B_INVALIDATE_ICACHE | B_FLUSH_DCACHE);
549   #endif
550          set_area_protection(rom_area, B_READ_AREA);
551  
# Line 577 | Line 602 | void SheepShaver::StartEmulator(void)
602          WriteMacInt32(XLM_PVR, PVR);                                                                    // Theoretical PVR
603          WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed);                                    // For DriverServicesLib patch
604          WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN);                // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode)
605 +        WriteMacInt32(XLM_ZERO_PAGE, SheepMem::ZeroPage());                             // Pointer to read-only page with all bits set to 0
606   #if !EMULATED_PPC
607          WriteMacInt32(XLM_TOC, (uint32)TOC);                                                    // TOC pointer of emulator
608          WriteMacInt32(XLM_ETHER_INIT, *(uint32 *)InitStreamModule);             // DLPI ethernet driver functions
# Line 673 | Line 699 | void SheepShaver::Quit(void)
699          DiskExit();
700          SonyExit();
701  
702 +        // Delete thunks
703 +        ThunksExit();
704 +
705 +        // Delete SheepShaver globals
706 +        SheepMem::Exit();
707 +
708 +        // Delete DR Emulator area
709 +        if (dr_emulator_area >= 0)
710 +                delete_area(dr_emulator_area);
711 +
712          // Delete DR Cache area
713          if (dr_cache_area >= 0)
714                  delete_area(dr_cache_area);
# Line 717 | Line 753 | void SheepShaver::Quit(void)
753  
754   void SheepShaver::init_rom(void)
755   {
756 +        // Size of a native page
757 +        page_size = B_PAGE_SIZE;
758 +
759          // Create area for ROM
760 <        void *rom_addr = (void *)ROM_BASE;
761 <        rom_area = create_area(ROM_AREA_NAME, &rom_addr, B_EXACT_ADDRESS, ROM_AREA_SIZE, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA);
760 >        ROMBaseHost = (uint8 *)ROM_BASE;
761 >        rom_area = create_area(ROM_AREA_NAME, (void **)&ROMBaseHost, B_EXACT_ADDRESS, ROM_AREA_SIZE, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA);
762          if (rom_area < 0)
763                  throw area_error();
764          D(bug("ROM area %ld at %p\n", rom_area, rom_addr));
# Line 1261 | Line 1300 | void Execute68kTrap(uint16 trap, M68kReg
1300  
1301  
1302   /*
1264 *  Execute PPC code from EMUL_OP routine (real mode switch)
1265 */
1266
1267 void ExecutePPC(void (*func)())
1268 {
1269        RoutineDescriptor desc = BUILD_PPC_ROUTINE_DESCRIPTOR(0, func);
1270        M68kRegisters r;
1271        Execute68k((uint32)&desc, &r);
1272 }
1273
1274
1275 /*
1303   *  Quit emulator (must only be called from main thread)
1304   */
1305  
# Line 1313 | Line 1340 | void Dump68kRegs(M68kRegisters *r)
1340   *  Make code executable
1341   */
1342  
1343 < void MakeExecutable(int dummy, void *start, uint32 length)
1343 > void MakeExecutable(int dummy, uint32 start, uint32 length)
1344   {
1345 <        if (((uint32)start >= ROM_BASE) && ((uint32)start < (ROM_BASE + ROM_SIZE)))
1345 >        if ((start >= ROM_BASE) && (start < (ROM_BASE + ROM_SIZE)))
1346                  return;
1347 <        clear_caches(start, length, B_INVALIDATE_ICACHE | B_FLUSH_DCACHE);
1347 >        clear_caches((void *)start, length, B_INVALIDATE_ICACHE | B_FLUSH_DCACHE);
1348   }
1349  
1350  
# Line 1327 | Line 1354 | void MakeExecutable(int dummy, void *sta
1354  
1355   void PatchAfterStartup(void)
1356   {
1357 <        ExecutePPC(VideoInstallAccel);
1357 >        ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL);
1358          InstallExtFS();
1359   }
1360  
# Line 1336 | Line 1363 | void PatchAfterStartup(void)
1363   *  NVRAM watchdog thread (saves NVRAM every minute)
1364   */
1365  
1366 < static status_t SheepShaver::nvram_func(void *arg)
1366 > status_t SheepShaver::nvram_func(void *arg)
1367   {
1368          SheepShaver *obj = (SheepShaver *)arg;
1369  
# Line 1575 | Line 1602 | void SheepShaver::sigusr1_handler(vregs
1602                                          if (InterruptFlags & INTFLAG_VIA) {
1603                                                  ClearInterruptFlag(INTFLAG_VIA);
1604                                                  ADBInterrupt();
1605 <                                                ExecutePPC(VideoVBL);
1605 >                                                ExecuteNative(NATIVE_VIDEO_VBL);
1606                                          }
1607                                  }
1608   #endif
# Line 2070 | Line 2097 | rti:
2097   }
2098  
2099  
2100 + /*
2101 + *  Helpers to share 32-bit addressable data with MacOS
2102 + */
2103 +
2104 + bool SheepMem::Init(void)
2105 + {
2106 +        // Delete old area
2107 +        area_id old_sheep_area = find_area(SHEEP_AREA_NAME);
2108 +        if (old_sheep_area > 0)
2109 +                delete_area(old_sheep_area);
2110 +
2111 +        // Create area for SheepShaver data
2112 +        base = 0x60000000;
2113 +        SheepMemArea = create_area(SHEEP_AREA_NAME, (void **)&base, B_BASE_ADDRESS, size, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA);
2114 +        if (SheepMemArea < 0)
2115 +                return false;
2116 +
2117 +        // Create read-only area with all bits set to 0
2118 +        static const uint8 const_zero_page[4096] = {0,};
2119 +        zero_page = const_zero_page;
2120 +
2121 +        D(bug("SheepShaver area %ld at %p\n", SheepMemArea, base));
2122 +        top = base + size;
2123 +        return true;
2124 + }
2125 +
2126 + void SheepMem::Exit(void)
2127 + {
2128 +        if (SheepMemArea >= 0)
2129 +                delete_area(SheepMemArea);
2130 + }
2131 +
2132 +
2133   /*
2134   *  Display error alert
2135   */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines