|
|
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.10! root 132: /* We don't read the opcode if PC is located in the IO region (rare cases */
! 133: /* used in some games/demos protections) as this can create recursive calls */
! 134: /* (protection of the "Union Demo" runs at $ff8240) */
! 135: if ( ( ( BusErrorPC & 0xffffff ) < 0xff0000 ) || ( ( BusErrorPC & 0xffffff ) > 0xffffff ) )
! 136: Opcode = get_word(BusErrorPC); /* BusErrorPC points to the current opcode */
! 137: else
! 138: Opcode = -1;
1.1.1.6 root 139: //fprintf ( stderr , "opcode=%x\n" , Opcode );
140:
141: /* Assume we use 'move src,dst' : access cycle depends on dst mode */
142: 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 143: AddCycles = CurrentInstrCycles + nWaitStateCycles - 8; /* read is effective before the 8 write cycles for dst */
1.1.1.7 root 144: else if ( OpcodeFamily == i_MVPRM ) /* eg movep.l d0,$ffc3(a1) in E605 (STE) */
1.1.1.9 root 145: AddCycles = 12 + MovepByteNbr * 4; /* [NP] FIXME, it works with E605 but gives 20-32 cycles instead of 16-28 */
1.1.1.7 root 146: /* something must be wrong in video.c */
1.1.1.6 root 147: else
1.1.1.9 root 148: AddCycles = CurrentInstrCycles + nWaitStateCycles; /* assume dest is reg : read is effective at the end of the instr */
1.1.1.4 root 149: }
1.1 root 150:
1.1.1.9 root 151: return AddCycles;
1.1 root 152: }
153:
154:
1.1.1.9 root 155:
1.1 root 156: /*-----------------------------------------------------------------------*/
1.1.1.2 root 157: /**
1.1.1.9 root 158: * Compute the cycles where a write actually happens inside a specific
159: * instruction type. We use some common cases, this should be handled more
160: * accurately in the cpu emulation for each opcode.
1.1.1.2 root 161: */
1.1.1.9 root 162: static int Cycles_GetInternalCycleOnWriteAccess(void)
1.1 root 163: {
1.1.1.9 root 164: int AddCycles;
1.1 root 165:
1.1.1.4 root 166: if ( BusMode == BUS_MODE_BLITTER )
167: {
1.1.1.9 root 168: AddCycles = 4 + nWaitStateCycles;
1.1.1.4 root 169: }
170: else /* BUS_MODE_CPU */
171: {
172: /* TODO: Find proper cycles count depending on the type of the current instruction */
173: /* (e.g. movem is not correctly handled) */
1.1.1.9 root 174: AddCycles = CurrentInstrCycles + nWaitStateCycles;
1.1.1.4 root 175:
1.1.1.10! root 176: if ( ( OpcodeFamily == i_CLR ) || ( OpcodeFamily == i_NEG ) || ( OpcodeFamily == i_NEGX ) || ( OpcodeFamily == i_NOT ) )
! 177: ; /* Do nothing, the write is done during the last 4 cycles */
! 178: /* (e.g i_CLR for bottom border removal in No Scroll / Delirious Demo 4) */
! 179:
! 180: else if ( ( OpcodeFamily == i_ADD ) || ( OpcodeFamily == i_SUB ) )
! 181: ; /* Do nothing, the write is done during the last 4 cycles */
! 182: /* (eg 'add d1,(a0)' in rasters.prg by TOS Crew */
! 183:
! 184: else if ( ( OpcodeFamily == i_AND ) || ( OpcodeFamily == i_OR ) || ( OpcodeFamily == i_EOR ) )
1.1.1.7 root 185: ; /* Do nothing, the write is done during the last 4 cycles */
1.1.1.8 root 186:
187: else if ( ( OpcodeFamily == i_BCHG ) || ( OpcodeFamily == i_BCLR ) || ( OpcodeFamily == i_BSET ) )
188: ; /* Do nothing, the write is done during the last 4 cycles */
189:
1.1.1.7 root 190: else
191: {
192: /* assume the behaviour of a 'move' (since this is the most */
193: /* common instr used when requiring cycle precise writes) */
1.1.1.9 root 194: if ( AddCycles >= 8 )
195: AddCycles -= 4; /* last 4 cycles are for prefetch */
1.1.1.7 root 196: }
1.1.1.4 root 197: }
1.1 root 198:
1.1.1.9 root 199: return AddCycles;
200: }
201:
202:
203: /*-----------------------------------------------------------------------*/
204: /**
205: * Read a counter on CPU memory read access by taking care of the instruction
206: * type (add the needed amount of additional cycles).
207: */
208: int Cycles_GetCounterOnReadAccess(int nId)
209: {
210: int AddCycles;
211:
212: AddCycles = Cycles_GetInternalCycleOnReadAccess();
213:
214: return Cycles_GetCounter(nId) + AddCycles;
215: }
216:
217:
218: /*-----------------------------------------------------------------------*/
219: /**
220: * Read a counter on CPU memory write access by taking care of the instruction
221: * type (add the needed amount of additional cycles).
222: */
223: int Cycles_GetCounterOnWriteAccess(int nId)
224: {
225: int AddCycles;
226:
227: AddCycles = Cycles_GetInternalCycleOnWriteAccess();
228:
229: return Cycles_GetCounter(nId) + AddCycles;
1.1 root 230: }
1.1.1.9 root 231:
232:
233: /*-----------------------------------------------------------------------*/
234: /**
235: * Read the main clock counter on CPU memory read access by taking care of the instruction
236: * type (add the needed amount of additional cycles).
237: */
238: Uint64 Cycles_GetClockCounterOnReadAccess(void)
239: {
240: int AddCycles;
241:
242: AddCycles = Cycles_GetInternalCycleOnReadAccess();
243:
244: return CyclesGlobalClockCounter + AddCycles;
245: }
246:
247:
248: /*-----------------------------------------------------------------------*/
249: /**
250: * Read the main clock counter on CPU memory write access by taking care of the instruction
251: * type (add the needed amount of additional cycles).
252: */
253: Uint64 Cycles_GetClockCounterOnWriteAccess(void)
254: {
255: int AddCycles;
256:
257: AddCycles = Cycles_GetInternalCycleOnWriteAccess();
258:
259: return CyclesGlobalClockCounter + AddCycles;
260: }
261:
262:
263:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.