浏览代码

cz80, improve cycle accounting

kub 3 年之前
父节点
当前提交
85894ad406
共有 4 个文件被更改,包括 46 次插入45 次删除
  1. 10 6
      cpu/cz80/cz80.c
  2. 3 6
      cpu/cz80/cz80_op.c
  3. 32 32
      cpu/cz80/cz80macro.h
  4. 1 1
      pico/pico_int.h

+ 10 - 6
cpu/cz80/cz80.c

@@ -275,23 +275,22 @@ Cz80_Check_Interrupt:
 			if (CPU->IRQState != CLEAR_LINE)
 			{
 				CHECK_INT
-				CPU->ICount -= CPU->ExtraCycles;
-				CPU->ExtraCycles = 0;
 			}
+			CPU->ICount -= CPU->ExtraCycles;
+			CPU->ExtraCycles = 0;
 			if (!CPU->HaltState)
 				goto Cz80_Exec;
 		}
 	}
-	else CPU->ICount = 0;
 
 Cz80_Exec_End:
 	CPU->PC = PC;
 #if CZ80_ENCRYPTED_ROM
 	CPU->OPBase = OPBase;
 #endif
-	if (CPU->HaltState)
-		CPU->ICount = 0;
-	cycles -= CPU->ICount;
+	if (!(CPU->HaltState && CPU->ICount > 0))
+		cycles -= CPU->ICount;
+	CPU->ICount = 0;
 #if !CZ80_EMULATE_R_EXACTLY
 	zR = (zR + (cycles >> 2)) & 0x7f;
 #endif
@@ -333,6 +332,11 @@ void Cz80_Set_IRQ(cz80_struc *CPU, INT32 line, INT32 state)
 #endif
 		}
 	}
+	if (CPU->ICount > 0)
+	{
+		CPU->ICount -= CPU->ExtraCycles;
+		CPU->ExtraCycles  = 0;
+	}
 }
 
 

+ 3 - 6
cpu/cz80/cz80_op.c

@@ -709,12 +709,9 @@ OP_EI:
 				zR++;
 #endif
 			}
-			if (CPU->IRQState)
-			{
-				afterEI = 1;
-				CPU->ExtraCycles += 1 - CPU->ICount;
-				CPU->ICount = 1;
-			}
+			afterEI = 1;
+			CPU->ExtraCycles += 1 - CPU->ICount;
+			CPU->ICount = 1;
 		}
 		else zIFF2 = (1 << 2);
 		goto Cz80_Exec_nocheck;

+ 32 - 32
cpu/cz80/cz80macro.h

@@ -94,36 +94,36 @@
 #define IN(A)				CPU->IN_Port(A)
 #define OUT(A, D)			CPU->OUT_Port(A, D)
 
-#define CHECK_INT													\
-	if (zIFF1)														\
-	{																\
-		UINT32 IntVect;												\
-																	\
-		if (CPU->IRQState == HOLD_LINE)								\
-			CPU->IRQState = CLEAR_LINE;								\
-																	\
-		CPU->HaltState = 0;											\
-		zIFF1 = zIFF2 = 0;											\
-		IntVect = CPU->Interrupt_Callback(CPU->IRQLine);			\
-																	\
-		PUSH_16(zRealPC)											\
-																	\
-		if (zIM == 2)												\
-		{															\
-			IntVect = (IntVect & 0xff) | (zI << 8);					\
-			PC = READ_MEM16(IntVect);								\
-			CPU->ExtraCycles += 17;									\
-		}															\
-		else if (zIM == 1)											\
-		{															\
-			PC = 0x38;												\
-			CPU->ExtraCycles += 13;									\
-		}															\
-		else														\
-		{															\
-			PC = IntVect & 0x38;									\
-			CPU->ExtraCycles += 13;									\
-		}															\
-																	\
-		SET_PC(PC)													\
+#define CHECK_INT							\
+	if (zIFF1)							\
+	{								\
+		UINT32 IntVect;						\
+									\
+		if (CPU->IRQState == HOLD_LINE)				\
+			CPU->IRQState = CLEAR_LINE;			\
+									\
+		CPU->HaltState = 0;					\
+		zIFF1 = zIFF2 = 0;					\
+		IntVect = CPU->Interrupt_Callback(CPU->IRQLine);	\
+									\
+		PUSH_16(zRealPC)					\
+									\
+		if (zIM == 2)						\
+		{							\
+			IntVect = (IntVect & 0xff) | (zI << 8);		\
+			PC = READ_MEM16(IntVect);			\
+			CPU->ExtraCycles += 17;				\
+		}							\
+		else if (zIM == 1)					\
+		{							\
+			PC = 0x38;					\
+			CPU->ExtraCycles += 13;				\
+		}							\
+		else							\
+		{							\
+			PC = IntVect & 0x38;				\
+			CPU->ExtraCycles += 13;				\
+		}							\
+									\
+		SET_PC(PC)						\
 	}

+ 1 - 1
pico/pico_int.h

@@ -183,7 +183,7 @@ extern struct DrZ80 drZ80;
 #define z80_int_assert(a)  Cz80_Set_IRQ(&CZ80, 0, (a) ? ASSERT_LINE : CLEAR_LINE)
 #define z80_nmi()          Cz80_Set_IRQ(&CZ80, IRQ_LINE_NMI, 0)
 
-#define z80_cyclesLeft     CZ80.ICount
+#define z80_cyclesLeft     (CZ80.ICount - CZ80.ExtraCycles)
 #define z80_subCLeft(c)    CZ80.ICount -= c
 #define z80_pc()           Cz80_Get_Reg(&CZ80, CZ80_PC)