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

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:        {
1.1.1.11! root      130:                /* TODO: Find proper cycles count depending on the opcode/family of the current instruction */
1.1.1.4   root      131:                /* (e.g. movem is not correctly handled) */
1.1.1.11! root      132:                Opcode = M68000_CurrentOpcode;
1.1.1.6   root      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.11! root      141:                        /* FIXME : this should be : AddCycles = 4 + MovepByteNbr * 4, but this breaks e605 in video.c */
1.1.1.6   root      142:                else
1.1.1.9   root      143:                        AddCycles = CurrentInstrCycles + nWaitStateCycles;              /* assume dest is reg : read is effective at the end of the instr */
1.1.1.4   root      144:        }
1.1       root      145: 
1.1.1.9   root      146:        return AddCycles;
1.1       root      147: }
                    148: 
                    149: 
1.1.1.9   root      150: 
1.1       root      151: /*-----------------------------------------------------------------------*/
1.1.1.2   root      152: /**
1.1.1.9   root      153:  * Compute the cycles where a write actually happens inside a specific
                    154:  * instruction type. We use some common cases, this should be handled more
                    155:  * accurately in the cpu emulation for each opcode.
1.1.1.2   root      156:  */
1.1.1.9   root      157: static int Cycles_GetInternalCycleOnWriteAccess(void)
1.1       root      158: {
1.1.1.9   root      159:        int AddCycles;
1.1       root      160: 
1.1.1.4   root      161:        if ( BusMode == BUS_MODE_BLITTER )
                    162:        {
1.1.1.9   root      163:                AddCycles = 4 + nWaitStateCycles;
1.1.1.4   root      164:        }
                    165:        else                                                    /* BUS_MODE_CPU */
                    166:        {
                    167:                /* TODO: Find proper cycles count depending on the type of the current instruction */
                    168:                /* (e.g. movem is not correctly handled) */
1.1.1.9   root      169:                AddCycles = CurrentInstrCycles + nWaitStateCycles;
1.1.1.4   root      170: 
1.1.1.10  root      171:                if ( ( OpcodeFamily == i_CLR ) || ( OpcodeFamily == i_NEG ) || ( OpcodeFamily == i_NEGX ) || ( OpcodeFamily == i_NOT ) )
                    172:                        ;                                               /* Do nothing, the write is done during the last 4 cycles */
                    173:                                                                        /* (e.g i_CLR for bottom border removal in No Scroll / Delirious Demo 4) */
                    174: 
                    175:                else if ( ( OpcodeFamily == i_ADD ) || ( OpcodeFamily == i_SUB ) )
                    176:                        ;                                               /* Do nothing, the write is done during the last 4 cycles */
                    177:                                                                        /* (eg 'add d1,(a0)' in rasters.prg by TOS Crew */
                    178: 
                    179:                else if ( ( OpcodeFamily == i_AND ) || ( OpcodeFamily == i_OR ) || ( OpcodeFamily == i_EOR ) )
1.1.1.7   root      180:                        ;                                               /* Do nothing, the write is done during the last 4 cycles */
1.1.1.8   root      181: 
                    182:                else if ( ( OpcodeFamily == i_BCHG ) || ( OpcodeFamily == i_BCLR ) || ( OpcodeFamily == i_BSET ) )
                    183:                        ;                                               /* Do nothing, the write is done during the last 4 cycles */
                    184: 
1.1.1.7   root      185:                else
                    186:                {
                    187:                        /* assume the behaviour of a 'move' (since this is the most */
                    188:                        /* common instr used when requiring cycle precise writes) */
1.1.1.9   root      189:                        if ( AddCycles >= 8 )
                    190:                                AddCycles -= 4;                 /* last 4 cycles are for prefetch */
1.1.1.7   root      191:                }
1.1.1.4   root      192:        }
1.1       root      193: 
1.1.1.9   root      194:        return AddCycles;
                    195: }
                    196: 
                    197: 
                    198: /*-----------------------------------------------------------------------*/
                    199: /**
                    200:  * Read a counter on CPU memory read access by taking care of the instruction
                    201:  * type (add the needed amount of additional cycles).
                    202:  */
                    203: int Cycles_GetCounterOnReadAccess(int nId)
                    204: {
                    205:        int AddCycles;
                    206: 
                    207:        AddCycles = Cycles_GetInternalCycleOnReadAccess();
                    208: 
                    209:        return Cycles_GetCounter(nId) + AddCycles;
                    210: }
                    211: 
                    212: 
                    213: /*-----------------------------------------------------------------------*/
                    214: /**
                    215:  * Read a counter on CPU memory write access by taking care of the instruction
                    216:  * type (add the needed amount of additional cycles).
                    217:  */
                    218: int Cycles_GetCounterOnWriteAccess(int nId)
                    219: {
                    220:        int AddCycles;
                    221: 
                    222:        AddCycles = Cycles_GetInternalCycleOnWriteAccess();
                    223: 
                    224:        return Cycles_GetCounter(nId) + AddCycles;
1.1       root      225: }
1.1.1.9   root      226: 
                    227: 
                    228: /*-----------------------------------------------------------------------*/
                    229: /**
                    230:  * Read the main clock counter on CPU memory read access by taking care of the instruction
                    231:  * type (add the needed amount of additional cycles).
                    232:  */
                    233: Uint64 Cycles_GetClockCounterOnReadAccess(void)
                    234: {
                    235:        int AddCycles;
                    236: 
                    237:        AddCycles = Cycles_GetInternalCycleOnReadAccess();
                    238: 
                    239:        return CyclesGlobalClockCounter + AddCycles;
                    240: }
                    241: 
                    242: 
                    243: /*-----------------------------------------------------------------------*/
                    244: /**
                    245:  * Read the main clock counter on CPU memory write access by taking care of the instruction
                    246:  * type (add the needed amount of additional cycles).
                    247:  */
                    248: Uint64 Cycles_GetClockCounterOnWriteAccess(void)
                    249: {
                    250:        int AddCycles;
                    251: 
                    252:        AddCycles = Cycles_GetInternalCycleOnWriteAccess();
                    253: 
                    254:        return CyclesGlobalClockCounter + AddCycles;
                    255: }
                    256: 
                    257: 
                    258: 

unix.superglobalmegacorp.com

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