Annotation of hatari/src/cycles.c, revision 1.1.1.9

1.1       root        1: /*
                      2:   Hatari - cycles.c
                      3: 
1.1.1.9 ! root        4:   This file is distributed under the GNU General Public License, version 2
        !             5:   or at your option any later version. Read the file gpl.txt for details.
1.1       root        6: 
                      7:   Here we take care of cycle counters. For performance reasons we don't increase
                      8:   all counters after each 68k instruction, but only one main counter.
                      9:   When we need to read one of the normal counters (currently only for video
                     10:   and sound cycles), we simply update these counters with the main counter
                     11:   before returning the current counter value.
                     12: */
1.1.1.2   root       13: 
                     14: 
                     15: /* 2007/03/xx  [NP]    Use 'CurrentInstrCycles' to get a good approximation for        */
                     16: /*                     Cycles_GetCounterOnReadAccess and Cycles_GetCounterOnWriteAccess*/
                     17: /*                     (this should work correctly with 'move' instruction).           */
1.1.1.3   root       18: /* 2008/04/14  [NP]    Take nWaitStateCycles into account when computing the value of  */
                     19: /*                     Cycles_GetCounterOnReadAccess and Cycles_GetCounterOnWriteAccess*/
1.1.1.4   root       20: /* 2008/12/21  [NP]    Use BusMode to adjust Cycles_GetCounterOnReadAccess and         */
                     21: /*                     Cycles_GetCounterOnWriteAccess depending on who is owning the   */
                     22: /*                     bus (cpu, blitter).                                             */
1.1.1.6   root       23: /* 2011/03/26  [NP]    In Cycles_GetCounterOnReadAccess, add a special case for opcode */
                     24: /*                     $11f8 'move.b xxx.w,xxx.w' (fix MOVE.B $ffff8209.w,$26.w in     */
                     25: /*                     'Bird Mad Girl Show' demo's loader/protection)                  */
1.1.1.9 ! root       26: /* 2012/08/19  [NP]    Add a global counter CyclesGlobalClockCounter to count cycles   */
        !            27: /*                     since the last reset.                                           */
1.1.1.2   root       28: 
                     29: 
1.1.1.4   root       30: const char Cycles_fileid[] = "Hatari cycles.c : " __DATE__ " " __TIME__;
1.1       root       31: 
                     32: #include "main.h"
1.1.1.3   root       33: #include "m68000.h"
1.1.1.5   root       34: #include "memorySnapShot.h"
1.1       root       35: #include "cycles.h"
                     36: 
                     37: 
1.1.1.9 ! root       38: int    nCyclesMainCounter;                     /* Main cycles counter since previous Cycles_UpdateCounters() */
1.1       root       39: 
1.1.1.5   root       40: static int nCyclesCounter[CYCLES_COUNTER_MAX]; /* Array with all counters */
1.1       root       41: 
1.1.1.9 ! root       42: Uint64 CyclesGlobalClockCounter = 0;           /* Global clock counter since starting Hatari (it's never reset afterwards) */
1.1.1.7   root       43: 
1.1.1.9 ! root       44: int    CurrentInstrCycles;
        !            45: int    MovepByteNbr = 0;                       /* Number of the byte currently transferred in a movep (1..2 or 1..4) */
        !            46:                                                /* 0 means current instruction is not a movep */
        !            47: 
        !            48: 
        !            49: static void    Cycles_UpdateCounters(void);
        !            50: static int     Cycles_GetInternalCycleOnReadAccess(void);
        !            51: static int     Cycles_GetInternalCycleOnWriteAccess(void);
1.1.1.2   root       52: 
1.1       root       53: 
1.1.1.5   root       54: 
                     55: /*-----------------------------------------------------------------------*/
                     56: /**
                     57:  * Save/Restore snapshot of local variables ('MemorySnapShot_Store' handles type)
                     58:  */
                     59: void Cycles_MemorySnapShot_Capture(bool bSave)
                     60: {
                     61:        /* Save/Restore details */
                     62:        MemorySnapShot_Store(&nCyclesMainCounter, sizeof(nCyclesMainCounter));
                     63:        MemorySnapShot_Store(nCyclesCounter, sizeof(nCyclesCounter));
1.1.1.9 ! root       64:        MemorySnapShot_Store(&CyclesGlobalClockCounter, sizeof(CyclesGlobalClockCounter));
1.1.1.5   root       65:        MemorySnapShot_Store(&CurrentInstrCycles, sizeof(CurrentInstrCycles));
                     66: }
                     67: 
                     68: 
