--- hatari/src/uae-cpu/newcpu.c 2019/04/01 07:14:57 1.1.1.13 +++ hatari/src/uae-cpu/newcpu.c 2019/04/09 08:47:24 1.1.1.15 @@ -75,14 +75,42 @@ /* 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 bytes if no MFP exception */ +/* 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.13 2019/04/01 07:14:57 root Exp $"; +const char NewCpu_fileid[] = "Hatari newcpu.c : " __DATE__ " " __TIME__; #include "sysdeps.h" #include "hatari-glue.h" @@ -98,10 +126,12 @@ const char NewCpu_rcsid[] = "Hatari $Id: #include "../includes/vdi.h" #include "../includes/cart.h" #include "../includes/debugui.h" +#include "../includes/dialog.h" #include "../includes/bios.h" #include "../includes/xbios.h" #include "../includes/video.h" #include "../includes/options.h" +#include "../falcon/dsp.h" //#define DEBUG_PREFETCH @@ -770,6 +800,37 @@ static void exception_trace (int nr) } +/* + * 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. */ @@ -868,8 +929,8 @@ 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, 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) && (ExceptionSource != M68000_EXCEPTION_SRC_INT_MFP) ) { @@ -885,7 +946,7 @@ void Exception(int nr, uaecptr oldpc, in 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) { + if (bExceptionDebugging) { fprintf(stderr,"Address Error at address $%x, PC=$%x\n",last_fault_for_exception_3,currpc); DebugUI(); } @@ -907,13 +968,15 @@ void Exception(int nr, uaecptr oldpc, in fprintf(stderr, "Detected double bus error at address $%x, PC=$%lx => CPU halted!\n", BusErrorAddress, (long)currpc); unset_special(SPCFLAG_BUSERROR); - if (bEnableDebug) + if (bExceptionDebugging) DebugUI(); + else + DlgAlert_Notice("Detected double bus error => CPU halted!\nEmulation needs to be reseted.\n"); regs.intmask = 7; - m68k_setstopped(TRUE); + m68k_setstopped(true); return; } - if (bEnableDebug && BusErrorAddress!=0xff8a00) { + if (bExceptionDebugging && BusErrorAddress!=0xff8a00) { fprintf(stderr,"Bus Error at address $%x, PC=$%lx\n", BusErrorAddress, (long)currpc); DebugUI(); } @@ -921,7 +984,7 @@ void Exception(int nr, uaecptr oldpc, in } /* Set PC and flags */ - if (bEnableDebug && get_long (regs.vbr + 4*nr) == 0) { + if (bExceptionDebugging && get_long (regs.vbr + 4*nr) == 0) { write_log("Uninitialized exception handler #%i!\n", nr); DebugUI(); } @@ -937,7 +1000,13 @@ void Exception(int nr, uaecptr oldpc, in } else if (nr >= 24 && nr <= 31) { - if ( ( nr == 26 ) || ( nr == 28 ) ) /* HBL or VBL */ + if ( nr == 26 ) /* HBL */ + { + /* store current cycle pos when then interrupt was received (see video.c) */ + LastCycleHblException = Cycles_GetCounter(CYCLES_COUNTER_VIDEO); + M68000_AddCycles(44+12); /* Video Interrupt */ + } + else if ( nr == 28 ) /* VBL */ M68000_AddCycles(44+12); /* Video Interrupt */ else M68000_AddCycles(44+4); /* Other Interrupts */ @@ -966,10 +1035,11 @@ void Exception(int nr, uaecptr oldpc, in 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;*/ @@ -982,6 +1052,9 @@ static void Interrupt(int nr) regs.intmask = nr; set_special (SPCFLAG_INT); + + /* Handle Atari ST's specific jitter for hbl/vbl */ + InterruptAddJitter ( nr , Pending ); } @@ -1430,6 +1503,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) { @@ -1451,50 +1549,95 @@ static int do_specialties (void) 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); - - /* 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 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); + + /* 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)) { /* 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) { + + /* 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; - } + 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) { @@ -1505,6 +1648,10 @@ static int do_specialties (void) if (regs.spcflags & SPCFLAG_MFP) { /* Check for MFP interrupts */ MFP_CheckPendingInterrupts(); } +#endif + + if (regs.spcflags & SPCFLAG_DEBUGGER) + DebugUI_CpuCheck(); if (regs.spcflags & (SPCFLAG_BRK | SPCFLAG_MODE_CHANGE)) { unset_special(SPCFLAG_MODE_CHANGE); @@ -1539,13 +1686,15 @@ static void m68k_run_1 (void) #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 ); + 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 ); m68k_disasm(stderr, m68k_getpc (), NULL, 1); - } + } /* assert (!regs.stopped && !(regs.spcflags & SPCFLAG_STOP)); */ /* regs_backup[backup_pointer = (backup_pointer + 1) % 16] = regs;*/ @@ -1578,13 +1727,37 @@ static void m68k_run_1 (void) nWaitStateCycles = 0; } - while (PendingInterruptCount <= 0 && PendingInterruptFunction) +#if 0 + while (PendingInterruptCount <= 0 && PendingInterruptFunction) CALL_VAR(PendingInterruptFunction); +#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 + } +#endif if (regs.spcflags) { if (do_specialties ()) return; } + + /* Run DSP 56k code if necessary */ + if (bDspEnabled) { + DSP_Run(cycles); + } } } @@ -1597,13 +1770,15 @@ static void m68k_run_2 (void) uae_u32 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 ); + 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 ); m68k_disasm(stderr, m68k_getpc (), NULL, 1); - } + } /* assert (!regs.stopped && !(regs.spcflags & SPCFLAG_STOP)); */ /* regs_backup[backup_pointer = (backup_pointer + 1) % 16] = regs;*/ @@ -1631,6 +1806,11 @@ static void m68k_run_2 (void) if (do_specialties ()) return; } + + /* Run DSP 56k code if necessary */ + if (bDspEnabled) { + DSP_Run(cycles); + } } }