--- hatari/src/uae-cpu/newcpu.c 2019/04/01 07:15:37 1.1.1.14 +++ hatari/src/uae-cpu/newcpu.c 2019/04/09 08:59:35 1.1.1.23 @@ -7,8 +7,8 @@ * * Adaptation to Hatari by Thomas Huth * - * This file is distributed under the GNU Public License, version 2 or at - * your option any later version. Read the file gpl.txt for details. + * This file is distributed under the GNU General Public License, version 2 + * or at your option any later version. Read the file gpl.txt for details. */ @@ -46,7 +46,7 @@ /* YEAH! Hatari is now the first emulator to pass the Transbeauce 2 protection :) */ /* 2007/12/18 [NP] More precise timings for HBL, VBL and MFP interrupts. On ST, these interrupts */ /* are taking 56 cycles instead of the 44 cycles in the 68000's documentation. */ -/* 2007/12/24 [NP] If an interrupt (HBL, VBL) is pending after intruction 'n' was processed, the */ +/* 2007/12/24 [NP] If an interrupt (HBL, VBL) is pending after instruction 'n' was processed, the */ /* exception should be called before instr. 'n+1' is processed, not after (else the*/ /* interrupt's handler is delayed by one 68000's instruction, which could break */ /* some demos with too strict timings) (ACF's Demo Main Menu). */ @@ -80,7 +80,7 @@ /* expires very often). Such precision is required for very recent hardscroll */ /* techniques that use 'stop' to stay in sync with the video shifter. */ /* 2008/11/23 [NP] In 'do_specialties()', when in STOP state, we must first test for a pending */ -/* interrupt that would exit the STOP state immediatly, without doing a 'while' */ +/* interrupt that would exit the STOP state immediately, without doing a 'while' */ /* loop until 'SPCFLAG_INT' or 'SPCFLAG_DOINT' are set. */ /* 2008/11/29 [NP] Call 'InterruptAddJitter()' when a video interrupt happens to precisely emulate */ /* the jitter happening on the Atari (see video.c for the jitter patterns). */ @@ -108,30 +108,68 @@ /* 2008/12/20 [NP] In m68k_run_1(), when checking interrupts and STOP mode, we should test */ /* PendingInterruptCount before regs.spcflags to have a faster evaluation of the */ /* 'while' condition (PendingInterruptCount <= 0 is true less often than STOP==0) */ +/* 2011/04/29 [NP] In Exception(), check the new PC is not on odd address ; raise an address error */ +/* exception if it's the case. */ +/* 2012/09/01 [NP] Add a special case to correct the stacked PC when a bus error happens during */ +/* a movem (fix the game Blood Money). */ +/* 2013/03/16 [NP] In refill_prefetch(), reload only one new word in regs.prefetch if low word is */ +/* still valid : low word goes to high word and we reload only low word */ +/* (fix EOR/ADD self modified code in the protection for the game Damocles). */ +/* 2013/04/11 [NP] In Exception(), call MFP_ProcessIACK after 12 cycles to update the MFP's vector */ +/* number used for the exception (see mfp.c). */ +/* 2013/05/03 [NP] In Exception(), handle IACK for HBL and VBL interrupts too, allowing pending bit*/ +/* to be set twice during an active video interrupt (correct fix for Super Monaco */ +/* GP, Super Hang On, Monster Business, European Demo's Intro, BBC Menu 52). */ +/* 2014/02/22 [NP] In Exception(), call valid_address() before reading the opcode at BusErrorPC, */ +/* else this will cause an unwanted "double bus error" ("Union Demo" loader). */ +/* 2014/02/22 [NP] In refill_prefetch(), use get_word() instead of do_get_mem_word() to generate */ +/* a bus error when trying to read from an invalid region. */ +/* 2014/03/18 [NP] In Exception(), add a specific case to restore the dest part of a "move" after */ +/* it was overwritten during a bus error (fix the game Dragon Flight). */ +/* 2014/04/06 [NP] In Exception(), add a special case for last_addr_for_exception_3 stored in the */ +/* stack after a bus error (fix the game Batman The Movie). */ +/* 2014/09/07 [NP] In m68k_run_1(), if get_iword_prefetch() triggers a bus error, we must call the */ +/* bus error immediately and fetch the correct opcode for the bus error handler */ +/* (fix Blood Money on Superior 65, PC=4e664e66 after RTS). */ +/* 2014/09/07 [NP] For address error, store if the access was in cpu space or data space and fix */ +/* the exception stack frame accordingly (fix Blood Money on Superior 65, */ +/* PC=4e664e66 after RTS) */ +/* 2015/02/11 [NP] Replace BusErrorPC by regs.instruction_pc, to get similar code to WinUAE's cpu */ - - -const char NewCpu_rcsid[] = "Hatari $Id: newcpu.c,v 1.1.1.14 2019/04/01 07:15:37 root Exp $"; +const char NewCpu_fileid[] = "Hatari newcpu.c : " __DATE__ " " __TIME__; #include "sysdeps.h" #include "hatari-glue.h" #include "maccess.h" #include "memory.h" #include "newcpu.h" -#include "../includes/main.h" -#include "../includes/log.h" -#include "../includes/m68000.h" -#include "../includes/int.h" -#include "../includes/mfp.h" -#include "../includes/tos.h" -#include "../includes/vdi.h" -#include "../includes/cart.h" -#include "../includes/debugui.h" -#include "../includes/bios.h" -#include "../includes/xbios.h" -#include "../includes/video.h" -#include "../includes/options.h" +#include "main.h" +#include "m68000.h" +#include "reset.h" +#include "cycInt.h" +#include "mfp.h" +#include "tos.h" +#include "vdi.h" +#include "cart.h" +#include "dialog.h" +#include "bios.h" +#include "xbios.h" +#include "screen.h" +#include "video.h" +#include "options.h" +#include "dsp.h" +#include "log.h" +#include "debugui.h" +#include "debugcpu.h" +#include "68kDisass.h" +#include "stMemory.h" + +#ifdef HAVE_CAPSIMAGE +#if CAPSIMAGE_VERSION == 5 +#include +#endif +#endif //#define DEBUG_PREFETCH @@ -143,6 +181,10 @@ uae_u16 last_op_for_exception_3; uaecptr last_addr_for_exception_3; /* Address that generated the exception */ uaecptr last_fault_for_exception_3; +/* read (0) or write (1) access */ +int last_writeaccess_for_exception_3; +/* instruction (1) or data (0) access */ +int last_instructionaccess_for_exception_3; const int areg_byteinc[] = { 1,1,1,1,1,1,1,2 }; const int imm8_table[] = { 8,1,2,3,4,5,6,7 }; @@ -158,6 +200,7 @@ int fpp_movem_next[256]; cpuop_func *cpufunctbl[65536]; int OpcodeFamily; +int BusCyclePenalty = 0; #define COUNT_INSTRS 0 @@ -211,6 +254,11 @@ void dump_counts (void) } #endif +static void cpu_halt ( void ) +{ + Dialog_HaltDlg(); +} + static unsigned long op_illg_1 (uae_u32 opcode) REGPARAM; @@ -344,9 +392,9 @@ struct regstruct regs; static long int m68kpc_offset; -#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)) +#define get_ibyte_1(o) STMemory_ReadByte(regs.pc + (regs.pc_p - regs.pc_oldp) + (o) + 1) +#define get_iword_1(o) STMemory_ReadWord(regs.pc + (regs.pc_p - regs.pc_oldp) + (o)) +#define get_ilong_1(o) STMemory_ReadLong(regs.pc + (regs.pc_p - regs.pc_oldp) + (o)) uae_s32 ShowEA (FILE *f, int reg, amodes mode, wordsizes size, char *buf) { @@ -836,38 +884,58 @@ static void InterruptAddJitter (int Leve /* and get a conflict with 'normal' cpu exceptions. */ void Exception(int nr, uaecptr oldpc, int ExceptionSource) { - uae_u32 currpc = m68k_getpc (); + uae_u32 currpc = m68k_getpc () , newpc; /*if( nr>=2 && nr<10 ) fprintf(stderr,"Exception (-> %i bombs)!\n",nr);*/ - /* Intercept VDI exception (Trap #2 with D0 = 0x73) */ - if ( ExceptionSource != M68000_EXCEPTION_SRC_INT_MFP ) + /* Pending bits / vector number can change before the end of the IACK sequence. */ + /* We need to handle MFP and HBL/VBL cases for this. */ + if ( ExceptionSource == M68000_EXC_SRC_INT_MFP ) + { + M68000_AddCycles ( CPU_IACK_CYCLES_START+CPU_IACK_CYCLES_MFP ); + CPU_IACK = true; + while ( ( PendingInterruptCount <= 0 ) && ( PendingInterruptFunction ) ) + CALL_VAR(PendingInterruptFunction); + nr = MFP_ProcessIACK ( nr ); + CPU_IACK = false; + } + else if ( ( ExceptionSource == M68000_EXC_SRC_AUTOVEC ) && ( ( nr == 26 ) || ( nr == 28 ) ) ) + { + M68000_AddCycles ( CPU_IACK_CYCLES_START+CPU_IACK_CYCLES_VIDEO ); + CPU_IACK = true; + while ( ( PendingInterruptCount <= 0 ) && ( PendingInterruptFunction ) ) + CALL_VAR(PendingInterruptFunction); + if ( MFP_UpdateNeeded == true ) + MFP_UpdateIRQ ( 0 ); /* update MFP's state if some internal timers related to MFP expired */ + pendingInterrupts &= ~( 1 << ( nr - 24 ) ); /* clear HBL or VBL pending bit */ + CPU_IACK = false; + } + + + if (ExceptionSource == M68000_EXC_SRC_CPU) { - if(bUseVDIRes && nr == 0x22 && regs.regs[0] == 0x73) + if (nr == 0x22) { - if(!VDI()) + /* Intercept VDI & AES exceptions (Trap #2) */ + if(bVdiAesIntercept && VDI_AES_Entry()) { - /* Set 'PC' as address of 'VDI_OPCODE' illegal instruction - * This will call OpCode_VDI after completion of Trap call! - * Use to modify return structure from VDI */ + /* Set 'PC' to address of 'VDI_OPCODE' illegal instruction. + * This will call OpCode_VDI() after completion of Trap call! + * This is used to modify specific VDI return vectors contents. + */ VDI_OldPC = currpc; currpc = CART_VDI_OPCODE_ADDR; } } - - if (bBiosIntercept) + else if (nr == 0x2d) { - /* Intercept BIOS or XBIOS trap (Trap #13 or #14) */ - if (nr == 0x2d) - { - /* Intercept BIOS calls */ - if (Bios()) return; - } - else if (nr == 0x2e) - { - /* Intercept XBIOS calls */ - if (XBios()) return; - } + /* Intercept BIOS (Trap #13) calls */ + if (Bios()) return; + } + else if (nr == 0x2e) + { + /* Intercept XBIOS (Trap #14) calls */ + if (XBios()) return; } } @@ -886,7 +954,8 @@ void Exception(int nr, uaecptr oldpc, in /* Build additional exception stack frame for 68010 and higher */ /* (special case for MFP) */ if (currprefs.cpu_level > 0) { - if (ExceptionSource == M68000_EXCEPTION_SRC_INT_MFP) { + if (ExceptionSource == M68000_EXC_SRC_INT_MFP + || ExceptionSource == M68000_EXC_SRC_INT_DSP) { m68k_areg(regs, 7) -= 2; put_word (m68k_areg(regs, 7), nr * 4); /* MFP interrupt, 'nr' can be in a different range depending on $fffa17 */ } @@ -929,12 +998,13 @@ void Exception(int nr, uaecptr oldpc, in m68k_areg(regs, 7) -= 2; put_word (m68k_areg(regs, 7), regs.sr); - HATARI_TRACE ( HATARI_TRACE_CPU_EXCEPTION , "cpu exception %d currpc %x buspc %x newpc %x fault_e3 %x op_e3 %hx addr_e3 %x\n" , - nr, currpc, BusErrorPC, get_long (regs.vbr + 4*nr), last_fault_for_exception_3, last_op_for_exception_3, last_addr_for_exception_3 ); + LOG_TRACE(TRACE_CPU_EXCEPTION, "cpu exception %d currpc %x buspc %x newpc %x fault_e3 %x op_e3 %hx addr_e3 %x\n", + nr, currpc, regs.instruction_pc, get_long (regs.vbr + 4*nr), last_fault_for_exception_3, last_op_for_exception_3, last_addr_for_exception_3); /* 68000 bus/address errors: */ - if (currprefs.cpu_level==0 && (nr==2 || nr==3) && (ExceptionSource != M68000_EXCEPTION_SRC_INT_MFP) ) { - uae_u16 specialstatus = 1; + if (currprefs.cpu_level==0 && (nr==2 || nr==3) && ExceptionSource == M68000_EXC_SRC_CPU) { + uae_u16 specialstatus = last_instructionaccess_for_exception_3 ? 2 : 1; + uae_u16 BusError_opcode; /* Special status word emulation isn't perfect yet... :-( */ if (regs.sr & 0x2000) @@ -942,64 +1012,114 @@ void Exception(int nr, uaecptr oldpc, in m68k_areg(regs, 7) -= 8; if (nr == 3) { /* Address error */ specialstatus |= ( last_op_for_exception_3 & (~0x1f) ); /* [NP] unused bits of specialstatus are those of the last opcode ! */ + if (last_writeaccess_for_exception_3==0) + specialstatus |= 0x10; /* bit 4 : 0=write 1=read */ put_word (m68k_areg(regs, 7), specialstatus); 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_long (m68k_areg(regs, 7)+10, last_addr_for_exception_3); - if (bEnableDebug) { - fprintf(stderr,"Address Error at address $%x, PC=$%x\n",last_fault_for_exception_3,currpc); - DebugUI(); + + /* [NP] PC stored in the stack frame is not necessarily pointing to the next instruction ! */ + /* FIXME : we should have a proper model for this, in the meantime we handle specific cases */ + if ( last_op_for_exception_3 == 0x2285 ) /* move.l d5,(a1) (War Heli) */ + put_long (m68k_areg(regs, 7)+10, currpc+4); /* correct PC is 2 bytes more than usual value */ + + fprintf(stderr,"Address Error at address $%x, PC=$%x addr_e3=%x op_e3=%x\n",last_fault_for_exception_3, currpc, last_addr_for_exception_3, last_op_for_exception_3); + if (ExceptionDebugMask & EXCEPT_ADDRESS) { + DebugUI(REASON_CPU_EXCEPTION); } } else { /* Bus error */ - specialstatus |= ( get_word(BusErrorPC) & (~0x1f) ); /* [NP] unused bits of special status are those of the last opcode ! */ + /* Get the opcode that caused the bus error, to adapt the stack frame in some cases */ + BusError_opcode = regs.opcode; + + specialstatus |= ( BusError_opcode & (~0x1f) ); /* [NP] unused bits of special status are those of the last opcode ! */ if (bBusErrorReadWrite) - specialstatus |= 0x10; + specialstatus |= 0x10; /* bit 4 : 0=write 1=read */ put_word (m68k_areg(regs, 7), specialstatus); put_long (m68k_areg(regs, 7)+2, BusErrorAddress); - put_word (m68k_areg(regs, 7)+6, get_word(BusErrorPC)); /* Opcode */ + put_word (m68k_areg(regs, 7)+6, BusError_opcode); /* Opcode */ /* [NP] PC stored in the stack frame is not necessarily pointing to the next instruction ! */ /* FIXME : we should have a proper model for this, in the meantime we handle specific cases */ - if ( get_word(BusErrorPC) == 0x21f8 ) /* move.l $0.w,$24.w (Transbeauce 2 loader) */ - put_long (m68k_areg(regs, 7)+10, currpc-2); /* correct PC is 2 bytes less than usual value */ + if ( BusError_opcode == 0x21f8 ) /* move.l $0.w,$24.w (Transbeauce 2 loader) */ + put_long (m68k_areg(regs, 7)+10, currpc-2); /* correct PC is 2 bytes less than usual value */ + + else if ( ( regs.instruction_pc == 0xccc ) && ( BusError_opcode == 0x48d6 ) ) /* 48d6 3f00 movem.l a0-a5,(a6) (Blood Money) */ + put_long (m68k_areg(regs, 7)+10, currpc+2); /* correct PC is 2 bytes more than usual value */ + + else if ( ( regs.instruction_pc == 0x1fece ) && ( BusError_opcode == 0x33d4 ) ) /* 1fece : 33d4 0001 fdca move.w (a4),$1fdca (Batman The Movie) */ + put_long (m68k_areg(regs, 7)+10, currpc-4); /* correct PC is 4 bytes less than usual value */ + + /* [NP] In case of a move with a bus error on the read part, uae cpu is writing to the dest part */ + /* then process the bus error ; on a real CPU, the bus error occurs after the read and before the */ + /* write, so the dest part doesn't change. For now, we restore the dest part on some specific cases */ + /* FIXME : the bus error should be processed just after the read, not at the end of the instruction */ + else if ( ( regs.instruction_pc == 0x62a ) && ( BusError_opcode == 0x3079 ) ) /* 3079 4ef9 0000 move.l $4ef90000,a0 (Dragon Flight) */ + m68k_areg(regs, 0) = 8; /* A0 should not be changed to "0" but keep its value "8" */ + + else if ( get_long(regs.instruction_pc) == 0x13f88e21 ) /* 13f8 8e21 move.b $ffff8e21.w,$xxxxx (Tymewarp) */ + put_byte ( get_long(regs.instruction_pc+4) , 0x00 ); /* dest content should not be changed to "ff" but keep its value "00" */ + + if (M68000_IsVerboseBusError(currpc, BusErrorAddress)) { + Log_Printf(LOG_WARN, "Bus Error at address $%x, PC=$%x addr_e3=%x op_e3=%x\n", + BusErrorAddress, currpc, + get_long(m68k_areg(regs, 7) + 10), BusError_opcode); + } + /* Check for double bus errors: */ if (regs.spcflags & SPCFLAG_BUSERROR) { - fprintf(stderr, "Detected double bus error at address $%x, PC=$%lx => CPU halted!\n", - BusErrorAddress, (long)currpc); - unset_special(SPCFLAG_BUSERROR); - if (bEnableDebug) - DebugUI(); - regs.intmask = 7; - m68k_setstopped(TRUE); + fprintf(stderr, "Detected double bus error at address $%x, PC=$%lx => CPU halted!\n", BusErrorAddress, (long)currpc); + cpu_halt(); return; } - if (bEnableDebug && BusErrorAddress!=0xff8a00) { + if ((ExceptionDebugMask & EXCEPT_BUS) && BusErrorAddress!=0xff8a00) { fprintf(stderr,"Bus Error at address $%x, PC=$%lx\n", BusErrorAddress, (long)currpc); - DebugUI(); + DebugUI(REASON_CPU_EXCEPTION); } } } /* Set PC and flags */ - if (bEnableDebug && get_long (regs.vbr + 4*nr) == 0) { - write_log("Uninitialized exception handler #%i!\n", nr); - DebugUI(); + if ((ExceptionDebugMask & EXCEPT_NOHANDLER) && (regs.vbr + 4*nr) == 0) { + fprintf(stderr,"Uninitialized exception handler #%i!\n", nr); + DebugUI(REASON_CPU_EXCEPTION); } + newpc = get_long (regs.vbr + 4*nr); + if ( newpc & 1) /* check new pc is even */ + { + if ( nr==2 || nr==3 ) /* address error during bus/address error -> stop emulation */ + { + fprintf(stderr,"Address Error during exception 2/3, new PC=$%x => CPU halted\n",newpc); + cpu_halt(); + } + else + { + fprintf(stderr,"Address Error during exception, new PC=$%x\n",newpc); + Exception ( 3 , m68k_getpc() , M68000_EXC_SRC_CPU ); + } + return; + } + /* handle debugger invocation for rest of exceptions */ + if (ExceptionDebugMask && nr > 3 && nr < 9) + DebugUI_Exceptions(nr, currpc); + m68k_setpc (get_long (regs.vbr + 4*nr)); fill_prefetch_0 (); /* Handle trace flags depending on current state */ exception_trace (nr); /* Handle exception cycles (special case for MFP) */ - if ( ExceptionSource == M68000_EXCEPTION_SRC_INT_MFP ) + if (ExceptionSource == M68000_EXC_SRC_INT_MFP) { - M68000_AddCycles(44+12); /* MFP interrupt, 'nr' can be in a different range depending on $fffa17 */ + M68000_AddCycles(44+12-CPU_IACK_CYCLES_START-CPU_IACK_CYCLES_MFP); /* MFP interrupt, 'nr' can be in a different range depending on $fffa17 */ } else if (nr >= 24 && nr <= 31) { - if ( ( nr == 26 ) || ( nr == 28 ) ) /* HBL or VBL */ - M68000_AddCycles(44+12); /* Video Interrupt */ + if ( nr == 26 ) /* HBL */ + M68000_AddCycles(44+12-CPU_IACK_CYCLES_START-CPU_IACK_CYCLES_VIDEO); /* Video Interrupt */ + else if ( nr == 28 ) /* VBL */ + M68000_AddCycles(44+12-CPU_IACK_CYCLES_START-CPU_IACK_CYCLES_VIDEO); /* Video Interrupt */ else M68000_AddCycles(44+4); /* Other Interrupts */ } @@ -1024,7 +1144,7 @@ void Exception(int nr, uaecptr oldpc, in if(nr < 64) M68000_AddCycles(4); /* Coprocessor and unassigned exceptions (???) */ else - M68000_AddCycles(44+12); /* Must be a MFP interrupt, should be processed above */ + M68000_AddCycles(44+12); /* Must be a MFP or DSP interrupt */ break; } @@ -1037,10 +1157,8 @@ static void Interrupt(int nr , int Pendi /*lastint_regs = regs;*/ /*lastint_no = nr;*/ - /* [NP] On Hatari, only video ints are using SPCFLAG_INT (see m68000.c) */ - /* TODO : to be really precise, we should use a global variable to store the last ExceptionSource */ - /* passed to M68000_Exception, instead of hardcoding M68000_EXCEPTION_SRC_INT_VIDEO here */ - Exception(nr+24, 0, M68000_EXCEPTION_SRC_INT_VIDEO); + /* On Hatari, only video ints are using SPCFLAG_INT (see m68000.c) */ + Exception(nr+24, 0, M68000_EXC_SRC_AUTOVEC); regs.intmask = nr; set_special (SPCFLAG_INT); @@ -1050,7 +1168,6 @@ static void Interrupt(int nr , int Pendi } -uae_u32 caar, cacr; static uae_u32 itt0, itt1, dtt0, dtt1, tc, mmusr, urp, srp; @@ -1088,7 +1205,17 @@ int m68k_move2c (int regno, uae_u32 *reg switch (regno) { case 0: regs.sfc = *regp & 7; break; case 1: regs.dfc = *regp & 7; break; - case 2: cacr = *regp & (currprefs.cpu_level < 4 ? 0x3 : 0x80008000); break; + case 2: + { + uae_u32 cacr_mask = 0; + if (currprefs.cpu_level == 2) // 68020 + cacr_mask = 0x0000000f; + else if (currprefs.cpu_level == 3) // Fake 68030 + cacr_mask = 0x00003f1f; + else if (currprefs.cpu_level == 4) // 68040 + cacr_mask = 0x80008000; + regs.cacr = *regp & cacr_mask; + } case 3: tc = *regp & 0xc000; break; /* Mask out fields that should be zero. */ case 4: itt0 = *regp & 0xffffe364; break; @@ -1098,7 +1225,7 @@ int m68k_move2c (int regno, uae_u32 *reg case 0x800: regs.usp = *regp; break; case 0x801: regs.vbr = *regp; break; - case 0x802: caar = *regp & 0xfc; break; + case 0x802: regs.caar = *regp; 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; @@ -1121,7 +1248,7 @@ int m68k_movec2 (int regno, uae_u32 *reg switch (regno) { case 0: *regp = regs.sfc; break; case 1: *regp = regs.dfc; break; - case 2: *regp = cacr; break; + case 2: *regp = regs.cacr; break; case 3: *regp = tc; break; case 4: *regp = itt0; break; case 5: *regp = itt1; break; @@ -1129,7 +1256,7 @@ int m68k_movec2 (int regno, uae_u32 *reg case 7: *regp = dtt1; break; case 0x800: *regp = regs.usp; break; case 0x801: *regp = regs.vbr; break; - case 0x802: *regp = caar; break; + case 0x802: *regp = regs.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; @@ -1143,6 +1270,7 @@ int m68k_movec2 (int regno, uae_u32 *reg return 1; } +#if !defined(uae_s64) STATIC_INLINE int div_unsigned(uae_u32 src_hi, uae_u32 src_lo, uae_u32 ndiv, uae_u32 *quot, uae_u32 *rem) { @@ -1167,12 +1295,13 @@ div_unsigned(uae_u32 src_hi, uae_u32 src *rem = src_hi; return 0; } +#endif void m68k_divl (uae_u32 opcode, uae_u32 src, uae_u16 extra, uaecptr oldpc) { #if defined(uae_s64) if (src == 0) { - Exception (5, oldpc,M68000_EXCEPTION_SRC_CPU); + Exception (5, oldpc,M68000_EXC_SRC_CPU); return; } if (extra & 0x800) { @@ -1227,7 +1356,7 @@ void m68k_divl (uae_u32 opcode, uae_u32 } #else if (src == 0) { - Exception (5, oldpc,M68000_EXCEPTION_SRC_CPU); + Exception (5, oldpc,M68000_EXC_SRC_CPU); return; } if (extra & 0x800) { @@ -1289,6 +1418,7 @@ void m68k_divl (uae_u32 opcode, uae_u32 #endif } +#if !defined(uae_s64) STATIC_INLINE void mul_unsigned(uae_u32 src1, uae_u32 src2, uae_u32 *dst_hi, uae_u32 *dst_lo) { @@ -1307,6 +1437,7 @@ mul_unsigned(uae_u32 src1, uae_u32 src2, *dst_lo = lo; *dst_hi = r3; } +#endif void m68k_mull (uae_u32 opcode, uae_u32 src, uae_u16 extra) { @@ -1408,7 +1539,7 @@ void m68k_reset (void) SET_CFLG (0); SET_VFLG (0); SET_NFLG (0); - regs.spcflags &= SPCFLAG_MODE_CHANGE; /* Clear specialflags except mode-change */ + regs.spcflags &= ( SPCFLAG_MODE_CHANGE | SPCFLAG_DEBUGGER ); /* Clear specialflags except mode-change and debugger */ regs.intmask = 7; regs.vbr = regs.sfc = regs.dfc = 0; regs.fpcr = regs.fpsr = regs.fpiar = 0; @@ -1425,17 +1556,17 @@ unsigned long REGPARAM2 op_illg (uae_u32 uaecptr pc = m68k_getpc (); #endif if ((opcode & 0xF000) == 0xF000) { - Exception(0xB,0,M68000_EXCEPTION_SRC_CPU); + Exception(0xB,0,M68000_EXC_SRC_CPU); return 4; } if ((opcode & 0xF000) == 0xA000) { - Exception(0xA,0,M68000_EXCEPTION_SRC_CPU); + Exception(0xA,0,M68000_EXC_SRC_CPU); return 4; } #if 0 write_log ("Illegal instruction: %04x at %08lx\n", opcode, (long)pc); #endif - Exception (4,0,M68000_EXCEPTION_SRC_CPU); + Exception (4,0,M68000_EXC_SRC_CPU); return 4; } @@ -1466,7 +1597,7 @@ static void do_trace (void) m68k_setpc (m68k_getpc ()); fill_prefetch_0 (); opcode = get_word (regs.pc); - if (opcode == 0x4e72 /* RTE */ + if (opcode == 0x4e73 /* RTE */ || opcode == 0x4e74 /* RTD */ || opcode == 0x4e75 /* RTS */ || opcode == 0x4e77 /* RTR */ @@ -1498,10 +1629,18 @@ static void do_trace (void) static bool do_specialties_interrupt (int Pending) { - /* Check for MFP ints first (level 6) */ +#if ENABLE_DSP_EMU + /* Check for DSP int first (if enabled) (level 6) */ + if (regs.spcflags & SPCFLAG_DSP) { + if (DSP_ProcessIRQ() == true) + return true; + } +#endif + + /* Check for MFP ints (level 6) */ if (regs.spcflags & SPCFLAG_MFP) { - if ( MFP_CheckPendingInterrupts() == TRUE ) - return TRUE; /* MFP exception was generated, no higher interrupt can happen */ + if (MFP_ProcessIRQ() == true) + return true; /* MFP exception was generated, no higher interrupt can happen */ } /* No MFP int, check for VBL/HBL ints (levels 4/2) */ @@ -1512,11 +1651,11 @@ static bool do_specialties_interrupt (in unset_special (SPCFLAG_INT | SPCFLAG_DOINT); if (intr != -1 && intr > regs.intmask) { Interrupt (intr , Pending); /* process the interrupt and add pending jitter if necessary */ - return TRUE; + return true; } } - return FALSE; /* no interrupt was found */ + return false; /* no interrupt was found */ } @@ -1527,43 +1666,30 @@ static int do_specialties (void) * functions since the PC should point to the address of the next * instruction, so we're executing the bus errors here: */ unset_special(SPCFLAG_BUSERROR); - Exception(2,0,M68000_EXCEPTION_SRC_CPU); + Exception(2,0,M68000_EXC_SRC_CPU); } - if(regs.spcflags & SPCFLAG_EXTRA_CYCLES) { + if ( WaitStateCycles ) { /* Add some extra cycles to simulate a wait state */ - unset_special(SPCFLAG_EXTRA_CYCLES); - M68000_AddCycles(nWaitStateCycles); - nWaitStateCycles = 0; + M68000_AddCycles(WaitStateCycles); + WaitStateCycles = 0; } if (regs.spcflags & SPCFLAG_DOTRACE) { - Exception (9,last_trace_ad,M68000_EXCEPTION_SRC_CPU); + Exception (9,last_trace_ad,M68000_EXC_SRC_CPU); } /* Handle the STOP instruction */ if ( regs.spcflags & SPCFLAG_STOP ) { +//fprintf ( stderr , "test stop %d\n" , nCyclesMainCounter ); /* We first test if there's a pending interrupt that would */ - /* allow to immediatly leave the STOP state */ - if ( do_specialties_interrupt ( TRUE ) ) { /* test if there's an interrupt and add pending jitter */ + /* allow to immediately leave the STOP state */ + if ( do_specialties_interrupt(true) ) { /* test if there's an interrupt and add pending jitter */ regs.stopped = 0; unset_special (SPCFLAG_STOP); +//fprintf ( stderr , "exit stop %d\n" , nCyclesMainCounter ); } -#if 0 - if (regs.spcflags & SPCFLAG_MFP) /* MFP int */ - MFP_CheckPendingInterrupts(); - - if (regs.spcflags & (SPCFLAG_INT | SPCFLAG_DOINT)) { /* VBL/HBL ints */ - int intr = intlev (); - unset_special (SPCFLAG_INT | SPCFLAG_DOINT); - if (intr != -1 && intr > regs.intmask) { - Interrupt (intr , TRUE); /* process the interrupt and add pending jitter */ - regs.stopped = 0; - unset_special (SPCFLAG_STOP); - } - } -#endif /* No pending int, we have to wait for the next matching int */ while (regs.spcflags & SPCFLAG_STOP) { @@ -1575,33 +1701,16 @@ static int do_specialties (void) M68000_AddCycles(4); /* It is possible one or more ints happen at the same time */ - /* We must process them during the same cpu cycle until the special INT flag is set */ - while (PendingInterruptCount<=0 && PendingInterruptFunction) { - /* 1st, we call the interrupt handler */ + /* We must process them during the same cpu cycle then choose the highest priority one */ + while ( ( PendingInterruptCount <= 0 ) && ( PendingInterruptFunction ) ) CALL_VAR(PendingInterruptFunction); - - /* Then we check if this handler triggered an interrupt to process */ - if ( do_specialties_interrupt ( FALSE ) ) { /* test if there's an interrupt and add non pending jitter */ - regs.stopped = 0; - unset_special (SPCFLAG_STOP); - break; - } -#if 0 - /* Then we check if this handler triggered an MFP int to process */ - if (regs.spcflags & SPCFLAG_MFP) - MFP_CheckPendingInterrupts(); - - if (regs.spcflags & (SPCFLAG_INT | SPCFLAG_DOINT)) { - int intr = intlev (); - unset_special (SPCFLAG_INT | SPCFLAG_DOINT); - if (intr != -1 && intr > regs.intmask) { - Interrupt (intr , FALSE); /* process the interrupt and add non pending jitter */ - regs.stopped = 0; - unset_special (SPCFLAG_STOP); - break; - } - } -#endif + if ( MFP_UpdateNeeded == true ) + MFP_UpdateIRQ ( 0 ); + + /* Check is there's an interrupt to process (could be a delayed MFP interrupt) */ + if ( do_specialties_interrupt(false) ) { /* test if there's an interrupt and add non pending jitter */ + regs.stopped = 0; + unset_special (SPCFLAG_STOP); } } } @@ -1613,34 +1722,17 @@ static int do_specialties (void) // if (regs.spcflags & SPCFLAG_DOINT) { /* [NP] pending int should be processed now, not after the current instr */ /* so we check for (SPCFLAG_INT | SPCFLAG_DOINT), not just for SPCFLAG_DOINT */ - - if ( do_specialties_interrupt ( FALSE ) ) { /* test if there's an interrupt and add non pending jitter */ + + if ( do_specialties_interrupt(false) ) { /* test if there's an interrupt and add non pending jitter */ regs.stopped = 0; /* [NP] useless ? */ } if (regs.spcflags & SPCFLAG_INT) { unset_special (SPCFLAG_INT); set_special (SPCFLAG_DOINT); } -#if 0 - if (regs.spcflags & (SPCFLAG_INT | SPCFLAG_DOINT)) { - int intr = intlev (); - /* SPCFLAG_DOINT will be enabled again in MakeFromSR to handle pending interrupts! */ -// unset_special (SPCFLAG_DOINT); - unset_special (SPCFLAG_INT | SPCFLAG_DOINT); - if (intr != -1 && intr > regs.intmask) { - Interrupt (intr , FALSE); /* call Interrupt() with Pending=FALSE, not necessarily true but harmless */ - regs.stopped = 0; /* [NP] useless ? */ - } - } - if (regs.spcflags & SPCFLAG_INT) { - unset_special (SPCFLAG_INT); - set_special (SPCFLAG_DOINT); - } - if (regs.spcflags & SPCFLAG_MFP) { /* Check for MFP interrupts */ - MFP_CheckPendingInterrupts(); - } -#endif + if (regs.spcflags & SPCFLAG_DEBUGGER) + DebugCpu_Check(); if (regs.spcflags & (SPCFLAG_BRK | SPCFLAG_MODE_CHANGE)) { unset_special(SPCFLAG_MODE_CHANGE); @@ -1657,95 +1749,109 @@ static void m68k_run_1 (void) { #ifdef DEBUG_PREFETCH uae_u8 saved_bytes[20]; - uae_u16 *oldpcp; + uae_u8 *oldpcp; #endif for (;;) { int cycles; - uae_u32 opcode = get_iword_prefetch (0); +//fprintf (stderr, "ir in %x %x\n",do_get_mem_long(®s.prefetch) , regs.prefetch_pc); + regs.opcode = get_iword_prefetch (0); -#ifdef DEBUG_PREFETCH - if (get_ilong (0) != do_get_mem_long (®s.prefetch)) { - fprintf (stderr, "Prefetch differs from memory.\n"); - debugging = 1; - return; + if (regs.spcflags & SPCFLAG_BUSERROR) + { + unset_special(SPCFLAG_BUSERROR); + Exception(2,0,M68000_EXC_SRC_CPU); + + /* Get opcode for bus error handler and check other special bits */ + regs.opcode = get_iword_prefetch (0); + if (regs.spcflags) { + if (do_specialties ()) + return; + } } + +#ifdef DEBUG_PREFETCH +// if (get_ilong (0) != do_get_mem_long (®s.prefetch)) { +// fprintf (stderr, "Prefetch differs from memory.\n"); +// debugging = 1; +// return; +// } oldpcp = regs.pc_p; memcpy (saved_bytes, regs.pc_p, 20); #endif /*m68k_dumpstate(stderr, NULL);*/ - if ( HATARI_TRACE_LEVEL ( HATARI_TRACE_CPU_DISASM ) ) - { - int nFrameCycles = Cycles_GetCounter(CYCLES_COUNTER_VIDEO);; - int nLineCycles = nFrameCycles % nCyclesPerLine; - HATARI_TRACE_PRINT ( "video_cyc=%6d %3d@%3d : " , nFrameCycles, nLineCycles, nHBL ); - m68k_disasm(stderr, m68k_getpc (), NULL, 1); - } + if (LOG_TRACE_LEVEL(TRACE_CPU_DISASM)) + { + int FrameCycles, HblCounterVideo, LineCycles; + + Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles ); + + LOG_TRACE_PRINT ( "cpu video_cyc=%6d %3d@%3d : " , FrameCycles, LineCycles, HblCounterVideo ); + Disasm(stderr, m68k_getpc (), NULL, 1); + } /* assert (!regs.stopped && !(regs.spcflags & SPCFLAG_STOP)); */ /* regs_backup[backup_pointer = (backup_pointer + 1) % 16] = regs;*/ #if COUNT_INSTRS == 2 - if (table68k[opcode].handler != -1) - instrcount[table68k[opcode].handler]++; + if (table68k[regs.opcode].handler != -1) + instrcount[table68k[regs.opcode].handler]++; #elif COUNT_INSTRS == 1 - instrcount[opcode]++; + instrcount[regs.opcode]++; #endif /* In case of a Bus Error, we need the PC of the instruction that caused */ /* the error to build the exception stack frame */ - BusErrorPC = m68k_getpc(); + regs.instruction_pc = m68k_getpc(); + + if (bDspEnabled) + Cycles_SetCounter(CYCLES_COUNTER_CPU, 0); /* to measure the total number of cycles spent in the cpu */ - cycles = (*cpufunctbl[opcode])(opcode); + /* Uncomment following lines to call capslib's debugger after each instruction */ + //if ( CAPSGetDebugRequest() ) + // DebugUI(REASON_CPU_BREAKPOINT); + + cycles = (*cpufunctbl[regs.opcode])(regs.opcode); +//fprintf (stderr, "ir out %x %x\n",do_get_mem_long(®s.prefetch) , regs.prefetch_pc); #ifdef DEBUG_PREFETCH if (memcmp (saved_bytes, oldpcp, 20) != 0) { fprintf (stderr, "Self-modifying code detected %x.\n" , m68k_getpc() ); - set_special (SPCFLAG_BRK); - debugging = 1; +// set_special (SPCFLAG_BRK); +// debugging = 1; } #endif M68000_AddCyclesWithPairing(cycles); - if (regs.spcflags & SPCFLAG_EXTRA_CYCLES) { + + if ( WaitStateCycles ) { /* Add some extra cycles to simulate a wait state */ - unset_special(SPCFLAG_EXTRA_CYCLES); - M68000_AddCycles(nWaitStateCycles); - nWaitStateCycles = 0; + M68000_AddCycles(WaitStateCycles); + WaitStateCycles = 0; } -#if 0 - while (PendingInterruptCount <= 0 && PendingInterruptFunction) - CALL_VAR(PendingInterruptFunction); - - if (regs.spcflags) { - if (do_specialties ()) - return; - } -#else /* We can have several interrupts at the same time before the next CPU instruction */ /* We must check for pending interrupt and call do_specialties_interrupt() only */ /* if the cpu is not in the STOP state. Else, the int could be acknowledged now */ /* and prevent exiting the STOP state when calling do_specialties() after. */ /* For performance, we first test PendingInterruptCount, then regs.spcflags */ - while ( ( PendingInterruptCount <= 0 ) && ( PendingInterruptFunction ) && ( ( regs.spcflags & SPCFLAG_STOP ) == 0 ) ) - { - CALL_VAR(PendingInterruptFunction); /* call the interrupt handler */ - do_specialties_interrupt ( FALSE ); /* test if there's an mfp/video interrupt and add non pending jitter */ -#if 0 - if ( regs.spcflags & ( SPCFLAG_MFP | SPCFLAG_INT ) ) { /* only check mfp/video interrupts */ - if (do_specialties ()) /* check if this latest int has higher priority */ - return; - } -#endif - } + if ( PendingInterruptCount <= 0 ) + { + while ( ( PendingInterruptCount <= 0 ) && ( PendingInterruptFunction ) && ( ( regs.spcflags & SPCFLAG_STOP ) == 0 ) ) + CALL_VAR ( PendingInterruptFunction ); /* call the interrupt's handler */ + if ( MFP_UpdateNeeded == true ) + MFP_UpdateIRQ ( 0 ); /* update MFP's state if some internal timers related to MFP expired */ + } if (regs.spcflags) { if (do_specialties ()) return; } -#endif + /* Run DSP 56k code if necessary */ + if (bDspEnabled) { + DSP_Run( Cycles_GetCounter(CYCLES_COUNTER_CPU) * DSP_CPU_FREQ_RATIO); + } } } @@ -1755,43 +1861,63 @@ static void m68k_run_2 (void) { for (;;) { int cycles; - uae_u32 opcode = get_iword (0); + + regs.opcode = get_iword (0); /*m68k_dumpstate(stderr, NULL);*/ - if ( HATARI_TRACE_LEVEL ( HATARI_TRACE_CPU_DISASM ) ) - { - int nFrameCycles = Cycles_GetCounter(CYCLES_COUNTER_VIDEO);; - int nLineCycles = nFrameCycles % nCyclesPerLine; - HATARI_TRACE_PRINT ( "video_cyc=%6d %3d@%3d : " , nFrameCycles, nLineCycles, nHBL ); - m68k_disasm(stderr, m68k_getpc (), NULL, 1); - } + if (LOG_TRACE_LEVEL(TRACE_CPU_DISASM)) + { + int FrameCycles, HblCounterVideo, LineCycles; + + Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles ); + + LOG_TRACE_PRINT ( "cpu video_cyc=%6d %3d@%3d : " , FrameCycles, LineCycles, HblCounterVideo ); + Disasm(stderr, m68k_getpc (), NULL, 1); + } /* assert (!regs.stopped && !(regs.spcflags & SPCFLAG_STOP)); */ /* regs_backup[backup_pointer = (backup_pointer + 1) % 16] = regs;*/ #if COUNT_INSTRS == 2 - if (table68k[opcode].handler != -1) - instrcount[table68k[opcode].handler]++; + if (table68k[regs.opcode].handler != -1) + instrcount[table68k[regs.opcode].handler]++; #elif COUNT_INSTRS == 1 - instrcount[opcode]++; + instrcount[regs.opcode]++; #endif - cycles = (*cpufunctbl[opcode])(opcode); + /* In case of a Bus Error, we need the PC of the instruction that caused */ + /* the error to build the exception stack frame */ + regs.instruction_pc = m68k_getpc(); + + cycles = (*cpufunctbl[regs.opcode])(regs.opcode); + + if (bDspEnabled) + Cycles_SetCounter(CYCLES_COUNTER_CPU, 0); /* to measure the total number of cycles spent in the cpu */ M68000_AddCycles(cycles); - if (regs.spcflags & SPCFLAG_EXTRA_CYCLES) { + + if ( WaitStateCycles ) { /* Add some extra cycles to simulate a wait state */ - unset_special(SPCFLAG_EXTRA_CYCLES); - M68000_AddCycles(nWaitStateCycles); - nWaitStateCycles = 0; + M68000_AddCycles(WaitStateCycles); + WaitStateCycles = 0; } - while (PendingInterruptCount <= 0 && PendingInterruptFunction) - CALL_VAR(PendingInterruptFunction); + if ( PendingInterruptCount <= 0 ) + { + while ( ( PendingInterruptCount <= 0 ) && ( PendingInterruptFunction ) ) + CALL_VAR(PendingInterruptFunction); + if ( MFP_UpdateNeeded == true ) + MFP_UpdateIRQ ( 0 ); + } if (regs.spcflags) { if (do_specialties ()) return; } + + /* Run DSP 56k code if necessary */ + if (bDspEnabled) { + DSP_Run( Cycles_GetCounter(CYCLES_COUNTER_CPU) * DSP_CPU_FREQ_RATIO); + } } } @@ -1834,13 +1960,13 @@ static void m68k_verify (uaecptr addr, u if (dp->suse) { if (!verify_ea (dp->sreg, dp->smode, dp->size, &val)) { - Exception (3, 0,M68000_EXCEPTION_SRC_CPU); + Exception (3, 0,M68000_EXC_SRC_CPU); return; } } if (dp->duse) { if (!verify_ea (dp->dreg, dp->dmode, dp->size, &val)) { - Exception (3, 0,M68000_EXCEPTION_SRC_CPU); + Exception (3, 0,M68000_EXC_SRC_CPU); return; } } @@ -1864,8 +1990,17 @@ void m68k_disasm (FILE *f, uaecptr addr, struct instr *dp; fprintf (f, "%08lx: ", m68k_getpc () + m68kpc_offset); for (opwords = 0; opwords < 5; opwords++){ - fprintf (f, "%04x ", get_iword_1 (m68kpc_offset + opwords*2)); + if ( valid_address ( m68k_getpc () + m68kpc_offset + opwords*2 , 2 ) ) + fprintf (f, "%04x ", get_iword_1 (m68kpc_offset + opwords*2)); + else + fprintf (f, "**** " ); } + if ( ! valid_address ( m68k_getpc () + m68kpc_offset , 2 ) ) { + fprintf (f , "BUS ERROR\n" ); /* If region is not valid (bus error region) */ + m68kpc_offset += 2; + continue; + } + opcode = get_iword_1 (m68kpc_offset); m68kpc_offset += 2; if (cpufunctbl[opcode] == op_illg_1) {