--- hatari/src/uae-cpu/newcpu.c 2019/04/01 07:13:48 1.1.1.12 +++ hatari/src/uae-cpu/newcpu.c 2019/04/01 07:15:37 1.1.1.14 @@ -56,10 +56,63 @@ /* an MFP exception could be wrong if the MFP VR was set to another value than the */ /* default $40 (this could be a problem with programs requiring a precise cycles */ /* calculation while changing VR, but no such programs were encountered so far). */ +/* -> FIXED, see 2008/10/05 */ +/* 2008/04/17 [NP] In m68k_run_1/m68k_run_2, add the wait state cycles before testing if content */ +/* of PendingInterruptCount is <= 0 (else the int could happen a few cycles earlier*/ +/* than expected in some rare cases (reading $fffa21 in BIG Demo Screen 1)). */ +/* 2008/09/14 [NP] Add the value of the new PC in the exception's log. */ +/* 2008/09/14 [NP] Correct cycles for TRAP are 34 not 38 (4 more cycles were counted because cpuemu*/ +/* returns 4 and Exception() adds 34) (Phaleon / Illusion Demo by Next). */ +/* FIXME : Others exception cycles may be wrong too. */ +/* 2008/10/05 [NP] Add a parameter 'ExceptionSource' to Exception(). This allows to know the source*/ +/* of the exception (video, mfp, cpu) and properly handle MFP interrupts. Since */ +/* it's possible to change the vector base in $fffa17, MFP int vectors can overlap */ +/* the 'normal' 68000 ones and the exception number is not enough to decide. */ +/* We need ExceptionSource to remove the ambiguity. */ +/* Fix High Fidelity Dreams by Aura which sets MFP vector base to $c0 instead of */ +/* $100. In that case, timer B int becomes exception nr 56 and conflicts with the */ +/* 'MMU config error' exception, which takes 4 cycles instead of 56 cycles for MFP.*/ +/* 2008/11/18 [NP] In 'do_specialties()', when the cpu is in the STOP state, we must test all */ +/* possible int handlers while PendingInterruptCount <= 0 without increasing the */ +/* cpu cycle counter. In the case where both an MFP int and an HBL occur at the */ +/* same time for example, the HBL was delayed by 4 cycles if no MFP exception */ +/* was triggered, which was wrong (this happened mainly with the TOS timer D that */ +/* 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' */ +/* 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). */ +/* FIXME : Pattern is not always correct when handling pending interrupt in STOP */ +/* state, but this should be harmless as no program has been found using this. */ +/* 2008/12/05 [NP] On Atari it takes 56 cycles to process an interrupt. During that time, a higher */ +/* level interrupt could happen and we must execute it before the previous int */ +/* (see m68k_run_1()). */ +/* This is the case for the VBL which can interrupt the last HBL of a screen */ +/* (end of line 312) at various point (from 0 to 8 cycles). */ +/* This fixes the fullscreen tunnel in Suretrip 49% by Checkpoint, which uses a */ +/* really buggy vbl/hbl combination, even on a real ST. Also fixes sample sound */ +/* in Swedish New Year's TCB screen. */ +/* 2008/12/11 [NP] Extract interrupt handling from do_specialties() in do_specialties_interrupt() */ +/* and factorize some code. In m68k_run_1 when testing for multiple interrupts at */ +/* the same time, call do_specialties_interrupt() to check only the special flags */ +/* related to interrupts (MFP and video) (else, this caused problem when the TRACE */ +/* flag was set). */ +/* 2008/12/14 [NP] In m68k_run_1(), we should check for simultaneous ints only if the cpu is not */ +/* in the STOP state after the last instruction was executed. Else, the call to */ +/* do_specialties_interrupt() could acknowledge the interrupt and we would never */ +/* exit the STOP state in do_specialties() just after (the problem can happen if */ +/* the TOS timer D expires just at the same time as the STOP instruction). */ +/* Fix regression since 2008/12/11 in the hidden screen from ULM in Oh Crickey... */ +/* 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) */ -const char NewCpu_rcsid[] = "Hatari $Id: newcpu.c,v 1.1.1.12 2019/04/01 07:13:48 root Exp $"; + +const char NewCpu_rcsid[] = "Hatari $Id: newcpu.c,v 1.1.1.14 2019/04/01 07:15:37 root Exp $"; #include "sysdeps.h" #include "hatari-glue.h" @@ -78,7 +131,7 @@ const char NewCpu_rcsid[] = "Hatari $Id: #include "../includes/bios.h" #include "../includes/xbios.h" #include "../includes/video.h" -#include "../includes/trace.h" +#include "../includes/options.h" //#define DEBUG_PREFETCH @@ -747,38 +800,76 @@ static void exception_trace (int nr) } -void Exception(int nr, uaecptr oldpc) +/* + * Compute the number of jitter cycles to add when a video interrupt occurs + * (this is specific to the Atari ST) + */ +static void InterruptAddJitter (int Level , int Pending) +{ + int cycles = 0; + + if ( Level == 2 ) /* HBL */ + { + if ( Pending ) + cycles = HblJitterArrayPending[ HblJitterIndex ]; + else + cycles = HblJitterArray[ HblJitterIndex ]; + } + + else if ( Level == 4 ) /* VBL */ + { + if ( Pending ) + cycles = VblJitterArrayPending[ VblJitterIndex ]; + else + cycles = VblJitterArray[ VblJitterIndex ]; + } + +//fprintf ( stderr , "jitter %d\n" , cycles ); +//cycles=0; + if ( cycles > 0 ) /* no need to call M68000_AddCycles if cycles == 0 */ + M68000_AddCycles ( cycles ); +} + + +/* Handle exceptions. We need a special case to handle MFP exceptions */ +/* on Atari ST, because it's possible to change the MFP's vector base */ +/* and get a conflict with 'normal' cpu exceptions. */ +void Exception(int nr, uaecptr oldpc, int ExceptionSource) { uae_u32 currpc = m68k_getpc (); /*if( nr>=2 && nr<10 ) fprintf(stderr,"Exception (-> %i bombs)!\n",nr);*/ /* Intercept VDI exception (Trap #2 with D0 = 0x73) */ - if(bUseVDIRes && nr == 0x22 && regs.regs[0] == 0x73) - { - if(!VDI()) + if ( ExceptionSource != M68000_EXCEPTION_SRC_INT_MFP ) { - /* 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 */ - VDI_OldPC = currpc; - currpc = CART_VDI_OPCODE_ADDR; + if(bUseVDIRes && nr == 0x22 && regs.regs[0] == 0x73) + { + if(!VDI()) + { + /* 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 */ + VDI_OldPC = currpc; + currpc = CART_VDI_OPCODE_ADDR; + } + } + + if (bBiosIntercept) + { + /* 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; + } + } } - } - -#if 0 - /* 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; - } -#endif MakeSR(); @@ -793,8 +884,13 @@ void Exception(int nr, uaecptr oldpc) } /* Build additional exception stack frame for 68010 and higher */ + /* (special case for MFP) */ if (currprefs.cpu_level > 0) { - if (nr == 2 || nr == 3) { + if (ExceptionSource == M68000_EXCEPTION_SRC_INT_MFP) { + 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 */ + } + else if (nr == 2 || nr == 3) { int i; /* @@@ this is probably wrong (?) */ for (i = 0 ; i < 12 ; i++) { @@ -833,11 +929,11 @@ void Exception(int nr, uaecptr oldpc) 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 fault_e3 %x op_e3 %hx addr_e3 %x\n" , - nr, currpc, BusErrorPC, last_fault_for_exception_3, last_op_for_exception_3, last_addr_for_exception_3 ); + 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 ); /* 68000 bus/address errors: */ - if (currprefs.cpu_level==0 && (nr==2 || nr==3)) { + if (currprefs.cpu_level==0 && (nr==2 || nr==3) && (ExceptionSource != M68000_EXCEPTION_SRC_INT_MFP) ) { uae_u16 specialstatus = 1; /* Special status word emulation isn't perfect yet... :-( */ @@ -888,16 +984,19 @@ void Exception(int nr, uaecptr oldpc) /* Set PC and flags */ if (bEnableDebug && get_long (regs.vbr + 4*nr) == 0) { write_log("Uninitialized exception handler #%i!\n", nr); - if (bEnableDebug) - DebugUI(); + DebugUI(); } m68k_setpc (get_long (regs.vbr + 4*nr)); fill_prefetch_0 (); /* Handle trace flags depending on current state */ exception_trace (nr); - /* Handle exception cycles */ - if(nr >= 24 && nr <= 31) + /* Handle exception cycles (special case for MFP) */ + if ( ExceptionSource == M68000_EXCEPTION_SRC_INT_MFP ) + { + M68000_AddCycles(44+12); /* 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 */ @@ -906,7 +1005,7 @@ void Exception(int nr, uaecptr oldpc) } else if(nr >= 32 && nr <= 47) { - M68000_AddCycles(34); /* Trap */ + M68000_AddCycles(34-4); /* Trap (total is 34, but cpuemu.c already adds 4) */ } else switch(nr) { @@ -925,21 +1024,29 @@ void Exception(int nr, uaecptr oldpc) if(nr < 64) M68000_AddCycles(4); /* Coprocessor and unassigned exceptions (???) */ else - M68000_AddCycles(44+12); /* Must be a MFP interrupt */ + M68000_AddCycles(44+12); /* Must be a MFP interrupt, should be processed above */ break; } + } -static void Interrupt(int nr) +static void Interrupt(int nr , int Pending) { assert(nr < 8 && nr >= 0); /*lastint_regs = regs;*/ /*lastint_no = nr;*/ - Exception(nr+24, 0); + + /* [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); regs.intmask = nr; set_special (SPCFLAG_INT); + + /* Handle Atari ST's specific jitter for hbl/vbl */ + InterruptAddJitter ( nr , Pending ); } @@ -1065,7 +1172,7 @@ void m68k_divl (uae_u32 opcode, uae_u32 { #if defined(uae_s64) if (src == 0) { - Exception (5, oldpc); + Exception (5, oldpc,M68000_EXCEPTION_SRC_CPU); return; } if (extra & 0x800) { @@ -1120,7 +1227,7 @@ void m68k_divl (uae_u32 opcode, uae_u32 } #else if (src == 0) { - Exception (5, oldpc); + Exception (5, oldpc,M68000_EXCEPTION_SRC_CPU); return; } if (extra & 0x800) { @@ -1318,17 +1425,17 @@ unsigned long REGPARAM2 op_illg (uae_u32 uaecptr pc = m68k_getpc (); #endif if ((opcode & 0xF000) == 0xF000) { - Exception(0xB,0); + Exception(0xB,0,M68000_EXCEPTION_SRC_CPU); return 4; } if ((opcode & 0xF000) == 0xA000) { - Exception(0xA,0); + Exception(0xA,0,M68000_EXCEPTION_SRC_CPU); return 4; } #if 0 write_log ("Illegal instruction: %04x at %08lx\n", opcode, (long)pc); #endif - Exception (4,0); + Exception (4,0,M68000_EXCEPTION_SRC_CPU); return 4; } @@ -1388,6 +1495,31 @@ static void do_trace (void) /* * Handle special flags */ + +static bool do_specialties_interrupt (int Pending) +{ + /* Check for MFP ints first (level 6) */ + if (regs.spcflags & SPCFLAG_MFP) { + if ( MFP_CheckPendingInterrupts() == TRUE ) + return TRUE; /* MFP exception was generated, no higher interrupt can happen */ + } + + /* No MFP int, check for VBL/HBL ints (levels 4/2) */ + 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 , Pending); /* process the interrupt and add pending jitter if necessary */ + return TRUE; + } + } + + return FALSE; /* no interrupt was found */ +} + + static int do_specialties (void) { if(regs.spcflags & SPCFLAG_BUSERROR) { @@ -1395,7 +1527,7 @@ 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); + Exception(2,0,M68000_EXCEPTION_SRC_CPU); } if(regs.spcflags & SPCFLAG_EXTRA_CYCLES) { @@ -1406,46 +1538,98 @@ static int do_specialties (void) } if (regs.spcflags & SPCFLAG_DOTRACE) { - Exception (9,last_trace_ad); + Exception (9,last_trace_ad,M68000_EXCEPTION_SRC_CPU); } - while (regs.spcflags & SPCFLAG_STOP) { - if (regs.intmask > 5) { - /* We still have to care about events when IPL==7 ! */ - Main_EventHandler(); - if (regs.spcflags & SPCFLAG_BRK) return 1; - } - M68000_AddCycles(4); - if (PendingInterruptCount<=0 && PendingInterruptFunction) { - CALL_VAR(PendingInterruptFunction); - } - if (regs.spcflags & SPCFLAG_MFP) { + + /* Handle the STOP instruction */ + if ( regs.spcflags & SPCFLAG_STOP ) { + /* 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 */ + regs.stopped = 0; + unset_special (SPCFLAG_STOP); + } +#if 0 + if (regs.spcflags & SPCFLAG_MFP) /* MFP int */ MFP_CheckPendingInterrupts(); - } - if (regs.spcflags & (SPCFLAG_INT | SPCFLAG_DOINT)) { + + 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); + 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) { + + /* Take care of quit event if needed */ + if (regs.spcflags & SPCFLAG_BRK) + return 1; + + 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 */ + 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 (regs.spcflags & SPCFLAG_TRACE) do_trace (); // 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 */ + 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); - regs.stopped = 0; + Interrupt (intr , FALSE); /* call Interrupt() with Pending=FALSE, not necessarily true but harmless */ + regs.stopped = 0; /* [NP] useless ? */ } } if (regs.spcflags & SPCFLAG_INT) { @@ -1456,6 +1640,7 @@ static int do_specialties (void) if (regs.spcflags & SPCFLAG_MFP) { /* Check for MFP interrupts */ MFP_CheckPendingInterrupts(); } +#endif if (regs.spcflags & (SPCFLAG_BRK | SPCFLAG_MODE_CHANGE)) { unset_special(SPCFLAG_MODE_CHANGE); @@ -1522,6 +1707,14 @@ static void m68k_run_1 (void) #endif M68000_AddCyclesWithPairing(cycles); + if (regs.spcflags & SPCFLAG_EXTRA_CYCLES) { + /* Add some extra cycles to simulate a wait state */ + unset_special(SPCFLAG_EXTRA_CYCLES); + M68000_AddCycles(nWaitStateCycles); + nWaitStateCycles = 0; + } + +#if 0 while (PendingInterruptCount <= 0 && PendingInterruptFunction) CALL_VAR(PendingInterruptFunction); @@ -1529,6 +1722,30 @@ static void m68k_run_1 (void) 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 (regs.spcflags) { + if (do_specialties ()) + return; + } +#endif + } } @@ -1549,7 +1766,6 @@ static void m68k_run_2 (void) m68k_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 @@ -1562,6 +1778,13 @@ static void m68k_run_2 (void) cycles = (*cpufunctbl[opcode])(opcode); M68000_AddCycles(cycles); + if (regs.spcflags & SPCFLAG_EXTRA_CYCLES) { + /* Add some extra cycles to simulate a wait state */ + unset_special(SPCFLAG_EXTRA_CYCLES); + M68000_AddCycles(nWaitStateCycles); + nWaitStateCycles = 0; + } + while (PendingInterruptCount <= 0 && PendingInterruptFunction) CALL_VAR(PendingInterruptFunction); @@ -1611,13 +1834,13 @@ static void m68k_verify (uaecptr addr, u if (dp->suse) { if (!verify_ea (dp->sreg, dp->smode, dp->size, &val)) { - Exception (3, 0); + Exception (3, 0,M68000_EXCEPTION_SRC_CPU); return; } } if (dp->duse) { if (!verify_ea (dp->dreg, dp->dmode, dp->size, &val)) { - Exception (3, 0); + Exception (3, 0,M68000_EXCEPTION_SRC_CPU); return; } }