1.1       root       69: /*-----------------------------------------------------------------------*/
1.1.1.2   root       70: /**
                     71:  * Update all cycles counters with the current value of nCyclesMainCounter.
                     72:  */
1.1       root       73: static void Cycles_UpdateCounters(void)
                     74: {
                     75:        int i;
                     76: 
                     77:        for (i = 0; i < CYCLES_COUNTER_MAX; i++)
                     78:        {
                     79:                nCyclesCounter[i] += nCyclesMainCounter;
                     80:        }
                     81: 
                     82:        nCyclesMainCounter = 0;
                     83: }
                     84: 
                     85: 
                     86: /*-----------------------------------------------------------------------*/
1.1.1.2   root       87: /**
                     88:  * Set a counter to a new value.
                     89:  */
1.1       root       90: void Cycles_SetCounter(int nId, int nValue)
                     91: {
                     92:        /* Update counters first (nCyclesMainCounter must be 0 afterwards) */
                     93:        Cycles_UpdateCounters();
                     94: 
                     95:        /* Now set the new value: */
                     96:        nCyclesCounter[nId] = nValue;
                     97: }
                     98: 
                     99: 
                    100: /*-----------------------------------------------------------------------*/
1.1.1.2   root      101: /**
                    102:  * Read a counter.
                    103:  */
1.1       root      104: int Cycles_GetCounter(int nId)
                    105: {
                    106:        /* Update counters first so we read an up-to-date value */
                    107:        Cycles_UpdateCounters();
                    108: 
                    109:        return nCyclesCounter[nId];
                    110: }
                    111: 
                    112: 
                    113: /*-----------------------------------------------------------------------*/
1.1.1.2   root      114: /**
1.1.1.9 ! root      115:  * Compute the cycles where a read actually happens inside a specific
        !           116:  * instruction type. We use some common cases, this should be handled more
        !           117:  * accurately in the cpu emulation for each opcode.
1.1.1.2   root      118:  */
1.1.1.9 ! root      119: static int Cycles_GetInternalCycleOnReadAccess(void)
1.1       root      120: {
1.1.1.9 ! root      121:        int AddCycles;
1.1.1.6   root      122:        int Opcode;
1.1       root      123: 
1.1.1.4   root      124:        if ( BusMode == BUS_MODE_BLITTER )
                    125:        {
1.1.1.9 ! root      126:                AddCycles = 4 + nWaitStateCycles;
1.1.1.4   root      127:        }
                    128:        else                                                    /* BUS_MODE_CPU */
                    129:        {
                    130:                /* TODO: Find proper cycles count depending on the type of the current instruction */
                    131:                /* (e.g. movem is not correctly handled) */
1.1.1.6   root      132:                Opcode = get_word(BusErrorPC);
                    133:                //fprintf ( stderr , "opcode=%x\n" , Opcode );
                    134: 
                    135:                /* Assume we use 'move src,dst' : access cycle depends on dst mode */
                    136:                if ( Opcode == 0x11f8 )                         /* move.b xxx.w,xxx.w (eg MOVE.B $ffff8209.w,$26.w in Bird Mad Girl Show) */
1.1.1.9 ! root      137:                        AddCycles = CurrentInstrCycles + nWaitStateCycles - 8;          /* read is effective before the 8 write cycles for dst */
1.1.1.7   root      138:                else if ( OpcodeFamily == i_MVPRM )                                     /* eg movep.l d0,$ffc3(a1) in E605 (STE) */
1.1.1.9 ! root      139:                        AddCycles = 12 + MovepByteNbr * 4;                              /* [NP] FIXME, it works with E605 but gives 20-32 cycles instead of 16-28 */
1.1.1.7   root      140:                                                                                        /* something must be wrong in video.c */
1.1.1.6   root      141:                else
1.1.1.9 ! root      142:                        AddCycles = CurrentInstrCycles + nWaitStateCycles;              /* assume dest is reg : read is effective at the end of the instr */
1.1.1.4   root      143:        }
1.1       root      144: 
1.1.1.9 ! root      145:        return AddCycles;
1.1       root      146: }
                    147: 
                    148: 
