cz80, improve cycle accounting
authorkub <derkub@gmail.com>
Mon, 14 Dec 2020 20:06:24 +0000 (21:06 +0100)
committerkub <derkub@gmail.com>
Mon, 14 Dec 2020 20:06:24 +0000 (21:06 +0100)
cpu/cz80/cz80.c
cpu/cz80/cz80_op.c
cpu/cz80/cz80macro.h
pico/pico_int.h

index 1a3a676..7f432bd 100644 (file)
@@ -275,23 +275,22 @@ Cz80_Check_Interrupt:
                        if (CPU->IRQState != CLEAR_LINE)\r
                        {\r
                                CHECK_INT\r
-                               CPU->ICount -= CPU->ExtraCycles;\r
-                               CPU->ExtraCycles = 0;\r
                        }\r
+                       CPU->ICount -= CPU->ExtraCycles;\r
+                       CPU->ExtraCycles = 0;\r
                        if (!CPU->HaltState)\r
                                goto Cz80_Exec;\r
                }\r
        }\r
-       else CPU->ICount = 0;\r
 \r
 Cz80_Exec_End:\r
        CPU->PC = PC;\r
 #if CZ80_ENCRYPTED_ROM\r
        CPU->OPBase = OPBase;\r
 #endif\r
-       if (CPU->HaltState)\r
-               CPU->ICount = 0;\r
-       cycles -= CPU->ICount;\r
+       if (!(CPU->HaltState && CPU->ICount > 0))\r
+               cycles -= CPU->ICount;\r
+       CPU->ICount = 0;\r
 #if !CZ80_EMULATE_R_EXACTLY\r
        zR = (zR + (cycles >> 2)) & 0x7f;\r
 #endif\r
@@ -333,6 +332,11 @@ void Cz80_Set_IRQ(cz80_struc *CPU, INT32 line, INT32 state)
 #endif\r
                }\r
        }\r
+       if (CPU->ICount > 0)\r
+       {\r
+               CPU->ICount -= CPU->ExtraCycles;\r
+               CPU->ExtraCycles  = 0;\r
+       }\r
 }\r
 \r
 \r
index b152008..60810f1 100644 (file)
@@ -709,12 +709,9 @@ OP_EI:
                                zR++;\r
 #endif\r
                        }\r
-                       if (CPU->IRQState)\r
-                       {\r
-                               afterEI = 1;\r
-                               CPU->ExtraCycles += 1 - CPU->ICount;\r
-                               CPU->ICount = 1;\r
-                       }\r
+                       afterEI = 1;\r
+                       CPU->ExtraCycles += 1 - CPU->ICount;\r
+                       CPU->ICount = 1;\r
                }\r
                else zIFF2 = (1 << 2);\r
                goto Cz80_Exec_nocheck;\r
index 2e21d40..36fdacb 100644 (file)
 #define IN(A)                          CPU->IN_Port(A)\r
 #define OUT(A, D)                      CPU->OUT_Port(A, D)\r
 \r
-#define CHECK_INT                                                                                                      \\r
-       if (zIFF1)                                                                                                              \\r
-       {                                                                                                                               \\r
-               UINT32 IntVect;                                                                                         \\r
-                                                                                                                                       \\r
-               if (CPU->IRQState == HOLD_LINE)                                                         \\r
-                       CPU->IRQState = CLEAR_LINE;                                                             \\r
-                                                                                                                                       \\r
-               CPU->HaltState = 0;                                                                                     \\r
-               zIFF1 = zIFF2 = 0;                                                                                      \\r
-               IntVect = CPU->Interrupt_Callback(CPU->IRQLine);                        \\r
-                                                                                                                                       \\r
-               PUSH_16(zRealPC)                                                                                        \\r
-                                                                                                                                       \\r
-               if (zIM == 2)                                                                                           \\r
-               {                                                                                                                       \\r
-                       IntVect = (IntVect & 0xff) | (zI << 8);                                 \\r
-                       PC = READ_MEM16(IntVect);                                                               \\r
-                       CPU->ExtraCycles += 17;                                                                 \\r
-               }                                                                                                                       \\r
-               else if (zIM == 1)                                                                                      \\r
-               {                                                                                                                       \\r
-                       PC = 0x38;                                                                                              \\r
-                       CPU->ExtraCycles += 13;                                                                 \\r
-               }                                                                                                                       \\r
-               else                                                                                                            \\r
-               {                                                                                                                       \\r
-                       PC = IntVect & 0x38;                                                                    \\r
-                       CPU->ExtraCycles += 13;                                                                 \\r
-               }                                                                                                                       \\r
-                                                                                                                                       \\r
-               SET_PC(PC)                                                                                                      \\r
+#define CHECK_INT                                                      \\r
+       if (zIFF1)                                                      \\r
+       {                                                               \\r
+               UINT32 IntVect;                                         \\r
+                                                                       \\r
+               if (CPU->IRQState == HOLD_LINE)                         \\r
+                       CPU->IRQState = CLEAR_LINE;                     \\r
+                                                                       \\r
+               CPU->HaltState = 0;                                     \\r
+               zIFF1 = zIFF2 = 0;                                      \\r
+               IntVect = CPU->Interrupt_Callback(CPU->IRQLine);        \\r
+                                                                       \\r
+               PUSH_16(zRealPC)                                        \\r
+                                                                       \\r
+               if (zIM == 2)                                           \\r
+               {                                                       \\r
+                       IntVect = (IntVect & 0xff) | (zI << 8);         \\r
+                       PC = READ_MEM16(IntVect);                       \\r
+                       CPU->ExtraCycles += 17;                         \\r
+               }                                                       \\r
+               else if (zIM == 1)                                      \\r
+               {                                                       \\r
+                       PC = 0x38;                                      \\r
+                       CPU->ExtraCycles += 13;                         \\r
+               }                                                       \\r
+               else                                                    \\r
+               {                                                       \\r
+                       PC = IntVect & 0x38;                            \\r
+                       CPU->ExtraCycles += 13;                         \\r
+               }                                                       \\r
+                                                                       \\r
+               SET_PC(PC)                                              \\r
        }\r
index f6ea29c..411d9f9 100644 (file)
@@ -183,7 +183,7 @@ extern struct DrZ80 drZ80;
 #define z80_int_assert(a)  Cz80_Set_IRQ(&CZ80, 0, (a) ? ASSERT_LINE : CLEAR_LINE)\r
 #define z80_nmi()          Cz80_Set_IRQ(&CZ80, IRQ_LINE_NMI, 0)\r
 \r
-#define z80_cyclesLeft     CZ80.ICount\r
+#define z80_cyclesLeft     (CZ80.ICount - CZ80.ExtraCycles)\r
 #define z80_subCLeft(c)    CZ80.ICount -= c\r
 #define z80_pc()           Cz80_Get_Reg(&CZ80, CZ80_PC)\r
 \r