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.14 by gbeauche, 2004-11-13T14:09:15Z vs.
Revision 1.22 by asvitkine, 2009-08-26T00:11:56Z

# Line 1 | Line 1
1   /*
2   *  main_beos.cpp - Emulation core, BeOS implementation
3   *
4 < *  SheepShaver (C) 1997-2004 Christian Bauer and Marc Hellwig
4 > *  SheepShaver (C) 1997-2008 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 74 | Line 74
74   #include "xpram.h"
75   #include "timer.h"
76   #include "adb.h"
77 #include "sony.h"
78 #include "disk.h"
79 #include "cdrom.h"
80 #include "scsi.h"
77   #include "video.h"
82 #include "audio.h"
83 #include "ether.h"
84 #include "serial.h"
85 #include "clip.h"
86 #include "extfs.h"
78   #include "sys.h"
79   #include "macos_util.h"
80   #include "rom_patches.h"
81   #include "user_strings.h"
91 #include "thunks.h"
82  
83   #include "sheep_driver.h"
84  
# Line 120 | Line 110 | const char DR_CACHE_AREA_NAME[] = "Macin
110   const char DR_EMULATOR_AREA_NAME[] = "Macintosh DR Emulator";
111   const char SHEEP_AREA_NAME[] = "SheepShaver Virtual Stack";
112  
113 + const uintptr ROM_BASE = 0x40800000;            // Base address of ROM
114 +
115   const uint32 SIG_STACK_SIZE = 8192;                     // Size of signal stack
116  
117   const uint32 MSG_START = 'strt';                        // Emulator start message
# Line 212 | Line 204 | void *TOC;                             // TOC pointer
204   #endif
205   uint32 RAMBase;                 // Base address of Mac RAM
206   uint32 RAMSize;                 // Size of Mac RAM
207 + uint32 ROMBase;                 // Base address of Mac ROM
208   uint32 KernelDataAddr;  // Address of Kernel Data
209   uint32 BootGlobsAddr;   // Address of BootGlobs structure at top of Mac RAM
210   uint32 DRCacheAddr;             // Address of DR Cache
# Line 229 | Line 222 | static void *extra_stack = NULL;       // Stac
222   uint32  SheepMem::page_size;            // Size of a native page
223   uintptr SheepMem::zero_page = 0;        // Address of ro page filled in with zeros
224   uintptr SheepMem::base;                         // Address of SheepShaver data
225 < uintptr SheepMem::top;                          // Top of SheepShaver data (stack like storage)
225 > uintptr SheepMem::proc;                         // Bottom address of SheepShave procedures
226 > uintptr SheepMem::data;                         // Top of SheepShaver data (stack like storage)
227   static area_id SheepMemArea;            // SheepShaver data area ID
228  
229  
# Line 327 | Line 321 | void SheepShaver::ReadyToRun(void)
321          // Read preferences
322          int argc = 0;
323          char **argv = NULL;
324 <        PrefsInit(argc, argv);
324 >        PrefsInit(NULL, argc, argv);
325  
326          // Init system routines
327          SysInit();
# Line 370 | Line 364 | void SheepShaver::MessageReceived(BMessa
364   void SheepShaver::StartEmulator(void)
365   {
366          char str[256];
373        int16 i16;
367  
368          // Open sheep driver and remap low memory
369          sheep_fd = open("/dev/sheep", 0);
# Line 480 | Line 473 | void SheepShaver::StartEmulator(void)
473          }
474          D(bug("DR Emulator area %ld at %p\n", dr_emulator_area, DREmulatorAddr));
475  
476 <        // Load NVRAM
477 <        XPRAMInit();
485 <
486 <        // Set boot volume
487 <        i16 = PrefsFindInt32("bootdrive");
488 <        XPRAM[0x1378] = i16 >> 8;
489 <        XPRAM[0x1379] = i16 & 0xff;
490 <        i16 = PrefsFindInt32("bootdriver");
491 <        XPRAM[0x137a] = i16 >> 8;
492 <        XPRAM[0x137b] = i16 & 0xff;
493 <
494 <        // Create BootGlobs at top of Mac memory
495 <        memset((void *)(RAMBase + RAMSize - 4096), 0, 4096);
496 <        BootGlobsAddr = RAMBase + RAMSize - 0x1c;
497 <        uint32 *boot_globs = (uint32 *)BootGlobsAddr;
498 <        boot_globs[-5] = htonl(RAMBase + RAMSize);              // MemTop
499 <        boot_globs[0] = htonl(RAMBase);                                 // First RAM bank
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();
512 <        CDROMInit();
513 <        SCSIInit();
514 <
515 <        // Init external file system
516 <        ExtFSInit();
517 <
518 <        // Init audio
519 <        AudioInit();
520 <
521 <        // Init network
522 <        EtherInit();
523 <
524 <        // Init serial ports
525 <        SerialInit();
526 <
527 <        // Init Time Manager
528 <        TimerInit();
529 <
530 <        // Init clipboard
531 <        ClipInit();
532 <
533 <        // Init video
534 <        if (!VideoInit()) {
535 <                PostMessage(B_QUIT_REQUESTED);
536 <                return;
537 <        }
538 <
539 <        // Install ROM patches
540 <        if (!PatchROM()) {
541 <                ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR));
476 >        // Initialize everything
477 >        if (!InitAll()) {
478                  PostMessage(B_QUIT_REQUESTED);
479                  return;
480          }
481 +        D(bug("Initialization complete\n"));
482  
483          // Clear caches (as we loaded and patched code) and write protect ROM
484   #if !EMULATED_PPC
# Line 549 | Line 486 | void SheepShaver::StartEmulator(void)
486   #endif
487          set_area_protection(rom_area, B_READ_AREA);
488  
552        // Initialize Kernel Data
553        memset(kernel_data, 0, sizeof(KernelData));
554        if (ROMType == ROMTYPE_NEWWORLD) {
555                static uint32 of_dev_tree[4] = {0, 0, 0, 0};
556                static uint8 vector_lookup_tbl[128];
557                static uint8 vector_mask_tbl[64];
558                memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80);
559                memset(vector_lookup_tbl, 0, 128);
560                memset(vector_mask_tbl, 0, 64);
561                kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE);
562                kernel_data->v[0xb84 >> 2] = htonl((uint32)of_dev_tree);        // OF device tree base
563                kernel_data->v[0xb90 >> 2] = htonl((uint32)vector_lookup_tbl);
564                kernel_data->v[0xb94 >> 2] = htonl((uint32)vector_mask_tbl);
565                kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE);                           // OpenPIC base
566                kernel_data->v[0xbb0 >> 2] = htonl(0);                                          // ADB base
567                kernel_data->v[0xc20 >> 2] = htonl(RAMSize);
568                kernel_data->v[0xc24 >> 2] = htonl(RAMSize);
569                kernel_data->v[0xc30 >> 2] = htonl(RAMSize);
570                kernel_data->v[0xc34 >> 2] = htonl(RAMSize);
571                kernel_data->v[0xc38 >> 2] = htonl(0x00010020);
572                kernel_data->v[0xc3c >> 2] = htonl(0x00200001);
573                kernel_data->v[0xc40 >> 2] = htonl(0x00010000);
574                kernel_data->v[0xc50 >> 2] = htonl(RAMBase);
575                kernel_data->v[0xc54 >> 2] = htonl(RAMSize);
576                kernel_data->v[0xf60 >> 2] = htonl(PVR);
577                kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed);
578                kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed);
579                kernel_data->v[0xf6c >> 2] = htonl(CPUClockSpeed);
580        } else {
581                kernel_data->v[0xc80 >> 2] = htonl(RAMSize);
582                kernel_data->v[0xc84 >> 2] = htonl(RAMSize);
583                kernel_data->v[0xc90 >> 2] = htonl(RAMSize);
584                kernel_data->v[0xc94 >> 2] = htonl(RAMSize);
585                kernel_data->v[0xc98 >> 2] = htonl(0x00010020);
586                kernel_data->v[0xc9c >> 2] = htonl(0x00200001);
587                kernel_data->v[0xca0 >> 2] = htonl(0x00010000);
588                kernel_data->v[0xcb0 >> 2] = htonl(RAMBase);
589                kernel_data->v[0xcb4 >> 2] = htonl(RAMSize);
590                kernel_data->v[0xf80 >> 2] = htonl(PVR);
591                kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed);
592                kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed);
593                kernel_data->v[0xf8c >> 2] = htonl(CPUClockSpeed);
594        }
595
489          // Initialize extra low memory
490 <        D(bug("Initializing Low Memory...\n"));
598 <        memset(NULL, 0, 0x3000);
599 <        WriteMacInt32(XLM_SIGNATURE, 'Baah');                                                   // Signature to detect SheepShaver
600 <        WriteMacInt32(XLM_KERNEL_DATA, (uint32)kernel_data);                    // For trap replacement routines
490 >        D(bug("Initializing extra Low Memory...\n"));
491          WriteMacInt32(XLM_SHEEP_OBJ, (uint32)this);                                             // Pointer to SheepShaver object
492 <        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
609 <        WriteMacInt32(XLM_ETHER_TERM, *(uint32 *)TerminateStreamModule);
610 <        WriteMacInt32(XLM_ETHER_OPEN, *(uint32 *)ether_open);
611 <        WriteMacInt32(XLM_ETHER_CLOSE, *(uint32 *)ether_close);
612 <        WriteMacInt32(XLM_ETHER_WPUT, *(uint32 *)ether_wput);
613 <        WriteMacInt32(XLM_ETHER_RSRV, *(uint32 *)ether_rsrv);
614 <        WriteMacInt32(XLM_VIDEO_DOIO, *(uint32 *)VideoDoDriverIO);
615 < #endif
616 <        D(bug("Low Memory initialized\n"));
492 >        D(bug("Extra Low Memory initialized\n"));
493  
494          // Disallow quitting with Alt-Q from now on
495          AllowQuitting = false;
# Line 669 | Line 545 | void SheepShaver::Quit(void)
545          if (emul_thread > 0)
546                  wait_for_thread(emul_thread, &l);
547  
548 <        // Save NVRAM
549 <        XPRAMExit();
674 <
675 <        // Exit clipboard
676 <        ClipExit();
677 <
678 <        // Exit Time Manager
679 <        TimerExit();
680 <
681 <        // Exit serial
682 <        SerialExit();
683 <
684 <        // Exit network
685 <        EtherExit();
686 <
687 <        // Exit audio
688 <        AudioExit();
689 <
690 <        // Exit video
691 <        VideoExit();
692 <
693 <        // Exit external file system
694 <        ExtFSExit();
695 <
696 <        // Exit drivers
697 <        SCSIExit();
698 <        CDROMExit();
699 <        DiskExit();
700 <        SonyExit();
701 <
702 <        // Delete thunks
703 <        ThunksExit();
548 >        // Deinitialize everything
549 >        ExitAll();
550  
551          // Delete SheepShaver globals
552          SheepMem::Exit();
# Line 757 | Line 603 | void SheepShaver::init_rom(void)
603          page_size = B_PAGE_SIZE;
604  
605          // Create area for ROM
606 <        ROMBaseHost = (uint8 *)ROM_BASE;
607 <        rom_area = create_area(ROM_AREA_NAME, (void **)&ROMBaseHost, B_EXACT_ADDRESS, ROM_AREA_SIZE, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA);
606 >        ROMBase = ROM_BASE;
607 >        rom_area = create_area(ROM_AREA_NAME, (void **)&ROMBase, B_EXACT_ADDRESS, ROM_AREA_SIZE, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA);
608          if (rom_area < 0)
609                  throw area_error();
610 +        ROMBaseHost = (uint8 *)ROMBase;
611          D(bug("ROM area %ld at %p\n", rom_area, rom_addr));
612  
613          // Load ROM
# Line 781 | Line 628 | void SheepShaver::load_rom(void)
628          const char *rom_path = PrefsFindString("rom");
629  
630          // Try to open ROM file
631 <        BFile file(rom_path ? rom_path : ROM_FILE_NAME, B_READ_ONLY);
631 >        BFile file(rom_path && *rom_path ? rom_path : ROM_FILE_NAME, B_READ_ONLY);
632          if (file.InitCheck() != B_NO_ERROR) {
633  
634                  // Failed, then ask memory_mess driver for ROM
635                  uint8 *rom = new uint8[ROM_SIZE];       // Reading directly into the area doesn't work
636                  ssize_t actual = read(sheep_fd, (void *)rom, ROM_SIZE);
637                  if (actual == ROM_SIZE) {
638 <                        memcpy((void *)ROM_BASE, rom, ROM_SIZE);
638 >                        memcpy(ROMBaseHost, rom, ROM_SIZE);
639                          delete[] rom;
640                          return;
641                  } else
# Line 859 | Line 706 | status_t SheepShaver::emul_func(void *ar
706  
707          // Jump to ROM boot routine
708          D(bug("Jumping to ROM\n"));
709 <        obj->jump_to_rom(ROM_BASE + 0x310000);
709 >        obj->jump_to_rom(ROMBase + 0x310000);
710          D(bug("Returned from ROM\n"));
711  
712          // We're no longer ready to receive signals
# Line 1342 | Line 1189 | void Dump68kRegs(M68kRegisters *r)
1189  
1190   void MakeExecutable(int dummy, uint32 start, uint32 length)
1191   {
1192 <        if ((start >= ROM_BASE) && (start < (ROM_BASE + ROM_SIZE)))
1192 >        if ((start >= ROMBase) && (start < (ROMBase + ROM_SIZE)))
1193                  return;
1194          clear_caches((void *)start, length, B_INVALIDATE_ICACHE | B_FLUSH_DCACHE);
1195   }
1196  
1197  
1198   /*
1352 *  Patch things after system startup (gets called by disk driver accRun routine)
1353 */
1354
1355 void PatchAfterStartup(void)
1356 {
1357        ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL);
1358        InstallExtFS();
1359 }
1360
1361
1362 /*
1199   *  NVRAM watchdog thread (saves NVRAM every minute)
1200   */
1201  
# Line 1416 | Line 1252 | status_t SheepShaver::tick_func(void *ar
1252  
1253   void TriggerInterrupt(void)
1254   {
1255 +        idle_resume();
1256   #if 0
1257          WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1);
1258   #else
# Line 1567 | Line 1404 | void SheepShaver::sigusr1_handler(vregs
1404                                  // Execute nanokernel interrupt routine (this will activate the 68k emulator)
1405                                  atomic_add((int32 *)XLM_IRQ_NEST, 1);
1406                                  if (ROMType == ROMTYPE_NEWWORLD)
1407 <                                        ppc_interrupt(ROM_BASE + 0x312b1c);
1407 >                                        ppc_interrupt(ROMBase + 0x312b1c);
1408                                  else
1409 <                                        ppc_interrupt(ROM_BASE + 0x312a3c);
1409 >                                        ppc_interrupt(ROMBase + 0x312a3c);
1410                          }
1411                          break;
1412   #endif
# Line 1673 | Line 1510 | static void sigsegv_handler(vregs *r)
1510          uint32 imm = opcode & 0xffff;
1511  
1512          // Fault in Mac ROM or RAM?
1513 <        bool mac_fault = (r->pc >= ROM_BASE) && (r->pc < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc >= RAMBase) && (r->pc < (RAMBase + RAMSize));
1513 >        bool mac_fault = (r->pc >= ROMBase) && (r->pc < (ROMBase + ROM_AREA_SIZE)) || (r->pc >= RAMBase) && (r->pc < (RAMBase + RAMSize));
1514          if (mac_fault) {
1515  
1516                  // "VM settings" during MacOS 8 installation
1517 <                if (r->pc == ROM_BASE + 0x488160 && segv_r[20] == 0xf8000000) {
1517 >                if (r->pc == ROMBase + 0x488160 && segv_r[20] == 0xf8000000) {
1518                          r->pc += 4;
1519                          segv_r[8] = 0;
1520                          goto rti;
1521  
1522                  // MacOS 8.5 installation
1523 <                } else if (r->pc == ROM_BASE + 0x488140 && segv_r[16] == 0xf8000000) {
1523 >                } else if (r->pc == ROMBase + 0x488140 && segv_r[16] == 0xf8000000) {
1524                          r->pc += 4;
1525                          segv_r[8] = 0;
1526                          goto rti;
1527  
1528                  // MacOS 8 serial drivers on startup
1529 <                } else if (r->pc == ROM_BASE + 0x48e080 && (segv_r[8] == 0xf3012002 || segv_r[8] == 0xf3012000)) {
1529 >                } else if (r->pc == ROMBase + 0x48e080 && (segv_r[8] == 0xf3012002 || segv_r[8] == 0xf3012000)) {
1530                          r->pc += 4;
1531                          segv_r[8] = 0;
1532                          goto rti;
1533  
1534                  // MacOS 8.1 serial drivers on startup
1535 <                } else if (r->pc == ROM_BASE + 0x48c5e0 && (segv_r[20] == 0xf3012002 || segv_r[20] == 0xf3012000)) {
1535 >                } else if (r->pc == ROMBase + 0x48c5e0 && (segv_r[20] == 0xf3012002 || segv_r[20] == 0xf3012000)) {
1536                          r->pc += 4;
1537                          goto rti;
1538 <                } else if (r->pc == ROM_BASE + 0x4a10a0 && (segv_r[20] == 0xf3012002 || segv_r[20] == 0xf3012000)) {
1538 >                } else if (r->pc == ROMBase + 0x4a10a0 && (segv_r[20] == 0xf3012002 || segv_r[20] == 0xf3012000)) {
1539                          r->pc += 4;
1540                          goto rti;
1541                  }
# Line 1809 | Line 1646 | static void sigsegv_handler(vregs *r)
1646          }
1647  
1648          // Ignore ROM writes
1649 <        if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) {
1649 >        if (transfer_type == TYPE_STORE && addr >= ROMBase && addr < ROMBase + ROM_SIZE) {
1650                  D(bug("WARNING: %s write access to ROM at %p, pc %p\n", transfer_size == SIZE_BYTE ? "Byte" : transfer_size == SIZE_HALFWORD ? "Halfword" : "Word", addr, r->pc));
1651                  if (addr_mode == MODE_U || addr_mode == MODE_UX)
1652                          segv_r[ra] = addr;
# Line 1944 | Line 1781 | static void sigill_handler(vregs *r)
1781          uint32 imm = opcode & 0xffff;
1782  
1783          // Fault in Mac ROM or RAM?
1784 <        bool mac_fault = (r->pc >= ROM_BASE) && (r->pc < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc >= RAMBase) && (r->pc < (RAMBase + RAMSize));
1784 >        bool mac_fault = (r->pc >= ROMBase) && (r->pc < (ROMBase + ROM_AREA_SIZE)) || (r->pc >= RAMBase) && (r->pc < (RAMBase + RAMSize));
1785          if (mac_fault) {
1786  
1787                  switch (primop) {
# Line 2109 | Line 1946 | bool SheepMem::Init(void)
1946                  delete_area(old_sheep_area);
1947  
1948          // Create area for SheepShaver data
1949 <        base = 0x60000000;
1949 >        proc = base = 0x60000000;
1950          SheepMemArea = create_area(SHEEP_AREA_NAME, (void **)&base, B_BASE_ADDRESS, size, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA);
1951          if (SheepMemArea < 0)
1952                  return false;
# Line 2119 | Line 1956 | bool SheepMem::Init(void)
1956          zero_page = const_zero_page;
1957  
1958          D(bug("SheepShaver area %ld at %p\n", SheepMemArea, base));
1959 <        top = base + size;
1959 >        data = base + size;
1960          return true;
1961   }
1962  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines