--- BasiliskII/src/uae_cpu/newcpu.cpp 2000/09/05 16:53:19 1.6 +++ BasiliskII/src/uae_cpu/newcpu.cpp 2002/09/17 16:05:39 1.16 @@ -22,10 +22,20 @@ extern int intlev(void); // From baisili #include "memory.h" #include "readcpu.h" #include "newcpu.h" -#include "compiler.h" +#include "compiler/compemu.h" +#include "fpu/fpu.h" + +#if defined(ENABLE_EXCLUSIVE_SPCFLAGS) && !defined(HAVE_HARDWARE_LOCKS) +B2_mutex *spcflags_lock = NULL; +#endif + +#if ENABLE_MON +#include "mon.h" +#include "mon_disass.h" +#endif int quit_program = 0; -int debugging = 0; +const int debugging = 0; struct flag_struct regflags; /* Opcode of faulting instruction */ @@ -42,12 +52,55 @@ int movem_index1[256]; int movem_index2[256]; int movem_next[256]; -int fpp_movem_index1[256]; -int fpp_movem_index2[256]; -int fpp_movem_next[256]; - cpuop_func *cpufunctbl[65536]; +#if FLIGHT_RECORDER +struct rec_step { + uae_u32 d[8]; + uae_u32 a[8]; + uae_u32 pc; +}; + +const int LOG_SIZE = 8192; +static rec_step log[LOG_SIZE]; +static int log_ptr = -1; // First time initialization + +static const char *log_filename(void) +{ + const char *name = getenv("M68K_LOG_FILE"); + return name ? name : "log.68k"; +} + +void m68k_record_step(uaecptr pc) +{ + for (int i = 0; i < 8; i++) { + log[log_ptr].d[i] = m68k_dreg(regs, i); + log[log_ptr].a[i] = m68k_areg(regs, i); + } + log[log_ptr].pc = pc; + log_ptr = (log_ptr + 1) % LOG_SIZE; +} + +static void dump_log(void) +{ + FILE *f = fopen(log_filename(), "w"); + if (f == NULL) + return; + for (int i = 0; i < LOG_SIZE; i++) { + int j = (i + log_ptr) % LOG_SIZE; + fprintf(f, "pc %08x\n", log[j].pc); + fprintf(f, "d0 %08x d1 %08x d2 %08x d3 %08x\n", log[j].d[0], log[j].d[1], log[j].d[2], log[j].d[3]); + fprintf(f, "d4 %08x d5 %08x d6 %08x d7 %08x\n", log[j].d[4], log[j].d[5], log[j].d[6], log[j].d[7]); + fprintf(f, "a0 %08x a1 %08x a2 %08x a3 %08x\n", log[j].a[0], log[j].a[1], log[j].a[2], log[j].a[3]); + fprintf(f, "a4 %08x a5 %08x a6 %08x a7 %08x\n", log[j].a[4], log[j].a[5], log[j].a[6], log[j].a[7]); +#if ENABLE_MON + disass_68k(f, log[j].pc); +#endif + } + fclose(f); +} +#endif + #define COUNT_INSTRS 0 #if COUNT_INSTRS @@ -111,11 +164,11 @@ static __inline__ unsigned int cft_map ( #endif } -static void REGPARAM2 op_illg_1 (uae_u32 opcode) REGPARAM; +cpuop_rettype REGPARAM2 op_illg_1 (uae_u32 opcode) REGPARAM; -static void REGPARAM2 op_illg_1 (uae_u32 opcode) +cpuop_rettype REGPARAM2 op_illg_1 (uae_u32 opcode) { - op_illg (cft_map (opcode)); + cpuop_return( op_illg (cft_map (opcode)) ); } static void build_cpufunctbl (void) @@ -134,11 +187,11 @@ static void build_cpufunctbl (void) cpu_level = 1; } struct cputbl *tbl = ( - cpu_level == 4 ? op_smalltbl_0 - : cpu_level == 3 ? op_smalltbl_1 - : cpu_level == 2 ? op_smalltbl_2 - : cpu_level == 1 ? op_smalltbl_3 - : op_smalltbl_4); + cpu_level == 4 ? op_smalltbl_0_ff + : cpu_level == 3 ? op_smalltbl_1_ff + : cpu_level == 2 ? op_smalltbl_2_ff + : cpu_level == 1 ? op_smalltbl_3_ff + : op_smalltbl_4_ff); for (opcode = 0; opcode < 65536; opcode++) cpufunctbl[cft_map (opcode)] = op_illg_1; @@ -178,15 +231,6 @@ void init_m68k (void) movem_index2[i] = 7-j; movem_next[i] = i & (~(1 << j)); } - for (i = 0 ; i < 256 ; i++) { - int j; - for (j = 7 ; j >= 0 ; j--) { - if (i & (1 << j)) break; - } - fpp_movem_index1[i] = 7-j; - fpp_movem_index2[i] = j; - fpp_movem_next[i] = i & (~(1 << j)); - } #if COUNT_INSTRS { FILE *f = fopen (icountfilename (), "r"); @@ -207,14 +251,19 @@ void init_m68k (void) do_merges (); build_cpufunctbl (); - - fpu_init (); - fpu_set_integral_fpu (CPUType == 4); + +#if defined(ENABLE_EXCLUSIVE_SPCFLAGS) && !defined(HAVE_HARDWARE_LOCKS) + spcflags_lock = B2_create_mutex(); +#endif + fpu_init(CPUType == 4); } void exit_m68k (void) { fpu_exit (); +#if defined(ENABLE_EXCLUSIVE_SPCFLAGS) && !defined(HAVE_HARDWARE_LOCKS) + B2_delete_mutex(spcflags_lock); +#endif } struct regstruct regs, lastint_regs; @@ -223,9 +272,15 @@ static int backup_pointer = 0; static long int m68kpc_offset; int lastint_no; +#if REAL_ADDRESSING || DIRECT_ADDRESSING +#define get_ibyte_1(o) get_byte(get_virtual_address(regs.pc_p) + (o) + 1) +#define get_iword_1(o) get_word(get_virtual_address(regs.pc_p) + (o)) +#define get_ilong_1(o) get_long(get_virtual_address(regs.pc_p) + (o)) +#else #define get_ibyte_1(o) get_byte(regs.pc + (regs.pc_p - regs.pc_oldp) + (o) + 1) #define get_iword_1(o) get_word(regs.pc + (regs.pc_p - regs.pc_oldp) + (o)) #define get_ilong_1(o) get_long(regs.pc + (regs.pc_p - regs.pc_oldp) + (o)) +#endif uae_s32 ShowEA (int reg, amodes mode, wordsizes size, char *buf) { @@ -644,16 +699,18 @@ void MakeFromSR (void) } } - regs.spcflags |= SPCFLAG_INT; + SPCFLAGS_SET( SPCFLAG_INT ); if (regs.t1 || regs.t0) - regs.spcflags |= SPCFLAG_TRACE; + SPCFLAGS_SET( SPCFLAG_TRACE ); else - regs.spcflags &= ~(SPCFLAG_TRACE | SPCFLAG_DOTRACE); + /* Keep SPCFLAG_DOTRACE, we still want a trace exception for + SR-modifying instructions (including STOP). */ + SPCFLAGS_CLEAR( SPCFLAG_TRACE ); } void Exception(int nr, uaecptr oldpc) { - compiler_flush_jsr_stack(); + uae_u32 currpc = m68k_getpc (); MakeSR(); if (!regs.s) { regs.usp = m68k_areg(regs, 7); @@ -682,7 +739,7 @@ void Exception(int nr, uaecptr oldpc) m68k_areg(regs, 7) -= 2; put_word (m68k_areg(regs, 7), nr * 4); m68k_areg(regs, 7) -= 4; - put_long (m68k_areg(regs, 7), m68k_getpc ()); + put_long (m68k_areg(regs, 7), currpc); m68k_areg(regs, 7) -= 2; put_word (m68k_areg(regs, 7), regs.sr); regs.sr |= (1 << 13); @@ -708,14 +765,15 @@ void Exception(int nr, uaecptr oldpc) } } m68k_areg(regs, 7) -= 4; - put_long (m68k_areg(regs, 7), m68k_getpc ()); + 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)); + SPCFLAGS_SET( SPCFLAG_JIT_END_COMPILE ); fill_prefetch_0 (); regs.t1 = regs.t0 = regs.m = 0; - regs.spcflags &= ~(SPCFLAG_TRACE | SPCFLAG_DOTRACE); + SPCFLAGS_CLEAR( SPCFLAG_TRACE | SPCFLAG_DOTRACE ); } static void Interrupt(int nr) @@ -726,20 +784,39 @@ static void Interrupt(int nr) Exception(nr+24, 0); regs.intmask = nr; - regs.spcflags |= SPCFLAG_INT; + SPCFLAGS_SET( SPCFLAG_INT ); } -static int caar, cacr, tc, itt0, itt1, dtt0, dtt1; +static int caar, cacr, tc, itt0, itt1, dtt0, dtt1, mmusr, urp, srp; -void m68k_move2c (int regno, uae_u32 *regp) +int m68k_move2c (int regno, uae_u32 *regp) { - if (CPUType == 1 && (regno & 0x7FF) > 1) + if ((CPUType == 1 && (regno & 0x7FF) > 1) + || (CPUType < 4 && (regno & 0x7FF) > 2) + || (CPUType == 4 && regno == 0x802)) + { op_illg (0x4E7B); - else + return 0; + } else { switch (regno) { case 0: regs.sfc = *regp & 7; break; case 1: regs.dfc = *regp & 7; break; - case 2: cacr = *regp & 0x3; break; /* ignore C and CE */ + case 2: + cacr = *regp & (CPUType < 4 ? 0x3 : 0x80008000); +#if USE_JIT + if (CPUType < 4) { + set_cache_state(cacr&1); + if (*regp & 0x08) + flush_icache(1); + } + else { + set_cache_state((cacr&0x8000) || 0); + // FIXME: The User Manual claims bit 3 of CACR is undefined + if (*regp & 0x08) + flush_icache(2); + } +#endif + break; case 3: tc = *regp & 0xc000; break; case 4: itt0 = *regp & 0xffffe364; break; case 5: itt1 = *regp & 0xffffe364; break; @@ -750,17 +827,26 @@ void m68k_move2c (int regno, uae_u32 *re case 0x802: caar = *regp &0xfc; break; case 0x803: regs.msp = *regp; if (regs.m == 1) m68k_areg(regs, 7) = regs.msp; break; case 0x804: regs.isp = *regp; if (regs.m == 0) m68k_areg(regs, 7) = regs.isp; break; + case 0x805: mmusr = *regp; break; + case 0x806: urp = *regp; break; + case 0x807: srp = *regp; break; default: op_illg (0x4E7B); - break; + return 0; } + } + return 1; } -void m68k_movec2 (int regno, uae_u32 *regp) +int m68k_movec2 (int regno, uae_u32 *regp) { - if (CPUType == 1 && (regno & 0x7FF) > 1) + if ((CPUType == 1 && (regno & 0x7FF) > 1) + || (CPUType < 4 && (regno & 0x7FF) > 2) + || (CPUType == 4 && regno == 0x802)) + { op_illg (0x4E7A); - else + return 0; + } else { switch (regno) { case 0: *regp = regs.sfc; break; case 1: *regp = regs.dfc; break; @@ -775,10 +861,15 @@ void m68k_movec2 (int regno, uae_u32 *re case 0x802: *regp = caar; break; case 0x803: *regp = regs.m == 1 ? m68k_areg(regs, 7) : regs.msp; break; case 0x804: *regp = regs.m == 0 ? m68k_areg(regs, 7) : regs.isp; break; + case 0x805: *regp = mmusr; break; + case 0x806: *regp = urp; break; + case 0x807: *regp = srp; break; default: op_illg (0x4E7A); - break; + return 0; + } } + return 1; } static __inline__ int @@ -1036,12 +1127,16 @@ static char* ccnames[] = { "T ","F ","HI","LS","CC","CS","NE","EQ", "VC","VS","PL","MI","GE","LT","GT","LE" }; +// If value is greater than zero, this means we are still processing an EmulOp +// because the counter is incremented only in m68k_execute(), i.e. interpretive +// execution only +static int m68k_execute_depth = 0; + void m68k_reset (void) { m68k_areg (regs, 7) = 0x2000; m68k_setpc (ROMBaseMac + 0x2a); fill_prefetch_0 (); - regs.kick_mask = 0xF80000; regs.s = 1; regs.m = 0; regs.stopped = 0; @@ -1052,32 +1147,34 @@ void m68k_reset (void) SET_CFLG (0); SET_VFLG (0); SET_NFLG (0); - regs.spcflags = 0; + SPCFLAGS_INIT( 0 ); regs.intmask = 7; regs.vbr = regs.sfc = regs.dfc = 0; - /* gb-- moved into {fpp,fpu_x86}.cpp::fpu_init() - regs.fpcr = regs.fpsr = regs.fpiar = 0; */ fpu_reset(); + +#if FLIGHT_RECORDER +#if ENABLE_MON + if (log_ptr == -1) { + // Install "log" command in mon + mon_add_command("log", dump_log, "log Dump m68k emulation log\n"); + } +#endif + log_ptr = 0; + memset(log, 0, sizeof(log)); +#endif } -void REGPARAM2 op_illg (uae_u32 opcode) +void m68k_emulop_return(void) { - uaecptr pc = m68k_getpc (); - - compiler_flush_jsr_stack (); + SPCFLAGS_SET( SPCFLAG_BRK ); + quit_program = 1; +} - if ((opcode & 0xFF00) == 0x7100) { +void m68k_emulop(uae_u32 opcode) +{ struct M68kRegisters r; int i; - // Return from Execute68k()? - if (opcode == M68K_EXEC_RETURN) { - regs.spcflags |= SPCFLAG_BRK; - quit_program = 1; - return; - } - - // Call EMUL_OP opcode for (i=0; i<8; i++) { r.d[i] = m68k_dreg(regs, i); r.a[i] = m68k_areg(regs, i); @@ -1091,36 +1188,40 @@ void REGPARAM2 op_illg (uae_u32 opcode) } regs.sr = r.sr; MakeFromSR(); - m68k_incpc(2); - fill_prefetch_0 (); - return; - } +} + +cpuop_rettype REGPARAM2 op_illg (uae_u32 opcode) +{ + uaecptr pc = m68k_getpc (); if ((opcode & 0xF000) == 0xA000) { Exception(0xA,0); - return; + cpuop_return(CFLOW_TRAP); } -// write_log ("Illegal instruction: %04x at %08lx\n", opcode, pc); - if ((opcode & 0xF000) == 0xF000) { Exception(0xB,0); - return; + cpuop_return(CFLOW_TRAP); } write_log ("Illegal instruction: %04x at %08lx\n", opcode, pc); +#if USE_JIT && JIT_DEBUG + compiler_dumpstate(); +#endif Exception (4,0); + cpuop_return(CFLOW_TRAP); } void mmu_op(uae_u32 opcode, uae_u16 extra) { - if ((extra & 0xB000) == 0) { /* PMOVE instruction */ - - } else if ((extra & 0xF000) == 0x2000) { /* PLOAD instruction */ - } else if ((extra & 0xF000) == 0x8000) { /* PTEST instruction */ + if ((opcode & 0xFE0) == 0x0500) { + /* PFLUSH */ + mmusr = 0; + } else if ((opcode & 0x0FD8) == 0x548) { + /* PTEST */ } else - op_illg (opcode); + op_illg (opcode); } static int n_insns = 0, n_spcinsns = 0; @@ -1129,14 +1230,14 @@ static uaecptr last_trace_ad = 0; static void do_trace (void) { - if (regs.t0) { + if (regs.t0 && CPUType >= 2) { uae_u16 opcode; /* should also include TRAP, CHK, SR modification FPcc */ /* probably never used so why bother */ /* We can afford this to be inefficient... */ m68k_setpc (m68k_getpc ()); fill_prefetch_0 (); - opcode = get_word (regs.pc); + opcode = get_word(m68k_getpc()); if (opcode == 0x4e72 /* RTE */ || opcode == 0x4e74 /* RTD */ || opcode == 0x4e75 /* RTS */ @@ -1152,83 +1253,113 @@ static void do_trace (void) && (uae_s16)m68k_dreg(regs, opcode & 7) != 0)) { last_trace_ad = m68k_getpc (); - regs.spcflags &= ~SPCFLAG_TRACE; - regs.spcflags |= SPCFLAG_DOTRACE; + SPCFLAGS_CLEAR( SPCFLAG_TRACE ); + SPCFLAGS_SET( SPCFLAG_DOTRACE ); } } else if (regs.t1) { last_trace_ad = m68k_getpc (); - regs.spcflags &= ~SPCFLAG_TRACE; - regs.spcflags |= SPCFLAG_DOTRACE; + SPCFLAGS_CLEAR( SPCFLAG_TRACE ); + SPCFLAGS_SET( SPCFLAG_DOTRACE ); } } - -static int do_specialties (void) +int m68k_do_specialties (void) { - /*n_spcinsns++;*/ - run_compiled_code(); - if (regs.spcflags & SPCFLAG_DOTRACE) { +#if USE_JIT + // Block was compiled + SPCFLAGS_CLEAR( SPCFLAG_JIT_END_COMPILE ); + + // Retain the request to get out of compiled code until + // we reached the toplevel execution, i.e. the one that + // can compile then run compiled code. This also means + // we processed all (nested) EmulOps + if ((m68k_execute_depth == 0) && SPCFLAGS_TEST( SPCFLAG_JIT_EXEC_RETURN )) + SPCFLAGS_CLEAR( SPCFLAG_JIT_EXEC_RETURN ); +#endif + + if (SPCFLAGS_TEST( SPCFLAG_DOTRACE )) { Exception (9,last_trace_ad); } - while (regs.spcflags & SPCFLAG_STOP) { - if (regs.spcflags & (SPCFLAG_INT | SPCFLAG_DOINT)){ + while (SPCFLAGS_TEST( SPCFLAG_STOP )) { + if (SPCFLAGS_TEST( SPCFLAG_INT | SPCFLAG_DOINT )){ + SPCFLAGS_CLEAR( SPCFLAG_INT | SPCFLAG_DOINT ); int intr = intlev (); - regs.spcflags &= ~(SPCFLAG_INT | SPCFLAG_DOINT); if (intr != -1 && intr > regs.intmask) { Interrupt (intr); regs.stopped = 0; - regs.spcflags &= ~SPCFLAG_STOP; + SPCFLAGS_CLEAR( SPCFLAG_STOP ); } } } - if (regs.spcflags & SPCFLAG_TRACE) + if (SPCFLAGS_TEST( SPCFLAG_TRACE )) do_trace (); - if (regs.spcflags & SPCFLAG_DOINT) { + if (SPCFLAGS_TEST( SPCFLAG_DOINT )) { + SPCFLAGS_CLEAR( SPCFLAG_DOINT ); int intr = intlev (); - regs.spcflags &= ~SPCFLAG_DOINT; if (intr != -1 && intr > regs.intmask) { Interrupt (intr); regs.stopped = 0; } } - if (regs.spcflags & SPCFLAG_INT) { - regs.spcflags &= ~SPCFLAG_INT; - regs.spcflags |= SPCFLAG_DOINT; - } - if (regs.spcflags & (SPCFLAG_BRK | SPCFLAG_MODE_CHANGE)) { - regs.spcflags &= ~(SPCFLAG_BRK | SPCFLAG_MODE_CHANGE); - return 1; + if (SPCFLAGS_TEST( SPCFLAG_INT )) { + SPCFLAGS_CLEAR( SPCFLAG_INT ); + SPCFLAGS_SET( SPCFLAG_DOINT ); + } + if (SPCFLAGS_TEST( SPCFLAG_BRK )) { + SPCFLAGS_CLEAR( SPCFLAG_BRK ); + return CFLOW_EXEC_RETURN; } return 0; } -static void m68k_run_1 (void) +void m68k_do_execute (void) { for (;;) { uae_u32 opcode = GET_OPCODE; +#if FLIGHT_RECORDER + m68k_record_step(m68k_getpc()); +#endif +#ifdef X86_ASSEMBLY + __asm__ __volatile__("\tpushl %%ebp\n\tcall *%%ebx\n\tpopl %%ebp" /* FIXME */ + : : "b" (cpufunctbl[opcode]), "a" (opcode) + : "%edx", "%ecx", "%esi", "%edi", "%ebp", "memory", "cc"); +#else (*cpufunctbl[opcode])(opcode); - if (regs.spcflags) { - if (do_specialties()) +#endif + if (SPCFLAGS_TEST(SPCFLAG_ALL_BUT_EXEC_RETURN)) { + if (m68k_do_specialties()) return; } } } -#define m68k_run1 m68k_run_1 - -int in_m68k_go = 0; - -void m68k_go (int may_quit) +#if USE_JIT +void m68k_compile_execute (void) { -// m68k_go() must be reentrant for Execute68k() and Execute68kTrap() to work -/* - if (in_m68k_go || !may_quit) { - write_log("Bug! m68k_go is not reentrant.\n"); - abort(); + for (;;) { + if (quit_program > 0) { + if (quit_program == 1) + break; + quit_program = 0; + m68k_reset (); + } + m68k_do_compile_execute(); } -*/ - in_m68k_go++; + if (debugging) { + uaecptr nextpc; + m68k_dumpstate(&nextpc); + exit(1); + } +} +#endif + +void m68k_execute (void) +{ +#if USE_JIT + ++m68k_execute_depth; +#endif + for (;;) { if (quit_program > 0) { if (quit_program == 1) @@ -1236,14 +1367,17 @@ void m68k_go (int may_quit) quit_program = 0; m68k_reset (); } - m68k_run1(); + m68k_do_execute(); } if (debugging) { uaecptr nextpc; m68k_dumpstate(&nextpc); exit(1); } - in_m68k_go--; + +#if USE_JIT + --m68k_execute_depth; +#endif } static void m68k_verify (uaecptr addr, uaecptr *nextpc) @@ -1352,16 +1486,10 @@ void m68k_dumpstate (uaecptr *nextpc) printf ("T=%d%d S=%d M=%d X=%d N=%d Z=%d V=%d C=%d IMASK=%d\n", regs.t1, regs.t0, regs.s, regs.m, GET_XFLG, GET_NFLG, GET_ZFLG, GET_VFLG, GET_CFLG, regs.intmask); - for (i = 0; i < 8; i++){ - printf ("FP%d: %g ", i, regs.fp[i]); - if ((i & 3) == 3) printf ("\n"); - } - printf ("N=%d Z=%d I=%d NAN=%d\n", - (regs.fpsr & 0x8000000) != 0, - (regs.fpsr & 0x4000000) != 0, - (regs.fpsr & 0x2000000) != 0, - (regs.fpsr & 0x1000000) != 0); - + + fpu_dump_registers(); + fpu_dump_flags(); + m68k_disasm(m68k_getpc (), nextpc, 1); if (nextpc) printf ("next PC: %08lx\n", *nextpc);