From 3fa2828d5b3d61491612f5a72b451b8810dc8b02 Mon Sep 17 00:00:00 2001 From: alyosha-tas Date: Thu, 3 Nov 2016 20:50:47 -0400 Subject: [PATCH] Accuracy improvement to z80 properly set interrupt enable after the NEXT instruction --- BizHawk.Emulation.Cores/CPUs/Z80/Execute.cs | 21 +++++++++++++++++---- BizHawk.Emulation.Cores/CPUs/Z80/Z80A.cs | 1 + 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/BizHawk.Emulation.Cores/CPUs/Z80/Execute.cs b/BizHawk.Emulation.Cores/CPUs/Z80/Execute.cs index d49fdf5432..74e02b1ff0 100644 --- a/BizHawk.Emulation.Cores/CPUs/Z80/Execute.cs +++ b/BizHawk.Emulation.Cores/CPUs/Z80/Execute.cs @@ -14,6 +14,8 @@ namespace BizHawk.Emulation.Cores.Components.Z80 private int pendingCycles; public int PendingCycles { get { return pendingCycles; } set { pendingCycles = value; } } + private int EI_pending; + public bool Debug; public Action Logger; @@ -35,7 +37,8 @@ namespace BizHawk.Emulation.Cores.Components.Z80 while (pendingCycles > 0) { Interruptable = true; - + //if (interrupt == true) + //Console.WriteLine(totalExecutedCycles); if (halted) { @@ -6629,7 +6632,7 @@ namespace BizHawk.Emulation.Cores.Components.Z80 totalExecutedCycles += 10; pendingCycles -= 10; break; case 0xFB: // EI - IFF1 = IFF2 = true; + EI_pending = 2; Interruptable = false; totalExecutedCycles += 4; pendingCycles -= 4; break; @@ -8011,7 +8014,7 @@ namespace BizHawk.Emulation.Cores.Components.Z80 totalExecutedCycles += 10; pendingCycles -= 10; break; case 0xFB: // EI - IFF1 = IFF2 = true; + EI_pending = 2; Interruptable = false; totalExecutedCycles += 4; pendingCycles -= 4; break; @@ -11857,7 +11860,7 @@ namespace BizHawk.Emulation.Cores.Components.Z80 totalExecutedCycles += 10; pendingCycles -= 10; break; case 0xFB: // EI - IFF1 = IFF2 = true; + EI_pending = 2; Interruptable = false; totalExecutedCycles += 4; pendingCycles -= 4; break; @@ -11944,6 +11947,16 @@ namespace BizHawk.Emulation.Cores.Components.Z80 } IRQCallback(); } + + //EI (enable interrupts) actually takes effect after the NEXT instruction + if (EI_pending > 0) + { + EI_pending--; + if (EI_pending == 0) + { + IFF1 = IFF2 = true; + } + } } } diff --git a/BizHawk.Emulation.Cores/CPUs/Z80/Z80A.cs b/BizHawk.Emulation.Cores/CPUs/Z80/Z80A.cs index 6674982d86..9a3192813d 100644 --- a/BizHawk.Emulation.Cores/CPUs/Z80/Z80A.cs +++ b/BizHawk.Emulation.Cores/CPUs/Z80/Z80A.cs @@ -132,6 +132,7 @@ namespace BizHawk.Emulation.Cores.Components.Z80 ser.Sync("Halted", ref halted); ser.Sync("ExecutedCycles", ref totalExecutedCycles); ser.Sync("PendingCycles", ref pendingCycles); + ser.Sync("EI_pending", ref EI_pending); ser.EndSection(); } }