30 |
|
# include <pthread.h> |
31 |
|
#endif |
32 |
|
|
33 |
< |
#if defined(USE_MAPPED_MEMORY) || REAL_ADDRESSING |
33 |
> |
#if defined(USE_MAPPED_MEMORY) || REAL_ADDRESSING || DIRECT_ADDRESSING |
34 |
|
# include <sys/mman.h> |
35 |
|
#endif |
36 |
|
|
108 |
|
Display *x_display = NULL; // X11 display handle |
109 |
|
|
110 |
|
static int zero_fd = -1; // FD of /dev/zero |
111 |
– |
static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped |
111 |
|
static uint8 last_xpram[256]; // Buffer for monitoring XPRAM changes |
112 |
|
|
113 |
|
#ifdef HAVE_PTHREADS |
133 |
|
static struct sigaction sigill_sa; // Illegal instruction |
134 |
|
static void *sig_stack = NULL; // Stack for signal handlers |
135 |
|
uint16 EmulatedSR; // Emulated bits of SR (supervisor bit and interrupt mask) |
136 |
< |
uint32 ScratchMem = NULL; // Scratch memory for Mac ROM writes |
136 |
> |
#endif |
137 |
> |
|
138 |
> |
#if USE_SCRATCHMEM_SUBTERFUGE |
139 |
> |
uint8 *ScratchMem = NULL; // Scratch memory for Mac ROM writes |
140 |
|
#endif |
141 |
|
|
142 |
|
static struct sigaction timer_sa; // sigaction used for timer |
151 |
|
static void sigint_handler(...); |
152 |
|
#endif |
153 |
|
|
154 |
+ |
#if REAL_ADDRESSING |
155 |
+ |
static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped |
156 |
+ |
static bool memory_mapped_from_zero = false; // Flag: Could allocate RAM area from 0 |
157 |
+ |
#endif |
158 |
+ |
|
159 |
+ |
#if REAL_ADDRESSING || DIRECT_ADDRESSING |
160 |
+ |
static uint32 mapped_ram_rom_size; // Total size of mmap()ed RAM/ROM area |
161 |
+ |
#endif |
162 |
+ |
|
163 |
|
#ifdef USE_MAPPED_MEMORY |
164 |
|
extern char *address_space, *good_address_map; |
165 |
|
#endif |
268 |
|
RAMSize = 1024*1024; |
269 |
|
} |
270 |
|
|
271 |
+ |
#if REAL_ADDRESSING || DIRECT_ADDRESSING |
272 |
+ |
const uint32 page_size = getpagesize(); |
273 |
+ |
const uint32 page_mask = page_size - 1; |
274 |
+ |
const uint32 aligned_ram_size = (RAMSize + page_mask) & ~page_mask; |
275 |
+ |
mapped_ram_rom_size = aligned_ram_size + 0x100000; |
276 |
+ |
#endif |
277 |
+ |
|
278 |
|
#if REAL_ADDRESSING |
279 |
+ |
// Try to allocate the complete address space from zero |
280 |
+ |
// gb-- the Solaris manpage about mmap(2) states that using MAP_FIXED |
281 |
+ |
// implies undefined behaviour for further use of sbrk(), malloc(), etc. |
282 |
+ |
// cebix-- on NetBSD/m68k, this causes a segfault |
283 |
+ |
#if defined(OS_solaris) || defined(OS_netbsd) |
284 |
+ |
// Anyway, it doesn't work... |
285 |
+ |
if (0) { |
286 |
+ |
#else |
287 |
+ |
if (mmap((caddr_t)0x0000, mapped_ram_rom_size, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0) != MAP_FAILED) { |
288 |
+ |
#endif |
289 |
+ |
D(bug("Could allocate RAM and ROM from 0x0000\n")); |
290 |
+ |
memory_mapped_from_zero = true; |
291 |
+ |
} |
292 |
|
// Create Low Memory area (0x0000..0x2000) |
293 |
< |
if (mmap((char *)0x0000, 0x2000, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0) == (void *)-1) { |
293 |
> |
else if (mmap((char *)0x0000, 0x2000, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0) != MAP_FAILED) { |
294 |
> |
D(bug("Could allocate the Low Memory globals\n")); |
295 |
> |
lm_area_mapped = true; |
296 |
> |
} |
297 |
> |
// Exit on error |
298 |
> |
else { |
299 |
|
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
300 |
|
ErrorAlert(str); |
301 |
|
QuitEmulator(); |
302 |
|
} |
267 |
– |
lm_area_mapped = true; |
303 |
|
#endif |
304 |
|
|
305 |
< |
#if !EMULATED_68K |
305 |
> |
#if USE_SCRATCHMEM_SUBTERFUGE |
306 |
|
// Allocate scratch memory |
307 |
< |
ScratchMem = (uint32)malloc(SCRATCH_MEM_SIZE); |
307 |
> |
ScratchMem = (uint8 *)malloc(SCRATCH_MEM_SIZE); |
308 |
|
if (ScratchMem == NULL) { |
309 |
|
ErrorAlert(GetString(STR_NO_MEM_ERR)); |
310 |
|
QuitEmulator(); |
332 |
|
mmap(good_address_map + i, 4096, PROT_READ, MAP_FIXED | MAP_PRIVATE, good_address_fd, 0); |
333 |
|
for (int i=0; i<0x80000; i+=4096) |
334 |
|
mmap(good_address_map + i + 0x00400000, 4096, PROT_READ, MAP_FIXED | MAP_PRIVATE, good_address_fd, 0); |
335 |
+ |
#elif REAL_ADDRESSING || DIRECT_ADDRESSING |
336 |
+ |
// gb-- Overkill, needs to be cleaned up. Probably explode it for either |
337 |
+ |
// real or direct addressing mode. |
338 |
+ |
#if REAL_ADDRESSING |
339 |
+ |
if (memory_mapped_from_zero) { |
340 |
+ |
RAMBaseHost = (uint8 *)0; |
341 |
+ |
ROMBaseHost = RAMBaseHost + aligned_ram_size; |
342 |
+ |
} |
343 |
+ |
else |
344 |
+ |
#endif |
345 |
+ |
{ |
346 |
+ |
RAMBaseHost = (uint8 *)mmap(0, mapped_ram_rom_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, zero_fd, 0); |
347 |
+ |
if (RAMBaseHost == (uint8 *)MAP_FAILED) { |
348 |
+ |
ErrorAlert(GetString(STR_NO_MEM_ERR)); |
349 |
+ |
QuitEmulator(); |
350 |
+ |
} |
351 |
+ |
ROMBaseHost = RAMBaseHost + aligned_ram_size; |
352 |
+ |
} |
353 |
|
#else |
354 |
< |
RAMBaseHost = new uint8[RAMSize]; |
355 |
< |
ROMBaseHost = new uint8[0x100000]; |
354 |
> |
RAMBaseHost = (uint8 *)malloc(RAMSize); |
355 |
> |
ROMBaseHost = (uint8 *)malloc(0x100000); |
356 |
> |
if (RAMBaseHost == NULL || ROMBaseHost == NULL) { |
357 |
> |
ErrorAlert(GetString(STR_NO_MEM_ERR)); |
358 |
> |
QuitEmulator(); |
359 |
> |
} |
360 |
> |
#endif |
361 |
> |
|
362 |
> |
#if DIRECT_ADDRESSING |
363 |
> |
// Initialize MEMBaseDiff now so that Host2MacAddr in the Video module |
364 |
> |
// will return correct results |
365 |
> |
RAMBaseMac = 0; |
366 |
> |
ROMBaseMac = RAMBaseMac + aligned_ram_size; |
367 |
> |
InitMEMBaseDiff(RAMBaseHost, RAMBaseMac); |
368 |
|
#endif |
369 |
< |
#if REAL_ADDRESSING && !EMULATED_68K |
369 |
> |
#if REAL_ADDRESSING // && !EMULATED_68K |
370 |
|
RAMBaseMac = (uint32)RAMBaseHost; |
371 |
|
ROMBaseMac = (uint32)ROMBaseHost; |
372 |
|
#endif |
373 |
|
D(bug("Mac RAM starts at %p (%08x)\n", RAMBaseHost, RAMBaseMac)); |
374 |
|
D(bug("Mac ROM starts at %p (%08x)\n", ROMBaseHost, ROMBaseMac)); |
375 |
< |
|
375 |
> |
|
376 |
|
// Get rom file path from preferences |
377 |
|
const char *rom_path = PrefsFindString("rom"); |
378 |
|
|
484 |
|
#ifdef ENABLE_MON |
485 |
|
// Setup SIGINT handler to enter mon |
486 |
|
sigemptyset(&sigint_sa.sa_mask); |
487 |
< |
sigint_sa.sa_handler = sigint_handler; |
487 |
> |
sigint_sa.sa_handler = (void (*)(int))sigint_handler; |
488 |
|
sigint_sa.sa_flags = 0; |
489 |
|
sigaction(SIGINT, &sigint_sa, NULL); |
490 |
|
#endif |
493 |
|
|
494 |
|
// POSIX.4 timers and real-time signals available, start 60Hz timer |
495 |
|
sigemptyset(&timer_sa.sa_mask); |
496 |
< |
timer_sa.sa_sigaction = one_tick; |
496 |
> |
timer_sa.sa_sigaction = (void (*)(int, siginfo_t *, void *))one_tick; |
497 |
|
timer_sa.sa_flags = SA_SIGINFO | SA_RESTART; |
498 |
|
if (sigaction(SIG_TIMER, &timer_sa, NULL) < 0) { |
499 |
|
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIG_TIMER", strerror(errno)); |
622 |
|
// Deinitialize everything |
623 |
|
ExitAll(); |
624 |
|
|
625 |
< |
// Delete ROM area |
626 |
< |
delete[] ROMBaseHost; |
627 |
< |
|
628 |
< |
// Delete RAM area |
629 |
< |
delete[] RAMBaseHost; |
625 |
> |
// Free ROM/RAM areas |
626 |
> |
#if REAL_ADDRESSING |
627 |
> |
if (memory_mapped_from_zero) |
628 |
> |
munmap((caddr_t)0x0000, mapped_ram_rom_size); |
629 |
> |
else |
630 |
> |
#endif |
631 |
> |
#if REAL_ADDRESSING || DIRECT_ADDRESSING |
632 |
> |
if (RAMBaseHost != (uint8 *)MAP_FAILED) { |
633 |
> |
munmap((caddr_t)RAMBaseHost, mapped_ram_rom_size); |
634 |
> |
RAMBaseHost = NULL; |
635 |
> |
} |
636 |
> |
#else |
637 |
> |
if (ROMBaseHost) { |
638 |
> |
free(ROMBaseHost); |
639 |
> |
ROMBaseHost = NULL; |
640 |
> |
} |
641 |
> |
if (RAMBaseHost) { |
642 |
> |
free(RAMBaseHost); |
643 |
> |
RAMBaseHost = NULL; |
644 |
> |
} |
645 |
> |
#endif |
646 |
|
|
647 |
< |
#if !EMULATED_68K |
647 |
> |
#if USE_SCRATCHMEM_SUBTERFUGE |
648 |
|
// Delete scratch memory area |
649 |
< |
if (ScratchMem) |
649 |
> |
if (ScratchMem) { |
650 |
|
free((void *)(ScratchMem - SCRATCH_MEM_SIZE/2)); |
651 |
+ |
ScratchMem = NULL; |
652 |
+ |
} |
653 |
|
#endif |
654 |
|
|
655 |
|
#if REAL_ADDRESSING |
701 |
|
extern void m68k_dumpstate(uaecptr *nextpc); |
702 |
|
m68k_dumpstate(&nextpc); |
703 |
|
#else |
704 |
< |
char *arg[2] = {"rmon", NULL}; |
705 |
< |
mon(1, arg); |
704 |
> |
char *arg[4] = {"mon", "-m", "-r", NULL}; |
705 |
> |
mon(3, arg); |
706 |
|
QuitEmulator(); |
707 |
|
#endif |
708 |
|
} |
748 |
|
raise(SIG_IRQ); |
749 |
|
#endif |
750 |
|
} |
751 |
+ |
|
752 |
+ |
void TriggerNMI(void) |
753 |
+ |
{ |
754 |
+ |
// not yet supported |
755 |
+ |
} |
756 |
|
#endif |
757 |
|
|
758 |
|
|
772 |
|
static void *xpram_func(void *arg) |
773 |
|
{ |
774 |
|
while (!xpram_thread_cancel) { |
775 |
< |
for (int i=0; i<60 && !xpram_thread_cancel; i++) { |
776 |
< |
#ifdef HAVE_NANOSLEEP |
689 |
< |
struct timespec req = {1, 0}; |
690 |
< |
nanosleep(&req, NULL); |
691 |
< |
#else |
692 |
< |
usleep(1000000); |
693 |
< |
#endif |
694 |
< |
} |
775 |
> |
for (int i=0; i<60 && !xpram_thread_cancel; i++) |
776 |
> |
Delay_usec(1000000); |
777 |
|
xpram_watchdog(); |
778 |
|
} |
779 |
|
return NULL; |
790 |
|
// Pseudo Mac 1Hz interrupt, update local time |
791 |
|
WriteMacInt32(0x20c, TimerDateTime()); |
792 |
|
|
793 |
< |
SetInterruptFlag(INTFLAG_60HZ); |
793 |
> |
SetInterruptFlag(INTFLAG_1HZ); |
794 |
|
TriggerInterrupt(); |
795 |
|
|
796 |
|
#ifndef HAVE_PTHREADS |
825 |
|
#ifdef HAVE_PTHREADS |
826 |
|
static void *tick_func(void *arg) |
827 |
|
{ |
828 |
+ |
uint64 next = GetTicks_usec(); |
829 |
|
while (!tick_thread_cancel) { |
747 |
– |
|
748 |
– |
// Wait |
749 |
– |
#ifdef HAVE_NANOSLEEP |
750 |
– |
struct timespec req = {0, 16625000}; |
751 |
– |
nanosleep(&req, NULL); |
752 |
– |
#else |
753 |
– |
usleep(16625); |
754 |
– |
#endif |
755 |
– |
|
756 |
– |
// Action |
830 |
|
one_tick(); |
831 |
+ |
next += 16625; |
832 |
+ |
int64 delay = next - GetTicks_usec(); |
833 |
+ |
if (delay > 0) |
834 |
+ |
Delay_usec(delay); |
835 |
+ |
else if (delay < -16625) |
836 |
+ |
next = GetTicks_usec(); |
837 |
|
} |
838 |
|
return NULL; |
839 |
|
} |
840 |
|
#endif |
841 |
|
|
842 |
|
|
843 |
+ |
/* |
844 |
+ |
* Get current value of microsecond timer |
845 |
+ |
*/ |
846 |
+ |
|
847 |
+ |
uint64 GetTicks_usec(void) |
848 |
+ |
{ |
849 |
+ |
#ifdef HAVE_CLOCK_GETTIME |
850 |
+ |
struct timespec t; |
851 |
+ |
clock_gettime(CLOCK_REALTIME, &t); |
852 |
+ |
return (uint64)t.tv_sec * 1000000 + t.tv_nsec / 1000; |
853 |
+ |
#else |
854 |
+ |
struct timeval t; |
855 |
+ |
gettimeofday(&t, NULL); |
856 |
+ |
return (uint64)t.tv_sec * 1000000 + t.tv_usec; |
857 |
+ |
#endif |
858 |
+ |
} |
859 |
+ |
|
860 |
+ |
|
861 |
+ |
/* |
862 |
+ |
* Delay by specified number of microseconds (<1 second) |
863 |
+ |
* (adapted from SDL_Delay() source) |
864 |
+ |
*/ |
865 |
+ |
|
866 |
+ |
void Delay_usec(uint32 usec) |
867 |
+ |
{ |
868 |
+ |
int was_error; |
869 |
+ |
#ifndef __linux__ // Non-Linux implementations need to calculate time left |
870 |
+ |
uint64 then, now, elapsed; |
871 |
+ |
#endif |
872 |
+ |
struct timeval tv; |
873 |
+ |
|
874 |
+ |
// Set the timeout interval - Linux only needs to do this once |
875 |
+ |
#ifdef __linux__ |
876 |
+ |
tv.tv_sec = 0; |
877 |
+ |
tv.tv_usec = usec; |
878 |
+ |
#else |
879 |
+ |
then = GetTicks_usec(); |
880 |
+ |
#endif |
881 |
+ |
do { |
882 |
+ |
errno = 0; |
883 |
+ |
#ifndef __linux__ |
884 |
+ |
/* Calculate the time interval left (in case of interrupt) */ |
885 |
+ |
now = GetTicks_usec(); |
886 |
+ |
elapsed = now - then; |
887 |
+ |
then = now; |
888 |
+ |
if (elapsed >= usec) |
889 |
+ |
break; |
890 |
+ |
usec -= elapsed; |
891 |
+ |
tv.tv_sec = 0; |
892 |
+ |
tv.tv_usec = usec; |
893 |
+ |
#endif |
894 |
+ |
was_error = select(0, NULL, NULL, NULL, &tv); |
895 |
+ |
} while (was_error && (errno == EINTR)); |
896 |
+ |
} |
897 |
+ |
|
898 |
+ |
|
899 |
|
#if !EMULATED_68K |
900 |
|
/* |
901 |
|
* Virtual 68k interrupt handler |
1166 |
|
printf(" a%d %08x\n", i, state->ss_frame.f_regs[i+8]); |
1167 |
|
|
1168 |
|
#ifdef ENABLE_MON |
1169 |
< |
char *arg[2] = {"rmon", NULL}; |
1170 |
< |
mon(1, arg); |
1169 |
> |
char *arg[4] = {"mon", "-m", "-r", NULL}; |
1170 |
> |
mon(3, arg); |
1171 |
|
#endif |
1172 |
|
QuitEmulator(); |
1173 |
|
break; |