From 5b5932696d6e151d00c1d02c63e197f63a6f7b10 Mon Sep 17 00:00:00 2001 From: pjgat09 Date: Wed, 21 Mar 2012 01:02:24 +0000 Subject: [PATCH] M6532: Added a dedicated cycle count variable to fix a timer issue. TIA: Increment the M6532 cycle count when in wsync. --- .../Consoles/Atari/2600/Atari2600.Core.cs | 1 + BizHawk.Emulation/Consoles/Atari/2600/M6532.cs | 15 ++++++++++----- BizHawk.Emulation/Consoles/Atari/2600/TIA.cs | 8 ++++++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/BizHawk.Emulation/Consoles/Atari/2600/Atari2600.Core.cs b/BizHawk.Emulation/Consoles/Atari/2600/Atari2600.Core.cs index 1745ecb744..188f2ba90c 100644 --- a/BizHawk.Emulation/Consoles/Atari/2600/Atari2600.Core.cs +++ b/BizHawk.Emulation/Consoles/Atari/2600/Atari2600.Core.cs @@ -142,6 +142,7 @@ namespace BizHawk tia.execute(1); tia.execute(1); + m6532.tick(); cpu.Execute(1); if (cpu.PendingCycles <= 0) { diff --git a/BizHawk.Emulation/Consoles/Atari/2600/M6532.cs b/BizHawk.Emulation/Consoles/Atari/2600/M6532.cs index c32c2354f0..5bd2e432d0 100644 --- a/BizHawk.Emulation/Consoles/Atari/2600/M6532.cs +++ b/BizHawk.Emulation/Consoles/Atari/2600/M6532.cs @@ -22,7 +22,7 @@ namespace BizHawk.Emulation.Consoles.Atari public byte swchb = 0x0B; public bool resetOccured = false; - + public int totalCycles = 0; public M6532(MOS6507 cpu, byte[] ram, Atari2600 core) { @@ -35,6 +35,11 @@ namespace BizHawk.Emulation.Consoles.Atari } + public void tick() + { + totalCycles++; + } + public byte ReadMemory(ushort addr) { ushort maskedAddr; @@ -53,12 +58,12 @@ namespace BizHawk.Emulation.Consoles.Atari Console.WriteLine("6532 timer read: " + maskedAddr.ToString("x")); // Calculate the current value on the timer - int timerCurrentValue = timerFinishedCycles - Cpu.TotalExecutedCycles; + int timerCurrentValue = timerFinishedCycles - totalCycles; interruptTriggered = false; // If the timer has not finished, shift the value down for the game - if (Cpu.TotalExecutedCycles < timerFinishedCycles) + if (totalCycles < timerFinishedCycles) { return (byte)(((timerCurrentValue) >> timerShift) & 0xFF); } @@ -93,7 +98,7 @@ namespace BizHawk.Emulation.Consoles.Atari } else if (maskedAddr == 0x05) // interrupt { - if ((timerFinishedCycles - Cpu.TotalExecutedCycles >= 0)|| (interruptEnabled && interruptTriggered)) + if ((timerFinishedCycles - totalCycles >= 0)|| (interruptEnabled && interruptTriggered)) { return 0x00; } @@ -130,7 +135,7 @@ namespace BizHawk.Emulation.Consoles.Atari timerStartValue = value << timerShift; // Calculate when the timer will be finished - timerFinishedCycles = timerStartValue + Cpu.TotalExecutedCycles; + timerFinishedCycles = timerStartValue + totalCycles; Console.WriteLine("6532 timer write: " + maskedAddr.ToString("x")); diff --git a/BizHawk.Emulation/Consoles/Atari/2600/TIA.cs b/BizHawk.Emulation/Consoles/Atari/2600/TIA.cs index a0aa5c5f39..1734436f7d 100644 --- a/BizHawk.Emulation/Consoles/Atari/2600/TIA.cs +++ b/BizHawk.Emulation/Consoles/Atari/2600/TIA.cs @@ -719,9 +719,17 @@ namespace BizHawk.Emulation.Consoles.Atari } else if (maskedAddr == 0x02) // WSYNC { + int count = 0; while (hsyncCnt > 0) { + count++; execute(1); + + // Add a cycle to the cpu every 3 TIA clocks (corrects timer error in M6532) + if (count % 3 == 0) + { + core.m6532.tick(); + } } } else if (maskedAddr == 0x04) // NUSIZ0