75 |
|
# include "mon.h" |
76 |
|
#endif |
77 |
|
|
78 |
< |
#define DEBUG 1 |
78 |
> |
#define DEBUG 0 |
79 |
|
#include "debug.h" |
80 |
|
|
81 |
|
|
104 |
|
|
105 |
|
|
106 |
|
// Global variables |
107 |
< |
static char *x_display_name = NULL; // X11 display name |
107 |
> |
char *x_display_name = NULL; // X11 display name |
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 |
148 |
|
static void sigint_handler(...); |
149 |
|
#endif |
150 |
|
|
151 |
+ |
#if REAL_ADDRESSING |
152 |
+ |
static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped |
153 |
+ |
#endif |
154 |
+ |
|
155 |
|
#ifdef USE_MAPPED_MEMORY |
156 |
|
extern char *address_space, *good_address_map; |
157 |
|
#endif |
301 |
|
for (int i=0; i<0x80000; i+=4096) |
302 |
|
mmap(good_address_map + i + 0x00400000, 4096, PROT_READ, MAP_FIXED | MAP_PRIVATE, good_address_fd, 0); |
303 |
|
#else |
304 |
< |
RAMBaseHost = new uint8[RAMSize]; |
305 |
< |
ROMBaseHost = new uint8[0x100000]; |
304 |
> |
RAMBaseHost = (uint8 *)malloc(RAMSize); |
305 |
> |
ROMBaseHost = (uint8 *)malloc(0x100000); |
306 |
|
#endif |
307 |
+ |
if (RAMBaseHost == NULL || ROMBaseHost == NULL) { |
308 |
+ |
ErrorAlert(GetString(STR_NO_MEM_ERR)); |
309 |
+ |
QuitEmulator(); |
310 |
+ |
} |
311 |
|
#if REAL_ADDRESSING && !EMULATED_68K |
312 |
|
RAMBaseMac = (uint32)RAMBaseHost; |
313 |
|
ROMBaseMac = (uint32)ROMBaseHost; |
565 |
|
ExitAll(); |
566 |
|
|
567 |
|
// Delete ROM area |
568 |
< |
delete[] ROMBaseHost; |
568 |
> |
if (ROMBaseHost) { |
569 |
> |
free(ROMBaseHost); |
570 |
> |
ROMBaseHost = NULL; |
571 |
> |
} |
572 |
|
|
573 |
|
// Delete RAM area |
574 |
< |
delete[] RAMBaseHost; |
574 |
> |
if (RAMBaseHost) { |
575 |
> |
free(RAMBaseHost); |
576 |
> |
RAMBaseHost = NULL; |
577 |
> |
} |
578 |
|
|
579 |
|
#if !EMULATED_68K |
580 |
|
// Delete scratch memory area |
581 |
< |
if (ScratchMem) |
581 |
> |
if (ScratchMem) { |
582 |
|
free((void *)(ScratchMem - SCRATCH_MEM_SIZE/2)); |
583 |
+ |
ScratchMem = NULL; |
584 |
+ |
} |
585 |
|
#endif |
586 |
|
|
587 |
|
#if REAL_ADDRESSING |
699 |
|
static void *xpram_func(void *arg) |
700 |
|
{ |
701 |
|
while (!xpram_thread_cancel) { |
702 |
< |
for (int i=0; i<60 && !xpram_thread_cancel; i++) { |
703 |
< |
#ifdef HAVE_NANOSLEEP |
689 |
< |
struct timespec req = {1, 0}; |
690 |
< |
nanosleep(&req, NULL); |
691 |
< |
#else |
692 |
< |
usleep(1000000); |
693 |
< |
#endif |
694 |
< |
} |
702 |
> |
for (int i=0; i<60 && !xpram_thread_cancel; i++) |
703 |
> |
Delay_usec(1000000); |
704 |
|
xpram_watchdog(); |
705 |
|
} |
706 |
|
return NULL; |
717 |
|
// Pseudo Mac 1Hz interrupt, update local time |
718 |
|
WriteMacInt32(0x20c, TimerDateTime()); |
719 |
|
|
720 |
+ |
SetInterruptFlag(INTFLAG_60HZ); |
721 |
+ |
TriggerInterrupt(); |
722 |
+ |
|
723 |
|
#ifndef HAVE_PTHREADS |
724 |
|
static int second_counter = 0; |
725 |
|
if (++second_counter > 60) { |
752 |
|
#ifdef HAVE_PTHREADS |
753 |
|
static void *tick_func(void *arg) |
754 |
|
{ |
755 |
+ |
uint64 next = GetTicks_usec(); |
756 |
|
while (!tick_thread_cancel) { |
744 |
– |
|
745 |
– |
// Wait |
746 |
– |
#ifdef HAVE_NANOSLEEP |
747 |
– |
struct timespec req = {0, 16625000}; |
748 |
– |
nanosleep(&req, NULL); |
749 |
– |
#else |
750 |
– |
usleep(16625); |
751 |
– |
#endif |
752 |
– |
|
753 |
– |
// Action |
757 |
|
one_tick(); |
758 |
+ |
next += 16625; |
759 |
+ |
int64 delay = next - GetTicks_usec(); |
760 |
+ |
if (delay > 0) |
761 |
+ |
Delay_usec(delay); |
762 |
+ |
else if (delay < -16625) |
763 |
+ |
next = GetTicks_usec(); |
764 |
|
} |
765 |
|
return NULL; |
766 |
|
} |
767 |
|
#endif |
768 |
|
|
769 |
|
|
770 |
+ |
/* |
771 |
+ |
* Get current value of microsecond timer |
772 |
+ |
*/ |
773 |
+ |
|
774 |
+ |
uint64 GetTicks_usec(void) |
775 |
+ |
{ |
776 |
+ |
#ifdef HAVE_CLOCK_GETTIME |
777 |
+ |
struct timespec t; |
778 |
+ |
clock_gettime(CLOCK_REALTIME, &t); |
779 |
+ |
return (uint64)t.tv_sec * 1000000 + t.tv_nsec / 1000; |
780 |
+ |
#else |
781 |
+ |
struct timeval t; |
782 |
+ |
gettimeofday(&t, NULL); |
783 |
+ |
return (uint64)t.tv_sec * 1000000 + t.tv_usec; |
784 |
+ |
#endif |
785 |
+ |
} |
786 |
+ |
|
787 |
+ |
|
788 |
+ |
/* |
789 |
+ |
* Delay by specified number of microseconds (<1 second) |
790 |
+ |
* (adapted from SDL_Delay() source) |
791 |
+ |
*/ |
792 |
+ |
|
793 |
+ |
void Delay_usec(uint32 usec) |
794 |
+ |
{ |
795 |
+ |
int was_error; |
796 |
+ |
#ifndef __linux__ // Non-Linux implementations need to calculate time left |
797 |
+ |
uint64 then, now, elapsed; |
798 |
+ |
#endif |
799 |
+ |
struct timeval tv; |
800 |
+ |
|
801 |
+ |
// Set the timeout interval - Linux only needs to do this once |
802 |
+ |
#ifdef __linux__ |
803 |
+ |
tv.tv_sec = 0; |
804 |
+ |
tv.tv_usec = usec; |
805 |
+ |
#else |
806 |
+ |
then = GetTicks_usec(); |
807 |
+ |
#endif |
808 |
+ |
do { |
809 |
+ |
errno = 0; |
810 |
+ |
#ifndef __linux__ |
811 |
+ |
/* Calculate the time interval left (in case of interrupt) */ |
812 |
+ |
now = GetTicks_usec(); |
813 |
+ |
elapsed = now - then; |
814 |
+ |
then = now; |
815 |
+ |
if (elapsed >= usec) |
816 |
+ |
break; |
817 |
+ |
usec -= elapsed; |
818 |
+ |
tv.tv_sec = 0; |
819 |
+ |
tv.tv_usec = usec; |
820 |
+ |
#endif |
821 |
+ |
was_error = select(0, NULL, NULL, NULL, &tv); |
822 |
+ |
} while (was_error && (errno == EINTR)); |
823 |
+ |
} |
824 |
+ |
|
825 |
+ |
|
826 |
|
#if !EMULATED_68K |
827 |
|
/* |
828 |
|
* Virtual 68k interrupt handler |