ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/main_unix.cpp
(Generate patch)

Comparing BasiliskII/src/Unix/main_unix.cpp (file contents):
Revision 1.20 by gbeauche, 2000-09-22T17:14:28Z vs.
Revision 1.28 by cebix, 2001-01-04T19:50:22Z

# Line 30 | Line 30
30   # include <pthread.h>
31   #endif
32  
33 < #if defined(USE_MAPPED_MEMORY) || REAL_ADDRESSING || DIRECT_ADDRESSING
33 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
34   # include <sys/mman.h>
35   #endif
36  
# Line 50 | Line 50 | struct sigstate {
50  
51   #ifdef ENABLE_GTK
52   # include <gtk/gtk.h>
53 + # include <gdk/gdk.h>
54   #endif
55  
56   #ifdef ENABLE_XF86_DGA
# Line 133 | Line 134 | static struct sigaction sigirq_sa;     // Vi
134   static struct sigaction sigill_sa;      // Illegal instruction
135   static void *sig_stack = NULL;          // Stack for signal handlers
136   uint16 EmulatedSR;                                      // Emulated bits of SR (supervisor bit and interrupt mask)
136 uint8 *ScratchMem = NULL;                       // Scratch memory for Mac ROM writes
137   #endif
138  
139   #if USE_SCRATCHMEM_SUBTERFUGE
140 < uint8 *ScratchMem = 0;                          // Scratch memory for Mac ROM writes
140 > uint8 *ScratchMem = NULL;                       // Scratch memory for Mac ROM writes
141   #endif
142  
143   static struct sigaction timer_sa;       // sigaction used for timer
# Line 157 | Line 157 | static bool lm_area_mapped = false;    // F
157   static bool memory_mapped_from_zero = false; // Flag: Could allocate RAM area from 0
158   #endif
159  
160 < #ifdef USE_MAPPED_MEMORY
161 < extern char *address_space, *good_address_map;
160 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
161 > static uint32 mapped_ram_rom_size;              // Total size of mmap()ed RAM/ROM area
162   #endif
163  
164  
# Line 209 | Line 209 | int main(int argc, char **argv)
209          printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR);
210          printf(" %s\n", GetString(STR_ABOUT_TEXT2));
211  
212 <        // Parse arguments
212 > #ifdef ENABLE_GTK
213 >        // Init GTK
214 >        gtk_set_locale();
215 >        gtk_init(&argc, &argv);
216 >        x_display_name = gdk_get_display(); // gtk_init() handles and removes the "--display" argument
217 > #endif
218 >
219 >        // Parse and remove arguments
220          for (int i=1; i<argc; i++) {
221 <                if (strcmp(argv[i], "-display") == 0 && ++i < argc)
222 <                        x_display_name = argv[i];
223 <                else if (strcmp(argv[i], "-break") == 0 && ++i < argc)
224 <                        ROMBreakpoint = strtol(argv[i], NULL, 0);
225 <                else if (strcmp(argv[i], "-rominfo") == 0)
221 >                if (strcmp(argv[i], "--display") == 0) {
222 >                        argv[i] = NULL;
223 >                        if ((i + 1) < argc && argv[i + 1]) {
224 >                                argv[i++] = NULL;
225 >                                x_display_name = strdup(argv[i]);
226 >                        }
227 >                } else if (strcmp(argv[i], "--break") == 0) {
228 >                        argv[i] = NULL;
229 >                        if ((i + 1) < argc && argv[i + 1]) {
230 >                                argv[i++] = NULL;
231 >                                ROMBreakpoint = strtol(argv[i], NULL, 0);
232 >                        }
233 >                } else if (strcmp(argv[i], "--rominfo") == 0) {
234 >                        argv[i] = NULL;
235                          PrintROMInfo = true;
236 +                }
237 +        }
238 +        for (int i=1; i<argc; i++) {
239 +                int k;
240 +                for (k=i; k<argc; k++)
241 +                        if (argv[k] != NULL)
242 +                                break;
243 +                if (k > i) {
244 +                        k -= i;
245 +                        for (int j=i+k; j<argc; j++)
246 +                                argv[j-k] = argv[j];
247 +                        argc -= k;
248 +                }
249          }
250  
251          // Open display
# Line 233 | Line 262 | int main(int argc, char **argv)
262          XF86DGAForkApp(DefaultScreen(x_display));
263   #endif
264  
236 #ifdef ENABLE_GTK
237        // Init GTK
238        gtk_set_locale();
239        gtk_init(&argc, &argv);
240 #endif
241
265          // Read preferences
266 <        PrefsInit();
266 >        PrefsInit(argc, argv);
267  
268          // Init system routines
269          SysInit();
# Line 269 | Line 292 | int main(int argc, char **argv)
292          const uint32 page_size = getpagesize();
293          const uint32 page_mask = page_size - 1;
294          const uint32 aligned_ram_size = (RAMSize + page_mask) & ~page_mask;
295 <        const uint32 ram_rom_size = aligned_ram_size + 0x100000;
295 >        mapped_ram_rom_size = aligned_ram_size + 0x100000;
296   #endif
297  
298   #if REAL_ADDRESSING
299          // Try to allocate the complete address space from zero
300          // gb-- the Solaris manpage about mmap(2) states that using MAP_FIXED
301          // implies undefined behaviour for further use of sbrk(), malloc(), etc.
302 < #if defined(OS_solaris)
302 >        // cebix-- on NetBSD/m68k, this causes a segfault
303 > #if defined(OS_solaris) || defined(OS_netbsd)
304          // Anyway, it doesn't work...
305          if (0) {
306   #else
307 <        if (mmap((caddr_t)0x0000, ram_rom_size, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0) != MAP_FAILED) {
307 >        if (mmap((caddr_t)0x0000, mapped_ram_rom_size, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0) != MAP_FAILED) {
308   #endif
309                  D(bug("Could allocate RAM and ROM from 0x0000\n"));
310                  memory_mapped_from_zero = true;
# Line 298 | Line 322 | int main(int argc, char **argv)
322          }
323   #endif
324  
325 < #if !EMULATED_68K || USE_SCRATCHMEM_SUBTERFUGE
325 > #if USE_SCRATCHMEM_SUBTERFUGE
326          // Allocate scratch memory
327          ScratchMem = (uint8 *)malloc(SCRATCH_MEM_SIZE);
328          if (ScratchMem == NULL) {
# Line 309 | Line 333 | int main(int argc, char **argv)
333   #endif
334  
335          // Create areas for Mac RAM and ROM
336 < #if defined(USE_MAPPED_MEMORY)
313 <    good_address_map = (char *)mmap(NULL, 1<<24, PROT_READ, MAP_PRIVATE, zero_fd, 0);
314 <    address_space = (char *)mmap(NULL, 1<<24, PROT_READ | PROT_WRITE, MAP_PRIVATE, zero_fd, 0);
315 <    if ((int)address_space < 0 || (int)good_address_map < 0) {
316 <                ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
317 <                QuitEmulator();
318 <    }
319 <    RAMBaseHost = (uint8 *)mmap(address_space, RAMSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, zero_fd, 0);
320 <    ROMBaseHost = (uint8 *)mmap(address_space + 0x00400000, 0x80000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, zero_fd, 0);
321 <        char *nam = tmpnam(NULL);
322 <    int good_address_fd = open(nam, O_CREAT | O_RDWR, 0600);
323 <        char buffer[4096];
324 <    memset(buffer, 1, sizeof(buffer));
325 <    write(good_address_fd, buffer, sizeof(buffer));
326 <    unlink(nam);
327 <    for (int i=0; i<RAMSize; i+=4096)
328 <        mmap(good_address_map + i, 4096, PROT_READ, MAP_FIXED | MAP_PRIVATE, good_address_fd, 0);
329 <    for (int i=0; i<0x80000; i+=4096)
330 <        mmap(good_address_map + i + 0x00400000, 4096, PROT_READ, MAP_FIXED | MAP_PRIVATE, good_address_fd, 0);
331 < #elif REAL_ADDRESSING || DIRECT_ADDRESSING
336 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
337          // gb-- Overkill, needs to be cleaned up. Probably explode it for either
338          // real or direct addressing mode.
339   #if REAL_ADDRESSING
# Line 339 | Line 344 | int main(int argc, char **argv)
344          else
345   #endif
346          {
347 <                RAMBaseHost = (uint8 *)mmap(0, ram_rom_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, zero_fd, 0);
347 >                RAMBaseHost = (uint8 *)mmap(0, mapped_ram_rom_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, zero_fd, 0);
348                  if (RAMBaseHost == (uint8 *)MAP_FAILED) {
349                          ErrorAlert(GetString(STR_NO_MEM_ERR));
350                          QuitEmulator();
# Line 354 | Line 359 | int main(int argc, char **argv)
359                  QuitEmulator();
360          }
361   #endif
362 +
363   #if DIRECT_ADDRESSING
364          // Initialize MEMBaseDiff now so that Host2MacAddr in the Video module
365          // will return correct results
# Line 416 | Line 422 | int main(int argc, char **argv)
422                  printf("WARNING: Cannot detect CPU type, assuming 68020\n");
423                  CPUType = 2;
424          }
425 <        FPUType = 0;    //!!
425 >        FPUType = 1;    // NetBSD has an FPU emulation, so the FPU ought to be available at all times
426          TwentyFourBitAddressing = false;
427   #endif
428  
# Line 479 | Line 485 | int main(int argc, char **argv)
485   #ifdef ENABLE_MON
486          // Setup SIGINT handler to enter mon
487          sigemptyset(&sigint_sa.sa_mask);
488 <        sigint_sa.sa_handler = sigint_handler;
488 >        sigint_sa.sa_handler = (void (*)(int))sigint_handler;
489          sigint_sa.sa_flags = 0;
490          sigaction(SIGINT, &sigint_sa, NULL);
491   #endif
# Line 617 | Line 623 | void QuitEmulator(void)
623          // Deinitialize everything
624          ExitAll();
625  
626 +        // Free ROM/RAM areas
627 + #if REAL_ADDRESSING
628 +        if (memory_mapped_from_zero)
629 +                munmap((caddr_t)0x0000, mapped_ram_rom_size);
630 +        else
631 + #endif
632   #if REAL_ADDRESSING || DIRECT_ADDRESSING
621        // Unmap ROM area
622        if (ROMBaseHost != (uint8 *)MAP_FAILED) {
623                munmap((caddr_t)ROMBaseHost, 0x100000);
624                ROMBaseHost = 0;
625        }
626        
627        //Unmap RAM area
633          if (RAMBaseHost != (uint8 *)MAP_FAILED) {
634 <                const uint32 page_size = getpagesize();
635 <                const uint32 page_mask = page_size - 1;
631 <                munmap((caddr_t)RAMBaseHost, ((RAMSize + page_mask) & ~page_mask));
634 >                munmap((caddr_t)RAMBaseHost, mapped_ram_rom_size);
635 >                RAMBaseHost = NULL;
636          }
637   #else
634        // Delete ROM area
638          if (ROMBaseHost) {
639                  free(ROMBaseHost);
640                  ROMBaseHost = NULL;
641          }
639
640        // Delete RAM area
642          if (RAMBaseHost) {
643                  free(RAMBaseHost);
644                  RAMBaseHost = NULL;
645          }
646   #endif
647  
648 < #if !EMULATED_68K || USE_SCRATMEM_SUBTERFUGE
648 > #if USE_SCRATCHMEM_SUBTERFUGE
649          // Delete scratch memory area
650          if (ScratchMem) {
651                  free((void *)(ScratchMem - SCRATCH_MEM_SIZE/2));
# Line 701 | Line 702 | static void sigint_handler(...)
702          extern void m68k_dumpstate(uaecptr *nextpc);
703          m68k_dumpstate(&nextpc);
704   #else
705 <        char *arg[2] = {"rmon", NULL};
706 <        mon(1, arg);
705 >        char *arg[4] = {"mon", "-m", "-r", NULL};
706 >        mon(3, arg);
707          QuitEmulator();
708   #endif
709   }
# Line 748 | Line 749 | void TriggerInterrupt(void)
749          raise(SIG_IRQ);
750   #endif
751   }
752 +
753 + void TriggerNMI(void)
754 + {
755 +        // not yet supported
756 + }
757   #endif
758  
759  
# Line 768 | Line 774 | static void *xpram_func(void *arg)
774   {
775          while (!xpram_thread_cancel) {
776                  for (int i=0; i<60 && !xpram_thread_cancel; i++)
777 <                        Delay_usec(1000000);
777 >                        Delay_usec(999999);
778                  xpram_watchdog();
779          }
780          return NULL;
# Line 855 | Line 861 | uint64 GetTicks_usec(void)
861  
862   /*
863   *  Delay by specified number of microseconds (<1 second)
864 < *  (adapted from SDL_Delay() source)
864 > *  (adapted from SDL_Delay() source; this function is designed to provide
865 > *  the highest accuracy possible)
866   */
867  
868 + #if defined(linux)
869 + // Linux select() changes its timeout parameter upon return to contain
870 + // the remaining time. Most other unixen leave it unchanged or undefined.
871 + #define SELECT_SETS_REMAINING
872 + #elif defined(__FreeBSD__) || defined(__sun__) || defined(sgi)
873 + #define USE_NANOSLEEP
874 + #endif
875 +
876   void Delay_usec(uint32 usec)
877   {
878          int was_error;
879 < #ifndef __linux__       // Non-Linux implementations need to calculate time left
879 >
880 > #ifdef USE_NANOSLEEP
881 >        struct timespec elapsed, tv;
882 > #else
883 >        struct timeval tv;
884 > #ifndef SELECT_SETS_REMAINING
885          uint64 then, now, elapsed;
886   #endif
887 <        struct timeval tv;
887 > #endif
888  
889          // Set the timeout interval - Linux only needs to do this once
890 < #ifdef __linux__
891 <        tv.tv_sec = 0;
892 <        tv.tv_usec = usec;
890 > #ifdef SELECT_SETS_REMAINING
891 >    tv.tv_sec = 0;
892 >    tv.tv_usec = usec;
893 > #elif defined(USE_NANOSLEEP)
894 >    elapsed.tv_sec = 0;
895 >    elapsed.tv_nsec = usec * 1000;
896   #else
897 <        then = GetTicks_usec();
897 >    then = GetTicks_usec();
898   #endif
899 +
900          do {
901                  errno = 0;
902 < #ifndef __linux__
903 <                /* Calculate the time interval left (in case of interrupt) */
902 > #ifdef USE_NANOSLEEP
903 >                tv.tv_sec = elapsed.tv_sec;
904 >                tv.tv_nsec = elapsed.tv_nsec;
905 >                was_error = nanosleep(&tv, &elapsed);
906 > #else
907 > #ifndef SELECT_SETS_REMAINING
908 >                // Calculate the time interval left (in case of interrupt)
909                  now = GetTicks_usec();
910                  elapsed = now - then;
911                  then = now;
# Line 887 | Line 916 | void Delay_usec(uint32 usec)
916                  tv.tv_usec = usec;
917   #endif
918                  was_error = select(0, NULL, NULL, NULL, &tv);
919 + #endif
920          } while (was_error && (errno == EINTR));
921   }
922  
# Line 941 | Line 971 | static void sigill_handler(int sig, int
971  
972   #define STORE_SR(v) \
973          scp->sc_ps = (v) & 0xff; \
974 <        EmulatedSR = (v) & 0x2700; \
974 >        EmulatedSR = (v) & 0xe700; \
975          if (((v) & 0x0700) == 0 && InterruptFlags) \
976                  TriggerInterrupt();
977  
# Line 1004 | Line 1034 | static void sigill_handler(int sig, int
1034                  case 0x007c: {  // ori #xxxx,sr
1035                          uint16 sr = GET_SR | pc[1];
1036                          scp->sc_ps = sr & 0xff;         // oring bits into the sr can't enable interrupts, so we don't need to call STORE_SR
1037 <                        EmulatedSR = sr & 0x2700;
1037 >                        EmulatedSR = sr & 0xe700;
1038                          INC_PC(4);
1039                          break;
1040                  }
# Line 1081 | Line 1111 | static void sigill_handler(int sig, int
1111                  }
1112  
1113                  case 0xf327:    // fsave -(sp)
1114 <                        goto ill;       //!!
1114 >                        if (CPUIs68060) {
1115 >                                regs->a[7] -= 4;
1116 >                                WriteMacInt32(regs->a[7], 0x60000000);  // Idle frame
1117 >                                regs->a[7] -= 4;
1118 >                                WriteMacInt32(regs->a[7], 0);
1119 >                                regs->a[7] -= 4;
1120 >                                WriteMacInt32(regs->a[7], 0);
1121 >                        } else {
1122 >                                regs->a[7] -= 4;
1123 >                                WriteMacInt32(regs->a[7], 0x41000000);  // Idle frame
1124 >                        }
1125 >                        scp->sc_sp = regs->a[7];
1126 >                        INC_PC(2);
1127 >                        break;
1128  
1129                  case 0xf35f:    // frestore (sp)+
1130 <                        goto ill;       //!!
1130 >                        if (CPUIs68060)
1131 >                                regs->a[7] += 12;
1132 >                        else
1133 >                                regs->a[7] += 4;
1134 >                        scp->sc_sp = regs->a[7];
1135 >                        INC_PC(2);
1136 >                        break;
1137  
1138 <                case 0x4e73: {  // rte (only handles format 0)
1138 >                case 0x4e73: {  // rte
1139                          uint32 a7 = regs->a[7];
1140                          uint16 sr = ReadMacInt16(a7);
1141                          a7 += 2;
1142                          scp->sc_ps = sr & 0xff;
1143 <                        EmulatedSR = sr & 0x2700;
1143 >                        EmulatedSR = sr & 0xe700;
1144                          scp->sc_pc = ReadMacInt32(a7);
1145 <                        a7 += 6;
1146 <                        scp->sc_sp = regs->a[7] = a7;
1145 >                        a7 += 4;
1146 >                        uint16 format = ReadMacInt16(a7) >> 12;
1147 >                        a7 += 2;
1148 >                        static const int frame_adj[16] = {
1149 >                                0, 0, 4, 4, 8, 0, 0, 52, 50, 12, 24, 84, 16, 0, 0, 0
1150 >                        };
1151 >                        scp->sc_sp = regs->a[7] = a7 + frame_adj[format];
1152                          break;
1153                  }
1154  
1155                  case 0x4e7a:    // movec cr,x
1156                          switch (pc[1]) {
1103                                case 0x8801:    // movec vbr,a0
1104                                        regs->a[0] = 0;
1105                                        break;
1106                                case 0x9801:    // movec vbr,a1
1107                                        regs->a[1] = 0;
1108                                        break;
1157                                  case 0x0002:    // movec cacr,d0
1158                                          regs->d[0] = 0x3111;
1159                                          break;
# Line 1113 | Line 1161 | static void sigill_handler(int sig, int
1161                                          regs->d[1] = 0x3111;
1162                                          break;
1163                                  case 0x0003:    // movec tc,d0
1164 +                                case 0x0004:    // movec itt0,d0
1165 +                                case 0x0005:    // movec itt1,d0
1166 +                                case 0x0006:    // movec dtt0,d0
1167 +                                case 0x0007:    // movec dtt1,d0
1168 +                                case 0x0806:    // movec urp,d0
1169 +                                case 0x0807:    // movec srp,d0
1170                                          regs->d[0] = 0;
1171                                          break;
1172 +                                case 0x1000:    // movec sfc,d1
1173 +                                case 0x1001:    // movec dfc,d1
1174                                  case 0x1003:    // movec tc,d1
1175 +                                case 0x1801:    // movec vbr,d1
1176                                          regs->d[1] = 0;
1177                                          break;
1178 +                                case 0x8801:    // movec vbr,a0
1179 +                                        regs->a[0] = 0;
1180 +                                        break;
1181 +                                case 0x9801:    // movec vbr,a1
1182 +                                        regs->a[1] = 0;
1183 +                                        break;
1184                                  default:
1185                                          goto ill;
1186                          }
# Line 1126 | Line 1189 | static void sigill_handler(int sig, int
1189  
1190                  case 0x4e7b:    // movec x,cr
1191                          switch (pc[1]) {
1192 +                                case 0x1000:    // movec d1,sfc
1193 +                                case 0x1001:    // movec d1,dfc
1194                                  case 0x0801:    // movec d0,vbr
1195 +                                case 0x1801:    // movec d1,vbr
1196                                          break;
1197                                  case 0x0002:    // movec d0,cacr
1198                                  case 0x1002:    // movec d1,cacr
# Line 1161 | Line 1227 | ill:           printf("SIGILL num %d, code %d\n",
1227                                  printf("  a%d %08x\n", i, state->ss_frame.f_regs[i+8]);
1228  
1229   #ifdef ENABLE_MON
1230 <                        char *arg[2] = {"rmon", NULL};
1231 <                        mon(1, arg);
1230 >                        char *arg[4] = {"mon", "-m", "-r", NULL};
1231 >                        mon(3, arg);
1232   #endif
1233                          QuitEmulator();
1234                          break;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines