--- uae/src/newcpu.c 2018/04/24 17:09:07 1.1.1.19 +++ uae/src/newcpu.c 2018/04/24 17:14:19 1.1.1.22 @@ -19,17 +19,20 @@ #include "autoconf.h" #include "ersatz.h" #include "debug.h" -#include "compiler.h" #include "gui.h" #include "savestate.h" #include "blitter.h" /* Opcode of faulting instruction */ -uae_u16 last_op_for_exception_3; +static uae_u16 last_op_for_exception_3; /* PC at fault time */ -uaecptr last_addr_for_exception_3; +static uaecptr last_addr_for_exception_3; /* Address that generated the exception */ -uaecptr last_fault_for_exception_3; +static uaecptr last_fault_for_exception_3; +/* read (0) or write (1) access */ +static int last_writeaccess_for_exception_3; +/* instruction (1) or data (0) access */ +static int last_instructionaccess_for_exception_3; int areg_byteinc[] = { 1,1,1,1,1,1,1,2 }; int imm8_table[] = { 8,1,2,3,4,5,6,7 }; @@ -114,11 +117,10 @@ static void build_cpufunctbl (void) : currprefs.cpu_level == 3 ? op_smalltbl_1_ff : currprefs.cpu_level == 2 ? op_smalltbl_2_ff : currprefs.cpu_level == 1 ? op_smalltbl_3_ff - : ! currprefs.cpu_compatible ? op_smalltbl_4_ff : op_smalltbl_5_ff); - write_log ("Building CPU function table (%d %d %d).\n", - currprefs.cpu_level, currprefs.cpu_compatible, currprefs.address_space_24); + write_log ("Building CPU function table (%d %d).\n", + currprefs.cpu_level, currprefs.address_space_24); for (opcode = 0; opcode < 65536; opcode++) cpufunctbl[opcode] = op_illg_1; @@ -145,6 +147,21 @@ static void build_cpufunctbl (void) } } +void fill_prefetch_slow (void) +{ +#ifdef CPUEMU_6 + if (currprefs.cpu_cycle_exact) { + regs.ir = get_word_ce (m68k_getpc ()); + regs.irc = get_word_ce (m68k_getpc () + 2); + } else { +#endif + regs.ir = get_word (m68k_getpc ()); + regs.irc = get_word (m68k_getpc () + 2); +#ifdef CPUEMU_6 + } +#endif +} + unsigned long cycles_mask, cycles_val; static void update_68k_cycles (void) @@ -159,12 +176,6 @@ static void update_68k_cycles (void) void check_prefs_changed_cpu (void) { - if (currprefs.cpu_level != changed_prefs.cpu_level - || currprefs.cpu_compatible != changed_prefs.cpu_compatible) { - currprefs.cpu_level = changed_prefs.cpu_level; - currprefs.cpu_compatible = changed_prefs.cpu_compatible; - build_cpufunctbl (); - } if (currprefs.m68k_speed != changed_prefs.m68k_speed) { currprefs.m68k_speed = changed_prefs.m68k_speed; reset_frame_rate_hack (); @@ -213,30 +224,33 @@ void init_m68k (void) } #endif write_log ("Building CPU table for configuration: 68"); + regs.address_space_mask = 0xffffffff; if (currprefs.address_space_24 && currprefs.cpu_level > 1) - write_log ("EC"); + write_log ("EC"); switch (currprefs.cpu_level) { case 1: - write_log ("010"); - break; + write_log ("010"); + break; case 2: - write_log ("020"); - break; + write_log ("020"); + break; case 3: - write_log ("020/881"); - break; + write_log ("020/881"); + break; case 4: - /* Who is going to miss the MMU anyway...? :-) */ - write_log ("040"); - break; + /* Who is going to miss the MMU anyway...? :-) */ + write_log ("040"); + break; default: - write_log ("000"); - break; + write_log ("000"); + break; + } + if (currprefs.address_space_24) { + regs.address_space_mask = 0x00ffffff; + write_log (" 24-bit addressing"); } - if (currprefs.cpu_compatible) - write_log (" (compatible mode)"); write_log ("\n"); - + read_table68k (); do_merges (); @@ -550,6 +564,8 @@ static int verify_ea (int reg, amodes mo last_addr_for_exception_3 = m68k_getpc () + m68kpc_offset; last_fault_for_exception_3 = addr; + last_writeaccess_for_exception_3 = 0; + last_instructionaccess_for_exception_3 = 0; return 0; } @@ -676,16 +692,39 @@ void MakeFromSR (void) if (regs.t1 || regs.t0) set_special (SPCFLAG_TRACE); else - /* Keep SPCFLAG_DOTRACE, we still want a trace exception for + /* Keep SPCFLAG_DOTRACE, we still want a trace exception for SR-modifying instructions (including STOP). */ unset_special (SPCFLAG_TRACE); } -void Exception(int nr, uaecptr oldpc) +static void exception_trace (int nr) +{ + unset_special (SPCFLAG_TRACE | SPCFLAG_DOTRACE); + if (regs.t1 && !regs.t0) { + /* trace stays pending if exception is div by zero, chk, + * trapv or trap #x + */ + if (nr == 5 || nr == 6 || nr == 7 || (nr >= 32 && nr <= 47)) + set_special (SPCFLAG_DOTRACE); + } + regs.t1 = regs.t0 = regs.m = 0; +} + +static void exception_debug (int nr) +{ +#ifdef DEBUGGER + if (!exception_debugging) + return; + console_out ("Exception %d, PC=%08.8X\n", nr, m68k_getpc()); +#endif +} + +void Exception_normal (int nr, uaecptr oldpc) { - uae_u32 currpc = m68k_getpc (); + uae_u32 currpc = m68k_getpc (), newpc; + int sv = regs.s; - compiler_flush_jsr_stack(); + exception_debug (nr); MakeSR(); if (!regs.s) { @@ -699,19 +738,63 @@ void Exception(int nr, uaecptr oldpc) if (currprefs.cpu_level > 0) { if (nr == 2 || nr == 3) { int i; - /* @@@ this is probably wrong (?) */ - for (i = 0 ; i < 12 ; i++) { + if (currprefs.cpu_level >= 4) { /* 68040 */ + if (nr == 2) { + for (i = 0 ; i < 18 ; i++) { + m68k_areg(regs, 7) -= 2; + put_word (m68k_areg(regs, 7), 0); + } + m68k_areg(regs, 7) -= 4; + put_long (m68k_areg(regs, 7), last_fault_for_exception_3); + m68k_areg(regs, 7) -= 2; + put_word (m68k_areg(regs, 7), 0); + m68k_areg(regs, 7) -= 2; + put_word (m68k_areg(regs, 7), 0); + m68k_areg(regs, 7) -= 2; + put_word (m68k_areg(regs, 7), 0); + m68k_areg(regs, 7) -= 2; + put_word (m68k_areg(regs, 7), 0x0140 | (sv ? 6 : 2)); /* SSW */ + m68k_areg(regs, 7) -= 4; + put_long (m68k_areg(regs, 7), last_addr_for_exception_3); + m68k_areg(regs, 7) -= 2; + put_word (m68k_areg(regs, 7), 0x7000 + nr * 4); + } else { + m68k_areg(regs, 7) -= 4; + put_long (m68k_areg(regs, 7), last_fault_for_exception_3); + m68k_areg(regs, 7) -= 2; + put_word (m68k_areg(regs, 7), 0x2000 + nr * 4); + } + } else { + uae_u16 ssw = (sv ? 4 : 0) | (last_instructionaccess_for_exception_3 ? 2 : 1); + ssw |= last_writeaccess_for_exception_3 ? 0 : 0x40; + ssw |= 0x20; + for (i = 0 ; i < 36; i++) { + m68k_areg(regs, 7) -= 2; + put_word (m68k_areg(regs, 7), 0); + } + m68k_areg(regs, 7) -= 4; + put_long (m68k_areg(regs, 7), last_fault_for_exception_3); + m68k_areg(regs, 7) -= 2; + put_word (m68k_areg(regs, 7), 0); + m68k_areg(regs, 7) -= 2; + put_word (m68k_areg(regs, 7), 0); m68k_areg(regs, 7) -= 2; put_word (m68k_areg(regs, 7), 0); + m68k_areg(regs, 7) -= 2; + put_word (m68k_areg(regs, 7), ssw); + m68k_areg(regs, 7) -= 2; + put_word (m68k_areg(regs, 7), 0xb000 + nr * 4); } - m68k_areg(regs, 7) -= 2; - put_word (m68k_areg(regs, 7), 0xa000 + nr * 4); +#if 0 + write_log ("Exception %d (%x) at %x -> %x!\n", nr, oldpc, currpc, get_long (regs.vbr + 4*nr)); +#endif + } else if (nr ==5 || nr == 6 || nr == 7 || nr == 9) { m68k_areg(regs, 7) -= 4; put_long (m68k_areg(regs, 7), oldpc); m68k_areg(regs, 7) -= 2; put_word (m68k_areg(regs, 7), 0x2000 + nr * 4); - } else if (regs.m && nr >= 24 && nr < 32) { + } else if (regs.m && nr >= 24 && nr < 32) { /* M + Interrupt */ m68k_areg(regs, 7) -= 2; put_word (m68k_areg(regs, 7), nr * 4); m68k_areg(regs, 7) -= 4; @@ -727,36 +810,50 @@ void Exception(int nr, uaecptr oldpc) m68k_areg(regs, 7) -= 2; put_word (m68k_areg(regs, 7), nr * 4); } - } else { - if (nr == 2 || nr == 3) { - m68k_areg(regs, 7) -= 12; - /* ??????? */ - if (nr == 3) { - put_long (m68k_areg(regs, 7), last_fault_for_exception_3); - put_word (m68k_areg(regs, 7)+4, last_op_for_exception_3); - put_long (m68k_areg(regs, 7)+8, last_addr_for_exception_3); - } - write_log ("Exception!\n"); - goto kludge_me_do; - } + } else if (nr == 2 || nr == 3) { + uae_u16 mode = (sv ? 4 : 0) | (last_instructionaccess_for_exception_3 ? 2 : 1); + mode |= last_writeaccess_for_exception_3 ? 0 : 16; + m68k_areg(regs, 7) -= 14; + /* fixme: bit3=I/N */ + put_word (m68k_areg(regs, 7) + 0, mode); + put_long (m68k_areg(regs, 7) + 2, last_fault_for_exception_3); + put_word (m68k_areg(regs, 7) + 6, last_op_for_exception_3); + put_word (m68k_areg(regs, 7) + 8, regs.sr); + put_long (m68k_areg(regs, 7) + 10, last_addr_for_exception_3); + write_log ("Exception %d (%x) at %x -> %x!\n", nr, oldpc, currpc, get_long (regs.vbr + 4*nr)); + goto kludge_me_do; } m68k_areg(regs, 7) -= 4; put_long (m68k_areg(regs, 7), currpc); -kludge_me_do: m68k_areg(regs, 7) -= 2; put_word (m68k_areg(regs, 7), regs.sr); - m68k_setpc (get_long (regs.vbr + 4*nr)); - fill_prefetch_0 (); - regs.t1 = regs.t0 = regs.m = 0; - unset_special (SPCFLAG_TRACE | SPCFLAG_DOTRACE); +kludge_me_do: + newpc = get_long (regs.vbr + 4 * nr); + if (newpc & 1) { + if (nr == 2 || nr == 3) + uae_reset (1); /* there is nothing else we can do.. */ + else + exception3 (regs.ir, m68k_getpc(), newpc); + return; + } + m68k_setpc (newpc); + fill_prefetch_slow (); + exception_trace (nr); +} + +void Exception (int nr, uaecptr oldpc) +{ + Exception_normal (nr, oldpc); } static void Interrupt (int nr) { + regs.stopped = 0; + unset_special (SPCFLAG_STOP); assert(nr < 8 && nr >= 0); lastint_regs = regs; lastint_no = nr; - Exception(nr+24, 0); + Exception (nr + 24, 0); regs.intmask = nr; set_special (SPCFLAG_INT); @@ -783,7 +880,7 @@ int m68k_move2c (int regno, uae_u32 *reg case 5: itt1 = *regp & 0xffffe364; break; case 6: dtt0 = *regp & 0xffffe364; break; case 7: dtt1 = *regp & 0xffffe364; break; - + case 0x800: regs.usp = *regp; break; case 0x801: regs.vbr = *regp; break; case 0x802: caar = *regp & 0xfc; break; @@ -1091,10 +1188,18 @@ static char* ccnames[] = void m68k_reset (void) { + if (currprefs.cpu_level != changed_prefs.cpu_level + || currprefs.address_space_24 != changed_prefs.address_space_24) + { + currprefs.address_space_24 = changed_prefs.address_space_24; + currprefs.cpu_level = changed_prefs.cpu_level; + build_cpufunctbl (); + } + regs.kick_mask = 0x00F80000; regs.spcflags = 0; if (savestate_state == STATE_RESTORE) { - m68k_setpc (regs.pc); + m68k_setpc (regs.pc); /* MakeFromSR() must not swap stack pointer */ regs.s = (regs.sr >> 13) & 1; MakeFromSR(); @@ -1108,8 +1213,6 @@ void m68k_reset (void) m68k_areg (regs, 7) = get_long (0x00f80000); m68k_setpc (get_long (0x00f80004)); - refill_prefetch (m68k_getpc (), 0); - fill_prefetch_0 (); regs.s = 1; regs.m = 0; regs.stopped = 0; @@ -1123,20 +1226,21 @@ void m68k_reset (void) regs.intmask = 7; regs.vbr = regs.sfc = regs.dfc = 0; regs.fpcr = regs.fpsr = regs.fpiar = 0; + regs.irc = 0xffff; + fill_prefetch_slow (); } unsigned long REGPARAM2 op_illg (uae_u32 opcode) { uaecptr pc = m68k_getpc (); - + if (cloanto_rom && (opcode & 0xF100) == 0x7100) { m68k_dreg (regs, (opcode >> 9) & 7) = (uae_s8)(opcode & 0xFF); m68k_incpc (2); - fill_prefetch_0 (); + fill_prefetch_slow (); return 4; } - compiler_flush_jsr_stack (); if (opcode == 0x4E7B && get_long (0x10) == 0 && (pc & 0xF80000) == 0xF80000) { write_log ("Your Kickstart requires a 68020 CPU. Giving up.\n"); broken_in = 1; @@ -1149,7 +1253,7 @@ unsigned long REGPARAM2 op_illg (uae_u32 uae_u16 arg = get_iword (2); m68k_incpc (4); ersatz_perform (arg); - fill_prefetch_0 (); + fill_prefetch_slow (); return 4; } else if ((pc & 0xFFFF0000) == RTAREA_BASE) { /* User-mode STOP replacement */ @@ -1162,7 +1266,7 @@ unsigned long REGPARAM2 op_illg (uae_u32 /* Calltrap. */ m68k_incpc(2); call_calltrap (opcode & 0xFFF); - fill_prefetch_0 (); + fill_prefetch_slow (); return 4; } @@ -1210,7 +1314,7 @@ static void do_trace (void) /* probably never used so why bother */ /* We can afford this to be inefficient... */ m68k_setpc (m68k_getpc ()); - fill_prefetch_0 (); + fill_prefetch_slow (); opcode = get_word (regs.pc); if (opcode == 0x4e72 /* RTE */ || opcode == 0x4e74 /* RTD */ @@ -1256,15 +1360,14 @@ static int do_specialties (int cycles) do_copper (); } - run_compiled_code(); - if (regs.spcflags & SPCFLAG_DOTRACE) { + if (regs.spcflags & SPCFLAG_DOTRACE) Exception (9,last_trace_ad); - } + while (regs.spcflags & SPCFLAG_STOP) { do_cycles (4 * CYCLE_UNIT); if (regs.spcflags & SPCFLAG_COPPER) do_copper (); - if (regs.spcflags & (SPCFLAG_INT | SPCFLAG_DOINT)){ + if (regs.spcflags & (SPCFLAG_INT | SPCFLAG_DOINT)) { int intr = intlev (); unset_special (SPCFLAG_INT | SPCFLAG_DOINT); if (intr != -1 && intr > regs.intmask) { @@ -1300,22 +1403,10 @@ static int do_specialties (int cycles) do it all for performance... :( */ static void m68k_run_1 (void) { -#ifdef DEBUG_PREFETCH - uae_u8 saved_bytes[20]; - uae_u16 *oldpcp; -#endif for (;;) { int cycles; - uae_u32 opcode = get_iword_prefetch (0); -#ifdef DEBUG_PREFETCH - if (get_ilong (0) != do_get_mem_long (®s.prefetch)) { - write_log ("Prefetch differs from memory.\n"); - debugging = 1; - return; - } - oldpcp = regs.pc_p; - memcpy (saved_bytes, regs.pc_p, 20); -#endif + uae_u32 opcode = regs.ir; + /* assert (!regs.stopped && !(regs.spcflags & SPCFLAG_STOP)); */ /* regs_backup[backup_pointer = (backup_pointer + 1) % 16] = regs;*/ #if COUNT_INSTRS == 2 @@ -1324,21 +1415,7 @@ static void m68k_run_1 (void) #elif COUNT_INSTRS == 1 instrcount[opcode]++; #endif -#if defined X86_ASSEMBLY - __asm__ __volatile__("\tcall *%%ebx" - : "=&a" (cycles) : "b" (cpufunctbl[opcode]), "0" (opcode) - : "%edx", "%ecx", - "%esi", "%edi", "%ebp", "memory", "cc"); -#else cycles = (*cpufunctbl[opcode])(opcode); -#endif -#ifdef DEBUG_PREFETCH - if (memcmp (saved_bytes, oldpcp, 20) != 0) { - write_log ("Self-modifying code detected.\n"); - set_special (SPCFLAG_BRK); - debugging = 1; - } -#endif /*n_insns++;*/ cycles &= cycles_mask; cycles |= cycles_val; @@ -1367,19 +1444,12 @@ static void m68k_run_2 (void) #elif COUNT_INSTRS == 1 instrcount[opcode]++; #endif -#if defined X86_ASSEMBLY - __asm__ __volatile__("\tcall *%%ebx" - : "=&a" (cycles) : "b" (cpufunctbl[opcode]), "0" (opcode) - : "%edx", "%ecx", - "%esi", "%edi", "%ebp", "memory", "cc"); -#else cycles = (*cpufunctbl[opcode])(opcode); -#endif /*n_insns++;*/ cycles &= cycles_mask; cycles |= cycles_val; - do_cycles (cycles); + do_cycles (cycles); if (regs.spcflags) { if (do_specialties (cycles)) return; @@ -1387,19 +1457,19 @@ static void m68k_run_2 (void) } } -#ifdef X86_ASSEMBLY -STATIC_INLINE void m68k_run1 (void (*func)(void)) -{ - /* Work around compiler bug: GCC doesn't push %ebp in m68k_run_1. */ - __asm__ __volatile__ ("pushl %%ebp\n\tcall *%0\n\tpopl %%ebp" - : : "r" (func) : "%eax", "%edx", "%ecx", "memory", "cc"); -} -#else #define m68k_run1(F) (F) () -#endif int in_m68k_go = 0; +static void exception2_handle (uaecptr addr, uaecptr fault) +{ + last_addr_for_exception_3 = addr; + last_fault_for_exception_3 = fault; + last_writeaccess_for_exception_3 = 0; + last_instructionaccess_for_exception_3 = 0; + Exception (2, addr); +} + void m68k_go (int may_quit) { if (in_m68k_go || !may_quit) { @@ -1427,14 +1497,31 @@ void m68k_go (int may_quit) customreset (); /* We may have been restoring state, but we're done now. */ savestate_restore_finish (); + fill_prefetch_slow (); handle_active_events (); if (regs.spcflags) do_specialties (0); + m68k_setpc (regs.pc); } if (debugging) debug (); - m68k_run1 (currprefs.cpu_compatible ? m68k_run_1 : m68k_run_2); + if (regs.panic) { + regs.panic = 0; + /* program jumped to non-existing memory and cpu was >= 68020 */ + get_real_address (regs.isp); /* stack in no one's land? -> reboot */ + if (regs.isp & 1) + regs.panic = 1; + if (!regs.panic) + exception2_handle (regs.panic_pc, regs.panic_addr); + if (regs.panic) { + /* system is very badly confused */ + write_log ("double bus error or corrupted stack, forcing reboot..\n"); + regs.panic = 0; + uae_reset (1); + } + } + m68k_run1 (currprefs.cpu_level == 0 ? m68k_run_1 : m68k_run_2); } in_m68k_go--; } @@ -1554,8 +1641,6 @@ void m68k_dumpstate (FILE *f, uaecptr *n (regs.fpsr & 0x4000000) != 0, (regs.fpsr & 0x2000000) != 0, (regs.fpsr & 0x1000000) != 0); - if (currprefs.cpu_compatible) - fprintf (f, "prefetch %08lx\n", (unsigned long)do_get_mem_long(®s.prefetch)); m68k_disasm (f, m68k_getpc (), nextpc, 1); if (nextpc) @@ -1596,11 +1681,8 @@ uae_u8 *restore_cpu (uae_u8 *src) for (i = 0; i < 15; i++) regs.regs[i] = restore_u32 (); regs.pc = restore_u32 (); - /* We don't actually use this - we deliberately set prefetch_pc to a - zero so that prefetch isn't used for the first insn after a state - restore. */ - regs.prefetch = restore_u32 (); - regs.prefetch_pc = regs.pc + 128; + regs.irc = restore_u16 (); + regs.ir = restore_u16 (); regs.usp = restore_u32 (); regs.isp = restore_u32 (); regs.sr = restore_u16 (); @@ -1628,18 +1710,22 @@ uae_u8 *restore_cpu (uae_u8 *src) static int cpumodel[] = { 68000, 68010, 68020, 68020 }; -uae_u8 *save_cpu (int *len) +uae_u8 *save_cpu (int *len, uae_u8 *dstptr) { uae_u8 *dstbak,*dst; int model,i; - dstbak = dst = malloc(4+4+15*4+4+4+4+4+2+4+4+4+4+4+4+4); + if (dstptr) + dstbak = dst = dstptr; + else + dstbak = dst = malloc(4+4+15*4+4+4+4+4+2+4+4+4+4+4+4+4); model = cpumodel[currprefs.cpu_level]; save_u32 (model); /* MODEL */ save_u32 (currprefs.address_space_24 ? 1 : 0); /* FLAGS */ for(i = 0;i < 15; i++) save_u32 (regs.regs[i]); /* D0-D7 A0-A6 */ save_u32 (m68k_getpc ()); /* PC */ - save_u32 (regs.prefetch); /* prefetch */ + save_u16 (regs.irc); /* prefetch */ + save_u16 (regs.ir); /* instruction prefetch */ MakeSR (); save_u32 (!regs.s ? regs.regs[15] : regs.usp); /* USP */ save_u32 (regs.s ? regs.regs[15] : regs.isp); /* ISP */ @@ -1658,3 +1744,56 @@ uae_u8 *save_cpu (int *len) *len = dst - dstbak; return dstbak; } + +static void exception3f (uae_u32 opcode, uaecptr addr, uaecptr fault, int writeaccess, int instructionaccess) +{ + last_addr_for_exception_3 = addr; + last_fault_for_exception_3 = fault; + last_op_for_exception_3 = opcode; + last_writeaccess_for_exception_3 = writeaccess; + last_instructionaccess_for_exception_3 = instructionaccess; + Exception (3, fault); +} + +void exception3 (uae_u32 opcode, uaecptr addr, uaecptr fault) +{ + exception3f (opcode, addr, fault, 0, 0); +} + +void exception3i (uae_u32 opcode, uaecptr addr, uaecptr fault) +{ + exception3f (opcode, addr, fault, 0, 1); +} + +void exception2 (uaecptr addr, uaecptr fault) +{ + write_log ("delayed exception2!\n"); + regs.panic_pc = m68k_getpc(); + regs.panic_addr = addr; + regs.panic = 2; + set_special (SPCFLAG_BRK); + m68k_setpc (0xf80000); + fill_prefetch_slow (); +} + +void cpureset (void) +{ + customreset (); +#if 0 + uae_u16 ins; + if (currprefs.cpu_level == 0 && (currprefs.cpu_compatible || currprefs.cpu_cycle_exact)) { + customreset (); + return; + } + ins = get_word (m68k_getpc() + 2); + if ((ins & ~7) == 0x4ed0) { + int reg = ins & 7; + uae_u32 addr = m68k_areg (regs, reg); + write_log ("reset/jmp (ax) combination emulated\n"); + customreset (); + if (addr < 0x80000) + addr += 0xf80000; + m68k_setpc (addr); + } +#endif +}