1.1.1.9 ! root      149: 
1.1       root      150: /*-----------------------------------------------------------------------*/
1.1.1.2   root      151: /**
1.1.1.9 ! root      152:  * Compute the cycles where a write actually happens inside a specific
        !           153:  * instruction type. We use some common cases, this should be handled more
        !           154:  * accurately in the cpu emulation for each opcode.
1.1.1.2   root      155:  */
1.1.1.9 ! root      156: static int Cycles_GetInternalCycleOnWriteAccess(void)
1.1       root      157: {
1.1.1.9 ! root      158:        int AddCycles;
1.1       root      159: 
1.1.1.4   root      160:        if ( BusMode == BUS_MODE_BLITTER )
                    161:        {
1.1.1.9 ! root      162:                AddCycles = 4 + nWaitStateCycles;
1.1.1.4   root      163:        }
                    164:        else                                                    /* BUS_MODE_CPU */
                    165:        {
                    166:                /* TODO: Find proper cycles count depending on the type of the current instruction */
                    167:                /* (e.g. movem is not correctly handled) */
1.1.1.9 ! root      168:                AddCycles = CurrentInstrCycles + nWaitStateCycles;
1.1.1.4   root      169: 
1.1.1.7   root      170:                if ( OpcodeFamily == i_CLR )                            /* should also be the case for add, sub, and, or, eor, neg, not */
                    171:                        ;                                               /* Do nothing, the write is done during the last 4 cycles */
                    172:                                                                        /* (e.g bottom border removal in No Scroll / Delirious Demo 4) */
1.1.1.8   root      173: 
                    174:                else if ( ( OpcodeFamily == i_BCHG ) || ( OpcodeFamily == i_BCLR ) || ( OpcodeFamily == i_BSET ) )
                    175:                        ;                                               /* Do nothing, the write is done during the last 4 cycles */
                    176: 
1.1.1.7   root      177:                else
                    178:                {
                    179:                        /* assume the behaviour of a 'move' (since this is the most */
                    180:                        /* common instr used when requiring cycle precise writes) */
1.1.1.9 ! root      181:                        if ( AddCycles >= 8 )
        !           182:                                AddCycles -= 4;                 /* last 4 cycles are for prefetch */
1.1.1.7   root      183:                }
1.1.1.4   root      184:        }
1.1       root      185: 
1.1.1.9 ! root      186:        return AddCycles;
        !           187: }
        !           188: 
        !           189: 
        !           190: /*-----------------------------------------------------------------------*/
        !           191: /**
        !           192:  * Read a counter on CPU memory read access by taking care of the instruction
        !           193:  * type (add the needed amount of additional cycles).
        !           194:  */
        !           195: int Cycles_GetCounterOnReadAccess(int nId)
        !           196: {
        !           197:        int AddCycles;
        !           198: 
        !           199:        AddCycles = Cycles_GetInternalCycleOnReadAccess();
        !           200: 
        !           201:        return Cycles_GetCounter(nId) + AddCycles;
        !           202: }
        !           203: 
        !           204: 
        !           205: /*-----------------------------------------------------------------------*/
        !           206: /**
        !           207:  * Read a counter on CPU memory write access by taking care of the instruction
        !           208:  * type (add the needed amount of additional cycles).
        !           209:  */
        !           210: int Cycles_GetCounterOnWriteAccess(int nId)
        !           211: {
        !           212:        int AddCycles;
        !           213: 
        !           214:        AddCycles = Cycles_GetInternalCycleOnWriteAccess();
        !           215: 
        !           216:        return Cycles_GetCounter(nId) + AddCycles;
1.1       root      217: }
1.1.1.9 ! root      218: 
        !           219: 
        !           220: /*-----------------------------------------------------------------------*/
        !           221: /**
        !           222:  * Read the main clock counter on CPU memory read access by taking care of the instruction
        !           223:  * type (add the needed amount of additional cycles).
        !           224:  */
        !           225: Uint64 Cycles_GetClockCounterOnReadAccess(void)
        !           226: {
        !           227:        int AddCycles;
        !           228: 
        !           229:        AddCycles = Cycles_GetInternalCycleOnReadAccess();
        !           230: 
        !           231:        return CyclesGlobalClockCounter + AddCycles;
        !           232: }
        !           233: 
        !           234: 
        !           235: /*-----------------------------------------------------------------------*/
        !           236: /**
        !           237:  * Read the main clock counter on CPU memory write access by taking care of the instruction
        !           238:  * type (add the needed amount of additional cycles).
        !           239:  */
        !           240: Uint64 Cycles_GetClockCounterOnWriteAccess(void)
        !           241: {
        !           242:        int AddCycles;
        !           243: 
        !           244:        AddCycles = Cycles_GetInternalCycleOnWriteAccess();
        !           245: 
        !           246:        return CyclesGlobalClockCounter + AddCycles;
        !           247: }
        !           248: 
        !           249: 
        !           250: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.