- added USE_SCRATCHMEM_SUBTERFUGE

- added memory allocation in real and direct addressing modes through mmap()
- added the possibility to allocate the whole mac memory from zero
This commit is contained in:
gbeauche 2000-09-22 17:14:28 +00:00
parent da8b5373b0
commit cbe6fea745

View File

@ -30,7 +30,7 @@
# include <pthread.h> # include <pthread.h>
#endif #endif
#if defined(USE_MAPPED_MEMORY) || REAL_ADDRESSING #if defined(USE_MAPPED_MEMORY) || REAL_ADDRESSING || DIRECT_ADDRESSING
# include <sys/mman.h> # include <sys/mman.h>
#endif #endif
@ -133,7 +133,11 @@ static struct sigaction sigirq_sa; // Virtual 68k interrupt signal
static struct sigaction sigill_sa; // Illegal instruction static struct sigaction sigill_sa; // Illegal instruction
static void *sig_stack = NULL; // Stack for signal handlers static void *sig_stack = NULL; // Stack for signal handlers
uint16 EmulatedSR; // Emulated bits of SR (supervisor bit and interrupt mask) uint16 EmulatedSR; // Emulated bits of SR (supervisor bit and interrupt mask)
uint32 ScratchMem = NULL; // Scratch memory for Mac ROM writes uint8 *ScratchMem = NULL; // Scratch memory for Mac ROM writes
#endif
#if USE_SCRATCHMEM_SUBTERFUGE
uint8 *ScratchMem = 0; // Scratch memory for Mac ROM writes
#endif #endif
static struct sigaction timer_sa; // sigaction used for timer static struct sigaction timer_sa; // sigaction used for timer
@ -150,6 +154,7 @@ static void sigint_handler(...);
#if REAL_ADDRESSING #if REAL_ADDRESSING
static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped
static bool memory_mapped_from_zero = false; // Flag: Could allocate RAM area from 0
#endif #endif
#ifdef USE_MAPPED_MEMORY #ifdef USE_MAPPED_MEMORY
@ -260,19 +265,42 @@ int main(int argc, char **argv)
RAMSize = 1024*1024; RAMSize = 1024*1024;
} }
#if REAL_ADDRESSING || DIRECT_ADDRESSING
const uint32 page_size = getpagesize();
const uint32 page_mask = page_size - 1;
const uint32 aligned_ram_size = (RAMSize + page_mask) & ~page_mask;
const uint32 ram_rom_size = aligned_ram_size + 0x100000;
#endif
#if REAL_ADDRESSING #if REAL_ADDRESSING
// Try to allocate the complete address space from zero
// gb-- the Solaris manpage about mmap(2) states that using MAP_FIXED
// implies undefined behaviour for further use of sbrk(), malloc(), etc.
#if defined(OS_solaris)
// Anyway, it doesn't work...
if (0) {
#else
if (mmap((caddr_t)0x0000, ram_rom_size, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0) != MAP_FAILED) {
#endif
D(bug("Could allocate RAM and ROM from 0x0000\n"));
memory_mapped_from_zero = true;
}
// Create Low Memory area (0x0000..0x2000) // Create Low Memory area (0x0000..0x2000)
if (mmap((char *)0x0000, 0x2000, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0) == (void *)-1) { else if (mmap((char *)0x0000, 0x2000, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0) != MAP_FAILED) {
D(bug("Could allocate the Low Memory globals\n"));
lm_area_mapped = true;
}
// Exit on error
else {
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno));
ErrorAlert(str); ErrorAlert(str);
QuitEmulator(); QuitEmulator();
} }
lm_area_mapped = true;
#endif #endif
#if !EMULATED_68K #if !EMULATED_68K || USE_SCRATCHMEM_SUBTERFUGE
// Allocate scratch memory // Allocate scratch memory
ScratchMem = (uint32)malloc(SCRATCH_MEM_SIZE); ScratchMem = (uint8 *)malloc(SCRATCH_MEM_SIZE);
if (ScratchMem == NULL) { if (ScratchMem == NULL) {
ErrorAlert(GetString(STR_NO_MEM_ERR)); ErrorAlert(GetString(STR_NO_MEM_ERR));
QuitEmulator(); QuitEmulator();
@ -300,15 +328,40 @@ int main(int argc, char **argv)
mmap(good_address_map + i, 4096, PROT_READ, MAP_FIXED | MAP_PRIVATE, good_address_fd, 0); mmap(good_address_map + i, 4096, PROT_READ, MAP_FIXED | MAP_PRIVATE, good_address_fd, 0);
for (int i=0; i<0x80000; i+=4096) for (int i=0; i<0x80000; i+=4096)
mmap(good_address_map + i + 0x00400000, 4096, PROT_READ, MAP_FIXED | MAP_PRIVATE, good_address_fd, 0); mmap(good_address_map + i + 0x00400000, 4096, PROT_READ, MAP_FIXED | MAP_PRIVATE, good_address_fd, 0);
#elif REAL_ADDRESSING || DIRECT_ADDRESSING
// gb-- Overkill, needs to be cleaned up. Probably explode it for either
// real or direct addressing mode.
#if REAL_ADDRESSING
if (memory_mapped_from_zero) {
RAMBaseHost = (uint8 *)0;
ROMBaseHost = RAMBaseHost + aligned_ram_size;
}
else
#endif
{
RAMBaseHost = (uint8 *)mmap(0, ram_rom_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, zero_fd, 0);
if (RAMBaseHost == (uint8 *)MAP_FAILED) {
ErrorAlert(GetString(STR_NO_MEM_ERR));
QuitEmulator();
}
ROMBaseHost = RAMBaseHost + aligned_ram_size;
}
#else #else
RAMBaseHost = (uint8 *)malloc(RAMSize); RAMBaseHost = (uint8 *)malloc(RAMSize);
ROMBaseHost = (uint8 *)malloc(0x100000); ROMBaseHost = (uint8 *)malloc(0x100000);
#endif
if (RAMBaseHost == NULL || ROMBaseHost == NULL) { if (RAMBaseHost == NULL || ROMBaseHost == NULL) {
ErrorAlert(GetString(STR_NO_MEM_ERR)); ErrorAlert(GetString(STR_NO_MEM_ERR));
QuitEmulator(); QuitEmulator();
} }
#if REAL_ADDRESSING && !EMULATED_68K #endif
#if DIRECT_ADDRESSING
// Initialize MEMBaseDiff now so that Host2MacAddr in the Video module
// will return correct results
RAMBaseMac = 0;
ROMBaseMac = RAMBaseMac + aligned_ram_size;
InitMEMBaseDiff(RAMBaseHost, RAMBaseMac);
#endif
#if REAL_ADDRESSING // && !EMULATED_68K
RAMBaseMac = (uint32)RAMBaseHost; RAMBaseMac = (uint32)RAMBaseHost;
ROMBaseMac = (uint32)ROMBaseHost; ROMBaseMac = (uint32)ROMBaseHost;
#endif #endif
@ -564,6 +617,20 @@ void QuitEmulator(void)
// Deinitialize everything // Deinitialize everything
ExitAll(); ExitAll();
#if REAL_ADDRESSING || DIRECT_ADDRESSING
// Unmap ROM area
if (ROMBaseHost != (uint8 *)MAP_FAILED) {
munmap((caddr_t)ROMBaseHost, 0x100000);
ROMBaseHost = 0;
}
//Unmap RAM area
if (RAMBaseHost != (uint8 *)MAP_FAILED) {
const uint32 page_size = getpagesize();
const uint32 page_mask = page_size - 1;
munmap((caddr_t)RAMBaseHost, ((RAMSize + page_mask) & ~page_mask));
}
#else
// Delete ROM area // Delete ROM area
if (ROMBaseHost) { if (ROMBaseHost) {
free(ROMBaseHost); free(ROMBaseHost);
@ -575,8 +642,9 @@ void QuitEmulator(void)
free(RAMBaseHost); free(RAMBaseHost);
RAMBaseHost = NULL; RAMBaseHost = NULL;
} }
#endif
#if !EMULATED_68K #if !EMULATED_68K || USE_SCRATMEM_SUBTERFUGE
// Delete scratch memory area // Delete scratch memory area
if (ScratchMem) { if (ScratchMem) {
free((void *)(ScratchMem - SCRATCH_MEM_SIZE/2)); free((void *)(ScratchMem - SCRATCH_MEM_SIZE/2));