Sync memory changes to uae_cpu_2021

This commit is contained in:
Seg 2021-06-20 03:18:02 -07:00
parent 8959de5899
commit 42ed1c915a
5 changed files with 210 additions and 150 deletions

View File

@ -31,29 +31,39 @@
#include "readcpu.h"
#include "newcpu.h"
#include "compiler/compemu.h"
#include "vm_alloc.h"
#include "user_strings.h"
// RAM and ROM pointers
uint32 RAMBaseMac = 0; // RAM base (Mac address space) gb-- initializer is important
uint8 *RAMBaseHost; // RAM base (host address space)
uint32 RAMSize; // Size of RAM
uint32 ROMBaseMac; // ROM base (Mac address space)
uint8 *ROMBaseHost; // ROM base (host address space)
uint32 ROMSize; // Size of ROM
#if !REAL_ADDRESSING
// Mac frame buffer
uint8 *MacFrameBaseHost; // Frame buffer base (host address space)
uint32 MacFrameSize; // Size of frame buffer
int MacFrameLayout; // Frame buffer layout
#endif
#include "debug.h"
#if DIRECT_ADDRESSING
uintptr MEMBaseDiff; // Global offset between a Mac address and its Host equivalent
size_t MEMBaseDiff = 0; // Global offset between a Mac address and its Host equivalent
#endif
#if USE_JIT
bool UseJIT = false;
// RAM and ROM pointers
uint8* RAMBaseHost = 0; // RAM base (host address space)
uint8* ROMBaseHost = 0; // ROM base (host address space)
const uint32 RAMBaseMac = 0; // RAM base (Mac address space)
uint32 ROMBaseMac = 0; // ROM base (Mac address space)
uint32 RAMSize = 0; // Size of RAM
uint32 ROMSize = 0; // Size of ROM
// Mac frame buffer
uint8* MacFrameBaseHost = 0; // Frame buffer base (host address space)
uint32 MacFrameSize = 0; // Size of current frame buffer
int MacFrameLayout = 0; // Frame buffer layout
uint32 VRAMSize = 0; // Size of VRAM
uint32 JITCacheSize=0;
const char ROM_FILE_NAME[] = "ROM";
const int MAX_ROM_SIZE = 1024*1024; // 1mb
#if USE_SCRATCHMEM_SUBTERFUGE
uint8* ScratchMem = NULL; // Scratch memory for Mac ROM writes
int ScratchMemSize = 64*1024; // 64k
#else
int ScratchMemSize = 0;
#endif
// #if defined(ENABLE_EXCLUSIVE_SPCFLAGS) && !defined(HAVE_HARDWARE_LOCKS)
@ -63,26 +73,120 @@ B2_mutex *spcflags_lock = NULL;
// From newcpu.cpp
extern int quit_program;
// Create our virtual Macintosh memory map and load ROM
bool InitMacMem(void){
assert(RAMBaseHost==0); // don't call us twice
/*
* Initialize 680x0 emulation, CheckROM() must have been called first
*/
// Read RAM size
RAMSize = PrefsFindInt32("ramsize");
if (RAMSize <= 1000) {
RAMSize *= 1024 * 1024;
}
RAMSize &= 0xfff00000; // Round down to 1MB boundary
if (RAMSize < 1024*1024) {
WarningAlert(GetString(STR_SMALL_RAM_WARN));
RAMSize = 1024*1024;
}
if (RAMSize > 1023*1024*1024) // Cap to 1023MB (APD crashes at 1GB)
RAMSize = 1023*1024*1024;
bool Init680x0(void)
{
spcflags_lock = B2_create_mutex();
#if REAL_ADDRESSING
// Mac address space = host address space
RAMBaseMac = (uintptr)RAMBaseHost;
ROMBaseMac = (uintptr)ROMBaseHost;
#elif DIRECT_ADDRESSING
VRAMSize = 16*1024*1024; // 16mb, more than enough for 1920x1440x32
#if USE_JIT
JITCacheSize = compiler_get_jit_cache_size();
#endif
// Initialize VM system
vm_init();
// Create our virtual Macintosh memory map
RAMBaseHost = (uint8*)vm_acquire(
RAMSize + ScratchMemSize + MAX_ROM_SIZE + VRAMSize + JITCacheSize,
#if USE_JIT
// FIXME: JIT is not 64bit clean
((JITCacheSize>0)?VM_MAP_32BIT:0)|
#endif
VM_MAP_DEFAULT);
if (RAMBaseHost == VM_MAP_FAILED) {
ErrorAlert(STR_NO_MEM_ERR);
return false;
}
ROMBaseHost = RAMBaseHost + RAMSize + ScratchMemSize;
MacFrameBaseHost = ROMBaseHost + MAX_ROM_SIZE;
#if USE_SCRATCHMEM_SUBTERFUGE
// points to middle of scratch memory
ScratchMem = RAMBaseHost + RAMSize + ScratchMemSize/2;
#endif
// Get rom file path from preferences
const char *rom_path = PrefsFindString("rom");
// Load Mac ROM
#ifdef WIN32
HANDLE rom_fh = CreateFile(
rom_path ? rom_path : ROM_FILE_NAME,
GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (rom_fh == INVALID_HANDLE_VALUE) {
ErrorAlert(STR_NO_ROM_FILE_ERR);
return false;
}
#else
int rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME, O_RDONLY);
if (rom_fd < 0) {
ErrorAlert(STR_NO_ROM_FILE_ERR);
return false;
}
#endif
printf("%s", GetString(STR_READING_ROM_FILE));
#ifdef WIN32
ROMSize = GetFileSize(rom_fh, NULL);
#else
ROMSize = lseek(rom_fd, 0, SEEK_END);
#endif
switch(ROMSize){
case 64*1024:
case 128*1024:
case 256*1024:
case 512*1024:
case MAX_ROM_SIZE:
break;
default:
ErrorAlert(STR_ROM_SIZE_ERR);
#ifdef WIN32
CloseHandle(rom_fh);
#else
close(rom_fd);
#endif
return false;
}
#ifdef WIN32
DWORD bytes_read;
if (ReadFile(rom_fh, ROMBaseHost, ROMSize, &bytes_read, NULL) == 0 || bytes_read != ROMSize) {
#else
lseek(rom_fd, 0, SEEK_SET);
if (read(rom_fd, ROMBaseHost, ROMSize) != ROMSize) {
#endif
ErrorAlert(STR_ROM_FILE_READ_ERR);
#ifdef WIN32
CloseHandle(rom_fh);
#else
close(rom_fd);
#endif
return false;
}
if (!CheckROM()) {
ErrorAlert(STR_UNSUPPORTED_ROM_TYPE_ERR);
return false;
}
#if DIRECT_ADDRESSING
// Mac address space = host address space minus constant offset (MEMBaseDiff)
// NOTE: MEMBaseDiff is set up in main_unix.cpp/main()
RAMBaseMac = 0;
ROMBaseMac = Host2MacAddr(ROMBaseHost);
#else
// Initialize UAE memory banks
RAMBaseMac = 0;
switch (ROMVersion) {
case ROM_VERSION_64K:
case ROM_VERSION_PLUS:
@ -100,58 +204,62 @@ bool Init680x0(void)
}
memory_init();
#endif
D(bug("Mac RAM starts at %p (%08x)\n", RAMBaseHost, RAMBaseMac));
D(bug("Mac ROM starts at %p (%08x)\n", ROMBaseHost, ROMBaseMac));
init_m68k();
#if USE_JIT
UseJIT = compiler_use_jit();
if (UseJIT)
compiler_init();
#endif
return true;
}
void MacMemExit(void){
if(RAMBaseHost){
vm_release(RAMBaseHost,
RAMSize + ScratchMemSize + MAX_ROM_SIZE + VRAMSize + JITCacheSize);
}
RAMBaseHost = NULL;
ROMBaseHost = NULL;
//Exit VM wrappers
vm_exit();
}
/*
* Initialize 680x0 emulation, CheckROM() must have been called first
*/
bool Init680x0(void){
init_m68k();
#if USE_JIT
if(JITCacheSize>0)
compiler_init(MacFrameBaseHost + VRAMSize, JITCacheSize); // put JIT cache after VRAM
#endif
return true;
}
/*
* Deinitialize 680x0 emulation
*/
void Exit680x0(void)
{
void Exit680x0(void){
#if USE_JIT
if (UseJIT)
compiler_exit();
if(JITCacheSize>0)
compiler_exit();
#endif
exit_m68k();
}
/*
* Initialize memory mapping of frame buffer (called upon video mode change)
*/
void InitFrameBufferMapping(void)
{
#if !REAL_ADDRESSING && !DIRECT_ADDRESSING
memory_init();
#endif
}
/*
* Reset and start 680x0 emulation (doesn't return)
*/
void Start680x0(void)
{
void Start680x0(void){
m68k_reset();
#if USE_JIT
if (UseJIT)
m68k_compile_execute();
else
if (JITCacheSize>0)
m68k_compile_execute();
else
#endif
m68k_execute();
}
/*
* Trigger interrupt
*/
@ -283,3 +391,19 @@ void report_double_bus_error()
CPU_ACTION;
#endif
}
#if USE_JIT
extern void flush_icache_range(uint8 *start, uint32 size); // from compemu_support.cpp
#endif
/*
* Code was patched, flush caches if neccessary (i.e. when using a real 680x0
* or a dynamically recompiling emulator)
*/
void FlushCodeCache(void *start, uint32 size){
#if USE_JIT
if (JITCacheSize>0)
flush_icache_range((uint8 *)start, size);
#endif
}

View File

@ -164,9 +164,9 @@ union cacheline {
/* Functions exposed to newcpu, or to what was moved from newcpu.c to
* compemu_support.c */
#ifdef WINUAE_ARANYM
extern void compiler_init(void);
extern void compiler_init(void*,int);
extern void compiler_exit(void);
extern bool compiler_use_jit(void);
extern uint32 compiler_get_jit_cache_size(void);
#endif
extern void flush(int save_regs);
void flush_reg(int reg);

View File

@ -2899,11 +2899,13 @@ static inline const char *str_on_off(bool b)
#ifdef UAE
static
#endif
void compiler_init(void)
{
void compiler_init(void* buf, int JITCacheSize){
static bool initialized = false;
if (initialized)
return;
compiled_code=(uint8*)buf;
cache_size=JITCacheSize;
#ifdef UAE
#else
@ -2922,10 +2924,6 @@ void compiler_init(void)
#endif
jit_log("<JIT compiler> : compile FPU instructions : %s", !avoid_fpu ? "yes" : "no");
// Get size of the translation cache (in KB)
cache_size = PrefsFindInt32("jitcachesize");
jit_log("<JIT compiler> : requested translation cache size : %d KB", cache_size);
setzflg_uses_bsf = target_check_bsf();
jit_log("<JIT compiler> : target processor has CMOV instructions : %s", have_cmov ? "yes" : "no");
jit_log("<JIT compiler> : target processor can suffer from partial register stalls : %s", have_rat_stall ? "yes" : "no");
@ -2970,8 +2968,7 @@ void compiler_init(void)
#ifdef UAE
static
#endif
void compiler_exit(void)
{
void compiler_exit(void){
#ifdef PROFILE_COMPILE_TIME
emul_end_time = clock();
#endif
@ -2983,18 +2980,6 @@ void compiler_exit(void)
jit_log("data_wasted = %ld bytes", data_wasted);
#endif
#endif
// Deallocate translation cache
if (compiled_code) {
vm_release(compiled_code, cache_size * 1024);
compiled_code = 0;
}
// Deallocate popallspace
if (popallspace) {
vm_release(popallspace, POPALLSPACE_SIZE);
popallspace = 0;
}
#endif
#ifdef PROFILE_COMPILE_TIME
@ -3051,19 +3036,21 @@ void compiler_exit(void)
#ifdef UAE
#else
bool compiler_use_jit(void)
{
// Return bytes needed for JIT cache
uint32 compiler_get_jit_cache_size(void){
// Check for the "jit" prefs item
if (!PrefsFindBool("jit"))
return false;
return 0;
// Don't use JIT if translation cache size is less then MIN_CACHE_SIZE KB
if (PrefsFindInt32("jitcachesize") < MIN_CACHE_SIZE) {
uint32 JITCacheSize=PrefsFindInt32("jitcachesize");
if (JITCacheSize < MIN_CACHE_SIZE) {
write_log("<JIT compiler> : translation cache size is less than %d KB. Disabling JIT.\n", MIN_CACHE_SIZE);
return false;
return 0;
}
return true;
write_log("<JIT compiler> : translation cache size : %d KB\n", JITCacheSize);
return (1024*JITCacheSize) + POPALLSPACE_SIZE;
}
#endif
@ -3669,10 +3656,6 @@ void calc_disp_ea_020(int base, uae_u32 dp, int target, int tmp)
forget_about(tmp);
}
void set_cache_state(int enabled)
{
if (enabled!=cache_enabled)
@ -3692,45 +3675,13 @@ uae_u32 get_jitted_size(void)
return 0;
}
static uint8 *do_alloc_code(uint32 size, int depth)
{
UNUSED(depth);
uint8 *code = (uint8 *)vm_acquire(size, VM_MAP_DEFAULT | VM_MAP_32BIT);
return code == VM_MAP_FAILED ? NULL : code;
}
void alloc_cache(void){
assert(compiled_code);
assert(cache_size);
static inline uint8 *alloc_code(uint32 size)
{
uint8 *ptr = do_alloc_code(size, 0);
/* allocated code must fit in 32-bit boundaries */
assert((uintptr)ptr <= 0xffffffff);
return ptr;
}
void alloc_cache(void)
{
if (compiled_code) {
flush_icache_hard();
vm_release(compiled_code, cache_size * 1024);
compiled_code = 0;
}
#ifdef UAE
cache_size = currprefs.cachesize;
#endif
if (cache_size == 0)
return;
while (!compiled_code && cache_size) {
if ((compiled_code = alloc_code(cache_size * 1024)) == NULL) {
compiled_code = 0;
cache_size /= 2;
}
}
vm_protect(compiled_code, cache_size * 1024, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE);
if (compiled_code) {
jit_log("<JIT compiler> : actual translation cache size : %d KB at %p-%p", cache_size, compiled_code, compiled_code + cache_size*1024);
#ifdef USE_DATA_BUFFER
max_compile_start = compiled_code + cache_size*1024 - BYTES_PER_INST - DATA_BUFFER_SIZE;
#else
@ -3979,27 +3930,9 @@ static inline void match_states(blockinfo* bi)
}
}
static inline void create_popalls(void)
{
static inline void create_popalls(void){
int i,r;
if (popallspace == NULL) {
if ((popallspace = alloc_code(POPALLSPACE_SIZE)) == NULL) {
jit_log("WARNING: Could not allocate popallspace!");
#ifdef UAE
if (currprefs.cachesize > 0)
#endif
{
jit_abort("Could not allocate popallspace!");
}
#ifdef UAE
/* This is not fatal if JIT is not used. If JIT is
* turned on, it will crash, but it would have crashed
* anyway. */
return;
#endif
}
}
vm_protect(popallspace, POPALLSPACE_SIZE, VM_PAGE_READ | VM_PAGE_WRITE);
int stack_space = STACK_OFFSET;
@ -4487,8 +4420,8 @@ void build_comp(void)
jit_log("<JIT compiler> : supposedly %d compileable opcodes!",count);
/* Initialise state */
create_popalls();
alloc_cache();
create_popalls();
reset_lists();
for (i=0;i<TAGSIZE;i+=2) {

View File

@ -51,6 +51,7 @@ extern uint32 ROMBaseMac; // ROM base (Mac address space)
#endif
extern uint8 *ROMBaseHost; // ROM base (host address space)
extern uint32 ROMSize; // Size of ROM
extern uint32 VRAMSize; // Size of VRAM
#if 0
extern uint32 RealROMSize; // Real size of ROM
extern memptr HWBase; // HW base (Atari address space)
@ -203,6 +204,8 @@ extern int exit_val;
#if 0
extern bool InitMEM();
#endif
extern bool InitMacMem(void);
extern void MacMemExit(void);
extern bool Init680x0(void);
#if 0
extern void Reset680x0(void);

View File

@ -24,7 +24,7 @@
#define UAE_MEMORY_H
#if DIRECT_ADDRESSING
extern uintptr MEMBaseDiff;
extern size_t MEMBaseDiff;
#endif
extern void Exception (int, uaecptr);
@ -62,7 +62,7 @@ static __inline__ uae_u8 *do_get_real_address(uaecptr addr)
}
static __inline__ uae_u32 do_get_virtual_address(uae_u8 *addr)
{
return (uintptr)addr - MEMBaseDiff;
return (size_t)addr - MEMBaseDiff;
}
static __inline__ uae_u32 get_long(uaecptr addr)
{