diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/CPUMonitor.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/CPUMonitor.cs index 8ac36b391a..fbdc5d2134 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/CPUMonitor.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/CPUMonitor.cs @@ -1,4 +1,5 @@ -using BizHawk.Emulation.Cores.Components.Z80A; +using BizHawk.Common; +using BizHawk.Emulation.Cores.Components.Z80A; using System; using System.Collections.Generic; using System.Linq; @@ -19,19 +20,17 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum #region Lookups - public ushort[] cur_instr => _cpu.cur_instr; - public int instr_pntr => _cpu.instr_pntr; - public ushort RegPC => _cpu.RegPC; + /// + /// CPU total executes t-states + /// public long TotalExecutedCycles => _cpu.TotalExecutedCycles; + + /// + /// Current BUSRQ line array + /// public ushort BUSRQ { - get - { - //if (_cpu.bus_pntr < _cpu.BUSRQ.Length - 1) - return _cpu.BUSRQ[_cpu.bus_pntr]; - - //return 0; - } + get { return _cpu.BUSRQ[_cpu.bus_pntr]; } } #endregion @@ -48,18 +47,15 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum #region State - public bool IsContending = false; - public int ContCounter = -1; - public int portContCounter = 0; - public int portContTotalLen = 0; - public bool portContending = false; + /// + /// The last 16-bit port address that was detected + /// public ushort lastPortAddr; - public int[] portContArr = new int[4]; #endregion #region Methods - + /// /// Handles the ULA and CPU cycle clocks, along with any memory and port contention /// @@ -89,7 +85,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum } /// - /// Looks up the BUSRQ address that is about to be signalled + /// Looks up the current BUSRQ address that is about to be signalled on the upcoming cycle /// /// private ushort AscertainBUSRQAddress() @@ -156,6 +152,79 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum return addr; } + /// + /// Running every cycle, this determines whether the upcoming BUSRQ is for an IO operation + /// Also processes any contention + /// + /// + private bool CheckIO() + { + bool isIO = false; + + switch (BUSRQ) + { + // BC: T1 + case Z80A.BIO1: + lastPortAddr = AscertainBUSRQAddress(); + isIO = true; + if (IsIOCycleContended(1)) + _cpu.TotalExecutedCycles += _machine.ULADevice.GetPortContentionValue((int)_machine.CurrentFrameCycle); + break; + // BC: T2 + case Z80A.BIO2: + lastPortAddr = AscertainBUSRQAddress(); + isIO = true; + if (IsIOCycleContended(2)) + _cpu.TotalExecutedCycles += _machine.ULADevice.GetPortContentionValue((int)_machine.CurrentFrameCycle); + break; + // BC: T3 + case Z80A.BIO3: + lastPortAddr = AscertainBUSRQAddress(); + isIO = true; + if (IsIOCycleContended(3)) + _cpu.TotalExecutedCycles += _machine.ULADevice.GetPortContentionValue((int)_machine.CurrentFrameCycle); + break; + // BC: T4 + case Z80A.BIO4: + lastPortAddr = AscertainBUSRQAddress(); + isIO = true; + if (IsIOCycleContended(4)) + _cpu.TotalExecutedCycles += _machine.ULADevice.GetPortContentionValue((int)_machine.CurrentFrameCycle); + break; + + // WZ: T1 + case Z80A.WIO1: + lastPortAddr = AscertainBUSRQAddress(); + isIO = true; + if (IsIOCycleContended(1)) + _cpu.TotalExecutedCycles += _machine.ULADevice.GetPortContentionValue((int)_machine.CurrentFrameCycle); + break; + // WZ: T2 + case Z80A.WIO2: + lastPortAddr = AscertainBUSRQAddress(); + isIO = true; + if (IsIOCycleContended(2)) + _cpu.TotalExecutedCycles += _machine.ULADevice.GetPortContentionValue((int)_machine.CurrentFrameCycle); + break; + // WZ: T3 + case Z80A.WIO3: + lastPortAddr = AscertainBUSRQAddress(); + isIO = true; + if (IsIOCycleContended(3)) + _cpu.TotalExecutedCycles += _machine.ULADevice.GetPortContentionValue((int)_machine.CurrentFrameCycle); + break; + // WZ: T4 + case Z80A.WIO4: + lastPortAddr = AscertainBUSRQAddress(); + isIO = true; + if (IsIOCycleContended(4)) + _cpu.TotalExecutedCycles += _machine.ULADevice.GetPortContentionValue((int)_machine.CurrentFrameCycle); + break; + } + + return isIO; + } + /// /// Returns TRUE if the supplied T-cycle within an IO operation has the possibility of being contended /// This can be different based on the emulated ZX Spectrum model @@ -236,80 +305,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum } return false; - } - - /// - /// Running every cycle, this determines whether the upcoming BUSRQ is for an IO operation - /// Also processes any contention - /// - /// - private bool CheckIO() - { - bool isIO = false; - - switch (BUSRQ) - { - // BC: T1 - case Z80A.BIO1: - lastPortAddr = AscertainBUSRQAddress(); - isIO = true; - if (IsIOCycleContended(1)) - _cpu.TotalExecutedCycles += _machine.ULADevice.GetPortContentionValue((int)_machine.CurrentFrameCycle); - break; - // BC: T2 - case Z80A.BIO2: - lastPortAddr = AscertainBUSRQAddress(); - isIO = true; - if (IsIOCycleContended(2)) - _cpu.TotalExecutedCycles += _machine.ULADevice.GetPortContentionValue((int)_machine.CurrentFrameCycle); - break; - // BC: T3 - case Z80A.BIO3: - lastPortAddr = AscertainBUSRQAddress(); - isIO = true; - if (IsIOCycleContended(3)) - _cpu.TotalExecutedCycles += _machine.ULADevice.GetPortContentionValue((int)_machine.CurrentFrameCycle); - break; - // BC: T4 - case Z80A.BIO4: - lastPortAddr = AscertainBUSRQAddress(); - isIO = true; - if (IsIOCycleContended(4)) - _cpu.TotalExecutedCycles += _machine.ULADevice.GetPortContentionValue((int)_machine.CurrentFrameCycle); - break; - - // WZ: T1 - case Z80A.WIO1: - lastPortAddr = AscertainBUSRQAddress(); - isIO = true; - if (IsIOCycleContended(1)) - _cpu.TotalExecutedCycles += _machine.ULADevice.GetPortContentionValue((int)_machine.CurrentFrameCycle); - break; - // WZ: T2 - case Z80A.WIO2: - lastPortAddr = AscertainBUSRQAddress(); - isIO = true; - if (IsIOCycleContended(2)) - _cpu.TotalExecutedCycles += _machine.ULADevice.GetPortContentionValue((int)_machine.CurrentFrameCycle); - break; - // WZ: T3 - case Z80A.WIO3: - lastPortAddr = AscertainBUSRQAddress(); - isIO = true; - if (IsIOCycleContended(3)) - _cpu.TotalExecutedCycles += _machine.ULADevice.GetPortContentionValue((int)_machine.CurrentFrameCycle); - break; - // WZ: T4 - case Z80A.WIO4: - lastPortAddr = AscertainBUSRQAddress(); - isIO = true; - if (IsIOCycleContended(4)) - _cpu.TotalExecutedCycles += _machine.ULADevice.GetPortContentionValue((int)_machine.CurrentFrameCycle); - break; - } - - return isIO; - } + } /// /// Called when the first byte of an instruction is fetched @@ -323,5 +319,15 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum #endregion + #region Serialization + + public void SyncState(Serializer ser) + { + ser.BeginSection("CPUMonitor"); + ser.Sync("", ref lastPortAddr); + ser.EndSection(); + } + + #endregion } }