From 85be6af3d33be53d1c70cfbb11d0ebfde8cd417f Mon Sep 17 00:00:00 2001 From: adelikat Date: Fri, 6 Dec 2019 17:47:59 -0600 Subject: [PATCH] Convert spaces to tabs in ZX Spectrum and AmstradCPC cores --- .../AmstradCPC/AmstradCPC.Controllers.cs | 164 +- .../AmstradCPC/AmstradCPC.IDebuggable.cs | 274 +- .../AmstradCPC/AmstradCPC.IEmulator.cs | 126 +- .../AmstradCPC/AmstradCPC.IMemoryDomains.cs | 128 +- .../AmstradCPC/AmstradCPC.ISettable.cs | 444 +- .../AmstradCPC/AmstradCPC.IStatable.cs | 156 +- .../AmstradCPC/AmstradCPC.InputPollable.cs | 40 +- .../AmstradCPC/AmstradCPC.Messaging.cs | 964 ++-- .../Computers/AmstradCPC/AmstradCPC.Util.cs | 76 +- .../Computers/AmstradCPC/AmstradCPC.cs | 344 +- .../Hardware/Abstraction/IBeeperDevice.cs | 36 +- .../Hardware/Abstraction/IFDDHost.cs | 44 +- .../Hardware/Abstraction/IJoystick.cs | 52 +- .../Hardware/Abstraction/IKeyboard.cs | 80 +- .../AmstradCPC/Hardware/Abstraction/IPSG.cs | 100 +- .../Hardware/Abstraction/IPortIODevice.cs | 28 +- .../Hardware/Datacorder/DatacorderDevice.cs | 1344 ++--- .../AmstradCPC/Hardware/Disk/CHRN.cs | 316 +- .../Hardware/Disk/NECUPD765.Definitions.cs | 1630 +++--- .../AmstradCPC/Hardware/Disk/NECUPD765.FDC.cs | 4992 ++++++++--------- .../AmstradCPC/Hardware/Disk/NECUPD765.FDD.cs | 744 +-- .../Hardware/Disk/NECUPD765.IPortIODevice.cs | 298 +- .../Hardware/Disk/NECUPD765.Timing.cs | 194 +- .../AmstradCPC/Hardware/Disk/NECUPD765.cs | 334 +- .../Hardware/Disk/NECUPS765.Static.cs | 148 +- .../Hardware/Display/AmstradGateArray.cs | 2480 ++++---- .../AmstradCPC/Hardware/Display/CRCT_6845.cs | 1962 +++---- .../AmstradCPC/Hardware/Display/CRTC6845.cs | 14 +- .../AmstradCPC/Hardware/Display/CRTDevice.cs | 974 ++-- .../Hardware/Input/StandardKeyboard.cs | 214 +- .../AmstradCPC/Hardware/PPI/PPI_8255.cs | 710 +-- .../Hardware/SoundOutput/AY38912.cs | 1302 ++--- .../AmstradCPC/Hardware/SoundOutput/Beeper.cs | 330 +- .../Machine/CPC464/CPC464.Memory.cs | 244 +- .../AmstradCPC/Machine/CPC464/CPC464.Port.cs | 160 +- .../AmstradCPC/Machine/CPC464/CPC464.cs | 60 +- .../Machine/CPC6128/CPC6128.Memory.cs | 472 +- .../Machine/CPC6128/CPC6128.Port.cs | 228 +- .../AmstradCPC/Machine/CPC6128/CPC6128.cs | 64 +- .../AmstradCPC/Machine/CPCBase.Input.cs | 528 +- .../AmstradCPC/Machine/CPCBase.Media.cs | 448 +- .../AmstradCPC/Machine/CPCBase.Memory.cs | 234 +- .../AmstradCPC/Machine/CPCBase.Port.cs | 158 +- .../Computers/AmstradCPC/Machine/CPCBase.cs | 434 +- .../AmstradCPC/Machine/GateArrayBase.cs | 760 +-- .../AmstradCPC/Machine/MachineType.cs | 28 +- .../Media/Disk/CPCExtendedFloppyDisk.cs | 418 +- .../AmstradCPC/Media/Disk/CPCFloppyDisk.cs | 388 +- .../AmstradCPC/Media/Disk/DiskHandler.cs | 14 +- .../AmstradCPC/Media/Disk/DiskType.cs | 28 +- .../AmstradCPC/Media/Disk/FloppyDisk.cs | 1070 ++-- .../AmstradCPC/Media/MediaConverter.cs | 228 +- .../AmstradCPC/Media/MediaConverterType.cs | 18 +- .../AmstradCPC/Media/Tape/CDT/CdtConverter.cs | 3538 ++++++------ .../AmstradCPC/Media/Tape/TapeCommand.cs | 24 +- .../AmstradCPC/Media/Tape/TapeDataBlock.cs | 426 +- .../Computers/AmstradCPC/ROM/RomData.cs | 104 +- .../AmstradCPC/SoundProviderMixer.cs | 354 +- .../Hardware/Abstraction/IBeeperDevice.cs | 36 +- .../Hardware/Abstraction/IFDDHost.cs | 44 +- .../Hardware/Abstraction/IJoystick.cs | 52 +- .../Hardware/Abstraction/IKeyboard.cs | 108 +- .../Hardware/Abstraction/IPSG.cs | 98 +- .../Hardware/Abstraction/IPortIODevice.cs | 28 +- .../Hardware/Datacorder/DatacorderDevice.cs | 1652 +++--- .../SinclairSpectrum/Hardware/Disk/CHRN.cs | 316 +- .../Hardware/Disk/NECUPD765.cs | 334 +- .../Hardware/Input/CursorJoystick.cs | 162 +- .../Hardware/Input/KempstonJoystick.cs | 138 +- .../Hardware/Input/NullJoystick.cs | 134 +- .../Hardware/Input/SinclairJoystick1.cs | 160 +- .../Hardware/Input/SinclairJoystick2.cs | 160 +- .../Hardware/Input/StandardKeyboard.cs | 414 +- .../SinclairSpectrum/Hardware/Rom/RomData.cs | 144 +- .../Hardware/SoundOuput/AY38912.cs | 1262 ++--- .../SinclairSpectrum/Machine/CPUMonitor.cs | 720 +-- .../SinclairSpectrum/Machine/MachineType.cs | 60 +- .../Pentagon128K/Pentagon128.Screen.cs | 74 +- .../Machine/Pentagon128K/Pentagon128.cs | 64 +- .../SinclairSpectrum/Machine/SpectrumBase.cs | 634 +-- .../Computers/SinclairSpectrum/Machine/ULA.cs | 2006 +++---- .../Machine/ZXSpectrum128K/ZX128.Screen.cs | 78 +- .../Machine/ZXSpectrum128K/ZX128.cs | 60 +- .../Machine/ZXSpectrum128KPlus2/ZX128Plus2.cs | 34 +- .../ZX128Plus2a.Screen.cs | 80 +- .../ZXSpectrum128KPlus2a/ZX128Plus2a.cs | 60 +- .../Machine/ZXSpectrum128KPlus3/ZX128Plus3.cs | 64 +- .../Machine/ZXSpectrum16K/ZX16.cs | 202 +- .../Machine/ZXSpectrum48K/ZX48.Screen.cs | 76 +- .../Machine/ZXSpectrum48K/ZX48.cs | 68 +- .../Disk/CPCFormat/CPCExtendedFloppyDisk.cs | 436 +- .../Media/Disk/CPCFormat/CPCFloppyDisk.cs | 418 +- .../SinclairSpectrum/Media/Disk/DiskType.cs | 52 +- .../SinclairSpectrum/Media/Disk/FloppyDisk.cs | 1082 ++-- .../Media/Disk/IPFFormat/IPFFloppyDisk.cs | 784 +-- .../Media/Disk/UDIFormat/UDI1_0FloppyDisk.cs | 346 +- .../SinclairSpectrum/Media/MediaConverter.cs | 362 +- .../Media/MediaConverterType.cs | 26 +- .../Media/Snapshot/SZX/SZX.Methods.cs | 674 +-- .../Media/Snapshot/SZX/SZX.Objects.cs | 680 +-- .../Media/Tape/CSW/CswConverter.cs | 356 +- .../Media/Tape/PZX/PzxConverter.cs | 554 +- .../Media/Tape/TAP/TapConverter.cs | 524 +- .../Media/Tape/TZX/TzxConverter.cs | 3482 ++++++------ .../Media/Tape/TapeCommand.cs | 24 +- .../Media/Tape/TapeDataBlock.cs | 426 +- .../Media/Tape/WAV/StreamHelper.cs | 168 +- .../Media/Tape/WAV/WavConverter.cs | 196 +- .../Media/Tape/WAV/WavHeader.cs | 178 +- .../Media/Tape/WAV/WavStreamReader.cs | 188 +- .../SinclairSpectrum/ZXSpectrum.IStatable.cs | 156 +- .../Computers/SinclairSpectrum/ZXSpectrum.cs | 482 +- 112 files changed, 26295 insertions(+), 26291 deletions(-) diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.Controllers.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.Controllers.cs index bcde9619a2..dae5ccb5ed 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.Controllers.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.Controllers.cs @@ -3,50 +3,50 @@ using BizHawk.Emulation.Common; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// CPCHawk: Core Class - /// * Controllers * - /// - public partial class AmstradCPC - { - /// - /// The one CPCHawk ControllerDefinition - /// - public ControllerDefinition AmstradCPCControllerDefinition - { - get - { - ControllerDefinition definition = new ControllerDefinition(); - definition.Name = "AmstradCPC Controller"; + /// + /// CPCHawk: Core Class + /// * Controllers * + /// + public partial class AmstradCPC + { + /// + /// The one CPCHawk ControllerDefinition + /// + public ControllerDefinition AmstradCPCControllerDefinition + { + get + { + ControllerDefinition definition = new ControllerDefinition(); + definition.Name = "AmstradCPC Controller"; - // joysticks - List joys1 = new List - { + // joysticks + List joys1 = new List + { // P1 Joystick "P1 Up", "P1 Down", "P1 Left", "P1 Right", "P1 Fire1", "P1 Fire2", "P1 Fire3" - }; + }; - foreach (var s in joys1) - { - definition.BoolButtons.Add(s); - definition.CategoryLabels[s] = "J1"; - } + foreach (var s in joys1) + { + definition.BoolButtons.Add(s); + definition.CategoryLabels[s] = "J1"; + } - List joys2 = new List - { + List joys2 = new List + { // P2 Joystick "P2 Up", "P2 Down", "P2 Left", "P2 Right", "P2 Fire", - }; + }; - foreach (var s in joys2) - { - definition.BoolButtons.Add(s); - definition.CategoryLabels[s] = "J2"; - } + foreach (var s in joys2) + { + definition.BoolButtons.Add(s); + definition.CategoryLabels[s] = "J2"; + } - // keyboard - List keys = new List - { + // keyboard + List keys = new List + { // http://www.cpcwiki.eu/index.php/Programming:Keyboard_scanning // http://www.cpcwiki.eu/index.php/File:Grimware_cpc464_version3_case_top.jpg @@ -64,66 +64,66 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC "Key CURUP", "Key CURDOWN", "Key CURLEFT", "Key CURRIGHT", "Key COPY", // Keyboard - Numpad "Key NUM0", "Key NUM1", "Key NUM2", "Key NUM3", "Key NUM4", "Key NUM5", "Key NUM6", "Key NUM7", "Key NUM8", "Key NUM9", "Key NUMPERIOD", "KEY ENTER" - }; + }; - foreach (var s in keys) - { - definition.BoolButtons.Add(s); - definition.CategoryLabels[s] = "Keyboard"; - } + foreach (var s in keys) + { + definition.BoolButtons.Add(s); + definition.CategoryLabels[s] = "Keyboard"; + } - // Power functions - List power = new List - { + // Power functions + List power = new List + { // Power functions "Reset", "Power" - }; + }; - foreach (var s in power) - { - definition.BoolButtons.Add(s); - definition.CategoryLabels[s] = "Power"; - } + foreach (var s in power) + { + definition.BoolButtons.Add(s); + definition.CategoryLabels[s] = "Power"; + } - // Datacorder (tape device) - List tape = new List - { + // Datacorder (tape device) + List tape = new List + { // Tape functions "Play Tape", "Stop Tape", "RTZ Tape", "Record Tape", "Insert Next Tape", - "Insert Previous Tape", "Next Tape Block", "Prev Tape Block", "Get Tape Status" - }; + "Insert Previous Tape", "Next Tape Block", "Prev Tape Block", "Get Tape Status" + }; - foreach (var s in tape) - { - definition.BoolButtons.Add(s); - definition.CategoryLabels[s] = "Datacorder"; - } + foreach (var s in tape) + { + definition.BoolButtons.Add(s); + definition.CategoryLabels[s] = "Datacorder"; + } - // Datacorder (tape device) - List disk = new List - { + // Datacorder (tape device) + List disk = new List + { // Tape functions "Insert Next Disk", "Insert Previous Disk", /*"Eject Current Disk",*/ "Get Disk Status" - }; + }; - foreach (var s in disk) - { - definition.BoolButtons.Add(s); - definition.CategoryLabels[s] = "Amstrad Disk Drive"; - } + foreach (var s in disk) + { + definition.BoolButtons.Add(s); + definition.CategoryLabels[s] = "Amstrad Disk Drive"; + } - return definition; - } - } - } + return definition; + } + } + } - /// - /// The possible joystick types - /// - public enum JoystickType - { - NULL, - Joystick1, - Joystick2 - } + /// + /// The possible joystick types + /// + public enum JoystickType + { + NULL, + Joystick1, + Joystick2 + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.IDebuggable.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.IDebuggable.cs index 917a08865b..4c223a67c9 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.IDebuggable.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.IDebuggable.cs @@ -4,146 +4,146 @@ using BizHawk.Emulation.Common; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// CPCHawk: Core Class - /// * IDebugggable * - /// - public partial class AmstradCPC : IDebuggable - { - public IDictionary GetCpuFlagsAndRegisters() - { - return new Dictionary - { - ["A"] = _cpu.Regs[_cpu.A], - ["AF"] = _cpu.Regs[_cpu.F] + (_cpu.Regs[_cpu.A] << 8), - ["B"] = _cpu.Regs[_cpu.B], - ["BC"] = _cpu.Regs[_cpu.C] + (_cpu.Regs[_cpu.B] << 8), - ["C"] = _cpu.Regs[_cpu.C], - ["D"] = _cpu.Regs[_cpu.D], - ["DE"] = _cpu.Regs[_cpu.E] + (_cpu.Regs[_cpu.D] << 8), - ["E"] = _cpu.Regs[_cpu.E], - ["F"] = _cpu.Regs[_cpu.F], - ["H"] = _cpu.Regs[_cpu.H], - ["HL"] = _cpu.Regs[_cpu.L] + (_cpu.Regs[_cpu.H] << 8), - ["I"] = _cpu.Regs[_cpu.I], - ["IX"] = _cpu.Regs[_cpu.Ixl] + (_cpu.Regs[_cpu.Ixh] << 8), - ["IY"] = _cpu.Regs[_cpu.Iyl] + (_cpu.Regs[_cpu.Iyh] << 8), - ["L"] = _cpu.Regs[_cpu.L], - ["PC"] = _cpu.Regs[_cpu.PCl] + (_cpu.Regs[_cpu.PCh] << 8), - ["R"] = _cpu.Regs[_cpu.R], - ["Shadow AF"] = _cpu.Regs[_cpu.F_s] + (_cpu.Regs[_cpu.A_s] << 8), - ["Shadow BC"] = _cpu.Regs[_cpu.C_s] + (_cpu.Regs[_cpu.B_s] << 8), - ["Shadow DE"] = _cpu.Regs[_cpu.E_s] + (_cpu.Regs[_cpu.D_s] << 8), - ["Shadow HL"] = _cpu.Regs[_cpu.L_s] + (_cpu.Regs[_cpu.H_s] << 8), - ["SP"] = _cpu.Regs[_cpu.Iyl] + (_cpu.Regs[_cpu.Iyh] << 8), - ["Flag C"] = _cpu.FlagC, - ["Flag N"] = _cpu.FlagN, - ["Flag P/V"] = _cpu.FlagP, - ["Flag 3rd"] = _cpu.Flag3, - ["Flag H"] = _cpu.FlagH, - ["Flag 5th"] = _cpu.Flag5, - ["Flag Z"] = _cpu.FlagZ, - ["Flag S"] = _cpu.FlagS - }; - } + /// + /// CPCHawk: Core Class + /// * IDebugggable * + /// + public partial class AmstradCPC : IDebuggable + { + public IDictionary GetCpuFlagsAndRegisters() + { + return new Dictionary + { + ["A"] = _cpu.Regs[_cpu.A], + ["AF"] = _cpu.Regs[_cpu.F] + (_cpu.Regs[_cpu.A] << 8), + ["B"] = _cpu.Regs[_cpu.B], + ["BC"] = _cpu.Regs[_cpu.C] + (_cpu.Regs[_cpu.B] << 8), + ["C"] = _cpu.Regs[_cpu.C], + ["D"] = _cpu.Regs[_cpu.D], + ["DE"] = _cpu.Regs[_cpu.E] + (_cpu.Regs[_cpu.D] << 8), + ["E"] = _cpu.Regs[_cpu.E], + ["F"] = _cpu.Regs[_cpu.F], + ["H"] = _cpu.Regs[_cpu.H], + ["HL"] = _cpu.Regs[_cpu.L] + (_cpu.Regs[_cpu.H] << 8), + ["I"] = _cpu.Regs[_cpu.I], + ["IX"] = _cpu.Regs[_cpu.Ixl] + (_cpu.Regs[_cpu.Ixh] << 8), + ["IY"] = _cpu.Regs[_cpu.Iyl] + (_cpu.Regs[_cpu.Iyh] << 8), + ["L"] = _cpu.Regs[_cpu.L], + ["PC"] = _cpu.Regs[_cpu.PCl] + (_cpu.Regs[_cpu.PCh] << 8), + ["R"] = _cpu.Regs[_cpu.R], + ["Shadow AF"] = _cpu.Regs[_cpu.F_s] + (_cpu.Regs[_cpu.A_s] << 8), + ["Shadow BC"] = _cpu.Regs[_cpu.C_s] + (_cpu.Regs[_cpu.B_s] << 8), + ["Shadow DE"] = _cpu.Regs[_cpu.E_s] + (_cpu.Regs[_cpu.D_s] << 8), + ["Shadow HL"] = _cpu.Regs[_cpu.L_s] + (_cpu.Regs[_cpu.H_s] << 8), + ["SP"] = _cpu.Regs[_cpu.Iyl] + (_cpu.Regs[_cpu.Iyh] << 8), + ["Flag C"] = _cpu.FlagC, + ["Flag N"] = _cpu.FlagN, + ["Flag P/V"] = _cpu.FlagP, + ["Flag 3rd"] = _cpu.Flag3, + ["Flag H"] = _cpu.FlagH, + ["Flag 5th"] = _cpu.Flag5, + ["Flag Z"] = _cpu.FlagZ, + ["Flag S"] = _cpu.FlagS + }; + } - public void SetCpuRegister(string register, int value) - { - switch (register) - { - default: - throw new InvalidOperationException(); - case "A": - _cpu.Regs[_cpu.A] = (ushort)value; - break; - case "AF": - _cpu.Regs[_cpu.F] = (ushort)(value & 0xFF); - _cpu.Regs[_cpu.A] = (ushort)(value & 0xFF00); - break; - case "B": - _cpu.Regs[_cpu.B] = (ushort)value; - break; - case "BC": - _cpu.Regs[_cpu.C] = (ushort)(value & 0xFF); - _cpu.Regs[_cpu.B] = (ushort)(value & 0xFF00); - break; - case "C": - _cpu.Regs[_cpu.C] = (ushort)value; - break; - case "D": - _cpu.Regs[_cpu.D] = (ushort)value; - break; - case "DE": - _cpu.Regs[_cpu.E] = (ushort)(value & 0xFF); - _cpu.Regs[_cpu.D] = (ushort)(value & 0xFF00); - break; - case "E": - _cpu.Regs[_cpu.E] = (ushort)value; - break; - case "F": - _cpu.Regs[_cpu.F] = (ushort)value; - break; - case "H": - _cpu.Regs[_cpu.H] = (ushort)value; - break; - case "HL": - _cpu.Regs[_cpu.L] = (ushort)(value & 0xFF); - _cpu.Regs[_cpu.H] = (ushort)(value & 0xFF00); - break; - case "I": - _cpu.Regs[_cpu.I] = (ushort)value; - break; - case "IX": - _cpu.Regs[_cpu.Ixl] = (ushort)(value & 0xFF); - _cpu.Regs[_cpu.Ixh] = (ushort)(value & 0xFF00); - break; - case "IY": - _cpu.Regs[_cpu.Iyl] = (ushort)(value & 0xFF); - _cpu.Regs[_cpu.Iyh] = (ushort)(value & 0xFF00); - break; - case "L": - _cpu.Regs[_cpu.L] = (ushort)value; - break; - case "PC": - _cpu.Regs[_cpu.PCl] = (ushort)(value & 0xFF); - _cpu.Regs[_cpu.PCh] = (ushort)(value & 0xFF00); - break; - case "R": - _cpu.Regs[_cpu.R] = (ushort)value; - break; - case "Shadow AF": - _cpu.Regs[_cpu.F_s] = (ushort)(value & 0xFF); - _cpu.Regs[_cpu.A_s] = (ushort)(value & 0xFF00); - break; - case "Shadow BC": - _cpu.Regs[_cpu.C_s] = (ushort)(value & 0xFF); - _cpu.Regs[_cpu.B_s] = (ushort)(value & 0xFF00); - break; - case "Shadow DE": - _cpu.Regs[_cpu.E_s] = (ushort)(value & 0xFF); - _cpu.Regs[_cpu.D_s] = (ushort)(value & 0xFF00); - break; - case "Shadow HL": - _cpu.Regs[_cpu.L_s] = (ushort)(value & 0xFF); - _cpu.Regs[_cpu.H_s] = (ushort)(value & 0xFF00); - break; - case "SP": - _cpu.Regs[_cpu.SPl] = (ushort)(value & 0xFF); - _cpu.Regs[_cpu.SPh] = (ushort)(value & 0xFF00); - break; - } - } + public void SetCpuRegister(string register, int value) + { + switch (register) + { + default: + throw new InvalidOperationException(); + case "A": + _cpu.Regs[_cpu.A] = (ushort)value; + break; + case "AF": + _cpu.Regs[_cpu.F] = (ushort)(value & 0xFF); + _cpu.Regs[_cpu.A] = (ushort)(value & 0xFF00); + break; + case "B": + _cpu.Regs[_cpu.B] = (ushort)value; + break; + case "BC": + _cpu.Regs[_cpu.C] = (ushort)(value & 0xFF); + _cpu.Regs[_cpu.B] = (ushort)(value & 0xFF00); + break; + case "C": + _cpu.Regs[_cpu.C] = (ushort)value; + break; + case "D": + _cpu.Regs[_cpu.D] = (ushort)value; + break; + case "DE": + _cpu.Regs[_cpu.E] = (ushort)(value & 0xFF); + _cpu.Regs[_cpu.D] = (ushort)(value & 0xFF00); + break; + case "E": + _cpu.Regs[_cpu.E] = (ushort)value; + break; + case "F": + _cpu.Regs[_cpu.F] = (ushort)value; + break; + case "H": + _cpu.Regs[_cpu.H] = (ushort)value; + break; + case "HL": + _cpu.Regs[_cpu.L] = (ushort)(value & 0xFF); + _cpu.Regs[_cpu.H] = (ushort)(value & 0xFF00); + break; + case "I": + _cpu.Regs[_cpu.I] = (ushort)value; + break; + case "IX": + _cpu.Regs[_cpu.Ixl] = (ushort)(value & 0xFF); + _cpu.Regs[_cpu.Ixh] = (ushort)(value & 0xFF00); + break; + case "IY": + _cpu.Regs[_cpu.Iyl] = (ushort)(value & 0xFF); + _cpu.Regs[_cpu.Iyh] = (ushort)(value & 0xFF00); + break; + case "L": + _cpu.Regs[_cpu.L] = (ushort)value; + break; + case "PC": + _cpu.Regs[_cpu.PCl] = (ushort)(value & 0xFF); + _cpu.Regs[_cpu.PCh] = (ushort)(value & 0xFF00); + break; + case "R": + _cpu.Regs[_cpu.R] = (ushort)value; + break; + case "Shadow AF": + _cpu.Regs[_cpu.F_s] = (ushort)(value & 0xFF); + _cpu.Regs[_cpu.A_s] = (ushort)(value & 0xFF00); + break; + case "Shadow BC": + _cpu.Regs[_cpu.C_s] = (ushort)(value & 0xFF); + _cpu.Regs[_cpu.B_s] = (ushort)(value & 0xFF00); + break; + case "Shadow DE": + _cpu.Regs[_cpu.E_s] = (ushort)(value & 0xFF); + _cpu.Regs[_cpu.D_s] = (ushort)(value & 0xFF00); + break; + case "Shadow HL": + _cpu.Regs[_cpu.L_s] = (ushort)(value & 0xFF); + _cpu.Regs[_cpu.H_s] = (ushort)(value & 0xFF00); + break; + case "SP": + _cpu.Regs[_cpu.SPl] = (ushort)(value & 0xFF); + _cpu.Regs[_cpu.SPh] = (ushort)(value & 0xFF00); + break; + } + } - public IMemoryCallbackSystem MemoryCallbacks { get; } + public IMemoryCallbackSystem MemoryCallbacks { get; } - public bool CanStep(StepType type) => false; + public bool CanStep(StepType type) => false; - [FeatureNotImplemented] - public void Step(StepType type) - { - throw new NotImplementedException(); - } + [FeatureNotImplemented] + public void Step(StepType type) + { + throw new NotImplementedException(); + } - public long TotalExecutedCycles => _cpu.TotalExecutedCycles; - } + public long TotalExecutedCycles => _cpu.TotalExecutedCycles; + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.IEmulator.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.IEmulator.cs index bb7fe9b5f3..4b7bf3cb9e 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.IEmulator.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.IEmulator.cs @@ -2,84 +2,84 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// CPCHawk: Core Class - /// * IEmulator * - /// - public partial class AmstradCPC : IEmulator - { - public IEmulatorServiceProvider ServiceProvider { get; } + /// + /// CPCHawk: Core Class + /// * IEmulator * + /// + public partial class AmstradCPC : IEmulator + { + public IEmulatorServiceProvider ServiceProvider { get; } - public ControllerDefinition ControllerDefinition { get; set; } + public ControllerDefinition ControllerDefinition { get; set; } public bool FrameAdvance(IController controller, bool render, bool renderSound) - { - _controller = controller; + { + _controller = controller; - bool ren = render; - bool renSound = renderSound; + bool ren = render; + bool renSound = renderSound; - if (DeterministicEmulation) - { - ren = true; - renSound = true; - } + if (DeterministicEmulation) + { + ren = true; + renSound = true; + } - _isLag = true; + _isLag = true; - if (_tracer.Enabled) - { - _cpu.TraceCallback = s => _tracer.Put(s); - } - else - { - _cpu.TraceCallback = null; - } + if (_tracer.Enabled) + { + _cpu.TraceCallback = s => _tracer.Put(s); + } + else + { + _cpu.TraceCallback = null; + } - _machine.ExecuteFrame(ren, renSound); + _machine.ExecuteFrame(ren, renSound); - if (_isLag) - { - _lagCount++; - } + if (_isLag) + { + _lagCount++; + } return true; - } + } - public int Frame - { - get - { - if (_machine == null) - return 0; - else - return _machine.FrameCount; - } - } + public int Frame + { + get + { + if (_machine == null) + return 0; + else + return _machine.FrameCount; + } + } - public string SystemId => "AmstradCPC"; + public string SystemId => "AmstradCPC"; - private bool deterministicEmulation; - public bool DeterministicEmulation - { - get { return deterministicEmulation; } - } + private bool deterministicEmulation; + public bool DeterministicEmulation + { + get { return deterministicEmulation; } + } - public void ResetCounters() - { - _machine.FrameCount = 0; - _lagCount = 0; - _isLag = false; - } + public void ResetCounters() + { + _machine.FrameCount = 0; + _lagCount = 0; + _isLag = false; + } - public CoreComm CoreComm { get; } + public CoreComm CoreComm { get; } - public void Dispose() - { - if (_machine != null) - { - _machine = null; - } - } - } + public void Dispose() + { + if (_machine != null) + { + _machine = null; + } + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.IMemoryDomains.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.IMemoryDomains.cs index 01b844ff3c..e3d8a16beb 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.IMemoryDomains.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.IMemoryDomains.cs @@ -5,74 +5,74 @@ using BizHawk.Emulation.Common; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// CPCHawk: Core Class - /// * Memory Domains * - /// - public partial class AmstradCPC - { - internal IMemoryDomains memoryDomains; - private readonly Dictionary _byteArrayDomains = new Dictionary(); - private bool _memoryDomainsInit = false; + /// + /// CPCHawk: Core Class + /// * Memory Domains * + /// + public partial class AmstradCPC + { + internal IMemoryDomains memoryDomains; + private readonly Dictionary _byteArrayDomains = new Dictionary(); + private bool _memoryDomainsInit = false; - private void SetupMemoryDomains() - { - var domains = new List - { - new MemoryDomainDelegate("System Bus", 0x10000, MemoryDomain.Endian.Little, - (addr) => - { - if (addr < 0 || addr >= 65536) - { - throw new ArgumentOutOfRangeException(); - } - return _machine.ReadBus((ushort)addr); - }, - (addr, value) => - { - if (addr < 0 || addr >= 65536) - { - throw new ArgumentOutOfRangeException(); - } - _machine.WriteBus((ushort)addr, value); - }, 1) - }; + private void SetupMemoryDomains() + { + var domains = new List + { + new MemoryDomainDelegate("System Bus", 0x10000, MemoryDomain.Endian.Little, + (addr) => + { + if (addr < 0 || addr >= 65536) + { + throw new ArgumentOutOfRangeException(); + } + return _machine.ReadBus((ushort)addr); + }, + (addr, value) => + { + if (addr < 0 || addr >= 65536) + { + throw new ArgumentOutOfRangeException(); + } + _machine.WriteBus((ushort)addr, value); + }, 1) + }; - SyncAllByteArrayDomains(); + SyncAllByteArrayDomains(); - memoryDomains = new MemoryDomainList(_byteArrayDomains.Values.Concat(domains).ToList()); - (ServiceProvider as BasicServiceProvider).Register(memoryDomains); + memoryDomains = new MemoryDomainList(_byteArrayDomains.Values.Concat(domains).ToList()); + (ServiceProvider as BasicServiceProvider).Register(memoryDomains); - _memoryDomainsInit = true; - } + _memoryDomainsInit = true; + } - private void SyncAllByteArrayDomains() - { - SyncByteArrayDomain("ROMLower", _machine.ROMLower); - SyncByteArrayDomain("ROM0", _machine.ROM0); - SyncByteArrayDomain("ROM7", _machine.ROM7); - SyncByteArrayDomain("RAM0", _machine.RAM0); - SyncByteArrayDomain("RAM1", _machine.RAM1); - SyncByteArrayDomain("RAM2", _machine.RAM2); - SyncByteArrayDomain("RAM3", _machine.RAM3); - SyncByteArrayDomain("RAM4", _machine.RAM4); - SyncByteArrayDomain("RAM5", _machine.RAM5); - SyncByteArrayDomain("RAM6", _machine.RAM6); - SyncByteArrayDomain("RAM7", _machine.RAM7); - } + private void SyncAllByteArrayDomains() + { + SyncByteArrayDomain("ROMLower", _machine.ROMLower); + SyncByteArrayDomain("ROM0", _machine.ROM0); + SyncByteArrayDomain("ROM7", _machine.ROM7); + SyncByteArrayDomain("RAM0", _machine.RAM0); + SyncByteArrayDomain("RAM1", _machine.RAM1); + SyncByteArrayDomain("RAM2", _machine.RAM2); + SyncByteArrayDomain("RAM3", _machine.RAM3); + SyncByteArrayDomain("RAM4", _machine.RAM4); + SyncByteArrayDomain("RAM5", _machine.RAM5); + SyncByteArrayDomain("RAM6", _machine.RAM6); + SyncByteArrayDomain("RAM7", _machine.RAM7); + } - private void SyncByteArrayDomain(string name, byte[] data) - { - if (_memoryDomainsInit || _byteArrayDomains.ContainsKey(name)) - { - var m = _byteArrayDomains[name]; - m.Data = data; - } - else - { - var m = new MemoryDomainByteArray(name, MemoryDomain.Endian.Little, data, true, 1); - _byteArrayDomains.Add(name, m); - } - } - } + private void SyncByteArrayDomain(string name, byte[] data) + { + if (_memoryDomainsInit || _byteArrayDomains.ContainsKey(name)) + { + var m = _byteArrayDomains[name]; + m.Data = data; + } + else + { + var m = new MemoryDomainByteArray(name, MemoryDomain.Endian.Little, data, true, 1); + _byteArrayDomains.Add(name, m); + } + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.ISettable.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.ISettable.cs index d6960d93e0..158d087582 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.ISettable.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.ISettable.cs @@ -6,190 +6,190 @@ using System.Text; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// CPCHawk: Core Class - /// * ISettable * - /// - public partial class AmstradCPC : ISettable - { - internal AmstradCPCSettings Settings = new AmstradCPCSettings(); - internal AmstradCPCSyncSettings SyncSettings = new AmstradCPCSyncSettings(); + /// + /// CPCHawk: Core Class + /// * ISettable * + /// + public partial class AmstradCPC : ISettable + { + internal AmstradCPCSettings Settings = new AmstradCPCSettings(); + internal AmstradCPCSyncSettings SyncSettings = new AmstradCPCSyncSettings(); - public AmstradCPCSettings GetSettings() - { - return Settings.Clone(); - } + public AmstradCPCSettings GetSettings() + { + return Settings.Clone(); + } - public AmstradCPCSyncSettings GetSyncSettings() - { - return SyncSettings.Clone(); - } + public AmstradCPCSyncSettings GetSyncSettings() + { + return SyncSettings.Clone(); + } - public bool PutSettings(AmstradCPCSettings o) - { - - // restore user settings to devices - if (_machine != null && _machine.AYDevice != null) - { - ((AY38912)_machine.AYDevice as AY38912).PanningConfiguration = o.AYPanConfig; - _machine.AYDevice.Volume = o.AYVolume; - } - if (_machine != null && _machine.TapeBuzzer != null) - { - ((Beeper)_machine.TapeBuzzer as Beeper).Volume = o.TapeVolume; - } - + public bool PutSettings(AmstradCPCSettings o) + { - Settings = o; + // restore user settings to devices + if (_machine != null && _machine.AYDevice != null) + { + ((AY38912)_machine.AYDevice as AY38912).PanningConfiguration = o.AYPanConfig; + _machine.AYDevice.Volume = o.AYVolume; + } + if (_machine != null && _machine.TapeBuzzer != null) + { + ((Beeper)_machine.TapeBuzzer as Beeper).Volume = o.TapeVolume; + } - return false; - } - public bool PutSyncSettings(AmstradCPCSyncSettings o) - { - bool ret = AmstradCPCSyncSettings.NeedsReboot(SyncSettings, o); - SyncSettings = o; - return ret; - } + Settings = o; - public class AmstradCPCSettings - { - [DisplayName("AY-3-8912 Panning Config")] - [Description("Set the PSG panning configuration.\nThe chip has 3 audio channels that can be outputed in different configurations")] - [DefaultValue(AY38912.AYPanConfig.ABC)] - public AY38912.AYPanConfig AYPanConfig { get; set; } + return false; + } - [DisplayName("AY-3-8912 Volume")] - [Description("The AY chip volume")] - [DefaultValue(75)] - public int AYVolume { get; set; } + public bool PutSyncSettings(AmstradCPCSyncSettings o) + { + bool ret = AmstradCPCSyncSettings.NeedsReboot(SyncSettings, o); + SyncSettings = o; + return ret; + } - [DisplayName("Core OSD Message Verbosity")] - [Description("Full: Display all GUI messages\nMedium: Display only emulator/device generated messages\nNone: Show no messages")] - [DefaultValue(OSDVerbosity.Medium)] - public OSDVerbosity OSDMessageVerbosity { get; set; } + public class AmstradCPCSettings + { + [DisplayName("AY-3-8912 Panning Config")] + [Description("Set the PSG panning configuration.\nThe chip has 3 audio channels that can be outputed in different configurations")] + [DefaultValue(AY38912.AYPanConfig.ABC)] + public AY38912.AYPanConfig AYPanConfig { get; set; } - [DisplayName("Tape Loading Volume")] - [Description("The buzzer volume when the tape is playing")] - [DefaultValue(50)] - public int TapeVolume { get; set; } + [DisplayName("AY-3-8912 Volume")] + [Description("The AY chip volume")] + [DefaultValue(75)] + public int AYVolume { get; set; } - public AmstradCPCSettings Clone() - { - return (AmstradCPCSettings)MemberwiseClone(); - } + [DisplayName("Core OSD Message Verbosity")] + [Description("Full: Display all GUI messages\nMedium: Display only emulator/device generated messages\nNone: Show no messages")] + [DefaultValue(OSDVerbosity.Medium)] + public OSDVerbosity OSDMessageVerbosity { get; set; } - public AmstradCPCSettings() - { - BizHawk.Common.SettingsUtil.SetDefaultValues(this); - } - } + [DisplayName("Tape Loading Volume")] + [Description("The buzzer volume when the tape is playing")] + [DefaultValue(50)] + public int TapeVolume { get; set; } - public class AmstradCPCSyncSettings - { - [DisplayName("Deterministic Emulation")] - [Description("If true, the core agrees to behave in a completely deterministic manner")] - [DefaultValue(true)] - public bool DeterministicEmulation { get; set; } + public AmstradCPCSettings Clone() + { + return (AmstradCPCSettings)MemberwiseClone(); + } - [DisplayName("CPC Model")] - [Description("The model of Amstrad CPC machine to be emulated")] - [DefaultValue(MachineType.CPC464)] - public MachineType MachineType { get; set; } + public AmstradCPCSettings() + { + BizHawk.Common.SettingsUtil.SetDefaultValues(this); + } + } - [DisplayName("Auto Start/Stop Tape")] - [Description("If true, CPCHawk will automatically start and stop the tape when the tape motor is triggered")] - [DefaultValue(true)] - public bool AutoStartStopTape { get; set; } + public class AmstradCPCSyncSettings + { + [DisplayName("Deterministic Emulation")] + [Description("If true, the core agrees to behave in a completely deterministic manner")] + [DefaultValue(true)] + public bool DeterministicEmulation { get; set; } - [DisplayName("Border type")] - [Description("Select how to show the border area")] - [DefaultValue(BorderType.Uniform)] - public BorderType BorderType { get; set; } + [DisplayName("CPC Model")] + [Description("The model of Amstrad CPC machine to be emulated")] + [DefaultValue(MachineType.CPC464)] + public MachineType MachineType { get; set; } - public AmstradCPCSyncSettings Clone() - { - return (AmstradCPCSyncSettings)MemberwiseClone(); - } + [DisplayName("Auto Start/Stop Tape")] + [Description("If true, CPCHawk will automatically start and stop the tape when the tape motor is triggered")] + [DefaultValue(true)] + public bool AutoStartStopTape { get; set; } - public AmstradCPCSyncSettings() - { - BizHawk.Common.SettingsUtil.SetDefaultValues(this); - } + [DisplayName("Border type")] + [Description("Select how to show the border area")] + [DefaultValue(BorderType.Uniform)] + public BorderType BorderType { get; set; } - public static bool NeedsReboot(AmstradCPCSyncSettings x, AmstradCPCSyncSettings y) - { - return !DeepEquality.DeepEquals(x, y); - } - } + public AmstradCPCSyncSettings Clone() + { + return (AmstradCPCSyncSettings)MemberwiseClone(); + } - /// - /// Verbosity of the CPCHawk generated OSD messages - /// - public enum OSDVerbosity - { - /// - /// Show all OSD messages - /// - Full, - /// - /// Only show machine/device generated messages - /// - Medium, - /// - /// No core-driven OSD messages - /// - None - } + public AmstradCPCSyncSettings() + { + BizHawk.Common.SettingsUtil.SetDefaultValues(this); + } - /// - /// Provides information on each emulated machine - /// - public class CPCMachineMetaData - { - public MachineType MachineType { get; set; } - public string Name { get; set; } - public string Description { get; set; } - public string Released { get; set; } - public string CPU { get; set; } - public string Memory { get; set; } - public string Video { get; set; } - public string Audio { get; set; } - public string Media { get; set; } - public string OtherMisc { get; set; } + public static bool NeedsReboot(AmstradCPCSyncSettings x, AmstradCPCSyncSettings y) + { + return !DeepEquality.DeepEquals(x, y); + } + } + + /// + /// Verbosity of the CPCHawk generated OSD messages + /// + public enum OSDVerbosity + { + /// + /// Show all OSD messages + /// + Full, + /// + /// Only show machine/device generated messages + /// + Medium, + /// + /// No core-driven OSD messages + /// + None + } + + /// + /// Provides information on each emulated machine + /// + public class CPCMachineMetaData + { + public MachineType MachineType { get; set; } + public string Name { get; set; } + public string Description { get; set; } + public string Released { get; set; } + public string CPU { get; set; } + public string Memory { get; set; } + public string Video { get; set; } + public string Audio { get; set; } + public string Media { get; set; } + public string OtherMisc { get; set; } Dictionary Data = new Dictionary(); public static CPCMachineMetaData GetMetaObject(MachineType type) - { - CPCMachineMetaData m = new CPCMachineMetaData(); - m.MachineType = type; + { + CPCMachineMetaData m = new CPCMachineMetaData(); + m.MachineType = type; - switch (type) - { - case MachineType.CPC464: - m.Name = "Amstrad CPC 464"; - m.Description = "The CPC 464 was the first personal home computer built by Amstrad in 1984. "; - m.Description += "The 464 was popular with consumers for various reasons. Aside from the joystick port, the computer, keyboard, and tape deck were all combined into one unit."; - m.Released = "1984"; - m.CPU = "Zilog Z80A @ 4MHz"; - m.Memory = "64KB RAM / 32KB ROM"; - m.Video = "Amstrad Gate Array @ 16Mhz & CRCT @ 1Mhz"; - m.Audio = "General Instruments AY-3-8912 PSG (3ch)"; - m.Media = "Cassette Tape (via built-in Datacorder)"; - break; - case MachineType.CPC6128: - m.Name = "Amstrad CPC 6128"; - m.Description = "The CPC6128 features 128 KB RAM and an internal 3-inch floppy disk drive. "; - m.Description += "Aside from various hardware and firmware improvements, one of the CPC6128's most prominent features is the compatibility with the CP/M+ operating system that rendered it attractive for business uses."; - m.Released = "1985"; - m.CPU = "Zilog Z80A @ 4MHz"; - m.Memory = "64KB RAM / 32KB ROM"; - m.Video = "Amstrad Gate Array @ 16Mhz & CRCT @ 1Mhz"; - m.Audio = "General Instruments AY-3-8912 PSG (3ch)"; - m.Media = "3\" Floppy Disk (via built-in Floppy Drive) & Cassette Tape (via external cassette player)"; - break; - } + switch (type) + { + case MachineType.CPC464: + m.Name = "Amstrad CPC 464"; + m.Description = "The CPC 464 was the first personal home computer built by Amstrad in 1984. "; + m.Description += "The 464 was popular with consumers for various reasons. Aside from the joystick port, the computer, keyboard, and tape deck were all combined into one unit."; + m.Released = "1984"; + m.CPU = "Zilog Z80A @ 4MHz"; + m.Memory = "64KB RAM / 32KB ROM"; + m.Video = "Amstrad Gate Array @ 16Mhz & CRCT @ 1Mhz"; + m.Audio = "General Instruments AY-3-8912 PSG (3ch)"; + m.Media = "Cassette Tape (via built-in Datacorder)"; + break; + case MachineType.CPC6128: + m.Name = "Amstrad CPC 6128"; + m.Description = "The CPC6128 features 128 KB RAM and an internal 3-inch floppy disk drive. "; + m.Description += "Aside from various hardware and firmware improvements, one of the CPC6128's most prominent features is the compatibility with the CP/M+ operating system that rendered it attractive for business uses."; + m.Released = "1985"; + m.CPU = "Zilog Z80A @ 4MHz"; + m.Memory = "64KB RAM / 32KB ROM"; + m.Video = "Amstrad Gate Array @ 16Mhz & CRCT @ 1Mhz"; + m.Audio = "General Instruments AY-3-8912 PSG (3ch)"; + m.Media = "3\" Floppy Disk (via built-in Floppy Drive) & Cassette Tape (via external cassette player)"; + break; + } m.Data.Add(AmstradCPC.GetMemberName((CPCMachineMetaData c) => c.Name), m.Name.Trim()); m.Data.Add(AmstradCPC.GetMemberName((CPCMachineMetaData c) => c.Description), m.Description.Trim()); @@ -201,7 +201,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC m.Data.Add(AmstradCPC.GetMemberName((CPCMachineMetaData c) => c.Media), m.Media.Trim()); return m; - } + } public static string GetMetaString(MachineType type) { @@ -275,75 +275,75 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC } public static string GetMetaStringOld(MachineType type) - { - var m = GetMetaObject(type); + { + var m = GetMetaObject(type); - StringBuilder sb = new StringBuilder(); + StringBuilder sb = new StringBuilder(); - sb.Append(m.Name); - sb.Append("\n"); - sb.Append("-----------------------------------------------------------------\n"); - // Release - sb.Append("Released:"); - sb.Append(" "); - sb.Append(m.Released); - sb.Append("\n"); - // CPU - sb.Append("CPU:"); - sb.Append(" "); - sb.Append(m.CPU); - sb.Append("\n"); - // Memory - sb.Append("Memory:"); - sb.Append(" "); - sb.Append(m.Memory); - sb.Append("\n"); - // Video - sb.Append("Video:"); - sb.Append(" "); - sb.Append(m.Video); - sb.Append("\n"); - // Audio - sb.Append("Audio:"); - sb.Append(" "); - sb.Append(m.Audio); - sb.Append("\n"); - // Audio - sb.Append("Media:"); - sb.Append(" "); - sb.Append(m.Media); - sb.Append("\n"); + sb.Append(m.Name); + sb.Append("\n"); + sb.Append("-----------------------------------------------------------------\n"); + // Release + sb.Append("Released:"); + sb.Append(" "); + sb.Append(m.Released); + sb.Append("\n"); + // CPU + sb.Append("CPU:"); + sb.Append(" "); + sb.Append(m.CPU); + sb.Append("\n"); + // Memory + sb.Append("Memory:"); + sb.Append(" "); + sb.Append(m.Memory); + sb.Append("\n"); + // Video + sb.Append("Video:"); + sb.Append(" "); + sb.Append(m.Video); + sb.Append("\n"); + // Audio + sb.Append("Audio:"); + sb.Append(" "); + sb.Append(m.Audio); + sb.Append("\n"); + // Audio + sb.Append("Media:"); + sb.Append(" "); + sb.Append(m.Media); + sb.Append("\n"); - sb.Append("-----------------------------------------------------------------\n"); - // description - sb.Append(m.Description); - if (m.OtherMisc != null) - sb.Append("\n" + m.OtherMisc); + sb.Append("-----------------------------------------------------------------\n"); + // description + sb.Append(m.Description); + if (m.OtherMisc != null) + sb.Append("\n" + m.OtherMisc); - return sb.ToString(); + return sb.ToString(); - } - } + } + } - /// - /// The size of the Spectrum border - /// - public enum BorderType - { - /// - /// Attempts to equalise the border areas - /// - Uniform, + /// + /// The size of the Spectrum border + /// + public enum BorderType + { + /// + /// Attempts to equalise the border areas + /// + Uniform, - /// - /// Pretty much the signal the gate array is generating (looks shit) - /// - Uncropped, + /// + /// Pretty much the signal the gate array is generating (looks shit) + /// + Uncropped, - /// - /// Top and bottom border removed so that the result is *almost* 16:9 - /// - Widescreen, - } - } + /// + /// Top and bottom border removed so that the result is *almost* 16:9 + /// + Widescreen, + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.IStatable.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.IStatable.cs index 92bc33c506..5536d2c7d9 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.IStatable.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.IStatable.cs @@ -4,94 +4,94 @@ using BizHawk.Emulation.Common; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// CPCHawk: Core Class - /// * IStatable * - /// - public partial class AmstradCPC : IStatable - { - public bool BinarySaveStatesPreferred => true; + /// + /// CPCHawk: Core Class + /// * IStatable * + /// + public partial class AmstradCPC : IStatable + { + public bool BinarySaveStatesPreferred => true; public void SaveStateText(TextWriter writer) - { - SyncState(new Serializer(writer)); - } + { + SyncState(new Serializer(writer)); + } - public void LoadStateText(TextReader reader) - { - SyncState(new Serializer(reader)); - } + public void LoadStateText(TextReader reader) + { + SyncState(new Serializer(reader)); + } - public void SaveStateBinary(BinaryWriter bw) - { - SyncState(new Serializer(bw)); - } + public void SaveStateBinary(BinaryWriter bw) + { + SyncState(new Serializer(bw)); + } - public void LoadStateBinary(BinaryReader br) - { - SyncState(new Serializer(br)); - } + public void LoadStateBinary(BinaryReader br) + { + SyncState(new Serializer(br)); + } - public byte[] SaveStateBinary() - { - using var ms = new MemoryStream(); + public byte[] SaveStateBinary() + { + using var ms = new MemoryStream(); using var bw = new BinaryWriter(ms); - SaveStateBinary(bw); - bw.Flush(); - return ms.ToArray(); - } + SaveStateBinary(bw); + bw.Flush(); + return ms.ToArray(); + } - private void SyncState(Serializer ser) - { - byte[] core = null; - if (ser.IsWriter) - { - var ms = new MemoryStream(); - ms.Close(); - core = ms.ToArray(); - } + private void SyncState(Serializer ser) + { + byte[] core = null; + if (ser.IsWriter) + { + var ms = new MemoryStream(); + ms.Close(); + core = ms.ToArray(); + } - if (ser.IsWriter) - { - ser.SyncEnum(nameof(_machineType), ref _machineType); + if (ser.IsWriter) + { + ser.SyncEnum(nameof(_machineType), ref _machineType); - _cpu.SyncState(ser); - ser.BeginSection(nameof(AmstradCPC)); - _machine.SyncState(ser); - ser.Sync("Frame", ref _machine.FrameCount); - ser.Sync("LagCount", ref _lagCount); - ser.Sync("IsLag", ref _isLag); - ser.EndSection(); - } + _cpu.SyncState(ser); + ser.BeginSection(nameof(AmstradCPC)); + _machine.SyncState(ser); + ser.Sync("Frame", ref _machine.FrameCount); + ser.Sync("LagCount", ref _lagCount); + ser.Sync("IsLag", ref _isLag); + ser.EndSection(); + } - if (ser.IsReader) - { - var tmpM = _machineType; - ser.SyncEnum(nameof(_machineType), ref _machineType); - if (tmpM != _machineType && _machineType.ToString() != "72") - { - string msg = "SAVESTATE FAILED TO LOAD!!\n\n"; - msg += "Current Configuration: " + tmpM.ToString(); - msg += "\n"; - msg += "Saved Configuration: " + _machineType.ToString(); - msg += "\n\n"; - msg += "If you wish to load this SaveState ensure that you have the correct machine configuration selected, reboot the core, then try again."; - CoreComm.ShowMessage(msg); - _machineType = tmpM; - } - else - { - _cpu.SyncState(ser); - ser.BeginSection(nameof(AmstradCPC)); - _machine.SyncState(ser); - ser.Sync("Frame", ref _machine.FrameCount); - ser.Sync("LagCount", ref _lagCount); - ser.Sync("IsLag", ref _isLag); - ser.EndSection(); + if (ser.IsReader) + { + var tmpM = _machineType; + ser.SyncEnum(nameof(_machineType), ref _machineType); + if (tmpM != _machineType && _machineType.ToString() != "72") + { + string msg = "SAVESTATE FAILED TO LOAD!!\n\n"; + msg += "Current Configuration: " + tmpM.ToString(); + msg += "\n"; + msg += "Saved Configuration: " + _machineType.ToString(); + msg += "\n\n"; + msg += "If you wish to load this SaveState ensure that you have the correct machine configuration selected, reboot the core, then try again."; + CoreComm.ShowMessage(msg); + _machineType = tmpM; + } + else + { + _cpu.SyncState(ser); + ser.BeginSection(nameof(AmstradCPC)); + _machine.SyncState(ser); + ser.Sync("Frame", ref _machine.FrameCount); + ser.Sync("LagCount", ref _lagCount); + ser.Sync("IsLag", ref _isLag); + ser.EndSection(); - SyncAllByteArrayDomains(); - } - } - } - } + SyncAllByteArrayDomains(); + } + } + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.InputPollable.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.InputPollable.cs index 6d5bd4ea90..dfe8866e46 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.InputPollable.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.InputPollable.cs @@ -3,27 +3,27 @@ using BizHawk.Emulation.Common; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// CPCHawk: Core Class - /// * IInputPollable * - /// - public partial class AmstradCPC : IInputPollable - { - public int LagCount - { - get { return _lagCount; } - set { _lagCount = value; } - } + /// + /// CPCHawk: Core Class + /// * IInputPollable * + /// + public partial class AmstradCPC : IInputPollable + { + public int LagCount + { + get { return _lagCount; } + set { _lagCount = value; } + } - public bool IsLagFrame - { - get { return _isLag; } - set { _isLag = value; } - } + public bool IsLagFrame + { + get { return _isLag; } + set { _isLag = value; } + } - public IInputCallbackSystem InputCallbacks { get; } + public IInputCallbackSystem InputCallbacks { get; } - private int _lagCount = 0; - private bool _isLag = false; - } + private int _lagCount = 0; + private bool _isLag = false; + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.Messaging.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.Messaging.cs index 7f47a0fb50..6d490092b3 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.Messaging.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.Messaging.cs @@ -4,127 +4,127 @@ using System.Text; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// CPCHawk: Core Class - /// * Handles all messaging (OSD) operations * - /// - public partial class AmstradCPC - { - /// - /// Writes a message to the OSD - /// - public void SendMessage(string message, MessageCategory category) - { - if (!CheckMessageSettings(category)) - return; + /// + /// CPCHawk: Core Class + /// * Handles all messaging (OSD) operations * + /// + public partial class AmstradCPC + { + /// + /// Writes a message to the OSD + /// + public void SendMessage(string message, MessageCategory category) + { + if (!CheckMessageSettings(category)) + return; - StringBuilder sb = new StringBuilder(); + StringBuilder sb = new StringBuilder(); - switch (category) - { - case MessageCategory.Tape: - sb.Append("DATACORDER: "); - sb.Append(message); - break; - case MessageCategory.Input: - sb.Append("INPUT DETECTED: "); - sb.Append(message); - break; - case MessageCategory.Disk: - sb.Append("DISK DRIVE: "); - sb.Append(message); - break; - case MessageCategory.Emulator: - case MessageCategory.Misc: - sb.Append("CPCHAWK: "); - sb.Append(message); - break; - } + switch (category) + { + case MessageCategory.Tape: + sb.Append("DATACORDER: "); + sb.Append(message); + break; + case MessageCategory.Input: + sb.Append("INPUT DETECTED: "); + sb.Append(message); + break; + case MessageCategory.Disk: + sb.Append("DISK DRIVE: "); + sb.Append(message); + break; + case MessageCategory.Emulator: + case MessageCategory.Misc: + sb.Append("CPCHAWK: "); + sb.Append(message); + break; + } - CoreComm.Notify(sb.ToString()); - } + CoreComm.Notify(sb.ToString()); + } - #region Input Message Methods + #region Input Message Methods - /// - /// Called when certain input presses are detected - /// - public void OSD_FireInputMessage(string input) - { - StringBuilder sb = new StringBuilder(); - sb.Append(input); - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Input); - } + /// + /// Called when certain input presses are detected + /// + public void OSD_FireInputMessage(string input) + { + StringBuilder sb = new StringBuilder(); + sb.Append(input); + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Input); + } - #endregion + #endregion - #region DiskDevice Message Methods + #region DiskDevice Message Methods - /// - /// Disk message that is fired on core init - /// - public void OSD_DiskInit() - { - StringBuilder sb = new StringBuilder(); - if (_machine.diskImages != null && _machine.UPDDiskDevice != null) - { - sb.Append("Disk Media Imported (count: " + _machine.diskImages.Count() + ")"); - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Emulator); - } - } + /// + /// Disk message that is fired on core init + /// + public void OSD_DiskInit() + { + StringBuilder sb = new StringBuilder(); + if (_machine.diskImages != null && _machine.UPDDiskDevice != null) + { + sb.Append("Disk Media Imported (count: " + _machine.diskImages.Count() + ")"); + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Emulator); + } + } - /// - /// Disk message that is fired when a new disk is inserted into the drive - /// - public void OSD_DiskInserted() - { - StringBuilder sb = new StringBuilder(); + /// + /// Disk message that is fired when a new disk is inserted into the drive + /// + public void OSD_DiskInserted() + { + StringBuilder sb = new StringBuilder(); - if (_machine.UPDDiskDevice == null) - { - sb.Append("No Drive Present"); - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Disk); - return; - } + if (_machine.UPDDiskDevice == null) + { + sb.Append("No Drive Present"); + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Disk); + return; + } - sb.Append("DISK INSERTED (" + _machine.DiskMediaIndex + ": " + _diskInfo[_machine.DiskMediaIndex].Name + ")"); - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Disk); - } + sb.Append("DISK INSERTED (" + _machine.DiskMediaIndex + ": " + _diskInfo[_machine.DiskMediaIndex].Name + ")"); + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Disk); + } - /// - /// Tape message that prints the current status of the tape device - /// - public void OSD_ShowDiskStatus() - { - StringBuilder sb = new StringBuilder(); + /// + /// Tape message that prints the current status of the tape device + /// + public void OSD_ShowDiskStatus() + { + StringBuilder sb = new StringBuilder(); - if (_machine.UPDDiskDevice == null) - { - sb.Append("No Drive Present"); - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Disk); - return; - } + if (_machine.UPDDiskDevice == null) + { + sb.Append("No Drive Present"); + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Disk); + return; + } - if (_diskInfo.Count == 0) - { - sb.Append("No Disk Loaded"); - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Disk); - return; - } + if (_diskInfo.Count == 0) + { + sb.Append("No Disk Loaded"); + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Disk); + return; + } - if (_machine.UPDDiskDevice != null) - { - if (_machine.UPDDiskDevice.DiskPointer == null) - { - sb.Append("No Disk Loaded"); - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Disk); - return; - } + if (_machine.UPDDiskDevice != null) + { + if (_machine.UPDDiskDevice.DiskPointer == null) + { + sb.Append("No Disk Loaded"); + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Disk); + return; + } - sb.Append("Disk: " + _machine.DiskMediaIndex + ": " + _diskInfo[_machine.DiskMediaIndex].Name); - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Disk); - sb.Clear(); - /* + sb.Append("Disk: " + _machine.DiskMediaIndex + ": " + _diskInfo[_machine.DiskMediaIndex].Name); + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Disk); + sb.Clear(); + /* string protection = "None"; protection = Enum.GetName(typeof(ProtectionType), _machine.UPDDiskDevice.DiskPointer.Protection); if (protection == "None") @@ -135,384 +135,384 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC sb.Clear(); */ - sb.Append("Status: "); - - if (_machine.UPDDiskDevice.DriveLight) - sb.Append("READING/WRITING DATA"); - else - sb.Append("UNKNOWN"); - - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Disk); - sb.Clear(); - } - } - - #endregion - - #region TapeDevice Message Methods - - /// - /// Tape message that is fired on core init - /// - public void OSD_TapeInit() - { - if (_tapeInfo.Count == 0) - return; - - StringBuilder sb = new StringBuilder(); - sb.Append("Tape Media Imported (count: " + _tapeInfo.Count() + ")"); - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Emulator); - } - - /// - /// Tape message that is fired when tape is playing - /// - public void OSD_TapeMotorActive() - { - if (_tapeInfo.Count == 0) - return; - - StringBuilder sb = new StringBuilder(); - sb.Append("MOTOR ON (" + _machine.TapeMediaIndex + ": " + _tapeInfo[_machine.TapeMediaIndex].Name + ")"); - - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - } - - /// - /// Tape message that is fired when tape is playing - /// - public void OSD_TapeMotorInactive() - { - if (_tapeInfo.Count == 0) - return; - - StringBuilder sb = new StringBuilder(); - sb.Append("MOTOR OFF (" + _machine.TapeMediaIndex + ": " + _tapeInfo[_machine.TapeMediaIndex].Name + ")"); - - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - } - - /// - /// Tape message that is fired when tape is playing - /// - public void OSD_TapePlaying() - { - if (_tapeInfo.Count == 0) - return; - - StringBuilder sb = new StringBuilder(); - sb.Append("PLAYING MANUAL (" + _machine.TapeMediaIndex + ": " + _tapeInfo[_machine.TapeMediaIndex].Name + ")"); - - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - } - - /// - /// Tape message that is fired when tape is stopped - /// - public void OSD_TapeStopped() - { - if (_tapeInfo.Count == 0) - return; - - StringBuilder sb = new StringBuilder(); - sb.Append("STOPPED MANUAL (" + _machine.TapeMediaIndex + ": " + _tapeInfo[_machine.TapeMediaIndex].Name + ")"); - - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - } - - /// - /// Tape message that is fired when tape is rewound - /// - public void OSD_TapeRTZ() - { - if (_tapeInfo.Count == 0) - return; - - StringBuilder sb = new StringBuilder(); - sb.Append("REWOUND (" + _machine.TapeMediaIndex + ": " + _tapeInfo[_machine.TapeMediaIndex].Name + ")"); - - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - } - - /// - /// Tape message that is fired when a new tape is inserted into the datacorder - /// - public void OSD_TapeInserted() - { - if (_tapeInfo.Count == 0) - return; - - StringBuilder sb = new StringBuilder(); - sb.Append("TAPE INSERTED (" + _machine.TapeMediaIndex + ": " + _tapeInfo[_machine.TapeMediaIndex].Name + ")"); - - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - } - - - /// - /// Tape message that is fired when a tape is stopped automatically - /// - public void OSD_TapeStoppedAuto() - { - StringBuilder sb = new StringBuilder(); - - if (_tapeInfo.Count == 0) - { - sb.Append("No Tape Loaded"); - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - return; - } - - - sb.Append("STOPPED (Auto Tape Trap Detected)"); - - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - } - - /// - /// Tape message that is fired when a tape is started automatically - /// - public void OSD_TapePlayingAuto() - { - StringBuilder sb = new StringBuilder(); - - if (_tapeInfo.Count == 0) - { - sb.Append("No Tape Loaded"); - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - return; - } - - - sb.Append("PLAYING (Auto Tape Trap Detected)"); + sb.Append("Status: "); + + if (_machine.UPDDiskDevice.DriveLight) + sb.Append("READING/WRITING DATA"); + else + sb.Append("UNKNOWN"); + + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Disk); + sb.Clear(); + } + } + + #endregion + + #region TapeDevice Message Methods + + /// + /// Tape message that is fired on core init + /// + public void OSD_TapeInit() + { + if (_tapeInfo.Count == 0) + return; + + StringBuilder sb = new StringBuilder(); + sb.Append("Tape Media Imported (count: " + _tapeInfo.Count() + ")"); + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Emulator); + } + + /// + /// Tape message that is fired when tape is playing + /// + public void OSD_TapeMotorActive() + { + if (_tapeInfo.Count == 0) + return; + + StringBuilder sb = new StringBuilder(); + sb.Append("MOTOR ON (" + _machine.TapeMediaIndex + ": " + _tapeInfo[_machine.TapeMediaIndex].Name + ")"); + + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + } + + /// + /// Tape message that is fired when tape is playing + /// + public void OSD_TapeMotorInactive() + { + if (_tapeInfo.Count == 0) + return; + + StringBuilder sb = new StringBuilder(); + sb.Append("MOTOR OFF (" + _machine.TapeMediaIndex + ": " + _tapeInfo[_machine.TapeMediaIndex].Name + ")"); + + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + } + + /// + /// Tape message that is fired when tape is playing + /// + public void OSD_TapePlaying() + { + if (_tapeInfo.Count == 0) + return; + + StringBuilder sb = new StringBuilder(); + sb.Append("PLAYING MANUAL (" + _machine.TapeMediaIndex + ": " + _tapeInfo[_machine.TapeMediaIndex].Name + ")"); + + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + } + + /// + /// Tape message that is fired when tape is stopped + /// + public void OSD_TapeStopped() + { + if (_tapeInfo.Count == 0) + return; + + StringBuilder sb = new StringBuilder(); + sb.Append("STOPPED MANUAL (" + _machine.TapeMediaIndex + ": " + _tapeInfo[_machine.TapeMediaIndex].Name + ")"); + + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + } + + /// + /// Tape message that is fired when tape is rewound + /// + public void OSD_TapeRTZ() + { + if (_tapeInfo.Count == 0) + return; + + StringBuilder sb = new StringBuilder(); + sb.Append("REWOUND (" + _machine.TapeMediaIndex + ": " + _tapeInfo[_machine.TapeMediaIndex].Name + ")"); + + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + } + + /// + /// Tape message that is fired when a new tape is inserted into the datacorder + /// + public void OSD_TapeInserted() + { + if (_tapeInfo.Count == 0) + return; + + StringBuilder sb = new StringBuilder(); + sb.Append("TAPE INSERTED (" + _machine.TapeMediaIndex + ": " + _tapeInfo[_machine.TapeMediaIndex].Name + ")"); + + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + } + + + /// + /// Tape message that is fired when a tape is stopped automatically + /// + public void OSD_TapeStoppedAuto() + { + StringBuilder sb = new StringBuilder(); + + if (_tapeInfo.Count == 0) + { + sb.Append("No Tape Loaded"); + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + return; + } + + + sb.Append("STOPPED (Auto Tape Trap Detected)"); + + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + } + + /// + /// Tape message that is fired when a tape is started automatically + /// + public void OSD_TapePlayingAuto() + { + StringBuilder sb = new StringBuilder(); + + if (_tapeInfo.Count == 0) + { + sb.Append("No Tape Loaded"); + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + return; + } + + + sb.Append("PLAYING (Auto Tape Trap Detected)"); - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - } + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + } - /// - /// Tape message that is fired when a new block starts playing - /// - public void OSD_TapePlayingBlockInfo(string blockinfo) - { - StringBuilder sb = new StringBuilder(); - - if (_tapeInfo.Count == 0) - { - sb.Append("No Tape Loaded"); - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - return; - } - - - sb.Append("...Starting Block " + blockinfo); - - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - } - - /// - /// Tape message that is fired when a tape block is skipped (because it is empty) - /// - public void OSD_TapePlayingSkipBlockInfo(string blockinfo) - { - StringBuilder sb = new StringBuilder(); - - if (_tapeInfo.Count == 0) - { - sb.Append("No Tape Loaded"); - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - return; - } - - - sb.Append("...Skipping Empty Block " + blockinfo); + /// + /// Tape message that is fired when a new block starts playing + /// + public void OSD_TapePlayingBlockInfo(string blockinfo) + { + StringBuilder sb = new StringBuilder(); + + if (_tapeInfo.Count == 0) + { + sb.Append("No Tape Loaded"); + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + return; + } + + + sb.Append("...Starting Block " + blockinfo); + + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + } + + /// + /// Tape message that is fired when a tape block is skipped (because it is empty) + /// + public void OSD_TapePlayingSkipBlockInfo(string blockinfo) + { + StringBuilder sb = new StringBuilder(); + + if (_tapeInfo.Count == 0) + { + sb.Append("No Tape Loaded"); + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + return; + } + + + sb.Append("...Skipping Empty Block " + blockinfo); - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - } - - /// - /// Tape message that is fired when a tape is started automatically - /// - public void OSD_TapeEndDetected(string blockinfo) - { - StringBuilder sb = new StringBuilder(); - - if (_tapeInfo.Count == 0) - { - sb.Append("No Tape Loaded"); - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - return; - } - - - sb.Append("...Skipping Empty Block " + blockinfo); - - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - } - - /// - /// Tape message that is fired when user has manually skipped to the next block - /// - public void OSD_TapeNextBlock(string blockinfo) - { - StringBuilder sb = new StringBuilder(); - - if (_tapeInfo.Count == 0) - { - sb.Append("No Tape Loaded"); - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - return; - } - - - sb.Append("Manual Skip Next " + blockinfo); - - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - } - - /// - /// Tape message that is fired when user has manually skipped to the next block - /// - public void OSD_TapePrevBlock(string blockinfo) - { - StringBuilder sb = new StringBuilder(); - - if (_tapeInfo.Count == 0) - { - sb.Append("No Tape Loaded"); - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - return; - } - - - sb.Append("Manual Skip Prev " + blockinfo); - - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - } - - /// - /// Tape message that prints the current status of the tape device - /// - public void OSD_ShowTapeStatus() - { - StringBuilder sb = new StringBuilder(); - - if (_tapeInfo.Count == 0) - { - sb.Append("No Tape Loaded"); - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - return; - } - - sb.Append("Status: "); - - if (_machine.TapeDevice.TapeIsPlaying) - sb.Append("PLAYING"); - else - sb.Append("STOPPED"); - - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - sb.Clear(); - - sb.Append("Tape: " + _machine.TapeMediaIndex + ": " + _tapeInfo[_machine.TapeMediaIndex].Name); - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - sb.Clear(); - - sb.Append("Block: "); - sb.Append("(" + (_machine.TapeDevice.CurrentDataBlockIndex + 1) + - " of " + _machine.TapeDevice.DataBlocks.Count() + ") " + - _machine.TapeDevice.DataBlocks[_machine.TapeDevice.CurrentDataBlockIndex].BlockDescription); - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - sb.Clear(); - - sb.Append("Block Pos: "); - - int pos = _machine.TapeDevice.Position; - int end = _machine.TapeDevice.DataBlocks[_machine.TapeDevice.CurrentDataBlockIndex].DataPeriods.Count; - double p = 0; - if (end != 0) - p = ((double)pos / (double)end) * (double)100; - - sb.Append(p.ToString("N0") + "%"); - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - sb.Clear(); - - // get position within the tape itself - sb.Append("Tape Pos: "); - var ind = _machine.TapeDevice.CurrentDataBlockIndex; - int cnt = 0; - for (int i = 0; i < ind; i++) - { - cnt += _machine.TapeDevice.DataBlocks[i].DataPeriods.Count; - } - // now we are at our current block - int ourPos = cnt + pos; - cnt += end; - // count periods in the remaining blocks - for (int i = ind + 1; i < _machine.TapeDevice.DataBlocks.Count; i++) - { - cnt += _machine.TapeDevice.DataBlocks[i].DataPeriods.Count; - } - // work out overall position within the tape - p = 0; - p = ((double)ourPos / (double)cnt) * (double)100; - sb.Append(p.ToString("N0") + "%"); - SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); - } - - #endregion - - /// - /// Checks whether message category is allowed to be sent - /// - public bool CheckMessageSettings(MessageCategory category) - { - switch (Settings.OSDMessageVerbosity) - { - case OSDVerbosity.Full: - return true; - case OSDVerbosity.None: - return false; - case OSDVerbosity.Medium: - switch (category) - { - case MessageCategory.Disk: - case MessageCategory.Emulator: - case MessageCategory.Tape: - case MessageCategory.Misc: - return true; - default: - return false; - } - default: - return true; - } - } - - /// - /// Defines the different message categories - /// - public enum MessageCategory - { - /// - /// No defined category as such - /// - Misc, - /// - /// User generated input messages (at the moment only tape/disk controls) - /// - Input, - /// - /// Tape device generated messages - /// - Tape, - /// - /// Disk device generated messages - /// - Disk, - /// - /// Emulator generated messages - /// - Emulator - } - } + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + } + + /// + /// Tape message that is fired when a tape is started automatically + /// + public void OSD_TapeEndDetected(string blockinfo) + { + StringBuilder sb = new StringBuilder(); + + if (_tapeInfo.Count == 0) + { + sb.Append("No Tape Loaded"); + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + return; + } + + + sb.Append("...Skipping Empty Block " + blockinfo); + + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + } + + /// + /// Tape message that is fired when user has manually skipped to the next block + /// + public void OSD_TapeNextBlock(string blockinfo) + { + StringBuilder sb = new StringBuilder(); + + if (_tapeInfo.Count == 0) + { + sb.Append("No Tape Loaded"); + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + return; + } + + + sb.Append("Manual Skip Next " + blockinfo); + + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + } + + /// + /// Tape message that is fired when user has manually skipped to the next block + /// + public void OSD_TapePrevBlock(string blockinfo) + { + StringBuilder sb = new StringBuilder(); + + if (_tapeInfo.Count == 0) + { + sb.Append("No Tape Loaded"); + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + return; + } + + + sb.Append("Manual Skip Prev " + blockinfo); + + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + } + + /// + /// Tape message that prints the current status of the tape device + /// + public void OSD_ShowTapeStatus() + { + StringBuilder sb = new StringBuilder(); + + if (_tapeInfo.Count == 0) + { + sb.Append("No Tape Loaded"); + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + return; + } + + sb.Append("Status: "); + + if (_machine.TapeDevice.TapeIsPlaying) + sb.Append("PLAYING"); + else + sb.Append("STOPPED"); + + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + sb.Clear(); + + sb.Append("Tape: " + _machine.TapeMediaIndex + ": " + _tapeInfo[_machine.TapeMediaIndex].Name); + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + sb.Clear(); + + sb.Append("Block: "); + sb.Append("(" + (_machine.TapeDevice.CurrentDataBlockIndex + 1) + + " of " + _machine.TapeDevice.DataBlocks.Count() + ") " + + _machine.TapeDevice.DataBlocks[_machine.TapeDevice.CurrentDataBlockIndex].BlockDescription); + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + sb.Clear(); + + sb.Append("Block Pos: "); + + int pos = _machine.TapeDevice.Position; + int end = _machine.TapeDevice.DataBlocks[_machine.TapeDevice.CurrentDataBlockIndex].DataPeriods.Count; + double p = 0; + if (end != 0) + p = ((double)pos / (double)end) * (double)100; + + sb.Append(p.ToString("N0") + "%"); + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + sb.Clear(); + + // get position within the tape itself + sb.Append("Tape Pos: "); + var ind = _machine.TapeDevice.CurrentDataBlockIndex; + int cnt = 0; + for (int i = 0; i < ind; i++) + { + cnt += _machine.TapeDevice.DataBlocks[i].DataPeriods.Count; + } + // now we are at our current block + int ourPos = cnt + pos; + cnt += end; + // count periods in the remaining blocks + for (int i = ind + 1; i < _machine.TapeDevice.DataBlocks.Count; i++) + { + cnt += _machine.TapeDevice.DataBlocks[i].DataPeriods.Count; + } + // work out overall position within the tape + p = 0; + p = ((double)ourPos / (double)cnt) * (double)100; + sb.Append(p.ToString("N0") + "%"); + SendMessage(sb.ToString().TrimEnd('\n'), MessageCategory.Tape); + } + + #endregion + + /// + /// Checks whether message category is allowed to be sent + /// + public bool CheckMessageSettings(MessageCategory category) + { + switch (Settings.OSDMessageVerbosity) + { + case OSDVerbosity.Full: + return true; + case OSDVerbosity.None: + return false; + case OSDVerbosity.Medium: + switch (category) + { + case MessageCategory.Disk: + case MessageCategory.Emulator: + case MessageCategory.Tape: + case MessageCategory.Misc: + return true; + default: + return false; + } + default: + return true; + } + } + + /// + /// Defines the different message categories + /// + public enum MessageCategory + { + /// + /// No defined category as such + /// + Misc, + /// + /// User generated input messages (at the moment only tape/disk controls) + /// + Input, + /// + /// Tape device generated messages + /// + Tape, + /// + /// Disk device generated messages + /// + Disk, + /// + /// Emulator generated messages + /// + Emulator + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.Util.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.Util.cs index f51a83db9f..bc1ea0475c 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.Util.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.Util.cs @@ -4,48 +4,48 @@ using System.Linq.Expressions; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// CPCHawk: Core Class - /// * Misc Utilities * - /// - public partial class AmstradCPC - { - /// - /// Helper method that returns a single INT32 from a BitArray - /// - public static int GetIntFromBitArray(BitArray bitArray) - { - if (bitArray.Length > 32) - throw new ArgumentException("Argument length shall be at most 32 bits."); + /// + /// CPCHawk: Core Class + /// * Misc Utilities * + /// + public partial class AmstradCPC + { + /// + /// Helper method that returns a single INT32 from a BitArray + /// + public static int GetIntFromBitArray(BitArray bitArray) + { + if (bitArray.Length > 32) + throw new ArgumentException("Argument length shall be at most 32 bits."); - int[] array = new int[1]; - bitArray.CopyTo(array, 0); - return array[0]; - } + int[] array = new int[1]; + bitArray.CopyTo(array, 0); + return array[0]; + } - /// - /// POKEs a memory bus address - /// - public void PokeMemory(ushort addr, byte value) - { - _machine.WriteBus(addr, value); - } + /// + /// POKEs a memory bus address + /// + public void PokeMemory(ushort addr, byte value) + { + _machine.WriteBus(addr, value); + } - public string GetMachineType() - { - string m = ""; - switch (SyncSettings.MachineType) - { - case MachineType.CPC464: - m = "(Amstrad) CPC 464 (64K)"; - break; - case MachineType.CPC6128: - m = "(Amstrad) CPC 6464 (128K)"; - break; - } + public string GetMachineType() + { + string m = ""; + switch (SyncSettings.MachineType) + { + case MachineType.CPC464: + m = "(Amstrad) CPC 464 (64K)"; + break; + case MachineType.CPC6128: + m = "(Amstrad) CPC 6464 (128K)"; + break; + } - return m; - } + return m; + } public static string GetMemberName(Expression> memberAccess) { diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.cs index a11785c72d..c700adb72c 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.cs @@ -9,212 +9,212 @@ using System.Linq; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// CPCHawk: Core Class - /// * Main Initialization * - /// - [Core( - "CPCHawk", - "Asnivor", - isPorted: false, - isReleased: false)] - public partial class AmstradCPC : IRegionable, IDriveLight - { - public AmstradCPC(CoreComm comm, IEnumerable files, List game, object settings, object syncSettings) - { - var ser = new BasicServiceProvider(this); - ServiceProvider = ser; - InputCallbacks = new InputCallbackSystem(); - MemoryCallbacks = new MemoryCallbackSystem(new[] { "System Bus" }); - CoreComm = comm; - _gameInfo = game; - _cpu = new Z80A(); - _tracer = new TraceBuffer { Header = _cpu.TraceHeader }; - _files = files?.ToList() ?? new List(); + /// + /// CPCHawk: Core Class + /// * Main Initialization * + /// + [Core( + "CPCHawk", + "Asnivor", + isPorted: false, + isReleased: false)] + public partial class AmstradCPC : IRegionable, IDriveLight + { + public AmstradCPC(CoreComm comm, IEnumerable files, List game, object settings, object syncSettings) + { + var ser = new BasicServiceProvider(this); + ServiceProvider = ser; + InputCallbacks = new InputCallbackSystem(); + MemoryCallbacks = new MemoryCallbackSystem(new[] { "System Bus" }); + CoreComm = comm; + _gameInfo = game; + _cpu = new Z80A(); + _tracer = new TraceBuffer { Header = _cpu.TraceHeader }; + _files = files?.ToList() ?? new List(); - if (settings == null) - settings = new AmstradCPCSettings(); - if (syncSettings == null) - syncSettings = new AmstradCPCSyncSettings(); + if (settings == null) + settings = new AmstradCPCSettings(); + if (syncSettings == null) + syncSettings = new AmstradCPCSyncSettings(); - PutSyncSettings((AmstradCPCSyncSettings)syncSettings ?? new AmstradCPCSyncSettings()); - PutSettings((AmstradCPCSettings)settings ?? new AmstradCPCSettings()); + PutSyncSettings((AmstradCPCSyncSettings)syncSettings ?? new AmstradCPCSyncSettings()); + PutSettings((AmstradCPCSettings)settings ?? new AmstradCPCSettings()); - deterministicEmulation = ((AmstradCPCSyncSettings)syncSettings as AmstradCPCSyncSettings).DeterministicEmulation; + deterministicEmulation = ((AmstradCPCSyncSettings)syncSettings as AmstradCPCSyncSettings).DeterministicEmulation; - switch (SyncSettings.MachineType) - { - case MachineType.CPC464: - ControllerDefinition = AmstradCPCControllerDefinition; - Init(MachineType.CPC464, _files, ((AmstradCPCSyncSettings)syncSettings as AmstradCPCSyncSettings).AutoStartStopTape, - ((AmstradCPCSyncSettings)syncSettings as AmstradCPCSyncSettings).BorderType); - break; - case MachineType.CPC6128: - ControllerDefinition = AmstradCPCControllerDefinition; - Init(MachineType.CPC6128, _files, ((AmstradCPCSyncSettings)syncSettings as AmstradCPCSyncSettings).AutoStartStopTape, ((AmstradCPCSyncSettings)syncSettings as AmstradCPCSyncSettings).BorderType); - break; - default: - throw new InvalidOperationException("Machine not yet emulated"); - } + switch (SyncSettings.MachineType) + { + case MachineType.CPC464: + ControllerDefinition = AmstradCPCControllerDefinition; + Init(MachineType.CPC464, _files, ((AmstradCPCSyncSettings)syncSettings as AmstradCPCSyncSettings).AutoStartStopTape, + ((AmstradCPCSyncSettings)syncSettings as AmstradCPCSyncSettings).BorderType); + break; + case MachineType.CPC6128: + ControllerDefinition = AmstradCPCControllerDefinition; + Init(MachineType.CPC6128, _files, ((AmstradCPCSyncSettings)syncSettings as AmstradCPCSyncSettings).AutoStartStopTape, ((AmstradCPCSyncSettings)syncSettings as AmstradCPCSyncSettings).BorderType); + break; + default: + throw new InvalidOperationException("Machine not yet emulated"); + } - _cpu.MemoryCallbacks = MemoryCallbacks; + _cpu.MemoryCallbacks = MemoryCallbacks; - HardReset = _machine.HardReset; - SoftReset = _machine.SoftReset; + HardReset = _machine.HardReset; + SoftReset = _machine.SoftReset; - _cpu.FetchMemory = _machine.ReadMemory; - _cpu.ReadMemory = _machine.ReadMemory; - _cpu.WriteMemory = _machine.WriteMemory; - _cpu.ReadHardware = _machine.ReadPort; - _cpu.WriteHardware = _machine.WritePort; - _cpu.FetchDB = _machine.PushBus; - _cpu.IRQACKCallback = _machine.GateArray.IORQA; - //_cpu.OnExecFetch = _machine.CPUMon.OnExecFetch; + _cpu.FetchMemory = _machine.ReadMemory; + _cpu.ReadMemory = _machine.ReadMemory; + _cpu.WriteMemory = _machine.WriteMemory; + _cpu.ReadHardware = _machine.ReadPort; + _cpu.WriteHardware = _machine.WritePort; + _cpu.FetchDB = _machine.PushBus; + _cpu.IRQACKCallback = _machine.GateArray.IORQA; + //_cpu.OnExecFetch = _machine.CPUMon.OnExecFetch; - ser.Register(_tracer); - ser.Register(_cpu); - ser.Register(_machine.GateArray); + ser.Register(_tracer); + ser.Register(_cpu); + ser.Register(_machine.GateArray); - // initialize sound mixer and attach the various ISoundProvider devices - SoundMixer = new SoundProviderMixer((int)(32767 / 10), "Tape Audio", (ISoundProvider)_machine.TapeBuzzer); - if (_machine.AYDevice != null) - SoundMixer.AddSource(_machine.AYDevice, "AY-3-3912"); + // initialize sound mixer and attach the various ISoundProvider devices + SoundMixer = new SoundProviderMixer((int)(32767 / 10), "Tape Audio", (ISoundProvider)_machine.TapeBuzzer); + if (_machine.AYDevice != null) + SoundMixer.AddSource(_machine.AYDevice, "AY-3-3912"); - // set audio device settings - if (_machine.AYDevice != null && _machine.AYDevice.GetType() == typeof(AY38912)) - { - ((AY38912)_machine.AYDevice as AY38912).PanningConfiguration = ((AmstradCPCSettings)settings as AmstradCPCSettings).AYPanConfig; - _machine.AYDevice.Volume = ((AmstradCPCSettings)settings as AmstradCPCSettings).AYVolume; - } + // set audio device settings + if (_machine.AYDevice != null && _machine.AYDevice.GetType() == typeof(AY38912)) + { + ((AY38912)_machine.AYDevice as AY38912).PanningConfiguration = ((AmstradCPCSettings)settings as AmstradCPCSettings).AYPanConfig; + _machine.AYDevice.Volume = ((AmstradCPCSettings)settings as AmstradCPCSettings).AYVolume; + } - if (_machine.TapeBuzzer != null) - { - ((Beeper)_machine.TapeBuzzer as Beeper).Volume = ((AmstradCPCSettings)settings as AmstradCPCSettings).TapeVolume; - } + if (_machine.TapeBuzzer != null) + { + ((Beeper)_machine.TapeBuzzer as Beeper).Volume = ((AmstradCPCSettings)settings as AmstradCPCSettings).TapeVolume; + } - ser.Register(SoundMixer); + ser.Register(SoundMixer); - HardReset(); - SetupMemoryDomains(); - } + HardReset(); + SetupMemoryDomains(); + } - public Action HardReset; - public Action SoftReset; + public Action HardReset; + public Action SoftReset; - private readonly Z80A _cpu; - private readonly TraceBuffer _tracer; - public IController _controller; - public CPCBase _machine; + private readonly Z80A _cpu; + private readonly TraceBuffer _tracer; + public IController _controller; + public CPCBase _machine; - public List _gameInfo; - public List _tapeInfo = new List(); - public List _diskInfo = new List(); + public List _gameInfo; + public List _tapeInfo = new List(); + public List _diskInfo = new List(); - private SoundProviderMixer SoundMixer; + private SoundProviderMixer SoundMixer; - private readonly List _files; + private readonly List _files; - private byte[] GetFirmware(int length, params string[] names) - { - // Amstrad licensed ROMs are free to distribute and shipped with BizHawk - byte[] embeddedRom = new byte[length]; - bool embeddedFound = true; - switch (names.FirstOrDefault()) - { - // CPC 464 ROMS - case "OS464ROM": - embeddedRom = Util.DecompressGzipFile(new MemoryStream(Resources.OS_464_ROM)); - break; - case "BASIC1-0ROM": - embeddedRom = Util.DecompressGzipFile(new MemoryStream(Resources.CPC_BASIC_1_0_ROM)); - break; + private byte[] GetFirmware(int length, params string[] names) + { + // Amstrad licensed ROMs are free to distribute and shipped with BizHawk + byte[] embeddedRom = new byte[length]; + bool embeddedFound = true; + switch (names.FirstOrDefault()) + { + // CPC 464 ROMS + case "OS464ROM": + embeddedRom = Util.DecompressGzipFile(new MemoryStream(Resources.OS_464_ROM)); + break; + case "BASIC1-0ROM": + embeddedRom = Util.DecompressGzipFile(new MemoryStream(Resources.CPC_BASIC_1_0_ROM)); + break; - // CPC 6128 ROMS - case "OS6128ROM": - embeddedRom = Util.DecompressGzipFile(new MemoryStream(Resources.CPC_OS_6128_ROM)); - break; - case "BASIC1-1ROM": - embeddedRom = Util.DecompressGzipFile(new MemoryStream(Resources.CPC_BASIC_1_1_ROM)); - break; - case "AMSDOS0-5ROM": - embeddedRom = Util.DecompressGzipFile(new MemoryStream(Resources.CPC_AMSDOS_0_5_ROM)); - break; - default: - embeddedFound = false; - break; - } + // CPC 6128 ROMS + case "OS6128ROM": + embeddedRom = Util.DecompressGzipFile(new MemoryStream(Resources.CPC_OS_6128_ROM)); + break; + case "BASIC1-1ROM": + embeddedRom = Util.DecompressGzipFile(new MemoryStream(Resources.CPC_BASIC_1_1_ROM)); + break; + case "AMSDOS0-5ROM": + embeddedRom = Util.DecompressGzipFile(new MemoryStream(Resources.CPC_AMSDOS_0_5_ROM)); + break; + default: + embeddedFound = false; + break; + } - if (embeddedFound) - return embeddedRom; + if (embeddedFound) + return embeddedRom; - // Embedded ROM not found, maybe this is a peripheral ROM? - var result = names.Select(n => CoreComm.CoreFileProvider.GetFirmware("AmstradCPC", n, false)).FirstOrDefault(b => b != null && b.Length == length); - if (result == null) - { - throw new MissingFirmwareException($"At least one of these firmwares is required: {string.Join(", ", names)}"); - } + // Embedded ROM not found, maybe this is a peripheral ROM? + var result = names.Select(n => CoreComm.CoreFileProvider.GetFirmware("AmstradCPC", n, false)).FirstOrDefault(b => b != null && b.Length == length); + if (result == null) + { + throw new MissingFirmwareException($"At least one of these firmwares is required: {string.Join(", ", names)}"); + } - return result; - } + return result; + } - private MachineType _machineType; + private MachineType _machineType; - private void Init(MachineType machineType, List files, bool autoTape, BorderType bType) - { - _machineType = machineType; + private void Init(MachineType machineType, List files, bool autoTape, BorderType bType) + { + _machineType = machineType; - // setup the emulated model based on the MachineType - switch (machineType) - { - case MachineType.CPC464: - _machine = new CPC464(this, _cpu, files, autoTape, bType); - List roms64 = new List(); - roms64.Add(RomData.InitROM(MachineType.CPC464, GetFirmware(0x4000, "OS464ROM"), RomData.ROMChipType.Lower)); - roms64.Add(RomData.InitROM(MachineType.CPC464, GetFirmware(0x4000, "BASIC1-0ROM"), RomData.ROMChipType.Upper, 0)); - _machine.InitROM(roms64.ToArray()); - break; + // setup the emulated model based on the MachineType + switch (machineType) + { + case MachineType.CPC464: + _machine = new CPC464(this, _cpu, files, autoTape, bType); + List roms64 = new List(); + roms64.Add(RomData.InitROM(MachineType.CPC464, GetFirmware(0x4000, "OS464ROM"), RomData.ROMChipType.Lower)); + roms64.Add(RomData.InitROM(MachineType.CPC464, GetFirmware(0x4000, "BASIC1-0ROM"), RomData.ROMChipType.Upper, 0)); + _machine.InitROM(roms64.ToArray()); + break; - case MachineType.CPC6128: - _machine = new CPC6128(this, _cpu, files, autoTape, bType); - List roms128 = new List(); - roms128.Add(RomData.InitROM(MachineType.CPC6128, GetFirmware(0x4000, "OS6128ROM"), RomData.ROMChipType.Lower)); - roms128.Add(RomData.InitROM(MachineType.CPC6128, GetFirmware(0x4000, "BASIC1-1ROM"), RomData.ROMChipType.Upper, 0)); - roms128.Add(RomData.InitROM(MachineType.CPC6128, GetFirmware(0x4000, "AMSDOS0-5ROM"), RomData.ROMChipType.Upper, 7)); - _machine.InitROM(roms128.ToArray()); - break; - } - } + case MachineType.CPC6128: + _machine = new CPC6128(this, _cpu, files, autoTape, bType); + List roms128 = new List(); + roms128.Add(RomData.InitROM(MachineType.CPC6128, GetFirmware(0x4000, "OS6128ROM"), RomData.ROMChipType.Lower)); + roms128.Add(RomData.InitROM(MachineType.CPC6128, GetFirmware(0x4000, "BASIC1-1ROM"), RomData.ROMChipType.Upper, 0)); + roms128.Add(RomData.InitROM(MachineType.CPC6128, GetFirmware(0x4000, "AMSDOS0-5ROM"), RomData.ROMChipType.Upper, 7)); + _machine.InitROM(roms128.ToArray()); + break; + } + } - #region IRegionable + #region IRegionable - public DisplayType Region => DisplayType.PAL; + public DisplayType Region => DisplayType.PAL; - #endregion + #endregion - #region IDriveLight + #region IDriveLight - public bool DriveLightEnabled - { - get - { - return true; - } - } + public bool DriveLightEnabled + { + get + { + return true; + } + } - public bool DriveLightOn - { - get - { - if (_machine != null && - (_machine.TapeDevice != null && _machine.TapeDevice.TapeIsPlaying) || - (_machine.UPDDiskDevice != null && _machine.UPDDiskDevice.DriveLight)) - return true; + public bool DriveLightOn + { + get + { + if (_machine != null && + (_machine.TapeDevice != null && _machine.TapeDevice.TapeIsPlaying) || + (_machine.UPDDiskDevice != null && _machine.UPDDiskDevice.DriveLight)) + return true; - return false; - } - } + return false; + } + } - #endregion - } + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Abstraction/IBeeperDevice.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Abstraction/IBeeperDevice.cs index 42626f6590..0572218c95 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Abstraction/IBeeperDevice.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Abstraction/IBeeperDevice.cs @@ -2,24 +2,24 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Represents a beeper/buzzer device - /// - public interface IBeeperDevice - { - /// - /// Initialisation - /// - void Init(int sampleRate, int tStatesPerFrame); + /// + /// Represents a beeper/buzzer device + /// + public interface IBeeperDevice + { + /// + /// Initialisation + /// + void Init(int sampleRate, int tStatesPerFrame); - /// - /// Processes an incoming pulse value and adds it to the blipbuffer - /// - void ProcessPulseValue(bool pulse); + /// + /// Processes an incoming pulse value and adds it to the blipbuffer + /// + void ProcessPulseValue(bool pulse); - /// - /// State serialization - /// - void SyncState(Serializer ser); - } + /// + /// State serialization + /// + void SyncState(Serializer ser); + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Abstraction/IFDDHost.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Abstraction/IFDDHost.cs index c65f7a6eda..3bea240dee 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Abstraction/IFDDHost.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Abstraction/IFDDHost.cs @@ -1,29 +1,29 @@  namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Defines an object that can load a floppy disk image - /// - public interface IFDDHost - { - /// - /// The currently inserted diskimage - /// - FloppyDisk Disk { get; set; } + /// + /// Defines an object that can load a floppy disk image + /// + public interface IFDDHost + { + /// + /// The currently inserted diskimage + /// + FloppyDisk Disk { get; set; } - /// - /// Parses a new disk image and loads it into this floppy drive - /// - void FDD_LoadDisk(byte[] diskData); + /// + /// Parses a new disk image and loads it into this floppy drive + /// + void FDD_LoadDisk(byte[] diskData); - /// - /// Ejects the current disk - /// - void FDD_EjectDisk(); + /// + /// Ejects the current disk + /// + void FDD_EjectDisk(); - /// - /// Signs whether the current active drive has a disk inserted - /// - bool FDD_IsDiskLoaded { get; } - } + /// + /// Signs whether the current active drive has a disk inserted + /// + bool FDD_IsDiskLoaded { get; } + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Abstraction/IJoystick.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Abstraction/IJoystick.cs index 7afe0d007c..50088cceae 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Abstraction/IJoystick.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Abstraction/IJoystick.cs @@ -1,34 +1,34 @@  namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Represents a spectrum joystick - /// - public interface IJoystick - { - /// - /// The type of joystick - /// - JoystickType JoyType { get; } + /// + /// Represents a spectrum joystick + /// + public interface IJoystick + { + /// + /// The type of joystick + /// + JoystickType JoyType { get; } - /// - /// Array of all the possibly button press names - /// - string[] ButtonCollection { get; set; } + /// + /// Array of all the possibly button press names + /// + string[] ButtonCollection { get; set; } - /// - /// The player number that this controller is currently assigned to - /// - int PlayerNumber { get; set; } + /// + /// The player number that this controller is currently assigned to + /// + int PlayerNumber { get; set; } - /// - /// Sets the joystick line based on key pressed - /// - void SetJoyInput(string key, bool isPressed); + /// + /// Sets the joystick line based on key pressed + /// + void SetJoyInput(string key, bool isPressed); - /// - /// Gets the state of a particular joystick binding - /// - bool GetJoyInput(string key); - } + /// + /// Gets the state of a particular joystick binding + /// + bool GetJoyInput(string key); + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Abstraction/IKeyboard.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Abstraction/IKeyboard.cs index 194837241f..976fac07cb 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Abstraction/IKeyboard.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Abstraction/IKeyboard.cs @@ -2,52 +2,52 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Represents a spectrum keyboard - /// - public interface IKeyboard - { - /// - /// The calling spectrumbase class - /// - CPCBase _machine { get; } + /// + /// Represents a spectrum keyboard + /// + public interface IKeyboard + { + /// + /// The calling spectrumbase class + /// + CPCBase _machine { get; } - /// - /// The keyboard matrix for a particular CPC model - /// - string[] KeyboardMatrix { get; set; } + /// + /// The keyboard matrix for a particular CPC model + /// + string[] KeyboardMatrix { get; set; } - /// - /// Other keyboard keys that are not in the matrix - /// (usually keys derived from key combos) - /// - string[] NonMatrixKeys { get; set; } + /// + /// Other keyboard keys that are not in the matrix + /// (usually keys derived from key combos) + /// + string[] NonMatrixKeys { get; set; } - /// - /// Represents the spectrum key state - /// - bool[] KeyStatus { get; set; } + /// + /// Represents the spectrum key state + /// + bool[] KeyStatus { get; set; } - /// - /// The currently selected line - /// - int CurrentLine { get; set; } + /// + /// The currently selected line + /// + int CurrentLine { get; set; } - /// - /// Reads the current line status - /// - byte ReadCurrentLine(); + /// + /// Reads the current line status + /// + byte ReadCurrentLine(); - /// - /// Sets the CPC key status - /// - void SetKeyStatus(string key, bool isPressed); + /// + /// Sets the CPC key status + /// + void SetKeyStatus(string key, bool isPressed); - /// - /// Gets the status of a CPC key - /// - bool GetKeyStatus(string key); + /// + /// Gets the status of a CPC key + /// + bool GetKeyStatus(string key); - void SyncState(Serializer ser); - } + void SyncState(Serializer ser); + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Abstraction/IPSG.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Abstraction/IPSG.cs index 2481387986..f272899598 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Abstraction/IPSG.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Abstraction/IPSG.cs @@ -3,64 +3,64 @@ using BizHawk.Emulation.Common; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Represents a PSG device (in this case an AY-3-891x) - /// - public interface IPSG : ISoundProvider - { - /// - /// Initlization routine - /// - void Init(int sampleRate, int tStatesPerFrame); + /// + /// Represents a PSG device (in this case an AY-3-891x) + /// + public interface IPSG : ISoundProvider + { + /// + /// Initlization routine + /// + void Init(int sampleRate, int tStatesPerFrame); - void SetFunction(int data); + void SetFunction(int data); - //void ClockCycle(); + //void ClockCycle(); - /// - /// Activates a register - /// - int SelectedRegister { get; set; } - - /// - /// Writes to the PSG - /// - void PortWrite(int value); + /// + /// Activates a register + /// + int SelectedRegister { get; set; } - /// - /// Reads from the PSG - /// - int PortRead(); - + /// + /// Writes to the PSG + /// + void PortWrite(int value); - /// - /// Resets the PSG - /// - void Reset(); + /// + /// Reads from the PSG + /// + int PortRead(); - /// - /// The volume of the AY chip - /// - int Volume { get; set; } - /// - /// Called at the start of a frame - /// - void StartFrame(); + /// + /// Resets the PSG + /// + void Reset(); - /// - /// called at the end of a frame - /// - void EndFrame(); + /// + /// The volume of the AY chip + /// + int Volume { get; set; } - /// - /// Updates the sound based on number of frame cycles - /// - void UpdateSound(int frameCycle); + /// + /// Called at the start of a frame + /// + void StartFrame(); - /// - /// IStatable serialization - /// - void SyncState(Serializer ser); - } + /// + /// called at the end of a frame + /// + void EndFrame(); + + /// + /// Updates the sound based on number of frame cycles + /// + void UpdateSound(int frameCycle); + + /// + /// IStatable serialization + /// + void SyncState(Serializer ser); + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Abstraction/IPortIODevice.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Abstraction/IPortIODevice.cs index 01a351f4b4..b71e08ee08 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Abstraction/IPortIODevice.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Abstraction/IPortIODevice.cs @@ -1,19 +1,19 @@  namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Represents a device that utilizes port IN & OUT - /// - public interface IPortIODevice - { - /// - /// Device responds to an IN instruction - /// - bool ReadPort(ushort port, ref int result); + /// + /// Represents a device that utilizes port IN & OUT + /// + public interface IPortIODevice + { + /// + /// Device responds to an IN instruction + /// + bool ReadPort(ushort port, ref int result); - /// - /// Device responds to an OUT instruction - /// - bool WritePort(ushort port, int result); - } + /// + /// Device responds to an OUT instruction + /// + bool WritePort(ushort port, int result); + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Datacorder/DatacorderDevice.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Datacorder/DatacorderDevice.cs index 32adc5dbde..5fd62aebf5 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Datacorder/DatacorderDevice.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Datacorder/DatacorderDevice.cs @@ -7,566 +7,566 @@ using System.Text; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Represents the tape device - /// - public class DatacorderDevice - { - #region Construction - - private CPCBase _machine; - private Z80A _cpu => _machine.CPU; - private IBeeperDevice _buzzer => _machine.TapeBuzzer; - - /// - /// Default constructor - /// - public DatacorderDevice(bool autoTape) - { - _autoPlay = autoTape; - } - - /// - /// Initializes the datacorder device - /// - public void Init(CPCBase machine) - { - _machine = machine; - } - - #endregion - - #region State Information - - /// - /// Signs whether the tape motor is running - /// - private bool tapeMotor; - public bool TapeMotor - { - get { return tapeMotor; } - set - { - if (tapeMotor == value) - return; - - tapeMotor = value; - if (tapeMotor) - { - _machine.CPC.OSD_TapeMotorActive(); - - if (_autoPlay) - { - Play(); - } - } - - else - { - _machine.CPC.OSD_TapeMotorInactive(); - - if (_autoPlay) - { - Stop(); - } - } - - } - } - - /// - /// Internal counter used to trigger tape buzzer output - /// - private int counter = 0; - - /// - /// The index of the current tape data block that is loaded - /// - private int _currentDataBlockIndex = 0; - public int CurrentDataBlockIndex - { - get - { - if (_dataBlocks.Count() > 0) { return _currentDataBlockIndex; } - else { return -1; } - } - set - { - if (value == _currentDataBlockIndex) { return; } - if (value < _dataBlocks.Count() && value >= 0) - { - _currentDataBlockIndex = value; - _position = 0; - } - } - } - - /// - /// The current position within the current data block - /// - private int _position = 0; - public int Position - { - get - { - if (_position >= _dataBlocks[_currentDataBlockIndex].DataPeriods.Count) { return 0; } - else { return _position; } - } - } - - /// - /// Signs whether the tape is currently playing or not - /// - private bool _tapeIsPlaying = false; - public bool TapeIsPlaying - { - get { return _tapeIsPlaying; } - } - - /// - /// A list of the currently loaded data blocks - /// - private List _dataBlocks = new List(); - public List DataBlocks - { - get { return _dataBlocks; } - set { _dataBlocks = value; } - } - - /// - /// Stores the last CPU t-state value - /// - private long _lastCycle = 0; - - /// - /// Edge - /// - private int _waitEdge = 0; - - /// - /// Current tapebit state - /// - private bool currentState = false; - - #endregion - - #region Datacorder Device Settings - - /// - /// Signs whether the device should autodetect when the Z80 has entered into - /// 'load' mode and auto-play the tape if neccesary - /// - private bool _autoPlay; - - #endregion - - #region Emulator - - /// - /// Should be fired at the end of every frame - /// Primary purpose is to detect tape traps and manage auto play (if/when this is ever implemented) - /// - public void EndFrame() - { - //MonitorFrame(); - } - - public void StartFrame() - { - //_buzzer.ProcessPulseValue(currentState); - } - - #endregion - - #region Tape Controls - - /// - /// Starts the tape playing from the beginning of the current block - /// - public void Play() - { - if (_tapeIsPlaying) - return; - - if (!_autoPlay) - _machine.CPC.OSD_TapePlaying(); - - _machine.CPC.OSD_TapeMotorActive(); - - // update the lastCycle - _lastCycle = _cpu.TotalExecutedCycles; - - // reset waitEdge and position - _waitEdge = 0; - _position = 0; - - if ( - _dataBlocks.Count > 0 && // data blocks are present && - _currentDataBlockIndex >= 0 // the current data block index is 1 or greater - ) - { - while (_position >= _dataBlocks[_currentDataBlockIndex].DataPeriods.Count) - { - // we are at the end of a data block - move to the next - _position = 0; - _currentDataBlockIndex++; - - // are we at the end of the tape? - if (_currentDataBlockIndex >= _dataBlocks.Count) - { - break; - } - } - - // check for end of tape - if (_currentDataBlockIndex >= _dataBlocks.Count) - { - // end of tape reached. Rewind to beginning - AutoStopTape(); - RTZ(); - return; - } - - // update waitEdge with the current position in the current block - _waitEdge = _dataBlocks[_currentDataBlockIndex].DataPeriods[_position]; - - // sign that the tape is now playing - _tapeIsPlaying = true; - } - } - - /// - /// Stops the tape - /// (should move to the beginning of the next block) - /// - public void Stop() - { - if (!_tapeIsPlaying) - return; - - _machine.CPC.OSD_TapeStopped(); - - // sign that the tape is no longer playing - _tapeIsPlaying = false; - - if ( - _currentDataBlockIndex >= 0 && // we are at datablock 1 or above - _position >= _dataBlocks[_currentDataBlockIndex].DataPeriods.Count - 1 // the block is still playing back - ) - { - // move to the next block - _currentDataBlockIndex++; - - if (_currentDataBlockIndex >= _dataBlocks.Count()) - { - _currentDataBlockIndex = -1; - } - - // reset waitEdge and position - _waitEdge = 0; - _position = 0; - - if ( - _currentDataBlockIndex < 0 && // block index is -1 - _dataBlocks.Count() > 0 // number of blocks is greater than 0 - ) - { - // move the index on to 0 - _currentDataBlockIndex = 0; - } - } - - // update the lastCycle - _lastCycle = _cpu.TotalExecutedCycles; - } - - /// - /// Rewinds the tape to it's beginning (return to zero) - /// - public void RTZ() - { - Stop(); - _machine.CPC.OSD_TapeRTZ(); - _currentDataBlockIndex = 0; - } - - /// - /// Performs a block skip operation on the current tape - /// TRUE: skip forward - /// FALSE: skip backward - /// - public void SkipBlock(bool skipForward) - { - int blockCount = _dataBlocks.Count; - int targetBlockId = _currentDataBlockIndex; - - if (skipForward) - { - if (_currentDataBlockIndex == blockCount - 1) - { - // last block, go back to beginning - targetBlockId = 0; - } - else - { - targetBlockId++; - } - } - else - { - if (_currentDataBlockIndex == 0) - { - // already first block, goto last block - targetBlockId = blockCount - 1; - } - else - { - targetBlockId--; - } - } - - var bl = _dataBlocks[targetBlockId]; - - StringBuilder sbd = new StringBuilder(); - sbd.Append("("); - sbd.Append((targetBlockId + 1) + " of " + _dataBlocks.Count()); - sbd.Append(") : "); - //sbd.Append("ID" + bl.BlockID.ToString("X2") + " - "); - sbd.Append(bl.BlockDescription); - if (bl.MetaData.Count > 0) - { - sbd.Append(" - "); - sbd.Append(bl.MetaData.First().Key + ": " + bl.MetaData.First().Value); - //sbd.Append("\n"); - //sbd.Append(bl.MetaData.Skip(1).First().Key + ": " + bl.MetaData.Skip(1).First().Value); - } - - if (skipForward) - _machine.CPC.OSD_TapeNextBlock(sbd.ToString()); - else - _machine.CPC.OSD_TapePrevBlock(sbd.ToString()); - - CurrentDataBlockIndex = targetBlockId; - } - - /// - /// Inserts a new tape and sets up the tape device accordingly - /// - public void LoadTape(byte[] tapeData) - { - // instantiate converters - CdtConverter cdtSer = new CdtConverter(this); - - // CDT - if (cdtSer.CheckType(tapeData)) - { - // this file has a tzx header - attempt serialization - try - { - cdtSer.Read(tapeData); - // reset block index - CurrentDataBlockIndex = 0; - return; - } - catch (Exception ex) - { - // exception during operation - var e = ex; - throw new Exception(this.GetType().ToString() + - "\n\nTape image file has a valid CDT header, but threw an exception whilst data was being parsed.\n\n" + e.ToString()); - } - } - } - - /// - /// Resets the tape - /// - public void Reset() - { - RTZ(); - } - - #endregion - - #region Tape Device Methods - - /// - /// Is called every cpu cycle but runs every 50 t-states - /// This enables the tape devices to play out even if the spectrum itself is not - /// requesting tape data - /// - public void TapeCycle() - { - if (TapeMotor) - { - counter++; - - if (counter > 20) - { - counter = 0; - bool state = GetEarBit(_machine.CPU.TotalExecutedCycles); - _buzzer.ProcessPulseValue(state); - } - } - } - - /// - /// Simulates the spectrum 'EAR' input reading data from the tape - /// - public bool GetEarBit(long cpuCycle) - { - // decide how many cycles worth of data we are capturing - long cycles = cpuCycle - _lastCycle; - - // check whether tape is actually playing - if (tapeMotor == false) - { - // it's not playing. Update lastCycle and return - _lastCycle = cpuCycle; - return false; - } - - // check for end of tape - if (_currentDataBlockIndex < 0) - { - // end of tape reached - RTZ (and stop) - RTZ(); - return currentState; - } - - // process the cycles based on the waitEdge - while (cycles >= _waitEdge) - { - // decrement cycles - cycles -= _waitEdge; - - if (_position == 0 && tapeMotor) - { - // start of block - take care of initial pulse level for PZX - switch (_dataBlocks[_currentDataBlockIndex].BlockDescription) - { - case BlockType.PULS: - // initial pulse level is always low - if (currentState) - FlipTapeState(); - break; - case BlockType.DATA: - // initial pulse level is stored in block - if (currentState != _dataBlocks[_currentDataBlockIndex].InitialPulseLevel) - FlipTapeState(); - break; - case BlockType.PAUS: - // initial pulse level is stored in block - if (currentState != _dataBlocks[_currentDataBlockIndex].InitialPulseLevel) - FlipTapeState(); - break; - } - - // most of these amstrad tapes appear to have a pause block at the start - // skip this if it is the first block - switch (_dataBlocks[_currentDataBlockIndex].BlockDescription) - { - case BlockType.PAUS: - case BlockType.PAUSE_BLOCK: - case BlockType.Pause_or_Stop_the_Tape: - if (_currentDataBlockIndex == 0) - { - // this is the first block on the tape - SkipBlock(true); - } - else - { - // there may be non-data blocks before this - bool okToSkipPause = true; - for (int i = _currentDataBlockIndex; i >= 0; i--) - { - switch (_dataBlocks[i].BlockDescription) - { - case BlockType.Archive_Info: - case BlockType.BRWS: - case BlockType.Custom_Info_Block: - case BlockType.Emulation_Info: - case BlockType.Glue_Block: - case BlockType.Hardware_Type: - case BlockType.Message_Block: - case BlockType.PZXT: - case BlockType.Text_Description: - break; - default: - okToSkipPause = false; - break; - } - - if (!okToSkipPause) - break; - } - - if (okToSkipPause) - { - SkipBlock(true); - } - } - break; - } - - // notify about the current block - var bl = _dataBlocks[_currentDataBlockIndex]; - - StringBuilder sbd = new StringBuilder(); - sbd.Append("("); - sbd.Append((_currentDataBlockIndex + 1) + " of " + _dataBlocks.Count()); - sbd.Append(") : "); - //sbd.Append("ID" + bl.BlockID.ToString("X2") + " - "); - sbd.Append(bl.BlockDescription); - if (bl.MetaData.Count > 0) - { - sbd.Append(" - "); - sbd.Append(bl.MetaData.First().Key + ": " + bl.MetaData.First().Value); - } - _machine.CPC.OSD_TapePlayingBlockInfo(sbd.ToString()); - } - - - // increment the current period position - _position++; - - if (_position >= _dataBlocks[_currentDataBlockIndex].DataPeriods.Count()) - { - // we have reached the end of the current block - - if (_dataBlocks[_currentDataBlockIndex].DataPeriods.Count() == 0) - { - // notify about the current block (we are skipping it because its empty) - var bl = _dataBlocks[_currentDataBlockIndex]; - StringBuilder sbd = new StringBuilder(); - sbd.Append("("); - sbd.Append((_currentDataBlockIndex + 1) + " of " + _dataBlocks.Count()); - sbd.Append(") : "); - //sbd.Append("ID" + bl.BlockID.ToString("X2") + " - "); - sbd.Append(bl.BlockDescription); - if (bl.MetaData.Count > 0) - { - sbd.Append(" - "); - sbd.Append(bl.MetaData.First().Key + ": " + bl.MetaData.First().Value); - } - _machine.CPC.OSD_TapePlayingSkipBlockInfo(sbd.ToString()); - - } - - // skip any empty blocks (and process any command blocks) - while (_position >= _dataBlocks[_currentDataBlockIndex].DataPeriods.Count()) - { - // check for any commands - var command = _dataBlocks[_currentDataBlockIndex].Command; - var block = _dataBlocks[_currentDataBlockIndex]; - bool shouldStop = false; - switch (command) - { - case TapeCommand.STOP_THE_TAPE: - case TapeCommand.STOP_THE_TAPE_48K: - throw new Exception("spectrum tape command found in CPC tape"); - - /* + /// + /// Represents the tape device + /// + public class DatacorderDevice + { + #region Construction + + private CPCBase _machine; + private Z80A _cpu => _machine.CPU; + private IBeeperDevice _buzzer => _machine.TapeBuzzer; + + /// + /// Default constructor + /// + public DatacorderDevice(bool autoTape) + { + _autoPlay = autoTape; + } + + /// + /// Initializes the datacorder device + /// + public void Init(CPCBase machine) + { + _machine = machine; + } + + #endregion + + #region State Information + + /// + /// Signs whether the tape motor is running + /// + private bool tapeMotor; + public bool TapeMotor + { + get { return tapeMotor; } + set + { + if (tapeMotor == value) + return; + + tapeMotor = value; + if (tapeMotor) + { + _machine.CPC.OSD_TapeMotorActive(); + + if (_autoPlay) + { + Play(); + } + } + + else + { + _machine.CPC.OSD_TapeMotorInactive(); + + if (_autoPlay) + { + Stop(); + } + } + + } + } + + /// + /// Internal counter used to trigger tape buzzer output + /// + private int counter = 0; + + /// + /// The index of the current tape data block that is loaded + /// + private int _currentDataBlockIndex = 0; + public int CurrentDataBlockIndex + { + get + { + if (_dataBlocks.Count() > 0) { return _currentDataBlockIndex; } + else { return -1; } + } + set + { + if (value == _currentDataBlockIndex) { return; } + if (value < _dataBlocks.Count() && value >= 0) + { + _currentDataBlockIndex = value; + _position = 0; + } + } + } + + /// + /// The current position within the current data block + /// + private int _position = 0; + public int Position + { + get + { + if (_position >= _dataBlocks[_currentDataBlockIndex].DataPeriods.Count) { return 0; } + else { return _position; } + } + } + + /// + /// Signs whether the tape is currently playing or not + /// + private bool _tapeIsPlaying = false; + public bool TapeIsPlaying + { + get { return _tapeIsPlaying; } + } + + /// + /// A list of the currently loaded data blocks + /// + private List _dataBlocks = new List(); + public List DataBlocks + { + get { return _dataBlocks; } + set { _dataBlocks = value; } + } + + /// + /// Stores the last CPU t-state value + /// + private long _lastCycle = 0; + + /// + /// Edge + /// + private int _waitEdge = 0; + + /// + /// Current tapebit state + /// + private bool currentState = false; + + #endregion + + #region Datacorder Device Settings + + /// + /// Signs whether the device should autodetect when the Z80 has entered into + /// 'load' mode and auto-play the tape if neccesary + /// + private bool _autoPlay; + + #endregion + + #region Emulator + + /// + /// Should be fired at the end of every frame + /// Primary purpose is to detect tape traps and manage auto play (if/when this is ever implemented) + /// + public void EndFrame() + { + //MonitorFrame(); + } + + public void StartFrame() + { + //_buzzer.ProcessPulseValue(currentState); + } + + #endregion + + #region Tape Controls + + /// + /// Starts the tape playing from the beginning of the current block + /// + public void Play() + { + if (_tapeIsPlaying) + return; + + if (!_autoPlay) + _machine.CPC.OSD_TapePlaying(); + + _machine.CPC.OSD_TapeMotorActive(); + + // update the lastCycle + _lastCycle = _cpu.TotalExecutedCycles; + + // reset waitEdge and position + _waitEdge = 0; + _position = 0; + + if ( + _dataBlocks.Count > 0 && // data blocks are present && + _currentDataBlockIndex >= 0 // the current data block index is 1 or greater + ) + { + while (_position >= _dataBlocks[_currentDataBlockIndex].DataPeriods.Count) + { + // we are at the end of a data block - move to the next + _position = 0; + _currentDataBlockIndex++; + + // are we at the end of the tape? + if (_currentDataBlockIndex >= _dataBlocks.Count) + { + break; + } + } + + // check for end of tape + if (_currentDataBlockIndex >= _dataBlocks.Count) + { + // end of tape reached. Rewind to beginning + AutoStopTape(); + RTZ(); + return; + } + + // update waitEdge with the current position in the current block + _waitEdge = _dataBlocks[_currentDataBlockIndex].DataPeriods[_position]; + + // sign that the tape is now playing + _tapeIsPlaying = true; + } + } + + /// + /// Stops the tape + /// (should move to the beginning of the next block) + /// + public void Stop() + { + if (!_tapeIsPlaying) + return; + + _machine.CPC.OSD_TapeStopped(); + + // sign that the tape is no longer playing + _tapeIsPlaying = false; + + if ( + _currentDataBlockIndex >= 0 && // we are at datablock 1 or above + _position >= _dataBlocks[_currentDataBlockIndex].DataPeriods.Count - 1 // the block is still playing back + ) + { + // move to the next block + _currentDataBlockIndex++; + + if (_currentDataBlockIndex >= _dataBlocks.Count()) + { + _currentDataBlockIndex = -1; + } + + // reset waitEdge and position + _waitEdge = 0; + _position = 0; + + if ( + _currentDataBlockIndex < 0 && // block index is -1 + _dataBlocks.Count() > 0 // number of blocks is greater than 0 + ) + { + // move the index on to 0 + _currentDataBlockIndex = 0; + } + } + + // update the lastCycle + _lastCycle = _cpu.TotalExecutedCycles; + } + + /// + /// Rewinds the tape to it's beginning (return to zero) + /// + public void RTZ() + { + Stop(); + _machine.CPC.OSD_TapeRTZ(); + _currentDataBlockIndex = 0; + } + + /// + /// Performs a block skip operation on the current tape + /// TRUE: skip forward + /// FALSE: skip backward + /// + public void SkipBlock(bool skipForward) + { + int blockCount = _dataBlocks.Count; + int targetBlockId = _currentDataBlockIndex; + + if (skipForward) + { + if (_currentDataBlockIndex == blockCount - 1) + { + // last block, go back to beginning + targetBlockId = 0; + } + else + { + targetBlockId++; + } + } + else + { + if (_currentDataBlockIndex == 0) + { + // already first block, goto last block + targetBlockId = blockCount - 1; + } + else + { + targetBlockId--; + } + } + + var bl = _dataBlocks[targetBlockId]; + + StringBuilder sbd = new StringBuilder(); + sbd.Append("("); + sbd.Append((targetBlockId + 1) + " of " + _dataBlocks.Count()); + sbd.Append(") : "); + //sbd.Append("ID" + bl.BlockID.ToString("X2") + " - "); + sbd.Append(bl.BlockDescription); + if (bl.MetaData.Count > 0) + { + sbd.Append(" - "); + sbd.Append(bl.MetaData.First().Key + ": " + bl.MetaData.First().Value); + //sbd.Append("\n"); + //sbd.Append(bl.MetaData.Skip(1).First().Key + ": " + bl.MetaData.Skip(1).First().Value); + } + + if (skipForward) + _machine.CPC.OSD_TapeNextBlock(sbd.ToString()); + else + _machine.CPC.OSD_TapePrevBlock(sbd.ToString()); + + CurrentDataBlockIndex = targetBlockId; + } + + /// + /// Inserts a new tape and sets up the tape device accordingly + /// + public void LoadTape(byte[] tapeData) + { + // instantiate converters + CdtConverter cdtSer = new CdtConverter(this); + + // CDT + if (cdtSer.CheckType(tapeData)) + { + // this file has a tzx header - attempt serialization + try + { + cdtSer.Read(tapeData); + // reset block index + CurrentDataBlockIndex = 0; + return; + } + catch (Exception ex) + { + // exception during operation + var e = ex; + throw new Exception(this.GetType().ToString() + + "\n\nTape image file has a valid CDT header, but threw an exception whilst data was being parsed.\n\n" + e.ToString()); + } + } + } + + /// + /// Resets the tape + /// + public void Reset() + { + RTZ(); + } + + #endregion + + #region Tape Device Methods + + /// + /// Is called every cpu cycle but runs every 50 t-states + /// This enables the tape devices to play out even if the spectrum itself is not + /// requesting tape data + /// + public void TapeCycle() + { + if (TapeMotor) + { + counter++; + + if (counter > 20) + { + counter = 0; + bool state = GetEarBit(_machine.CPU.TotalExecutedCycles); + _buzzer.ProcessPulseValue(state); + } + } + } + + /// + /// Simulates the spectrum 'EAR' input reading data from the tape + /// + public bool GetEarBit(long cpuCycle) + { + // decide how many cycles worth of data we are capturing + long cycles = cpuCycle - _lastCycle; + + // check whether tape is actually playing + if (tapeMotor == false) + { + // it's not playing. Update lastCycle and return + _lastCycle = cpuCycle; + return false; + } + + // check for end of tape + if (_currentDataBlockIndex < 0) + { + // end of tape reached - RTZ (and stop) + RTZ(); + return currentState; + } + + // process the cycles based on the waitEdge + while (cycles >= _waitEdge) + { + // decrement cycles + cycles -= _waitEdge; + + if (_position == 0 && tapeMotor) + { + // start of block - take care of initial pulse level for PZX + switch (_dataBlocks[_currentDataBlockIndex].BlockDescription) + { + case BlockType.PULS: + // initial pulse level is always low + if (currentState) + FlipTapeState(); + break; + case BlockType.DATA: + // initial pulse level is stored in block + if (currentState != _dataBlocks[_currentDataBlockIndex].InitialPulseLevel) + FlipTapeState(); + break; + case BlockType.PAUS: + // initial pulse level is stored in block + if (currentState != _dataBlocks[_currentDataBlockIndex].InitialPulseLevel) + FlipTapeState(); + break; + } + + // most of these amstrad tapes appear to have a pause block at the start + // skip this if it is the first block + switch (_dataBlocks[_currentDataBlockIndex].BlockDescription) + { + case BlockType.PAUS: + case BlockType.PAUSE_BLOCK: + case BlockType.Pause_or_Stop_the_Tape: + if (_currentDataBlockIndex == 0) + { + // this is the first block on the tape + SkipBlock(true); + } + else + { + // there may be non-data blocks before this + bool okToSkipPause = true; + for (int i = _currentDataBlockIndex; i >= 0; i--) + { + switch (_dataBlocks[i].BlockDescription) + { + case BlockType.Archive_Info: + case BlockType.BRWS: + case BlockType.Custom_Info_Block: + case BlockType.Emulation_Info: + case BlockType.Glue_Block: + case BlockType.Hardware_Type: + case BlockType.Message_Block: + case BlockType.PZXT: + case BlockType.Text_Description: + break; + default: + okToSkipPause = false; + break; + } + + if (!okToSkipPause) + break; + } + + if (okToSkipPause) + { + SkipBlock(true); + } + } + break; + } + + // notify about the current block + var bl = _dataBlocks[_currentDataBlockIndex]; + + StringBuilder sbd = new StringBuilder(); + sbd.Append("("); + sbd.Append((_currentDataBlockIndex + 1) + " of " + _dataBlocks.Count()); + sbd.Append(") : "); + //sbd.Append("ID" + bl.BlockID.ToString("X2") + " - "); + sbd.Append(bl.BlockDescription); + if (bl.MetaData.Count > 0) + { + sbd.Append(" - "); + sbd.Append(bl.MetaData.First().Key + ": " + bl.MetaData.First().Value); + } + _machine.CPC.OSD_TapePlayingBlockInfo(sbd.ToString()); + } + + + // increment the current period position + _position++; + + if (_position >= _dataBlocks[_currentDataBlockIndex].DataPeriods.Count()) + { + // we have reached the end of the current block + + if (_dataBlocks[_currentDataBlockIndex].DataPeriods.Count() == 0) + { + // notify about the current block (we are skipping it because its empty) + var bl = _dataBlocks[_currentDataBlockIndex]; + StringBuilder sbd = new StringBuilder(); + sbd.Append("("); + sbd.Append((_currentDataBlockIndex + 1) + " of " + _dataBlocks.Count()); + sbd.Append(") : "); + //sbd.Append("ID" + bl.BlockID.ToString("X2") + " - "); + sbd.Append(bl.BlockDescription); + if (bl.MetaData.Count > 0) + { + sbd.Append(" - "); + sbd.Append(bl.MetaData.First().Key + ": " + bl.MetaData.First().Value); + } + _machine.CPC.OSD_TapePlayingSkipBlockInfo(sbd.ToString()); + + } + + // skip any empty blocks (and process any command blocks) + while (_position >= _dataBlocks[_currentDataBlockIndex].DataPeriods.Count()) + { + // check for any commands + var command = _dataBlocks[_currentDataBlockIndex].Command; + var block = _dataBlocks[_currentDataBlockIndex]; + bool shouldStop = false; + switch (command) + { + case TapeCommand.STOP_THE_TAPE: + case TapeCommand.STOP_THE_TAPE_48K: + throw new Exception("spectrum tape command found in CPC tape"); + + /* // Stop the tape command found - if this is the end of the tape RTZ // otherwise just STOP and move to the next block case TapeCommand.STOP_THE_TAPE: @@ -601,84 +601,84 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC } break; */ - default: - break; - } + default: + break; + } - if (shouldStop) - break; + if (shouldStop) + break; - _position = 0; - _currentDataBlockIndex++; + _position = 0; + _currentDataBlockIndex++; - if (_currentDataBlockIndex >= _dataBlocks.Count()) - { - break; - } - } + if (_currentDataBlockIndex >= _dataBlocks.Count()) + { + break; + } + } - // check for end of tape - if (_currentDataBlockIndex >= _dataBlocks.Count()) - { - _currentDataBlockIndex = -1; - RTZ(); - return currentState; - } - } + // check for end of tape + if (_currentDataBlockIndex >= _dataBlocks.Count()) + { + _currentDataBlockIndex = -1; + RTZ(); + return currentState; + } + } - // update waitEdge with current position within the current block - _waitEdge = _dataBlocks[_currentDataBlockIndex].DataPeriods[_position]; + // update waitEdge with current position within the current block + _waitEdge = _dataBlocks[_currentDataBlockIndex].DataPeriods[_position]; - // flip the current state - FlipTapeState(); + // flip the current state + FlipTapeState(); - } + } - // update lastCycle and return currentstate - _lastCycle = cpuCycle - (long)cycles; + // update lastCycle and return currentstate + _lastCycle = cpuCycle - (long)cycles; - // play the buzzer - //_buzzer.ProcessPulseValue(false, currentState); + // play the buzzer + //_buzzer.ProcessPulseValue(false, currentState); - return currentState; - } + return currentState; + } - private void FlipTapeState() - { - currentState = !currentState; - } + private void FlipTapeState() + { + currentState = !currentState; + } - #endregion + #endregion - #region TapeMonitor + #region TapeMonitor - - public void AutoStopTape() - { - if (!_tapeIsPlaying) - return; - if (!_autoPlay) - return; + public void AutoStopTape() + { + if (!_tapeIsPlaying) + return; - Stop(); - _machine.CPC.OSD_TapeStoppedAuto(); - } + if (!_autoPlay) + return; - public void AutoStartTape() - { - if (_tapeIsPlaying) - return; + Stop(); + _machine.CPC.OSD_TapeStoppedAuto(); + } - if (!_autoPlay) - return; + public void AutoStartTape() + { + if (_tapeIsPlaying) + return; - Play(); - _machine.CPC.OSD_TapePlayingAuto(); - } + if (!_autoPlay) + return; - /* + Play(); + _machine.CPC.OSD_TapePlayingAuto(); + } + + /* public int MaskableInterruptCount = 0; private void MonitorFrame() @@ -749,27 +749,27 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC } */ - #endregion + #endregion - #region IPortIODevice + #region IPortIODevice - /// - /// Mask constants - /// - private const int TAPE_BIT = 0x40; - private const int EAR_BIT = 0x10; - private const int MIC_BIT = 0x08; + /// + /// Mask constants + /// + private const int TAPE_BIT = 0x40; + private const int EAR_BIT = 0x10; + private const int MIC_BIT = 0x08; - /// - /// Device responds to an IN instruction - /// - public bool ReadPort() - { - if (TapeIsPlaying) - { - GetEarBit(_cpu.TotalExecutedCycles); - } - /* + /// + /// Device responds to an IN instruction + /// + public bool ReadPort() + { + if (TapeIsPlaying) + { + GetEarBit(_cpu.TotalExecutedCycles); + } + /* if (currentState) { result |= TAPE_BIT; @@ -780,53 +780,53 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC } */ - if (!TapeIsPlaying) - { - //if (_machine.UPDDiskDevice == null || !_machine.UPDDiskDevice.FDD_IsDiskLoaded) - //MonitorRead(); - } - //if (_machine.UPDDiskDevice == null || !_machine.UPDDiskDevice.FDD_IsDiskLoaded) - //MonitorRead(); + if (!TapeIsPlaying) + { + //if (_machine.UPDDiskDevice == null || !_machine.UPDDiskDevice.FDD_IsDiskLoaded) + //MonitorRead(); + } + //if (_machine.UPDDiskDevice == null || !_machine.UPDDiskDevice.FDD_IsDiskLoaded) + //MonitorRead(); - return true; - } + return true; + } - /// - /// Device responds to an OUT instruction - /// - public void WritePort(bool state) - { - // not implemented + /// + /// Device responds to an OUT instruction + /// + public void WritePort(bool state) + { + // not implemented - /* + /* if (!TapeIsPlaying) { currentState = ((byte)result & 0x10) != 0; } */ - } + } - #endregion + #endregion - #region State Serialization + #region State Serialization - /// - /// Bizhawk state serialization - /// - public void SyncState(Serializer ser) - { - ser.BeginSection(nameof(DatacorderDevice)); - ser.Sync(nameof(counter), ref counter); - ser.Sync(nameof(_currentDataBlockIndex), ref _currentDataBlockIndex); - ser.Sync(nameof(_position), ref _position); - ser.Sync(nameof(_tapeIsPlaying), ref _tapeIsPlaying); - ser.Sync(nameof(_lastCycle), ref _lastCycle); - ser.Sync(nameof(_waitEdge), ref _waitEdge); - ser.Sync(nameof(currentState), ref currentState); - ser.Sync(nameof(tapeMotor), ref tapeMotor); - ser.EndSection(); - } + /// + /// Bizhawk state serialization + /// + public void SyncState(Serializer ser) + { + ser.BeginSection(nameof(DatacorderDevice)); + ser.Sync(nameof(counter), ref counter); + ser.Sync(nameof(_currentDataBlockIndex), ref _currentDataBlockIndex); + ser.Sync(nameof(_position), ref _position); + ser.Sync(nameof(_tapeIsPlaying), ref _tapeIsPlaying); + ser.Sync(nameof(_lastCycle), ref _lastCycle); + ser.Sync(nameof(_waitEdge), ref _waitEdge); + ser.Sync(nameof(currentState), ref currentState); + ser.Sync(nameof(tapeMotor), ref tapeMotor); + ser.EndSection(); + } - #endregion - } + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/CHRN.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/CHRN.cs index 2eb7f7c5be..c94f99c9ae 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/CHRN.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/CHRN.cs @@ -1,180 +1,180 @@  namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Used for the sector CHRN structure - /// - public class CHRN - { - /// - /// Track - /// - public byte C { get; set; } + /// + /// Used for the sector CHRN structure + /// + public class CHRN + { + /// + /// Track + /// + public byte C { get; set; } - /// - /// Side - /// - public byte H { get; set; } + /// + /// Side + /// + public byte H { get; set; } - /// - /// Sector ID - /// - public byte R { get; set; } + /// + /// Sector ID + /// + public byte R { get; set; } - /// - /// Sector Size - /// - public byte N { get; set; } + /// + /// Sector Size + /// + public byte N { get; set; } - /// - /// Status register 1 - /// - private byte _flag1; - public byte Flag1 - { - get { return _flag1; } - set { _flag1 = value; } - } + /// + /// Status register 1 + /// + private byte _flag1; + public byte Flag1 + { + get { return _flag1; } + set { _flag1 = value; } + } - /// - /// Status register 2 - /// - private byte _flag2; - public byte Flag2 - { - get { return _flag2; } - set { _flag2 = value; } - } + /// + /// Status register 2 + /// + private byte _flag2; + public byte Flag2 + { + get { return _flag2; } + set { _flag2 = value; } + } - /// - /// Used to store the last transmitted/received data bytes - /// - public byte[] DataBytes { get; set; } + /// + /// Used to store the last transmitted/received data bytes + /// + public byte[] DataBytes { get; set; } - /// - /// ID for the read/write data command - /// - public int DataID { get; set; } + /// + /// ID for the read/write data command + /// + public int DataID { get; set; } - #region Helper Methods + #region Helper Methods - /// - /// Missing Address Mark (Sector_ID or DAM not found) - /// - public bool ST1MA - { - get { return NECUPD765.GetBit(0, _flag1); } - set - { - if (value) { NECUPD765.SetBit(0, ref _flag1); } - else { NECUPD765.UnSetBit(0, ref _flag1); } - } - } + /// + /// Missing Address Mark (Sector_ID or DAM not found) + /// + public bool ST1MA + { + get { return NECUPD765.GetBit(0, _flag1); } + set + { + if (value) { NECUPD765.SetBit(0, ref _flag1); } + else { NECUPD765.UnSetBit(0, ref _flag1); } + } + } - /// - /// No Data (Sector_ID not found, CRC fail in ID_field) - /// - public bool ST1ND - { - get { return NECUPD765.GetBit(2, _flag1); } - set - { - if (value) { NECUPD765.SetBit(2, ref _flag1); } - else { NECUPD765.UnSetBit(2, ref _flag1); } - } - } + /// + /// No Data (Sector_ID not found, CRC fail in ID_field) + /// + public bool ST1ND + { + get { return NECUPD765.GetBit(2, _flag1); } + set + { + if (value) { NECUPD765.SetBit(2, ref _flag1); } + else { NECUPD765.UnSetBit(2, ref _flag1); } + } + } - /// - /// Data Error (CRC-fail in ID- or Data-Field) - /// - public bool ST1DE - { - get { return NECUPD765.GetBit(5, _flag1); } - set - { - if (value) { NECUPD765.SetBit(5, ref _flag1); } - else { NECUPD765.UnSetBit(5, ref _flag1); } - } - } + /// + /// Data Error (CRC-fail in ID- or Data-Field) + /// + public bool ST1DE + { + get { return NECUPD765.GetBit(5, _flag1); } + set + { + if (value) { NECUPD765.SetBit(5, ref _flag1); } + else { NECUPD765.UnSetBit(5, ref _flag1); } + } + } - /// - /// End of Track (set past most read/write commands) (see IC) - /// - public bool ST1EN - { - get { return NECUPD765.GetBit(7, _flag1); } - set - { - if (value) { NECUPD765.SetBit(7, ref _flag1); } - else { NECUPD765.UnSetBit(7, ref _flag1); } - } - } + /// + /// End of Track (set past most read/write commands) (see IC) + /// + public bool ST1EN + { + get { return NECUPD765.GetBit(7, _flag1); } + set + { + if (value) { NECUPD765.SetBit(7, ref _flag1); } + else { NECUPD765.UnSetBit(7, ref _flag1); } + } + } - /// - /// Missing Address Mark in Data Field (DAM not found) - /// - public bool ST2MD - { - get { return NECUPD765.GetBit(0, _flag2); } - set - { - if (value) { NECUPD765.SetBit(0, ref _flag2); } - else { NECUPD765.UnSetBit(0, ref _flag2); } - } - } + /// + /// Missing Address Mark in Data Field (DAM not found) + /// + public bool ST2MD + { + get { return NECUPD765.GetBit(0, _flag2); } + set + { + if (value) { NECUPD765.SetBit(0, ref _flag2); } + else { NECUPD765.UnSetBit(0, ref _flag2); } + } + } - /// - /// Bad Cylinder (read/programmed track-ID different and read-ID = FF) - /// - public bool ST2BC - { - get { return NECUPD765.GetBit(1, _flag2); } - set - { - if (value) { NECUPD765.SetBit(1, ref _flag2); } - else { NECUPD765.UnSetBit(1, ref _flag2); } - } - } + /// + /// Bad Cylinder (read/programmed track-ID different and read-ID = FF) + /// + public bool ST2BC + { + get { return NECUPD765.GetBit(1, _flag2); } + set + { + if (value) { NECUPD765.SetBit(1, ref _flag2); } + else { NECUPD765.UnSetBit(1, ref _flag2); } + } + } - /// - /// Wrong Cylinder (read/programmed track-ID different) (see b1) - /// - public bool ST2WC - { - get { return NECUPD765.GetBit(4, _flag2); } - set - { - if (value) { NECUPD765.SetBit(4, ref _flag2); } - else { NECUPD765.UnSetBit(4, ref _flag2); } - } - } + /// + /// Wrong Cylinder (read/programmed track-ID different) (see b1) + /// + public bool ST2WC + { + get { return NECUPD765.GetBit(4, _flag2); } + set + { + if (value) { NECUPD765.SetBit(4, ref _flag2); } + else { NECUPD765.UnSetBit(4, ref _flag2); } + } + } - /// - /// Data Error in Data Field (CRC-fail in data-field) - /// - public bool ST2DD - { - get { return NECUPD765.GetBit(5, _flag2); } - set - { - if (value) { NECUPD765.SetBit(5, ref _flag2); } - else { NECUPD765.UnSetBit(5, ref _flag2); } - } - } + /// + /// Data Error in Data Field (CRC-fail in data-field) + /// + public bool ST2DD + { + get { return NECUPD765.GetBit(5, _flag2); } + set + { + if (value) { NECUPD765.SetBit(5, ref _flag2); } + else { NECUPD765.UnSetBit(5, ref _flag2); } + } + } - /// - /// Control Mark (read/scan command found sector with deleted DAM) - /// - public bool ST2CM - { - get { return NECUPD765.GetBit(6, _flag2); } - set - { - if (value) { NECUPD765.SetBit(6, ref _flag2); } - else { NECUPD765.UnSetBit(6, ref _flag2); } - } - } + /// + /// Control Mark (read/scan command found sector with deleted DAM) + /// + public bool ST2CM + { + get { return NECUPD765.GetBit(6, _flag2); } + set + { + if (value) { NECUPD765.SetBit(6, ref _flag2); } + else { NECUPD765.UnSetBit(6, ref _flag2); } + } + } - #endregion - } + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.Definitions.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.Definitions.cs index e72da558c8..a560164411 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.Definitions.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.Definitions.cs @@ -3,824 +3,824 @@ using System; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Definitions - /// - #region Attribution - /* + /// + /// Definitions + /// + #region Attribution + /* Implementation based on the information contained here: http://www.cpcwiki.eu/index.php/765_FDC and here: http://www.cpcwiki.eu/imgs/f/f3/UPD765_Datasheet_OCRed.pdf */ - #endregion - public partial class NECUPD765 - { - #region Enums - - /// - /// Defines the current phase of the controller - /// - private enum Phase - { - /// - /// FDC is in an idle state, awaiting the next initial command byte - /// - Idle, - - /// - /// FDC is in a state waiting for the next command instruction - /// A command consists of a command byte (eventually including the MF, MK, SK bits), and up to eight parameter bytes - /// - Command, - - /// - /// During this phase, the actual data is transferred (if any). Usually that are the data bytes for the read/written sector(s), except for the Format Track Command, - /// in that case four bytes for each sector are transferred - /// - Execution, - - /// - /// Returns up to seven result bytes (depending on the command) that are containing status information. The Recalibrate and Seek Track commands do not return result bytes directly, - /// instead the program must wait until the Main Status Register signalizes that the command has been completed, and then it must (!) send a - /// Sense Interrupt State command to 'terminate' the Seek/Recalibrate command. - /// - Result - } - - /// - /// The lifecycle of an instruction - /// Similar to phase, this describes the current 'sub-phase' we are in when dealing with an instruction - /// - private enum InstructionState - { - /// - /// FDC has received a command byte and is currently reading parameter bytes from the data bus - /// - ReceivingParameters, - - /// - /// All parameter bytes have been received. This phase allows any neccessary setup before instruction execution starts - /// - PreExecution, - - /// - /// The start of instruction execution. This may end up with the FDC moving into result phase, - /// but also may also prepare the way for further processing to occur later in execution phase - /// - StartExecute, - - /// - /// Data is read or written in execution phase - /// - ExecutionReadWrite, - - /// - /// Execution phase is well under way. This state primarily deals with data transfer between CPU and FDC - /// - ExecutionWrite, - - /// - /// Execution phase is well under way. This state primarily deals with data transfer between FDC and CPU - /// - ExecutionRead, - - /// - /// Execution has finished and results bytes are ready to be read by the CPU - /// Initial result setup - /// - StartResult, - - /// - /// Result processing - /// - ProcessResult, - - /// - /// Results are being sent - /// - SendingResults, - - /// - /// Final cleanup tasks when the instruction has fully completed - /// - Completed - - } - - /// - /// Represents internal interrupt state of the FDC - /// - public enum InterruptState - { - /// - /// There is no interrupt - /// - None, - /// - /// Execution interrupt - /// - Execution, - /// - /// Result interrupt - /// - Result, - /// - /// Ready interrupt - /// - Ready, - /// - /// Seek interrupt - /// - Seek - } - - /// - /// Possible main states that each drive can be in - /// - public enum DriveMainState - { - /// - /// Drive is not doing anything - /// - None, - /// - /// Seek operation is in progress - /// - Seek, - /// - /// Recalibrate operation is in progress - /// - Recalibrate, - /// - /// A scan data operation is in progress - /// - Scan, - /// - /// A read ID operation is in progress - /// - ReadID, - /// - /// A read data operation is in progress - /// - ReadData, - /// - /// A read diagnostic (read track) operation is in progress - /// - ReadDiagnostic, - /// - /// A write id (format track) operation is in progress - /// - WriteID, - /// - /// A write data operation is in progress - /// - WriteData, - } - - /// - /// State information during a seek/recalibration operation - /// - public enum SeekSubState - { - /// - /// Seek hasnt started yet - /// - Idle, - /// - /// Delayed - /// - Wait, - /// - /// Setup for head move - /// - MoveInit, - /// - /// Seek is currently happening - /// - HeadMove, - /// - /// Head move with no delay - /// - MoveImmediate, - /// - /// Ready to complete - /// - PerformCompletion, - /// - /// Seek operation has completed - /// - SeekCompleted - } - - /// - /// Seek int code - /// - public enum SeekIntStatus - { - Normal, - Abnormal, - DriveNotReady, - } - - /// - /// The direction of a specific command - /// - private enum CommandDirection - { - /// - /// Data flows from UPD765A to Z80 - /// - OUT, - /// - /// Data flows from Z80 to UPD765A - /// - IN - } - - /// - /// Enum defining the different types of result that can be returned - /// - private enum ResultType - { - /// - /// Standard 7 result bytes are returned - /// - Standard, - /// - /// 1 byte returned - ST3 - /// (used for SenseDriveStatus) - /// - ST3, - /// - /// 1 byte returned - ST0 - /// (used for version & invalid) - /// - ST0, - /// - /// 2 bytes returned for sense interrupt status command - /// ST0 - /// CurrentCylinder - /// - Interrupt - } - - /// - /// Possible list of encountered drive status errors - /// - public enum Status - { - /// - /// No error detected - /// - None, - /// - /// An undefined error has been detected - /// - Undefined, - /// - /// Drive is not ready - /// - DriveNotReady, - /// - /// Invalid command received - /// - Invalid, - /// - /// The disk has its write protection tab enabled - /// - WriteProtected, - /// - /// The requested sector has not been found - /// - SectorNotFound - } - - /// - /// Represents the direction that the head is moving over the cylinders - /// Increment: Track number increasing (head moving from outside of disk inwards) - /// Decrement: Track number decreasing (head moving from inside of disk outwards) - /// - public enum SkipDirection - { - Increment, - Decrement - } - - #endregion - - #region Constants - - // Command Instruction Constants - // Designates the default postitions within the cmdbuffer array - - public const int CM_HEAD = 0; - /// - /// C - Track - /// - public const int CM_C = 1; - /// - /// H - Side - /// - public const int CM_H = 2; - /// - /// R - Sector ID - /// - public const int CM_R = 3; - /// - /// N - Sector size - /// - public const int CM_N = 4; - /// - /// EOT - End of track - /// - public const int CM_EOT = 5; - /// - /// GPL - Gap length - /// - public const int CM_GPL = 6; - /// - /// DTL - Data length - /// - public const int CM_DTL = 7; - /// - /// STP - Step - /// - public const int CM_STP = 7; - - // Result Instruction Constants - // Designates the default postitions within the cmdbuffer array - - /// - /// Status register 0 - /// - public const int RS_ST0 = 0; - /// - /// Status register 1 - /// - public const int RS_ST1 = 1; - /// - /// Status register 2 - /// - public const int RS_ST2 = 2; - /// - /// C - Track - /// - public const int RS_C = 3; - /// - /// H - Side - /// - public const int RS_H = 4; - /// - /// R - Sector ID - /// - public const int RS_R = 5; - /// - /// N - Sector size - /// - public const int RS_N = 6; - - // Main Status Register Constants - // Designates the bit positions within the Main status register - - /// - /// FDD0 Busy (seek/recalib active, until succesful sense intstat) - /// FDD number 0 is in the seek mode. If any of the DnB bits IS set FDC will not accept read or write command. - /// - public const int MSR_D0B = 0; - /// - /// FDD1 Busy (seek/recalib active, until succesful sense intstat) - /// FDD number 1 is in the seek mode. If any of the DnB bits IS set FDC will not accept read or write command. - /// - public const int MSR_D1B = 1; - /// - /// FDD2 Busy (seek/recalib active, until succesful sense intstat) - /// FDD number 2 is in the seek mode. If any of the DnB bits IS set FDC will not accept read or write command. - /// - public const int MSR_D2B = 2; - /// - /// FDD3 Busy (seek/recalib active, until succesful sense intstat) - /// FDD number 3 is in the seek mode. If any of the DnB bits IS set FDC will not accept read or write command. - /// - public const int MSR_D3B = 3; - /// - /// FDC Busy (still in command-, execution- or result-phase) - /// A Read or Write command is in orocess. (FDC Busy) FDC will not accept any other command - /// - public const int MSR_CB = 4; - /// - /// Execution Mode (still in execution-phase, non_DMA_only) - /// This bit is set only during execution ohase (Execution Mode) in non-DMA mode When DB5 goes low, execution phase has ended and result phase has started.It operates only during - /// non-DMA mode of operation - /// - public const int MSR_EXM = 5; - /// - /// Data Input/Output (0=CPU->FDC, 1=FDC->CPU) (see b7) - /// Indicates direction of data transfer between FDC and data regrster If DIO = 1, then transfer is from data register to the - /// processor.If DIO = 0, then transfer is from the processor to data register - /// - public const int MSR_DIO = 6; - /// - /// Request For Master (1=ready for next byte) (see b6 for direction) - /// ndicates data register IS ready to send or receive data to or from the processor Both bits DIO and RQM should be - /// used to perform the hand-shaking functions of “ready” and “directron” to the processor - /// - public const int MSR_RQM = 7; - - // Status Register 0 Constants - // Designates the bit positions within the status register 0 - - /// - /// Unit Select (driveno during interrupt) - /// This flag IS used to indicate a drive unit number at interrupt - /// - public const int SR0_US0 = 0; - - /// - /// Unit Select (driveno during interrupt) - /// This flag IS used to indicate a drive unit number at interrupt - /// - public const int SR0_US1 = 1; - - /// - /// Head Address (head during interrupt) - /// State of the head at interrupt - /// - public const int SR0_HD = 2; - - /// - /// Not Ready (drive not ready or non-existing 2nd head selected) - /// Not Ready - When the FDD IS in the not-ready state and a Read or Write command IS Issued, this - /// flag IS set If a Read or Write command is issued to side 1 of a single-sided drive, then this flag IS set - /// - public const int SR0_NR = 3; - - /// - /// Equipment Check (drive failure or recalibrate failed (retry)) - /// Equipment check - If a fault srgnal IS received from the FDD, or if the track 0 srgnal fails to occur after 77 - /// step pulses(Recalibrate Command) then this flag is set - /// - public const int SR0_EC = 4; - - /// - /// Seek End (Set if seek-command completed) - /// Seek end - When the FDC completes the Seek command, this flag IS set lo 1 (high) - /// - public const int SR0_SE = 5; - - /// - /// Interrupt Code (low byte) - /// Interrupt Code (0=OK, 1=aborted:readfail/OK if EN, 2=unknown cmd - /// or senseint with no int occured, 3=aborted:disc removed etc.) - /// - public const int SR0_IC0 = 6; - - /// - /// Interrupt Code (high byte) - /// Interrupt Code (0=OK, 1=aborted:readfail/OK if EN, 2=unknown cmd - /// or senseint with no int occured, 3=aborted:disc removed etc.) - /// - public const int SR0_IC1 = 7; - - // Status Register 1 Constants - // Designates the bit positions within the status register 1 - - /// - /// Missing Address Mark (Sector_ID or DAM not found) - /// Missing address mark - This bit is set i f the FDC does not detect the IDAM before 2 index pulses It is also set if - /// the FDC cannot find the DAM or DDAM after the IDAM i s found.MD bit of ST2 is also set at this time - /// - public const int SR1_MA = 0; - - /// - /// Not Writeable (tried to write/format disc with wprot_tab=on) - /// Not writable (write protect) - During execution of Write Data, Write Deleted Data or Write ID command. if the FDC - /// detect: a write protect srgnal from the FDD.then this flag is Set - /// - public const int SR1_NW = 1; - - /// - /// No Data - /// No Data (Sector_ID not found, CRC fail in ID_field) - /// - /// During execution of Read Data. Read Deleted Data Write Data.Write Deleted Data or Scan command, if the FDC cannot find - /// the sector specified in the IDR(2)Register, this flag i s set. - /// - /// During execution of the Read ID command. if the FDC cannot read the ID field without an error, then this flag IS set - /// - /// During execution of the Read Diagnostic command. if the starting sector cannot be found, then this flag is set - /// - public const int SR1_ND = 2; - - /// - /// Over Run (CPU too slow in execution-phase (ca. 26us/Byte)) - /// Overrun - If the FDC i s not serviced by the host system during data transfers within a certain time interval.this flaa i s set - /// - public const int SR1_OR = 4; - - /// - /// Data Error (CRC-fail in ID- or Data-Field) - /// Data error - When the FDC detects a CRC(1) error in either the ID field or the data field, this flag is set - /// - public const int SR1_DE = 5; - - /// - /// End of Track (set past most read/write commands) (see IC) - /// End of cylinder - When the FDC tries to access a sector beyond the final sector of a cylinder, this flag I S set - /// - public const int SR1_EN = 7; - - // Status Register 2 Constants - // Designates the bit positions within the status register 2 - - /// - /// Missing Address Mark in Data Field (DAM not found) - /// Missing address mark - When data IS read from the medium, i f the FDC cannot find a data address mark or deleted - /// data address mark, then this flag is set - /// - public const int SR2_MD = 0; - - /// - /// Bad Cylinder (read/programmed track-ID different and read-ID = FF) - /// Bad cylinder - This bit i s related to the ND bit. and when the contents of C on the medium is different - /// from that stored i n the IDR and the contents of C IS FFH.then this flag IS set - /// - public const int SR2_BC = 1; - - /// - /// Scan Not Satisfied (no fitting sector found) - /// Scan not satisfied - During execution of the Scan command, i f the F D cannot find a sector on the cylinder - /// which meets the condition.then this flag i s set - /// - public const int SR2_SN = 2; - - /// - /// Scan Equal Hit (equal) - /// Scan equal hit - During execution of the Scan command. i f the condition of “equal” is satisfied, this flag i s set - /// - public const int SR2_SH = 3; - - /// - /// Wrong Cylinder (read/programmed track-ID different) (see b1) - /// Wrong cylinder - This bit IS related to the ND bit, and when the contents of C(3) on the medium is different - /// from that stored i n the IDR.this flag is set - /// - public const int SR2_WC = 4; - - /// - /// Data Error in Data Field (CRC-fail in data-field) - /// Data error in data field - If the FDC detects a CRC error i n the data field then this flag is set - /// - public const int SR2_DD = 5; - - /// - /// Control Mark (read/scan command found sector with deleted DAM) - /// Control mark - During execution of the Read Data or Scan command, if the FDC encounters a sector - /// which contains a deleted data address mark, this flag is set Also set if DAM is - /// found during Read Deleted Data - /// - public const int SR2_CM = 6; - - // Status Register 3 Constants - // Designates the bit positions within the status register 3 - - /// - /// Unit select 0 - /// Unit Select (pin 28,29 of FDC) - /// - public const int SR3_US0 = 0; - - /// - /// Unit select 1 - /// Unit Select (pin 28,29 of FDC) - /// - public const int SR3_US1 = 1; - - /// - /// Head address (side select) - /// Head Address (pin 27 of FDC) - /// - public const int SR3_HD = 2; - - /// - /// Two Side (0=yes, 1=no (!)) - /// Two-side - This bit IS used to indicate the status of the two-side signal from the FDD - /// - public const int SR3_TS = 3; - - /// - /// Track 0 (on track 0 we are) - /// Track 0 - This bit IS used to indicate the status of the track 0 signal from the FDD - /// - public const int SR3_T0 = 4; - - /// - /// Ready - status of the ready signal from the fdd - /// Ready (drive ready signal) - /// - public const int SR3_RY = 5; - - /// - /// Write Protected (write protected) - /// Write protect - status of the wp signal from the fdd - /// - public const int SR3_WP = 6; - - /// - /// Fault - This bit is used to indicate the status of the fault signal from the FDD - /// Fault (if supported: 1=Drive failure) - /// - public const int SR3_FT = 7; - - // Interrupt Code Masks - - /// - /// 1 = aborted:readfail / OK if EN (end of track) - /// - public const byte IC_OK = 0x00; - - /// - /// 1 = aborted:readfail / OK if EN (end of track) - /// - public const byte IC_ABORTED_RF_OKEN = 0x40; - - /// - /// 2 = unknown cmd or senseint with no int occured - /// - public const byte IC_NO_INT_OCCURED = 0x80; - - /// - /// 3 = aborted:disc removed etc - /// - public const byte IC_ABORTED_DISCREMOVED = 0xC0; - - // command code constants - public const int CC_READ_DATA = 0x06; - public const int CC_READ_ID = 0x0a; - public const int CC_SPECIFY = 0x03; - public const int CC_READ_DIAGNOSTIC = 0x02; - public const int CC_SCAN_EQUAL = 0x11; - public const int CC_SCAN_HIGHOREQUAL = 0x1d; - public const int CC_SCAN_LOWOREQUAL = 0x19; - public const int CC_READ_DELETEDDATA = 0x0c; - public const int CC_WRITE_DATA = 0x05; - public const int CC_WRITE_ID = 0x0d; - public const int CC_WRITE_DELETEDDATA = 0x09; - public const int CC_SEEK = 0x0f; - public const int CC_RECALIBRATE = 0x07; - public const int CC_SENSE_INTSTATUS = 0x08; - public const int CC_SENSE_DRIVESTATUS = 0x04; - public const int CC_VERSION = 0x10; - public const int CC_INVALID = 0x00; - - // drive seek state constants - public const int SEEK_IDLE = 0; - public const int SEEK_SEEK = 1; - public const int SEEK_RECALIBRATE = 2; - // seek interrupt - public const int SEEK_INTACKNOWLEDGED = 3; - public const int SEEK_NORMALTERM = 4; - public const int SEEK_ABNORMALTERM = 5; - public const int SEEK_DRIVENOTREADY = 6; - - #endregion - - #region Classes & Structs - - /// - /// Class that holds information about a specific command - /// - private class Command - { -// /// -// /// Mask to remove potential parameter bits (5,6, and or 7) in order to identify the command -// /// -// public int BitMask { get; set; } - /// - /// The command code after bitmask has been applied - /// - public int CommandCode { get; set; } - /// - /// The number of bytes that make up the full command - /// - public int ParameterByteCount { get; set; } - /// - /// The number of result bytes that will be generated from the command - /// - public int ResultByteCount { get; set; } - /// - /// The command direction - /// IN - Z80 to UPD765A - /// OUT - UPD765A to Z80 - /// - public CommandDirection Direction { get; set; } - /// - /// Command makes use of the MT bit - /// - public bool MT; - /// - /// Command makes use of the MF bit - /// - public bool MF; - /// - /// Command makes use of the SK bit - /// - public bool SK; - /// - /// Read/Write command that is READ - /// - public bool IsRead; - /// - /// Read/Write command that is WRITE - /// - public bool IsWrite; - - /// - /// Delegate function that is called by this command - /// bool 1: EXECUTE - if TRUE the command will be executed. if FALSE the method will instead parse commmand parameter bytes - /// bool 2: RESULT - if TRUE - /// - public Action CommandDelegate { get; set; } - } - - /// - /// Storage for command parameters - /// - public class CommandParameters - { - /// - /// The requested drive - /// - public byte UnitSelect; - /// - /// The requested physical side - /// - public byte Side; - /// - /// The requested track (C) - /// - public byte Cylinder; - /// - /// The requested head (H) - /// - public byte Head; - /// - /// The requested sector (R) - /// - public byte Sector; - /// - /// The specified sector size (N) - /// - public byte SectorSize; - /// - /// The end of track or last sector value (EOT) - /// - public byte EOT; - /// - /// Gap3 length (GPL) - /// - public byte Gap3Length; - /// - /// Data length (DTL) - When N is defined as 00, DTL stands for the data length - /// which users are going to read out or write into the sector - /// - public byte DTL; - - /// - /// Clear down - /// - public void Reset() - { - UnitSelect = 0; - Side = 0; - Cylinder = 0; - Head = 0; - Sector = 0; - SectorSize = 0; - EOT = 0; - Gap3Length = 0; - DTL = 0; - } - - public void SyncState(Serializer ser) - { - ser.BeginSection("ActiveCmdParams"); - - ser.Sync(nameof(UnitSelect), ref UnitSelect); - ser.Sync(nameof(Side), ref Side); - ser.Sync(nameof(Cylinder), ref Cylinder); - ser.Sync(nameof(Head), ref Head); - ser.Sync(nameof(Sector), ref Sector); - ser.Sync(nameof(SectorSize), ref SectorSize); - ser.Sync(nameof(EOT), ref EOT); - ser.Sync(nameof(Gap3Length), ref Gap3Length); - ser.Sync(nameof(DTL), ref DTL); - - ser.EndSection(); - } - } - - - #endregion - } + #endregion + public partial class NECUPD765 + { + #region Enums + + /// + /// Defines the current phase of the controller + /// + private enum Phase + { + /// + /// FDC is in an idle state, awaiting the next initial command byte + /// + Idle, + + /// + /// FDC is in a state waiting for the next command instruction + /// A command consists of a command byte (eventually including the MF, MK, SK bits), and up to eight parameter bytes + /// + Command, + + /// + /// During this phase, the actual data is transferred (if any). Usually that are the data bytes for the read/written sector(s), except for the Format Track Command, + /// in that case four bytes for each sector are transferred + /// + Execution, + + /// + /// Returns up to seven result bytes (depending on the command) that are containing status information. The Recalibrate and Seek Track commands do not return result bytes directly, + /// instead the program must wait until the Main Status Register signalizes that the command has been completed, and then it must (!) send a + /// Sense Interrupt State command to 'terminate' the Seek/Recalibrate command. + /// + Result + } + + /// + /// The lifecycle of an instruction + /// Similar to phase, this describes the current 'sub-phase' we are in when dealing with an instruction + /// + private enum InstructionState + { + /// + /// FDC has received a command byte and is currently reading parameter bytes from the data bus + /// + ReceivingParameters, + + /// + /// All parameter bytes have been received. This phase allows any neccessary setup before instruction execution starts + /// + PreExecution, + + /// + /// The start of instruction execution. This may end up with the FDC moving into result phase, + /// but also may also prepare the way for further processing to occur later in execution phase + /// + StartExecute, + + /// + /// Data is read or written in execution phase + /// + ExecutionReadWrite, + + /// + /// Execution phase is well under way. This state primarily deals with data transfer between CPU and FDC + /// + ExecutionWrite, + + /// + /// Execution phase is well under way. This state primarily deals with data transfer between FDC and CPU + /// + ExecutionRead, + + /// + /// Execution has finished and results bytes are ready to be read by the CPU + /// Initial result setup + /// + StartResult, + + /// + /// Result processing + /// + ProcessResult, + + /// + /// Results are being sent + /// + SendingResults, + + /// + /// Final cleanup tasks when the instruction has fully completed + /// + Completed + + } + + /// + /// Represents internal interrupt state of the FDC + /// + public enum InterruptState + { + /// + /// There is no interrupt + /// + None, + /// + /// Execution interrupt + /// + Execution, + /// + /// Result interrupt + /// + Result, + /// + /// Ready interrupt + /// + Ready, + /// + /// Seek interrupt + /// + Seek + } + + /// + /// Possible main states that each drive can be in + /// + public enum DriveMainState + { + /// + /// Drive is not doing anything + /// + None, + /// + /// Seek operation is in progress + /// + Seek, + /// + /// Recalibrate operation is in progress + /// + Recalibrate, + /// + /// A scan data operation is in progress + /// + Scan, + /// + /// A read ID operation is in progress + /// + ReadID, + /// + /// A read data operation is in progress + /// + ReadData, + /// + /// A read diagnostic (read track) operation is in progress + /// + ReadDiagnostic, + /// + /// A write id (format track) operation is in progress + /// + WriteID, + /// + /// A write data operation is in progress + /// + WriteData, + } + + /// + /// State information during a seek/recalibration operation + /// + public enum SeekSubState + { + /// + /// Seek hasnt started yet + /// + Idle, + /// + /// Delayed + /// + Wait, + /// + /// Setup for head move + /// + MoveInit, + /// + /// Seek is currently happening + /// + HeadMove, + /// + /// Head move with no delay + /// + MoveImmediate, + /// + /// Ready to complete + /// + PerformCompletion, + /// + /// Seek operation has completed + /// + SeekCompleted + } + + /// + /// Seek int code + /// + public enum SeekIntStatus + { + Normal, + Abnormal, + DriveNotReady, + } + + /// + /// The direction of a specific command + /// + private enum CommandDirection + { + /// + /// Data flows from UPD765A to Z80 + /// + OUT, + /// + /// Data flows from Z80 to UPD765A + /// + IN + } + + /// + /// Enum defining the different types of result that can be returned + /// + private enum ResultType + { + /// + /// Standard 7 result bytes are returned + /// + Standard, + /// + /// 1 byte returned - ST3 + /// (used for SenseDriveStatus) + /// + ST3, + /// + /// 1 byte returned - ST0 + /// (used for version & invalid) + /// + ST0, + /// + /// 2 bytes returned for sense interrupt status command + /// ST0 + /// CurrentCylinder + /// + Interrupt + } + + /// + /// Possible list of encountered drive status errors + /// + public enum Status + { + /// + /// No error detected + /// + None, + /// + /// An undefined error has been detected + /// + Undefined, + /// + /// Drive is not ready + /// + DriveNotReady, + /// + /// Invalid command received + /// + Invalid, + /// + /// The disk has its write protection tab enabled + /// + WriteProtected, + /// + /// The requested sector has not been found + /// + SectorNotFound + } + + /// + /// Represents the direction that the head is moving over the cylinders + /// Increment: Track number increasing (head moving from outside of disk inwards) + /// Decrement: Track number decreasing (head moving from inside of disk outwards) + /// + public enum SkipDirection + { + Increment, + Decrement + } + + #endregion + + #region Constants + + // Command Instruction Constants + // Designates the default postitions within the cmdbuffer array + + public const int CM_HEAD = 0; + /// + /// C - Track + /// + public const int CM_C = 1; + /// + /// H - Side + /// + public const int CM_H = 2; + /// + /// R - Sector ID + /// + public const int CM_R = 3; + /// + /// N - Sector size + /// + public const int CM_N = 4; + /// + /// EOT - End of track + /// + public const int CM_EOT = 5; + /// + /// GPL - Gap length + /// + public const int CM_GPL = 6; + /// + /// DTL - Data length + /// + public const int CM_DTL = 7; + /// + /// STP - Step + /// + public const int CM_STP = 7; + + // Result Instruction Constants + // Designates the default postitions within the cmdbuffer array + + /// + /// Status register 0 + /// + public const int RS_ST0 = 0; + /// + /// Status register 1 + /// + public const int RS_ST1 = 1; + /// + /// Status register 2 + /// + public const int RS_ST2 = 2; + /// + /// C - Track + /// + public const int RS_C = 3; + /// + /// H - Side + /// + public const int RS_H = 4; + /// + /// R - Sector ID + /// + public const int RS_R = 5; + /// + /// N - Sector size + /// + public const int RS_N = 6; + + // Main Status Register Constants + // Designates the bit positions within the Main status register + + /// + /// FDD0 Busy (seek/recalib active, until succesful sense intstat) + /// FDD number 0 is in the seek mode. If any of the DnB bits IS set FDC will not accept read or write command. + /// + public const int MSR_D0B = 0; + /// + /// FDD1 Busy (seek/recalib active, until succesful sense intstat) + /// FDD number 1 is in the seek mode. If any of the DnB bits IS set FDC will not accept read or write command. + /// + public const int MSR_D1B = 1; + /// + /// FDD2 Busy (seek/recalib active, until succesful sense intstat) + /// FDD number 2 is in the seek mode. If any of the DnB bits IS set FDC will not accept read or write command. + /// + public const int MSR_D2B = 2; + /// + /// FDD3 Busy (seek/recalib active, until succesful sense intstat) + /// FDD number 3 is in the seek mode. If any of the DnB bits IS set FDC will not accept read or write command. + /// + public const int MSR_D3B = 3; + /// + /// FDC Busy (still in command-, execution- or result-phase) + /// A Read or Write command is in orocess. (FDC Busy) FDC will not accept any other command + /// + public const int MSR_CB = 4; + /// + /// Execution Mode (still in execution-phase, non_DMA_only) + /// This bit is set only during execution ohase (Execution Mode) in non-DMA mode When DB5 goes low, execution phase has ended and result phase has started.It operates only during + /// non-DMA mode of operation + /// + public const int MSR_EXM = 5; + /// + /// Data Input/Output (0=CPU->FDC, 1=FDC->CPU) (see b7) + /// Indicates direction of data transfer between FDC and data regrster If DIO = 1, then transfer is from data register to the + /// processor.If DIO = 0, then transfer is from the processor to data register + /// + public const int MSR_DIO = 6; + /// + /// Request For Master (1=ready for next byte) (see b6 for direction) + /// ndicates data register IS ready to send or receive data to or from the processor Both bits DIO and RQM should be + /// used to perform the hand-shaking functions of “ready” and “directron” to the processor + /// + public const int MSR_RQM = 7; + + // Status Register 0 Constants + // Designates the bit positions within the status register 0 + + /// + /// Unit Select (driveno during interrupt) + /// This flag IS used to indicate a drive unit number at interrupt + /// + public const int SR0_US0 = 0; + + /// + /// Unit Select (driveno during interrupt) + /// This flag IS used to indicate a drive unit number at interrupt + /// + public const int SR0_US1 = 1; + + /// + /// Head Address (head during interrupt) + /// State of the head at interrupt + /// + public const int SR0_HD = 2; + + /// + /// Not Ready (drive not ready or non-existing 2nd head selected) + /// Not Ready - When the FDD IS in the not-ready state and a Read or Write command IS Issued, this + /// flag IS set If a Read or Write command is issued to side 1 of a single-sided drive, then this flag IS set + /// + public const int SR0_NR = 3; + + /// + /// Equipment Check (drive failure or recalibrate failed (retry)) + /// Equipment check - If a fault srgnal IS received from the FDD, or if the track 0 srgnal fails to occur after 77 + /// step pulses(Recalibrate Command) then this flag is set + /// + public const int SR0_EC = 4; + + /// + /// Seek End (Set if seek-command completed) + /// Seek end - When the FDC completes the Seek command, this flag IS set lo 1 (high) + /// + public const int SR0_SE = 5; + + /// + /// Interrupt Code (low byte) + /// Interrupt Code (0=OK, 1=aborted:readfail/OK if EN, 2=unknown cmd + /// or senseint with no int occured, 3=aborted:disc removed etc.) + /// + public const int SR0_IC0 = 6; + + /// + /// Interrupt Code (high byte) + /// Interrupt Code (0=OK, 1=aborted:readfail/OK if EN, 2=unknown cmd + /// or senseint with no int occured, 3=aborted:disc removed etc.) + /// + public const int SR0_IC1 = 7; + + // Status Register 1 Constants + // Designates the bit positions within the status register 1 + + /// + /// Missing Address Mark (Sector_ID or DAM not found) + /// Missing address mark - This bit is set i f the FDC does not detect the IDAM before 2 index pulses It is also set if + /// the FDC cannot find the DAM or DDAM after the IDAM i s found.MD bit of ST2 is also set at this time + /// + public const int SR1_MA = 0; + + /// + /// Not Writeable (tried to write/format disc with wprot_tab=on) + /// Not writable (write protect) - During execution of Write Data, Write Deleted Data or Write ID command. if the FDC + /// detect: a write protect srgnal from the FDD.then this flag is Set + /// + public const int SR1_NW = 1; + + /// + /// No Data + /// No Data (Sector_ID not found, CRC fail in ID_field) + /// + /// During execution of Read Data. Read Deleted Data Write Data.Write Deleted Data or Scan command, if the FDC cannot find + /// the sector specified in the IDR(2)Register, this flag i s set. + /// + /// During execution of the Read ID command. if the FDC cannot read the ID field without an error, then this flag IS set + /// + /// During execution of the Read Diagnostic command. if the starting sector cannot be found, then this flag is set + /// + public const int SR1_ND = 2; + + /// + /// Over Run (CPU too slow in execution-phase (ca. 26us/Byte)) + /// Overrun - If the FDC i s not serviced by the host system during data transfers within a certain time interval.this flaa i s set + /// + public const int SR1_OR = 4; + + /// + /// Data Error (CRC-fail in ID- or Data-Field) + /// Data error - When the FDC detects a CRC(1) error in either the ID field or the data field, this flag is set + /// + public const int SR1_DE = 5; + + /// + /// End of Track (set past most read/write commands) (see IC) + /// End of cylinder - When the FDC tries to access a sector beyond the final sector of a cylinder, this flag I S set + /// + public const int SR1_EN = 7; + + // Status Register 2 Constants + // Designates the bit positions within the status register 2 + + /// + /// Missing Address Mark in Data Field (DAM not found) + /// Missing address mark - When data IS read from the medium, i f the FDC cannot find a data address mark or deleted + /// data address mark, then this flag is set + /// + public const int SR2_MD = 0; + + /// + /// Bad Cylinder (read/programmed track-ID different and read-ID = FF) + /// Bad cylinder - This bit i s related to the ND bit. and when the contents of C on the medium is different + /// from that stored i n the IDR and the contents of C IS FFH.then this flag IS set + /// + public const int SR2_BC = 1; + + /// + /// Scan Not Satisfied (no fitting sector found) + /// Scan not satisfied - During execution of the Scan command, i f the F D cannot find a sector on the cylinder + /// which meets the condition.then this flag i s set + /// + public const int SR2_SN = 2; + + /// + /// Scan Equal Hit (equal) + /// Scan equal hit - During execution of the Scan command. i f the condition of “equal” is satisfied, this flag i s set + /// + public const int SR2_SH = 3; + + /// + /// Wrong Cylinder (read/programmed track-ID different) (see b1) + /// Wrong cylinder - This bit IS related to the ND bit, and when the contents of C(3) on the medium is different + /// from that stored i n the IDR.this flag is set + /// + public const int SR2_WC = 4; + + /// + /// Data Error in Data Field (CRC-fail in data-field) + /// Data error in data field - If the FDC detects a CRC error i n the data field then this flag is set + /// + public const int SR2_DD = 5; + + /// + /// Control Mark (read/scan command found sector with deleted DAM) + /// Control mark - During execution of the Read Data or Scan command, if the FDC encounters a sector + /// which contains a deleted data address mark, this flag is set Also set if DAM is + /// found during Read Deleted Data + /// + public const int SR2_CM = 6; + + // Status Register 3 Constants + // Designates the bit positions within the status register 3 + + /// + /// Unit select 0 + /// Unit Select (pin 28,29 of FDC) + /// + public const int SR3_US0 = 0; + + /// + /// Unit select 1 + /// Unit Select (pin 28,29 of FDC) + /// + public const int SR3_US1 = 1; + + /// + /// Head address (side select) + /// Head Address (pin 27 of FDC) + /// + public const int SR3_HD = 2; + + /// + /// Two Side (0=yes, 1=no (!)) + /// Two-side - This bit IS used to indicate the status of the two-side signal from the FDD + /// + public const int SR3_TS = 3; + + /// + /// Track 0 (on track 0 we are) + /// Track 0 - This bit IS used to indicate the status of the track 0 signal from the FDD + /// + public const int SR3_T0 = 4; + + /// + /// Ready - status of the ready signal from the fdd + /// Ready (drive ready signal) + /// + public const int SR3_RY = 5; + + /// + /// Write Protected (write protected) + /// Write protect - status of the wp signal from the fdd + /// + public const int SR3_WP = 6; + + /// + /// Fault - This bit is used to indicate the status of the fault signal from the FDD + /// Fault (if supported: 1=Drive failure) + /// + public const int SR3_FT = 7; + + // Interrupt Code Masks + + /// + /// 1 = aborted:readfail / OK if EN (end of track) + /// + public const byte IC_OK = 0x00; + + /// + /// 1 = aborted:readfail / OK if EN (end of track) + /// + public const byte IC_ABORTED_RF_OKEN = 0x40; + + /// + /// 2 = unknown cmd or senseint with no int occured + /// + public const byte IC_NO_INT_OCCURED = 0x80; + + /// + /// 3 = aborted:disc removed etc + /// + public const byte IC_ABORTED_DISCREMOVED = 0xC0; + + // command code constants + public const int CC_READ_DATA = 0x06; + public const int CC_READ_ID = 0x0a; + public const int CC_SPECIFY = 0x03; + public const int CC_READ_DIAGNOSTIC = 0x02; + public const int CC_SCAN_EQUAL = 0x11; + public const int CC_SCAN_HIGHOREQUAL = 0x1d; + public const int CC_SCAN_LOWOREQUAL = 0x19; + public const int CC_READ_DELETEDDATA = 0x0c; + public const int CC_WRITE_DATA = 0x05; + public const int CC_WRITE_ID = 0x0d; + public const int CC_WRITE_DELETEDDATA = 0x09; + public const int CC_SEEK = 0x0f; + public const int CC_RECALIBRATE = 0x07; + public const int CC_SENSE_INTSTATUS = 0x08; + public const int CC_SENSE_DRIVESTATUS = 0x04; + public const int CC_VERSION = 0x10; + public const int CC_INVALID = 0x00; + + // drive seek state constants + public const int SEEK_IDLE = 0; + public const int SEEK_SEEK = 1; + public const int SEEK_RECALIBRATE = 2; + // seek interrupt + public const int SEEK_INTACKNOWLEDGED = 3; + public const int SEEK_NORMALTERM = 4; + public const int SEEK_ABNORMALTERM = 5; + public const int SEEK_DRIVENOTREADY = 6; + + #endregion + + #region Classes & Structs + + /// + /// Class that holds information about a specific command + /// + private class Command + { + // /// + // /// Mask to remove potential parameter bits (5,6, and or 7) in order to identify the command + // /// + // public int BitMask { get; set; } + /// + /// The command code after bitmask has been applied + /// + public int CommandCode { get; set; } + /// + /// The number of bytes that make up the full command + /// + public int ParameterByteCount { get; set; } + /// + /// The number of result bytes that will be generated from the command + /// + public int ResultByteCount { get; set; } + /// + /// The command direction + /// IN - Z80 to UPD765A + /// OUT - UPD765A to Z80 + /// + public CommandDirection Direction { get; set; } + /// + /// Command makes use of the MT bit + /// + public bool MT; + /// + /// Command makes use of the MF bit + /// + public bool MF; + /// + /// Command makes use of the SK bit + /// + public bool SK; + /// + /// Read/Write command that is READ + /// + public bool IsRead; + /// + /// Read/Write command that is WRITE + /// + public bool IsWrite; + + /// + /// Delegate function that is called by this command + /// bool 1: EXECUTE - if TRUE the command will be executed. if FALSE the method will instead parse commmand parameter bytes + /// bool 2: RESULT - if TRUE + /// + public Action CommandDelegate { get; set; } + } + + /// + /// Storage for command parameters + /// + public class CommandParameters + { + /// + /// The requested drive + /// + public byte UnitSelect; + /// + /// The requested physical side + /// + public byte Side; + /// + /// The requested track (C) + /// + public byte Cylinder; + /// + /// The requested head (H) + /// + public byte Head; + /// + /// The requested sector (R) + /// + public byte Sector; + /// + /// The specified sector size (N) + /// + public byte SectorSize; + /// + /// The end of track or last sector value (EOT) + /// + public byte EOT; + /// + /// Gap3 length (GPL) + /// + public byte Gap3Length; + /// + /// Data length (DTL) - When N is defined as 00, DTL stands for the data length + /// which users are going to read out or write into the sector + /// + public byte DTL; + + /// + /// Clear down + /// + public void Reset() + { + UnitSelect = 0; + Side = 0; + Cylinder = 0; + Head = 0; + Sector = 0; + SectorSize = 0; + EOT = 0; + Gap3Length = 0; + DTL = 0; + } + + public void SyncState(Serializer ser) + { + ser.BeginSection("ActiveCmdParams"); + + ser.Sync(nameof(UnitSelect), ref UnitSelect); + ser.Sync(nameof(Side), ref Side); + ser.Sync(nameof(Cylinder), ref Cylinder); + ser.Sync(nameof(Head), ref Head); + ser.Sync(nameof(Sector), ref Sector); + ser.Sync(nameof(SectorSize), ref SectorSize); + ser.Sync(nameof(EOT), ref EOT); + ser.Sync(nameof(Gap3Length), ref Gap3Length); + ser.Sync(nameof(DTL), ref DTL); + + ser.EndSection(); + } + } + + + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.FDC.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.FDC.cs index 3d110314c5..42e747e908 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.FDC.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.FDC.cs @@ -5,223 +5,223 @@ using System.Linq; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// FDC State and Methods - /// - #region Attribution - /* + /// + /// FDC State and Methods + /// + #region Attribution + /* Implementation based on the information contained here: http://www.cpcwiki.eu/index.php/765_FDC and here: http://www.cpcwiki.eu/imgs/f/f3/UPD765_Datasheet_OCRed.pdf */ - #endregion - public partial class NECUPD765 - { - #region Controller State + #endregion + public partial class NECUPD765 + { + #region Controller State - /// - /// Signs whether the drive is active - /// - public bool DriveLight; + /// + /// Signs whether the drive is active + /// + public bool DriveLight; - /// - /// Collection of possible commands - /// - private List CommandList; + /// + /// Collection of possible commands + /// + private List CommandList; - /// - /// State parameters relating to the Active command - /// - public CommandParameters ActiveCommandParams = new CommandParameters(); + /// + /// State parameters relating to the Active command + /// + public CommandParameters ActiveCommandParams = new CommandParameters(); - /// - /// The current active phase of the controller - /// - private Phase ActivePhase = Phase.Command; + /// + /// The current active phase of the controller + /// + private Phase ActivePhase = Phase.Command; - /// - /// The currently active interrupt - /// - private InterruptState ActiveInterrupt = InterruptState.None; - /// - /// Command buffer - /// This does not contain the initial command byte (only parameter bytes) - /// - private byte[] CommBuffer = new byte[9]; + /// + /// The currently active interrupt + /// + private InterruptState ActiveInterrupt = InterruptState.None; + /// + /// Command buffer + /// This does not contain the initial command byte (only parameter bytes) + /// + private byte[] CommBuffer = new byte[9]; - /// - /// Current index within the command buffer - /// - private int CommCounter = 0; + /// + /// Current index within the command buffer + /// + private int CommCounter = 0; - /// - /// Initial command byte flag - /// Bit7 Multi Track (continue multi-sector-function on other head) - /// - private bool CMD_FLAG_MT; + /// + /// Initial command byte flag + /// Bit7 Multi Track (continue multi-sector-function on other head) + /// + private bool CMD_FLAG_MT; - /// - /// Initial command byte flag - /// Bit6 MFM-Mode-Bit (Default 1=Double Density) - /// - private bool CMD_FLAG_MF; + /// + /// Initial command byte flag + /// Bit6 MFM-Mode-Bit (Default 1=Double Density) + /// + private bool CMD_FLAG_MF; - /// - /// Initial command byte flag - /// Bit5 Skip-Bit (set if secs with deleted DAM shall be skipped) - /// - private bool CMD_FLAG_SK; + /// + /// Initial command byte flag + /// Bit5 Skip-Bit (set if secs with deleted DAM shall be skipped) + /// + private bool CMD_FLAG_SK; - /// - /// Step Rate Time (supplied via the specify command) - /// SRT stands for the steooino rate for the FDD ( 1 to 16 ms in 1 ms increments). - /// Stepping rate applies to all drives(FH= 1ms, EH= 2ms, etc.). - /// - private int SRT; + /// + /// Step Rate Time (supplied via the specify command) + /// SRT stands for the steooino rate for the FDD ( 1 to 16 ms in 1 ms increments). + /// Stepping rate applies to all drives(FH= 1ms, EH= 2ms, etc.). + /// + private int SRT; - /// - /// Keeps track of the current SRT state - /// - private int SRT_Counter; + /// + /// Keeps track of the current SRT state + /// + private int SRT_Counter; - /// - /// Head Unload Time (supplied via the specify command) - /// HUT stands for the head unload time after a Read or Write operation has occurred - /// (16 to 240 ms in 16 ms Increments) - /// - private int HUT; + /// + /// Head Unload Time (supplied via the specify command) + /// HUT stands for the head unload time after a Read or Write operation has occurred + /// (16 to 240 ms in 16 ms Increments) + /// + private int HUT; - /// - /// Keeps track of the current HUT state - /// - private int HUT_Counter; + /// + /// Keeps track of the current HUT state + /// + private int HUT_Counter; - /// - /// Head load Time (supplied via the specify command) - /// HLT stands for the head load time in the FDD (2 to 254 ms in 2 ms Increments) - /// - private int HLT; + /// + /// Head load Time (supplied via the specify command) + /// HLT stands for the head load time in the FDD (2 to 254 ms in 2 ms Increments) + /// + private int HLT; - /// - /// Keeps track of the current HLT state - /// - private int HLT_Counter; + /// + /// Keeps track of the current HLT state + /// + private int HLT_Counter; - /// - /// Non-DMA Mode (supplied via the specify command) - /// ND stands for operation in the non-DMA mode - /// - private bool ND; + /// + /// Non-DMA Mode (supplied via the specify command) + /// ND stands for operation in the non-DMA mode + /// + private bool ND; - /// - /// In lieu of actual timing, this will count status reads in execution phase - /// where the CPU hasnt actually read any bytes - /// - private int OverrunCounter; + /// + /// In lieu of actual timing, this will count status reads in execution phase + /// where the CPU hasnt actually read any bytes + /// + private int OverrunCounter; - /// - /// Contains result bytes in result phase - /// - private byte[] ResBuffer = new byte[7]; + /// + /// Contains result bytes in result phase + /// + private byte[] ResBuffer = new byte[7]; - /// - /// Contains sector data to be written/read in execution phase - /// - private byte[] ExecBuffer = new byte[0x8000]; + /// + /// Contains sector data to be written/read in execution phase + /// + private byte[] ExecBuffer = new byte[0x8000]; - /// - /// Interrupt result buffer - /// Persists (and returns when needed) the last result data when a sense interrupt status command happens - /// - private byte[] InterruptResultBuffer = new byte[2]; + /// + /// Interrupt result buffer + /// Persists (and returns when needed) the last result data when a sense interrupt status command happens + /// + private byte[] InterruptResultBuffer = new byte[2]; - /// - /// Current index within the result buffer - /// - private int ResCounter = 0; + /// + /// Current index within the result buffer + /// + private int ResCounter = 0; - /// - /// The byte length of the currently active command - /// This may or may not be the same as the actual command resultbytes value - /// - private int ResLength = 0; + /// + /// The byte length of the currently active command + /// This may or may not be the same as the actual command resultbytes value + /// + private int ResLength = 0; - /// - /// Index for sector data within the result buffer - /// - private int ExecCounter = 0; + /// + /// Index for sector data within the result buffer + /// + private int ExecCounter = 0; - /// - /// The length of the current exec command - /// - private int ExecLength = 0; + /// + /// The length of the current exec command + /// + private int ExecLength = 0; - /// - /// The last write byte that was received during execution phase - /// - private byte LastSectorDataWriteByte = 0; + /// + /// The last write byte that was received during execution phase + /// + private byte LastSectorDataWriteByte = 0; - /// - /// The last read byte to be sent during execution phase - /// - private byte LastSectorDataReadByte = 0; + /// + /// The last read byte to be sent during execution phase + /// + private byte LastSectorDataReadByte = 0; - /// - /// The last parameter byte that was written to the FDC - /// - private byte LastByteReceived = 0; + /// + /// The last parameter byte that was written to the FDC + /// + private byte LastByteReceived = 0; - /// - /// Delay for reading sector - /// - private int SectorDelayCounter = 0; + /// + /// Delay for reading sector + /// + private int SectorDelayCounter = 0; - /// - /// The phyical sector ID - /// - private int SectorID = 0; + /// + /// The phyical sector ID + /// + private int SectorID = 0; - /// - /// Counter for index pulses - /// - private int IndexPulseCounter; + /// + /// Counter for index pulses + /// + private int IndexPulseCounter; - /// - /// Specifies the index of the currently selected command (in the CommandList) - /// - public int CMDIndex - { - get { return _cmdIndex; } - set - { - _cmdIndex = value; - ActiveCommand = CommandList[_cmdIndex]; - } - } - private int _cmdIndex; + /// + /// Specifies the index of the currently selected command (in the CommandList) + /// + public int CMDIndex + { + get { return _cmdIndex; } + set + { + _cmdIndex = value; + ActiveCommand = CommandList[_cmdIndex]; + } + } + private int _cmdIndex; - /// - /// The currently active command - /// - private Command ActiveCommand; + /// + /// The currently active command + /// + private Command ActiveCommand; - /// - /// Main status register (accessed via reads to port 0x2ffd) - /// - /* + /// + /// Main status register (accessed via reads to port 0x2ffd) + /// + /* b0..3 DB FDD0..3 Busy (seek/recalib active, until succesful sense intstat) b4 CB FDC Busy (still in command-, execution- or result-phase) b5 EXM Execution Mode (still in execution-phase, non_DMA_only) b6 DIO Data Input/Output (0=CPU->FDC, 1=FDC->CPU) (see b7) b7 RQM Request For Master (1=ready for next byte) (see b6 for direction) */ - private byte StatusMain; + private byte StatusMain; - /// - /// Status Register 0 - /// - /* + /// + /// Status Register 0 + /// + /* b0,1 US Unit Select (driveno during interrupt) b2 HD Head Address (head during interrupt) b3 NR Not Ready (drive not ready or non-existing 2nd head selected) @@ -230,12 +230,12 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC b6,7 IC Interrupt Code (0=OK, 1=aborted:readfail/OK if EN, 2=unknown cmd or senseint with no int occured, 3=aborted:disc removed etc.) */ - private byte Status0; + private byte Status0; - /// - /// Status Register 1 - /// - /* + /// + /// Status Register 1 + /// + /* b0 MA Missing Address Mark (Sector_ID or DAM not found) b1 NW Not Writeable (tried to write/format disc with wprot_tab=on) b2 ND No Data (Sector_ID not found, CRC fail in ID_field) @@ -244,12 +244,12 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC b5 DE Data Error (CRC-fail in ID- or Data-Field) b7 EN End of Track (set past most read/write commands) (see IC) */ - private byte Status1; + private byte Status1; - /// - /// Status Register 2 - /// - /* + /// + /// Status Register 2 + /// + /* b0 MD Missing Address Mark in Data Field (DAM not found) b1 BC Bad Cylinder (read/programmed track-ID different and read-ID = FF) b2 SN Scan Not Satisfied (no fitting sector found) @@ -259,12 +259,12 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC b6 CM Control Mark (read/scan command found sector with deleted DAM) b7 0 Not Used */ - private byte Status2; + private byte Status2; - /// - /// Status Register 3 - /// - /* + /// + /// Status Register 3 + /// + /* b0,1 US Unit Select (pin 28,29 of FDC) b2 HD Head Address (pin 27 of FDC) b3 TS Two Side (0=yes, 1=no (!)) @@ -273,1900 +273,1900 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC b6 WP Write Protected (write protected) b7 FT Fault (if supported: 1=Drive failure) */ - private byte Status3; + private byte Status3; - #endregion + #endregion - #region UPD Internal Functions + #region UPD Internal Functions - #region READ Commands + #region READ Commands - /// - /// Read Data - /// COMMAND: 8 parameter bytes - /// EXECUTION: Data transfer between FDD and FDC - /// RESULT: 7 result bytes - /// - private void UPD_ReadData() - { - switch (ActivePhase) - { - //---------------------------------------- - // FDC is waiting for a command byte - //---------------------------------------- - case Phase.Idle: - break; + /// + /// Read Data + /// COMMAND: 8 parameter bytes + /// EXECUTION: Data transfer between FDD and FDC + /// RESULT: 7 result bytes + /// + private void UPD_ReadData() + { + switch (ActivePhase) + { + //---------------------------------------- + // FDC is waiting for a command byte + //---------------------------------------- + case Phase.Idle: + break; - //---------------------------------------- - // Receiving command parameter bytes - //---------------------------------------- - case Phase.Command: + //---------------------------------------- + // Receiving command parameter bytes + //---------------------------------------- + case Phase.Command: - // store the parameter in the command buffer - CommBuffer[CommCounter] = LastByteReceived; + // store the parameter in the command buffer + CommBuffer[CommCounter] = LastByteReceived; - // process parameter byte - ParseParamByteStandard(CommCounter); + // process parameter byte + ParseParamByteStandard(CommCounter); - // increment command parameter counter - CommCounter++; + // increment command parameter counter + CommCounter++; - // was that the last parameter byte? - if (CommCounter == ActiveCommand.ParameterByteCount) - { - // all parameter bytes received - setup for execution phase + // was that the last parameter byte? + if (CommCounter == ActiveCommand.ParameterByteCount) + { + // all parameter bytes received - setup for execution phase - // clear exec buffer and status registers - ClearExecBuffer(); - Status0 = 0; - Status1 = 0; - Status2 = 0; - Status3 = 0; + // clear exec buffer and status registers + ClearExecBuffer(); + Status0 = 0; + Status1 = 0; + Status2 = 0; + Status3 = 0; - // temp sector index - byte secIdx = ActiveCommandParams.Sector; + // temp sector index + byte secIdx = ActiveCommandParams.Sector; - // hack for when another drive (non-existent) is being called - if (ActiveDrive.ID != 0) - DiskDriveIndex = 0; + // hack for when another drive (non-existent) is being called + if (ActiveDrive.ID != 0) + DiskDriveIndex = 0; - // do we have a valid disk inserted? - if (!ActiveDrive.FLAG_READY) - { - // no disk, no tracks or motor is not on - SetBit(SR0_IC0, ref Status0); - SetBit(SR0_NR, ref Status0); + // do we have a valid disk inserted? + if (!ActiveDrive.FLAG_READY) + { + // no disk, no tracks or motor is not on + SetBit(SR0_IC0, ref Status0); + SetBit(SR0_NR, ref Status0); - CommitResultCHRN(); - CommitResultStatus(); - //ResBuffer[RS_ST0] = Status0; + CommitResultCHRN(); + CommitResultStatus(); + //ResBuffer[RS_ST0] = Status0; - // move to result phase - ActivePhase = Phase.Result; - break; - } + // move to result phase + ActivePhase = Phase.Result; + break; + } - int buffPos = 0; - int sectorSize = 0; - int maxTransferCap = 0; + int buffPos = 0; + int sectorSize = 0; + int maxTransferCap = 0; - // calculate requested size of data required - if (ActiveCommandParams.SectorSize == 0) - { - // When N=0, then DTL defines the data length which the FDC must treat as a sector. If DTL is smaller than the actual - // data length in a sector, the data beyond DTL in the sector is not sent to the Data Bus. The FDC reads (internally) - // the complete sector performing the CRC check and, depending upon the manner of command termination, may perform - // a Multi-Sector Read Operation. - sectorSize = ActiveCommandParams.DTL; + // calculate requested size of data required + if (ActiveCommandParams.SectorSize == 0) + { + // When N=0, then DTL defines the data length which the FDC must treat as a sector. If DTL is smaller than the actual + // data length in a sector, the data beyond DTL in the sector is not sent to the Data Bus. The FDC reads (internally) + // the complete sector performing the CRC check and, depending upon the manner of command termination, may perform + // a Multi-Sector Read Operation. + sectorSize = ActiveCommandParams.DTL; - // calculate maximum transfer capacity - if (!CMD_FLAG_MF) - maxTransferCap = 3328; - } - else - { - // When N is non - zero, then DTL has no meaning and should be set to ffh - ActiveCommandParams.DTL = 0xFF; + // calculate maximum transfer capacity + if (!CMD_FLAG_MF) + maxTransferCap = 3328; + } + else + { + // When N is non - zero, then DTL has no meaning and should be set to ffh + ActiveCommandParams.DTL = 0xFF; - // calculate maximum transfer capacity - switch (ActiveCommandParams.SectorSize) - { - case 1: - if (CMD_FLAG_MF) - maxTransferCap = 6656; - else - maxTransferCap = 3840; - break; - case 2: - if (CMD_FLAG_MF) - maxTransferCap = 7680; - else - maxTransferCap = 4096; - break; - case 3: - if (CMD_FLAG_MF) - maxTransferCap = 8192; - else - maxTransferCap = 4096; - break; - } + // calculate maximum transfer capacity + switch (ActiveCommandParams.SectorSize) + { + case 1: + if (CMD_FLAG_MF) + maxTransferCap = 6656; + else + maxTransferCap = 3840; + break; + case 2: + if (CMD_FLAG_MF) + maxTransferCap = 7680; + else + maxTransferCap = 4096; + break; + case 3: + if (CMD_FLAG_MF) + maxTransferCap = 8192; + else + maxTransferCap = 4096; + break; + } - sectorSize = 0x80 << ActiveCommandParams.SectorSize; - } + sectorSize = 0x80 << ActiveCommandParams.SectorSize; + } var mtc = maxTransferCap; - // get the current track - var track = ActiveDrive.Disk.DiskTracks.Where(a => a.TrackNumber == ActiveDrive.CurrentTrackID).FirstOrDefault(); + // get the current track + var track = ActiveDrive.Disk.DiskTracks.Where(a => a.TrackNumber == ActiveDrive.CurrentTrackID).FirstOrDefault(); - if (track == null || track.NumberOfSectors <= 0) - { - // track could not be found - SetBit(SR0_IC0, ref Status0); - SetBit(SR0_NR, ref Status0); + if (track == null || track.NumberOfSectors <= 0) + { + // track could not be found + SetBit(SR0_IC0, ref Status0); + SetBit(SR0_NR, ref Status0); - CommitResultCHRN(); - CommitResultStatus(); + CommitResultCHRN(); + CommitResultStatus(); - //ResBuffer[RS_ST0] = Status0; + //ResBuffer[RS_ST0] = Status0; - // move to result phase - ActivePhase = Phase.Result; - break; - } + // move to result phase + ActivePhase = Phase.Result; + break; + } - FloppyDisk.Sector sector = null; + FloppyDisk.Sector sector = null; - // sector read loop - for (;;) - { - bool terminate = false; + // sector read loop + for (; ; ) + { + bool terminate = false; - // lookup the sector - sector = GetSector(); + // lookup the sector + sector = GetSector(); - if (sector == null) - { - // sector was not found after two passes of the disk index hole - SetBit(SR1_ND, ref Status1); - SetBit(SR0_IC0, ref Status0); - UnSetBit(SR0_IC1, ref Status0); + if (sector == null) + { + // sector was not found after two passes of the disk index hole + SetBit(SR1_ND, ref Status1); + SetBit(SR0_IC0, ref Status0); + UnSetBit(SR0_IC1, ref Status0); - // result requires the actual track id, rather than the sector track id - ActiveCommandParams.Cylinder = track.TrackNumber; + // result requires the actual track id, rather than the sector track id + ActiveCommandParams.Cylinder = track.TrackNumber; - CommitResultCHRN(); - CommitResultStatus(); - ActivePhase = Phase.Result; - break; - } + CommitResultCHRN(); + CommitResultStatus(); + ActivePhase = Phase.Result; + break; + } - // sector ID was found on this track + // sector ID was found on this track - // get status regs from sector - Status1 = sector.Status1; - Status2 = sector.Status2; + // get status regs from sector + Status1 = sector.Status1; + Status2 = sector.Status2; - // we dont need EN - UnSetBit(SR1_EN, ref Status1); + // we dont need EN + UnSetBit(SR1_EN, ref Status1); - // If SK=1, the FDC skips the sector with the Deleted Data Address Mark and reads the next sector. - // The CRC bits in the deleted data field are not checked when SK=1 - if (CMD_FLAG_SK && Status2.Bit(SR2_CM)) - { - if (ActiveCommandParams.Sector != ActiveCommandParams.EOT) - { - // increment the sector ID and search again - ActiveCommandParams.Sector++; - continue; - } - else - { - // no execution phase - SetBit(SR0_IC0, ref Status0); - UnSetBit(SR0_IC1, ref Status0); + // If SK=1, the FDC skips the sector with the Deleted Data Address Mark and reads the next sector. + // The CRC bits in the deleted data field are not checked when SK=1 + if (CMD_FLAG_SK && Status2.Bit(SR2_CM)) + { + if (ActiveCommandParams.Sector != ActiveCommandParams.EOT) + { + // increment the sector ID and search again + ActiveCommandParams.Sector++; + continue; + } + else + { + // no execution phase + SetBit(SR0_IC0, ref Status0); + UnSetBit(SR0_IC1, ref Status0); - // result requires the actual track id, rather than the sector track id - ActiveCommandParams.Cylinder = track.TrackNumber; + // result requires the actual track id, rather than the sector track id + ActiveCommandParams.Cylinder = track.TrackNumber; - CommitResultCHRN(); - CommitResultStatus(); - ActivePhase = Phase.Result; - break; - } - } + CommitResultCHRN(); + CommitResultStatus(); + ActivePhase = Phase.Result; + break; + } + } - // read the sector - for (int i = 0; i < sector.DataLen; i++) - { - ExecBuffer[buffPos++] = sector.ActualData[i]; - } + // read the sector + for (int i = 0; i < sector.DataLen; i++) + { + ExecBuffer[buffPos++] = sector.ActualData[i]; + } - // mark the sector read - sector.SectorReadCompleted(); + // mark the sector read + sector.SectorReadCompleted(); - // any CRC errors? - if (Status1.Bit(SR1_DE) || Status2.Bit(SR2_DD)) - { - SetBit(SR0_IC0, ref Status0); - UnSetBit(SR0_IC1, ref Status0); - terminate = true; - } + // any CRC errors? + if (Status1.Bit(SR1_DE) || Status2.Bit(SR2_DD)) + { + SetBit(SR0_IC0, ref Status0); + UnSetBit(SR0_IC1, ref Status0); + terminate = true; + } - if (!CMD_FLAG_SK && Status2.Bit(SR2_CM)) - { - // deleted address mark was detected with NO skip flag set - ActiveCommandParams.EOT = ActiveCommandParams.Sector; - SetBit(SR2_CM, ref Status2); - SetBit(SR0_IC0, ref Status0); - UnSetBit(SR0_IC1, ref Status0); - terminate = true; - } + if (!CMD_FLAG_SK && Status2.Bit(SR2_CM)) + { + // deleted address mark was detected with NO skip flag set + ActiveCommandParams.EOT = ActiveCommandParams.Sector; + SetBit(SR2_CM, ref Status2); + SetBit(SR0_IC0, ref Status0); + UnSetBit(SR0_IC1, ref Status0); + terminate = true; + } - if (sector.SectorID == ActiveCommandParams.EOT || terminate) - { - // this was the last sector to read - // or termination requested + if (sector.SectorID == ActiveCommandParams.EOT || terminate) + { + // this was the last sector to read + // or termination requested - SetBit(SR1_EN, ref Status1); + SetBit(SR1_EN, ref Status1); - int keyIndex = 0; - for (int i = 0; i < track.Sectors.Length; i++) - { - if (track.Sectors[i].SectorID == sector.SectorID) - { - keyIndex = i; - break; - } - } + int keyIndex = 0; + for (int i = 0; i < track.Sectors.Length; i++) + { + if (track.Sectors[i].SectorID == sector.SectorID) + { + keyIndex = i; + break; + } + } - if (keyIndex == track.Sectors.Length - 1) - { - // last sector on the cylinder, set EN - SetBit(SR1_EN, ref Status1); + if (keyIndex == track.Sectors.Length - 1) + { + // last sector on the cylinder, set EN + SetBit(SR1_EN, ref Status1); - // increment cylinder - ActiveCommandParams.Cylinder++; + // increment cylinder + ActiveCommandParams.Cylinder++; - // reset sector - ActiveCommandParams.Sector = sector.SectorID; // 1; - ActiveDrive.SectorIndex = 0; - } - else - { - ActiveDrive.SectorIndex++; - } + // reset sector + ActiveCommandParams.Sector = sector.SectorID; // 1; + ActiveDrive.SectorIndex = 0; + } + else + { + ActiveDrive.SectorIndex++; + } - UnSetBit(SR0_IC1, ref Status0); - if (terminate) - SetBit(SR0_IC0, ref Status0); - else - UnSetBit(SR0_IC0, ref Status0); + UnSetBit(SR0_IC1, ref Status0); + if (terminate) + SetBit(SR0_IC0, ref Status0); + else + UnSetBit(SR0_IC0, ref Status0); - SetBit(SR0_IC0, ref Status0); + SetBit(SR0_IC0, ref Status0); - // result requires the actual track id, rather than the sector track id - ActiveCommandParams.Cylinder = track.TrackNumber; + // result requires the actual track id, rather than the sector track id + ActiveCommandParams.Cylinder = track.TrackNumber; - CommitResultCHRN(); - CommitResultStatus(); - ActivePhase = Phase.Execution; - break; - } - else - { - // continue with multi-sector read operation - ActiveCommandParams.Sector++; - //ActiveDrive.SectorIndex++; - } - } + CommitResultCHRN(); + CommitResultStatus(); + ActivePhase = Phase.Execution; + break; + } + else + { + // continue with multi-sector read operation + ActiveCommandParams.Sector++; + //ActiveDrive.SectorIndex++; + } + } - if (ActivePhase == Phase.Execution) - { - ExecLength = buffPos; - ExecCounter = buffPos; + if (ActivePhase == Phase.Execution) + { + ExecLength = buffPos; + ExecCounter = buffPos; - DriveLight = true; - } - } + DriveLight = true; + } + } - break; + break; - //---------------------------------------- - // FDC in execution phase reading/writing bytes - //---------------------------------------- - case Phase.Execution: + //---------------------------------------- + // FDC in execution phase reading/writing bytes + //---------------------------------------- + case Phase.Execution: - var index = ExecLength - ExecCounter; + var index = ExecLength - ExecCounter; - LastSectorDataReadByte = ExecBuffer[index]; + LastSectorDataReadByte = ExecBuffer[index]; - OverrunCounter--; - ExecCounter--; + OverrunCounter--; + ExecCounter--; - break; + break; - //---------------------------------------- - // Result bytes being sent to CPU - //---------------------------------------- - case Phase.Result: - break; - } - } + //---------------------------------------- + // Result bytes being sent to CPU + //---------------------------------------- + case Phase.Result: + break; + } + } - /// - /// Read Deleted Data - /// COMMAND: 8 parameter bytes - /// EXECUTION: Data transfer between the FDD and FDC - /// RESULT: 7 result bytes - /// - private void UPD_ReadDeletedData() - { - switch (ActivePhase) - { - //---------------------------------------- - // FDC is waiting for a command byte - //---------------------------------------- - case Phase.Idle: - break; + /// + /// Read Deleted Data + /// COMMAND: 8 parameter bytes + /// EXECUTION: Data transfer between the FDD and FDC + /// RESULT: 7 result bytes + /// + private void UPD_ReadDeletedData() + { + switch (ActivePhase) + { + //---------------------------------------- + // FDC is waiting for a command byte + //---------------------------------------- + case Phase.Idle: + break; - //---------------------------------------- - // Receiving command parameter bytes - //---------------------------------------- - case Phase.Command: - // store the parameter in the command buffer - CommBuffer[CommCounter] = LastByteReceived; + //---------------------------------------- + // Receiving command parameter bytes + //---------------------------------------- + case Phase.Command: + // store the parameter in the command buffer + CommBuffer[CommCounter] = LastByteReceived; - // process parameter byte - ParseParamByteStandard(CommCounter); + // process parameter byte + ParseParamByteStandard(CommCounter); - // increment command parameter counter - CommCounter++; + // increment command parameter counter + CommCounter++; - // was that the last parameter byte? - if (CommCounter == ActiveCommand.ParameterByteCount) - { - // all parameter bytes received - setup for execution phase + // was that the last parameter byte? + if (CommCounter == ActiveCommand.ParameterByteCount) + { + // all parameter bytes received - setup for execution phase - // clear exec buffer and status registers - ClearExecBuffer(); - Status0 = 0; - Status1 = 0; - Status2 = 0; - Status3 = 0; + // clear exec buffer and status registers + ClearExecBuffer(); + Status0 = 0; + Status1 = 0; + Status2 = 0; + Status3 = 0; - // temp sector index - byte secIdx = ActiveCommandParams.Sector; + // temp sector index + byte secIdx = ActiveCommandParams.Sector; - // do we have a valid disk inserted? - if (!ActiveDrive.FLAG_READY) - { - // no disk, no tracks or motor is not on - SetBit(SR0_IC0, ref Status0); - SetBit(SR0_NR, ref Status0); + // do we have a valid disk inserted? + if (!ActiveDrive.FLAG_READY) + { + // no disk, no tracks or motor is not on + SetBit(SR0_IC0, ref Status0); + SetBit(SR0_NR, ref Status0); - CommitResultCHRN(); - CommitResultStatus(); - //ResBuffer[RS_ST0] = Status0; + CommitResultCHRN(); + CommitResultStatus(); + //ResBuffer[RS_ST0] = Status0; - // move to result phase - ActivePhase = Phase.Result; - break; - } + // move to result phase + ActivePhase = Phase.Result; + break; + } - int buffPos = 0; - int sectorSize = 0; - int maxTransferCap = 0; + int buffPos = 0; + int sectorSize = 0; + int maxTransferCap = 0; if (maxTransferCap > 0) { } - // calculate requested size of data required - if (ActiveCommandParams.SectorSize == 0) - { - // When N=0, then DTL defines the data length which the FDC must treat as a sector. If DTL is smaller than the actual - // data length in a sector, the data beyond DTL in the sector is not sent to the Data Bus. The FDC reads (internally) - // the complete sector performing the CRC check and, depending upon the manner of command termination, may perform - // a Multi-Sector Read Operation. - sectorSize = ActiveCommandParams.DTL; + // calculate requested size of data required + if (ActiveCommandParams.SectorSize == 0) + { + // When N=0, then DTL defines the data length which the FDC must treat as a sector. If DTL is smaller than the actual + // data length in a sector, the data beyond DTL in the sector is not sent to the Data Bus. The FDC reads (internally) + // the complete sector performing the CRC check and, depending upon the manner of command termination, may perform + // a Multi-Sector Read Operation. + sectorSize = ActiveCommandParams.DTL; - // calculate maximum transfer capacity - if (!CMD_FLAG_MF) - maxTransferCap = 3328; - } - else - { - // When N is non - zero, then DTL has no meaning and should be set to ffh - ActiveCommandParams.DTL = 0xFF; + // calculate maximum transfer capacity + if (!CMD_FLAG_MF) + maxTransferCap = 3328; + } + else + { + // When N is non - zero, then DTL has no meaning and should be set to ffh + ActiveCommandParams.DTL = 0xFF; - // calculate maximum transfer capacity - switch (ActiveCommandParams.SectorSize) - { - case 1: - if (CMD_FLAG_MF) - maxTransferCap = 6656; - else - maxTransferCap = 3840; - break; - case 2: - if (CMD_FLAG_MF) - maxTransferCap = 7680; - else - maxTransferCap = 4096; - break; - case 3: - if (CMD_FLAG_MF) - maxTransferCap = 8192; - else - maxTransferCap = 4096; - break; - } + // calculate maximum transfer capacity + switch (ActiveCommandParams.SectorSize) + { + case 1: + if (CMD_FLAG_MF) + maxTransferCap = 6656; + else + maxTransferCap = 3840; + break; + case 2: + if (CMD_FLAG_MF) + maxTransferCap = 7680; + else + maxTransferCap = 4096; + break; + case 3: + if (CMD_FLAG_MF) + maxTransferCap = 8192; + else + maxTransferCap = 4096; + break; + } - sectorSize = 0x80 << ActiveCommandParams.SectorSize; - } + sectorSize = 0x80 << ActiveCommandParams.SectorSize; + } - // get the current track - var track = ActiveDrive.Disk.DiskTracks.Where(a => a.TrackNumber == ActiveDrive.CurrentTrackID).FirstOrDefault(); + // get the current track + var track = ActiveDrive.Disk.DiskTracks.Where(a => a.TrackNumber == ActiveDrive.CurrentTrackID).FirstOrDefault(); - if (track == null || track.NumberOfSectors <= 0) - { - // track could not be found - SetBit(SR0_IC0, ref Status0); - SetBit(SR0_NR, ref Status0); + if (track == null || track.NumberOfSectors <= 0) + { + // track could not be found + SetBit(SR0_IC0, ref Status0); + SetBit(SR0_NR, ref Status0); - CommitResultCHRN(); - CommitResultStatus(); + CommitResultCHRN(); + CommitResultStatus(); - //ResBuffer[RS_ST0] = Status0; + //ResBuffer[RS_ST0] = Status0; - // move to result phase - ActivePhase = Phase.Result; - break; - } + // move to result phase + ActivePhase = Phase.Result; + break; + } - FloppyDisk.Sector sector = null; + FloppyDisk.Sector sector = null; - // sector read loop - for (;;) - { - bool terminate = false; + // sector read loop + for (; ; ) + { + bool terminate = false; - // lookup the sector - sector = GetSector(); + // lookup the sector + sector = GetSector(); - if (sector == null) - { - // sector was not found after two passes of the disk index hole - SetBit(SR1_ND, ref Status1); - SetBit(SR0_IC0, ref Status0); - UnSetBit(SR0_IC1, ref Status0); + if (sector == null) + { + // sector was not found after two passes of the disk index hole + SetBit(SR1_ND, ref Status1); + SetBit(SR0_IC0, ref Status0); + UnSetBit(SR0_IC1, ref Status0); - // result requires the actual track id, rather than the sector track id - ActiveCommandParams.Cylinder = track.TrackNumber; + // result requires the actual track id, rather than the sector track id + ActiveCommandParams.Cylinder = track.TrackNumber; - CommitResultCHRN(); - CommitResultStatus(); - ActivePhase = Phase.Result; - break; - } + CommitResultCHRN(); + CommitResultStatus(); + ActivePhase = Phase.Result; + break; + } - // sector ID was found on this track + // sector ID was found on this track - // get status regs from sector - Status1 = sector.Status1; - Status2 = sector.Status2; + // get status regs from sector + Status1 = sector.Status1; + Status2 = sector.Status2; - // we dont need EN - UnSetBit(SR1_EN, ref Status1); + // we dont need EN + UnSetBit(SR1_EN, ref Status1); - // invert CM for read deleted data command - if (Status2.Bit(SR2_CM)) - UnSetBit(SR2_CM, ref Status2); - else - SetBit(SR2_CM, ref Status2); + // invert CM for read deleted data command + if (Status2.Bit(SR2_CM)) + UnSetBit(SR2_CM, ref Status2); + else + SetBit(SR2_CM, ref Status2); - // skip flag is set and no DAM found - if (CMD_FLAG_SK && Status2.Bit(SR2_CM)) - { - if (ActiveCommandParams.Sector != ActiveCommandParams.EOT) - { - // increment the sector ID and search again - ActiveCommandParams.Sector++; - continue; - } - else - { - // no execution phase - SetBit(SR0_IC0, ref Status0); - UnSetBit(SR0_IC1, ref Status0); + // skip flag is set and no DAM found + if (CMD_FLAG_SK && Status2.Bit(SR2_CM)) + { + if (ActiveCommandParams.Sector != ActiveCommandParams.EOT) + { + // increment the sector ID and search again + ActiveCommandParams.Sector++; + continue; + } + else + { + // no execution phase + SetBit(SR0_IC0, ref Status0); + UnSetBit(SR0_IC1, ref Status0); - // result requires the actual track id, rather than the sector track id - ActiveCommandParams.Cylinder = track.TrackNumber; + // result requires the actual track id, rather than the sector track id + ActiveCommandParams.Cylinder = track.TrackNumber; - CommitResultCHRN(); - CommitResultStatus(); - ActivePhase = Phase.Result; - break; - } - } - // we can read this sector - else - { - // if DAM is not set this will be the last sector to read - if (Status2.Bit(SR2_CM)) - { - ActiveCommandParams.EOT = ActiveCommandParams.Sector; - } + CommitResultCHRN(); + CommitResultStatus(); + ActivePhase = Phase.Result; + break; + } + } + // we can read this sector + else + { + // if DAM is not set this will be the last sector to read + if (Status2.Bit(SR2_CM)) + { + ActiveCommandParams.EOT = ActiveCommandParams.Sector; + } - if (!CMD_FLAG_SK && !Status2.Bit(SR2_CM) && - ActiveDrive.Disk.Protection == ProtectionType.PaulOwens) - { - ActiveCommandParams.EOT = ActiveCommandParams.Sector; - SetBit(SR2_CM, ref Status2); - SetBit(SR0_IC0, ref Status0); - UnSetBit(SR0_IC1, ref Status0); - terminate = true; - } + if (!CMD_FLAG_SK && !Status2.Bit(SR2_CM) && + ActiveDrive.Disk.Protection == ProtectionType.PaulOwens) + { + ActiveCommandParams.EOT = ActiveCommandParams.Sector; + SetBit(SR2_CM, ref Status2); + SetBit(SR0_IC0, ref Status0); + UnSetBit(SR0_IC1, ref Status0); + terminate = true; + } - // read the sector - for (int i = 0; i < sectorSize; i++) - { - ExecBuffer[buffPos++] = sector.ActualData[i]; - } + // read the sector + for (int i = 0; i < sectorSize; i++) + { + ExecBuffer[buffPos++] = sector.ActualData[i]; + } - // mark the sector read - sector.SectorReadCompleted(); + // mark the sector read + sector.SectorReadCompleted(); - if (sector.SectorID == ActiveCommandParams.EOT) - { - // this was the last sector to read + if (sector.SectorID == ActiveCommandParams.EOT) + { + // this was the last sector to read - SetBit(SR1_EN, ref Status1); + SetBit(SR1_EN, ref Status1); - int keyIndex = 0; - for (int i = 0; i < track.Sectors.Length; i++) - { - if (track.Sectors[i].SectorID == sector.SectorID) - { - keyIndex = i; - break; - } - } + int keyIndex = 0; + for (int i = 0; i < track.Sectors.Length; i++) + { + if (track.Sectors[i].SectorID == sector.SectorID) + { + keyIndex = i; + break; + } + } - if (keyIndex == track.Sectors.Length - 1) - { - // last sector on the cylinder, set EN - SetBit(SR1_EN, ref Status1); + if (keyIndex == track.Sectors.Length - 1) + { + // last sector on the cylinder, set EN + SetBit(SR1_EN, ref Status1); - // increment cylinder - ActiveCommandParams.Cylinder++; + // increment cylinder + ActiveCommandParams.Cylinder++; - // reset sector - ActiveCommandParams.Sector = 1; - ActiveDrive.SectorIndex = 0; - } - else - { - ActiveDrive.SectorIndex++; - } + // reset sector + ActiveCommandParams.Sector = 1; + ActiveDrive.SectorIndex = 0; + } + else + { + ActiveDrive.SectorIndex++; + } - UnSetBit(SR0_IC1, ref Status0); - if (terminate) - SetBit(SR0_IC0, ref Status0); - else - UnSetBit(SR0_IC0, ref Status0); + UnSetBit(SR0_IC1, ref Status0); + if (terminate) + SetBit(SR0_IC0, ref Status0); + else + UnSetBit(SR0_IC0, ref Status0); - SetBit(SR0_IC0, ref Status0); + SetBit(SR0_IC0, ref Status0); - // result requires the actual track id, rather than the sector track id - ActiveCommandParams.Cylinder = track.TrackNumber; + // result requires the actual track id, rather than the sector track id + ActiveCommandParams.Cylinder = track.TrackNumber; - // remove CM (appears to be required to defeat Alkatraz copy protection) - UnSetBit(SR2_CM, ref Status2); + // remove CM (appears to be required to defeat Alkatraz copy protection) + UnSetBit(SR2_CM, ref Status2); - CommitResultCHRN(); - CommitResultStatus(); - ActivePhase = Phase.Execution; - break; - } - else - { - // continue with multi-sector read operation - ActiveCommandParams.Sector++; - //ActiveDrive.SectorIndex++; - } - } - } + CommitResultCHRN(); + CommitResultStatus(); + ActivePhase = Phase.Execution; + break; + } + else + { + // continue with multi-sector read operation + ActiveCommandParams.Sector++; + //ActiveDrive.SectorIndex++; + } + } + } - if (ActivePhase == Phase.Execution) - { - ExecLength = buffPos; - ExecCounter = buffPos; - DriveLight = true; - } - } - break; + if (ActivePhase == Phase.Execution) + { + ExecLength = buffPos; + ExecCounter = buffPos; + DriveLight = true; + } + } + break; - //---------------------------------------- - // FDC in execution phase reading/writing bytes - //---------------------------------------- - case Phase.Execution: - var index = ExecLength - ExecCounter; + //---------------------------------------- + // FDC in execution phase reading/writing bytes + //---------------------------------------- + case Phase.Execution: + var index = ExecLength - ExecCounter; - LastSectorDataReadByte = ExecBuffer[index]; + LastSectorDataReadByte = ExecBuffer[index]; - OverrunCounter--; - ExecCounter--; - - break; + OverrunCounter--; + ExecCounter--; - //---------------------------------------- - // Result bytes being sent to CPU - //---------------------------------------- - case Phase.Result: - break; - } - } + break; - /// - /// Read Diagnostic (read track) - /// COMMAND: 8 parameter bytes - /// EXECUTION: Data transfer between FDD and FDC. FDC reads all data fields from index hole to EDT - /// RESULT: 7 result bytes - /// - private void UPD_ReadDiagnostic() - { - switch (ActivePhase) - { - //---------------------------------------- - // FDC is waiting for a command byte - //---------------------------------------- - case Phase.Idle: - break; + //---------------------------------------- + // Result bytes being sent to CPU + //---------------------------------------- + case Phase.Result: + break; + } + } - //---------------------------------------- - // Receiving command parameter bytes - //---------------------------------------- - case Phase.Command: + /// + /// Read Diagnostic (read track) + /// COMMAND: 8 parameter bytes + /// EXECUTION: Data transfer between FDD and FDC. FDC reads all data fields from index hole to EDT + /// RESULT: 7 result bytes + /// + private void UPD_ReadDiagnostic() + { + switch (ActivePhase) + { + //---------------------------------------- + // FDC is waiting for a command byte + //---------------------------------------- + case Phase.Idle: + break; - // store the parameter in the command buffer - CommBuffer[CommCounter] = LastByteReceived; + //---------------------------------------- + // Receiving command parameter bytes + //---------------------------------------- + case Phase.Command: - // process parameter byte - ParseParamByteStandard(CommCounter); + // store the parameter in the command buffer + CommBuffer[CommCounter] = LastByteReceived; - // increment command parameter counter - CommCounter++; + // process parameter byte + ParseParamByteStandard(CommCounter); - // was that the last parameter byte? - if (CommCounter == ActiveCommand.ParameterByteCount) - { - // all parameter bytes received - setup for execution phase + // increment command parameter counter + CommCounter++; - // clear exec buffer and status registers - ClearExecBuffer(); - Status0 = 0; - Status1 = 0; - Status2 = 0; - Status3 = 0; + // was that the last parameter byte? + if (CommCounter == ActiveCommand.ParameterByteCount) + { + // all parameter bytes received - setup for execution phase - // temp sector index - byte secIdx = ActiveCommandParams.Sector; + // clear exec buffer and status registers + ClearExecBuffer(); + Status0 = 0; + Status1 = 0; + Status2 = 0; + Status3 = 0; - // do we have a valid disk inserted? - if (!ActiveDrive.FLAG_READY) - { - // no disk, no tracks or motor is not on - SetBit(SR0_IC0, ref Status0); - SetBit(SR0_NR, ref Status0); + // temp sector index + byte secIdx = ActiveCommandParams.Sector; - CommitResultCHRN(); - CommitResultStatus(); - //ResBuffer[RS_ST0] = Status0; + // do we have a valid disk inserted? + if (!ActiveDrive.FLAG_READY) + { + // no disk, no tracks or motor is not on + SetBit(SR0_IC0, ref Status0); + SetBit(SR0_NR, ref Status0); - // move to result phase - ActivePhase = Phase.Result; - break; - } + CommitResultCHRN(); + CommitResultStatus(); + //ResBuffer[RS_ST0] = Status0; - int buffPos = 0; - int sectorSize = 0; - int maxTransferCap = 0; + // move to result phase + ActivePhase = Phase.Result; + break; + } + + int buffPos = 0; + int sectorSize = 0; + int maxTransferCap = 0; if (maxTransferCap > 0) { } - // calculate requested size of data required - if (ActiveCommandParams.SectorSize == 0) - { - // When N=0, then DTL defines the data length which the FDC must treat as a sector. If DTL is smaller than the actual - // data length in a sector, the data beyond DTL in the sector is not sent to the Data Bus. The FDC reads (internally) - // the complete sector performing the CRC check and, depending upon the manner of command termination, may perform - // a Multi-Sector Read Operation. - sectorSize = ActiveCommandParams.DTL; - - // calculate maximum transfer capacity - if (!CMD_FLAG_MF) - maxTransferCap = 3328; - } - else - { - // When N is non - zero, then DTL has no meaning and should be set to ffh - ActiveCommandParams.DTL = 0xFF; - - // calculate maximum transfer capacity - switch (ActiveCommandParams.SectorSize) - { - case 1: - if (CMD_FLAG_MF) - maxTransferCap = 6656; - else - maxTransferCap = 3840; - break; - case 2: - if (CMD_FLAG_MF) - maxTransferCap = 7680; - else - maxTransferCap = 4096; - break; - case 3: - if (CMD_FLAG_MF) - maxTransferCap = 8192; - else - maxTransferCap = 4096; - break; - } - - sectorSize = 0x80 << ActiveCommandParams.SectorSize; - } - - // get the current track - var track = ActiveDrive.Disk.DiskTracks.Where(a => a.TrackNumber == ActiveDrive.CurrentTrackID).FirstOrDefault(); - - if (track == null || track.NumberOfSectors <= 0) - { - // track could not be found - SetBit(SR0_IC0, ref Status0); - SetBit(SR0_NR, ref Status0); - - CommitResultCHRN(); - CommitResultStatus(); - - //ResBuffer[RS_ST0] = Status0; - - // move to result phase - ActivePhase = Phase.Result; - break; - } - - //FloppyDisk.Sector sector = null; - ActiveDrive.SectorIndex = 0; - - int secCount = 0; - - // read the whole track - for (int i = 0; i < track.Sectors.Length; i++) - { - if (secCount >= ActiveCommandParams.EOT) - { - break; - } - - var sec = track.Sectors[i]; - for (int b = 0; b < sec.ActualData.Length; b++) - { - ExecBuffer[buffPos++] = sec.ActualData[b]; - } - - // mark the sector read - sec.SectorReadCompleted(); - - // end of sector - compare IDs - if (sec.TrackNumber != ActiveCommandParams.Cylinder || - sec.SideNumber != ActiveCommandParams.Head || - sec.SectorID != ActiveCommandParams.Sector || - sec.SectorSize != ActiveCommandParams.SectorSize) - { - SetBit(SR1_ND, ref Status1); - } - - secCount++; - ActiveDrive.SectorIndex = i; - } - - if (secCount == ActiveCommandParams.EOT) - { - // this was the last sector to read - // or termination requested - - int keyIndex = 0; - for (int i = 0; i < track.Sectors.Length; i++) - { - if (track.Sectors[i].SectorID == track.Sectors[ActiveDrive.SectorIndex].SectorID) - { - keyIndex = i; - break; - } - } - - if (keyIndex == track.Sectors.Length - 1) - { - // last sector on the cylinder, set EN - SetBit(SR1_EN, ref Status1); - - // increment cylinder - ActiveCommandParams.Cylinder++; - - // reset sector - ActiveCommandParams.Sector = 1; - ActiveDrive.SectorIndex = 0; - } - else - { - ActiveDrive.SectorIndex++; - } - - UnSetBit(SR0_IC1, ref Status0); - UnSetBit(SR0_IC0, ref Status0); - - CommitResultCHRN(); - CommitResultStatus(); - ActivePhase = Phase.Execution; - } - - if (ActivePhase == Phase.Execution) - { - ExecLength = buffPos; - ExecCounter = buffPos; - - DriveLight = true; - } - } - - break; - - //---------------------------------------- - // FDC in execution phase reading/writing bytes - //---------------------------------------- - case Phase.Execution: - - var index = ExecLength - ExecCounter; - - LastSectorDataReadByte = ExecBuffer[index]; - - OverrunCounter--; - ExecCounter--; - - break; - - //---------------------------------------- - // Result bytes being sent to CPU - //---------------------------------------- - case Phase.Result: - break; - } - } - - /// - /// Read ID - /// COMMAND: 1 parameter byte - /// EXECUTION: The first correct ID information on the cylinder is stored in the data register - /// RESULT: 7 result bytes - /// - private void UPD_ReadID() - { - switch (ActivePhase) - { - //---------------------------------------- - // FDC is waiting for a command byte - //---------------------------------------- - case Phase.Idle: - break; - - //---------------------------------------- - // Receiving command parameter bytes - //---------------------------------------- - case Phase.Command: - - // store the parameter in the command buffer - CommBuffer[CommCounter] = LastByteReceived; - - // process parameter byte - ParseParamByteStandard(CommCounter); - - // increment command parameter counter - CommCounter++; - - // was that the last parameter byte? - if (CommCounter == ActiveCommand.ParameterByteCount) - { - DriveLight = true; - - // all parameter bytes received - ClearResultBuffer(); - Status0 = 0; - Status1 = 0; - Status2 = 0; - Status3 = 0; - - // set unit select - //SetUnitSelect(ActiveDrive.ID, ref Status0); - - // HD should always be 0 - UnSetBit(SR0_HD, ref Status0); - - if (!ActiveDrive.FLAG_READY) - { - // no disk, no tracks or motor is not on - // it is at this point the +3 detects whether a disk is present - // if not (and after another readid and SIS) it will eventually proceed to loading from tape - SetBit(SR0_IC0, ref Status0); - SetBit(SR0_NR, ref Status0); - - // setup the result buffer - ResBuffer[RS_ST0] = Status0; - for (int i = 1; i < 7; i++) - ResBuffer[i] = 0; - - // move to result phase - ActivePhase = Phase.Result; - break; - } - - var track = ActiveDrive.Disk.DiskTracks.Where(a => a.TrackNumber == ActiveDrive.CurrentTrackID).FirstOrDefault(); - - if (track != null && track.NumberOfSectors > 0 && track.TrackNumber != 0xff) - { - // formatted track - - // is the index out of bounds? - if (ActiveDrive.SectorIndex >= track.NumberOfSectors) - { - // reset the index - ActiveDrive.SectorIndex = 0; - } - - if (ActiveDrive.SectorIndex == 0 && ActiveDrive.Disk.DiskTracks[ActiveDrive.CurrentTrackID].Sectors.Length > 1) - { - // looks like readid always skips the first sector on a track - ActiveDrive.SectorIndex++; - } - - // read the sector data - var data = track.Sectors[ActiveDrive.SectorIndex]; //.GetCHRN(); - ResBuffer[RS_C] = data.TrackNumber; - ResBuffer[RS_H] = data.SideNumber; - ResBuffer[RS_R] = data.SectorID; - ResBuffer[RS_N] = data.SectorSize; - - ResBuffer[RS_ST0] = Status0; - - // check for DAM & CRC - //if (data.Status2.Bit(SR2_CM)) - //SetBit(SR2_CM, ref ResBuffer[RS_ST2]); - - - // increment the current sector - ActiveDrive.SectorIndex++; - - // is the index out of bounds? - if (ActiveDrive.SectorIndex >= track.NumberOfSectors) - { - // reset the index - ActiveDrive.SectorIndex = 0; - } - } - else - { - // unformatted track? - CommitResultCHRN(); - - SetBit(SR0_IC0, ref Status0); - ResBuffer[RS_ST0] = Status0; - ResBuffer[RS_ST1] = 0x01; - } - - ActivePhase = Phase.Result; - } - - break; - - //---------------------------------------- - // FDC in execution phase reading/writing bytes - //---------------------------------------- - case Phase.Execution: - break; - - //---------------------------------------- - // Result bytes being sent to CPU - //---------------------------------------- - case Phase.Result: - break; - } - } - - #endregion - - #region WRITE Commands - - /// - /// Write Data - /// COMMAND: 8 parameter bytes - /// EXECUTION: Data transfer between FDC and FDD - /// RESULT: 7 result bytes - /// - private void UPD_WriteData() - { - switch (ActivePhase) - { - //---------------------------------------- - // FDC is waiting for a command byte - //---------------------------------------- - case Phase.Idle: - break; - - //---------------------------------------- - // Receiving command parameter bytes - //---------------------------------------- - case Phase.Command: - - // store the parameter in the command buffer - CommBuffer[CommCounter] = LastByteReceived; - - // process parameter byte - ParseParamByteStandard(CommCounter); - - // increment command parameter counter - CommCounter++; - - // was that the last parameter byte? - if (CommCounter == ActiveCommand.ParameterByteCount) - { - // all parameter bytes received - setup for execution phase - - // clear exec buffer and status registers - ClearExecBuffer(); - Status0 = 0; - Status1 = 0; - Status2 = 0; - Status3 = 0; - - // temp sector index - byte secIdx = ActiveCommandParams.Sector; - - // hack for when another drive (non-existent) is being called - if (ActiveDrive.ID != 0) - DiskDriveIndex = 0; - - // do we have a valid disk inserted? - if (!ActiveDrive.FLAG_READY) - { - // no disk, no tracks or motor is not on - SetBit(SR0_IC0, ref Status0); - SetBit(SR0_NR, ref Status0); - - CommitResultCHRN(); - CommitResultStatus(); - //ResBuffer[RS_ST0] = Status0; - - // move to result phase - ActivePhase = Phase.Result; - break; - } - - // check write protect tab - if (ActiveDrive.FLAG_WRITEPROTECT) - { - SetBit(SR0_IC0, ref Status0); - SetBit(SR1_NW, ref Status1); - - CommitResultCHRN(); - CommitResultStatus(); - //ResBuffer[RS_ST0] = Status0; - - // move to result phase - ActivePhase = Phase.Result; - break; - } - else - { - - // calculate the number of bytes to write - int byteCounter = 0; - byte startSecID = ActiveCommandParams.Sector; - byte endSecID = ActiveCommandParams.EOT; - bool lastSec = false; - - // get the first sector - var track = ActiveDrive.Disk.DiskTracks[ActiveCommandParams.Cylinder]; - //int secIndex = 0; - for (int s = 0; s < track.Sectors.Length; s++) - { - if (track.Sectors[s].SectorID == endSecID) - lastSec = true; - - for (int i = 0; i < 0x80 << ActiveCommandParams.SectorSize; i++) - { - byteCounter++; - - if (i == (0x80 << ActiveCommandParams.SectorSize) - 1 && lastSec) - { - break; - } - } - - if (lastSec) - break; - } - - ExecCounter = byteCounter; - ExecLength = byteCounter; - ActivePhase = Phase.Execution; - DriveLight = true; - break; - } - } - - break; - - //---------------------------------------- - // FDC in execution phase reading/writing bytes - //---------------------------------------- - case Phase.Execution: - - var index = ExecLength - ExecCounter; - - ExecBuffer[index] = LastSectorDataWriteByte; - - OverrunCounter--; - ExecCounter--; - - if (ExecCounter <= 0) - { - int cnt = 0; - - // all data received - byte startSecID = ActiveCommandParams.Sector; - byte endSecID = ActiveCommandParams.EOT; - bool lastSec = false; - var track = ActiveDrive.Disk.DiskTracks[ActiveCommandParams.Cylinder]; - //int secIndex = 0; - - for (int s = 0; s < track.Sectors.Length; s++) - { - if (cnt == ExecLength) - break; - - ActiveCommandParams.Sector = track.Sectors[s].SectorID; - - if (track.Sectors[s].SectorID == endSecID) - lastSec = true; - - int size = 0x80 << track.Sectors[s].SectorSize; - - for (int d = 0; d < size; d++) - { - track.Sectors[s].SectorData[d] = ExecBuffer[cnt++]; - } - - if (lastSec) - break; - } - - SetBit(SR0_IC0, ref Status0); - SetBit(SR1_EN, ref Status1); - - CommitResultCHRN(); - CommitResultStatus(); - } - - break; - - //---------------------------------------- - // Result bytes being sent to CPU - //---------------------------------------- - case Phase.Result: - break; - } - } - - /// - /// Write ID (format write) - /// COMMAND: 5 parameter bytes - /// EXECUTION: Entire track is formatted - /// RESULT: 7 result bytes - /// - private void UPD_WriteID() - { - switch (ActivePhase) - { - //---------------------------------------- - // FDC is waiting for a command byte - //---------------------------------------- - case Phase.Idle: - break; - - //---------------------------------------- - // Receiving command parameter bytes - //---------------------------------------- - case Phase.Command: - - // store the parameter in the command buffer - CommBuffer[CommCounter] = LastByteReceived; - - // process parameter byte - ParseParamByteStandard(CommCounter); - - // increment command parameter counter - CommCounter++; - - // was that the last parameter byte? - if (CommCounter == ActiveCommand.ParameterByteCount) - { - // all parameter bytes received - setup for execution phase - DriveLight = true; - - // clear exec buffer and status registers - ClearExecBuffer(); - Status0 = 0; - Status1 = 0; - Status2 = 0; - Status3 = 0; - - // temp sector index - byte secIdx = ActiveCommandParams.Sector; - - // hack for when another drive (non-existent) is being called - if (ActiveDrive.ID != 0) - DiskDriveIndex = 0; - - // do we have a valid disk inserted? - if (!ActiveDrive.FLAG_READY) - { - // no disk, no tracks or motor is not on - SetBit(SR0_IC0, ref Status0); - SetBit(SR0_NR, ref Status0); - - CommitResultCHRN(); - CommitResultStatus(); - //ResBuffer[RS_ST0] = Status0; - - // move to result phase - ActivePhase = Phase.Result; - break; - } - - // check write protect tab - if (ActiveDrive.FLAG_WRITEPROTECT) - { - SetBit(SR0_IC0, ref Status0); - SetBit(SR1_NW, ref Status1); - - CommitResultCHRN(); - CommitResultStatus(); - //ResBuffer[RS_ST0] = Status0; - - // move to result phase - ActivePhase = Phase.Result; - break; - } - else - { - // not implemented yet - SetBit(SR0_IC0, ref Status0); - SetBit(SR1_NW, ref Status1); - - CommitResultCHRN(); - CommitResultStatus(); - //ResBuffer[RS_ST0] = Status0; - - // move to result phase - ActivePhase = Phase.Result; - break; - } - } - - break; - - //---------------------------------------- - // FDC in execution phase reading/writing bytes - //---------------------------------------- - case Phase.Execution: - break; - - //---------------------------------------- - // Result bytes being sent to CPU - //---------------------------------------- - case Phase.Result: - break; - } - } - - /// - /// Write Deleted Data - /// COMMAND: 8 parameter bytes - /// EXECUTION: Data transfer between FDC and FDD - /// RESULT: 7 result bytes - /// - private void UPD_WriteDeletedData() - { - switch (ActivePhase) - { - //---------------------------------------- - // FDC is waiting for a command byte - //---------------------------------------- - case Phase.Idle: - break; - - //---------------------------------------- - // Receiving command parameter bytes - //---------------------------------------- - case Phase.Command: - - // store the parameter in the command buffer - CommBuffer[CommCounter] = LastByteReceived; - - // process parameter byte - ParseParamByteStandard(CommCounter); - - // increment command parameter counter - CommCounter++; - - // was that the last parameter byte? - if (CommCounter == ActiveCommand.ParameterByteCount) - { - // all parameter bytes received - setup for execution phase - - // clear exec buffer and status registers - ClearExecBuffer(); - Status0 = 0; - Status1 = 0; - Status2 = 0; - Status3 = 0; - - // temp sector index - byte secIdx = ActiveCommandParams.Sector; - - // hack for when another drive (non-existent) is being called - if (ActiveDrive.ID != 0) - DiskDriveIndex = 0; - - // do we have a valid disk inserted? - if (!ActiveDrive.FLAG_READY) - { - // no disk, no tracks or motor is not on - SetBit(SR0_IC0, ref Status0); - SetBit(SR0_NR, ref Status0); - - CommitResultCHRN(); - CommitResultStatus(); - //ResBuffer[RS_ST0] = Status0; - - // move to result phase - ActivePhase = Phase.Result; - break; - } - - // check write protect tab - if (ActiveDrive.FLAG_WRITEPROTECT) - { - SetBit(SR0_IC0, ref Status0); - SetBit(SR1_NW, ref Status1); - - CommitResultCHRN(); - CommitResultStatus(); - //ResBuffer[RS_ST0] = Status0; - - // move to result phase - ActivePhase = Phase.Result; - break; - } - else - { - - // calculate the number of bytes to write - int byteCounter = 0; - byte startSecID = ActiveCommandParams.Sector; - byte endSecID = ActiveCommandParams.EOT; - bool lastSec = false; - - // get the first sector - var track = ActiveDrive.Disk.DiskTracks[ActiveCommandParams.Cylinder]; - //int secIndex = 0; - for (int s = 0; s < track.Sectors.Length; s++) - { - if (track.Sectors[s].SectorID == endSecID) - lastSec = true; - - for (int i = 0; i < 0x80 << ActiveCommandParams.SectorSize; i++) - { - byteCounter++; - - if (i == (0x80 << ActiveCommandParams.SectorSize) - 1 && lastSec) - { - break; - } - } - - if (lastSec) - break; - } - - ExecCounter = byteCounter; - ExecLength = byteCounter; - ActivePhase = Phase.Execution; - DriveLight = true; - break; - } - } - - break; - - //---------------------------------------- - // FDC in execution phase reading/writing bytes - //---------------------------------------- - case Phase.Execution: - - var index = ExecLength - ExecCounter; - - ExecBuffer[index] = LastSectorDataWriteByte; - - OverrunCounter--; - ExecCounter--; - - if (ExecCounter <= 0) - { - int cnt = 0; - - // all data received - byte startSecID = ActiveCommandParams.Sector; - byte endSecID = ActiveCommandParams.EOT; - bool lastSec = false; - var track = ActiveDrive.Disk.DiskTracks[ActiveCommandParams.Cylinder]; - //int secIndex = 0; - - for (int s = 0; s < track.Sectors.Length; s++) - { - if (cnt == ExecLength) - break; - - ActiveCommandParams.Sector = track.Sectors[s].SectorID; - - if (track.Sectors[s].SectorID == endSecID) - lastSec = true; - - int size = 0x80 << track.Sectors[s].SectorSize; - - for (int d = 0; d < size; d++) - { - track.Sectors[s].SectorData[d] = ExecBuffer[cnt++]; - } - - if (lastSec) - break; - } - - SetBit(SR0_IC0, ref Status0); - SetBit(SR1_EN, ref Status1); - - CommitResultCHRN(); - CommitResultStatus(); - } - - break; - - //---------------------------------------- - // Result bytes being sent to CPU - //---------------------------------------- - case Phase.Result: - break; - } - } - - #endregion - - #region SCAN Commands - - /// - /// Scan Equal - /// COMMAND: 8 parameter bytes - /// EXECUTION: Data compared between the FDD and FDC - /// RESULT: 7 result bytes - /// - private void UPD_ScanEqual() - { - switch (ActivePhase) - { - //---------------------------------------- - // FDC is waiting for a command byte - //---------------------------------------- - case Phase.Idle: - break; - - //---------------------------------------- - // Receiving command parameter bytes - //---------------------------------------- - case Phase.Command: - break; - - //---------------------------------------- - // FDC in execution phase reading/writing bytes - //---------------------------------------- - case Phase.Execution: - break; - - //---------------------------------------- - // Result bytes being sent to CPU - //---------------------------------------- - case Phase.Result: - break; - } - } - - /// - /// Scan Low or Equal - /// COMMAND: 8 parameter bytes - /// EXECUTION: Data compared between the FDD and FDC - /// RESULT: 7 result bytes - /// - private void UPD_ScanLowOrEqual() - { - switch (ActivePhase) - { - //---------------------------------------- - // FDC is waiting for a command byte - //---------------------------------------- - case Phase.Idle: - break; - - //---------------------------------------- - // Receiving command parameter bytes - //---------------------------------------- - case Phase.Command: - break; - - //---------------------------------------- - // FDC in execution phase reading/writing bytes - //---------------------------------------- - case Phase.Execution: - break; - - //---------------------------------------- - // Result bytes being sent to CPU - //---------------------------------------- - case Phase.Result: - break; - } - } - - /// - /// Scan High or Equal - /// COMMAND: 8 parameter bytes - /// EXECUTION: Data compared between the FDD and FDC - /// RESULT: 7 result bytes - /// - private void UPD_ScanHighOrEqual() - { - switch (ActivePhase) - { - //---------------------------------------- - // FDC is waiting for a command byte - //---------------------------------------- - case Phase.Idle: - break; - - //---------------------------------------- - // Receiving command parameter bytes - //---------------------------------------- - case Phase.Command: - break; - - //---------------------------------------- - // FDC in execution phase reading/writing bytes - //---------------------------------------- - case Phase.Execution: - break; - - //---------------------------------------- - // Result bytes being sent to CPU - //---------------------------------------- - case Phase.Result: - break; - } - } - - #endregion - - #region OTHER Commands - - /// - /// Specify - /// COMMAND: 2 parameter bytes - /// EXECUTION: NO execution phase - /// RESULT: NO result phase - /// - /// Looks like specify command returns status 0x80 throughout its lifecycle - /// so CB is NOT set - /// - private void UPD_Specify() - { - switch (ActivePhase) - { - //---------------------------------------- - // FDC is waiting for a command byte - //---------------------------------------- - case Phase.Idle: - break; - - //---------------------------------------- - // Receiving command parameter bytes - //---------------------------------------- - case Phase.Command: - - // store the parameter in the command buffer - CommBuffer[CommCounter] = LastByteReceived; - - // process parameter byte - byte currByte = CommBuffer[CommCounter]; - BitArray bi = new BitArray(new byte[] { currByte }); - - switch (CommCounter) - { - // SRT & HUT - case 0: - SRT = 16 - (currByte >> 4) & 0x0f; - HUT = (currByte & 0x0f) << 4; - if (HUT == 0) - { - HUT = 255; - } - break; - // HLT & ND - case 1: - if (bi[0]) - ND = true; - else - ND = false; - - HLT = currByte & 0xfe; - if (HLT == 0) - { - HLT = 255; - } - break; - } - - // increment command parameter counter - CommCounter++; - - // was that the last parameter byte? - if (CommCounter == ActiveCommand.ParameterByteCount) - { - // all parameter bytes received - ActivePhase = Phase.Idle; - } - - break; - - //---------------------------------------- - // FDC in execution phase reading/writing bytes - //---------------------------------------- - case Phase.Execution: - break; - - //---------------------------------------- - // Result bytes being sent to CPU - //---------------------------------------- - case Phase.Result: - break; - } - } - - /// - /// Seek - /// COMMAND: 2 parameter bytes - /// EXECUTION: Head is positioned over proper cylinder on disk - /// RESULT: NO result phase - /// - private void UPD_Seek() - { - switch (ActivePhase) - { - //---------------------------------------- - // FDC is waiting for a command byte - //---------------------------------------- - case Phase.Idle: - break; - - //---------------------------------------- - // Receiving command parameter bytes - //---------------------------------------- - case Phase.Command: - // store the parameter in the command buffer - CommBuffer[CommCounter] = LastByteReceived; - - // process parameter byte - byte currByte = CommBuffer[CommCounter]; - switch (CommCounter) - { - case 0: - ParseParamByteStandard(CommCounter); - break; - case 1: - ActiveDrive.SeekingTrack = currByte; - break; - } - - // increment command parameter counter - CommCounter++; - - // was that the last parameter byte? - if (CommCounter == ActiveCommand.ParameterByteCount) - { - // all parameter bytes received - DriveLight = true; - ActivePhase = Phase.Execution; - ActiveCommand.CommandDelegate(); - } - break; - - //---------------------------------------- - // FDC in execution phase reading/writing bytes - //---------------------------------------- - case Phase.Execution: - // set seek flag - ActiveDrive.SeekStatus = SEEK_SEEK; - - if (ActiveDrive.CurrentTrackID == CommBuffer[CM_C]) - { - // we are already on the correct track - ActiveDrive.SectorIndex = 0; - } - else - { - // immediate seek - ActiveDrive.CurrentTrackID = CommBuffer[CM_C]; - - ActiveDrive.SectorIndex = 0; - - if (ActiveDrive.Disk.DiskTracks[ActiveDrive.CurrentTrackID].Sectors.Length > 1) - { - // always read the first sector - //ActiveDrive.SectorIndex++; - } - } - - // skip execution mode and go directly to idle - // result is determined by SIS command - ActivePhase = Phase.Idle; - break; - - //---------------------------------------- - // Result bytes being sent to CPU - //---------------------------------------- - case Phase.Result: - break; - } - } - - /// - /// Recalibrate (seek track 0) - /// COMMAND: 1 parameter byte - /// EXECUTION: Head retracted to track 0 - /// RESULT: NO result phase - /// - private void UPD_Recalibrate() - { - switch (ActivePhase) - { - //---------------------------------------- - // FDC is waiting for a command byte - //---------------------------------------- - case Phase.Idle: - break; - - //---------------------------------------- - // Receiving command parameter bytes - //---------------------------------------- - case Phase.Command: - // store the parameter in the command buffer - CommBuffer[CommCounter] = LastByteReceived; - - // process parameter byte - ParseParamByteStandard(CommCounter); - - // increment command parameter counter - CommCounter++; - - // was that the last parameter byte? - if (CommCounter == ActiveCommand.ParameterByteCount) - { - // all parameter bytes received - DriveLight = true; - ActivePhase = Phase.Execution; - ActiveCommand.CommandDelegate(); - } - break; - - //---------------------------------------- - // FDC in execution phase reading/writing bytes - //---------------------------------------- - case Phase.Execution: - - // immediate recalibration - ActiveDrive.TrackIndex = 0; - ActiveDrive.SectorIndex = 0; - - // recalibrate appears to always skip the first sector - //if (ActiveDrive.Disk.DiskTracks[ActiveDrive.TrackIndex].Sectors.Length > 1) - //ActiveDrive.SectorIndex++; - - // set seek flag - ActiveDrive.SeekStatus = SEEK_RECALIBRATE; - - // skip execution mode and go directly to idle - // result is determined by SIS command - ActivePhase = Phase.Idle; - break; - - //---------------------------------------- - // Result bytes being sent to CPU - //---------------------------------------- - case Phase.Result: - break; - } - } - - /// - /// Sense Interrupt Status - /// COMMAND: NO parameter bytes - /// EXECUTION: NO execution phase - /// RESULT: 2 result bytes - /// - private void UPD_SenseInterruptStatus() - { - switch (ActivePhase) - { - //---------------------------------------- - // FDC is waiting for a command byte - //---------------------------------------- - case Phase.Idle: - break; - - //---------------------------------------- - // Receiving command parameter bytes - //---------------------------------------- - case Phase.Command: - break; - - //---------------------------------------- - // FDC in execution phase reading/writing bytes - //---------------------------------------- - case Phase.Execution: - // SIS should return 2 bytes if sucessfully sensed an interrupt - // 1 byte otherwise - - // it seems like the +3 ROM makes 3 SIS calls for each seek/recalibrate call for some reason - // possibly one for each drive??? - // 1 - the interrupt is acknowleged with ST0 = 32 and track number - // 2 - second sis returns 1 ST0 byte with 192 - // 3 - third SIS call returns standard 1 byte 0x80 (unknown cmd or SIS with no interrupt occured) - // for now I will assume that the first call is aimed at DriveA, the second at DriveB (which we are NOT implementing) - - // check active drive first - if (ActiveDrive.SeekStatus == SEEK_RECALIBRATE || - ActiveDrive.SeekStatus == SEEK_SEEK) - { - // interrupt has been raised for this drive - // acknowledge - ActiveDrive.SeekStatus = SEEK_IDLE;// SEEK_INTACKNOWLEDGED; - - // result length 2 - ResLength = 2; - - // first byte ST0 0x20 - Status0 = 0x20; - ResBuffer[0] = Status0; - // second byte is the current track id - ResBuffer[1] = ActiveDrive.CurrentTrackID; - } - /* + // calculate requested size of data required + if (ActiveCommandParams.SectorSize == 0) + { + // When N=0, then DTL defines the data length which the FDC must treat as a sector. If DTL is smaller than the actual + // data length in a sector, the data beyond DTL in the sector is not sent to the Data Bus. The FDC reads (internally) + // the complete sector performing the CRC check and, depending upon the manner of command termination, may perform + // a Multi-Sector Read Operation. + sectorSize = ActiveCommandParams.DTL; + + // calculate maximum transfer capacity + if (!CMD_FLAG_MF) + maxTransferCap = 3328; + } + else + { + // When N is non - zero, then DTL has no meaning and should be set to ffh + ActiveCommandParams.DTL = 0xFF; + + // calculate maximum transfer capacity + switch (ActiveCommandParams.SectorSize) + { + case 1: + if (CMD_FLAG_MF) + maxTransferCap = 6656; + else + maxTransferCap = 3840; + break; + case 2: + if (CMD_FLAG_MF) + maxTransferCap = 7680; + else + maxTransferCap = 4096; + break; + case 3: + if (CMD_FLAG_MF) + maxTransferCap = 8192; + else + maxTransferCap = 4096; + break; + } + + sectorSize = 0x80 << ActiveCommandParams.SectorSize; + } + + // get the current track + var track = ActiveDrive.Disk.DiskTracks.Where(a => a.TrackNumber == ActiveDrive.CurrentTrackID).FirstOrDefault(); + + if (track == null || track.NumberOfSectors <= 0) + { + // track could not be found + SetBit(SR0_IC0, ref Status0); + SetBit(SR0_NR, ref Status0); + + CommitResultCHRN(); + CommitResultStatus(); + + //ResBuffer[RS_ST0] = Status0; + + // move to result phase + ActivePhase = Phase.Result; + break; + } + + //FloppyDisk.Sector sector = null; + ActiveDrive.SectorIndex = 0; + + int secCount = 0; + + // read the whole track + for (int i = 0; i < track.Sectors.Length; i++) + { + if (secCount >= ActiveCommandParams.EOT) + { + break; + } + + var sec = track.Sectors[i]; + for (int b = 0; b < sec.ActualData.Length; b++) + { + ExecBuffer[buffPos++] = sec.ActualData[b]; + } + + // mark the sector read + sec.SectorReadCompleted(); + + // end of sector - compare IDs + if (sec.TrackNumber != ActiveCommandParams.Cylinder || + sec.SideNumber != ActiveCommandParams.Head || + sec.SectorID != ActiveCommandParams.Sector || + sec.SectorSize != ActiveCommandParams.SectorSize) + { + SetBit(SR1_ND, ref Status1); + } + + secCount++; + ActiveDrive.SectorIndex = i; + } + + if (secCount == ActiveCommandParams.EOT) + { + // this was the last sector to read + // or termination requested + + int keyIndex = 0; + for (int i = 0; i < track.Sectors.Length; i++) + { + if (track.Sectors[i].SectorID == track.Sectors[ActiveDrive.SectorIndex].SectorID) + { + keyIndex = i; + break; + } + } + + if (keyIndex == track.Sectors.Length - 1) + { + // last sector on the cylinder, set EN + SetBit(SR1_EN, ref Status1); + + // increment cylinder + ActiveCommandParams.Cylinder++; + + // reset sector + ActiveCommandParams.Sector = 1; + ActiveDrive.SectorIndex = 0; + } + else + { + ActiveDrive.SectorIndex++; + } + + UnSetBit(SR0_IC1, ref Status0); + UnSetBit(SR0_IC0, ref Status0); + + CommitResultCHRN(); + CommitResultStatus(); + ActivePhase = Phase.Execution; + } + + if (ActivePhase == Phase.Execution) + { + ExecLength = buffPos; + ExecCounter = buffPos; + + DriveLight = true; + } + } + + break; + + //---------------------------------------- + // FDC in execution phase reading/writing bytes + //---------------------------------------- + case Phase.Execution: + + var index = ExecLength - ExecCounter; + + LastSectorDataReadByte = ExecBuffer[index]; + + OverrunCounter--; + ExecCounter--; + + break; + + //---------------------------------------- + // Result bytes being sent to CPU + //---------------------------------------- + case Phase.Result: + break; + } + } + + /// + /// Read ID + /// COMMAND: 1 parameter byte + /// EXECUTION: The first correct ID information on the cylinder is stored in the data register + /// RESULT: 7 result bytes + /// + private void UPD_ReadID() + { + switch (ActivePhase) + { + //---------------------------------------- + // FDC is waiting for a command byte + //---------------------------------------- + case Phase.Idle: + break; + + //---------------------------------------- + // Receiving command parameter bytes + //---------------------------------------- + case Phase.Command: + + // store the parameter in the command buffer + CommBuffer[CommCounter] = LastByteReceived; + + // process parameter byte + ParseParamByteStandard(CommCounter); + + // increment command parameter counter + CommCounter++; + + // was that the last parameter byte? + if (CommCounter == ActiveCommand.ParameterByteCount) + { + DriveLight = true; + + // all parameter bytes received + ClearResultBuffer(); + Status0 = 0; + Status1 = 0; + Status2 = 0; + Status3 = 0; + + // set unit select + //SetUnitSelect(ActiveDrive.ID, ref Status0); + + // HD should always be 0 + UnSetBit(SR0_HD, ref Status0); + + if (!ActiveDrive.FLAG_READY) + { + // no disk, no tracks or motor is not on + // it is at this point the +3 detects whether a disk is present + // if not (and after another readid and SIS) it will eventually proceed to loading from tape + SetBit(SR0_IC0, ref Status0); + SetBit(SR0_NR, ref Status0); + + // setup the result buffer + ResBuffer[RS_ST0] = Status0; + for (int i = 1; i < 7; i++) + ResBuffer[i] = 0; + + // move to result phase + ActivePhase = Phase.Result; + break; + } + + var track = ActiveDrive.Disk.DiskTracks.Where(a => a.TrackNumber == ActiveDrive.CurrentTrackID).FirstOrDefault(); + + if (track != null && track.NumberOfSectors > 0 && track.TrackNumber != 0xff) + { + // formatted track + + // is the index out of bounds? + if (ActiveDrive.SectorIndex >= track.NumberOfSectors) + { + // reset the index + ActiveDrive.SectorIndex = 0; + } + + if (ActiveDrive.SectorIndex == 0 && ActiveDrive.Disk.DiskTracks[ActiveDrive.CurrentTrackID].Sectors.Length > 1) + { + // looks like readid always skips the first sector on a track + ActiveDrive.SectorIndex++; + } + + // read the sector data + var data = track.Sectors[ActiveDrive.SectorIndex]; //.GetCHRN(); + ResBuffer[RS_C] = data.TrackNumber; + ResBuffer[RS_H] = data.SideNumber; + ResBuffer[RS_R] = data.SectorID; + ResBuffer[RS_N] = data.SectorSize; + + ResBuffer[RS_ST0] = Status0; + + // check for DAM & CRC + //if (data.Status2.Bit(SR2_CM)) + //SetBit(SR2_CM, ref ResBuffer[RS_ST2]); + + + // increment the current sector + ActiveDrive.SectorIndex++; + + // is the index out of bounds? + if (ActiveDrive.SectorIndex >= track.NumberOfSectors) + { + // reset the index + ActiveDrive.SectorIndex = 0; + } + } + else + { + // unformatted track? + CommitResultCHRN(); + + SetBit(SR0_IC0, ref Status0); + ResBuffer[RS_ST0] = Status0; + ResBuffer[RS_ST1] = 0x01; + } + + ActivePhase = Phase.Result; + } + + break; + + //---------------------------------------- + // FDC in execution phase reading/writing bytes + //---------------------------------------- + case Phase.Execution: + break; + + //---------------------------------------- + // Result bytes being sent to CPU + //---------------------------------------- + case Phase.Result: + break; + } + } + + #endregion + + #region WRITE Commands + + /// + /// Write Data + /// COMMAND: 8 parameter bytes + /// EXECUTION: Data transfer between FDC and FDD + /// RESULT: 7 result bytes + /// + private void UPD_WriteData() + { + switch (ActivePhase) + { + //---------------------------------------- + // FDC is waiting for a command byte + //---------------------------------------- + case Phase.Idle: + break; + + //---------------------------------------- + // Receiving command parameter bytes + //---------------------------------------- + case Phase.Command: + + // store the parameter in the command buffer + CommBuffer[CommCounter] = LastByteReceived; + + // process parameter byte + ParseParamByteStandard(CommCounter); + + // increment command parameter counter + CommCounter++; + + // was that the last parameter byte? + if (CommCounter == ActiveCommand.ParameterByteCount) + { + // all parameter bytes received - setup for execution phase + + // clear exec buffer and status registers + ClearExecBuffer(); + Status0 = 0; + Status1 = 0; + Status2 = 0; + Status3 = 0; + + // temp sector index + byte secIdx = ActiveCommandParams.Sector; + + // hack for when another drive (non-existent) is being called + if (ActiveDrive.ID != 0) + DiskDriveIndex = 0; + + // do we have a valid disk inserted? + if (!ActiveDrive.FLAG_READY) + { + // no disk, no tracks or motor is not on + SetBit(SR0_IC0, ref Status0); + SetBit(SR0_NR, ref Status0); + + CommitResultCHRN(); + CommitResultStatus(); + //ResBuffer[RS_ST0] = Status0; + + // move to result phase + ActivePhase = Phase.Result; + break; + } + + // check write protect tab + if (ActiveDrive.FLAG_WRITEPROTECT) + { + SetBit(SR0_IC0, ref Status0); + SetBit(SR1_NW, ref Status1); + + CommitResultCHRN(); + CommitResultStatus(); + //ResBuffer[RS_ST0] = Status0; + + // move to result phase + ActivePhase = Phase.Result; + break; + } + else + { + + // calculate the number of bytes to write + int byteCounter = 0; + byte startSecID = ActiveCommandParams.Sector; + byte endSecID = ActiveCommandParams.EOT; + bool lastSec = false; + + // get the first sector + var track = ActiveDrive.Disk.DiskTracks[ActiveCommandParams.Cylinder]; + //int secIndex = 0; + for (int s = 0; s < track.Sectors.Length; s++) + { + if (track.Sectors[s].SectorID == endSecID) + lastSec = true; + + for (int i = 0; i < 0x80 << ActiveCommandParams.SectorSize; i++) + { + byteCounter++; + + if (i == (0x80 << ActiveCommandParams.SectorSize) - 1 && lastSec) + { + break; + } + } + + if (lastSec) + break; + } + + ExecCounter = byteCounter; + ExecLength = byteCounter; + ActivePhase = Phase.Execution; + DriveLight = true; + break; + } + } + + break; + + //---------------------------------------- + // FDC in execution phase reading/writing bytes + //---------------------------------------- + case Phase.Execution: + + var index = ExecLength - ExecCounter; + + ExecBuffer[index] = LastSectorDataWriteByte; + + OverrunCounter--; + ExecCounter--; + + if (ExecCounter <= 0) + { + int cnt = 0; + + // all data received + byte startSecID = ActiveCommandParams.Sector; + byte endSecID = ActiveCommandParams.EOT; + bool lastSec = false; + var track = ActiveDrive.Disk.DiskTracks[ActiveCommandParams.Cylinder]; + //int secIndex = 0; + + for (int s = 0; s < track.Sectors.Length; s++) + { + if (cnt == ExecLength) + break; + + ActiveCommandParams.Sector = track.Sectors[s].SectorID; + + if (track.Sectors[s].SectorID == endSecID) + lastSec = true; + + int size = 0x80 << track.Sectors[s].SectorSize; + + for (int d = 0; d < size; d++) + { + track.Sectors[s].SectorData[d] = ExecBuffer[cnt++]; + } + + if (lastSec) + break; + } + + SetBit(SR0_IC0, ref Status0); + SetBit(SR1_EN, ref Status1); + + CommitResultCHRN(); + CommitResultStatus(); + } + + break; + + //---------------------------------------- + // Result bytes being sent to CPU + //---------------------------------------- + case Phase.Result: + break; + } + } + + /// + /// Write ID (format write) + /// COMMAND: 5 parameter bytes + /// EXECUTION: Entire track is formatted + /// RESULT: 7 result bytes + /// + private void UPD_WriteID() + { + switch (ActivePhase) + { + //---------------------------------------- + // FDC is waiting for a command byte + //---------------------------------------- + case Phase.Idle: + break; + + //---------------------------------------- + // Receiving command parameter bytes + //---------------------------------------- + case Phase.Command: + + // store the parameter in the command buffer + CommBuffer[CommCounter] = LastByteReceived; + + // process parameter byte + ParseParamByteStandard(CommCounter); + + // increment command parameter counter + CommCounter++; + + // was that the last parameter byte? + if (CommCounter == ActiveCommand.ParameterByteCount) + { + // all parameter bytes received - setup for execution phase + DriveLight = true; + + // clear exec buffer and status registers + ClearExecBuffer(); + Status0 = 0; + Status1 = 0; + Status2 = 0; + Status3 = 0; + + // temp sector index + byte secIdx = ActiveCommandParams.Sector; + + // hack for when another drive (non-existent) is being called + if (ActiveDrive.ID != 0) + DiskDriveIndex = 0; + + // do we have a valid disk inserted? + if (!ActiveDrive.FLAG_READY) + { + // no disk, no tracks or motor is not on + SetBit(SR0_IC0, ref Status0); + SetBit(SR0_NR, ref Status0); + + CommitResultCHRN(); + CommitResultStatus(); + //ResBuffer[RS_ST0] = Status0; + + // move to result phase + ActivePhase = Phase.Result; + break; + } + + // check write protect tab + if (ActiveDrive.FLAG_WRITEPROTECT) + { + SetBit(SR0_IC0, ref Status0); + SetBit(SR1_NW, ref Status1); + + CommitResultCHRN(); + CommitResultStatus(); + //ResBuffer[RS_ST0] = Status0; + + // move to result phase + ActivePhase = Phase.Result; + break; + } + else + { + // not implemented yet + SetBit(SR0_IC0, ref Status0); + SetBit(SR1_NW, ref Status1); + + CommitResultCHRN(); + CommitResultStatus(); + //ResBuffer[RS_ST0] = Status0; + + // move to result phase + ActivePhase = Phase.Result; + break; + } + } + + break; + + //---------------------------------------- + // FDC in execution phase reading/writing bytes + //---------------------------------------- + case Phase.Execution: + break; + + //---------------------------------------- + // Result bytes being sent to CPU + //---------------------------------------- + case Phase.Result: + break; + } + } + + /// + /// Write Deleted Data + /// COMMAND: 8 parameter bytes + /// EXECUTION: Data transfer between FDC and FDD + /// RESULT: 7 result bytes + /// + private void UPD_WriteDeletedData() + { + switch (ActivePhase) + { + //---------------------------------------- + // FDC is waiting for a command byte + //---------------------------------------- + case Phase.Idle: + break; + + //---------------------------------------- + // Receiving command parameter bytes + //---------------------------------------- + case Phase.Command: + + // store the parameter in the command buffer + CommBuffer[CommCounter] = LastByteReceived; + + // process parameter byte + ParseParamByteStandard(CommCounter); + + // increment command parameter counter + CommCounter++; + + // was that the last parameter byte? + if (CommCounter == ActiveCommand.ParameterByteCount) + { + // all parameter bytes received - setup for execution phase + + // clear exec buffer and status registers + ClearExecBuffer(); + Status0 = 0; + Status1 = 0; + Status2 = 0; + Status3 = 0; + + // temp sector index + byte secIdx = ActiveCommandParams.Sector; + + // hack for when another drive (non-existent) is being called + if (ActiveDrive.ID != 0) + DiskDriveIndex = 0; + + // do we have a valid disk inserted? + if (!ActiveDrive.FLAG_READY) + { + // no disk, no tracks or motor is not on + SetBit(SR0_IC0, ref Status0); + SetBit(SR0_NR, ref Status0); + + CommitResultCHRN(); + CommitResultStatus(); + //ResBuffer[RS_ST0] = Status0; + + // move to result phase + ActivePhase = Phase.Result; + break; + } + + // check write protect tab + if (ActiveDrive.FLAG_WRITEPROTECT) + { + SetBit(SR0_IC0, ref Status0); + SetBit(SR1_NW, ref Status1); + + CommitResultCHRN(); + CommitResultStatus(); + //ResBuffer[RS_ST0] = Status0; + + // move to result phase + ActivePhase = Phase.Result; + break; + } + else + { + + // calculate the number of bytes to write + int byteCounter = 0; + byte startSecID = ActiveCommandParams.Sector; + byte endSecID = ActiveCommandParams.EOT; + bool lastSec = false; + + // get the first sector + var track = ActiveDrive.Disk.DiskTracks[ActiveCommandParams.Cylinder]; + //int secIndex = 0; + for (int s = 0; s < track.Sectors.Length; s++) + { + if (track.Sectors[s].SectorID == endSecID) + lastSec = true; + + for (int i = 0; i < 0x80 << ActiveCommandParams.SectorSize; i++) + { + byteCounter++; + + if (i == (0x80 << ActiveCommandParams.SectorSize) - 1 && lastSec) + { + break; + } + } + + if (lastSec) + break; + } + + ExecCounter = byteCounter; + ExecLength = byteCounter; + ActivePhase = Phase.Execution; + DriveLight = true; + break; + } + } + + break; + + //---------------------------------------- + // FDC in execution phase reading/writing bytes + //---------------------------------------- + case Phase.Execution: + + var index = ExecLength - ExecCounter; + + ExecBuffer[index] = LastSectorDataWriteByte; + + OverrunCounter--; + ExecCounter--; + + if (ExecCounter <= 0) + { + int cnt = 0; + + // all data received + byte startSecID = ActiveCommandParams.Sector; + byte endSecID = ActiveCommandParams.EOT; + bool lastSec = false; + var track = ActiveDrive.Disk.DiskTracks[ActiveCommandParams.Cylinder]; + //int secIndex = 0; + + for (int s = 0; s < track.Sectors.Length; s++) + { + if (cnt == ExecLength) + break; + + ActiveCommandParams.Sector = track.Sectors[s].SectorID; + + if (track.Sectors[s].SectorID == endSecID) + lastSec = true; + + int size = 0x80 << track.Sectors[s].SectorSize; + + for (int d = 0; d < size; d++) + { + track.Sectors[s].SectorData[d] = ExecBuffer[cnt++]; + } + + if (lastSec) + break; + } + + SetBit(SR0_IC0, ref Status0); + SetBit(SR1_EN, ref Status1); + + CommitResultCHRN(); + CommitResultStatus(); + } + + break; + + //---------------------------------------- + // Result bytes being sent to CPU + //---------------------------------------- + case Phase.Result: + break; + } + } + + #endregion + + #region SCAN Commands + + /// + /// Scan Equal + /// COMMAND: 8 parameter bytes + /// EXECUTION: Data compared between the FDD and FDC + /// RESULT: 7 result bytes + /// + private void UPD_ScanEqual() + { + switch (ActivePhase) + { + //---------------------------------------- + // FDC is waiting for a command byte + //---------------------------------------- + case Phase.Idle: + break; + + //---------------------------------------- + // Receiving command parameter bytes + //---------------------------------------- + case Phase.Command: + break; + + //---------------------------------------- + // FDC in execution phase reading/writing bytes + //---------------------------------------- + case Phase.Execution: + break; + + //---------------------------------------- + // Result bytes being sent to CPU + //---------------------------------------- + case Phase.Result: + break; + } + } + + /// + /// Scan Low or Equal + /// COMMAND: 8 parameter bytes + /// EXECUTION: Data compared between the FDD and FDC + /// RESULT: 7 result bytes + /// + private void UPD_ScanLowOrEqual() + { + switch (ActivePhase) + { + //---------------------------------------- + // FDC is waiting for a command byte + //---------------------------------------- + case Phase.Idle: + break; + + //---------------------------------------- + // Receiving command parameter bytes + //---------------------------------------- + case Phase.Command: + break; + + //---------------------------------------- + // FDC in execution phase reading/writing bytes + //---------------------------------------- + case Phase.Execution: + break; + + //---------------------------------------- + // Result bytes being sent to CPU + //---------------------------------------- + case Phase.Result: + break; + } + } + + /// + /// Scan High or Equal + /// COMMAND: 8 parameter bytes + /// EXECUTION: Data compared between the FDD and FDC + /// RESULT: 7 result bytes + /// + private void UPD_ScanHighOrEqual() + { + switch (ActivePhase) + { + //---------------------------------------- + // FDC is waiting for a command byte + //---------------------------------------- + case Phase.Idle: + break; + + //---------------------------------------- + // Receiving command parameter bytes + //---------------------------------------- + case Phase.Command: + break; + + //---------------------------------------- + // FDC in execution phase reading/writing bytes + //---------------------------------------- + case Phase.Execution: + break; + + //---------------------------------------- + // Result bytes being sent to CPU + //---------------------------------------- + case Phase.Result: + break; + } + } + + #endregion + + #region OTHER Commands + + /// + /// Specify + /// COMMAND: 2 parameter bytes + /// EXECUTION: NO execution phase + /// RESULT: NO result phase + /// + /// Looks like specify command returns status 0x80 throughout its lifecycle + /// so CB is NOT set + /// + private void UPD_Specify() + { + switch (ActivePhase) + { + //---------------------------------------- + // FDC is waiting for a command byte + //---------------------------------------- + case Phase.Idle: + break; + + //---------------------------------------- + // Receiving command parameter bytes + //---------------------------------------- + case Phase.Command: + + // store the parameter in the command buffer + CommBuffer[CommCounter] = LastByteReceived; + + // process parameter byte + byte currByte = CommBuffer[CommCounter]; + BitArray bi = new BitArray(new byte[] { currByte }); + + switch (CommCounter) + { + // SRT & HUT + case 0: + SRT = 16 - (currByte >> 4) & 0x0f; + HUT = (currByte & 0x0f) << 4; + if (HUT == 0) + { + HUT = 255; + } + break; + // HLT & ND + case 1: + if (bi[0]) + ND = true; + else + ND = false; + + HLT = currByte & 0xfe; + if (HLT == 0) + { + HLT = 255; + } + break; + } + + // increment command parameter counter + CommCounter++; + + // was that the last parameter byte? + if (CommCounter == ActiveCommand.ParameterByteCount) + { + // all parameter bytes received + ActivePhase = Phase.Idle; + } + + break; + + //---------------------------------------- + // FDC in execution phase reading/writing bytes + //---------------------------------------- + case Phase.Execution: + break; + + //---------------------------------------- + // Result bytes being sent to CPU + //---------------------------------------- + case Phase.Result: + break; + } + } + + /// + /// Seek + /// COMMAND: 2 parameter bytes + /// EXECUTION: Head is positioned over proper cylinder on disk + /// RESULT: NO result phase + /// + private void UPD_Seek() + { + switch (ActivePhase) + { + //---------------------------------------- + // FDC is waiting for a command byte + //---------------------------------------- + case Phase.Idle: + break; + + //---------------------------------------- + // Receiving command parameter bytes + //---------------------------------------- + case Phase.Command: + // store the parameter in the command buffer + CommBuffer[CommCounter] = LastByteReceived; + + // process parameter byte + byte currByte = CommBuffer[CommCounter]; + switch (CommCounter) + { + case 0: + ParseParamByteStandard(CommCounter); + break; + case 1: + ActiveDrive.SeekingTrack = currByte; + break; + } + + // increment command parameter counter + CommCounter++; + + // was that the last parameter byte? + if (CommCounter == ActiveCommand.ParameterByteCount) + { + // all parameter bytes received + DriveLight = true; + ActivePhase = Phase.Execution; + ActiveCommand.CommandDelegate(); + } + break; + + //---------------------------------------- + // FDC in execution phase reading/writing bytes + //---------------------------------------- + case Phase.Execution: + // set seek flag + ActiveDrive.SeekStatus = SEEK_SEEK; + + if (ActiveDrive.CurrentTrackID == CommBuffer[CM_C]) + { + // we are already on the correct track + ActiveDrive.SectorIndex = 0; + } + else + { + // immediate seek + ActiveDrive.CurrentTrackID = CommBuffer[CM_C]; + + ActiveDrive.SectorIndex = 0; + + if (ActiveDrive.Disk.DiskTracks[ActiveDrive.CurrentTrackID].Sectors.Length > 1) + { + // always read the first sector + //ActiveDrive.SectorIndex++; + } + } + + // skip execution mode and go directly to idle + // result is determined by SIS command + ActivePhase = Phase.Idle; + break; + + //---------------------------------------- + // Result bytes being sent to CPU + //---------------------------------------- + case Phase.Result: + break; + } + } + + /// + /// Recalibrate (seek track 0) + /// COMMAND: 1 parameter byte + /// EXECUTION: Head retracted to track 0 + /// RESULT: NO result phase + /// + private void UPD_Recalibrate() + { + switch (ActivePhase) + { + //---------------------------------------- + // FDC is waiting for a command byte + //---------------------------------------- + case Phase.Idle: + break; + + //---------------------------------------- + // Receiving command parameter bytes + //---------------------------------------- + case Phase.Command: + // store the parameter in the command buffer + CommBuffer[CommCounter] = LastByteReceived; + + // process parameter byte + ParseParamByteStandard(CommCounter); + + // increment command parameter counter + CommCounter++; + + // was that the last parameter byte? + if (CommCounter == ActiveCommand.ParameterByteCount) + { + // all parameter bytes received + DriveLight = true; + ActivePhase = Phase.Execution; + ActiveCommand.CommandDelegate(); + } + break; + + //---------------------------------------- + // FDC in execution phase reading/writing bytes + //---------------------------------------- + case Phase.Execution: + + // immediate recalibration + ActiveDrive.TrackIndex = 0; + ActiveDrive.SectorIndex = 0; + + // recalibrate appears to always skip the first sector + //if (ActiveDrive.Disk.DiskTracks[ActiveDrive.TrackIndex].Sectors.Length > 1) + //ActiveDrive.SectorIndex++; + + // set seek flag + ActiveDrive.SeekStatus = SEEK_RECALIBRATE; + + // skip execution mode and go directly to idle + // result is determined by SIS command + ActivePhase = Phase.Idle; + break; + + //---------------------------------------- + // Result bytes being sent to CPU + //---------------------------------------- + case Phase.Result: + break; + } + } + + /// + /// Sense Interrupt Status + /// COMMAND: NO parameter bytes + /// EXECUTION: NO execution phase + /// RESULT: 2 result bytes + /// + private void UPD_SenseInterruptStatus() + { + switch (ActivePhase) + { + //---------------------------------------- + // FDC is waiting for a command byte + //---------------------------------------- + case Phase.Idle: + break; + + //---------------------------------------- + // Receiving command parameter bytes + //---------------------------------------- + case Phase.Command: + break; + + //---------------------------------------- + // FDC in execution phase reading/writing bytes + //---------------------------------------- + case Phase.Execution: + // SIS should return 2 bytes if sucessfully sensed an interrupt + // 1 byte otherwise + + // it seems like the +3 ROM makes 3 SIS calls for each seek/recalibrate call for some reason + // possibly one for each drive??? + // 1 - the interrupt is acknowleged with ST0 = 32 and track number + // 2 - second sis returns 1 ST0 byte with 192 + // 3 - third SIS call returns standard 1 byte 0x80 (unknown cmd or SIS with no interrupt occured) + // for now I will assume that the first call is aimed at DriveA, the second at DriveB (which we are NOT implementing) + + // check active drive first + if (ActiveDrive.SeekStatus == SEEK_RECALIBRATE || + ActiveDrive.SeekStatus == SEEK_SEEK) + { + // interrupt has been raised for this drive + // acknowledge + ActiveDrive.SeekStatus = SEEK_IDLE;// SEEK_INTACKNOWLEDGED; + + // result length 2 + ResLength = 2; + + // first byte ST0 0x20 + Status0 = 0x20; + ResBuffer[0] = Status0; + // second byte is the current track id + ResBuffer[1] = ActiveDrive.CurrentTrackID; + } + /* else if (ActiveDrive.SeekStatus == SEEK_INTACKNOWLEDGED) { // DriveA interrupt has already been acknowledged @@ -2177,401 +2177,401 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC ResBuffer[0] = Status0; } */ - else if (ActiveDrive.SeekStatus == SEEK_IDLE) - { - // SIS with no interrupt - ResLength = 1; - Status0 = 0x80; - ResBuffer[0] = Status0; - } + else if (ActiveDrive.SeekStatus == SEEK_IDLE) + { + // SIS with no interrupt + ResLength = 1; + Status0 = 0x80; + ResBuffer[0] = Status0; + } - ActivePhase = Phase.Result; + ActivePhase = Phase.Result; - break; + break; - //---------------------------------------- - // Result bytes being sent to CPU - //---------------------------------------- - case Phase.Result: - break; - } - } + //---------------------------------------- + // Result bytes being sent to CPU + //---------------------------------------- + case Phase.Result: + break; + } + } - /// - /// Sense Drive Status - /// COMMAND: 1 parameter byte - /// EXECUTION: NO execution phase - /// RESULT: 1 result byte - /// - /// The ZX spectrum appears to only specify drive 1 as the parameter byte, NOT drive 0 - /// After the final param byte is received main status changes to 0xd0 - /// Data register (ST3) result is 0x51 if drive/disk not available - /// 0x71 if disk is present in 2nd drive - /// - private void UPD_SenseDriveStatus() - { - switch (ActivePhase) - { - //---------------------------------------- - // FDC is waiting for a command byte - //---------------------------------------- - case Phase.Idle: - break; + /// + /// Sense Drive Status + /// COMMAND: 1 parameter byte + /// EXECUTION: NO execution phase + /// RESULT: 1 result byte + /// + /// The ZX spectrum appears to only specify drive 1 as the parameter byte, NOT drive 0 + /// After the final param byte is received main status changes to 0xd0 + /// Data register (ST3) result is 0x51 if drive/disk not available + /// 0x71 if disk is present in 2nd drive + /// + private void UPD_SenseDriveStatus() + { + switch (ActivePhase) + { + //---------------------------------------- + // FDC is waiting for a command byte + //---------------------------------------- + case Phase.Idle: + break; - //---------------------------------------- - // Receiving command parameter bytes - //---------------------------------------- - case Phase.Command: - // store the parameter in the command buffer - CommBuffer[CommCounter] = LastByteReceived; + //---------------------------------------- + // Receiving command parameter bytes + //---------------------------------------- + case Phase.Command: + // store the parameter in the command buffer + CommBuffer[CommCounter] = LastByteReceived; - // process parameter byte - ParseParamByteStandard(CommCounter); + // process parameter byte + ParseParamByteStandard(CommCounter); - // increment command parameter counter - CommCounter++; + // increment command parameter counter + CommCounter++; - // was that the last parameter byte? - if (CommCounter == ActiveCommand.ParameterByteCount) - { - // all parameter bytes received - ActivePhase = Phase.Execution; - UPD_SenseDriveStatus(); - } - break; + // was that the last parameter byte? + if (CommCounter == ActiveCommand.ParameterByteCount) + { + // all parameter bytes received + ActivePhase = Phase.Execution; + UPD_SenseDriveStatus(); + } + break; - //---------------------------------------- - // FDC in execution phase reading/writing bytes - //---------------------------------------- - case Phase.Execution: - // one ST3 byte required + //---------------------------------------- + // FDC in execution phase reading/writing bytes + //---------------------------------------- + case Phase.Execution: + // one ST3 byte required - // set US - Status3 = (byte)ActiveDrive.ID; + // set US + Status3 = (byte)ActiveDrive.ID; - if (Status3 != 0) - { - // we only support 1 drive - SetBit(SR3_FT, ref Status3); - } - else - { - // HD - only one side - UnSetBit(SR3_HD, ref Status3); + if (Status3 != 0) + { + // we only support 1 drive + SetBit(SR3_FT, ref Status3); + } + else + { + // HD - only one side + UnSetBit(SR3_HD, ref Status3); - // write protect - if (ActiveDrive.FLAG_WRITEPROTECT) - SetBit(SR3_WP, ref Status3); + // write protect + if (ActiveDrive.FLAG_WRITEPROTECT) + SetBit(SR3_WP, ref Status3); - // track 0 - if (ActiveDrive.FLAG_TRACK0) - SetBit(SR3_T0, ref Status3); + // track 0 + if (ActiveDrive.FLAG_TRACK0) + SetBit(SR3_T0, ref Status3); - // rdy - if (ActiveDrive.Disk != null) - SetBit(SR3_RY, ref Status3); - } + // rdy + if (ActiveDrive.Disk != null) + SetBit(SR3_RY, ref Status3); + } - ResBuffer[0] = Status3; - ActivePhase = Phase.Result; - - break; + ResBuffer[0] = Status3; + ActivePhase = Phase.Result; - //---------------------------------------- - // Result bytes being sent to CPU - //---------------------------------------- - case Phase.Result: - break; - } - } + break; - /// - /// Version - /// COMMAND: NO parameter bytes - /// EXECUTION: NO execution phase - /// RESULT: 1 result byte - /// - private void UPD_Version() - { - switch (ActivePhase) - { - case Phase.Idle: - case Phase.Command: - case Phase.Execution: - case Phase.Result: - UPD_Invalid(); - break; - } - } + //---------------------------------------- + // Result bytes being sent to CPU + //---------------------------------------- + case Phase.Result: + break; + } + } - /// - /// Invalid - /// COMMAND: NO parameter bytes - /// EXECUTION: NO execution phase - /// RESULT: 1 result byte - /// - private void UPD_Invalid() - { - switch (ActivePhase) - { - //---------------------------------------- - // FDC is waiting for a command byte - //---------------------------------------- - case Phase.Idle: - break; + /// + /// Version + /// COMMAND: NO parameter bytes + /// EXECUTION: NO execution phase + /// RESULT: 1 result byte + /// + private void UPD_Version() + { + switch (ActivePhase) + { + case Phase.Idle: + case Phase.Command: + case Phase.Execution: + case Phase.Result: + UPD_Invalid(); + break; + } + } - //---------------------------------------- - // Receiving command parameter bytes - //---------------------------------------- - case Phase.Command: - break; + /// + /// Invalid + /// COMMAND: NO parameter bytes + /// EXECUTION: NO execution phase + /// RESULT: 1 result byte + /// + private void UPD_Invalid() + { + switch (ActivePhase) + { + //---------------------------------------- + // FDC is waiting for a command byte + //---------------------------------------- + case Phase.Idle: + break; - //---------------------------------------- - // FDC in execution phase reading/writing bytes - //---------------------------------------- - case Phase.Execution: - // no execution phase - ActivePhase = Phase.Result; - UPD_Invalid(); - break; + //---------------------------------------- + // Receiving command parameter bytes + //---------------------------------------- + case Phase.Command: + break; - //---------------------------------------- - // Result bytes being sent to CPU - //---------------------------------------- - case Phase.Result: - ResBuffer[0] = 0x80; - break; - } - } + //---------------------------------------- + // FDC in execution phase reading/writing bytes + //---------------------------------------- + case Phase.Execution: + // no execution phase + ActivePhase = Phase.Result; + UPD_Invalid(); + break; - #endregion + //---------------------------------------- + // Result bytes being sent to CPU + //---------------------------------------- + case Phase.Result: + ResBuffer[0] = 0x80; + break; + } + } - #endregion + #endregion - #region Controller Methods + #endregion - /// - /// Called when a status register read is required - /// This can be called at any time - /// The main status register appears to be queried nearly all the time - /// so needs to be kept updated. It keeps the CPU informed of the current state - /// - private byte ReadMainStatus() - { - SetBit(MSR_RQM, ref StatusMain); + #region Controller Methods - switch (ActivePhase) - { - case Phase.Idle: - UnSetBit(MSR_DIO, ref StatusMain); - UnSetBit(MSR_CB, ref StatusMain); - UnSetBit(MSR_EXM, ref StatusMain); - break; - case Phase.Command: - UnSetBit(MSR_DIO, ref StatusMain); - SetBit(MSR_CB, ref StatusMain); - UnSetBit(MSR_EXM, ref StatusMain); - break; - case Phase.Execution: - if (ActiveCommand.Direction == CommandDirection.OUT) - SetBit(MSR_DIO, ref StatusMain); - else - UnSetBit(MSR_DIO, ref StatusMain); + /// + /// Called when a status register read is required + /// This can be called at any time + /// The main status register appears to be queried nearly all the time + /// so needs to be kept updated. It keeps the CPU informed of the current state + /// + private byte ReadMainStatus() + { + SetBit(MSR_RQM, ref StatusMain); - SetBit(MSR_EXM, ref StatusMain); - SetBit(MSR_CB, ref StatusMain); + switch (ActivePhase) + { + case Phase.Idle: + UnSetBit(MSR_DIO, ref StatusMain); + UnSetBit(MSR_CB, ref StatusMain); + UnSetBit(MSR_EXM, ref StatusMain); + break; + case Phase.Command: + UnSetBit(MSR_DIO, ref StatusMain); + SetBit(MSR_CB, ref StatusMain); + UnSetBit(MSR_EXM, ref StatusMain); + break; + case Phase.Execution: + if (ActiveCommand.Direction == CommandDirection.OUT) + SetBit(MSR_DIO, ref StatusMain); + else + UnSetBit(MSR_DIO, ref StatusMain); - // overrun detection - OverrunCounter++; - if (OverrunCounter >= 64) - { - // CPU has read the status register 64 times without reading the data register - // switch the current command into result phase - ActivePhase = Phase.Result; + SetBit(MSR_EXM, ref StatusMain); + SetBit(MSR_CB, ref StatusMain); - // reset the overun counter - OverrunCounter = 0; - } + // overrun detection + OverrunCounter++; + if (OverrunCounter >= 64) + { + // CPU has read the status register 64 times without reading the data register + // switch the current command into result phase + ActivePhase = Phase.Result; - break; - case Phase.Result: - SetBit(MSR_DIO, ref StatusMain); - SetBit(MSR_CB, ref StatusMain); - UnSetBit(MSR_EXM, ref StatusMain); - break; - } + // reset the overun counter + OverrunCounter = 0; + } - return StatusMain; - } + break; + case Phase.Result: + SetBit(MSR_DIO, ref StatusMain); + SetBit(MSR_CB, ref StatusMain); + UnSetBit(MSR_EXM, ref StatusMain); + break; + } - /// - /// Handles CPU reading from the data register - /// - private byte ReadDataRegister() - { - // default return value - byte res = 0xff; + return StatusMain; + } - // check RQM flag status - if (!GetBit(MSR_RQM, StatusMain)) - { - // FDC is not ready to return data - return res; - } + /// + /// Handles CPU reading from the data register + /// + private byte ReadDataRegister() + { + // default return value + byte res = 0xff; - // check active direction - if (!GetBit(MSR_DIO, StatusMain)) - { - // FDC is expecting to receive, not send data - return res; - } + // check RQM flag status + if (!GetBit(MSR_RQM, StatusMain)) + { + // FDC is not ready to return data + return res; + } - switch (ActivePhase) - { - case Phase.Execution: - // reset overrun counter - OverrunCounter = 0; + // check active direction + if (!GetBit(MSR_DIO, StatusMain)) + { + // FDC is expecting to receive, not send data + return res; + } - // execute read - ActiveCommand.CommandDelegate(); + switch (ActivePhase) + { + case Phase.Execution: + // reset overrun counter + OverrunCounter = 0; - res = LastSectorDataReadByte; + // execute read + ActiveCommand.CommandDelegate(); - if (ExecCounter <= 0) - { - // end of execution phase - ActivePhase = Phase.Result; - } + res = LastSectorDataReadByte; - return res; - - case Phase.Result: + if (ExecCounter <= 0) + { + // end of execution phase + ActivePhase = Phase.Result; + } - DriveLight = false; + return res; - ActiveCommand.CommandDelegate(); + case Phase.Result: - // result byte reading - res = ResBuffer[ResCounter]; + DriveLight = false; - // increment result counter - ResCounter++; + ActiveCommand.CommandDelegate(); - if (ResCounter >= ResLength) - { - ActivePhase = Phase.Idle; - } + // result byte reading + res = ResBuffer[ResCounter]; - break; - } + // increment result counter + ResCounter++; - return res; - } + if (ResCounter >= ResLength) + { + ActivePhase = Phase.Idle; + } - /// - /// Handles CPU writing to the data register - /// - private void WriteDataRegister(byte data) - { - if (!GetBit(MSR_RQM, StatusMain) || GetBit(MSR_DIO, StatusMain)) - { - // FDC will not receive and process any bytes - return; - } + break; + } - // store the incoming byte - LastByteReceived = data; + return res; + } - // process incoming bytes - switch (ActivePhase) - { - //// controller is idle awaiting the first command byte of a new instruction - case Phase.Idle: - ParseCommandByte(data); - break; - //// we are in command phase - case Phase.Command: - // attempt to process this parameter byte - //ProcessCommand(data); - ActiveCommand.CommandDelegate(); - break; - //// we are in execution phase - case Phase.Execution: - // CPU is going to be sending data bytes to the FDC to be written to disk - - // store the byte - LastSectorDataWriteByte = data; - ActiveCommand.CommandDelegate(); + /// + /// Handles CPU writing to the data register + /// + private void WriteDataRegister(byte data) + { + if (!GetBit(MSR_RQM, StatusMain) || GetBit(MSR_DIO, StatusMain)) + { + // FDC will not receive and process any bytes + return; + } - if (ExecCounter <= 0) - { - // end of execution phase - ActivePhase = Phase.Result; - } + // store the incoming byte + LastByteReceived = data; - break; - //// result phase - case Phase.Result: - // data register will not receive bytes during result phase - break; - } - } + // process incoming bytes + switch (ActivePhase) + { + //// controller is idle awaiting the first command byte of a new instruction + case Phase.Idle: + ParseCommandByte(data); + break; + //// we are in command phase + case Phase.Command: + // attempt to process this parameter byte + //ProcessCommand(data); + ActiveCommand.CommandDelegate(); + break; + //// we are in execution phase + case Phase.Execution: + // CPU is going to be sending data bytes to the FDC to be written to disk - /// - /// Processes the first command byte (within a command instruction) - /// Returns TRUE if successful. FALSE if otherwise - /// Called only in idle phase - /// - private bool ParseCommandByte(byte cmdByte) - { - // clear counters - CommCounter = 0; - ResCounter = 0; + // store the byte + LastSectorDataWriteByte = data; + ActiveCommand.CommandDelegate(); - // get the first 4 bytes - byte cByte = (byte)(cmdByte & 0x0f); + if (ExecCounter <= 0) + { + // end of execution phase + ActivePhase = Phase.Result; + } - // get MT, MD and SK states - CMD_FLAG_MT = cmdByte.Bit(7); - CMD_FLAG_MF = cmdByte.Bit(6); - CMD_FLAG_SK = cmdByte.Bit(5); + break; + //// result phase + case Phase.Result: + // data register will not receive bytes during result phase + break; + } + } - cmdByte = cByte; + /// + /// Processes the first command byte (within a command instruction) + /// Returns TRUE if successful. FALSE if otherwise + /// Called only in idle phase + /// + private bool ParseCommandByte(byte cmdByte) + { + // clear counters + CommCounter = 0; + ResCounter = 0; - // lookup the command - var cmd = CommandList.Where(a => a.CommandCode == cmdByte).FirstOrDefault(); + // get the first 4 bytes + byte cByte = (byte)(cmdByte & 0x0f); - if (cmd == null) - { - // no command found - use invalid - CMDIndex = CommandList.Count() - 1; - } - else - { - // valid command found - CMDIndex = CommandList.FindIndex(a => a.CommandCode == cmdByte); + // get MT, MD and SK states + CMD_FLAG_MT = cmdByte.Bit(7); + CMD_FLAG_MF = cmdByte.Bit(6); + CMD_FLAG_SK = cmdByte.Bit(5); - // check validity of command byte flags - // if a flag is set but not valid for this command then it is invalid - bool invalid = false; + cmdByte = cByte; - if (!ActiveCommand.MT) - if (CMD_FLAG_MT) - invalid = true; - if (!ActiveCommand.MF) - if (CMD_FLAG_MF) - invalid = true; - if (!ActiveCommand.SK) - if (CMD_FLAG_SK) - invalid = true; + // lookup the command + var cmd = CommandList.Where(a => a.CommandCode == cmdByte).FirstOrDefault(); - if (invalid) - { - // command byte included spurious bit 5,6 or 7 flags - CMDIndex = CommandList.Count() - 1; - } + if (cmd == null) + { + // no command found - use invalid + CMDIndex = CommandList.Count() - 1; + } + else + { + // valid command found + CMDIndex = CommandList.FindIndex(a => a.CommandCode == cmdByte); - /* + // check validity of command byte flags + // if a flag is set but not valid for this command then it is invalid + bool invalid = false; + + if (!ActiveCommand.MT) + if (CMD_FLAG_MT) + invalid = true; + if (!ActiveCommand.MF) + if (CMD_FLAG_MF) + invalid = true; + if (!ActiveCommand.SK) + if (CMD_FLAG_SK) + invalid = true; + + if (invalid) + { + // command byte included spurious bit 5,6 or 7 flags + CMDIndex = CommandList.Count() - 1; + } + + /* if ((CMD_FLAG_MF && !ActiveCommand.MF) || (CMD_FLAG_MT && !ActiveCommand.MT) || (CMD_FLAG_SK && !ActiveCommand.SK)) @@ -2580,16 +2580,16 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC CMDIndex = CommandList.Count() - 1; } */ - } + } - CommCounter = 0; - ResCounter = 0; + CommCounter = 0; + ResCounter = 0; - // there will now be an active command set - // move to command phase - ActivePhase = Phase.Command; + // there will now be an active command set + // move to command phase + ActivePhase = Phase.Command; - /* + /* // check for invalid SIS if (ActiveInterrupt == InterruptState.None && CMDIndex == CC_SENSE_INTSTATUS) { @@ -2598,237 +2598,237 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC } */ - // set reslength - ResLength = ActiveCommand.ResultByteCount; - - // if there are no expected param bytes to receive - go ahead and run the command - if (ActiveCommand.ParameterByteCount == 0) - { - ActivePhase = Phase.Execution; - ActiveCommand.CommandDelegate(); - } + // set reslength + ResLength = ActiveCommand.ResultByteCount; - return true; - } + // if there are no expected param bytes to receive - go ahead and run the command + if (ActiveCommand.ParameterByteCount == 0) + { + ActivePhase = Phase.Execution; + ActiveCommand.CommandDelegate(); + } - /// - /// Parses the first 5 command argument bytes that are of the standard format - /// - private void ParseParamByteStandard(int index) - { - byte currByte = CommBuffer[index]; - BitArray bi = new BitArray(new byte[] { currByte }); + return true; + } - switch (index) - { - // HD & US - case CM_HEAD: - if (bi[2]) - ActiveCommandParams.Side = 1; - else - ActiveCommandParams.Side = 0; + /// + /// Parses the first 5 command argument bytes that are of the standard format + /// + private void ParseParamByteStandard(int index) + { + byte currByte = CommBuffer[index]; + BitArray bi = new BitArray(new byte[] { currByte }); - ActiveCommandParams.UnitSelect = (byte)(GetUnitSelect(currByte)); - DiskDriveIndex = ActiveCommandParams.UnitSelect; - break; - - // C - case CM_C: - ActiveCommandParams.Cylinder = currByte; - break; + switch (index) + { + // HD & US + case CM_HEAD: + if (bi[2]) + ActiveCommandParams.Side = 1; + else + ActiveCommandParams.Side = 0; - // H - case CM_H: - ActiveCommandParams.Head = currByte; - break; + ActiveCommandParams.UnitSelect = (byte)(GetUnitSelect(currByte)); + DiskDriveIndex = ActiveCommandParams.UnitSelect; + break; - // R - case CM_R: - ActiveCommandParams.Sector = currByte; - break; + // C + case CM_C: + ActiveCommandParams.Cylinder = currByte; + break; - // N - case CM_N: - ActiveCommandParams.SectorSize = currByte; - break; + // H + case CM_H: + ActiveCommandParams.Head = currByte; + break; - // EOT - case CM_EOT: - ActiveCommandParams.EOT = currByte; - break; + // R + case CM_R: + ActiveCommandParams.Sector = currByte; + break; - // GPL - case CM_GPL: - ActiveCommandParams.Gap3Length = currByte; - break; + // N + case CM_N: + ActiveCommandParams.SectorSize = currByte; + break; - // DTL - case CM_DTL: - ActiveCommandParams.DTL = currByte; - break; + // EOT + case CM_EOT: + ActiveCommandParams.EOT = currByte; + break; - default: - break; - } - } + // GPL + case CM_GPL: + ActiveCommandParams.Gap3Length = currByte; + break; - /// - /// Clears the result buffer - /// - public void ClearResultBuffer() - { - for (int i = 0; i < ResBuffer.Length; i++) - { - ResBuffer[i] = 0; - } - } + // DTL + case CM_DTL: + ActiveCommandParams.DTL = currByte; + break; - /// - /// Clears the result buffer - /// - public void ClearExecBuffer() - { - for (int i = 0; i < ExecBuffer.Length; i++) - { - ExecBuffer[i] = 0; - } - } + default: + break; + } + } - /// - /// Populates the result status registers - /// - private void CommitResultStatus() - { - // check for read diag - if (ActiveCommand.CommandCode == 0x02) - { - // commit to result buffer - ResBuffer[RS_ST0] = Status0; - ResBuffer[RS_ST1] = Status1; - return; - } + /// + /// Clears the result buffer + /// + public void ClearResultBuffer() + { + for (int i = 0; i < ResBuffer.Length; i++) + { + ResBuffer[i] = 0; + } + } - // check for error bits - if (GetBit(SR1_DE, Status1) || - GetBit(SR1_MA, Status1) || - GetBit(SR1_ND, Status1) || - GetBit(SR1_NW, Status1) || - GetBit(SR1_OR, Status1) || - GetBit(SR2_BC, Status2) || - GetBit(SR2_CM, Status2) || - GetBit(SR2_DD, Status2) || - GetBit(SR2_MD, Status2) || - GetBit(SR2_SN, Status2) || - GetBit(SR2_WC, Status2)) - { - // error bits set - unset end of track - UnSetBit(SR1_EN, ref Status1); - } + /// + /// Clears the result buffer + /// + public void ClearExecBuffer() + { + for (int i = 0; i < ExecBuffer.Length; i++) + { + ExecBuffer[i] = 0; + } + } - // check for data errors - if (GetBit(SR1_DE, Status1) || - GetBit(SR2_DD, Status2)) - { - // unset control mark - UnSetBit(SR2_CM, ref Status2); - } - else if (GetBit(SR2_CM, Status2)) - { - // DAM found - unset IC and US0 - UnSetBit(SR0_IC0, ref Status0); - UnSetBit(SR0_US0, ref Status0); - } + /// + /// Populates the result status registers + /// + private void CommitResultStatus() + { + // check for read diag + if (ActiveCommand.CommandCode == 0x02) + { + // commit to result buffer + ResBuffer[RS_ST0] = Status0; + ResBuffer[RS_ST1] = Status1; + return; + } - // commit to result buffer - ResBuffer[RS_ST0] = Status0; - ResBuffer[RS_ST1] = Status1; - ResBuffer[RS_ST2] = Status2; - - } + // check for error bits + if (GetBit(SR1_DE, Status1) || + GetBit(SR1_MA, Status1) || + GetBit(SR1_ND, Status1) || + GetBit(SR1_NW, Status1) || + GetBit(SR1_OR, Status1) || + GetBit(SR2_BC, Status2) || + GetBit(SR2_CM, Status2) || + GetBit(SR2_DD, Status2) || + GetBit(SR2_MD, Status2) || + GetBit(SR2_SN, Status2) || + GetBit(SR2_WC, Status2)) + { + // error bits set - unset end of track + UnSetBit(SR1_EN, ref Status1); + } - /// - /// Populates the result CHRN values - /// - private void CommitResultCHRN() - { - ResBuffer[RS_C] = ActiveCommandParams.Cylinder; - ResBuffer[RS_H] = ActiveCommandParams.Head; - ResBuffer[RS_R] = ActiveCommandParams.Sector; - ResBuffer[RS_N] = ActiveCommandParams.SectorSize; - } + // check for data errors + if (GetBit(SR1_DE, Status1) || + GetBit(SR2_DD, Status2)) + { + // unset control mark + UnSetBit(SR2_CM, ref Status2); + } + else if (GetBit(SR2_CM, Status2)) + { + // DAM found - unset IC and US0 + UnSetBit(SR0_IC0, ref Status0); + UnSetBit(SR0_US0, ref Status0); + } - /// - /// Moves active phase into idle - /// - public void SetPhase_Idle() - { - ActivePhase = Phase.Idle; + // commit to result buffer + ResBuffer[RS_ST0] = Status0; + ResBuffer[RS_ST1] = Status1; + ResBuffer[RS_ST2] = Status2; - // active direction - UnSetBit(MSR_DIO, ref StatusMain); - // CB - UnSetBit(MSR_CB, ref StatusMain); - // RQM - SetBit(MSR_RQM, ref StatusMain); + } - CommCounter = 0; - ResCounter = 0; - } + /// + /// Populates the result CHRN values + /// + private void CommitResultCHRN() + { + ResBuffer[RS_C] = ActiveCommandParams.Cylinder; + ResBuffer[RS_H] = ActiveCommandParams.Head; + ResBuffer[RS_R] = ActiveCommandParams.Sector; + ResBuffer[RS_N] = ActiveCommandParams.SectorSize; + } - /// - /// Moves to result phase - /// - public void SetPhase_Result() - { - ActivePhase = Phase.Result; + /// + /// Moves active phase into idle + /// + public void SetPhase_Idle() + { + ActivePhase = Phase.Idle; - // active direction - SetBit(MSR_DIO, ref StatusMain); - // CB - SetBit(MSR_CB, ref StatusMain); - // RQM - SetBit(MSR_RQM, ref StatusMain); - // EXM - UnSetBit(MSR_EXM, ref StatusMain); + // active direction + UnSetBit(MSR_DIO, ref StatusMain); + // CB + UnSetBit(MSR_CB, ref StatusMain); + // RQM + SetBit(MSR_RQM, ref StatusMain); - CommCounter = 0; - ResCounter = 0; - } + CommCounter = 0; + ResCounter = 0; + } - /// - /// Moves to command phase - /// - public void SetPhase_Command() - { - ActivePhase = Phase.Command; + /// + /// Moves to result phase + /// + public void SetPhase_Result() + { + ActivePhase = Phase.Result; - // default 0x80 - just RQM - SetBit(MSR_RQM, ref StatusMain); - UnSetBit(MSR_DIO, ref StatusMain); - UnSetBit(MSR_CB, ref StatusMain); - UnSetBit(MSR_EXM, ref StatusMain); - CommCounter = 0; - ResCounter = 0; - } + // active direction + SetBit(MSR_DIO, ref StatusMain); + // CB + SetBit(MSR_CB, ref StatusMain); + // RQM + SetBit(MSR_RQM, ref StatusMain); + // EXM + UnSetBit(MSR_EXM, ref StatusMain); - /// - /// Moves to execution phase - /// - public void SetPhase_Execution() - { - ActivePhase = Phase.Execution; + CommCounter = 0; + ResCounter = 0; + } - // EXM - SetBit(MSR_EXM, ref StatusMain); - // CB - SetBit(MSR_CB, ref StatusMain); - // RQM - UnSetBit(MSR_RQM, ref StatusMain); + /// + /// Moves to command phase + /// + public void SetPhase_Command() + { + ActivePhase = Phase.Command; - CommCounter = 0; - ResCounter = 0; - } + // default 0x80 - just RQM + SetBit(MSR_RQM, ref StatusMain); + UnSetBit(MSR_DIO, ref StatusMain); + UnSetBit(MSR_CB, ref StatusMain); + UnSetBit(MSR_EXM, ref StatusMain); + CommCounter = 0; + ResCounter = 0; + } - #endregion - } + /// + /// Moves to execution phase + /// + public void SetPhase_Execution() + { + ActivePhase = Phase.Execution; + + // EXM + SetBit(MSR_EXM, ref StatusMain); + // CB + SetBit(MSR_CB, ref StatusMain); + // RQM + UnSetBit(MSR_RQM, ref StatusMain); + + CommCounter = 0; + ResCounter = 0; + } + + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.FDD.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.FDD.cs index 248f2fcd0d..25a8c59a45 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.FDD.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.FDD.cs @@ -5,331 +5,331 @@ using System.Linq; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Floppy drive related stuff - /// - #region Attribution - /* + /// + /// Floppy drive related stuff + /// + #region Attribution + /* Implementation based on the information contained here: http://www.cpcwiki.eu/index.php/765_FDC and here: http://www.cpcwiki.eu/imgs/f/f3/UPD765_Datasheet_OCRed.pdf */ - #endregion - public partial class NECUPD765 : IFDDHost - { - #region Drive State + #endregion + public partial class NECUPD765 : IFDDHost + { + #region Drive State - /// - /// FDD Flag - motor on/off - /// - public bool FDD_FLAG_MOTOR; + /// + /// FDD Flag - motor on/off + /// + public bool FDD_FLAG_MOTOR; - /// - /// The index of the currently active disk drive - /// - public int DiskDriveIndex - { - get { return _diskDriveIndex; } - set - { - // when index is changed update the ActiveDrive - _diskDriveIndex = value; - ActiveDrive = DriveStates[_diskDriveIndex]; - } - } - private int _diskDriveIndex = 0; + /// + /// The index of the currently active disk drive + /// + public int DiskDriveIndex + { + get { return _diskDriveIndex; } + set + { + // when index is changed update the ActiveDrive + _diskDriveIndex = value; + ActiveDrive = DriveStates[_diskDriveIndex]; + } + } + private int _diskDriveIndex = 0; - /// - /// The currently active drive - /// - private DriveState ActiveDrive; + /// + /// The currently active drive + /// + private DriveState ActiveDrive; - /// - /// Array that holds state information for each possible drive - /// - private DriveState[] DriveStates = new DriveState[4]; + /// + /// Array that holds state information for each possible drive + /// + private DriveState[] DriveStates = new DriveState[4]; - #endregion + #endregion - #region FDD Methods + #region FDD Methods - /// - /// Initialization / reset of the floppy drive subsystem - /// - private void FDD_Init() - { - for (int i = 0; i < 4; i++) - { - DriveState ds = new DriveState(i, this); - DriveStates[i] = ds; - } - } + /// + /// Initialization / reset of the floppy drive subsystem + /// + private void FDD_Init() + { + for (int i = 0; i < 4; i++) + { + DriveState ds = new DriveState(i, this); + DriveStates[i] = ds; + } + } - /// - /// Searches for the requested sector - /// - private FloppyDisk.Sector GetSector() - { - FloppyDisk.Sector sector = null; + /// + /// Searches for the requested sector + /// + private FloppyDisk.Sector GetSector() + { + FloppyDisk.Sector sector = null; - // get the current track - var trk = ActiveDrive.Disk.DiskTracks[ActiveDrive.TrackIndex]; + // get the current track + var trk = ActiveDrive.Disk.DiskTracks[ActiveDrive.TrackIndex]; - // get the current sector index - int index = ActiveDrive.SectorIndex; + // get the current sector index + int index = ActiveDrive.SectorIndex; - // make sure this index exists - if (index > trk.Sectors.Length) - { - index = 0; - } + // make sure this index exists + if (index > trk.Sectors.Length) + { + index = 0; + } - // index hole count - int iHole = 0; + // index hole count + int iHole = 0; - // loop through the sectors in a track - // the loop ends with either the sector being found - // or the index hole being passed twice - while (iHole <= 2) - { - // does the requested sector match the current sector - if (trk.Sectors[index].SectorIDInfo.C == ActiveCommandParams.Cylinder && - trk.Sectors[index].SectorIDInfo.H == ActiveCommandParams.Head && - trk.Sectors[index].SectorIDInfo.R == ActiveCommandParams.Sector && - trk.Sectors[index].SectorIDInfo.N == ActiveCommandParams.SectorSize) - { - // sector has been found - sector = trk.Sectors[index]; + // loop through the sectors in a track + // the loop ends with either the sector being found + // or the index hole being passed twice + while (iHole <= 2) + { + // does the requested sector match the current sector + if (trk.Sectors[index].SectorIDInfo.C == ActiveCommandParams.Cylinder && + trk.Sectors[index].SectorIDInfo.H == ActiveCommandParams.Head && + trk.Sectors[index].SectorIDInfo.R == ActiveCommandParams.Sector && + trk.Sectors[index].SectorIDInfo.N == ActiveCommandParams.SectorSize) + { + // sector has been found + sector = trk.Sectors[index]; - UnSetBit(SR2_BC, ref Status2); - UnSetBit(SR2_WC, ref Status2); - break; - } + UnSetBit(SR2_BC, ref Status2); + UnSetBit(SR2_WC, ref Status2); + break; + } - // check for bad cylinder - if (trk.Sectors[index].SectorIDInfo.C == 255) - { - SetBit(SR2_BC, ref Status2); - } - // check for no cylinder - else if (trk.Sectors[index].SectorIDInfo.C != ActiveCommandParams.Cylinder) - { - SetBit(SR2_WC, ref Status2); - } + // check for bad cylinder + if (trk.Sectors[index].SectorIDInfo.C == 255) + { + SetBit(SR2_BC, ref Status2); + } + // check for no cylinder + else if (trk.Sectors[index].SectorIDInfo.C != ActiveCommandParams.Cylinder) + { + SetBit(SR2_WC, ref Status2); + } - // incrememnt sector index - index++; + // incrememnt sector index + index++; - // have we reached the index hole? - if (trk.Sectors.Length <= index) - { - // wrap around - index = 0; - iHole++; - } - } + // have we reached the index hole? + if (trk.Sectors.Length <= index) + { + // wrap around + index = 0; + iHole++; + } + } - // search loop has completed and the sector may or may not have been found + // search loop has completed and the sector may or may not have been found - // bad cylinder detected? - if (Status2.Bit(SR2_BC)) - { - // remove WC - UnSetBit(SR2_WC, ref Status2); - } + // bad cylinder detected? + if (Status2.Bit(SR2_BC)) + { + // remove WC + UnSetBit(SR2_WC, ref Status2); + } - // update sectorindex on drive - ActiveDrive.SectorIndex = index; + // update sectorindex on drive + ActiveDrive.SectorIndex = index; - return sector; - } + return sector; + } - #endregion + #endregion - #region IFDDHost + #region IFDDHost - // IFDDHost methods that fall through to the currently active drive + // IFDDHost methods that fall through to the currently active drive - /// - /// Parses a new disk image and loads it into this floppy drive - /// - public void FDD_LoadDisk(byte[] diskData) - { - // we are only going to load into the first drive - DriveStates[0].FDD_LoadDisk(diskData); - } + /// + /// Parses a new disk image and loads it into this floppy drive + /// + public void FDD_LoadDisk(byte[] diskData) + { + // we are only going to load into the first drive + DriveStates[0].FDD_LoadDisk(diskData); + } - /// - /// Ejects the current disk - /// - public void FDD_EjectDisk() - { - DriveStates[0].FDD_EjectDisk(); - } + /// + /// Ejects the current disk + /// + public void FDD_EjectDisk() + { + DriveStates[0].FDD_EjectDisk(); + } - /// - /// Signs whether the current active drive has a disk inserted - /// - public bool FDD_IsDiskLoaded - { - get { return DriveStates[DiskDriveIndex].FDD_IsDiskLoaded; } - } + /// + /// Signs whether the current active drive has a disk inserted + /// + public bool FDD_IsDiskLoaded + { + get { return DriveStates[DiskDriveIndex].FDD_IsDiskLoaded; } + } - /// - /// Returns the disk object from drive 0 - /// - public FloppyDisk DiskPointer - { - get { return DriveStates[0].Disk; } - } - - public FloppyDisk Disk { get; set; } + /// + /// Returns the disk object from drive 0 + /// + public FloppyDisk DiskPointer + { + get { return DriveStates[0].Disk; } + } - #endregion + public FloppyDisk Disk { get; set; } - #region Drive Status Class + #endregion - /// - /// Holds specfic state information about a drive - /// - private class DriveState : IFDDHost - { - #region State + #region Drive Status Class - /// - /// The drive ID from an FDC perspective - /// - public int ID; + /// + /// Holds specfic state information about a drive + /// + private class DriveState : IFDDHost + { + #region State - /// - /// Signs whether this drive ready - /// TRUE if both drive exists and has a disk inserted - /// - public bool FLAG_READY - { - get - { - if (!FDD_IsDiskLoaded || Disk.GetTrackCount() == 0 || !FDC.FDD_FLAG_MOTOR) - return false; - else - return true; - } - } + /// + /// The drive ID from an FDC perspective + /// + public int ID; - /// - /// Disk is write protected (TRUE BY DEFAULT) - /// - public bool FLAG_WRITEPROTECT = false; + /// + /// Signs whether this drive ready + /// TRUE if both drive exists and has a disk inserted + /// + public bool FLAG_READY + { + get + { + if (!FDD_IsDiskLoaded || Disk.GetTrackCount() == 0 || !FDC.FDD_FLAG_MOTOR) + return false; + else + return true; + } + } - /// - /// Storage for seek steps - /// One step for each indexpulse (track index) until seeked track - /// - public int SeekCounter; + /// + /// Disk is write protected (TRUE BY DEFAULT) + /// + public bool FLAG_WRITEPROTECT = false; - /// - /// Seek status - /// - public int SeekStatus; + /// + /// Storage for seek steps + /// One step for each indexpulse (track index) until seeked track + /// + public int SeekCounter; - /// - /// Age counter - /// - public int SeekAge; + /// + /// Seek status + /// + public int SeekStatus; - /// - /// The current side - /// - public byte CurrentSide; + /// + /// Age counter + /// + public int SeekAge; - /// - /// The current track index in the DiskTracks array - /// - public byte TrackIndex; + /// + /// The current side + /// + public byte CurrentSide; - /// - /// The track ID of the current cylinder - /// - public byte CurrentTrackID - { - get - { - // default invalid track - int id = 0xff; + /// + /// The current track index in the DiskTracks array + /// + public byte TrackIndex; - if (Disk == null) - return (byte)id; + /// + /// The track ID of the current cylinder + /// + public byte CurrentTrackID + { + get + { + // default invalid track + int id = 0xff; - if (Disk.DiskTracks.Count() == 0) - return (byte)id; + if (Disk == null) + return (byte)id; - if (TrackIndex >= Disk.GetTrackCount()) - TrackIndex = 0; - else if (TrackIndex < 0) - TrackIndex = 0; + if (Disk.DiskTracks.Count() == 0) + return (byte)id; - var track = Disk.DiskTracks[TrackIndex]; + if (TrackIndex >= Disk.GetTrackCount()) + TrackIndex = 0; + else if (TrackIndex < 0) + TrackIndex = 0; - id = track.TrackNumber; + var track = Disk.DiskTracks[TrackIndex]; - return (byte)id; - } - set - { - for (int i = 0; i < Disk.GetTrackCount(); i++) - { - if (Disk.DiskTracks[i].TrackNumber == value) - { - TrackIndex = (byte)i; - break; - } - } - } - } + id = track.TrackNumber; + + return (byte)id; + } + set + { + for (int i = 0; i < Disk.GetTrackCount(); i++) + { + if (Disk.DiskTracks[i].TrackNumber == value) + { + TrackIndex = (byte)i; + break; + } + } + } + } - /// - /// The new track that the drive is seeking to - /// (used in seek operations) - /// - public int SeekingTrack; + /// + /// The new track that the drive is seeking to + /// (used in seek operations) + /// + public int SeekingTrack; - /// - /// The current sector index in the Sectors array - /// - public int SectorIndex; + /// + /// The current sector index in the Sectors array + /// + public int SectorIndex; - /// - /// The currently loaded floppy disk - /// - public FloppyDisk Disk { get; set; } + /// + /// The currently loaded floppy disk + /// + public FloppyDisk Disk { get; set; } - /// - /// The parent controller - /// - private NECUPD765 FDC; + /// + /// The parent controller + /// + private NECUPD765 FDC; - #endregion + #endregion - #region Lookups + #region Lookups - /// - /// TRUE if we are on track 0 - /// - public bool FLAG_TRACK0 - { - get - { - if (TrackIndex == 0) { return true; } - else { return false; } - } - } + /// + /// TRUE if we are on track 0 + /// + public bool FLAG_TRACK0 + { + get + { + if (TrackIndex == 0) { return true; } + else { return false; } + } + } - #endregion + #endregion - #region Public Methods - /* + #region Public Methods + /* /// /// Moves the head across the disk cylinders /// @@ -369,7 +369,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC } */ - /* + /* /// /// Finds a supplied sector @@ -524,7 +524,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC } */ - /* + /* /// /// The drive performs a seek operation if necessary @@ -751,131 +751,131 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC SetBit(SR0_EC, ref IntStatus); } */ - /* - // UnitSelect - SetUnitSelect(ID, ref IntStatus); + /* + // UnitSelect + SetUnitSelect(ID, ref IntStatus); - // move to none state - //CurrentState = DriveMainState.None; + // move to none state + //CurrentState = DriveMainState.None; - //SeekState = SeekSubState.SeekCompleted; + //SeekState = SeekSubState.SeekCompleted; - // set the seek interrupt flag for this drive - // this will be cleared at the next successful senseint - FLAG_SEEK_INTERRUPT = true; + // set the seek interrupt flag for this drive + // this will be cleared at the next successful senseint + FLAG_SEEK_INTERRUPT = true; - //CurrentState = DriveMainState.None; + //CurrentState = DriveMainState.None; - } - */ + } + */ - #endregion + #endregion - #region Construction + #region Construction - public DriveState(int driveID, NECUPD765 fdc) - { - ID = driveID; - FDC = fdc; - } + public DriveState(int driveID, NECUPD765 fdc) + { + ID = driveID; + FDC = fdc; + } - #endregion + #endregion - #region IFDDHost + #region IFDDHost - /// - /// Parses a new disk image and loads it into this floppy drive - /// - public void FDD_LoadDisk(byte[] diskData) - { - // try dsk first - FloppyDisk fdd = null; - bool found = false; + /// + /// Parses a new disk image and loads it into this floppy drive + /// + public void FDD_LoadDisk(byte[] diskData) + { + // try dsk first + FloppyDisk fdd = null; + bool found = false; - foreach (DiskType type in Enum.GetValues(typeof(DiskType))) - { - switch (type) - { - case DiskType.CPCExtended: - fdd = new CPCExtendedFloppyDisk(); - found = fdd.ParseDisk(diskData); - break; - case DiskType.CPC: - fdd = new CPCFloppyDisk(); - found = fdd.ParseDisk(diskData); - break; - } + foreach (DiskType type in Enum.GetValues(typeof(DiskType))) + { + switch (type) + { + case DiskType.CPCExtended: + fdd = new CPCExtendedFloppyDisk(); + found = fdd.ParseDisk(diskData); + break; + case DiskType.CPC: + fdd = new CPCFloppyDisk(); + found = fdd.ParseDisk(diskData); + break; + } - if (found) - { - Disk = fdd; - break; - } - } + if (found) + { + Disk = fdd; + break; + } + } - if (!found) - { - throw new Exception(this.GetType().ToString() + - "\n\nDisk image file could not be parsed. Potentially an unknown format."); - } - } + if (!found) + { + throw new Exception(this.GetType().ToString() + + "\n\nDisk image file could not be parsed. Potentially an unknown format."); + } + } - /// - /// Ejects the current disk - /// - public void FDD_EjectDisk() - { - Disk = null; - //FLAG_READY = false; - } + /// + /// Ejects the current disk + /// + public void FDD_EjectDisk() + { + Disk = null; + //FLAG_READY = false; + } - /// - /// Signs whether the current active drive has a disk inserted - /// - public bool FDD_IsDiskLoaded - { - get - { - if (Disk != null) - return true; - else - return false; - } - } + /// + /// Signs whether the current active drive has a disk inserted + /// + public bool FDD_IsDiskLoaded + { + get + { + if (Disk != null) + return true; + else + return false; + } + } - #endregion + #endregion - #region StateSerialization + #region StateSerialization - public void SyncState(Serializer ser) - { - ser.Sync(nameof(ID), ref ID); - ser.Sync(nameof(FLAG_WRITEPROTECT), ref FLAG_WRITEPROTECT); - //ser.Sync(nameof(FLAG_DISKCHANGED), ref FLAG_DISKCHANGED); - //ser.Sync(nameof(FLAG_RECALIBRATING), ref FLAG_RECALIBRATING); - //ser.Sync(nameof(FLAG_SEEK_INTERRUPT), ref FLAG_SEEK_INTERRUPT); - //ser.Sync(nameof(IntStatus), ref IntStatus); - //ser.Sync(nameof(ST0), ref ST0); - //ser.Sync(nameof(RecalibrationCounter), ref RecalibrationCounter); - ser.Sync(nameof(SeekCounter), ref SeekCounter); - ser.Sync(nameof(SeekStatus), ref SeekStatus); - ser.Sync(nameof(SeekAge), ref SeekAge); - ser.Sync(nameof(CurrentSide), ref CurrentSide); - //ser.Sync(nameof(CurrentTrack), ref CurrentTrack); - ser.Sync(nameof(TrackIndex), ref TrackIndex); - ser.Sync(nameof(SeekingTrack), ref SeekingTrack); - //ser.Sync(nameof(CurrentSector), ref CurrentSector); - ser.Sync(nameof(SectorIndex), ref SectorIndex); - //ser.Sync(nameof(RAngles), ref RAngles); - //ser.Sync(nameof(DataPointer), ref DataPointer); - //ser.SyncEnum(nameof(CurrentState), ref CurrentState); - //ser.SyncEnum(nameof(SeekState), ref SeekState); - //ser.SyncEnum(nameof(SeekIntState), ref SeekIntState); - } + public void SyncState(Serializer ser) + { + ser.Sync(nameof(ID), ref ID); + ser.Sync(nameof(FLAG_WRITEPROTECT), ref FLAG_WRITEPROTECT); + //ser.Sync(nameof(FLAG_DISKCHANGED), ref FLAG_DISKCHANGED); + //ser.Sync(nameof(FLAG_RECALIBRATING), ref FLAG_RECALIBRATING); + //ser.Sync(nameof(FLAG_SEEK_INTERRUPT), ref FLAG_SEEK_INTERRUPT); + //ser.Sync(nameof(IntStatus), ref IntStatus); + //ser.Sync(nameof(ST0), ref ST0); + //ser.Sync(nameof(RecalibrationCounter), ref RecalibrationCounter); + ser.Sync(nameof(SeekCounter), ref SeekCounter); + ser.Sync(nameof(SeekStatus), ref SeekStatus); + ser.Sync(nameof(SeekAge), ref SeekAge); + ser.Sync(nameof(CurrentSide), ref CurrentSide); + //ser.Sync(nameof(CurrentTrack), ref CurrentTrack); + ser.Sync(nameof(TrackIndex), ref TrackIndex); + ser.Sync(nameof(SeekingTrack), ref SeekingTrack); + //ser.Sync(nameof(CurrentSector), ref CurrentSector); + ser.Sync(nameof(SectorIndex), ref SectorIndex); + //ser.Sync(nameof(RAngles), ref RAngles); + //ser.Sync(nameof(DataPointer), ref DataPointer); + //ser.SyncEnum(nameof(CurrentState), ref CurrentState); + //ser.SyncEnum(nameof(SeekState), ref SeekState); + //ser.SyncEnum(nameof(SeekIntState), ref SeekIntState); + } - #endregion - } + #endregion + } -#endregion - } + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.IPortIODevice.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.IPortIODevice.cs index e58821a477..5f6e87f99a 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.IPortIODevice.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.IPortIODevice.cs @@ -5,32 +5,32 @@ using System.Text; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// IPortIODevice - /// - #region Attribution - /* + /// + /// IPortIODevice + /// + #region Attribution + /* Implementation based on the information contained here: http://www.cpcwiki.eu/index.php/765_FDC and here: http://www.cpcwiki.eu/imgs/f/f3/UPD765_Datasheet_OCRed.pdf */ - #endregion - public partial class NECUPD765 : IPortIODevice - { - #region Dev Logging + #endregion + public partial class NECUPD765 : IPortIODevice + { + #region Dev Logging - public string outputfile = @"D:\Dropbox\Dropbox\_Programming\TASVideos\BizHawk\output\zxhawkio-" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".csv"; - public string outputString = "STATUS,WRITE,READ,CODE,MT,MF,SK,CMDCNT,RESCNT,EXECCNT,EXECLEN\r\n"; - public bool writeDebug = false; + public string outputfile = @"D:\Dropbox\Dropbox\_Programming\TASVideos\BizHawk\output\zxhawkio-" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".csv"; + public string outputString = "STATUS,WRITE,READ,CODE,MT,MF,SK,CMDCNT,RESCNT,EXECCNT,EXECLEN\r\n"; + public bool writeDebug = false; - public List dLog = new List - { - "STATUS,WRITE,READ,CODE,MT,MF,SK,CMDCNT,RESCNT,EXECCNT,EXECLEN" - }; + public List dLog = new List + { + "STATUS,WRITE,READ,CODE,MT,MF,SK,CMDCNT,RESCNT,EXECCNT,EXECLEN" + }; - /* + /* * Status read * Data write * Data read @@ -40,154 +40,154 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC * MK flag * SK flag * */ - private string[] workingArr = new string[3]; + private string[] workingArr = new string[3]; - private void BuildCSVLine() - { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < 3; i++) - { - sb.Append(workingArr[i]); - sb.Append(","); - workingArr[i] = ""; - } + private void BuildCSVLine() + { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < 3; i++) + { + sb.Append(workingArr[i]); + sb.Append(","); + workingArr[i] = ""; + } - sb.Append(ActiveCommand.CommandCode).Append(","); + sb.Append(ActiveCommand.CommandCode).Append(","); - sb.Append(CMD_FLAG_MT).Append(","); - sb.Append(CMD_FLAG_MF).Append(","); - sb.Append(CMD_FLAG_SK).Append(","); + sb.Append(CMD_FLAG_MT).Append(","); + sb.Append(CMD_FLAG_MF).Append(","); + sb.Append(CMD_FLAG_SK).Append(","); - sb.Append(CommCounter).Append(","); - sb.Append(ResCounter).Append(","); - sb.Append(ExecCounter).Append(","); - sb.Append(ExecLength); + sb.Append(CommCounter).Append(","); + sb.Append(ResCounter).Append(","); + sb.Append(ExecCounter).Append(","); + sb.Append(ExecLength); - //sb.Append("\r\n"); + //sb.Append("\r\n"); - //outputString += sb.ToString(); - dLog.Add(sb.ToString()); - } + //outputString += sb.ToString(); + dLog.Add(sb.ToString()); + } - #endregion + #endregion - public void ReadStatus(ref int data) - { - // read main status register - // this can happen at any time - data = ReadMainStatus(); - if (writeDebug) - { - //outputString += data + ",,," + ActiveCommand.CommandCode + "\r\n"; - workingArr[0] = data.ToString(); - BuildCSVLine(); - //System.IO.File.WriteAllText(outputfile, outputString); - } - } + public void ReadStatus(ref int data) + { + // read main status register + // this can happen at any time + data = ReadMainStatus(); + if (writeDebug) + { + //outputString += data + ",,," + ActiveCommand.CommandCode + "\r\n"; + workingArr[0] = data.ToString(); + BuildCSVLine(); + //System.IO.File.WriteAllText(outputfile, outputString); + } + } - public void ReadData(ref int data) - { - // Z80 is trying to read from the data register - data = ReadDataRegister(); - if (writeDebug) - { - workingArr[2] = data.ToString(); - //outputString += ",," + data + "," + ActiveCommand.CommandCode + "\r\n"; - BuildCSVLine(); - } - } + public void ReadData(ref int data) + { + // Z80 is trying to read from the data register + data = ReadDataRegister(); + if (writeDebug) + { + workingArr[2] = data.ToString(); + //outputString += ",," + data + "," + ActiveCommand.CommandCode + "\r\n"; + BuildCSVLine(); + } + } - public void WriteData(int data) - { - // Z80 is attempting to write to the data register - WriteDataRegister((byte)data); - if (writeDebug) - { - //outputString += "," + data + ",," + ActiveCommand.CommandCode + "\r\n"; - workingArr[1] = data.ToString(); - BuildCSVLine(); - //System.IO.File.WriteAllText(outputfile, outputString); - } - } + public void WriteData(int data) + { + // Z80 is attempting to write to the data register + WriteDataRegister((byte)data); + if (writeDebug) + { + //outputString += "," + data + ",," + ActiveCommand.CommandCode + "\r\n"; + workingArr[1] = data.ToString(); + BuildCSVLine(); + //System.IO.File.WriteAllText(outputfile, outputString); + } + } - public void Motor(int data) - { - // set disk motor on/off - if (data > 0) - FDD_FLAG_MOTOR = true; - else - FDD_FLAG_MOTOR = false; - } + public void Motor(int data) + { + // set disk motor on/off + if (data > 0) + FDD_FLAG_MOTOR = true; + else + FDD_FLAG_MOTOR = false; + } - /// - /// Device responds to an IN instruction - /// - public bool ReadPort(ushort port, ref int data) - { - BitArray bits = new BitArray(new byte[] { (byte)data }); + /// + /// Device responds to an IN instruction + /// + public bool ReadPort(ushort port, ref int data) + { + BitArray bits = new BitArray(new byte[] { (byte)data }); - if (port == 0x3ffd) - { - // Z80 is trying to read from the data register - data = ReadDataRegister(); - if (writeDebug) - { - workingArr[2] = data.ToString(); - //outputString += ",," + data + "," + ActiveCommand.CommandCode + "\r\n"; - BuildCSVLine(); - } - - return true; - } + if (port == 0x3ffd) + { + // Z80 is trying to read from the data register + data = ReadDataRegister(); + if (writeDebug) + { + workingArr[2] = data.ToString(); + //outputString += ",," + data + "," + ActiveCommand.CommandCode + "\r\n"; + BuildCSVLine(); + } - if (port == 0x2ffd) - { - // read main status register - // this can happen at any time - data = ReadMainStatus(); - if (writeDebug) - { - //outputString += data + ",,," + ActiveCommand.CommandCode + "\r\n"; - workingArr[0] = data.ToString(); - BuildCSVLine(); - //System.IO.File.WriteAllText(outputfile, outputString); - } - - return true; - } + return true; + } - return false; - } + if (port == 0x2ffd) + { + // read main status register + // this can happen at any time + data = ReadMainStatus(); + if (writeDebug) + { + //outputString += data + ",,," + ActiveCommand.CommandCode + "\r\n"; + workingArr[0] = data.ToString(); + BuildCSVLine(); + //System.IO.File.WriteAllText(outputfile, outputString); + } - /// - /// Device responds to an OUT instruction - /// - public bool WritePort(ushort port, int data) - { - BitArray bits = new BitArray(new byte[] { (byte)data }); + return true; + } - if (port == 0x3ffd) - { - // Z80 is attempting to write to the data register - WriteDataRegister((byte)data); - if (writeDebug) - { - //outputString += "," + data + ",," + ActiveCommand.CommandCode + "\r\n"; - workingArr[1] = data.ToString(); - BuildCSVLine(); - //System.IO.File.WriteAllText(outputfile, outputString); - } - - return true; - } + return false; + } - if (port == 0x1ffd) - { - // set disk motor on/off - FDD_FLAG_MOTOR = bits[3]; - return true; - } - return false; - } - } + /// + /// Device responds to an OUT instruction + /// + public bool WritePort(ushort port, int data) + { + BitArray bits = new BitArray(new byte[] { (byte)data }); + + if (port == 0x3ffd) + { + // Z80 is attempting to write to the data register + WriteDataRegister((byte)data); + if (writeDebug) + { + //outputString += "," + data + ",," + ActiveCommand.CommandCode + "\r\n"; + workingArr[1] = data.ToString(); + BuildCSVLine(); + //System.IO.File.WriteAllText(outputfile, outputString); + } + + return true; + } + + if (port == 0x1ffd) + { + // set disk motor on/off + FDD_FLAG_MOTOR = bits[3]; + return true; + } + return false; + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.Timing.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.Timing.cs index 41b4465f44..c6fb76300b 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.Timing.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.Timing.cs @@ -1,120 +1,120 @@  namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Timimng - /// - #region Attribution - /* + /// + /// Timimng + /// + #region Attribution + /* Implementation based on the information contained here: http://www.cpcwiki.eu/index.php/765_FDC and here: http://www.cpcwiki.eu/imgs/f/f3/UPD765_Datasheet_OCRed.pdf */ - #endregion - public partial class NECUPD765 - { - /// - /// The current Z80 cycle - /// - private long CurrentCPUCycle - { - get - { - if (_machine == null) - return 0; - else - return _machine.CPU.TotalExecutedCycles; - } - } + #endregion + public partial class NECUPD765 + { + /// + /// The current Z80 cycle + /// + private long CurrentCPUCycle + { + get + { + if (_machine == null) + return 0; + else + return _machine.CPU.TotalExecutedCycles; + } + } - /// - /// The last CPU cycle when the FDC accepted an IO read/write - /// - private long LastCPUCycle; + /// + /// The last CPU cycle when the FDC accepted an IO read/write + /// + private long LastCPUCycle; - /// - /// The current delay figure (in Z80 t-states) - /// This implementation only introduces delay upon main status register reads - /// All timing calculations should be done during the other read/write operations - /// - private long StatusDelay; + /// + /// The current delay figure (in Z80 t-states) + /// This implementation only introduces delay upon main status register reads + /// All timing calculations should be done during the other read/write operations + /// + private long StatusDelay; - /// - /// Defines the numbers of Z80 cycles per MS - /// - private long CPUCyclesPerMs; + /// + /// Defines the numbers of Z80 cycles per MS + /// + private long CPUCyclesPerMs; - /// - /// The floppy drive emulated clock speed - /// - public const double DriveClock = 31250; + /// + /// The floppy drive emulated clock speed + /// + public const double DriveClock = 31250; - /// - /// The number of floppy drive cycles per MS - /// - public long DriveCyclesPerMs; + /// + /// The number of floppy drive cycles per MS + /// + public long DriveCyclesPerMs; - /// - /// The number of T-States in one floppy drive clock tick - /// - public long StatesPerDriveTick; + /// + /// The number of T-States in one floppy drive clock tick + /// + public long StatesPerDriveTick; - /// - /// Responsible for measuring when the floppy drive is ready to run a cycle - /// - private long TickCounter; + /// + /// Responsible for measuring when the floppy drive is ready to run a cycle + /// + private long TickCounter; - /// - /// Internal drive cycle counter - /// - private int DriveCycleCounter = 1; + /// + /// Internal drive cycle counter + /// + private int DriveCycleCounter = 1; - /// - /// Initializes the timing routines - /// - private void TimingInit() - { - // z80 timing - double frameSize = _machine.GateArray.FrameLength; - double rRate = _machine.GateArray.Z80ClockSpeed / frameSize; - long tPerSecond = (long)(frameSize * rRate); - CPUCyclesPerMs = tPerSecond / 1000; + /// + /// Initializes the timing routines + /// + private void TimingInit() + { + // z80 timing + double frameSize = _machine.GateArray.FrameLength; + double rRate = _machine.GateArray.Z80ClockSpeed / frameSize; + long tPerSecond = (long)(frameSize * rRate); + CPUCyclesPerMs = tPerSecond / 1000; - // drive timing - double dRate = DriveClock / frameSize; - long dPerSecond = (long)(frameSize * dRate); - DriveCyclesPerMs = dPerSecond / 1000; + // drive timing + double dRate = DriveClock / frameSize; + long dPerSecond = (long)(frameSize * dRate); + DriveCyclesPerMs = dPerSecond / 1000; - long TStatesPerDriveCycle = (long)((double)_machine.GateArray.Z80ClockSpeed / DriveClock); - StatesPerDriveTick = TStatesPerDriveCycle; + long TStatesPerDriveCycle = (long)((double)_machine.GateArray.Z80ClockSpeed / DriveClock); + StatesPerDriveTick = TStatesPerDriveCycle; - } + } - /// - /// Called by reads to the main status register - /// Returns true if there is no delay - /// Returns false if read is to be deferred - /// - private bool CheckTiming() - { - // get delta - long delta = CurrentCPUCycle - LastCPUCycle; + /// + /// Called by reads to the main status register + /// Returns true if there is no delay + /// Returns false if read is to be deferred + /// + private bool CheckTiming() + { + // get delta + long delta = CurrentCPUCycle - LastCPUCycle; - if (StatusDelay >= delta) - { - // there is still delay remaining - StatusDelay -= delta; - LastCPUCycle = CurrentCPUCycle; - return false; - } - else - { - // no delay remaining - StatusDelay = 0; - LastCPUCycle = CurrentCPUCycle; - return true; - } - } - } + if (StatusDelay >= delta) + { + // there is still delay remaining + StatusDelay -= delta; + LastCPUCycle = CurrentCPUCycle; + return false; + } + else + { + // no delay remaining + StatusDelay = 0; + LastCPUCycle = CurrentCPUCycle; + return true; + } + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.cs index 25f623548d..f22522f971 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.cs @@ -3,243 +3,243 @@ using System.Collections.Generic; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// The NEC floppy disk controller (and floppy drive) found in the +3 - /// - #region Attribution - /* + /// + /// The NEC floppy disk controller (and floppy drive) found in the +3 + /// + #region Attribution + /* Implementation based on the information contained here: http://www.cpcwiki.eu/index.php/765_FDC and here: http://www.cpcwiki.eu/imgs/f/f3/UPD765_Datasheet_OCRed.pdf */ - #endregion - public partial class NECUPD765 - { - #region Devices + #endregion + public partial class NECUPD765 + { + #region Devices - /// - /// The emulated spectrum machine - /// - private CPCBase _machine; + /// + /// The emulated spectrum machine + /// + private CPCBase _machine; - #endregion + #endregion - #region Construction & Initialization + #region Construction & Initialization - /// - /// Main constructor - /// - public NECUPD765() - { - InitCommandList(); - } + /// + /// Main constructor + /// + public NECUPD765() + { + InitCommandList(); + } - /// - /// Initialization routine - /// - public void Init(CPCBase machine) - { - _machine = machine; - FDD_Init(); - TimingInit(); - Reset(); - } - - /// - /// Resets the FDC - /// - public void Reset() - { - // setup main status - StatusMain = 0; + /// + /// Initialization routine + /// + public void Init(CPCBase machine) + { + _machine = machine; + FDD_Init(); + TimingInit(); + Reset(); + } - Status0 = 0; - Status1 = 0; - Status2 = 0; - Status3 = 0; + /// + /// Resets the FDC + /// + public void Reset() + { + // setup main status + StatusMain = 0; - SetBit(MSR_RQM, ref StatusMain); + Status0 = 0; + Status1 = 0; + Status2 = 0; + Status3 = 0; - SetPhase_Idle(); + SetBit(MSR_RQM, ref StatusMain); - //FDC_FLAG_RQM = true; - //ActiveDirection = CommandDirection.IN; - SRT = 6; - HUT = 16; - HLT = 2; - HLT_Counter = 0; - HUT_Counter = 0; - IndexPulseCounter = 0; - CMD_FLAG_MF = false; + SetPhase_Idle(); - foreach (var d in DriveStates) - { - //d.SeekingTrack = d.CurrentTrack; - ////d.SeekCounter = 0; - //d.FLAG_SEEK_INTERRUPT = false; - //d.IntStatus = 0; - //d.SeekState = SeekSubState.Idle; - //d.SeekIntState = SeekIntStatus.Normal; + //FDC_FLAG_RQM = true; + //ActiveDirection = CommandDirection.IN; + SRT = 6; + HUT = 16; + HLT = 2; + HLT_Counter = 0; + HUT_Counter = 0; + IndexPulseCounter = 0; + CMD_FLAG_MF = false; - } - - } + foreach (var d in DriveStates) + { + //d.SeekingTrack = d.CurrentTrack; + ////d.SeekCounter = 0; + //d.FLAG_SEEK_INTERRUPT = false; + //d.IntStatus = 0; + //d.SeekState = SeekSubState.Idle; + //d.SeekIntState = SeekIntStatus.Normal; - /// - /// Setup the command structure - /// Each command represents one of the internal UPD765 commands - /// - private void InitCommandList() - { - CommandList = new List - { + } + + } + + /// + /// Setup the command structure + /// Each command represents one of the internal UPD765 commands + /// + private void InitCommandList() + { + CommandList = new List + { // read data new Command { CommandDelegate = UPD_ReadData, CommandCode = 0x06, MT = true, MF = true, SK = true, IsRead = true, - Direction = CommandDirection.OUT, ParameterByteCount = 8, ResultByteCount = 7 }, + Direction = CommandDirection.OUT, ParameterByteCount = 8, ResultByteCount = 7 }, // read id new Command { CommandDelegate = UPD_ReadID, CommandCode = 0x0a, MF = true, IsRead = true, - Direction = CommandDirection.OUT, ParameterByteCount = 1, ResultByteCount = 7 }, + Direction = CommandDirection.OUT, ParameterByteCount = 1, ResultByteCount = 7 }, // specify new Command { CommandDelegate = UPD_Specify, CommandCode = 0x03, - Direction = CommandDirection.OUT, ParameterByteCount = 2, ResultByteCount = 0 }, + Direction = CommandDirection.OUT, ParameterByteCount = 2, ResultByteCount = 0 }, // read diagnostic new Command { CommandDelegate = UPD_ReadDiagnostic, CommandCode = 0x02, MF = true, SK = true, IsRead = true, - Direction = CommandDirection.OUT, ParameterByteCount = 8, ResultByteCount = 7 }, + Direction = CommandDirection.OUT, ParameterByteCount = 8, ResultByteCount = 7 }, // scan equal new Command { CommandDelegate = UPD_ScanEqual, CommandCode = 0x11, MT = true, MF = true, SK = true, IsRead = true, - Direction = CommandDirection.IN, ParameterByteCount = 8, ResultByteCount = 7 }, + Direction = CommandDirection.IN, ParameterByteCount = 8, ResultByteCount = 7 }, // scan high or equal new Command { CommandDelegate = UPD_ScanHighOrEqual, CommandCode = 0x1d, MT = true, MF = true, SK = true, IsRead = true, - Direction = CommandDirection.IN, ParameterByteCount = 8, ResultByteCount = 7 }, + Direction = CommandDirection.IN, ParameterByteCount = 8, ResultByteCount = 7 }, // scan low or equal new Command { CommandDelegate = UPD_ScanLowOrEqual, CommandCode = 0x19, MT = true, MF = true, SK = true, IsRead = true, - Direction = CommandDirection.IN, ParameterByteCount = 8, ResultByteCount = 7 }, + Direction = CommandDirection.IN, ParameterByteCount = 8, ResultByteCount = 7 }, // read deleted data new Command { CommandDelegate = UPD_ReadDeletedData, CommandCode = 0x0c, MT = true, MF = true, SK = true, IsRead = true, - Direction = CommandDirection.OUT, ParameterByteCount = 8, ResultByteCount = 7 }, + Direction = CommandDirection.OUT, ParameterByteCount = 8, ResultByteCount = 7 }, // write data new Command { CommandDelegate = UPD_WriteData, CommandCode = 0x05, MT = true, MF = true, IsWrite = true, - Direction = CommandDirection.IN, ParameterByteCount = 8, ResultByteCount = 7 }, + Direction = CommandDirection.IN, ParameterByteCount = 8, ResultByteCount = 7 }, // write id new Command { CommandDelegate = UPD_WriteID, CommandCode = 0x0d, MF = true, IsWrite = true, - Direction = CommandDirection.IN, ParameterByteCount = 5, ResultByteCount = 7 }, + Direction = CommandDirection.IN, ParameterByteCount = 5, ResultByteCount = 7 }, // write deleted data new Command { CommandDelegate = UPD_WriteDeletedData, CommandCode = 0x09, MT = true, MF = true, IsWrite = true, - Direction = CommandDirection.IN, ParameterByteCount = 8, ResultByteCount = 7 }, + Direction = CommandDirection.IN, ParameterByteCount = 8, ResultByteCount = 7 }, // seek new Command { CommandDelegate = UPD_Seek, CommandCode = 0x0f, - Direction = CommandDirection.OUT, ParameterByteCount = 2, ResultByteCount = 0 }, + Direction = CommandDirection.OUT, ParameterByteCount = 2, ResultByteCount = 0 }, // recalibrate (seek track00) new Command { CommandDelegate = UPD_Recalibrate, CommandCode = 0x07, - Direction = CommandDirection.OUT, ParameterByteCount = 1, ResultByteCount = 0 }, + Direction = CommandDirection.OUT, ParameterByteCount = 1, ResultByteCount = 0 }, // sense interrupt status new Command { CommandDelegate = UPD_SenseInterruptStatus, CommandCode = 0x08, - Direction = CommandDirection.OUT, ParameterByteCount = 0, ResultByteCount = 2 }, + Direction = CommandDirection.OUT, ParameterByteCount = 0, ResultByteCount = 2 }, // sense drive status new Command { CommandDelegate = UPD_SenseDriveStatus, CommandCode = 0x04, - Direction = CommandDirection.OUT, ParameterByteCount = 1, ResultByteCount = 1 }, + Direction = CommandDirection.OUT, ParameterByteCount = 1, ResultByteCount = 1 }, // version new Command { CommandDelegate = UPD_Version, CommandCode = 0x10, - Direction = CommandDirection.OUT, ParameterByteCount = 0, ResultByteCount = 1 }, + Direction = CommandDirection.OUT, ParameterByteCount = 0, ResultByteCount = 1 }, // invalid new Command { CommandDelegate = UPD_Invalid, CommandCode = 0x00, - Direction = CommandDirection.OUT, ParameterByteCount = 0, ResultByteCount = 1 }, - }; - } + Direction = CommandDirection.OUT, ParameterByteCount = 0, ResultByteCount = 1 }, + }; + } - #endregion + #endregion - #region State Serialization + #region State Serialization - public void SyncState(Serializer ser) - { - ser.BeginSection("NEC-UPD765"); + public void SyncState(Serializer ser) + { + ser.BeginSection("NEC-UPD765"); - #region FDD - - ser.Sync(nameof(FDD_FLAG_MOTOR), ref FDD_FLAG_MOTOR); + #region FDD - for (int i = 0; i < 4; i++) - { - ser.BeginSection("HITDrive_" + i); - DriveStates[i].SyncState(ser); - ser.EndSection(); - } + ser.Sync(nameof(FDD_FLAG_MOTOR), ref FDD_FLAG_MOTOR); - ser.Sync(nameof(DiskDriveIndex), ref _diskDriveIndex); - // set active drive - DiskDriveIndex = _diskDriveIndex; + for (int i = 0; i < 4; i++) + { + ser.BeginSection("HITDrive_" + i); + DriveStates[i].SyncState(ser); + ser.EndSection(); + } - #endregion + ser.Sync(nameof(DiskDriveIndex), ref _diskDriveIndex); + // set active drive + DiskDriveIndex = _diskDriveIndex; - #region Registers + #endregion - ser.Sync("_RegMain", ref StatusMain); - ser.Sync("_Reg0", ref Status0); - ser.Sync("_Reg1", ref Status1); - ser.Sync("_Reg2", ref Status2); - ser.Sync("_Reg3", ref Status3); + #region Registers - #endregion + ser.Sync("_RegMain", ref StatusMain); + ser.Sync("_Reg0", ref Status0); + ser.Sync("_Reg1", ref Status1); + ser.Sync("_Reg2", ref Status2); + ser.Sync("_Reg3", ref Status3); - #region Controller state + #endregion - ser.Sync(nameof(DriveLight), ref DriveLight); - ser.SyncEnum(nameof(ActivePhase), ref ActivePhase); - //ser.SyncEnum(nameof(ActiveDirection), ref ActiveDirection); - ser.SyncEnum(nameof(ActiveInterrupt), ref ActiveInterrupt); - ser.Sync(nameof(CommBuffer), ref CommBuffer, false); - ser.Sync(nameof(CommCounter), ref CommCounter); - ser.Sync(nameof(ResBuffer), ref ResBuffer, false); - ser.Sync(nameof(ExecBuffer), ref ExecBuffer, false); - ser.Sync(nameof(ExecCounter), ref ExecCounter); - ser.Sync(nameof(ExecLength), ref ExecLength); - ser.Sync(nameof(InterruptResultBuffer), ref InterruptResultBuffer, false); - ser.Sync(nameof(ResCounter), ref ResCounter); - ser.Sync(nameof(ResLength), ref ResLength); - ser.Sync(nameof(LastSectorDataWriteByte), ref LastSectorDataWriteByte); - ser.Sync(nameof(LastSectorDataReadByte), ref LastSectorDataReadByte); - ser.Sync(nameof(LastByteReceived), ref LastByteReceived); - - ser.Sync(nameof(_cmdIndex), ref _cmdIndex); - // resync the ActiveCommand - CMDIndex = _cmdIndex; + #region Controller state - ActiveCommandParams.SyncState(ser); - - ser.Sync(nameof(IndexPulseCounter), ref IndexPulseCounter); - //ser.SyncEnum(nameof(_activeStatus), ref _activeStatus); - //ser.SyncEnum(nameof(_statusRaised), ref _statusRaised); + ser.Sync(nameof(DriveLight), ref DriveLight); + ser.SyncEnum(nameof(ActivePhase), ref ActivePhase); + //ser.SyncEnum(nameof(ActiveDirection), ref ActiveDirection); + ser.SyncEnum(nameof(ActiveInterrupt), ref ActiveInterrupt); + ser.Sync(nameof(CommBuffer), ref CommBuffer, false); + ser.Sync(nameof(CommCounter), ref CommCounter); + ser.Sync(nameof(ResBuffer), ref ResBuffer, false); + ser.Sync(nameof(ExecBuffer), ref ExecBuffer, false); + ser.Sync(nameof(ExecCounter), ref ExecCounter); + ser.Sync(nameof(ExecLength), ref ExecLength); + ser.Sync(nameof(InterruptResultBuffer), ref InterruptResultBuffer, false); + ser.Sync(nameof(ResCounter), ref ResCounter); + ser.Sync(nameof(ResLength), ref ResLength); + ser.Sync(nameof(LastSectorDataWriteByte), ref LastSectorDataWriteByte); + ser.Sync(nameof(LastSectorDataReadByte), ref LastSectorDataReadByte); + ser.Sync(nameof(LastByteReceived), ref LastByteReceived); - ser.Sync(nameof(CMD_FLAG_MT), ref CMD_FLAG_MT); - ser.Sync(nameof(CMD_FLAG_MF), ref CMD_FLAG_MF); - ser.Sync(nameof(CMD_FLAG_SK), ref CMD_FLAG_SK); - ser.Sync(nameof(SRT), ref SRT); - ser.Sync(nameof(HUT), ref HUT); - ser.Sync(nameof(HLT), ref HLT); - ser.Sync(nameof(ND), ref ND); - ser.Sync(nameof(SRT_Counter), ref SRT_Counter); - ser.Sync(nameof(HUT_Counter), ref HUT_Counter); - ser.Sync(nameof(HLT_Counter), ref HLT_Counter); + ser.Sync(nameof(_cmdIndex), ref _cmdIndex); + // resync the ActiveCommand + CMDIndex = _cmdIndex; - ser.Sync(nameof(SectorDelayCounter), ref SectorDelayCounter); - ser.Sync(nameof(SectorID), ref SectorID); + ActiveCommandParams.SyncState(ser); - #endregion + ser.Sync(nameof(IndexPulseCounter), ref IndexPulseCounter); + //ser.SyncEnum(nameof(_activeStatus), ref _activeStatus); + //ser.SyncEnum(nameof(_statusRaised), ref _statusRaised); - #region Timing + ser.Sync(nameof(CMD_FLAG_MT), ref CMD_FLAG_MT); + ser.Sync(nameof(CMD_FLAG_MF), ref CMD_FLAG_MF); + ser.Sync(nameof(CMD_FLAG_SK), ref CMD_FLAG_SK); + ser.Sync(nameof(SRT), ref SRT); + ser.Sync(nameof(HUT), ref HUT); + ser.Sync(nameof(HLT), ref HLT); + ser.Sync(nameof(ND), ref ND); + ser.Sync(nameof(SRT_Counter), ref SRT_Counter); + ser.Sync(nameof(HUT_Counter), ref HUT_Counter); + ser.Sync(nameof(HLT_Counter), ref HLT_Counter); - ser.Sync(nameof(LastCPUCycle), ref LastCPUCycle); - ser.Sync(nameof(StatusDelay), ref StatusDelay); - ser.Sync(nameof(TickCounter), ref TickCounter); - ser.Sync(nameof(DriveCycleCounter), ref DriveCycleCounter); + ser.Sync(nameof(SectorDelayCounter), ref SectorDelayCounter); + ser.Sync(nameof(SectorID), ref SectorID); - #endregion + #endregion - ser.EndSection(); - } + #region Timing - #endregion - } + ser.Sync(nameof(LastCPUCycle), ref LastCPUCycle); + ser.Sync(nameof(StatusDelay), ref StatusDelay); + ser.Sync(nameof(TickCounter), ref TickCounter); + ser.Sync(nameof(DriveCycleCounter), ref DriveCycleCounter); + + #endregion + + ser.EndSection(); + } + + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPS765.Static.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPS765.Static.cs index f74d8b4d29..9abd27e6a5 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPS765.Static.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPS765.Static.cs @@ -2,95 +2,95 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Static helper methods - /// - #region Attribution - /* + /// + /// Static helper methods + /// + #region Attribution + /* Implementation based on the information contained here: http://www.cpcwiki.eu/index.php/765_FDC and here: http://www.cpcwiki.eu/imgs/f/f3/UPD765_Datasheet_OCRed.pdf */ - #endregion - public partial class NECUPD765 - { - /// - /// Returns the specified bit value from supplied byte - /// - public static bool GetBit(int bitNumber, byte dataByte) - { - if (bitNumber < 0 || bitNumber > 7) - return false; + #endregion + public partial class NECUPD765 + { + /// + /// Returns the specified bit value from supplied byte + /// + public static bool GetBit(int bitNumber, byte dataByte) + { + if (bitNumber < 0 || bitNumber > 7) + return false; - BitArray bi = new BitArray(new byte[] { dataByte }); + BitArray bi = new BitArray(new byte[] { dataByte }); - return bi[bitNumber]; - } + return bi[bitNumber]; + } - /// - /// Sets the specified bit of the supplied byte to 1 - /// - public static void SetBit(int bitNumber, ref byte dataByte) - { - if (bitNumber < 0 || bitNumber > 7) - return; + /// + /// Sets the specified bit of the supplied byte to 1 + /// + public static void SetBit(int bitNumber, ref byte dataByte) + { + if (bitNumber < 0 || bitNumber > 7) + return; - int db = (int)dataByte; + int db = (int)dataByte; - db |= 1 << bitNumber; + db |= 1 << bitNumber; - dataByte = (byte)db; - } + dataByte = (byte)db; + } - /// - /// Sets the specified bit of the supplied byte to 0 - /// - public static void UnSetBit(int bitNumber, ref byte dataByte) - { - if (bitNumber < 0 || bitNumber > 7) - return; + /// + /// Sets the specified bit of the supplied byte to 0 + /// + public static void UnSetBit(int bitNumber, ref byte dataByte) + { + if (bitNumber < 0 || bitNumber > 7) + return; - int db = (int)dataByte; + int db = (int)dataByte; - db &= ~(1 << bitNumber); + db &= ~(1 << bitNumber); - dataByte = (byte)db; - } + dataByte = (byte)db; + } - /// - /// Returns a drive number (0-3) based on the first two bits of the supplied byte - /// - public static int GetUnitSelect(byte dataByte) - { - int driveNumber = dataByte & 0x03; - return driveNumber; - } + /// + /// Returns a drive number (0-3) based on the first two bits of the supplied byte + /// + public static int GetUnitSelect(byte dataByte) + { + int driveNumber = dataByte & 0x03; + return driveNumber; + } - /// - /// Sets the first two bits of a byte based on the supplied drive number (0-3) - /// - public static void SetUnitSelect(int driveNumber, ref byte dataByte) - { - switch (driveNumber) - { - case 0: - UnSetBit(SR0_US0, ref dataByte); - UnSetBit(SR0_US1, ref dataByte); - break; - case 1: - SetBit(SR0_US0, ref dataByte); - UnSetBit(SR0_US1, ref dataByte); - break; - case 2: - SetBit(SR0_US1, ref dataByte); - UnSetBit(SR0_US0, ref dataByte); - break; - case 3: - SetBit(SR0_US0, ref dataByte); - SetBit(SR0_US1, ref dataByte); - break; - } - } - } + /// + /// Sets the first two bits of a byte based on the supplied drive number (0-3) + /// + public static void SetUnitSelect(int driveNumber, ref byte dataByte) + { + switch (driveNumber) + { + case 0: + UnSetBit(SR0_US0, ref dataByte); + UnSetBit(SR0_US1, ref dataByte); + break; + case 1: + SetBit(SR0_US0, ref dataByte); + UnSetBit(SR0_US1, ref dataByte); + break; + case 2: + SetBit(SR0_US1, ref dataByte); + UnSetBit(SR0_US0, ref dataByte); + break; + case 3: + SetBit(SR0_US0, ref dataByte); + SetBit(SR0_US1, ref dataByte); + break; + } + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Display/AmstradGateArray.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Display/AmstradGateArray.cs index 38c2da72d9..55b2bf0106 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Display/AmstradGateArray.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Display/AmstradGateArray.cs @@ -11,38 +11,38 @@ using System.Threading.Tasks; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// * Amstrad Gate Array * - /// http://www.cpcwiki.eu/index.php/Gate_Array - /// https://web.archive.org/web/20170612081209/http://www.grimware.org/doku.php/documentations/devices/gatearray - /// - public class AmstradGateArray : IPortIODevice, IVideoProvider - { - #region Devices + /// + /// * Amstrad Gate Array * + /// http://www.cpcwiki.eu/index.php/Gate_Array + /// https://web.archive.org/web/20170612081209/http://www.grimware.org/doku.php/documentations/devices/gatearray + /// + public class AmstradGateArray : IPortIODevice, IVideoProvider + { + #region Devices - private CPCBase _machine; - private Z80A CPU => _machine.CPU; - private CRCT_6845 CRCT => _machine.CRCT; - //private CRTDevice CRT => _machine.CRT; - private IPSG PSG => _machine.AYDevice; - private NECUPD765 FDC => _machine.UPDDiskDevice; - private DatacorderDevice DATACORDER => _machine.TapeDevice; - private ushort BUSRQ => CPU.MEMRQ[CPU.bus_pntr]; - public const ushort PCh = 1; + private CPCBase _machine; + private Z80A CPU => _machine.CPU; + private CRCT_6845 CRCT => _machine.CRCT; + //private CRTDevice CRT => _machine.CRT; + private IPSG PSG => _machine.AYDevice; + private NECUPD765 FDC => _machine.UPDDiskDevice; + private DatacorderDevice DATACORDER => _machine.TapeDevice; + private ushort BUSRQ => CPU.MEMRQ[CPU.bus_pntr]; + public const ushort PCh = 1; - private GateArrayType ChipType; + private GateArrayType ChipType; - #endregion + #endregion - #region Palettes + #region Palettes - /// - /// The standard CPC Pallete (ordered by firmware #) - /// http://www.cpcwiki.eu/index.php/CPC_Palette - /// - public static readonly int[] CPCFirmwarePalette = - { - Colors.ARGB(0x00, 0x00, 0x00), // Black + /// + /// The standard CPC Pallete (ordered by firmware #) + /// http://www.cpcwiki.eu/index.php/CPC_Palette + /// + public static readonly int[] CPCFirmwarePalette = + { + Colors.ARGB(0x00, 0x00, 0x00), // Black Colors.ARGB(0x00, 0x00, 0x80), // Blue Colors.ARGB(0x00, 0x00, 0xFF), // Bright Blue Colors.ARGB(0x80, 0x00, 0x00), // Red @@ -71,13 +71,13 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC Colors.ARGB(0xFF, 0xFF, 0xFF), // Bright White }; - /// - /// The standard CPC Pallete (ordered by hardware #) - /// http://www.cpcwiki.eu/index.php/CPC_Palette - /// - public static readonly int[] CPCHardwarePalette = - { - Colors.ARGB(0x80, 0x80, 0x80), // White + /// + /// The standard CPC Pallete (ordered by hardware #) + /// http://www.cpcwiki.eu/index.php/CPC_Palette + /// + public static readonly int[] CPCHardwarePalette = + { + Colors.ARGB(0x80, 0x80, 0x80), // White Colors.ARGB(0x80, 0x80, 0x80), // White (duplicate) Colors.ARGB(0x00, 0xFF, 0x80), // Sea Green Colors.ARGB(0xFF, 0xFF, 0x80), // Pastel Yellow @@ -111,997 +111,997 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC Colors.ARGB(0x80, 0x80, 0xFF), // Pastel Blue }; - #endregion - - #region Clocks and Timing - - /// - /// The Gate Array Clock Speed - /// - public int GAClockSpeed = 16000000; - - /// - /// The CPU Clock Speed - /// - public int Z80ClockSpeed = 4000000; - - /// - /// CRCT Clock Speed - /// - public int CRCTClockSpeed = 1000000; - - /// - /// AY-3-8912 Clock Speed - /// - public int PSGClockSpeed = 1000000; - - /// - /// Number of CPU cycles in one frame - /// - public int FrameLength = 79872; - - /// - /// Number of Gate Array cycles within one frame - /// - public int GAFrameLength = 319488; - - #endregion - - #region Construction - - public AmstradGateArray(CPCBase machine, GateArrayType chipType) - { - _machine = machine; - ChipType = chipType; - //PenColours = new int[17]; - borderType = _machine.CPC.SyncSettings.BorderType; - SetupScreenSize(); - //Reset(); - - CRCT.AttachHSYNCCallback(OnHSYNC); - CRCT.AttachVSYNCCallback(OnVSYNC); - - CurrentLine = new CharacterLine(); - InitByteLookup(); - CalculateNextScreenMemory(); - } - - #endregion - - #region Registers and Internal State - - /// - /// PENR (register 0) - Pen Selection - /// This register can be used to select one of the 17 color-registers (pen 0 to 15 or the border). - /// It will remain selected until another PENR command is executed. - /// PENR Index - /// 7 6 5 4 3 2 1 0 color register selected - /// 0 0 0 0 n n n n pen n from 0 to 15 (4bits) - /// 0 0 0 1 x x x x border - /// - /// x can be 0 or 1, it doesn't matter - /// - private byte _PENR; - public byte PENR - { - get { return _PENR; } - set - { - _PENR = value; - if (_PENR.Bit(4)) - { - // border select - CurrentPen = 16; - } - else - { - // pen select - CurrentPen = _PENR & 0x0f; - } - } - } - - /// - /// 0-15: Pen Registers - /// 16: Border Colour - /// - public int[] ColourRegisters = new int[17]; - - /// - /// The currently selected Pen - /// - private int CurrentPen; - - /// - /// INKR (register 1) - Colour Selection - /// This register takes a 5bits parameter which is a color-code. This color-code range from 0 to 31 but there's only 27 differents colors - /// (because the Gate Array use a 3-states logic on the R,G and B signals, thus 3x3x3=27). - /// INKR Color - /// 7 6 5 4 3 2 1 0 - /// 0 1 0 n n n n n where n is a color code (5 bits) - /// - /// The PEN affected by the INKR command is updated (almost) immediatly - /// - private byte _INKR; - public byte INKR - { - get { return _INKR; } - set - { - _INKR = value; - ColourRegisters[CurrentPen] = _INKR & 0x1f; - } - } - - /// - /// RMR (register 2) - Select screen mode and ROM configuration - /// This register control the interrupt counter (reset), the upper and lower ROM paging and the video mode. - /// RMR Commands - /// 7 6 5 4 3 2 1 0 - /// 1 0 0 I UR LR VM--> - /// - /// I : if set (1), this will reset the interrupt counter - /// UR : Enable (0) or Disable (1) the upper ROM paging (&C000 to &FFFF). You can select which upper ROM with the I/O address &DF00 - /// LR : Enable (0) or Disable (1) the lower ROM paging - /// VM : Select the video mode 0,1,2 or 3 (it will take effect after the next HSync) - /// - private byte _RMR; - public byte RMR - { - get { return _RMR; } - set - { - _RMR = value; - //ScreenMode = _RMR & 0x03; - var sm = _RMR & 0x03; - if (sm != 1) - { - - } - - if ((_RMR & 0x08) != 0) - _machine.UpperROMPaged = false; - else - _machine.UpperROMPaged = true; - - if ((_RMR & 0x04) != 0) - _machine.LowerROMPaged = false; - else - _machine.LowerROMPaged = true; - - if (_RMR.Bit(4)) - { - // reset interrupt counter - InterruptCounter = 0; - } - } - } - - /// - /// RAMR (register 3) - RAM Banking - /// This register exists only in CPCs with 128K RAM (like the CPC 6128, or CPCs with Standard Memory Expansions) - /// Note: In the CPC 6128, the register is a separate PAL that assists the Gate Array chip - /// - /// Bit Value Function - /// 7 1 Gate Array function 3 - /// 6 1 - /// 5 b 64K bank number(0..7); always 0 on an unexpanded CPC6128, 0-7 on Standard Memory Expansions - /// 4 b - /// 3 b - /// 2 x RAM Config(0..7) - /// 1 x "" - /// 0 x "" - /// - /// The 3bit RAM Config value is used to access the second 64K of the total 128K RAM that is built into the CPC 6128 or the additional 64K-512K of standard memory expansions. - /// These contain up to eight 64K ram banks, which are selected with bit 3-5. A standard CPC 6128 only contains bank 0. Normally the register is set to 0, so that only the - /// first 64K RAM are used (identical to the CPC 464 and 664 models). The register can be used to select between the following eight predefined configurations only: - /// - /// -Address- 0 1 2 3 4 5 6 7 - /// 0000-3FFF RAM_0 RAM_0 RAM_4 RAM_0 RAM_0 RAM_0 RAM_0 RAM_0 - /// 4000-7FFF RAM_1 RAM_1 RAM_5 RAM_3 RAM_4 RAM_5 RAM_6 RAM_7 - /// 8000-BFFF RAM_2 RAM_2 RAM_6 RAM_2 RAM_2 RAM_2 RAM_2 RAM_2 - /// C000-FFFF RAM_3 RAM_7 RAM_7 RAM_7 RAM_3 RAM_3 RAM_3 RAM_3 - /// - /// The Video RAM is always located in the first 64K, VRAM is in no way affected by this register - /// - private byte _RAMR; - /// - /// This is actually implemented outside of here. These values do nothing. - /// - public byte RAMR - { - get { return _RAMR; } - set - { - _RAMR = value; - } - } - - /// - /// The selected screen mode (updated after every HSYNC) - /// - private int ScreenMode; - - /// - /// Simulates the internal 6bit INT counter - /// - private int _interruptCounter; - public int InterruptCounter - { - get { return _interruptCounter; } - set { _interruptCounter = value; } - } - - /// - /// Interrupts are delayed when a VSYNC occurs - /// - private int VSYNCDelay; - - /// - /// Signals that the frame end has been reached - /// - public bool FrameEnd; - - /// - /// Internal phase clock - /// - private int ClockCounter; - - /// - /// Master frame clock counter - /// - public int FrameClock; - - /// - /// Simulates the gate array memory /WAIT line - /// - private bool WaitLine; - - /// - /// 16-bit address - read from the CRCT - /// - private short _MA; - private short MA - { - get - { - _MA = CRCT.MA; - return _MA; - } - } - - /// - /// Set when the HSYNC signal is detected from the CRCT - /// - private bool HSYNC; - -// /// -// /// Is set when an initial HSYNC is seen from the CRCT -// /// On real hardware interrupt generation is based on the falling edge of the HSYNC signal -// /// So in this emulation, once the falling edge is detected, interrupt processing happens -// /// -// private bool HSYNC_falling; - - /// - /// Used to count HSYNCs during a VSYNC - /// - private int HSYNC_counter; - - /// - /// Set when the VSYNC signal is detected from the CRCT - /// - private bool VSYNC; - - /// - /// TRUE when the /INT pin is held low - /// - private bool InterruptRaised; - - /// - /// Counts the GA cycles that the /INT pin should be held low - /// - private int InterruptHoldCounter; - - /// - /// Set at the start of a new frame - /// - public bool IsNewFrame; - - /// - /// Set when a new line is beginning - /// - public bool IsNewLine; - - /// - /// Horizontal Character Counter - /// - private int HCC; - - /// - /// Vertical Line Counter - /// - private int VLC; - - /// - /// The first video byte fetched - /// - private byte VideoByte1; - - /// - /// The second video byte fetched - /// - private byte VideoByte2; - - #endregion - - #region Clock Business - - /// - /// Called every CPU cycle - /// In reality the GA is clocked at 16Mhz (4 times the frequency of the CPU) - /// Therefore this method has to take care of: - /// 4 GA cycles - /// 1 CRCT cycle every 4 calls - /// 1 PSG cycle every 4 calls - /// 1 CPU cycle (uncontended) - /// - public void ClockCycle() - { - // gatearray uses 4-phase clock to supply clocks to other devices - switch (ClockCounter) - { - case 0: - CRCT.ClockCycle(); - WaitLine = false; - break; - case 1: - WaitLine = true; - // detect new scanline and upcoming new frame on next render cycle - //FrameDetector(); - break; - case 2: - // video fetch - WaitLine = true; - //FetchByte(1); - break; - case 3: - // video fetch and render - WaitLine = true; - //FetchByte(2); - GACharacterCycle(); - //PixelGenerator(); - break; - } - - if (!HSYNC && CRCT.HSYNC) - { - HSYNC = true; - } - - // run the interrupt generator routine - InterruptGenerator(); - - if (!CRCT.HSYNC) - { - HSYNC = false; - } - - // conditional CPU cycle - DoConditionalCPUCycle(); - - AdvanceClock(); - } - - /// - /// Increments the internal clock counters by one - /// - private void AdvanceClock() - { - FrameClock++; - ClockCounter++; - - if (ClockCounter == 4) - ClockCounter = 0; - - // check for frame end - if (FrameClock == FrameLength) - { - FrameEnd = true; - } - } - - /// - /// Runs a 4 Mhz CPU cycle if neccessary - /// /WAIT line status is a factor here - /// - private void DoConditionalCPUCycle() - { - if (!WaitLine) - { - // /WAIT line is NOT active - CPU.ExecuteOne(); - return; - } - - // /WAIT line is active - switch (ClockCounter) - { - case 2: - case 3: - // gate array video fetch is occuring - // check for memory access - if (BUSRQ > 0) - { - // memory action upcoming - CPU clock is halted - CPU.TotalExecutedCycles++; - } - break; - - case 1: - // CPU accesses RAM if it's performing a non-opcode read or write - // assume for now that an opcode fetch is always looking at PC - if (BUSRQ == PCh) - { - // opcode fetch memory action upcoming - CPU clock is halted - CPU.TotalExecutedCycles++; - } - else - { - // no fetch, or non-opcode fetch - CPU.ExecuteOne(); - } - break; - } - } - - #endregion - - #region Frame & Interrupt Handling - - /// - /// The CRCT builds the picture in a strange way, so that the top left of the display area is the first pixel from - /// video RAM. The borders come either side of the HSYNC and VSYNCs later on: - /// https://web.archive.org/web/20170501112330im_/http://www.grimware.org/lib/exe/fetch.php/documentations/devices/crtc.6845/crtc.standard.video.frame.png?w=800&h=500 - /// Therefore when the gate array initialises, we will attempt end the frame early in order to - /// sync up at the point where VSYNC is active and HSYNC just begins. This is roughly how a CRT monitor would display the picture. - /// The CRT would start a new line at the point where an HSYNC is detected. - /// - private void FrameDetector() - { - if (CRCT.HSYNC && !IsNewLine) - { - // start of a new line on the next render cycle - IsNewLine = true; - - // process scanline - //CRT.CurrentLine.CommitScanline(); - - // check for end of frame - if (CRCT.VSYNC && !IsNewFrame) - { - // start of a new frame on the next render cycle - IsNewFrame = true; - //FrameEnd = true; - VLC = 0; - } - else if (!CRCT.VSYNC) - { - // increment line counter - VLC++; - IsNewFrame = false; - } - - HCC = 0; - - // update screenmode - //ScreenMode = RMR & 0x03; - //CRT.CurrentLine.InitScanline(ScreenMode, VLC); - } - else if (!CRCT.HSYNC) - { - // reset the flags - IsNewLine = false; - IsNewFrame = false; - } - } - - /// - /// Handles interrupt generation - /// - private void InterruptGenerator() - { - if (HSYNC && !CRCT.HSYNC) - { - // falling edge of the HSYNC detected - InterruptCounter++; - - if (CRCT.VSYNC) - { - if (HSYNC_counter >= 2) - { - // x2 HSYNC have happened during VSYNC - if (InterruptCounter >= 32) - { - // no interrupt - InterruptCounter = 0; - } - else if (InterruptCounter < 32) - { - // interrupt - InterruptRaised = true; - InterruptCounter = 0; - } - - HSYNC_counter = 0; - } - else - { - HSYNC_counter++; - } - } - - if (InterruptCounter == 52) - { - // gatearray should raise an interrupt - InterruptRaised = true; - InterruptCounter = 0; - } - } - - if (InterruptRaised) - { - // interrupt should been raised - CPU.FlagI = true; - InterruptHoldCounter++; - - // the INT signal should be held low for 1.4us. - // in gatearray cycles, this equates to 22.4 - // we will round down to 22 for emulation purposes - if (InterruptHoldCounter >= 22) - { - CPU.FlagI = false; - InterruptRaised = false; - InterruptHoldCounter = 0; - } - } - } - - #endregion - - #region Rendering Business - - /// - /// Builds up current scanline character information - /// Ther GA modifies HSYNC and VSYNC signals before they are sent to the monitor - /// This is handled here - /// Runs at 1Mhz - /// - private void GACharacterCycle() - { - if (CRCT.VSYNC && CRCT.HSYNC) - { - // both hsync and vsync active - CurrentLine.AddCharacter(Phase.HSYNCandVSYNC); - } - else if (CRCT.VSYNC) - { - // vsync is active but hsync is not - CurrentLine.AddCharacter(Phase.VSYNC); - } - else if (CRCT.HSYNC) - { - // hsync is active but vsync is not - CurrentLine.AddCharacter(Phase.HSYNC); - } - else if (!CRCT.DISPTMG) - { - // border generation - CurrentLine.AddCharacter(Phase.BORDER); - } - else if (CRCT.DISPTMG) - { - // pixels generated from video RAM - CurrentLine.AddCharacter(Phase.DISPLAY); - } - } - - /// - /// Holds the upcoming video RAM addresses for the next scanline - /// Firmware default size is 80 (40 characters - 2 bytes per character) - /// - private ushort[] NextVidRamLine = new ushort[40 * 2]; - - /// - /// The current character line we are working from - /// - private CharacterLine CurrentLine; - - /// - /// List of screen lines as they are built up - /// - private List ScreenLines = new List(); - - /// - /// Pixel value lookups for every scanline byte value - /// Based on the lookup at https://github.com/gavinpugh/xnacpc - /// - private int[][] ByteLookup = new int[4][]; - private void InitByteLookup() - { - int pix; - for (int m = 0; m < 4; m++) - { - int pos = 0; - ByteLookup[m] = new int[256 * 8]; - for (int b = 0; b < 256; b++) - { - switch (m) - { - case 0: - pix = b & 0xaa; - pix = (((pix & 0x80) >> 7) | ((pix & 0x08) >> 2) | ((pix & 0x20) >> 3) | ((pix & 0x02) << 2)); - for (int c = 0; c < 4; c++) - ByteLookup[m][pos++] = pix; - pix = b & 0x55; - pix = (((pix & 0x40) >> 6) | ((pix & 0x04) >> 1) | ((pix & 0x10) >> 2) | ((pix & 0x01) << 3)); - for (int c = 0; c < 4; c++) - ByteLookup[m][pos++] = pix; - break; - case 1: - pix = (((b & 0x80) >> 7) | ((b & 0x08) >> 2)); - ByteLookup[m][pos++] = pix; - ByteLookup[m][pos++] = pix; - pix = (((b & 0x40) >> 6) | ((b & 0x04) >> 1)); - ByteLookup[m][pos++] = pix; - ByteLookup[m][pos++] = pix; - pix = (((b & 0x20) >> 5) | (b & 0x02)); - ByteLookup[m][pos++] = pix; - ByteLookup[m][pos++] = pix; - pix = (((b & 0x10) >> 4) | ((b & 0x01) << 1)); - ByteLookup[m][pos++] = pix; - ByteLookup[m][pos++] = pix; - break; - case 2: - for (int i = 7; i >= 0; i--) - ByteLookup[m][pos++] = ((b & (1 << i)) != 0) ? 1 : 0; - break; - case 3: - pix = b & 0xaa; - pix = (((pix & 0x80) >> 7) | ((pix & 0x08) >> 2) | ((pix & 0x20) >> 3) | ((pix & 0x02) << 2)); - for (int c = 0; c < 4; c++) - ByteLookup[m][pos++] = pix; - pix = b & 0x55; - pix = (((pix & 0x40) >> 6) | ((pix & 0x04) >> 1) | ((pix & 0x10) >> 2) | ((pix & 0x01) << 3)); - for (int c = 0; c < 4; c++) - ByteLookup[m][pos++] = pix; - break; - } - } - } - } - - /// - /// Runs at HSYNC *AFTER* the scanline has been commmitted - /// Sets up the upcoming memory addresses for the next scanline - /// - private void CalculateNextScreenMemory() - { - var verCharCount = CRCT.VCC; - var verRasCount = CRCT.VLC; - - var screenWidthByteCount = CRCT.DisplayWidth * 2; - NextVidRamLine = new ushort[screenWidthByteCount * 2]; - var screenHeightCharCount = CRCT.DisplayHeightInChars; - var screenAddress = CRCT.MA; - - int baseAddress = ((screenAddress << 2) & 0xf000); - int offset = (screenAddress * 2) & 0x7ff; - - int x = offset + ((verCharCount * screenWidthByteCount) & 0x7ff); - int y = baseAddress + (verRasCount * 0x800); - - for (int b = 0; b < screenWidthByteCount; b++) - { - NextVidRamLine[b] = (ushort)(y + x); - x++; - x &= 0x7ff; - } - } - - /// - /// Called at the start of HSYNC, this renders the currently built-up scanline - /// - private void RenderScanline() - { - // memory addresses - int cRow = CRCT.VCC; - int cRas = CRCT.VLC; - - int screenByteWidth = CRCT.DisplayWidth * 2; - var screenHeightCharCount = CRCT.DisplayHeightInChars; - //CalculateNextScreenMemory(); - var crctAddr = CRCT.DStartHigh << 8; - crctAddr |= CRCT.DStartLow; - var baseAddr = ((crctAddr << 2) & (0xF000)); //CRCT.VideoPageBase;// - var baseOffset = (crctAddr * 2) & 0x7FF; //CRCT.VideoRAMOffset * 2; // - var xA = baseOffset + ((cRow * screenByteWidth) & 0x7ff); - var yA = baseAddr + (cRas * 2048); - - // border and display - int pix = 0; - int scrByte = 0; - - for (int i = 0; i < CurrentLine.PhaseCount; i++) - { - // every character renders 8 pixels - switch (CurrentLine.Phases[i]) - { - case Phase.NONE: - break; - - case Phase.HSYNC: - break; - case Phase.HSYNCandVSYNC: - break; - case Phase.VSYNC: - break; - case Phase.BORDER: - // output current border colour - for (pix = 0; pix < 16; pix++) - { - CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[16]]); - } - break; - case Phase.DISPLAY: - // each character references 2 bytes in video RAM - byte data; - - for (int by = 0; by < 2; by++) - { - ushort addr = (ushort)(yA + xA); - data = _machine.FetchScreenMemory(addr); - scrByte++; - - switch (CurrentLine.ScreenMode) - { - case 0: - pix = data & 0xaa; - pix = (((pix & 0x80) >> 7) | ((pix & 0x08) >> 2) | ((pix & 0x20) >> 3) | ((pix & 0x02) << 2)); - for (int c = 0; c < 4; c++) - CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix]]); - pix = data & 0x55; - pix = (((pix & 0x40) >> 6) | ((pix & 0x04) >> 1) | ((pix & 0x10) >> 2) | ((pix & 0x01) << 3)); - for (int c = 0; c < 4; c++) - CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix]]); - break; - case 1: - pix = (((data & 0x80) >> 7) | ((data & 0x08) >> 2)); - CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix]]); - CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix]]); - pix = (((data & 0x40) >> 6) | ((data & 0x04) >> 1)); - CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix]]); - CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix]]); - pix = (((data & 0x20) >> 5) | (data & 0x02)); - CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix]]); - CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix]]); - pix = (((data & 0x10) >> 4) | ((data & 0x01) << 1)); - CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix]]); - CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix]]); - break; - case 2: - pix = data; - CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix.Bit(7) ? 1 : 0]]); - CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix.Bit(6) ? 1 : 0]]); - CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix.Bit(5) ? 1 : 0]]); - CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix.Bit(4) ? 1 : 0]]); - CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix.Bit(3) ? 1 : 0]]); - CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix.Bit(2) ? 1 : 0]]); - CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix.Bit(1) ? 1 : 0]]); - CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix.Bit(0) ? 1 : 0]]); - break; - case 3: - pix = data & 0xaa; - pix = (((pix & 0x80) >> 7) | ((pix & 0x08) >> 2) | ((pix & 0x20) >> 3) | ((pix & 0x02) << 2)); - for (int c = 0; c < 4; c++) - CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix]]); - pix = data & 0x55; - pix = (((pix & 0x40) >> 6) | ((pix & 0x04) >> 1) | ((pix & 0x10) >> 2) | ((pix & 0x01) << 3)); - for (int c = 0; c < 4; c++) - CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix]]); - break; - } - - xA++; - xA &= 0x7ff; - } - - break; - } - } - - // add to the list - ScreenLines.Add(new CharacterLine - { - ScreenMode = CurrentLine.ScreenMode, - Phases = CurrentLine.Phases.ToList(), - Pixels = CurrentLine.Pixels.ToList() - }); - } - - #endregion - - #region Public Methods - - /// - /// Called when the Z80 acknowledges an interrupt - /// - public void IORQA() - { - // bit 5 of the interrupt counter is reset - InterruptCounter &= ~(1 << 5); - } - - private int slCounter = 0; - private int slBackup = 0; - - /// - /// Fired when the CRCT flags HSYNC - /// - public void OnHSYNC() - { - HSYNC = true; - slCounter++; - - // commit the scanline - RenderScanline(); - - // setup vid memory for next scanline - CalculateNextScreenMemory(); - - if (CRCT.VLC == 0) - { - // update screenmode - ScreenMode = _RMR & 0x03; - } - - // setup scanline for next - CurrentLine.Clear(ScreenMode); - } - - /// - /// Fired when the CRCT flags VSYNC - /// - public void OnVSYNC() - { - FrameEnd = true; - slBackup = slCounter; - slCounter = 0; - } - - #endregion - - #region IVideoProvider - - public int[] ScreenBuffer; - - private int _virtualWidth; - private int _virtualHeight; - private int _bufferWidth; - private int _bufferHeight; - - public int BackgroundColor - { - get { return CPCHardwarePalette[0]; } - } - - public int VirtualWidth - { - get { return _virtualWidth; } - set { _virtualWidth = value; } - } - - public int VirtualHeight - { - get { return _virtualHeight; } - set { _virtualHeight = value; } - } - - public int BufferWidth - { - get { return _bufferWidth; } - set { _bufferWidth = value; } - } - - public int BufferHeight - { - get { return _bufferHeight; } - set { _bufferHeight = value; } - } - - public int SysBufferWidth; - public int SysBufferHeight; - - public int VsyncNumerator - { - get { return 200000000; } - set { } - } - - public int VsyncDenominator - { - get { return Z80ClockSpeed; } - } - - public int[] GetVideoBuffer() - { - // get only lines that have pixel data - var lines = ScreenLines.Where(a => a.Pixels.Count > 0); - - int pos = 0; - int lCount = 0; - foreach (var l in lines) - { - var lCop = l.Pixels.ToList(); - var len = l.Pixels.Count; - if (l.Phases.Contains(Phase.VSYNC) && l.Phases.Contains(Phase.BORDER)) - continue; - - if (len < 320) - continue; - - var pad = BufferWidth - len; - if (pad < 0) - { - // trim the left and right - var padPos = pad * -1; - var excessL = padPos / 2; - var excessR = excessL + (padPos % 2); - for (int i = 0; i < excessL; i++) - { - var lThing = lCop.First(); - - lCop.Remove(lThing); - } - for (int i = 0; i < excessL; i++) - { - var lThing = lCop.Last(); - - lCop.Remove(lThing); - } - } - - var lPad = pad / 2; - var rPad = lPad + (pad % 2); - - for (int i = 0; i < 2; i++) - { - lCount++; - - for (int pL = 0; pL < lPad; pL++) - { - ScreenBuffer[pos++] = 0; - } - - for (int pix = 0; pix < lCop.Count; pix++) - { - ScreenBuffer[pos++] = lCop[pix]; - } - - for (int pR = 0; pR < rPad; pR++) - { - ScreenBuffer[pos++] = 0; - } - } - - if (lCount >= BufferHeight - 2) - { - break; - } - } - - ScreenLines.Clear(); - - return ScreenBuffer; + #endregion + + #region Clocks and Timing + + /// + /// The Gate Array Clock Speed + /// + public int GAClockSpeed = 16000000; + + /// + /// The CPU Clock Speed + /// + public int Z80ClockSpeed = 4000000; + + /// + /// CRCT Clock Speed + /// + public int CRCTClockSpeed = 1000000; + + /// + /// AY-3-8912 Clock Speed + /// + public int PSGClockSpeed = 1000000; + + /// + /// Number of CPU cycles in one frame + /// + public int FrameLength = 79872; + + /// + /// Number of Gate Array cycles within one frame + /// + public int GAFrameLength = 319488; + + #endregion + + #region Construction + + public AmstradGateArray(CPCBase machine, GateArrayType chipType) + { + _machine = machine; + ChipType = chipType; + //PenColours = new int[17]; + borderType = _machine.CPC.SyncSettings.BorderType; + SetupScreenSize(); + //Reset(); + + CRCT.AttachHSYNCCallback(OnHSYNC); + CRCT.AttachVSYNCCallback(OnVSYNC); + + CurrentLine = new CharacterLine(); + InitByteLookup(); + CalculateNextScreenMemory(); + } + + #endregion + + #region Registers and Internal State + + /// + /// PENR (register 0) - Pen Selection + /// This register can be used to select one of the 17 color-registers (pen 0 to 15 or the border). + /// It will remain selected until another PENR command is executed. + /// PENR Index + /// 7 6 5 4 3 2 1 0 color register selected + /// 0 0 0 0 n n n n pen n from 0 to 15 (4bits) + /// 0 0 0 1 x x x x border + /// + /// x can be 0 or 1, it doesn't matter + /// + private byte _PENR; + public byte PENR + { + get { return _PENR; } + set + { + _PENR = value; + if (_PENR.Bit(4)) + { + // border select + CurrentPen = 16; + } + else + { + // pen select + CurrentPen = _PENR & 0x0f; + } + } + } + + /// + /// 0-15: Pen Registers + /// 16: Border Colour + /// + public int[] ColourRegisters = new int[17]; + + /// + /// The currently selected Pen + /// + private int CurrentPen; + + /// + /// INKR (register 1) - Colour Selection + /// This register takes a 5bits parameter which is a color-code. This color-code range from 0 to 31 but there's only 27 differents colors + /// (because the Gate Array use a 3-states logic on the R,G and B signals, thus 3x3x3=27). + /// INKR Color + /// 7 6 5 4 3 2 1 0 + /// 0 1 0 n n n n n where n is a color code (5 bits) + /// + /// The PEN affected by the INKR command is updated (almost) immediatly + /// + private byte _INKR; + public byte INKR + { + get { return _INKR; } + set + { + _INKR = value; + ColourRegisters[CurrentPen] = _INKR & 0x1f; + } + } + + /// + /// RMR (register 2) - Select screen mode and ROM configuration + /// This register control the interrupt counter (reset), the upper and lower ROM paging and the video mode. + /// RMR Commands + /// 7 6 5 4 3 2 1 0 + /// 1 0 0 I UR LR VM--> + /// + /// I : if set (1), this will reset the interrupt counter + /// UR : Enable (0) or Disable (1) the upper ROM paging (&C000 to &FFFF). You can select which upper ROM with the I/O address &DF00 + /// LR : Enable (0) or Disable (1) the lower ROM paging + /// VM : Select the video mode 0,1,2 or 3 (it will take effect after the next HSync) + /// + private byte _RMR; + public byte RMR + { + get { return _RMR; } + set + { + _RMR = value; + //ScreenMode = _RMR & 0x03; + var sm = _RMR & 0x03; + if (sm != 1) + { + + } + + if ((_RMR & 0x08) != 0) + _machine.UpperROMPaged = false; + else + _machine.UpperROMPaged = true; + + if ((_RMR & 0x04) != 0) + _machine.LowerROMPaged = false; + else + _machine.LowerROMPaged = true; + + if (_RMR.Bit(4)) + { + // reset interrupt counter + InterruptCounter = 0; + } + } + } + + /// + /// RAMR (register 3) - RAM Banking + /// This register exists only in CPCs with 128K RAM (like the CPC 6128, or CPCs with Standard Memory Expansions) + /// Note: In the CPC 6128, the register is a separate PAL that assists the Gate Array chip + /// + /// Bit Value Function + /// 7 1 Gate Array function 3 + /// 6 1 + /// 5 b 64K bank number(0..7); always 0 on an unexpanded CPC6128, 0-7 on Standard Memory Expansions + /// 4 b + /// 3 b + /// 2 x RAM Config(0..7) + /// 1 x "" + /// 0 x "" + /// + /// The 3bit RAM Config value is used to access the second 64K of the total 128K RAM that is built into the CPC 6128 or the additional 64K-512K of standard memory expansions. + /// These contain up to eight 64K ram banks, which are selected with bit 3-5. A standard CPC 6128 only contains bank 0. Normally the register is set to 0, so that only the + /// first 64K RAM are used (identical to the CPC 464 and 664 models). The register can be used to select between the following eight predefined configurations only: + /// + /// -Address- 0 1 2 3 4 5 6 7 + /// 0000-3FFF RAM_0 RAM_0 RAM_4 RAM_0 RAM_0 RAM_0 RAM_0 RAM_0 + /// 4000-7FFF RAM_1 RAM_1 RAM_5 RAM_3 RAM_4 RAM_5 RAM_6 RAM_7 + /// 8000-BFFF RAM_2 RAM_2 RAM_6 RAM_2 RAM_2 RAM_2 RAM_2 RAM_2 + /// C000-FFFF RAM_3 RAM_7 RAM_7 RAM_7 RAM_3 RAM_3 RAM_3 RAM_3 + /// + /// The Video RAM is always located in the first 64K, VRAM is in no way affected by this register + /// + private byte _RAMR; + /// + /// This is actually implemented outside of here. These values do nothing. + /// + public byte RAMR + { + get { return _RAMR; } + set + { + _RAMR = value; + } + } + + /// + /// The selected screen mode (updated after every HSYNC) + /// + private int ScreenMode; + + /// + /// Simulates the internal 6bit INT counter + /// + private int _interruptCounter; + public int InterruptCounter + { + get { return _interruptCounter; } + set { _interruptCounter = value; } + } + + /// + /// Interrupts are delayed when a VSYNC occurs + /// + private int VSYNCDelay; + + /// + /// Signals that the frame end has been reached + /// + public bool FrameEnd; + + /// + /// Internal phase clock + /// + private int ClockCounter; + + /// + /// Master frame clock counter + /// + public int FrameClock; + + /// + /// Simulates the gate array memory /WAIT line + /// + private bool WaitLine; + + /// + /// 16-bit address - read from the CRCT + /// + private short _MA; + private short MA + { + get + { + _MA = CRCT.MA; + return _MA; + } + } + + /// + /// Set when the HSYNC signal is detected from the CRCT + /// + private bool HSYNC; + + // /// + // /// Is set when an initial HSYNC is seen from the CRCT + // /// On real hardware interrupt generation is based on the falling edge of the HSYNC signal + // /// So in this emulation, once the falling edge is detected, interrupt processing happens + // /// + // private bool HSYNC_falling; + + /// + /// Used to count HSYNCs during a VSYNC + /// + private int HSYNC_counter; + + /// + /// Set when the VSYNC signal is detected from the CRCT + /// + private bool VSYNC; + + /// + /// TRUE when the /INT pin is held low + /// + private bool InterruptRaised; + + /// + /// Counts the GA cycles that the /INT pin should be held low + /// + private int InterruptHoldCounter; + + /// + /// Set at the start of a new frame + /// + public bool IsNewFrame; + + /// + /// Set when a new line is beginning + /// + public bool IsNewLine; + + /// + /// Horizontal Character Counter + /// + private int HCC; + + /// + /// Vertical Line Counter + /// + private int VLC; + + /// + /// The first video byte fetched + /// + private byte VideoByte1; + + /// + /// The second video byte fetched + /// + private byte VideoByte2; + + #endregion + + #region Clock Business + + /// + /// Called every CPU cycle + /// In reality the GA is clocked at 16Mhz (4 times the frequency of the CPU) + /// Therefore this method has to take care of: + /// 4 GA cycles + /// 1 CRCT cycle every 4 calls + /// 1 PSG cycle every 4 calls + /// 1 CPU cycle (uncontended) + /// + public void ClockCycle() + { + // gatearray uses 4-phase clock to supply clocks to other devices + switch (ClockCounter) + { + case 0: + CRCT.ClockCycle(); + WaitLine = false; + break; + case 1: + WaitLine = true; + // detect new scanline and upcoming new frame on next render cycle + //FrameDetector(); + break; + case 2: + // video fetch + WaitLine = true; + //FetchByte(1); + break; + case 3: + // video fetch and render + WaitLine = true; + //FetchByte(2); + GACharacterCycle(); + //PixelGenerator(); + break; + } + + if (!HSYNC && CRCT.HSYNC) + { + HSYNC = true; + } + + // run the interrupt generator routine + InterruptGenerator(); + + if (!CRCT.HSYNC) + { + HSYNC = false; + } + + // conditional CPU cycle + DoConditionalCPUCycle(); + + AdvanceClock(); + } + + /// + /// Increments the internal clock counters by one + /// + private void AdvanceClock() + { + FrameClock++; + ClockCounter++; + + if (ClockCounter == 4) + ClockCounter = 0; + + // check for frame end + if (FrameClock == FrameLength) + { + FrameEnd = true; + } + } + + /// + /// Runs a 4 Mhz CPU cycle if neccessary + /// /WAIT line status is a factor here + /// + private void DoConditionalCPUCycle() + { + if (!WaitLine) + { + // /WAIT line is NOT active + CPU.ExecuteOne(); + return; + } + + // /WAIT line is active + switch (ClockCounter) + { + case 2: + case 3: + // gate array video fetch is occuring + // check for memory access + if (BUSRQ > 0) + { + // memory action upcoming - CPU clock is halted + CPU.TotalExecutedCycles++; + } + break; + + case 1: + // CPU accesses RAM if it's performing a non-opcode read or write + // assume for now that an opcode fetch is always looking at PC + if (BUSRQ == PCh) + { + // opcode fetch memory action upcoming - CPU clock is halted + CPU.TotalExecutedCycles++; + } + else + { + // no fetch, or non-opcode fetch + CPU.ExecuteOne(); + } + break; + } + } + + #endregion + + #region Frame & Interrupt Handling + + /// + /// The CRCT builds the picture in a strange way, so that the top left of the display area is the first pixel from + /// video RAM. The borders come either side of the HSYNC and VSYNCs later on: + /// https://web.archive.org/web/20170501112330im_/http://www.grimware.org/lib/exe/fetch.php/documentations/devices/crtc.6845/crtc.standard.video.frame.png?w=800&h=500 + /// Therefore when the gate array initialises, we will attempt end the frame early in order to + /// sync up at the point where VSYNC is active and HSYNC just begins. This is roughly how a CRT monitor would display the picture. + /// The CRT would start a new line at the point where an HSYNC is detected. + /// + private void FrameDetector() + { + if (CRCT.HSYNC && !IsNewLine) + { + // start of a new line on the next render cycle + IsNewLine = true; + + // process scanline + //CRT.CurrentLine.CommitScanline(); + + // check for end of frame + if (CRCT.VSYNC && !IsNewFrame) + { + // start of a new frame on the next render cycle + IsNewFrame = true; + //FrameEnd = true; + VLC = 0; + } + else if (!CRCT.VSYNC) + { + // increment line counter + VLC++; + IsNewFrame = false; + } + + HCC = 0; + + // update screenmode + //ScreenMode = RMR & 0x03; + //CRT.CurrentLine.InitScanline(ScreenMode, VLC); + } + else if (!CRCT.HSYNC) + { + // reset the flags + IsNewLine = false; + IsNewFrame = false; + } + } + + /// + /// Handles interrupt generation + /// + private void InterruptGenerator() + { + if (HSYNC && !CRCT.HSYNC) + { + // falling edge of the HSYNC detected + InterruptCounter++; + + if (CRCT.VSYNC) + { + if (HSYNC_counter >= 2) + { + // x2 HSYNC have happened during VSYNC + if (InterruptCounter >= 32) + { + // no interrupt + InterruptCounter = 0; + } + else if (InterruptCounter < 32) + { + // interrupt + InterruptRaised = true; + InterruptCounter = 0; + } + + HSYNC_counter = 0; + } + else + { + HSYNC_counter++; + } + } + + if (InterruptCounter == 52) + { + // gatearray should raise an interrupt + InterruptRaised = true; + InterruptCounter = 0; + } + } + + if (InterruptRaised) + { + // interrupt should been raised + CPU.FlagI = true; + InterruptHoldCounter++; + + // the INT signal should be held low for 1.4us. + // in gatearray cycles, this equates to 22.4 + // we will round down to 22 for emulation purposes + if (InterruptHoldCounter >= 22) + { + CPU.FlagI = false; + InterruptRaised = false; + InterruptHoldCounter = 0; + } + } + } + + #endregion + + #region Rendering Business + + /// + /// Builds up current scanline character information + /// Ther GA modifies HSYNC and VSYNC signals before they are sent to the monitor + /// This is handled here + /// Runs at 1Mhz + /// + private void GACharacterCycle() + { + if (CRCT.VSYNC && CRCT.HSYNC) + { + // both hsync and vsync active + CurrentLine.AddCharacter(Phase.HSYNCandVSYNC); + } + else if (CRCT.VSYNC) + { + // vsync is active but hsync is not + CurrentLine.AddCharacter(Phase.VSYNC); + } + else if (CRCT.HSYNC) + { + // hsync is active but vsync is not + CurrentLine.AddCharacter(Phase.HSYNC); + } + else if (!CRCT.DISPTMG) + { + // border generation + CurrentLine.AddCharacter(Phase.BORDER); + } + else if (CRCT.DISPTMG) + { + // pixels generated from video RAM + CurrentLine.AddCharacter(Phase.DISPLAY); + } + } + + /// + /// Holds the upcoming video RAM addresses for the next scanline + /// Firmware default size is 80 (40 characters - 2 bytes per character) + /// + private ushort[] NextVidRamLine = new ushort[40 * 2]; + + /// + /// The current character line we are working from + /// + private CharacterLine CurrentLine; + + /// + /// List of screen lines as they are built up + /// + private List ScreenLines = new List(); + + /// + /// Pixel value lookups for every scanline byte value + /// Based on the lookup at https://github.com/gavinpugh/xnacpc + /// + private int[][] ByteLookup = new int[4][]; + private void InitByteLookup() + { + int pix; + for (int m = 0; m < 4; m++) + { + int pos = 0; + ByteLookup[m] = new int[256 * 8]; + for (int b = 0; b < 256; b++) + { + switch (m) + { + case 0: + pix = b & 0xaa; + pix = (((pix & 0x80) >> 7) | ((pix & 0x08) >> 2) | ((pix & 0x20) >> 3) | ((pix & 0x02) << 2)); + for (int c = 0; c < 4; c++) + ByteLookup[m][pos++] = pix; + pix = b & 0x55; + pix = (((pix & 0x40) >> 6) | ((pix & 0x04) >> 1) | ((pix & 0x10) >> 2) | ((pix & 0x01) << 3)); + for (int c = 0; c < 4; c++) + ByteLookup[m][pos++] = pix; + break; + case 1: + pix = (((b & 0x80) >> 7) | ((b & 0x08) >> 2)); + ByteLookup[m][pos++] = pix; + ByteLookup[m][pos++] = pix; + pix = (((b & 0x40) >> 6) | ((b & 0x04) >> 1)); + ByteLookup[m][pos++] = pix; + ByteLookup[m][pos++] = pix; + pix = (((b & 0x20) >> 5) | (b & 0x02)); + ByteLookup[m][pos++] = pix; + ByteLookup[m][pos++] = pix; + pix = (((b & 0x10) >> 4) | ((b & 0x01) << 1)); + ByteLookup[m][pos++] = pix; + ByteLookup[m][pos++] = pix; + break; + case 2: + for (int i = 7; i >= 0; i--) + ByteLookup[m][pos++] = ((b & (1 << i)) != 0) ? 1 : 0; + break; + case 3: + pix = b & 0xaa; + pix = (((pix & 0x80) >> 7) | ((pix & 0x08) >> 2) | ((pix & 0x20) >> 3) | ((pix & 0x02) << 2)); + for (int c = 0; c < 4; c++) + ByteLookup[m][pos++] = pix; + pix = b & 0x55; + pix = (((pix & 0x40) >> 6) | ((pix & 0x04) >> 1) | ((pix & 0x10) >> 2) | ((pix & 0x01) << 3)); + for (int c = 0; c < 4; c++) + ByteLookup[m][pos++] = pix; + break; + } + } + } + } + + /// + /// Runs at HSYNC *AFTER* the scanline has been commmitted + /// Sets up the upcoming memory addresses for the next scanline + /// + private void CalculateNextScreenMemory() + { + var verCharCount = CRCT.VCC; + var verRasCount = CRCT.VLC; + + var screenWidthByteCount = CRCT.DisplayWidth * 2; + NextVidRamLine = new ushort[screenWidthByteCount * 2]; + var screenHeightCharCount = CRCT.DisplayHeightInChars; + var screenAddress = CRCT.MA; + + int baseAddress = ((screenAddress << 2) & 0xf000); + int offset = (screenAddress * 2) & 0x7ff; + + int x = offset + ((verCharCount * screenWidthByteCount) & 0x7ff); + int y = baseAddress + (verRasCount * 0x800); + + for (int b = 0; b < screenWidthByteCount; b++) + { + NextVidRamLine[b] = (ushort)(y + x); + x++; + x &= 0x7ff; + } + } + + /// + /// Called at the start of HSYNC, this renders the currently built-up scanline + /// + private void RenderScanline() + { + // memory addresses + int cRow = CRCT.VCC; + int cRas = CRCT.VLC; + + int screenByteWidth = CRCT.DisplayWidth * 2; + var screenHeightCharCount = CRCT.DisplayHeightInChars; + //CalculateNextScreenMemory(); + var crctAddr = CRCT.DStartHigh << 8; + crctAddr |= CRCT.DStartLow; + var baseAddr = ((crctAddr << 2) & (0xF000)); //CRCT.VideoPageBase;// + var baseOffset = (crctAddr * 2) & 0x7FF; //CRCT.VideoRAMOffset * 2; // + var xA = baseOffset + ((cRow * screenByteWidth) & 0x7ff); + var yA = baseAddr + (cRas * 2048); + + // border and display + int pix = 0; + int scrByte = 0; + + for (int i = 0; i < CurrentLine.PhaseCount; i++) + { + // every character renders 8 pixels + switch (CurrentLine.Phases[i]) + { + case Phase.NONE: + break; + + case Phase.HSYNC: + break; + case Phase.HSYNCandVSYNC: + break; + case Phase.VSYNC: + break; + case Phase.BORDER: + // output current border colour + for (pix = 0; pix < 16; pix++) + { + CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[16]]); + } + break; + case Phase.DISPLAY: + // each character references 2 bytes in video RAM + byte data; + + for (int by = 0; by < 2; by++) + { + ushort addr = (ushort)(yA + xA); + data = _machine.FetchScreenMemory(addr); + scrByte++; + + switch (CurrentLine.ScreenMode) + { + case 0: + pix = data & 0xaa; + pix = (((pix & 0x80) >> 7) | ((pix & 0x08) >> 2) | ((pix & 0x20) >> 3) | ((pix & 0x02) << 2)); + for (int c = 0; c < 4; c++) + CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix]]); + pix = data & 0x55; + pix = (((pix & 0x40) >> 6) | ((pix & 0x04) >> 1) | ((pix & 0x10) >> 2) | ((pix & 0x01) << 3)); + for (int c = 0; c < 4; c++) + CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix]]); + break; + case 1: + pix = (((data & 0x80) >> 7) | ((data & 0x08) >> 2)); + CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix]]); + CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix]]); + pix = (((data & 0x40) >> 6) | ((data & 0x04) >> 1)); + CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix]]); + CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix]]); + pix = (((data & 0x20) >> 5) | (data & 0x02)); + CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix]]); + CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix]]); + pix = (((data & 0x10) >> 4) | ((data & 0x01) << 1)); + CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix]]); + CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix]]); + break; + case 2: + pix = data; + CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix.Bit(7) ? 1 : 0]]); + CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix.Bit(6) ? 1 : 0]]); + CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix.Bit(5) ? 1 : 0]]); + CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix.Bit(4) ? 1 : 0]]); + CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix.Bit(3) ? 1 : 0]]); + CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix.Bit(2) ? 1 : 0]]); + CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix.Bit(1) ? 1 : 0]]); + CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix.Bit(0) ? 1 : 0]]); + break; + case 3: + pix = data & 0xaa; + pix = (((pix & 0x80) >> 7) | ((pix & 0x08) >> 2) | ((pix & 0x20) >> 3) | ((pix & 0x02) << 2)); + for (int c = 0; c < 4; c++) + CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix]]); + pix = data & 0x55; + pix = (((pix & 0x40) >> 6) | ((pix & 0x04) >> 1) | ((pix & 0x10) >> 2) | ((pix & 0x01) << 3)); + for (int c = 0; c < 4; c++) + CurrentLine.Pixels.Add(CPCHardwarePalette[ColourRegisters[pix]]); + break; + } + + xA++; + xA &= 0x7ff; + } + + break; + } + } + + // add to the list + ScreenLines.Add(new CharacterLine + { + ScreenMode = CurrentLine.ScreenMode, + Phases = CurrentLine.Phases.ToList(), + Pixels = CurrentLine.Pixels.ToList() + }); + } + + #endregion + + #region Public Methods + + /// + /// Called when the Z80 acknowledges an interrupt + /// + public void IORQA() + { + // bit 5 of the interrupt counter is reset + InterruptCounter &= ~(1 << 5); + } + + private int slCounter = 0; + private int slBackup = 0; + + /// + /// Fired when the CRCT flags HSYNC + /// + public void OnHSYNC() + { + HSYNC = true; + slCounter++; + + // commit the scanline + RenderScanline(); + + // setup vid memory for next scanline + CalculateNextScreenMemory(); + + if (CRCT.VLC == 0) + { + // update screenmode + ScreenMode = _RMR & 0x03; + } + + // setup scanline for next + CurrentLine.Clear(ScreenMode); + } + + /// + /// Fired when the CRCT flags VSYNC + /// + public void OnVSYNC() + { + FrameEnd = true; + slBackup = slCounter; + slCounter = 0; + } + + #endregion + + #region IVideoProvider + + public int[] ScreenBuffer; + + private int _virtualWidth; + private int _virtualHeight; + private int _bufferWidth; + private int _bufferHeight; + + public int BackgroundColor + { + get { return CPCHardwarePalette[0]; } + } + + public int VirtualWidth + { + get { return _virtualWidth; } + set { _virtualWidth = value; } + } + + public int VirtualHeight + { + get { return _virtualHeight; } + set { _virtualHeight = value; } + } + + public int BufferWidth + { + get { return _bufferWidth; } + set { _bufferWidth = value; } + } + + public int BufferHeight + { + get { return _bufferHeight; } + set { _bufferHeight = value; } + } + + public int SysBufferWidth; + public int SysBufferHeight; + + public int VsyncNumerator + { + get { return 200000000; } + set { } + } + + public int VsyncDenominator + { + get { return Z80ClockSpeed; } + } + + public int[] GetVideoBuffer() + { + // get only lines that have pixel data + var lines = ScreenLines.Where(a => a.Pixels.Count > 0); + + int pos = 0; + int lCount = 0; + foreach (var l in lines) + { + var lCop = l.Pixels.ToList(); + var len = l.Pixels.Count; + if (l.Phases.Contains(Phase.VSYNC) && l.Phases.Contains(Phase.BORDER)) + continue; + + if (len < 320) + continue; + + var pad = BufferWidth - len; + if (pad < 0) + { + // trim the left and right + var padPos = pad * -1; + var excessL = padPos / 2; + var excessR = excessL + (padPos % 2); + for (int i = 0; i < excessL; i++) + { + var lThing = lCop.First(); + + lCop.Remove(lThing); + } + for (int i = 0; i < excessL; i++) + { + var lThing = lCop.Last(); + + lCop.Remove(lThing); + } + } + + var lPad = pad / 2; + var rPad = lPad + (pad % 2); + + for (int i = 0; i < 2; i++) + { + lCount++; + + for (int pL = 0; pL < lPad; pL++) + { + ScreenBuffer[pos++] = 0; + } + + for (int pix = 0; pix < lCop.Count; pix++) + { + ScreenBuffer[pos++] = lCop[pix]; + } + + for (int pR = 0; pR < rPad; pR++) + { + ScreenBuffer[pos++] = 0; + } + } + + if (lCount >= BufferHeight - 2) + { + break; + } + } + + ScreenLines.Clear(); + + return ScreenBuffer; /* switch (borderType) { @@ -1151,249 +1151,249 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC } public void SetupScreenSize() - { - SysBufferWidth = 800; - SysBufferHeight = 600; - BufferHeight = SysBufferHeight; - BufferWidth = SysBufferWidth; - VirtualHeight = BufferHeight; - VirtualWidth = BufferWidth; - ScreenBuffer = new int[BufferWidth * BufferHeight]; - croppedBuffer = ScreenBuffer; + { + SysBufferWidth = 800; + SysBufferHeight = 600; + BufferHeight = SysBufferHeight; + BufferWidth = SysBufferWidth; + VirtualHeight = BufferHeight; + VirtualWidth = BufferWidth; + ScreenBuffer = new int[BufferWidth * BufferHeight]; + croppedBuffer = ScreenBuffer; - switch (borderType) - { - case AmstradCPC.BorderType.Uncropped: - break; + switch (borderType) + { + case AmstradCPC.BorderType.Uncropped: + break; - case AmstradCPC.BorderType.Uniform: - BufferWidth = 800; - BufferHeight = 600; - VirtualHeight = BufferHeight; - VirtualWidth = BufferWidth; - croppedBuffer = new int[BufferWidth * BufferHeight]; - break; + case AmstradCPC.BorderType.Uniform: + BufferWidth = 800; + BufferHeight = 600; + VirtualHeight = BufferHeight; + VirtualWidth = BufferWidth; + croppedBuffer = new int[BufferWidth * BufferHeight]; + break; - case AmstradCPC.BorderType.Widescreen: - break; - } - } + case AmstradCPC.BorderType.Widescreen: + break; + } + } - protected int[] croppedBuffer; + protected int[] croppedBuffer; - private AmstradCPC.BorderType _borderType; + private AmstradCPC.BorderType _borderType; - public AmstradCPC.BorderType borderType - { - get { return _borderType; } - set { _borderType = value; } - } + public AmstradCPC.BorderType borderType + { + get { return _borderType; } + set { _borderType = value; } + } - #endregion + #endregion - #region IPortIODevice + #region IPortIODevice - /// - /// Device responds to an IN instruction - /// - public bool ReadPort(ushort port, ref int result) - { - // gate array is OUT only - return false; - } + /// + /// Device responds to an IN instruction + /// + public bool ReadPort(ushort port, ref int result) + { + // gate array is OUT only + return false; + } - /// - /// Device responds to an OUT instruction - /// - public bool WritePort(ushort port, int result) - { - BitArray portBits = new BitArray(BitConverter.GetBytes(port)); - BitArray dataBits = new BitArray(BitConverter.GetBytes((byte)result)); - byte portUpper = (byte)(port >> 8); - byte portLower = (byte)(port & 0xff); + /// + /// Device responds to an OUT instruction + /// + public bool WritePort(ushort port, int result) + { + BitArray portBits = new BitArray(BitConverter.GetBytes(port)); + BitArray dataBits = new BitArray(BitConverter.GetBytes((byte)result)); + byte portUpper = (byte)(port >> 8); + byte portLower = (byte)(port & 0xff); - // The gate array is selected when bit 15 of the I/O port address is set to "0" and bit 14 of the I/O port address is set to "1" - bool accessed = false; - if (!portUpper.Bit(7) && portUpper.Bit(6)) - accessed = true; + // The gate array is selected when bit 15 of the I/O port address is set to "0" and bit 14 of the I/O port address is set to "1" + bool accessed = false; + if (!portUpper.Bit(7) && portUpper.Bit(6)) + accessed = true; - if (!accessed) - return accessed; + if (!accessed) + return accessed; - // Bit 9 and 8 of the data byte define the function to access - if (!dataBits[6] && !dataBits[7]) - { - // select pen - PENR = (byte)result; - } + // Bit 9 and 8 of the data byte define the function to access + if (!dataBits[6] && !dataBits[7]) + { + // select pen + PENR = (byte)result; + } - if (dataBits[6] && !dataBits[7]) - { - // select colour for selected pen - INKR = (byte)result; - } + if (dataBits[6] && !dataBits[7]) + { + // select colour for selected pen + INKR = (byte)result; + } - if (!dataBits[6] && dataBits[7]) - { - // select screen mode, ROM configuration and interrupt control - RMR = (byte)result; - } + if (!dataBits[6] && dataBits[7]) + { + // select screen mode, ROM configuration and interrupt control + RMR = (byte)result; + } - if (dataBits[6] && dataBits[7]) - { - // RAM memory management - RAMR = (byte)result; - } + if (dataBits[6] && dataBits[7]) + { + // RAM memory management + RAMR = (byte)result; + } - return true; - } + return true; + } - #endregion + #endregion - #region Serialization + #region Serialization - public void SyncState(Serializer ser) - { - ser.BeginSection("GateArray"); - ser.SyncEnum(nameof(ChipType), ref ChipType); - ser.Sync(nameof(_PENR), ref _PENR); - ser.Sync(nameof(_INKR), ref _INKR); - ser.Sync(nameof(_RMR), ref _RMR); - ser.Sync(nameof(_RAMR), ref _RAMR); - ser.Sync(nameof(ColourRegisters), ref ColourRegisters, false); - ser.Sync(nameof(CurrentPen), ref CurrentPen); - ser.Sync(nameof(ClockCounter), ref ClockCounter); - ser.Sync(nameof(FrameClock), ref FrameClock); - ser.Sync(nameof(FrameEnd), ref FrameEnd); - ser.Sync(nameof(WaitLine), ref WaitLine); - ser.Sync(nameof(_interruptCounter), ref _interruptCounter); - ser.Sync(nameof(VSYNCDelay), ref VSYNCDelay); - ser.Sync(nameof(ScreenMode), ref ScreenMode); - ser.Sync(nameof(HSYNC), ref HSYNC); - //ser.Sync(nameof(HSYNC_falling), ref HSYNC_falling); - ser.Sync(nameof(HSYNC_counter), ref HSYNC_counter); - ser.Sync(nameof(VSYNC), ref VSYNC); - ser.Sync(nameof(InterruptRaised), ref InterruptRaised); - ser.Sync(nameof(InterruptHoldCounter), ref InterruptHoldCounter); - ser.Sync(nameof(_MA), ref _MA); - ser.Sync(nameof(IsNewFrame), ref IsNewFrame); - ser.Sync(nameof(IsNewLine), ref IsNewLine); - ser.Sync(nameof(HCC), ref HCC); - ser.Sync(nameof(VLC), ref VLC); - ser.Sync(nameof(VideoByte1), ref VideoByte1); - ser.Sync(nameof(VideoByte2), ref VideoByte2); - ser.Sync(nameof(NextVidRamLine), ref NextVidRamLine, false); - ser.EndSection(); - } + public void SyncState(Serializer ser) + { + ser.BeginSection("GateArray"); + ser.SyncEnum(nameof(ChipType), ref ChipType); + ser.Sync(nameof(_PENR), ref _PENR); + ser.Sync(nameof(_INKR), ref _INKR); + ser.Sync(nameof(_RMR), ref _RMR); + ser.Sync(nameof(_RAMR), ref _RAMR); + ser.Sync(nameof(ColourRegisters), ref ColourRegisters, false); + ser.Sync(nameof(CurrentPen), ref CurrentPen); + ser.Sync(nameof(ClockCounter), ref ClockCounter); + ser.Sync(nameof(FrameClock), ref FrameClock); + ser.Sync(nameof(FrameEnd), ref FrameEnd); + ser.Sync(nameof(WaitLine), ref WaitLine); + ser.Sync(nameof(_interruptCounter), ref _interruptCounter); + ser.Sync(nameof(VSYNCDelay), ref VSYNCDelay); + ser.Sync(nameof(ScreenMode), ref ScreenMode); + ser.Sync(nameof(HSYNC), ref HSYNC); + //ser.Sync(nameof(HSYNC_falling), ref HSYNC_falling); + ser.Sync(nameof(HSYNC_counter), ref HSYNC_counter); + ser.Sync(nameof(VSYNC), ref VSYNC); + ser.Sync(nameof(InterruptRaised), ref InterruptRaised); + ser.Sync(nameof(InterruptHoldCounter), ref InterruptHoldCounter); + ser.Sync(nameof(_MA), ref _MA); + ser.Sync(nameof(IsNewFrame), ref IsNewFrame); + ser.Sync(nameof(IsNewLine), ref IsNewLine); + ser.Sync(nameof(HCC), ref HCC); + ser.Sync(nameof(VLC), ref VLC); + ser.Sync(nameof(VideoByte1), ref VideoByte1); + ser.Sync(nameof(VideoByte2), ref VideoByte2); + ser.Sync(nameof(NextVidRamLine), ref NextVidRamLine, false); + ser.EndSection(); + } - #endregion + #endregion - #region Enums, Classes & Lookups + #region Enums, Classes & Lookups - /// - /// Represents a single scanline (in characters) - /// - public class CharacterLine - { - /// - /// Screenmode is defined at each HSYNC (start of a new character line) - /// Therefore we pass the mode in via constructor - /// - //public CharacterLine(int screenMode) - //{ - //ScreenMode = screenMode; - //} + /// + /// Represents a single scanline (in characters) + /// + public class CharacterLine + { + /// + /// Screenmode is defined at each HSYNC (start of a new character line) + /// Therefore we pass the mode in via constructor + /// + //public CharacterLine(int screenMode) + //{ + //ScreenMode = screenMode; + //} - public int ScreenMode = 1; - public List Phases = new List(); - public List Pixels = new List(); + public int ScreenMode = 1; + public List Phases = new List(); + public List Pixels = new List(); - /// - /// Adds a new horizontal character to the list - /// - public void AddCharacter(Phase phase) - { - Phases.Add(phase); - } + /// + /// Adds a new horizontal character to the list + /// + public void AddCharacter(Phase phase) + { + Phases.Add(phase); + } - public int PhaseCount - { - get { return Phases.Count(); } - } + public int PhaseCount + { + get { return Phases.Count(); } + } - public void Clear(int screenMode) - { - ScreenMode = screenMode; - Phases.Clear(); - Pixels.Clear(); - } - } + public void Clear(int screenMode) + { + ScreenMode = screenMode; + Phases.Clear(); + Pixels.Clear(); + } + } - [Flags] - public enum Phase : int - { - /// - /// Nothing - /// - NONE = 0, - /// - /// Border is being rendered - /// - BORDER = 1, - /// - /// Display rendered from video RAM - /// - DISPLAY = 2, - /// - /// HSYNC in progress - /// - HSYNC = 3, - /// - /// VSYNC in process - /// - VSYNC = 4, - /// - /// HSYNC occurs within a VSYNC - /// - HSYNCandVSYNC = 5 - } + [Flags] + public enum Phase : int + { + /// + /// Nothing + /// + NONE = 0, + /// + /// Border is being rendered + /// + BORDER = 1, + /// + /// Display rendered from video RAM + /// + DISPLAY = 2, + /// + /// HSYNC in progress + /// + HSYNC = 3, + /// + /// VSYNC in process + /// + VSYNC = 4, + /// + /// HSYNC occurs within a VSYNC + /// + HSYNCandVSYNC = 5 + } - public enum GateArrayType - { - /// - /// CPC 464 - /// The first version of the Gate Array is the 40007 and was released with the CPC 464 - /// - Amstrad40007, - /// - /// CPC 664 - /// Later, the CPC 664 came out fitted with the 40008 version (and at the same time, the CPC 464 was also upgraded with this version). - /// This version is pinout incompatible with the 40007 (that's why the upgraded 464 of this period have two Gate Array slots on the motherboard, - /// one for a 40007 and one for a 40008) - /// - Amstrad40008, - /// - /// CPC 6128 - /// The CPC 6128 was released with the 40010 version (and the CPC 464 and 664 manufactured at that time were also upgraded to this version). - /// The 40010 is pinout compatible with the previous 40008 - /// - Amstrad40010, - /// - /// Costdown CPC - /// In the last serie of CPC 464 and 6128 produced by Amstrad in 1988, a small ASIC chip have been used to reduce the manufacturing costs. - /// This ASIC emulates the Gate Array, the PAL and the CRTC 6845. And no, there is no extra features like on the Amstrad Plus. - /// The only noticeable difference seems to be about the RGB output levels which are not exactly the same than those produced with a real Gate Array - /// - Amstrad40226, - /// - /// Plus & GX-4000 - /// All the Plus range is built upon a bigger ASIC chip which is integrating many features of the classic CPC (FDC, CRTC, PPI, Gate Array/PAL) and all - /// the new Plus specific features. The Gate Array on the Plus have a new register, named RMR2, to expand the ROM mapping functionnalities of the machine. - /// This register requires to be unlocked first to be available. And finally, the RGB levels produced by the ASIC on the Plus are noticeably differents - /// - Amstrad40489, - } + public enum GateArrayType + { + /// + /// CPC 464 + /// The first version of the Gate Array is the 40007 and was released with the CPC 464 + /// + Amstrad40007, + /// + /// CPC 664 + /// Later, the CPC 664 came out fitted with the 40008 version (and at the same time, the CPC 464 was also upgraded with this version). + /// This version is pinout incompatible with the 40007 (that's why the upgraded 464 of this period have two Gate Array slots on the motherboard, + /// one for a 40007 and one for a 40008) + /// + Amstrad40008, + /// + /// CPC 6128 + /// The CPC 6128 was released with the 40010 version (and the CPC 464 and 664 manufactured at that time were also upgraded to this version). + /// The 40010 is pinout compatible with the previous 40008 + /// + Amstrad40010, + /// + /// Costdown CPC + /// In the last serie of CPC 464 and 6128 produced by Amstrad in 1988, a small ASIC chip have been used to reduce the manufacturing costs. + /// This ASIC emulates the Gate Array, the PAL and the CRTC 6845. And no, there is no extra features like on the Amstrad Plus. + /// The only noticeable difference seems to be about the RGB output levels which are not exactly the same than those produced with a real Gate Array + /// + Amstrad40226, + /// + /// Plus & GX-4000 + /// All the Plus range is built upon a bigger ASIC chip which is integrating many features of the classic CPC (FDC, CRTC, PPI, Gate Array/PAL) and all + /// the new Plus specific features. The Gate Array on the Plus have a new register, named RMR2, to expand the ROM mapping functionnalities of the machine. + /// This register requires to be unlocked first to be available. And finally, the RGB levels produced by the ASIC on the Plus are noticeably differents + /// + Amstrad40489, + } - #endregion - } + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Display/CRCT_6845.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Display/CRCT_6845.cs index eb8e17b87d..61119c3748 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Display/CRCT_6845.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Display/CRCT_6845.cs @@ -5,303 +5,303 @@ using System.Collections; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Cathode Ray Tube Controller Chip - 6845 - /// http://www.cpcwiki.eu/index.php/CRTC - /// https://web.archive.org/web/20170501112330/http://www.grimware.org/doku.php/documentations/devices/crtc - /// - public class CRCT_6845 : IPortIODevice - { - #region Devices + /// + /// Cathode Ray Tube Controller Chip - 6845 + /// http://www.cpcwiki.eu/index.php/CRTC + /// https://web.archive.org/web/20170501112330/http://www.grimware.org/doku.php/documentations/devices/crtc + /// + public class CRCT_6845 : IPortIODevice + { + #region Devices - private CPCBase _machine { get; set; } - private CRCTType ChipType; + private CPCBase _machine { get; set; } + private CRCTType ChipType; - #endregion + #endregion - #region CallBacks + #region CallBacks - public delegate void CallBack(); + public delegate void CallBack(); - private CallBack HSYNC_Callbacks; - private CallBack VSYNC_Callbacks; + private CallBack HSYNC_Callbacks; + private CallBack VSYNC_Callbacks; - public void AttachVSYNCCallback(CallBack vCall) - { - VSYNC_Callbacks += vCall; - } + public void AttachVSYNCCallback(CallBack vCall) + { + VSYNC_Callbacks += vCall; + } - public void AttachHSYNCCallback(CallBack hCall) - { - HSYNC_Callbacks += hCall; - } + public void AttachHSYNCCallback(CallBack hCall) + { + HSYNC_Callbacks += hCall; + } - #endregion + #endregion - #region Construction + #region Construction - public CRCT_6845(CRCTType chipType, CPCBase machine) - { - _machine = machine; - ChipType = chipType; - Reset(); - } + public CRCT_6845(CRCTType chipType, CPCBase machine) + { + _machine = machine; + ChipType = chipType; + Reset(); + } - private const int WRITE = 0; - private const int READ = 1; + private const int WRITE = 0; + private const int READ = 1; - #endregion + #endregion - #region Public Lines + #region Public Lines - /// - /// Denotes that HSYNC is active - /// - public bool HSYNC = false; + /// + /// Denotes that HSYNC is active + /// + public bool HSYNC = false; - /// - /// Denotes that VSYNC is active - /// - public bool VSYNC = false; + /// + /// Denotes that VSYNC is active + /// + public bool VSYNC = false; - /// - /// TRUE: bits outputted to screen from video RAM - /// FALSE: current border colour is outputted - /// - public bool DISPTMG = true; + /// + /// TRUE: bits outputted to screen from video RAM + /// FALSE: current border colour is outputted + /// + public bool DISPTMG = true; - /// - /// 16-bit memory address lines - /// The gate array uses this to grab the correct bits from video RAM - /// - public short MA; + /// + /// 16-bit memory address lines + /// The gate array uses this to grab the correct bits from video RAM + /// + public short MA; - /// - /// Vertical Character Count - /// - public int VCC; + /// + /// Vertical Character Count + /// + public int VCC; - /// - /// Vertical Scanline Count (within the current vertical character) - /// - public int VLC; + /// + /// Vertical Scanline Count (within the current vertical character) + /// + public int VLC; - #endregion + #endregion - #region Public Lookups + #region Public Lookups - /* + /* * These are not accessible directlyon real hardware * It just makes screen generation easier to have these accessbile from the gate array */ - /// - /// The total frame width (in characters) - /// - public int FrameWidth - { - get - { - return (int)Regs[HOR_TOTAL] + 1; - } - } + /// + /// The total frame width (in characters) + /// + public int FrameWidth + { + get + { + return (int)Regs[HOR_TOTAL] + 1; + } + } - /// - /// The total frame height (in scanlines) - /// - public int FrameHeight - { - get - { - return ((int)Regs[VER_TOTAL] + 1) * ((int)Regs[MAX_RASTER_ADDR] + 1); - } - } + /// + /// The total frame height (in scanlines) + /// + public int FrameHeight + { + get + { + return ((int)Regs[VER_TOTAL] + 1) * ((int)Regs[MAX_RASTER_ADDR] + 1); + } + } - /// - /// The total frame height (in scanlines) - /// - public int FrameHeightInChars - { - get - { - return ((int)Regs[VER_TOTAL] + 1); - } - } + /// + /// The total frame height (in scanlines) + /// + public int FrameHeightInChars + { + get + { + return ((int)Regs[VER_TOTAL] + 1); + } + } - /// - /// The width of the display area (in characters) - /// - public int DisplayWidth - { - get - { - return (int)Regs[HOR_DISPLAYED]; - } - } + /// + /// The width of the display area (in characters) + /// + public int DisplayWidth + { + get + { + return (int)Regs[HOR_DISPLAYED]; + } + } - /// - /// The width of the display area (in scanlines) - /// - public int DisplayHeight - { - get - { - return (int)Regs[VER_DISPLAYED] * ((int)Regs[MAX_RASTER_ADDR] + 1); - } - } + /// + /// The width of the display area (in scanlines) + /// + public int DisplayHeight + { + get + { + return (int)Regs[VER_DISPLAYED] * ((int)Regs[MAX_RASTER_ADDR] + 1); + } + } - /// - /// The width of the display area (in scanlines) - /// - public int DisplayHeightInChars - { - get - { - return (int)Regs[VER_DISPLAYED]; - } - } + /// + /// The width of the display area (in scanlines) + /// + public int DisplayHeightInChars + { + get + { + return (int)Regs[VER_DISPLAYED]; + } + } - /// - /// The character at which to start HSYNC - /// - public int HorizontalSyncPos - { - get - { - return (int)Regs[HOR_SYNC_POS]; - } - } + /// + /// The character at which to start HSYNC + /// + public int HorizontalSyncPos + { + get + { + return (int)Regs[HOR_SYNC_POS]; + } + } - /// - /// Width (in characters) of the HSYNC - /// - public int HorizontalSyncWidth - { - get - { - return HSYNCWidth; - } - } + /// + /// Width (in characters) of the HSYNC + /// + public int HorizontalSyncWidth + { + get + { + return HSYNCWidth; + } + } - /// - /// The vertical scanline at which to start VSYNC - /// - public int VerticalSyncPos - { - get - { - return (int)Regs[VER_SYNC_POS] * ((int)Regs[MAX_RASTER_ADDR] + 1); - } - } + /// + /// The vertical scanline at which to start VSYNC + /// + public int VerticalSyncPos + { + get + { + return (int)Regs[VER_SYNC_POS] * ((int)Regs[MAX_RASTER_ADDR] + 1); + } + } - /// - /// Height (in scanlines) of the VSYNC - /// - public int VerticalSyncHeight - { - get - { - return VSYNCWidth; // * ((int)Regs[MAX_RASTER_ADDR] + 1); - } - } + /// + /// Height (in scanlines) of the VSYNC + /// + public int VerticalSyncHeight + { + get + { + return VSYNCWidth; // * ((int)Regs[MAX_RASTER_ADDR] + 1); + } + } - /// - /// The number of scanlines in one character (MAXRASTER) - /// - public int ScanlinesPerCharacter - { - get - { - return (int)Regs[MAX_RASTER_ADDR] + 1; - } - } + /// + /// The number of scanlines in one character (MAXRASTER) + /// + public int ScanlinesPerCharacter + { + get + { + return (int)Regs[MAX_RASTER_ADDR] + 1; + } + } - /// - /// Returns the starting video page address as specified within R12 - /// - public int VideoPageBase - { - get - { - if (!Regs[12].Bit(4) && Regs[12].Bit(5)) - return 0x8000; + /// + /// Returns the starting video page address as specified within R12 + /// + public int VideoPageBase + { + get + { + if (!Regs[12].Bit(4) && Regs[12].Bit(5)) + return 0x8000; - if (Regs[12].Bit(4) && !Regs[12].Bit(5)) - return 0x4000; + if (Regs[12].Bit(4) && !Regs[12].Bit(5)) + return 0x4000; - if (!Regs[12].Bit(4) && !Regs[12].Bit(5)) - return 0x0000; + if (!Regs[12].Bit(4) && !Regs[12].Bit(5)) + return 0x0000; - return 0xC000; - } - } + return 0xC000; + } + } - public int DStartHigh - { get { return Regs[DISP_START_ADDR_H]; } } + public int DStartHigh + { get { return Regs[DISP_START_ADDR_H]; } } - public int DStartLow - { get { return Regs[DISP_START_ADDR_L]; } } + public int DStartLow + { get { return Regs[DISP_START_ADDR_L]; } } - /// - /// Returns the video buffer size as specified within R12 - /// - public int VideoBufferSize - { - get - { - if (Regs[12].Bit(3) && Regs[12].Bit(2)) - return 0x8000; + /// + /// Returns the video buffer size as specified within R12 + /// + public int VideoBufferSize + { + get + { + if (Regs[12].Bit(3) && Regs[12].Bit(2)) + return 0x8000; - return 0x4000; - } - } + return 0x4000; + } + } - /// - /// The offset into vRAM - /// - public int VideoRAMOffset - { - get - { - ushort combined = (ushort)(Regs[12] << 8 | Regs[13]); - int offset = combined & 0x3ff; - return offset; - } - } + /// + /// The offset into vRAM + /// + public int VideoRAMOffset + { + get + { + ushort combined = (ushort)(Regs[12] << 8 | Regs[13]); + int offset = combined & 0x3ff; + return offset; + } + } - /* Easier memory functions */ + /* Easier memory functions */ - /// - /// The current byte address - /// - public ushort CurrentByteAddress; + /// + /// The current byte address + /// + public ushort CurrentByteAddress; - /// - /// ByteCounter - /// - public int ByteCounter; + /// + /// ByteCounter + /// + public int ByteCounter; - /// - /// Set at every HSYNC - /// - public int LatchedRAMOffset; + /// + /// Set at every HSYNC + /// + public int LatchedRAMOffset; - /// - /// set at every HSYNC - /// - public int LatchedRAMStartAddress; + /// + /// set at every HSYNC + /// + public int LatchedRAMStartAddress; - /// - /// set at every HSYNC - /// - public int LatchedScreenWidthBytes; + /// + /// set at every HSYNC + /// + public int LatchedScreenWidthBytes; - #endregion + #endregion - #region Internal Registers and State + #region Internal Registers and State - /* + /* Index Register Name Range CPC Setting Notes 0 Horizontal Total 00000000 63 Width of the screen, in characters. Should always be 63 (64 characters). 1 character == 1μs. 1 Horizontal Displayed 00000000 40 Number of characters displayed. Once horizontal character count (HCC) matches this value, DISPTMG is set to 1. @@ -322,502 +322,502 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC 16 Light Pen Address (High) xx000000 Read Only 17 Light Pen Address (Low) 00000000 Read Only */ - /// - /// 6845 internal registers - /// - private byte[] Regs = new byte[18]; + /// + /// 6845 internal registers + /// + private byte[] Regs = new byte[18]; - // CRTC Register constants - /// - /// R0: Horizontal total character number - /// Unit: Character - /// Notes: Defines the width of a scanline - /// - public const int HOR_TOTAL = 0; - /// - /// R1: Horizontal displayed character number - /// Unit: Character - /// Notes: Defines when DISPEN goes OFF on the scanline - /// - public const int HOR_DISPLAYED = 1; - /// - /// R2: Position of horizontal sync. pulse - /// Unit: Character - /// Notes: Defines when the HSync goes ON on the scanline - /// - public const int HOR_SYNC_POS = 2; - /// - /// R3: Width of horizontal/vertical sync. pulses - /// Unit: Character - /// Notes: VSync width can only be changed on type 3 and 4 - /// - public const int HOR_AND_VER_SYNC_WIDTHS = 3; - /// - /// R4: Vertical total Line character number - /// Unit: Character - /// Notes: Defines the height of a screen - /// - public const int VER_TOTAL = 4; - /// - /// R5: Vertical raster adjust - /// Unit: Scanline - /// Notes: Defines additionnal scanlines at the end of a screen - /// can be used for smooth vertical scrolling on CPC - /// - public const int VER_TOTAL_ADJUST = 5; - /// - /// R6: Vertical displayed character number - /// Unit: Character - /// Notes: Define when DISPEN remains OFF until a new screen starts - /// Height of displayed screen in characters (Once vertical character count (VCC) matches this value, DISPTMG is set to 1) - /// - public const int VER_DISPLAYED = 6; - /// - /// R7: Position of vertical sync. pulse - /// Unit: Character - /// Notes: Define when the VSync goes ON on a screen - /// - public const int VER_SYNC_POS = 7; - /// - /// R8: Interlaced mode - /// Unit: - /// Notes: 00: No interlace; 01: Interlace Sync Raster Scan Mode; 10: No Interlace; 11: Interlace Sync and Video Raster Scan Mode - /// (crct type specific) - /// - public const int INTERLACE_SKEW = 8; - /// - /// R9: Maximum raster - /// Unit: Scanline - /// Notes: Defines the height of a CRTC-Char in scanlines - /// - public const int MAX_RASTER_ADDR = 9; - /// - /// R10: Cursor start raster - /// Unit: - /// Notes: Cursor not used on CPC. - /// (xBP00000) - /// B = Blink On/Off; - /// P = Blink Period Control (Slow/Fast). - /// Sets first raster row of character that cursor is on to invert - /// - public const int CUR_START_RASTER = 10; - /// - /// R11: Cursor end - /// Unit: - /// Notes: Sets last raster row of character that cursor is on to invert - /// - public const int CUR_END_RASTER = 11; - /// - /// R12: Display Start Address (High) - /// Unit: - /// Notes: Define the MSB of MA when a CRTC-screen starts - /// - public const int DISP_START_ADDR_H = 12; - /// - /// R13: Display Start Address (Low) - /// Unit: - /// Notes: Define the LSB of MA when a CRTC-screen starts - /// Allows you to offset the start of screen memory for hardware scrolling, and if using memory from address &0000 with the firmware. - /// - public const int DISP_START_ADDR_L = 13; - /// - /// R14: Cursor Address (High) - /// Unit: - /// Notes: Useless on the Amstrad CPC/Plus (text-mode is not wired) - /// - public const int CUR_ADDR_H = 14; - /// - /// R15: Cursor Address (Low) - /// Unit: - /// Notes: Useless on the Amstrad CPC/Plus (text-mode is not wired) - /// - public const int CUR_ADDR_L = 15; - /// - /// R16: Light Pen Address (High) - /// Unit: - /// Notes: Hold the MSB of the cursor position when the lightpen was ON - /// - public const int LPEN_ADDR_H = 16; - /// - /// R17: Light Pen Address (Low) - /// Unit: - /// Notes: Hold the LSB of the cursor position when the lightpen was ON - /// - public const int LPEN_ADDR_L = 17; + // CRTC Register constants + /// + /// R0: Horizontal total character number + /// Unit: Character + /// Notes: Defines the width of a scanline + /// + public const int HOR_TOTAL = 0; + /// + /// R1: Horizontal displayed character number + /// Unit: Character + /// Notes: Defines when DISPEN goes OFF on the scanline + /// + public const int HOR_DISPLAYED = 1; + /// + /// R2: Position of horizontal sync. pulse + /// Unit: Character + /// Notes: Defines when the HSync goes ON on the scanline + /// + public const int HOR_SYNC_POS = 2; + /// + /// R3: Width of horizontal/vertical sync. pulses + /// Unit: Character + /// Notes: VSync width can only be changed on type 3 and 4 + /// + public const int HOR_AND_VER_SYNC_WIDTHS = 3; + /// + /// R4: Vertical total Line character number + /// Unit: Character + /// Notes: Defines the height of a screen + /// + public const int VER_TOTAL = 4; + /// + /// R5: Vertical raster adjust + /// Unit: Scanline + /// Notes: Defines additionnal scanlines at the end of a screen + /// can be used for smooth vertical scrolling on CPC + /// + public const int VER_TOTAL_ADJUST = 5; + /// + /// R6: Vertical displayed character number + /// Unit: Character + /// Notes: Define when DISPEN remains OFF until a new screen starts + /// Height of displayed screen in characters (Once vertical character count (VCC) matches this value, DISPTMG is set to 1) + /// + public const int VER_DISPLAYED = 6; + /// + /// R7: Position of vertical sync. pulse + /// Unit: Character + /// Notes: Define when the VSync goes ON on a screen + /// + public const int VER_SYNC_POS = 7; + /// + /// R8: Interlaced mode + /// Unit: + /// Notes: 00: No interlace; 01: Interlace Sync Raster Scan Mode; 10: No Interlace; 11: Interlace Sync and Video Raster Scan Mode + /// (crct type specific) + /// + public const int INTERLACE_SKEW = 8; + /// + /// R9: Maximum raster + /// Unit: Scanline + /// Notes: Defines the height of a CRTC-Char in scanlines + /// + public const int MAX_RASTER_ADDR = 9; + /// + /// R10: Cursor start raster + /// Unit: + /// Notes: Cursor not used on CPC. + /// (xBP00000) + /// B = Blink On/Off; + /// P = Blink Period Control (Slow/Fast). + /// Sets first raster row of character that cursor is on to invert + /// + public const int CUR_START_RASTER = 10; + /// + /// R11: Cursor end + /// Unit: + /// Notes: Sets last raster row of character that cursor is on to invert + /// + public const int CUR_END_RASTER = 11; + /// + /// R12: Display Start Address (High) + /// Unit: + /// Notes: Define the MSB of MA when a CRTC-screen starts + /// + public const int DISP_START_ADDR_H = 12; + /// + /// R13: Display Start Address (Low) + /// Unit: + /// Notes: Define the LSB of MA when a CRTC-screen starts + /// Allows you to offset the start of screen memory for hardware scrolling, and if using memory from address &0000 with the firmware. + /// + public const int DISP_START_ADDR_L = 13; + /// + /// R14: Cursor Address (High) + /// Unit: + /// Notes: Useless on the Amstrad CPC/Plus (text-mode is not wired) + /// + public const int CUR_ADDR_H = 14; + /// + /// R15: Cursor Address (Low) + /// Unit: + /// Notes: Useless on the Amstrad CPC/Plus (text-mode is not wired) + /// + public const int CUR_ADDR_L = 15; + /// + /// R16: Light Pen Address (High) + /// Unit: + /// Notes: Hold the MSB of the cursor position when the lightpen was ON + /// + public const int LPEN_ADDR_H = 16; + /// + /// R17: Light Pen Address (Low) + /// Unit: + /// Notes: Hold the LSB of the cursor position when the lightpen was ON + /// + public const int LPEN_ADDR_L = 17; - /// - /// The currently selected register - /// - private int SelectedRegister; + /// + /// The currently selected register + /// + private int SelectedRegister; - /// - /// CPC register default values - /// Taken from https://web.archive.org/web/20170501112330/http://www.grimware.org/doku.php/documentations/devices/crtc - /// http://www.cantrell.org.uk/david/tech/cpc/cpc-firmware/firmware.pdf - /// (The defaults values given here are those programmed by the firmware ROM after a cold/warm boot of the CPC/Plus) - /// - private byte[] RegDefaults = new byte[] { 63, 40, 46, 112, 38, 0, 25, 30, 0, 7, 0, 0, 48, 0, 192, 7, 0, 0 }; + /// + /// CPC register default values + /// Taken from https://web.archive.org/web/20170501112330/http://www.grimware.org/doku.php/documentations/devices/crtc + /// http://www.cantrell.org.uk/david/tech/cpc/cpc-firmware/firmware.pdf + /// (The defaults values given here are those programmed by the firmware ROM after a cold/warm boot of the CPC/Plus) + /// + private byte[] RegDefaults = new byte[] { 63, 40, 46, 112, 38, 0, 25, 30, 0, 7, 0, 0, 48, 0, 192, 7, 0, 0 }; - /// - /// Register masks - /// 0 = WRITE - /// 1 = READ - /// - private byte[] CPCMask = new byte[] { 255, 255, 255, 255, 127, 31, 127, 126, 3, 31, 31, 31, 63, 255, 63, 255, 63, 255 }; + /// + /// Register masks + /// 0 = WRITE + /// 1 = READ + /// + private byte[] CPCMask = new byte[] { 255, 255, 255, 255, 127, 31, 127, 126, 3, 31, 31, 31, 63, 255, 63, 255, 63, 255 }; - /// - /// Horizontal Character Count - /// - private int HCC; + /// + /// Horizontal Character Count + /// + private int HCC; - /// - /// Internal cycle counter - /// - private int CycleCounter; + /// + /// Internal cycle counter + /// + private int CycleCounter; - /// - /// Signs that we have finished the last character row - /// - private bool EndOfScreen; + /// + /// Signs that we have finished the last character row + /// + private bool EndOfScreen; - /// - /// HSYNC pulse width (in characters) - /// - private int HSYNCWidth; + /// + /// HSYNC pulse width (in characters) + /// + private int HSYNCWidth; - /// - /// Internal HSYNC counter - /// - private int HSYNCCounter; + /// + /// Internal HSYNC counter + /// + private int HSYNCCounter; - /// - /// VSYNC pulse width (in characters) - /// - private int VSYNCWidth; + /// + /// VSYNC pulse width (in characters) + /// + private int VSYNCWidth; - /// - /// Internal VSYNC counter - /// - private int VSYNCCounter; + /// + /// Internal VSYNC counter + /// + private int VSYNCCounter; - #endregion + #endregion - #region Public Methods + #region Public Methods - public void ClockCycle() - { - CheckHSYNCOff(); + public void ClockCycle() + { + CheckHSYNCOff(); - HCC++; + HCC++; - if (HCC == Regs[HOR_TOTAL] + 1) - { - // end of scanline - HCC = 0; - - if (VSYNCCounter > 0) - { - VSYNCCounter--; - if (VSYNCCounter == 0) - { - VSYNC = false; - } - } + if (HCC == Regs[HOR_TOTAL] + 1) + { + // end of scanline + HCC = 0; - VLC++; + if (VSYNCCounter > 0) + { + VSYNCCounter--; + if (VSYNCCounter == 0) + { + VSYNC = false; + } + } - if (VLC == Regs[MAX_RASTER_ADDR] + 1) - { - // end of rasterline - VLC = 0; - VCC++; + VLC++; - if (VCC == Regs[VER_TOTAL] + 1) - { - // end of screen - VCC = 0; - } + if (VLC == Regs[MAX_RASTER_ADDR] + 1) + { + // end of rasterline + VLC = 0; + VCC++; - if (VCC == Regs[VER_SYNC_POS] && !VSYNC) - { - VSYNC = true; - VSYNCCounter = VSYNCWidth; - VSYNC_Callbacks(); - } - } - } - else - { - // still on the current scanline - if (HCC == Regs[HOR_SYNC_POS] && !HSYNC) - { - HSYNC = true; - HSYNCCounter = HSYNCWidth; - HSYNC_Callbacks(); - ByteCounter = 0; - } + if (VCC == Regs[VER_TOTAL] + 1) + { + // end of screen + VCC = 0; + } - if (HCC >= Regs[HOR_DISPLAYED] + 1 || VCC >= Regs[VER_DISPLAYED]) - { - DISPTMG = false; - } - else - { - DISPTMG = true; + if (VCC == Regs[VER_SYNC_POS] && !VSYNC) + { + VSYNC = true; + VSYNCCounter = VSYNCWidth; + VSYNC_Callbacks(); + } + } + } + else + { + // still on the current scanline + if (HCC == Regs[HOR_SYNC_POS] && !HSYNC) + { + HSYNC = true; + HSYNCCounter = HSYNCWidth; + HSYNC_Callbacks(); + ByteCounter = 0; + } - var line = VCC; - var row = VLC; - var addrX = (LatchedRAMOffset * 2) + ((VCC * LatchedScreenWidthBytes) & 0x7ff) + ByteCounter; - // remove artifacts caused by certain hardware scrolling addresses - addrX &= 0x7ff; - var addrY = LatchedRAMStartAddress + (2048 * VLC); + if (HCC >= Regs[HOR_DISPLAYED] + 1 || VCC >= Regs[VER_DISPLAYED]) + { + DISPTMG = false; + } + else + { + DISPTMG = true; - //var addr = VideoPageBase + (line * (0x50)) + (row * 0x800) + (ByteCounter); - CurrentByteAddress = (ushort)(addrX + addrY); + var line = VCC; + var row = VLC; + var addrX = (LatchedRAMOffset * 2) + ((VCC * LatchedScreenWidthBytes) & 0x7ff) + ByteCounter; + // remove artifacts caused by certain hardware scrolling addresses + addrX &= 0x7ff; + var addrY = LatchedRAMStartAddress + (2048 * VLC); - ByteCounter += 2; - } - } - } + //var addr = VideoPageBase + (line * (0x50)) + (row * 0x800) + (ByteCounter); + CurrentByteAddress = (ushort)(addrX + addrY); - private void CheckHSYNCOff() - { - if (HSYNCCounter > 0) - { - HSYNCCounter--; - if (HSYNCCounter == 0) - { - HSYNC = false; - } - } - } + ByteCounter += 2; + } + } + } - /// - /// Runs a CRCT clock cycle - /// This should be called at 1Mhz / 1us / every 4 uncontended CPU t-states - /// - public void ClockCycle2() - { - if (HSYNC) - { - // HSYNC in progress - HSYNCCounter++; + private void CheckHSYNCOff() + { + if (HSYNCCounter > 0) + { + HSYNCCounter--; + if (HSYNCCounter == 0) + { + HSYNC = false; + } + } + } - ByteCounter = 0; + /// + /// Runs a CRCT clock cycle + /// This should be called at 1Mhz / 1us / every 4 uncontended CPU t-states + /// + public void ClockCycle2() + { + if (HSYNC) + { + // HSYNC in progress + HSYNCCounter++; - if (HSYNCCounter >= HSYNCWidth) - { - // end of HSYNC - HSYNCCounter = 0; - HSYNC = false; - } - } + ByteCounter = 0; - if (HSYNC && HSYNCCounter == 1) - { + if (HSYNCCounter >= HSYNCWidth) + { + // end of HSYNC + HSYNCCounter = 0; + HSYNC = false; + } + } - } + if (HSYNC && HSYNCCounter == 1) + { - // move one horizontal character - HCC++; + } - // check for DISPTMG - if (HCC >= Regs[HOR_DISPLAYED] + 1) - { - DISPTMG = false; - } - else if (VCC >= Regs[VER_DISPLAYED]) - { - DISPTMG = false; - } - else - { - DISPTMG = true; + // move one horizontal character + HCC++; - var line = VCC; - var row = VLC; - var addrX = (LatchedRAMOffset * 2) + ((VCC * LatchedScreenWidthBytes) & 0x7ff) + ByteCounter; - // remove artifacts caused by certain hardware scrolling addresses - addrX &= 0x7ff; - var addrY = LatchedRAMStartAddress + (2048 * VLC); + // check for DISPTMG + if (HCC >= Regs[HOR_DISPLAYED] + 1) + { + DISPTMG = false; + } + else if (VCC >= Regs[VER_DISPLAYED]) + { + DISPTMG = false; + } + else + { + DISPTMG = true; - //var addr = VideoPageBase + (line * (0x50)) + (row * 0x800) + (ByteCounter); - CurrentByteAddress = (ushort)(addrX + addrY); + var line = VCC; + var row = VLC; + var addrX = (LatchedRAMOffset * 2) + ((VCC * LatchedScreenWidthBytes) & 0x7ff) + ByteCounter; + // remove artifacts caused by certain hardware scrolling addresses + addrX &= 0x7ff; + var addrY = LatchedRAMStartAddress + (2048 * VLC); - ByteCounter += 2; - } + //var addr = VideoPageBase + (line * (0x50)) + (row * 0x800) + (ByteCounter); + CurrentByteAddress = (ushort)(addrX + addrY); - // check for the end of the current scanline - if (HCC == Regs[HOR_TOTAL] + 1) - { - // end of the current scanline - HCC = 0; + ByteCounter += 2; + } + + // check for the end of the current scanline + if (HCC == Regs[HOR_TOTAL] + 1) + { + // end of the current scanline + HCC = 0; - if (ChipType == (CRCTType)1 && VLC <= Regs[MAX_RASTER_ADDR]) - { - // https://web.archive.org/web/20170501112330/http://www.grimware.org/doku.php/documentations/devices/crtc - // The MA is reloaded with the value from R12 and R13 when VCC=0 and VLC=0 (that's when a new CRTC screen begin). - // However, CRTC Type 1 keep updating the MA on every new scanline while VCC=0 (and VLC== Regs[VER_TOTAL] + 1) - { - VCC = 0; - EndOfScreen = true; - } - } + // end of screen? + if (VCC >= Regs[VER_TOTAL] + 1) + { + VCC = 0; + EndOfScreen = true; + } + } - // does VSYNC need to be raised? - if (!VSYNC) - { - if (VCC == Regs[VER_SYNC_POS]) - { - VSYNC = true; - VSYNCCounter = 0; - VSYNC_Callbacks(); - } - } - } - else - { - // still processing a scanline - // check whether HSYNC needs raising - if (!HSYNC) - { - if (HCC == Regs[HOR_SYNC_POS]) - { - HSYNC = true; - HSYNCCounter = 0; - HSYNC_Callbacks(); - lineCounter++; + // does VSYNC need to be raised? + if (!VSYNC) + { + if (VCC == Regs[VER_SYNC_POS]) + { + VSYNC = true; + VSYNCCounter = 0; + VSYNC_Callbacks(); + } + } + } + else + { + // still processing a scanline + // check whether HSYNC needs raising + if (!HSYNC) + { + if (HCC == Regs[HOR_SYNC_POS]) + { + HSYNC = true; + HSYNCCounter = 0; + HSYNC_Callbacks(); + lineCounter++; - LatchedRAMStartAddress = VideoPageBase; - LatchedRAMOffset = VideoRAMOffset; - LatchedScreenWidthBytes = DisplayWidth * 2; + LatchedRAMStartAddress = VideoPageBase; + LatchedRAMOffset = VideoRAMOffset; + LatchedScreenWidthBytes = DisplayWidth * 2; - } - } - } - } + } + } + } + } - /// - /// Runs a CRCT clock cycle - /// This should be called at 1Mhz / 1us / every 4 uncontended CPU t-states - /// - public void ClockCycle1() - { - // HSYNC processing - if (HSYNCCounter > 0) - { - HSYNCCounter--; - if (HSYNCCounter == 0) - HSYNC = false; - } + /// + /// Runs a CRCT clock cycle + /// This should be called at 1Mhz / 1us / every 4 uncontended CPU t-states + /// + public void ClockCycle1() + { + // HSYNC processing + if (HSYNCCounter > 0) + { + HSYNCCounter--; + if (HSYNCCounter == 0) + HSYNC = false; + } - HCC++; + HCC++; - if (HCC == FrameWidth) - { - // we have finished the current scanline - HCC = 0; + if (HCC == FrameWidth) + { + // we have finished the current scanline + HCC = 0; - if (VSYNCCounter > 0) - { - VSYNCCounter--; - if (VSYNCCounter == 0) - VSYNC = false; - } + if (VSYNCCounter > 0) + { + VSYNCCounter--; + if (VSYNCCounter == 0) + VSYNC = false; + } - VLC++; + VLC++; - if (VLC == ScanlinesPerCharacter) - { - // completed a vertical character - VLC = 0; - VCC++; + if (VLC == ScanlinesPerCharacter) + { + // completed a vertical character + VLC = 0; + VCC++; - if (VCC == FrameHeight) - { - // screen has completed - VCC = 0; - } - } + if (VCC == FrameHeight) + { + // screen has completed + VCC = 0; + } + } - // check whether VSYNC should be raised - if (VCC == VerticalSyncPos && !VSYNC) - { - VSYNC = true; - VSYNCCounter = VSYNCWidth; - VSYNC_Callbacks(); - } - } - else if (HCC == HorizontalSyncPos && !HSYNC) - { - // start of HSYNC period - HSYNC = true; - HSYNCCounter = HSYNCWidth; - HSYNC_Callbacks(); - } + // check whether VSYNC should be raised + if (VCC == VerticalSyncPos && !VSYNC) + { + VSYNC = true; + VSYNCCounter = VSYNCWidth; + VSYNC_Callbacks(); + } + } + else if (HCC == HorizontalSyncPos && !HSYNC) + { + // start of HSYNC period + HSYNC = true; + HSYNCCounter = HSYNCWidth; + HSYNC_Callbacks(); + } - // DISPTMG - if (HCC >= Regs[HOR_DISPLAYED] || VCC >= Regs[VER_DISPLAYED]) - { - DISPTMG = false; - } - else - { - DISPTMG = true; - } - /* + // DISPTMG + if (HCC >= Regs[HOR_DISPLAYED] || VCC >= Regs[VER_DISPLAYED]) + { + DISPTMG = false; + } + else + { + DISPTMG = true; + } + /* // check for DISPTMG if (HCC >= Regs[HOR_DISPLAYED] + 1) { @@ -832,53 +832,53 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC DISPTMG = true; } */ - } + } - public int lineCounter = 0; + public int lineCounter = 0; - - /// - /// Resets the chip - /// - public void Reset() - { - // set regs to default - for (int i = 0; i < 18; i++) - Regs[i] = RegDefaults[i]; - SelectedRegister = 0; + /// + /// Resets the chip + /// + public void Reset() + { + // set regs to default + for (int i = 0; i < 18; i++) + Regs[i] = RegDefaults[i]; - // populate initial MA address - MA = (short)(((Regs[DISP_START_ADDR_H]) & 0xff) << 8 | (Regs[DISP_START_ADDR_L]) & 0xff); + SelectedRegister = 0; - // updates widths - UpdateWidths(); + // populate initial MA address + MA = (short)(((Regs[DISP_START_ADDR_H]) & 0xff) << 8 | (Regs[DISP_START_ADDR_L]) & 0xff); - HSYNC = false; - VSYNC = false; + // updates widths + UpdateWidths(); - HSYNCCounter = 0; - VSYNCCounter = 0; + HSYNC = false; + VSYNC = false; - HCC = 0; - VCC = 0; - VLC = 0; - } + HSYNCCounter = 0; + VSYNCCounter = 0; - #endregion + HCC = 0; + VCC = 0; + VLC = 0; + } - #region Internal Methods + #endregion - /// - /// Selects a register - /// - private void RegisterSelect(int data) - { - SelectedRegister = data & 0x1F; - } + #region Internal Methods - /* + /// + /// Selects a register + /// + private void RegisterSelect(int data) + { + SelectedRegister = data & 0x1F; + } + + /* RegIdx Register Name Type 0 1 2 3 4 0 Horizontal Total Write Only Write Only Write Only (note 2) (note 3) @@ -905,310 +905,310 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC 3. CRTC type 4 is the same as CRTC type 3. The registers also repeat as they do on the type 3. */ - /// - /// Writes to the currently selected register - /// - private void WriteRegister(int data) - { - // 16 and 17 are read only registers on all types - if (SelectedRegister == 16 || SelectedRegister == 17) - return; + /// + /// Writes to the currently selected register + /// + private void WriteRegister(int data) + { + // 16 and 17 are read only registers on all types + if (SelectedRegister == 16 || SelectedRegister == 17) + return; - // non existing registers - if (SelectedRegister > 17) - return; + // non existing registers + if (SelectedRegister > 17) + return; - if (SelectedRegister == DISP_START_ADDR_L) - { + if (SelectedRegister == DISP_START_ADDR_L) + { - } + } - if (SelectedRegister == DISP_START_ADDR_H) - { + if (SelectedRegister == DISP_START_ADDR_H) + { - } + } - if (SelectedRegister == HOR_TOTAL) - { - // always 63 - if (data != 63) - return; - } + if (SelectedRegister == HOR_TOTAL) + { + // always 63 + if (data != 63) + return; + } - if (SelectedRegister == 1) - { - var d = data; - } + if (SelectedRegister == 1) + { + var d = data; + } - Regs[SelectedRegister] = (byte)(data & CPCMask[SelectedRegister]); + Regs[SelectedRegister] = (byte)(data & CPCMask[SelectedRegister]); - if (SelectedRegister == HOR_AND_VER_SYNC_WIDTHS) - { - UpdateWidths(); - } - } + if (SelectedRegister == HOR_AND_VER_SYNC_WIDTHS) + { + UpdateWidths(); + } + } - /// - /// Reads from the currently selected register - /// - private bool ReadRegister(ref int data) - { - bool addressed = false; - switch (SelectedRegister) - { - case 0: - case 1: - case 2: - case 3: - case 4: - case 5: - case 6: - case 7: - case 8: - case 9: - case 10: - case 11: - if ((int)ChipType == 0 || (int)ChipType == 1) - { - addressed = true; - data = 0; - } - break; - case 12: - case 13: - addressed = true; - if ((int)ChipType == 0) - data = Regs[SelectedRegister]; - else if ((int)ChipType == 1) - data = 0; - break; - case 14: - case 15: - case 16: - case 17: - addressed = true; - data = Regs[SelectedRegister]; - break; + /// + /// Reads from the currently selected register + /// + private bool ReadRegister(ref int data) + { + bool addressed = false; + switch (SelectedRegister) + { + case 0: + case 1: + case 2: + case 3: + case 4: + case 5: + case 6: + case 7: + case 8: + case 9: + case 10: + case 11: + if ((int)ChipType == 0 || (int)ChipType == 1) + { + addressed = true; + data = 0; + } + break; + case 12: + case 13: + addressed = true; + if ((int)ChipType == 0) + data = Regs[SelectedRegister]; + else if ((int)ChipType == 1) + data = 0; + break; + case 14: + case 15: + case 16: + case 17: + addressed = true; + data = Regs[SelectedRegister]; + break; - default: - // registers 18-31 read as 0, on type 0 and 2. registers 18-30 read as 0 on type1, register 31 reads as 0x0ff. - if (SelectedRegister >= 18 && SelectedRegister <= 30) - { - switch ((int)ChipType) - { - case 0: - case 2: - case 1: - addressed = true; - data = 0; - break; - } - } - else if (SelectedRegister == 31) - { - if ((int)ChipType == 1) - { - addressed = true; - data = 0x0ff; - } - else if ((int)ChipType == 0 || (int)ChipType == 2) - { - addressed = true; - data = 0; - } - } - break; - } + default: + // registers 18-31 read as 0, on type 0 and 2. registers 18-30 read as 0 on type1, register 31 reads as 0x0ff. + if (SelectedRegister >= 18 && SelectedRegister <= 30) + { + switch ((int)ChipType) + { + case 0: + case 2: + case 1: + addressed = true; + data = 0; + break; + } + } + else if (SelectedRegister == 31) + { + if ((int)ChipType == 1) + { + addressed = true; + data = 0x0ff; + } + else if ((int)ChipType == 0 || (int)ChipType == 2) + { + addressed = true; + data = 0; + } + } + break; + } - return addressed; - } + return addressed; + } - /// - /// Reads from the status register - /// - private bool ReadStatus(ref int data) - { - bool addressed = false; - switch ((int)ChipType) - { - case 1: - // read status - //todo!! - addressed = true; - break; - case 0: - case 2: - // status reg not available - break; - case 3: - case 4: - // read from internal register instead - addressed = ReadRegister(ref data); - break; - } - return addressed; - } + /// + /// Reads from the status register + /// + private bool ReadStatus(ref int data) + { + bool addressed = false; + switch ((int)ChipType) + { + case 1: + // read status + //todo!! + addressed = true; + break; + case 0: + case 2: + // status reg not available + break; + case 3: + case 4: + // read from internal register instead + addressed = ReadRegister(ref data); + break; + } + return addressed; + } - /// - /// Updates the V and H SYNC widths - /// - private void UpdateWidths() - { - switch (ChipType) - { - case CRCTType.HD6845S: - // Bits 7..4 define Vertical Sync Width. If 0 is programmed this gives 16 lines of VSYNC. Bits 3..0 define Horizontal Sync Width. - // If 0 is programmed no HSYNC is generated. - HSYNCWidth = (Regs[HOR_AND_VER_SYNC_WIDTHS] >> 0) & 0x0F; - VSYNCWidth = (Regs[HOR_AND_VER_SYNC_WIDTHS] >> 4) & 0x0F; - break; - case CRCTType.UM6845R: - // Bits 7..4 are ignored. Vertical Sync is fixed at 16 lines. Bits 3..0 define Horizontal Sync Width. If 0 is programmed no HSYNC is generated. - HSYNCWidth = (Regs[HOR_AND_VER_SYNC_WIDTHS] >> 0) & 0x0F; - VSYNCWidth = 16; - break; - case CRCTType.MC6845: - // Bits 7..4 are ignored. Vertical Sync is fixed at 16 lines. Bits 3..0 define Horizontal Sync Width. If 0 is programmed this gives a HSYNC width of 16. - HSYNCWidth = (Regs[HOR_AND_VER_SYNC_WIDTHS] >> 0) & 0x0F; - if (HSYNCWidth == 0) - HSYNCWidth = 16; - VSYNCWidth = 16; - break; - case CRCTType.AMS40489: - case CRCTType.AMS40226: - // Bits 7..4 define Vertical Sync Width. If 0 is programmed this gives 16 lines of VSYNC.Bits 3..0 define Horizontal Sync Width. - // If 0 is programmed this gives a HSYNC width of 16. - HSYNCWidth = (Regs[HOR_AND_VER_SYNC_WIDTHS] >> 0) & 0x0F; - VSYNCWidth = (Regs[HOR_AND_VER_SYNC_WIDTHS] >> 4) & 0x0F; - if (HSYNCWidth == 0) - HSYNCWidth = 16; - if (VSYNCWidth == 0) - VSYNCWidth = 16; - break; - } - } + /// + /// Updates the V and H SYNC widths + /// + private void UpdateWidths() + { + switch (ChipType) + { + case CRCTType.HD6845S: + // Bits 7..4 define Vertical Sync Width. If 0 is programmed this gives 16 lines of VSYNC. Bits 3..0 define Horizontal Sync Width. + // If 0 is programmed no HSYNC is generated. + HSYNCWidth = (Regs[HOR_AND_VER_SYNC_WIDTHS] >> 0) & 0x0F; + VSYNCWidth = (Regs[HOR_AND_VER_SYNC_WIDTHS] >> 4) & 0x0F; + break; + case CRCTType.UM6845R: + // Bits 7..4 are ignored. Vertical Sync is fixed at 16 lines. Bits 3..0 define Horizontal Sync Width. If 0 is programmed no HSYNC is generated. + HSYNCWidth = (Regs[HOR_AND_VER_SYNC_WIDTHS] >> 0) & 0x0F; + VSYNCWidth = 16; + break; + case CRCTType.MC6845: + // Bits 7..4 are ignored. Vertical Sync is fixed at 16 lines. Bits 3..0 define Horizontal Sync Width. If 0 is programmed this gives a HSYNC width of 16. + HSYNCWidth = (Regs[HOR_AND_VER_SYNC_WIDTHS] >> 0) & 0x0F; + if (HSYNCWidth == 0) + HSYNCWidth = 16; + VSYNCWidth = 16; + break; + case CRCTType.AMS40489: + case CRCTType.AMS40226: + // Bits 7..4 define Vertical Sync Width. If 0 is programmed this gives 16 lines of VSYNC.Bits 3..0 define Horizontal Sync Width. + // If 0 is programmed this gives a HSYNC width of 16. + HSYNCWidth = (Regs[HOR_AND_VER_SYNC_WIDTHS] >> 0) & 0x0F; + VSYNCWidth = (Regs[HOR_AND_VER_SYNC_WIDTHS] >> 4) & 0x0F; + if (HSYNCWidth == 0) + HSYNCWidth = 16; + if (VSYNCWidth == 0) + VSYNCWidth = 16; + break; + } + } - #endregion + #endregion - #region PortIODevice + #region PortIODevice - /* + /* #BCXX %x0xxxx00 xxxxxxxx 6845 CRTC Index - Write #BDXX %x0xxxx01 xxxxxxxx 6845 CRTC Data Out - Write #BEXX %x0xxxx10 xxxxxxxx 6845 CRTC Status (as far as supported) Read - #BFXX %x0xxxx11 xxxxxxxx 6845 CRTC Data In (as far as supported) Read - */ - /// - /// Device responds to an IN instruction - /// - public bool ReadPort(ushort port, ref int result) - { - byte portUpper = (byte)(port >> 8); - byte portLower = (byte)(port & 0xff); + /// + /// Device responds to an IN instruction + /// + public bool ReadPort(ushort port, ref int result) + { + byte portUpper = (byte)(port >> 8); + byte portLower = (byte)(port & 0xff); - bool accessed = false; + bool accessed = false; - // The 6845 is selected when bit 14 of the I/O port address is set to "0" - if (portUpper.Bit(6)) - return accessed; + // The 6845 is selected when bit 14 of the I/O port address is set to "0" + if (portUpper.Bit(6)) + return accessed; - // Bit 9 and 8 of the I/O port address define the function to access - if (portUpper.Bit(1) && !portUpper.Bit(0)) - { - // read status register - accessed = ReadStatus(ref result); - } - else if ((portUpper & 3) == 3) - { - // read data register - accessed = ReadRegister(ref result); - } - else - { - result = 0; - } + // Bit 9 and 8 of the I/O port address define the function to access + if (portUpper.Bit(1) && !portUpper.Bit(0)) + { + // read status register + accessed = ReadStatus(ref result); + } + else if ((portUpper & 3) == 3) + { + // read data register + accessed = ReadRegister(ref result); + } + else + { + result = 0; + } - return accessed; - } + return accessed; + } - /// - /// Device responds to an OUT instruction - /// - public bool WritePort(ushort port, int result) - { - byte portUpper = (byte)(port >> 8); - byte portLower = (byte)(port & 0xff); + /// + /// Device responds to an OUT instruction + /// + public bool WritePort(ushort port, int result) + { + byte portUpper = (byte)(port >> 8); + byte portLower = (byte)(port & 0xff); - bool accessed = false; + bool accessed = false; - // The 6845 is selected when bit 14 of the I/O port address is set to "0" - if (portUpper.Bit(6)) - return accessed; + // The 6845 is selected when bit 14 of the I/O port address is set to "0" + if (portUpper.Bit(6)) + return accessed; - var func = portUpper & 3; + var func = portUpper & 3; - switch (func) - { - // reg select - case 0: - RegisterSelect(result); - break; + switch (func) + { + // reg select + case 0: + RegisterSelect(result); + break; - // data write - case 1: - WriteRegister(result); - break; - } + // data write + case 1: + WriteRegister(result); + break; + } - return accessed; - } + return accessed; + } - #endregion + #endregion - #region Serialization + #region Serialization - public void SyncState(Serializer ser) - { - ser.BeginSection("CRTC"); - ser.SyncEnum(nameof(ChipType), ref ChipType); - ser.Sync(nameof(HSYNC), ref HSYNC); - ser.Sync(nameof(VSYNC), ref VSYNC); - ser.Sync(nameof(DISPTMG), ref DISPTMG); - ser.Sync(nameof(MA), ref MA); - ser.Sync(nameof(CurrentByteAddress), ref CurrentByteAddress); - ser.Sync(nameof(ByteCounter), ref ByteCounter); - ser.Sync(nameof(Regs), ref Regs, false); - ser.Sync(nameof(SelectedRegister), ref SelectedRegister); - ser.Sync(nameof(HCC), ref HCC); - ser.Sync(nameof(VCC), ref VCC); - ser.Sync(nameof(VLC), ref VLC); - ser.Sync(nameof(CycleCounter), ref CycleCounter); - ser.Sync(nameof(EndOfScreen), ref EndOfScreen); - ser.Sync(nameof(HSYNCWidth), ref HSYNCWidth); - ser.Sync(nameof(HSYNCCounter), ref HSYNCCounter); - ser.Sync(nameof(VSYNCWidth), ref VSYNCWidth); - ser.Sync(nameof(VSYNCCounter), ref VSYNCCounter); - ser.EndSection(); - } + public void SyncState(Serializer ser) + { + ser.BeginSection("CRTC"); + ser.SyncEnum(nameof(ChipType), ref ChipType); + ser.Sync(nameof(HSYNC), ref HSYNC); + ser.Sync(nameof(VSYNC), ref VSYNC); + ser.Sync(nameof(DISPTMG), ref DISPTMG); + ser.Sync(nameof(MA), ref MA); + ser.Sync(nameof(CurrentByteAddress), ref CurrentByteAddress); + ser.Sync(nameof(ByteCounter), ref ByteCounter); + ser.Sync(nameof(Regs), ref Regs, false); + ser.Sync(nameof(SelectedRegister), ref SelectedRegister); + ser.Sync(nameof(HCC), ref HCC); + ser.Sync(nameof(VCC), ref VCC); + ser.Sync(nameof(VLC), ref VLC); + ser.Sync(nameof(CycleCounter), ref CycleCounter); + ser.Sync(nameof(EndOfScreen), ref EndOfScreen); + ser.Sync(nameof(HSYNCWidth), ref HSYNCWidth); + ser.Sync(nameof(HSYNCCounter), ref HSYNCCounter); + ser.Sync(nameof(VSYNCWidth), ref VSYNCWidth); + ser.Sync(nameof(VSYNCCounter), ref VSYNCCounter); + ser.EndSection(); + } - #endregion + #endregion - #region Enums + #region Enums - /// - /// The types of CRCT chip found in the CPC range - /// - public enum CRCTType - { - HD6845S = 0, - UM6845 = 0, - UM6845R = 1, - MC6845 = 2, - AMS40489 = 3, - AMS40226 = 4 - } + /// + /// The types of CRCT chip found in the CPC range + /// + public enum CRCTType + { + HD6845S = 0, + UM6845 = 0, + UM6845R = 1, + MC6845 = 2, + AMS40489 = 3, + AMS40226 = 4 + } - #endregion - } + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Display/CRTC6845.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Display/CRTC6845.cs index c13fcd6522..e43c30d8cc 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Display/CRTC6845.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Display/CRTC6845.cs @@ -283,7 +283,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC /// The CRTC latches the Display Start H & L address at different times /// (depending on the chip type) /// - private int StartAddressLatch; + private int StartAddressLatch; #endregion @@ -541,7 +541,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC case 1: return ReadStatus_Type1(ref data); case 3: case 4: return ReadStatus_Type3_4(ref data); - default: return false; + default: return false; } } @@ -561,7 +561,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC // Bits 5 and 4 determine the skew res = (val & 0x30) >> 4; if (res > 2) - return -1; + return -1; break; // UMR6845R @@ -600,7 +600,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC if (res > 2) return -1; break; - + // UMR6845R case 1: return 0; @@ -1141,7 +1141,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC #region Clock Cycles - /* persistent switch signals */ + /* persistent switch signals */ bool s_VS; bool s_HDISP; bool s_VDISP; @@ -1172,7 +1172,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC /// private void ClockCycle_Generic() { - + } /// @@ -1788,7 +1788,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC else { _DISPTMG = false; - } + } /* Cursor Control */ if (s_HDISP && s_VDISP) diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Display/CRTDevice.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Display/CRTDevice.cs index c1bc6b89a2..2b6ffdec7e 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Display/CRTDevice.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Display/CRTDevice.cs @@ -9,41 +9,41 @@ using System.Threading.Tasks; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Render pixels to the screen - /// - public class CRTDevice : IVideoProvider - { - #region Devices + /// + /// Render pixels to the screen + /// + public class CRTDevice : IVideoProvider + { + #region Devices - private CPCBase _machine; - private CRCT_6845 CRCT => _machine.CRCT; - private AmstradGateArray GateArray => _machine.GateArray; + private CPCBase _machine; + private CRCT_6845 CRCT => _machine.CRCT; + private AmstradGateArray GateArray => _machine.GateArray; - #endregion + #endregion - #region Construction + #region Construction - public CRTDevice(CPCBase machine) - { - _machine = machine; - CurrentLine = new ScanLine(this); + public CRTDevice(CPCBase machine) + { + _machine = machine; + CurrentLine = new ScanLine(this); - CRCT.AttachHSYNCCallback(OnHSYNC); - CRCT.AttachVSYNCCallback(OnVSYNC); - } + CRCT.AttachHSYNCCallback(OnHSYNC); + CRCT.AttachVSYNCCallback(OnVSYNC); + } - #endregion + #endregion - #region Palettes - - /// - /// The standard CPC Pallete (ordered by firmware #) - /// http://www.cpcwiki.eu/index.php/CPC_Palette - /// - public static readonly int[] CPCFirmwarePalette = - { - Colors.ARGB(0x00, 0x00, 0x00), // Black + #region Palettes + + /// + /// The standard CPC Pallete (ordered by firmware #) + /// http://www.cpcwiki.eu/index.php/CPC_Palette + /// + public static readonly int[] CPCFirmwarePalette = + { + Colors.ARGB(0x00, 0x00, 0x00), // Black Colors.ARGB(0x00, 0x00, 0x80), // Blue Colors.ARGB(0x00, 0x00, 0xFF), // Bright Blue Colors.ARGB(0x80, 0x00, 0x00), // Red @@ -72,13 +72,13 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC Colors.ARGB(0xFF, 0xFF, 0xFF), // Bright White }; - /// - /// The standard CPC Pallete (ordered by hardware #) - /// http://www.cpcwiki.eu/index.php/CPC_Palette - /// - public static readonly int[] CPCHardwarePalette = - { - Colors.ARGB(0x80, 0x80, 0x80), // White + /// + /// The standard CPC Pallete (ordered by hardware #) + /// http://www.cpcwiki.eu/index.php/CPC_Palette + /// + public static readonly int[] CPCHardwarePalette = + { + Colors.ARGB(0x80, 0x80, 0x80), // White Colors.ARGB(0x80, 0x80, 0x80), // White (duplicate) Colors.ARGB(0x00, 0xFF, 0x80), // Sea Green Colors.ARGB(0xFF, 0xFF, 0x80), // Pastel Yellow @@ -111,306 +111,306 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC Colors.ARGB(0x80, 0x80, 0x00), // Yellow Colors.ARGB(0x80, 0x80, 0xFF), // Pastel Blue }; - - #endregion - #region Public Stuff + #endregion - /// - /// The current scanline that is being added to - /// (will be processed and committed to the screen buffer every HSYNC) - /// - public ScanLine CurrentLine; + #region Public Stuff - /// - /// The number of top border scanlines to ommit when rendering - /// - public int TopLinesToTrim = 20; + /// + /// The current scanline that is being added to + /// (will be processed and committed to the screen buffer every HSYNC) + /// + public ScanLine CurrentLine; - /// - /// Count of rendered scanlines this frame - /// - public int ScanlineCounter = 0; + /// + /// The number of top border scanlines to ommit when rendering + /// + public int TopLinesToTrim = 20; - /// - /// Video buffer processing - /// - public int[] ProcessVideoBuffer() - { - return ScreenBuffer; - } + /// + /// Count of rendered scanlines this frame + /// + public int ScanlineCounter = 0; - /// - /// Sets up buffers and the like at the start of a frame - /// - public void SetupVideo() - { - if (BufferHeight == 576) - return; + /// + /// Video buffer processing + /// + public int[] ProcessVideoBuffer() + { + return ScreenBuffer; + } - BufferWidth = 800; - BufferHeight = 576; + /// + /// Sets up buffers and the like at the start of a frame + /// + public void SetupVideo() + { + if (BufferHeight == 576) + return; - VirtualWidth = BufferWidth / 2; - VirtualHeight = BufferHeight / 2; + BufferWidth = 800; + BufferHeight = 576; - ScreenBuffer = new int[BufferWidth * BufferHeight]; - } + VirtualWidth = BufferWidth / 2; + VirtualHeight = BufferHeight / 2; - /// - /// Fired when the CRCT flags HSYNC - /// - public void OnHSYNC() - { + ScreenBuffer = new int[BufferWidth * BufferHeight]; + } - } + /// + /// Fired when the CRCT flags HSYNC + /// + public void OnHSYNC() + { - /// - /// Fired when the CRCT flags VSYNC - /// - public void OnVSYNC() - { + } - } + /// + /// Fired when the CRCT flags VSYNC + /// + public void OnVSYNC() + { - #endregion + } - #region IVideoProvider + #endregion - /// - /// Video output buffer - /// - public int[] ScreenBuffer; + #region IVideoProvider - private int _virtualWidth; - private int _virtualHeight; - private int _bufferWidth; - private int _bufferHeight; + /// + /// Video output buffer + /// + public int[] ScreenBuffer; - public int BackgroundColor - { - get { return CPCHardwarePalette[0]; } - } + private int _virtualWidth; + private int _virtualHeight; + private int _bufferWidth; + private int _bufferHeight; - public int VirtualWidth - { - get { return _virtualWidth; } - set { _virtualWidth = value; } - } + public int BackgroundColor + { + get { return CPCHardwarePalette[0]; } + } - public int VirtualHeight - { - get { return _virtualHeight; } - set { _virtualHeight = value; } - } + public int VirtualWidth + { + get { return _virtualWidth; } + set { _virtualWidth = value; } + } - public int BufferWidth - { - get { return _bufferWidth; } - set { _bufferWidth = value; } - } + public int VirtualHeight + { + get { return _virtualHeight; } + set { _virtualHeight = value; } + } - public int BufferHeight - { - get { return _bufferHeight; } - set { _bufferHeight = value; } - } + public int BufferWidth + { + get { return _bufferWidth; } + set { _bufferWidth = value; } + } - public int VsyncNumerator - { - get { return GateArray.Z80ClockSpeed * 50; } - set { } - } + public int BufferHeight + { + get { return _bufferHeight; } + set { _bufferHeight = value; } + } - public int VsyncDenominator - { - get { return GateArray.Z80ClockSpeed; } - } + public int VsyncNumerator + { + get { return GateArray.Z80ClockSpeed * 50; } + set { } + } - public int[] GetVideoBuffer() - { - return ProcessVideoBuffer(); - } + public int VsyncDenominator + { + get { return GateArray.Z80ClockSpeed; } + } - public void SetupScreenSize() - { - BufferWidth = 1024; // 512; - BufferHeight = 768; - VirtualHeight = BufferHeight; - VirtualWidth = BufferWidth; - ScreenBuffer = new int[BufferWidth * BufferHeight]; - croppedBuffer = ScreenBuffer; - } + public int[] GetVideoBuffer() + { + return ProcessVideoBuffer(); + } - protected int[] croppedBuffer; + public void SetupScreenSize() + { + BufferWidth = 1024; // 512; + BufferHeight = 768; + VirtualHeight = BufferHeight; + VirtualWidth = BufferWidth; + ScreenBuffer = new int[BufferWidth * BufferHeight]; + croppedBuffer = ScreenBuffer; + } - #endregion + protected int[] croppedBuffer; - #region Serialization + #endregion - public void SyncState(Serializer ser) - { - ser.BeginSection("CRT"); - ser.Sync("BufferWidth", ref _bufferWidth); - ser.Sync("BufferHeight", ref _bufferHeight); - ser.Sync("VirtualHeight", ref _virtualHeight); - ser.Sync("VirtualWidth", ref _virtualWidth); - ser.Sync(nameof(ScreenBuffer), ref ScreenBuffer, false); - ser.Sync(nameof(ScanlineCounter), ref ScanlineCounter); - ser.EndSection(); - } + #region Serialization - #endregion - } + public void SyncState(Serializer ser) + { + ser.BeginSection("CRT"); + ser.Sync("BufferWidth", ref _bufferWidth); + ser.Sync("BufferHeight", ref _bufferHeight); + ser.Sync("VirtualHeight", ref _virtualHeight); + ser.Sync("VirtualWidth", ref _virtualWidth); + ser.Sync(nameof(ScreenBuffer), ref ScreenBuffer, false); + ser.Sync(nameof(ScanlineCounter), ref ScanlineCounter); + ser.EndSection(); + } - /// - /// Represents a single scanline buffer - /// - public class ScanLine - { - /// - /// Array of character information - /// - public Character[] Characters; + #endregion + } - /// - /// The screenmode that was set at the start of this scanline - /// - public int ScreenMode = 1; + /// + /// Represents a single scanline buffer + /// + public class ScanLine + { + /// + /// Array of character information + /// + public Character[] Characters; - /// - /// The scanline number (0 based) - /// - public int LineIndex; + /// + /// The screenmode that was set at the start of this scanline + /// + public int ScreenMode = 1; - /// - /// The calling CRT device - /// - private CRTDevice CRT; + /// + /// The scanline number (0 based) + /// + public int LineIndex; - public ScanLine(CRTDevice crt) - { - Reset(); - CRT = crt; - } + /// + /// The calling CRT device + /// + private CRTDevice CRT; - // To be run after scanline has been fully processed - public void InitScanline(int screenMode, int lineIndex) - { - Reset(); - ScreenMode = screenMode; - LineIndex = lineIndex; - } + public ScanLine(CRTDevice crt) + { + Reset(); + CRT = crt; + } - /// - /// Adds a single scanline character into the matrix - /// - public void AddScanlineCharacter(int index, RenderPhase phase, byte vid1, byte vid2, int[] pens) - { - if (index >= 64) - { - return; - } + // To be run after scanline has been fully processed + public void InitScanline(int screenMode, int lineIndex) + { + Reset(); + ScreenMode = screenMode; + LineIndex = lineIndex; + } - switch (phase) - { - case RenderPhase.BORDER: - AddBorderValue(index, CRTDevice.CPCHardwarePalette[pens[16]]); - break; - case RenderPhase.DISPLAY: - AddDisplayValue(index, vid1, vid2, pens); - break; - default: - AddSyncValue(index, phase); - break; - } - } + /// + /// Adds a single scanline character into the matrix + /// + public void AddScanlineCharacter(int index, RenderPhase phase, byte vid1, byte vid2, int[] pens) + { + if (index >= 64) + { + return; + } - /// - /// Adds a HSYNC, VSYNC or HSYNC+VSYNC character into the scanline - /// - private void AddSyncValue(int charIndex, RenderPhase phase) - { - Characters[charIndex].Phase = phase; - Characters[charIndex].Pixels = new int[0]; - } + switch (phase) + { + case RenderPhase.BORDER: + AddBorderValue(index, CRTDevice.CPCHardwarePalette[pens[16]]); + break; + case RenderPhase.DISPLAY: + AddDisplayValue(index, vid1, vid2, pens); + break; + default: + AddSyncValue(index, phase); + break; + } + } - /// - /// Adds a border character into the scanline - /// - private void AddBorderValue(int charIndex, int colourValue) - { - Characters[charIndex].Phase = RenderPhase.BORDER; + /// + /// Adds a HSYNC, VSYNC or HSYNC+VSYNC character into the scanline + /// + private void AddSyncValue(int charIndex, RenderPhase phase) + { + Characters[charIndex].Phase = phase; + Characters[charIndex].Pixels = new int[0]; + } - switch (ScreenMode) - { - case 0: - Characters[charIndex].Pixels = new int[4]; - break; - case 1: - Characters[charIndex].Pixels = new int[8]; - break; - case 2: - Characters[charIndex].Pixels = new int[16]; - break; - case 3: - Characters[charIndex].Pixels = new int[8]; - break; - } + /// + /// Adds a border character into the scanline + /// + private void AddBorderValue(int charIndex, int colourValue) + { + Characters[charIndex].Phase = RenderPhase.BORDER; - + switch (ScreenMode) + { + case 0: + Characters[charIndex].Pixels = new int[4]; + break; + case 1: + Characters[charIndex].Pixels = new int[8]; + break; + case 2: + Characters[charIndex].Pixels = new int[16]; + break; + case 3: + Characters[charIndex].Pixels = new int[8]; + break; + } - for (int i = 0; i < Characters[charIndex].Pixels.Length; i++) - { - Characters[charIndex].Pixels[i] = colourValue; - } - } - /// - /// Adds a display character into the scanline - /// Pixel matrix is calculated based on the current ScreenMode - /// - public void AddDisplayValue(int charIndex, byte vid1, byte vid2, int[] pens) - { - Characters[charIndex].Phase = RenderPhase.DISPLAY; - // generate pixels based on screen mode - switch (ScreenMode) - { - // 4 bits per pixel - 2 bytes - 4 pixels (8 CRT pixels) - // RECT - case 0: - Characters[charIndex].Pixels = new int[16]; + for (int i = 0; i < Characters[charIndex].Pixels.Length; i++) + { + Characters[charIndex].Pixels[i] = colourValue; + } + } - int m0Count = 0; + /// + /// Adds a display character into the scanline + /// Pixel matrix is calculated based on the current ScreenMode + /// + public void AddDisplayValue(int charIndex, byte vid1, byte vid2, int[] pens) + { + Characters[charIndex].Phase = RenderPhase.DISPLAY; - int pix = vid1 & 0xaa; - pix = ((pix & 0x80) >> 7) | ((pix & 0x08) >> 2) | ((pix & 0x20) >> 3) | ((pix & 0x02 << 2)); - Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; - Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; - Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; - Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; - pix = vid1 & 0x55; - pix = (((pix & 0x40) >> 6) | ((pix & 0x04) >> 1) | ((pix & 0x10) >> 2) | ((pix & 0x01 << 3))); - Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; - Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; - Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; - Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; + // generate pixels based on screen mode + switch (ScreenMode) + { + // 4 bits per pixel - 2 bytes - 4 pixels (8 CRT pixels) + // RECT + case 0: + Characters[charIndex].Pixels = new int[16]; - pix = vid2 & 0xaa; - pix = ((pix & 0x80) >> 7) | ((pix & 0x08) >> 2) | ((pix & 0x20) >> 3) | ((pix & 0x02 << 2)); - Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; - Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; - Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; - Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; - pix = vid2 & 0x55; - pix = (((pix & 0x40) >> 6) | ((pix & 0x04) >> 1) | ((pix & 0x10) >> 2) | ((pix & 0x01 << 3))); - Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; - Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; - Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; - Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; - /* + int m0Count = 0; + + int pix = vid1 & 0xaa; + pix = ((pix & 0x80) >> 7) | ((pix & 0x08) >> 2) | ((pix & 0x20) >> 3) | ((pix & 0x02 << 2)); + Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; + Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; + Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; + Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; + pix = vid1 & 0x55; + pix = (((pix & 0x40) >> 6) | ((pix & 0x04) >> 1) | ((pix & 0x10) >> 2) | ((pix & 0x01 << 3))); + Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; + Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; + Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; + Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; + + pix = vid2 & 0xaa; + pix = ((pix & 0x80) >> 7) | ((pix & 0x08) >> 2) | ((pix & 0x20) >> 3) | ((pix & 0x02 << 2)); + Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; + Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; + Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; + Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; + pix = vid2 & 0x55; + pix = (((pix & 0x40) >> 6) | ((pix & 0x04) >> 1) | ((pix & 0x10) >> 2) | ((pix & 0x01 << 3))); + Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; + Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; + Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; + Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[pix]]; + /* int m0B0P0i = vid1 & 0xaa; int m0B0P0 = ((m0B0P0i & 0x80) >> 7) | ((m0B0P0i & 0x08) >> 2) | ((m0B0P0i & 0x20) >> 3) | ((m0B0P0i & 0x02 << 2)); int m0B0P1i = vid1 & 85; @@ -431,245 +431,245 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[m0B1P1]]; Characters[charIndex].Pixels[m0Count++] = CRTDevice.CPCHardwarePalette[pens[m0B1P1]]; */ - break; + break; - // 2 bits per pixel - 2 bytes - 8 pixels (16 CRT pixels) - // SQUARE - case 1: - Characters[charIndex].Pixels = new int[8]; + // 2 bits per pixel - 2 bytes - 8 pixels (16 CRT pixels) + // SQUARE + case 1: + Characters[charIndex].Pixels = new int[8]; - int m1Count = 0; + int m1Count = 0; - int m1B0P0 = (((vid1 & 0x80) >> 7) | ((vid1 & 0x08) >> 2)); - int m1B0P1 = (((vid1 & 0x40) >> 6) | ((vid1 & 0x04) >> 1)); - int m1B0P2 = (((vid1 & 0x20) >> 5) | ((vid1 & 0x02))); - int m1B0P3 = (((vid1 & 0x10) >> 4) | ((vid1 & 0x01) << 1)); + int m1B0P0 = (((vid1 & 0x80) >> 7) | ((vid1 & 0x08) >> 2)); + int m1B0P1 = (((vid1 & 0x40) >> 6) | ((vid1 & 0x04) >> 1)); + int m1B0P2 = (((vid1 & 0x20) >> 5) | ((vid1 & 0x02))); + int m1B0P3 = (((vid1 & 0x10) >> 4) | ((vid1 & 0x01) << 1)); - Characters[charIndex].Pixels[m1Count++] = CRTDevice.CPCHardwarePalette[pens[m1B0P0]]; - Characters[charIndex].Pixels[m1Count++] = CRTDevice.CPCHardwarePalette[pens[m1B0P1]]; - Characters[charIndex].Pixels[m1Count++] = CRTDevice.CPCHardwarePalette[pens[m1B0P2]]; - Characters[charIndex].Pixels[m1Count++] = CRTDevice.CPCHardwarePalette[pens[m1B0P3]]; + Characters[charIndex].Pixels[m1Count++] = CRTDevice.CPCHardwarePalette[pens[m1B0P0]]; + Characters[charIndex].Pixels[m1Count++] = CRTDevice.CPCHardwarePalette[pens[m1B0P1]]; + Characters[charIndex].Pixels[m1Count++] = CRTDevice.CPCHardwarePalette[pens[m1B0P2]]; + Characters[charIndex].Pixels[m1Count++] = CRTDevice.CPCHardwarePalette[pens[m1B0P3]]; - int m1B1P0 = (((vid2 & 0x80) >> 7) | ((vid2 & 0x08) >> 2)); - int m1B1P1 = (((vid2 & 0x40) >> 6) | ((vid2 & 0x04) >> 1)); - int m1B1P2 = (((vid2 & 0x20) >> 5) | ((vid2 & 0x02))); - int m1B1P3 = (((vid2 & 0x10) >> 4) | ((vid2 & 0x01) << 1)); + int m1B1P0 = (((vid2 & 0x80) >> 7) | ((vid2 & 0x08) >> 2)); + int m1B1P1 = (((vid2 & 0x40) >> 6) | ((vid2 & 0x04) >> 1)); + int m1B1P2 = (((vid2 & 0x20) >> 5) | ((vid2 & 0x02))); + int m1B1P3 = (((vid2 & 0x10) >> 4) | ((vid2 & 0x01) << 1)); - Characters[charIndex].Pixels[m1Count++] = CRTDevice.CPCHardwarePalette[pens[m1B1P0]]; - Characters[charIndex].Pixels[m1Count++] = CRTDevice.CPCHardwarePalette[pens[m1B1P1]]; - Characters[charIndex].Pixels[m1Count++] = CRTDevice.CPCHardwarePalette[pens[m1B1P2]]; - Characters[charIndex].Pixels[m1Count++] = CRTDevice.CPCHardwarePalette[pens[m1B1P3]]; - break; + Characters[charIndex].Pixels[m1Count++] = CRTDevice.CPCHardwarePalette[pens[m1B1P0]]; + Characters[charIndex].Pixels[m1Count++] = CRTDevice.CPCHardwarePalette[pens[m1B1P1]]; + Characters[charIndex].Pixels[m1Count++] = CRTDevice.CPCHardwarePalette[pens[m1B1P2]]; + Characters[charIndex].Pixels[m1Count++] = CRTDevice.CPCHardwarePalette[pens[m1B1P3]]; + break; - // 1 bit per pixel - 2 bytes - 16 pixels (16 CRT pixels) - // RECT - case 2: - Characters[charIndex].Pixels = new int[16]; + // 1 bit per pixel - 2 bytes - 16 pixels (16 CRT pixels) + // RECT + case 2: + Characters[charIndex].Pixels = new int[16]; - int m2Count = 0; + int m2Count = 0; - int[] pixBuff = new int[16]; + int[] pixBuff = new int[16]; - for (int bit = 7; bit >= 0; bit--) - { - int val = vid1.Bit(bit) ? 1 : 0; - Characters[charIndex].Pixels[m2Count++] = CRTDevice.CPCHardwarePalette[pens[val]]; + for (int bit = 7; bit >= 0; bit--) + { + int val = vid1.Bit(bit) ? 1 : 0; + Characters[charIndex].Pixels[m2Count++] = CRTDevice.CPCHardwarePalette[pens[val]]; - } - for (int bit = 7; bit >= 0; bit--) - { - int val = vid2.Bit(bit) ? 1 : 0; - Characters[charIndex].Pixels[m2Count++] = CRTDevice.CPCHardwarePalette[pens[val]]; - } - break; + } + for (int bit = 7; bit >= 0; bit--) + { + int val = vid2.Bit(bit) ? 1 : 0; + Characters[charIndex].Pixels[m2Count++] = CRTDevice.CPCHardwarePalette[pens[val]]; + } + break; - // 4 bits per pixel - 2 bytes - 4 pixels (8 CRT pixels) - // RECT - case 3: - Characters[charIndex].Pixels = new int[4]; + // 4 bits per pixel - 2 bytes - 4 pixels (8 CRT pixels) + // RECT + case 3: + Characters[charIndex].Pixels = new int[4]; - int m3Count = 0; + int m3Count = 0; - int m3B0P0i = vid1 & 170; - int m3B0P0 = ((m3B0P0i & 0x80) >> 7) | ((m3B0P0i & 0x08) >> 2) | ((m3B0P0i & 0x20) >> 3) | ((m3B0P0i & 0x02 << 2)); - int m3B0P1i = vid1 & 85; - int m3B0P1 = ((m3B0P1i & 0x40) >> 6) | ((m3B0P1i & 0x04) >> 1) | ((m3B0P1i & 0x10) >> 2) | ((m3B0P1i & 0x01 << 3)); + int m3B0P0i = vid1 & 170; + int m3B0P0 = ((m3B0P0i & 0x80) >> 7) | ((m3B0P0i & 0x08) >> 2) | ((m3B0P0i & 0x20) >> 3) | ((m3B0P0i & 0x02 << 2)); + int m3B0P1i = vid1 & 85; + int m3B0P1 = ((m3B0P1i & 0x40) >> 6) | ((m3B0P1i & 0x04) >> 1) | ((m3B0P1i & 0x10) >> 2) | ((m3B0P1i & 0x01 << 3)); - Characters[charIndex].Pixels[m3Count++] = CRTDevice.CPCHardwarePalette[pens[m3B0P0]]; - Characters[charIndex].Pixels[m3Count++] = CRTDevice.CPCHardwarePalette[pens[m3B0P1]]; + Characters[charIndex].Pixels[m3Count++] = CRTDevice.CPCHardwarePalette[pens[m3B0P0]]; + Characters[charIndex].Pixels[m3Count++] = CRTDevice.CPCHardwarePalette[pens[m3B0P1]]; - int m3B1P0i = vid1 & 170; - int m3B1P0 = ((m3B1P0i & 0x80) >> 7) | ((m3B1P0i & 0x08) >> 2) | ((m3B1P0i & 0x20) >> 3) | ((m3B1P0i & 0x02 << 2)); - int m3B1P1i = vid1 & 85; - int m3B1P1 = ((m3B1P1i & 0x40) >> 6) | ((m3B1P1i & 0x04) >> 1) | ((m3B1P1i & 0x10) >> 2) | ((m3B1P1i & 0x01 << 3)); + int m3B1P0i = vid1 & 170; + int m3B1P0 = ((m3B1P0i & 0x80) >> 7) | ((m3B1P0i & 0x08) >> 2) | ((m3B1P0i & 0x20) >> 3) | ((m3B1P0i & 0x02 << 2)); + int m3B1P1i = vid1 & 85; + int m3B1P1 = ((m3B1P1i & 0x40) >> 6) | ((m3B1P1i & 0x04) >> 1) | ((m3B1P1i & 0x10) >> 2) | ((m3B1P1i & 0x01 << 3)); - Characters[charIndex].Pixels[m3Count++] = CRTDevice.CPCHardwarePalette[pens[m3B1P0]]; - Characters[charIndex].Pixels[m3Count++] = CRTDevice.CPCHardwarePalette[pens[m3B1P1]]; - break; - } - } + Characters[charIndex].Pixels[m3Count++] = CRTDevice.CPCHardwarePalette[pens[m3B1P0]]; + Characters[charIndex].Pixels[m3Count++] = CRTDevice.CPCHardwarePalette[pens[m3B1P1]]; + break; + } + } - /// - /// Returns the number of pixels decoded in this scanline (border and display) - /// - private int GetPixelCount() - { - int cnt = 0; + /// + /// Returns the number of pixels decoded in this scanline (border and display) + /// + private int GetPixelCount() + { + int cnt = 0; - foreach (var c in Characters) - { - if (c.Pixels != null) - cnt += c.Pixels.Length; - } + foreach (var c in Characters) + { + if (c.Pixels != null) + cnt += c.Pixels.Length; + } - return cnt; - } + return cnt; + } - /// - /// Called at the start of HSYNC - /// Processes and adds the scanline to the Screen Buffer - /// - public void CommitScanline() - { - int hScale = 1; - int vScale = 1; + /// + /// Called at the start of HSYNC + /// Processes and adds the scanline to the Screen Buffer + /// + public void CommitScanline() + { + int hScale = 1; + int vScale = 1; - switch (ScreenMode) - { - case 0: - hScale = 1; - vScale = 2; - break; - case 1: - case 3: - hScale = 2; - vScale = 2; - break; + switch (ScreenMode) + { + case 0: + hScale = 1; + vScale = 2; + break; + case 1: + case 3: + hScale = 2; + vScale = 2; + break; - case 2: - hScale = 1; - vScale = 2; - break; - } + case 2: + hScale = 1; + vScale = 2; + break; + } - int hPix = GetPixelCount() * hScale; - //int hPix = GetPixelCount() * 2; - int leftOver = CRT.BufferWidth - hPix; - int lPad = leftOver / 2; - int rPad = lPad; - int rem = leftOver % 2; - if (rem != 0) - rPad += rem; + int hPix = GetPixelCount() * hScale; + //int hPix = GetPixelCount() * 2; + int leftOver = CRT.BufferWidth - hPix; + int lPad = leftOver / 2; + int rPad = lPad; + int rem = leftOver % 2; + if (rem != 0) + rPad += rem; - if (LineIndex < CRT.TopLinesToTrim) - { - return; - } + if (LineIndex < CRT.TopLinesToTrim) + { + return; + } - // render out the scanline - int pCount = (LineIndex - CRT.TopLinesToTrim) * vScale * CRT.BufferWidth; + // render out the scanline + int pCount = (LineIndex - CRT.TopLinesToTrim) * vScale * CRT.BufferWidth; - // vScale - for (int s = 0; s < vScale; s++) - { - // left padding - for (int lP = 0; lP < lPad; lP++) - { - CRT.ScreenBuffer[pCount++] = 0; - } + // vScale + for (int s = 0; s < vScale; s++) + { + // left padding + for (int lP = 0; lP < lPad; lP++) + { + CRT.ScreenBuffer[pCount++] = 0; + } - // border and display - foreach (var c in Characters) - { - if (c.Pixels == null || c.Pixels.Length == 0) - continue; + // border and display + foreach (var c in Characters) + { + if (c.Pixels == null || c.Pixels.Length == 0) + continue; - for (int p = 0; p < c.Pixels.Length; p++) - { - // hScale - for (int h = 0; h < hScale; h++) - { - CRT.ScreenBuffer[pCount++] = c.Pixels[p]; - } - - //CRT.ScreenBuffer[pCount++] = c.Pixels[p]; - } - } + for (int p = 0; p < c.Pixels.Length; p++) + { + // hScale + for (int h = 0; h < hScale; h++) + { + CRT.ScreenBuffer[pCount++] = c.Pixels[p]; + } - // right padding - for (int rP = 0; rP < rPad; rP++) - { - CRT.ScreenBuffer[pCount++] = 0; - } + //CRT.ScreenBuffer[pCount++] = c.Pixels[p]; + } + } - if (pCount != hPix) - { + // right padding + for (int rP = 0; rP < rPad; rP++) + { + CRT.ScreenBuffer[pCount++] = 0; + } - } + if (pCount != hPix) + { - CRT.ScanlineCounter++; - } - } + } - public void Reset() - { - ScreenMode = 1; - Characters = new Character[64]; + CRT.ScanlineCounter++; + } + } - for (int i = 0; i < Characters.Length; i++) - { - Characters[i] = new Character(); - } - } - } + public void Reset() + { + ScreenMode = 1; + Characters = new Character[64]; - /// - /// Contains data relating to one character written on one scanline - /// - public class Character - { - /// - /// Array of pixels generated for this character - /// - public int[] Pixels; + for (int i = 0; i < Characters.Length; i++) + { + Characters[i] = new Character(); + } + } + } - /// - /// The type (NONE/BORDER/DISPLAY/HSYNC/VSYNC/HSYNC+VSYNC - /// - public RenderPhase Phase = RenderPhase.NONE; + /// + /// Contains data relating to one character written on one scanline + /// + public class Character + { + /// + /// Array of pixels generated for this character + /// + public int[] Pixels; - public Character() - { - Pixels = new int[0]; - } - } + /// + /// The type (NONE/BORDER/DISPLAY/HSYNC/VSYNC/HSYNC+VSYNC + /// + public RenderPhase Phase = RenderPhase.NONE; - [Flags] - public enum RenderPhase : int - { - /// - /// Nothing - /// - NONE = 0, - /// - /// Border is being rendered - /// - BORDER = 1, - /// - /// Display rendered from video RAM - /// - DISPLAY = 2, - /// - /// HSYNC in progress - /// - HSYNC = 3, - /// - /// VSYNC in process - /// - VSYNC = 4, - /// - /// HSYNC occurs within a VSYNC - /// - HSYNCandVSYNC = 5 - } + public Character() + { + Pixels = new int[0]; + } + } + + [Flags] + public enum RenderPhase : int + { + /// + /// Nothing + /// + NONE = 0, + /// + /// Border is being rendered + /// + BORDER = 1, + /// + /// Display rendered from video RAM + /// + DISPLAY = 2, + /// + /// HSYNC in progress + /// + HSYNC = 3, + /// + /// VSYNC in process + /// + VSYNC = 4, + /// + /// HSYNC occurs within a VSYNC + /// + HSYNCandVSYNC = 5 + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Input/StandardKeyboard.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Input/StandardKeyboard.cs index 993943a333..5ceb1bcae3 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Input/StandardKeyboard.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Input/StandardKeyboard.cs @@ -6,61 +6,61 @@ using System.Linq; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// The 48k keyboard device - /// - public class StandardKeyboard : IKeyboard - { - public CPCBase _machine { get; set; } + /// + /// The 48k keyboard device + /// + public class StandardKeyboard : IKeyboard + { + public CPCBase _machine { get; set; } - private int _currentLine; - public int CurrentLine - { - get { return _currentLine; } - set - { - // bits 0-3 contain the line - var line = value & 0x0f; + private int _currentLine; + public int CurrentLine + { + get { return _currentLine; } + set + { + // bits 0-3 contain the line + var line = value & 0x0f; - if (line > 0) - { + if (line > 0) + { - } + } - _currentLine = line; - } - } + _currentLine = line; + } + } - private bool[] _keyStatus; - public bool[] KeyStatus - { - get { return _keyStatus; } - set { _keyStatus = value; } - } + private bool[] _keyStatus; + public bool[] KeyStatus + { + get { return _keyStatus; } + set { _keyStatus = value; } + } - private string[] _keyboardMatrix; - public string[] KeyboardMatrix - { - get { return _keyboardMatrix; } - set { _keyboardMatrix = value; } - } + private string[] _keyboardMatrix; + public string[] KeyboardMatrix + { + get { return _keyboardMatrix; } + set { _keyboardMatrix = value; } + } - private string[] _nonMatrixKeys; - public string[] NonMatrixKeys - { - get { return _nonMatrixKeys; } - set { _nonMatrixKeys = value; } - } + private string[] _nonMatrixKeys; + public string[] NonMatrixKeys + { + get { return _nonMatrixKeys; } + set { _nonMatrixKeys = value; } + } - public StandardKeyboard(CPCBase machine) - { - _machine = machine; - //_machine.AYDevice.PortA_IN_CallBack = INCallback; - //_machine.AYDevice.PortA_OUT_CallBack = OUTCallback; + public StandardKeyboard(CPCBase machine) + { + _machine = machine; + //_machine.AYDevice.PortA_IN_CallBack = INCallback; + //_machine.AYDevice.PortA_OUT_CallBack = OUTCallback; - // scancode rows, ascending (Bit0 - Bit7) - KeyboardMatrix = new string[] - { + // scancode rows, ascending (Bit0 - Bit7) + KeyboardMatrix = new string[] + { // 0x40 "Key CURUP", "Key CURRIGHT", "Key CURDOWN", "Key NUM9", "Key NUM6", "Key NUM3", "Key ENTER", "Key NUMPERIOD", // 0x41 @@ -82,72 +82,72 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC // 0x49 "P1 Up", "P1 Down", "P1 Left", "P1 Right", "P1 Fire1", "P1 Fire2", "P1 Fire3", "Key DEL", - }; + }; - // keystatus array to match the matrix - KeyStatus = new bool[8 * 10]; - - // nonmatrix keys (anything that hasnt already been taken) - var nonMatrix = new List(); - - foreach (var key in _machine.CPC.AmstradCPCControllerDefinition.BoolButtons) - { - if (!KeyboardMatrix.Any(s => s == key)) - nonMatrix.Add(key); - } - - NonMatrixKeys = nonMatrix.ToArray(); - } + // keystatus array to match the matrix + KeyStatus = new bool[8 * 10]; - /// - /// Reads the currently selected line - /// - public byte ReadCurrentLine() - { - var lin = _currentLine; // - 0x40; - var pos = lin * 8; - var l = KeyStatus.Skip(pos).Take(8).ToArray(); - BitArray bi = new BitArray(l); - byte[] bytes = new byte[1]; - bi.CopyTo(bytes, 0); - byte inv = (byte)(~bytes[0]); - return inv; - } + // nonmatrix keys (anything that hasnt already been taken) + var nonMatrix = new List(); - /// - /// Returns the index of the key within the matrix - /// - public int GetKeyIndexFromMatrix(string key) - { - int index = Array.IndexOf(KeyboardMatrix, key); - return index; - } + foreach (var key in _machine.CPC.AmstradCPCControllerDefinition.BoolButtons) + { + if (!KeyboardMatrix.Any(s => s == key)) + nonMatrix.Add(key); + } - /// - /// Sets key status - /// - public void SetKeyStatus(string key, bool isPressed) - { - int index = GetKeyIndexFromMatrix(key); - KeyStatus[index] = isPressed; - } + NonMatrixKeys = nonMatrix.ToArray(); + } - /// - /// Gets a key's status - /// - public bool GetKeyStatus(string key) - { - int index = GetKeyIndexFromMatrix(key); - return KeyStatus[index]; - } + /// + /// Reads the currently selected line + /// + public byte ReadCurrentLine() + { + var lin = _currentLine; // - 0x40; + var pos = lin * 8; + var l = KeyStatus.Skip(pos).Take(8).ToArray(); + BitArray bi = new BitArray(l); + byte[] bytes = new byte[1]; + bi.CopyTo(bytes, 0); + byte inv = (byte)(~bytes[0]); + return inv; + } + + /// + /// Returns the index of the key within the matrix + /// + public int GetKeyIndexFromMatrix(string key) + { + int index = Array.IndexOf(KeyboardMatrix, key); + return index; + } + + /// + /// Sets key status + /// + public void SetKeyStatus(string key, bool isPressed) + { + int index = GetKeyIndexFromMatrix(key); + KeyStatus[index] = isPressed; + } + + /// + /// Gets a key's status + /// + public bool GetKeyStatus(string key) + { + int index = GetKeyIndexFromMatrix(key); + return KeyStatus[index]; + } - public void SyncState(Serializer ser) - { - ser.BeginSection("Keyboard"); - ser.Sync("currentLine", ref _currentLine); - ser.Sync("keyStatus", ref _keyStatus, false); - ser.EndSection(); - } - } + public void SyncState(Serializer ser) + { + ser.BeginSection("Keyboard"); + ser.Sync("currentLine", ref _currentLine); + ser.Sync("keyStatus", ref _keyStatus, false); + ser.EndSection(); + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/PPI/PPI_8255.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/PPI/PPI_8255.cs index 1e7822e773..cfc69b6744 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/PPI/PPI_8255.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/PPI/PPI_8255.cs @@ -5,456 +5,456 @@ using System.Collections; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Emulates the PPI (8255) chip - /// http://www.cpcwiki.eu/imgs/d/df/PPI_M5L8255AP-5.pdf - /// http://www.cpcwiki.eu/index.php/8255 - /// - public class PPI_8255 : IPortIODevice - { - #region Devices + /// + /// Emulates the PPI (8255) chip + /// http://www.cpcwiki.eu/imgs/d/df/PPI_M5L8255AP-5.pdf + /// http://www.cpcwiki.eu/index.php/8255 + /// + public class PPI_8255 : IPortIODevice + { + #region Devices - private CPCBase _machine; - private CRCT_6845 CRTC => _machine.CRCT; - private AmstradGateArray GateArray => _machine.GateArray; - private IPSG PSG => _machine.AYDevice; - private DatacorderDevice Tape => _machine.TapeDevice; - private IKeyboard Keyboard => _machine.KeyboardDevice; + private CPCBase _machine; + private CRCT_6845 CRTC => _machine.CRCT; + private AmstradGateArray GateArray => _machine.GateArray; + private IPSG PSG => _machine.AYDevice; + private DatacorderDevice Tape => _machine.TapeDevice; + private IKeyboard Keyboard => _machine.KeyboardDevice; - #endregion + #endregion - #region Construction + #region Construction - public PPI_8255(CPCBase machine) - { - _machine = machine; - Reset(); - } + public PPI_8255(CPCBase machine) + { + _machine = machine; + Reset(); + } - #endregion + #endregion - #region Implementation + #region Implementation - /// - /// BDIR Line connected to PSG - /// - public bool BDIR - { - get { return Regs[PORT_C].Bit(7); } - } + /// + /// BDIR Line connected to PSG + /// + public bool BDIR + { + get { return Regs[PORT_C].Bit(7); } + } - /// - /// BC1 Line connected to PSG - /// - public bool BC1 - { - get { return Regs[PORT_C].Bit(6); } - } + /// + /// BC1 Line connected to PSG + /// + public bool BC1 + { + get { return Regs[PORT_C].Bit(6); } + } - /* Port Constants */ - private const int PORT_A = 0; - private const int PORT_B = 1; - private const int PORT_C = 2; - private const int PORT_CONTROL = 3; + /* Port Constants */ + private const int PORT_A = 0; + private const int PORT_B = 1; + private const int PORT_C = 2; + private const int PORT_CONTROL = 3; - /// - /// The i8255 internal data registers - /// - private byte[] Regs = new byte[4]; + /// + /// The i8255 internal data registers + /// + private byte[] Regs = new byte[4]; - /// - /// Returns the currently latched port direction for Port A - /// - private PortDirection DirPortA - { - get { return Regs[PORT_CONTROL].Bit(4) ? PortDirection.Input : PortDirection.Output; } - } + /// + /// Returns the currently latched port direction for Port A + /// + private PortDirection DirPortA + { + get { return Regs[PORT_CONTROL].Bit(4) ? PortDirection.Input : PortDirection.Output; } + } - /// - /// Returns the currently latched port direction for Port B - /// - private PortDirection DirPortB - { - get { return Regs[PORT_CONTROL].Bit(1) ? PortDirection.Input : PortDirection.Output; } - } + /// + /// Returns the currently latched port direction for Port B + /// + private PortDirection DirPortB + { + get { return Regs[PORT_CONTROL].Bit(1) ? PortDirection.Input : PortDirection.Output; } + } - /// - /// Returns the currently latched port direction for Port C (lower half) - /// - private PortDirection DirPortCL - { - get { return Regs[PORT_CONTROL].Bit(0) ? PortDirection.Input : PortDirection.Output; } - } + /// + /// Returns the currently latched port direction for Port C (lower half) + /// + private PortDirection DirPortCL + { + get { return Regs[PORT_CONTROL].Bit(0) ? PortDirection.Input : PortDirection.Output; } + } - /// - /// Returns the currently latched port direction for Port C (upper half) - /// - private PortDirection DirPortCU - { - get { return Regs[PORT_CONTROL].Bit(3) ? PortDirection.Input : PortDirection.Output; } - } + /// + /// Returns the currently latched port direction for Port C (upper half) + /// + private PortDirection DirPortCU + { + get { return Regs[PORT_CONTROL].Bit(3) ? PortDirection.Input : PortDirection.Output; } + } - #region OUT Methods + #region OUT Methods - /// - /// Writes to Port A - /// - private void OUTPortA(int data) - { - // latch the data - Regs[PORT_A] = (byte)data; + /// + /// Writes to Port A + /// + private void OUTPortA(int data) + { + // latch the data + Regs[PORT_A] = (byte)data; - if (DirPortA == PortDirection.Output) - { - // PSG write - PSG.PortWrite(data); - } - } + if (DirPortA == PortDirection.Output) + { + // PSG write + PSG.PortWrite(data); + } + } - /// - /// Writes to Port B - /// - private void OUTPortB(int data) - { - // PortB is read only - // just latch the data - Regs[PORT_B] = (byte)data; - } + /// + /// Writes to Port B + /// + private void OUTPortB(int data) + { + // PortB is read only + // just latch the data + Regs[PORT_B] = (byte)data; + } - /// - /// Writes to Port C - /// - private void OUTPortC(int data) - { - // latch the data - Regs[PORT_C] = (byte)data; + /// + /// Writes to Port C + /// + private void OUTPortC(int data) + { + // latch the data + Regs[PORT_C] = (byte)data; - if (DirPortCL == PortDirection.Output) - { - // lower Port C bits OUT - // keyboard line update - Keyboard.CurrentLine = Regs[PORT_C] & 0x0f; - } + if (DirPortCL == PortDirection.Output) + { + // lower Port C bits OUT + // keyboard line update + Keyboard.CurrentLine = Regs[PORT_C] & 0x0f; + } - if (DirPortCU == PortDirection.Output) - { - // upper Port C bits OUT - // write to PSG using latched data - PSG.SetFunction(data); - PSG.PortWrite(Regs[PORT_A]); + if (DirPortCU == PortDirection.Output) + { + // upper Port C bits OUT + // write to PSG using latched data + PSG.SetFunction(data); + PSG.PortWrite(Regs[PORT_A]); - // cassete write data - //not implemeted + // cassete write data + //not implemeted - // cas motor control - Tape.TapeMotor = Regs[PORT_C].Bit(4); - } - } + // cas motor control + Tape.TapeMotor = Regs[PORT_C].Bit(4); + } + } - /// - /// Writes to the control register - /// - private void OUTControl(int data) - { - if (data.Bit(7)) - { - // update configuration - Regs[PORT_CONTROL] = (byte)data; + /// + /// Writes to the control register + /// + private void OUTControl(int data) + { + if (data.Bit(7)) + { + // update configuration + Regs[PORT_CONTROL] = (byte)data; - // Writing to PIO Control Register (with Bit7 set), automatically resets PIO Ports A,B,C to 00h each - Regs[PORT_A] = 0; - Regs[PORT_B] = 0; - Regs[PORT_C] = 0; - } - else - { - // register is used to set/reset a single bit in Port C - bool isSet = data.Bit(0); + // Writing to PIO Control Register (with Bit7 set), automatically resets PIO Ports A,B,C to 00h each + Regs[PORT_A] = 0; + Regs[PORT_B] = 0; + Regs[PORT_C] = 0; + } + else + { + // register is used to set/reset a single bit in Port C + bool isSet = data.Bit(0); - // get the bit in PortC that we wish to change - var bit = (data >> 1) & 7; + // get the bit in PortC that we wish to change + var bit = (data >> 1) & 7; - // modify this bit - if (isSet) - { - Regs[PORT_C] = (byte)(Regs[PORT_C] | (bit * bit)); - } - else - { - Regs[PORT_C] = (byte)(Regs[PORT_C] & ~(bit * bit)); - } + // modify this bit + if (isSet) + { + Regs[PORT_C] = (byte)(Regs[PORT_C] | (bit * bit)); + } + else + { + Regs[PORT_C] = (byte)(Regs[PORT_C] & ~(bit * bit)); + } - // any other ouput business - if (DirPortCL == PortDirection.Output) - { - // update keyboard line - Keyboard.CurrentLine = Regs[PORT_C] & 0x0f; - } + // any other ouput business + if (DirPortCL == PortDirection.Output) + { + // update keyboard line + Keyboard.CurrentLine = Regs[PORT_C] & 0x0f; + } - if (DirPortCU == PortDirection.Output) - { - // write to PSG using latched data - PSG.SetFunction(data); - PSG.PortWrite(Regs[PORT_A]); + if (DirPortCU == PortDirection.Output) + { + // write to PSG using latched data + PSG.SetFunction(data); + PSG.PortWrite(Regs[PORT_A]); - // cassete write data - //not implemeted + // cassete write data + //not implemeted - // cas motor control - Tape.TapeMotor = Regs[PORT_C].Bit(4); - } - } - } + // cas motor control + Tape.TapeMotor = Regs[PORT_C].Bit(4); + } + } + } - #endregion + #endregion - #region IN Methods + #region IN Methods - /// - /// Reads from Port A - /// - private int INPortA() - { - if (DirPortA == PortDirection.Input) - { - // read from PSG - return PSG.PortRead(); - } - else - { - // Port A is set to output - // return latched value - return Regs[PORT_A]; - } - } + /// + /// Reads from Port A + /// + private int INPortA() + { + if (DirPortA == PortDirection.Input) + { + // read from PSG + return PSG.PortRead(); + } + else + { + // Port A is set to output + // return latched value + return Regs[PORT_A]; + } + } - /// - /// Reads from Port B - /// - private int INPortB() - { - if (DirPortB == PortDirection.Input) - { - // build the PortB output - // start with every bit reset - BitArray rBits = new BitArray(8); + /// + /// Reads from Port B + /// + private int INPortB() + { + if (DirPortB == PortDirection.Input) + { + // build the PortB output + // start with every bit reset + BitArray rBits = new BitArray(8); - // Bit0 - Vertical Sync ("1"=VSYNC active, "0"=VSYNC inactive) - if (CRTC.VSYNC) - rBits[0] = true; + // Bit0 - Vertical Sync ("1"=VSYNC active, "0"=VSYNC inactive) + if (CRTC.VSYNC) + rBits[0] = true; - // Bits1-3 - Distributor ID. Usually set to 4=Awa, 5=Schneider, or 7=Amstrad - // force AMstrad - rBits[1] = true; - rBits[2] = true; - rBits[3] = true; + // Bits1-3 - Distributor ID. Usually set to 4=Awa, 5=Schneider, or 7=Amstrad + // force AMstrad + rBits[1] = true; + rBits[2] = true; + rBits[3] = true; - // Bit4 - Screen Refresh Rate ("1"=50Hz, "0"=60Hz) - rBits[4] = true; + // Bit4 - Screen Refresh Rate ("1"=50Hz, "0"=60Hz) + rBits[4] = true; - // Bit5 - Expansion Port /EXP pin - rBits[5] = false; + // Bit5 - Expansion Port /EXP pin + rBits[5] = false; - // Bit6 - Parallel/Printer port ready signal, "1" = not ready, "0" = Ready - rBits[6] = true; + // Bit6 - Parallel/Printer port ready signal, "1" = not ready, "0" = Ready + rBits[6] = true; - // Bit7 - Cassette data input - rBits[7] = Tape.GetEarBit(_machine.CPU.TotalExecutedCycles); + // Bit7 - Cassette data input + rBits[7] = Tape.GetEarBit(_machine.CPU.TotalExecutedCycles); - // return the byte - byte[] bytes = new byte[1]; - rBits.CopyTo(bytes, 0); - return bytes[0]; - } - else - { - // return the latched value - return Regs[PORT_B]; - } - } + // return the byte + byte[] bytes = new byte[1]; + rBits.CopyTo(bytes, 0); + return bytes[0]; + } + else + { + // return the latched value + return Regs[PORT_B]; + } + } - /// - /// Reads from Port C - /// - private int INPortC() - { - // get the PortC value - int val = Regs[PORT_C]; + /// + /// Reads from Port C + /// + private int INPortC() + { + // get the PortC value + int val = Regs[PORT_C]; - if (DirPortCU == PortDirection.Input) - { - // upper port C bits - // remove upper half - val &= 0x0f; + if (DirPortCU == PortDirection.Input) + { + // upper port C bits + // remove upper half + val &= 0x0f; - // isolate control bits - var v = Regs[PORT_C] & 0xc0; + // isolate control bits + var v = Regs[PORT_C] & 0xc0; - if (v == 0xc0) - { - // set reg is present. change to write reg - v = 0x80; - } + if (v == 0xc0) + { + // set reg is present. change to write reg + v = 0x80; + } - // cas wr is always set - val |= v | 0x20; + // cas wr is always set + val |= v | 0x20; - if (Tape.TapeMotor) - { - val |= 0x10; - } - } + if (Tape.TapeMotor) + { + val |= 0x10; + } + } - if (DirPortCL == PortDirection.Input) - { - // lower port C bits - val |= 0x0f; - } + if (DirPortCL == PortDirection.Input) + { + // lower port C bits + val |= 0x0f; + } - return val; - } + return val; + } - #endregion + #endregion - #endregion + #endregion - #region Reset + #region Reset - public void Reset() - { - for (int i = 0; i < 3; i++) - { - Regs[i] = 0xff; - } + public void Reset() + { + for (int i = 0; i < 3; i++) + { + Regs[i] = 0xff; + } - Regs[3] = 0xff; - } + Regs[3] = 0xff; + } - #endregion + #endregion - #region IPortIODevice + #region IPortIODevice - /* + /* #F4XX %xxxx0x00 xxxxxxxx 8255 PIO Port A (PSG Data) Read Write #F5XX %xxxx0x01 xxxxxxxx 8255 PIO Port B (Vsync,PrnBusy,Tape,etc.) Read - #F6XX %xxxx0x10 xxxxxxxx 8255 PIO Port C (KeybRow,Tape,PSG Control) - Write #F7XX %xxxx0x11 xxxxxxxx 8255 PIO Control-Register - Write */ - /// - /// Device responds to an IN instruction - /// - public bool ReadPort(ushort port, ref int result) - { - byte portUpper = (byte)(port >> 8); - byte portLower = (byte)(port & 0xff); + /// + /// Device responds to an IN instruction + /// + public bool ReadPort(ushort port, ref int result) + { + byte portUpper = (byte)(port >> 8); + byte portLower = (byte)(port & 0xff); - // The 8255 responds to bit 11 reset with A10 and A12-A15 set - //if (portUpper.Bit(3)) - //return false; + // The 8255 responds to bit 11 reset with A10 and A12-A15 set + //if (portUpper.Bit(3)) + //return false; - var PPIFunc = (port & 0x0300) >> 8; // portUpper & 3; + var PPIFunc = (port & 0x0300) >> 8; // portUpper & 3; - switch (PPIFunc) - { - // Port A Read - case 0: - - // PSG (Sound/Keyboard/Joystick) - result = INPortA(); + switch (PPIFunc) + { + // Port A Read + case 0: - break; + // PSG (Sound/Keyboard/Joystick) + result = INPortA(); - // Port B Read - case 1: + break; - // Vsync/Jumpers/PrinterBusy/CasIn/Exp - result = INPortB(); + // Port B Read + case 1: - break; + // Vsync/Jumpers/PrinterBusy/CasIn/Exp + result = INPortB(); - // Port C Read (docs define this as write-only but we do need to do some processing) - case 2: + break; - // KeybRow/CasOut/PSG - result = INPortC(); + // Port C Read (docs define this as write-only but we do need to do some processing) + case 2: - break; - } + // KeybRow/CasOut/PSG + result = INPortC(); - return true; - } + break; + } - /// - /// Device responds to an OUT instruction - /// - public bool WritePort(ushort port, int result) - { - byte portUpper = (byte)(port >> 8); - byte portLower = (byte)(port & 0xff); + return true; + } - // The 8255 responds to bit 11 reset with A10 and A12-A15 set - if (portUpper.Bit(3)) - return false; + /// + /// Device responds to an OUT instruction + /// + public bool WritePort(ushort port, int result) + { + byte portUpper = (byte)(port >> 8); + byte portLower = (byte)(port & 0xff); - var PPIFunc = portUpper & 3; + // The 8255 responds to bit 11 reset with A10 and A12-A15 set + if (portUpper.Bit(3)) + return false; - switch (PPIFunc) - { - // Port A Write - case 0: + var PPIFunc = portUpper & 3; - // PSG (Sound/Keyboard/Joystick) - OUTPortA(result); + switch (PPIFunc) + { + // Port A Write + case 0: - break; + // PSG (Sound/Keyboard/Joystick) + OUTPortA(result); - // Port B Write - case 1: + break; - // Vsync/Jumpers/PrinterBusy/CasIn/Exp - OUTPortB(result); + // Port B Write + case 1: - break; + // Vsync/Jumpers/PrinterBusy/CasIn/Exp + OUTPortB(result); - // Port C Write - case 2: + break; - // KeybRow/CasOut/PSG - OUTPortC(result); + // Port C Write + case 2: - break; + // KeybRow/CasOut/PSG + OUTPortC(result); - // Control Register Write - case 3: + break; - // Control - OUTControl((byte)result); + // Control Register Write + case 3: - break; - } + // Control + OUTControl((byte)result); - return true; - } + break; + } - #endregion + return true; + } - #region Serialization + #endregion - public void SyncState(Serializer ser) - { - ser.BeginSection("PPI"); - ser.Sync(nameof(Regs), ref Regs, false); - ser.EndSection(); - } + #region Serialization - #endregion - } + public void SyncState(Serializer ser) + { + ser.BeginSection("PPI"); + ser.Sync(nameof(Regs), ref Regs, false); + ser.EndSection(); + } - public enum PortDirection - { - Input, - Output - } + #endregion + } + + public enum PortDirection + { + Input, + Output + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/SoundOutput/AY38912.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/SoundOutput/AY38912.cs index 34057aa8c6..67e329f654 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/SoundOutput/AY38912.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/SoundOutput/AY38912.cs @@ -5,144 +5,144 @@ using System.Collections.Generic; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Based heavily on the YM-2149F / AY-3-8910 emulator used in Unreal Speccy - /// (Originally created under Public Domain license by SMT jan.2006) /// - /// https://github.com/mkoloberdin/unrealspeccy/blob/master/sndrender/sndchip.cpp - /// https://github.com/mkoloberdin/unrealspeccy/blob/master/sndrender/sndchip.h - /// - public class AY38912 : IPSG - { - #region Device Fields + /// + /// Based heavily on the YM-2149F / AY-3-8910 emulator used in Unreal Speccy + /// (Originally created under Public Domain license by SMT jan.2006) /// + /// https://github.com/mkoloberdin/unrealspeccy/blob/master/sndrender/sndchip.cpp + /// https://github.com/mkoloberdin/unrealspeccy/blob/master/sndrender/sndchip.h + /// + public class AY38912 : IPSG + { + #region Device Fields - /// - /// The emulated machine (passed in via constructor) - /// - private CPCBase _machine; - private IKeyboard _keyboard => _machine.KeyboardDevice; + /// + /// The emulated machine (passed in via constructor) + /// + private CPCBase _machine; + private IKeyboard _keyboard => _machine.KeyboardDevice; - private int _tStatesPerFrame; - private int _sampleRate; - private int _samplesPerFrame; - private double _tStatesPerSample; - private short[] _audioBuffer; - private int _audioBufferIndex; - private int _lastStateRendered; - //private int _clockCyclesPerFrame; - //private int _cyclesPerSample; + private int _tStatesPerFrame; + private int _sampleRate; + private int _samplesPerFrame; + private double _tStatesPerSample; + private short[] _audioBuffer; + private int _audioBufferIndex; + private int _lastStateRendered; + //private int _clockCyclesPerFrame; + //private int _cyclesPerSample; - #endregion + #endregion - #region Construction & Initialization + #region Construction & Initialization - /// - /// Main constructor - /// - public AY38912(CPCBase machine) - { - _machine = machine; + /// + /// Main constructor + /// + public AY38912(CPCBase machine) + { + _machine = machine; - //_blipL.SetRates(1000000, 44100); - //_blipL.SetRates((_machine.GateArray.FrameLength * 50) / 4, 44100); - //_blipR.SetRates(1000000, 44100); - //_blipR.SetRates((_machine.GateArray.FrameLength * 50) / 4, 44100); - } + //_blipL.SetRates(1000000, 44100); + //_blipL.SetRates((_machine.GateArray.FrameLength * 50) / 4, 44100); + //_blipR.SetRates(1000000, 44100); + //_blipR.SetRates((_machine.GateArray.FrameLength * 50) / 4, 44100); + } - /// - /// Initialises the AY chip - /// - public void Init(int sampleRate, int tStatesPerFrame) - { - InitTiming(sampleRate, tStatesPerFrame); - UpdateVolume(); - Reset(); - } + /// + /// Initialises the AY chip + /// + public void Init(int sampleRate, int tStatesPerFrame) + { + InitTiming(sampleRate, tStatesPerFrame); + UpdateVolume(); + Reset(); + } - #endregion + #endregion - #region AY Implementation + #region AY Implementation - #region Public Properties + #region Public Properties - /// - /// AY mixer panning configuration - /// - [Flags] - public enum AYPanConfig - { - MONO = 0, - ABC = 1, - ACB = 2, - BAC = 3, - BCA = 4, - CAB = 5, - CBA = 6, - } + /// + /// AY mixer panning configuration + /// + [Flags] + public enum AYPanConfig + { + MONO = 0, + ABC = 1, + ACB = 2, + BAC = 3, + BCA = 4, + CAB = 5, + CBA = 6, + } - /// - /// The AY panning configuration - /// - public AYPanConfig PanningConfiguration - { - get - { - return _currentPanTab; - } - set - { - if (value != _currentPanTab) - { - _currentPanTab = value; - UpdateVolume(); - } - } - } + /// + /// The AY panning configuration + /// + public AYPanConfig PanningConfiguration + { + get + { + return _currentPanTab; + } + set + { + if (value != _currentPanTab) + { + _currentPanTab = value; + UpdateVolume(); + } + } + } - /// - /// The AY chip output volume - /// (0 - 100) - /// - public int Volume - { - get - { - return _volume; - } - set - { - //value = Math.Max(0, value); - //value = Math.Max(100, value); - if (_volume == value) - { - return; - } - _volume = value; - UpdateVolume(); - } - } + /// + /// The AY chip output volume + /// (0 - 100) + /// + public int Volume + { + get + { + return _volume; + } + set + { + //value = Math.Max(0, value); + //value = Math.Max(100, value); + if (_volume == value) + { + return; + } + _volume = value; + UpdateVolume(); + } + } - /// - /// The currently selected register - /// - public int SelectedRegister - { - get { return _activeRegister; } - set - { - _activeRegister = (byte)value; - } - } + /// + /// The currently selected register + /// + public int SelectedRegister + { + get { return _activeRegister; } + set + { + _activeRegister = (byte)value; + } + } - #endregion + #endregion - #region Public Methods + #region Public Methods - /// - /// Resets the PSG - /// - public void Reset() - { - /* + /// + /// Resets the PSG + /// + public void Reset() + { + /* _noiseVal = 0x0FFFF; _outABC = 0; _outNoiseABC = 0; @@ -167,233 +167,233 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC // update the audio buffer BufferUpdate(fr); */ - } + } - /// - /// 0: Inactive - /// 1: Read Register - /// 2: Write Register - /// 3: Select Register - /// - public int ActiveFunction; + /// + /// 0: Inactive + /// 1: Read Register + /// 2: Write Register + /// 3: Select Register + /// + public int ActiveFunction; - public void SetFunction(int val) - { - int b = ((val & 0xc0) >> 6); - ActiveFunction = b; - } + public void SetFunction(int val) + { + int b = ((val & 0xc0) >> 6); + ActiveFunction = b; + } - /// - /// Reads the value from the currently selected register - /// - public int PortRead() - { - if (ActiveFunction == 1) - { - if (_activeRegister == 14) - { - if (PortAInput) - { - // exteral keyboard register - return _keyboard.ReadCurrentLine(); - } - else - { - return _keyboard.ReadCurrentLine() & _registers[_activeRegister]; - } - } + /// + /// Reads the value from the currently selected register + /// + public int PortRead() + { + if (ActiveFunction == 1) + { + if (_activeRegister == 14) + { + if (PortAInput) + { + // exteral keyboard register + return _keyboard.ReadCurrentLine(); + } + else + { + return _keyboard.ReadCurrentLine() & _registers[_activeRegister]; + } + } - if (_activeRegister < 16) - return _registers[_activeRegister]; - } + if (_activeRegister < 16) + return _registers[_activeRegister]; + } - return 0; - } + return 0; + } - /// - /// Writes to the currently selected register - /// - public void PortWrite(int value) - { - switch (ActiveFunction) - { - default: - break; - // select reg - case 3: + /// + /// Writes to the currently selected register + /// + public void PortWrite(int value) + { + switch (ActiveFunction) + { + default: + break; + // select reg + case 3: - int b = (value & 0x0f); - SelectedRegister = b; + int b = (value & 0x0f); + SelectedRegister = b; - break; + break; - // write reg - case 2: + // write reg + case 2: - if (_activeRegister == 14) - { - // external keyboard register - //return; - } + if (_activeRegister == 14) + { + // external keyboard register + //return; + } - if (_activeRegister >= 0x10) - return; + if (_activeRegister >= 0x10) + return; - byte val = (byte)value; + byte val = (byte)value; - if (((1 << _activeRegister) & ((1 << 1) | (1 << 3) | (1 << 5) | (1 << 13))) != 0) - val &= 0x0F; + if (((1 << _activeRegister) & ((1 << 1) | (1 << 3) | (1 << 5) | (1 << 13))) != 0) + val &= 0x0F; - if (((1 << _activeRegister) & ((1 << 6) | (1 << 8) | (1 << 9) | (1 << 10))) != 0) - val &= 0x1F; + if (((1 << _activeRegister) & ((1 << 6) | (1 << 8) | (1 << 9) | (1 << 10))) != 0) + val &= 0x1F; - if (_activeRegister != 13 && _registers[_activeRegister] == val) - return; + if (_activeRegister != 13 && _registers[_activeRegister] == val) + return; - _registers[_activeRegister] = val; + _registers[_activeRegister] = val; - switch (_activeRegister) - { - // Channel A (Combined Pitch) - // (not written to directly) - case 0: - case 1: - _dividerA = _registers[AY_A_FINE] | (_registers[AY_A_COARSE] << 8); - break; - // Channel B (Combined Pitch) - // (not written to directly) - case 2: - case 3: - _dividerB = _registers[AY_B_FINE] | (_registers[AY_B_COARSE] << 8); - break; - // Channel C (Combined Pitch) - // (not written to directly) - case 4: - case 5: - _dividerC = _registers[AY_C_FINE] | (_registers[AY_C_COARSE] << 8); - break; - // Noise Pitch - case 6: - _dividerN = val * 2; - break; - // Mixer - case 7: - _bit0 = 0 - ((val >> 0) & 1); - _bit1 = 0 - ((val >> 1) & 1); - _bit2 = 0 - ((val >> 2) & 1); - _bit3 = 0 - ((val >> 3) & 1); - _bit4 = 0 - ((val >> 4) & 1); - _bit5 = 0 - ((val >> 5) & 1); + switch (_activeRegister) + { + // Channel A (Combined Pitch) + // (not written to directly) + case 0: + case 1: + _dividerA = _registers[AY_A_FINE] | (_registers[AY_A_COARSE] << 8); + break; + // Channel B (Combined Pitch) + // (not written to directly) + case 2: + case 3: + _dividerB = _registers[AY_B_FINE] | (_registers[AY_B_COARSE] << 8); + break; + // Channel C (Combined Pitch) + // (not written to directly) + case 4: + case 5: + _dividerC = _registers[AY_C_FINE] | (_registers[AY_C_COARSE] << 8); + break; + // Noise Pitch + case 6: + _dividerN = val * 2; + break; + // Mixer + case 7: + _bit0 = 0 - ((val >> 0) & 1); + _bit1 = 0 - ((val >> 1) & 1); + _bit2 = 0 - ((val >> 2) & 1); + _bit3 = 0 - ((val >> 3) & 1); + _bit4 = 0 - ((val >> 4) & 1); + _bit5 = 0 - ((val >> 5) & 1); - PortAInput = ((value & 0x40) == 0); - PortBInput = ((value & 0x80) == 0); + PortAInput = ((value & 0x40) == 0); + PortBInput = ((value & 0x80) == 0); - break; - // Channel Volumes - case 8: - _eMaskA = (val & 0x10) != 0 ? -1 : 0; - _vA = ((val & 0x0F) * 2 + 1) & ~_eMaskA; - break; - case 9: - _eMaskB = (val & 0x10) != 0 ? -1 : 0; - _vB = ((val & 0x0F) * 2 + 1) & ~_eMaskB; - break; - case 10: - _eMaskC = (val & 0x10) != 0 ? -1 : 0; - _vC = ((val & 0x0F) * 2 + 1) & ~_eMaskC; - break; - // Envelope (Combined Duration) - // (not written to directly) - case 11: - case 12: - _dividerE = _registers[AY_E_FINE] | (_registers[AY_E_COARSE] << 8); - break; - // Envelope Shape - case 13: - // reset the envelope counter - _countE = 0; + break; + // Channel Volumes + case 8: + _eMaskA = (val & 0x10) != 0 ? -1 : 0; + _vA = ((val & 0x0F) * 2 + 1) & ~_eMaskA; + break; + case 9: + _eMaskB = (val & 0x10) != 0 ? -1 : 0; + _vB = ((val & 0x0F) * 2 + 1) & ~_eMaskB; + break; + case 10: + _eMaskC = (val & 0x10) != 0 ? -1 : 0; + _vC = ((val & 0x0F) * 2 + 1) & ~_eMaskC; + break; + // Envelope (Combined Duration) + // (not written to directly) + case 11: + case 12: + _dividerE = _registers[AY_E_FINE] | (_registers[AY_E_COARSE] << 8); + break; + // Envelope Shape + case 13: + // reset the envelope counter + _countE = 0; - if ((_registers[AY_E_SHAPE] & 4) != 0) - { - // attack - _eState = 0; - _eDirection = 1; - } - else - { - // decay - _eState = 31; - _eDirection = -1; - } - break; - case 14: - // IO Port - not implemented - break; - } + if ((_registers[AY_E_SHAPE] & 4) != 0) + { + // attack + _eState = 0; + _eDirection = 1; + } + else + { + // decay + _eState = 31; + _eDirection = -1; + } + break; + case 14: + // IO Port - not implemented + break; + } - // do audio processing - BufferUpdate((int)_machine.CurrentFrameCycle); + // do audio processing + BufferUpdate((int)_machine.CurrentFrameCycle); - break; - } + break; + } - } + } - /// - /// Start of frame - /// - public void StartFrame() - { - _audioBufferIndex = 0; - BufferUpdate(0); - } + /// + /// Start of frame + /// + public void StartFrame() + { + _audioBufferIndex = 0; + BufferUpdate(0); + } - /// - /// End of frame - /// - public void EndFrame() - { - BufferUpdate(_tStatesPerFrame); - } + /// + /// End of frame + /// + public void EndFrame() + { + BufferUpdate(_tStatesPerFrame); + } - /// - /// Updates the audiobuffer based on the current frame t-state - /// - public void UpdateSound(int frameCycle) - { - BufferUpdate(frameCycle); - } + /// + /// Updates the audiobuffer based on the current frame t-state + /// + public void UpdateSound(int frameCycle) + { + BufferUpdate(frameCycle); + } - #endregion + #endregion - #region Private Fields + #region Private Fields - /// - /// Register indicies - /// - private const int AY_A_FINE = 0; - private const int AY_A_COARSE = 1; - private const int AY_B_FINE = 2; - private const int AY_B_COARSE = 3; - private const int AY_C_FINE = 4; - private const int AY_C_COARSE = 5; - private const int AY_NOISEPITCH = 6; - private const int AY_MIXER = 7; - private const int AY_A_VOL = 8; - private const int AY_B_VOL = 9; - private const int AY_C_VOL = 10; - private const int AY_E_FINE = 11; - private const int AY_E_COARSE = 12; - private const int AY_E_SHAPE = 13; - private const int AY_PORT_A = 14; - private const int AY_PORT_B = 15; + /// + /// Register indicies + /// + private const int AY_A_FINE = 0; + private const int AY_A_COARSE = 1; + private const int AY_B_FINE = 2; + private const int AY_B_COARSE = 3; + private const int AY_C_FINE = 4; + private const int AY_C_COARSE = 5; + private const int AY_NOISEPITCH = 6; + private const int AY_MIXER = 7; + private const int AY_A_VOL = 8; + private const int AY_B_VOL = 9; + private const int AY_C_VOL = 10; + private const int AY_E_FINE = 11; + private const int AY_E_COARSE = 12; + private const int AY_E_SHAPE = 13; + private const int AY_PORT_A = 14; + private const int AY_PORT_B = 15; - /// - /// The register array - /// - /* + /// + /// The register array + /// + /* The AY-3-8910/8912 contains 16 internal registers as follows: Register Function Range @@ -424,117 +424,117 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC The AY-3-8912 ignores bit 7 of this register. */ - private int[] _registers = new int[16]; + private int[] _registers = new int[16]; - /// - /// The currently selected register - /// - private byte _activeRegister; + /// + /// The currently selected register + /// + private byte _activeRegister; - private bool PortAInput = true; - private bool PortBInput = true; + private bool PortAInput = true; + private bool PortBInput = true; - /// - /// The frequency of the AY chip - /// - private static int _chipFrequency = 1000000; // 1773400; + /// + /// The frequency of the AY chip + /// + private static int _chipFrequency = 1000000; // 1773400; - /// - /// The rendering resolution of the chip - /// - private double _resolution = 50D * 8D / _chipFrequency; + /// + /// The rendering resolution of the chip + /// + private double _resolution = 50D * 8D / _chipFrequency; - /// - /// Channel generator state - /// - private int _bitA; - private int _bitB; - private int _bitC; + /// + /// Channel generator state + /// + private int _bitA; + private int _bitB; + private int _bitC; - /// - /// Envelope state - /// - private int _eState; + /// + /// Envelope state + /// + private int _eState; - /// - /// Envelope direction - /// - private int _eDirection; + /// + /// Envelope direction + /// + private int _eDirection; - /// - /// Noise seed - /// - private int _noiseSeed; + /// + /// Noise seed + /// + private int _noiseSeed; - /// - /// Mixer state - /// - private int _bit0; - private int _bit1; - private int _bit2; - private int _bit3; - private int _bit4; - private int _bit5; + /// + /// Mixer state + /// + private int _bit0; + private int _bit1; + private int _bit2; + private int _bit3; + private int _bit4; + private int _bit5; - /// - /// Noise generator state - /// - private int _bitN; + /// + /// Noise generator state + /// + private int _bitN; - /// - /// Envelope masks - /// - private int _eMaskA; - private int _eMaskB; - private int _eMaskC; + /// + /// Envelope masks + /// + private int _eMaskA; + private int _eMaskB; + private int _eMaskC; - /// - /// Amplitudes - /// - private int _vA; - private int _vB; - private int _vC; + /// + /// Amplitudes + /// + private int _vA; + private int _vB; + private int _vC; - /// - /// Channel gen counters - /// - private int _countA; - private int _countB; - private int _countC; + /// + /// Channel gen counters + /// + private int _countA; + private int _countB; + private int _countC; - /// - /// Envelope gen counter - /// - private int _countE; + /// + /// Envelope gen counter + /// + private int _countE; - /// - /// Noise gen counter - /// - private int _countN; + /// + /// Noise gen counter + /// + private int _countN; - /// - /// Channel gen dividers - /// - private int _dividerA; - private int _dividerB; - private int _dividerC; + /// + /// Channel gen dividers + /// + private int _dividerA; + private int _dividerB; + private int _dividerC; - /// - /// Envelope gen divider - /// - private int _dividerE; + /// + /// Envelope gen divider + /// + private int _dividerE; - /// - /// Noise gen divider - /// - private int _dividerN; + /// + /// Noise gen divider + /// + private int _dividerN; - /// - /// Panning table list - /// - private static List PanTabs = new List - { + /// + /// Panning table list + /// + private static List PanTabs = new List + { // MONO new uint[] { 50,50, 50,50, 50,50 }, // ABC @@ -549,228 +549,228 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC new uint[] { 66,66, 10,100, 100,10 }, // CBA new uint[] { 10,100, 66,66, 100,10 } - }; + }; - /// - /// The currently selected panning configuration - /// - private AYPanConfig _currentPanTab = AYPanConfig.ABC; + /// + /// The currently selected panning configuration + /// + private AYPanConfig _currentPanTab = AYPanConfig.ABC; - /// - /// The current volume - /// - private int _volume = 75; + /// + /// The current volume + /// + private int _volume = 75; - /// - /// Volume tables state - /// - private uint[][] _volumeTables; + /// + /// Volume tables state + /// + private uint[][] _volumeTables; - /// - /// Volume table to be used - /// - private static uint[] AYVolumes = new uint[] - { - 0x0000,0x0000,0x0340,0x0340,0x04C0,0x04C0,0x06F2,0x06F2, - 0x0A44,0x0A44,0x0F13,0x0F13,0x1510,0x1510,0x227E,0x227E, - 0x289F,0x289F,0x414E,0x414E,0x5B21,0x5B21,0x7258,0x7258, - 0x905E,0x905E,0xB550,0xB550,0xD7A0,0xD7A0,0xFFFF,0xFFFF, - }; + /// + /// Volume table to be used + /// + private static uint[] AYVolumes = new uint[] + { + 0x0000,0x0000,0x0340,0x0340,0x04C0,0x04C0,0x06F2,0x06F2, + 0x0A44,0x0A44,0x0F13,0x0F13,0x1510,0x1510,0x227E,0x227E, + 0x289F,0x289F,0x414E,0x414E,0x5B21,0x5B21,0x7258,0x7258, + 0x905E,0x905E,0xB550,0xB550,0xD7A0,0xD7A0,0xFFFF,0xFFFF, + }; - #endregion + #endregion - #region Private Methods + #region Private Methods - /// - /// Forces an update of the volume tables - /// - private void UpdateVolume() - { - int upperFloor = 40000; - var inc = (0xFFFF - upperFloor) / 100; + /// + /// Forces an update of the volume tables + /// + private void UpdateVolume() + { + int upperFloor = 40000; + var inc = (0xFFFF - upperFloor) / 100; - var vol = inc * _volume; // ((ulong)0xFFFF * (ulong)_volume / 100UL) - 20000 ; - _volumeTables = new uint[6][]; + var vol = inc * _volume; // ((ulong)0xFFFF * (ulong)_volume / 100UL) - 20000 ; + _volumeTables = new uint[6][]; - // parent array - for (int j = 0; j < _volumeTables.Length; j++) - { - _volumeTables[j] = new uint[32]; + // parent array + for (int j = 0; j < _volumeTables.Length; j++) + { + _volumeTables[j] = new uint[32]; - // child array - for (int i = 0; i < _volumeTables[j].Length; i++) - { - _volumeTables[j][i] = (uint)( - (PanTabs[(int)_currentPanTab][j] * AYVolumes[i] * vol) / - (3 * 65535 * 100)); - } - } - } + // child array + for (int i = 0; i < _volumeTables[j].Length; i++) + { + _volumeTables[j][i] = (uint)( + (PanTabs[(int)_currentPanTab][j] * AYVolumes[i] * vol) / + (3 * 65535 * 100)); + } + } + } - /// - /// Initializes timing information for the frame - /// - private void InitTiming(int sampleRate, int frameTactCount) - { - _sampleRate = sampleRate; - _tStatesPerFrame = frameTactCount; - _samplesPerFrame = sampleRate / 50; //882 + /// + /// Initializes timing information for the frame + /// + private void InitTiming(int sampleRate, int frameTactCount) + { + _sampleRate = sampleRate; + _tStatesPerFrame = frameTactCount; + _samplesPerFrame = sampleRate / 50; //882 - _tStatesPerSample = (double)frameTactCount / (double)_samplesPerFrame; // 90; //(int)Math.Round(((double)_tStatesPerFrame * 50D) / - //(16D * (double)_sampleRate), - //MidpointRounding.AwayFromZero); - _audioBuffer = new short[_samplesPerFrame * 2]; - _audioBufferIndex = 0; + _tStatesPerSample = (double)frameTactCount / (double)_samplesPerFrame; // 90; //(int)Math.Round(((double)_tStatesPerFrame * 50D) / + //(16D * (double)_sampleRate), + //MidpointRounding.AwayFromZero); + _audioBuffer = new short[_samplesPerFrame * 2]; + _audioBufferIndex = 0; - ticksPerSample = ((double)_chipFrequency / sampleRate / 8); - } - private double ticksPerSample; - - private double tickCounter = 0; + ticksPerSample = ((double)_chipFrequency / sampleRate / 8); + } + private double ticksPerSample; - /// - /// Updates the audiobuffer based on the current frame t-state - /// - private void BufferUpdate(int cycle) - { - if (cycle > _tStatesPerFrame) - { - // we are outside of the frame - just process the last value - cycle = _tStatesPerFrame; - } + private double tickCounter = 0; - // get the current length of the audiobuffer - int bufferLength = _samplesPerFrame; // _audioBuffer.Length; + /// + /// Updates the audiobuffer based on the current frame t-state + /// + private void BufferUpdate(int cycle) + { + if (cycle > _tStatesPerFrame) + { + // we are outside of the frame - just process the last value + cycle = _tStatesPerFrame; + } - double toEnd = ((double)(bufferLength * cycle) / (double)_tStatesPerFrame); + // get the current length of the audiobuffer + int bufferLength = _samplesPerFrame; // _audioBuffer.Length; - // loop through the number of samples we need to render - while (_audioBufferIndex < toEnd) - { - // run the AY chip processing at the correct resolution - tickCounter += ticksPerSample; + double toEnd = ((double)(bufferLength * cycle) / (double)_tStatesPerFrame); - while (tickCounter > 0) - { - tickCounter--; + // loop through the number of samples we need to render + while (_audioBufferIndex < toEnd) + { + // run the AY chip processing at the correct resolution + tickCounter += ticksPerSample; - if (++_countA >= _dividerA) - { - _countA = 0; - _bitA ^= -1; - } + while (tickCounter > 0) + { + tickCounter--; - if (++_countB >= _dividerB) - { - _countB = 0; - _bitB ^= -1; - } + if (++_countA >= _dividerA) + { + _countA = 0; + _bitA ^= -1; + } - if (++_countC >= _dividerC) - { - _countC = 0; - _bitC ^= -1; - } + if (++_countB >= _dividerB) + { + _countB = 0; + _bitB ^= -1; + } - if (++_countN >= _dividerN) - { - _countN = 0; - _noiseSeed = (_noiseSeed * 2 + 1) ^ (((_noiseSeed >> 16) ^ (_noiseSeed >> 13)) & 1); - _bitN = 0 - ((_noiseSeed >> 16) & 1); - } + if (++_countC >= _dividerC) + { + _countC = 0; + _bitC ^= -1; + } - if (++_countE >= _dividerE) - { - _countE = 0; - _eState += +_eDirection; + if (++_countN >= _dividerN) + { + _countN = 0; + _noiseSeed = (_noiseSeed * 2 + 1) ^ (((_noiseSeed >> 16) ^ (_noiseSeed >> 13)) & 1); + _bitN = 0 - ((_noiseSeed >> 16) & 1); + } - if ((_eState & ~31) != 0) - { - var mask = (1 << _registers[AY_E_SHAPE]); + if (++_countE >= _dividerE) + { + _countE = 0; + _eState += +_eDirection; - if ((mask & ((1 << 0) | (1 << 1) | (1 << 2) | - (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | - (1 << 7) | (1 << 9) | (1 << 15))) != 0) - { - _eState = _eDirection = 0; - } - else if ((mask & ((1 << 8) | (1 << 12))) != 0) - { - _eState &= 31; - } - else if ((mask & ((1 << 10) | (1 << 14))) != 0) - { - _eDirection = -_eDirection; - _eState += _eDirection; - } - else - { - // 11,13 - _eState = 31; - _eDirection = 0; - } - } - } - } + if ((_eState & ~31) != 0) + { + var mask = (1 << _registers[AY_E_SHAPE]); - // mix the sample - var mixA = ((_eMaskA & _eState) | _vA) & ((_bitA | _bit0) & (_bitN | _bit3)); - var mixB = ((_eMaskB & _eState) | _vB) & ((_bitB | _bit1) & (_bitN | _bit4)); - var mixC = ((_eMaskC & _eState) | _vC) & ((_bitC | _bit2) & (_bitN | _bit5)); + if ((mask & ((1 << 0) | (1 << 1) | (1 << 2) | + (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | + (1 << 7) | (1 << 9) | (1 << 15))) != 0) + { + _eState = _eDirection = 0; + } + else if ((mask & ((1 << 8) | (1 << 12))) != 0) + { + _eState &= 31; + } + else if ((mask & ((1 << 10) | (1 << 14))) != 0) + { + _eDirection = -_eDirection; + _eState += _eDirection; + } + else + { + // 11,13 + _eState = 31; + _eDirection = 0; + } + } + } + } - var l = _volumeTables[0][mixA]; - var r = _volumeTables[1][mixA]; + // mix the sample + var mixA = ((_eMaskA & _eState) | _vA) & ((_bitA | _bit0) & (_bitN | _bit3)); + var mixB = ((_eMaskB & _eState) | _vB) & ((_bitB | _bit1) & (_bitN | _bit4)); + var mixC = ((_eMaskC & _eState) | _vC) & ((_bitC | _bit2) & (_bitN | _bit5)); - l += _volumeTables[2][mixB]; - r += _volumeTables[3][mixB]; - l += _volumeTables[4][mixC]; - r += _volumeTables[5][mixC]; + var l = _volumeTables[0][mixA]; + var r = _volumeTables[1][mixA]; - _audioBuffer[_audioBufferIndex * 2] = (short)l; - _audioBuffer[(_audioBufferIndex * 2) + 1] = (short)r; + l += _volumeTables[2][mixB]; + r += _volumeTables[3][mixB]; + l += _volumeTables[4][mixC]; + r += _volumeTables[5][mixC]; - _audioBufferIndex++; - } + _audioBuffer[_audioBufferIndex * 2] = (short)l; + _audioBuffer[(_audioBufferIndex * 2) + 1] = (short)r; - _lastStateRendered = cycle; - } + _audioBufferIndex++; + } - #endregion + _lastStateRendered = cycle; + } - #endregion + #endregion - #region ISoundProvider + #endregion - public bool CanProvideAsync => false; + #region ISoundProvider - public SyncSoundMode SyncMode => SyncSoundMode.Sync; + public bool CanProvideAsync => false; - public void SetSyncMode(SyncSoundMode mode) - { - if (mode != SyncSoundMode.Sync) - throw new InvalidOperationException("Only Sync mode is supported."); - } + public SyncSoundMode SyncMode => SyncSoundMode.Sync; - public void GetSamplesAsync(short[] samples) - { - throw new NotSupportedException("Async is not available"); - } + public void SetSyncMode(SyncSoundMode mode) + { + if (mode != SyncSoundMode.Sync) + throw new InvalidOperationException("Only Sync mode is supported."); + } - public void DiscardSamples() - { - _audioBuffer = new short[_samplesPerFrame * 2]; - //_blipL.Clear(); - //_blipR.Clear(); - } + public void GetSamplesAsync(short[] samples) + { + throw new NotSupportedException("Async is not available"); + } - public void GetSamplesSync(out short[] samples, out int nsamp) - { - nsamp = _samplesPerFrame; - samples = _audioBuffer; - DiscardSamples(); - tickCounter = 0; - return; - /* + public void DiscardSamples() + { + _audioBuffer = new short[_samplesPerFrame * 2]; + //_blipL.Clear(); + //_blipR.Clear(); + } + + public void GetSamplesSync(out short[] samples, out int nsamp) + { + nsamp = _samplesPerFrame; + samples = _audioBuffer; + DiscardSamples(); + tickCounter = 0; + return; + /* _blipL.EndFrame((uint)SampleClock); _blipR.EndFrame((uint)SampleClock); SampleClock = 0; @@ -805,77 +805,77 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC samples = _audioBuffer; DiscardSamples(); */ - } + } - #endregion + #endregion - #region State Serialization + #region State Serialization - public int nullDump = 0; + public int nullDump = 0; - /// - /// State serialization - /// - public void SyncState(Serializer ser) - { - ser.BeginSection("PSG-AY"); + /// + /// State serialization + /// + public void SyncState(Serializer ser) + { + ser.BeginSection("PSG-AY"); - ser.Sync(nameof(ActiveFunction), ref ActiveFunction); + ser.Sync(nameof(ActiveFunction), ref ActiveFunction); - ser.Sync(nameof(_tStatesPerFrame), ref _tStatesPerFrame); - ser.Sync(nameof(_sampleRate), ref _sampleRate); - ser.Sync(nameof(_samplesPerFrame), ref _samplesPerFrame); - //ser.Sync(nameof(_tStatesPerSample), ref _tStatesPerSample); - ser.Sync(nameof(_audioBufferIndex), ref _audioBufferIndex); - ser.Sync(nameof(_audioBuffer), ref _audioBuffer, false); - ser.Sync(nameof(PortAInput), ref PortAInput); - ser.Sync(nameof(PortBInput), ref PortBInput); + ser.Sync(nameof(_tStatesPerFrame), ref _tStatesPerFrame); + ser.Sync(nameof(_sampleRate), ref _sampleRate); + ser.Sync(nameof(_samplesPerFrame), ref _samplesPerFrame); + //ser.Sync(nameof(_tStatesPerSample), ref _tStatesPerSample); + ser.Sync(nameof(_audioBufferIndex), ref _audioBufferIndex); + ser.Sync(nameof(_audioBuffer), ref _audioBuffer, false); + ser.Sync(nameof(PortAInput), ref PortAInput); + ser.Sync(nameof(PortBInput), ref PortBInput); - ser.Sync(nameof(_registers), ref _registers, false); - ser.Sync(nameof(_activeRegister), ref _activeRegister); - ser.Sync(nameof(_bitA), ref _bitA); - ser.Sync(nameof(_bitB), ref _bitB); - ser.Sync(nameof(_bitC), ref _bitC); - ser.Sync(nameof(_eState), ref _eState); - ser.Sync(nameof(_eDirection), ref _eDirection); - ser.Sync(nameof(_noiseSeed), ref _noiseSeed); - ser.Sync(nameof(_bit0), ref _bit0); - ser.Sync(nameof(_bit1), ref _bit1); - ser.Sync(nameof(_bit2), ref _bit2); - ser.Sync(nameof(_bit3), ref _bit3); - ser.Sync(nameof(_bit4), ref _bit4); - ser.Sync(nameof(_bit5), ref _bit5); - ser.Sync(nameof(_bitN), ref _bitN); - ser.Sync(nameof(_eMaskA), ref _eMaskA); - ser.Sync(nameof(_eMaskB), ref _eMaskB); - ser.Sync(nameof(_eMaskC), ref _eMaskC); - ser.Sync(nameof(_vA), ref _vA); - ser.Sync(nameof(_vB), ref _vB); - ser.Sync(nameof(_vC), ref _vC); - ser.Sync(nameof(_countA), ref _countA); - ser.Sync(nameof(_countB), ref _countB); - ser.Sync(nameof(_countC), ref _countC); - ser.Sync(nameof(_countE), ref _countE); - ser.Sync(nameof(_countN), ref _countN); - ser.Sync(nameof(_dividerA), ref _dividerA); - ser.Sync(nameof(_dividerB), ref _dividerB); - ser.Sync(nameof(_dividerC), ref _dividerC); - ser.Sync(nameof(_dividerE), ref _dividerE); - ser.Sync(nameof(_dividerN), ref _dividerN); - ser.SyncEnum(nameof(_currentPanTab), ref _currentPanTab); - ser.Sync(nameof(_volume), ref nullDump); + ser.Sync(nameof(_registers), ref _registers, false); + ser.Sync(nameof(_activeRegister), ref _activeRegister); + ser.Sync(nameof(_bitA), ref _bitA); + ser.Sync(nameof(_bitB), ref _bitB); + ser.Sync(nameof(_bitC), ref _bitC); + ser.Sync(nameof(_eState), ref _eState); + ser.Sync(nameof(_eDirection), ref _eDirection); + ser.Sync(nameof(_noiseSeed), ref _noiseSeed); + ser.Sync(nameof(_bit0), ref _bit0); + ser.Sync(nameof(_bit1), ref _bit1); + ser.Sync(nameof(_bit2), ref _bit2); + ser.Sync(nameof(_bit3), ref _bit3); + ser.Sync(nameof(_bit4), ref _bit4); + ser.Sync(nameof(_bit5), ref _bit5); + ser.Sync(nameof(_bitN), ref _bitN); + ser.Sync(nameof(_eMaskA), ref _eMaskA); + ser.Sync(nameof(_eMaskB), ref _eMaskB); + ser.Sync(nameof(_eMaskC), ref _eMaskC); + ser.Sync(nameof(_vA), ref _vA); + ser.Sync(nameof(_vB), ref _vB); + ser.Sync(nameof(_vC), ref _vC); + ser.Sync(nameof(_countA), ref _countA); + ser.Sync(nameof(_countB), ref _countB); + ser.Sync(nameof(_countC), ref _countC); + ser.Sync(nameof(_countE), ref _countE); + ser.Sync(nameof(_countN), ref _countN); + ser.Sync(nameof(_dividerA), ref _dividerA); + ser.Sync(nameof(_dividerB), ref _dividerB); + ser.Sync(nameof(_dividerC), ref _dividerC); + ser.Sync(nameof(_dividerE), ref _dividerE); + ser.Sync(nameof(_dividerN), ref _dividerN); + ser.SyncEnum(nameof(_currentPanTab), ref _currentPanTab); + ser.Sync(nameof(_volume), ref nullDump); - for (int i = 0; i < 6; i++) - { - ser.Sync("volTable" + i, ref _volumeTables[i], false); - } + for (int i = 0; i < 6; i++) + { + ser.Sync("volTable" + i, ref _volumeTables[i], false); + } - if (ser.IsReader) - _volume = _machine.CPC.Settings.AYVolume; + if (ser.IsReader) + _volume = _machine.CPC.Settings.AYVolume; - ser.EndSection(); - } + ser.EndSection(); + } - #endregion - } + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/SoundOutput/Beeper.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/SoundOutput/Beeper.cs index e655305d41..fd34c3abd8 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/SoundOutput/Beeper.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/SoundOutput/Beeper.cs @@ -4,206 +4,206 @@ using System; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Logical Beeper class - /// Used to emulate the sound generated by tape loading - /// This implementation uses BlipBuffer and should *always* output at 44100 with 882 samples per frame - /// (so that it can be mixed easily further down the line) - /// - public class Beeper : ISoundProvider, IBeeperDevice - { - #region Fields and Properties + /// + /// Logical Beeper class + /// Used to emulate the sound generated by tape loading + /// This implementation uses BlipBuffer and should *always* output at 44100 with 882 samples per frame + /// (so that it can be mixed easily further down the line) + /// + public class Beeper : ISoundProvider, IBeeperDevice + { + #region Fields and Properties - /// - /// Sample Rate - /// This usually has to be 44100 for ISoundProvider - /// - private int _sampleRate; - public int SampleRate - { - get { return _sampleRate; } - set { _sampleRate = value; } - } + /// + /// Sample Rate + /// This usually has to be 44100 for ISoundProvider + /// + private int _sampleRate; + public int SampleRate + { + get { return _sampleRate; } + set { _sampleRate = value; } + } - /// - /// Buzzer volume - /// Accepts an int 0-100 value - /// - private int _volume; - public int Volume - { - get - { - return VolumeConverterOut(_volume); - } - set - { - var newVol = VolumeConverterIn(value); - if (newVol != _volume) - blip.Clear(); - _volume = VolumeConverterIn(value); - } - } + /// + /// Buzzer volume + /// Accepts an int 0-100 value + /// + private int _volume; + public int Volume + { + get + { + return VolumeConverterOut(_volume); + } + set + { + var newVol = VolumeConverterIn(value); + if (newVol != _volume) + blip.Clear(); + _volume = VolumeConverterIn(value); + } + } - /// - /// The last used volume (used to modify blipbuffer delta values) - /// - private int lastVolume; + /// + /// The last used volume (used to modify blipbuffer delta values) + /// + private int lastVolume; - /// - /// The number of cpu cycles per frame - /// - private long _tStatesPerFrame; + /// + /// The number of cpu cycles per frame + /// + private long _tStatesPerFrame; - /// - /// The parent emulated machine - /// - private CPCBase _machine; + /// + /// The parent emulated machine + /// + private CPCBase _machine; - /// - /// The last pulse - /// - private bool LastPulse; + /// + /// The last pulse + /// + private bool LastPulse; - /// - /// The last T-State (cpu cycle) that the last pulse was received - /// - private long LastPulseTState; + /// + /// The last T-State (cpu cycle) that the last pulse was received + /// + private long LastPulseTState; - /// - /// Device blipbuffer - /// - private readonly BlipBuffer blip = new BlipBuffer(883); + /// + /// Device blipbuffer + /// + private readonly BlipBuffer blip = new BlipBuffer(883); - #endregion + #endregion - #region Private Methods + #region Private Methods - /// - /// Takes an int 0-100 and returns the relevant short volume to output - /// - private int VolumeConverterIn(int vol) - { - int maxLimit = short.MaxValue / 3; - int increment = maxLimit / 100; + /// + /// Takes an int 0-100 and returns the relevant short volume to output + /// + private int VolumeConverterIn(int vol) + { + int maxLimit = short.MaxValue / 3; + int increment = maxLimit / 100; - return vol * increment; - } + return vol * increment; + } - /// - /// Takes an short volume and returns the relevant int value 0-100 - /// - private int VolumeConverterOut(int shortvol) - { - int maxLimit = short.MaxValue / 3; - int increment = maxLimit / 100; + /// + /// Takes an short volume and returns the relevant int value 0-100 + /// + private int VolumeConverterOut(int shortvol) + { + int maxLimit = short.MaxValue / 3; + int increment = maxLimit / 100; - if (shortvol > maxLimit) - shortvol = maxLimit; + if (shortvol > maxLimit) + shortvol = maxLimit; - return shortvol / increment; - } + return shortvol / increment; + } - #endregion + #endregion - #region Construction & Initialisation + #region Construction & Initialisation - public Beeper(CPCBase machine) - { - _machine = machine; - } + public Beeper(CPCBase machine) + { + _machine = machine; + } - /// - /// Initialises the beeper - /// - public void Init(int sampleRate, int tStatesPerFrame) - { - blip.SetRates((4000000), sampleRate); - _sampleRate = sampleRate; - _tStatesPerFrame = tStatesPerFrame; - } + /// + /// Initialises the beeper + /// + public void Init(int sampleRate, int tStatesPerFrame) + { + blip.SetRates((4000000), sampleRate); + _sampleRate = sampleRate; + _tStatesPerFrame = tStatesPerFrame; + } - #endregion + #endregion - #region IBeeperDevice + #region IBeeperDevice - /// - /// Processes an incoming pulse value and adds it to the blipbuffer - /// - public void ProcessPulseValue(bool pulse) - { - if (!_machine._renderSound) - return; + /// + /// Processes an incoming pulse value and adds it to the blipbuffer + /// + public void ProcessPulseValue(bool pulse) + { + if (!_machine._renderSound) + return; - if (LastPulse == pulse) - { - // no change - blip.AddDelta((uint)_machine.CurrentFrameCycle, 0); - } - - else - { - if (pulse) - blip.AddDelta((uint)_machine.CurrentFrameCycle, (short)(_volume)); - else - blip.AddDelta((uint)_machine.CurrentFrameCycle, -(short)(_volume)); + if (LastPulse == pulse) + { + // no change + blip.AddDelta((uint)_machine.CurrentFrameCycle, 0); + } - lastVolume = _volume; - } + else + { + if (pulse) + blip.AddDelta((uint)_machine.CurrentFrameCycle, (short)(_volume)); + else + blip.AddDelta((uint)_machine.CurrentFrameCycle, -(short)(_volume)); - LastPulse = pulse; - } + lastVolume = _volume; + } - #endregion + LastPulse = pulse; + } - #region ISoundProvider + #endregion - public bool CanProvideAsync => false; + #region ISoundProvider - public SyncSoundMode SyncMode => SyncSoundMode.Sync; + public bool CanProvideAsync => false; - public void SetSyncMode(SyncSoundMode mode) - { - if (mode != SyncSoundMode.Sync) - throw new InvalidOperationException("Only Sync mode is supported."); - } + public SyncSoundMode SyncMode => SyncSoundMode.Sync; - public void GetSamplesAsync(short[] samples) - { - throw new NotSupportedException("Async is not available"); - } + public void SetSyncMode(SyncSoundMode mode) + { + if (mode != SyncSoundMode.Sync) + throw new InvalidOperationException("Only Sync mode is supported."); + } - public void DiscardSamples() - { - blip.Clear(); - } + public void GetSamplesAsync(short[] samples) + { + throw new NotSupportedException("Async is not available"); + } - public void GetSamplesSync(out short[] samples, out int nsamp) - { - blip.EndFrame((uint)_tStatesPerFrame); - nsamp = blip.SamplesAvailable(); - samples = new short[nsamp * 2]; - blip.ReadSamples(samples, nsamp, true); - for (int i = 0; i < nsamp * 2; i += 2) - { - samples[i + 1] = samples[i]; - } - } + public void DiscardSamples() + { + blip.Clear(); + } - #endregion + public void GetSamplesSync(out short[] samples, out int nsamp) + { + blip.EndFrame((uint)_tStatesPerFrame); + nsamp = blip.SamplesAvailable(); + samples = new short[nsamp * 2]; + blip.ReadSamples(samples, nsamp, true); + for (int i = 0; i < nsamp * 2; i += 2) + { + samples[i + 1] = samples[i]; + } + } - #region State Serialization + #endregion - public void SyncState(Serializer ser) - { - ser.BeginSection("Buzzer"); - ser.Sync(nameof(_tStatesPerFrame), ref _tStatesPerFrame); - ser.Sync(nameof(_sampleRate), ref _sampleRate); - ser.Sync(nameof(LastPulse), ref LastPulse); - ser.Sync(nameof(LastPulseTState), ref LastPulseTState); - ser.EndSection(); - } + #region State Serialization - #endregion - } + public void SyncState(Serializer ser) + { + ser.BeginSection("Buzzer"); + ser.Sync(nameof(_tStatesPerFrame), ref _tStatesPerFrame); + ser.Sync(nameof(_sampleRate), ref _sampleRate); + ser.Sync(nameof(LastPulse), ref LastPulse); + ser.Sync(nameof(LastPulseTState), ref LastPulseTState); + ser.EndSection(); + } + + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPC464/CPC464.Memory.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPC464/CPC464.Memory.cs index 6ba6df3e80..62dc8b29a3 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPC464/CPC464.Memory.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPC464/CPC464.Memory.cs @@ -6,142 +6,142 @@ using System.Threading.Tasks; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// CPC464 - /// * Memory * - /// - public partial class CPC464 : CPCBase - { - /// - /// Simulates reading from the bus - /// ROM paging should be handled here - /// - public override byte ReadBus(ushort addr) - { - int divisor = addr / 0x4000; - byte result = 0xff; + /// + /// CPC464 + /// * Memory * + /// + public partial class CPC464 : CPCBase + { + /// + /// Simulates reading from the bus + /// ROM paging should be handled here + /// + public override byte ReadBus(ushort addr) + { + int divisor = addr / 0x4000; + byte result = 0xff; - switch (divisor) - { - // 0x000 or LowerROM - case 0: - if (LowerROMPaged) - result = ROMLower[addr % 0x4000]; - else - result = RAM0[addr % 0x4000]; - break; + switch (divisor) + { + // 0x000 or LowerROM + case 0: + if (LowerROMPaged) + result = ROMLower[addr % 0x4000]; + else + result = RAM0[addr % 0x4000]; + break; - // 0x4000 - case 1: - result = RAM1[addr % 0x4000]; - break; + // 0x4000 + case 1: + result = RAM1[addr % 0x4000]; + break; - // 0x8000 - case 2: - result = RAM2[addr % 0x4000]; - break; + // 0x8000 + case 2: + result = RAM2[addr % 0x4000]; + break; - // 0xc000 or UpperROM - case 3: - if (UpperROMPaged) - result = ROM0[addr % 0x4000]; - else - result = RAM3[addr % 0x4000]; - break; - default: - break; - } + // 0xc000 or UpperROM + case 3: + if (UpperROMPaged) + result = ROM0[addr % 0x4000]; + else + result = RAM3[addr % 0x4000]; + break; + default: + break; + } - return result; - } + return result; + } - /// - /// Simulates writing to the bus - /// Writes to the bus ALWAYS go to RAM, regardless of what upper and lower ROMs are paged in - /// - public override void WriteBus(ushort addr, byte value) - { - int divisor = addr / 0x4000; + /// + /// Simulates writing to the bus + /// Writes to the bus ALWAYS go to RAM, regardless of what upper and lower ROMs are paged in + /// + public override void WriteBus(ushort addr, byte value) + { + int divisor = addr / 0x4000; - switch (divisor) - { - // RAM 0x000 - case 0: - RAM0[addr % 0x4000] = value; - break; + switch (divisor) + { + // RAM 0x000 + case 0: + RAM0[addr % 0x4000] = value; + break; - // RAM 0x4000 - case 1: - RAM1[addr % 0x4000] = value; - break; + // RAM 0x4000 + case 1: + RAM1[addr % 0x4000] = value; + break; - // RAM 0x8000 - case 2: - RAM2[addr % 0x4000] = value; - break; + // RAM 0x8000 + case 2: + RAM2[addr % 0x4000] = value; + break; - // RAM 0xc000 - case 3: - RAM3[addr % 0x4000] = value; - break; - default: - break; - } - } + // RAM 0xc000 + case 3: + RAM3[addr % 0x4000] = value; + break; + default: + break; + } + } - /// - /// Reads a byte of data from a specified memory address - /// - public override byte ReadMemory(ushort addr) - { - var data = ReadBus(addr); - return data; - } + /// + /// Reads a byte of data from a specified memory address + /// + public override byte ReadMemory(ushort addr) + { + var data = ReadBus(addr); + return data; + } - /// - /// Writes a byte of data to a specified memory address - /// (with memory contention if appropriate) - /// - public override void WriteMemory(ushort addr, byte value) - { - WriteBus(addr, value); - } + /// + /// Writes a byte of data to a specified memory address + /// (with memory contention if appropriate) + /// + public override void WriteMemory(ushort addr, byte value) + { + WriteBus(addr, value); + } - /// - /// Sets up the ROM - /// - public override void InitROM(RomData[] romData) - { - foreach (var r in romData) - { - if (r.ROMType == RomData.ROMChipType.Lower) - { - for (int i = 0; i < 0x4000; i++) - { - ROMLower[i] = r.RomBytes[i]; + /// + /// Sets up the ROM + /// + public override void InitROM(RomData[] romData) + { + foreach (var r in romData) + { + if (r.ROMType == RomData.ROMChipType.Lower) + { + for (int i = 0; i < 0x4000; i++) + { + ROMLower[i] = r.RomBytes[i]; - } - } - else - { - for (int i = 0; i < 0x4000; i++) - { - switch (r.ROMPosition) - { - case 0: - ROM0[i] = r.RomBytes[i]; - break; - case 7: - ROM7[i] = r.RomBytes[i]; - break; - } - } - } - } + } + } + else + { + for (int i = 0; i < 0x4000; i++) + { + switch (r.ROMPosition) + { + case 0: + ROM0[i] = r.RomBytes[i]; + break; + case 7: + ROM7[i] = r.RomBytes[i]; + break; + } + } + } + } - LowerROMPaged = true; - UpperROMPaged = true; - } - } + LowerROMPaged = true; + UpperROMPaged = true; + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPC464/CPC464.Port.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPC464/CPC464.Port.cs index 73f6e99746..6f7a2cae3d 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPC464/CPC464.Port.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPC464/CPC464.Port.cs @@ -7,97 +7,97 @@ using System.Threading.Tasks; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// CPC464 - /// * Port * - /// - public partial class CPC464 : CPCBase - { - /// - /// Reads a byte of data from a specified port address - /// - public override byte ReadPort(ushort port) - { - BitArray portBits = new BitArray(BitConverter.GetBytes(port)); - byte portUpper = (byte)(port >> 8); - byte portLower = (byte)(port & 0xff); + /// + /// CPC464 + /// * Port * + /// + public partial class CPC464 : CPCBase + { + /// + /// Reads a byte of data from a specified port address + /// + public override byte ReadPort(ushort port) + { + BitArray portBits = new BitArray(BitConverter.GetBytes(port)); + byte portUpper = (byte)(port >> 8); + byte portLower = (byte)(port & 0xff); - int result = 0xff; + int result = 0xff; - if (DecodeINPort(port) == PortDevice.GateArray) - { - GateArray.ReadPort(port, ref result); - } - else if (DecodeINPort(port) == PortDevice.CRCT) - { - CRCT.ReadPort(port, ref result); - } - else if (DecodeINPort(port) == PortDevice.ROMSelect) - { + if (DecodeINPort(port) == PortDevice.GateArray) + { + GateArray.ReadPort(port, ref result); + } + else if (DecodeINPort(port) == PortDevice.CRCT) + { + CRCT.ReadPort(port, ref result); + } + else if (DecodeINPort(port) == PortDevice.ROMSelect) + { - } - else if (DecodeINPort(port) == PortDevice.Printer) - { + } + else if (DecodeINPort(port) == PortDevice.Printer) + { - } - else if (DecodeINPort(port) == PortDevice.PPI) - { - PPI.ReadPort(port, ref result); - } - else if (DecodeINPort(port) == PortDevice.Expansion) - { + } + else if (DecodeINPort(port) == PortDevice.PPI) + { + PPI.ReadPort(port, ref result); + } + else if (DecodeINPort(port) == PortDevice.Expansion) + { - } + } - return (byte)result; - } + return (byte)result; + } - /// - /// Writes a byte of data to a specified port address - /// Because of the port decoding, multiple devices can be written to - /// - public override void WritePort(ushort port, byte value) - { - BitArray portBits = new BitArray(BitConverter.GetBytes(port)); - BitArray dataBits = new BitArray(BitConverter.GetBytes(value)); - byte portUpper = (byte)(port >> 8); - byte portLower = (byte)(port & 0xff); + /// + /// Writes a byte of data to a specified port address + /// Because of the port decoding, multiple devices can be written to + /// + public override void WritePort(ushort port, byte value) + { + BitArray portBits = new BitArray(BitConverter.GetBytes(port)); + BitArray dataBits = new BitArray(BitConverter.GetBytes(value)); + byte portUpper = (byte)(port >> 8); + byte portLower = (byte)(port & 0xff); - var devs = DecodeOUTPort(port); + var devs = DecodeOUTPort(port); - foreach (var d in devs) - { - if (d == PortDevice.GateArray) - { - GateArray.WritePort(port, value); - } - else if (d == PortDevice.RAMManagement) - { - // not present in the unexpanded CPC464 - } - else if (d == PortDevice.CRCT) - { - CRCT.WritePort(port, value); - } - else if (d == PortDevice.ROMSelect) - { + foreach (var d in devs) + { + if (d == PortDevice.GateArray) + { + GateArray.WritePort(port, value); + } + else if (d == PortDevice.RAMManagement) + { + // not present in the unexpanded CPC464 + } + else if (d == PortDevice.CRCT) + { + CRCT.WritePort(port, value); + } + else if (d == PortDevice.ROMSelect) + { - } - else if (d == PortDevice.Printer) - { + } + else if (d == PortDevice.Printer) + { - } - else if (d == PortDevice.PPI) - { - PPI.WritePort(port, value); - } - else if (d == PortDevice.Expansion) - { + } + else if (d == PortDevice.PPI) + { + PPI.WritePort(port, value); + } + else if (d == PortDevice.Expansion) + { - } - } + } + } - return; - } - } + return; + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPC464/CPC464.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPC464/CPC464.cs index e1f1f57511..3deca82750 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPC464/CPC464.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPC464/CPC464.cs @@ -4,43 +4,43 @@ using System.Collections.Generic; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// CPC464 construction - /// - public partial class CPC464 : CPCBase - { - #region Construction + /// + /// CPC464 construction + /// + public partial class CPC464 : CPCBase + { + #region Construction - /// - /// Main constructor - /// - public CPC464(AmstradCPC cpc, Z80A cpu, List files, bool autoTape, AmstradCPC.BorderType borderType) - { - CPC = cpc; - CPU = cpu; + /// + /// Main constructor + /// + public CPC464(AmstradCPC cpc, Z80A cpu, List files, bool autoTape, AmstradCPC.BorderType borderType) + { + CPC = cpc; + CPU = cpu; - FrameLength = 79872; + FrameLength = 79872; - CRCT = new CRCT_6845(CRCT_6845.CRCTType.MC6845, this); - //CRT = new CRTDevice(this); - GateArray = new AmstradGateArray(this, AmstradGateArray.GateArrayType.Amstrad40007); - PPI = new PPI_8255(this); + CRCT = new CRCT_6845(CRCT_6845.CRCTType.MC6845, this); + //CRT = new CRTDevice(this); + GateArray = new AmstradGateArray(this, AmstradGateArray.GateArrayType.Amstrad40007); + PPI = new PPI_8255(this); - TapeBuzzer = new Beeper(this); - TapeBuzzer.Init(44100, FrameLength); + TapeBuzzer = new Beeper(this); + TapeBuzzer.Init(44100, FrameLength); - //AYDevice = new PSG(this, PSG.ay38910_type_t.AY38910_TYPE_8912, GateArray.PSGClockSpeed, 882 * 50); - AYDevice = new AY38912(this); - AYDevice.Init(44100, FrameLength); + //AYDevice = new PSG(this, PSG.ay38910_type_t.AY38910_TYPE_8912, GateArray.PSGClockSpeed, 882 * 50); + AYDevice = new AY38912(this); + AYDevice.Init(44100, FrameLength); - KeyboardDevice = new StandardKeyboard(this); + KeyboardDevice = new StandardKeyboard(this); - TapeDevice = new DatacorderDevice(autoTape); - TapeDevice.Init(this); + TapeDevice = new DatacorderDevice(autoTape); + TapeDevice.Init(this); - InitializeMedia(files); - } + InitializeMedia(files); + } - #endregion - } + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPC6128/CPC6128.Memory.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPC6128/CPC6128.Memory.cs index 0f89faff43..731fe3cd4d 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPC6128/CPC6128.Memory.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPC6128/CPC6128.Memory.cs @@ -6,257 +6,257 @@ using System.Threading.Tasks; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// CPC6128 - /// * Memory * - /// - public partial class CPC6128 : CPCBase - { - /// - /// Simulates reading from the bus - /// ROM and RAM paging should be handled here - /// - public override byte ReadBus(ushort addr) - { - int divisor = addr / 0x4000; - byte result = 0xff; + /// + /// CPC6128 + /// * Memory * + /// + public partial class CPC6128 : CPCBase + { + /// + /// Simulates reading from the bus + /// ROM and RAM paging should be handled here + /// + public override byte ReadBus(ushort addr) + { + int divisor = addr / 0x4000; + byte result = 0xff; - switch (divisor) - { - // RAM 0x000 - case 0: - if (LowerROMPaged) - { - result = ROMLower[addr % 0x4000]; - } - else - { - switch (RAMConfig) - { - case 2: - result = RAM4[addr % 0x4000]; - break; - default: - result = RAM0[addr % 0x4000]; - break; - } - } - break; + switch (divisor) + { + // RAM 0x000 + case 0: + if (LowerROMPaged) + { + result = ROMLower[addr % 0x4000]; + } + else + { + switch (RAMConfig) + { + case 2: + result = RAM4[addr % 0x4000]; + break; + default: + result = RAM0[addr % 0x4000]; + break; + } + } + break; - // RAM 0x4000 - case 1: - switch (RAMConfig) - { - case 0: - case 1: - result = RAM1[addr % 0x4000]; - break; - case 2: - case 5: - result = RAM5[addr % 0x4000]; - break; - case 3: - result = RAM3[addr % 0x4000]; - break; - case 4: - result = RAM4[addr % 0x4000]; - break; - case 6: - result = RAM6[addr % 0x4000]; - break; - case 7: - result = RAM7[addr % 0x4000]; - break; - } + // RAM 0x4000 + case 1: + switch (RAMConfig) + { + case 0: + case 1: + result = RAM1[addr % 0x4000]; + break; + case 2: + case 5: + result = RAM5[addr % 0x4000]; + break; + case 3: + result = RAM3[addr % 0x4000]; + break; + case 4: + result = RAM4[addr % 0x4000]; + break; + case 6: + result = RAM6[addr % 0x4000]; + break; + case 7: + result = RAM7[addr % 0x4000]; + break; + } - break; + break; - // RAM 0x8000 - case 2: - switch (RAMConfig) - { - case 2: - result = RAM6[addr % 0x4000]; - break; - default: - result = RAM2[addr % 0x4000]; - break; - } - break; + // RAM 0x8000 + case 2: + switch (RAMConfig) + { + case 2: + result = RAM6[addr % 0x4000]; + break; + default: + result = RAM2[addr % 0x4000]; + break; + } + break; - // RAM 0xc000 - case 3: - if (UpperROMPaged) - { - switch (UpperROMPosition) - { - case 7: - result = ROM7[addr % 0x4000]; - break; - case 0: - default: - result = ROM0[addr % 0x4000]; - break; - } - } - else - { - switch (RAMConfig) - { - case 1: - case 2: - case 3: - result = RAM7[addr % 0x4000]; - break; - default: - result = RAM3[addr % 0x4000]; - break; - } - } - break; - default: - break; - } + // RAM 0xc000 + case 3: + if (UpperROMPaged) + { + switch (UpperROMPosition) + { + case 7: + result = ROM7[addr % 0x4000]; + break; + case 0: + default: + result = ROM0[addr % 0x4000]; + break; + } + } + else + { + switch (RAMConfig) + { + case 1: + case 2: + case 3: + result = RAM7[addr % 0x4000]; + break; + default: + result = RAM3[addr % 0x4000]; + break; + } + } + break; + default: + break; + } - return result; - } + return result; + } - /// - /// Simulates writing to the bus - /// Writes to the bus ALWAYS go to RAM, regardless of what upper and lower ROMs are paged in - /// - public override void WriteBus(ushort addr, byte value) - { - int divisor = addr / 0x4000; + /// + /// Simulates writing to the bus + /// Writes to the bus ALWAYS go to RAM, regardless of what upper and lower ROMs are paged in + /// + public override void WriteBus(ushort addr, byte value) + { + int divisor = addr / 0x4000; - switch (divisor) - { - // RAM 0x000 - case 0: - switch (RAMConfig) - { - case 2: - RAM4[addr % 0x4000] = value; - break; - default: - RAM0[addr % 0x4000] = value; - break; - } - break; + switch (divisor) + { + // RAM 0x000 + case 0: + switch (RAMConfig) + { + case 2: + RAM4[addr % 0x4000] = value; + break; + default: + RAM0[addr % 0x4000] = value; + break; + } + break; - // RAM 0x4000 - case 1: - switch (RAMConfig) - { - case 0: - case 1: - RAM1[addr % 0x4000] = value; - break; - case 2: - case 5: - RAM5[addr % 0x4000] = value; - break; - case 3: - RAM3[addr % 0x4000] = value; - break; - case 4: - RAM4[addr % 0x4000] = value; - break; - case 6: - RAM6[addr % 0x4000] = value; - break; - case 7: - RAM7[addr % 0x4000] = value; - break; - } - - break; + // RAM 0x4000 + case 1: + switch (RAMConfig) + { + case 0: + case 1: + RAM1[addr % 0x4000] = value; + break; + case 2: + case 5: + RAM5[addr % 0x4000] = value; + break; + case 3: + RAM3[addr % 0x4000] = value; + break; + case 4: + RAM4[addr % 0x4000] = value; + break; + case 6: + RAM6[addr % 0x4000] = value; + break; + case 7: + RAM7[addr % 0x4000] = value; + break; + } - // RAM 0x8000 - case 2: - switch (RAMConfig) - { - case 2: - RAM6[addr % 0x4000] = value; - break; - default: - RAM2[addr % 0x4000] = value; - break; - } - break; + break; - // RAM 0xc000 - case 3: - switch (RAMConfig) - { - case 1: - case 2: - case 3: - RAM7[addr % 0x4000] = value; - break; - default: - RAM3[addr % 0x4000] = value; - break; - } - break; - default: - break; - } - } + // RAM 0x8000 + case 2: + switch (RAMConfig) + { + case 2: + RAM6[addr % 0x4000] = value; + break; + default: + RAM2[addr % 0x4000] = value; + break; + } + break; - /// - /// Reads a byte of data from a specified memory address - /// - public override byte ReadMemory(ushort addr) - { - var data = ReadBus(addr); - return data; - } + // RAM 0xc000 + case 3: + switch (RAMConfig) + { + case 1: + case 2: + case 3: + RAM7[addr % 0x4000] = value; + break; + default: + RAM3[addr % 0x4000] = value; + break; + } + break; + default: + break; + } + } - /// - /// Writes a byte of data to a specified memory address - /// (with memory contention if appropriate) - /// - public override void WriteMemory(ushort addr, byte value) - { - WriteBus(addr, value); - } + /// + /// Reads a byte of data from a specified memory address + /// + public override byte ReadMemory(ushort addr) + { + var data = ReadBus(addr); + return data; + } + + /// + /// Writes a byte of data to a specified memory address + /// (with memory contention if appropriate) + /// + public override void WriteMemory(ushort addr, byte value) + { + WriteBus(addr, value); + } - /// - /// Sets up the ROM - /// - public override void InitROM(RomData[] romData) - { - foreach (var r in romData) - { - if (r.ROMType == RomData.ROMChipType.Lower) - { - for (int i = 0; i < 0x4000; i++) - { - ROMLower[i] = r.RomBytes[i]; + /// + /// Sets up the ROM + /// + public override void InitROM(RomData[] romData) + { + foreach (var r in romData) + { + if (r.ROMType == RomData.ROMChipType.Lower) + { + for (int i = 0; i < 0x4000; i++) + { + ROMLower[i] = r.RomBytes[i]; - } - } - else - { - for (int i = 0; i < 0x4000; i++) - { - switch (r.ROMPosition) - { - case 0: - ROM0[i] = r.RomBytes[i]; - break; - case 7: - ROM7[i] = r.RomBytes[i]; - break; - } - } - } - } + } + } + else + { + for (int i = 0; i < 0x4000; i++) + { + switch (r.ROMPosition) + { + case 0: + ROM0[i] = r.RomBytes[i]; + break; + case 7: + ROM7[i] = r.RomBytes[i]; + break; + } + } + } + } - LowerROMPaged = true; - UpperROMPaged = true; - } - } + LowerROMPaged = true; + UpperROMPaged = true; + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPC6128/CPC6128.Port.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPC6128/CPC6128.Port.cs index 86c2fb5536..80138015c3 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPC6128/CPC6128.Port.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPC6128/CPC6128.Port.cs @@ -8,129 +8,129 @@ using System.Threading.Tasks; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// CPC6128 - /// * Port * - /// - public partial class CPC6128 : CPCBase - { - /// - /// Reads a byte of data from a specified port address - /// - public override byte ReadPort(ushort port) - { - BitArray portBits = new BitArray(BitConverter.GetBytes(port)); - byte portUpper = (byte)(port >> 8); - byte portLower = (byte)(port & 0xff); + /// + /// CPC6128 + /// * Port * + /// + public partial class CPC6128 : CPCBase + { + /// + /// Reads a byte of data from a specified port address + /// + public override byte ReadPort(ushort port) + { + BitArray portBits = new BitArray(BitConverter.GetBytes(port)); + byte portUpper = (byte)(port >> 8); + byte portLower = (byte)(port & 0xff); - int result = 0xff; + int result = 0xff; - if (DecodeINPort(port) == PortDevice.GateArray) - { - GateArray.ReadPort(port, ref result); - } - else if (DecodeINPort(port) == PortDevice.CRCT) - { - CRCT.ReadPort(port, ref result); - } - else if (DecodeINPort(port) == PortDevice.ROMSelect) - { + if (DecodeINPort(port) == PortDevice.GateArray) + { + GateArray.ReadPort(port, ref result); + } + else if (DecodeINPort(port) == PortDevice.CRCT) + { + CRCT.ReadPort(port, ref result); + } + else if (DecodeINPort(port) == PortDevice.ROMSelect) + { - } - else if (DecodeINPort(port) == PortDevice.Printer) - { + } + else if (DecodeINPort(port) == PortDevice.Printer) + { - } - else if (DecodeINPort(port) == PortDevice.PPI) - { - PPI.ReadPort(port, ref result); - } - else if (DecodeINPort(port) == PortDevice.Expansion) - { - if (!port.Bit(7)) - { - // FDC - if (port.Bit(8) && !port.Bit(0)) - { - // FDC status register - UPDDiskDevice.ReadStatus(ref result); - } - if (port.Bit(8) && port.Bit(0)) - { - // FDC data register - UPDDiskDevice.ReadData(ref result); - } - } - } + } + else if (DecodeINPort(port) == PortDevice.PPI) + { + PPI.ReadPort(port, ref result); + } + else if (DecodeINPort(port) == PortDevice.Expansion) + { + if (!port.Bit(7)) + { + // FDC + if (port.Bit(8) && !port.Bit(0)) + { + // FDC status register + UPDDiskDevice.ReadStatus(ref result); + } + if (port.Bit(8) && port.Bit(0)) + { + // FDC data register + UPDDiskDevice.ReadData(ref result); + } + } + } - return (byte)result; - } + return (byte)result; + } - /// - /// Writes a byte of data to a specified port address - /// Because of the port decoding, multiple devices can be written to - /// - public override void WritePort(ushort port, byte value) - { - BitArray portBits = new BitArray(BitConverter.GetBytes(port)); - BitArray dataBits = new BitArray(BitConverter.GetBytes(value)); - byte portUpper = (byte)(port >> 8); - byte portLower = (byte)(port & 0xff); + /// + /// Writes a byte of data to a specified port address + /// Because of the port decoding, multiple devices can be written to + /// + public override void WritePort(ushort port, byte value) + { + BitArray portBits = new BitArray(BitConverter.GetBytes(port)); + BitArray dataBits = new BitArray(BitConverter.GetBytes(value)); + byte portUpper = (byte)(port >> 8); + byte portLower = (byte)(port & 0xff); - var devs = DecodeOUTPort(port); + var devs = DecodeOUTPort(port); - foreach (var d in devs) - { - if (d == PortDevice.GateArray) - { - GateArray.WritePort(port, value); - } - else if (d == PortDevice.RAMManagement) - { - if (value.Bit(7) && value.Bit(6)) - { - RAMConfig = value & 0x07; + foreach (var d in devs) + { + if (d == PortDevice.GateArray) + { + GateArray.WritePort(port, value); + } + else if (d == PortDevice.RAMManagement) + { + if (value.Bit(7) && value.Bit(6)) + { + RAMConfig = value & 0x07; - // additional 64K bank index - var b64 = value & 0x38; - } - } - else if (d == PortDevice.CRCT) - { - CRCT.WritePort(port, value); - } - else if (d == PortDevice.ROMSelect) - { - UpperROMPosition = value; - } - else if (d == PortDevice.Printer) - { + // additional 64K bank index + var b64 = value & 0x38; + } + } + else if (d == PortDevice.CRCT) + { + CRCT.WritePort(port, value); + } + else if (d == PortDevice.ROMSelect) + { + UpperROMPosition = value; + } + else if (d == PortDevice.Printer) + { - } - else if (d == PortDevice.PPI) - { - PPI.WritePort(port, value); - } - else if (d == PortDevice.Expansion) - { - if (!port.Bit(7)) - { - // FDC - if (port.Bit(8) && !port.Bit(0) || port.Bit(8) && port.Bit(0)) - { - // FDC data register - UPDDiskDevice.WriteData(value); - } - if ((!port.Bit(8) && !port.Bit(0)) || (!port.Bit(8) && port.Bit(0))) - { - // FDC motor - UPDDiskDevice.Motor(value); - } - } - } - } + } + else if (d == PortDevice.PPI) + { + PPI.WritePort(port, value); + } + else if (d == PortDevice.Expansion) + { + if (!port.Bit(7)) + { + // FDC + if (port.Bit(8) && !port.Bit(0) || port.Bit(8) && port.Bit(0)) + { + // FDC data register + UPDDiskDevice.WriteData(value); + } + if ((!port.Bit(8) && !port.Bit(0)) || (!port.Bit(8) && port.Bit(0))) + { + // FDC motor + UPDDiskDevice.Motor(value); + } + } + } + } - return; - } - } + return; + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPC6128/CPC6128.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPC6128/CPC6128.cs index d14333524d..fee955f0f9 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPC6128/CPC6128.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPC6128/CPC6128.cs @@ -4,46 +4,46 @@ using System.Collections.Generic; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// CPC6128 construction - /// - public partial class CPC6128 : CPCBase - { - #region Construction + /// + /// CPC6128 construction + /// + public partial class CPC6128 : CPCBase + { + #region Construction - /// - /// Main constructor - /// - public CPC6128(AmstradCPC cpc, Z80A cpu, List files, bool autoTape, AmstradCPC.BorderType borderType) - { - CPC = cpc; - CPU = cpu; + /// + /// Main constructor + /// + public CPC6128(AmstradCPC cpc, Z80A cpu, List files, bool autoTape, AmstradCPC.BorderType borderType) + { + CPC = cpc; + CPU = cpu; - FrameLength = 79872; + FrameLength = 79872; - CRCT = new CRCT_6845(CRCT_6845.CRCTType.MC6845, this); - //CRT = new CRTDevice(this); - GateArray = new AmstradGateArray(this, AmstradGateArray.GateArrayType.Amstrad40007); - PPI = new PPI_8255(this); + CRCT = new CRCT_6845(CRCT_6845.CRCTType.MC6845, this); + //CRT = new CRTDevice(this); + GateArray = new AmstradGateArray(this, AmstradGateArray.GateArrayType.Amstrad40007); + PPI = new PPI_8255(this); - TapeBuzzer = new Beeper(this); - TapeBuzzer.Init(44100, FrameLength); + TapeBuzzer = new Beeper(this); + TapeBuzzer.Init(44100, FrameLength); - //AYDevice = new PSG(this, PSG.ay38910_type_t.AY38910_TYPE_8912, GateArray.PSGClockSpeed, 882 * 50); - AYDevice = new AY38912(this); - AYDevice.Init(44100, FrameLength); + //AYDevice = new PSG(this, PSG.ay38910_type_t.AY38910_TYPE_8912, GateArray.PSGClockSpeed, 882 * 50); + AYDevice = new AY38912(this); + AYDevice.Init(44100, FrameLength); - KeyboardDevice = new StandardKeyboard(this); + KeyboardDevice = new StandardKeyboard(this); - TapeDevice = new DatacorderDevice(autoTape); - TapeDevice.Init(this); + TapeDevice = new DatacorderDevice(autoTape); + TapeDevice.Init(this); - UPDDiskDevice = new NECUPD765(); - UPDDiskDevice.Init(this); + UPDDiskDevice = new NECUPD765(); + UPDDiskDevice.Init(this); - InitializeMedia(files); - } + InitializeMedia(files); + } - #endregion - } + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPCBase.Input.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPCBase.Input.cs index 5fc7868bab..bd22b023a8 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPCBase.Input.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPCBase.Input.cs @@ -3,293 +3,293 @@ using System.Linq; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// The abstract class that all emulated models will inherit from - /// * Input * - /// - public abstract partial class CPCBase - { - string Play = "Play Tape"; - string Stop = "Stop Tape"; - string RTZ = "RTZ Tape"; - string Record = "Record Tape"; - string NextTape = "Insert Next Tape"; - string PrevTape = "Insert Previous Tape"; - string NextBlock = "Next Tape Block"; - string PrevBlock = "Prev Tape Block"; - string TapeStatus = "Get Tape Status"; + /// + /// The abstract class that all emulated models will inherit from + /// * Input * + /// + public abstract partial class CPCBase + { + string Play = "Play Tape"; + string Stop = "Stop Tape"; + string RTZ = "RTZ Tape"; + string Record = "Record Tape"; + string NextTape = "Insert Next Tape"; + string PrevTape = "Insert Previous Tape"; + string NextBlock = "Next Tape Block"; + string PrevBlock = "Prev Tape Block"; + string TapeStatus = "Get Tape Status"; - string NextDisk = "Insert Next Disk"; - string PrevDisk = "Insert Previous Disk"; - string EjectDisk = "Eject Current Disk"; - string DiskStatus = "Get Disk Status"; + string NextDisk = "Insert Next Disk"; + string PrevDisk = "Insert Previous Disk"; + string EjectDisk = "Eject Current Disk"; + string DiskStatus = "Get Disk Status"; - string HardResetStr = "Power"; - string SoftResetStr = "Reset"; + string HardResetStr = "Power"; + string SoftResetStr = "Reset"; - bool pressed_Play = false; - bool pressed_Stop = false; - bool pressed_RTZ = false; - bool pressed_NextTape = false; - bool pressed_PrevTape = false; - bool pressed_NextBlock = false; - bool pressed_PrevBlock = false; - bool pressed_TapeStatus = false; - bool pressed_NextDisk = false; - bool pressed_PrevDisk = false; - bool pressed_EjectDisk = false; - bool pressed_DiskStatus = false; - bool pressed_HardReset = false; - bool pressed_SoftReset = false; + bool pressed_Play = false; + bool pressed_Stop = false; + bool pressed_RTZ = false; + bool pressed_NextTape = false; + bool pressed_PrevTape = false; + bool pressed_NextBlock = false; + bool pressed_PrevBlock = false; + bool pressed_TapeStatus = false; + bool pressed_NextDisk = false; + bool pressed_PrevDisk = false; + bool pressed_EjectDisk = false; + bool pressed_DiskStatus = false; + bool pressed_HardReset = false; + bool pressed_SoftReset = false; - /// - /// Cycles through all the input callbacks - /// This should be done once per frame - /// - public void PollInput() - { - CPC.InputCallbacks.Call(); + /// + /// Cycles through all the input callbacks + /// This should be done once per frame + /// + public void PollInput() + { + CPC.InputCallbacks.Call(); - lock (this) - { - // parse single keyboard matrix keys. - // J1 and J2 are scanned as part of the keyboard - for (var i = 0; i < KeyboardDevice.KeyboardMatrix.Length; i++) - { - string key = KeyboardDevice.KeyboardMatrix[i]; - bool prevState = KeyboardDevice.GetKeyStatus(key); - bool currState = CPC._controller.IsPressed(key); + lock (this) + { + // parse single keyboard matrix keys. + // J1 and J2 are scanned as part of the keyboard + for (var i = 0; i < KeyboardDevice.KeyboardMatrix.Length; i++) + { + string key = KeyboardDevice.KeyboardMatrix[i]; + bool prevState = KeyboardDevice.GetKeyStatus(key); + bool currState = CPC._controller.IsPressed(key); - if (currState != prevState) - KeyboardDevice.SetKeyStatus(key, currState); - } + if (currState != prevState) + KeyboardDevice.SetKeyStatus(key, currState); + } - // non matrix keys (J2) - foreach (string k in KeyboardDevice.NonMatrixKeys) - { - if (!k.StartsWith("P2")) - continue; + // non matrix keys (J2) + foreach (string k in KeyboardDevice.NonMatrixKeys) + { + if (!k.StartsWith("P2")) + continue; - bool currState = CPC._controller.IsPressed(k); + bool currState = CPC._controller.IsPressed(k); - switch (k) - { - case "P2 Up": - if (currState) - KeyboardDevice.SetKeyStatus("Key 6", true); - else if (!KeyboardDevice.GetKeyStatus("Key 6")) - KeyboardDevice.SetKeyStatus("Key 6", false); - break; - case "P2 Down": - if (currState) - KeyboardDevice.SetKeyStatus("Key 5", true); - else if (!KeyboardDevice.GetKeyStatus("Key 5")) - KeyboardDevice.SetKeyStatus("Key 5", false); - break; - case "P2 Left": - if (currState) - KeyboardDevice.SetKeyStatus("Key R", true); - else if (!KeyboardDevice.GetKeyStatus("Key R")) - KeyboardDevice.SetKeyStatus("Key R", false); - break; - case "P2 Right": - if (currState) - KeyboardDevice.SetKeyStatus("Key T", true); - else if (!KeyboardDevice.GetKeyStatus("Key T")) - KeyboardDevice.SetKeyStatus("Key T", false); - break; - case "P2 Fire": - if (currState) - KeyboardDevice.SetKeyStatus("Key G", true); - else if (!KeyboardDevice.GetKeyStatus("Key G")) - KeyboardDevice.SetKeyStatus("Key G", false); - break; - } - } - } + switch (k) + { + case "P2 Up": + if (currState) + KeyboardDevice.SetKeyStatus("Key 6", true); + else if (!KeyboardDevice.GetKeyStatus("Key 6")) + KeyboardDevice.SetKeyStatus("Key 6", false); + break; + case "P2 Down": + if (currState) + KeyboardDevice.SetKeyStatus("Key 5", true); + else if (!KeyboardDevice.GetKeyStatus("Key 5")) + KeyboardDevice.SetKeyStatus("Key 5", false); + break; + case "P2 Left": + if (currState) + KeyboardDevice.SetKeyStatus("Key R", true); + else if (!KeyboardDevice.GetKeyStatus("Key R")) + KeyboardDevice.SetKeyStatus("Key R", false); + break; + case "P2 Right": + if (currState) + KeyboardDevice.SetKeyStatus("Key T", true); + else if (!KeyboardDevice.GetKeyStatus("Key T")) + KeyboardDevice.SetKeyStatus("Key T", false); + break; + case "P2 Fire": + if (currState) + KeyboardDevice.SetKeyStatus("Key G", true); + else if (!KeyboardDevice.GetKeyStatus("Key G")) + KeyboardDevice.SetKeyStatus("Key G", false); + break; + } + } + } - // Tape control - if (CPC._controller.IsPressed(Play)) - { - if (!pressed_Play) - { - CPC.OSD_FireInputMessage(Play); - TapeDevice.Play(); - pressed_Play = true; - } - } - else - pressed_Play = false; + // Tape control + if (CPC._controller.IsPressed(Play)) + { + if (!pressed_Play) + { + CPC.OSD_FireInputMessage(Play); + TapeDevice.Play(); + pressed_Play = true; + } + } + else + pressed_Play = false; - if (CPC._controller.IsPressed(Stop)) - { - if (!pressed_Stop) - { - CPC.OSD_FireInputMessage(Stop); - TapeDevice.Stop(); - pressed_Stop = true; - } - } - else - pressed_Stop = false; + if (CPC._controller.IsPressed(Stop)) + { + if (!pressed_Stop) + { + CPC.OSD_FireInputMessage(Stop); + TapeDevice.Stop(); + pressed_Stop = true; + } + } + else + pressed_Stop = false; - if (CPC._controller.IsPressed(RTZ)) - { - if (!pressed_RTZ) - { - CPC.OSD_FireInputMessage(RTZ); - TapeDevice.RTZ(); - pressed_RTZ = true; - } - } - else - pressed_RTZ = false; + if (CPC._controller.IsPressed(RTZ)) + { + if (!pressed_RTZ) + { + CPC.OSD_FireInputMessage(RTZ); + TapeDevice.RTZ(); + pressed_RTZ = true; + } + } + else + pressed_RTZ = false; - if (CPC._controller.IsPressed(Record)) - { + if (CPC._controller.IsPressed(Record)) + { - } - if (CPC._controller.IsPressed(NextTape)) - { - if (!pressed_NextTape) - { - CPC.OSD_FireInputMessage(NextTape); - TapeMediaIndex++; - pressed_NextTape = true; - } - } - else - pressed_NextTape = false; + } + if (CPC._controller.IsPressed(NextTape)) + { + if (!pressed_NextTape) + { + CPC.OSD_FireInputMessage(NextTape); + TapeMediaIndex++; + pressed_NextTape = true; + } + } + else + pressed_NextTape = false; - if (CPC._controller.IsPressed(PrevTape)) - { - if (!pressed_PrevTape) - { - CPC.OSD_FireInputMessage(PrevTape); - TapeMediaIndex--; - pressed_PrevTape = true; - } - } - else - pressed_PrevTape = false; + if (CPC._controller.IsPressed(PrevTape)) + { + if (!pressed_PrevTape) + { + CPC.OSD_FireInputMessage(PrevTape); + TapeMediaIndex--; + pressed_PrevTape = true; + } + } + else + pressed_PrevTape = false; - if (CPC._controller.IsPressed(NextBlock)) - { - if (!pressed_NextBlock) - { - CPC.OSD_FireInputMessage(NextBlock); - TapeDevice.SkipBlock(true); - pressed_NextBlock = true; - } - } - else - pressed_NextBlock = false; + if (CPC._controller.IsPressed(NextBlock)) + { + if (!pressed_NextBlock) + { + CPC.OSD_FireInputMessage(NextBlock); + TapeDevice.SkipBlock(true); + pressed_NextBlock = true; + } + } + else + pressed_NextBlock = false; - if (CPC._controller.IsPressed(PrevBlock)) - { - if (!pressed_PrevBlock) - { - CPC.OSD_FireInputMessage(PrevBlock); - TapeDevice.SkipBlock(false); - pressed_PrevBlock = true; - } - } - else - pressed_PrevBlock = false; + if (CPC._controller.IsPressed(PrevBlock)) + { + if (!pressed_PrevBlock) + { + CPC.OSD_FireInputMessage(PrevBlock); + TapeDevice.SkipBlock(false); + pressed_PrevBlock = true; + } + } + else + pressed_PrevBlock = false; - if (CPC._controller.IsPressed(TapeStatus)) - { - if (!pressed_TapeStatus) - { - //Spectrum.OSD_FireInputMessage(TapeStatus); - CPC.OSD_ShowTapeStatus(); - pressed_TapeStatus = true; - } - } - else - pressed_TapeStatus = false; + if (CPC._controller.IsPressed(TapeStatus)) + { + if (!pressed_TapeStatus) + { + //Spectrum.OSD_FireInputMessage(TapeStatus); + CPC.OSD_ShowTapeStatus(); + pressed_TapeStatus = true; + } + } + else + pressed_TapeStatus = false; - if (CPC._controller.IsPressed(HardResetStr)) - { - if (!pressed_HardReset) - { - HardReset(); - pressed_HardReset = true; - } - } - else - pressed_HardReset = false; + if (CPC._controller.IsPressed(HardResetStr)) + { + if (!pressed_HardReset) + { + HardReset(); + pressed_HardReset = true; + } + } + else + pressed_HardReset = false; - if (CPC._controller.IsPressed(SoftResetStr)) - { - if (!pressed_SoftReset) - { - SoftReset(); - pressed_SoftReset = true; - } - } - else - pressed_SoftReset = false; + if (CPC._controller.IsPressed(SoftResetStr)) + { + if (!pressed_SoftReset) + { + SoftReset(); + pressed_SoftReset = true; + } + } + else + pressed_SoftReset = false; - // disk control - if (CPC._controller.IsPressed(NextDisk)) - { - if (!pressed_NextDisk) - { - CPC.OSD_FireInputMessage(NextDisk); - DiskMediaIndex++; - pressed_NextDisk = true; - } - } - else - pressed_NextDisk = false; + // disk control + if (CPC._controller.IsPressed(NextDisk)) + { + if (!pressed_NextDisk) + { + CPC.OSD_FireInputMessage(NextDisk); + DiskMediaIndex++; + pressed_NextDisk = true; + } + } + else + pressed_NextDisk = false; - if (CPC._controller.IsPressed(PrevDisk)) - { - if (!pressed_PrevDisk) - { - CPC.OSD_FireInputMessage(PrevDisk); - DiskMediaIndex--; - pressed_PrevDisk = true; - } - } - else - pressed_PrevDisk = false; + if (CPC._controller.IsPressed(PrevDisk)) + { + if (!pressed_PrevDisk) + { + CPC.OSD_FireInputMessage(PrevDisk); + DiskMediaIndex--; + pressed_PrevDisk = true; + } + } + else + pressed_PrevDisk = false; - if (CPC._controller.IsPressed(EjectDisk)) - { - if (!pressed_EjectDisk) - { - CPC.OSD_FireInputMessage(EjectDisk); - //if (UPDDiskDevice != null) - // UPDDiskDevice.FDD_EjectDisk(); - } - } - else - pressed_EjectDisk = false; + if (CPC._controller.IsPressed(EjectDisk)) + { + if (!pressed_EjectDisk) + { + CPC.OSD_FireInputMessage(EjectDisk); + //if (UPDDiskDevice != null) + // UPDDiskDevice.FDD_EjectDisk(); + } + } + else + pressed_EjectDisk = false; - if (CPC._controller.IsPressed(DiskStatus)) - { - if (!pressed_DiskStatus) - { - //Spectrum.OSD_FireInputMessage(TapeStatus); - CPC.OSD_ShowDiskStatus(); - pressed_DiskStatus = true; - } - } - else - pressed_DiskStatus = false; - } + if (CPC._controller.IsPressed(DiskStatus)) + { + if (!pressed_DiskStatus) + { + //Spectrum.OSD_FireInputMessage(TapeStatus); + CPC.OSD_ShowDiskStatus(); + pressed_DiskStatus = true; + } + } + else + pressed_DiskStatus = false; + } - /// - /// Signs whether input read has been requested - /// This forms part of the IEmulator LagFrame implementation - /// - private bool inputRead; - public bool InputRead - { - get { return inputRead; } - set { inputRead = value; } - } - } + /// + /// Signs whether input read has been requested + /// This forms part of the IEmulator LagFrame implementation + /// + private bool inputRead; + public bool InputRead + { + get { return inputRead; } + set { inputRead = value; } + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPCBase.Media.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPCBase.Media.cs index 0c3d98f48a..e790fb1afe 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPCBase.Media.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPCBase.Media.cs @@ -5,262 +5,262 @@ using System.Text; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// The abstract class that all emulated models will inherit from - /// * Imported media * - /// - public abstract partial class CPCBase - { - /// - /// The tape or disk image(s) that are passed in from the main ZXSpectrum class - /// + /// + /// The abstract class that all emulated models will inherit from + /// * Imported media * + /// + public abstract partial class CPCBase + { + /// + /// The tape or disk image(s) that are passed in from the main ZXSpectrum class + /// protected List mediaImages { get; set; } - /// - /// Tape images - /// - public List tapeImages { get; set; } + /// + /// Tape images + /// + public List tapeImages { get; set; } - /// - /// Disk images - /// - public List diskImages { get; set; } + /// + /// Disk images + /// + public List diskImages { get; set; } - /// - /// The index of the currently 'loaded' tape image - /// - protected int tapeMediaIndex; - public int TapeMediaIndex - { - get { return tapeMediaIndex; } - set - { - int tmp = value; - int result = value; + /// + /// The index of the currently 'loaded' tape image + /// + protected int tapeMediaIndex; + public int TapeMediaIndex + { + get { return tapeMediaIndex; } + set + { + int tmp = value; + int result = value; - if (tapeImages == null || tapeImages.Count() == 0) - { - // no tape images found - return; - } + if (tapeImages == null || tapeImages.Count() == 0) + { + // no tape images found + return; + } - if (value >= tapeImages.Count()) - { - // media at this index does not exist - loop back to 0 - result = 0; - } - else if (value < 0) - { - // negative index not allowed - move to last item in the collection - result = tapeImages.Count() - 1; - } + if (value >= tapeImages.Count()) + { + // media at this index does not exist - loop back to 0 + result = 0; + } + else if (value < 0) + { + // negative index not allowed - move to last item in the collection + result = tapeImages.Count() - 1; + } - // load the media into the tape device - tapeMediaIndex = result; - // fire osd message - //Spectrum.OSD_TapeInserted(); - LoadTapeMedia(); - } - } + // load the media into the tape device + tapeMediaIndex = result; + // fire osd message + //Spectrum.OSD_TapeInserted(); + LoadTapeMedia(); + } + } - /// - /// The index of the currently 'loaded' disk image - /// - protected int diskMediaIndex; - public int DiskMediaIndex - { - get { return diskMediaIndex; } - set - { - int tmp = value; - int result = value; + /// + /// The index of the currently 'loaded' disk image + /// + protected int diskMediaIndex; + public int DiskMediaIndex + { + get { return diskMediaIndex; } + set + { + int tmp = value; + int result = value; - if (diskImages == null || diskImages.Count() == 0) - { - // no tape images found - return; - } + if (diskImages == null || diskImages.Count() == 0) + { + // no tape images found + return; + } - if (value >= diskImages.Count()) - { - // media at this index does not exist - loop back to 0 - result = 0; - } - else if (value < 0) - { - // negative index not allowed - move to last item in the collection - result = diskImages.Count() - 1; - } + if (value >= diskImages.Count()) + { + // media at this index does not exist - loop back to 0 + result = 0; + } + else if (value < 0) + { + // negative index not allowed - move to last item in the collection + result = diskImages.Count() - 1; + } - // load the media into the disk device - diskMediaIndex = result; + // load the media into the disk device + diskMediaIndex = result; - // fire osd message - CPC.OSD_DiskInserted(); + // fire osd message + CPC.OSD_DiskInserted(); - LoadDiskMedia(); - } - } + LoadDiskMedia(); + } + } - /// - /// Called on first instantiation (and subsequent core reboots) - /// - protected void InitializeMedia(List files) - { - mediaImages = files; - LoadAllMedia(); - } + /// + /// Called on first instantiation (and subsequent core reboots) + /// + protected void InitializeMedia(List files) + { + mediaImages = files; + LoadAllMedia(); + } - /// - /// Attempts to load all media into the relevant structures - /// - protected void LoadAllMedia() - { - tapeImages = new List(); - diskImages = new List(); + /// + /// Attempts to load all media into the relevant structures + /// + protected void LoadAllMedia() + { + tapeImages = new List(); + diskImages = new List(); - int cnt = 0; - foreach (var m in mediaImages) - { - switch (IdentifyMedia(m)) - { - case CPCMediaType.Tape: - tapeImages.Add(m); - CPC._tapeInfo.Add(CPC._gameInfo[cnt]); - break; - case CPCMediaType.Disk: - diskImages.Add(m); - CPC._diskInfo.Add(CPC._gameInfo[cnt]); - break; - case CPCMediaType.DiskDoubleSided: - // this is a bit tricky. we will attempt to parse the double sided disk image byte array, - // then output two separate image byte arrays - List working = new List(); - foreach (DiskType type in Enum.GetValues(typeof(DiskType))) - { - bool found = false; + int cnt = 0; + foreach (var m in mediaImages) + { + switch (IdentifyMedia(m)) + { + case CPCMediaType.Tape: + tapeImages.Add(m); + CPC._tapeInfo.Add(CPC._gameInfo[cnt]); + break; + case CPCMediaType.Disk: + diskImages.Add(m); + CPC._diskInfo.Add(CPC._gameInfo[cnt]); + break; + case CPCMediaType.DiskDoubleSided: + // this is a bit tricky. we will attempt to parse the double sided disk image byte array, + // then output two separate image byte arrays + List working = new List(); + foreach (DiskType type in Enum.GetValues(typeof(DiskType))) + { + bool found = false; - switch (type) - { - case DiskType.CPCExtended: - found = CPCExtendedFloppyDisk.SplitDoubleSided(m, working); - break; - case DiskType.CPC: - found = CPCFloppyDisk.SplitDoubleSided(m, working); - break; - } + switch (type) + { + case DiskType.CPCExtended: + found = CPCExtendedFloppyDisk.SplitDoubleSided(m, working); + break; + case DiskType.CPC: + found = CPCFloppyDisk.SplitDoubleSided(m, working); + break; + } - if (found) - { - // add side 1 - diskImages.Add(working[0]); - // add side 2 - diskImages.Add(working[1]); + if (found) + { + // add side 1 + diskImages.Add(working[0]); + // add side 2 + diskImages.Add(working[1]); - Common.GameInfo one = new Common.GameInfo(); - Common.GameInfo two = new Common.GameInfo(); - var gi = CPC._gameInfo[cnt]; - for (int i = 0; i < 2; i++) - { - Common.GameInfo work = new Common.GameInfo(); - if (i == 0) - { - work = one; - } - else if (i == 1) - { - work = two; - } + Common.GameInfo one = new Common.GameInfo(); + Common.GameInfo two = new Common.GameInfo(); + var gi = CPC._gameInfo[cnt]; + for (int i = 0; i < 2; i++) + { + Common.GameInfo work = new Common.GameInfo(); + if (i == 0) + { + work = one; + } + else if (i == 1) + { + work = two; + } - work.FirmwareHash = gi.FirmwareHash; - work.Hash = gi.Hash; - work.Name = gi.Name + " (Parsed Side " + (i + 1) + ")"; - work.Region = gi.Region; - work.NotInDatabase = gi.NotInDatabase; - work.Status = gi.Status; - work.System = gi.System; + work.FirmwareHash = gi.FirmwareHash; + work.Hash = gi.Hash; + work.Name = gi.Name + " (Parsed Side " + (i + 1) + ")"; + work.Region = gi.Region; + work.NotInDatabase = gi.NotInDatabase; + work.Status = gi.Status; + work.System = gi.System; - CPC._diskInfo.Add(work); - } - } - else - { + CPC._diskInfo.Add(work); + } + } + else + { - } - } - break; - } + } + } + break; + } - cnt++; - } + cnt++; + } - if (tapeImages.Count > 0) - LoadTapeMedia(); + if (tapeImages.Count > 0) + LoadTapeMedia(); - if (diskImages.Count > 0) - LoadDiskMedia(); - } + if (diskImages.Count > 0) + LoadDiskMedia(); + } - /// - /// Attempts to load a tape into the tape device based on tapeMediaIndex - /// - protected void LoadTapeMedia() - { - TapeDevice.LoadTape(tapeImages[tapeMediaIndex]); - } + /// + /// Attempts to load a tape into the tape device based on tapeMediaIndex + /// + protected void LoadTapeMedia() + { + TapeDevice.LoadTape(tapeImages[tapeMediaIndex]); + } - /// - /// Attempts to load a disk into the disk device based on diskMediaIndex - /// - protected void LoadDiskMedia() - { - if (this.GetType() == typeof(CPC464)) - { - CPC.CoreComm.ShowMessage("You are trying to load one of more disk images.\n\n Please select something other than CPC 464 emulation immediately and reboot the core"); - return; - } + /// + /// Attempts to load a disk into the disk device based on diskMediaIndex + /// + protected void LoadDiskMedia() + { + if (this.GetType() == typeof(CPC464)) + { + CPC.CoreComm.ShowMessage("You are trying to load one of more disk images.\n\n Please select something other than CPC 464 emulation immediately and reboot the core"); + return; + } - UPDDiskDevice.FDD_LoadDisk(diskImages[diskMediaIndex]); - } + UPDDiskDevice.FDD_LoadDisk(diskImages[diskMediaIndex]); + } - /// - /// Identifies and sorts the various media types - /// - private CPCMediaType IdentifyMedia(byte[] data) - { - // get first 16 bytes as a string - string hdr = Encoding.ASCII.GetString(data.Take(16).ToArray()); + /// + /// Identifies and sorts the various media types + /// + private CPCMediaType IdentifyMedia(byte[] data) + { + // get first 16 bytes as a string + string hdr = Encoding.ASCII.GetString(data.Take(16).ToArray()); - // disk checking first - if (hdr.ToUpper().Contains("EXTENDED CPC DSK") || hdr.ToUpper().Contains("MV - CPC")) - { - // amstrad .dsk disk file - // check for number of sides - var sides = data[0x31]; - if (sides == 1) - return CPCMediaType.Disk; - else - return CPCMediaType.DiskDoubleSided; - } + // disk checking first + if (hdr.ToUpper().Contains("EXTENDED CPC DSK") || hdr.ToUpper().Contains("MV - CPC")) + { + // amstrad .dsk disk file + // check for number of sides + var sides = data[0x31]; + if (sides == 1) + return CPCMediaType.Disk; + else + return CPCMediaType.DiskDoubleSided; + } - // tape checking - if (hdr.ToUpper().StartsWith("ZXTAPE!")) - { - // cdt tape file - return CPCMediaType.Tape; - } + // tape checking + if (hdr.ToUpper().StartsWith("ZXTAPE!")) + { + // cdt tape file + return CPCMediaType.Tape; + } - // not found - return CPCMediaType.None; - } - } + // not found + return CPCMediaType.None; + } + } - public enum CPCMediaType - { - None, - Tape, - Disk, - DiskDoubleSided - } + public enum CPCMediaType + { + None, + Tape, + Disk, + DiskDoubleSided + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPCBase.Memory.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPCBase.Memory.cs index ccfdf22ccc..b4e3badeb8 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPCBase.Memory.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPCBase.Memory.cs @@ -3,144 +3,144 @@ using System.Collections.Generic; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// The abstract class that all emulated models will inherit from - /// * Memory * - /// - public abstract partial class CPCBase - { - #region Memory Fields & Properties + /// + /// The abstract class that all emulated models will inherit from + /// * Memory * + /// + public abstract partial class CPCBase + { + #region Memory Fields & Properties - /* ROM Banks */ - /// - /// Lower: OS ROM - /// - public byte[] ROMLower = new byte[0x4000]; - /// - /// Upper: POS 0 (usually BASIC) - /// - public byte[] ROM0 = new byte[0x4000]; - /// - /// Upper: POS 7 (usually AMSDOS) - /// - public byte[] ROM7 = new byte[0x4000]; + /* ROM Banks */ + /// + /// Lower: OS ROM + /// + public byte[] ROMLower = new byte[0x4000]; + /// + /// Upper: POS 0 (usually BASIC) + /// + public byte[] ROM0 = new byte[0x4000]; + /// + /// Upper: POS 7 (usually AMSDOS) + /// + public byte[] ROM7 = new byte[0x4000]; - /* RAM Banks - Lower 64K */ - public byte[] RAM0 = new byte[0x4000]; - public byte[] RAM1 = new byte[0x4000]; - public byte[] RAM2 = new byte[0x4000]; - public byte[] RAM3 = new byte[0x4000]; + /* RAM Banks - Lower 64K */ + public byte[] RAM0 = new byte[0x4000]; + public byte[] RAM1 = new byte[0x4000]; + public byte[] RAM2 = new byte[0x4000]; + public byte[] RAM3 = new byte[0x4000]; - /* RAM Banks - Upper 64K */ - public byte[] RAM4 = new byte[0x4000]; - public byte[] RAM5 = new byte[0x4000]; - public byte[] RAM6 = new byte[0x4000]; - public byte[] RAM7 = new byte[0x4000]; + /* RAM Banks - Upper 64K */ + public byte[] RAM4 = new byte[0x4000]; + public byte[] RAM5 = new byte[0x4000]; + public byte[] RAM6 = new byte[0x4000]; + public byte[] RAM7 = new byte[0x4000]; - /// - /// Signs whether Upper ROM is paged in - /// - public bool UpperROMPaged; + /// + /// Signs whether Upper ROM is paged in + /// + public bool UpperROMPaged; - /// - /// The position of the currently paged upper ROM - /// - public int UpperROMPosition; + /// + /// The position of the currently paged upper ROM + /// + public int UpperROMPosition; - /// - /// Signs whether Lower ROM is paged in - /// - public bool LowerROMPaged; + /// + /// Signs whether Lower ROM is paged in + /// + public bool LowerROMPaged; - /// - /// The currently selected RAM config - /// - public int RAMConfig; + /// + /// The currently selected RAM config + /// + public int RAMConfig; - /// - /// Always 0 on a CPC6128 - /// On a machine with more than 128K RAM (standard memory expansion) this selects each additional 64K above the first upper 64K - /// - public int RAM64KBank; + /// + /// Always 0 on a CPC6128 + /// On a machine with more than 128K RAM (standard memory expansion) this selects each additional 64K above the first upper 64K + /// + public int RAM64KBank; - #endregion + #endregion - #region Memory Related Methods + #region Memory Related Methods - /// - /// Simulates reading from the bus - /// Paging should be handled here - /// - public abstract byte ReadBus(ushort addr); + /// + /// Simulates reading from the bus + /// Paging should be handled here + /// + public abstract byte ReadBus(ushort addr); - /// - /// Pushes a value onto the data bus that should be valid as long as the interrupt is true - /// - public virtual byte PushBus() - { - return 0xFF; - } + /// + /// Pushes a value onto the data bus that should be valid as long as the interrupt is true + /// + public virtual byte PushBus() + { + return 0xFF; + } - /// - /// Simulates writing to the bus - /// Paging should be handled here - /// - public abstract void WriteBus(ushort addr, byte value); + /// + /// Simulates writing to the bus + /// Paging should be handled here + /// + public abstract void WriteBus(ushort addr, byte value); - /// - /// Reads a byte of data from a specified memory address - /// (with memory contention if appropriate) - /// - public abstract byte ReadMemory(ushort addr); + /// + /// Reads a byte of data from a specified memory address + /// (with memory contention if appropriate) + /// + public abstract byte ReadMemory(ushort addr); - /// - /// Writes a byte of data to a specified memory address - /// (with memory contention if appropriate) - /// - public abstract void WriteMemory(ushort addr, byte value); + /// + /// Writes a byte of data to a specified memory address + /// (with memory contention if appropriate) + /// + public abstract void WriteMemory(ushort addr, byte value); - /// - /// Sets up the ROM - /// - public abstract void InitROM(RomData[] romData); + /// + /// Sets up the ROM + /// + public abstract void InitROM(RomData[] romData); - /// - /// ULA reads the memory at the specified address - /// (No memory contention) - /// - public virtual byte FetchScreenMemory(ushort addr) - { - int divisor = addr / 0x4000; - byte result = 0xff; + /// + /// ULA reads the memory at the specified address + /// (No memory contention) + /// + public virtual byte FetchScreenMemory(ushort addr) + { + int divisor = addr / 0x4000; + byte result = 0xff; - switch (divisor) - { - // 0x000 - case 0: - result = RAM0[addr % 0x4000]; - break; + switch (divisor) + { + // 0x000 + case 0: + result = RAM0[addr % 0x4000]; + break; - // 0x4000 - case 1: - result = RAM1[addr % 0x4000]; - break; + // 0x4000 + case 1: + result = RAM1[addr % 0x4000]; + break; - // 0x8000 - case 2: - result = RAM2[addr % 0x4000]; - break; + // 0x8000 + case 2: + result = RAM2[addr % 0x4000]; + break; - // 0xc000 or UpperROM - case 3: - result = RAM3[addr % 0x4000]; - break; - default: - break; - } + // 0xc000 or UpperROM + case 3: + result = RAM3[addr % 0x4000]; + break; + default: + break; + } - return result; - } + return result; + } - #endregion - } + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPCBase.Port.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPCBase.Port.cs index 4cee77f59f..73fc120790 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPCBase.Port.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPCBase.Port.cs @@ -5,103 +5,103 @@ using System.Collections.Generic; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// The abstract class that all emulated models will inherit from - /// * Port Access * - /// - public abstract partial class CPCBase - { - /// - /// Reads a byte of data from a specified port address - /// - public abstract byte ReadPort(ushort port); + /// + /// The abstract class that all emulated models will inherit from + /// * Port Access * + /// + public abstract partial class CPCBase + { + /// + /// Reads a byte of data from a specified port address + /// + public abstract byte ReadPort(ushort port); - /// - /// Writes a byte of data to a specified port address - /// - public abstract void WritePort(ushort port, byte value); + /// + /// Writes a byte of data to a specified port address + /// + public abstract void WritePort(ushort port, byte value); - /// - /// Returns a single port device enum based on the port address - /// (for IN operations) - /// https://web.archive.org/web/20090808085929/http://www.cepece.info/amstrad/docs/iopord.html - /// http://www.cpcwiki.eu/index.php/I/O_Port_Summary - /// - protected virtual PortDevice DecodeINPort(ushort port) - { - PortDevice dev = PortDevice.Unknown; + /// + /// Returns a single port device enum based on the port address + /// (for IN operations) + /// https://web.archive.org/web/20090808085929/http://www.cepece.info/amstrad/docs/iopord.html + /// http://www.cpcwiki.eu/index.php/I/O_Port_Summary + /// + protected virtual PortDevice DecodeINPort(ushort port) + { + PortDevice dev = PortDevice.Unknown; - if (!port.Bit(15) && port.Bit(14)) - dev = PortDevice.GateArray; + if (!port.Bit(15) && port.Bit(14)) + dev = PortDevice.GateArray; - else if (!port.Bit(15)) - dev = PortDevice.RAMManagement; + else if (!port.Bit(15)) + dev = PortDevice.RAMManagement; - else if (!port.Bit(14)) - dev = PortDevice.CRCT; + else if (!port.Bit(14)) + dev = PortDevice.CRCT; - else if (!port.Bit(13)) - dev = PortDevice.ROMSelect; + else if (!port.Bit(13)) + dev = PortDevice.ROMSelect; - else if (!port.Bit(12)) - dev = PortDevice.Printer; + else if (!port.Bit(12)) + dev = PortDevice.Printer; - else if (!port.Bit(11)) - dev = PortDevice.PPI; + else if (!port.Bit(11)) + dev = PortDevice.PPI; - else if (!port.Bit(10)) - dev = PortDevice.Expansion; + else if (!port.Bit(10)) + dev = PortDevice.Expansion; - return dev; - } + return dev; + } - /// - /// Returns a list of port device enums based on the port address - /// (for OUT operations) - /// https://web.archive.org/web/20090808085929/http://www.cepece.info/amstrad/docs/iopord.html - /// http://www.cpcwiki.eu/index.php/I/O_Port_Summary - /// - protected virtual List DecodeOUTPort(ushort port) - { - List devs = new List(); + /// + /// Returns a list of port device enums based on the port address + /// (for OUT operations) + /// https://web.archive.org/web/20090808085929/http://www.cepece.info/amstrad/docs/iopord.html + /// http://www.cpcwiki.eu/index.php/I/O_Port_Summary + /// + protected virtual List DecodeOUTPort(ushort port) + { + List devs = new List(); - if (!port.Bit(15) && port.Bit(14)) - devs.Add(PortDevice.GateArray); + if (!port.Bit(15) && port.Bit(14)) + devs.Add(PortDevice.GateArray); - if (!port.Bit(15)) - devs.Add(PortDevice.RAMManagement); + if (!port.Bit(15)) + devs.Add(PortDevice.RAMManagement); - if (!port.Bit(14)) - devs.Add(PortDevice.CRCT); + if (!port.Bit(14)) + devs.Add(PortDevice.CRCT); - if (!port.Bit(13)) - devs.Add(PortDevice.ROMSelect); + if (!port.Bit(13)) + devs.Add(PortDevice.ROMSelect); - if (!port.Bit(12)) - devs.Add(PortDevice.Printer); + if (!port.Bit(12)) + devs.Add(PortDevice.Printer); - if (!port.Bit(11)) - devs.Add(PortDevice.PPI); + if (!port.Bit(11)) + devs.Add(PortDevice.PPI); - if (!port.Bit(10)) - devs.Add(PortDevice.Expansion); + if (!port.Bit(10)) + devs.Add(PortDevice.Expansion); - return devs; - } + return devs; + } - /// - /// Potential port devices - /// - public enum PortDevice - { - Unknown, - GateArray, - RAMManagement, - CRCT, - ROMSelect, - Printer, - PPI, - Expansion - } - } + /// + /// Potential port devices + /// + public enum PortDevice + { + Unknown, + GateArray, + RAMManagement, + CRCT, + ROMSelect, + Printer, + PPI, + Expansion + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPCBase.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPCBase.cs index 60dab69ecc..89e7d94216 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPCBase.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/CPCBase.cs @@ -3,213 +3,213 @@ using BizHawk.Emulation.Cores.Components.Z80A; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// The abstract class that all emulated models will inherit from - /// * Main properties / fields / contruction* - /// - public abstract partial class CPCBase - { - #region Devices + /// + /// The abstract class that all emulated models will inherit from + /// * Main properties / fields / contruction* + /// + public abstract partial class CPCBase + { + #region Devices - /// - /// The calling ZXSpectrum class (piped in via constructor) - /// - public AmstradCPC CPC { get; set; } + /// + /// The calling ZXSpectrum class (piped in via constructor) + /// + public AmstradCPC CPC { get; set; } - /// - /// Reference to the instantiated Z80 cpu (piped in via constructor) - /// - public Z80A CPU { get; set; } + /// + /// Reference to the instantiated Z80 cpu (piped in via constructor) + /// + public Z80A CPU { get; set; } - /// - /// ROM and extended info - /// - public RomData RomData { get; set; } + /// + /// ROM and extended info + /// + public RomData RomData { get; set; } - /// - /// The Amstrad datacorder device - /// - public virtual DatacorderDevice TapeDevice { get; set; } + /// + /// The Amstrad datacorder device + /// + public virtual DatacorderDevice TapeDevice { get; set; } - /// - /// beeper output for the tape - /// - public IBeeperDevice TapeBuzzer { get; set; } + /// + /// beeper output for the tape + /// + public IBeeperDevice TapeBuzzer { get; set; } - /// - /// Device representing the AY-3-8912 chip found in the CPC - /// - public IPSG AYDevice { get; set; } + /// + /// Device representing the AY-3-8912 chip found in the CPC + /// + public IPSG AYDevice { get; set; } - /// - /// The keyboard device - /// Technically, this is controlled by the PSG, but has been abstracted due to the port over from ZXHawk - /// - public IKeyboard KeyboardDevice { get; set; } + /// + /// The keyboard device + /// Technically, this is controlled by the PSG, but has been abstracted due to the port over from ZXHawk + /// + public IKeyboard KeyboardDevice { get; set; } - /// - /// The Amstrad disk drive - /// - public virtual NECUPD765 UPDDiskDevice { get; set; } + /// + /// The Amstrad disk drive + /// + public virtual NECUPD765 UPDDiskDevice { get; set; } - /// - /// The Cathode Ray Tube Controller chip - /// - public CRCT_6845 CRCT { get; set; } + /// + /// The Cathode Ray Tube Controller chip + /// + public CRCT_6845 CRCT { get; set; } - /// - /// The Amstrad gate array - /// - public AmstradGateArray GateArray { get; set; } + /// + /// The Amstrad gate array + /// + public AmstradGateArray GateArray { get; set; } -// /// -// /// Renders pixels to the screen -// /// -// public CRTDevice CRT { get; set; } + // /// + // /// Renders pixels to the screen + // /// + // public CRTDevice CRT { get; set; } - /// - /// The PPI contoller chip - /// - public PPI_8255 PPI { get; set; } + /// + /// The PPI contoller chip + /// + public PPI_8255 PPI { get; set; } - /// - /// The length of a standard frame in CPU cycles - /// - public int FrameLength; + /// + /// The length of a standard frame in CPU cycles + /// + public int FrameLength; - #endregion + #endregion - #region Emulator State + #region Emulator State - /// - /// Signs whether the frame has ended - /// - public bool FrameCompleted; + /// + /// Signs whether the frame has ended + /// + public bool FrameCompleted; - /// - /// Overflow from the previous frame (in Z80 cycles) - /// - public int OverFlow; + /// + /// Overflow from the previous frame (in Z80 cycles) + /// + public int OverFlow; - /// - /// The total number of frames rendered - /// - public int FrameCount; + /// + /// The total number of frames rendered + /// + public int FrameCount; - /// - /// The current cycle (T-State) that we are at in the frame - /// - public long _frameCycles; + /// + /// The current cycle (T-State) that we are at in the frame + /// + public long _frameCycles; - /// - /// Stores where we are in the frame after each CPU cycle - /// - public long LastFrameStartCPUTick; + /// + /// Stores where we are in the frame after each CPU cycle + /// + public long LastFrameStartCPUTick; - /// - /// Gets the current frame cycle according to the CPU tick count - /// - public virtual long CurrentFrameCycle => GateArray.FrameClock; // CPU.TotalExecutedCycles - LastFrameStartCPUTick; + /// + /// Gets the current frame cycle according to the CPU tick count + /// + public virtual long CurrentFrameCycle => GateArray.FrameClock; // CPU.TotalExecutedCycles - LastFrameStartCPUTick; - /// - /// Non-Deterministic bools - /// - public bool _render; - public bool _renderSound; + /// + /// Non-Deterministic bools + /// + public bool _render; + public bool _renderSound; - #endregion + #endregion - #region Constants + #region Constants - /// - /// Mask constants & misc - /// - protected const int BORDER_BIT = 0x07; - protected const int EAR_BIT = 0x10; - protected const int MIC_BIT = 0x08; - protected const int TAPE_BIT = 0x40; - protected const int AY_SAMPLE_RATE = 16; + /// + /// Mask constants & misc + /// + protected const int BORDER_BIT = 0x07; + protected const int EAR_BIT = 0x10; + protected const int MIC_BIT = 0x08; + protected const int TAPE_BIT = 0x40; + protected const int AY_SAMPLE_RATE = 16; - #endregion + #endregion - #region Emulation Loop + #region Emulation Loop - /// - /// Executes a single frame - /// - public virtual void ExecuteFrame(bool render, bool renderSound) - { - GateArray.FrameEnd = false; - CRCT.lineCounter = 0; + /// + /// Executes a single frame + /// + public virtual void ExecuteFrame(bool render, bool renderSound) + { + GateArray.FrameEnd = false; + CRCT.lineCounter = 0; - InputRead = false; - _render = render; - _renderSound = renderSound; + InputRead = false; + _render = render; + _renderSound = renderSound; - FrameCompleted = false; + FrameCompleted = false; - if (UPDDiskDevice == null || !UPDDiskDevice.FDD_IsDiskLoaded) - TapeDevice.StartFrame(); + if (UPDDiskDevice == null || !UPDDiskDevice.FDD_IsDiskLoaded) + TapeDevice.StartFrame(); - if (_renderSound) - { - AYDevice.StartFrame(); - } + if (_renderSound) + { + AYDevice.StartFrame(); + } - PollInput(); + PollInput(); - //CRT.SetupVideo(); - //CRT.ScanlineCounter = 0; + //CRT.SetupVideo(); + //CRT.ScanlineCounter = 0; - while (!GateArray.FrameEnd) - { - GateArray.ClockCycle(); + while (!GateArray.FrameEnd) + { + GateArray.ClockCycle(); - // cycle the tape device - if (UPDDiskDevice == null || !UPDDiskDevice.FDD_IsDiskLoaded) - TapeDevice.TapeCycle(); - } - // we have reached the end of a frame - LastFrameStartCPUTick = CPU.TotalExecutedCycles; // - OverFlow; + // cycle the tape device + if (UPDDiskDevice == null || !UPDDiskDevice.FDD_IsDiskLoaded) + TapeDevice.TapeCycle(); + } + // we have reached the end of a frame + LastFrameStartCPUTick = CPU.TotalExecutedCycles; // - OverFlow; - if (AYDevice != null) - AYDevice.EndFrame(); + if (AYDevice != null) + AYDevice.EndFrame(); - FrameCount++; + FrameCount++; - if (UPDDiskDevice == null || !UPDDiskDevice.FDD_IsDiskLoaded) - TapeDevice.EndFrame(); + if (UPDDiskDevice == null || !UPDDiskDevice.FDD_IsDiskLoaded) + TapeDevice.EndFrame(); - FrameCompleted = true; + FrameCompleted = true; - // is this a lag frame? - CPC.IsLagFrame = !InputRead; + // is this a lag frame? + CPC.IsLagFrame = !InputRead; - // FDC debug - if (UPDDiskDevice != null && UPDDiskDevice.writeDebug) - { - // only write UPD log every second - if (FrameCount % 10 == 0) - { - System.IO.File.AppendAllLines(UPDDiskDevice.outputfile, UPDDiskDevice.dLog); - UPDDiskDevice.dLog = new System.Collections.Generic.List(); - //System.IO.File.WriteAllText(UPDDiskDevice.outputfile, UPDDiskDevice.outputString); - } - } + // FDC debug + if (UPDDiskDevice != null && UPDDiskDevice.writeDebug) + { + // only write UPD log every second + if (FrameCount % 10 == 0) + { + System.IO.File.AppendAllLines(UPDDiskDevice.outputfile, UPDDiskDevice.dLog); + UPDDiskDevice.dLog = new System.Collections.Generic.List(); + //System.IO.File.WriteAllText(UPDDiskDevice.outputfile, UPDDiskDevice.outputString); + } + } - GateArray.FrameClock = 0; - } + GateArray.FrameClock = 0; + } - #endregion + #endregion - #region Reset Functions + #region Reset Functions - /// - /// Hard reset of the emulated machine - /// - public virtual void HardReset() - { - /* + /// + /// Hard reset of the emulated machine + /// + public virtual void HardReset() + { + /* //ULADevice.ResetInterrupt(); ROMPaged = 0; SpecialPagingMode = false; @@ -256,14 +256,14 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC } } */ - } + } - /// - /// Soft reset of the emulated machine - /// - public virtual void SoftReset() - { - /* + /// + /// Soft reset of the emulated machine + /// + public virtual void SoftReset() + { + /* //ULADevice.ResetInterrupt(); ROMPaged = 0; SpecialPagingMode = false; @@ -310,65 +310,65 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC } } */ - } + } - #endregion + #endregion - #region IStatable + #region IStatable - public void SyncState(Serializer ser) - { - ser.BeginSection("CPCMachine"); - ser.Sync(nameof(FrameCompleted), ref FrameCompleted); - ser.Sync(nameof(OverFlow), ref OverFlow); - ser.Sync(nameof(FrameCount), ref FrameCount); - ser.Sync(nameof(_frameCycles), ref _frameCycles); - ser.Sync(nameof(inputRead), ref inputRead); - ser.Sync(nameof(LastFrameStartCPUTick), ref LastFrameStartCPUTick); - ser.Sync(nameof(ROMLower), ref ROMLower, false); - ser.Sync(nameof(ROM0), ref ROM0, false); - ser.Sync(nameof(ROM7), ref ROM7, false); - ser.Sync(nameof(RAM0), ref RAM0, false); - ser.Sync(nameof(RAM1), ref RAM1, false); - ser.Sync(nameof(RAM2), ref RAM2, false); - ser.Sync(nameof(RAM3), ref RAM3, false); - ser.Sync(nameof(RAM4), ref RAM4, false); - ser.Sync(nameof(RAM5), ref RAM5, false); - ser.Sync(nameof(RAM6), ref RAM6, false); - ser.Sync(nameof(RAM7), ref RAM7, false); + public void SyncState(Serializer ser) + { + ser.BeginSection("CPCMachine"); + ser.Sync(nameof(FrameCompleted), ref FrameCompleted); + ser.Sync(nameof(OverFlow), ref OverFlow); + ser.Sync(nameof(FrameCount), ref FrameCount); + ser.Sync(nameof(_frameCycles), ref _frameCycles); + ser.Sync(nameof(inputRead), ref inputRead); + ser.Sync(nameof(LastFrameStartCPUTick), ref LastFrameStartCPUTick); + ser.Sync(nameof(ROMLower), ref ROMLower, false); + ser.Sync(nameof(ROM0), ref ROM0, false); + ser.Sync(nameof(ROM7), ref ROM7, false); + ser.Sync(nameof(RAM0), ref RAM0, false); + ser.Sync(nameof(RAM1), ref RAM1, false); + ser.Sync(nameof(RAM2), ref RAM2, false); + ser.Sync(nameof(RAM3), ref RAM3, false); + ser.Sync(nameof(RAM4), ref RAM4, false); + ser.Sync(nameof(RAM5), ref RAM5, false); + ser.Sync(nameof(RAM6), ref RAM6, false); + ser.Sync(nameof(RAM7), ref RAM7, false); - ser.Sync(nameof(UpperROMPosition), ref UpperROMPosition); - ser.Sync(nameof(UpperROMPaged), ref UpperROMPaged); - ser.Sync(nameof(LowerROMPaged), ref LowerROMPaged); - ser.Sync(nameof(RAMConfig), ref RAMConfig); - ser.Sync(nameof(RAM64KBank), ref RAM64KBank); + ser.Sync(nameof(UpperROMPosition), ref UpperROMPosition); + ser.Sync(nameof(UpperROMPaged), ref UpperROMPaged); + ser.Sync(nameof(LowerROMPaged), ref LowerROMPaged); + ser.Sync(nameof(RAMConfig), ref RAMConfig); + ser.Sync(nameof(RAM64KBank), ref RAM64KBank); - CRCT.SyncState(ser); - //CRT.SyncState(ser); - GateArray.SyncState(ser); - KeyboardDevice.SyncState(ser); - TapeBuzzer.SyncState(ser); - AYDevice.SyncState(ser); + CRCT.SyncState(ser); + //CRT.SyncState(ser); + GateArray.SyncState(ser); + KeyboardDevice.SyncState(ser); + TapeBuzzer.SyncState(ser); + AYDevice.SyncState(ser); - ser.Sync(nameof(tapeMediaIndex), ref tapeMediaIndex); - if (ser.IsReader) - TapeMediaIndex = tapeMediaIndex; + ser.Sync(nameof(tapeMediaIndex), ref tapeMediaIndex); + if (ser.IsReader) + TapeMediaIndex = tapeMediaIndex; - TapeDevice.SyncState(ser); + TapeDevice.SyncState(ser); - ser.Sync(nameof(diskMediaIndex), ref diskMediaIndex); - if (ser.IsReader) - DiskMediaIndex = diskMediaIndex; + ser.Sync(nameof(diskMediaIndex), ref diskMediaIndex); + if (ser.IsReader) + DiskMediaIndex = diskMediaIndex; - if (UPDDiskDevice != null) - { - UPDDiskDevice.SyncState(ser); - } + if (UPDDiskDevice != null) + { + UPDDiskDevice.SyncState(ser); + } - ser.EndSection(); - } + ser.EndSection(); + } - #endregion + #endregion - } + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/GateArrayBase.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/GateArrayBase.cs index 31fd0a3573..fcbc6ad3aa 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/GateArrayBase.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/GateArrayBase.cs @@ -10,60 +10,60 @@ using System.Threading.Tasks; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// The abstract class that all emulated models will inherit from - /// * Amstrad Gate Array * - /// https://web.archive.org/web/20170612081209/http://www.grimware.org/doku.php/documentations/devices/gatearray - /// - public abstract class GateArrayBase : IVideoProvider - { - public int Z80ClockSpeed = 4000000; - public int FrameLength = 79872; + /// + /// The abstract class that all emulated models will inherit from + /// * Amstrad Gate Array * + /// https://web.archive.org/web/20170612081209/http://www.grimware.org/doku.php/documentations/devices/gatearray + /// + public abstract class GateArrayBase : IVideoProvider + { + public int Z80ClockSpeed = 4000000; + public int FrameLength = 79872; - #region Devices + #region Devices - private CPCBase _machine; - private Z80A CPU => _machine.CPU; - private CRCT_6845 CRCT => _machine.CRCT; - private IPSG PSG => _machine.AYDevice; + private CPCBase _machine; + private Z80A CPU => _machine.CPU; + private CRCT_6845 CRCT => _machine.CRCT; + private IPSG PSG => _machine.AYDevice; - #endregion + #endregion - #region Constants + #region Constants - /// - /// CRTC Register constants - /// - public const int HOR_TOTAL = 0; - public const int HOR_DISPLAYED = 1; - public const int HOR_SYNC_POS = 2; - public const int HOR_AND_VER_SYNC_WIDTHS = 3; - public const int VER_TOTAL = 4; - public const int VER_TOTAL_ADJUST = 5; - public const int VER_DISPLAYED = 6; - public const int VER_SYNC_POS = 7; - public const int INTERLACE_SKEW = 8; - public const int MAX_RASTER_ADDR = 9; - public const int CUR_START_RASTER = 10; - public const int CUR_END_RASTER = 11; - public const int DISP_START_ADDR_H = 12; - public const int DISP_START_ADDR_L = 13; - public const int CUR_ADDR_H = 14; - public const int CUR_ADDR_L = 15; - public const int LPEN_ADDR_H = 16; - public const int LPEN_ADDR_L = 17; + /// + /// CRTC Register constants + /// + public const int HOR_TOTAL = 0; + public const int HOR_DISPLAYED = 1; + public const int HOR_SYNC_POS = 2; + public const int HOR_AND_VER_SYNC_WIDTHS = 3; + public const int VER_TOTAL = 4; + public const int VER_TOTAL_ADJUST = 5; + public const int VER_DISPLAYED = 6; + public const int VER_SYNC_POS = 7; + public const int INTERLACE_SKEW = 8; + public const int MAX_RASTER_ADDR = 9; + public const int CUR_START_RASTER = 10; + public const int CUR_END_RASTER = 11; + public const int DISP_START_ADDR_H = 12; + public const int DISP_START_ADDR_L = 13; + public const int CUR_ADDR_H = 14; + public const int CUR_ADDR_L = 15; + public const int LPEN_ADDR_H = 16; + public const int LPEN_ADDR_L = 17; - #endregion + #endregion - #region Palletes + #region Palletes - /// - /// The standard CPC Pallete (ordered by firmware #) - /// http://www.cpcwiki.eu/index.php/CPC_Palette - /// - private static readonly int[] CPCFirmwarePalette = - { - Colors.ARGB(0x00, 0x00, 0x00), // Black + /// + /// The standard CPC Pallete (ordered by firmware #) + /// http://www.cpcwiki.eu/index.php/CPC_Palette + /// + private static readonly int[] CPCFirmwarePalette = + { + Colors.ARGB(0x00, 0x00, 0x00), // Black Colors.ARGB(0x00, 0x00, 0x80), // Blue Colors.ARGB(0x00, 0x00, 0xFF), // Bright Blue Colors.ARGB(0x80, 0x00, 0x00), // Red @@ -92,13 +92,13 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC Colors.ARGB(0xFF, 0xFF, 0xFF), // Bright White }; - /// - /// The standard CPC Pallete (ordered by hardware #) - /// http://www.cpcwiki.eu/index.php/CPC_Palette - /// - private static readonly int[] CPCHardwarePalette = - { - Colors.ARGB(0x80, 0x80, 0x80), // White + /// + /// The standard CPC Pallete (ordered by hardware #) + /// http://www.cpcwiki.eu/index.php/CPC_Palette + /// + private static readonly int[] CPCHardwarePalette = + { + Colors.ARGB(0x80, 0x80, 0x80), // White Colors.ARGB(0x80, 0x80, 0x80), // White (duplicate) Colors.ARGB(0x00, 0xFF, 0x80), // Sea Green Colors.ARGB(0xFF, 0xFF, 0x80), // Pastel Yellow @@ -132,374 +132,374 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC Colors.ARGB(0x80, 0x80, 0xFF), // Pastel Blue }; - #endregion + #endregion - #region Construction + #region Construction - public GateArrayBase(CPCBase machine) - { - _machine = machine; - PenColours = new int[17]; - SetupScreenSize(); - Reset(); - } + public GateArrayBase(CPCBase machine) + { + _machine = machine; + PenColours = new int[17]; + SetupScreenSize(); + Reset(); + } - /// - /// Inits the pen lookup table - /// - public void SetupScreenMapping() - { - for (int m = 0; m < 4; m++) - { - Lookup[m] = new int[256 * 8]; - int pos = 0; + /// + /// Inits the pen lookup table + /// + public void SetupScreenMapping() + { + for (int m = 0; m < 4; m++) + { + Lookup[m] = new int[256 * 8]; + int pos = 0; - for (int b = 0; b < 256; b++) - { - switch (m) - { - case 1: - int pc = (((b & 0x80) >> 7) | ((b & 0x80) >> 2)); - Lookup[m][pos++] = pc; - Lookup[m][pos++] = pc; - pc = (((b & 0x40) >> 6) | ((b & 0x04) >> 1)); - Lookup[m][pos++] = pc; - Lookup[m][pos++] = pc; - pc = (((b & 0x20) >> 5) | (b & 0x02)); - Lookup[m][pos++] = pc; - Lookup[m][pos++] = pc; - pc = (((b & 0x10) >> 4) | ((b & 0x01) << 1)); - break; + for (int b = 0; b < 256; b++) + { + switch (m) + { + case 1: + int pc = (((b & 0x80) >> 7) | ((b & 0x80) >> 2)); + Lookup[m][pos++] = pc; + Lookup[m][pos++] = pc; + pc = (((b & 0x40) >> 6) | ((b & 0x04) >> 1)); + Lookup[m][pos++] = pc; + Lookup[m][pos++] = pc; + pc = (((b & 0x20) >> 5) | (b & 0x02)); + Lookup[m][pos++] = pc; + Lookup[m][pos++] = pc; + pc = (((b & 0x10) >> 4) | ((b & 0x01) << 1)); + break; - case 2: - for (int i = 7; i >= 0; i--) - { - bool pixel_on = ((b & (1 << i)) != 0); - Lookup[m][pos++] = pixel_on ? 1 : 0; - } - break; + case 2: + for (int i = 7; i >= 0; i--) + { + bool pixel_on = ((b & (1 << i)) != 0); + Lookup[m][pos++] = pixel_on ? 1 : 0; + } + break; - case 3: - case 0: - int pc2 = (b & 0xAA); - pc2 = ( - ((pc2 & 0x80) >> 7) | - ((pc2 & 0x08) >> 2) | - ((pc2 & 0x20) >> 3) | - ((pc2 & 0x02) << 2)); - Lookup[m][pos++] = pc2; - Lookup[m][pos++] = pc2; - Lookup[m][pos++] = pc2; - Lookup[m][pos++] = pc2; - pc2 = (b & 0x55); - pc2 = ( - ((pc2 & 0x40) >> 6) | - ((pc2 & 0x04) >> 1) | - ((pc2 & 0x10) >> 2) | - ((pc2 & 0x01) << 3)); + case 3: + case 0: + int pc2 = (b & 0xAA); + pc2 = ( + ((pc2 & 0x80) >> 7) | + ((pc2 & 0x08) >> 2) | + ((pc2 & 0x20) >> 3) | + ((pc2 & 0x02) << 2)); + Lookup[m][pos++] = pc2; + Lookup[m][pos++] = pc2; + Lookup[m][pos++] = pc2; + Lookup[m][pos++] = pc2; + pc2 = (b & 0x55); + pc2 = ( + ((pc2 & 0x40) >> 6) | + ((pc2 & 0x04) >> 1) | + ((pc2 & 0x10) >> 2) | + ((pc2 & 0x01) << 3)); - Lookup[m][pos++] = pc2; - Lookup[m][pos++] = pc2; - Lookup[m][pos++] = pc2; - Lookup[m][pos++] = pc2; - break; - } - } - } - } + Lookup[m][pos++] = pc2; + Lookup[m][pos++] = pc2; + Lookup[m][pos++] = pc2; + Lookup[m][pos++] = pc2; + break; + } + } + } + } - #endregion + #endregion - #region State + #region State - private int[] PenColours; - private int CurrentPen; - private int ScreenMode; - private int INTScanlineCnt; - //private int VSYNCDelyCnt; + private int[] PenColours; + private int CurrentPen; + private int ScreenMode; + private int INTScanlineCnt; + //private int VSYNCDelyCnt; - private int[][] Lookup = new int[4][]; + private int[][] Lookup = new int[4][]; - //private bool DoModeUpdate; + //private bool DoModeUpdate; - //private int LatchedMode; - //private int buffPos; + //private int LatchedMode; + //private int buffPos; - public bool FrameEnd; + public bool FrameEnd; - public bool WaitLine; + public bool WaitLine; - #endregion + #endregion - #region Clock Operations + #region Clock Operations - /// - /// The gatearray runs on a 16Mhz clock - /// (for the purposes of emulation, we will use a 4Mhz clock) - /// From this it generates: - /// 1Mhz clock for the CRTC chip - /// 1Mhz clock for the AY-3-8912 PSG - /// 4Mhz clock for the Z80 CPU - /// - public void ClockCycle() - { - // 4-phase clock - for (int i = 1; i < 5; i++) - { - switch (i) - { - // Phase 1 - case 1: - CRCT.ClockCycle(); - CPU.ExecuteOne(); - break; - // Phase 2 - case 2: - CPU.ExecuteOne(); - break; - // Phase 3 - case 3: - // video fetch - break; - // Phase 4 - case 4: - // video fetch - break; - } - } - } + /// + /// The gatearray runs on a 16Mhz clock + /// (for the purposes of emulation, we will use a 4Mhz clock) + /// From this it generates: + /// 1Mhz clock for the CRTC chip + /// 1Mhz clock for the AY-3-8912 PSG + /// 4Mhz clock for the Z80 CPU + /// + public void ClockCycle() + { + // 4-phase clock + for (int i = 1; i < 5; i++) + { + switch (i) + { + // Phase 1 + case 1: + CRCT.ClockCycle(); + CPU.ExecuteOne(); + break; + // Phase 2 + case 2: + CPU.ExecuteOne(); + break; + // Phase 3 + case 3: + // video fetch + break; + // Phase 4 + case 4: + // video fetch + break; + } + } + } - #endregion + #endregion - #region Internal Methods + #region Internal Methods - /// - /// Selects the pen - /// - public virtual void SetPen(BitArray bi) - { - if (bi[4]) - { - // border select - CurrentPen = 16; - } - else - { - // pen select - byte[] b = new byte[1]; - bi.CopyTo(b, 0); - CurrentPen = b[0] & 0x0f; - } - } + /// + /// Selects the pen + /// + public virtual void SetPen(BitArray bi) + { + if (bi[4]) + { + // border select + CurrentPen = 16; + } + else + { + // pen select + byte[] b = new byte[1]; + bi.CopyTo(b, 0); + CurrentPen = b[0] & 0x0f; + } + } - /// - /// Selects colour for the currently selected pen - /// - public virtual void SetPenColour(BitArray bi) - { - byte[] b = new byte[1]; - bi.CopyTo(b, 0); - var colour = b[0] & 0x1f; - PenColours[CurrentPen] = colour; - } + /// + /// Selects colour for the currently selected pen + /// + public virtual void SetPenColour(BitArray bi) + { + byte[] b = new byte[1]; + bi.CopyTo(b, 0); + var colour = b[0] & 0x1f; + PenColours[CurrentPen] = colour; + } - /// - /// Returns the actual ARGB pen colour value - /// - public virtual int GetPenColour(int idx) - { - return CPCHardwarePalette[PenColours[idx]]; - } + /// + /// Returns the actual ARGB pen colour value + /// + public virtual int GetPenColour(int idx) + { + return CPCHardwarePalette[PenColours[idx]]; + } - /// - /// Screen mode and ROM config - /// - public virtual void SetReg2(BitArray bi) - { - byte[] b = new byte[1]; - bi.CopyTo(b, 0); + /// + /// Screen mode and ROM config + /// + public virtual void SetReg2(BitArray bi) + { + byte[] b = new byte[1]; + bi.CopyTo(b, 0); - // screen mode - var mode = b[0] & 0x03; - ScreenMode = mode; + // screen mode + var mode = b[0] & 0x03; + ScreenMode = mode; - // ROM + // ROM - // upper - if ((b[0] & 0x08) != 0) - { - _machine.UpperROMPaged = false; - } - else - { - _machine.UpperROMPaged = true; - } + // upper + if ((b[0] & 0x08) != 0) + { + _machine.UpperROMPaged = false; + } + else + { + _machine.UpperROMPaged = true; + } - // lower - if ((b[0] & 0x04) != 0) - { - _machine.LowerROMPaged = false; - } - else - { - _machine.LowerROMPaged = true; - } + // lower + if ((b[0] & 0x04) != 0) + { + _machine.LowerROMPaged = false; + } + else + { + _machine.LowerROMPaged = true; + } - // INT delay - if ((b[0] & 0x10) != 0) - { - INTScanlineCnt = 0; - } - } + // INT delay + if ((b[0] & 0x10) != 0) + { + INTScanlineCnt = 0; + } + } - /// - /// Only available on machines with a 64KB memory expansion - /// Default assume we dont have this - /// - public virtual void SetRAM(BitArray bi) - { - return; - } + /// + /// Only available on machines with a 64KB memory expansion + /// Default assume we dont have this + /// + public virtual void SetRAM(BitArray bi) + { + return; + } - public void InterruptACK() - { - INTScanlineCnt &= 0x01f; - } + public void InterruptACK() + { + INTScanlineCnt &= 0x01f; + } - - - #endregion - #region Reset - public void Reset() - { - CurrentPen = 0; - ScreenMode = 1; - for (int i = 0; i < 17; i++) - PenColours[i] = 0; - INTScanlineCnt = 0; - //VSYNCDelyCnt = 0; - } + #endregion - #endregion + #region Reset - #region IPortIODevice + public void Reset() + { + CurrentPen = 0; + ScreenMode = 1; + for (int i = 0; i < 17; i++) + PenColours[i] = 0; + INTScanlineCnt = 0; + //VSYNCDelyCnt = 0; + } - /// - /// Device responds to an IN instruction - /// - public bool ReadPort(ushort port, ref int result) - { - // gate array is OUT only - return false; - } + #endregion - /// - /// Device responds to an OUT instruction - /// - public bool WritePort(ushort port, int result) - { - BitArray portBits = new BitArray(BitConverter.GetBytes(port)); - BitArray dataBits = new BitArray(BitConverter.GetBytes((byte)result)); + #region IPortIODevice - // The gate array responds to port 0x7F - bool accessed = !portBits[15]; - if (!accessed) - return false; + /// + /// Device responds to an IN instruction + /// + public bool ReadPort(ushort port, ref int result) + { + // gate array is OUT only + return false; + } - // Bit 9 and 8 of the data byte define the function to access - if (!dataBits[6] && !dataBits[7]) - { - // select pen - SetPen(dataBits); - } + /// + /// Device responds to an OUT instruction + /// + public bool WritePort(ushort port, int result) + { + BitArray portBits = new BitArray(BitConverter.GetBytes(port)); + BitArray dataBits = new BitArray(BitConverter.GetBytes((byte)result)); - if (dataBits[6] && !dataBits[7]) - { - // select colour for selected pen - SetPenColour(dataBits); - } + // The gate array responds to port 0x7F + bool accessed = !portBits[15]; + if (!accessed) + return false; - if (!dataBits[6] && dataBits[7]) - { - // select screen mode, ROM configuration and interrupt control - SetReg2(dataBits); - } + // Bit 9 and 8 of the data byte define the function to access + if (!dataBits[6] && !dataBits[7]) + { + // select pen + SetPen(dataBits); + } - if (dataBits[6] && dataBits[7]) - { - // RAM memory management - SetRAM(dataBits); - } + if (dataBits[6] && !dataBits[7]) + { + // select colour for selected pen + SetPenColour(dataBits); + } - return true; - } + if (!dataBits[6] && dataBits[7]) + { + // select screen mode, ROM configuration and interrupt control + SetReg2(dataBits); + } - #endregion + if (dataBits[6] && dataBits[7]) + { + // RAM memory management + SetRAM(dataBits); + } - #region IVideoProvider + return true; + } - /// - /// Video output buffer - /// - public int[] ScreenBuffer; + #endregion - private int _virtualWidth; - private int _virtualHeight; - private int _bufferWidth; - private int _bufferHeight; + #region IVideoProvider - public int BackgroundColor - { - get { return CPCHardwarePalette[16]; } - } + /// + /// Video output buffer + /// + public int[] ScreenBuffer; - public int VirtualWidth - { - get { return _virtualWidth; } - set { _virtualWidth = value; } - } + private int _virtualWidth; + private int _virtualHeight; + private int _bufferWidth; + private int _bufferHeight; - public int VirtualHeight - { - get { return _virtualHeight; } - set { _virtualHeight = value; } - } + public int BackgroundColor + { + get { return CPCHardwarePalette[16]; } + } - public int BufferWidth - { - get { return _bufferWidth; } - set { _bufferWidth = value; } - } + public int VirtualWidth + { + get { return _virtualWidth; } + set { _virtualWidth = value; } + } - public int BufferHeight - { - get { return _bufferHeight; } - set { _bufferHeight = value; } - } + public int VirtualHeight + { + get { return _virtualHeight; } + set { _virtualHeight = value; } + } - public int VsyncNumerator - { - get { return Z80ClockSpeed * 50; } - set { } - } + public int BufferWidth + { + get { return _bufferWidth; } + set { _bufferWidth = value; } + } - public int VsyncDenominator - { - get { return Z80ClockSpeed; } - } + public int BufferHeight + { + get { return _bufferHeight; } + set { _bufferHeight = value; } + } - public int[] GetVideoBuffer() - { - return ScreenBuffer; - } + public int VsyncNumerator + { + get { return Z80ClockSpeed * 50; } + set { } + } - protected void SetupScreenSize() - { - /* + public int VsyncDenominator + { + get { return Z80ClockSpeed; } + } + + public int[] GetVideoBuffer() + { + return ScreenBuffer; + } + + protected void SetupScreenSize() + { + /* * Rect Pixels: Mode 0: 160×200 pixels with 16 colors (4 bpp) Sqaure Pixels: Mode 1: 320×200 pixels with 4 colors (2 bpp) Rect Pixels: Mode 2: 640×200 pixels with 2 colors (1 bpp) @@ -507,25 +507,25 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC * * */ - // define maximum screen buffer size - // to fit all possible resolutions, 640x400 should do it - // therefore a scanline will take two buffer rows - // and buffer columns will be: - // Mode 1: 2 pixels - // Mode 2: 1 pixel - // Mode 0: 4 pixels - // Mode 3: 4 pixels + // define maximum screen buffer size + // to fit all possible resolutions, 640x400 should do it + // therefore a scanline will take two buffer rows + // and buffer columns will be: + // Mode 1: 2 pixels + // Mode 2: 1 pixel + // Mode 0: 4 pixels + // Mode 3: 4 pixels - BufferWidth = 640; - BufferHeight = 400; - VirtualHeight = BufferHeight; - VirtualWidth = BufferWidth; - ScreenBuffer = new int[BufferWidth * BufferHeight]; - croppedBuffer = ScreenBuffer; - } + BufferWidth = 640; + BufferHeight = 400; + VirtualHeight = BufferHeight; + VirtualWidth = BufferWidth; + ScreenBuffer = new int[BufferWidth * BufferHeight]; + croppedBuffer = ScreenBuffer; + } - protected int[] croppedBuffer; + protected int[] croppedBuffer; - #endregion - } + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/MachineType.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/MachineType.cs index f7f88a7a8b..0933141af9 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/MachineType.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Machine/MachineType.cs @@ -1,18 +1,18 @@  namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// The various CPC models CPCHawk emulates - /// - public enum MachineType - { - /// - /// Original Amstrad CPC model with builtin datacorder - /// - CPC464, - /// - /// 128K model with builtin 3" disk drive - /// - CPC6128, - } + /// + /// The various CPC models CPCHawk emulates + /// + public enum MachineType + { + /// + /// Original Amstrad CPC model with builtin datacorder + /// + CPC464, + /// + /// 128K model with builtin 3" disk drive + /// + CPC6128, + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/CPCExtendedFloppyDisk.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/CPCExtendedFloppyDisk.cs index c83bf7d840..a386a01b5e 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/CPCExtendedFloppyDisk.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/CPCExtendedFloppyDisk.cs @@ -5,249 +5,249 @@ using System.Collections.Generic; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Logical object representing a standard +3 disk image - /// - public class CPCExtendedFloppyDisk : FloppyDisk - { - /// - /// The format type - /// - public override DiskType DiskFormatType => DiskType.CPCExtended; + /// + /// Logical object representing a standard +3 disk image + /// + public class CPCExtendedFloppyDisk : FloppyDisk + { + /// + /// The format type + /// + public override DiskType DiskFormatType => DiskType.CPCExtended; - /// - /// Attempts to parse incoming disk data - /// - /// - /// TRUE: disk parsed - /// FALSE: unable to parse disk - /// - public override bool ParseDisk(byte[] data) - { - // look for standard magic string - string ident = Encoding.ASCII.GetString(data, 0, 16); + /// + /// Attempts to parse incoming disk data + /// + /// + /// TRUE: disk parsed + /// FALSE: unable to parse disk + /// + public override bool ParseDisk(byte[] data) + { + // look for standard magic string + string ident = Encoding.ASCII.GetString(data, 0, 16); - if (!ident.ToUpper().Contains("EXTENDED CPC DSK")) - { - // incorrect format - return false; - } + if (!ident.ToUpper().Contains("EXTENDED CPC DSK")) + { + // incorrect format + return false; + } - // read the disk information block - DiskHeader.DiskIdent = ident; - DiskHeader.DiskCreatorString = Encoding.ASCII.GetString(data, 0x22, 14); - DiskHeader.NumberOfTracks = data[0x30]; - DiskHeader.NumberOfSides = data[0x31]; - DiskHeader.TrackSizes = new int[DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides]; - DiskTracks = new Track[DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides]; - DiskData = data; - int pos = 0x34; + // read the disk information block + DiskHeader.DiskIdent = ident; + DiskHeader.DiskCreatorString = Encoding.ASCII.GetString(data, 0x22, 14); + DiskHeader.NumberOfTracks = data[0x30]; + DiskHeader.NumberOfSides = data[0x31]; + DiskHeader.TrackSizes = new int[DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides]; + DiskTracks = new Track[DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides]; + DiskData = data; + int pos = 0x34; - if (DiskHeader.NumberOfSides > 1) - { - StringBuilder sbm = new StringBuilder(); - sbm.AppendLine(); - sbm.AppendLine(); - sbm.AppendLine("The detected disk image contains multiple sides."); - sbm.AppendLine("This is NOT currently supported in CPCHawk."); - sbm.AppendLine("Please find an alternate image/dump where each side has been saved as a separate *.dsk image (and use the mutli-disk bundler tool to load into Bizhawk)."); - throw new System.NotImplementedException(sbm.ToString()); - } + if (DiskHeader.NumberOfSides > 1) + { + StringBuilder sbm = new StringBuilder(); + sbm.AppendLine(); + sbm.AppendLine(); + sbm.AppendLine("The detected disk image contains multiple sides."); + sbm.AppendLine("This is NOT currently supported in CPCHawk."); + sbm.AppendLine("Please find an alternate image/dump where each side has been saved as a separate *.dsk image (and use the mutli-disk bundler tool to load into Bizhawk)."); + throw new System.NotImplementedException(sbm.ToString()); + } - for (int i = 0; i < DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides; i++) - { - DiskHeader.TrackSizes[i] = data[pos++] * 256; - } + for (int i = 0; i < DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides; i++) + { + DiskHeader.TrackSizes[i] = data[pos++] * 256; + } - // move to first track information block - pos = 0x100; + // move to first track information block + pos = 0x100; - // parse each track - for (int i = 0; i < DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides; i++) - { - // check for unformatted track - if (DiskHeader.TrackSizes[i] == 0) - { - DiskTracks[i] = new Track(); - DiskTracks[i].Sectors = new Sector[0]; - continue; - } + // parse each track + for (int i = 0; i < DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides; i++) + { + // check for unformatted track + if (DiskHeader.TrackSizes[i] == 0) + { + DiskTracks[i] = new Track(); + DiskTracks[i].Sectors = new Sector[0]; + continue; + } - int p = pos; - DiskTracks[i] = new Track(); + int p = pos; + DiskTracks[i] = new Track(); - // track info block - DiskTracks[i].TrackIdent = Encoding.ASCII.GetString(data, p, 12); - p += 16; - DiskTracks[i].TrackNumber = data[p++]; - DiskTracks[i].SideNumber = data[p++]; - DiskTracks[i].DataRate = data[p++]; - DiskTracks[i].RecordingMode = data[p++]; - DiskTracks[i].SectorSize = data[p++]; - DiskTracks[i].NumberOfSectors = data[p++]; - DiskTracks[i].GAP3Length = data[p++]; - DiskTracks[i].FillerByte = data[p++]; + // track info block + DiskTracks[i].TrackIdent = Encoding.ASCII.GetString(data, p, 12); + p += 16; + DiskTracks[i].TrackNumber = data[p++]; + DiskTracks[i].SideNumber = data[p++]; + DiskTracks[i].DataRate = data[p++]; + DiskTracks[i].RecordingMode = data[p++]; + DiskTracks[i].SectorSize = data[p++]; + DiskTracks[i].NumberOfSectors = data[p++]; + DiskTracks[i].GAP3Length = data[p++]; + DiskTracks[i].FillerByte = data[p++]; - int dpos = pos + 0x100; + int dpos = pos + 0x100; - // sector info list - DiskTracks[i].Sectors = new Sector[DiskTracks[i].NumberOfSectors]; - for (int s = 0; s < DiskTracks[i].NumberOfSectors; s++) - { - DiskTracks[i].Sectors[s] = new Sector(); + // sector info list + DiskTracks[i].Sectors = new Sector[DiskTracks[i].NumberOfSectors]; + for (int s = 0; s < DiskTracks[i].NumberOfSectors; s++) + { + DiskTracks[i].Sectors[s] = new Sector(); - DiskTracks[i].Sectors[s].TrackNumber = data[p++]; - DiskTracks[i].Sectors[s].SideNumber = data[p++]; - DiskTracks[i].Sectors[s].SectorID = data[p++]; - DiskTracks[i].Sectors[s].SectorSize = data[p++]; - DiskTracks[i].Sectors[s].Status1 = data[p++]; - DiskTracks[i].Sectors[s].Status2 = data[p++]; - DiskTracks[i].Sectors[s].ActualDataByteLength = MediaConverter.GetWordValue(data, p); - p += 2; + DiskTracks[i].Sectors[s].TrackNumber = data[p++]; + DiskTracks[i].Sectors[s].SideNumber = data[p++]; + DiskTracks[i].Sectors[s].SectorID = data[p++]; + DiskTracks[i].Sectors[s].SectorSize = data[p++]; + DiskTracks[i].Sectors[s].Status1 = data[p++]; + DiskTracks[i].Sectors[s].Status2 = data[p++]; + DiskTracks[i].Sectors[s].ActualDataByteLength = MediaConverter.GetWordValue(data, p); + p += 2; - // sector data - begins at 0x100 offset from the start of the track info block (in this case dpos) - DiskTracks[i].Sectors[s].SectorData = new byte[DiskTracks[i].Sectors[s].ActualDataByteLength]; + // sector data - begins at 0x100 offset from the start of the track info block (in this case dpos) + DiskTracks[i].Sectors[s].SectorData = new byte[DiskTracks[i].Sectors[s].ActualDataByteLength]; - // copy the data - for (int b = 0; b < DiskTracks[i].Sectors[s].ActualDataByteLength; b++) - { - DiskTracks[i].Sectors[s].SectorData[b] = data[dpos + b]; - } + // copy the data + for (int b = 0; b < DiskTracks[i].Sectors[s].ActualDataByteLength; b++) + { + DiskTracks[i].Sectors[s].SectorData[b] = data[dpos + b]; + } - // check for multiple weak/random sectors stored - if (DiskTracks[i].Sectors[s].SectorSize <= 7) - { - // sectorsize n=8 is equivilent to n=0 - FDC will use DTL for length - int specifiedSize = 0x80 << DiskTracks[i].Sectors[s].SectorSize; + // check for multiple weak/random sectors stored + if (DiskTracks[i].Sectors[s].SectorSize <= 7) + { + // sectorsize n=8 is equivilent to n=0 - FDC will use DTL for length + int specifiedSize = 0x80 << DiskTracks[i].Sectors[s].SectorSize; - if (specifiedSize < DiskTracks[i].Sectors[s].ActualDataByteLength) - { - // more data stored than sectorsize defines - // check for multiple weak/random copies - if (DiskTracks[i].Sectors[s].ActualDataByteLength % specifiedSize != 0) - { - DiskTracks[i].Sectors[s].ContainsMultipleWeakSectors = true; - } - } - } + if (specifiedSize < DiskTracks[i].Sectors[s].ActualDataByteLength) + { + // more data stored than sectorsize defines + // check for multiple weak/random copies + if (DiskTracks[i].Sectors[s].ActualDataByteLength % specifiedSize != 0) + { + DiskTracks[i].Sectors[s].ContainsMultipleWeakSectors = true; + } + } + } - // move dpos to the next sector data postion - dpos += DiskTracks[i].Sectors[s].ActualDataByteLength; - } + // move dpos to the next sector data postion + dpos += DiskTracks[i].Sectors[s].ActualDataByteLength; + } - // move to the next track info block - pos += DiskHeader.TrackSizes[i]; - } + // move to the next track info block + pos += DiskHeader.TrackSizes[i]; + } - // run protection scheme detector - ParseProtection(); + // run protection scheme detector + ParseProtection(); - return true; - } + return true; + } - /// - /// Takes a double-sided disk byte array and converts into 2 single-sided arrays - /// - public static bool SplitDoubleSided(byte[] data, List results) - { - // look for standard magic string - string ident = Encoding.ASCII.GetString(data, 0, 16); - if (!ident.ToUpper().Contains("EXTENDED CPC DSK")) - { - // incorrect format - return false; - } + /// + /// Takes a double-sided disk byte array and converts into 2 single-sided arrays + /// + public static bool SplitDoubleSided(byte[] data, List results) + { + // look for standard magic string + string ident = Encoding.ASCII.GetString(data, 0, 16); + if (!ident.ToUpper().Contains("EXTENDED CPC DSK")) + { + // incorrect format + return false; + } - byte[] S0 = new byte[data.Length]; - byte[] S1 = new byte[data.Length]; + byte[] S0 = new byte[data.Length]; + byte[] S1 = new byte[data.Length]; - // disk info block - Array.Copy(data, 0, S0, 0, 0x100); - Array.Copy(data, 0, S1, 0, 0x100); - // change side number - S0[0x31] = 1; - S1[0x31] = 1; + // disk info block + Array.Copy(data, 0, S0, 0, 0x100); + Array.Copy(data, 0, S1, 0, 0x100); + // change side number + S0[0x31] = 1; + S1[0x31] = 1; - // extended format has different track sizes - int[] trkSizes = new int[data[0x30] * data[0x31]]; + // extended format has different track sizes + int[] trkSizes = new int[data[0x30] * data[0x31]]; - int pos = 0x34; - for (int i = 0; i < data[0x30] * data[0x31]; i++) - { - trkSizes[i] = data[pos] * 256; - // clear destination trk sizes (will be added later) - S0[pos] = 0; - S1[pos] = 0; - pos++; - } + int pos = 0x34; + for (int i = 0; i < data[0x30] * data[0x31]; i++) + { + trkSizes[i] = data[pos] * 256; + // clear destination trk sizes (will be added later) + S0[pos] = 0; + S1[pos] = 0; + pos++; + } - // start at track info blocks - int mPos = 0x100; - int s0Pos = 0x100; - int s0tCount = 0; - int s1tCount = 0; - int s1Pos = 0x100; - int tCount = 0; + // start at track info blocks + int mPos = 0x100; + int s0Pos = 0x100; + int s0tCount = 0; + int s1tCount = 0; + int s1Pos = 0x100; + int tCount = 0; - while (tCount < data[0x30] * data[0x31]) - { - // which side is this? - var side = data[mPos + 0x11]; - if (side == 0) - { - // side 1 - Array.Copy(data, mPos, S0, s0Pos, trkSizes[tCount]); - s0Pos += trkSizes[tCount]; - // trk size table - S0[0x34 + s0tCount++] = (byte)(trkSizes[tCount] / 256); - } - else if (side == 1) - { - // side 2 - Array.Copy(data, mPos, S1, s1Pos, trkSizes[tCount]); - s1Pos += trkSizes[tCount]; - // trk size table - S1[0x34 + s1tCount++] = (byte)(trkSizes[tCount] / 256); - } - - mPos += trkSizes[tCount++]; - } + while (tCount < data[0x30] * data[0x31]) + { + // which side is this? + var side = data[mPos + 0x11]; + if (side == 0) + { + // side 1 + Array.Copy(data, mPos, S0, s0Pos, trkSizes[tCount]); + s0Pos += trkSizes[tCount]; + // trk size table + S0[0x34 + s0tCount++] = (byte)(trkSizes[tCount] / 256); + } + else if (side == 1) + { + // side 2 + Array.Copy(data, mPos, S1, s1Pos, trkSizes[tCount]); + s1Pos += trkSizes[tCount]; + // trk size table + S1[0x34 + s1tCount++] = (byte)(trkSizes[tCount] / 256); + } - byte[] s0final = new byte[s0Pos]; - byte[] s1final = new byte[s1Pos]; - Array.Copy(S0, 0, s0final, 0, s0Pos); - Array.Copy(S1, 0, s1final, 0, s1Pos); + mPos += trkSizes[tCount++]; + } - results.Add(s0final); - results.Add(s1final); + byte[] s0final = new byte[s0Pos]; + byte[] s1final = new byte[s1Pos]; + Array.Copy(S0, 0, s0final, 0, s0Pos); + Array.Copy(S1, 0, s1final, 0, s1Pos); - return true; - } + results.Add(s0final); + results.Add(s1final); - /// - /// State serlialization - /// - public override void SyncState(Serializer ser) - { - ser.BeginSection("Plus3FloppyDisk"); + return true; + } - ser.Sync(nameof(CylinderCount), ref CylinderCount); - ser.Sync(nameof(SideCount), ref SideCount); - ser.Sync(nameof(BytesPerTrack), ref BytesPerTrack); - ser.Sync(nameof(WriteProtected), ref WriteProtected); - ser.SyncEnum(nameof(Protection), ref Protection); + /// + /// State serlialization + /// + public override void SyncState(Serializer ser) + { + ser.BeginSection("Plus3FloppyDisk"); - ser.Sync(nameof(DirtyData), ref DirtyData); - if (DirtyData) - { + ser.Sync(nameof(CylinderCount), ref CylinderCount); + ser.Sync(nameof(SideCount), ref SideCount); + ser.Sync(nameof(BytesPerTrack), ref BytesPerTrack); + ser.Sync(nameof(WriteProtected), ref WriteProtected); + ser.SyncEnum(nameof(Protection), ref Protection); - } + ser.Sync(nameof(DirtyData), ref DirtyData); + if (DirtyData) + { - // sync deterministic track and sector counters - ser.Sync(nameof( _randomCounter), ref _randomCounter); - RandomCounter = _randomCounter; + } - ser.EndSection(); - } - } + // sync deterministic track and sector counters + ser.Sync(nameof(_randomCounter), ref _randomCounter); + RandomCounter = _randomCounter; + + ser.EndSection(); + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/CPCFloppyDisk.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/CPCFloppyDisk.cs index 54ac222dae..dc0455556e 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/CPCFloppyDisk.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/CPCFloppyDisk.cs @@ -5,232 +5,232 @@ using System; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Logical object representing a standard +3 disk image - /// - public class CPCFloppyDisk : FloppyDisk - { - /// - /// The format type - /// - public override DiskType DiskFormatType => DiskType.CPC; + /// + /// Logical object representing a standard +3 disk image + /// + public class CPCFloppyDisk : FloppyDisk + { + /// + /// The format type + /// + public override DiskType DiskFormatType => DiskType.CPC; - /// - /// Attempts to parse incoming disk data - /// - /// - /// TRUE: disk parsed - /// FALSE: unable to parse disk - /// - public override bool ParseDisk(byte[] data) - { - // look for standard magic string - string ident = Encoding.ASCII.GetString(data, 0, 16); + /// + /// Attempts to parse incoming disk data + /// + /// + /// TRUE: disk parsed + /// FALSE: unable to parse disk + /// + public override bool ParseDisk(byte[] data) + { + // look for standard magic string + string ident = Encoding.ASCII.GetString(data, 0, 16); - if (!ident.ToUpper().Contains("MV - CPC")) - { - // incorrect format - return false; - } + if (!ident.ToUpper().Contains("MV - CPC")) + { + // incorrect format + return false; + } - // read the disk information block - DiskHeader.DiskIdent = ident; - DiskHeader.DiskCreatorString = Encoding.ASCII.GetString(data, 0x22, 14); - DiskHeader.NumberOfTracks = data[0x30]; - DiskHeader.NumberOfSides = data[0x31]; - DiskHeader.TrackSizes = new int[DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides]; - DiskTracks = new Track[DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides]; - DiskData = data; - int pos = 0x32; + // read the disk information block + DiskHeader.DiskIdent = ident; + DiskHeader.DiskCreatorString = Encoding.ASCII.GetString(data, 0x22, 14); + DiskHeader.NumberOfTracks = data[0x30]; + DiskHeader.NumberOfSides = data[0x31]; + DiskHeader.TrackSizes = new int[DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides]; + DiskTracks = new Track[DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides]; + DiskData = data; + int pos = 0x32; - if (DiskHeader.NumberOfSides > 1) - { - StringBuilder sbm = new StringBuilder(); - sbm.AppendLine(); - sbm.AppendLine(); - sbm.AppendLine("The detected disk image contains multiple sides."); - sbm.AppendLine("This is NOT currently supported in CPCHawk."); - sbm.AppendLine("Please find an alternate image/dump where each side has been saved as a separate *.dsk image (and use the mutli-disk bundler tool to load into Bizhawk)."); - throw new System.NotImplementedException(sbm.ToString()); - } + if (DiskHeader.NumberOfSides > 1) + { + StringBuilder sbm = new StringBuilder(); + sbm.AppendLine(); + sbm.AppendLine(); + sbm.AppendLine("The detected disk image contains multiple sides."); + sbm.AppendLine("This is NOT currently supported in CPCHawk."); + sbm.AppendLine("Please find an alternate image/dump where each side has been saved as a separate *.dsk image (and use the mutli-disk bundler tool to load into Bizhawk)."); + throw new System.NotImplementedException(sbm.ToString()); + } - // standard CPC format all track sizes are the same in the image - for (int i = 0; i < DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides; i++) - { - DiskHeader.TrackSizes[i] = MediaConverter.GetWordValue(data, pos); - } + // standard CPC format all track sizes are the same in the image + for (int i = 0; i < DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides; i++) + { + DiskHeader.TrackSizes[i] = MediaConverter.GetWordValue(data, pos); + } - // move to first track information block - pos = 0x100; + // move to first track information block + pos = 0x100; - // parse each track - for (int i = 0; i < DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides; i++) - { - // check for unformatted track - if (DiskHeader.TrackSizes[i] == 0) - { - DiskTracks[i] = new Track(); - DiskTracks[i].Sectors = new Sector[0]; - continue; - } + // parse each track + for (int i = 0; i < DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides; i++) + { + // check for unformatted track + if (DiskHeader.TrackSizes[i] == 0) + { + DiskTracks[i] = new Track(); + DiskTracks[i].Sectors = new Sector[0]; + continue; + } - int p = pos; - DiskTracks[i] = new Track(); + int p = pos; + DiskTracks[i] = new Track(); - // track info block - DiskTracks[i].TrackIdent = Encoding.ASCII.GetString(data, p, 12); - p += 16; - DiskTracks[i].TrackNumber = data[p++]; - DiskTracks[i].SideNumber = data[p++]; - p += 2; - DiskTracks[i].SectorSize = data[p++]; - DiskTracks[i].NumberOfSectors = data[p++]; - DiskTracks[i].GAP3Length = data[p++]; - DiskTracks[i].FillerByte = data[p++]; + // track info block + DiskTracks[i].TrackIdent = Encoding.ASCII.GetString(data, p, 12); + p += 16; + DiskTracks[i].TrackNumber = data[p++]; + DiskTracks[i].SideNumber = data[p++]; + p += 2; + DiskTracks[i].SectorSize = data[p++]; + DiskTracks[i].NumberOfSectors = data[p++]; + DiskTracks[i].GAP3Length = data[p++]; + DiskTracks[i].FillerByte = data[p++]; - int dpos = pos + 0x100; + int dpos = pos + 0x100; - // sector info list - DiskTracks[i].Sectors = new Sector[DiskTracks[i].NumberOfSectors]; - for (int s = 0; s < DiskTracks[i].NumberOfSectors; s++) - { - DiskTracks[i].Sectors[s] = new Sector(); + // sector info list + DiskTracks[i].Sectors = new Sector[DiskTracks[i].NumberOfSectors]; + for (int s = 0; s < DiskTracks[i].NumberOfSectors; s++) + { + DiskTracks[i].Sectors[s] = new Sector(); - DiskTracks[i].Sectors[s].TrackNumber = data[p++]; - DiskTracks[i].Sectors[s].SideNumber = data[p++]; - DiskTracks[i].Sectors[s].SectorID = data[p++]; - DiskTracks[i].Sectors[s].SectorSize = data[p++]; - DiskTracks[i].Sectors[s].Status1 = data[p++]; - DiskTracks[i].Sectors[s].Status2 = data[p++]; - DiskTracks[i].Sectors[s].ActualDataByteLength = MediaConverter.GetWordValue(data, p); - p += 2; + DiskTracks[i].Sectors[s].TrackNumber = data[p++]; + DiskTracks[i].Sectors[s].SideNumber = data[p++]; + DiskTracks[i].Sectors[s].SectorID = data[p++]; + DiskTracks[i].Sectors[s].SectorSize = data[p++]; + DiskTracks[i].Sectors[s].Status1 = data[p++]; + DiskTracks[i].Sectors[s].Status2 = data[p++]; + DiskTracks[i].Sectors[s].ActualDataByteLength = MediaConverter.GetWordValue(data, p); + p += 2; - // actualdatabytelength value is calculated now - if (DiskTracks[i].Sectors[s].SectorSize == 0) - { - // no sectorsize specified - DTL will be used at runtime - DiskTracks[i].Sectors[s].ActualDataByteLength = DiskHeader.TrackSizes[i]; - } - else if (DiskTracks[i].Sectors[s].SectorSize > 6) - { - // invalid - wrap around to 0 - DiskTracks[i].Sectors[s].ActualDataByteLength = DiskHeader.TrackSizes[i]; - } - else if (DiskTracks[i].Sectors[s].SectorSize == 6) - { - // only 0x1800 bytes are stored - DiskTracks[i].Sectors[s].ActualDataByteLength = 0x1800; - } - else - { - // valid sector size for this format - DiskTracks[i].Sectors[s].ActualDataByteLength = 0x80 << DiskTracks[i].Sectors[s].SectorSize; - } + // actualdatabytelength value is calculated now + if (DiskTracks[i].Sectors[s].SectorSize == 0) + { + // no sectorsize specified - DTL will be used at runtime + DiskTracks[i].Sectors[s].ActualDataByteLength = DiskHeader.TrackSizes[i]; + } + else if (DiskTracks[i].Sectors[s].SectorSize > 6) + { + // invalid - wrap around to 0 + DiskTracks[i].Sectors[s].ActualDataByteLength = DiskHeader.TrackSizes[i]; + } + else if (DiskTracks[i].Sectors[s].SectorSize == 6) + { + // only 0x1800 bytes are stored + DiskTracks[i].Sectors[s].ActualDataByteLength = 0x1800; + } + else + { + // valid sector size for this format + DiskTracks[i].Sectors[s].ActualDataByteLength = 0x80 << DiskTracks[i].Sectors[s].SectorSize; + } - // sector data - begins at 0x100 offset from the start of the track info block (in this case dpos) - DiskTracks[i].Sectors[s].SectorData = new byte[DiskTracks[i].Sectors[s].ActualDataByteLength]; + // sector data - begins at 0x100 offset from the start of the track info block (in this case dpos) + DiskTracks[i].Sectors[s].SectorData = new byte[DiskTracks[i].Sectors[s].ActualDataByteLength]; - // copy the data - for (int b = 0; b < DiskTracks[i].Sectors[s].ActualDataByteLength; b++) - { - DiskTracks[i].Sectors[s].SectorData[b] = data[dpos + b]; - } + // copy the data + for (int b = 0; b < DiskTracks[i].Sectors[s].ActualDataByteLength; b++) + { + DiskTracks[i].Sectors[s].SectorData[b] = data[dpos + b]; + } - // move dpos to the next sector data postion - dpos += DiskTracks[i].Sectors[s].ActualDataByteLength; - } + // move dpos to the next sector data postion + dpos += DiskTracks[i].Sectors[s].ActualDataByteLength; + } - // move to the next track info block - pos += DiskHeader.TrackSizes[i]; - } + // move to the next track info block + pos += DiskHeader.TrackSizes[i]; + } - // run protection scheme detector - ParseProtection(); + // run protection scheme detector + ParseProtection(); - return true; - } + return true; + } - /// - /// Takes a double-sided disk byte array and converts into 2 single-sided arrays - /// - public static bool SplitDoubleSided(byte[] data, List results) - { - // look for standard magic string - string ident = Encoding.ASCII.GetString(data, 0, 16); - if (!ident.ToUpper().Contains("MV - CPC")) - { - // incorrect format - return false; - } + /// + /// Takes a double-sided disk byte array and converts into 2 single-sided arrays + /// + public static bool SplitDoubleSided(byte[] data, List results) + { + // look for standard magic string + string ident = Encoding.ASCII.GetString(data, 0, 16); + if (!ident.ToUpper().Contains("MV - CPC")) + { + // incorrect format + return false; + } - byte[] S0 = new byte[data.Length]; - byte[] S1 = new byte[data.Length]; + byte[] S0 = new byte[data.Length]; + byte[] S1 = new byte[data.Length]; - // disk info block - Array.Copy(data, 0, S0, 0, 0x100); - Array.Copy(data, 0, S1, 0, 0x100); - // change side number - S0[0x31] = 1; - S1[0x31] = 1; + // disk info block + Array.Copy(data, 0, S0, 0, 0x100); + Array.Copy(data, 0, S1, 0, 0x100); + // change side number + S0[0x31] = 1; + S1[0x31] = 1; - int trkSize = MediaConverter.GetWordValue(data, 0x32); + int trkSize = MediaConverter.GetWordValue(data, 0x32); - // start at track info blocks - int mPos = 0x100; - int s0Pos = 0x100; - int s1Pos = 0x100; + // start at track info blocks + int mPos = 0x100; + int s0Pos = 0x100; + int s1Pos = 0x100; - while (mPos < trkSize * data[0x30] * data[0x31]) - { - // which side is this? - var side = data[mPos + 0x11]; - if (side == 0) - { - // side 1 - Array.Copy(data, mPos, S0, s0Pos, trkSize); - s0Pos += trkSize; - } - else if (side == 1) - { - // side 2 - Array.Copy(data, mPos, S1, s1Pos, trkSize); - s1Pos += trkSize; - } + while (mPos < trkSize * data[0x30] * data[0x31]) + { + // which side is this? + var side = data[mPos + 0x11]; + if (side == 0) + { + // side 1 + Array.Copy(data, mPos, S0, s0Pos, trkSize); + s0Pos += trkSize; + } + else if (side == 1) + { + // side 2 + Array.Copy(data, mPos, S1, s1Pos, trkSize); + s1Pos += trkSize; + } - mPos += trkSize; - } + mPos += trkSize; + } - byte[] s0final = new byte[s0Pos]; - byte[] s1final = new byte[s1Pos]; - Array.Copy(S0, 0, s0final, 0, s0Pos); - Array.Copy(S1, 0, s1final, 0, s1Pos); + byte[] s0final = new byte[s0Pos]; + byte[] s1final = new byte[s1Pos]; + Array.Copy(S0, 0, s0final, 0, s0Pos); + Array.Copy(S1, 0, s1final, 0, s1Pos); - results.Add(s0final); - results.Add(s1final); + results.Add(s0final); + results.Add(s1final); - return true; - } + return true; + } - /// - /// State serlialization - /// - public override void SyncState(Serializer ser) - { - ser.BeginSection("Plus3FloppyDisk"); + /// + /// State serlialization + /// + public override void SyncState(Serializer ser) + { + ser.BeginSection("Plus3FloppyDisk"); - ser.Sync(nameof(CylinderCount), ref CylinderCount); - ser.Sync(nameof(SideCount), ref SideCount); - ser.Sync(nameof(BytesPerTrack), ref BytesPerTrack); - ser.Sync(nameof(WriteProtected), ref WriteProtected); - ser.SyncEnum(nameof(Protection), ref Protection); + ser.Sync(nameof(CylinderCount), ref CylinderCount); + ser.Sync(nameof(SideCount), ref SideCount); + ser.Sync(nameof(BytesPerTrack), ref BytesPerTrack); + ser.Sync(nameof(WriteProtected), ref WriteProtected); + ser.SyncEnum(nameof(Protection), ref Protection); - ser.Sync(nameof(DirtyData), ref DirtyData); - if (DirtyData) - { + ser.Sync(nameof(DirtyData), ref DirtyData); + if (DirtyData) + { - } + } - ser.EndSection(); - } - } + ser.EndSection(); + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/DiskHandler.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/DiskHandler.cs index bc06c1094b..b7f7a26520 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/DiskHandler.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/DiskHandler.cs @@ -6,12 +6,12 @@ using System.Threading.Tasks; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// This is called first when importing disk images - /// Disk images can be single or double-sided, so we need to handle that - /// - public class DiskHandler - { + /// + /// This is called first when importing disk images + /// Disk images can be single or double-sided, so we need to handle that + /// + public class DiskHandler + { - } + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/DiskType.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/DiskType.cs index 75719b9df9..df211b4e55 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/DiskType.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/DiskType.cs @@ -1,19 +1,19 @@  namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// The different disk formats ZXHawk currently supports - /// - public enum DiskType - { - /// - /// Standard CPCEMU disk format (used in the built-in +3 disk drive) - /// - CPC, + /// + /// The different disk formats ZXHawk currently supports + /// + public enum DiskType + { + /// + /// Standard CPCEMU disk format (used in the built-in +3 disk drive) + /// + CPC, - /// - /// Extended CPCEMU disk format (used in the built-in +3 disk drive) - /// - CPCExtended - } + /// + /// Extended CPCEMU disk format (used in the built-in +3 disk drive) + /// + CPCExtended + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/FloppyDisk.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/FloppyDisk.cs index c8145e910b..7d5aaabe47 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/FloppyDisk.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/FloppyDisk.cs @@ -6,403 +6,403 @@ using System.Text; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// This abstract class defines a logical floppy disk - /// - public abstract class FloppyDisk - { - /// - /// The disk format type - /// - public abstract DiskType DiskFormatType { get; } + /// + /// This abstract class defines a logical floppy disk + /// + public abstract class FloppyDisk + { + /// + /// The disk format type + /// + public abstract DiskType DiskFormatType { get; } - /// - /// Disk information header - /// - public Header DiskHeader = new Header(); + /// + /// Disk information header + /// + public Header DiskHeader = new Header(); - /// - /// Track array - /// - public Track[] DiskTracks = null; + /// + /// Track array + /// + public Track[] DiskTracks = null; - /// - /// No. of tracks per side - /// - public int CylinderCount; - - /// - /// The number of physical sides - /// - public int SideCount; + /// + /// No. of tracks per side + /// + public int CylinderCount; - /// - /// The number of bytes per track - /// - public int BytesPerTrack; + /// + /// The number of physical sides + /// + public int SideCount; - /// - /// The write-protect tab on the disk - /// - public bool WriteProtected; + /// + /// The number of bytes per track + /// + public int BytesPerTrack; - /// - /// The detected protection scheme (if any) - /// - public ProtectionType Protection; + /// + /// The write-protect tab on the disk + /// + public bool WriteProtected; - /// - /// The actual disk image data - /// - public byte[] DiskData; + /// + /// The detected protection scheme (if any) + /// + public ProtectionType Protection; - /// - /// If TRUE then data on the disk has changed (been written to) - /// This will be used to determine whether the disk data needs to be included - /// in any SyncState operations - /// - protected bool DirtyData = false; + /// + /// The actual disk image data + /// + public byte[] DiskData; - /// - /// Used to deterministically choose a 'random' sector when dealing with weak reads - /// - public int RandomCounter - { - get { return _randomCounter; } - set - { - _randomCounter = value; + /// + /// If TRUE then data on the disk has changed (been written to) + /// This will be used to determine whether the disk data needs to be included + /// in any SyncState operations + /// + protected bool DirtyData = false; - foreach (var trk in DiskTracks) - { - foreach (var sec in trk.Sectors) - { - sec.RandSecCounter = _randomCounter; - } - } - } - } - protected int _randomCounter; + /// + /// Used to deterministically choose a 'random' sector when dealing with weak reads + /// + public int RandomCounter + { + get { return _randomCounter; } + set + { + _randomCounter = value; + + foreach (var trk in DiskTracks) + { + foreach (var sec in trk.Sectors) + { + sec.RandSecCounter = _randomCounter; + } + } + } + } + protected int _randomCounter; - /// - /// Attempts to parse incoming disk data - /// - /// - /// TRUE: disk parsed - /// FALSE: unable to parse disk - /// - public virtual bool ParseDisk(byte[] diskData) - { - // default result - // override in inheriting class - return false; - } + /// + /// Attempts to parse incoming disk data + /// + /// + /// TRUE: disk parsed + /// FALSE: unable to parse disk + /// + public virtual bool ParseDisk(byte[] diskData) + { + // default result + // override in inheriting class + return false; + } - /// - /// Examines the floppydisk data to work out what protection (if any) is present - /// If possible it will also fix the disk data for this protection - /// This should be run at the end of the ParseDisk() method - /// - public virtual void ParseProtection() - { - int[] weakArr = new int[2]; + /// + /// Examines the floppydisk data to work out what protection (if any) is present + /// If possible it will also fix the disk data for this protection + /// This should be run at the end of the ParseDisk() method + /// + public virtual void ParseProtection() + { + int[] weakArr = new int[2]; - // speedlock - if (DetectSpeedlock(ref weakArr)) - { - Protection = ProtectionType.Speedlock; + // speedlock + if (DetectSpeedlock(ref weakArr)) + { + Protection = ProtectionType.Speedlock; - Sector sec = DiskTracks[0].Sectors[1]; - if (!sec.ContainsMultipleWeakSectors) - { - byte[] origData = sec.SectorData.ToArray(); - List data = new List(); - for (int m = 0; m < 3; m++) - { - for (int i = 0; i < 512; i++) - { - // deterministic 'random' implementation - int n = origData[i] + m + 1; - if (n > 0xff) - n = n - 0xff; - else if (n < 0) - n = 0xff + n; + Sector sec = DiskTracks[0].Sectors[1]; + if (!sec.ContainsMultipleWeakSectors) + { + byte[] origData = sec.SectorData.ToArray(); + List data = new List(); + for (int m = 0; m < 3; m++) + { + for (int i = 0; i < 512; i++) + { + // deterministic 'random' implementation + int n = origData[i] + m + 1; + if (n > 0xff) + n = n - 0xff; + else if (n < 0) + n = 0xff + n; - byte nByte = (byte)n; + byte nByte = (byte)n; - if (m == 0) - { - data.Add(origData[i]); - continue; - } + if (m == 0) + { + data.Add(origData[i]); + continue; + } - if (i < weakArr[0]) - { - data.Add(origData[i]); - } - - else if (weakArr[1] > 0) - { - data.Add(nByte); - weakArr[1]--; - } - - else - { - data.Add(origData[i]); - } - } - } + if (i < weakArr[0]) + { + data.Add(origData[i]); + } - sec.SectorData = data.ToArray(); - sec.ActualDataByteLength = data.Count(); - sec.ContainsMultipleWeakSectors = true; - } - } - else if (DetectAlkatraz(ref weakArr)) - { - Protection = ProtectionType.Alkatraz; - } - else if (DetectPaulOwens(ref weakArr)) - { - Protection = ProtectionType.PaulOwens; - } - else if (DetectHexagon(ref weakArr)) - { - Protection = ProtectionType.Hexagon; - } - else if (DetectShadowOfTheBeast()) - { - Protection = ProtectionType.ShadowOfTheBeast; - } - } + else if (weakArr[1] > 0) + { + data.Add(nByte); + weakArr[1]--; + } - /// - /// Detection routine for shadow of the beast game - /// Still cannot get this to work, but at least the game is detected - /// - public bool DetectShadowOfTheBeast() - { - if (DiskTracks[0].Sectors.Length != 9) - return false; + else + { + data.Add(origData[i]); + } + } + } - var zeroSecs = DiskTracks[0].Sectors; - if (zeroSecs[0].SectorID != 65 || - zeroSecs[1].SectorID != 66 || - zeroSecs[2].SectorID != 67 || - zeroSecs[3].SectorID != 68 || - zeroSecs[4].SectorID != 69 || - zeroSecs[5].SectorID != 70 || - zeroSecs[6].SectorID != 71 || - zeroSecs[7].SectorID != 72 || - zeroSecs[8].SectorID != 73) - return false; + sec.SectorData = data.ToArray(); + sec.ActualDataByteLength = data.Count(); + sec.ContainsMultipleWeakSectors = true; + } + } + else if (DetectAlkatraz(ref weakArr)) + { + Protection = ProtectionType.Alkatraz; + } + else if (DetectPaulOwens(ref weakArr)) + { + Protection = ProtectionType.PaulOwens; + } + else if (DetectHexagon(ref weakArr)) + { + Protection = ProtectionType.Hexagon; + } + else if (DetectShadowOfTheBeast()) + { + Protection = ProtectionType.ShadowOfTheBeast; + } + } - var oneSecs = DiskTracks[1].Sectors; + /// + /// Detection routine for shadow of the beast game + /// Still cannot get this to work, but at least the game is detected + /// + public bool DetectShadowOfTheBeast() + { + if (DiskTracks[0].Sectors.Length != 9) + return false; - if (oneSecs.Length != 8) - return false; + var zeroSecs = DiskTracks[0].Sectors; + if (zeroSecs[0].SectorID != 65 || + zeroSecs[1].SectorID != 66 || + zeroSecs[2].SectorID != 67 || + zeroSecs[3].SectorID != 68 || + zeroSecs[4].SectorID != 69 || + zeroSecs[5].SectorID != 70 || + zeroSecs[6].SectorID != 71 || + zeroSecs[7].SectorID != 72 || + zeroSecs[8].SectorID != 73) + return false; - if (oneSecs[0].SectorID != 17 || - oneSecs[1].SectorID != 18 || - oneSecs[2].SectorID != 19 || - oneSecs[3].SectorID != 20 || - oneSecs[4].SectorID != 21 || - oneSecs[5].SectorID != 22 || - oneSecs[6].SectorID != 23 || - oneSecs[7].SectorID != 24) - return false; + var oneSecs = DiskTracks[1].Sectors; - return true; - } + if (oneSecs.Length != 8) + return false; - /// - /// Detect speedlock weak sector - /// - public bool DetectSpeedlock(ref int[] weak) - { - // SPEEDLOCK NOTES (-asni 2018-05-01) - // --------------------------------- - // Speedlock is one of the more common +3 disk protections and there are a few different versions - // Usually, track 0 sector 1 (ID 2) has data CRC errors that result in certain bytes returning a different value every time they are read - // Speedlock will generally read this track a number of times during the load process - // and if the correct bytes are not different between reads, the load fails + if (oneSecs[0].SectorID != 17 || + oneSecs[1].SectorID != 18 || + oneSecs[2].SectorID != 19 || + oneSecs[3].SectorID != 20 || + oneSecs[4].SectorID != 21 || + oneSecs[5].SectorID != 22 || + oneSecs[6].SectorID != 23 || + oneSecs[7].SectorID != 24) + return false; - // always must have track 0 containing 9 sectors - if (DiskTracks[0].Sectors.Length != 9) - return false; + return true; + } - // check for SPEEDLOCK ident in sector 0 - string ident = Encoding.ASCII.GetString(DiskTracks[0].Sectors[0].SectorData, 0, DiskTracks[0].Sectors[0].SectorData.Length); - if (!ident.ToUpper().Contains("SPEEDLOCK")) - return false; + /// + /// Detect speedlock weak sector + /// + public bool DetectSpeedlock(ref int[] weak) + { + // SPEEDLOCK NOTES (-asni 2018-05-01) + // --------------------------------- + // Speedlock is one of the more common +3 disk protections and there are a few different versions + // Usually, track 0 sector 1 (ID 2) has data CRC errors that result in certain bytes returning a different value every time they are read + // Speedlock will generally read this track a number of times during the load process + // and if the correct bytes are not different between reads, the load fails - // check for correct sector 0 lengths - if (DiskTracks[0].Sectors[0].SectorSize != 2 || - DiskTracks[0].Sectors[0].SectorData.Length < 0x200) - return false; + // always must have track 0 containing 9 sectors + if (DiskTracks[0].Sectors.Length != 9) + return false; - // sector[1] (SectorID 2) contains the weak sectors - Sector sec = DiskTracks[0].Sectors[1]; + // check for SPEEDLOCK ident in sector 0 + string ident = Encoding.ASCII.GetString(DiskTracks[0].Sectors[0].SectorData, 0, DiskTracks[0].Sectors[0].SectorData.Length); + if (!ident.ToUpper().Contains("SPEEDLOCK")) + return false; - // check for correct sector 1 lengths - if (sec.SectorSize != 2 || - sec.SectorData.Length < 0x200) - return false; + // check for correct sector 0 lengths + if (DiskTracks[0].Sectors[0].SectorSize != 2 || + DiskTracks[0].Sectors[0].SectorData.Length < 0x200) + return false; - // secID 2 needs a CRC error - //if (!(sec.Status1.Bit(5) || sec.Status2.Bit(5))) - //return false; + // sector[1] (SectorID 2) contains the weak sectors + Sector sec = DiskTracks[0].Sectors[1]; - // check for filler - bool startFillerFound = true; - for (int i = 0; i < 250; i++) - { - if (sec.SectorData[i] != sec.SectorData[i + 1]) - { - startFillerFound = false; - break; - } - } + // check for correct sector 1 lengths + if (sec.SectorSize != 2 || + sec.SectorData.Length < 0x200) + return false; - if (!startFillerFound) - { - weak[0] = 0; - weak[1] = 0x200; - } - else - { - weak[0] = 0x150; - weak[1] = 0x20; - } + // secID 2 needs a CRC error + //if (!(sec.Status1.Bit(5) || sec.Status2.Bit(5))) + //return false; - return true; - } + // check for filler + bool startFillerFound = true; + for (int i = 0; i < 250; i++) + { + if (sec.SectorData[i] != sec.SectorData[i + 1]) + { + startFillerFound = false; + break; + } + } - /// - /// Detect Alkatraz - /// - public bool DetectAlkatraz(ref int[] weak) - { - try - { - var data1 = DiskTracks[0].Sectors[0].SectorData; - var data2 = DiskTracks[0].Sectors[0].SectorData.Length; - } - catch (Exception) - { - return false; - } + if (!startFillerFound) + { + weak[0] = 0; + weak[1] = 0x200; + } + else + { + weak[0] = 0x150; + weak[1] = 0x20; + } - // check for ALKATRAZ ident in sector 0 - string ident = Encoding.ASCII.GetString(DiskTracks[0].Sectors[0].SectorData, 0, DiskTracks[0].Sectors[0].SectorData.Length); - if (!ident.ToUpper().Contains("ALKATRAZ PROTECTION SYSTEM")) - return false; + return true; + } - // ALKATRAZ NOTES (-asni 2018-05-01) - // --------------------------------- - // Alkatraz protection appears to revolve around a track on the disk with 18 sectors, - // (track position is not consistent) with the sector ID info being incorrect: - // TrackID is consistent between the sectors although is usually high (233, 237 etc) - // SideID is fairly random looking but with all IDs being even - // SectorID is also fairly random looking but contains both odd and even numbers - // - // There doesnt appear to be any CRC errors in this track, but the sector size is always 1 (256 bytes) - // Each sector contains different filler byte - // Once track 0 is loaded the CPU completely reads all the sectors in this track one-by-one. - // Data transferred during execution must be correct, also result ST0, ST1 and ST2 must be 64, 128 and 0 respectively + /// + /// Detect Alkatraz + /// + public bool DetectAlkatraz(ref int[] weak) + { + try + { + var data1 = DiskTracks[0].Sectors[0].SectorData; + var data2 = DiskTracks[0].Sectors[0].SectorData.Length; + } + catch (Exception) + { + return false; + } - // Immediately following this track are a number of tracks and sectors with a DAM set. - // These are all read in sector by sector - // Again, Alkatraz appears to require that ST0, ST1, and ST2 result bytes are set to 64, 128 and 0 respectively - // (so the CM in ST2 needs to be reset) + // check for ALKATRAZ ident in sector 0 + string ident = Encoding.ASCII.GetString(DiskTracks[0].Sectors[0].SectorData, 0, DiskTracks[0].Sectors[0].SectorData.Length); + if (!ident.ToUpper().Contains("ALKATRAZ PROTECTION SYSTEM")) + return false; - return true; - } + // ALKATRAZ NOTES (-asni 2018-05-01) + // --------------------------------- + // Alkatraz protection appears to revolve around a track on the disk with 18 sectors, + // (track position is not consistent) with the sector ID info being incorrect: + // TrackID is consistent between the sectors although is usually high (233, 237 etc) + // SideID is fairly random looking but with all IDs being even + // SectorID is also fairly random looking but contains both odd and even numbers + // + // There doesnt appear to be any CRC errors in this track, but the sector size is always 1 (256 bytes) + // Each sector contains different filler byte + // Once track 0 is loaded the CPU completely reads all the sectors in this track one-by-one. + // Data transferred during execution must be correct, also result ST0, ST1 and ST2 must be 64, 128 and 0 respectively - /// - /// Detect Paul Owens - /// - public bool DetectPaulOwens(ref int[] weak) - { - try - { - var data1 = DiskTracks[0].Sectors[2].SectorData; - var data2 = DiskTracks[0].Sectors[2].SectorData.Length; - } - catch (Exception) - { - return false; - } + // Immediately following this track are a number of tracks and sectors with a DAM set. + // These are all read in sector by sector + // Again, Alkatraz appears to require that ST0, ST1, and ST2 result bytes are set to 64, 128 and 0 respectively + // (so the CM in ST2 needs to be reset) - // check for PAUL OWENS ident in sector 2 - string ident = Encoding.ASCII.GetString(DiskTracks[0].Sectors[2].SectorData, 0, DiskTracks[0].Sectors[2].SectorData.Length); - if (!ident.ToUpper().Contains("PAUL OWENS")) - return false; + return true; + } - // Paul Owens Disk Protection Notes (-asni 2018-05-01) - // --------------------------------------------------- - // - // This scheme looks a little similar to Alkatraz with incorrect sector ID info in many places - // and deleted address marks (although these do not seem to show the strict relience on removing the CM mark from ST2 result that Alkatraz does) - // There are also data CRC errors but these dont look to be read more than once/checked for changes during load - // Main identifiers: - // - // * There are more than 10 cylinders - // * Cylinder 1 has no sector data - // * The sector ID infomation in most cases contains incorrect track IDs - // * Tracks 0 (boot) and 5 appear to be pretty much the only tracks that do not have incorrect sector ID marks + /// + /// Detect Paul Owens + /// + public bool DetectPaulOwens(ref int[] weak) + { + try + { + var data1 = DiskTracks[0].Sectors[2].SectorData; + var data2 = DiskTracks[0].Sectors[2].SectorData.Length; + } + catch (Exception) + { + return false; + } - return true; - } + // check for PAUL OWENS ident in sector 2 + string ident = Encoding.ASCII.GetString(DiskTracks[0].Sectors[2].SectorData, 0, DiskTracks[0].Sectors[2].SectorData.Length); + if (!ident.ToUpper().Contains("PAUL OWENS")) + return false; - /// - /// Detect Hexagon copy protection - /// - public bool DetectHexagon(ref int[] weak) - { - try - { - var data1 = DiskTracks[0].Sectors.Length; - var data2 = DiskTracks[0].Sectors[8].ActualDataByteLength; - var data3 = DiskTracks[0].Sectors[8].SectorData; - var data4 = DiskTracks[0].Sectors[8].SectorData.Length; - var data5 = DiskTracks[1].Sectors[0]; - } - catch (Exception) - { - return false; - } + // Paul Owens Disk Protection Notes (-asni 2018-05-01) + // --------------------------------------------------- + // + // This scheme looks a little similar to Alkatraz with incorrect sector ID info in many places + // and deleted address marks (although these do not seem to show the strict relience on removing the CM mark from ST2 result that Alkatraz does) + // There are also data CRC errors but these dont look to be read more than once/checked for changes during load + // Main identifiers: + // + // * There are more than 10 cylinders + // * Cylinder 1 has no sector data + // * The sector ID infomation in most cases contains incorrect track IDs + // * Tracks 0 (boot) and 5 appear to be pretty much the only tracks that do not have incorrect sector ID marks - if (DiskTracks[0].Sectors.Length != 10 || DiskTracks[0].Sectors[8].ActualDataByteLength != 512) - return false; + return true; + } - // check for Hexagon ident in sector 8 - string ident = Encoding.ASCII.GetString(DiskTracks[0].Sectors[8].SectorData, 0, DiskTracks[0].Sectors[8].SectorData.Length); - if (ident.ToUpper().Contains("GON DISK PROT")) - return true; + /// + /// Detect Hexagon copy protection + /// + public bool DetectHexagon(ref int[] weak) + { + try + { + var data1 = DiskTracks[0].Sectors.Length; + var data2 = DiskTracks[0].Sectors[8].ActualDataByteLength; + var data3 = DiskTracks[0].Sectors[8].SectorData; + var data4 = DiskTracks[0].Sectors[8].SectorData.Length; + var data5 = DiskTracks[1].Sectors[0]; + } + catch (Exception) + { + return false; + } - // hexagon protection may not be labelled as such - var track = DiskTracks[1]; - var sector = track.Sectors[0]; + if (DiskTracks[0].Sectors.Length != 10 || DiskTracks[0].Sectors[8].ActualDataByteLength != 512) + return false; - if (sector.SectorSize == 6 && sector.Status1 == 0x20 && sector.Status2 == 0x60) - { - if (track.Sectors.Length == 1) - return true; - } + // check for Hexagon ident in sector 8 + string ident = Encoding.ASCII.GetString(DiskTracks[0].Sectors[8].SectorData, 0, DiskTracks[0].Sectors[8].SectorData.Length); + if (ident.ToUpper().Contains("GON DISK PROT")) + return true; + + // hexagon protection may not be labelled as such + var track = DiskTracks[1]; + var sector = track.Sectors[0]; + + if (sector.SectorSize == 6 && sector.Status1 == 0x20 && sector.Status2 == 0x60) + { + if (track.Sectors.Length == 1) + return true; + } - // Hexagon Copy Protection Notes (-asni 2018-05-01) - // --------------------------------------------------- - // - // + // Hexagon Copy Protection Notes (-asni 2018-05-01) + // --------------------------------------------------- + // + // - return false; - } + return false; + } - /* + /* /// /// Should be run at the end of the ParseDisk process /// If speedlock is detected the flag is set in the disk image @@ -501,193 +501,193 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC } */ - /// - /// Returns the track count for the disk - /// - public virtual int GetTrackCount() - { - return DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides; - } + /// + /// Returns the track count for the disk + /// + public virtual int GetTrackCount() + { + return DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides; + } - /// - /// Reads the current sector ID info - /// - public virtual CHRN ReadID(byte trackIndex, byte side, int sectorIndex) - { - if (side != 0) - return null; + /// + /// Reads the current sector ID info + /// + public virtual CHRN ReadID(byte trackIndex, byte side, int sectorIndex) + { + if (side != 0) + return null; - if (DiskTracks.Length <= trackIndex || trackIndex < 0) - { - // invalid track - wrap around - trackIndex = 0; - } + if (DiskTracks.Length <= trackIndex || trackIndex < 0) + { + // invalid track - wrap around + trackIndex = 0; + } - var track = DiskTracks[trackIndex]; + var track = DiskTracks[trackIndex]; - if (track.NumberOfSectors <= sectorIndex) - { - // invalid sector - wrap around - sectorIndex = 0; - } + if (track.NumberOfSectors <= sectorIndex) + { + // invalid sector - wrap around + sectorIndex = 0; + } - var sector = track.Sectors[sectorIndex]; + var sector = track.Sectors[sectorIndex]; - CHRN chrn = new CHRN(); + CHRN chrn = new CHRN(); - chrn.C = sector.TrackNumber; - chrn.H = sector.SideNumber; - chrn.R = sector.SectorID; + chrn.C = sector.TrackNumber; + chrn.H = sector.SideNumber; + chrn.R = sector.SectorID; - // wrap around for N > 7 - if (sector.SectorSize > 7) - { - chrn.N = (byte)(sector.SectorSize - 7); - } - else if (sector.SectorSize < 0) - { - chrn.N = 0; - } - else - { - chrn.N = sector.SectorSize; - } + // wrap around for N > 7 + if (sector.SectorSize > 7) + { + chrn.N = (byte)(sector.SectorSize - 7); + } + else if (sector.SectorSize < 0) + { + chrn.N = 0; + } + else + { + chrn.N = sector.SectorSize; + } - chrn.Flag1 = (byte)(sector.Status1 & 0x25); - chrn.Flag2 = (byte)(sector.Status2 & 0x61); + chrn.Flag1 = (byte)(sector.Status1 & 0x25); + chrn.Flag2 = (byte)(sector.Status2 & 0x61); - chrn.DataBytes = sector.ActualData; + chrn.DataBytes = sector.ActualData; - return chrn; - } + return chrn; + } - /// - /// State serialization routines - /// - public abstract void SyncState(Serializer ser); + /// + /// State serialization routines + /// + public abstract void SyncState(Serializer ser); - public class Header - { - public string DiskIdent { get; set; } - public string DiskCreatorString { get; set; } - public byte NumberOfTracks { get; set; } - public byte NumberOfSides { get; set; } - public int[] TrackSizes { get; set; } - } + public class Header + { + public string DiskIdent { get; set; } + public string DiskCreatorString { get; set; } + public byte NumberOfTracks { get; set; } + public byte NumberOfSides { get; set; } + public int[] TrackSizes { get; set; } + } - public class Track - { - public string TrackIdent { get; set; } - public byte TrackNumber { get; set; } - public byte SideNumber { get; set; } - public byte DataRate { get; set; } - public byte RecordingMode { get; set; } - public byte SectorSize { get; set; } - public byte NumberOfSectors { get; set; } - public byte GAP3Length { get; set; } - public byte FillerByte { get; set; } - public Sector[] Sectors { get; set; } + public class Track + { + public string TrackIdent { get; set; } + public byte TrackNumber { get; set; } + public byte SideNumber { get; set; } + public byte DataRate { get; set; } + public byte RecordingMode { get; set; } + public byte SectorSize { get; set; } + public byte NumberOfSectors { get; set; } + public byte GAP3Length { get; set; } + public byte FillerByte { get; set; } + public Sector[] Sectors { get; set; } - /// - /// Presents a contiguous byte array of all sector data for this track - /// (including any multiple weak/random data) - /// - public byte[] TrackSectorData - { - get - { - List list = new List(); + /// + /// Presents a contiguous byte array of all sector data for this track + /// (including any multiple weak/random data) + /// + public byte[] TrackSectorData + { + get + { + List list = new List(); - foreach (var sec in Sectors) - { - list.AddRange(sec.ActualData); - } + foreach (var sec in Sectors) + { + list.AddRange(sec.ActualData); + } - return list.ToArray(); - } - } - } + return list.ToArray(); + } + } + } - public class Sector - { - public byte TrackNumber { get; set; } - public byte SideNumber { get; set; } - public byte SectorID { get; set; } - public byte SectorSize { get; set; } - public byte Status1 { get; set; } - public byte Status2 { get; set; } - public int ActualDataByteLength { get; set; } - public byte[] SectorData { get; set; } - public bool ContainsMultipleWeakSectors { get; set; } + public class Sector + { + public byte TrackNumber { get; set; } + public byte SideNumber { get; set; } + public byte SectorID { get; set; } + public byte SectorSize { get; set; } + public byte Status1 { get; set; } + public byte Status2 { get; set; } + public int ActualDataByteLength { get; set; } + public byte[] SectorData { get; set; } + public bool ContainsMultipleWeakSectors { get; set; } - public int WeakReadIndex = 0; + public int WeakReadIndex = 0; - public void SectorReadCompleted() - { - if (ContainsMultipleWeakSectors) - WeakReadIndex++; - } + public void SectorReadCompleted() + { + if (ContainsMultipleWeakSectors) + WeakReadIndex++; + } - public int DataLen - { - get - { - if (!ContainsMultipleWeakSectors) - { - return ActualDataByteLength; - } - else - { - return ActualDataByteLength / (ActualDataByteLength / (0x80 << SectorSize)); - } - } - } + public int DataLen + { + get + { + if (!ContainsMultipleWeakSectors) + { + return ActualDataByteLength; + } + else + { + return ActualDataByteLength / (ActualDataByteLength / (0x80 << SectorSize)); + } + } + } - public int RandSecCounter = 0; + public int RandSecCounter = 0; - public byte[] ActualData - { - get - { - if (!ContainsMultipleWeakSectors) - { - // check whether filler bytes are needed - int size = 0x80 << SectorSize; - if (size > ActualDataByteLength) - { - List l = new List(); - l.AddRange(SectorData); - for (int i = 0; i < size - ActualDataByteLength; i++) - { - //l.Add(SectorData[i]); - l.Add(SectorData.Last()); - } + public byte[] ActualData + { + get + { + if (!ContainsMultipleWeakSectors) + { + // check whether filler bytes are needed + int size = 0x80 << SectorSize; + if (size > ActualDataByteLength) + { + List l = new List(); + l.AddRange(SectorData); + for (int i = 0; i < size - ActualDataByteLength; i++) + { + //l.Add(SectorData[i]); + l.Add(SectorData.Last()); + } - return l.ToArray(); - } - else - { - return SectorData; - } - } - else - { - // weak read neccessary - int copies = ActualDataByteLength / (0x80 << SectorSize); + return l.ToArray(); + } + else + { + return SectorData; + } + } + else + { + // weak read neccessary + int copies = ActualDataByteLength / (0x80 << SectorSize); - // handle index wrap-around - if (WeakReadIndex > copies - 1) - WeakReadIndex = copies - 1; + // handle index wrap-around + if (WeakReadIndex > copies - 1) + WeakReadIndex = copies - 1; - // get the sector data based on the current weakreadindex - int step = WeakReadIndex * (0x80 << SectorSize); - byte[] res = new byte[(0x80 << SectorSize)]; - Array.Copy(SectorData, step, res, 0, 0x80 << SectorSize); - return res; + // get the sector data based on the current weakreadindex + int step = WeakReadIndex * (0x80 << SectorSize); + byte[] res = new byte[(0x80 << SectorSize)]; + Array.Copy(SectorData, step, res, 0, 0x80 << SectorSize); + return res; - /* + /* int copies = ActualDataByteLength / (0x80 << SectorSize); Random rnd = new Random(); int r = rnd.Next(0, copies - 1); @@ -696,40 +696,40 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC Array.Copy(SectorData, step, res, 0, 0x80 << SectorSize); return res; */ - } - } - } + } + } + } - public CHRN SectorIDInfo - { - get - { - return new CHRN - { - C = TrackNumber, - H = SideNumber, - R = SectorID, - N = SectorSize, - Flag1 = Status1, - Flag2 = Status2, - }; - } - } - } - } + public CHRN SectorIDInfo + { + get + { + return new CHRN + { + C = TrackNumber, + H = SideNumber, + R = SectorID, + N = SectorSize, + Flag1 = Status1, + Flag2 = Status2, + }; + } + } + } + } - /// - /// Defines the type of speedlock detection found - /// - public enum ProtectionType - { - None, - Speedlock, - Alkatraz, - Hexagon, - Frontier, - PaulOwens, - ShadowOfTheBeast - } + /// + /// Defines the type of speedlock detection found + /// + public enum ProtectionType + { + None, + Speedlock, + Alkatraz, + Hexagon, + Frontier, + PaulOwens, + ShadowOfTheBeast + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/MediaConverter.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/MediaConverter.cs index 435b067a90..effd75ba8a 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/MediaConverter.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/MediaConverter.cs @@ -4,132 +4,132 @@ using System.IO.Compression; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Abtract class that represents all Media Converters - /// - public abstract class MediaConverter - { - /// - /// The type of serializer - /// - public abstract MediaConverterType FormatType { get; } + /// + /// Abtract class that represents all Media Converters + /// + public abstract class MediaConverter + { + /// + /// The type of serializer + /// + public abstract MediaConverterType FormatType { get; } - /// - /// Signs whether this class can be used to read the data format - /// - public virtual bool IsReader - { - get - { - return false; - } - } + /// + /// Signs whether this class can be used to read the data format + /// + public virtual bool IsReader + { + get + { + return false; + } + } - /// - /// Signs whether this class can be used to write the data format - /// - public virtual bool IsWriter - { - get - { - return false; - } - } + /// + /// Signs whether this class can be used to write the data format + /// + public virtual bool IsWriter + { + get + { + return false; + } + } - /// - /// Serialization method - /// - public virtual void Read(byte[] data) - { - throw new NotImplementedException(this.GetType().ToString() + - "Read operation is not implemented for this converter"); - } + /// + /// Serialization method + /// + public virtual void Read(byte[] data) + { + throw new NotImplementedException(this.GetType().ToString() + + "Read operation is not implemented for this converter"); + } - /// - /// DeSerialization method - /// - public virtual void Write(byte[] data) - { - throw new NotImplementedException(this.GetType().ToString() + - "Write operation is not implemented for this converter"); - } + /// + /// DeSerialization method + /// + public virtual void Write(byte[] data) + { + throw new NotImplementedException(this.GetType().ToString() + + "Write operation is not implemented for this converter"); + } - /// - /// Serializer does a quick check, returns TRUE if file is detected as this type - /// - public virtual bool CheckType(byte[] data) - { - throw new NotImplementedException(this.GetType().ToString() + - "Check type operation is not implemented for this converter"); - } + /// + /// Serializer does a quick check, returns TRUE if file is detected as this type + /// + public virtual bool CheckType(byte[] data) + { + throw new NotImplementedException(this.GetType().ToString() + + "Check type operation is not implemented for this converter"); + } - #region Static Tools + #region Static Tools - /// - /// Converts an int32 value into a byte array - /// - public static byte[] GetBytes(int value) - { - byte[] buf = new byte[4]; - buf[0] = (byte)value; - buf[1] = (byte)(value >> 8); - buf[2] = (byte)(value >> 16); - buf[3] = (byte)(value >> 24); - return buf; - } + /// + /// Converts an int32 value into a byte array + /// + public static byte[] GetBytes(int value) + { + byte[] buf = new byte[4]; + buf[0] = (byte)value; + buf[1] = (byte)(value >> 8); + buf[2] = (byte)(value >> 16); + buf[3] = (byte)(value >> 24); + return buf; + } - /// - /// Returns an int32 from a byte array based on offset - /// - public static int GetInt32(byte[] buf, int offsetIndex) - { - return buf[offsetIndex] | buf[offsetIndex + 1] << 8 | buf[offsetIndex + 2] << 16 | buf[offsetIndex + 3] << 24; - } + /// + /// Returns an int32 from a byte array based on offset + /// + public static int GetInt32(byte[] buf, int offsetIndex) + { + return buf[offsetIndex] | buf[offsetIndex + 1] << 8 | buf[offsetIndex + 2] << 16 | buf[offsetIndex + 3] << 24; + } - /// - /// Returns an uint16 from a byte array based on offset - /// - public static ushort GetWordValue(byte[] buf, int offsetIndex) - { - return (ushort)(buf[offsetIndex] | buf[offsetIndex + 1] << 8); - } + /// + /// Returns an uint16 from a byte array based on offset + /// + public static ushort GetWordValue(byte[] buf, int offsetIndex) + { + return (ushort)(buf[offsetIndex] | buf[offsetIndex + 1] << 8); + } - /// - /// Updates a byte array with a uint16 value based on offset - /// - public static void SetWordValue(byte[] buf, int offsetIndex, ushort value) - { - buf[offsetIndex] = (byte)value; - buf[offsetIndex + 1] = (byte)(value >> 8); - } + /// + /// Updates a byte array with a uint16 value based on offset + /// + public static void SetWordValue(byte[] buf, int offsetIndex, ushort value) + { + buf[offsetIndex] = (byte)value; + buf[offsetIndex + 1] = (byte)(value >> 8); + } - /// - /// Takes a PauseInMilliseconds value and returns the value in T-States - /// - public static int TranslatePause(int pauseInMS) - { - // t-states per millisecond - var tspms = (69888 * 50) / 1000; - // get value - int res = pauseInMS * tspms; + /// + /// Takes a PauseInMilliseconds value and returns the value in T-States + /// + public static int TranslatePause(int pauseInMS) + { + // t-states per millisecond + var tspms = (69888 * 50) / 1000; + // get value + int res = pauseInMS * tspms; - return res; - } + return res; + } - /// - /// Decompresses a byte array that is Z-RLE compressed - /// - public static void DecompressZRLE(byte[] sourceBuffer, ref byte[] destBuffer) - { - MemoryStream stream = new MemoryStream(); - stream.Write(sourceBuffer, 0, sourceBuffer.Length); - stream.Position = 0; - stream.ReadByte(); - stream.ReadByte(); - DeflateStream ds = new DeflateStream(stream, CompressionMode.Decompress, false); - ds.Read(destBuffer, 0, destBuffer.Length); - } + /// + /// Decompresses a byte array that is Z-RLE compressed + /// + public static void DecompressZRLE(byte[] sourceBuffer, ref byte[] destBuffer) + { + MemoryStream stream = new MemoryStream(); + stream.Write(sourceBuffer, 0, sourceBuffer.Length); + stream.Position = 0; + stream.ReadByte(); + stream.ReadByte(); + DeflateStream ds = new DeflateStream(stream, CompressionMode.Decompress, false); + ds.Read(destBuffer, 0, destBuffer.Length); + } - #endregion - } + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/MediaConverterType.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/MediaConverterType.cs index 08f5f73250..218a11a72a 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/MediaConverterType.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/MediaConverterType.cs @@ -1,13 +1,13 @@  namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Represents the different types of media serializer avaiable - /// - public enum MediaConverterType - { - NONE, - CDT, - DSK - } + /// + /// Represents the different types of media serializer avaiable + /// + public enum MediaConverterType + { + NONE, + CDT, + DSK + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Tape/CDT/CdtConverter.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Tape/CDT/CdtConverter.cs index f8c5a62b68..a501bd0d07 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Tape/CDT/CdtConverter.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Tape/CDT/CdtConverter.cs @@ -5,112 +5,112 @@ using System.Text; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Reponsible for TZX format serializaton - /// - public class CdtConverter : MediaConverter - { - /// - /// The type of serializer - /// - private MediaConverterType _formatType = MediaConverterType.CDT; - public override MediaConverterType FormatType - { - get - { - return _formatType; - } - } + /// + /// Reponsible for TZX format serializaton + /// + public class CdtConverter : MediaConverter + { + /// + /// The type of serializer + /// + private MediaConverterType _formatType = MediaConverterType.CDT; + public override MediaConverterType FormatType + { + get + { + return _formatType; + } + } - /// - /// Signs whether this class can be used to read the data format - /// - public override bool IsReader { get { return true; } } + /// + /// Signs whether this class can be used to read the data format + /// + public override bool IsReader { get { return true; } } - /// - /// Signs whether this class can be used to write the data format - /// - public override bool IsWriter { get { return false; } } + /// + /// Signs whether this class can be used to write the data format + /// + public override bool IsWriter { get { return false; } } - /// - /// Working list of generated tape data blocks - /// - private List _blocks = new List(); + /// + /// Working list of generated tape data blocks + /// + private List _blocks = new List(); - /// - /// Position counter - /// - private int _position = 0; + /// + /// Position counter + /// + private int _position = 0; - /// - /// Object to keep track of loops - this assumes there is only one loop at a time - /// - private List> _loopCounter = new List>(); + /// + /// Object to keep track of loops - this assumes there is only one loop at a time + /// + private List> _loopCounter = new List>(); - #region Construction + #region Construction - private DatacorderDevice _datacorder; + private DatacorderDevice _datacorder; - public CdtConverter(DatacorderDevice _tapeDevice) - { - _datacorder = _tapeDevice; - } + public CdtConverter(DatacorderDevice _tapeDevice) + { + _datacorder = _tapeDevice; + } - #endregion + #endregion - /// - /// CDT format is essentially exactly the same as the TZX format - /// However all timings are based on spectrum timings (3.5Mhz) - /// so need to be adjusted for the CPC (4Mhz) - /// - private TapeDataBlock ConvertClock(TapeDataBlock db) - { - TapeDataBlock tb = new TapeDataBlock(); - tb.BlockDescription = db.BlockDescription; - tb.BlockID = db.BlockID; - tb.Command = db.Command; - tb.DataPeriods = new List(); - tb.InitialPulseLevel = db.InitialPulseLevel; - tb.MetaData = db.MetaData; - tb.PauseInMS = db.PauseInMS; + /// + /// CDT format is essentially exactly the same as the TZX format + /// However all timings are based on spectrum timings (3.5Mhz) + /// so need to be adjusted for the CPC (4Mhz) + /// + private TapeDataBlock ConvertClock(TapeDataBlock db) + { + TapeDataBlock tb = new TapeDataBlock(); + tb.BlockDescription = db.BlockDescription; + tb.BlockID = db.BlockID; + tb.Command = db.Command; + tb.DataPeriods = new List(); + tb.InitialPulseLevel = db.InitialPulseLevel; + tb.MetaData = db.MetaData; + tb.PauseInMS = db.PauseInMS; - double multiplier = (double)4 / (double)3.5; - //double cycleScale = ((40 << 16) / 35); - double origPeriods = db.DataPeriods.Count(); + double multiplier = (double)4 / (double)3.5; + //double cycleScale = ((40 << 16) / 35); + double origPeriods = db.DataPeriods.Count(); - for (int i = 0; i < origPeriods; i++) - { - int orig = db.DataPeriods[i]; - int np = (int)((double)orig * multiplier); - int nnp = ClockAdjust(orig); - tb.DataPeriods.Add(np); - } + for (int i = 0; i < origPeriods; i++) + { + int orig = db.DataPeriods[i]; + int np = (int)((double)orig * multiplier); + int nnp = ClockAdjust(orig); + tb.DataPeriods.Add(np); + } - return tb; - } + return tb; + } - private int ClockAdjust(int val) - { - int cycleScale = ((40 << 16) / 35); - int res = (val * cycleScale) >> 16; - return res; - } + private int ClockAdjust(int val) + { + int cycleScale = ((40 << 16) / 35); + int res = (val * cycleScale) >> 16; + return res; + } - private int Scale => ((40 << 16) / 35); + private int Scale => ((40 << 16) / 35); - private int Adjust(int val) - { - return (int)((val * CLOCK_MULTIPLIER)); - } + private int Adjust(int val) + { + return (int)((val * CLOCK_MULTIPLIER)); + } - private const double CLOCK_MULTIPLIER = 1.142857; + private const double CLOCK_MULTIPLIER = 1.142857; - /// - /// Returns TRUE if tzx header is detected - /// - public override bool CheckType(byte[] data) - { - /* + /// + /// Returns TRUE if tzx header is detected + /// + public override bool CheckType(byte[] data) + { + /* // TZX Header length: 10 bytes Offset Value Type Description @@ -120,74 +120,74 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC 0x09 20 BYTE TZX minor revision number */ - // check whether this is a valid tzx format file by looking at the identifier in the header - // (first 7 bytes of the file) - string ident = Encoding.ASCII.GetString(data, 0, 7); - // and 'end of text' marker - byte eotm = data[7]; + // check whether this is a valid tzx format file by looking at the identifier in the header + // (first 7 bytes of the file) + string ident = Encoding.ASCII.GetString(data, 0, 7); + // and 'end of text' marker + byte eotm = data[7]; - // version info - int majorVer = data[8]; - int minorVer = data[9]; + // version info + int majorVer = data[8]; + int minorVer = data[9]; - if (ident != "ZXTape!" || eotm != 0x1A) - { - // this is not a valid TZX format file - return false; - } - else - { - return true; - } - } + if (ident != "ZXTape!" || eotm != 0x1A) + { + // this is not a valid TZX format file + return false; + } + else + { + return true; + } + } - /// - /// DeSerialization method - /// - public override void Read(byte[] data) - { - // clear existing tape blocks - _datacorder.DataBlocks.Clear(); + /// + /// DeSerialization method + /// + public override void Read(byte[] data) + { + // clear existing tape blocks + _datacorder.DataBlocks.Clear(); -/* - // TZX Header - length: 10 bytes - Offset Value Type Description - 0x00 "ZXTape!" ASCII[7] TZX signature - 0x07 0x1A BYTE End of text file marker - 0x08 1 BYTE TZX major revision number - 0x09 20 BYTE TZX minor revision number -*/ + /* + // TZX Header + length: 10 bytes + Offset Value Type Description + 0x00 "ZXTape!" ASCII[7] TZX signature + 0x07 0x1A BYTE End of text file marker + 0x08 1 BYTE TZX major revision number + 0x09 20 BYTE TZX minor revision number + */ - // check whether this is a valid tzx format file by looking at the identifier in the header - // (first 7 bytes of the file) - string ident = Encoding.ASCII.GetString(data, 0, 7); - // and 'end of text' marker - byte eotm = data[7]; + // check whether this is a valid tzx format file by looking at the identifier in the header + // (first 7 bytes of the file) + string ident = Encoding.ASCII.GetString(data, 0, 7); + // and 'end of text' marker + byte eotm = data[7]; - // version info - int majorVer = data[8]; - int minorVer = data[9]; + // version info + int majorVer = data[8]; + int minorVer = data[9]; - if (ident != "ZXTape!" || eotm != 0x1A) - { - // this is not a valid TZX format file - throw new Exception(this.GetType().ToString() + - "This is not a valid TZX format file"); - } + if (ident != "ZXTape!" || eotm != 0x1A) + { + // this is not a valid TZX format file + throw new Exception(this.GetType().ToString() + + "This is not a valid TZX format file"); + } - // iterate through each block - _position = 10; - while (_position < data.Length) - { - // block ID is the first byte in a new block - int ID = data[_position++]; + // iterate through each block + _position = 10; + while (_position < data.Length) + { + // block ID is the first byte in a new block + int ID = data[_position++]; - // process the data - ProcessBlock(data, ID); - } + // process the data + ProcessBlock(data, ID); + } - /* + /* // convert for Amstrad CPC List newBlocks = new List(); for (int i = 0; i < _datacorder.DataBlocks.Count(); i++) @@ -198,1432 +198,1432 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC _datacorder.DataBlocks.Clear(); _datacorder.DataBlocks.AddRange(newBlocks); */ - } - - /// - /// Processes a TZX block - /// - private void ProcessBlock(byte[] data, int id) - { - // process based on detected block ID - switch (id) - { - // ID 10 - Standard Speed Data Block - case 0x10: - ProcessBlockID10(data); - break; - // ID 11 - Turbo Speed Data Block - case 0x11: - ProcessBlockID11(data); - break; - // ID 12 - Pure Tone - case 0x12: - ProcessBlockID12(data); - break; - // ID 13 - Pulse sequence - case 0x13: - ProcessBlockID13(data); - break; - // ID 14 - Pure Data Block - case 0x14: - ProcessBlockID14(data); - break; - // ID 15 - Direct Recording - case 0x15: - ProcessBlockID15(data); - break; - // ID 18 - CSW Recording - case 0x18: - ProcessBlockID18(data); - break; - // ID 19 - Generalized Data Block - case 0x19: - ProcessBlockID19(data); - break; - // ID 20 - Pause (silence) or 'Stop the Tape' command - case 0x20: - ProcessBlockID20(data); - break; - // ID 21 - Group start - case 0x21: - ProcessBlockID21(data); - break; - // ID 22 - Group end - case 0x22: - ProcessBlockID22(data); - break; - // ID 23 - Jump to block - case 0x23: - ProcessBlockID23(data); - break; - // ID 24 - Loop start - case 0x24: - ProcessBlockID24(data); - break; - // ID 25 - Loop end - case 0x25: - ProcessBlockID25(data); - break; - // ID 26 - Call sequence - case 0x26: - ProcessBlockID26(data); - break; - // ID 27 - Return from sequence - case 0x27: - ProcessBlockID27(data); - break; - // ID 28 - Select block - case 0x28: - ProcessBlockID28(data); - break; - // ID 2A - Stop the tape if in 48K mode - case 0x2A: - ProcessBlockID2A(data); - break; - // ID 2B - Set signal level - case 0x2B: - ProcessBlockID2B(data); - break; - // ID 30 - Text description - case 0x30: - ProcessBlockID30(data); - break; - // ID 31 - Message block - case 0x31: - ProcessBlockID31(data); - break; - // ID 32 - Archive info - case 0x32: - ProcessBlockID32(data); - break; - // ID 33 - Hardware type - case 0x33: - ProcessBlockID33(data); - break; - // ID 35 - Custom info block - case 0x35: - ProcessBlockID35(data); - break; - // ID 5A - "Glue" block - case 0x5A: - ProcessBlockID5A(data); - break; - - #region Depreciated Blocks - - // ID 16 - C64 ROM Type Data Block - case 0x16: - ProcessBlockID16(data); - break; - // ID 17 - C64 Turbo Tape Data Block - case 0x17: - ProcessBlockID17(data); - break; - // ID 34 - Emulation info - case 0x34: - ProcessBlockID34(data); - break; - // ID 40 - Snapshot block - case 0x40: - ProcessBlockID40(data); - break; - - #endregion - - default: - ProcessUnidentifiedBlock(data); - break; - } - } - - #region TZX Block Processors - - #region ID 10 - Standard Speed Data Block -/* length: [02,03]+04 - Offset Value Type Description - 0x00 - WORD Pause after this block (ms.) {1000} - 0x02 N WORD Length of data that follow - 0x04 - BYTE[N] Data as in .TAP files - - This block must be replayed with the standard Spectrum ROM timing values - see the values in - curly brackets in block ID 11. The pilot tone consists in 8063 pulses if the first data byte - (flag byte) is < 128, 3223 otherwise. This block can be used for the ROM loading routines AND - for custom loading routines that use the same timings as ROM ones do. */ - private void ProcessBlockID10(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x10; - t.BlockDescription = BlockType.Standard_Speed_Data_Block; - t.DataPeriods = new List(); - - int pauseLen = GetWordValue(data, _position); - if (pauseLen == 0) - pauseLen = 1000; - - t.PauseInMS = pauseLen; - - int blockLen = GetWordValue(data, _position + 2); - - _position += 4; - - byte[] tmp = new byte[blockLen]; - tmp = data.Skip(_position).Take(blockLen).ToArray(); - - var t2 = DecodeDataBlock(t, tmp, DataBlockType.Standard, pauseLen); - - // add the block - _datacorder.DataBlocks.Add(t2); - - // advance the position to the next block - _position += blockLen; - - // generate PAUSE block - CreatePauseBlock(_datacorder.DataBlocks.Last()); - } - #endregion - - #region ID 11 - Turbo Speed Data Block -/* length: [0F,10,11]+12 - Offset Value Type Description - 0x00 - WORD Length of PILOT pulse {2168} - 0x02 - WORD Length of SYNC first pulse {667} - 0x04 - WORD Length of SYNC second pulse {735} - 0x06 - WORD Length of ZERO bit pulse {855} - 0x08 - WORD Length of ONE bit pulse {1710} - 0x0A - WORD Length of PILOT tone (number of pulses) {8063 header (flag<128), 3223 data (flag>=128)} - 0x0C - BYTE Used bits in the last byte (other bits should be 0) {8} - (e.g. if this is 6, then the bits used (x) in the last byte are: xxxxxx00, - where MSb is the leftmost bit, LSb is the rightmost bit) - 0x0D - WORD Pause after this block (ms.) {1000} - 0x0F N BYTE[3] Length of data that follow - 0x12 - BYTE[N] Data as in .TAP files - - This block is very similar to the normal TAP block but with some additional info on the timings and other important - differences. The same tape encoding is used as for the standard speed data block. If a block should use some non-standard - sync or pilot tones (i.e. all sorts of protection schemes) then use the next three blocks to describe it.*/ - private void ProcessBlockID11(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x11; - t.BlockDescription = BlockType.Turbo_Speed_Data_Block; - t.DataPeriods = new List(); - - int pilotPL = GetWordValue(data, _position); - int sync1P = GetWordValue(data, _position + 2); - int sync2P = GetWordValue(data, _position + 4); - int bit0P = GetWordValue(data, _position + 6); - int bit1P = GetWordValue(data, _position + 8); - int pilotTL = GetWordValue(data, _position + 10); - int bitinbyte = data[_position + 12]; - int pause = GetWordValue(data, _position + 13); - - - int blockLen = 0xFFFFFF & GetInt32(data, _position + 0x0F); - - byte[] bLenArr = data.Skip(_position + 0x0F).Take(3).ToArray(); - - _position += 0x12; - - byte[] tmp = new byte[blockLen]; - tmp = data.Skip(_position).Take(blockLen).ToArray(); - - var t2 = DecodeDataBlock(t, tmp, DataBlockType.Turbo, pause, pilotTL, pilotPL, sync1P, sync2P, bit0P, bit1P, bitinbyte); - - t.PauseInMS = pause; - - // add the block - _datacorder.DataBlocks.Add(t2); - - // advance the position to the next block - _position += blockLen; - - // generate PAUSE block - CreatePauseBlock(_datacorder.DataBlocks.Last()); - } - #endregion - - #region ID 12 - Pure Tone -/* length: 04 - Offset Value Type Description - 0x00 - WORD Length of one pulse in T-states - 0x02 - WORD Number of pulses - - This will produce a tone which is basically the same as the pilot tone in the ID 10, ID 11 blocks. You can define how - long the pulse is and how many pulses are in the tone. */ - private void ProcessBlockID12(byte[] data) - { - int blockLen = 4; - - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x12; - t.BlockDescription = BlockType.Pure_Tone; - t.DataPeriods = new List(); - t.PauseInMS = 0; - - // get values - int pulseLength = GetWordValue(data, _position); - int pulseCount = GetWordValue(data, _position + 2); - - t.AddMetaData(BlockDescriptorTitle.Pulse_Length, pulseLength.ToString() + " T-States"); - t.AddMetaData(BlockDescriptorTitle.Pulse_Count, pulseCount.ToString()); - - // build period information - for (int p = 0; p < pulseCount; p++) - { - t.DataPeriods.Add(pulseLength); - } - - // add the block - _datacorder.DataBlocks.Add(t); - - // advance the position to the next block - _position += blockLen; - } - #endregion - - #region ID 13 - Pulse sequence -/* length: [00]*02+01 - Offset Value Type Description - 0x00 N BYTE Number of pulses - 0x01 - WORD[N] Pulses' lengths - - This will produce N pulses, each having its own timing. Up to 255 pulses can be stored in this block; this is useful for non-standard - sync tones used by some protection schemes. */ - private void ProcessBlockID13(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x13; - t.BlockDescription = BlockType.Pulse_Sequence; - t.DataPeriods = new List(); - - t.PauseInMS = 0; - - // get pulse count - int pulseCount = data[_position]; - t.AddMetaData(BlockDescriptorTitle.Pulse_Count, pulseCount.ToString()); - _position++; - - // build period information - for (int p = 0; p < pulseCount; p++, _position += 2) - { - // get pulse length - int pulseLength = GetWordValue(data, _position); - t.AddMetaData(BlockDescriptorTitle.Needs_Parsing, "Pulse " + p + " Length\t" + pulseLength.ToString() + " T-States"); - t.DataPeriods.Add(pulseLength); - } - - // add the block - _datacorder.DataBlocks.Add(t); - } - #endregion - - #region ID 14 - Pure Data Block -/* length: [07,08,09]+0A - Offset Value Type Description - 0x00 - WORD Length of ZERO bit pulse - 0x02 - WORD Length of ONE bit pulse - 0x04 - BYTE Used bits in last byte (other bits should be 0) - (e.g. if this is 6, then the bits used (x) in the last byte are: xxxxxx00, - where MSb is the leftmost bit, LSb is the rightmost bit) - 0x05 - WORD Pause after this block (ms.) - 0x07 N BYTE[3] Length of data that follow - 0x0A - BYTE[N] Data as in .TAP files - - This is the same as in the turbo loading data block, except that it has no pilot or sync pulses. */ - private void ProcessBlockID14(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x14; - t.BlockDescription = BlockType.Pure_Data_Block; - t.DataPeriods = new List(); - - int pilotPL = 0; - int sync1P = 0; - int sync2P = 0; - int bit0P = GetWordValue(data, _position + 0); - int bit1P = GetWordValue(data, _position + 2); - int pilotTL = 0; - int bitinbyte = data[_position + 4]; - int pause = GetWordValue(data, _position + 5); - - int blockLen = 0xFFFFFF & GetInt32(data, _position + 0x07); - - _position += 0x0A; - - byte[] tmp = new byte[blockLen]; - tmp = data.Skip(_position).Take(blockLen).ToArray(); - - var t2 = DecodeDataBlock(t, tmp, DataBlockType.Pure, pause, pilotTL, pilotPL, sync1P, sync2P, bit0P, bit1P, bitinbyte); - - t.PauseInMS = pause; - - // add the block - _datacorder.DataBlocks.Add(t2); - - // advance the position to the next block - _position += blockLen; - - // generate PAUSE block - CreatePauseBlock(_datacorder.DataBlocks.Last()); - } - #endregion - - #region ID 15 - Direct Recording -/* length: [05,06,07]+08 - Offset Value Type Description - 0x00 - WORD Number of T-states per sample (bit of data) - 0x02 - WORD Pause after this block in milliseconds (ms.) - 0x04 - BYTE Used bits (samples) in last byte of data (1-8) - (e.g. if this is 2, only first two samples of the last byte will be played) - 0x05 N BYTE[3] Length of samples' data - 0x08 - BYTE[N] Samples data. Each bit represents a state on the EAR port (i.e. one sample). - MSb is played first. - - This block is used for tapes which have some parts in a format such that the turbo loader block cannot be used. - This is not like a VOC file, since the information is much more compact. Each sample value is represented by one bit only - (0 for low, 1 for high) which means that the block will be at most 1/8 the size of the equivalent VOC. - The preferred sampling frequencies are 22050 or 44100 Hz (158 or 79 T-states/sample). - Please, if you can, don't use other sampling frequencies. - Please use this block only if you cannot use any other block. */ - private void ProcessBlockID15(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x15; - t.BlockDescription = BlockType.Direct_Recording; - t.DataPeriods = new List(); - - // get values - int samLen = GetInt32(data, _position + 5); - int samSize = 0xFFFFFF & samLen; - - int tStatesPerSample = GetWordValue(data, _position); - int pauseAfterBlock = GetWordValue(data, _position + 2); - int usedBitsInLastByte = data[_position + 4]; - - // skip to samples data - _position += 8; - - int pulseLength = 0; - int pulseCount = 0; - - // ascertain the pulse count - for (int i = 0; i < samSize; i++) - { - for (int p = 0x80; p != 0; p >>= 1) - { - if (((data[_position + i] ^ pulseLength) & p) != 0) - { - pulseCount++; - pulseLength ^= -1; - } - } - } - - // get the pulses - t.DataPeriods = new List(pulseCount + 2); - int tStateCount = 0; - pulseLength = 0; - for (int i = 1; i < samSize; i++) - { - for (int p = 0x80; p != 0; p >>= 1) - { - tStateCount += tStatesPerSample; - if (((data[_position] ^ pulseLength) & p) != 0) - { - t.DataPeriods.Add(tStateCount); - pulseLength ^= -1; - tStateCount = 0; - } - } - - // incrememt position - _position++; - } - - // get the pulses in the last byte of data - for (int p = 0x80; p != (byte)(0x80 >> usedBitsInLastByte); p >>= 1) - { - tStateCount += tStatesPerSample; - if (((data[_position] ^ pulseLength) & p) != 0) - { - t.DataPeriods.Add(tStateCount); - pulseLength ^= -1; - tStateCount = 0; - } - } - - // add final pulse - t.DataPeriods.Add(tStateCount); - - // add end of block pause - if (pauseAfterBlock > 0) - { - //t.DataPeriods.Add(3500 * pauseAfterBlock); - } - - t.PauseInMS = pauseAfterBlock; - - // increment position - _position++; - - // add the block - _datacorder.DataBlocks.Add(t); - - // generate PAUSE block - CreatePauseBlock(_datacorder.DataBlocks.Last()); - } - #endregion - - #region ID 18 - CSW Recording -/* length: [00,01,02,03]+04 - Offset Value Type Description - 0x00 10+N DWORD Block length (without these four bytes) - 0x04 - WORD Pause after this block (in ms). - 0x06 - BYTE[3] Sampling rate - 0x09 - BYTE Compression type - 0x01: RLE - 0x02: Z-RLE - 0x0A - DWORD Number of stored pulses (after decompression, for validation purposes) - 0x0E - BYTE[N] CSW data, encoded according to the CSW file format specification. - - This block contains a sequence of raw pulses encoded in CSW format v2 (Compressed Square Wave). */ - private void ProcessBlockID18(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x18; - t.BlockDescription = BlockType.CSW_Recording; - t.DataPeriods = new List(); - - int blockLen = GetInt32(data, _position); - _position += 4; - - t.PauseInMS = GetWordValue(data, _position); - - _position += 2; - - int sampleRate = data[_position++] << 16 | data[_position++] << 8 | data[_position++]; - byte compType = data[_position++]; - int pulses = GetInt32(data, _position); - _position += 4; - - int dataLen = blockLen - 10; - - // build source array - byte[] src = new byte[dataLen]; - // build destination array - byte[] dest = new byte[pulses + 1]; - - // process the CSW data - BizHawk.Emulation.Cores.Computers.SinclairSpectrum.CswConverter.ProcessCSWV2(src, ref dest, compType, pulses); - - // create the periods - var rate = (69888 * 50) / sampleRate; - - for (int i = 0; i < dest.Length;) - { - int length = dest[i++] * rate; - if (length == 0) - { - length = GetInt32(dest, i) / rate; - i += 4; - } - - t.DataPeriods.Add(length); - } - - // add closing period - t.DataPeriods.Add((69888 * 50) / 10); - - _position += dataLen; - //_position += blockLen; - - // add the block - _datacorder.DataBlocks.Add(t); - - // generate PAUSE block - CreatePauseBlock(_datacorder.DataBlocks.Last()); - } - #endregion - - #region ID 19 - Generalized Data Block -/* length: [00,01,02,03]+04 - Offset Value Type Description - 0x00 - DWORD Block length (without these four bytes) - 0x04 - WORD Pause after this block (ms) - 0x06 TOTP DWORD Total number of symbols in pilot/sync block (can be 0) - 0x0A NPP BYTE Maximum number of pulses per pilot/sync symbol - 0x0B ASP BYTE Number of pilot/sync symbols in the alphabet table (0=256) - 0x0C TOTD DWORD Total number of symbols in data stream (can be 0) - 0x10 NPD BYTE Maximum number of pulses per data symbol - 0x11 ASD BYTE Number of data symbols in the alphabet table (0=256) - 0x12 - SYMDEF[ASP] Pilot and sync symbols definition table - This field is present only if TOTP>0 - 0x12+ - (2*NPP+1)*ASP - PRLE[TOTP] Pilot and sync data stream - This field is present only if TOTP>0 - 0x12+ - (TOTP>0)*((2*NPP+1)*ASP)+ - TOTP*3 - SYMDEF[ASD] Data symbols definition table - This field is present only if TOTD>0 - 0x12+ - (TOTP>0)*((2*NPP+1)*ASP)+ - TOTP*3+ - (2*NPD+1)*ASD - BYTE[DS] Data stream - This field is present only if TOTD>0 - - This block has been specifically developed to represent an extremely wide range of data encoding techniques. - The basic idea is that each loading component (pilot tone, sync pulses, data) is associated to a specific sequence - of pulses, where each sequence (wave) can contain a different number of pulses from the others. - In this way we can have a situation where bit 0 is represented with 4 pulses and bit 1 with 8 pulses. - - ---- - SYMDEF structure format - Offset Value Type Description - 0x00 - BYTE Symbol flags - b0-b1: starting symbol polarity - 00: opposite to the current level (make an edge, as usual) - default - 01: same as the current level (no edge - prolongs the previous pulse) - 10: force low level - 11: force high level - 0x01 - WORD[MAXP] Array of pulse lengths. - - The alphabet is stored using a table where each symbol is a row of pulses. The number of columns (i.e. pulses) of the table is the - length of the longest sequence amongst all (MAXP=NPP or NPD, for pilot/sync or data blocks respectively); shorter waves are terminated by a - zero-length pulse in the sequence. - Any number of data symbols is allowed, so we can have more than two distinct waves; for example, imagine a loader which writes two bits at a - time by encoding them with four distinct pulse lengths: this loader would have an alphabet of four symbols, each associated to a specific - sequence of pulses (wave). - ---- - ---- - PRLE structure format - Offset Value Type Description - 0x00 - BYTE Symbol to be represented - 0x01 - WORD Number of repetitions - - Most commonly, pilot and sync are repetitions of the same pulse, thus they are represented using a very simple RLE encoding structure which stores - the symbol and the number of times it must be repeated. - Each symbol in the data stream is represented by a string of NB bits of the block data, where NB = ceiling(Log2(ASD)). - Thus the length of the whole data stream in bits is NB*TOTD, or in bytes DS=ceil(NB*TOTD/8). - ---- */ - private void ProcessBlockID19(byte[] data) - { - // not currently implemented properly - - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x19; - t.BlockDescription = BlockType.Generalized_Data_Block; - t.DataPeriods = new List(); - - int blockLen = GetInt32(data, _position); - _position += 4; - - int pause = GetWordValue(data, _position); - _position += 2; - - int totp = GetInt32(data, _position); - _position += 4; - - int npp = data[_position++]; - - int asp = data[_position++]; - - int totd = GetInt32(data, _position); - _position += 4; - - int npd = data[_position++]; - - int asd = data[_position++]; - - // add the block - _datacorder.DataBlocks.Add(t); - - // advance the position to the next block - _position += blockLen; - } - #endregion - - #region ID 20 - Pause (silence) or 'Stop the Tape' command -/* length: 02 - Offset Value Type Description - 0x00 - WORD Pause duration (ms.) - - This will make a silence (low amplitude level (0)) for a given time in milliseconds. If the value is 0 then the - emulator or utility should (in effect) STOP THE TAPE, i.e. should not continue loading until the user or emulator requests it. */ - private void ProcessBlockID20(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x20; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Pause_or_Stop_the_Tape; - - int pauseDuration = GetWordValue(data, _position); - if (pauseDuration != 0) - { - //t.BlockDescription = "Pause: " + pauseDuration + " ms"; - } - else - { - //t.BlockDescription = "[STOP THE TAPE]"; - } - - t.PauseInMS = pauseDuration; - - if (pauseDuration == 0) - { - // issue stop the tape command - t.Command = TapeCommand.STOP_THE_TAPE; - // add 1ms period - //t.DataPeriods.Add(3500); - //pauseDuration = -1; - - } - else - { - // this is actually just a pause - //pauseDuration = 3500 * pauseDuration; - //t.DataPeriods.Add(pauseDuration); - } - - // add end of block pause - //t.DataPeriods.Add(pauseDuration); - - // add to tape - _datacorder.DataBlocks.Add(t); - - // advanced position to next block - _position += 2; - - // generate PAUSE block - CreatePauseBlock(_datacorder.DataBlocks.Last()); - - } - #endregion - - #region ID 21 - Group start -/* length: [00]+01 - Offset Value Type Description - 0x00 L BYTE Length of the group name string - 0x01 - CHAR[L] Group name in ASCII format (please keep it under 30 characters long) - - This block marks the start of a group of blocks which are to be treated as one single (composite) block. - This is very handy for tapes that use lots of subblocks like Bleepload (which may well have over 160 custom loading blocks). - You can also give the group a name (example 'Bleepload Block 1'). - For each group start block, there must be a group end block. Nesting of groups is not allowed. */ - private void ProcessBlockID21(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x21; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Group_Start; - - int nameLength = data[_position]; - _position++; - - string name = Encoding.ASCII.GetString(data, _position, nameLength); - //t.BlockDescription = "[GROUP: " + name + "]"; - t.Command = TapeCommand.BEGIN_GROUP; - - t.PauseInMS = 0; - - // add to tape - _datacorder.DataBlocks.Add(t); - - // advance to next block - _position += nameLength; - } - #endregion - - #region ID 22 - Group end -/* length: 00 - - This indicates the end of a group. This block has no body. */ - private void ProcessBlockID22(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x22; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Group_End; - t.Command = TapeCommand.END_GROUP; - - t.PauseInMS = 0; - - // add to tape - _datacorder.DataBlocks.Add(t); - } - #endregion - - #region ID 23 - Jump to block -/* length: 02 - Offset Value Type Description - 0x00 - WORD Relative jump value - - This block will enable you to jump from one block to another within the file. The value is a signed short word - (usually 'signed short' in C); Some examples: - Jump 0 = 'Loop Forever' - this should never happen - Jump 1 = 'Go to the next block' - it is like NOP in assembler ;) - Jump 2 = 'Skip one block' - Jump -1 = 'Go to the previous block' - All blocks are included in the block count!. */ - private void ProcessBlockID23(byte[] data) - { - // not implemented properly - - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x23; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Jump_to_Block; - - int relativeJumpValue = GetWordValue(data, _position); - string result = string.Empty; - - switch(relativeJumpValue) - { - case 0: - result = "Loop Forever"; - break; - case 1: - result = "To Next Block"; - break; - case 2: - result = "Skip One Block"; - break; - case -1: - result = "Go to Previous Block"; - break; - } - - //t.BlockDescription = "[JUMP BLOCK - " + result +"]"; - - t.PauseInMS = 0; - - // add to tape - _datacorder.DataBlocks.Add(t); - - // advance to next block - _position += 2; - } - #endregion - - #region ID 24 - Loop start -/* length: 02 - Offset Value Type Description - 0x00 - WORD Number of repetitions (greater than 1) - - If you have a sequence of identical blocks, or of identical groups of blocks, you can use this block to tell how many times they should - be repeated. This block is the same as the FOR statement in BASIC. - For simplicity reasons don't nest loop blocks! */ - private void ProcessBlockID24(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x24; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Loop_Start; - - // loop should start from the next block - int loopStart = _datacorder.DataBlocks.Count() + 1; - - int numberOfRepetitions = GetWordValue(data, _position); - - // update loop counter - _loopCounter.Add( - new KeyValuePair( - loopStart, - numberOfRepetitions)); - - // update description - //t.BlockDescription = "[LOOP START - " + numberOfRepetitions + " times]"; - - t.PauseInMS = 0; - - // add to tape - _datacorder.DataBlocks.Add(t); - - // advance to next block - _position += 2; - } - #endregion - - #region ID 25 - Loop end -/* length: 00 - - This is the same as BASIC's NEXT statement. It means that the utility should jump back to the start of the loop if it hasn't - been run for the specified number of times. - This block has no body. */ - private void ProcessBlockID25(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x25; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Loop_End; - - // get the most recent loop info - var loop = _loopCounter.LastOrDefault(); - - int loopStart = loop.Key; - int numberOfRepetitions = loop.Value; - - if (numberOfRepetitions == 0) - { - return; - } - - // get the number of blocks to loop - int blockCnt = _datacorder.DataBlocks.Count() - loopStart; - - // loop through each group to repeat - for (int b = 0; b < numberOfRepetitions; b++) - { - TapeDataBlock repeater = new TapeDataBlock(); - //repeater.BlockDescription = "[LOOP REPEAT - " + (b + 1) + "]"; - repeater.DataPeriods = new List(); - - // add the repeat block - _datacorder.DataBlocks.Add(repeater); - - // now iterate through and add the blocks to be repeated - for (int i = 0; i < blockCnt; i++) - { - var block = _datacorder.DataBlocks[loopStart + i]; - _datacorder.DataBlocks.Add(block); - } - } - } - #endregion - - #region ID 26 - Call sequence -/* length: [00,01]*02+02 - Offset Value Type Description - 0x00 N WORD Number of calls to be made - 0x02 - WORD[N] Array of call block numbers (relative-signed offsets) - - This block is an analogue of the CALL Subroutine statement. It basically executes a sequence of blocks that are somewhere else and - then goes back to the next block. Because more than one call can be normally used you can include a list of sequences to be called. - The 'nesting' of call blocks is also not allowed for the simplicity reasons. You can, of course, use the CALL blocks in the LOOP - sequences and vice versa. The value is relative for the obvious reasons - so that you can add some blocks in the beginning of the - file without disturbing the call values. Please take a look at 'Jump To Block' for reference on the values. */ - private void ProcessBlockID26(byte[] data) - { - // block processing not implemented for this - just gets added for informational purposes only - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x26; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Call_Sequence; - - int blockSize = 2 + 2 * GetWordValue(data, _position); - t.PauseInMS = 0; - - - // add to tape - _datacorder.DataBlocks.Add(t); - - // advance to next block - _position += blockSize; - } - #endregion - - #region ID 27 - Return from sequence -/* length: 00 - - This block indicates the end of the Called Sequence. The next block played will be the block after the last CALL block (or the next Call, - if the Call block had multiple calls). - Again, this block has no body. */ - private void ProcessBlockID27(byte[] data) - { - // block processing not implemented for this - just gets added for informational purposes only - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x27; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Return_From_Sequence; - t.PauseInMS = 0; - - - // add to tape - _datacorder.DataBlocks.Add(t); - } - #endregion - - #region ID 28 - Select block -/* length: [00,01]+02 - Offset Value Type Description - 0x00 - WORD Length of the whole block (without these two bytes) - 0x02 N BYTE Number of selections - 0x03 - SELECT[N] List of selections - - ---- - SELECT structure format - Offset Value Type Description - 0x00 - WORD Relative Offset - 0x02 L BYTE Length of description text - 0x03 - CHAR[L] Description text (please use single line and max. 30 chars) - ---- - - This block is useful when the tape consists of two or more separately-loadable parts. With this block, you are able to select - one of the parts and the utility/emulator will start loading from that block. For example you can use it when the game has a - separate Trainer or when it is a multiload. Of course, to make some use of it the emulator/utility has to show a menu with the - selections when it encounters such a block. All offsets are relative signed words. */ - private void ProcessBlockID28(byte[] data) - { - // block processing not implemented for this - just gets added for informational purposes only - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x28; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Select_Block; - - int blockSize = 2 + GetWordValue(data, _position); - - t.PauseInMS = 0; - - // add to tape - _datacorder.DataBlocks.Add(t); - - // advance to next block - _position += blockSize; - } - #endregion - - #region ID 2A - Stop the tape if in 48K mode -/* length: 04 - Offset Value Type Description - 0x00 0 DWORD Length of the block without these four bytes (0) - - When this block is encountered, the tape will stop ONLY if the machine is an 48K Spectrum. This block is to be used for - multiloading games that load one level at a time in 48K mode, but load the entire tape at once if in 128K mode. - This block has no body of its own, but follows the extension rule. */ - private void ProcessBlockID2A(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x2A; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Stop_the_Tape_48K; - t.Command = TapeCommand.STOP_THE_TAPE_48K; - - int blockSize = 4 + GetWordValue(data, _position); - - t.PauseInMS = 0; - - // add to tape - _datacorder.DataBlocks.Add(t); - - // advance to next block - _position += blockSize; - } - #endregion - - #region ID 2B - Set signal level -/* length: 05 - Offset Value Type Description - 0x00 1 DWORD Block length (without these four bytes) - 0x04 - BYTE Signal level (0=low, 1=high) - - This block sets the current signal level to the specified value (high or low). It should be used whenever it is necessary to avoid any - ambiguities, e.g. with custom loaders which are level-sensitive. */ - private void ProcessBlockID2B(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x2B; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Set_Signal_Level; - - t.PauseInMS = 0; - - // add to tape - _datacorder.DataBlocks.Add(t); - - // advance to next block - _position += 5; - } - #endregion - - #region ID 30 - Text description -/* length: [00]+01 - Offset Value Type Description - 0x00 N BYTE Length of the text description - 0x01 - CHAR[N] Text description in ASCII format - - This is meant to identify parts of the tape, so you know where level 1 starts, where to rewind to when the game ends, etc. - This description is not guaranteed to be shown while the tape is playing, but can be read while browsing the tape or changing - the tape pointer. - The description can be up to 255 characters long but please keep it down to about 30 so the programs can show it in one line - (where this is appropriate). - Please use 'Archive Info' block for title, authors, publisher, etc. */ - private void ProcessBlockID30(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x30; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Text_Description; - - int textLen = data[_position]; - _position++; - - string desc = Encoding.ASCII.GetString(data, _position, textLen); - - t.PauseInMS = 0; - - //t.BlockDescription = "[" + desc + "]"; - - // add to tape - _datacorder.DataBlocks.Add(t); - - // advance to next block - _position += textLen; - } - #endregion - - #region ID 31 - Message block -/* length: [01]+02 - Offset Value Type Description - 0x00 - BYTE Time (in seconds) for which the message should be displayed - 0x01 N BYTE Length of the text message - 0x02 - CHAR[N] Message that should be displayed in ASCII format - - This will enable the emulators to display a message for a given time. This should not stop the tape and it should not make silence. - If the time is 0 then the emulator should wait for the user to press a key. - The text message should: - stick to a maximum of 30 chars per line; - use single 0x0D (13 decimal) to separate lines; - stick to a maximum of 8 lines. - If you do not obey these rules, emulators may display your message in any way they like. */ - private void ProcessBlockID31(byte[] data) - { - // currently not implemented properly in ZXHawk - - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x31; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Message_Block; - - _position++; - - int msgLen = data[_position]; - _position++; - - string desc = Encoding.ASCII.GetString(data, _position, msgLen); - - t.Command = TapeCommand.SHOW_MESSAGE; - - //t.BlockDescription = "[MESSAGE: " + desc + "]"; - - t.PauseInMS = 0; - - // add to tape - _datacorder.DataBlocks.Add(t); - - // advance to next block - _position += msgLen; - } - #endregion - - #region ID 32 - Archive info -/* length: [00,01]+02 - Offset Value Type Description - 0x00 - WORD Length of the whole block (without these two bytes) - 0x02 N BYTE Number of text strings - 0x03 - TEXT[N] List of text strings - - ---- - TEXT structure format - Offset Value Type Description - 0x00 - BYTE Text identification byte: - 00 - Full title - 01 - Software house/publisher - 02 - Author(s) - 03 - Year of publication - 04 - Language - 05 - Game/utility type - 06 - Price - 07 - Protection scheme/loader - 08 - Origin - FF - Comment(s) - 0x01 L BYTE Length of text string - 0x02 - CHAR[L] Text string in ASCII format - ---- - - Use this block at the beginning of the tape to identify the title of the game, author, publisher, year of publication, price (including - the currency), type of software (arcade adventure, puzzle, word processor, ...), protection scheme it uses (Speedlock 1, Alkatraz, ...) - and its origin (Original, Budget re-release, ...), etc. This block is built in a way that allows easy future expansion. - The block consists of a series of text strings. Each text has its identification number (which tells us what the text means) and then - the ASCII text. To make it possible to skip this block, if needed, the length of the whole block is at the beginning of it. - If all texts on the tape are in English language then you don't have to supply the 'Language' field - The information about what hardware the tape uses is in the 'Hardware Type' block, so no need for it here. */ - private void ProcessBlockID32(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x32; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Archive_Info; - - int blockLen = GetWordValue(data, _position); - _position += 2; - int stringCount = data[_position++]; - - // iterate through each string - for (int s = 0; s < stringCount; s++) - { - // identify the type of text - int type = data[_position++]; - - // get text length - int strLen = data[_position++]; + } + + /// + /// Processes a TZX block + /// + private void ProcessBlock(byte[] data, int id) + { + // process based on detected block ID + switch (id) + { + // ID 10 - Standard Speed Data Block + case 0x10: + ProcessBlockID10(data); + break; + // ID 11 - Turbo Speed Data Block + case 0x11: + ProcessBlockID11(data); + break; + // ID 12 - Pure Tone + case 0x12: + ProcessBlockID12(data); + break; + // ID 13 - Pulse sequence + case 0x13: + ProcessBlockID13(data); + break; + // ID 14 - Pure Data Block + case 0x14: + ProcessBlockID14(data); + break; + // ID 15 - Direct Recording + case 0x15: + ProcessBlockID15(data); + break; + // ID 18 - CSW Recording + case 0x18: + ProcessBlockID18(data); + break; + // ID 19 - Generalized Data Block + case 0x19: + ProcessBlockID19(data); + break; + // ID 20 - Pause (silence) or 'Stop the Tape' command + case 0x20: + ProcessBlockID20(data); + break; + // ID 21 - Group start + case 0x21: + ProcessBlockID21(data); + break; + // ID 22 - Group end + case 0x22: + ProcessBlockID22(data); + break; + // ID 23 - Jump to block + case 0x23: + ProcessBlockID23(data); + break; + // ID 24 - Loop start + case 0x24: + ProcessBlockID24(data); + break; + // ID 25 - Loop end + case 0x25: + ProcessBlockID25(data); + break; + // ID 26 - Call sequence + case 0x26: + ProcessBlockID26(data); + break; + // ID 27 - Return from sequence + case 0x27: + ProcessBlockID27(data); + break; + // ID 28 - Select block + case 0x28: + ProcessBlockID28(data); + break; + // ID 2A - Stop the tape if in 48K mode + case 0x2A: + ProcessBlockID2A(data); + break; + // ID 2B - Set signal level + case 0x2B: + ProcessBlockID2B(data); + break; + // ID 30 - Text description + case 0x30: + ProcessBlockID30(data); + break; + // ID 31 - Message block + case 0x31: + ProcessBlockID31(data); + break; + // ID 32 - Archive info + case 0x32: + ProcessBlockID32(data); + break; + // ID 33 - Hardware type + case 0x33: + ProcessBlockID33(data); + break; + // ID 35 - Custom info block + case 0x35: + ProcessBlockID35(data); + break; + // ID 5A - "Glue" block + case 0x5A: + ProcessBlockID5A(data); + break; + + #region Depreciated Blocks + + // ID 16 - C64 ROM Type Data Block + case 0x16: + ProcessBlockID16(data); + break; + // ID 17 - C64 Turbo Tape Data Block + case 0x17: + ProcessBlockID17(data); + break; + // ID 34 - Emulation info + case 0x34: + ProcessBlockID34(data); + break; + // ID 40 - Snapshot block + case 0x40: + ProcessBlockID40(data); + break; + + #endregion + + default: + ProcessUnidentifiedBlock(data); + break; + } + } + + #region TZX Block Processors + + #region ID 10 - Standard Speed Data Block + /* length: [02,03]+04 + Offset Value Type Description + 0x00 - WORD Pause after this block (ms.) {1000} + 0x02 N WORD Length of data that follow + 0x04 - BYTE[N] Data as in .TAP files + + This block must be replayed with the standard Spectrum ROM timing values - see the values in + curly brackets in block ID 11. The pilot tone consists in 8063 pulses if the first data byte + (flag byte) is < 128, 3223 otherwise. This block can be used for the ROM loading routines AND + for custom loading routines that use the same timings as ROM ones do. */ + private void ProcessBlockID10(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x10; + t.BlockDescription = BlockType.Standard_Speed_Data_Block; + t.DataPeriods = new List(); + + int pauseLen = GetWordValue(data, _position); + if (pauseLen == 0) + pauseLen = 1000; + + t.PauseInMS = pauseLen; + + int blockLen = GetWordValue(data, _position + 2); + + _position += 4; + + byte[] tmp = new byte[blockLen]; + tmp = data.Skip(_position).Take(blockLen).ToArray(); + + var t2 = DecodeDataBlock(t, tmp, DataBlockType.Standard, pauseLen); + + // add the block + _datacorder.DataBlocks.Add(t2); + + // advance the position to the next block + _position += blockLen; + + // generate PAUSE block + CreatePauseBlock(_datacorder.DataBlocks.Last()); + } + #endregion + + #region ID 11 - Turbo Speed Data Block + /* length: [0F,10,11]+12 + Offset Value Type Description + 0x00 - WORD Length of PILOT pulse {2168} + 0x02 - WORD Length of SYNC first pulse {667} + 0x04 - WORD Length of SYNC second pulse {735} + 0x06 - WORD Length of ZERO bit pulse {855} + 0x08 - WORD Length of ONE bit pulse {1710} + 0x0A - WORD Length of PILOT tone (number of pulses) {8063 header (flag<128), 3223 data (flag>=128)} + 0x0C - BYTE Used bits in the last byte (other bits should be 0) {8} + (e.g. if this is 6, then the bits used (x) in the last byte are: xxxxxx00, + where MSb is the leftmost bit, LSb is the rightmost bit) + 0x0D - WORD Pause after this block (ms.) {1000} + 0x0F N BYTE[3] Length of data that follow + 0x12 - BYTE[N] Data as in .TAP files + + This block is very similar to the normal TAP block but with some additional info on the timings and other important + differences. The same tape encoding is used as for the standard speed data block. If a block should use some non-standard + sync or pilot tones (i.e. all sorts of protection schemes) then use the next three blocks to describe it.*/ + private void ProcessBlockID11(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x11; + t.BlockDescription = BlockType.Turbo_Speed_Data_Block; + t.DataPeriods = new List(); + + int pilotPL = GetWordValue(data, _position); + int sync1P = GetWordValue(data, _position + 2); + int sync2P = GetWordValue(data, _position + 4); + int bit0P = GetWordValue(data, _position + 6); + int bit1P = GetWordValue(data, _position + 8); + int pilotTL = GetWordValue(data, _position + 10); + int bitinbyte = data[_position + 12]; + int pause = GetWordValue(data, _position + 13); + + + int blockLen = 0xFFFFFF & GetInt32(data, _position + 0x0F); + + byte[] bLenArr = data.Skip(_position + 0x0F).Take(3).ToArray(); + + _position += 0x12; + + byte[] tmp = new byte[blockLen]; + tmp = data.Skip(_position).Take(blockLen).ToArray(); + + var t2 = DecodeDataBlock(t, tmp, DataBlockType.Turbo, pause, pilotTL, pilotPL, sync1P, sync2P, bit0P, bit1P, bitinbyte); + + t.PauseInMS = pause; + + // add the block + _datacorder.DataBlocks.Add(t2); + + // advance the position to the next block + _position += blockLen; + + // generate PAUSE block + CreatePauseBlock(_datacorder.DataBlocks.Last()); + } + #endregion + + #region ID 12 - Pure Tone + /* length: 04 + Offset Value Type Description + 0x00 - WORD Length of one pulse in T-states + 0x02 - WORD Number of pulses + + This will produce a tone which is basically the same as the pilot tone in the ID 10, ID 11 blocks. You can define how + long the pulse is and how many pulses are in the tone. */ + private void ProcessBlockID12(byte[] data) + { + int blockLen = 4; + + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x12; + t.BlockDescription = BlockType.Pure_Tone; + t.DataPeriods = new List(); + t.PauseInMS = 0; + + // get values + int pulseLength = GetWordValue(data, _position); + int pulseCount = GetWordValue(data, _position + 2); + + t.AddMetaData(BlockDescriptorTitle.Pulse_Length, pulseLength.ToString() + " T-States"); + t.AddMetaData(BlockDescriptorTitle.Pulse_Count, pulseCount.ToString()); + + // build period information + for (int p = 0; p < pulseCount; p++) + { + t.DataPeriods.Add(pulseLength); + } + + // add the block + _datacorder.DataBlocks.Add(t); + + // advance the position to the next block + _position += blockLen; + } + #endregion + + #region ID 13 - Pulse sequence + /* length: [00]*02+01 + Offset Value Type Description + 0x00 N BYTE Number of pulses + 0x01 - WORD[N] Pulses' lengths + + This will produce N pulses, each having its own timing. Up to 255 pulses can be stored in this block; this is useful for non-standard + sync tones used by some protection schemes. */ + private void ProcessBlockID13(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x13; + t.BlockDescription = BlockType.Pulse_Sequence; + t.DataPeriods = new List(); + + t.PauseInMS = 0; + + // get pulse count + int pulseCount = data[_position]; + t.AddMetaData(BlockDescriptorTitle.Pulse_Count, pulseCount.ToString()); + _position++; + + // build period information + for (int p = 0; p < pulseCount; p++, _position += 2) + { + // get pulse length + int pulseLength = GetWordValue(data, _position); + t.AddMetaData(BlockDescriptorTitle.Needs_Parsing, "Pulse " + p + " Length\t" + pulseLength.ToString() + " T-States"); + t.DataPeriods.Add(pulseLength); + } + + // add the block + _datacorder.DataBlocks.Add(t); + } + #endregion + + #region ID 14 - Pure Data Block + /* length: [07,08,09]+0A + Offset Value Type Description + 0x00 - WORD Length of ZERO bit pulse + 0x02 - WORD Length of ONE bit pulse + 0x04 - BYTE Used bits in last byte (other bits should be 0) + (e.g. if this is 6, then the bits used (x) in the last byte are: xxxxxx00, + where MSb is the leftmost bit, LSb is the rightmost bit) + 0x05 - WORD Pause after this block (ms.) + 0x07 N BYTE[3] Length of data that follow + 0x0A - BYTE[N] Data as in .TAP files + + This is the same as in the turbo loading data block, except that it has no pilot or sync pulses. */ + private void ProcessBlockID14(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x14; + t.BlockDescription = BlockType.Pure_Data_Block; + t.DataPeriods = new List(); + + int pilotPL = 0; + int sync1P = 0; + int sync2P = 0; + int bit0P = GetWordValue(data, _position + 0); + int bit1P = GetWordValue(data, _position + 2); + int pilotTL = 0; + int bitinbyte = data[_position + 4]; + int pause = GetWordValue(data, _position + 5); + + int blockLen = 0xFFFFFF & GetInt32(data, _position + 0x07); + + _position += 0x0A; + + byte[] tmp = new byte[blockLen]; + tmp = data.Skip(_position).Take(blockLen).ToArray(); + + var t2 = DecodeDataBlock(t, tmp, DataBlockType.Pure, pause, pilotTL, pilotPL, sync1P, sync2P, bit0P, bit1P, bitinbyte); + + t.PauseInMS = pause; + + // add the block + _datacorder.DataBlocks.Add(t2); + + // advance the position to the next block + _position += blockLen; + + // generate PAUSE block + CreatePauseBlock(_datacorder.DataBlocks.Last()); + } + #endregion + + #region ID 15 - Direct Recording + /* length: [05,06,07]+08 + Offset Value Type Description + 0x00 - WORD Number of T-states per sample (bit of data) + 0x02 - WORD Pause after this block in milliseconds (ms.) + 0x04 - BYTE Used bits (samples) in last byte of data (1-8) + (e.g. if this is 2, only first two samples of the last byte will be played) + 0x05 N BYTE[3] Length of samples' data + 0x08 - BYTE[N] Samples data. Each bit represents a state on the EAR port (i.e. one sample). + MSb is played first. + + This block is used for tapes which have some parts in a format such that the turbo loader block cannot be used. + This is not like a VOC file, since the information is much more compact. Each sample value is represented by one bit only + (0 for low, 1 for high) which means that the block will be at most 1/8 the size of the equivalent VOC. + The preferred sampling frequencies are 22050 or 44100 Hz (158 or 79 T-states/sample). + Please, if you can, don't use other sampling frequencies. + Please use this block only if you cannot use any other block. */ + private void ProcessBlockID15(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x15; + t.BlockDescription = BlockType.Direct_Recording; + t.DataPeriods = new List(); + + // get values + int samLen = GetInt32(data, _position + 5); + int samSize = 0xFFFFFF & samLen; + + int tStatesPerSample = GetWordValue(data, _position); + int pauseAfterBlock = GetWordValue(data, _position + 2); + int usedBitsInLastByte = data[_position + 4]; + + // skip to samples data + _position += 8; + + int pulseLength = 0; + int pulseCount = 0; + + // ascertain the pulse count + for (int i = 0; i < samSize; i++) + { + for (int p = 0x80; p != 0; p >>= 1) + { + if (((data[_position + i] ^ pulseLength) & p) != 0) + { + pulseCount++; + pulseLength ^= -1; + } + } + } + + // get the pulses + t.DataPeriods = new List(pulseCount + 2); + int tStateCount = 0; + pulseLength = 0; + for (int i = 1; i < samSize; i++) + { + for (int p = 0x80; p != 0; p >>= 1) + { + tStateCount += tStatesPerSample; + if (((data[_position] ^ pulseLength) & p) != 0) + { + t.DataPeriods.Add(tStateCount); + pulseLength ^= -1; + tStateCount = 0; + } + } + + // incrememt position + _position++; + } + + // get the pulses in the last byte of data + for (int p = 0x80; p != (byte)(0x80 >> usedBitsInLastByte); p >>= 1) + { + tStateCount += tStatesPerSample; + if (((data[_position] ^ pulseLength) & p) != 0) + { + t.DataPeriods.Add(tStateCount); + pulseLength ^= -1; + tStateCount = 0; + } + } + + // add final pulse + t.DataPeriods.Add(tStateCount); + + // add end of block pause + if (pauseAfterBlock > 0) + { + //t.DataPeriods.Add(3500 * pauseAfterBlock); + } + + t.PauseInMS = pauseAfterBlock; + + // increment position + _position++; + + // add the block + _datacorder.DataBlocks.Add(t); + + // generate PAUSE block + CreatePauseBlock(_datacorder.DataBlocks.Last()); + } + #endregion + + #region ID 18 - CSW Recording + /* length: [00,01,02,03]+04 + Offset Value Type Description + 0x00 10+N DWORD Block length (without these four bytes) + 0x04 - WORD Pause after this block (in ms). + 0x06 - BYTE[3] Sampling rate + 0x09 - BYTE Compression type + 0x01: RLE + 0x02: Z-RLE + 0x0A - DWORD Number of stored pulses (after decompression, for validation purposes) + 0x0E - BYTE[N] CSW data, encoded according to the CSW file format specification. + + This block contains a sequence of raw pulses encoded in CSW format v2 (Compressed Square Wave). */ + private void ProcessBlockID18(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x18; + t.BlockDescription = BlockType.CSW_Recording; + t.DataPeriods = new List(); + + int blockLen = GetInt32(data, _position); + _position += 4; + + t.PauseInMS = GetWordValue(data, _position); + + _position += 2; + + int sampleRate = data[_position++] << 16 | data[_position++] << 8 | data[_position++]; + byte compType = data[_position++]; + int pulses = GetInt32(data, _position); + _position += 4; + + int dataLen = blockLen - 10; + + // build source array + byte[] src = new byte[dataLen]; + // build destination array + byte[] dest = new byte[pulses + 1]; + + // process the CSW data + BizHawk.Emulation.Cores.Computers.SinclairSpectrum.CswConverter.ProcessCSWV2(src, ref dest, compType, pulses); + + // create the periods + var rate = (69888 * 50) / sampleRate; + + for (int i = 0; i < dest.Length;) + { + int length = dest[i++] * rate; + if (length == 0) + { + length = GetInt32(dest, i) / rate; + i += 4; + } + + t.DataPeriods.Add(length); + } + + // add closing period + t.DataPeriods.Add((69888 * 50) / 10); + + _position += dataLen; + //_position += blockLen; + + // add the block + _datacorder.DataBlocks.Add(t); + + // generate PAUSE block + CreatePauseBlock(_datacorder.DataBlocks.Last()); + } + #endregion + + #region ID 19 - Generalized Data Block + /* length: [00,01,02,03]+04 + Offset Value Type Description + 0x00 - DWORD Block length (without these four bytes) + 0x04 - WORD Pause after this block (ms) + 0x06 TOTP DWORD Total number of symbols in pilot/sync block (can be 0) + 0x0A NPP BYTE Maximum number of pulses per pilot/sync symbol + 0x0B ASP BYTE Number of pilot/sync symbols in the alphabet table (0=256) + 0x0C TOTD DWORD Total number of symbols in data stream (can be 0) + 0x10 NPD BYTE Maximum number of pulses per data symbol + 0x11 ASD BYTE Number of data symbols in the alphabet table (0=256) + 0x12 - SYMDEF[ASP] Pilot and sync symbols definition table + This field is present only if TOTP>0 + 0x12+ + (2*NPP+1)*ASP - PRLE[TOTP] Pilot and sync data stream + This field is present only if TOTP>0 + 0x12+ + (TOTP>0)*((2*NPP+1)*ASP)+ + TOTP*3 - SYMDEF[ASD] Data symbols definition table + This field is present only if TOTD>0 + 0x12+ + (TOTP>0)*((2*NPP+1)*ASP)+ + TOTP*3+ + (2*NPD+1)*ASD - BYTE[DS] Data stream + This field is present only if TOTD>0 + + This block has been specifically developed to represent an extremely wide range of data encoding techniques. + The basic idea is that each loading component (pilot tone, sync pulses, data) is associated to a specific sequence + of pulses, where each sequence (wave) can contain a different number of pulses from the others. + In this way we can have a situation where bit 0 is represented with 4 pulses and bit 1 with 8 pulses. + + ---- + SYMDEF structure format + Offset Value Type Description + 0x00 - BYTE Symbol flags + b0-b1: starting symbol polarity + 00: opposite to the current level (make an edge, as usual) - default + 01: same as the current level (no edge - prolongs the previous pulse) + 10: force low level + 11: force high level + 0x01 - WORD[MAXP] Array of pulse lengths. + + The alphabet is stored using a table where each symbol is a row of pulses. The number of columns (i.e. pulses) of the table is the + length of the longest sequence amongst all (MAXP=NPP or NPD, for pilot/sync or data blocks respectively); shorter waves are terminated by a + zero-length pulse in the sequence. + Any number of data symbols is allowed, so we can have more than two distinct waves; for example, imagine a loader which writes two bits at a + time by encoding them with four distinct pulse lengths: this loader would have an alphabet of four symbols, each associated to a specific + sequence of pulses (wave). + ---- + ---- + PRLE structure format + Offset Value Type Description + 0x00 - BYTE Symbol to be represented + 0x01 - WORD Number of repetitions + + Most commonly, pilot and sync are repetitions of the same pulse, thus they are represented using a very simple RLE encoding structure which stores + the symbol and the number of times it must be repeated. + Each symbol in the data stream is represented by a string of NB bits of the block data, where NB = ceiling(Log2(ASD)). + Thus the length of the whole data stream in bits is NB*TOTD, or in bytes DS=ceil(NB*TOTD/8). + ---- */ + private void ProcessBlockID19(byte[] data) + { + // not currently implemented properly + + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x19; + t.BlockDescription = BlockType.Generalized_Data_Block; + t.DataPeriods = new List(); + + int blockLen = GetInt32(data, _position); + _position += 4; + + int pause = GetWordValue(data, _position); + _position += 2; + + int totp = GetInt32(data, _position); + _position += 4; + + int npp = data[_position++]; + + int asp = data[_position++]; + + int totd = GetInt32(data, _position); + _position += 4; + + int npd = data[_position++]; + + int asd = data[_position++]; + + // add the block + _datacorder.DataBlocks.Add(t); + + // advance the position to the next block + _position += blockLen; + } + #endregion + + #region ID 20 - Pause (silence) or 'Stop the Tape' command + /* length: 02 + Offset Value Type Description + 0x00 - WORD Pause duration (ms.) + + This will make a silence (low amplitude level (0)) for a given time in milliseconds. If the value is 0 then the + emulator or utility should (in effect) STOP THE TAPE, i.e. should not continue loading until the user or emulator requests it. */ + private void ProcessBlockID20(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x20; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Pause_or_Stop_the_Tape; + + int pauseDuration = GetWordValue(data, _position); + if (pauseDuration != 0) + { + //t.BlockDescription = "Pause: " + pauseDuration + " ms"; + } + else + { + //t.BlockDescription = "[STOP THE TAPE]"; + } + + t.PauseInMS = pauseDuration; + + if (pauseDuration == 0) + { + // issue stop the tape command + t.Command = TapeCommand.STOP_THE_TAPE; + // add 1ms period + //t.DataPeriods.Add(3500); + //pauseDuration = -1; + + } + else + { + // this is actually just a pause + //pauseDuration = 3500 * pauseDuration; + //t.DataPeriods.Add(pauseDuration); + } + + // add end of block pause + //t.DataPeriods.Add(pauseDuration); + + // add to tape + _datacorder.DataBlocks.Add(t); + + // advanced position to next block + _position += 2; + + // generate PAUSE block + CreatePauseBlock(_datacorder.DataBlocks.Last()); + + } + #endregion + + #region ID 21 - Group start + /* length: [00]+01 + Offset Value Type Description + 0x00 L BYTE Length of the group name string + 0x01 - CHAR[L] Group name in ASCII format (please keep it under 30 characters long) + + This block marks the start of a group of blocks which are to be treated as one single (composite) block. + This is very handy for tapes that use lots of subblocks like Bleepload (which may well have over 160 custom loading blocks). + You can also give the group a name (example 'Bleepload Block 1'). + For each group start block, there must be a group end block. Nesting of groups is not allowed. */ + private void ProcessBlockID21(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x21; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Group_Start; + + int nameLength = data[_position]; + _position++; + + string name = Encoding.ASCII.GetString(data, _position, nameLength); + //t.BlockDescription = "[GROUP: " + name + "]"; + t.Command = TapeCommand.BEGIN_GROUP; + + t.PauseInMS = 0; + + // add to tape + _datacorder.DataBlocks.Add(t); + + // advance to next block + _position += nameLength; + } + #endregion + + #region ID 22 - Group end + /* length: 00 + + This indicates the end of a group. This block has no body. */ + private void ProcessBlockID22(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x22; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Group_End; + t.Command = TapeCommand.END_GROUP; + + t.PauseInMS = 0; + + // add to tape + _datacorder.DataBlocks.Add(t); + } + #endregion + + #region ID 23 - Jump to block + /* length: 02 + Offset Value Type Description + 0x00 - WORD Relative jump value + + This block will enable you to jump from one block to another within the file. The value is a signed short word + (usually 'signed short' in C); Some examples: + Jump 0 = 'Loop Forever' - this should never happen + Jump 1 = 'Go to the next block' - it is like NOP in assembler ;) + Jump 2 = 'Skip one block' + Jump -1 = 'Go to the previous block' + All blocks are included in the block count!. */ + private void ProcessBlockID23(byte[] data) + { + // not implemented properly + + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x23; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Jump_to_Block; + + int relativeJumpValue = GetWordValue(data, _position); + string result = string.Empty; + + switch (relativeJumpValue) + { + case 0: + result = "Loop Forever"; + break; + case 1: + result = "To Next Block"; + break; + case 2: + result = "Skip One Block"; + break; + case -1: + result = "Go to Previous Block"; + break; + } + + //t.BlockDescription = "[JUMP BLOCK - " + result +"]"; + + t.PauseInMS = 0; + + // add to tape + _datacorder.DataBlocks.Add(t); + + // advance to next block + _position += 2; + } + #endregion + + #region ID 24 - Loop start + /* length: 02 + Offset Value Type Description + 0x00 - WORD Number of repetitions (greater than 1) + + If you have a sequence of identical blocks, or of identical groups of blocks, you can use this block to tell how many times they should + be repeated. This block is the same as the FOR statement in BASIC. + For simplicity reasons don't nest loop blocks! */ + private void ProcessBlockID24(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x24; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Loop_Start; + + // loop should start from the next block + int loopStart = _datacorder.DataBlocks.Count() + 1; + + int numberOfRepetitions = GetWordValue(data, _position); + + // update loop counter + _loopCounter.Add( + new KeyValuePair( + loopStart, + numberOfRepetitions)); + + // update description + //t.BlockDescription = "[LOOP START - " + numberOfRepetitions + " times]"; + + t.PauseInMS = 0; + + // add to tape + _datacorder.DataBlocks.Add(t); + + // advance to next block + _position += 2; + } + #endregion + + #region ID 25 - Loop end + /* length: 00 + + This is the same as BASIC's NEXT statement. It means that the utility should jump back to the start of the loop if it hasn't + been run for the specified number of times. + This block has no body. */ + private void ProcessBlockID25(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x25; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Loop_End; + + // get the most recent loop info + var loop = _loopCounter.LastOrDefault(); + + int loopStart = loop.Key; + int numberOfRepetitions = loop.Value; + + if (numberOfRepetitions == 0) + { + return; + } + + // get the number of blocks to loop + int blockCnt = _datacorder.DataBlocks.Count() - loopStart; + + // loop through each group to repeat + for (int b = 0; b < numberOfRepetitions; b++) + { + TapeDataBlock repeater = new TapeDataBlock(); + //repeater.BlockDescription = "[LOOP REPEAT - " + (b + 1) + "]"; + repeater.DataPeriods = new List(); + + // add the repeat block + _datacorder.DataBlocks.Add(repeater); + + // now iterate through and add the blocks to be repeated + for (int i = 0; i < blockCnt; i++) + { + var block = _datacorder.DataBlocks[loopStart + i]; + _datacorder.DataBlocks.Add(block); + } + } + } + #endregion + + #region ID 26 - Call sequence + /* length: [00,01]*02+02 + Offset Value Type Description + 0x00 N WORD Number of calls to be made + 0x02 - WORD[N] Array of call block numbers (relative-signed offsets) + + This block is an analogue of the CALL Subroutine statement. It basically executes a sequence of blocks that are somewhere else and + then goes back to the next block. Because more than one call can be normally used you can include a list of sequences to be called. + The 'nesting' of call blocks is also not allowed for the simplicity reasons. You can, of course, use the CALL blocks in the LOOP + sequences and vice versa. The value is relative for the obvious reasons - so that you can add some blocks in the beginning of the + file without disturbing the call values. Please take a look at 'Jump To Block' for reference on the values. */ + private void ProcessBlockID26(byte[] data) + { + // block processing not implemented for this - just gets added for informational purposes only + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x26; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Call_Sequence; + + int blockSize = 2 + 2 * GetWordValue(data, _position); + t.PauseInMS = 0; + + + // add to tape + _datacorder.DataBlocks.Add(t); + + // advance to next block + _position += blockSize; + } + #endregion + + #region ID 27 - Return from sequence + /* length: 00 + + This block indicates the end of the Called Sequence. The next block played will be the block after the last CALL block (or the next Call, + if the Call block had multiple calls). + Again, this block has no body. */ + private void ProcessBlockID27(byte[] data) + { + // block processing not implemented for this - just gets added for informational purposes only + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x27; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Return_From_Sequence; + t.PauseInMS = 0; + + + // add to tape + _datacorder.DataBlocks.Add(t); + } + #endregion + + #region ID 28 - Select block + /* length: [00,01]+02 + Offset Value Type Description + 0x00 - WORD Length of the whole block (without these two bytes) + 0x02 N BYTE Number of selections + 0x03 - SELECT[N] List of selections + + ---- + SELECT structure format + Offset Value Type Description + 0x00 - WORD Relative Offset + 0x02 L BYTE Length of description text + 0x03 - CHAR[L] Description text (please use single line and max. 30 chars) + ---- + + This block is useful when the tape consists of two or more separately-loadable parts. With this block, you are able to select + one of the parts and the utility/emulator will start loading from that block. For example you can use it when the game has a + separate Trainer or when it is a multiload. Of course, to make some use of it the emulator/utility has to show a menu with the + selections when it encounters such a block. All offsets are relative signed words. */ + private void ProcessBlockID28(byte[] data) + { + // block processing not implemented for this - just gets added for informational purposes only + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x28; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Select_Block; + + int blockSize = 2 + GetWordValue(data, _position); + + t.PauseInMS = 0; + + // add to tape + _datacorder.DataBlocks.Add(t); + + // advance to next block + _position += blockSize; + } + #endregion + + #region ID 2A - Stop the tape if in 48K mode + /* length: 04 + Offset Value Type Description + 0x00 0 DWORD Length of the block without these four bytes (0) + + When this block is encountered, the tape will stop ONLY if the machine is an 48K Spectrum. This block is to be used for + multiloading games that load one level at a time in 48K mode, but load the entire tape at once if in 128K mode. + This block has no body of its own, but follows the extension rule. */ + private void ProcessBlockID2A(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x2A; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Stop_the_Tape_48K; + t.Command = TapeCommand.STOP_THE_TAPE_48K; + + int blockSize = 4 + GetWordValue(data, _position); + + t.PauseInMS = 0; + + // add to tape + _datacorder.DataBlocks.Add(t); + + // advance to next block + _position += blockSize; + } + #endregion + + #region ID 2B - Set signal level + /* length: 05 + Offset Value Type Description + 0x00 1 DWORD Block length (without these four bytes) + 0x04 - BYTE Signal level (0=low, 1=high) + + This block sets the current signal level to the specified value (high or low). It should be used whenever it is necessary to avoid any + ambiguities, e.g. with custom loaders which are level-sensitive. */ + private void ProcessBlockID2B(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x2B; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Set_Signal_Level; + + t.PauseInMS = 0; + + // add to tape + _datacorder.DataBlocks.Add(t); + + // advance to next block + _position += 5; + } + #endregion + + #region ID 30 - Text description + /* length: [00]+01 + Offset Value Type Description + 0x00 N BYTE Length of the text description + 0x01 - CHAR[N] Text description in ASCII format + + This is meant to identify parts of the tape, so you know where level 1 starts, where to rewind to when the game ends, etc. + This description is not guaranteed to be shown while the tape is playing, but can be read while browsing the tape or changing + the tape pointer. + The description can be up to 255 characters long but please keep it down to about 30 so the programs can show it in one line + (where this is appropriate). + Please use 'Archive Info' block for title, authors, publisher, etc. */ + private void ProcessBlockID30(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x30; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Text_Description; + + int textLen = data[_position]; + _position++; + + string desc = Encoding.ASCII.GetString(data, _position, textLen); + + t.PauseInMS = 0; + + //t.BlockDescription = "[" + desc + "]"; + + // add to tape + _datacorder.DataBlocks.Add(t); + + // advance to next block + _position += textLen; + } + #endregion + + #region ID 31 - Message block + /* length: [01]+02 + Offset Value Type Description + 0x00 - BYTE Time (in seconds) for which the message should be displayed + 0x01 N BYTE Length of the text message + 0x02 - CHAR[N] Message that should be displayed in ASCII format + + This will enable the emulators to display a message for a given time. This should not stop the tape and it should not make silence. + If the time is 0 then the emulator should wait for the user to press a key. + The text message should: + stick to a maximum of 30 chars per line; + use single 0x0D (13 decimal) to separate lines; + stick to a maximum of 8 lines. + If you do not obey these rules, emulators may display your message in any way they like. */ + private void ProcessBlockID31(byte[] data) + { + // currently not implemented properly in ZXHawk + + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x31; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Message_Block; + + _position++; + + int msgLen = data[_position]; + _position++; + + string desc = Encoding.ASCII.GetString(data, _position, msgLen); + + t.Command = TapeCommand.SHOW_MESSAGE; + + //t.BlockDescription = "[MESSAGE: " + desc + "]"; + + t.PauseInMS = 0; + + // add to tape + _datacorder.DataBlocks.Add(t); + + // advance to next block + _position += msgLen; + } + #endregion + + #region ID 32 - Archive info + /* length: [00,01]+02 + Offset Value Type Description + 0x00 - WORD Length of the whole block (without these two bytes) + 0x02 N BYTE Number of text strings + 0x03 - TEXT[N] List of text strings + + ---- + TEXT structure format + Offset Value Type Description + 0x00 - BYTE Text identification byte: + 00 - Full title + 01 - Software house/publisher + 02 - Author(s) + 03 - Year of publication + 04 - Language + 05 - Game/utility type + 06 - Price + 07 - Protection scheme/loader + 08 - Origin + FF - Comment(s) + 0x01 L BYTE Length of text string + 0x02 - CHAR[L] Text string in ASCII format + ---- + + Use this block at the beginning of the tape to identify the title of the game, author, publisher, year of publication, price (including + the currency), type of software (arcade adventure, puzzle, word processor, ...), protection scheme it uses (Speedlock 1, Alkatraz, ...) + and its origin (Original, Budget re-release, ...), etc. This block is built in a way that allows easy future expansion. + The block consists of a series of text strings. Each text has its identification number (which tells us what the text means) and then + the ASCII text. To make it possible to skip this block, if needed, the length of the whole block is at the beginning of it. + If all texts on the tape are in English language then you don't have to supply the 'Language' field + The information about what hardware the tape uses is in the 'Hardware Type' block, so no need for it here. */ + private void ProcessBlockID32(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x32; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Archive_Info; + + int blockLen = GetWordValue(data, _position); + _position += 2; + int stringCount = data[_position++]; + + // iterate through each string + for (int s = 0; s < stringCount; s++) + { + // identify the type of text + int type = data[_position++]; + + // get text length + int strLen = data[_position++]; string title = string.Empty; title = "Info: "; - switch (type) - { - case 0x00: - title = "Full Title: "; - break; - case 0x01: - title = "Software House/Publisher: "; - break; - case 0x02: - title = "Author(s): "; - break; - case 0x03: - title = "Year of Publication: "; - break; - case 0x04: - title = "Language: "; - break; - case 0x05: - title = "Game/Utility Type: "; - break; - case 0x06: - title = "Price: "; - break; - case 0x07: - title = "Protection Scheme/Loader: "; - break; - case 0x08: - title = "Origin: "; - break; - case 0xFF: - title = "Comment(s): "; - break; - default: - break; - } + switch (type) + { + case 0x00: + title = "Full Title: "; + break; + case 0x01: + title = "Software House/Publisher: "; + break; + case 0x02: + title = "Author(s): "; + break; + case 0x03: + title = "Year of Publication: "; + break; + case 0x04: + title = "Language: "; + break; + case 0x05: + title = "Game/Utility Type: "; + break; + case 0x06: + title = "Price: "; + break; + case 0x07: + title = "Protection Scheme/Loader: "; + break; + case 0x08: + title = "Origin: "; + break; + case 0xFF: + title = "Comment(s): "; + break; + default: + break; + } - // add title to description - //t.BlockDescription += title; + // add title to description + //t.BlockDescription += title; - // get string data - string val = Encoding.ASCII.GetString(data, _position, strLen); - //t.BlockDescription += val + " \n"; + // get string data + string val = Encoding.ASCII.GetString(data, _position, strLen); + //t.BlockDescription += val + " \n"; - t.PauseInMS = 0; + t.PauseInMS = 0; - // advance to next string block - _position += strLen; - } + // advance to next string block + _position += strLen; + } - // add to tape - _datacorder.DataBlocks.Add(t); - } - #endregion + // add to tape + _datacorder.DataBlocks.Add(t); + } + #endregion - #region ID 33 - Hardware type -/* length: [00]*03+01 - Offset Value Type Description - 0x00 N BYTE Number of machines and hardware types for which info is supplied - 0x01 - HWINFO[N] List of machines and hardware + #region ID 33 - Hardware type + /* length: [00]*03+01 + Offset Value Type Description + 0x00 N BYTE Number of machines and hardware types for which info is supplied + 0x01 - HWINFO[N] List of machines and hardware - ---- - HWINFO structure format - Offset Value Type Description - 0x00 - BYTE Hardware type - 0x01 - BYTE Hardware ID - 0x02 - BYTE Hardware information: - 00 - The tape RUNS on this machine or with this hardware, - but may or may not use the hardware or special features of the machine. - 01 - The tape USES the hardware or special features of the machine, - such as extra memory or a sound chip. - 02 - The tape RUNS but it DOESN'T use the hardware - or special features of the machine. - 03 - The tape DOESN'T RUN on this machine or with this hardware. - ---- + ---- + HWINFO structure format + Offset Value Type Description + 0x00 - BYTE Hardware type + 0x01 - BYTE Hardware ID + 0x02 - BYTE Hardware information: + 00 - The tape RUNS on this machine or with this hardware, + but may or may not use the hardware or special features of the machine. + 01 - The tape USES the hardware or special features of the machine, + such as extra memory or a sound chip. + 02 - The tape RUNS but it DOESN'T use the hardware + or special features of the machine. + 03 - The tape DOESN'T RUN on this machine or with this hardware. + ---- - This blocks contains information about the hardware that the programs on this tape use. Please include only machines and hardware for - which you are 100% sure that it either runs (or doesn't run) on or with, or you know it uses (or doesn't use) the hardware or special - features of that machine. - If the tape runs only on the ZX81 (and TS1000, etc.) then it clearly won't work on any Spectrum or Spectrum variant, so there's no - need to list this information. - If you are not sure or you haven't tested a tape on some particular machine/hardware combination then do not include it in the list. - The list of hardware types and IDs is somewhat large, and may be found at the end of the format description. */ - private void ProcessBlockID33(byte[] data) - { - // currently not implemented properly in ZXHawk + This blocks contains information about the hardware that the programs on this tape use. Please include only machines and hardware for + which you are 100% sure that it either runs (or doesn't run) on or with, or you know it uses (or doesn't use) the hardware or special + features of that machine. + If the tape runs only on the ZX81 (and TS1000, etc.) then it clearly won't work on any Spectrum or Spectrum variant, so there's no + need to list this information. + If you are not sure or you haven't tested a tape on some particular machine/hardware combination then do not include it in the list. + The list of hardware types and IDs is somewhat large, and may be found at the end of the format description. */ + private void ProcessBlockID33(byte[] data) + { + // currently not implemented properly in ZXHawk - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x33; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Hardware_Type; + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x33; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Hardware_Type; - t.PauseInMS = 0; + t.PauseInMS = 0; - // first byte contains number of HWINFOs - int infos = data[_position]; + // first byte contains number of HWINFOs + int infos = data[_position]; - _position += 1; + _position += 1; - // now starts the HW infos (each block 3 bytes) - for (int i = 0; i < infos; i++) - { - _position += 3; - } + // now starts the HW infos (each block 3 bytes) + for (int i = 0; i < infos; i++) + { + _position += 3; + } - // add to tape - _datacorder.DataBlocks.Add(t); - } - #endregion + // add to tape + _datacorder.DataBlocks.Add(t); + } + #endregion - #region ID 35 - Custom info block -/* length: [10,11,12,13]+14 - Offset Value Type Description - 0x00 - CHAR[10] Identification string (in ASCII) - 0x10 L DWORD Length of the custom info - 0x14 - BYTE[L] Custom info + #region ID 35 - Custom info block + /* length: [10,11,12,13]+14 + Offset Value Type Description + 0x00 - CHAR[10] Identification string (in ASCII) + 0x10 L DWORD Length of the custom info + 0x14 - BYTE[L] Custom info - This block can be used to save any information you want. For example, it might contain some information written by a utility, - extra settings required by a particular emulator, or even poke data. */ - private void ProcessBlockID35(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x35; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Custom_Info_Block; + This block can be used to save any information you want. For example, it might contain some information written by a utility, + extra settings required by a particular emulator, or even poke data. */ + private void ProcessBlockID35(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x35; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Custom_Info_Block; - t.PauseInMS = 0; + t.PauseInMS = 0; - string info = Encoding.ASCII.GetString(data, _position, 0x10); - //t.BlockDescription = "[CUSTOM INFO: " + info + "]"; - _position += 0x10; + string info = Encoding.ASCII.GetString(data, _position, 0x10); + //t.BlockDescription = "[CUSTOM INFO: " + info + "]"; + _position += 0x10; - int blockLen = BitConverter.ToInt32(data, _position); - _position += 4; + int blockLen = BitConverter.ToInt32(data, _position); + _position += 4; - // add to tape - _datacorder.DataBlocks.Add(t); + // add to tape + _datacorder.DataBlocks.Add(t); - // advance to next block - _position += blockLen; - } - #endregion + // advance to next block + _position += blockLen; + } + #endregion - #region ID 5A - "Glue" block -/* length: 09 - Offset Value Type Description - 0x00 - BYTE[9] Value: { "XTape!",0x1A,MajR,MinR } - Just skip these 9 bytes and you will end up on the next ID. + #region ID 5A - "Glue" block + /* length: 09 + Offset Value Type Description + 0x00 - BYTE[9] Value: { "XTape!",0x1A,MajR,MinR } + Just skip these 9 bytes and you will end up on the next ID. - This block is generated when you merge two ZX Tape files together. It is here so that you can easily copy the files together and use - them. Of course, this means that resulting file would be 10 bytes longer than if this block was not used. All you have to do - if you encounter this block ID is to skip next 9 bytes. - If you can avoid using this block for this purpose, then do so; it is preferable to use a utility to join the two files and - ensure that they are both of the higher version number. */ - private void ProcessBlockID5A(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x5A; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Glue_Block; + This block is generated when you merge two ZX Tape files together. It is here so that you can easily copy the files together and use + them. Of course, this means that resulting file would be 10 bytes longer than if this block was not used. All you have to do + if you encounter this block ID is to skip next 9 bytes. + If you can avoid using this block for this purpose, then do so; it is preferable to use a utility to join the two files and + ensure that they are both of the higher version number. */ + private void ProcessBlockID5A(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x5A; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Glue_Block; - t.PauseInMS = 0; + t.PauseInMS = 0; - // add to tape - _datacorder.DataBlocks.Add(t); + // add to tape + _datacorder.DataBlocks.Add(t); - // advance to next block - _position += 9; - } - #endregion + // advance to next block + _position += 9; + } + #endregion - #region UnDetected Blocks + #region UnDetected Blocks - private void ProcessUnidentifiedBlock(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = -2; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Unsupported; - //t.BlockDescription = "[UNSUPPORTED - 0x" + data[_position - 1] + "]"; + private void ProcessUnidentifiedBlock(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = -2; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Unsupported; + //t.BlockDescription = "[UNSUPPORTED - 0x" + data[_position - 1] + "]"; - _position += GetInt32(data, _position) & 0xFFFFFF; + _position += GetInt32(data, _position) & 0xFFFFFF; - // add to tape - _datacorder.DataBlocks.Add(t); + // add to tape + _datacorder.DataBlocks.Add(t); - // advance to next block - _position += 4; - } + // advance to next block + _position += 4; + } - #endregion + #endregion - #region Depreciated Blocks + #region Depreciated Blocks - // These mostly should be ignored by ZXHawk - here for completeness + // These mostly should be ignored by ZXHawk - here for completeness - #region ID 16 - C64 ROM Type Data Block - private void ProcessBlockID16(byte[] data) - { - // zxhawk will not implement this block. it will however handle it so subsequent blocks can be parsed - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x16; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.C64_ROM_Type_Data_Block; + #region ID 16 - C64 ROM Type Data Block + private void ProcessBlockID16(byte[] data) + { + // zxhawk will not implement this block. it will however handle it so subsequent blocks can be parsed + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x16; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.C64_ROM_Type_Data_Block; - t.PauseInMS = 0; + t.PauseInMS = 0; - // add to tape - _datacorder.DataBlocks.Add(t); + // add to tape + _datacorder.DataBlocks.Add(t); - // advance to next block - int blockLen = GetInt32(data, _position); - _position += blockLen; - } - #endregion + // advance to next block + int blockLen = GetInt32(data, _position); + _position += blockLen; + } + #endregion - #region ID 17 - C64 Turbo Tape Data Block - private void ProcessBlockID17(byte[] data) - { - // zxhawk will not implement this block. it will however handle it so subsequent blocks can be parsed - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x17; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.C64_Turbo_Tape_Data_Block; + #region ID 17 - C64 Turbo Tape Data Block + private void ProcessBlockID17(byte[] data) + { + // zxhawk will not implement this block. it will however handle it so subsequent blocks can be parsed + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x17; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.C64_Turbo_Tape_Data_Block; - t.PauseInMS = 0; + t.PauseInMS = 0; - // add to tape - _datacorder.DataBlocks.Add(t); + // add to tape + _datacorder.DataBlocks.Add(t); - // advance to next block - int blockLen = GetInt32(data, _position); - _position += blockLen; - } - #endregion + // advance to next block + int blockLen = GetInt32(data, _position); + _position += blockLen; + } + #endregion - #region ID 34 - Emulation info - private void ProcessBlockID34(byte[] data) - { - // currently not implemented properly in ZXHawk + #region ID 34 - Emulation info + private void ProcessBlockID34(byte[] data) + { + // currently not implemented properly in ZXHawk - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x34; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Emulation_Info; + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x34; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Emulation_Info; - // add to tape - _datacorder.DataBlocks.Add(t); + // add to tape + _datacorder.DataBlocks.Add(t); - // advance to next block - _position += 8; - } - #endregion + // advance to next block + _position += 8; + } + #endregion - #region ID 40 - Snapshot block - /* length: [01,02,03]+04 + #region ID 40 - Snapshot block + /* length: [01,02,03]+04 Offset Value Type Description 0x00 - BYTE Snapshot type: 00: .Z80 format @@ -1635,121 +1635,121 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC Only .Z80 and .SNA snapshots are supported for compatibility reasons! The emulator should take care of that the snapshot is not taken while the actual Tape loading is taking place (which doesn't do much sense). And when an emulator encounters the snapshot block it should load it and then continue with the next block. */ - private void ProcessBlockID40(byte[] data) - { - // currently not implemented properly in ZXHawk + private void ProcessBlockID40(byte[] data) + { + // currently not implemented properly in ZXHawk - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x40; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Snapshot_Block; + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x40; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Snapshot_Block; - _position++; + _position++; - int blockLen = data[_position] | - data[_position + 1] << 8 | - data[_position + 2] << 16; - _position += 3; + int blockLen = data[_position] | + data[_position + 1] << 8 | + data[_position + 2] << 16; + _position += 3; - // add to tape - _datacorder.DataBlocks.Add(t); + // add to tape + _datacorder.DataBlocks.Add(t); - // advance to next block - _position += blockLen; - } - #endregion + // advance to next block + _position += blockLen; + } + #endregion - #endregion + #endregion - #endregion + #endregion - #region DataBlockDecoder + #region DataBlockDecoder - /// - /// Used to process either a standard or turbo data block - /// - private TapeDataBlock DecodeDataBlock - ( - TapeDataBlock block, - byte[] blockdata, - DataBlockType dataBlockType, - int pauseAfterBlock, - int pilotCount, + /// + /// Used to process either a standard or turbo data block + /// + private TapeDataBlock DecodeDataBlock + ( + TapeDataBlock block, + byte[] blockdata, + DataBlockType dataBlockType, + int pauseAfterBlock, + int pilotCount, - int pilotToneLength = 2168, - int sync1PulseLength = 667, - int sync2PulseLength = 735, - int bit0PulseLength = 855, - int bit1PulseLength = 1710, - int bitsInLastByte = 8 - ) - { + int pilotToneLength = 2168, + int sync1PulseLength = 667, + int sync2PulseLength = 735, + int bit0PulseLength = 855, + int bit1PulseLength = 1710, + int bitsInLastByte = 8 + ) + { - // first get the block description - string description = string.Empty; + // first get the block description + string description = string.Empty; - // process the type byte - /* (The type is 0,1,2 or 3 for a Program, Number array, Character array or Code file. + // process the type byte + /* (The type is 0,1,2 or 3 for a Program, Number array, Character array or Code file. A SCREEN$ file is regarded as a Code file with start address 16384 and length 6912 decimal. If the file is a Program file, parameter 1 holds the autostart line number (or a number >=32768 if no LINE parameter was given) and parameter 2 holds the start of the variable area relative to the start of the program. If it's a Code file, parameter 1 holds the start of the code block when saved, and parameter 2 holds 32768. For data files finally, the byte at position 14 decimal holds the variable name.) */ - int blockSize = blockdata.Length; + int blockSize = blockdata.Length; - // dont get description info for Pure Data Blocks - if (dataBlockType != DataBlockType.Pure) - { - if (blockdata[0] == 0x00 && blockSize == 19) - { - string fileName = Encoding.ASCII.GetString(blockdata.Skip(2).Take(10).ToArray()).Trim(); - string type = "Unknown Type"; - StringBuilder sb = new StringBuilder(); + // dont get description info for Pure Data Blocks + if (dataBlockType != DataBlockType.Pure) + { + if (blockdata[0] == 0x00 && blockSize == 19) + { + string fileName = Encoding.ASCII.GetString(blockdata.Skip(2).Take(10).ToArray()).Trim(); + string type = "Unknown Type"; + StringBuilder sb = new StringBuilder(); - var param1 = GetWordValue(blockdata, 12); - var param2 = GetWordValue(blockdata, 14); + var param1 = GetWordValue(blockdata, 12); + var param2 = GetWordValue(blockdata, 14); - // header block - examine first byte of header - if (blockdata[1] == 0) - { - type = "Program"; - sb.Append(type + ": "); - sb.Append(fileName + " "); - } - else if (blockdata[1] == 1) - { - type = "NumArray"; - sb.Append(type + ": "); - sb.Append(fileName + " "); - } - else if (blockdata[1] == 2) - { - type = "CharArray"; - sb.Append(type + ": "); - sb.Append(fileName + " "); - } - else if (blockdata[1] == 3) - { - type = "Code"; - sb.Append(type + ": "); - sb.Append(fileName + " "); - } - } - else if (blockdata[0] == 0xff) - { - // data block - description = "Data Block " + (blockSize - 2) + "bytes"; - block.AddMetaData(BlockDescriptorTitle.Data_Bytes, (blockSize - 2).ToString() + " Bytes"); - } - else - { - // some other type (turbo data etc..) - description = $"#{blockdata[0].ToString("X2")} block, {blockSize} bytes"; - //description += (crc != 0) ? $", crc bad (#{crcFile:X2}!=#{crcValue:X2})" : ", crc ok"; - block.AddMetaData(BlockDescriptorTitle.Undefined, description); - } - /* + // header block - examine first byte of header + if (blockdata[1] == 0) + { + type = "Program"; + sb.Append(type + ": "); + sb.Append(fileName + " "); + } + else if (blockdata[1] == 1) + { + type = "NumArray"; + sb.Append(type + ": "); + sb.Append(fileName + " "); + } + else if (blockdata[1] == 2) + { + type = "CharArray"; + sb.Append(type + ": "); + sb.Append(fileName + " "); + } + else if (blockdata[1] == 3) + { + type = "Code"; + sb.Append(type + ": "); + sb.Append(fileName + " "); + } + } + else if (blockdata[0] == 0xff) + { + // data block + description = "Data Block " + (blockSize - 2) + "bytes"; + block.AddMetaData(BlockDescriptorTitle.Data_Bytes, (blockSize - 2).ToString() + " Bytes"); + } + else + { + // some other type (turbo data etc..) + description = $"#{blockdata[0].ToString("X2")} block, {blockSize} bytes"; + //description += (crc != 0) ? $", crc bad (#{crcFile:X2}!=#{crcValue:X2})" : ", crc ok"; + block.AddMetaData(BlockDescriptorTitle.Undefined, description); + } + /* if (blockdata[0] == 0x00 && blockSize == 19 && (blockdata[1] == 0x00) || (blockdata[1] == 3 && blockdata.Length > 3)) { if (dataBlockType != DataBlockType.Turbo) @@ -1792,124 +1792,124 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC //description += (crc != 0) ? $", crc bad (#{crcFile:X2}!=#{crcValue:X2})" : ", crc ok"; block.AddMetaData(BlockDescriptorTitle.Undefined, description); } - */ - } + */ + } - // update metadata - switch (dataBlockType) - { - case DataBlockType.Standard: - case DataBlockType.Turbo: + // update metadata + switch (dataBlockType) + { + case DataBlockType.Standard: + case DataBlockType.Turbo: - if (dataBlockType == DataBlockType.Standard) - block.BlockDescription = BlockType.Standard_Speed_Data_Block; - if (dataBlockType == DataBlockType.Turbo) - block.BlockDescription = BlockType.Turbo_Speed_Data_Block; + if (dataBlockType == DataBlockType.Standard) + block.BlockDescription = BlockType.Standard_Speed_Data_Block; + if (dataBlockType == DataBlockType.Turbo) + block.BlockDescription = BlockType.Turbo_Speed_Data_Block; - block.AddMetaData(BlockDescriptorTitle.Pilot_Pulse_Length, pilotToneLength.ToString() + " T-States"); - block.AddMetaData(BlockDescriptorTitle.Pilot_Pulse_Count, pilotCount.ToString() + " Pulses"); - block.AddMetaData(BlockDescriptorTitle.First_Sync_Length, sync1PulseLength.ToString() + " T-States"); - block.AddMetaData(BlockDescriptorTitle.Second_Sync_Length, sync2PulseLength.ToString() + " T-States"); - break; + block.AddMetaData(BlockDescriptorTitle.Pilot_Pulse_Length, pilotToneLength.ToString() + " T-States"); + block.AddMetaData(BlockDescriptorTitle.Pilot_Pulse_Count, pilotCount.ToString() + " Pulses"); + block.AddMetaData(BlockDescriptorTitle.First_Sync_Length, sync1PulseLength.ToString() + " T-States"); + block.AddMetaData(BlockDescriptorTitle.Second_Sync_Length, sync2PulseLength.ToString() + " T-States"); + break; - case DataBlockType.Pure: - block.BlockDescription = BlockType.Pure_Data_Block; - break; - } + case DataBlockType.Pure: + block.BlockDescription = BlockType.Pure_Data_Block; + break; + } - block.AddMetaData(BlockDescriptorTitle.Zero_Bit_Length, bit0PulseLength.ToString() + " T-States"); - block.AddMetaData(BlockDescriptorTitle.One_Bit_Length, bit1PulseLength.ToString() + " T-States"); - block.AddMetaData(BlockDescriptorTitle.Data_Length, blockSize.ToString() + " Bytes"); - block.AddMetaData(BlockDescriptorTitle.Bits_In_Last_Byte, bitsInLastByte.ToString() + " Bits"); - block.AddMetaData(BlockDescriptorTitle.Pause_After_Data, pauseAfterBlock.ToString() + " ms"); + block.AddMetaData(BlockDescriptorTitle.Zero_Bit_Length, bit0PulseLength.ToString() + " T-States"); + block.AddMetaData(BlockDescriptorTitle.One_Bit_Length, bit1PulseLength.ToString() + " T-States"); + block.AddMetaData(BlockDescriptorTitle.Data_Length, blockSize.ToString() + " Bytes"); + block.AddMetaData(BlockDescriptorTitle.Bits_In_Last_Byte, bitsInLastByte.ToString() + " Bits"); + block.AddMetaData(BlockDescriptorTitle.Pause_After_Data, pauseAfterBlock.ToString() + " ms"); - // calculate period information - List dataPeriods = new List(); + // calculate period information + List dataPeriods = new List(); - // generate pilot pulses + // generate pilot pulses - if (pilotCount > 0) - { - for (int i = 0; i < pilotCount; i++) - { - dataPeriods.Add(pilotToneLength); - } + if (pilotCount > 0) + { + for (int i = 0; i < pilotCount; i++) + { + dataPeriods.Add(pilotToneLength); + } - // add syncro pulses - dataPeriods.Add(sync1PulseLength); - dataPeriods.Add(sync2PulseLength); - } + // add syncro pulses + dataPeriods.Add(sync1PulseLength); + dataPeriods.Add(sync2PulseLength); + } - int pos = 0; + int pos = 0; - // add bit0 and bit1 periods - for (int i = 0; i < blockSize - 1; i++, pos++) - { - for (byte b = 0x80; b != 0; b >>= 1) - { - if ((blockdata[i] & b) != 0) - dataPeriods.Add(bit1PulseLength); - else - dataPeriods.Add(bit0PulseLength); - if ((blockdata[i] & b) != 0) - dataPeriods.Add(bit1PulseLength); - else - dataPeriods.Add(bit0PulseLength); - } - } + // add bit0 and bit1 periods + for (int i = 0; i < blockSize - 1; i++, pos++) + { + for (byte b = 0x80; b != 0; b >>= 1) + { + if ((blockdata[i] & b) != 0) + dataPeriods.Add(bit1PulseLength); + else + dataPeriods.Add(bit0PulseLength); + if ((blockdata[i] & b) != 0) + dataPeriods.Add(bit1PulseLength); + else + dataPeriods.Add(bit0PulseLength); + } + } - // add the last byte - for (byte c = 0x80; c != (byte)(0x80 >> bitsInLastByte); c >>= 1) - { - if ((blockdata[pos] & c) != 0) - dataPeriods.Add(bit1PulseLength); - else - dataPeriods.Add(bit0PulseLength); - if ((blockdata[pos] & c) != 0) - dataPeriods.Add(bit1PulseLength); - else - dataPeriods.Add(bit0PulseLength); - } + // add the last byte + for (byte c = 0x80; c != (byte)(0x80 >> bitsInLastByte); c >>= 1) + { + if ((blockdata[pos] & c) != 0) + dataPeriods.Add(bit1PulseLength); + else + dataPeriods.Add(bit0PulseLength); + if ((blockdata[pos] & c) != 0) + dataPeriods.Add(bit1PulseLength); + else + dataPeriods.Add(bit0PulseLength); + } - // add block pause if pause is not 0 - if (pauseAfterBlock != 0) - { - block.PauseInMS = pauseAfterBlock; - //int actualPause = pauseAfterBlock * 3500; - //dataPeriods.Add(actualPause); - } + // add block pause if pause is not 0 + if (pauseAfterBlock != 0) + { + block.PauseInMS = pauseAfterBlock; + //int actualPause = pauseAfterBlock * 3500; + //dataPeriods.Add(actualPause); + } - // add to the tapedatablock object - block.DataPeriods = dataPeriods; + // add to the tapedatablock object + block.DataPeriods = dataPeriods; - // add the raw data - block.BlockData = blockdata; + // add the raw data + block.BlockData = blockdata; - return block; - } + return block; + } - /// - /// Used to process either a standard or turbo data block - /// - private TapeDataBlock DecodeDataBlock - ( - TapeDataBlock block, - byte[] blockData, - DataBlockType dataBlockType, - int pauseAfterBlock, + /// + /// Used to process either a standard or turbo data block + /// + private TapeDataBlock DecodeDataBlock + ( + TapeDataBlock block, + byte[] blockData, + DataBlockType dataBlockType, + int pauseAfterBlock, - int pilotToneLength = 2168, - int sync1PulseLength = 667, - int sync2PulseLength = 735, - int bit0PulseLength = 855, - int bit1PulseLength = 1710, - int bitsInLastByte = 8 - ) - { + int pilotToneLength = 2168, + int sync1PulseLength = 667, + int sync2PulseLength = 735, + int bit0PulseLength = 855, + int bit1PulseLength = 1710, + int bitsInLastByte = 8 + ) + { - - int pilotCount = 3220; - /* + + int pilotCount = 3220; + /* // pilot count needs to be ascertained from flag byte int pilotCount; if (blockData[0] < 128) @@ -1918,70 +1918,70 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC pilotCount = 3223; */ - // now we can decode - var nBlock = DecodeDataBlock - ( - block, - blockData, - dataBlockType, - pauseAfterBlock, - pilotCount, - pilotToneLength, - sync1PulseLength, - sync2PulseLength, - bit0PulseLength, - bit1PulseLength, - bitsInLastByte - ); + // now we can decode + var nBlock = DecodeDataBlock + ( + block, + blockData, + dataBlockType, + pauseAfterBlock, + pilotCount, + pilotToneLength, + sync1PulseLength, + sync2PulseLength, + bit0PulseLength, + bit1PulseLength, + bitsInLastByte + ); - return nBlock; - } + return nBlock; + } - #endregion + #endregion - #region Pause Block Creator + #region Pause Block Creator - /// - /// If neccessary a seperate PAUSE block will be created - /// - private void CreatePauseBlock(TapeDataBlock original) - { - if (original.PauseInMS > 0) - { - TapeDataBlock pBlock = new TapeDataBlock(); - pBlock.DataPeriods = new List(); - pBlock.BlockDescription = BlockType.PAUSE_BLOCK; - pBlock.PauseInMS = 0; - var pauseInTStates = TranslatePause(original.PauseInMS); + /// + /// If neccessary a seperate PAUSE block will be created + /// + private void CreatePauseBlock(TapeDataBlock original) + { + if (original.PauseInMS > 0) + { + TapeDataBlock pBlock = new TapeDataBlock(); + pBlock.DataPeriods = new List(); + pBlock.BlockDescription = BlockType.PAUSE_BLOCK; + pBlock.PauseInMS = 0; + var pauseInTStates = TranslatePause(original.PauseInMS); - pBlock.AddMetaData(BlockDescriptorTitle.Block_ID, pauseInTStates.ToString() + " cycles"); + pBlock.AddMetaData(BlockDescriptorTitle.Block_ID, pauseInTStates.ToString() + " cycles"); - int by1000 = pauseInTStates / 70000; - int rem1000 = pauseInTStates % 70000; + int by1000 = pauseInTStates / 70000; + int rem1000 = pauseInTStates % 70000; - if (by1000 > 1) - { - pBlock.DataPeriods.Add(35000); - pBlock.DataPeriods.Add(pauseInTStates - 35000); - } - else - { - pBlock.DataPeriods.Add(pauseInTStates); - pBlock.DataPeriods.Add(0); - } + if (by1000 > 1) + { + pBlock.DataPeriods.Add(35000); + pBlock.DataPeriods.Add(pauseInTStates - 35000); + } + else + { + pBlock.DataPeriods.Add(pauseInTStates); + pBlock.DataPeriods.Add(0); + } - _datacorder.DataBlocks.Add(pBlock); - } - } + _datacorder.DataBlocks.Add(pBlock); + } + } - #endregion - } + #endregion + } - public enum DataBlockType - { - Standard, - Turbo, - Pure - } + public enum DataBlockType + { + Standard, + Turbo, + Pure + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Tape/TapeCommand.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Tape/TapeCommand.cs index 58aa8cdbee..5af5c32109 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Tape/TapeCommand.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Tape/TapeCommand.cs @@ -1,16 +1,16 @@  namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Represents the possible commands that can be raised from each tape block - /// - public enum TapeCommand - { - NONE, - STOP_THE_TAPE, - STOP_THE_TAPE_48K, - BEGIN_GROUP, - END_GROUP, - SHOW_MESSAGE, - } + /// + /// Represents the possible commands that can be raised from each tape block + /// + public enum TapeCommand + { + NONE, + STOP_THE_TAPE, + STOP_THE_TAPE_48K, + BEGIN_GROUP, + END_GROUP, + SHOW_MESSAGE, + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Tape/TapeDataBlock.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Tape/TapeDataBlock.cs index 64c31d87c4..0cd5627d5e 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Tape/TapeDataBlock.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Tape/TapeDataBlock.cs @@ -5,51 +5,53 @@ using System.Text; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Represents a tape block - /// - public class TapeDataBlock - { - /// - /// Either the TZX block ID, or -1 in the case of non-tzx blocks - /// - private int _blockID = -1; - public int BlockID - { - get { return _blockID; } - set { - _blockID = value; + /// + /// Represents a tape block + /// + public class TapeDataBlock + { + /// + /// Either the TZX block ID, or -1 in the case of non-tzx blocks + /// + private int _blockID = -1; + public int BlockID + { + get { return _blockID; } + set + { + _blockID = value; - if (MetaData == null) - MetaData = new Dictionary(); + if (MetaData == null) + MetaData = new Dictionary(); - AddMetaData(BlockDescriptorTitle.Block_ID, value.ToString()); - } - } + AddMetaData(BlockDescriptorTitle.Block_ID, value.ToString()); + } + } - /// - /// The block type - /// - private BlockType _blockType; - public BlockType BlockDescription - { - get { return _blockType; } - set { - _blockType = value; - if (MetaData == null) - MetaData = new Dictionary(); - } - } + /// + /// The block type + /// + private BlockType _blockType; + public BlockType BlockDescription + { + get { return _blockType; } + set + { + _blockType = value; + if (MetaData == null) + MetaData = new Dictionary(); + } + } - /// - /// Byte array containing the raw block data - /// - private byte[] _blockData; - public byte[] BlockData - { - get { return _blockData; } - set { _blockData = value; } - } + /// + /// Byte array containing the raw block data + /// + private byte[] _blockData; + public byte[] BlockData + { + get { return _blockData; } + set { _blockData = value; } + } /* /// @@ -78,203 +80,203 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC */ - #region Block Meta Data + #region Block Meta Data - /// - /// Dictionary of block related data - /// - public Dictionary MetaData { get; set; } + /// + /// Dictionary of block related data + /// + public Dictionary MetaData { get; set; } - /// - /// Adds a single metadata item to the Dictionary - /// - public void AddMetaData(BlockDescriptorTitle descriptor, string data) - { - // check whether entry already exists - bool check = MetaData.ContainsKey(descriptor); - if (check) - { - // already exists - update - MetaData[descriptor] = data; - } - else - { - // create new - MetaData.Add(descriptor, data); - } - } + /// + /// Adds a single metadata item to the Dictionary + /// + public void AddMetaData(BlockDescriptorTitle descriptor, string data) + { + // check whether entry already exists + bool check = MetaData.ContainsKey(descriptor); + if (check) + { + // already exists - update + MetaData[descriptor] = data; + } + else + { + // create new + MetaData.Add(descriptor, data); + } + } - #endregion + #endregion - /// - /// List containing the pulse timing values - /// - public List DataPeriods = new List(); + /// + /// List containing the pulse timing values + /// + public List DataPeriods = new List(); - public bool InitialPulseLevel; + public bool InitialPulseLevel; - /// - /// Command that is raised by this data block - /// (that may or may not need to be acted on) - /// - private TapeCommand _command = TapeCommand.NONE; - public TapeCommand Command - { - get { return _command; } - set { _command = value; } - } + /// + /// Command that is raised by this data block + /// (that may or may not need to be acted on) + /// + private TapeCommand _command = TapeCommand.NONE; + public TapeCommand Command + { + get { return _command; } + set { _command = value; } + } - /// - /// The defined post-block pause - /// - private int _pauseInMS; - public int PauseInMS - { - get { return _pauseInMS; } - set { _pauseInMS = value; } - } + /// + /// The defined post-block pause + /// + private int _pauseInMS; + public int PauseInMS + { + get { return _pauseInMS; } + set { _pauseInMS = value; } + } - /// - /// Returns the data periods as an array - /// (primarily to aid in bizhawk state serialization) - /// - public int[] GetDataPeriodsArray() - { - return DataPeriods.ToArray(); - } + /// + /// Returns the data periods as an array + /// (primarily to aid in bizhawk state serialization) + /// + public int[] GetDataPeriodsArray() + { + return DataPeriods.ToArray(); + } - /// - /// Accepts an array of data periods and updates the DataPeriods list accordingly - /// (primarily to aid in bizhawk state serialization) - /// - public void SetDataPeriodsArray(int[] periodArray) - { - DataPeriods = periodArray?.ToList() ?? new List(); - } + /// + /// Accepts an array of data periods and updates the DataPeriods list accordingly + /// (primarily to aid in bizhawk state serialization) + /// + public void SetDataPeriodsArray(int[] periodArray) + { + DataPeriods = periodArray?.ToList() ?? new List(); + } - /// - /// Bizhawk state serialization - /// - public void SyncState(Serializer ser, int blockPosition) - { - ser.BeginSection("DataBlock" + blockPosition); + /// + /// Bizhawk state serialization + /// + public void SyncState(Serializer ser, int blockPosition) + { + ser.BeginSection("DataBlock" + blockPosition); - ser.Sync(nameof(_blockID), ref _blockID); - //ser.SyncFixedString(nameof(_blockDescription), ref _blockDescription, 200); - ser.SyncEnum(nameof(_blockType), ref _blockType); - ser.Sync(nameof(_blockData), ref _blockData, true); - ser.SyncEnum(nameof(_command), ref _command); + ser.Sync(nameof(_blockID), ref _blockID); + //ser.SyncFixedString(nameof(_blockDescription), ref _blockDescription, 200); + ser.SyncEnum(nameof(_blockType), ref _blockType); + ser.Sync(nameof(_blockData), ref _blockData, true); + ser.SyncEnum(nameof(_command), ref _command); - int[] tempArray = null; + int[] tempArray = null; - if (ser.IsWriter) - { - tempArray = GetDataPeriodsArray(); - ser.Sync("_periods", ref tempArray, true); - } - else - { - ser.Sync("_periods", ref tempArray, true); - SetDataPeriodsArray(tempArray); - } + if (ser.IsWriter) + { + tempArray = GetDataPeriodsArray(); + ser.Sync("_periods", ref tempArray, true); + } + else + { + ser.Sync("_periods", ref tempArray, true); + SetDataPeriodsArray(tempArray); + } - ser.EndSection(); - } - } + ser.EndSection(); + } + } - /// - /// The types of TZX blocks - /// - public enum BlockType - { - Standard_Speed_Data_Block = 0x10, - Turbo_Speed_Data_Block = 0x11, - Pure_Tone = 0x12, - Pulse_Sequence = 0x13, - Pure_Data_Block = 0x14, - Direct_Recording = 0x15, - CSW_Recording = 0x18, - Generalized_Data_Block = 0x19, - Pause_or_Stop_the_Tape = 0x20, - Group_Start = 0x21, - Group_End = 0x22, - Jump_to_Block = 0x23, - Loop_Start = 0x24, - Loop_End = 0x25, - Call_Sequence = 0x26, - Return_From_Sequence = 0x27, - Select_Block = 0x28, - Stop_the_Tape_48K = 0x2A, - Set_Signal_Level = 0x2B, - Text_Description = 0x30, - Message_Block = 0x31, - Archive_Info = 0x32, - Hardware_Type = 0x33, - Custom_Info_Block = 0x35, - Glue_Block = 0x5A, + /// + /// The types of TZX blocks + /// + public enum BlockType + { + Standard_Speed_Data_Block = 0x10, + Turbo_Speed_Data_Block = 0x11, + Pure_Tone = 0x12, + Pulse_Sequence = 0x13, + Pure_Data_Block = 0x14, + Direct_Recording = 0x15, + CSW_Recording = 0x18, + Generalized_Data_Block = 0x19, + Pause_or_Stop_the_Tape = 0x20, + Group_Start = 0x21, + Group_End = 0x22, + Jump_to_Block = 0x23, + Loop_Start = 0x24, + Loop_End = 0x25, + Call_Sequence = 0x26, + Return_From_Sequence = 0x27, + Select_Block = 0x28, + Stop_the_Tape_48K = 0x2A, + Set_Signal_Level = 0x2B, + Text_Description = 0x30, + Message_Block = 0x31, + Archive_Info = 0x32, + Hardware_Type = 0x33, + Custom_Info_Block = 0x35, + Glue_Block = 0x5A, - // depreciated blocks - C64_ROM_Type_Data_Block = 0x16, - C64_Turbo_Tape_Data_Block = 0x17, - Emulation_Info = 0x34, - Snapshot_Block = 0x40, + // depreciated blocks + C64_ROM_Type_Data_Block = 0x16, + C64_Turbo_Tape_Data_Block = 0x17, + Emulation_Info = 0x34, + Snapshot_Block = 0x40, - // unsupported / undetected - Unsupported, + // unsupported / undetected + Unsupported, - // PZX blocks - PZXT, - PULS, - DATA, - BRWS, - PAUS, + // PZX blocks + PZXT, + PULS, + DATA, + BRWS, + PAUS, - // zxhawk proprietry - PAUSE_BLOCK, + // zxhawk proprietry + PAUSE_BLOCK, - WAV_Recording - } - + WAV_Recording + } - /// - /// Different title possibilities - /// - public enum BlockDescriptorTitle - { - Undefined, - Block_ID, - Program, - Data_Bytes, - Bytes, - Pilot_Pulse_Length, - Pilot_Pulse_Count, - First_Sync_Length, - Second_Sync_Length, - Zero_Bit_Length, - One_Bit_Length, - Data_Length, - Bits_In_Last_Byte, - Pause_After_Data, + /// + /// Different title possibilities + /// + public enum BlockDescriptorTitle + { + Undefined, + Block_ID, + Program, + Data_Bytes, + Bytes, - Pulse_Length, - Pulse_Count, + Pilot_Pulse_Length, + Pilot_Pulse_Count, + First_Sync_Length, + Second_Sync_Length, + Zero_Bit_Length, + One_Bit_Length, + Data_Length, + Bits_In_Last_Byte, + Pause_After_Data, - Text_Description, - Title, - Publisher, - Author, - Year, - Language, - Type, - Price, - Protection, - Origin, - Comments, + Pulse_Length, + Pulse_Count, - Needs_Parsing - } + Text_Description, + Title, + Publisher, + Author, + Year, + Language, + Type, + Price, + Protection, + Origin, + Comments, + + Needs_Parsing + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/ROM/RomData.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/ROM/RomData.cs index 9520f50323..2e67a20491 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/ROM/RomData.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/ROM/RomData.cs @@ -1,64 +1,64 @@  namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// Information about Amstrad ROM - /// - public class RomData - { - /// - /// ROM Contents - /// - public byte[] RomBytes - { - get { return _romBytes; } - set { _romBytes = value; } - } - private byte[] _romBytes; + /// + /// Information about Amstrad ROM + /// + public class RomData + { + /// + /// ROM Contents + /// + public byte[] RomBytes + { + get { return _romBytes; } + set { _romBytes = value; } + } + private byte[] _romBytes; - public enum ROMChipType - { - Lower, - Upper - } + public enum ROMChipType + { + Lower, + Upper + } - /// - /// Whether this is an Upper or Lower ROM - /// - public ROMChipType ROMType; + /// + /// Whether this is an Upper or Lower ROM + /// + public ROMChipType ROMType; - /// - /// The designated ROM position for this ROM - /// - public int ROMPosition; + /// + /// The designated ROM position for this ROM + /// + public int ROMPosition; - /// - /// Initialise a RomData object - /// - public static RomData InitROM(MachineType machineType, byte[] rom, ROMChipType type, int romPosition = 0) - { - RomData RD = new RomData(); - RD.RomBytes = new byte[rom.Length]; - RD.RomBytes = rom; - RD.ROMType = type; + /// + /// Initialise a RomData object + /// + public static RomData InitROM(MachineType machineType, byte[] rom, ROMChipType type, int romPosition = 0) + { + RomData RD = new RomData(); + RD.RomBytes = new byte[rom.Length]; + RD.RomBytes = rom; + RD.ROMType = type; - if (type == ROMChipType.Upper) - { - RD.ROMPosition = romPosition; - } + if (type == ROMChipType.Upper) + { + RD.ROMPosition = romPosition; + } - for (int i = 0; i < rom.Length; i++) - RD.RomBytes[i] = rom[i]; + for (int i = 0; i < rom.Length; i++) + RD.RomBytes[i] = rom[i]; - switch (machineType) - { - case MachineType.CPC464: - break; - case MachineType.CPC6128: - break; - } + switch (machineType) + { + case MachineType.CPC464: + break; + case MachineType.CPC6128: + break; + } - return RD; - } - } + return RD; + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/SoundProviderMixer.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/SoundProviderMixer.cs index 77e30f5fca..14ecef4e39 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/SoundProviderMixer.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/SoundProviderMixer.cs @@ -5,213 +5,213 @@ using System.Linq; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { - /// - /// My attempt at mixing multiple ISoundProvider sources together and outputting another ISoundProvider - /// Currently only supports SyncSoundMode.Sync - /// Attached ISoundProvider sources must already be stereo 44.1khz and ideally sound buffers should be the same length (882) - /// (if not, only 882 samples of their buffer will be used) - /// - internal sealed class SoundProviderMixer : ISoundProvider - { - private class Provider - { - public ISoundProvider SoundProvider { get; set; } - public string ProviderDescription { get; set; } - public int MaxVolume { get; set; } - public short[] Buffer { get; set; } - public int NSamp { get; set; } - } + /// + /// My attempt at mixing multiple ISoundProvider sources together and outputting another ISoundProvider + /// Currently only supports SyncSoundMode.Sync + /// Attached ISoundProvider sources must already be stereo 44.1khz and ideally sound buffers should be the same length (882) + /// (if not, only 882 samples of their buffer will be used) + /// + internal sealed class SoundProviderMixer : ISoundProvider + { + private class Provider + { + public ISoundProvider SoundProvider { get; set; } + public string ProviderDescription { get; set; } + public int MaxVolume { get; set; } + public short[] Buffer { get; set; } + public int NSamp { get; set; } + } - private bool _stereo = true; - public bool Stereo - { - get { return _stereo; } - set { _stereo = value; } - } + private bool _stereo = true; + public bool Stereo + { + get { return _stereo; } + set { _stereo = value; } + } - private readonly List SoundProviders; - - public SoundProviderMixer(params ISoundProvider[] soundProviders) - { - SoundProviders = new List(); + private readonly List SoundProviders; - foreach (var s in soundProviders) - { - SoundProviders.Add(new Provider - { - SoundProvider = s, - MaxVolume = short.MaxValue, - }); - } + public SoundProviderMixer(params ISoundProvider[] soundProviders) + { + SoundProviders = new List(); - EqualizeVolumes(); - } + foreach (var s in soundProviders) + { + SoundProviders.Add(new Provider + { + SoundProvider = s, + MaxVolume = short.MaxValue, + }); + } - public SoundProviderMixer(short maxVolume, string description, params ISoundProvider[] soundProviders) - { - SoundProviders = new List(); + EqualizeVolumes(); + } - foreach (var s in soundProviders) - { - SoundProviders.Add(new Provider - { - SoundProvider = s, - MaxVolume = maxVolume, - ProviderDescription = description - }); - } + public SoundProviderMixer(short maxVolume, string description, params ISoundProvider[] soundProviders) + { + SoundProviders = new List(); - EqualizeVolumes(); - } + foreach (var s in soundProviders) + { + SoundProviders.Add(new Provider + { + SoundProvider = s, + MaxVolume = maxVolume, + ProviderDescription = description + }); + } - public void AddSource(ISoundProvider source, string description) - { - SoundProviders.Add(new Provider - { - SoundProvider = source, - MaxVolume = short.MaxValue, - ProviderDescription = description - }); + EqualizeVolumes(); + } - EqualizeVolumes(); - } + public void AddSource(ISoundProvider source, string description) + { + SoundProviders.Add(new Provider + { + SoundProvider = source, + MaxVolume = short.MaxValue, + ProviderDescription = description + }); - public void AddSource(ISoundProvider source, short maxVolume, string description) - { - SoundProviders.Add(new Provider - { - SoundProvider = source, - MaxVolume = maxVolume, - ProviderDescription = description - }); + EqualizeVolumes(); + } - EqualizeVolumes(); - } + public void AddSource(ISoundProvider source, short maxVolume, string description) + { + SoundProviders.Add(new Provider + { + SoundProvider = source, + MaxVolume = maxVolume, + ProviderDescription = description + }); - public void DisableSource(ISoundProvider source) - { - var sp = SoundProviders.Where(a => a.SoundProvider == source); - if (sp.Count() == 1) - SoundProviders.Remove(sp.First()); - else if (sp.Count() > 1) - foreach (var s in sp) - SoundProviders.Remove(s); + EqualizeVolumes(); + } - EqualizeVolumes(); - } + public void DisableSource(ISoundProvider source) + { + var sp = SoundProviders.Where(a => a.SoundProvider == source); + if (sp.Count() == 1) + SoundProviders.Remove(sp.First()); + else if (sp.Count() > 1) + foreach (var s in sp) + SoundProviders.Remove(s); - public void EqualizeVolumes() - { - if (SoundProviders.Count < 1) - return; + EqualizeVolumes(); + } - int eachVolume = short.MaxValue / SoundProviders.Count; - foreach (var source in SoundProviders) - { - source.MaxVolume = eachVolume; - } - } + public void EqualizeVolumes() + { + if (SoundProviders.Count < 1) + return; - #region ISoundProvider + int eachVolume = short.MaxValue / SoundProviders.Count; + foreach (var source in SoundProviders) + { + source.MaxVolume = eachVolume; + } + } - public bool CanProvideAsync => false; - public SyncSoundMode SyncMode => SyncSoundMode.Sync; + #region ISoundProvider - public void SetSyncMode(SyncSoundMode mode) - { - if (mode != SyncSoundMode.Sync) - throw new InvalidOperationException("Only Sync mode is supported."); - } + public bool CanProvideAsync => false; + public SyncSoundMode SyncMode => SyncSoundMode.Sync; - public void GetSamplesAsync(short[] samples) - { - throw new NotSupportedException("Async is not available"); - } + public void SetSyncMode(SyncSoundMode mode) + { + if (mode != SyncSoundMode.Sync) + throw new InvalidOperationException("Only Sync mode is supported."); + } - public void DiscardSamples() - { - foreach (var soundSource in SoundProviders) - { - soundSource.SoundProvider.DiscardSamples(); - } - } + public void GetSamplesAsync(short[] samples) + { + throw new NotSupportedException("Async is not available"); + } - public void GetSamplesSync(out short[] samples, out int nsamp) - { - samples = null; - nsamp = 0; + public void DiscardSamples() + { + foreach (var soundSource in SoundProviders) + { + soundSource.SoundProvider.DiscardSamples(); + } + } - // get samples from all the providers - foreach (var sp in SoundProviders) - { - int sampCount; - short[] samp; - sp.SoundProvider.GetSamplesSync(out samp, out sampCount); - sp.NSamp = sampCount; - sp.Buffer = samp; - } + public void GetSamplesSync(out short[] samples, out int nsamp) + { + samples = null; + nsamp = 0; - // are all the sample lengths the same? - var firstEntry = SoundProviders.First(); - bool sameCount = SoundProviders.All(s => s.NSamp == firstEntry.NSamp); + // get samples from all the providers + foreach (var sp in SoundProviders) + { + int sampCount; + short[] samp; + sp.SoundProvider.GetSamplesSync(out samp, out sampCount); + sp.NSamp = sampCount; + sp.Buffer = samp; + } - if (!sameCount) - { - // this is a bit hacky, really all ISoundProviders should be supplying 44100 with 882 samples per frame. - // we will make sure this happens (no matter how it sounds) - if (SoundProviders.Count > 1) - { - for (int i = 0; i < SoundProviders.Count; i++) - { - int ns = SoundProviders[i].NSamp; - short[] buff = new short[882 * 2]; + // are all the sample lengths the same? + var firstEntry = SoundProviders.First(); + bool sameCount = SoundProviders.All(s => s.NSamp == firstEntry.NSamp); - for (int b = 0; b < 882 * 2; b++) - { - if (b == SoundProviders[i].Buffer.Length - 1) - { - // end of source buffer - break; - } + if (!sameCount) + { + // this is a bit hacky, really all ISoundProviders should be supplying 44100 with 882 samples per frame. + // we will make sure this happens (no matter how it sounds) + if (SoundProviders.Count > 1) + { + for (int i = 0; i < SoundProviders.Count; i++) + { + int ns = SoundProviders[i].NSamp; + short[] buff = new short[882 * 2]; - buff[b] = SoundProviders[i].Buffer[b]; - } + for (int b = 0; b < 882 * 2; b++) + { + if (b == SoundProviders[i].Buffer.Length - 1) + { + // end of source buffer + break; + } - // save back to the soundprovider - SoundProviders[i].NSamp = 882; - SoundProviders[i].Buffer = buff; - } - } - else - { - // just process what we have as-is - } - } + buff[b] = SoundProviders[i].Buffer[b]; + } - // mix the soundproviders together - nsamp = 882; - samples = new short[nsamp * 2]; + // save back to the soundprovider + SoundProviders[i].NSamp = 882; + SoundProviders[i].Buffer = buff; + } + } + else + { + // just process what we have as-is + } + } - for (int i = 0; i < samples.Length; i++) - { - short sectorVal = 0; - foreach (var sp in SoundProviders) - { - if (i < sp.Buffer.Length) - { - if (sp.Buffer[i] > sp.MaxVolume) - sectorVal += (short)sp.MaxVolume; - else - sectorVal += sp.Buffer[i]; - } - - } + // mix the soundproviders together + nsamp = 882; + samples = new short[nsamp * 2]; - samples[i] = sectorVal; - } - } + for (int i = 0; i < samples.Length; i++) + { + short sectorVal = 0; + foreach (var sp in SoundProviders) + { + if (i < sp.Buffer.Length) + { + if (sp.Buffer[i] > sp.MaxVolume) + sectorVal += (short)sp.MaxVolume; + else + sectorVal += sp.Buffer[i]; + } - #endregion + } - } + samples[i] = sectorVal; + } + } + + #endregion + + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Abstraction/IBeeperDevice.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Abstraction/IBeeperDevice.cs index 3b8ec3b0a6..8d40ce166c 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Abstraction/IBeeperDevice.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Abstraction/IBeeperDevice.cs @@ -2,24 +2,24 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Represents a beeper/buzzer device - /// - public interface IBeeperDevice - { - /// - /// Initialisation - /// - void Init(int sampleRate, int tStatesPerFrame); + /// + /// Represents a beeper/buzzer device + /// + public interface IBeeperDevice + { + /// + /// Initialisation + /// + void Init(int sampleRate, int tStatesPerFrame); - /// - /// Processes an incoming pulse value and adds it to the blipbuffer - /// - void ProcessPulseValue(bool pulse); + /// + /// Processes an incoming pulse value and adds it to the blipbuffer + /// + void ProcessPulseValue(bool pulse); - /// - /// State serialization - /// - void SyncState(Serializer ser); - } + /// + /// State serialization + /// + void SyncState(Serializer ser); + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Abstraction/IFDDHost.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Abstraction/IFDDHost.cs index 3bbc0c81d8..2586d05d61 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Abstraction/IFDDHost.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Abstraction/IFDDHost.cs @@ -1,29 +1,29 @@  namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Defines an object that can load a floppy disk image - /// - public interface IFDDHost - { - /// - /// The currently inserted diskimage - /// - FloppyDisk Disk { get; set; } + /// + /// Defines an object that can load a floppy disk image + /// + public interface IFDDHost + { + /// + /// The currently inserted diskimage + /// + FloppyDisk Disk { get; set; } - /// - /// Parses a new disk image and loads it into this floppy drive - /// - void FDD_LoadDisk(byte[] diskData); + /// + /// Parses a new disk image and loads it into this floppy drive + /// + void FDD_LoadDisk(byte[] diskData); - /// - /// Ejects the current disk - /// - void FDD_EjectDisk(); + /// + /// Ejects the current disk + /// + void FDD_EjectDisk(); - /// - /// Signs whether the current active drive has a disk inserted - /// - bool FDD_IsDiskLoaded { get; } - } + /// + /// Signs whether the current active drive has a disk inserted + /// + bool FDD_IsDiskLoaded { get; } + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Abstraction/IJoystick.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Abstraction/IJoystick.cs index 6e29e02012..6ea7acd897 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Abstraction/IJoystick.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Abstraction/IJoystick.cs @@ -1,34 +1,34 @@  namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Represents a spectrum joystick - /// - public interface IJoystick - { - /// - /// The type of joystick - /// - JoystickType JoyType { get; } + /// + /// Represents a spectrum joystick + /// + public interface IJoystick + { + /// + /// The type of joystick + /// + JoystickType JoyType { get; } - /// - /// Array of all the possibly button press names - /// - string[] ButtonCollection { get; set; } + /// + /// Array of all the possibly button press names + /// + string[] ButtonCollection { get; set; } - /// - /// The player number that this controller is currently assigned to - /// - int PlayerNumber { get; set; } + /// + /// The player number that this controller is currently assigned to + /// + int PlayerNumber { get; set; } - /// - /// Sets the joystick line based on key pressed - /// - void SetJoyInput(string key, bool isPressed); + /// + /// Sets the joystick line based on key pressed + /// + void SetJoyInput(string key, bool isPressed); - /// - /// Gets the state of a particular joystick binding - /// - bool GetJoyInput(string key); - } + /// + /// Gets the state of a particular joystick binding + /// + bool GetJoyInput(string key); + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Abstraction/IKeyboard.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Abstraction/IKeyboard.cs index 085792d874..ad1cc4acc2 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Abstraction/IKeyboard.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Abstraction/IKeyboard.cs @@ -2,69 +2,69 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Represents a spectrum keyboard - /// - public interface IKeyboard : IPortIODevice - { - /// - /// The calling spectrumbase class - /// - SpectrumBase _machine { get; } + /// + /// Represents a spectrum keyboard + /// + public interface IKeyboard : IPortIODevice + { + /// + /// The calling spectrumbase class + /// + SpectrumBase _machine { get; } - /// - /// The keyboard matrix for a particular spectrum model - /// - string[] KeyboardMatrix { get; set; } + /// + /// The keyboard matrix for a particular spectrum model + /// + string[] KeyboardMatrix { get; set; } - /// - /// Other keyboard keys that are not in the matrix - /// (usually keys derived from key combos) - /// - string[] NonMatrixKeys { get; set; } + /// + /// Other keyboard keys that are not in the matrix + /// (usually keys derived from key combos) + /// + string[] NonMatrixKeys { get; set; } - /// - /// Represents the spectrum key state - /// - int[] KeyLine { get; set; } + /// + /// Represents the spectrum key state + /// + int[] KeyLine { get; set; } - /// - /// Resets the line status - /// - void ResetLineStatus(); + /// + /// Resets the line status + /// + void ResetLineStatus(); - /// - /// There are some slight differences in how PortIN and PortOUT functions - /// between Issue2 and Issue3 keyboards (16k/48k spectrum only) - /// It is possible that some very old games require Issue2 emulation - /// - bool IsIssue2Keyboard { get; set; } + /// + /// There are some slight differences in how PortIN and PortOUT functions + /// between Issue2 and Issue3 keyboards (16k/48k spectrum only) + /// It is possible that some very old games require Issue2 emulation + /// + bool IsIssue2Keyboard { get; set; } - /// - /// Sets the spectrum key status - /// - void SetKeyStatus(string key, bool isPressed); + /// + /// Sets the spectrum key status + /// + void SetKeyStatus(string key, bool isPressed); - /// - /// Gets the status of a spectrum key - /// - bool GetKeyStatus(string key); + /// + /// Gets the status of a spectrum key + /// + bool GetKeyStatus(string key); - /// - /// Returns the query byte - /// - byte GetLineStatus(byte lines); + /// + /// Returns the query byte + /// + byte GetLineStatus(byte lines); - /// - /// Reads a keyboard byte - /// - byte ReadKeyboardByte(ushort addr); + /// + /// Reads a keyboard byte + /// + byte ReadKeyboardByte(ushort addr); - /// - /// Looks up a key in the keyboard matrix and returns the relevent byte value - /// - byte GetByteFromKeyMatrix(string key); + /// + /// Looks up a key in the keyboard matrix and returns the relevent byte value + /// + byte GetByteFromKeyMatrix(string key); - void SyncState(Serializer ser); - } + void SyncState(Serializer ser); + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Abstraction/IPSG.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Abstraction/IPSG.cs index 4f1523e70e..cd50120130 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Abstraction/IPSG.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Abstraction/IPSG.cs @@ -3,62 +3,62 @@ using BizHawk.Emulation.Common; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Represents a PSG device (in this case an AY-3-891x) - /// - public interface IPSG : ISoundProvider, IPortIODevice - { - /// - /// Initlization routine - /// - void Init(int sampleRate, int tStatesPerFrame); + /// + /// Represents a PSG device (in this case an AY-3-891x) + /// + public interface IPSG : ISoundProvider, IPortIODevice + { + /// + /// Initlization routine + /// + void Init(int sampleRate, int tStatesPerFrame); - /// - /// Activates a register - /// - int SelectedRegister { get; set; } + /// + /// Activates a register + /// + int SelectedRegister { get; set; } - int[] ExportRegisters(); - - /// - /// Writes to the PSG - /// - void PortWrite(int value); + int[] ExportRegisters(); - /// - /// Reads from the PSG - /// - int PortRead(); - + /// + /// Writes to the PSG + /// + void PortWrite(int value); - /// - /// Resets the PSG - /// - void Reset(); + /// + /// Reads from the PSG + /// + int PortRead(); - /// - /// The volume of the AY chip - /// - int Volume { get; set; } - /// - /// Called at the start of a frame - /// - void StartFrame(); + /// + /// Resets the PSG + /// + void Reset(); - /// - /// called at the end of a frame - /// - void EndFrame(); + /// + /// The volume of the AY chip + /// + int Volume { get; set; } - /// - /// Updates the sound based on number of frame cycles - /// - void UpdateSound(int frameCycle); + /// + /// Called at the start of a frame + /// + void StartFrame(); - /// - /// IStatable serialization - /// - void SyncState(Serializer ser); - } + /// + /// called at the end of a frame + /// + void EndFrame(); + + /// + /// Updates the sound based on number of frame cycles + /// + void UpdateSound(int frameCycle); + + /// + /// IStatable serialization + /// + void SyncState(Serializer ser); + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Abstraction/IPortIODevice.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Abstraction/IPortIODevice.cs index 2ca7909fbd..7a24c2d1ba 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Abstraction/IPortIODevice.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Abstraction/IPortIODevice.cs @@ -1,19 +1,19 @@  namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Represents a device that utilizes port IN & OUT - /// - public interface IPortIODevice - { - /// - /// Device responds to an IN instruction - /// - bool ReadPort(ushort port, ref int result); + /// + /// Represents a device that utilizes port IN & OUT + /// + public interface IPortIODevice + { + /// + /// Device responds to an IN instruction + /// + bool ReadPort(ushort port, ref int result); - /// - /// Device responds to an OUT instruction - /// - bool WritePort(ushort port, int result); - } + /// + /// Device responds to an OUT instruction + /// + bool WritePort(ushort port, int result); + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Datacorder/DatacorderDevice.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Datacorder/DatacorderDevice.cs index 7b33813ba9..ec27e4e624 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Datacorder/DatacorderDevice.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Datacorder/DatacorderDevice.cs @@ -8,986 +8,986 @@ using BizHawk.Emulation.Cores.Sound; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Represents the tape device (or build-in datacorder as it was called +2 and above) - /// - public class DatacorderDevice : IPortIODevice - { - #region Construction + /// + /// Represents the tape device (or build-in datacorder as it was called +2 and above) + /// + public class DatacorderDevice : IPortIODevice + { + #region Construction - private SpectrumBase _machine { get; set; } - private Z80A _cpu { get; set; } - private OneBitBeeper _buzzer { get; set; } + private SpectrumBase _machine { get; set; } + private Z80A _cpu { get; set; } + private OneBitBeeper _buzzer { get; set; } - /// - /// Default constructor - /// - public DatacorderDevice(bool autoplay) - { - _autoPlay = autoplay; - } + /// + /// Default constructor + /// + public DatacorderDevice(bool autoplay) + { + _autoPlay = autoplay; + } - /// - /// Initializes the datacorder device - /// - public void Init(SpectrumBase machine) - { - _machine = machine; - _cpu = _machine.CPU; - _buzzer = machine.TapeBuzzer; - } + /// + /// Initializes the datacorder device + /// + public void Init(SpectrumBase machine) + { + _machine = machine; + _cpu = _machine.CPU; + _buzzer = machine.TapeBuzzer; + } - #endregion + #endregion - #region State Information + #region State Information - /// - /// Internal counter used to trigger tape buzzer output - /// - private int counter = 0; + /// + /// Internal counter used to trigger tape buzzer output + /// + private int counter = 0; - /// - /// The index of the current tape data block that is loaded - /// - private int _currentDataBlockIndex = 0; - public int CurrentDataBlockIndex - { - get - { - if (_dataBlocks.Count() > 0) { return _currentDataBlockIndex; } - else { return -1; } - } - set - { - if (value == _currentDataBlockIndex) { return; } - if (value < _dataBlocks.Count() && value >= 0) - { - _currentDataBlockIndex = value; - _position = 0; - } - } - } + /// + /// The index of the current tape data block that is loaded + /// + private int _currentDataBlockIndex = 0; + public int CurrentDataBlockIndex + { + get + { + if (_dataBlocks.Count() > 0) { return _currentDataBlockIndex; } + else { return -1; } + } + set + { + if (value == _currentDataBlockIndex) { return; } + if (value < _dataBlocks.Count() && value >= 0) + { + _currentDataBlockIndex = value; + _position = 0; + } + } + } - /// - /// The current position within the current data block - /// - private int _position = 0; - public int Position - { - get - { - if (_position >= _dataBlocks[_currentDataBlockIndex].DataPeriods.Count) { return 0; } - else { return _position; } - } - } + /// + /// The current position within the current data block + /// + private int _position = 0; + public int Position + { + get + { + if (_position >= _dataBlocks[_currentDataBlockIndex].DataPeriods.Count) { return 0; } + else { return _position; } + } + } - /// - /// Signs whether the tape is currently playing or not - /// - private bool _tapeIsPlaying = false; - public bool TapeIsPlaying - { - get { return _tapeIsPlaying; } - } + /// + /// Signs whether the tape is currently playing or not + /// + private bool _tapeIsPlaying = false; + public bool TapeIsPlaying + { + get { return _tapeIsPlaying; } + } - /// - /// A list of the currently loaded data blocks - /// - private List _dataBlocks = new List(); - public List DataBlocks - { - get { return _dataBlocks; } - set { _dataBlocks = value; } - } + /// + /// A list of the currently loaded data blocks + /// + private List _dataBlocks = new List(); + public List DataBlocks + { + get { return _dataBlocks; } + set { _dataBlocks = value; } + } - /// - /// Stores the last CPU t-state value - /// - private long _lastCycle = 0; + /// + /// Stores the last CPU t-state value + /// + private long _lastCycle = 0; - /// - /// Edge - /// - private int _waitEdge = 0; + /// + /// Edge + /// + private int _waitEdge = 0; - /// - /// Current tapebit state - /// - private bool currentState = false; + /// + /// Current tapebit state + /// + private bool currentState = false; - #endregion + #endregion - #region Datacorder Device Settings + #region Datacorder Device Settings - /// - /// Signs whether the device should autodetect when the Z80 has entered into - /// 'load' mode and auto-play the tape if neccesary - /// - private bool _autoPlay; + /// + /// Signs whether the device should autodetect when the Z80 has entered into + /// 'load' mode and auto-play the tape if neccesary + /// + private bool _autoPlay; - #endregion + #endregion - #region Emulator + #region Emulator - /// - /// Should be fired at the end of every frame - /// Primary purpose is to detect tape traps and manage auto play - /// - public void EndFrame() - { - MonitorFrame(); - } + /// + /// Should be fired at the end of every frame + /// Primary purpose is to detect tape traps and manage auto play + /// + public void EndFrame() + { + MonitorFrame(); + } /// /// No longer in use /// - public void StartFrame() - { - //_buzzer.ProcessPulseValue(currentState); - } + public void StartFrame() + { + //_buzzer.ProcessPulseValue(currentState); + } - #endregion + #endregion - #region Tape Controls + #region Tape Controls - /// - /// Starts the tape playing from the beginning of the current block - /// - public void Play() - { - if (_tapeIsPlaying) - return; + /// + /// Starts the tape playing from the beginning of the current block + /// + public void Play() + { + if (_tapeIsPlaying) + return; - _machine.Spectrum.OSD_TapePlaying(); + _machine.Spectrum.OSD_TapePlaying(); - // update the lastCycle - _lastCycle = _cpu.TotalExecutedCycles; + // update the lastCycle + _lastCycle = _cpu.TotalExecutedCycles; - // reset waitEdge and position - _waitEdge = 0; - _position = 0; + // reset waitEdge and position + _waitEdge = 0; + _position = 0; - if ( - _dataBlocks.Count > 0 && // data blocks are present && - _currentDataBlockIndex >= 0 // the current data block index is 1 or greater - ) - { - while (_position >= _dataBlocks[_currentDataBlockIndex].DataPeriods.Count) - { - // we are at the end of a data block - move to the next - _position = 0; - _currentDataBlockIndex++; + if ( + _dataBlocks.Count > 0 && // data blocks are present && + _currentDataBlockIndex >= 0 // the current data block index is 1 or greater + ) + { + while (_position >= _dataBlocks[_currentDataBlockIndex].DataPeriods.Count) + { + // we are at the end of a data block - move to the next + _position = 0; + _currentDataBlockIndex++; - // are we at the end of the tape? - if (_currentDataBlockIndex >= _dataBlocks.Count) - { - break; - } - } + // are we at the end of the tape? + if (_currentDataBlockIndex >= _dataBlocks.Count) + { + break; + } + } - // check for end of tape - if (_currentDataBlockIndex >= _dataBlocks.Count) - { - // end of tape reached. Rewind to beginning - AutoStopTape(); - RTZ(); - return; - } + // check for end of tape + if (_currentDataBlockIndex >= _dataBlocks.Count) + { + // end of tape reached. Rewind to beginning + AutoStopTape(); + RTZ(); + return; + } - // update waitEdge with the current position in the current block - _waitEdge = _dataBlocks[_currentDataBlockIndex].DataPeriods[_position]; + // update waitEdge with the current position in the current block + _waitEdge = _dataBlocks[_currentDataBlockIndex].DataPeriods[_position]; - // sign that the tape is now playing - _tapeIsPlaying = true; - } - } + // sign that the tape is now playing + _tapeIsPlaying = true; + } + } - /// - /// Stops the tape - /// (should move to the beginning of the next block) - /// - public void Stop() - { - if (!_tapeIsPlaying) - return; + /// + /// Stops the tape + /// (should move to the beginning of the next block) + /// + public void Stop() + { + if (!_tapeIsPlaying) + return; - _machine.Spectrum.OSD_TapeStopped(); + _machine.Spectrum.OSD_TapeStopped(); - // sign that the tape is no longer playing - _tapeIsPlaying = false; + // sign that the tape is no longer playing + _tapeIsPlaying = false; - if ( - _currentDataBlockIndex >= 0 && // we are at datablock 1 or above - _position >= _dataBlocks[_currentDataBlockIndex].DataPeriods.Count - 1 // the block is still playing back - ) - { - // move to the next block - _currentDataBlockIndex++; + if ( + _currentDataBlockIndex >= 0 && // we are at datablock 1 or above + _position >= _dataBlocks[_currentDataBlockIndex].DataPeriods.Count - 1 // the block is still playing back + ) + { + // move to the next block + _currentDataBlockIndex++; - if (_currentDataBlockIndex >= _dataBlocks.Count()) - { - _currentDataBlockIndex = -1; - } + if (_currentDataBlockIndex >= _dataBlocks.Count()) + { + _currentDataBlockIndex = -1; + } - // reset waitEdge and position - _waitEdge = 0; - _position = 0; + // reset waitEdge and position + _waitEdge = 0; + _position = 0; - if ( - _currentDataBlockIndex < 0 && // block index is -1 - _dataBlocks.Count() > 0 // number of blocks is greater than 0 - ) - { - // move the index on to 0 - _currentDataBlockIndex = 0; - } - } + if ( + _currentDataBlockIndex < 0 && // block index is -1 + _dataBlocks.Count() > 0 // number of blocks is greater than 0 + ) + { + // move the index on to 0 + _currentDataBlockIndex = 0; + } + } - // update the lastCycle - _lastCycle = _cpu.TotalExecutedCycles; - } + // update the lastCycle + _lastCycle = _cpu.TotalExecutedCycles; + } - /// - /// Rewinds the tape to it's beginning (return to zero) - /// - public void RTZ() - { - Stop(); - _machine.Spectrum.OSD_TapeRTZ(); - _currentDataBlockIndex = 0; - } + /// + /// Rewinds the tape to it's beginning (return to zero) + /// + public void RTZ() + { + Stop(); + _machine.Spectrum.OSD_TapeRTZ(); + _currentDataBlockIndex = 0; + } - /// - /// Performs a block skip operation on the current tape - /// TRUE: skip forward - /// FALSE: skip backward - /// - public void SkipBlock(bool skipForward) - { - int blockCount = _dataBlocks.Count; - int targetBlockId = _currentDataBlockIndex; + /// + /// Performs a block skip operation on the current tape + /// TRUE: skip forward + /// FALSE: skip backward + /// + public void SkipBlock(bool skipForward) + { + int blockCount = _dataBlocks.Count; + int targetBlockId = _currentDataBlockIndex; - if (skipForward) - { - if (_currentDataBlockIndex == blockCount - 1) - { - // last block, go back to beginning - targetBlockId = 0; - } - else - { - targetBlockId++; - } - } - else - { - if (_currentDataBlockIndex == 0) - { - // already first block, goto last block - targetBlockId = blockCount - 1; - } - else - { - targetBlockId--; - } - } + if (skipForward) + { + if (_currentDataBlockIndex == blockCount - 1) + { + // last block, go back to beginning + targetBlockId = 0; + } + else + { + targetBlockId++; + } + } + else + { + if (_currentDataBlockIndex == 0) + { + // already first block, goto last block + targetBlockId = blockCount - 1; + } + else + { + targetBlockId--; + } + } - var bl = _dataBlocks[targetBlockId]; + var bl = _dataBlocks[targetBlockId]; - StringBuilder sbd = new StringBuilder(); - sbd.Append("("); - sbd.Append((targetBlockId + 1) + " of " + _dataBlocks.Count()); - sbd.Append(") : "); - sbd.Append(bl.BlockDescription); - if (bl.MetaData.Count > 0) - { - sbd.Append(" - "); - sbd.Append(bl.MetaData.First().Key + ": " + bl.MetaData.First().Value); - } + StringBuilder sbd = new StringBuilder(); + sbd.Append("("); + sbd.Append((targetBlockId + 1) + " of " + _dataBlocks.Count()); + sbd.Append(") : "); + sbd.Append(bl.BlockDescription); + if (bl.MetaData.Count > 0) + { + sbd.Append(" - "); + sbd.Append(bl.MetaData.First().Key + ": " + bl.MetaData.First().Value); + } - if (skipForward) - _machine.Spectrum.OSD_TapeNextBlock(sbd.ToString()); - else - _machine.Spectrum.OSD_TapePrevBlock(sbd.ToString()); + if (skipForward) + _machine.Spectrum.OSD_TapeNextBlock(sbd.ToString()); + else + _machine.Spectrum.OSD_TapePrevBlock(sbd.ToString()); - CurrentDataBlockIndex = targetBlockId; - } + CurrentDataBlockIndex = targetBlockId; + } - /// - /// Inserts a new tape and sets up the tape device accordingly - /// - public void LoadTape(byte[] tapeData) - { - // instantiate converters - TzxConverter tzxSer = new TzxConverter(this); - TapConverter tapSer = new TapConverter(this); - PzxConverter pzxSer = new PzxConverter(this); - CswConverter cswSer = new CswConverter(this); - WavConverter wavSer = new WavConverter(this); + /// + /// Inserts a new tape and sets up the tape device accordingly + /// + public void LoadTape(byte[] tapeData) + { + // instantiate converters + TzxConverter tzxSer = new TzxConverter(this); + TapConverter tapSer = new TapConverter(this); + PzxConverter pzxSer = new PzxConverter(this); + CswConverter cswSer = new CswConverter(this); + WavConverter wavSer = new WavConverter(this); - // TZX - if (tzxSer.CheckType(tapeData)) - { - // this file has a tzx header - attempt serialization - try - { - tzxSer.Read(tapeData); - // reset block index - CurrentDataBlockIndex = 0; - return; - } - catch (Exception ex) - { - // exception during operation - var e = ex; - throw new Exception(this.GetType().ToString() + - "\n\nTape image file has a valid TZX header, but threw an exception whilst data was being parsed.\n\n" + e.ToString()); - } - } + // TZX + if (tzxSer.CheckType(tapeData)) + { + // this file has a tzx header - attempt serialization + try + { + tzxSer.Read(tapeData); + // reset block index + CurrentDataBlockIndex = 0; + return; + } + catch (Exception ex) + { + // exception during operation + var e = ex; + throw new Exception(this.GetType().ToString() + + "\n\nTape image file has a valid TZX header, but threw an exception whilst data was being parsed.\n\n" + e.ToString()); + } + } - // PZX - else if (pzxSer.CheckType(tapeData)) - { - // this file has a pzx header - attempt serialization - try - { - pzxSer.Read(tapeData); - // reset block index - CurrentDataBlockIndex = 0; - return; - } - catch (Exception ex) - { - // exception during operation - var e = ex; - throw new Exception(this.GetType().ToString() + - "\n\nTape image file has a valid PZX header, but threw an exception whilst data was being parsed.\n\n" + e.ToString()); - } - } + // PZX + else if (pzxSer.CheckType(tapeData)) + { + // this file has a pzx header - attempt serialization + try + { + pzxSer.Read(tapeData); + // reset block index + CurrentDataBlockIndex = 0; + return; + } + catch (Exception ex) + { + // exception during operation + var e = ex; + throw new Exception(this.GetType().ToString() + + "\n\nTape image file has a valid PZX header, but threw an exception whilst data was being parsed.\n\n" + e.ToString()); + } + } - // CSW - else if (cswSer.CheckType(tapeData)) - { - // this file has a csw header - attempt serialization - try - { - cswSer.Read(tapeData); - // reset block index - CurrentDataBlockIndex = 0; - return; - } - catch (Exception ex) - { - // exception during operation - var e = ex; - throw new Exception(this.GetType().ToString() + - "\n\nTape image file has a valid CSW header, but threw an exception whilst data was being parsed.\n\n" + e.ToString()); - } - } + // CSW + else if (cswSer.CheckType(tapeData)) + { + // this file has a csw header - attempt serialization + try + { + cswSer.Read(tapeData); + // reset block index + CurrentDataBlockIndex = 0; + return; + } + catch (Exception ex) + { + // exception during operation + var e = ex; + throw new Exception(this.GetType().ToString() + + "\n\nTape image file has a valid CSW header, but threw an exception whilst data was being parsed.\n\n" + e.ToString()); + } + } - // WAV - else if (wavSer.CheckType(tapeData)) - { - // this file has a csw header - attempt serialization - try - { - wavSer.Read(tapeData); - // reset block index - CurrentDataBlockIndex = 0; - return; - } - catch (Exception ex) - { - // exception during operation - var e = ex; - throw new Exception(this.GetType().ToString() + - "\n\nTape image file has a valid WAV header, but threw an exception whilst data was being parsed.\n\n" + e.ToString()); - } - } + // WAV + else if (wavSer.CheckType(tapeData)) + { + // this file has a csw header - attempt serialization + try + { + wavSer.Read(tapeData); + // reset block index + CurrentDataBlockIndex = 0; + return; + } + catch (Exception ex) + { + // exception during operation + var e = ex; + throw new Exception(this.GetType().ToString() + + "\n\nTape image file has a valid WAV header, but threw an exception whilst data was being parsed.\n\n" + e.ToString()); + } + } - // Assume TAP - else - { - try - { - tapSer.Read(tapeData); - // reset block index - CurrentDataBlockIndex = 0; - return; - } - catch (Exception ex) - { - // exception during operation - var e = ex; - throw new Exception(this.GetType().ToString() + - "\n\nAn exception was thrown whilst data from this tape image was being parsed as TAP.\n\n" + e.ToString()); - } - } - } + // Assume TAP + else + { + try + { + tapSer.Read(tapeData); + // reset block index + CurrentDataBlockIndex = 0; + return; + } + catch (Exception ex) + { + // exception during operation + var e = ex; + throw new Exception(this.GetType().ToString() + + "\n\nAn exception was thrown whilst data from this tape image was being parsed as TAP.\n\n" + e.ToString()); + } + } + } - /// - /// Resets the tape - /// - public void Reset() - { - RTZ(); - } + /// + /// Resets the tape + /// + public void Reset() + { + RTZ(); + } - #endregion + #endregion - #region Tape Device Methods + #region Tape Device Methods - /// - /// Is called every cpu cycle but runs every 50 t-states - /// This enables the tape devices to play out even if the spectrum itself is not - /// requesting tape data - /// - public void TapeCycle() - { - if (TapeIsPlaying) - { - counter++; + /// + /// Is called every cpu cycle but runs every 50 t-states + /// This enables the tape devices to play out even if the spectrum itself is not + /// requesting tape data + /// + public void TapeCycle() + { + if (TapeIsPlaying) + { + counter++; - if (counter > 20) - { - counter = 0; - bool state = GetEarBit(_machine.CPU.TotalExecutedCycles); - _buzzer.ProcessPulseValue(state); - } - } - } - - /// - /// Simulates the spectrum 'EAR' input reading data from the tape - /// - public bool GetEarBit(long cpuCycle) - { - // decide how many cycles worth of data we are capturing - long cycles = cpuCycle - _lastCycle; + if (counter > 20) + { + counter = 0; + bool state = GetEarBit(_machine.CPU.TotalExecutedCycles); + _buzzer.ProcessPulseValue(state); + } + } + } - bool is48k = _machine.IsIn48kMode(); + /// + /// Simulates the spectrum 'EAR' input reading data from the tape + /// + public bool GetEarBit(long cpuCycle) + { + // decide how many cycles worth of data we are capturing + long cycles = cpuCycle - _lastCycle; - // check whether tape is actually playing - if (_tapeIsPlaying == false) - { - // it's not playing. Update lastCycle and return - _lastCycle = cpuCycle; - return false; - } + bool is48k = _machine.IsIn48kMode(); - // check for end of tape - if (_currentDataBlockIndex < 0) - { - // end of tape reached - RTZ (and stop) - RTZ(); - return currentState; - } + // check whether tape is actually playing + if (_tapeIsPlaying == false) + { + // it's not playing. Update lastCycle and return + _lastCycle = cpuCycle; + return false; + } - // process the cycles based on the waitEdge - while (cycles >= _waitEdge) - { - // decrement cycles - cycles -= _waitEdge; + // check for end of tape + if (_currentDataBlockIndex < 0) + { + // end of tape reached - RTZ (and stop) + RTZ(); + return currentState; + } - if (_position == 0 && _tapeIsPlaying) - { - // start of block - take care of initial pulse level for PZX - switch (_dataBlocks[_currentDataBlockIndex].BlockDescription) - { - case BlockType.PULS: - // initial pulse level is always low - if (currentState) - FlipTapeState(); - break; - case BlockType.DATA: - // initial pulse level is stored in block - if (currentState != _dataBlocks[_currentDataBlockIndex].InitialPulseLevel) - FlipTapeState(); - break; - case BlockType.PAUS: - // initial pulse level is stored in block - if (currentState != _dataBlocks[_currentDataBlockIndex].InitialPulseLevel) - FlipTapeState(); - break; - } + // process the cycles based on the waitEdge + while (cycles >= _waitEdge) + { + // decrement cycles + cycles -= _waitEdge; - // notify about the current block - var bl = _dataBlocks[_currentDataBlockIndex]; + if (_position == 0 && _tapeIsPlaying) + { + // start of block - take care of initial pulse level for PZX + switch (_dataBlocks[_currentDataBlockIndex].BlockDescription) + { + case BlockType.PULS: + // initial pulse level is always low + if (currentState) + FlipTapeState(); + break; + case BlockType.DATA: + // initial pulse level is stored in block + if (currentState != _dataBlocks[_currentDataBlockIndex].InitialPulseLevel) + FlipTapeState(); + break; + case BlockType.PAUS: + // initial pulse level is stored in block + if (currentState != _dataBlocks[_currentDataBlockIndex].InitialPulseLevel) + FlipTapeState(); + break; + } - StringBuilder sbd = new StringBuilder(); - sbd.Append("("); - sbd.Append((_currentDataBlockIndex + 1) + " of " + _dataBlocks.Count()); - sbd.Append(") : "); - sbd.Append(bl.BlockDescription); - if (bl.MetaData.Count > 0) - { - sbd.Append(" - "); - sbd.Append(bl.MetaData.First().Key + ": " + bl.MetaData.First().Value); - } - _machine.Spectrum.OSD_TapePlayingBlockInfo(sbd.ToString()); - } + // notify about the current block + var bl = _dataBlocks[_currentDataBlockIndex]; - // increment the current period position - _position++; - - if (_position >= _dataBlocks[_currentDataBlockIndex].DataPeriods.Count()) - { - // we have reached the end of the current block - if (_dataBlocks[_currentDataBlockIndex].DataPeriods.Count() == 0) - { - // notify about the current block (we are skipping it because its empty) - var bl = _dataBlocks[_currentDataBlockIndex]; - StringBuilder sbd = new StringBuilder(); - sbd.Append("("); - sbd.Append((_currentDataBlockIndex + 1) + " of " + _dataBlocks.Count()); - sbd.Append(") : "); - sbd.Append(bl.BlockDescription); - if (bl.MetaData.Count > 0) + StringBuilder sbd = new StringBuilder(); + sbd.Append("("); + sbd.Append((_currentDataBlockIndex + 1) + " of " + _dataBlocks.Count()); + sbd.Append(") : "); + sbd.Append(bl.BlockDescription); + if (bl.MetaData.Count > 0) + { + sbd.Append(" - "); + sbd.Append(bl.MetaData.First().Key + ": " + bl.MetaData.First().Value); + } + _machine.Spectrum.OSD_TapePlayingBlockInfo(sbd.ToString()); + } + + // increment the current period position + _position++; + + if (_position >= _dataBlocks[_currentDataBlockIndex].DataPeriods.Count()) + { + // we have reached the end of the current block + if (_dataBlocks[_currentDataBlockIndex].DataPeriods.Count() == 0) + { + // notify about the current block (we are skipping it because its empty) + var bl = _dataBlocks[_currentDataBlockIndex]; + StringBuilder sbd = new StringBuilder(); + sbd.Append("("); + sbd.Append((_currentDataBlockIndex + 1) + " of " + _dataBlocks.Count()); + sbd.Append(") : "); + sbd.Append(bl.BlockDescription); + if (bl.MetaData.Count > 0) { sbd.Append(" - "); sbd.Append(bl.MetaData.First().Key + ": " + bl.MetaData.First().Value); } - _machine.Spectrum.OSD_TapePlayingSkipBlockInfo(sbd.ToString()); + _machine.Spectrum.OSD_TapePlayingSkipBlockInfo(sbd.ToString()); - } + } - // skip any empty blocks (and process any command blocks) - while (_position >= _dataBlocks[_currentDataBlockIndex].DataPeriods.Count()) - { - // check for any commands - var command = _dataBlocks[_currentDataBlockIndex].Command; - var block = _dataBlocks[_currentDataBlockIndex]; - bool shouldStop = false; - switch (command) - { - // Stop the tape command found - if this is the end of the tape RTZ - // otherwise just STOP and move to the next block - case TapeCommand.STOP_THE_TAPE: + // skip any empty blocks (and process any command blocks) + while (_position >= _dataBlocks[_currentDataBlockIndex].DataPeriods.Count()) + { + // check for any commands + var command = _dataBlocks[_currentDataBlockIndex].Command; + var block = _dataBlocks[_currentDataBlockIndex]; + bool shouldStop = false; + switch (command) + { + // Stop the tape command found - if this is the end of the tape RTZ + // otherwise just STOP and move to the next block + case TapeCommand.STOP_THE_TAPE: - _machine.Spectrum.OSD_TapeStoppedAuto(); - shouldStop = true; + _machine.Spectrum.OSD_TapeStoppedAuto(); + shouldStop = true; - if (_currentDataBlockIndex >= _dataBlocks.Count()) - RTZ(); - else - { - Stop(); - } + if (_currentDataBlockIndex >= _dataBlocks.Count()) + RTZ(); + else + { + Stop(); + } - _monitorTimeOut = 2000; - break; - case TapeCommand.STOP_THE_TAPE_48K: - if (is48k) - { - _machine.Spectrum.OSD_TapeStoppedAuto(); - shouldStop = true; + _monitorTimeOut = 2000; + break; + case TapeCommand.STOP_THE_TAPE_48K: + if (is48k) + { + _machine.Spectrum.OSD_TapeStoppedAuto(); + shouldStop = true; - if (_currentDataBlockIndex >= _dataBlocks.Count()) - RTZ(); - else - { - Stop(); - } + if (_currentDataBlockIndex >= _dataBlocks.Count()) + RTZ(); + else + { + Stop(); + } - _monitorTimeOut = 2000; - } - break; - } + _monitorTimeOut = 2000; + } + break; + } - if (shouldStop) - break; + if (shouldStop) + break; - _position = 0; - _currentDataBlockIndex++; + _position = 0; + _currentDataBlockIndex++; - if (_currentDataBlockIndex >= _dataBlocks.Count()) - { - break; - } - } + if (_currentDataBlockIndex >= _dataBlocks.Count()) + { + break; + } + } - // check for end of tape - if (_currentDataBlockIndex >= _dataBlocks.Count()) - { - _currentDataBlockIndex = -1; - RTZ(); - return currentState; - } - } + // check for end of tape + if (_currentDataBlockIndex >= _dataBlocks.Count()) + { + _currentDataBlockIndex = -1; + RTZ(); + return currentState; + } + } // update waitEdge with current position within the current block _waitEdge = _dataBlocks[_currentDataBlockIndex].DataPeriods.Count() > 0 ? _dataBlocks[_currentDataBlockIndex].DataPeriods[_position] : 0; - // flip the current state - FlipTapeState(); - } + // flip the current state + FlipTapeState(); + } - // update lastCycle and return currentstate - _lastCycle = cpuCycle - (long)cycles; + // update lastCycle and return currentstate + _lastCycle = cpuCycle - (long)cycles; - return currentState; - } + return currentState; + } - private void FlipTapeState() - { - currentState = !currentState; - } + private void FlipTapeState() + { + currentState = !currentState; + } - /// - /// Flash loading implementation - /// (Deterministic Emulation must be FALSE) + /// + /// Flash loading implementation + /// (Deterministic Emulation must be FALSE) /// CURRENTLY NOT ENABLED/WORKING - /// - private bool FlashLoad() - { - // deterministic emulation must = false - //if (_machine.Spectrum.SyncSettings.DeterministicEmulation) - //return; + /// + private bool FlashLoad() + { + // deterministic emulation must = false + //if (_machine.Spectrum.SyncSettings.DeterministicEmulation) + //return; - var util = _machine.Spectrum; + var util = _machine.Spectrum; - if (_currentDataBlockIndex < 0) - _currentDataBlockIndex = 0; + if (_currentDataBlockIndex < 0) + _currentDataBlockIndex = 0; - if (_currentDataBlockIndex >= DataBlocks.Count) - return false; + if (_currentDataBlockIndex >= DataBlocks.Count) + return false; - //var val = GetEarBit(_cpu.TotalExecutedCycles); - //_buzzer.ProcessPulseValue(true, val); + //var val = GetEarBit(_cpu.TotalExecutedCycles); + //_buzzer.ProcessPulseValue(true, val); - ushort addr = _cpu.RegPC; + ushort addr = _cpu.RegPC; - if (_machine.Spectrum.SyncSettings.DeterministicEmulation) - { + if (_machine.Spectrum.SyncSettings.DeterministicEmulation) + { - } + } - var tb = DataBlocks[_currentDataBlockIndex]; - var tData = tb.BlockData; + var tb = DataBlocks[_currentDataBlockIndex]; + var tData = tb.BlockData; - if (tData == null || tData.Length < 2) - { - // skip this - return false; - } + if (tData == null || tData.Length < 2) + { + // skip this + return false; + } - var toRead = tData.Length - 1; + var toRead = tData.Length - 1; - if (toRead < _cpu.Regs[_cpu.E] + (_cpu.Regs[_cpu.D] << 8)) - { + if (toRead < _cpu.Regs[_cpu.E] + (_cpu.Regs[_cpu.D] << 8)) + { - } - else - { - toRead = _cpu.Regs[_cpu.E] + (_cpu.Regs[_cpu.D] << 8); - } + } + else + { + toRead = _cpu.Regs[_cpu.E] + (_cpu.Regs[_cpu.D] << 8); + } - if (toRead <= 0) - return false; + if (toRead <= 0) + return false; - var parity = tData[0]; + var parity = tData[0]; - if (parity != _cpu.Regs[_cpu.F_s] + (_cpu.Regs[_cpu.A_s] << 8) >> 8) - return false; + if (parity != _cpu.Regs[_cpu.F_s] + (_cpu.Regs[_cpu.A_s] << 8) >> 8) + return false; - util.SetCpuRegister("Shadow AF", 0x0145); + util.SetCpuRegister("Shadow AF", 0x0145); - for (var i = 0; i < toRead; i++) - { - var v = tData[i + 1]; - _cpu.Regs[_cpu.L] = v; - parity ^= v; - var d = (ushort)(_cpu.Regs[_cpu.Ixl] + (_cpu.Regs[_cpu.Ixh] << 8) + 1); - _machine.WriteBus(d, v); - } - var pc = (ushort)0x05DF; + for (var i = 0; i < toRead; i++) + { + var v = tData[i + 1]; + _cpu.Regs[_cpu.L] = v; + parity ^= v; + var d = (ushort)(_cpu.Regs[_cpu.Ixl] + (_cpu.Regs[_cpu.Ixh] << 8) + 1); + _machine.WriteBus(d, v); + } + var pc = (ushort)0x05DF; - if (_cpu.Regs[_cpu.E] + (_cpu.Regs[_cpu.D] << 8) == toRead && - toRead + 1 < tData.Length) - { - var v = tData[toRead + 1]; - _cpu.Regs[_cpu.L] = v; - parity ^= v; - _cpu.Regs[_cpu.B] = 0xB0; - } - else - { - _cpu.Regs[_cpu.L] = 1; - _cpu.Regs[_cpu.B] = 0; - _cpu.Regs[_cpu.F] = 0x50; - _cpu.Regs[_cpu.A] = parity; - pc = 0x05EE; - } + if (_cpu.Regs[_cpu.E] + (_cpu.Regs[_cpu.D] << 8) == toRead && + toRead + 1 < tData.Length) + { + var v = tData[toRead + 1]; + _cpu.Regs[_cpu.L] = v; + parity ^= v; + _cpu.Regs[_cpu.B] = 0xB0; + } + else + { + _cpu.Regs[_cpu.L] = 1; + _cpu.Regs[_cpu.B] = 0; + _cpu.Regs[_cpu.F] = 0x50; + _cpu.Regs[_cpu.A] = parity; + pc = 0x05EE; + } - _cpu.Regs[_cpu.H] = parity; - var de = _cpu.Regs[_cpu.E] + (_cpu.Regs[_cpu.D] << 8); - util.SetCpuRegister("DE", de - toRead); - var ix = _cpu.Regs[_cpu.Ixl] + (_cpu.Regs[_cpu.Ixh] << 8); - util.SetCpuRegister("IX", ix + toRead); + _cpu.Regs[_cpu.H] = parity; + var de = _cpu.Regs[_cpu.E] + (_cpu.Regs[_cpu.D] << 8); + util.SetCpuRegister("DE", de - toRead); + var ix = _cpu.Regs[_cpu.Ixl] + (_cpu.Regs[_cpu.Ixh] << 8); + util.SetCpuRegister("IX", ix + toRead); - util.SetCpuRegister("PC", pc); + util.SetCpuRegister("PC", pc); - _currentDataBlockIndex++; + _currentDataBlockIndex++; - return true; - - } + return true; - #endregion + } - #region TapeMonitor + #endregion - private long _lastINCycle = 0; - private int _monitorCount; - private int _monitorTimeOut; - private ushort _monitorLastPC; - private ushort[] _monitorLastRegs = new ushort[7]; + #region TapeMonitor - /// - /// Resets the TapeMonitor - /// - private void MonitorReset() - { - _lastINCycle = 0; - _monitorCount = 0; - _monitorLastPC = 0; - _monitorLastRegs = null; - } + private long _lastINCycle = 0; + private int _monitorCount; + private int _monitorTimeOut; + private ushort _monitorLastPC; + private ushort[] _monitorLastRegs = new ushort[7]; - /// - /// An iteration of the monitor process - /// - public void MonitorRead() - { - long cpuCycle = _cpu.TotalExecutedCycles; - int delta = (int)(cpuCycle - _lastINCycle); - _lastINCycle = cpuCycle; + /// + /// Resets the TapeMonitor + /// + private void MonitorReset() + { + _lastINCycle = 0; + _monitorCount = 0; + _monitorLastPC = 0; + _monitorLastRegs = null; + } - var nRegs = new ushort[] - { - _cpu.Regs[_cpu.A], - _cpu.Regs[_cpu.B], - _cpu.Regs[_cpu.C], - _cpu.Regs[_cpu.D], - _cpu.Regs[_cpu.E], - _cpu.Regs[_cpu.H], - _cpu.Regs[_cpu.L] - }; + /// + /// An iteration of the monitor process + /// + public void MonitorRead() + { + long cpuCycle = _cpu.TotalExecutedCycles; + int delta = (int)(cpuCycle - _lastINCycle); + _lastINCycle = cpuCycle; - if (delta > 0 && - delta < 96 && - _cpu.RegPC == _monitorLastPC && - _monitorLastRegs != null) - { - int dCnt = 0; - int dVal = 0; + var nRegs = new ushort[] + { + _cpu.Regs[_cpu.A], + _cpu.Regs[_cpu.B], + _cpu.Regs[_cpu.C], + _cpu.Regs[_cpu.D], + _cpu.Regs[_cpu.E], + _cpu.Regs[_cpu.H], + _cpu.Regs[_cpu.L] + }; - for (int i = 0; i < nRegs.Length; i++) - { - if (_monitorLastRegs[i] != nRegs[i]) - { - dVal = _monitorLastRegs[i] - nRegs[i]; - dCnt++; - } - } + if (delta > 0 && + delta < 96 && + _cpu.RegPC == _monitorLastPC && + _monitorLastRegs != null) + { + int dCnt = 0; + int dVal = 0; - if (dCnt == 1 && - (dVal == 1 || dVal == -1)) - { - _monitorCount++; + for (int i = 0; i < nRegs.Length; i++) + { + if (_monitorLastRegs[i] != nRegs[i]) + { + dVal = _monitorLastRegs[i] - nRegs[i]; + dCnt++; + } + } - if (_monitorCount >= 16 && _autoPlay) - { - if (!_tapeIsPlaying) - { - Play(); - _machine.Spectrum.OSD_TapePlayingAuto(); - } + if (dCnt == 1 && + (dVal == 1 || dVal == -1)) + { + _monitorCount++; - _monitorTimeOut = 50; - } - } - else - { - _monitorCount = 0; - } - } + if (_monitorCount >= 16 && _autoPlay) + { + if (!_tapeIsPlaying) + { + Play(); + _machine.Spectrum.OSD_TapePlayingAuto(); + } - _monitorLastRegs = nRegs; - _monitorLastPC = _cpu.RegPC; - } + _monitorTimeOut = 50; + } + } + else + { + _monitorCount = 0; + } + } - public void AutoStopTape() - { - if (!_tapeIsPlaying) - return; + _monitorLastRegs = nRegs; + _monitorLastPC = _cpu.RegPC; + } - if (!_autoPlay) - return; + public void AutoStopTape() + { + if (!_tapeIsPlaying) + return; - Stop(); - _machine.Spectrum.OSD_TapeStoppedAuto(); - } + if (!_autoPlay) + return; - public void AutoStartTape() - { - if (_tapeIsPlaying) - return; + Stop(); + _machine.Spectrum.OSD_TapeStoppedAuto(); + } - if (!_autoPlay) - return; + public void AutoStartTape() + { + if (_tapeIsPlaying) + return; - Play(); - _machine.Spectrum.OSD_TapePlayingAuto(); - } + if (!_autoPlay) + return; - public int MaskableInterruptCount = 0; + Play(); + _machine.Spectrum.OSD_TapePlayingAuto(); + } - private void MonitorFrame() - { - if (_tapeIsPlaying && _autoPlay) - { - if (DataBlocks.Count > 1 || - (_dataBlocks[_currentDataBlockIndex].BlockDescription != BlockType.CSW_Recording && - _dataBlocks[_currentDataBlockIndex].BlockDescription != BlockType.WAV_Recording)) - { - // we should only stop the tape when there are multiple blocks - // if we just have one big block (maybe a CSW or WAV) then auto stopping will cock things up - _monitorTimeOut--; - } + public int MaskableInterruptCount = 0; - if (_monitorTimeOut < 0) - { - if (_dataBlocks[_currentDataBlockIndex].BlockDescription != BlockType.PAUSE_BLOCK && - _dataBlocks[_currentDataBlockIndex].BlockDescription != BlockType.PAUS) - { - AutoStopTape(); - } - - return; - } + private void MonitorFrame() + { + if (_tapeIsPlaying && _autoPlay) + { + if (DataBlocks.Count > 1 || + (_dataBlocks[_currentDataBlockIndex].BlockDescription != BlockType.CSW_Recording && + _dataBlocks[_currentDataBlockIndex].BlockDescription != BlockType.WAV_Recording)) + { + // we should only stop the tape when there are multiple blocks + // if we just have one big block (maybe a CSW or WAV) then auto stopping will cock things up + _monitorTimeOut--; + } - // fallback in case usual monitor detection methods do not work + if (_monitorTimeOut < 0) + { + if (_dataBlocks[_currentDataBlockIndex].BlockDescription != BlockType.PAUSE_BLOCK && + _dataBlocks[_currentDataBlockIndex].BlockDescription != BlockType.PAUS) + { + AutoStopTape(); + } - // number of t-states since last IN operation - long diff = _machine.CPU.TotalExecutedCycles - _lastINCycle; + return; + } - // get current datablock - var block = DataBlocks[_currentDataBlockIndex]; + // fallback in case usual monitor detection methods do not work - // is this a pause block? - if (block.BlockDescription == BlockType.PAUS || block.BlockDescription == BlockType.PAUSE_BLOCK) - { - // dont autostop the tape here - return; - } + // number of t-states since last IN operation + long diff = _machine.CPU.TotalExecutedCycles - _lastINCycle; - // pause in ms at the end of the current block - int blockPause = block.PauseInMS; + // get current datablock + var block = DataBlocks[_currentDataBlockIndex]; - // timeout in t-states (equiv. to blockpause) - int timeout = ((_machine.ULADevice.FrameLength * 50) / 1000) * blockPause; + // is this a pause block? + if (block.BlockDescription == BlockType.PAUS || block.BlockDescription == BlockType.PAUSE_BLOCK) + { + // dont autostop the tape here + return; + } - // dont use autostop detection if block has no pause at the end - if (timeout == 0) - return; + // pause in ms at the end of the current block + int blockPause = block.PauseInMS; - // dont autostop if there is only 1 block - if (DataBlocks.Count == 1 || _dataBlocks[_currentDataBlockIndex].BlockDescription == BlockType.CSW_Recording || - _dataBlocks[_currentDataBlockIndex].BlockDescription == BlockType.WAV_Recording - ) - { - return; - } + // timeout in t-states (equiv. to blockpause) + int timeout = ((_machine.ULADevice.FrameLength * 50) / 1000) * blockPause; - if (diff >= timeout * 2) - { - // There have been no attempted tape reads by the CPU within the double timeout period - // Autostop the tape - AutoStopTape(); - _lastCycle = _cpu.TotalExecutedCycles; - } - } - } + // dont use autostop detection if block has no pause at the end + if (timeout == 0) + return; - #endregion + // dont autostop if there is only 1 block + if (DataBlocks.Count == 1 || _dataBlocks[_currentDataBlockIndex].BlockDescription == BlockType.CSW_Recording || + _dataBlocks[_currentDataBlockIndex].BlockDescription == BlockType.WAV_Recording + ) + { + return; + } - #region IPortIODevice + if (diff >= timeout * 2) + { + // There have been no attempted tape reads by the CPU within the double timeout period + // Autostop the tape + AutoStopTape(); + _lastCycle = _cpu.TotalExecutedCycles; + } + } + } - /// - /// Mask constants - /// - private const int TAPE_BIT = 0x40; - private const int EAR_BIT = 0x10; - private const int MIC_BIT = 0x08; + #endregion - /// - /// Device responds to an IN instruction - /// - public bool ReadPort(ushort port, ref int result) - { - if (TapeIsPlaying) - { - GetEarBit(_cpu.TotalExecutedCycles); - } - if (currentState) - { - result |= TAPE_BIT; - } - else - { - result &= ~TAPE_BIT; - } + #region IPortIODevice - if (!TapeIsPlaying) - { - if (_machine.UPDDiskDevice == null || !_machine.UPDDiskDevice.FDD_IsDiskLoaded) - MonitorRead(); - } - if (_machine.UPDDiskDevice == null || !_machine.UPDDiskDevice.FDD_IsDiskLoaded) - MonitorRead(); + /// + /// Mask constants + /// + private const int TAPE_BIT = 0x40; + private const int EAR_BIT = 0x10; + private const int MIC_BIT = 0x08; - return true; - } + /// + /// Device responds to an IN instruction + /// + public bool ReadPort(ushort port, ref int result) + { + if (TapeIsPlaying) + { + GetEarBit(_cpu.TotalExecutedCycles); + } + if (currentState) + { + result |= TAPE_BIT; + } + else + { + result &= ~TAPE_BIT; + } - /// - /// Device responds to an OUT instruction - /// - public bool WritePort(ushort port, int result) - { - if (!TapeIsPlaying) - { - currentState = ((byte)result & 0x10) != 0; - } + if (!TapeIsPlaying) + { + if (_machine.UPDDiskDevice == null || !_machine.UPDDiskDevice.FDD_IsDiskLoaded) + MonitorRead(); + } + if (_machine.UPDDiskDevice == null || !_machine.UPDDiskDevice.FDD_IsDiskLoaded) + MonitorRead(); - return true; - } + return true; + } - #endregion + /// + /// Device responds to an OUT instruction + /// + public bool WritePort(ushort port, int result) + { + if (!TapeIsPlaying) + { + currentState = ((byte)result & 0x10) != 0; + } - #region State Serialization + return true; + } - /// - /// Bizhawk state serialization - /// - public void SyncState(Serializer ser) - { - ser.BeginSection(nameof(DatacorderDevice)); - ser.Sync(nameof(counter), ref counter); - ser.Sync(nameof(_currentDataBlockIndex), ref _currentDataBlockIndex); - ser.Sync(nameof(_position), ref _position); - ser.Sync(nameof(_tapeIsPlaying), ref _tapeIsPlaying); - ser.Sync(nameof(_lastCycle), ref _lastCycle); - ser.Sync(nameof(_waitEdge), ref _waitEdge); - ser.Sync(nameof(currentState), ref currentState); - ser.Sync(nameof(_lastINCycle), ref _lastINCycle); - ser.Sync(nameof(_monitorCount), ref _monitorCount); - ser.Sync(nameof(_monitorTimeOut), ref _monitorTimeOut); - ser.Sync(nameof(_monitorLastPC), ref _monitorLastPC); - ser.Sync(nameof(_monitorLastRegs), ref _monitorLastRegs, false); - ser.EndSection(); - } + #endregion - #endregion - } + #region State Serialization + + /// + /// Bizhawk state serialization + /// + public void SyncState(Serializer ser) + { + ser.BeginSection(nameof(DatacorderDevice)); + ser.Sync(nameof(counter), ref counter); + ser.Sync(nameof(_currentDataBlockIndex), ref _currentDataBlockIndex); + ser.Sync(nameof(_position), ref _position); + ser.Sync(nameof(_tapeIsPlaying), ref _tapeIsPlaying); + ser.Sync(nameof(_lastCycle), ref _lastCycle); + ser.Sync(nameof(_waitEdge), ref _waitEdge); + ser.Sync(nameof(currentState), ref currentState); + ser.Sync(nameof(_lastINCycle), ref _lastINCycle); + ser.Sync(nameof(_monitorCount), ref _monitorCount); + ser.Sync(nameof(_monitorTimeOut), ref _monitorTimeOut); + ser.Sync(nameof(_monitorLastPC), ref _monitorLastPC); + ser.Sync(nameof(_monitorLastRegs), ref _monitorLastRegs, false); + ser.EndSection(); + } + + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Disk/CHRN.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Disk/CHRN.cs index d9f5c9822e..2595f59974 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Disk/CHRN.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Disk/CHRN.cs @@ -1,180 +1,180 @@  namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Used for the sector CHRN structure - /// - public class CHRN - { - /// - /// Track - /// - public byte C { get; set; } + /// + /// Used for the sector CHRN structure + /// + public class CHRN + { + /// + /// Track + /// + public byte C { get; set; } - /// - /// Side - /// - public byte H { get; set; } + /// + /// Side + /// + public byte H { get; set; } - /// - /// Sector ID - /// - public byte R { get; set; } + /// + /// Sector ID + /// + public byte R { get; set; } - /// - /// Sector Size - /// - public byte N { get; set; } + /// + /// Sector Size + /// + public byte N { get; set; } - /// - /// Status register 1 - /// - private byte _flag1; - public byte Flag1 - { - get { return _flag1; } - set { _flag1 = value; } - } + /// + /// Status register 1 + /// + private byte _flag1; + public byte Flag1 + { + get { return _flag1; } + set { _flag1 = value; } + } - /// - /// Status register 2 - /// - private byte _flag2; - public byte Flag2 - { - get { return _flag2; } - set { _flag2 = value; } - } + /// + /// Status register 2 + /// + private byte _flag2; + public byte Flag2 + { + get { return _flag2; } + set { _flag2 = value; } + } - /// - /// Used to store the last transmitted/received data bytes - /// - public byte[] DataBytes { get; set; } + /// + /// Used to store the last transmitted/received data bytes + /// + public byte[] DataBytes { get; set; } - /// - /// ID for the read/write data command - /// - public int DataID { get; set; } + /// + /// ID for the read/write data command + /// + public int DataID { get; set; } - #region Helper Methods + #region Helper Methods - /// - /// Missing Address Mark (Sector_ID or DAM not found) - /// - public bool ST1MA - { - get { return NECUPD765.GetBit(0, _flag1); } - set - { - if (value) { NECUPD765.SetBit(0, ref _flag1); } - else { NECUPD765.UnSetBit(0, ref _flag1); } - } - } + /// + /// Missing Address Mark (Sector_ID or DAM not found) + /// + public bool ST1MA + { + get { return NECUPD765.GetBit(0, _flag1); } + set + { + if (value) { NECUPD765.SetBit(0, ref _flag1); } + else { NECUPD765.UnSetBit(0, ref _flag1); } + } + } - /// - /// No Data (Sector_ID not found, CRC fail in ID_field) - /// - public bool ST1ND - { - get { return NECUPD765.GetBit(2, _flag1); } - set - { - if (value) { NECUPD765.SetBit(2, ref _flag1); } - else { NECUPD765.UnSetBit(2, ref _flag1); } - } - } + /// + /// No Data (Sector_ID not found, CRC fail in ID_field) + /// + public bool ST1ND + { + get { return NECUPD765.GetBit(2, _flag1); } + set + { + if (value) { NECUPD765.SetBit(2, ref _flag1); } + else { NECUPD765.UnSetBit(2, ref _flag1); } + } + } - /// - /// Data Error (CRC-fail in ID- or Data-Field) - /// - public bool ST1DE - { - get { return NECUPD765.GetBit(5, _flag1); } - set - { - if (value) { NECUPD765.SetBit(5, ref _flag1); } - else { NECUPD765.UnSetBit(5, ref _flag1); } - } - } + /// + /// Data Error (CRC-fail in ID- or Data-Field) + /// + public bool ST1DE + { + get { return NECUPD765.GetBit(5, _flag1); } + set + { + if (value) { NECUPD765.SetBit(5, ref _flag1); } + else { NECUPD765.UnSetBit(5, ref _flag1); } + } + } - /// - /// End of Track (set past most read/write commands) (see IC) - /// - public bool ST1EN - { - get { return NECUPD765.GetBit(7, _flag1); } - set - { - if (value) { NECUPD765.SetBit(7, ref _flag1); } - else { NECUPD765.UnSetBit(7, ref _flag1); } - } - } + /// + /// End of Track (set past most read/write commands) (see IC) + /// + public bool ST1EN + { + get { return NECUPD765.GetBit(7, _flag1); } + set + { + if (value) { NECUPD765.SetBit(7, ref _flag1); } + else { NECUPD765.UnSetBit(7, ref _flag1); } + } + } - /// - /// Missing Address Mark in Data Field (DAM not found) - /// - public bool ST2MD - { - get { return NECUPD765.GetBit(0, _flag2); } - set - { - if (value) { NECUPD765.SetBit(0, ref _flag2); } - else { NECUPD765.UnSetBit(0, ref _flag2); } - } - } + /// + /// Missing Address Mark in Data Field (DAM not found) + /// + public bool ST2MD + { + get { return NECUPD765.GetBit(0, _flag2); } + set + { + if (value) { NECUPD765.SetBit(0, ref _flag2); } + else { NECUPD765.UnSetBit(0, ref _flag2); } + } + } - /// - /// Bad Cylinder (read/programmed track-ID different and read-ID = FF) - /// - public bool ST2BC - { - get { return NECUPD765.GetBit(1, _flag2); } - set - { - if (value) { NECUPD765.SetBit(1, ref _flag2); } - else { NECUPD765.UnSetBit(1, ref _flag2); } - } - } + /// + /// Bad Cylinder (read/programmed track-ID different and read-ID = FF) + /// + public bool ST2BC + { + get { return NECUPD765.GetBit(1, _flag2); } + set + { + if (value) { NECUPD765.SetBit(1, ref _flag2); } + else { NECUPD765.UnSetBit(1, ref _flag2); } + } + } - /// - /// Wrong Cylinder (read/programmed track-ID different) (see b1) - /// - public bool ST2WC - { - get { return NECUPD765.GetBit(4, _flag2); } - set - { - if (value) { NECUPD765.SetBit(4, ref _flag2); } - else { NECUPD765.UnSetBit(4, ref _flag2); } - } - } + /// + /// Wrong Cylinder (read/programmed track-ID different) (see b1) + /// + public bool ST2WC + { + get { return NECUPD765.GetBit(4, _flag2); } + set + { + if (value) { NECUPD765.SetBit(4, ref _flag2); } + else { NECUPD765.UnSetBit(4, ref _flag2); } + } + } - /// - /// Data Error in Data Field (CRC-fail in data-field) - /// - public bool ST2DD - { - get { return NECUPD765.GetBit(5, _flag2); } - set - { - if (value) { NECUPD765.SetBit(5, ref _flag2); } - else { NECUPD765.UnSetBit(5, ref _flag2); } - } - } + /// + /// Data Error in Data Field (CRC-fail in data-field) + /// + public bool ST2DD + { + get { return NECUPD765.GetBit(5, _flag2); } + set + { + if (value) { NECUPD765.SetBit(5, ref _flag2); } + else { NECUPD765.UnSetBit(5, ref _flag2); } + } + } - /// - /// Control Mark (read/scan command found sector with deleted DAM) - /// - public bool ST2CM - { - get { return NECUPD765.GetBit(6, _flag2); } - set - { - if (value) { NECUPD765.SetBit(6, ref _flag2); } - else { NECUPD765.UnSetBit(6, ref _flag2); } - } - } + /// + /// Control Mark (read/scan command found sector with deleted DAM) + /// + public bool ST2CM + { + get { return NECUPD765.GetBit(6, _flag2); } + set + { + if (value) { NECUPD765.SetBit(6, ref _flag2); } + else { NECUPD765.UnSetBit(6, ref _flag2); } + } + } - #endregion - } + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Disk/NECUPD765.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Disk/NECUPD765.cs index c4b8d755a4..5b49dbaf97 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Disk/NECUPD765.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Disk/NECUPD765.cs @@ -3,243 +3,243 @@ using System.Collections.Generic; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// The NEC floppy disk controller (and floppy drive) found in the +3 - /// - #region Attribution - /* + /// + /// The NEC floppy disk controller (and floppy drive) found in the +3 + /// + #region Attribution + /* Implementation based on the information contained here: http://www.cpcwiki.eu/index.php/765_FDC and here: http://www.cpcwiki.eu/imgs/f/f3/UPD765_Datasheet_OCRed.pdf */ - #endregion - public partial class NECUPD765 - { - #region Devices + #endregion + public partial class NECUPD765 + { + #region Devices - /// - /// The emulated spectrum machine - /// - private SpectrumBase _machine; + /// + /// The emulated spectrum machine + /// + private SpectrumBase _machine; - #endregion + #endregion - #region Construction & Initialization + #region Construction & Initialization - /// - /// Main constructor - /// - public NECUPD765() - { - InitCommandList(); - } + /// + /// Main constructor + /// + public NECUPD765() + { + InitCommandList(); + } - /// - /// Initialization routine - /// - public void Init(SpectrumBase machine) - { - _machine = machine; - FDD_Init(); - TimingInit(); - Reset(); - } - - /// - /// Resets the FDC - /// - public void Reset() - { - // setup main status - StatusMain = 0; + /// + /// Initialization routine + /// + public void Init(SpectrumBase machine) + { + _machine = machine; + FDD_Init(); + TimingInit(); + Reset(); + } - Status0 = 0; - Status1 = 0; - Status2 = 0; - Status3 = 0; + /// + /// Resets the FDC + /// + public void Reset() + { + // setup main status + StatusMain = 0; - SetBit(MSR_RQM, ref StatusMain); + Status0 = 0; + Status1 = 0; + Status2 = 0; + Status3 = 0; - SetPhase_Idle(); + SetBit(MSR_RQM, ref StatusMain); - //FDC_FLAG_RQM = true; - //ActiveDirection = CommandDirection.IN; - SRT = 6; - HUT = 16; - HLT = 2; - HLT_Counter = 0; - HUT_Counter = 0; - IndexPulseCounter = 0; - CMD_FLAG_MF = false; + SetPhase_Idle(); - foreach (var d in DriveStates) - { - //d.SeekingTrack = d.CurrentTrack; - ////d.SeekCounter = 0; - //d.FLAG_SEEK_INTERRUPT = false; - //d.IntStatus = 0; - //d.SeekState = SeekSubState.Idle; - //d.SeekIntState = SeekIntStatus.Normal; + //FDC_FLAG_RQM = true; + //ActiveDirection = CommandDirection.IN; + SRT = 6; + HUT = 16; + HLT = 2; + HLT_Counter = 0; + HUT_Counter = 0; + IndexPulseCounter = 0; + CMD_FLAG_MF = false; - } - - } + foreach (var d in DriveStates) + { + //d.SeekingTrack = d.CurrentTrack; + ////d.SeekCounter = 0; + //d.FLAG_SEEK_INTERRUPT = false; + //d.IntStatus = 0; + //d.SeekState = SeekSubState.Idle; + //d.SeekIntState = SeekIntStatus.Normal; - /// - /// Setup the command structure - /// Each command represents one of the internal UPD765 commands - /// - private void InitCommandList() - { - CommandList = new List - { + } + + } + + /// + /// Setup the command structure + /// Each command represents one of the internal UPD765 commands + /// + private void InitCommandList() + { + CommandList = new List + { // read data new Command { CommandDelegate = UPD_ReadData, CommandCode = 0x06, MT = true, MF = true, SK = true, IsRead = true, - Direction = CommandDirection.OUT, ParameterByteCount = 8, ResultByteCount = 7 }, + Direction = CommandDirection.OUT, ParameterByteCount = 8, ResultByteCount = 7 }, // read id new Command { CommandDelegate = UPD_ReadID, CommandCode = 0x0a, MF = true, IsRead = true, - Direction = CommandDirection.OUT, ParameterByteCount = 1, ResultByteCount = 7 }, + Direction = CommandDirection.OUT, ParameterByteCount = 1, ResultByteCount = 7 }, // specify new Command { CommandDelegate = UPD_Specify, CommandCode = 0x03, - Direction = CommandDirection.OUT, ParameterByteCount = 2, ResultByteCount = 0 }, + Direction = CommandDirection.OUT, ParameterByteCount = 2, ResultByteCount = 0 }, // read diagnostic new Command { CommandDelegate = UPD_ReadDiagnostic, CommandCode = 0x02, MF = true, SK = true, IsRead = true, - Direction = CommandDirection.OUT, ParameterByteCount = 8, ResultByteCount = 7 }, + Direction = CommandDirection.OUT, ParameterByteCount = 8, ResultByteCount = 7 }, // scan equal new Command { CommandDelegate = UPD_ScanEqual, CommandCode = 0x11, MT = true, MF = true, SK = true, IsRead = true, - Direction = CommandDirection.IN, ParameterByteCount = 8, ResultByteCount = 7 }, + Direction = CommandDirection.IN, ParameterByteCount = 8, ResultByteCount = 7 }, // scan high or equal new Command { CommandDelegate = UPD_ScanHighOrEqual, CommandCode = 0x1d, MT = true, MF = true, SK = true, IsRead = true, - Direction = CommandDirection.IN, ParameterByteCount = 8, ResultByteCount = 7 }, + Direction = CommandDirection.IN, ParameterByteCount = 8, ResultByteCount = 7 }, // scan low or equal new Command { CommandDelegate = UPD_ScanLowOrEqual, CommandCode = 0x19, MT = true, MF = true, SK = true, IsRead = true, - Direction = CommandDirection.IN, ParameterByteCount = 8, ResultByteCount = 7 }, + Direction = CommandDirection.IN, ParameterByteCount = 8, ResultByteCount = 7 }, // read deleted data new Command { CommandDelegate = UPD_ReadDeletedData, CommandCode = 0x0c, MT = true, MF = true, SK = true, IsRead = true, - Direction = CommandDirection.OUT, ParameterByteCount = 8, ResultByteCount = 7 }, + Direction = CommandDirection.OUT, ParameterByteCount = 8, ResultByteCount = 7 }, // write data new Command { CommandDelegate = UPD_WriteData, CommandCode = 0x05, MT = true, MF = true, IsWrite = true, - Direction = CommandDirection.IN, ParameterByteCount = 8, ResultByteCount = 7 }, + Direction = CommandDirection.IN, ParameterByteCount = 8, ResultByteCount = 7 }, // write id new Command { CommandDelegate = UPD_WriteID, CommandCode = 0x0d, MF = true, IsWrite = true, - Direction = CommandDirection.IN, ParameterByteCount = 5, ResultByteCount = 7 }, + Direction = CommandDirection.IN, ParameterByteCount = 5, ResultByteCount = 7 }, // write deleted data new Command { CommandDelegate = UPD_WriteDeletedData, CommandCode = 0x09, MT = true, MF = true, IsWrite = true, - Direction = CommandDirection.IN, ParameterByteCount = 8, ResultByteCount = 7 }, + Direction = CommandDirection.IN, ParameterByteCount = 8, ResultByteCount = 7 }, // seek new Command { CommandDelegate = UPD_Seek, CommandCode = 0x0f, - Direction = CommandDirection.OUT, ParameterByteCount = 2, ResultByteCount = 0 }, + Direction = CommandDirection.OUT, ParameterByteCount = 2, ResultByteCount = 0 }, // recalibrate (seek track00) new Command { CommandDelegate = UPD_Recalibrate, CommandCode = 0x07, - Direction = CommandDirection.OUT, ParameterByteCount = 1, ResultByteCount = 0 }, + Direction = CommandDirection.OUT, ParameterByteCount = 1, ResultByteCount = 0 }, // sense interrupt status new Command { CommandDelegate = UPD_SenseInterruptStatus, CommandCode = 0x08, - Direction = CommandDirection.OUT, ParameterByteCount = 0, ResultByteCount = 2 }, + Direction = CommandDirection.OUT, ParameterByteCount = 0, ResultByteCount = 2 }, // sense drive status new Command { CommandDelegate = UPD_SenseDriveStatus, CommandCode = 0x04, - Direction = CommandDirection.OUT, ParameterByteCount = 1, ResultByteCount = 1 }, + Direction = CommandDirection.OUT, ParameterByteCount = 1, ResultByteCount = 1 }, // version new Command { CommandDelegate = UPD_Version, CommandCode = 0x10, - Direction = CommandDirection.OUT, ParameterByteCount = 0, ResultByteCount = 1 }, + Direction = CommandDirection.OUT, ParameterByteCount = 0, ResultByteCount = 1 }, // invalid new Command { CommandDelegate = UPD_Invalid, CommandCode = 0x00, - Direction = CommandDirection.OUT, ParameterByteCount = 0, ResultByteCount = 1 }, - }; - } + Direction = CommandDirection.OUT, ParameterByteCount = 0, ResultByteCount = 1 }, + }; + } - #endregion + #endregion - #region State Serialization + #region State Serialization - public void SyncState(Serializer ser) - { - ser.BeginSection("NEC-UPD765"); + public void SyncState(Serializer ser) + { + ser.BeginSection("NEC-UPD765"); - #region FDD - - ser.Sync(nameof(FDD_FLAG_MOTOR), ref FDD_FLAG_MOTOR); + #region FDD - for (int i = 0; i < 4; i++) - { - ser.BeginSection("HITDrive_" + i); - DriveStates[i].SyncState(ser); - ser.EndSection(); - } + ser.Sync(nameof(FDD_FLAG_MOTOR), ref FDD_FLAG_MOTOR); - ser.Sync(nameof(DiskDriveIndex), ref _diskDriveIndex); - // set active drive - DiskDriveIndex = _diskDriveIndex; + for (int i = 0; i < 4; i++) + { + ser.BeginSection("HITDrive_" + i); + DriveStates[i].SyncState(ser); + ser.EndSection(); + } - #endregion + ser.Sync(nameof(DiskDriveIndex), ref _diskDriveIndex); + // set active drive + DiskDriveIndex = _diskDriveIndex; - #region Registers + #endregion - ser.Sync("_RegMain", ref StatusMain); - ser.Sync("_Reg0", ref Status0); - ser.Sync("_Reg1", ref Status1); - ser.Sync("_Reg2", ref Status2); - ser.Sync("_Reg3", ref Status3); + #region Registers - #endregion + ser.Sync("_RegMain", ref StatusMain); + ser.Sync("_Reg0", ref Status0); + ser.Sync("_Reg1", ref Status1); + ser.Sync("_Reg2", ref Status2); + ser.Sync("_Reg3", ref Status3); - #region Controller state + #endregion - ser.Sync(nameof(DriveLight), ref DriveLight); - ser.SyncEnum(nameof(ActivePhase), ref ActivePhase); - //ser.SyncEnum(nameof(ActiveDirection), ref ActiveDirection); - ser.SyncEnum(nameof(ActiveInterrupt), ref ActiveInterrupt); - ser.Sync(nameof(CommBuffer), ref CommBuffer, false); - ser.Sync(nameof(CommCounter), ref CommCounter); - ser.Sync(nameof(ResBuffer), ref ResBuffer, false); - ser.Sync(nameof(ExecBuffer), ref ExecBuffer, false); - ser.Sync(nameof(ExecCounter), ref ExecCounter); - ser.Sync(nameof(ExecLength), ref ExecLength); - ser.Sync(nameof(InterruptResultBuffer), ref InterruptResultBuffer, false); - ser.Sync(nameof(ResCounter), ref ResCounter); - ser.Sync(nameof(ResLength), ref ResLength); - ser.Sync(nameof(LastSectorDataWriteByte), ref LastSectorDataWriteByte); - ser.Sync(nameof(LastSectorDataReadByte), ref LastSectorDataReadByte); - ser.Sync(nameof(LastByteReceived), ref LastByteReceived); - - ser.Sync(nameof(_cmdIndex), ref _cmdIndex); - // resync the ActiveCommand - CMDIndex = _cmdIndex; + #region Controller state - ActiveCommandParams.SyncState(ser); - - ser.Sync(nameof(IndexPulseCounter), ref IndexPulseCounter); - //ser.SyncEnum(nameof(_activeStatus), ref _activeStatus); - //ser.SyncEnum(nameof(_statusRaised), ref _statusRaised); + ser.Sync(nameof(DriveLight), ref DriveLight); + ser.SyncEnum(nameof(ActivePhase), ref ActivePhase); + //ser.SyncEnum(nameof(ActiveDirection), ref ActiveDirection); + ser.SyncEnum(nameof(ActiveInterrupt), ref ActiveInterrupt); + ser.Sync(nameof(CommBuffer), ref CommBuffer, false); + ser.Sync(nameof(CommCounter), ref CommCounter); + ser.Sync(nameof(ResBuffer), ref ResBuffer, false); + ser.Sync(nameof(ExecBuffer), ref ExecBuffer, false); + ser.Sync(nameof(ExecCounter), ref ExecCounter); + ser.Sync(nameof(ExecLength), ref ExecLength); + ser.Sync(nameof(InterruptResultBuffer), ref InterruptResultBuffer, false); + ser.Sync(nameof(ResCounter), ref ResCounter); + ser.Sync(nameof(ResLength), ref ResLength); + ser.Sync(nameof(LastSectorDataWriteByte), ref LastSectorDataWriteByte); + ser.Sync(nameof(LastSectorDataReadByte), ref LastSectorDataReadByte); + ser.Sync(nameof(LastByteReceived), ref LastByteReceived); - ser.Sync(nameof(CMD_FLAG_MT), ref CMD_FLAG_MT); - ser.Sync(nameof(CMD_FLAG_MF), ref CMD_FLAG_MF); - ser.Sync(nameof(CMD_FLAG_SK), ref CMD_FLAG_SK); - ser.Sync(nameof(SRT), ref SRT); - ser.Sync(nameof(HUT), ref HUT); - ser.Sync(nameof(HLT), ref HLT); - ser.Sync(nameof(ND), ref ND); - ser.Sync(nameof(SRT_Counter), ref SRT_Counter); - ser.Sync(nameof(HUT_Counter), ref HUT_Counter); - ser.Sync(nameof(HLT_Counter), ref HLT_Counter); + ser.Sync(nameof(_cmdIndex), ref _cmdIndex); + // resync the ActiveCommand + CMDIndex = _cmdIndex; - ser.Sync(nameof(SectorDelayCounter), ref SectorDelayCounter); - ser.Sync(nameof(SectorID), ref SectorID); + ActiveCommandParams.SyncState(ser); - #endregion + ser.Sync(nameof(IndexPulseCounter), ref IndexPulseCounter); + //ser.SyncEnum(nameof(_activeStatus), ref _activeStatus); + //ser.SyncEnum(nameof(_statusRaised), ref _statusRaised); - #region Timing + ser.Sync(nameof(CMD_FLAG_MT), ref CMD_FLAG_MT); + ser.Sync(nameof(CMD_FLAG_MF), ref CMD_FLAG_MF); + ser.Sync(nameof(CMD_FLAG_SK), ref CMD_FLAG_SK); + ser.Sync(nameof(SRT), ref SRT); + ser.Sync(nameof(HUT), ref HUT); + ser.Sync(nameof(HLT), ref HLT); + ser.Sync(nameof(ND), ref ND); + ser.Sync(nameof(SRT_Counter), ref SRT_Counter); + ser.Sync(nameof(HUT_Counter), ref HUT_Counter); + ser.Sync(nameof(HLT_Counter), ref HLT_Counter); - ser.Sync(nameof(LastCPUCycle), ref LastCPUCycle); - ser.Sync(nameof(StatusDelay), ref StatusDelay); - ser.Sync(nameof(TickCounter), ref TickCounter); - ser.Sync(nameof(DriveCycleCounter), ref DriveCycleCounter); + ser.Sync(nameof(SectorDelayCounter), ref SectorDelayCounter); + ser.Sync(nameof(SectorID), ref SectorID); - #endregion + #endregion - ser.EndSection(); - } + #region Timing - #endregion - } + ser.Sync(nameof(LastCPUCycle), ref LastCPUCycle); + ser.Sync(nameof(StatusDelay), ref StatusDelay); + ser.Sync(nameof(TickCounter), ref TickCounter); + ser.Sync(nameof(DriveCycleCounter), ref DriveCycleCounter); + + #endregion + + ser.EndSection(); + } + + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Input/CursorJoystick.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Input/CursorJoystick.cs index 45e19d65e0..a648f88bad 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Input/CursorJoystick.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Input/CursorJoystick.cs @@ -3,104 +3,104 @@ using System.Collections.Generic; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Cursor joystick - /// Maps to a combination of 0xf7fe and 0xeffe - /// - public class CursorJoystick : IJoystick - { - //private int _joyLine; - private SpectrumBase _machine; + /// + /// Cursor joystick + /// Maps to a combination of 0xf7fe and 0xeffe + /// + public class CursorJoystick : IJoystick + { + //private int _joyLine; + private SpectrumBase _machine; - #region Construction + #region Construction - public CursorJoystick(SpectrumBase machine, int playerNumber) - { - _machine = machine; - //_joyLine = 0; - _playerNumber = playerNumber; + public CursorJoystick(SpectrumBase machine, int playerNumber) + { + _machine = machine; + //_joyLine = 0; + _playerNumber = playerNumber; - ButtonCollection = new List - { - "P" + _playerNumber + " Left", - "P" + _playerNumber + " Right", - "P" + _playerNumber + " Down", - "P" + _playerNumber + " Up", - "P" + _playerNumber + " Button", - }.ToArray(); - } + ButtonCollection = new List + { + "P" + _playerNumber + " Left", + "P" + _playerNumber + " Right", + "P" + _playerNumber + " Down", + "P" + _playerNumber + " Up", + "P" + _playerNumber + " Button", + }.ToArray(); + } - private List btnLookups = new List - { - "Key 5", // left + private List btnLookups = new List + { + "Key 5", // left "Key 8", // right "Key 6", // down "Key 7", // up "Key 0", // fire }; - #endregion + #endregion - #region IJoystick + #region IJoystick - public JoystickType JoyType => JoystickType.Cursor; + public JoystickType JoyType => JoystickType.Cursor; - public string[] ButtonCollection { get; set; } + public string[] ButtonCollection { get; set; } - private int _playerNumber; - public int PlayerNumber - { - get { return _playerNumber; } - set { _playerNumber = value; } - } + private int _playerNumber; + public int PlayerNumber + { + get { return _playerNumber; } + set { _playerNumber = value; } + } - /// - /// Sets the joystick line based on key pressed - /// - public void SetJoyInput(string key, bool isPressed) - { - var pos = GetBitPos(key); + /// + /// Sets the joystick line based on key pressed + /// + public void SetJoyInput(string key, bool isPressed) + { + var pos = GetBitPos(key); - if (isPressed) - { - _machine.KeyboardDevice.SetKeyStatus(btnLookups[pos], true); - } - else - { - if (_machine.KeyboardDevice.GetKeyStatus(btnLookups[pos])) - { - // key is already pressed elswhere - leave it as is - } - else - { - // key is safe to unpress - _machine.KeyboardDevice.SetKeyStatus(btnLookups[pos], false); - } - } - } + if (isPressed) + { + _machine.KeyboardDevice.SetKeyStatus(btnLookups[pos], true); + } + else + { + if (_machine.KeyboardDevice.GetKeyStatus(btnLookups[pos])) + { + // key is already pressed elswhere - leave it as is + } + else + { + // key is safe to unpress + _machine.KeyboardDevice.SetKeyStatus(btnLookups[pos], false); + } + } + } - /// - /// Gets the state of a particular joystick binding - /// - public bool GetJoyInput(string key) - { - var pos = GetBitPos(key); - if (_machine == null) - return false; + /// + /// Gets the state of a particular joystick binding + /// + public bool GetJoyInput(string key) + { + var pos = GetBitPos(key); + if (_machine == null) + return false; - var l = _machine.KeyboardDevice.GetKeyStatus(btnLookups[pos]); - return l; - } - - #endregion + var l = _machine.KeyboardDevice.GetKeyStatus(btnLookups[pos]); + return l; + } - /// - /// Gets the bit position of a particular joystick binding from the matrix - /// - public int GetBitPos(string key) - { - int index = Array.IndexOf(ButtonCollection, key); - return index; - } - } + #endregion + + /// + /// Gets the bit position of a particular joystick binding from the matrix + /// + public int GetBitPos(string key) + { + int index = Array.IndexOf(ButtonCollection, key); + return index; + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Input/KempstonJoystick.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Input/KempstonJoystick.cs index 733d33e6b1..b3f4ac04c5 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Input/KempstonJoystick.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Input/KempstonJoystick.cs @@ -3,88 +3,88 @@ using System.Collections.Generic; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - public class KempstonJoystick : IJoystick - { - private int _joyLine; - private SpectrumBase _machine; + public class KempstonJoystick : IJoystick + { + private int _joyLine; + private SpectrumBase _machine; - #region Construction + #region Construction - public KempstonJoystick(SpectrumBase machine, int playerNumber) - { - _machine = machine; - _joyLine = 0; - _playerNumber = playerNumber; + public KempstonJoystick(SpectrumBase machine, int playerNumber) + { + _machine = machine; + _joyLine = 0; + _playerNumber = playerNumber; - ButtonCollection = new List - { - "P" + _playerNumber + " Right", - "P" + _playerNumber + " Left", - "P" + _playerNumber + " Down", - "P" + _playerNumber + " Up", - "P" + _playerNumber + " Button", - }.ToArray(); - } + ButtonCollection = new List + { + "P" + _playerNumber + " Right", + "P" + _playerNumber + " Left", + "P" + _playerNumber + " Down", + "P" + _playerNumber + " Up", + "P" + _playerNumber + " Button", + }.ToArray(); + } - #endregion + #endregion - #region IJoystick + #region IJoystick - public JoystickType JoyType => JoystickType.Kempston; + public JoystickType JoyType => JoystickType.Kempston; - public string[] ButtonCollection { get; set; } + public string[] ButtonCollection { get; set; } - private int _playerNumber; - public int PlayerNumber - { - get { return _playerNumber; } - set { _playerNumber = value; } - } + private int _playerNumber; + public int PlayerNumber + { + get { return _playerNumber; } + set { _playerNumber = value; } + } - /// - /// Sets the joystick line based on key pressed - /// - public void SetJoyInput(string key, bool isPressed) - { - var pos = GetBitPos(key); - if (isPressed) - _joyLine |= (1 << pos); - else - _joyLine &= ~(1 << pos); - } + /// + /// Sets the joystick line based on key pressed + /// + public void SetJoyInput(string key, bool isPressed) + { + var pos = GetBitPos(key); + if (isPressed) + _joyLine |= (1 << pos); + else + _joyLine &= ~(1 << pos); + } - /// - /// Gets the state of a particular joystick binding - /// - public bool GetJoyInput(string key) - { - var pos = GetBitPos(key); - return (_joyLine & (1 << pos)) != 0; - } - - #endregion + /// + /// Gets the state of a particular joystick binding + /// + public bool GetJoyInput(string key) + { + var pos = GetBitPos(key); + return (_joyLine & (1 << pos)) != 0; + } - /// - /// Active bits high - /// 0 0 0 F U D L R - /// - public int JoyLine - { - get { return _joyLine; } - set { _joyLine = value; } - } + #endregion - /// - /// Gets the bit position of a particular joystick binding from the matrix - /// - public int GetBitPos(string key) - { - int index = Array.IndexOf(ButtonCollection, key); - return index; - } + /// + /// Active bits high + /// 0 0 0 F U D L R + /// + public int JoyLine + { + get { return _joyLine; } + set { _joyLine = value; } + } + + /// + /// Gets the bit position of a particular joystick binding from the matrix + /// + public int GetBitPos(string key) + { + int index = Array.IndexOf(ButtonCollection, key); + return index; + } - /* + /* public readonly string[] _bitPos = new string[] { "P1 Right", @@ -94,5 +94,5 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum "P1 Button" }; */ - } + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Input/NullJoystick.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Input/NullJoystick.cs index ac4fd96c55..2383632927 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Input/NullJoystick.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Input/NullJoystick.cs @@ -3,87 +3,87 @@ using System.Collections.Generic; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// A null joystick object - /// - public class NullJoystick : IJoystick - { - private int _joyLine; - private SpectrumBase _machine; + /// + /// A null joystick object + /// + public class NullJoystick : IJoystick + { + private int _joyLine; + private SpectrumBase _machine; - #region Construction + #region Construction - public NullJoystick(SpectrumBase machine, int playerNumber) - { - _machine = machine; - _joyLine = 0; - _playerNumber = playerNumber; + public NullJoystick(SpectrumBase machine, int playerNumber) + { + _machine = machine; + _joyLine = 0; + _playerNumber = playerNumber; - ButtonCollection = new List - { + ButtonCollection = new List + { - }.ToArray(); - } + }.ToArray(); + } - #endregion + #endregion - #region IJoystick + #region IJoystick - public JoystickType JoyType => JoystickType.NULL; + public JoystickType JoyType => JoystickType.NULL; - public string[] ButtonCollection { get; set; } + public string[] ButtonCollection { get; set; } - private int _playerNumber; - public int PlayerNumber - { - get { return _playerNumber; } - set { _playerNumber = value; } - } + private int _playerNumber; + public int PlayerNumber + { + get { return _playerNumber; } + set { _playerNumber = value; } + } - /// - /// Sets the joystick line based on key pressed - /// - public void SetJoyInput(string key, bool isPressed) - { - var pos = GetBitPos(key); - if (isPressed) - _joyLine |= (1 << pos); - else - _joyLine &= ~(1 << pos); - } + /// + /// Sets the joystick line based on key pressed + /// + public void SetJoyInput(string key, bool isPressed) + { + var pos = GetBitPos(key); + if (isPressed) + _joyLine |= (1 << pos); + else + _joyLine &= ~(1 << pos); + } - /// - /// Gets the state of a particular joystick binding - /// - public bool GetJoyInput(string key) - { - var pos = GetBitPos(key); - return (_joyLine & (1 << pos)) != 0; - } - - #endregion + /// + /// Gets the state of a particular joystick binding + /// + public bool GetJoyInput(string key) + { + var pos = GetBitPos(key); + return (_joyLine & (1 << pos)) != 0; + } - /// - /// Active bits high - /// 0 0 0 F U D L R - /// - public int JoyLine - { - get { return _joyLine; } - set { _joyLine = value; } - } + #endregion - /// - /// Gets the bit position of a particular joystick binding from the matrix - /// - public int GetBitPos(string key) - { - int index = Array.IndexOf(ButtonCollection, key); - return index; - } + /// + /// Active bits high + /// 0 0 0 F U D L R + /// + public int JoyLine + { + get { return _joyLine; } + set { _joyLine = value; } + } + + /// + /// Gets the bit position of a particular joystick binding from the matrix + /// + public int GetBitPos(string key) + { + int index = Array.IndexOf(ButtonCollection, key); + return index; + } - /* + /* public readonly string[] _bitPos = new string[] { "P1 Right", @@ -93,5 +93,5 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum "P1 Button" }; */ - } + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Input/SinclairJoystick1.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Input/SinclairJoystick1.cs index f96c125153..f1d4e6ea3f 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Input/SinclairJoystick1.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Input/SinclairJoystick1.cs @@ -3,103 +3,103 @@ using System.Collections.Generic; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Sinclair Joystick LEFT - /// Just maps to the standard keyboard and is read the same (from port 0xf7fe) - /// - public class SinclairJoystick1 : IJoystick - { - //private int _joyLine; - private SpectrumBase _machine; + /// + /// Sinclair Joystick LEFT + /// Just maps to the standard keyboard and is read the same (from port 0xf7fe) + /// + public class SinclairJoystick1 : IJoystick + { + //private int _joyLine; + private SpectrumBase _machine; - #region Construction + #region Construction - public SinclairJoystick1(SpectrumBase machine, int playerNumber) - { - _machine = machine; - //_joyLine = 0; - _playerNumber = playerNumber; + public SinclairJoystick1(SpectrumBase machine, int playerNumber) + { + _machine = machine; + //_joyLine = 0; + _playerNumber = playerNumber; - ButtonCollection = new List - { - "P" + _playerNumber + " Left", - "P" + _playerNumber + " Right", - "P" + _playerNumber + " Down", - "P" + _playerNumber + " Up", - "P" + _playerNumber + " Button", - }.ToArray(); - } + ButtonCollection = new List + { + "P" + _playerNumber + " Left", + "P" + _playerNumber + " Right", + "P" + _playerNumber + " Down", + "P" + _playerNumber + " Up", + "P" + _playerNumber + " Button", + }.ToArray(); + } - private List btnLookups = new List - { - "Key 1", // left + private List btnLookups = new List + { + "Key 1", // left "Key 2", // right "Key 3", // down "Key 4", // up "Key 5", // fire }; - #endregion + #endregion - #region IJoystick + #region IJoystick - public JoystickType JoyType => JoystickType.SinclairLEFT; + public JoystickType JoyType => JoystickType.SinclairLEFT; - public string[] ButtonCollection { get; set; } + public string[] ButtonCollection { get; set; } - private int _playerNumber; - public int PlayerNumber - { - get { return _playerNumber; } - set { _playerNumber = value; } - } + private int _playerNumber; + public int PlayerNumber + { + get { return _playerNumber; } + set { _playerNumber = value; } + } - /// - /// Sets the joystick line based on key pressed - /// - public void SetJoyInput(string key, bool isPressed) - { - var pos = GetBitPos(key); + /// + /// Sets the joystick line based on key pressed + /// + public void SetJoyInput(string key, bool isPressed) + { + var pos = GetBitPos(key); - if (isPressed) - { - _machine.KeyboardDevice.SetKeyStatus(btnLookups[pos], true); - } - else - { - if (_machine.KeyboardDevice.GetKeyStatus(btnLookups[pos])) - { - // key is already pressed elswhere - leave it as is - } - else - { - // key is safe to unpress - _machine.KeyboardDevice.SetKeyStatus(btnLookups[pos], false); - } - } - } + if (isPressed) + { + _machine.KeyboardDevice.SetKeyStatus(btnLookups[pos], true); + } + else + { + if (_machine.KeyboardDevice.GetKeyStatus(btnLookups[pos])) + { + // key is already pressed elswhere - leave it as is + } + else + { + // key is safe to unpress + _machine.KeyboardDevice.SetKeyStatus(btnLookups[pos], false); + } + } + } - /// - /// Gets the state of a particular joystick binding - /// - public bool GetJoyInput(string key) - { - var pos = GetBitPos(key); - if (_machine == null) - return false; + /// + /// Gets the state of a particular joystick binding + /// + public bool GetJoyInput(string key) + { + var pos = GetBitPos(key); + if (_machine == null) + return false; - return _machine.KeyboardDevice.GetKeyStatus(btnLookups[pos]); - } - - #endregion + return _machine.KeyboardDevice.GetKeyStatus(btnLookups[pos]); + } - /// - /// Gets the bit position of a particular joystick binding from the matrix - /// - public int GetBitPos(string key) - { - int index = Array.IndexOf(ButtonCollection, key); - return index; - } - } + #endregion + + /// + /// Gets the bit position of a particular joystick binding from the matrix + /// + public int GetBitPos(string key) + { + int index = Array.IndexOf(ButtonCollection, key); + return index; + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Input/SinclairJoystick2.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Input/SinclairJoystick2.cs index 59c1922372..e623fac8e5 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Input/SinclairJoystick2.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Input/SinclairJoystick2.cs @@ -3,103 +3,103 @@ using System.Collections.Generic; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Sinclair Joystick RIGHT - /// Just maps to the standard keyboard and is read the same (from port 0xeffe) - /// - public class SinclairJoystick2 : IJoystick - { - //private int _joyLine; - private SpectrumBase _machine; + /// + /// Sinclair Joystick RIGHT + /// Just maps to the standard keyboard and is read the same (from port 0xeffe) + /// + public class SinclairJoystick2 : IJoystick + { + //private int _joyLine; + private SpectrumBase _machine; - #region Construction + #region Construction - public SinclairJoystick2(SpectrumBase machine, int playerNumber) - { - _machine = machine; - //_joyLine = 0; - _playerNumber = playerNumber; + public SinclairJoystick2(SpectrumBase machine, int playerNumber) + { + _machine = machine; + //_joyLine = 0; + _playerNumber = playerNumber; - ButtonCollection = new List - { - "P" + _playerNumber + " Left", - "P" + _playerNumber + " Right", - "P" + _playerNumber + " Down", - "P" + _playerNumber + " Up", - "P" + _playerNumber + " Button", - }.ToArray(); - } + ButtonCollection = new List + { + "P" + _playerNumber + " Left", + "P" + _playerNumber + " Right", + "P" + _playerNumber + " Down", + "P" + _playerNumber + " Up", + "P" + _playerNumber + " Button", + }.ToArray(); + } - private List btnLookups = new List - { - "Key 6", // left + private List btnLookups = new List + { + "Key 6", // left "Key 7", // right "Key 8", // down "Key 9", // up "Key 0", // fire }; - #endregion + #endregion - #region IJoystick + #region IJoystick - public JoystickType JoyType => JoystickType.SinclairRIGHT; + public JoystickType JoyType => JoystickType.SinclairRIGHT; - public string[] ButtonCollection { get; set; } + public string[] ButtonCollection { get; set; } - private int _playerNumber; - public int PlayerNumber - { - get { return _playerNumber; } - set { _playerNumber = value; } - } + private int _playerNumber; + public int PlayerNumber + { + get { return _playerNumber; } + set { _playerNumber = value; } + } - /// - /// Sets the joystick line based on key pressed - /// - public void SetJoyInput(string key, bool isPressed) - { - var pos = GetBitPos(key); + /// + /// Sets the joystick line based on key pressed + /// + public void SetJoyInput(string key, bool isPressed) + { + var pos = GetBitPos(key); - if (isPressed) - { - _machine.KeyboardDevice.SetKeyStatus(btnLookups[pos], true); - } - else - { - if (_machine.KeyboardDevice.GetKeyStatus(btnLookups[pos])) - { - // key is already pressed elswhere - leave it as is - } - else - { - // key is safe to unpress - _machine.KeyboardDevice.SetKeyStatus(btnLookups[pos], false); - } - } - } + if (isPressed) + { + _machine.KeyboardDevice.SetKeyStatus(btnLookups[pos], true); + } + else + { + if (_machine.KeyboardDevice.GetKeyStatus(btnLookups[pos])) + { + // key is already pressed elswhere - leave it as is + } + else + { + // key is safe to unpress + _machine.KeyboardDevice.SetKeyStatus(btnLookups[pos], false); + } + } + } - /// - /// Gets the state of a particular joystick binding - /// - public bool GetJoyInput(string key) - { - var pos = GetBitPos(key); - if (_machine == null) - return false; + /// + /// Gets the state of a particular joystick binding + /// + public bool GetJoyInput(string key) + { + var pos = GetBitPos(key); + if (_machine == null) + return false; - return _machine.KeyboardDevice.GetKeyStatus(btnLookups[pos]); - } - - #endregion + return _machine.KeyboardDevice.GetKeyStatus(btnLookups[pos]); + } - /// - /// Gets the bit position of a particular joystick binding from the matrix - /// - public int GetBitPos(string key) - { - int index = Array.IndexOf(ButtonCollection, key); - return index; - } - } + #endregion + + /// + /// Gets the bit position of a particular joystick binding from the matrix + /// + public int GetBitPos(string key) + { + int index = Array.IndexOf(ButtonCollection, key); + return index; + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Input/StandardKeyboard.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Input/StandardKeyboard.cs index 012b4207a4..9cba7b9030 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Input/StandardKeyboard.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Input/StandardKeyboard.cs @@ -5,48 +5,48 @@ using System.Linq; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// The 48k keyboard device - /// - public class StandardKeyboard : IKeyboard - { - public SpectrumBase _machine { get; set; } - private byte[] LineStatus; - private string[] _keyboardMatrix; - private int[] _keyLine; - private bool _isIssue2Keyboard; - private string[] _nonMatrixKeys; + /// + /// The 48k keyboard device + /// + public class StandardKeyboard : IKeyboard + { + public SpectrumBase _machine { get; set; } + private byte[] LineStatus; + private string[] _keyboardMatrix; + private int[] _keyLine; + private bool _isIssue2Keyboard; + private string[] _nonMatrixKeys; - public bool IsIssue2Keyboard - { - get { return _isIssue2Keyboard; } - set { _isIssue2Keyboard = value; } - } + public bool IsIssue2Keyboard + { + get { return _isIssue2Keyboard; } + set { _isIssue2Keyboard = value; } + } - public int[] KeyLine - { - get { return _keyLine; } - set { _keyLine = value; } - } + public int[] KeyLine + { + get { return _keyLine; } + set { _keyLine = value; } + } - public string[] KeyboardMatrix - { - get { return _keyboardMatrix; } - set { _keyboardMatrix = value; } - } + public string[] KeyboardMatrix + { + get { return _keyboardMatrix; } + set { _keyboardMatrix = value; } + } - public string[] NonMatrixKeys - { - get { return _nonMatrixKeys; } - set { _nonMatrixKeys = value; } - } + public string[] NonMatrixKeys + { + get { return _nonMatrixKeys; } + set { _nonMatrixKeys = value; } + } - public StandardKeyboard(SpectrumBase machine) - { - _machine = machine; + public StandardKeyboard(SpectrumBase machine) + { + _machine = machine; - KeyboardMatrix = new string[] - { + KeyboardMatrix = new string[] + { // 0xfefe - 0 - 4 "Key Caps Shift", "Key Z", "Key X", "Key C", "Key V", // 0xfdfe - 5 - 9 @@ -63,134 +63,134 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum "Key Return", "Key L", "Key K", "Key J", "Key H", // 0x7ffe - 35 - 39 "Key Space", "Key Symbol Shift", "Key M", "Key N", "Key B" - }; + }; - var nonMatrix = new List(); - - foreach (var key in _machine.Spectrum.ZXSpectrumControllerDefinition.BoolButtons) - { - if (!KeyboardMatrix.Any(s => s == key)) - nonMatrix.Add(key); - } - - NonMatrixKeys = nonMatrix.ToArray(); + var nonMatrix = new List(); - LineStatus = new byte[8]; - _keyLine = new int[] { 255, 255, 255, 255, 255, 255, 255, 255 }; - IsIssue2Keyboard = true; - } + foreach (var key in _machine.Spectrum.ZXSpectrumControllerDefinition.BoolButtons) + { + if (!KeyboardMatrix.Any(s => s == key)) + nonMatrix.Add(key); + } - public void SetKeyStatus(string key, bool isPressed) - { - int k = GetByteFromKeyMatrix(key); - - if (k != 255) - { - var lineIndex = k / 5; - var lineMask = 1 << k % 5; + NonMatrixKeys = nonMatrix.ToArray(); - _keyLine[lineIndex] = isPressed - ? (byte)(_keyLine[lineIndex] & ~lineMask) - : (byte)(_keyLine[lineIndex] | lineMask); - } + LineStatus = new byte[8]; + _keyLine = new int[] { 255, 255, 255, 255, 255, 255, 255, 255 }; + IsIssue2Keyboard = true; + } - // Combination keys that are not in the keyboard matrix - // but are available on the Spectrum+, 128k +2 & +3 - // (GetByteFromKeyMatrix() should return 255) - // Processed after the matrix keys - only presses handled (unpressed get done above) - if (k == 255) - { - if (isPressed) - { - switch (key) - { - // Delete key (simulates Caps Shift + 0) - case "Key Delete": - _keyLine[0] = _keyLine[0] & ~(0x1); - _keyLine[4] = _keyLine[4] & ~(0x1); - break; - // Cursor left (simulates Caps Shift + 5) - case "Key Left Cursor": - _keyLine[0] = _keyLine[0] & ~(0x1); - _keyLine[3] = _keyLine[3] & ~(0x10); - break; - // Cursor right (simulates Caps Shift + 8) - case "Key Right Cursor": - _keyLine[0] = _keyLine[0] & ~(0x1); - _keyLine[4] = _keyLine[4] & ~(0x04); - break; - // Cursor up (simulates Caps Shift + 7) - case "Key Up Cursor": - _keyLine[0] = _keyLine[0] & ~(0x1); - _keyLine[4] = _keyLine[4] & ~(0x08); - break; - // Cursor down (simulates Caps Shift + 6) - case "Key Down Cursor": - _keyLine[0] = _keyLine[0] & ~(0x1); - _keyLine[4] = _keyLine[4] & ~(0x10); - break; - } - } - } - } + public void SetKeyStatus(string key, bool isPressed) + { + int k = GetByteFromKeyMatrix(key); - public bool GetKeyStatus(string key) - { - byte keyByte = GetByteFromKeyMatrix(key); - var lineIndex = keyByte / 5; - var lineMask = 1 << keyByte % 5; + if (k != 255) + { + var lineIndex = k / 5; + var lineMask = 1 << k % 5; - return (_keyLine[lineIndex] & lineMask) == 0; - } + _keyLine[lineIndex] = isPressed + ? (byte)(_keyLine[lineIndex] & ~lineMask) + : (byte)(_keyLine[lineIndex] | lineMask); + } - public void ResetLineStatus() - { - lock (this) - { - for (int i = 0; i < KeyLine.Length; i++) - KeyLine[i] = 255; - } - } + // Combination keys that are not in the keyboard matrix + // but are available on the Spectrum+, 128k +2 & +3 + // (GetByteFromKeyMatrix() should return 255) + // Processed after the matrix keys - only presses handled (unpressed get done above) + if (k == 255) + { + if (isPressed) + { + switch (key) + { + // Delete key (simulates Caps Shift + 0) + case "Key Delete": + _keyLine[0] = _keyLine[0] & ~(0x1); + _keyLine[4] = _keyLine[4] & ~(0x1); + break; + // Cursor left (simulates Caps Shift + 5) + case "Key Left Cursor": + _keyLine[0] = _keyLine[0] & ~(0x1); + _keyLine[3] = _keyLine[3] & ~(0x10); + break; + // Cursor right (simulates Caps Shift + 8) + case "Key Right Cursor": + _keyLine[0] = _keyLine[0] & ~(0x1); + _keyLine[4] = _keyLine[4] & ~(0x04); + break; + // Cursor up (simulates Caps Shift + 7) + case "Key Up Cursor": + _keyLine[0] = _keyLine[0] & ~(0x1); + _keyLine[4] = _keyLine[4] & ~(0x08); + break; + // Cursor down (simulates Caps Shift + 6) + case "Key Down Cursor": + _keyLine[0] = _keyLine[0] & ~(0x1); + _keyLine[4] = _keyLine[4] & ~(0x10); + break; + } + } + } + } - public byte GetLineStatus(byte lines) - { - lock(this) - { - byte status = 0; - lines = (byte)~lines; - var lineIndex = 0; - while (lines > 0) - { - if ((lines & 0x01) != 0) - status |= (byte)_keyLine[lineIndex]; - lineIndex++; - lines >>= 1; - } - var result = (byte)status; + public bool GetKeyStatus(string key) + { + byte keyByte = GetByteFromKeyMatrix(key); + var lineIndex = keyByte / 5; + var lineMask = 1 << keyByte % 5; - return result; - } - } + return (_keyLine[lineIndex] & lineMask) == 0; + } - public byte ReadKeyboardByte(ushort addr) - { - return GetLineStatus((byte)(addr >> 8)); - } + public void ResetLineStatus() + { + lock (this) + { + for (int i = 0; i < KeyLine.Length; i++) + KeyLine[i] = 255; + } + } - public byte GetByteFromKeyMatrix(string key) - { - int index = Array.IndexOf(KeyboardMatrix, key); - return (byte)index; - } + public byte GetLineStatus(byte lines) + { + lock (this) + { + byte status = 0; + lines = (byte)~lines; + var lineIndex = 0; + while (lines > 0) + { + if ((lines & 0x01) != 0) + status |= (byte)_keyLine[lineIndex]; + lineIndex++; + lines >>= 1; + } + var result = (byte)status; - #region IPortIODevice + return result; + } + } - /// - /// Device responds to an IN instruction - /// - public bool ReadPort(ushort port, ref int result) - { - /* + public byte ReadKeyboardByte(ushort addr) + { + return GetLineStatus((byte)(addr >> 8)); + } + + public byte GetByteFromKeyMatrix(string key) + { + int index = Array.IndexOf(KeyboardMatrix, key); + return (byte)index; + } + + #region IPortIODevice + + /// + /// Device responds to an IN instruction + /// + public bool ReadPort(ushort port, ref int result) + { + /* The high byte indicates which half-row of keys is being polled A zero on one of these lines selects a particular half-row of five keys: @@ -206,75 +206,75 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum (for instance by XOR A/IN A,(FE)) is one, no key is pressed */ - if ((port & 0x0001) != 0) - return false; + if ((port & 0x0001) != 0) + return false; - if ((port & 0x8000) == 0) - { - result &= KeyLine[7]; - } + if ((port & 0x8000) == 0) + { + result &= KeyLine[7]; + } - if ((port & 0x4000) == 0) - { - result &= KeyLine[6]; - } + if ((port & 0x4000) == 0) + { + result &= KeyLine[6]; + } - if ((port & 0x2000) == 0) - { - result &= KeyLine[5]; - } + if ((port & 0x2000) == 0) + { + result &= KeyLine[5]; + } - if ((port & 0x1000) == 0) - { - result &= KeyLine[4]; - } + if ((port & 0x1000) == 0) + { + result &= KeyLine[4]; + } - if ((port & 0x800) == 0) - { - result &= KeyLine[3]; - } + if ((port & 0x800) == 0) + { + result &= KeyLine[3]; + } - if ((port & 0x400) == 0) - { - result &= KeyLine[2]; - } + if ((port & 0x400) == 0) + { + result &= KeyLine[2]; + } - if ((port & 0x200) == 0) - { - result &= KeyLine[1]; - } + if ((port & 0x200) == 0) + { + result &= KeyLine[1]; + } - if ((port & 0x100) == 0) - { - result &= KeyLine[0]; - } + if ((port & 0x100) == 0) + { + result &= KeyLine[0]; + } - // mask out lower 4 bits - result = result & 0x1f; + // mask out lower 4 bits + result = result & 0x1f; - // set bit 5 & 7 to 1 - result = result | 0xa0; + // set bit 5 & 7 to 1 + result = result | 0xa0; - return true; - } + return true; + } - /// - /// Device responds to an OUT instruction - /// - public bool WritePort(ushort port, int result) - { - // not implemented - return false; - } + /// + /// Device responds to an OUT instruction + /// + public bool WritePort(ushort port, int result) + { + // not implemented + return false; + } - #endregion + #endregion - public void SyncState(Serializer ser) - { - ser.BeginSection("Keyboard"); - ser.Sync(nameof(LineStatus), ref LineStatus, false); - ser.Sync(nameof(_keyLine), ref _keyLine, false); - ser.EndSection(); - } - } + public void SyncState(Serializer ser) + { + ser.BeginSection("Keyboard"); + ser.Sync(nameof(LineStatus), ref LineStatus, false); + ser.Sync(nameof(_keyLine), ref _keyLine, false); + ser.EndSection(); + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Rom/RomData.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Rom/RomData.cs index 972f7030aa..b9484377cd 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Rom/RomData.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Rom/RomData.cs @@ -1,83 +1,83 @@  namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Information about spectrum ROM - /// - public class RomData - { - /// - /// ROM Contents - /// - public byte[] RomBytes - { - get { return _romBytes; } - set { _romBytes = value; } - } + /// + /// Information about spectrum ROM + /// + public class RomData + { + /// + /// ROM Contents + /// + public byte[] RomBytes + { + get { return _romBytes; } + set { _romBytes = value; } + } - /// - /// Useful ROM addresses that are needed during tape operations - /// - public ushort SaveBytesRoutineAddress - { - get { return _saveBytesRoutineAddress; } - set { _saveBytesRoutineAddress = value; } - } - public ushort LoadBytesRoutineAddress - { - get { return _loadBytesRoutineAddress; } - set { _loadBytesRoutineAddress = value; } - } - public ushort SaveBytesResumeAddress - { - get { return _saveBytesResumeAddress; } - set { _saveBytesResumeAddress = value; } - } - public ushort LoadBytesResumeAddress - { - get { return _loadBytesResumeAddress; } - set { _loadBytesResumeAddress = value; } - } - public ushort LoadBytesInvalidHeaderAddress - { - get { return _loadBytesInvalidHeaderAddress; } - set { _loadBytesInvalidHeaderAddress = value; } - } + /// + /// Useful ROM addresses that are needed during tape operations + /// + public ushort SaveBytesRoutineAddress + { + get { return _saveBytesRoutineAddress; } + set { _saveBytesRoutineAddress = value; } + } + public ushort LoadBytesRoutineAddress + { + get { return _loadBytesRoutineAddress; } + set { _loadBytesRoutineAddress = value; } + } + public ushort SaveBytesResumeAddress + { + get { return _saveBytesResumeAddress; } + set { _saveBytesResumeAddress = value; } + } + public ushort LoadBytesResumeAddress + { + get { return _loadBytesResumeAddress; } + set { _loadBytesResumeAddress = value; } + } + public ushort LoadBytesInvalidHeaderAddress + { + get { return _loadBytesInvalidHeaderAddress; } + set { _loadBytesInvalidHeaderAddress = value; } + } - private byte[] _romBytes; - private ushort _saveBytesRoutineAddress; - private ushort _loadBytesRoutineAddress; - private ushort _saveBytesResumeAddress; - private ushort _loadBytesResumeAddress; - private ushort _loadBytesInvalidHeaderAddress; + private byte[] _romBytes; + private ushort _saveBytesRoutineAddress; + private ushort _loadBytesRoutineAddress; + private ushort _saveBytesResumeAddress; + private ushort _loadBytesResumeAddress; + private ushort _loadBytesInvalidHeaderAddress; - public static RomData InitROM(MachineType machineType, byte[] rom) - { - RomData RD = new RomData(); - RD.RomBytes = new byte[rom.Length]; - RD.RomBytes = rom; + public static RomData InitROM(MachineType machineType, byte[] rom) + { + RomData RD = new RomData(); + RD.RomBytes = new byte[rom.Length]; + RD.RomBytes = rom; - switch (machineType) - { - case MachineType.ZXSpectrum48: - RD.SaveBytesRoutineAddress = 0x04C2; - RD.SaveBytesResumeAddress = 0x0000; - RD.LoadBytesRoutineAddress = 0x0808; //0x0556; //0x056C; - RD.LoadBytesResumeAddress = 0x05E2; - RD.LoadBytesInvalidHeaderAddress = 0x05B6; - break; + switch (machineType) + { + case MachineType.ZXSpectrum48: + RD.SaveBytesRoutineAddress = 0x04C2; + RD.SaveBytesResumeAddress = 0x0000; + RD.LoadBytesRoutineAddress = 0x0808; //0x0556; //0x056C; + RD.LoadBytesResumeAddress = 0x05E2; + RD.LoadBytesInvalidHeaderAddress = 0x05B6; + break; - case MachineType.ZXSpectrum128: - RD.SaveBytesRoutineAddress = 0x04C2; - RD.SaveBytesResumeAddress = 0x0000; - RD.LoadBytesRoutineAddress = 0x0808; //0x0556; //0x056C; - RD.LoadBytesResumeAddress = 0x05E2; - RD.LoadBytesInvalidHeaderAddress = 0x05B6; - break; - } + case MachineType.ZXSpectrum128: + RD.SaveBytesRoutineAddress = 0x04C2; + RD.SaveBytesResumeAddress = 0x0000; + RD.LoadBytesRoutineAddress = 0x0808; //0x0556; //0x056C; + RD.LoadBytesResumeAddress = 0x05E2; + RD.LoadBytesInvalidHeaderAddress = 0x05B6; + break; + } - return RD; - } - } + return RD; + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/SoundOuput/AY38912.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/SoundOuput/AY38912.cs index 5f3e12d0ae..c15be8d2a3 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/SoundOuput/AY38912.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/SoundOuput/AY38912.cs @@ -5,194 +5,194 @@ using System.Collections.Generic; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// AY-3-8912 Emulated Device - /// - /// Based heavily on the YM-2149F / AY-3-8910 emulator used in Unreal Speccy - /// (Originally created under Public Domain license by SMT jan.2006) - /// - /// https://github.com/mkoloberdin/unrealspeccy/blob/master/sndrender/sndchip.cpp - /// https://github.com/mkoloberdin/unrealspeccy/blob/master/sndrender/sndchip.h - /// - public class AY38912 : IPSG - { - #region Device Fields + /// + /// AY-3-8912 Emulated Device + /// + /// Based heavily on the YM-2149F / AY-3-8910 emulator used in Unreal Speccy + /// (Originally created under Public Domain license by SMT jan.2006) + /// + /// https://github.com/mkoloberdin/unrealspeccy/blob/master/sndrender/sndchip.cpp + /// https://github.com/mkoloberdin/unrealspeccy/blob/master/sndrender/sndchip.h + /// + public class AY38912 : IPSG + { + #region Device Fields - /// - /// The emulated machine (passed in via constructor) - /// - private SpectrumBase _machine; + /// + /// The emulated machine (passed in via constructor) + /// + private SpectrumBase _machine; - private int _tStatesPerFrame; - private int _sampleRate; - private int _samplesPerFrame; - private int _tStatesPerSample; - private short[] _audioBuffer; - private int _audioBufferIndex; - private int _lastStateRendered; + private int _tStatesPerFrame; + private int _sampleRate; + private int _samplesPerFrame; + private int _tStatesPerSample; + private short[] _audioBuffer; + private int _audioBufferIndex; + private int _lastStateRendered; - #endregion + #endregion - #region Construction & Initialization + #region Construction & Initialization - /// - /// Main constructor - /// - public AY38912(SpectrumBase machine) - { - _machine = machine; - } + /// + /// Main constructor + /// + public AY38912(SpectrumBase machine) + { + _machine = machine; + } - /// - /// Initialises the AY chip - /// - public void Init(int sampleRate, int tStatesPerFrame) - { - InitTiming(sampleRate, tStatesPerFrame); - UpdateVolume(); - Reset(); - } + /// + /// Initialises the AY chip + /// + public void Init(int sampleRate, int tStatesPerFrame) + { + InitTiming(sampleRate, tStatesPerFrame); + UpdateVolume(); + Reset(); + } - #endregion + #endregion - #region IPortIODevice + #region IPortIODevice - public bool ReadPort(ushort port, ref int value) - { - if (port != 0xfffd) - { - // port read is not addressing this device - return false; - } + public bool ReadPort(ushort port, ref int value) + { + if (port != 0xfffd) + { + // port read is not addressing this device + return false; + } - value = PortRead(); + value = PortRead(); - return true; - } + return true; + } - public bool WritePort(ushort port, int value) - { - if (port == 0xfffd) - { - // register select - SelectedRegister = value & 0x0f; - return true; - } - else if (port == 0xbffd) - { - // Update the audiobuffer based on the current CPU cycle - // (this process the previous data BEFORE writing to the currently selected register) - int d = (int)(_machine.CurrentFrameCycle); - BufferUpdate(d); + public bool WritePort(ushort port, int value) + { + if (port == 0xfffd) + { + // register select + SelectedRegister = value & 0x0f; + return true; + } + else if (port == 0xbffd) + { + // Update the audiobuffer based on the current CPU cycle + // (this process the previous data BEFORE writing to the currently selected register) + int d = (int)(_machine.CurrentFrameCycle); + BufferUpdate(d); - // write to register - PortWrite(value); - return true; - } - return false; - } + // write to register + PortWrite(value); + return true; + } + return false; + } - #endregion + #endregion - #region AY Implementation + #region AY Implementation - #region Public Properties + #region Public Properties - /// - /// AY mixer panning configuration - /// - [Flags] - public enum AYPanConfig - { - MONO = 0, - ABC = 1, - ACB = 2, - BAC = 3, - BCA = 4, - CAB = 5, - CBA = 6, - } + /// + /// AY mixer panning configuration + /// + [Flags] + public enum AYPanConfig + { + MONO = 0, + ABC = 1, + ACB = 2, + BAC = 3, + BCA = 4, + CAB = 5, + CBA = 6, + } - /// - /// The AY panning configuration - /// - public AYPanConfig PanningConfiguration - { - get - { - return _currentPanTab; - } - set - { - if (value != _currentPanTab) - { - _currentPanTab = value; - UpdateVolume(); - } - } - } + /// + /// The AY panning configuration + /// + public AYPanConfig PanningConfiguration + { + get + { + return _currentPanTab; + } + set + { + if (value != _currentPanTab) + { + _currentPanTab = value; + UpdateVolume(); + } + } + } - /// - /// The AY chip output volume - /// (0 - 100) - /// - public int Volume - { - get - { - return _volume; - } - set - { - //value = Math.Max(0, value); - //value = Math.Max(100, value); - if (_volume == value) - { - return; - } - _volume = value; - UpdateVolume(); - } - } + /// + /// The AY chip output volume + /// (0 - 100) + /// + public int Volume + { + get + { + return _volume; + } + set + { + //value = Math.Max(0, value); + //value = Math.Max(100, value); + if (_volume == value) + { + return; + } + _volume = value; + UpdateVolume(); + } + } - /// - /// The currently selected register - /// - public int SelectedRegister - { - get { return _activeRegister; } - set - { - _activeRegister = (byte)value; - } - } + /// + /// The currently selected register + /// + public int SelectedRegister + { + get { return _activeRegister; } + set + { + _activeRegister = (byte)value; + } + } - /// - /// Used for snapshot generation - /// - public int[] ExportRegisters() - { - return _registers; - } + /// + /// Used for snapshot generation + /// + public int[] ExportRegisters() + { + return _registers; + } - #endregion + #endregion - #region Public Methods + #region Public Methods - /// - /// Resets the PSG - /// - public void Reset() - { - for (int i = 0; i < 16; i++) - { - if (i == 6) - _registers[i] = 0xff; - else - _registers[i] = 0; - } + /// + /// Resets the PSG + /// + public void Reset() + { + for (int i = 0; i < 16; i++) + { + if (i == 6) + _registers[i] = 0xff; + else + _registers[i] = 0; + } - /* + /* _noiseVal = 0x0FFFF; _outABC = 0; _outNoiseABC = 0; @@ -217,166 +217,166 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum // update the audio buffer BufferUpdate(fr); */ - } + } - /// - /// Reads the value from the currently selected register - /// - public int PortRead() - { - return _registers[_activeRegister]; - } + /// + /// Reads the value from the currently selected register + /// + public int PortRead() + { + return _registers[_activeRegister]; + } - /// - /// Writes to the currently selected register - /// - public void PortWrite(int value) - { - if (_activeRegister >= 0x10) - return; + /// + /// Writes to the currently selected register + /// + public void PortWrite(int value) + { + if (_activeRegister >= 0x10) + return; - byte val = (byte)value; + byte val = (byte)value; - if (((1 << _activeRegister) & ((1 << 1) | (1 << 3) | (1 << 5) | (1 << 13))) != 0) - val &= 0x0F; + if (((1 << _activeRegister) & ((1 << 1) | (1 << 3) | (1 << 5) | (1 << 13))) != 0) + val &= 0x0F; - if (((1 << _activeRegister) & ((1 << 6) | (1 << 8) | (1 << 9) | (1 << 10))) != 0) - val &= 0x1F; + if (((1 << _activeRegister) & ((1 << 6) | (1 << 8) | (1 << 9) | (1 << 10))) != 0) + val &= 0x1F; - if (_activeRegister != 13 && _registers[_activeRegister] == val) - return; + if (_activeRegister != 13 && _registers[_activeRegister] == val) + return; - _registers[_activeRegister] = val; + _registers[_activeRegister] = val; - switch (_activeRegister) - { - // Channel A (Combined Pitch) - // (not written to directly) - case 0: - case 1: - _dividerA = _registers[AY_A_FINE] | (_registers[AY_A_COARSE] << 8); - break; - // Channel B (Combined Pitch) - // (not written to directly) - case 2: - case 3: - _dividerB = _registers[AY_B_FINE] | (_registers[AY_B_COARSE] << 8); - break; - // Channel C (Combined Pitch) - // (not written to directly) - case 4: - case 5: - _dividerC = _registers[AY_C_FINE] | (_registers[AY_C_COARSE] << 8); - break; - // Noise Pitch - case 6: - _dividerN = val * 2; - break; - // Mixer - case 7: - _bit0 = 0 - ((val >> 0) & 1); - _bit1 = 0 - ((val >> 1) & 1); - _bit2 = 0 - ((val >> 2) & 1); - _bit3 = 0 - ((val >> 3) & 1); - _bit4 = 0 - ((val >> 4) & 1); - _bit5 = 0 - ((val >> 5) & 1); - break; - // Channel Volumes - case 8: - _eMaskA = (val & 0x10) != 0 ? -1 : 0; - _vA = ((val & 0x0F) * 2 + 1) & ~_eMaskA; - break; - case 9: - _eMaskB = (val & 0x10) != 0 ? -1 : 0; - _vB = ((val & 0x0F) * 2 + 1) & ~_eMaskB; - break; - case 10: - _eMaskC = (val & 0x10) != 0 ? -1 : 0; - _vC = ((val & 0x0F) * 2 + 1) & ~_eMaskC; - break; - // Envelope (Combined Duration) - // (not written to directly) - case 11: - case 12: - _dividerE = _registers[AY_E_FINE] | (_registers[AY_E_COARSE] << 8); - break; - // Envelope Shape - case 13: - // reset the envelope counter - _countE = 0; + switch (_activeRegister) + { + // Channel A (Combined Pitch) + // (not written to directly) + case 0: + case 1: + _dividerA = _registers[AY_A_FINE] | (_registers[AY_A_COARSE] << 8); + break; + // Channel B (Combined Pitch) + // (not written to directly) + case 2: + case 3: + _dividerB = _registers[AY_B_FINE] | (_registers[AY_B_COARSE] << 8); + break; + // Channel C (Combined Pitch) + // (not written to directly) + case 4: + case 5: + _dividerC = _registers[AY_C_FINE] | (_registers[AY_C_COARSE] << 8); + break; + // Noise Pitch + case 6: + _dividerN = val * 2; + break; + // Mixer + case 7: + _bit0 = 0 - ((val >> 0) & 1); + _bit1 = 0 - ((val >> 1) & 1); + _bit2 = 0 - ((val >> 2) & 1); + _bit3 = 0 - ((val >> 3) & 1); + _bit4 = 0 - ((val >> 4) & 1); + _bit5 = 0 - ((val >> 5) & 1); + break; + // Channel Volumes + case 8: + _eMaskA = (val & 0x10) != 0 ? -1 : 0; + _vA = ((val & 0x0F) * 2 + 1) & ~_eMaskA; + break; + case 9: + _eMaskB = (val & 0x10) != 0 ? -1 : 0; + _vB = ((val & 0x0F) * 2 + 1) & ~_eMaskB; + break; + case 10: + _eMaskC = (val & 0x10) != 0 ? -1 : 0; + _vC = ((val & 0x0F) * 2 + 1) & ~_eMaskC; + break; + // Envelope (Combined Duration) + // (not written to directly) + case 11: + case 12: + _dividerE = _registers[AY_E_FINE] | (_registers[AY_E_COARSE] << 8); + break; + // Envelope Shape + case 13: + // reset the envelope counter + _countE = 0; - if ((_registers[AY_E_SHAPE] & 4) != 0) - { - // attack - _eState = 0; - _eDirection = 1; - } - else - { - // decay - _eState = 31; - _eDirection = -1; - } - break; - case 14: - // IO Port - not implemented - break; - } - } + if ((_registers[AY_E_SHAPE] & 4) != 0) + { + // attack + _eState = 0; + _eDirection = 1; + } + else + { + // decay + _eState = 31; + _eDirection = -1; + } + break; + case 14: + // IO Port - not implemented + break; + } + } - /// - /// Start of frame - /// - public void StartFrame() - { - _audioBufferIndex = 0; - BufferUpdate(0); - } + /// + /// Start of frame + /// + public void StartFrame() + { + _audioBufferIndex = 0; + BufferUpdate(0); + } - /// - /// End of frame - /// - public void EndFrame() - { - BufferUpdate(_tStatesPerFrame); - } + /// + /// End of frame + /// + public void EndFrame() + { + BufferUpdate(_tStatesPerFrame); + } - /// - /// Updates the audiobuffer based on the current frame t-state - /// - public void UpdateSound(int frameCycle) - { - BufferUpdate(frameCycle); - } + /// + /// Updates the audiobuffer based on the current frame t-state + /// + public void UpdateSound(int frameCycle) + { + BufferUpdate(frameCycle); + } - #endregion + #endregion - #region Private Fields + #region Private Fields - /// - /// Register indicies - /// - private const int AY_A_FINE = 0; - private const int AY_A_COARSE = 1; - private const int AY_B_FINE = 2; - private const int AY_B_COARSE = 3; - private const int AY_C_FINE = 4; - private const int AY_C_COARSE = 5; - private const int AY_NOISEPITCH = 6; - private const int AY_MIXER = 7; - private const int AY_A_VOL = 8; - private const int AY_B_VOL = 9; - private const int AY_C_VOL = 10; - private const int AY_E_FINE = 11; - private const int AY_E_COARSE = 12; - private const int AY_E_SHAPE = 13; - private const int AY_PORT_A = 14; - private const int AY_PORT_B = 15; + /// + /// Register indicies + /// + private const int AY_A_FINE = 0; + private const int AY_A_COARSE = 1; + private const int AY_B_FINE = 2; + private const int AY_B_COARSE = 3; + private const int AY_C_FINE = 4; + private const int AY_C_COARSE = 5; + private const int AY_NOISEPITCH = 6; + private const int AY_MIXER = 7; + private const int AY_A_VOL = 8; + private const int AY_B_VOL = 9; + private const int AY_C_VOL = 10; + private const int AY_E_FINE = 11; + private const int AY_E_COARSE = 12; + private const int AY_E_SHAPE = 13; + private const int AY_PORT_A = 14; + private const int AY_PORT_B = 15; - /// - /// The register array - /// - /* + /// + /// The register array + /// + /* The AY-3-8910/8912 contains 16 internal registers as follows: Register Function Range @@ -407,113 +407,113 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum The AY-3-8912 ignores bit 7 of this register. */ - private int[] _registers = new int[16]; + private int[] _registers = new int[16]; - /// - /// The currently selected register - /// - private byte _activeRegister; + /// + /// The currently selected register + /// + private byte _activeRegister; - /// - /// The frequency of the AY chip - /// - private static int _chipFrequency = 1773400; + /// + /// The frequency of the AY chip + /// + private static int _chipFrequency = 1773400; - /// - /// The rendering resolution of the chip - /// - private double _resolution = 50D * 8D / _chipFrequency; + /// + /// The rendering resolution of the chip + /// + private double _resolution = 50D * 8D / _chipFrequency; - /// - /// Channel generator state - /// - private int _bitA; - private int _bitB; - private int _bitC; + /// + /// Channel generator state + /// + private int _bitA; + private int _bitB; + private int _bitC; - /// - /// Envelope state - /// - private int _eState; + /// + /// Envelope state + /// + private int _eState; - /// - /// Envelope direction - /// - private int _eDirection; + /// + /// Envelope direction + /// + private int _eDirection; - /// - /// Noise seed - /// - private int _noiseSeed; + /// + /// Noise seed + /// + private int _noiseSeed; - /// - /// Mixer state - /// - private int _bit0; - private int _bit1; - private int _bit2; - private int _bit3; - private int _bit4; - private int _bit5; + /// + /// Mixer state + /// + private int _bit0; + private int _bit1; + private int _bit2; + private int _bit3; + private int _bit4; + private int _bit5; - /// - /// Noise generator state - /// - private int _bitN; + /// + /// Noise generator state + /// + private int _bitN; - /// - /// Envelope masks - /// - private int _eMaskA; - private int _eMaskB; - private int _eMaskC; + /// + /// Envelope masks + /// + private int _eMaskA; + private int _eMaskB; + private int _eMaskC; - /// - /// Amplitudes - /// - private int _vA; - private int _vB; - private int _vC; + /// + /// Amplitudes + /// + private int _vA; + private int _vB; + private int _vC; - /// - /// Channel gen counters - /// - private int _countA; - private int _countB; - private int _countC; + /// + /// Channel gen counters + /// + private int _countA; + private int _countB; + private int _countC; - /// - /// Envelope gen counter - /// - private int _countE; + /// + /// Envelope gen counter + /// + private int _countE; - /// - /// Noise gen counter - /// - private int _countN; + /// + /// Noise gen counter + /// + private int _countN; - /// - /// Channel gen dividers - /// - private int _dividerA; - private int _dividerB; - private int _dividerC; + /// + /// Channel gen dividers + /// + private int _dividerA; + private int _dividerB; + private int _dividerC; - /// - /// Envelope gen divider - /// - private int _dividerE; + /// + /// Envelope gen divider + /// + private int _dividerE; - /// - /// Noise gen divider - /// - private int _dividerN; + /// + /// Noise gen divider + /// + private int _dividerN; - /// - /// Panning table list - /// - private static List PanTabs = new List - { + /// + /// Panning table list + /// + private static List PanTabs = new List + { // MONO new uint[] { 50,50, 50,50, 50,50 }, // ABC @@ -528,290 +528,290 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum new uint[] { 66,66, 10,100, 100,10 }, // CBA new uint[] { 10,100, 66,66, 100,10 } - }; + }; - /// - /// The currently selected panning configuration - /// - private AYPanConfig _currentPanTab = AYPanConfig.ABC; + /// + /// The currently selected panning configuration + /// + private AYPanConfig _currentPanTab = AYPanConfig.ABC; - /// - /// The current volume - /// - private int _volume = 75; + /// + /// The current volume + /// + private int _volume = 75; - /// - /// Volume tables state - /// - private uint[][] _volumeTables; + /// + /// Volume tables state + /// + private uint[][] _volumeTables; - /// - /// Volume table to be used - /// - private static uint[] AYVolumes = new uint[] - { - 0x0000,0x0000,0x0340,0x0340,0x04C0,0x04C0,0x06F2,0x06F2, - 0x0A44,0x0A44,0x0F13,0x0F13,0x1510,0x1510,0x227E,0x227E, - 0x289F,0x289F,0x414E,0x414E,0x5B21,0x5B21,0x7258,0x7258, - 0x905E,0x905E,0xB550,0xB550,0xD7A0,0xD7A0,0xFFFF,0xFFFF, - }; + /// + /// Volume table to be used + /// + private static uint[] AYVolumes = new uint[] + { + 0x0000,0x0000,0x0340,0x0340,0x04C0,0x04C0,0x06F2,0x06F2, + 0x0A44,0x0A44,0x0F13,0x0F13,0x1510,0x1510,0x227E,0x227E, + 0x289F,0x289F,0x414E,0x414E,0x5B21,0x5B21,0x7258,0x7258, + 0x905E,0x905E,0xB550,0xB550,0xD7A0,0xD7A0,0xFFFF,0xFFFF, + }; - #endregion + #endregion - #region Private Methods + #region Private Methods - /// - /// Forces an update of the volume tables - /// - private void UpdateVolume() - { - int upperFloor = 40000; - var inc = (0xFFFF - upperFloor) / 100; + /// + /// Forces an update of the volume tables + /// + private void UpdateVolume() + { + int upperFloor = 40000; + var inc = (0xFFFF - upperFloor) / 100; - var vol = inc * _volume; // ((ulong)0xFFFF * (ulong)_volume / 100UL) - 20000 ; - _volumeTables = new uint[6][]; + var vol = inc * _volume; // ((ulong)0xFFFF * (ulong)_volume / 100UL) - 20000 ; + _volumeTables = new uint[6][]; - // parent array - for (int j = 0; j < _volumeTables.Length; j++) - { - _volumeTables[j] = new uint[32]; + // parent array + for (int j = 0; j < _volumeTables.Length; j++) + { + _volumeTables[j] = new uint[32]; - // child array - for (int i = 0; i < _volumeTables[j].Length; i++) - { - _volumeTables[j][i] = (uint)( - (PanTabs[(int)_currentPanTab][j] * AYVolumes[i] * vol) / - (3 * 65535 * 100)); - } - } - } + // child array + for (int i = 0; i < _volumeTables[j].Length; i++) + { + _volumeTables[j][i] = (uint)( + (PanTabs[(int)_currentPanTab][j] * AYVolumes[i] * vol) / + (3 * 65535 * 100)); + } + } + } - private int mult_const; + private int mult_const; - /// - /// Initializes timing information for the frame - /// - private void InitTiming(int sampleRate, int frameTactCount) - { - _sampleRate = sampleRate; - _tStatesPerFrame = frameTactCount; - _samplesPerFrame = 882; + /// + /// Initializes timing information for the frame + /// + private void InitTiming(int sampleRate, int frameTactCount) + { + _sampleRate = sampleRate; + _tStatesPerFrame = frameTactCount; + _samplesPerFrame = 882; - _tStatesPerSample = 79; //(int)Math.Round(((double)_tStatesPerFrame * 50D) / - //(16D * (double)_sampleRate), - //MidpointRounding.AwayFromZero); + _tStatesPerSample = 79; //(int)Math.Round(((double)_tStatesPerFrame * 50D) / + //(16D * (double)_sampleRate), + //MidpointRounding.AwayFromZero); - //_samplesPerFrame = _tStatesPerFrame / _tStatesPerSample; - _audioBuffer = new short[_samplesPerFrame * 2]; //[_sampleRate / 50]; - _audioBufferIndex = 0; + //_samplesPerFrame = _tStatesPerFrame / _tStatesPerSample; + _audioBuffer = new short[_samplesPerFrame * 2]; //[_sampleRate / 50]; + _audioBufferIndex = 0; - mult_const = ((_chipFrequency / 8) << 14) / _machine.ULADevice.ClockSpeed; + mult_const = ((_chipFrequency / 8) << 14) / _machine.ULADevice.ClockSpeed; - var aytickspercputick = (double)_machine.ULADevice.ClockSpeed / (double)_chipFrequency; - int ayCyclesPerSample = (int)((double)_tStatesPerSample * (double)aytickspercputick); - } + var aytickspercputick = (double)_machine.ULADevice.ClockSpeed / (double)_chipFrequency; + int ayCyclesPerSample = (int)((double)_tStatesPerSample * (double)aytickspercputick); + } - /// - /// Updates the audiobuffer based on the current frame t-state - /// - private void BufferUpdate(int cycle) - { - if (cycle > _tStatesPerFrame) - { - // we are outside of the frame - just process the last value - cycle = _tStatesPerFrame; - } + /// + /// Updates the audiobuffer based on the current frame t-state + /// + private void BufferUpdate(int cycle) + { + if (cycle > _tStatesPerFrame) + { + // we are outside of the frame - just process the last value + cycle = _tStatesPerFrame; + } - // get the current length of the audiobuffer - int bufferLength = _samplesPerFrame; // _audioBuffer.Length; + // get the current length of the audiobuffer + int bufferLength = _samplesPerFrame; // _audioBuffer.Length; - int toEnd = ((bufferLength * cycle) / _tStatesPerFrame); + int toEnd = ((bufferLength * cycle) / _tStatesPerFrame); - // loop through the number of samples we need to render - while (_audioBufferIndex < toEnd) - { - // run the AY chip processing at the correct resolution - for (int i = 0; i < _tStatesPerSample / 14; i++) - { - if (++_countA >= _dividerA) - { - _countA = 0; - _bitA ^= -1; - } + // loop through the number of samples we need to render + while (_audioBufferIndex < toEnd) + { + // run the AY chip processing at the correct resolution + for (int i = 0; i < _tStatesPerSample / 14; i++) + { + if (++_countA >= _dividerA) + { + _countA = 0; + _bitA ^= -1; + } - if (++_countB >= _dividerB) - { - _countB = 0; - _bitB ^= -1; - } + if (++_countB >= _dividerB) + { + _countB = 0; + _bitB ^= -1; + } - if (++_countC >= _dividerC) - { - _countC = 0; - _bitC ^= -1; - } + if (++_countC >= _dividerC) + { + _countC = 0; + _bitC ^= -1; + } - if (++_countN >= _dividerN) - { - _countN = 0; - _noiseSeed = (_noiseSeed * 2 + 1) ^ (((_noiseSeed >> 16) ^ (_noiseSeed >> 13)) & 1); - _bitN = 0 - ((_noiseSeed >> 16) & 1); - } + if (++_countN >= _dividerN) + { + _countN = 0; + _noiseSeed = (_noiseSeed * 2 + 1) ^ (((_noiseSeed >> 16) ^ (_noiseSeed >> 13)) & 1); + _bitN = 0 - ((_noiseSeed >> 16) & 1); + } - if (++_countE >= _dividerE) - { - _countE = 0; - _eState += +_eDirection; + if (++_countE >= _dividerE) + { + _countE = 0; + _eState += +_eDirection; - if ((_eState & ~31) != 0) - { - var mask = (1 << _registers[AY_E_SHAPE]); + if ((_eState & ~31) != 0) + { + var mask = (1 << _registers[AY_E_SHAPE]); - if ((mask & ((1 << 0) | (1 << 1) | (1 << 2) | - (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | - (1 << 7) | (1 << 9) | (1 << 15))) != 0) - { - _eState = _eDirection = 0; - } - else if ((mask & ((1 << 8) | (1 << 12))) != 0) - { - _eState &= 31; - } - else if ((mask & ((1 << 10) | (1 << 14))) != 0) - { - _eDirection = -_eDirection; - _eState += _eDirection; - } - else - { - // 11,13 - _eState = 31; - _eDirection = 0; - } - } - } - } + if ((mask & ((1 << 0) | (1 << 1) | (1 << 2) | + (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | + (1 << 7) | (1 << 9) | (1 << 15))) != 0) + { + _eState = _eDirection = 0; + } + else if ((mask & ((1 << 8) | (1 << 12))) != 0) + { + _eState &= 31; + } + else if ((mask & ((1 << 10) | (1 << 14))) != 0) + { + _eDirection = -_eDirection; + _eState += _eDirection; + } + else + { + // 11,13 + _eState = 31; + _eDirection = 0; + } + } + } + } - // mix the sample - var mixA = ((_eMaskA & _eState) | _vA) & ((_bitA | _bit0) & (_bitN | _bit3)); - var mixB = ((_eMaskB & _eState) | _vB) & ((_bitB | _bit1) & (_bitN | _bit4)); - var mixC = ((_eMaskC & _eState) | _vC) & ((_bitC | _bit2) & (_bitN | _bit5)); + // mix the sample + var mixA = ((_eMaskA & _eState) | _vA) & ((_bitA | _bit0) & (_bitN | _bit3)); + var mixB = ((_eMaskB & _eState) | _vB) & ((_bitB | _bit1) & (_bitN | _bit4)); + var mixC = ((_eMaskC & _eState) | _vC) & ((_bitC | _bit2) & (_bitN | _bit5)); - var l = _volumeTables[0][mixA]; - var r = _volumeTables[1][mixA]; + var l = _volumeTables[0][mixA]; + var r = _volumeTables[1][mixA]; - l += _volumeTables[2][mixB]; - r += _volumeTables[3][mixB]; - l += _volumeTables[4][mixC]; - r += _volumeTables[5][mixC]; + l += _volumeTables[2][mixB]; + r += _volumeTables[3][mixB]; + l += _volumeTables[4][mixC]; + r += _volumeTables[5][mixC]; - _audioBuffer[_audioBufferIndex * 2] = (short)l; - _audioBuffer[(_audioBufferIndex * 2) + 1] = (short)r; + _audioBuffer[_audioBufferIndex * 2] = (short)l; + _audioBuffer[(_audioBufferIndex * 2) + 1] = (short)r; - _audioBufferIndex++; - } + _audioBufferIndex++; + } - _lastStateRendered = cycle; - } + _lastStateRendered = cycle; + } - #endregion + #endregion - #endregion + #endregion - #region ISoundProvider + #region ISoundProvider - public bool CanProvideAsync => false; + public bool CanProvideAsync => false; - public SyncSoundMode SyncMode => SyncSoundMode.Sync; + public SyncSoundMode SyncMode => SyncSoundMode.Sync; - public void SetSyncMode(SyncSoundMode mode) - { - if (mode != SyncSoundMode.Sync) - throw new InvalidOperationException("Only Sync mode is supported."); - } + public void SetSyncMode(SyncSoundMode mode) + { + if (mode != SyncSoundMode.Sync) + throw new InvalidOperationException("Only Sync mode is supported."); + } - public void GetSamplesAsync(short[] samples) - { - throw new NotSupportedException("Async is not available"); - } + public void GetSamplesAsync(short[] samples) + { + throw new NotSupportedException("Async is not available"); + } - public void DiscardSamples() - { - _audioBuffer = new short[_samplesPerFrame * 2]; - } + public void DiscardSamples() + { + _audioBuffer = new short[_samplesPerFrame * 2]; + } - public void GetSamplesSync(out short[] samples, out int nsamp) - { - nsamp = _samplesPerFrame; - samples = _audioBuffer; - DiscardSamples(); - } + public void GetSamplesSync(out short[] samples, out int nsamp) + { + nsamp = _samplesPerFrame; + samples = _audioBuffer; + DiscardSamples(); + } - #endregion + #endregion - #region State Serialization + #region State Serialization - public int nullDump = 0; + public int nullDump = 0; - /// - /// State serialization - /// - public void SyncState(Serializer ser) - { - ser.BeginSection("PSG-AY"); + /// + /// State serialization + /// + public void SyncState(Serializer ser) + { + ser.BeginSection("PSG-AY"); - ser.Sync(nameof(_tStatesPerFrame), ref _tStatesPerFrame); - ser.Sync(nameof(_sampleRate), ref _sampleRate); - ser.Sync(nameof(_samplesPerFrame), ref _samplesPerFrame); - ser.Sync(nameof(_tStatesPerSample), ref _tStatesPerSample); - ser.Sync(nameof(_audioBufferIndex), ref _audioBufferIndex); - ser.Sync(nameof(_audioBuffer), ref _audioBuffer, false); + ser.Sync(nameof(_tStatesPerFrame), ref _tStatesPerFrame); + ser.Sync(nameof(_sampleRate), ref _sampleRate); + ser.Sync(nameof(_samplesPerFrame), ref _samplesPerFrame); + ser.Sync(nameof(_tStatesPerSample), ref _tStatesPerSample); + ser.Sync(nameof(_audioBufferIndex), ref _audioBufferIndex); + ser.Sync(nameof(_audioBuffer), ref _audioBuffer, false); - ser.Sync(nameof(_registers), ref _registers, false); - ser.Sync(nameof(_activeRegister), ref _activeRegister); - ser.Sync(nameof(_bitA), ref _bitA); - ser.Sync(nameof(_bitB), ref _bitB); - ser.Sync(nameof(_bitC), ref _bitC); - ser.Sync(nameof(_eState), ref _eState); - ser.Sync(nameof(_eDirection), ref _eDirection); - ser.Sync(nameof(_noiseSeed), ref _noiseSeed); - ser.Sync(nameof(_bit0), ref _bit0); - ser.Sync(nameof(_bit1), ref _bit1); - ser.Sync(nameof(_bit2), ref _bit2); - ser.Sync(nameof(_bit3), ref _bit3); - ser.Sync(nameof(_bit4), ref _bit4); - ser.Sync(nameof(_bit5), ref _bit5); - ser.Sync(nameof(_bitN), ref _bitN); - ser.Sync(nameof(_eMaskA), ref _eMaskA); - ser.Sync(nameof(_eMaskB), ref _eMaskB); - ser.Sync(nameof(_eMaskC), ref _eMaskC); - ser.Sync(nameof(_vA), ref _vA); - ser.Sync(nameof(_vB), ref _vB); - ser.Sync(nameof(_vC), ref _vC); - ser.Sync(nameof(_countA), ref _countA); - ser.Sync(nameof(_countB), ref _countB); - ser.Sync(nameof(_countC), ref _countC); - ser.Sync(nameof(_countE), ref _countE); - ser.Sync(nameof(_countN), ref _countN); - ser.Sync(nameof(_dividerA), ref _dividerA); - ser.Sync(nameof(_dividerB), ref _dividerB); - ser.Sync(nameof(_dividerC), ref _dividerC); - ser.Sync(nameof(_dividerE), ref _dividerE); - ser.Sync(nameof(_dividerN), ref _dividerN); - ser.SyncEnum(nameof(_currentPanTab), ref _currentPanTab); - ser.Sync(nameof(_volume), ref nullDump); + ser.Sync(nameof(_registers), ref _registers, false); + ser.Sync(nameof(_activeRegister), ref _activeRegister); + ser.Sync(nameof(_bitA), ref _bitA); + ser.Sync(nameof(_bitB), ref _bitB); + ser.Sync(nameof(_bitC), ref _bitC); + ser.Sync(nameof(_eState), ref _eState); + ser.Sync(nameof(_eDirection), ref _eDirection); + ser.Sync(nameof(_noiseSeed), ref _noiseSeed); + ser.Sync(nameof(_bit0), ref _bit0); + ser.Sync(nameof(_bit1), ref _bit1); + ser.Sync(nameof(_bit2), ref _bit2); + ser.Sync(nameof(_bit3), ref _bit3); + ser.Sync(nameof(_bit4), ref _bit4); + ser.Sync(nameof(_bit5), ref _bit5); + ser.Sync(nameof(_bitN), ref _bitN); + ser.Sync(nameof(_eMaskA), ref _eMaskA); + ser.Sync(nameof(_eMaskB), ref _eMaskB); + ser.Sync(nameof(_eMaskC), ref _eMaskC); + ser.Sync(nameof(_vA), ref _vA); + ser.Sync(nameof(_vB), ref _vB); + ser.Sync(nameof(_vC), ref _vC); + ser.Sync(nameof(_countA), ref _countA); + ser.Sync(nameof(_countB), ref _countB); + ser.Sync(nameof(_countC), ref _countC); + ser.Sync(nameof(_countE), ref _countE); + ser.Sync(nameof(_countN), ref _countN); + ser.Sync(nameof(_dividerA), ref _dividerA); + ser.Sync(nameof(_dividerB), ref _dividerB); + ser.Sync(nameof(_dividerC), ref _dividerC); + ser.Sync(nameof(_dividerE), ref _dividerE); + ser.Sync(nameof(_dividerN), ref _dividerN); + ser.SyncEnum(nameof(_currentPanTab), ref _currentPanTab); + ser.Sync(nameof(_volume), ref nullDump); - for (int i = 0; i < 6; i++) - { - ser.Sync("volTable" + i, ref _volumeTables[i], false); - } + for (int i = 0; i < 6; i++) + { + ser.Sync("volTable" + i, ref _volumeTables[i], false); + } - if (ser.IsReader) - _volume = _machine.Spectrum.Settings.AYVolume; + if (ser.IsReader) + _volume = _machine.Spectrum.Settings.AYVolume; - ser.EndSection(); - } + ser.EndSection(); + } - #endregion - } + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/CPUMonitor.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/CPUMonitor.cs index 922bea1d84..c06b5cd8b3 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/CPUMonitor.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/CPUMonitor.cs @@ -3,402 +3,402 @@ using BizHawk.Emulation.Cores.Components.Z80A; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// An intermediary class that manages cycling the ULA and CPU - /// along with inherent Port and Memory contention - /// - public class CPUMonitor - { - #region Devices + /// + /// An intermediary class that manages cycling the ULA and CPU + /// along with inherent Port and Memory contention + /// + public class CPUMonitor + { + #region Devices - private SpectrumBase _machine; - private Z80A _cpu; - public MachineType machineType = MachineType.ZXSpectrum48; + private SpectrumBase _machine; + private Z80A _cpu; + public MachineType machineType = MachineType.ZXSpectrum48; - #endregion + #endregion - #region Lookups + #region Lookups - /// - /// CPU total executes t-states - /// - public long TotalExecutedCycles => _cpu.TotalExecutedCycles; + /// + /// CPU total executes t-states + /// + public long TotalExecutedCycles => _cpu.TotalExecutedCycles; - /// - /// Current BUSRQ line array - /// - public ushort BUSRQ - { - get - { - switch (machineType) - { - case MachineType.ZXSpectrum128Plus2a: - case MachineType.ZXSpectrum128Plus3: - return _cpu.MEMRQ[_cpu.bus_pntr]; - default: + /// + /// Current BUSRQ line array + /// + public ushort BUSRQ + { + get + { + switch (machineType) + { + case MachineType.ZXSpectrum128Plus2a: + case MachineType.ZXSpectrum128Plus3: + return _cpu.MEMRQ[_cpu.bus_pntr]; + default: return _cpu.BUSRQ[_cpu.mem_pntr]; - } - } - } + } + } + } - #endregion + #endregion - #region Construction + #region Construction - public CPUMonitor(SpectrumBase machine) - { - _machine = machine; - _cpu = _machine.CPU; - } + public CPUMonitor(SpectrumBase machine) + { + _machine = machine; + _cpu = _machine.CPU; + } - #endregion + #endregion - #region State + #region State - /// - /// The last 16-bit port address that was detected - /// - public ushort lastPortAddr; + /// + /// The last 16-bit port address that was detected + /// + public ushort lastPortAddr; - /// - /// If true, the next read memory operation has been contended - /// - public bool NextMemReadContended; + /// + /// If true, the next read memory operation has been contended + /// + public bool NextMemReadContended; - #endregion + #endregion - #region Methods + #region Methods - /// - /// Handles the ULA and CPU cycle clocks, along with any memory and port contention - /// - public void ExecuteCycle() - { - // simulate the ULA clock cycle before the CPU cycle - _machine.ULADevice.CycleClock(TotalExecutedCycles); + /// + /// Handles the ULA and CPU cycle clocks, along with any memory and port contention + /// + public void ExecuteCycle() + { + // simulate the ULA clock cycle before the CPU cycle + _machine.ULADevice.CycleClock(TotalExecutedCycles); - // is the next CPU cycle causing a BUSRQ or IORQ? - if (BUSRQ > 0) - { - // check for IORQ - if (!CheckIO()) - { - // is the memory address of the BUSRQ potentially contended? - if (_machine.IsContended(AscertainBUSRQAddress())) - { - var cont = _machine.ULADevice.GetContentionValue((int)_machine.CurrentFrameCycle); - if (cont > 0) - { - _cpu.TotalExecutedCycles += cont; - NextMemReadContended = true; - } - } - } - } + // is the next CPU cycle causing a BUSRQ or IORQ? + if (BUSRQ > 0) + { + // check for IORQ + if (!CheckIO()) + { + // is the memory address of the BUSRQ potentially contended? + if (_machine.IsContended(AscertainBUSRQAddress())) + { + var cont = _machine.ULADevice.GetContentionValue((int)_machine.CurrentFrameCycle); + if (cont > 0) + { + _cpu.TotalExecutedCycles += cont; + NextMemReadContended = true; + } + } + } + } _cpu.ExecuteOne(); - } + } - /// - /// Looks up the current BUSRQ address that is about to be signalled on the upcoming cycle - /// - private ushort AscertainBUSRQAddress() - { - ushort addr = 0; - switch (BUSRQ) - { - // PCh - case 1: - addr = (ushort)(_cpu.Regs[_cpu.PCl] | _cpu.Regs[_cpu.PCh] << 8); - break; - // SPh - case 3: - addr = (ushort)(_cpu.Regs[_cpu.SPl] | _cpu.Regs[_cpu.SPh] << 8); - break; - // A - case 4: - addr = (ushort)(_cpu.Regs[_cpu.F] | _cpu.Regs[_cpu.A] << 8); - break; - // B - case 6: - addr = (ushort)(_cpu.Regs[_cpu.C] | _cpu.Regs[_cpu.B] << 8); - break; - // D - case 8: - addr = (ushort)(_cpu.Regs[_cpu.E] | _cpu.Regs[_cpu.D] << 8); - break; - // H - case 10: - addr = (ushort)(_cpu.Regs[_cpu.L] | _cpu.Regs[_cpu.H] << 8); - break; - // W - case 12: - addr = (ushort)(_cpu.Regs[_cpu.Z] | _cpu.Regs[_cpu.W] << 8); - break; - // Ixh - case 16: - addr = (ushort)(_cpu.Regs[_cpu.Ixl] | _cpu.Regs[_cpu.Ixh] << 8); - break; - // Iyh - case 18: - addr = (ushort)(_cpu.Regs[_cpu.Iyl] | _cpu.Regs[_cpu.Iyh] << 8); - break; - // I - case 21: - addr = (ushort)(_cpu.Regs[_cpu.R] | _cpu.Regs[_cpu.I] << 8); - break; - // BC - case Z80A.BIO1: - case Z80A.BIO2: - case Z80A.BIO3: - case Z80A.BIO4: - addr = (ushort)(_cpu.Regs[_cpu.C] | _cpu.Regs[_cpu.B] << 8); - break; - // WZ - case Z80A.WIO1: - case Z80A.WIO2: - case Z80A.WIO3: - case Z80A.WIO4: - addr = (ushort)(_cpu.Regs[_cpu.Z] | _cpu.Regs[_cpu.W] << 8); - break; - } + /// + /// Looks up the current BUSRQ address that is about to be signalled on the upcoming cycle + /// + private ushort AscertainBUSRQAddress() + { + ushort addr = 0; + switch (BUSRQ) + { + // PCh + case 1: + addr = (ushort)(_cpu.Regs[_cpu.PCl] | _cpu.Regs[_cpu.PCh] << 8); + break; + // SPh + case 3: + addr = (ushort)(_cpu.Regs[_cpu.SPl] | _cpu.Regs[_cpu.SPh] << 8); + break; + // A + case 4: + addr = (ushort)(_cpu.Regs[_cpu.F] | _cpu.Regs[_cpu.A] << 8); + break; + // B + case 6: + addr = (ushort)(_cpu.Regs[_cpu.C] | _cpu.Regs[_cpu.B] << 8); + break; + // D + case 8: + addr = (ushort)(_cpu.Regs[_cpu.E] | _cpu.Regs[_cpu.D] << 8); + break; + // H + case 10: + addr = (ushort)(_cpu.Regs[_cpu.L] | _cpu.Regs[_cpu.H] << 8); + break; + // W + case 12: + addr = (ushort)(_cpu.Regs[_cpu.Z] | _cpu.Regs[_cpu.W] << 8); + break; + // Ixh + case 16: + addr = (ushort)(_cpu.Regs[_cpu.Ixl] | _cpu.Regs[_cpu.Ixh] << 8); + break; + // Iyh + case 18: + addr = (ushort)(_cpu.Regs[_cpu.Iyl] | _cpu.Regs[_cpu.Iyh] << 8); + break; + // I + case 21: + addr = (ushort)(_cpu.Regs[_cpu.R] | _cpu.Regs[_cpu.I] << 8); + break; + // BC + case Z80A.BIO1: + case Z80A.BIO2: + case Z80A.BIO3: + case Z80A.BIO4: + addr = (ushort)(_cpu.Regs[_cpu.C] | _cpu.Regs[_cpu.B] << 8); + break; + // WZ + case Z80A.WIO1: + case Z80A.WIO2: + case Z80A.WIO3: + case Z80A.WIO4: + addr = (ushort)(_cpu.Regs[_cpu.Z] | _cpu.Regs[_cpu.W] << 8); + break; + } - return addr; - } + 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; + /// + /// 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; + 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; - } + // 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; - } + 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 - /// - private bool IsIOCycleContended(int T) - { - bool lowBitSet = (lastPortAddr & 0x0001) != 0; - bool highByte407f = false; + /// + /// 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 + /// + private bool IsIOCycleContended(int T) + { + bool lowBitSet = (lastPortAddr & 0x0001) != 0; + bool highByte407f = false; - switch (machineType) - { - case MachineType.ZXSpectrum16: - case MachineType.ZXSpectrum48: + switch (machineType) + { + case MachineType.ZXSpectrum16: + case MachineType.ZXSpectrum48: - if ((lastPortAddr & 0xc000) == 0x4000) - highByte407f = true; + if ((lastPortAddr & 0xc000) == 0x4000) + highByte407f = true; - if (highByte407f) - { - // high byte 40-7f - if (lowBitSet) - { - // high byte 40-7f - // low bit set - // C:1, C:1, C:1, C:1 - switch (T) - { - case 1: - case 2: - case 3: - case 4: - return true; - } - } - else - { - // high byte 40-7f - // low bit reset - // C:1, C:3 - switch (T) - { - case 1: - case 2: - return true; - } - } - } - else - { - // high byte not 40-7f - if (lowBitSet) - { - // high byte not 40-7f - // low bit set - // N:4 - } - else - { - // high byte not 40-7f - // low bit reset - // N:1, C:3 - switch (T) - { - case 2: - return true; - } - } - } - break; + if (highByte407f) + { + // high byte 40-7f + if (lowBitSet) + { + // high byte 40-7f + // low bit set + // C:1, C:1, C:1, C:1 + switch (T) + { + case 1: + case 2: + case 3: + case 4: + return true; + } + } + else + { + // high byte 40-7f + // low bit reset + // C:1, C:3 + switch (T) + { + case 1: + case 2: + return true; + } + } + } + else + { + // high byte not 40-7f + if (lowBitSet) + { + // high byte not 40-7f + // low bit set + // N:4 + } + else + { + // high byte not 40-7f + // low bit reset + // N:1, C:3 + switch (T) + { + case 2: + return true; + } + } + } + break; - case MachineType.ZXSpectrum128: - case MachineType.ZXSpectrum128Plus2: - if ((lastPortAddr & 0xc000) == 0x4000 || (lastPortAddr & 0xc000) == 0xc000 && _machine.ContendedBankPaged()) - highByte407f = true; + case MachineType.ZXSpectrum128: + case MachineType.ZXSpectrum128Plus2: + if ((lastPortAddr & 0xc000) == 0x4000 || (lastPortAddr & 0xc000) == 0xc000 && _machine.ContendedBankPaged()) + highByte407f = true; - if (highByte407f) - { - // high byte 40-7f - if (lowBitSet) - { - // high byte 40-7f - // low bit set - // C:1, C:1, C:1, C:1 - switch (T) - { - case 1: - case 2: - case 3: - case 4: - return true; - } - } - else - { - // high byte 40-7f - // low bit reset - // C:1, C:3 - switch (T) - { - case 1: - case 2: - return true; - } - } - } - else - { - // high byte not 40-7f - if (lowBitSet) - { - // high byte not 40-7f - // low bit set - // N:4 - } - else - { - // high byte not 40-7f - // low bit reset - // N:1, C:3 - switch (T) - { - case 2: - return true; - } - } - } - break; + if (highByte407f) + { + // high byte 40-7f + if (lowBitSet) + { + // high byte 40-7f + // low bit set + // C:1, C:1, C:1, C:1 + switch (T) + { + case 1: + case 2: + case 3: + case 4: + return true; + } + } + else + { + // high byte 40-7f + // low bit reset + // C:1, C:3 + switch (T) + { + case 1: + case 2: + return true; + } + } + } + else + { + // high byte not 40-7f + if (lowBitSet) + { + // high byte not 40-7f + // low bit set + // N:4 + } + else + { + // high byte not 40-7f + // low bit reset + // N:1, C:3 + switch (T) + { + case 2: + return true; + } + } + } + break; - case MachineType.ZXSpectrum128Plus2a: - case MachineType.ZXSpectrum128Plus3: - // No contention occurs as the ULA only applies contention when the Z80 MREQ line is active - // (which is not during an IO operation) - break; - } + case MachineType.ZXSpectrum128Plus2a: + case MachineType.ZXSpectrum128Plus3: + // No contention occurs as the ULA only applies contention when the Z80 MREQ line is active + // (which is not during an IO operation) + break; + } - return false; - } + return false; + } - /// - /// Called when the first byte of an instruction is fetched - /// - public void OnExecFetch(ushort firstByte) - { - // fetch instruction without incrementing pc - //_cpu.FetchInstruction(_cpu.FetchMemory(firstByte)); - - } + /// + /// Called when the first byte of an instruction is fetched + /// + public void OnExecFetch(ushort firstByte) + { + // fetch instruction without incrementing pc + //_cpu.FetchInstruction(_cpu.FetchMemory(firstByte)); - #endregion + } - #region Serialization + #endregion - public void SyncState(Serializer ser) - { - ser.BeginSection(nameof(CPUMonitor)); - ser.Sync(nameof(lastPortAddr), ref lastPortAddr); - ser.Sync(nameof(NextMemReadContended), ref NextMemReadContended); - ser.EndSection(); - } + #region Serialization - #endregion - } + public void SyncState(Serializer ser) + { + ser.BeginSection(nameof(CPUMonitor)); + ser.Sync(nameof(lastPortAddr), ref lastPortAddr); + ser.Sync(nameof(NextMemReadContended), ref NextMemReadContended); + ser.EndSection(); + } + + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/MachineType.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/MachineType.cs index 0e786823b8..04b85f50b6 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/MachineType.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/MachineType.cs @@ -1,44 +1,44 @@  namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// The various spectrum models ZXHawk emulates - /// - public enum MachineType - { - /// - /// Original Sinclair Spectrum 16K model - /// - ZXSpectrum16, + /// + /// The various spectrum models ZXHawk emulates + /// + public enum MachineType + { + /// + /// Original Sinclair Spectrum 16K model + /// + ZXSpectrum16, - /// - /// Sinclair Spectrum 48K model - /// - ZXSpectrum48, + /// + /// Sinclair Spectrum 48K model + /// + ZXSpectrum48, - /// - /// Sinclair Spectrum 128K model - /// - ZXSpectrum128, + /// + /// Sinclair Spectrum 128K model + /// + ZXSpectrum128, - /// - /// Sinclair Spectrum 128 +2 model - /// - ZXSpectrum128Plus2, + /// + /// Sinclair Spectrum 128 +2 model + /// + ZXSpectrum128Plus2, - /// - /// Sinclair Spectrum 128 +2a model (same as the +3 just without disk drive) - /// - ZXSpectrum128Plus2a, + /// + /// Sinclair Spectrum 128 +2a model (same as the +3 just without disk drive) + /// + ZXSpectrum128Plus2a, - /// - /// Sinclair Spectrum 128 +3 model - /// - ZXSpectrum128Plus3, + /// + /// Sinclair Spectrum 128 +3 model + /// + ZXSpectrum128Plus3, /// /// Russian 128k pentagon clone /// Pentagon128, - } + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/Pentagon128K/Pentagon128.Screen.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/Pentagon128K/Pentagon128.Screen.cs index 9a9b8229ef..da3a95b61d 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/Pentagon128K/Pentagon128.Screen.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/Pentagon128K/Pentagon128.Screen.cs @@ -1,48 +1,48 @@  namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// 128K/+2 ULA - /// - class ScreenPentagon128 : ULA - { - #region Construction + /// + /// 128K/+2 ULA + /// + class ScreenPentagon128 : ULA + { + #region Construction - public ScreenPentagon128(SpectrumBase machine) + public ScreenPentagon128(SpectrumBase machine) : base(machine) - { + { // interrupt InterruptStartTime = 0;// 3; - InterruptLength = 32; - // offsets - RenderTableOffset = 58; - ContentionOffset = 6; - FloatingBusOffset = 1; - // timing - ClockSpeed = 3546900; - FrameCycleLength = 71680; - ScanlineTime = 224; - BorderLeftTime = 24; - BorderRightTime = 24; - FirstPaperLine = 80; - FirstPaperTState = 68; - // screen layout - Border4T = false; - Border4TStage = 1; - ScreenWidth = 256; - ScreenHeight = 192; - BorderTopHeight = 48; // 55; // 48; - BorderBottomHeight = 48; // 56; - BorderLeftWidth = 48; - BorderRightWidth = 48; - ScanLineWidth = BorderLeftWidth + ScreenWidth + BorderRightWidth; + InterruptLength = 32; + // offsets + RenderTableOffset = 58; + ContentionOffset = 6; + FloatingBusOffset = 1; + // timing + ClockSpeed = 3546900; + FrameCycleLength = 71680; + ScanlineTime = 224; + BorderLeftTime = 24; + BorderRightTime = 24; + FirstPaperLine = 80; + FirstPaperTState = 68; + // screen layout + Border4T = false; + Border4TStage = 1; + ScreenWidth = 256; + ScreenHeight = 192; + BorderTopHeight = 48; // 55; // 48; + BorderBottomHeight = 48; // 56; + BorderLeftWidth = 48; + BorderRightWidth = 48; + ScanLineWidth = BorderLeftWidth + ScreenWidth + BorderRightWidth; - RenderingTable = new RenderTable(this, - MachineType.ZXSpectrum128); + RenderingTable = new RenderTable(this, + MachineType.ZXSpectrum128); - SetupScreenSize(); - } + SetupScreenSize(); + } - #endregion - } + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/Pentagon128K/Pentagon128.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/Pentagon128K/Pentagon128.cs index c8f201157e..46e72eff95 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/Pentagon128K/Pentagon128.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/Pentagon128K/Pentagon128.cs @@ -4,48 +4,48 @@ using BizHawk.Emulation.Cores.Sound; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// 128K Constructor - /// - public partial class Pentagon128 : SpectrumBase - { - #region Construction + /// + /// 128K Constructor + /// + public partial class Pentagon128 : SpectrumBase + { + #region Construction - /// - /// Main constructor - /// - public Pentagon128(ZXSpectrum spectrum, Z80A cpu, ZXSpectrum.BorderType borderType, List files, List joysticks) - { - Spectrum = spectrum; - CPU = cpu; + /// + /// Main constructor + /// + public Pentagon128(ZXSpectrum spectrum, Z80A cpu, ZXSpectrum.BorderType borderType, List files, List joysticks) + { + Spectrum = spectrum; + CPU = cpu; - CPUMon = new CPUMonitor(this); - CPUMon.machineType = MachineType.Pentagon128; + CPUMon = new CPUMonitor(this); + CPUMon.machineType = MachineType.Pentagon128; - ROMPaged = 0; - SHADOWPaged = false; - RAMPaged = 0; - PagingDisabled = false; - - ULADevice = new ScreenPentagon128(this); + ROMPaged = 0; + SHADOWPaged = false; + RAMPaged = 0; + PagingDisabled = false; + + ULADevice = new ScreenPentagon128(this); BuzzerDevice = new OneBitBeeper(44100, ULADevice.FrameLength, 50, "SystemBuzzer"); - TapeBuzzer = new OneBitBeeper(44100, ULADevice.FrameLength, 50, "TapeBuzzer"); + TapeBuzzer = new OneBitBeeper(44100, ULADevice.FrameLength, 50, "TapeBuzzer"); - AYDevice = new AY38912(this); - AYDevice.Init(44100, ULADevice.FrameLength); + AYDevice = new AY38912(this); + AYDevice.Init(44100, ULADevice.FrameLength); - KeyboardDevice = new StandardKeyboard(this); + KeyboardDevice = new StandardKeyboard(this); - InitJoysticks(joysticks); + InitJoysticks(joysticks); - TapeDevice = new DatacorderDevice(spectrum.SyncSettings.AutoLoadTape); - TapeDevice.Init(this); + TapeDevice = new DatacorderDevice(spectrum.SyncSettings.AutoLoadTape); + TapeDevice.Init(this); - InitializeMedia(files); - } + InitializeMedia(files); + } - #endregion - } + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/SpectrumBase.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/SpectrumBase.cs index 56db60c5b0..3e71750447 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/SpectrumBase.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/SpectrumBase.cs @@ -4,166 +4,166 @@ using BizHawk.Emulation.Cores.Sound; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// The abstract class that all emulated models will inherit from - /// * Main properties / fields / contruction* - /// - public abstract partial class SpectrumBase - { - #region Devices + /// + /// The abstract class that all emulated models will inherit from + /// * Main properties / fields / contruction* + /// + public abstract partial class SpectrumBase + { + #region Devices - /// - /// The calling ZXSpectrum class (piped in via constructor) - /// - public ZXSpectrum Spectrum { get; set; } + /// + /// The calling ZXSpectrum class (piped in via constructor) + /// + public ZXSpectrum Spectrum { get; set; } - /// - /// Reference to the instantiated Z80 cpu (piped in via constructor) - /// - public Z80A CPU { get; set; } + /// + /// Reference to the instantiated Z80 cpu (piped in via constructor) + /// + public Z80A CPU { get; set; } - /// - /// ROM and extended info - /// - public RomData RomData { get; set; } + /// + /// ROM and extended info + /// + public RomData RomData { get; set; } - /// - /// The emulated ULA device - /// - //public ULABase ULADevice { get; set; } - public ULA ULADevice { get; set; } + /// + /// The emulated ULA device + /// + //public ULABase ULADevice { get; set; } + public ULA ULADevice { get; set; } - /// - /// Monitors CPU cycles - /// - public CPUMonitor CPUMon { get; set; } + /// + /// Monitors CPU cycles + /// + public CPUMonitor CPUMon { get; set; } - /// - /// The spectrum buzzer/beeper - /// - public OneBitBeeper BuzzerDevice { get; set; } + /// + /// The spectrum buzzer/beeper + /// + public OneBitBeeper BuzzerDevice { get; set; } - /// - /// A second beeper for the tape - /// - public OneBitBeeper TapeBuzzer { get; set; } + /// + /// A second beeper for the tape + /// + public OneBitBeeper TapeBuzzer { get; set; } - /// - /// Device representing the AY-3-8912 chip found in the 128k and up spectrums - /// - public IPSG AYDevice { get; set; } + /// + /// Device representing the AY-3-8912 chip found in the 128k and up spectrums + /// + public IPSG AYDevice { get; set; } - /// - /// The spectrum keyboard - /// - public virtual IKeyboard KeyboardDevice { get; set; } + /// + /// The spectrum keyboard + /// + public virtual IKeyboard KeyboardDevice { get; set; } - /// - /// The spectrum datacorder device - /// - public virtual DatacorderDevice TapeDevice { get; set; } + /// + /// The spectrum datacorder device + /// + public virtual DatacorderDevice TapeDevice { get; set; } - /// - /// The +3 built-in disk drive - /// - public virtual NECUPD765 UPDDiskDevice { get; set; } + /// + /// The +3 built-in disk drive + /// + public virtual NECUPD765 UPDDiskDevice { get; set; } - /// - /// Holds the currently selected joysticks - /// - public virtual IJoystick[] JoystickCollection { get; set; } + /// + /// Holds the currently selected joysticks + /// + public virtual IJoystick[] JoystickCollection { get; set; } - /// - /// +3/2a printer port strobe - /// - protected bool PrinterPortStrobe; + /// + /// +3/2a printer port strobe + /// + protected bool PrinterPortStrobe; - #endregion + #endregion - #region Emulator State + #region Emulator State - /// - /// Signs whether the frame has ended - /// - public bool FrameCompleted; + /// + /// Signs whether the frame has ended + /// + public bool FrameCompleted; - /// - /// Overflow from the previous frame (in Z80 cycles) - /// - public int OverFlow; + /// + /// Overflow from the previous frame (in Z80 cycles) + /// + public int OverFlow; - /// - /// The total number of frames rendered - /// - public int FrameCount; + /// + /// The total number of frames rendered + /// + public int FrameCount; - /// - /// The current cycle (T-State) that we are at in the frame - /// - public long _frameCycles; + /// + /// The current cycle (T-State) that we are at in the frame + /// + public long _frameCycles; - /// - /// Stores where we are in the frame after each CPU cycle - /// - public long LastFrameStartCPUTick; + /// + /// Stores where we are in the frame after each CPU cycle + /// + public long LastFrameStartCPUTick; - /// - /// Gets the current frame cycle according to the CPU tick count - /// - public virtual long CurrentFrameCycle => CPU.TotalExecutedCycles - LastFrameStartCPUTick; + /// + /// Gets the current frame cycle according to the CPU tick count + /// + public virtual long CurrentFrameCycle => CPU.TotalExecutedCycles - LastFrameStartCPUTick; - /// - /// Non-Deterministic bools - /// - public bool _render; - public bool _renderSound; + /// + /// Non-Deterministic bools + /// + public bool _render; + public bool _renderSound; - #endregion + #endregion - #region Constants + #region Constants - /// - /// Mask constants & misc - /// - protected const int BORDER_BIT = 0x07; - protected const int EAR_BIT = 0x10; - protected const int MIC_BIT = 0x08; - protected const int TAPE_BIT = 0x40; - protected const int AY_SAMPLE_RATE = 16; + /// + /// Mask constants & misc + /// + protected const int BORDER_BIT = 0x07; + protected const int EAR_BIT = 0x10; + protected const int MIC_BIT = 0x08; + protected const int TAPE_BIT = 0x40; + protected const int AY_SAMPLE_RATE = 16; - #endregion + #endregion - #region Emulation Loop + #region Emulation Loop - /// - /// Executes a single frame - /// - public virtual void ExecuteFrame(bool render, bool renderSound) - { - ULADevice.FrameEnd = false; - ULADevice.ULACycleCounter = CurrentFrameCycle; + /// + /// Executes a single frame + /// + public virtual void ExecuteFrame(bool render, bool renderSound) + { + ULADevice.FrameEnd = false; + ULADevice.ULACycleCounter = CurrentFrameCycle; - InputRead = false; - _render = render; - _renderSound = renderSound; + InputRead = false; + _render = render; + _renderSound = renderSound; - FrameCompleted = false; + FrameCompleted = false; - //if (UPDDiskDevice == null || !UPDDiskDevice.FDD_IsDiskLoaded) - //TapeDevice.StartFrame(); + //if (UPDDiskDevice == null || !UPDDiskDevice.FDD_IsDiskLoaded) + //TapeDevice.StartFrame(); - if (_renderSound) - { - if (AYDevice != null) - AYDevice.StartFrame(); - } + if (_renderSound) + { + if (AYDevice != null) + AYDevice.StartFrame(); + } - PollInput(); + PollInput(); - for (;;) - { - // run the CPU Monitor cycle - CPUMon.ExecuteCycle(); + for (; ; ) + { + // run the CPU Monitor cycle + CPUMon.ExecuteCycle(); // clock the beepers TapeBuzzer.SetClock((int)CurrentFrameCycle); @@ -171,229 +171,229 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum // cycle the tape device if (UPDDiskDevice == null || !UPDDiskDevice.FDD_IsDiskLoaded) - TapeDevice.TapeCycle(); + TapeDevice.TapeCycle(); - // has frame end been reached? - if (ULADevice.FrameEnd) - break; - } + // has frame end been reached? + if (ULADevice.FrameEnd) + break; + } - OverFlow = (int)CurrentFrameCycle - ULADevice.FrameLength; + OverFlow = (int)CurrentFrameCycle - ULADevice.FrameLength; - // we have reached the end of a frame - LastFrameStartCPUTick = CPU.TotalExecutedCycles - OverFlow; + // we have reached the end of a frame + LastFrameStartCPUTick = CPU.TotalExecutedCycles - OverFlow; - ULADevice.LastTState = 0; + ULADevice.LastTState = 0; - if (AYDevice != null) - AYDevice.EndFrame(); + if (AYDevice != null) + AYDevice.EndFrame(); - FrameCount++; - - if (UPDDiskDevice == null || !UPDDiskDevice.FDD_IsDiskLoaded) - TapeDevice.EndFrame(); + FrameCount++; - FrameCompleted = true; + if (UPDDiskDevice == null || !UPDDiskDevice.FDD_IsDiskLoaded) + TapeDevice.EndFrame(); - // is this a lag frame? - Spectrum.IsLagFrame = !InputRead; + FrameCompleted = true; - // FDC debug - if (UPDDiskDevice != null && UPDDiskDevice.writeDebug) - { - // only write UPD log every second - if (FrameCount % 10 == 0) - { - System.IO.File.AppendAllLines(UPDDiskDevice.outputfile, UPDDiskDevice.dLog); - UPDDiskDevice.dLog = new System.Collections.Generic.List(); - //System.IO.File.WriteAllText(UPDDiskDevice.outputfile, UPDDiskDevice.outputString); - } - } - } + // is this a lag frame? + Spectrum.IsLagFrame = !InputRead; - #endregion + // FDC debug + if (UPDDiskDevice != null && UPDDiskDevice.writeDebug) + { + // only write UPD log every second + if (FrameCount % 10 == 0) + { + System.IO.File.AppendAllLines(UPDDiskDevice.outputfile, UPDDiskDevice.dLog); + UPDDiskDevice.dLog = new System.Collections.Generic.List(); + //System.IO.File.WriteAllText(UPDDiskDevice.outputfile, UPDDiskDevice.outputString); + } + } + } - #region Reset Functions + #endregion - /// - /// Hard reset of the emulated machine - /// - public virtual void HardReset() - { - //ULADevice.ResetInterrupt(); - ROMPaged = 0; - SpecialPagingMode = false; - RAMPaged = 0; - CPU.RegPC = 0; + #region Reset Functions - Spectrum.SetCpuRegister("SP", 0xFFFF); - Spectrum.SetCpuRegister("IY", 0xFFFF); - Spectrum.SetCpuRegister("IX", 0xFFFF); - Spectrum.SetCpuRegister("AF", 0xFFFF); - Spectrum.SetCpuRegister("BC", 0xFFFF); - Spectrum.SetCpuRegister("DE", 0xFFFF); - Spectrum.SetCpuRegister("HL", 0xFFFF); - Spectrum.SetCpuRegister("SP", 0xFFFF); - Spectrum.SetCpuRegister("Shadow AF", 0xFFFF); - Spectrum.SetCpuRegister("Shadow BC", 0xFFFF); - Spectrum.SetCpuRegister("Shadow DE", 0xFFFF); - Spectrum.SetCpuRegister("Shadow HL", 0xFFFF); + /// + /// Hard reset of the emulated machine + /// + public virtual void HardReset() + { + //ULADevice.ResetInterrupt(); + ROMPaged = 0; + SpecialPagingMode = false; + RAMPaged = 0; + CPU.RegPC = 0; - CPU.Regs[CPU.I] = 0; - CPU.Regs[CPU.R] = 0; + Spectrum.SetCpuRegister("SP", 0xFFFF); + Spectrum.SetCpuRegister("IY", 0xFFFF); + Spectrum.SetCpuRegister("IX", 0xFFFF); + Spectrum.SetCpuRegister("AF", 0xFFFF); + Spectrum.SetCpuRegister("BC", 0xFFFF); + Spectrum.SetCpuRegister("DE", 0xFFFF); + Spectrum.SetCpuRegister("HL", 0xFFFF); + Spectrum.SetCpuRegister("SP", 0xFFFF); + Spectrum.SetCpuRegister("Shadow AF", 0xFFFF); + Spectrum.SetCpuRegister("Shadow BC", 0xFFFF); + Spectrum.SetCpuRegister("Shadow DE", 0xFFFF); + Spectrum.SetCpuRegister("Shadow HL", 0xFFFF); - TapeDevice.Reset(); - if (AYDevice != null) - AYDevice.Reset(); + CPU.Regs[CPU.I] = 0; + CPU.Regs[CPU.R] = 0; - byte[][] rams = new byte[][] - { - RAM0, - RAM1, - RAM2, - RAM3, - RAM4, - RAM5, - RAM6, - RAM7 - }; + TapeDevice.Reset(); + if (AYDevice != null) + AYDevice.Reset(); - foreach (var r in rams) - { - for (int i = 0; i < r.Length; i++) - { - r[i] = 0x00; - } - } - } + byte[][] rams = new byte[][] + { + RAM0, + RAM1, + RAM2, + RAM3, + RAM4, + RAM5, + RAM6, + RAM7 + }; - /// - /// Soft reset of the emulated machine - /// - public virtual void SoftReset() - { - //ULADevice.ResetInterrupt(); - ROMPaged = 0; - SpecialPagingMode = false; - RAMPaged = 0; - CPU.RegPC = 0; + foreach (var r in rams) + { + for (int i = 0; i < r.Length; i++) + { + r[i] = 0x00; + } + } + } - Spectrum.SetCpuRegister("SP", 0xFFFF); - Spectrum.SetCpuRegister("IY", 0xFFFF); - Spectrum.SetCpuRegister("IX", 0xFFFF); - Spectrum.SetCpuRegister("AF", 0xFFFF); - Spectrum.SetCpuRegister("BC", 0xFFFF); - Spectrum.SetCpuRegister("DE", 0xFFFF); - Spectrum.SetCpuRegister("HL", 0xFFFF); - Spectrum.SetCpuRegister("SP", 0xFFFF); - Spectrum.SetCpuRegister("Shadow AF", 0xFFFF); - Spectrum.SetCpuRegister("Shadow BC", 0xFFFF); - Spectrum.SetCpuRegister("Shadow DE", 0xFFFF); - Spectrum.SetCpuRegister("Shadow HL", 0xFFFF); + /// + /// Soft reset of the emulated machine + /// + public virtual void SoftReset() + { + //ULADevice.ResetInterrupt(); + ROMPaged = 0; + SpecialPagingMode = false; + RAMPaged = 0; + CPU.RegPC = 0; - CPU.Regs[CPU.I] = 0; - CPU.Regs[CPU.R] = 0; + Spectrum.SetCpuRegister("SP", 0xFFFF); + Spectrum.SetCpuRegister("IY", 0xFFFF); + Spectrum.SetCpuRegister("IX", 0xFFFF); + Spectrum.SetCpuRegister("AF", 0xFFFF); + Spectrum.SetCpuRegister("BC", 0xFFFF); + Spectrum.SetCpuRegister("DE", 0xFFFF); + Spectrum.SetCpuRegister("HL", 0xFFFF); + Spectrum.SetCpuRegister("SP", 0xFFFF); + Spectrum.SetCpuRegister("Shadow AF", 0xFFFF); + Spectrum.SetCpuRegister("Shadow BC", 0xFFFF); + Spectrum.SetCpuRegister("Shadow DE", 0xFFFF); + Spectrum.SetCpuRegister("Shadow HL", 0xFFFF); - TapeDevice.Reset(); - if (AYDevice != null) - AYDevice.Reset(); + CPU.Regs[CPU.I] = 0; + CPU.Regs[CPU.R] = 0; - byte[][] rams = new byte[][] - { - RAM0, - RAM1, - RAM2, - RAM3, - RAM4, - RAM5, - RAM6, - RAM7 - }; + TapeDevice.Reset(); + if (AYDevice != null) + AYDevice.Reset(); - foreach (var r in rams) - { - for (int i = 0; i < r.Length; i++) - { - r[i] = 0x00; - } - } - } + byte[][] rams = new byte[][] + { + RAM0, + RAM1, + RAM2, + RAM3, + RAM4, + RAM5, + RAM6, + RAM7 + }; - #endregion + foreach (var r in rams) + { + for (int i = 0; i < r.Length; i++) + { + r[i] = 0x00; + } + } + } - #region IStatable + #endregion - public void SyncState(Serializer ser) - { - ser.BeginSection("ZXMachine"); - ser.Sync(nameof(FrameCompleted), ref FrameCompleted); - ser.Sync(nameof(OverFlow), ref OverFlow); - ser.Sync(nameof(FrameCount), ref FrameCount); - ser.Sync(nameof(_frameCycles), ref _frameCycles); - ser.Sync(nameof(inputRead), ref inputRead); - ser.Sync(nameof(LastFrameStartCPUTick), ref LastFrameStartCPUTick); - ser.Sync(nameof(LastULAOutByte), ref LastULAOutByte); - ser.Sync(nameof(ROM0), ref ROM0, false); - ser.Sync(nameof(ROM1), ref ROM1, false); - ser.Sync(nameof(ROM2), ref ROM2, false); - ser.Sync(nameof(ROM3), ref ROM3, false); - ser.Sync(nameof(RAM0), ref RAM0, false); - ser.Sync(nameof(RAM1), ref RAM1, false); - ser.Sync(nameof(RAM2), ref RAM2, false); - ser.Sync(nameof(RAM3), ref RAM3, false); - ser.Sync(nameof(RAM4), ref RAM4, false); - ser.Sync(nameof(RAM5), ref RAM5, false); - ser.Sync(nameof(RAM6), ref RAM6, false); - ser.Sync(nameof(RAM7), ref RAM7, false); - ser.Sync(nameof(ROMPaged), ref ROMPaged); - ser.Sync(nameof(SHADOWPaged), ref SHADOWPaged); - ser.Sync(nameof(RAMPaged), ref RAMPaged); - ser.Sync(nameof(PagingDisabled), ref PagingDisabled); - ser.Sync(nameof(SpecialPagingMode), ref SpecialPagingMode); - ser.Sync(nameof(PagingConfiguration), ref PagingConfiguration); - ser.Sync(nameof(ROMhigh), ref ROMhigh); - ser.Sync(nameof(ROMlow), ref ROMlow); - ser.Sync(nameof(LastContendedReadByte), ref LastContendedReadByte); + #region IStatable - KeyboardDevice.SyncState(ser); - BuzzerDevice.SyncState(ser); - TapeBuzzer.SyncState(ser); - ULADevice.SyncState(ser); - CPUMon.SyncState(ser); + public void SyncState(Serializer ser) + { + ser.BeginSection("ZXMachine"); + ser.Sync(nameof(FrameCompleted), ref FrameCompleted); + ser.Sync(nameof(OverFlow), ref OverFlow); + ser.Sync(nameof(FrameCount), ref FrameCount); + ser.Sync(nameof(_frameCycles), ref _frameCycles); + ser.Sync(nameof(inputRead), ref inputRead); + ser.Sync(nameof(LastFrameStartCPUTick), ref LastFrameStartCPUTick); + ser.Sync(nameof(LastULAOutByte), ref LastULAOutByte); + ser.Sync(nameof(ROM0), ref ROM0, false); + ser.Sync(nameof(ROM1), ref ROM1, false); + ser.Sync(nameof(ROM2), ref ROM2, false); + ser.Sync(nameof(ROM3), ref ROM3, false); + ser.Sync(nameof(RAM0), ref RAM0, false); + ser.Sync(nameof(RAM1), ref RAM1, false); + ser.Sync(nameof(RAM2), ref RAM2, false); + ser.Sync(nameof(RAM3), ref RAM3, false); + ser.Sync(nameof(RAM4), ref RAM4, false); + ser.Sync(nameof(RAM5), ref RAM5, false); + ser.Sync(nameof(RAM6), ref RAM6, false); + ser.Sync(nameof(RAM7), ref RAM7, false); + ser.Sync(nameof(ROMPaged), ref ROMPaged); + ser.Sync(nameof(SHADOWPaged), ref SHADOWPaged); + ser.Sync(nameof(RAMPaged), ref RAMPaged); + ser.Sync(nameof(PagingDisabled), ref PagingDisabled); + ser.Sync(nameof(SpecialPagingMode), ref SpecialPagingMode); + ser.Sync(nameof(PagingConfiguration), ref PagingConfiguration); + ser.Sync(nameof(ROMhigh), ref ROMhigh); + ser.Sync(nameof(ROMlow), ref ROMlow); + ser.Sync(nameof(LastContendedReadByte), ref LastContendedReadByte); - if (AYDevice != null) - { - AYDevice.SyncState(ser); - ((AY38912)AYDevice as AY38912).PanningConfiguration = Spectrum.Settings.AYPanConfig; - } + KeyboardDevice.SyncState(ser); + BuzzerDevice.SyncState(ser); + TapeBuzzer.SyncState(ser); + ULADevice.SyncState(ser); + CPUMon.SyncState(ser); - ser.Sync(nameof(tapeMediaIndex), ref tapeMediaIndex); - if (ser.IsReader) - { - IsLoadState = true; - TapeMediaIndex = tapeMediaIndex; - IsLoadState = false; - } - + if (AYDevice != null) + { + AYDevice.SyncState(ser); + ((AY38912)AYDevice as AY38912).PanningConfiguration = Spectrum.Settings.AYPanConfig; + } - TapeDevice.SyncState(ser); + ser.Sync(nameof(tapeMediaIndex), ref tapeMediaIndex); + if (ser.IsReader) + { + IsLoadState = true; + TapeMediaIndex = tapeMediaIndex; + IsLoadState = false; + } - ser.Sync(nameof(diskMediaIndex), ref diskMediaIndex); - if (ser.IsReader) - { - IsLoadState = true; - DiskMediaIndex = diskMediaIndex; - IsLoadState = false; - } - if (UPDDiskDevice != null) - { - UPDDiskDevice.SyncState(ser); - } + TapeDevice.SyncState(ser); - ser.EndSection(); - } + ser.Sync(nameof(diskMediaIndex), ref diskMediaIndex); + if (ser.IsReader) + { + IsLoadState = true; + DiskMediaIndex = diskMediaIndex; + IsLoadState = false; + } - #endregion - } + if (UPDDiskDevice != null) + { + UPDDiskDevice.SyncState(ser); + } + + ser.EndSection(); + } + + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ULA.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ULA.cs index d9883a4321..a157f02d85 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ULA.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ULA.cs @@ -5,44 +5,44 @@ using System.Collections.Generic; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Uncommitted logic array implementation (ULA) - /// - public abstract class ULA : IVideoProvider - { - #region Other Devices + /// + /// Uncommitted logic array implementation (ULA) + /// + public abstract class ULA : IVideoProvider + { + #region Other Devices - /// - /// The emulated spectrum - /// - protected SpectrumBase _machine; + /// + /// The emulated spectrum + /// + protected SpectrumBase _machine; - /// - /// The CPU monitor class - /// - protected CPUMonitor CPUMon; + /// + /// The CPU monitor class + /// + protected CPUMonitor CPUMon; - #endregion + #endregion - #region Construction & Initialisation + #region Construction & Initialisation - public ULA (SpectrumBase machine) - { - _machine = machine; - CPUMon = _machine.CPUMon; - borderType = _machine.Spectrum.SyncSettings.BorderType; - } + public ULA(SpectrumBase machine) + { + _machine = machine; + CPUMon = _machine.CPUMon; + borderType = _machine.Spectrum.SyncSettings.BorderType; + } - #endregion + #endregion - #region Palettes + #region Palettes - /// - /// The standard ULA palette - /// - private static readonly int[] ULAPalette = - { - Colors.ARGB(0x00, 0x00, 0x00), // Black + /// + /// The standard ULA palette + /// + private static readonly int[] ULAPalette = + { + Colors.ARGB(0x00, 0x00, 0x00), // Black Colors.ARGB(0x00, 0x00, 0xD7), // Blue Colors.ARGB(0xD7, 0x00, 0x00), // Red Colors.ARGB(0xD7, 0x00, 0xD7), // Magenta @@ -60,998 +60,998 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum Colors.ARGB(0xFF, 0xFF, 0xFF), // Bright White }; - #endregion + #endregion - #region Timing + #region Timing - /// - /// The CPU speed - /// - public int ClockSpeed; + /// + /// The CPU speed + /// + public int ClockSpeed; - /// - /// Length of frame in T-State cycles - /// - public int FrameCycleLength; + /// + /// Length of frame in T-State cycles + /// + public int FrameCycleLength; - /// - /// The T-State at which the interrupt should be raised within the frame - /// - public int InterruptStartTime; + /// + /// The T-State at which the interrupt should be raised within the frame + /// + public int InterruptStartTime; - /// - /// The period for which the interrupt should he held - /// (simulated /INT pin held low) - /// - public int InterruptLength; + /// + /// The period for which the interrupt should he held + /// (simulated /INT pin held low) + /// + public int InterruptLength; - /// - /// Contention offset - /// - public int ContentionOffset; + /// + /// Contention offset + /// + public int ContentionOffset; - /// - /// Arbitrary offset for render table generation - /// - public int RenderTableOffset; + /// + /// Arbitrary offset for render table generation + /// + public int RenderTableOffset; - /// - /// The offset when return floating bus bytes - /// - public int FloatingBusOffset; + /// + /// The offset when return floating bus bytes + /// + public int FloatingBusOffset; - /// - /// The time in T-States for one scanline to complete - /// - public int ScanlineTime; + /// + /// The time in T-States for one scanline to complete + /// + public int ScanlineTime; - /// - /// T-States at the left border - /// - public int BorderLeftTime; + /// + /// T-States at the left border + /// + public int BorderLeftTime; - /// - /// T-States at the right border - /// - public int BorderRightTime; + /// + /// T-States at the right border + /// + public int BorderRightTime; - public int FirstPaperLine; - public int FirstPaperTState; - public bool Border4T; - public int Border4TStage; + public int FirstPaperLine; + public int FirstPaperTState; + public bool Border4T; + public int Border4TStage; - #endregion + #endregion - #region Interrupt Generation + #region Interrupt Generation - /// - /// Signs that an interrupt has been raised in this frame. - /// - protected bool InterruptRaised; + /// + /// Signs that an interrupt has been raised in this frame. + /// + protected bool InterruptRaised; - public long ULACycleCounter; - public long LastULATick; - public bool FrameEnd; + public long ULACycleCounter; + public long LastULATick; + public bool FrameEnd; - /// - /// Cycles the ULA clock - /// Handles interrupt generation - /// - public virtual void CycleClock(long totalCycles) - { - // render the screen - if (_machine._render) - RenderScreen((int)_machine.CurrentFrameCycle); + /// + /// Cycles the ULA clock + /// Handles interrupt generation + /// + public virtual void CycleClock(long totalCycles) + { + // render the screen + if (_machine._render) + RenderScreen((int)_machine.CurrentFrameCycle); - // has more than one cycle past since this last ran - // (this can be true if contention has taken place) - var ticksToProcess = totalCycles - LastULATick; + // has more than one cycle past since this last ran + // (this can be true if contention has taken place) + var ticksToProcess = totalCycles - LastULATick; - // store the current cycle - LastULATick = totalCycles; - - // process the cycles past as well as the upcoming one - for (int i = 0; i < ticksToProcess; i++) - { - ULACycleCounter++; + // store the current cycle + LastULATick = totalCycles; - if (InterruptRaised) - { - // /INT pin is currently being held low - if (ULACycleCounter < InterruptLength + InterruptStartTime) - { - // ULA should still hold the /INT pin low - _machine.CPU.FlagI = true; - } - else - { - // its time (or past time) to stop holding the /INT pin low - _machine.CPU.FlagI = false; - InterruptRaised = false; - } - } - else - { - // interrupt is currently not raised - if (ULACycleCounter == FrameLength + InterruptStartTime) - { - // time to raise the interrupt - InterruptRaised = true; + // process the cycles past as well as the upcoming one + for (int i = 0; i < ticksToProcess; i++) + { + ULACycleCounter++; + + if (InterruptRaised) + { + // /INT pin is currently being held low + if (ULACycleCounter < InterruptLength + InterruptStartTime) + { + // ULA should still hold the /INT pin low _machine.CPU.FlagI = true; - FrameEnd = true; - ULACycleCounter = InterruptStartTime; - CalcFlashCounter(); - } - } - } - } - - /// - /// Flash processing - /// - public void CalcFlashCounter() - { - flashCounter++; - - if (flashCounter > 15) - { - flashOn = !flashOn; - flashCounter = 0; - } - } - - #endregion - - #region Screen Layout - - /// - /// Total pixels in one display row - /// - protected int ScreenWidth; - /// - /// Total pixels in one display column - /// - protected int ScreenHeight; - /// - /// Total pixels in top border - /// - protected int BorderTopHeight; - /// - /// Total pixels in bottom border - /// - protected int BorderBottomHeight; - /// - /// Total pixels in left border width - /// - protected int BorderLeftWidth; - /// - /// Total pixels in right border width - /// - protected int BorderRightWidth; - /// - /// Total pixels in one scanline - /// - protected int ScanLineWidth; - - #endregion - - #region State - - /// - /// The last T-State cycle at which the screen was rendered - /// - public int LastTState; - - /// - /// Flash state - /// - public bool flashOn; - - private int flashCounter; - - protected byte fetchB1; - protected byte fetchA1; - protected byte fetchB2; - protected byte fetchA2; - protected int ink; - protected int paper; - protected int fetchBorder; - protected int bright; - protected int flash; - - public int palPaper; - public int palInk; - - public int BorderColor = 7; - - #endregion - - #region Conversions - - public int FrameLength => FrameCycleLength; - - #endregion - - #region Rendering Configuration - - /// - /// Holds all information regarding rendering the screen based on the current T-State - /// - public RenderTable RenderingTable; - - /// - /// Holds all information regarding rendering the screen based on the current T-State - /// - public class RenderTable - { - /// - /// The ULA device - /// - private ULA _ula; - - /// - /// Array of rendercycle entries - /// Starting from the interrupt - /// - public RenderCycle[] Renderer; - - /// - /// The emulated machine - /// - public MachineType _machineType; - - public int Offset; - - /// - /// Constructor - /// - public RenderTable(ULA ula, MachineType machineType) - { - _ula = ula; - _machineType = machineType; - Renderer = new RenderCycle[_ula.FrameCycleLength]; - InitRenderer(machineType); - } - - /// - /// Initializes the renderer - /// - private void InitRenderer(MachineType machineType) - { - for (var t = 0; t < _ula.FrameCycleLength; t++) - { - var tStateScreen = t + _ula.RenderTableOffset;// + _ula.InterruptStartTime; - - if (tStateScreen < 0) - tStateScreen += _ula.FrameCycleLength; - else if (tStateScreen >= _ula.FrameCycleLength) - tStateScreen -= _ula.FrameCycleLength; - - CalculateRenderItem(t, tStateScreen / _ula.ScanlineTime, tStateScreen % _ula.ScanlineTime); - } - - CreateContention(machineType); - } - - private void CalculateRenderItem(int item, int line, int pix) - { - Renderer[item] = new RenderCycle(); - - Renderer[item].RAction = RenderAction.None; - int pitchWidth = _ula.ScreenWidth + _ula.BorderRightWidth + _ula.BorderLeftWidth; - - int scrPix = pix - _ula.FirstPaperTState; - int scrLin = line - _ula.FirstPaperLine; - - if ((line >= (_ula.FirstPaperLine - _ula.BorderTopHeight)) && (line < (_ula.FirstPaperLine + 192 + _ula.BorderBottomHeight)) && - (pix >= (_ula.FirstPaperTState - _ula.BorderLeftTime)) && (pix < (_ula.FirstPaperTState + 128 + _ula.BorderRightTime))) - { - // visibleArea (vertical) - if ((line >= _ula.FirstPaperLine) && (line < (_ula.FirstPaperLine + 192)) && - (pix >= _ula.FirstPaperTState) && (pix < (_ula.FirstPaperTState + 128))) - { - // pixel area - switch (scrPix & 7) - { - case 0: - Renderer[item].RAction = RenderAction.Shift1AndFetchByte2; // shift 1 + fetch B2 - // +4 = prefetch! - Renderer[item].ByteAddress = CalculateByteAddress(scrPix + 4, scrLin); - break; - case 1: - Renderer[item].RAction = RenderAction.Shift1AndFetchAttribute2; // shift 1 + fetch A2 - // +3 = prefetch! - Renderer[item].AttributeAddress = CalculateAttributeAddress(scrPix + 3, scrLin); - break; - case 2: - Renderer[item].RAction = RenderAction.Shift1; // shift 1 - break; - case 3: - Renderer[item].RAction = RenderAction.Shift1Last; // shift 1 (last) - break; - case 4: - Renderer[item].RAction = RenderAction.Shift2; // shift 2 - break; - case 5: - Renderer[item].RAction = RenderAction.Shift2; // shift 2 - break; - case 6: - if (pix < (_ula.FirstPaperTState + 128 - 2)) - { - Renderer[item].RAction = RenderAction.Shift2AndFetchByte1; // shift 2 + fetch B2 - } - else - { - Renderer[item].RAction = RenderAction.Shift2; // shift 2 - } - - // +2 = prefetch! - Renderer[item].ByteAddress = CalculateByteAddress(scrPix + 2, scrLin); - break; - case 7: - if (pix < (_ula.FirstPaperTState + 128 - 2)) - { - //??? - Renderer[item].RAction = RenderAction.Shift2AndFetchAttribute1; // shift 2 + fetch A2 - } - else - { - Renderer[item].RAction = RenderAction.Shift2; // shift 2 - } - - // +1 = prefetch! - Renderer[item].AttributeAddress = CalculateAttributeAddress(scrPix + 1, scrLin); - break; - } - } - else if ((line >= _ula.FirstPaperLine) && (line < (_ula.FirstPaperLine + 192)) && - (pix == (_ula.FirstPaperTState - 2))) // border & fetch B1 - { - Renderer[item].RAction = RenderAction.BorderAndFetchByte1; // border & fetch B1 - // +2 = prefetch! - Renderer[item].ByteAddress = CalculateByteAddress(scrPix + 2, scrLin); - } - else if ((line >= _ula.FirstPaperLine) && (line < (_ula.FirstPaperLine + 192)) && - (pix == (_ula.FirstPaperTState - 1))) // border & fetch A1 - { - Renderer[item].RAction = RenderAction.BorderAndFetchAttribute1; // border & fetch A1 - // +1 = prefetch! - Renderer[item].AttributeAddress = CalculateAttributeAddress(scrPix + 1, scrLin); - } - else - { - Renderer[item].RAction = RenderAction.Border; // border - } - - int wy = line - (_ula.FirstPaperLine - _ula.BorderTopHeight); - int wx = (pix - (_ula.FirstPaperTState - _ula.BorderLeftTime)) * 2; - Renderer[item].LineOffset = wy * pitchWidth + wx; - } - } - - private void CreateContention(MachineType machineType) - { - int[] conPattern = new int[8]; - - switch (machineType) - { - case MachineType.ZXSpectrum16: - case MachineType.ZXSpectrum48: - case MachineType.ZXSpectrum128: - case MachineType.ZXSpectrum128Plus2: - conPattern = new int[] { 6, 5, 4, 3, 2, 1, 0, 0 }; - break; - - case MachineType.ZXSpectrum128Plus2a: - case MachineType.ZXSpectrum128Plus3: - conPattern = new int[] { 1, 0, 7, 6, 5, 4, 3, 2 }; - break; - } - - // calculate contention values - for (int t = 0; t < _ula.FrameCycleLength; t++) - { - int shifted = t + _ula.RenderTableOffset + _ula.ContentionOffset; // _ula.InterruptStartTime; - if (shifted < 0) - shifted += _ula.FrameCycleLength; - shifted %= _ula.FrameCycleLength; - - Renderer[t].ContentionValue = 0; - - int line = shifted / _ula.ScanlineTime; - int pix = shifted % _ula.ScanlineTime; - if (line < _ula.FirstPaperLine || line >= (_ula.FirstPaperLine + 192)) - { - Renderer[t].ContentionValue = 0; - continue; - } - int scrPix = pix - _ula.FirstPaperTState; - if (scrPix < 0 || scrPix >= 128) - { - Renderer[t].ContentionValue = 0; - continue; - } - int pixByte = scrPix % 8; - - Renderer[t].ContentionValue = conPattern[pixByte]; - } - } - - private ushort CalculateByteAddress(int x, int y) - { - x >>= 2; - var vp = x | (y << 5); - return (ushort)((vp & 0x181F) | ((vp & 0x0700) >> 3) | ((vp & 0x00E0) << 3)); - } - - private ushort CalculateAttributeAddress(int x, int y) - { - x >>= 2; - var ap = x | ((y >> 3) << 5); - return (ushort)(6144 + ap); - } - - /// - /// Render/contention information for a single T-State - /// - public class RenderCycle - { - /// - /// The ULA render action at this T-State - /// - public RenderAction RAction; - /// - /// The contention value at this T-State - /// - public int ContentionValue; - /// - /// The screen byte address at this T-State - /// - public ushort ByteAddress; - /// - /// The screen attribute address at this T-State - /// - public ushort AttributeAddress; - /// - /// The byte address returned by the floating bus at this T-State - /// - public ushort FloatingBusAddress; - /// - /// The offset - /// - public int LineOffset; - } - - public enum RenderAction - { - None, - Border, - BorderAndFetchByte1, - BorderAndFetchAttribute1, - Shift1AndFetchByte2, - Shift1AndFetchAttribute2, - Shift1, - Shift1Last, - Shift2, - Shift2Last, - Shift2AndFetchByte1, - Shift2AndFetchAttribute1 - } - } - - #endregion - - #region Render Methods - - /// - /// Renders to the screen buffer based on the current cycle - /// - public void RenderScreen(int toCycle) - { - // check boundaries - if (toCycle > FrameCycleLength) - toCycle = FrameCycleLength; - - // render the required number of cycles - for (int t = LastTState; t < toCycle; t++) - { - if (!Border4T || (t & 3) == Border4TStage) - { - fetchBorder = BorderColor; - } - else - { - - } - - //fetchBorder = BorderColor; - - // get the table entry - var item = RenderingTable.Renderer[t]; - - switch (item.RAction) - { - case RenderTable.RenderAction.None: - break; - - case RenderTable.RenderAction.Border: - ScreenBuffer[item.LineOffset] = ULAPalette[fetchBorder]; - ScreenBuffer[item.LineOffset + 1] = ULAPalette[fetchBorder]; - break; - - case RenderTable.RenderAction.BorderAndFetchByte1: - ScreenBuffer[item.LineOffset] = ULAPalette[fetchBorder]; - ScreenBuffer[item.LineOffset + 1] = ULAPalette[fetchBorder]; - fetchB1 = _machine.FetchScreenMemory(item.ByteAddress); - break; - - case RenderTable.RenderAction.BorderAndFetchAttribute1: - ScreenBuffer[item.LineOffset] = ULAPalette[fetchBorder]; - ScreenBuffer[item.LineOffset + 1] = ULAPalette[fetchBorder]; - fetchA1 = _machine.FetchScreenMemory(item.AttributeAddress); - ProcessInkPaper(fetchA1); - break; - - case RenderTable.RenderAction.Shift1AndFetchByte2: - ScreenBuffer[item.LineOffset] = ((fetchB1 & 0x80) != 0) ? palInk : palPaper; - ScreenBuffer[item.LineOffset + 1] = ((fetchB1 & 0x40) != 0) ? palInk : palPaper; - fetchB1 <<= 2; - fetchB2 = _machine.FetchScreenMemory(item.ByteAddress); - break; - - case RenderTable.RenderAction.Shift1AndFetchAttribute2: - ScreenBuffer[item.LineOffset] = ((fetchB1 & 0x80) != 0) ? palInk : palPaper; - ScreenBuffer[item.LineOffset + 1] = ((fetchB1 & 0x40) != 0) ? palInk : palPaper; - fetchB1 <<= 2; - fetchA2 = _machine.FetchScreenMemory(item.AttributeAddress); - break; - - case RenderTable.RenderAction.Shift1: - ScreenBuffer[item.LineOffset] = ((fetchB1 & 0x80) != 0) ? palInk : palPaper; - ScreenBuffer[item.LineOffset + 1] = ((fetchB1 & 0x40) != 0) ? palInk : palPaper; - fetchB1 <<= 2; - break; - - case RenderTable.RenderAction.Shift1Last: - ScreenBuffer[item.LineOffset] = ((fetchB1 & 0x80) != 0) ? palInk : palPaper; - ScreenBuffer[item.LineOffset + 1] = ((fetchB1 & 0x40) != 0) ? palInk : palPaper; - fetchB1 <<= 2; - ProcessInkPaper(fetchA2); - break; - - case RenderTable.RenderAction.Shift2: - ScreenBuffer[item.LineOffset] = ((fetchB2 & 0x80) != 0) ? palInk : palPaper; - ScreenBuffer[item.LineOffset + 1] = ((fetchB2 & 0x40) != 0) ? palInk : palPaper; - fetchB2 <<= 2; - break; - - case RenderTable.RenderAction.Shift2AndFetchByte1: - ScreenBuffer[item.LineOffset] = ((fetchB2 & 0x80) != 0) ? palInk : palPaper; - ScreenBuffer[item.LineOffset + 1] = ((fetchB2 & 0x40) != 0) ? palInk : palPaper; - fetchB2 <<= 2; - fetchB1 = _machine.FetchScreenMemory(item.ByteAddress); - break; - - case RenderTable.RenderAction.Shift2AndFetchAttribute1: - ScreenBuffer[item.LineOffset] = ((fetchB2 & 0x80) != 0) ? palInk : palPaper; - ScreenBuffer[item.LineOffset + 1] = ((fetchB2 & 0x40) != 0) ? palInk : palPaper; - fetchB2 <<= 2; - fetchA1 = _machine.FetchScreenMemory(item.AttributeAddress); - ProcessInkPaper(fetchA1); - break; - } - } - - LastTState = toCycle; - } - - private void ProcessInkPaper(byte attrData) - { - bright = (attrData & 0x40) >> 3; - flash = (attrData & 0x80) >> 7; - ink = (attrData & 0x07); - paper = ((attrData >> 3) & 0x7); - - palInk = ULAPalette[ink + bright]; - palPaper = ULAPalette[paper + bright]; - - // swap paper and ink when flash is on - if (flashOn && (flash != 0)) - { - int temp = palInk; - palInk = palPaper; - palPaper = temp; - } - } - - /// - /// Generates the port lookup table for +2a/+3 allowed floating bus ports - /// - public void GenerateP3PortTable() - { - List table = new List(); - for (int i = 0; i < 0x1000; i++) - { - ushort r = (ushort)(1 + (4 * i)); - if (r > 4093) - break; - table.Add(r); - } - - Plus3FBPortTable = table.ToArray(); - } - - private ushort[] Plus3FBPortTable = new ushort[1]; - - /// - /// Returns floating bus value (if available) - /// - public void ReadFloatingBus(int tstate, ref int result, ushort port) - { - tstate += FloatingBusOffset; - if (tstate >= RenderingTable.Renderer.Length) - tstate -= RenderingTable.Renderer.Length; - if (tstate < 0) - tstate += RenderingTable.Renderer.Length; - - var item = RenderingTable.Renderer[tstate]; - - switch (RenderingTable._machineType) - { - case MachineType.ZXSpectrum16: - case MachineType.ZXSpectrum48: - case MachineType.ZXSpectrum128: - case MachineType.ZXSpectrum128Plus2: - - switch (item.RAction) - { - case RenderTable.RenderAction.BorderAndFetchByte1: - case RenderTable.RenderAction.Shift1AndFetchByte2: - case RenderTable.RenderAction.Shift2AndFetchByte1: - result = _machine.FetchScreenMemory(item.ByteAddress); - break; - case RenderTable.RenderAction.BorderAndFetchAttribute1: - case RenderTable.RenderAction.Shift1AndFetchAttribute2: - case RenderTable.RenderAction.Shift2AndFetchAttribute1: - result = _machine.FetchScreenMemory(item.AttributeAddress); - break; - default: - break; - } - break; - - case MachineType.ZXSpectrum128Plus2a: - case MachineType.ZXSpectrum128Plus3: - - // http://sky.relative-path.com/zx/floating_bus.html - if (_machine.PagingDisabled) - { - result = 0xff; - break; - } - - // check whether fb is found on this port - ushort pLook = Array.Find(Plus3FBPortTable, s => s == port); - if (pLook == 0) - { - result = 0xff; - break; - } - - // floating bus on +2a/+3 always returns a byte with Bit0 set - switch (item.RAction) - { - case RenderTable.RenderAction.BorderAndFetchByte1: - case RenderTable.RenderAction.Shift1AndFetchByte2: - case RenderTable.RenderAction.Shift2AndFetchByte1: - result = (byte)(_machine.FetchScreenMemory(item.ByteAddress) | 0x01); - break; - case RenderTable.RenderAction.BorderAndFetchAttribute1: - case RenderTable.RenderAction.Shift1AndFetchAttribute2: - case RenderTable.RenderAction.Shift2AndFetchAttribute1: - result = (byte)(_machine.FetchScreenMemory(item.AttributeAddress) | 0x01); - break; - default: - result = (byte)(_machine.LastContendedReadByte | 0x01); - break; - } - - break; - } - } - - #endregion - - #region Contention - - /// - /// Returns the contention value for the current t-state - /// - public int GetContentionValue() - { - return GetContentionValue((int)_machine.CurrentFrameCycle); - } - - /// - /// Returns the contention value for the supplied t-state - /// - public int GetContentionValue(int tstate) - { - if (tstate >= FrameCycleLength) - tstate -= FrameCycleLength; - - if (tstate < 0) - tstate += FrameCycleLength; - - return RenderingTable.Renderer[tstate].ContentionValue; - } - - /// - /// Returns the contention value for the supplied t-state - /// - public int GetPortContentionValue(int tstate) - { - if (tstate >= FrameCycleLength) - tstate -= FrameCycleLength; - - if (tstate < 0) - tstate += FrameCycleLength; - - return RenderingTable.Renderer[tstate].ContentionValue; - } - - #endregion - - #region IVideoProvider - - /// - /// Video output buffer - /// - public int[] ScreenBuffer; - - private int _virtualWidth; - private int _virtualHeight; - private int _bufferWidth; - private int _bufferHeight; - - public int BackgroundColor - { - get - { - var settings = _machine.Spectrum.GetSettings(); - var color = settings.BackgroundColor; - if (!settings.UseCoreBorderForBackground) - return color; - else - return ULAPalette[fetchBorder]; - } - } - - public int VirtualWidth - { - get { return _virtualWidth; } - set { _virtualWidth = value; } - } - - public int VirtualHeight - { - get { return _virtualHeight; } - set { _virtualHeight = value; } - } - - public int BufferWidth - { - get { return _bufferWidth; } - set { _bufferWidth = value; } - } - - public int BufferHeight - { - get { return _bufferHeight; } - set { _bufferHeight = value; } - } - - public int VsyncNumerator - { - get { return ClockSpeed * 50; }// ClockSpeed; } - set { } - } - - public int VsyncDenominator - { - get { return ClockSpeed; }//FrameLength; } - } - - public int[] GetVideoBuffer() - { - switch (borderType) - { - // Full side borders, no top or bottom border (giving *almost* 16:9 output) - case ZXSpectrum.BorderType.Widescreen: - // we are cropping out the top and bottom borders - var startPixelsToCrop = ScanLineWidth * BorderTopHeight; - var endPixelsToCrop = ScanLineWidth * BorderBottomHeight; - int index = 0; - for (int i = startPixelsToCrop; i < ScreenBuffer.Length - endPixelsToCrop; i++) - { - croppedBuffer[index++] = ScreenBuffer[i]; - } - return croppedBuffer; - - // The full spectrum border - case ZXSpectrum.BorderType.Full: - return ScreenBuffer; - - case ZXSpectrum.BorderType.Medium: - // all border sizes now 24 - var lR = BorderLeftWidth - 24; - var rR = BorderRightWidth - 24; - var tR = BorderTopHeight - 24; - var bR = BorderBottomHeight - 24; - var startP = ScanLineWidth * tR; - var endP = ScanLineWidth * bR; - - int index2 = 0; - // line by line - for (int i = startP; i < ScreenBuffer.Length - endP; i += ScreenWidth + BorderLeftWidth + BorderRightWidth) - { - // each pixel in each line - for (int p = lR; p < ScreenWidth + BorderLeftWidth + BorderRightWidth - rR; p++) - { - if (index2 == croppedBuffer.Length) - break; - croppedBuffer[index2++] = ScreenBuffer[i + p]; - } - } - - return croppedBuffer; - - case ZXSpectrum.BorderType.Small: - // all border sizes now 24 - var lR_ = BorderLeftWidth - 10; - var rR_ = BorderRightWidth - 10; - var tR_ = BorderTopHeight - 10; - var bR_ = BorderBottomHeight - 10; - var startP_ = ScanLineWidth * tR_; - var endP_ = ScanLineWidth * bR_; - - int index2_ = 0; - // line by line - for (int i = startP_; i < ScreenBuffer.Length - endP_; i += ScreenWidth + BorderLeftWidth + BorderRightWidth) - { - // each pixel in each line - for (int p = lR_; p < ScreenWidth + BorderLeftWidth + BorderRightWidth - rR_; p++) - { - if (index2_ == croppedBuffer.Length) - break; - croppedBuffer[index2_++] = ScreenBuffer[i + p]; - } - } - - return croppedBuffer; - - case ZXSpectrum.BorderType.None: - // all border sizes now 0 - var lR__ = BorderLeftWidth; - var rR__ = BorderRightWidth; - var tR__ = BorderTopHeight; - var bR__ = BorderBottomHeight; - var startP__ = ScanLineWidth * tR__; - var endP__ = ScanLineWidth * bR__; - - int index2__ = 0; - // line by line - for (int i = startP__; i < ScreenBuffer.Length - endP__; i += ScreenWidth + BorderLeftWidth + BorderRightWidth) - { - // each pixel in each line - for (int p = lR__; p < ScreenWidth + BorderLeftWidth + BorderRightWidth - rR__; p++) - { - if (index2__ == croppedBuffer.Length) - break; - croppedBuffer[index2__++] = ScreenBuffer[i + p]; - } - } - - return croppedBuffer; - } - - return ScreenBuffer; - } - - protected void SetupScreenSize() - { - BufferWidth = ScreenWidth + BorderLeftWidth + BorderRightWidth; - BufferHeight = ScreenHeight + BorderTopHeight + BorderBottomHeight; - VirtualHeight = BufferHeight; - VirtualWidth = BufferWidth; - ScreenBuffer = new int[BufferWidth * BufferHeight]; - - switch (borderType) - { - case ZXSpectrum.BorderType.Full: - BufferWidth = ScreenWidth + BorderLeftWidth + BorderRightWidth; - BufferHeight = ScreenHeight + BorderTopHeight + BorderBottomHeight; - VirtualHeight = BufferHeight; - VirtualWidth = BufferWidth; - ScreenBuffer = new int[BufferWidth * BufferHeight]; - break; - - case ZXSpectrum.BorderType.Widescreen: - BufferWidth = ScreenWidth + BorderLeftWidth + BorderRightWidth; - BufferHeight = ScreenHeight; - VirtualHeight = BufferHeight; - VirtualWidth = BufferWidth; - croppedBuffer = new int[BufferWidth * BufferHeight]; - break; - - case ZXSpectrum.BorderType.Medium: - BufferWidth = ScreenWidth + (24) + (24); - BufferHeight = ScreenHeight + (24) + (24); - VirtualHeight = BufferHeight; - VirtualWidth = BufferWidth; - croppedBuffer = new int[BufferWidth * BufferHeight]; - break; - - case ZXSpectrum.BorderType.Small: - BufferWidth = ScreenWidth + (10) + (10); - BufferHeight = ScreenHeight + (10) + (10); - VirtualHeight = BufferHeight; - VirtualWidth = BufferWidth; - croppedBuffer = new int[BufferWidth * BufferHeight]; - break; - - case ZXSpectrum.BorderType.None: - BufferWidth = ScreenWidth; - BufferHeight = ScreenHeight; - VirtualHeight = BufferHeight; - VirtualWidth = BufferWidth; - croppedBuffer = new int[BufferWidth * BufferHeight]; - break; - } - } - - protected int[] croppedBuffer; - - private ZXSpectrum.BorderType _borderType; - - public ZXSpectrum.BorderType borderType - { - get { return _borderType; } - set { _borderType = value; } - } - - #endregion - - #region Serialization - - public void SyncState(Serializer ser) - { - ser.BeginSection(nameof(ULA)); - if (ScreenBuffer != null) - ser.Sync(nameof(ScreenBuffer), ref ScreenBuffer, false); - ser.Sync(nameof(BorderColor), ref BorderColor); - ser.Sync(nameof(LastTState), ref LastTState); - ser.Sync(nameof(flashOn), ref flashOn); - ser.Sync(nameof(fetchB1), ref fetchB1); - ser.Sync(nameof(fetchA1), ref fetchA1); - ser.Sync(nameof(fetchB2), ref fetchB2); - ser.Sync(nameof(fetchA2), ref fetchA2); - ser.Sync(nameof(ink), ref ink); - ser.Sync(nameof(paper), ref paper); - ser.Sync(nameof(fetchBorder), ref fetchBorder); - ser.Sync(nameof(bright), ref bright); - ser.Sync(nameof(flash), ref flash); - ser.Sync(nameof(palPaper), ref palPaper); - ser.Sync(nameof(palInk), ref palInk); - - ser.Sync(nameof(LastULATick), ref LastULATick); - ser.Sync(nameof(ULACycleCounter), ref ULACycleCounter); - ser.Sync(nameof(FrameEnd), ref FrameEnd); - - ser.Sync(nameof(InterruptRaised), ref InterruptRaised); - ser.EndSection(); - } - - #endregion - } + } + else + { + // its time (or past time) to stop holding the /INT pin low + _machine.CPU.FlagI = false; + InterruptRaised = false; + } + } + else + { + // interrupt is currently not raised + if (ULACycleCounter == FrameLength + InterruptStartTime) + { + // time to raise the interrupt + InterruptRaised = true; + _machine.CPU.FlagI = true; + FrameEnd = true; + ULACycleCounter = InterruptStartTime; + CalcFlashCounter(); + } + } + } + } + + /// + /// Flash processing + /// + public void CalcFlashCounter() + { + flashCounter++; + + if (flashCounter > 15) + { + flashOn = !flashOn; + flashCounter = 0; + } + } + + #endregion + + #region Screen Layout + + /// + /// Total pixels in one display row + /// + protected int ScreenWidth; + /// + /// Total pixels in one display column + /// + protected int ScreenHeight; + /// + /// Total pixels in top border + /// + protected int BorderTopHeight; + /// + /// Total pixels in bottom border + /// + protected int BorderBottomHeight; + /// + /// Total pixels in left border width + /// + protected int BorderLeftWidth; + /// + /// Total pixels in right border width + /// + protected int BorderRightWidth; + /// + /// Total pixels in one scanline + /// + protected int ScanLineWidth; + + #endregion + + #region State + + /// + /// The last T-State cycle at which the screen was rendered + /// + public int LastTState; + + /// + /// Flash state + /// + public bool flashOn; + + private int flashCounter; + + protected byte fetchB1; + protected byte fetchA1; + protected byte fetchB2; + protected byte fetchA2; + protected int ink; + protected int paper; + protected int fetchBorder; + protected int bright; + protected int flash; + + public int palPaper; + public int palInk; + + public int BorderColor = 7; + + #endregion + + #region Conversions + + public int FrameLength => FrameCycleLength; + + #endregion + + #region Rendering Configuration + + /// + /// Holds all information regarding rendering the screen based on the current T-State + /// + public RenderTable RenderingTable; + + /// + /// Holds all information regarding rendering the screen based on the current T-State + /// + public class RenderTable + { + /// + /// The ULA device + /// + private ULA _ula; + + /// + /// Array of rendercycle entries + /// Starting from the interrupt + /// + public RenderCycle[] Renderer; + + /// + /// The emulated machine + /// + public MachineType _machineType; + + public int Offset; + + /// + /// Constructor + /// + public RenderTable(ULA ula, MachineType machineType) + { + _ula = ula; + _machineType = machineType; + Renderer = new RenderCycle[_ula.FrameCycleLength]; + InitRenderer(machineType); + } + + /// + /// Initializes the renderer + /// + private void InitRenderer(MachineType machineType) + { + for (var t = 0; t < _ula.FrameCycleLength; t++) + { + var tStateScreen = t + _ula.RenderTableOffset;// + _ula.InterruptStartTime; + + if (tStateScreen < 0) + tStateScreen += _ula.FrameCycleLength; + else if (tStateScreen >= _ula.FrameCycleLength) + tStateScreen -= _ula.FrameCycleLength; + + CalculateRenderItem(t, tStateScreen / _ula.ScanlineTime, tStateScreen % _ula.ScanlineTime); + } + + CreateContention(machineType); + } + + private void CalculateRenderItem(int item, int line, int pix) + { + Renderer[item] = new RenderCycle(); + + Renderer[item].RAction = RenderAction.None; + int pitchWidth = _ula.ScreenWidth + _ula.BorderRightWidth + _ula.BorderLeftWidth; + + int scrPix = pix - _ula.FirstPaperTState; + int scrLin = line - _ula.FirstPaperLine; + + if ((line >= (_ula.FirstPaperLine - _ula.BorderTopHeight)) && (line < (_ula.FirstPaperLine + 192 + _ula.BorderBottomHeight)) && + (pix >= (_ula.FirstPaperTState - _ula.BorderLeftTime)) && (pix < (_ula.FirstPaperTState + 128 + _ula.BorderRightTime))) + { + // visibleArea (vertical) + if ((line >= _ula.FirstPaperLine) && (line < (_ula.FirstPaperLine + 192)) && + (pix >= _ula.FirstPaperTState) && (pix < (_ula.FirstPaperTState + 128))) + { + // pixel area + switch (scrPix & 7) + { + case 0: + Renderer[item].RAction = RenderAction.Shift1AndFetchByte2; // shift 1 + fetch B2 + // +4 = prefetch! + Renderer[item].ByteAddress = CalculateByteAddress(scrPix + 4, scrLin); + break; + case 1: + Renderer[item].RAction = RenderAction.Shift1AndFetchAttribute2; // shift 1 + fetch A2 + // +3 = prefetch! + Renderer[item].AttributeAddress = CalculateAttributeAddress(scrPix + 3, scrLin); + break; + case 2: + Renderer[item].RAction = RenderAction.Shift1; // shift 1 + break; + case 3: + Renderer[item].RAction = RenderAction.Shift1Last; // shift 1 (last) + break; + case 4: + Renderer[item].RAction = RenderAction.Shift2; // shift 2 + break; + case 5: + Renderer[item].RAction = RenderAction.Shift2; // shift 2 + break; + case 6: + if (pix < (_ula.FirstPaperTState + 128 - 2)) + { + Renderer[item].RAction = RenderAction.Shift2AndFetchByte1; // shift 2 + fetch B2 + } + else + { + Renderer[item].RAction = RenderAction.Shift2; // shift 2 + } + + // +2 = prefetch! + Renderer[item].ByteAddress = CalculateByteAddress(scrPix + 2, scrLin); + break; + case 7: + if (pix < (_ula.FirstPaperTState + 128 - 2)) + { + //??? + Renderer[item].RAction = RenderAction.Shift2AndFetchAttribute1; // shift 2 + fetch A2 + } + else + { + Renderer[item].RAction = RenderAction.Shift2; // shift 2 + } + + // +1 = prefetch! + Renderer[item].AttributeAddress = CalculateAttributeAddress(scrPix + 1, scrLin); + break; + } + } + else if ((line >= _ula.FirstPaperLine) && (line < (_ula.FirstPaperLine + 192)) && + (pix == (_ula.FirstPaperTState - 2))) // border & fetch B1 + { + Renderer[item].RAction = RenderAction.BorderAndFetchByte1; // border & fetch B1 + // +2 = prefetch! + Renderer[item].ByteAddress = CalculateByteAddress(scrPix + 2, scrLin); + } + else if ((line >= _ula.FirstPaperLine) && (line < (_ula.FirstPaperLine + 192)) && + (pix == (_ula.FirstPaperTState - 1))) // border & fetch A1 + { + Renderer[item].RAction = RenderAction.BorderAndFetchAttribute1; // border & fetch A1 + // +1 = prefetch! + Renderer[item].AttributeAddress = CalculateAttributeAddress(scrPix + 1, scrLin); + } + else + { + Renderer[item].RAction = RenderAction.Border; // border + } + + int wy = line - (_ula.FirstPaperLine - _ula.BorderTopHeight); + int wx = (pix - (_ula.FirstPaperTState - _ula.BorderLeftTime)) * 2; + Renderer[item].LineOffset = wy * pitchWidth + wx; + } + } + + private void CreateContention(MachineType machineType) + { + int[] conPattern = new int[8]; + + switch (machineType) + { + case MachineType.ZXSpectrum16: + case MachineType.ZXSpectrum48: + case MachineType.ZXSpectrum128: + case MachineType.ZXSpectrum128Plus2: + conPattern = new int[] { 6, 5, 4, 3, 2, 1, 0, 0 }; + break; + + case MachineType.ZXSpectrum128Plus2a: + case MachineType.ZXSpectrum128Plus3: + conPattern = new int[] { 1, 0, 7, 6, 5, 4, 3, 2 }; + break; + } + + // calculate contention values + for (int t = 0; t < _ula.FrameCycleLength; t++) + { + int shifted = t + _ula.RenderTableOffset + _ula.ContentionOffset; // _ula.InterruptStartTime; + if (shifted < 0) + shifted += _ula.FrameCycleLength; + shifted %= _ula.FrameCycleLength; + + Renderer[t].ContentionValue = 0; + + int line = shifted / _ula.ScanlineTime; + int pix = shifted % _ula.ScanlineTime; + if (line < _ula.FirstPaperLine || line >= (_ula.FirstPaperLine + 192)) + { + Renderer[t].ContentionValue = 0; + continue; + } + int scrPix = pix - _ula.FirstPaperTState; + if (scrPix < 0 || scrPix >= 128) + { + Renderer[t].ContentionValue = 0; + continue; + } + int pixByte = scrPix % 8; + + Renderer[t].ContentionValue = conPattern[pixByte]; + } + } + + private ushort CalculateByteAddress(int x, int y) + { + x >>= 2; + var vp = x | (y << 5); + return (ushort)((vp & 0x181F) | ((vp & 0x0700) >> 3) | ((vp & 0x00E0) << 3)); + } + + private ushort CalculateAttributeAddress(int x, int y) + { + x >>= 2; + var ap = x | ((y >> 3) << 5); + return (ushort)(6144 + ap); + } + + /// + /// Render/contention information for a single T-State + /// + public class RenderCycle + { + /// + /// The ULA render action at this T-State + /// + public RenderAction RAction; + /// + /// The contention value at this T-State + /// + public int ContentionValue; + /// + /// The screen byte address at this T-State + /// + public ushort ByteAddress; + /// + /// The screen attribute address at this T-State + /// + public ushort AttributeAddress; + /// + /// The byte address returned by the floating bus at this T-State + /// + public ushort FloatingBusAddress; + /// + /// The offset + /// + public int LineOffset; + } + + public enum RenderAction + { + None, + Border, + BorderAndFetchByte1, + BorderAndFetchAttribute1, + Shift1AndFetchByte2, + Shift1AndFetchAttribute2, + Shift1, + Shift1Last, + Shift2, + Shift2Last, + Shift2AndFetchByte1, + Shift2AndFetchAttribute1 + } + } + + #endregion + + #region Render Methods + + /// + /// Renders to the screen buffer based on the current cycle + /// + public void RenderScreen(int toCycle) + { + // check boundaries + if (toCycle > FrameCycleLength) + toCycle = FrameCycleLength; + + // render the required number of cycles + for (int t = LastTState; t < toCycle; t++) + { + if (!Border4T || (t & 3) == Border4TStage) + { + fetchBorder = BorderColor; + } + else + { + + } + + //fetchBorder = BorderColor; + + // get the table entry + var item = RenderingTable.Renderer[t]; + + switch (item.RAction) + { + case RenderTable.RenderAction.None: + break; + + case RenderTable.RenderAction.Border: + ScreenBuffer[item.LineOffset] = ULAPalette[fetchBorder]; + ScreenBuffer[item.LineOffset + 1] = ULAPalette[fetchBorder]; + break; + + case RenderTable.RenderAction.BorderAndFetchByte1: + ScreenBuffer[item.LineOffset] = ULAPalette[fetchBorder]; + ScreenBuffer[item.LineOffset + 1] = ULAPalette[fetchBorder]; + fetchB1 = _machine.FetchScreenMemory(item.ByteAddress); + break; + + case RenderTable.RenderAction.BorderAndFetchAttribute1: + ScreenBuffer[item.LineOffset] = ULAPalette[fetchBorder]; + ScreenBuffer[item.LineOffset + 1] = ULAPalette[fetchBorder]; + fetchA1 = _machine.FetchScreenMemory(item.AttributeAddress); + ProcessInkPaper(fetchA1); + break; + + case RenderTable.RenderAction.Shift1AndFetchByte2: + ScreenBuffer[item.LineOffset] = ((fetchB1 & 0x80) != 0) ? palInk : palPaper; + ScreenBuffer[item.LineOffset + 1] = ((fetchB1 & 0x40) != 0) ? palInk : palPaper; + fetchB1 <<= 2; + fetchB2 = _machine.FetchScreenMemory(item.ByteAddress); + break; + + case RenderTable.RenderAction.Shift1AndFetchAttribute2: + ScreenBuffer[item.LineOffset] = ((fetchB1 & 0x80) != 0) ? palInk : palPaper; + ScreenBuffer[item.LineOffset + 1] = ((fetchB1 & 0x40) != 0) ? palInk : palPaper; + fetchB1 <<= 2; + fetchA2 = _machine.FetchScreenMemory(item.AttributeAddress); + break; + + case RenderTable.RenderAction.Shift1: + ScreenBuffer[item.LineOffset] = ((fetchB1 & 0x80) != 0) ? palInk : palPaper; + ScreenBuffer[item.LineOffset + 1] = ((fetchB1 & 0x40) != 0) ? palInk : palPaper; + fetchB1 <<= 2; + break; + + case RenderTable.RenderAction.Shift1Last: + ScreenBuffer[item.LineOffset] = ((fetchB1 & 0x80) != 0) ? palInk : palPaper; + ScreenBuffer[item.LineOffset + 1] = ((fetchB1 & 0x40) != 0) ? palInk : palPaper; + fetchB1 <<= 2; + ProcessInkPaper(fetchA2); + break; + + case RenderTable.RenderAction.Shift2: + ScreenBuffer[item.LineOffset] = ((fetchB2 & 0x80) != 0) ? palInk : palPaper; + ScreenBuffer[item.LineOffset + 1] = ((fetchB2 & 0x40) != 0) ? palInk : palPaper; + fetchB2 <<= 2; + break; + + case RenderTable.RenderAction.Shift2AndFetchByte1: + ScreenBuffer[item.LineOffset] = ((fetchB2 & 0x80) != 0) ? palInk : palPaper; + ScreenBuffer[item.LineOffset + 1] = ((fetchB2 & 0x40) != 0) ? palInk : palPaper; + fetchB2 <<= 2; + fetchB1 = _machine.FetchScreenMemory(item.ByteAddress); + break; + + case RenderTable.RenderAction.Shift2AndFetchAttribute1: + ScreenBuffer[item.LineOffset] = ((fetchB2 & 0x80) != 0) ? palInk : palPaper; + ScreenBuffer[item.LineOffset + 1] = ((fetchB2 & 0x40) != 0) ? palInk : palPaper; + fetchB2 <<= 2; + fetchA1 = _machine.FetchScreenMemory(item.AttributeAddress); + ProcessInkPaper(fetchA1); + break; + } + } + + LastTState = toCycle; + } + + private void ProcessInkPaper(byte attrData) + { + bright = (attrData & 0x40) >> 3; + flash = (attrData & 0x80) >> 7; + ink = (attrData & 0x07); + paper = ((attrData >> 3) & 0x7); + + palInk = ULAPalette[ink + bright]; + palPaper = ULAPalette[paper + bright]; + + // swap paper and ink when flash is on + if (flashOn && (flash != 0)) + { + int temp = palInk; + palInk = palPaper; + palPaper = temp; + } + } + + /// + /// Generates the port lookup table for +2a/+3 allowed floating bus ports + /// + public void GenerateP3PortTable() + { + List table = new List(); + for (int i = 0; i < 0x1000; i++) + { + ushort r = (ushort)(1 + (4 * i)); + if (r > 4093) + break; + table.Add(r); + } + + Plus3FBPortTable = table.ToArray(); + } + + private ushort[] Plus3FBPortTable = new ushort[1]; + + /// + /// Returns floating bus value (if available) + /// + public void ReadFloatingBus(int tstate, ref int result, ushort port) + { + tstate += FloatingBusOffset; + if (tstate >= RenderingTable.Renderer.Length) + tstate -= RenderingTable.Renderer.Length; + if (tstate < 0) + tstate += RenderingTable.Renderer.Length; + + var item = RenderingTable.Renderer[tstate]; + + switch (RenderingTable._machineType) + { + case MachineType.ZXSpectrum16: + case MachineType.ZXSpectrum48: + case MachineType.ZXSpectrum128: + case MachineType.ZXSpectrum128Plus2: + + switch (item.RAction) + { + case RenderTable.RenderAction.BorderAndFetchByte1: + case RenderTable.RenderAction.Shift1AndFetchByte2: + case RenderTable.RenderAction.Shift2AndFetchByte1: + result = _machine.FetchScreenMemory(item.ByteAddress); + break; + case RenderTable.RenderAction.BorderAndFetchAttribute1: + case RenderTable.RenderAction.Shift1AndFetchAttribute2: + case RenderTable.RenderAction.Shift2AndFetchAttribute1: + result = _machine.FetchScreenMemory(item.AttributeAddress); + break; + default: + break; + } + break; + + case MachineType.ZXSpectrum128Plus2a: + case MachineType.ZXSpectrum128Plus3: + + // http://sky.relative-path.com/zx/floating_bus.html + if (_machine.PagingDisabled) + { + result = 0xff; + break; + } + + // check whether fb is found on this port + ushort pLook = Array.Find(Plus3FBPortTable, s => s == port); + if (pLook == 0) + { + result = 0xff; + break; + } + + // floating bus on +2a/+3 always returns a byte with Bit0 set + switch (item.RAction) + { + case RenderTable.RenderAction.BorderAndFetchByte1: + case RenderTable.RenderAction.Shift1AndFetchByte2: + case RenderTable.RenderAction.Shift2AndFetchByte1: + result = (byte)(_machine.FetchScreenMemory(item.ByteAddress) | 0x01); + break; + case RenderTable.RenderAction.BorderAndFetchAttribute1: + case RenderTable.RenderAction.Shift1AndFetchAttribute2: + case RenderTable.RenderAction.Shift2AndFetchAttribute1: + result = (byte)(_machine.FetchScreenMemory(item.AttributeAddress) | 0x01); + break; + default: + result = (byte)(_machine.LastContendedReadByte | 0x01); + break; + } + + break; + } + } + + #endregion + + #region Contention + + /// + /// Returns the contention value for the current t-state + /// + public int GetContentionValue() + { + return GetContentionValue((int)_machine.CurrentFrameCycle); + } + + /// + /// Returns the contention value for the supplied t-state + /// + public int GetContentionValue(int tstate) + { + if (tstate >= FrameCycleLength) + tstate -= FrameCycleLength; + + if (tstate < 0) + tstate += FrameCycleLength; + + return RenderingTable.Renderer[tstate].ContentionValue; + } + + /// + /// Returns the contention value for the supplied t-state + /// + public int GetPortContentionValue(int tstate) + { + if (tstate >= FrameCycleLength) + tstate -= FrameCycleLength; + + if (tstate < 0) + tstate += FrameCycleLength; + + return RenderingTable.Renderer[tstate].ContentionValue; + } + + #endregion + + #region IVideoProvider + + /// + /// Video output buffer + /// + public int[] ScreenBuffer; + + private int _virtualWidth; + private int _virtualHeight; + private int _bufferWidth; + private int _bufferHeight; + + public int BackgroundColor + { + get + { + var settings = _machine.Spectrum.GetSettings(); + var color = settings.BackgroundColor; + if (!settings.UseCoreBorderForBackground) + return color; + else + return ULAPalette[fetchBorder]; + } + } + + public int VirtualWidth + { + get { return _virtualWidth; } + set { _virtualWidth = value; } + } + + public int VirtualHeight + { + get { return _virtualHeight; } + set { _virtualHeight = value; } + } + + public int BufferWidth + { + get { return _bufferWidth; } + set { _bufferWidth = value; } + } + + public int BufferHeight + { + get { return _bufferHeight; } + set { _bufferHeight = value; } + } + + public int VsyncNumerator + { + get { return ClockSpeed * 50; }// ClockSpeed; } + set { } + } + + public int VsyncDenominator + { + get { return ClockSpeed; }//FrameLength; } + } + + public int[] GetVideoBuffer() + { + switch (borderType) + { + // Full side borders, no top or bottom border (giving *almost* 16:9 output) + case ZXSpectrum.BorderType.Widescreen: + // we are cropping out the top and bottom borders + var startPixelsToCrop = ScanLineWidth * BorderTopHeight; + var endPixelsToCrop = ScanLineWidth * BorderBottomHeight; + int index = 0; + for (int i = startPixelsToCrop; i < ScreenBuffer.Length - endPixelsToCrop; i++) + { + croppedBuffer[index++] = ScreenBuffer[i]; + } + return croppedBuffer; + + // The full spectrum border + case ZXSpectrum.BorderType.Full: + return ScreenBuffer; + + case ZXSpectrum.BorderType.Medium: + // all border sizes now 24 + var lR = BorderLeftWidth - 24; + var rR = BorderRightWidth - 24; + var tR = BorderTopHeight - 24; + var bR = BorderBottomHeight - 24; + var startP = ScanLineWidth * tR; + var endP = ScanLineWidth * bR; + + int index2 = 0; + // line by line + for (int i = startP; i < ScreenBuffer.Length - endP; i += ScreenWidth + BorderLeftWidth + BorderRightWidth) + { + // each pixel in each line + for (int p = lR; p < ScreenWidth + BorderLeftWidth + BorderRightWidth - rR; p++) + { + if (index2 == croppedBuffer.Length) + break; + croppedBuffer[index2++] = ScreenBuffer[i + p]; + } + } + + return croppedBuffer; + + case ZXSpectrum.BorderType.Small: + // all border sizes now 24 + var lR_ = BorderLeftWidth - 10; + var rR_ = BorderRightWidth - 10; + var tR_ = BorderTopHeight - 10; + var bR_ = BorderBottomHeight - 10; + var startP_ = ScanLineWidth * tR_; + var endP_ = ScanLineWidth * bR_; + + int index2_ = 0; + // line by line + for (int i = startP_; i < ScreenBuffer.Length - endP_; i += ScreenWidth + BorderLeftWidth + BorderRightWidth) + { + // each pixel in each line + for (int p = lR_; p < ScreenWidth + BorderLeftWidth + BorderRightWidth - rR_; p++) + { + if (index2_ == croppedBuffer.Length) + break; + croppedBuffer[index2_++] = ScreenBuffer[i + p]; + } + } + + return croppedBuffer; + + case ZXSpectrum.BorderType.None: + // all border sizes now 0 + var lR__ = BorderLeftWidth; + var rR__ = BorderRightWidth; + var tR__ = BorderTopHeight; + var bR__ = BorderBottomHeight; + var startP__ = ScanLineWidth * tR__; + var endP__ = ScanLineWidth * bR__; + + int index2__ = 0; + // line by line + for (int i = startP__; i < ScreenBuffer.Length - endP__; i += ScreenWidth + BorderLeftWidth + BorderRightWidth) + { + // each pixel in each line + for (int p = lR__; p < ScreenWidth + BorderLeftWidth + BorderRightWidth - rR__; p++) + { + if (index2__ == croppedBuffer.Length) + break; + croppedBuffer[index2__++] = ScreenBuffer[i + p]; + } + } + + return croppedBuffer; + } + + return ScreenBuffer; + } + + protected void SetupScreenSize() + { + BufferWidth = ScreenWidth + BorderLeftWidth + BorderRightWidth; + BufferHeight = ScreenHeight + BorderTopHeight + BorderBottomHeight; + VirtualHeight = BufferHeight; + VirtualWidth = BufferWidth; + ScreenBuffer = new int[BufferWidth * BufferHeight]; + + switch (borderType) + { + case ZXSpectrum.BorderType.Full: + BufferWidth = ScreenWidth + BorderLeftWidth + BorderRightWidth; + BufferHeight = ScreenHeight + BorderTopHeight + BorderBottomHeight; + VirtualHeight = BufferHeight; + VirtualWidth = BufferWidth; + ScreenBuffer = new int[BufferWidth * BufferHeight]; + break; + + case ZXSpectrum.BorderType.Widescreen: + BufferWidth = ScreenWidth + BorderLeftWidth + BorderRightWidth; + BufferHeight = ScreenHeight; + VirtualHeight = BufferHeight; + VirtualWidth = BufferWidth; + croppedBuffer = new int[BufferWidth * BufferHeight]; + break; + + case ZXSpectrum.BorderType.Medium: + BufferWidth = ScreenWidth + (24) + (24); + BufferHeight = ScreenHeight + (24) + (24); + VirtualHeight = BufferHeight; + VirtualWidth = BufferWidth; + croppedBuffer = new int[BufferWidth * BufferHeight]; + break; + + case ZXSpectrum.BorderType.Small: + BufferWidth = ScreenWidth + (10) + (10); + BufferHeight = ScreenHeight + (10) + (10); + VirtualHeight = BufferHeight; + VirtualWidth = BufferWidth; + croppedBuffer = new int[BufferWidth * BufferHeight]; + break; + + case ZXSpectrum.BorderType.None: + BufferWidth = ScreenWidth; + BufferHeight = ScreenHeight; + VirtualHeight = BufferHeight; + VirtualWidth = BufferWidth; + croppedBuffer = new int[BufferWidth * BufferHeight]; + break; + } + } + + protected int[] croppedBuffer; + + private ZXSpectrum.BorderType _borderType; + + public ZXSpectrum.BorderType borderType + { + get { return _borderType; } + set { _borderType = value; } + } + + #endregion + + #region Serialization + + public void SyncState(Serializer ser) + { + ser.BeginSection(nameof(ULA)); + if (ScreenBuffer != null) + ser.Sync(nameof(ScreenBuffer), ref ScreenBuffer, false); + ser.Sync(nameof(BorderColor), ref BorderColor); + ser.Sync(nameof(LastTState), ref LastTState); + ser.Sync(nameof(flashOn), ref flashOn); + ser.Sync(nameof(fetchB1), ref fetchB1); + ser.Sync(nameof(fetchA1), ref fetchA1); + ser.Sync(nameof(fetchB2), ref fetchB2); + ser.Sync(nameof(fetchA2), ref fetchA2); + ser.Sync(nameof(ink), ref ink); + ser.Sync(nameof(paper), ref paper); + ser.Sync(nameof(fetchBorder), ref fetchBorder); + ser.Sync(nameof(bright), ref bright); + ser.Sync(nameof(flash), ref flash); + ser.Sync(nameof(palPaper), ref palPaper); + ser.Sync(nameof(palInk), ref palInk); + + ser.Sync(nameof(LastULATick), ref LastULATick); + ser.Sync(nameof(ULACycleCounter), ref ULACycleCounter); + ser.Sync(nameof(FrameEnd), ref FrameEnd); + + ser.Sync(nameof(InterruptRaised), ref InterruptRaised); + ser.EndSection(); + } + + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum128K/ZX128.Screen.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum128K/ZX128.Screen.cs index 2c7d1a722b..cbb98eec3f 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum128K/ZX128.Screen.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum128K/ZX128.Screen.cs @@ -1,48 +1,48 @@  namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// 128K/+2 ULA - /// - class Screen128 : ULA - { - #region Construction + /// + /// 128K/+2 ULA + /// + class Screen128 : ULA + { + #region Construction - public Screen128(SpectrumBase machine) + public Screen128(SpectrumBase machine) : base(machine) - { - // interrupt - InterruptStartTime = 3; - InterruptLength = 36; - // offsets - RenderTableOffset = 58; - ContentionOffset = 6; - FloatingBusOffset = 1; - // timing - ClockSpeed = 3546900; - FrameCycleLength = 70908; - ScanlineTime = 228; - BorderLeftTime = 24; - BorderRightTime = 24; - FirstPaperLine = 63; - FirstPaperTState = 64; - // screen layout - Border4T = true; - Border4TStage = 2; - ScreenWidth = 256; - ScreenHeight = 192; - BorderTopHeight = 48; // 55; // 48; - BorderBottomHeight = 48; // 56; - BorderLeftWidth = 48; - BorderRightWidth = 48; - ScanLineWidth = BorderLeftWidth + ScreenWidth + BorderRightWidth; + { + // interrupt + InterruptStartTime = 3; + InterruptLength = 36; + // offsets + RenderTableOffset = 58; + ContentionOffset = 6; + FloatingBusOffset = 1; + // timing + ClockSpeed = 3546900; + FrameCycleLength = 70908; + ScanlineTime = 228; + BorderLeftTime = 24; + BorderRightTime = 24; + FirstPaperLine = 63; + FirstPaperTState = 64; + // screen layout + Border4T = true; + Border4TStage = 2; + ScreenWidth = 256; + ScreenHeight = 192; + BorderTopHeight = 48; // 55; // 48; + BorderBottomHeight = 48; // 56; + BorderLeftWidth = 48; + BorderRightWidth = 48; + ScanLineWidth = BorderLeftWidth + ScreenWidth + BorderRightWidth; - RenderingTable = new RenderTable(this, - MachineType.ZXSpectrum128); + RenderingTable = new RenderTable(this, + MachineType.ZXSpectrum128); - SetupScreenSize(); - } + SetupScreenSize(); + } - #endregion - } + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum128K/ZX128.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum128K/ZX128.cs index 5388165142..28859c3fa6 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum128K/ZX128.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum128K/ZX128.cs @@ -4,48 +4,48 @@ using BizHawk.Emulation.Cores.Sound; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// 128K Constructor - /// - public partial class ZX128 : SpectrumBase - { - #region Construction + /// + /// 128K Constructor + /// + public partial class ZX128 : SpectrumBase + { + #region Construction - /// - /// Main constructor - /// - public ZX128(ZXSpectrum spectrum, Z80A cpu, ZXSpectrum.BorderType borderType, List files, List joysticks) - { - Spectrum = spectrum; - CPU = cpu; + /// + /// Main constructor + /// + public ZX128(ZXSpectrum spectrum, Z80A cpu, ZXSpectrum.BorderType borderType, List files, List joysticks) + { + Spectrum = spectrum; + CPU = cpu; - CPUMon = new CPUMonitor(this); - CPUMon.machineType = MachineType.ZXSpectrum128; + CPUMon = new CPUMonitor(this); + CPUMon.machineType = MachineType.ZXSpectrum128; - ROMPaged = 0; - SHADOWPaged = false; - RAMPaged = 0; - PagingDisabled = false; - - ULADevice = new Screen128(this); + ROMPaged = 0; + SHADOWPaged = false; + RAMPaged = 0; + PagingDisabled = false; + + ULADevice = new Screen128(this); BuzzerDevice = new OneBitBeeper(44100, ULADevice.FrameLength, 50, "SystemBuzzer"); TapeBuzzer = new OneBitBeeper(44100, ULADevice.FrameLength, 50, "TapeBuzzer"); AYDevice = new AY38912(this); - AYDevice.Init(44100, ULADevice.FrameLength); + AYDevice.Init(44100, ULADevice.FrameLength); - KeyboardDevice = new StandardKeyboard(this); + KeyboardDevice = new StandardKeyboard(this); - InitJoysticks(joysticks); + InitJoysticks(joysticks); - TapeDevice = new DatacorderDevice(spectrum.SyncSettings.AutoLoadTape); - TapeDevice.Init(this); + TapeDevice = new DatacorderDevice(spectrum.SyncSettings.AutoLoadTape); + TapeDevice.Init(this); - InitializeMedia(files); - } + InitializeMedia(files); + } - #endregion - } + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum128KPlus2/ZX128Plus2.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum128KPlus2/ZX128Plus2.cs index d276e52bb1..e8aecf71f7 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum128KPlus2/ZX128Plus2.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum128KPlus2/ZX128Plus2.cs @@ -3,23 +3,23 @@ using System.Collections.Generic; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// The +2 is almost identical to the 128k from an emulation point of view - /// There are just a few small changes in the ROMs - /// - public partial class ZX128Plus2 : ZX128 - { - #region Construction + /// + /// The +2 is almost identical to the 128k from an emulation point of view + /// There are just a few small changes in the ROMs + /// + public partial class ZX128Plus2 : ZX128 + { + #region Construction - /// - /// Main constructor - /// - public ZX128Plus2(ZXSpectrum spectrum, Z80A cpu, ZXSpectrum.BorderType borderType, List files, List joysticks) - : base(spectrum, cpu, borderType, files, joysticks) - { - - } + /// + /// Main constructor + /// + public ZX128Plus2(ZXSpectrum spectrum, Z80A cpu, ZXSpectrum.BorderType borderType, List files, List joysticks) + : base(spectrum, cpu, borderType, files, joysticks) + { - #endregion - } + } + + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum128KPlus2a/ZX128Plus2a.Screen.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum128KPlus2a/ZX128Plus2a.Screen.cs index 7d9deeed41..ecbbccbb04 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum128KPlus2a/ZX128Plus2a.Screen.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum128KPlus2a/ZX128Plus2a.Screen.cs @@ -1,50 +1,50 @@  namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// +2A/+3 ULA - /// - class Screen128Plus2a : ULA - { - #region Construction + /// + /// +2A/+3 ULA + /// + class Screen128Plus2a : ULA + { + #region Construction - public Screen128Plus2a(SpectrumBase machine) + public Screen128Plus2a(SpectrumBase machine) : base(machine) - { - // interrupt - InterruptStartTime = 0; - InterruptLength = 32; - // offsets - RenderTableOffset = 58; - ContentionOffset = 9; - FloatingBusOffset = 0; - // timing - ClockSpeed = 3546900; - FrameCycleLength = 70908; - ScanlineTime = 228; - BorderLeftTime = 24; - BorderRightTime = 24; - FirstPaperLine = 63; - FirstPaperTState = 64; - // screen layout - Border4T = true; - Border4TStage = 2; - ScreenWidth = 256; - ScreenHeight = 192; - BorderTopHeight = 48;// 55; - BorderBottomHeight = 48; // 56; - BorderLeftWidth = 48; - BorderRightWidth = 48; - ScanLineWidth = BorderLeftWidth + ScreenWidth + BorderRightWidth; + { + // interrupt + InterruptStartTime = 0; + InterruptLength = 32; + // offsets + RenderTableOffset = 58; + ContentionOffset = 9; + FloatingBusOffset = 0; + // timing + ClockSpeed = 3546900; + FrameCycleLength = 70908; + ScanlineTime = 228; + BorderLeftTime = 24; + BorderRightTime = 24; + FirstPaperLine = 63; + FirstPaperTState = 64; + // screen layout + Border4T = true; + Border4TStage = 2; + ScreenWidth = 256; + ScreenHeight = 192; + BorderTopHeight = 48;// 55; + BorderBottomHeight = 48; // 56; + BorderLeftWidth = 48; + BorderRightWidth = 48; + ScanLineWidth = BorderLeftWidth + ScreenWidth + BorderRightWidth; - RenderingTable = new RenderTable(this, - MachineType.ZXSpectrum128Plus2a); + RenderingTable = new RenderTable(this, + MachineType.ZXSpectrum128Plus2a); - SetupScreenSize(); + SetupScreenSize(); - GenerateP3PortTable(); - } + GenerateP3PortTable(); + } - #endregion - } + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum128KPlus2a/ZX128Plus2a.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum128KPlus2a/ZX128Plus2a.cs index a05903b7e7..69140ea87d 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum128KPlus2a/ZX128Plus2a.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum128KPlus2a/ZX128Plus2a.cs @@ -4,48 +4,48 @@ using BizHawk.Emulation.Cores.Sound; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// +2A Constructor - /// - public partial class ZX128Plus2a : SpectrumBase - { - #region Construction + /// + /// +2A Constructor + /// + public partial class ZX128Plus2a : SpectrumBase + { + #region Construction - /// - /// Main constructor - /// - public ZX128Plus2a(ZXSpectrum spectrum, Z80A cpu, ZXSpectrum.BorderType borderType, List files, List joysticks) - { - Spectrum = spectrum; - CPU = cpu; + /// + /// Main constructor + /// + public ZX128Plus2a(ZXSpectrum spectrum, Z80A cpu, ZXSpectrum.BorderType borderType, List files, List joysticks) + { + Spectrum = spectrum; + CPU = cpu; - CPUMon = new CPUMonitor(this); - CPUMon.machineType = MachineType.ZXSpectrum128Plus2a; + CPUMon = new CPUMonitor(this); + CPUMon.machineType = MachineType.ZXSpectrum128Plus2a; - ROMPaged = 0; - SHADOWPaged = false; - RAMPaged = 0; - PagingDisabled = false; - - ULADevice = new Screen128Plus2a(this); + ROMPaged = 0; + SHADOWPaged = false; + RAMPaged = 0; + PagingDisabled = false; + + ULADevice = new Screen128Plus2a(this); BuzzerDevice = new OneBitBeeper(44100, ULADevice.FrameLength, 50, "SystemBuzzer"); TapeBuzzer = new OneBitBeeper(44100, ULADevice.FrameLength, 50, "TapeBuzzer"); AYDevice = new AY38912(this); - AYDevice.Init(44100, ULADevice.FrameLength); + AYDevice.Init(44100, ULADevice.FrameLength); - KeyboardDevice = new StandardKeyboard(this); + KeyboardDevice = new StandardKeyboard(this); - InitJoysticks(joysticks); + InitJoysticks(joysticks); - TapeDevice = new DatacorderDevice(spectrum.SyncSettings.AutoLoadTape); - TapeDevice.Init(this); + TapeDevice = new DatacorderDevice(spectrum.SyncSettings.AutoLoadTape); + TapeDevice.Init(this); - InitializeMedia(files); - } + InitializeMedia(files); + } - #endregion - } + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum128KPlus3/ZX128Plus3.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum128KPlus3/ZX128Plus3.cs index 0b4a0a4610..9b372a0a35 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum128KPlus3/ZX128Plus3.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum128KPlus3/ZX128Plus3.cs @@ -4,51 +4,51 @@ using BizHawk.Emulation.Cores.Sound; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// +3 Constructor - /// - public partial class ZX128Plus3 : SpectrumBase - { - #region Construction + /// + /// +3 Constructor + /// + public partial class ZX128Plus3 : SpectrumBase + { + #region Construction - /// - /// Main constructor - /// - public ZX128Plus3(ZXSpectrum spectrum, Z80A cpu, ZXSpectrum.BorderType borderType, List files, List joysticks) - { - Spectrum = spectrum; - CPU = cpu; + /// + /// Main constructor + /// + public ZX128Plus3(ZXSpectrum spectrum, Z80A cpu, ZXSpectrum.BorderType borderType, List files, List joysticks) + { + Spectrum = spectrum; + CPU = cpu; - CPUMon = new CPUMonitor(this); - CPUMon.machineType = MachineType.ZXSpectrum128Plus3; + CPUMon = new CPUMonitor(this); + CPUMon.machineType = MachineType.ZXSpectrum128Plus3; - ROMPaged = 0; - SHADOWPaged = false; - RAMPaged = 0; - PagingDisabled = false; - - ULADevice = new Screen128Plus2a(this); + ROMPaged = 0; + SHADOWPaged = false; + RAMPaged = 0; + PagingDisabled = false; + + ULADevice = new Screen128Plus2a(this); BuzzerDevice = new OneBitBeeper(44100, ULADevice.FrameLength, 50, "SystemBuzzer"); TapeBuzzer = new OneBitBeeper(44100, ULADevice.FrameLength, 50, "TapeBuzzer"); AYDevice = new AY38912(this); - AYDevice.Init(44100, ULADevice.FrameLength); + AYDevice.Init(44100, ULADevice.FrameLength); - KeyboardDevice = new StandardKeyboard(this); + KeyboardDevice = new StandardKeyboard(this); - InitJoysticks(joysticks); + InitJoysticks(joysticks); - TapeDevice = new DatacorderDevice(spectrum.SyncSettings.AutoLoadTape); - TapeDevice.Init(this); + TapeDevice = new DatacorderDevice(spectrum.SyncSettings.AutoLoadTape); + TapeDevice.Init(this); - UPDDiskDevice = new NECUPD765(); - UPDDiskDevice.Init(this); + UPDDiskDevice = new NECUPD765(); + UPDDiskDevice.Init(this); - InitializeMedia(files); - } + InitializeMedia(files); + } - #endregion - } + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum16K/ZX16.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum16K/ZX16.cs index ef4af265f2..410fa1843c 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum16K/ZX16.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum16K/ZX16.cs @@ -3,27 +3,27 @@ using System.Collections.Generic; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// 16K is idential to 48K, just without the top 32KB of RAM - /// - public class ZX16 : ZX48 - { - #region Construction + /// + /// 16K is idential to 48K, just without the top 32KB of RAM + /// + public class ZX16 : ZX48 + { + #region Construction - /// - /// Main constructor - /// - public ZX16(ZXSpectrum spectrum, Z80A cpu, ZXSpectrum.BorderType borderType, List files, List joysticks) - : base(spectrum, cpu, borderType, files, joysticks) - { + /// + /// Main constructor + /// + public ZX16(ZXSpectrum spectrum, Z80A cpu, ZXSpectrum.BorderType borderType, List files, List joysticks) + : base(spectrum, cpu, borderType, files, joysticks) + { - } + } - #endregion + #endregion - #region Memory + #region Memory - /* 48K Spectrum has NO memory paging + /* 48K Spectrum has NO memory paging * * | Bank 0 | @@ -38,101 +38,101 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum 0x0000 +--------+ */ - /// - /// Simulates reading from the bus (no contention) - /// Paging should be handled here - /// - public override byte ReadBus(ushort addr) - { - int divisor = addr / 0x4000; - var index = addr % 0x4000; + /// + /// Simulates reading from the bus (no contention) + /// Paging should be handled here + /// + public override byte ReadBus(ushort addr) + { + int divisor = addr / 0x4000; + var index = addr % 0x4000; - // paging logic goes here + // paging logic goes here - switch (divisor) - { - case 0: - TestForTapeTraps(addr % 0x4000); - return ROM0[index]; - case 1: return RAM0[index]; - default: - // memory does not exist - return 0xff; - } - } + switch (divisor) + { + case 0: + TestForTapeTraps(addr % 0x4000); + return ROM0[index]; + case 1: return RAM0[index]; + default: + // memory does not exist + return 0xff; + } + } - /// - /// Simulates writing to the bus (no contention) - /// Paging should be handled here - /// - public override void WriteBus(ushort addr, byte value) - { - int divisor = addr / 0x4000; - var index = addr % 0x4000; + /// + /// Simulates writing to the bus (no contention) + /// Paging should be handled here + /// + public override void WriteBus(ushort addr, byte value) + { + int divisor = addr / 0x4000; + var index = addr % 0x4000; - // paging logic goes here + // paging logic goes here - switch (divisor) - { - case 0: - // cannot write to ROM - break; - case 1: - //ULADevice.RenderScreen((int)CurrentFrameCycle); - RAM0[index] = value; - break; - } - } + switch (divisor) + { + case 0: + // cannot write to ROM + break; + case 1: + //ULADevice.RenderScreen((int)CurrentFrameCycle); + RAM0[index] = value; + break; + } + } - /// - /// Reads a byte of data from a specified memory address - /// (with memory contention if appropriate) - /// - public override byte ReadMemory(ushort addr) - { - var data = ReadBus(addr); - return data; - } + /// + /// Reads a byte of data from a specified memory address + /// (with memory contention if appropriate) + /// + public override byte ReadMemory(ushort addr) + { + var data = ReadBus(addr); + return data; + } - /// - /// Returns the ROM/RAM enum that relates to this particular memory read operation - /// - public override ZXSpectrum.CDLResult ReadCDL(ushort addr) - { - var res = new ZXSpectrum.CDLResult(); + /// + /// Returns the ROM/RAM enum that relates to this particular memory read operation + /// + public override ZXSpectrum.CDLResult ReadCDL(ushort addr) + { + var res = new ZXSpectrum.CDLResult(); - int divisor = addr / 0x4000; - res.Address = addr % 0x4000; + int divisor = addr / 0x4000; + res.Address = addr % 0x4000; - // paging logic goes here - switch (divisor) - { - case 0: res.Type = ZXSpectrum.CDLType.ROM0; break; - case 1: res.Type = ZXSpectrum.CDLType.RAM0; break; - } + // paging logic goes here + switch (divisor) + { + case 0: res.Type = ZXSpectrum.CDLType.ROM0; break; + case 1: res.Type = ZXSpectrum.CDLType.RAM0; break; + } - return res; - } + return res; + } - /// - /// Writes a byte of data to a specified memory address - /// (with memory contention if appropriate) - /// - public override void WriteMemory(ushort addr, byte value) - { - WriteBus(addr, value); - } - - /// - /// Sets up the ROM - /// - public override void InitROM(RomData romData) - { - RomData = romData; - // for 16/48k machines only ROM0 is used (no paging) - RomData.RomBytes?.CopyTo(ROM0, 0); - } + /// + /// Writes a byte of data to a specified memory address + /// (with memory contention if appropriate) + /// + public override void WriteMemory(ushort addr, byte value) + { + WriteBus(addr, value); + } - #endregion - } + /// + /// Sets up the ROM + /// + public override void InitROM(RomData romData) + { + RomData = romData; + // for 16/48k machines only ROM0 is used (no paging) + RomData.RomBytes?.CopyTo(ROM0, 0); + } + + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum48K/ZX48.Screen.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum48K/ZX48.Screen.cs index 9f01e83c04..3507b6ce10 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum48K/ZX48.Screen.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum48K/ZX48.Screen.cs @@ -1,48 +1,48 @@  namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// 48K ULA - /// - class Screen48 : ULA - { - #region Construction + /// + /// 48K ULA + /// + class Screen48 : ULA + { + #region Construction public Screen48(SpectrumBase machine) : base(machine) - { - // interrupt - InterruptStartTime = 3; - InterruptLength = 32; - // offsets - RenderTableOffset = 56; - ContentionOffset = 6; - FloatingBusOffset = 1; - // timing - ClockSpeed = 3500000; - FrameCycleLength = 69888; - ScanlineTime = 224; - BorderLeftTime = 24; - BorderRightTime = 24; - FirstPaperLine = 64; - FirstPaperTState = 64; - // screen layout - Border4T = true; - Border4TStage = 0; - ScreenWidth = 256; - ScreenHeight = 192; - BorderTopHeight = 48;// 55;// 48; - BorderBottomHeight = 48;// 56; - BorderLeftWidth = 48; - BorderRightWidth = 48; - ScanLineWidth = BorderLeftWidth + ScreenWidth + BorderRightWidth; + { + // interrupt + InterruptStartTime = 3; + InterruptLength = 32; + // offsets + RenderTableOffset = 56; + ContentionOffset = 6; + FloatingBusOffset = 1; + // timing + ClockSpeed = 3500000; + FrameCycleLength = 69888; + ScanlineTime = 224; + BorderLeftTime = 24; + BorderRightTime = 24; + FirstPaperLine = 64; + FirstPaperTState = 64; + // screen layout + Border4T = true; + Border4TStage = 0; + ScreenWidth = 256; + ScreenHeight = 192; + BorderTopHeight = 48;// 55;// 48; + BorderBottomHeight = 48;// 56; + BorderLeftWidth = 48; + BorderRightWidth = 48; + ScanLineWidth = BorderLeftWidth + ScreenWidth + BorderRightWidth; - RenderingTable = new RenderTable(this, - MachineType.ZXSpectrum48); + RenderingTable = new RenderTable(this, + MachineType.ZXSpectrum48); - SetupScreenSize(); - } + SetupScreenSize(); + } - #endregion - } + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum48K/ZX48.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum48K/ZX48.cs index 5c235eb47d..2a793f0a47 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum48K/ZX48.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Machine/ZXSpectrum48K/ZX48.cs @@ -5,23 +5,23 @@ using BizHawk.Emulation.Cores.Sound; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// 48K construction - /// - public partial class ZX48 : SpectrumBase - { - #region Construction + /// + /// 48K construction + /// + public partial class ZX48 : SpectrumBase + { + #region Construction - /// - /// Main constructor - /// - public ZX48(ZXSpectrum spectrum, Z80A cpu, ZXSpectrum.BorderType borderType, List files, List joysticks) - { - Spectrum = spectrum; - CPU = cpu; + /// + /// Main constructor + /// + public ZX48(ZXSpectrum spectrum, Z80A cpu, ZXSpectrum.BorderType borderType, List files, List joysticks) + { + Spectrum = spectrum; + CPU = cpu; - CPUMon = new CPUMonitor(this); - ULADevice = new Screen48(this); + CPUMon = new CPUMonitor(this); + ULADevice = new Screen48(this); BuzzerDevice = new OneBitBeeper(44100, ULADevice.FrameLength, 50, "SystemBuzzer"); @@ -29,29 +29,29 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum KeyboardDevice = new StandardKeyboard(this); - InitJoysticks(joysticks); + InitJoysticks(joysticks); - TapeDevice = new DatacorderDevice(spectrum.SyncSettings.AutoLoadTape); - TapeDevice.Init(this); + TapeDevice = new DatacorderDevice(spectrum.SyncSettings.AutoLoadTape); + TapeDevice.Init(this); - InitializeMedia(files); - } + InitializeMedia(files); + } - #endregion + #endregion - #region Reset + #region Reset - public override void HardReset() - { - base.HardReset(); + public override void HardReset() + { + base.HardReset(); - Random rn = new Random(); - for (int d = 0; d < 6912; d++) - { - RAM0[d] = (byte)rn.Next(255); - } - } - - #endregion - } + Random rn = new Random(); + for (int d = 0; d < 6912; d++) + { + RAM0[d] = (byte)rn.Next(255); + } + } + + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/CPCFormat/CPCExtendedFloppyDisk.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/CPCFormat/CPCExtendedFloppyDisk.cs index c0647b76e6..b6f1a6b9a2 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/CPCFormat/CPCExtendedFloppyDisk.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/CPCFormat/CPCExtendedFloppyDisk.cs @@ -5,260 +5,260 @@ using System.Collections.Generic; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Logical object representing a standard +3 disk image - /// - public class CPCExtendedFloppyDisk : FloppyDisk - { - /// - /// The format type - /// - public override DiskType DiskFormatType => DiskType.CPCExtended; + /// + /// Logical object representing a standard +3 disk image + /// + public class CPCExtendedFloppyDisk : FloppyDisk + { + /// + /// The format type + /// + public override DiskType DiskFormatType => DiskType.CPCExtended; - /// - /// Attempts to parse incoming disk data - /// - /// - /// TRUE: disk parsed - /// FALSE: unable to parse disk - /// - public override bool ParseDisk(byte[] data) - { - // look for standard magic string - string ident = Encoding.ASCII.GetString(data, 0, 16); + /// + /// Attempts to parse incoming disk data + /// + /// + /// TRUE: disk parsed + /// FALSE: unable to parse disk + /// + public override bool ParseDisk(byte[] data) + { + // look for standard magic string + string ident = Encoding.ASCII.GetString(data, 0, 16); - if (!ident.ToUpper().Contains("EXTENDED CPC DSK")) - { - // incorrect format - return false; - } + if (!ident.ToUpper().Contains("EXTENDED CPC DSK")) + { + // incorrect format + return false; + } - // read the disk information block - DiskHeader.DiskIdent = ident; - DiskHeader.DiskCreatorString = Encoding.ASCII.GetString(data, 0x22, 14); - DiskHeader.NumberOfTracks = data[0x30]; - DiskHeader.NumberOfSides = data[0x31]; - DiskHeader.TrackSizes = new int[DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides]; - DiskTracks = new Track[DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides]; - DiskData = data; - int pos = 0x34; + // read the disk information block + DiskHeader.DiskIdent = ident; + DiskHeader.DiskCreatorString = Encoding.ASCII.GetString(data, 0x22, 14); + DiskHeader.NumberOfTracks = data[0x30]; + DiskHeader.NumberOfSides = data[0x31]; + DiskHeader.TrackSizes = new int[DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides]; + DiskTracks = new Track[DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides]; + DiskData = data; + int pos = 0x34; - if (DiskHeader.NumberOfSides > 1) - { - StringBuilder sbm = new StringBuilder(); - sbm.AppendLine(); - sbm.AppendLine(); - sbm.AppendLine("The detected disk image contains multiple sides."); - sbm.AppendLine("This is NOT currently supported in ZXHawk."); - sbm.AppendLine("Please find an alternate image/dump where each side has been saved as a separate *.dsk image (and use the mutli-disk bundler tool to load into Bizhawk)."); - throw new System.NotImplementedException(sbm.ToString()); - } + if (DiskHeader.NumberOfSides > 1) + { + StringBuilder sbm = new StringBuilder(); + sbm.AppendLine(); + sbm.AppendLine(); + sbm.AppendLine("The detected disk image contains multiple sides."); + sbm.AppendLine("This is NOT currently supported in ZXHawk."); + sbm.AppendLine("Please find an alternate image/dump where each side has been saved as a separate *.dsk image (and use the mutli-disk bundler tool to load into Bizhawk)."); + throw new System.NotImplementedException(sbm.ToString()); + } - if (DiskHeader.NumberOfTracks > 42) - { - StringBuilder sbm = new StringBuilder(); - sbm.AppendLine(); - sbm.AppendLine(); - sbm.AppendLine("The detected disk is an " + DiskHeader.NumberOfTracks + " track disk image."); - sbm.AppendLine("This is currently incompatible with the emulated +3 disk drive (42 tracks)."); - sbm.AppendLine("Likely the disk image is an 80 track betadisk or opus image, the drives and controllers for which are not currently emulated in ZXHawk"); - throw new System.NotImplementedException(sbm.ToString()); - } + if (DiskHeader.NumberOfTracks > 42) + { + StringBuilder sbm = new StringBuilder(); + sbm.AppendLine(); + sbm.AppendLine(); + sbm.AppendLine("The detected disk is an " + DiskHeader.NumberOfTracks + " track disk image."); + sbm.AppendLine("This is currently incompatible with the emulated +3 disk drive (42 tracks)."); + sbm.AppendLine("Likely the disk image is an 80 track betadisk or opus image, the drives and controllers for which are not currently emulated in ZXHawk"); + throw new System.NotImplementedException(sbm.ToString()); + } - for (int i = 0; i < DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides; i++) - { - DiskHeader.TrackSizes[i] = data[pos++] * 256; - } + for (int i = 0; i < DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides; i++) + { + DiskHeader.TrackSizes[i] = data[pos++] * 256; + } - // move to first track information block - pos = 0x100; + // move to first track information block + pos = 0x100; - // parse each track - for (int i = 0; i < DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides; i++) - { - // check for unformatted track - if (DiskHeader.TrackSizes[i] == 0) - { - DiskTracks[i] = new Track(); - DiskTracks[i].Sectors = new Sector[0]; - continue; - } + // parse each track + for (int i = 0; i < DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides; i++) + { + // check for unformatted track + if (DiskHeader.TrackSizes[i] == 0) + { + DiskTracks[i] = new Track(); + DiskTracks[i].Sectors = new Sector[0]; + continue; + } - int p = pos; - DiskTracks[i] = new Track(); + int p = pos; + DiskTracks[i] = new Track(); - // track info block - DiskTracks[i].TrackIdent = Encoding.ASCII.GetString(data, p, 12); - p += 16; - DiskTracks[i].TrackNumber = data[p++]; - DiskTracks[i].SideNumber = data[p++]; - DiskTracks[i].DataRate = data[p++]; - DiskTracks[i].RecordingMode = data[p++]; - DiskTracks[i].SectorSize = data[p++]; - DiskTracks[i].NumberOfSectors = data[p++]; - DiskTracks[i].GAP3Length = data[p++]; - DiskTracks[i].FillerByte = data[p++]; + // track info block + DiskTracks[i].TrackIdent = Encoding.ASCII.GetString(data, p, 12); + p += 16; + DiskTracks[i].TrackNumber = data[p++]; + DiskTracks[i].SideNumber = data[p++]; + DiskTracks[i].DataRate = data[p++]; + DiskTracks[i].RecordingMode = data[p++]; + DiskTracks[i].SectorSize = data[p++]; + DiskTracks[i].NumberOfSectors = data[p++]; + DiskTracks[i].GAP3Length = data[p++]; + DiskTracks[i].FillerByte = data[p++]; - int dpos = pos + 0x100; + int dpos = pos + 0x100; - // sector info list - DiskTracks[i].Sectors = new Sector[DiskTracks[i].NumberOfSectors]; - for (int s = 0; s < DiskTracks[i].NumberOfSectors; s++) - { - DiskTracks[i].Sectors[s] = new Sector(); + // sector info list + DiskTracks[i].Sectors = new Sector[DiskTracks[i].NumberOfSectors]; + for (int s = 0; s < DiskTracks[i].NumberOfSectors; s++) + { + DiskTracks[i].Sectors[s] = new Sector(); - DiskTracks[i].Sectors[s].TrackNumber = data[p++]; - DiskTracks[i].Sectors[s].SideNumber = data[p++]; - DiskTracks[i].Sectors[s].SectorID = data[p++]; - DiskTracks[i].Sectors[s].SectorSize = data[p++]; - DiskTracks[i].Sectors[s].Status1 = data[p++]; - DiskTracks[i].Sectors[s].Status2 = data[p++]; - DiskTracks[i].Sectors[s].ActualDataByteLength = MediaConverter.GetWordValue(data, p); - p += 2; + DiskTracks[i].Sectors[s].TrackNumber = data[p++]; + DiskTracks[i].Sectors[s].SideNumber = data[p++]; + DiskTracks[i].Sectors[s].SectorID = data[p++]; + DiskTracks[i].Sectors[s].SectorSize = data[p++]; + DiskTracks[i].Sectors[s].Status1 = data[p++]; + DiskTracks[i].Sectors[s].Status2 = data[p++]; + DiskTracks[i].Sectors[s].ActualDataByteLength = MediaConverter.GetWordValue(data, p); + p += 2; - // sector data - begins at 0x100 offset from the start of the track info block (in this case dpos) - DiskTracks[i].Sectors[s].SectorData = new byte[DiskTracks[i].Sectors[s].ActualDataByteLength]; + // sector data - begins at 0x100 offset from the start of the track info block (in this case dpos) + DiskTracks[i].Sectors[s].SectorData = new byte[DiskTracks[i].Sectors[s].ActualDataByteLength]; - // copy the data - for (int b = 0; b < DiskTracks[i].Sectors[s].ActualDataByteLength; b++) - { - DiskTracks[i].Sectors[s].SectorData[b] = data[dpos + b]; - } + // copy the data + for (int b = 0; b < DiskTracks[i].Sectors[s].ActualDataByteLength; b++) + { + DiskTracks[i].Sectors[s].SectorData[b] = data[dpos + b]; + } - // check for multiple weak/random sectors stored - if (DiskTracks[i].Sectors[s].SectorSize <= 7) - { - // sectorsize n=8 is equivilent to n=0 - FDC will use DTL for length - int specifiedSize = 0x80 << DiskTracks[i].Sectors[s].SectorSize; + // check for multiple weak/random sectors stored + if (DiskTracks[i].Sectors[s].SectorSize <= 7) + { + // sectorsize n=8 is equivilent to n=0 - FDC will use DTL for length + int specifiedSize = 0x80 << DiskTracks[i].Sectors[s].SectorSize; - if (specifiedSize < DiskTracks[i].Sectors[s].ActualDataByteLength) - { - // more data stored than sectorsize defines - // check for multiple weak/random copies - if (DiskTracks[i].Sectors[s].ActualDataByteLength % specifiedSize != 0) - { - DiskTracks[i].Sectors[s].ContainsMultipleWeakSectors = true; - } - } - } + if (specifiedSize < DiskTracks[i].Sectors[s].ActualDataByteLength) + { + // more data stored than sectorsize defines + // check for multiple weak/random copies + if (DiskTracks[i].Sectors[s].ActualDataByteLength % specifiedSize != 0) + { + DiskTracks[i].Sectors[s].ContainsMultipleWeakSectors = true; + } + } + } - // move dpos to the next sector data postion - dpos += DiskTracks[i].Sectors[s].ActualDataByteLength; - } + // move dpos to the next sector data postion + dpos += DiskTracks[i].Sectors[s].ActualDataByteLength; + } - // move to the next track info block - pos += DiskHeader.TrackSizes[i]; - } + // move to the next track info block + pos += DiskHeader.TrackSizes[i]; + } - // run protection scheme detector - ParseProtection(); + // run protection scheme detector + ParseProtection(); - return true; - } + return true; + } - /// - /// Takes a double-sided disk byte array and converts into 2 single-sided arrays - /// - public static bool SplitDoubleSided(byte[] data, List results) - { - // look for standard magic string - string ident = Encoding.ASCII.GetString(data, 0, 16); - if (!ident.ToUpper().Contains("EXTENDED CPC DSK")) - { - // incorrect format - return false; - } + /// + /// Takes a double-sided disk byte array and converts into 2 single-sided arrays + /// + public static bool SplitDoubleSided(byte[] data, List results) + { + // look for standard magic string + string ident = Encoding.ASCII.GetString(data, 0, 16); + if (!ident.ToUpper().Contains("EXTENDED CPC DSK")) + { + // incorrect format + return false; + } - byte[] S0 = new byte[data.Length]; - byte[] S1 = new byte[data.Length]; + byte[] S0 = new byte[data.Length]; + byte[] S1 = new byte[data.Length]; - // disk info block - Array.Copy(data, 0, S0, 0, 0x100); - Array.Copy(data, 0, S1, 0, 0x100); - // change side number - S0[0x31] = 1; - S1[0x31] = 1; + // disk info block + Array.Copy(data, 0, S0, 0, 0x100); + Array.Copy(data, 0, S1, 0, 0x100); + // change side number + S0[0x31] = 1; + S1[0x31] = 1; - // extended format can have different track sizes - int[] trkSizes = new int[data[0x30] * data[0x31]]; + // extended format can have different track sizes + int[] trkSizes = new int[data[0x30] * data[0x31]]; - int pos = 0x34; - for (int i = 0; i < data[0x30] * data[0x31]; i++) - { - trkSizes[i] = data[pos] * 256; - // clear destination trk sizes (will be added later) - S0[pos] = 0; - S1[pos] = 0; - pos++; - } + int pos = 0x34; + for (int i = 0; i < data[0x30] * data[0x31]; i++) + { + trkSizes[i] = data[pos] * 256; + // clear destination trk sizes (will be added later) + S0[pos] = 0; + S1[pos] = 0; + pos++; + } - // start at track info blocks - int mPos = 0x100; - int s0Pos = 0x100; - int s0tCount = 0; - int s1tCount = 0; - int s1Pos = 0x100; - int tCount = 0; + // start at track info blocks + int mPos = 0x100; + int s0Pos = 0x100; + int s0tCount = 0; + int s1tCount = 0; + int s1Pos = 0x100; + int tCount = 0; - while (tCount < data[0x30] * data[0x31]) - { - // which side is this? - var side = data[mPos + 0x11]; - if (side == 0) - { - // side 1 - Array.Copy(data, mPos, S0, s0Pos, trkSizes[tCount]); - s0Pos += trkSizes[tCount]; - // trk size table - S0[0x34 + s0tCount++] = (byte)(trkSizes[tCount] / 256); - } - else if (side == 1) - { - // side 2 - Array.Copy(data, mPos, S1, s1Pos, trkSizes[tCount]); - s1Pos += trkSizes[tCount]; - // trk size table - S1[0x34 + s1tCount++] = (byte)(trkSizes[tCount] / 256); - } + while (tCount < data[0x30] * data[0x31]) + { + // which side is this? + var side = data[mPos + 0x11]; + if (side == 0) + { + // side 1 + Array.Copy(data, mPos, S0, s0Pos, trkSizes[tCount]); + s0Pos += trkSizes[tCount]; + // trk size table + S0[0x34 + s0tCount++] = (byte)(trkSizes[tCount] / 256); + } + else if (side == 1) + { + // side 2 + Array.Copy(data, mPos, S1, s1Pos, trkSizes[tCount]); + s1Pos += trkSizes[tCount]; + // trk size table + S1[0x34 + s1tCount++] = (byte)(trkSizes[tCount] / 256); + } - mPos += trkSizes[tCount++]; - } + mPos += trkSizes[tCount++]; + } - byte[] s0final = new byte[s0Pos]; - byte[] s1final = new byte[s1Pos]; - Array.Copy(S0, 0, s0final, 0, s0Pos); - Array.Copy(S1, 0, s1final, 0, s1Pos); + byte[] s0final = new byte[s0Pos]; + byte[] s1final = new byte[s1Pos]; + Array.Copy(S0, 0, s0final, 0, s0Pos); + Array.Copy(S1, 0, s1final, 0, s1Pos); - results.Add(s0final); - results.Add(s1final); + results.Add(s0final); + results.Add(s1final); - return true; - } + return true; + } - /// - /// State serlialization - /// - public override void SyncState(Serializer ser) - { - ser.BeginSection("Plus3FloppyDisk"); + /// + /// State serlialization + /// + public override void SyncState(Serializer ser) + { + ser.BeginSection("Plus3FloppyDisk"); - ser.Sync(nameof(CylinderCount), ref CylinderCount); - ser.Sync(nameof(SideCount), ref SideCount); - ser.Sync(nameof(BytesPerTrack), ref BytesPerTrack); - ser.Sync(nameof(WriteProtected), ref WriteProtected); - ser.SyncEnum(nameof(Protection), ref Protection); + ser.Sync(nameof(CylinderCount), ref CylinderCount); + ser.Sync(nameof(SideCount), ref SideCount); + ser.Sync(nameof(BytesPerTrack), ref BytesPerTrack); + ser.Sync(nameof(WriteProtected), ref WriteProtected); + ser.SyncEnum(nameof(Protection), ref Protection); - ser.Sync(nameof(DirtyData), ref DirtyData); - if (DirtyData) - { + ser.Sync(nameof(DirtyData), ref DirtyData); + if (DirtyData) + { - } + } - // sync deterministic track and sector counters - ser.Sync(nameof( _randomCounter), ref _randomCounter); - RandomCounter = _randomCounter; + // sync deterministic track and sector counters + ser.Sync(nameof(_randomCounter), ref _randomCounter); + RandomCounter = _randomCounter; - ser.EndSection(); - } - } + ser.EndSection(); + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/CPCFormat/CPCFloppyDisk.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/CPCFormat/CPCFloppyDisk.cs index 3851019be3..86ad8c8428 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/CPCFormat/CPCFloppyDisk.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/CPCFormat/CPCFloppyDisk.cs @@ -5,250 +5,250 @@ using System.Collections.Generic; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Logical object representing a standard +3 disk image - /// - public class CPCFloppyDisk : FloppyDisk - { - /// - /// The format type - /// - public override DiskType DiskFormatType => DiskType.CPC; + /// + /// Logical object representing a standard +3 disk image + /// + public class CPCFloppyDisk : FloppyDisk + { + /// + /// The format type + /// + public override DiskType DiskFormatType => DiskType.CPC; - /// - /// Attempts to parse incoming disk data - /// - /// - /// TRUE: disk parsed - /// FALSE: unable to parse disk - /// - public override bool ParseDisk(byte[] data) - { - // look for standard magic string - string ident = Encoding.ASCII.GetString(data, 0, 16); + /// + /// Attempts to parse incoming disk data + /// + /// + /// TRUE: disk parsed + /// FALSE: unable to parse disk + /// + public override bool ParseDisk(byte[] data) + { + // look for standard magic string + string ident = Encoding.ASCII.GetString(data, 0, 16); - if (!ident.ToUpper().Contains("MV - CPC")) - { - // incorrect format - return false; - } + if (!ident.ToUpper().Contains("MV - CPC")) + { + // incorrect format + return false; + } - // read the disk information block - DiskHeader.DiskIdent = ident; - DiskHeader.DiskCreatorString = Encoding.ASCII.GetString(data, 0x22, 14); - DiskHeader.NumberOfTracks = data[0x30]; - DiskHeader.NumberOfSides = data[0x31]; - DiskHeader.TrackSizes = new int[DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides]; - DiskTracks = new Track[DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides]; - DiskData = data; - int pos = 0x32; + // read the disk information block + DiskHeader.DiskIdent = ident; + DiskHeader.DiskCreatorString = Encoding.ASCII.GetString(data, 0x22, 14); + DiskHeader.NumberOfTracks = data[0x30]; + DiskHeader.NumberOfSides = data[0x31]; + DiskHeader.TrackSizes = new int[DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides]; + DiskTracks = new Track[DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides]; + DiskData = data; + int pos = 0x32; - if (DiskHeader.NumberOfSides > 1) - { - StringBuilder sbm = new StringBuilder(); - sbm.AppendLine(); - sbm.AppendLine(); - sbm.AppendLine("The detected disk image contains multiple sides."); - sbm.AppendLine("This is NOT currently supported in ZXHawk."); - sbm.AppendLine("Please find an alternate image/dump where each side has been saved as a separate *.dsk image (and use the mutli-disk bundler tool to load into Bizhawk)."); - throw new System.NotImplementedException(sbm.ToString()); - } + if (DiskHeader.NumberOfSides > 1) + { + StringBuilder sbm = new StringBuilder(); + sbm.AppendLine(); + sbm.AppendLine(); + sbm.AppendLine("The detected disk image contains multiple sides."); + sbm.AppendLine("This is NOT currently supported in ZXHawk."); + sbm.AppendLine("Please find an alternate image/dump where each side has been saved as a separate *.dsk image (and use the mutli-disk bundler tool to load into Bizhawk)."); + throw new System.NotImplementedException(sbm.ToString()); + } - if (DiskHeader.NumberOfTracks > 42) - { - StringBuilder sbm = new StringBuilder(); - sbm.AppendLine(); - sbm.AppendLine(); - sbm.AppendLine("The detected disk is an " + DiskHeader.NumberOfTracks + " track disk image."); - sbm.AppendLine("This is currently incompatible with the emulated +3 disk drive (42 tracks)."); - sbm.AppendLine("Likely the disk image is an 80 track betadisk or opus image, the drives and controllers for which are not currently emulated in ZXHawk"); - throw new System.NotImplementedException(sbm.ToString()); - } + if (DiskHeader.NumberOfTracks > 42) + { + StringBuilder sbm = new StringBuilder(); + sbm.AppendLine(); + sbm.AppendLine(); + sbm.AppendLine("The detected disk is an " + DiskHeader.NumberOfTracks + " track disk image."); + sbm.AppendLine("This is currently incompatible with the emulated +3 disk drive (42 tracks)."); + sbm.AppendLine("Likely the disk image is an 80 track betadisk or opus image, the drives and controllers for which are not currently emulated in ZXHawk"); + throw new System.NotImplementedException(sbm.ToString()); + } - // standard CPC format all track sizes are the same in the image - for (int i = 0; i < DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides; i++) - { - DiskHeader.TrackSizes[i] = MediaConverter.GetWordValue(data, pos); - } + // standard CPC format all track sizes are the same in the image + for (int i = 0; i < DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides; i++) + { + DiskHeader.TrackSizes[i] = MediaConverter.GetWordValue(data, pos); + } - // move to first track information block - pos = 0x100; + // move to first track information block + pos = 0x100; - // parse each track - for (int i = 0; i < DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides; i++) - { - // check for unformatted track - if (DiskHeader.TrackSizes[i] == 0) - { - DiskTracks[i] = new Track(); - DiskTracks[i].Sectors = new Sector[0]; - continue; - } + // parse each track + for (int i = 0; i < DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides; i++) + { + // check for unformatted track + if (DiskHeader.TrackSizes[i] == 0) + { + DiskTracks[i] = new Track(); + DiskTracks[i].Sectors = new Sector[0]; + continue; + } - int p = pos; - DiskTracks[i] = new Track(); + int p = pos; + DiskTracks[i] = new Track(); - // track info block - DiskTracks[i].TrackIdent = Encoding.ASCII.GetString(data, p, 12); - p += 16; - DiskTracks[i].TrackNumber = data[p++]; - DiskTracks[i].SideNumber = data[p++]; - p += 2; - DiskTracks[i].SectorSize = data[p++]; - DiskTracks[i].NumberOfSectors = data[p++]; - DiskTracks[i].GAP3Length = data[p++]; - DiskTracks[i].FillerByte = data[p++]; + // track info block + DiskTracks[i].TrackIdent = Encoding.ASCII.GetString(data, p, 12); + p += 16; + DiskTracks[i].TrackNumber = data[p++]; + DiskTracks[i].SideNumber = data[p++]; + p += 2; + DiskTracks[i].SectorSize = data[p++]; + DiskTracks[i].NumberOfSectors = data[p++]; + DiskTracks[i].GAP3Length = data[p++]; + DiskTracks[i].FillerByte = data[p++]; - int dpos = pos + 0x100; + int dpos = pos + 0x100; - // sector info list - DiskTracks[i].Sectors = new Sector[DiskTracks[i].NumberOfSectors]; - for (int s = 0; s < DiskTracks[i].NumberOfSectors; s++) - { - DiskTracks[i].Sectors[s] = new Sector(); + // sector info list + DiskTracks[i].Sectors = new Sector[DiskTracks[i].NumberOfSectors]; + for (int s = 0; s < DiskTracks[i].NumberOfSectors; s++) + { + DiskTracks[i].Sectors[s] = new Sector(); - DiskTracks[i].Sectors[s].TrackNumber = data[p++]; - DiskTracks[i].Sectors[s].SideNumber = data[p++]; - DiskTracks[i].Sectors[s].SectorID = data[p++]; - DiskTracks[i].Sectors[s].SectorSize = data[p++]; - DiskTracks[i].Sectors[s].Status1 = data[p++]; - DiskTracks[i].Sectors[s].Status2 = data[p++]; - DiskTracks[i].Sectors[s].ActualDataByteLength = MediaConverter.GetWordValue(data, p); - p += 2; + DiskTracks[i].Sectors[s].TrackNumber = data[p++]; + DiskTracks[i].Sectors[s].SideNumber = data[p++]; + DiskTracks[i].Sectors[s].SectorID = data[p++]; + DiskTracks[i].Sectors[s].SectorSize = data[p++]; + DiskTracks[i].Sectors[s].Status1 = data[p++]; + DiskTracks[i].Sectors[s].Status2 = data[p++]; + DiskTracks[i].Sectors[s].ActualDataByteLength = MediaConverter.GetWordValue(data, p); + p += 2; - // actualdatabytelength value is calculated now - if (DiskTracks[i].Sectors[s].SectorSize == 0) - { - // no sectorsize specified - DTL will be used at runtime - DiskTracks[i].Sectors[s].ActualDataByteLength = DiskHeader.TrackSizes[i]; - } - else if (DiskTracks[i].Sectors[s].SectorSize > 6) - { - // invalid - wrap around to 0 - DiskTracks[i].Sectors[s].ActualDataByteLength = DiskHeader.TrackSizes[i]; - } - else if (DiskTracks[i].Sectors[s].SectorSize == 6) - { - // only 0x1800 bytes are stored - DiskTracks[i].Sectors[s].ActualDataByteLength = 0x1800; - } - else - { - // valid sector size for this format - DiskTracks[i].Sectors[s].ActualDataByteLength = 0x80 << DiskTracks[i].Sectors[s].SectorSize; - } + // actualdatabytelength value is calculated now + if (DiskTracks[i].Sectors[s].SectorSize == 0) + { + // no sectorsize specified - DTL will be used at runtime + DiskTracks[i].Sectors[s].ActualDataByteLength = DiskHeader.TrackSizes[i]; + } + else if (DiskTracks[i].Sectors[s].SectorSize > 6) + { + // invalid - wrap around to 0 + DiskTracks[i].Sectors[s].ActualDataByteLength = DiskHeader.TrackSizes[i]; + } + else if (DiskTracks[i].Sectors[s].SectorSize == 6) + { + // only 0x1800 bytes are stored + DiskTracks[i].Sectors[s].ActualDataByteLength = 0x1800; + } + else + { + // valid sector size for this format + DiskTracks[i].Sectors[s].ActualDataByteLength = 0x80 << DiskTracks[i].Sectors[s].SectorSize; + } - // sector data - begins at 0x100 offset from the start of the track info block (in this case dpos) - DiskTracks[i].Sectors[s].SectorData = new byte[DiskTracks[i].Sectors[s].ActualDataByteLength]; + // sector data - begins at 0x100 offset from the start of the track info block (in this case dpos) + DiskTracks[i].Sectors[s].SectorData = new byte[DiskTracks[i].Sectors[s].ActualDataByteLength]; - // copy the data - for (int b = 0; b < DiskTracks[i].Sectors[s].ActualDataByteLength; b++) - { - DiskTracks[i].Sectors[s].SectorData[b] = data[dpos + b]; - } + // copy the data + for (int b = 0; b < DiskTracks[i].Sectors[s].ActualDataByteLength; b++) + { + DiskTracks[i].Sectors[s].SectorData[b] = data[dpos + b]; + } - // move dpos to the next sector data postion - dpos += DiskTracks[i].Sectors[s].ActualDataByteLength; - } + // move dpos to the next sector data postion + dpos += DiskTracks[i].Sectors[s].ActualDataByteLength; + } - // move to the next track info block - pos += DiskHeader.TrackSizes[i]; - } + // move to the next track info block + pos += DiskHeader.TrackSizes[i]; + } - // run protection scheme detector - ParseProtection(); + // run protection scheme detector + ParseProtection(); - return true; - } + return true; + } - /// - /// Takes a double-sided disk byte array and converts into 2 single-sided arrays - /// - public static bool SplitDoubleSided(byte[] data, List results) - { - // look for standard magic string - string ident = Encoding.ASCII.GetString(data, 0, 16); - if (!ident.ToUpper().Contains("MV - CPC")) - { - // incorrect format - return false; - } + /// + /// Takes a double-sided disk byte array and converts into 2 single-sided arrays + /// + public static bool SplitDoubleSided(byte[] data, List results) + { + // look for standard magic string + string ident = Encoding.ASCII.GetString(data, 0, 16); + if (!ident.ToUpper().Contains("MV - CPC")) + { + // incorrect format + return false; + } - byte[] S0 = new byte[data.Length]; - byte[] S1 = new byte[data.Length]; + byte[] S0 = new byte[data.Length]; + byte[] S1 = new byte[data.Length]; - // disk info block - Array.Copy(data, 0, S0, 0, 0x100); - Array.Copy(data, 0, S1, 0, 0x100); - // change side number - S0[0x31] = 1; - S1[0x31] = 1; + // disk info block + Array.Copy(data, 0, S0, 0, 0x100); + Array.Copy(data, 0, S1, 0, 0x100); + // change side number + S0[0x31] = 1; + S1[0x31] = 1; - var trkSize = MediaConverter.GetWordValue(data, 0x32); + var trkSize = MediaConverter.GetWordValue(data, 0x32); - // start at track info blocks - int mPos = 0x100; - int s0Pos = 0x100; - int s1Pos = 0x100; + // start at track info blocks + int mPos = 0x100; + int s0Pos = 0x100; + int s1Pos = 0x100; - var numTrks = data[0x30]; - var numSides = data[0x31]; + var numTrks = data[0x30]; + var numSides = data[0x31]; - while (mPos < trkSize * data[0x30] * data[0x31]) - { - // which side is this? - var side = data[mPos + 0x11]; - if (side == 0) - { - // side 1 - Array.Copy(data, mPos, S0, s0Pos, trkSize); - s0Pos += trkSize; - } - else if (side == 1) - { - // side 2 - Array.Copy(data, mPos, S1, s1Pos, trkSize); - s1Pos += trkSize; - } - else - { + while (mPos < trkSize * data[0x30] * data[0x31]) + { + // which side is this? + var side = data[mPos + 0x11]; + if (side == 0) + { + // side 1 + Array.Copy(data, mPos, S0, s0Pos, trkSize); + s0Pos += trkSize; + } + else if (side == 1) + { + // side 2 + Array.Copy(data, mPos, S1, s1Pos, trkSize); + s1Pos += trkSize; + } + else + { - } + } - mPos += trkSize; - } + mPos += trkSize; + } - byte[] s0final = new byte[s0Pos]; - byte[] s1final = new byte[s1Pos]; - Array.Copy(S0, 0, s0final, 0, s0Pos); - Array.Copy(S1, 0, s1final, 0, s1Pos); + byte[] s0final = new byte[s0Pos]; + byte[] s1final = new byte[s1Pos]; + Array.Copy(S0, 0, s0final, 0, s0Pos); + Array.Copy(S1, 0, s1final, 0, s1Pos); - results.Add(s0final); - results.Add(s1final); + results.Add(s0final); + results.Add(s1final); - return true; - } + return true; + } - /// - /// State serlialization - /// - public override void SyncState(Serializer ser) - { - ser.BeginSection("Plus3FloppyDisk"); + /// + /// State serlialization + /// + public override void SyncState(Serializer ser) + { + ser.BeginSection("Plus3FloppyDisk"); - ser.Sync(nameof(CylinderCount), ref CylinderCount); - ser.Sync(nameof(SideCount), ref SideCount); - ser.Sync(nameof(BytesPerTrack), ref BytesPerTrack); - ser.Sync(nameof(WriteProtected), ref WriteProtected); - ser.SyncEnum(nameof(Protection), ref Protection); + ser.Sync(nameof(CylinderCount), ref CylinderCount); + ser.Sync(nameof(SideCount), ref SideCount); + ser.Sync(nameof(BytesPerTrack), ref BytesPerTrack); + ser.Sync(nameof(WriteProtected), ref WriteProtected); + ser.SyncEnum(nameof(Protection), ref Protection); - ser.Sync(nameof(DirtyData), ref DirtyData); - if (DirtyData) - { + ser.Sync(nameof(DirtyData), ref DirtyData); + if (DirtyData) + { - } + } - ser.EndSection(); - } - } + ser.EndSection(); + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/DiskType.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/DiskType.cs index 0adbffd649..d403ec6ffa 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/DiskType.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/DiskType.cs @@ -1,34 +1,34 @@  namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// The different disk formats ZXHawk currently supports - /// - public enum DiskType - { - /// - /// Standard CPCEMU disk format (used in the built-in +3 disk drive) - /// - CPC, + /// + /// The different disk formats ZXHawk currently supports + /// + public enum DiskType + { + /// + /// Standard CPCEMU disk format (used in the built-in +3 disk drive) + /// + CPC, - /// - /// Extended CPCEMU disk format (used in the built-in +3 disk drive) - /// - CPCExtended, + /// + /// Extended CPCEMU disk format (used in the built-in +3 disk drive) + /// + CPCExtended, - /// - /// Interchangeable Preservation Format - /// - IPF, + /// + /// Interchangeable Preservation Format + /// + IPF, - /// - /// Ultra Disk Image Format (v1.0) - /// - UDI, + /// + /// Ultra Disk Image Format (v1.0) + /// + UDI, - /// - /// Ultra Disk Image Format (v1.1) - /// - UDIv1_1 - } + /// + /// Ultra Disk Image Format (v1.1) + /// + UDIv1_1 + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/FloppyDisk.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/FloppyDisk.cs index 63f709ab5b..aaac90489f 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/FloppyDisk.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/FloppyDisk.cs @@ -6,403 +6,403 @@ using System.Text; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// This abstract class defines a logical floppy disk - /// - public abstract class FloppyDisk - { - /// - /// The disk format type - /// - public abstract DiskType DiskFormatType { get; } + /// + /// This abstract class defines a logical floppy disk + /// + public abstract class FloppyDisk + { + /// + /// The disk format type + /// + public abstract DiskType DiskFormatType { get; } - /// - /// Disk information header - /// - public Header DiskHeader = new Header(); + /// + /// Disk information header + /// + public Header DiskHeader = new Header(); - /// - /// Track array - /// - public Track[] DiskTracks = null; + /// + /// Track array + /// + public Track[] DiskTracks = null; - /// - /// No. of tracks per side - /// - public int CylinderCount; - - /// - /// The number of physical sides - /// - public int SideCount; + /// + /// No. of tracks per side + /// + public int CylinderCount; - /// - /// The number of bytes per track - /// - public int BytesPerTrack; + /// + /// The number of physical sides + /// + public int SideCount; - /// - /// The write-protect tab on the disk - /// - public bool WriteProtected; + /// + /// The number of bytes per track + /// + public int BytesPerTrack; - /// - /// The detected protection scheme (if any) - /// - public ProtectionType Protection; + /// + /// The write-protect tab on the disk + /// + public bool WriteProtected; - /// - /// The actual disk image data - /// - public byte[] DiskData; + /// + /// The detected protection scheme (if any) + /// + public ProtectionType Protection; - /// - /// If TRUE then data on the disk has changed (been written to) - /// This will be used to determine whether the disk data needs to be included - /// in any SyncState operations - /// - protected bool DirtyData = false; + /// + /// The actual disk image data + /// + public byte[] DiskData; - /// - /// Used to deterministically choose a 'random' sector when dealing with weak reads - /// - public int RandomCounter - { - get { return _randomCounter; } - set - { - _randomCounter = value; + /// + /// If TRUE then data on the disk has changed (been written to) + /// This will be used to determine whether the disk data needs to be included + /// in any SyncState operations + /// + protected bool DirtyData = false; - foreach (var trk in DiskTracks) - { - foreach (var sec in trk.Sectors) - { - sec.RandSecCounter = _randomCounter; - } - } - } - } - protected int _randomCounter; + /// + /// Used to deterministically choose a 'random' sector when dealing with weak reads + /// + public int RandomCounter + { + get { return _randomCounter; } + set + { + _randomCounter = value; + + foreach (var trk in DiskTracks) + { + foreach (var sec in trk.Sectors) + { + sec.RandSecCounter = _randomCounter; + } + } + } + } + protected int _randomCounter; - /// - /// Attempts to parse incoming disk data - /// - /// - /// TRUE: disk parsed - /// FALSE: unable to parse disk - /// - public virtual bool ParseDisk(byte[] diskData) - { - // default result - // override in inheriting class - return false; - } + /// + /// Attempts to parse incoming disk data + /// + /// + /// TRUE: disk parsed + /// FALSE: unable to parse disk + /// + public virtual bool ParseDisk(byte[] diskData) + { + // default result + // override in inheriting class + return false; + } - /// - /// Examines the floppydisk data to work out what protection (if any) is present - /// If possible it will also fix the disk data for this protection - /// This should be run at the end of the ParseDisk() method - /// - public virtual void ParseProtection() - { - int[] weakArr = new int[2]; + /// + /// Examines the floppydisk data to work out what protection (if any) is present + /// If possible it will also fix the disk data for this protection + /// This should be run at the end of the ParseDisk() method + /// + public virtual void ParseProtection() + { + int[] weakArr = new int[2]; - // speedlock - if (DetectSpeedlock(ref weakArr)) - { - Protection = ProtectionType.Speedlock; + // speedlock + if (DetectSpeedlock(ref weakArr)) + { + Protection = ProtectionType.Speedlock; - Sector sec = DiskTracks[0].Sectors[1]; - if (!sec.ContainsMultipleWeakSectors) - { - byte[] origData = sec.SectorData.ToArray(); - List data = new List(); - for (int m = 0; m < 3; m++) - { - for (int i = 0; i < 512; i++) - { - // deterministic 'random' implementation - int n = origData[i] + m + 1; - if (n > 0xff) - n = n - 0xff; - else if (n < 0) - n = 0xff + n; + Sector sec = DiskTracks[0].Sectors[1]; + if (!sec.ContainsMultipleWeakSectors) + { + byte[] origData = sec.SectorData.ToArray(); + List data = new List(); + for (int m = 0; m < 3; m++) + { + for (int i = 0; i < 512; i++) + { + // deterministic 'random' implementation + int n = origData[i] + m + 1; + if (n > 0xff) + n = n - 0xff; + else if (n < 0) + n = 0xff + n; - byte nByte = (byte)n; + byte nByte = (byte)n; - if (m == 0) - { - data.Add(origData[i]); - continue; - } + if (m == 0) + { + data.Add(origData[i]); + continue; + } - if (i < weakArr[0]) - { - data.Add(origData[i]); - } - - else if (weakArr[1] > 0) - { - data.Add(nByte); - weakArr[1]--; - } - - else - { - data.Add(origData[i]); - } - } - } + if (i < weakArr[0]) + { + data.Add(origData[i]); + } - sec.SectorData = data.ToArray(); - sec.ActualDataByteLength = data.Count(); - sec.ContainsMultipleWeakSectors = true; - } - } - else if (DetectAlkatraz(ref weakArr)) - { - Protection = ProtectionType.Alkatraz; - } - else if (DetectPaulOwens(ref weakArr)) - { - Protection = ProtectionType.PaulOwens; - } - else if (DetectHexagon(ref weakArr)) - { - Protection = ProtectionType.Hexagon; - } - else if (DetectShadowOfTheBeast()) - { - Protection = ProtectionType.ShadowOfTheBeast; - } - } + else if (weakArr[1] > 0) + { + data.Add(nByte); + weakArr[1]--; + } - /// - /// Detection routine for shadow of the beast game - /// Still cannot get this to work, but at least the game is detected - /// - public bool DetectShadowOfTheBeast() - { - if (DiskTracks[0].Sectors.Length != 9) - return false; + else + { + data.Add(origData[i]); + } + } + } - var zeroSecs = DiskTracks[0].Sectors; - if (zeroSecs[0].SectorID != 65 || - zeroSecs[1].SectorID != 66 || - zeroSecs[2].SectorID != 67 || - zeroSecs[3].SectorID != 68 || - zeroSecs[4].SectorID != 69 || - zeroSecs[5].SectorID != 70 || - zeroSecs[6].SectorID != 71 || - zeroSecs[7].SectorID != 72 || - zeroSecs[8].SectorID != 73) - return false; + sec.SectorData = data.ToArray(); + sec.ActualDataByteLength = data.Count(); + sec.ContainsMultipleWeakSectors = true; + } + } + else if (DetectAlkatraz(ref weakArr)) + { + Protection = ProtectionType.Alkatraz; + } + else if (DetectPaulOwens(ref weakArr)) + { + Protection = ProtectionType.PaulOwens; + } + else if (DetectHexagon(ref weakArr)) + { + Protection = ProtectionType.Hexagon; + } + else if (DetectShadowOfTheBeast()) + { + Protection = ProtectionType.ShadowOfTheBeast; + } + } - var oneSecs = DiskTracks[1].Sectors; + /// + /// Detection routine for shadow of the beast game + /// Still cannot get this to work, but at least the game is detected + /// + public bool DetectShadowOfTheBeast() + { + if (DiskTracks[0].Sectors.Length != 9) + return false; - if (oneSecs.Length != 8) - return false; + var zeroSecs = DiskTracks[0].Sectors; + if (zeroSecs[0].SectorID != 65 || + zeroSecs[1].SectorID != 66 || + zeroSecs[2].SectorID != 67 || + zeroSecs[3].SectorID != 68 || + zeroSecs[4].SectorID != 69 || + zeroSecs[5].SectorID != 70 || + zeroSecs[6].SectorID != 71 || + zeroSecs[7].SectorID != 72 || + zeroSecs[8].SectorID != 73) + return false; - if (oneSecs[0].SectorID != 17 || - oneSecs[1].SectorID != 18 || - oneSecs[2].SectorID != 19 || - oneSecs[3].SectorID != 20 || - oneSecs[4].SectorID != 21 || - oneSecs[5].SectorID != 22 || - oneSecs[6].SectorID != 23 || - oneSecs[7].SectorID != 24) - return false; + var oneSecs = DiskTracks[1].Sectors; - return true; - } + if (oneSecs.Length != 8) + return false; - /// - /// Detect speedlock weak sector - /// - public bool DetectSpeedlock(ref int[] weak) - { - // SPEEDLOCK NOTES (-asni 2018-05-01) - // --------------------------------- - // Speedlock is one of the more common +3 disk protections and there are a few different versions - // Usually, track 0 sector 1 (ID 2) has data CRC errors that result in certain bytes returning a different value every time they are read - // Speedlock will generally read this track a number of times during the load process - // and if the correct bytes are not different between reads, the load fails + if (oneSecs[0].SectorID != 17 || + oneSecs[1].SectorID != 18 || + oneSecs[2].SectorID != 19 || + oneSecs[3].SectorID != 20 || + oneSecs[4].SectorID != 21 || + oneSecs[5].SectorID != 22 || + oneSecs[6].SectorID != 23 || + oneSecs[7].SectorID != 24) + return false; - // always must have track 0 containing 9 sectors - if (DiskTracks[0].Sectors.Length != 9) - return false; + return true; + } - // check for SPEEDLOCK ident in sector 0 - string ident = Encoding.ASCII.GetString(DiskTracks[0].Sectors[0].SectorData, 0, DiskTracks[0].Sectors[0].SectorData.Length); - if (!ident.ToUpper().Contains("SPEEDLOCK")) - return false; + /// + /// Detect speedlock weak sector + /// + public bool DetectSpeedlock(ref int[] weak) + { + // SPEEDLOCK NOTES (-asni 2018-05-01) + // --------------------------------- + // Speedlock is one of the more common +3 disk protections and there are a few different versions + // Usually, track 0 sector 1 (ID 2) has data CRC errors that result in certain bytes returning a different value every time they are read + // Speedlock will generally read this track a number of times during the load process + // and if the correct bytes are not different between reads, the load fails - // check for correct sector 0 lengths - if (DiskTracks[0].Sectors[0].SectorSize != 2 || - DiskTracks[0].Sectors[0].SectorData.Length < 0x200) - return false; + // always must have track 0 containing 9 sectors + if (DiskTracks[0].Sectors.Length != 9) + return false; - // sector[1] (SectorID 2) contains the weak sectors - Sector sec = DiskTracks[0].Sectors[1]; + // check for SPEEDLOCK ident in sector 0 + string ident = Encoding.ASCII.GetString(DiskTracks[0].Sectors[0].SectorData, 0, DiskTracks[0].Sectors[0].SectorData.Length); + if (!ident.ToUpper().Contains("SPEEDLOCK")) + return false; - // check for correct sector 1 lengths - if (sec.SectorSize != 2 || - sec.SectorData.Length < 0x200) - return false; + // check for correct sector 0 lengths + if (DiskTracks[0].Sectors[0].SectorSize != 2 || + DiskTracks[0].Sectors[0].SectorData.Length < 0x200) + return false; - // secID 2 needs a CRC error - //if (!(sec.Status1.Bit(5) || sec.Status2.Bit(5))) - //return false; + // sector[1] (SectorID 2) contains the weak sectors + Sector sec = DiskTracks[0].Sectors[1]; - // check for filler - bool startFillerFound = true; - for (int i = 0; i < 250; i++) - { - if (sec.SectorData[i] != sec.SectorData[i + 1]) - { - startFillerFound = false; - break; - } - } + // check for correct sector 1 lengths + if (sec.SectorSize != 2 || + sec.SectorData.Length < 0x200) + return false; - if (!startFillerFound) - { - weak[0] = 0; - weak[1] = 0x200; - } - else - { - weak[0] = 0x150; - weak[1] = 0x20; - } + // secID 2 needs a CRC error + //if (!(sec.Status1.Bit(5) || sec.Status2.Bit(5))) + //return false; - return true; - } + // check for filler + bool startFillerFound = true; + for (int i = 0; i < 250; i++) + { + if (sec.SectorData[i] != sec.SectorData[i + 1]) + { + startFillerFound = false; + break; + } + } - /// - /// Detect Alkatraz - /// - public bool DetectAlkatraz(ref int[] weak) - { - try - { - var data1 = DiskTracks[0].Sectors[0].SectorData; - var data2 = DiskTracks[0].Sectors[0].SectorData.Length; - } - catch (Exception) - { - return false; - } + if (!startFillerFound) + { + weak[0] = 0; + weak[1] = 0x200; + } + else + { + weak[0] = 0x150; + weak[1] = 0x20; + } - // check for ALKATRAZ ident in sector 0 - string ident = Encoding.ASCII.GetString(DiskTracks[0].Sectors[0].SectorData, 0, DiskTracks[0].Sectors[0].SectorData.Length); - if (!ident.ToUpper().Contains("ALKATRAZ PROTECTION SYSTEM")) - return false; + return true; + } - // ALKATRAZ NOTES (-asni 2018-05-01) - // --------------------------------- - // Alkatraz protection appears to revolve around a track on the disk with 18 sectors, - // (track position is not consistent) with the sector ID info being incorrect: - // TrackID is consistent between the sectors although is usually high (233, 237 etc) - // SideID is fairly random looking but with all IDs being even - // SectorID is also fairly random looking but contains both odd and even numbers - // - // There doesnt appear to be any CRC errors in this track, but the sector size is always 1 (256 bytes) - // Each sector contains different filler byte - // Once track 0 is loaded the CPU completely reads all the sectors in this track one-by-one. - // Data transferred during execution must be correct, also result ST0, ST1 and ST2 must be 64, 128 and 0 respectively + /// + /// Detect Alkatraz + /// + public bool DetectAlkatraz(ref int[] weak) + { + try + { + var data1 = DiskTracks[0].Sectors[0].SectorData; + var data2 = DiskTracks[0].Sectors[0].SectorData.Length; + } + catch (Exception) + { + return false; + } - // Immediately following this track are a number of tracks and sectors with a DAM set. - // These are all read in sector by sector - // Again, Alkatraz appears to require that ST0, ST1, and ST2 result bytes are set to 64, 128 and 0 respectively - // (so the CM in ST2 needs to be reset) + // check for ALKATRAZ ident in sector 0 + string ident = Encoding.ASCII.GetString(DiskTracks[0].Sectors[0].SectorData, 0, DiskTracks[0].Sectors[0].SectorData.Length); + if (!ident.ToUpper().Contains("ALKATRAZ PROTECTION SYSTEM")) + return false; - return true; - } + // ALKATRAZ NOTES (-asni 2018-05-01) + // --------------------------------- + // Alkatraz protection appears to revolve around a track on the disk with 18 sectors, + // (track position is not consistent) with the sector ID info being incorrect: + // TrackID is consistent between the sectors although is usually high (233, 237 etc) + // SideID is fairly random looking but with all IDs being even + // SectorID is also fairly random looking but contains both odd and even numbers + // + // There doesnt appear to be any CRC errors in this track, but the sector size is always 1 (256 bytes) + // Each sector contains different filler byte + // Once track 0 is loaded the CPU completely reads all the sectors in this track one-by-one. + // Data transferred during execution must be correct, also result ST0, ST1 and ST2 must be 64, 128 and 0 respectively - /// - /// Detect Paul Owens - /// - public bool DetectPaulOwens(ref int[] weak) - { - try - { - var data1 = DiskTracks[0].Sectors[2].SectorData; - var data2 = DiskTracks[0].Sectors[2].SectorData.Length; - } - catch (Exception) - { - return false; - } + // Immediately following this track are a number of tracks and sectors with a DAM set. + // These are all read in sector by sector + // Again, Alkatraz appears to require that ST0, ST1, and ST2 result bytes are set to 64, 128 and 0 respectively + // (so the CM in ST2 needs to be reset) - // check for PAUL OWENS ident in sector 2 - string ident = Encoding.ASCII.GetString(DiskTracks[0].Sectors[2].SectorData, 0, DiskTracks[0].Sectors[2].SectorData.Length); - if (!ident.ToUpper().Contains("PAUL OWENS")) - return false; + return true; + } - // Paul Owens Disk Protection Notes (-asni 2018-05-01) - // --------------------------------------------------- - // - // This scheme looks a little similar to Alkatraz with incorrect sector ID info in many places - // and deleted address marks (although these do not seem to show the strict relience on removing the CM mark from ST2 result that Alkatraz does) - // There are also data CRC errors but these dont look to be read more than once/checked for changes during load - // Main identifiers: - // - // * There are more than 10 cylinders - // * Cylinder 1 has no sector data - // * The sector ID infomation in most cases contains incorrect track IDs - // * Tracks 0 (boot) and 5 appear to be pretty much the only tracks that do not have incorrect sector ID marks + /// + /// Detect Paul Owens + /// + public bool DetectPaulOwens(ref int[] weak) + { + try + { + var data1 = DiskTracks[0].Sectors[2].SectorData; + var data2 = DiskTracks[0].Sectors[2].SectorData.Length; + } + catch (Exception) + { + return false; + } - return true; - } + // check for PAUL OWENS ident in sector 2 + string ident = Encoding.ASCII.GetString(DiskTracks[0].Sectors[2].SectorData, 0, DiskTracks[0].Sectors[2].SectorData.Length); + if (!ident.ToUpper().Contains("PAUL OWENS")) + return false; - /// - /// Detect Hexagon copy protection - /// - public bool DetectHexagon(ref int[] weak) - { - try - { - var data1 = DiskTracks[0].Sectors.Length; - var data2 = DiskTracks[0].Sectors[8].ActualDataByteLength; - var data3 = DiskTracks[0].Sectors[8].SectorData; - var data4 = DiskTracks[0].Sectors[8].SectorData.Length; - var data5 = DiskTracks[1].Sectors[0]; - } - catch (Exception) - { - return false; - } + // Paul Owens Disk Protection Notes (-asni 2018-05-01) + // --------------------------------------------------- + // + // This scheme looks a little similar to Alkatraz with incorrect sector ID info in many places + // and deleted address marks (although these do not seem to show the strict relience on removing the CM mark from ST2 result that Alkatraz does) + // There are also data CRC errors but these dont look to be read more than once/checked for changes during load + // Main identifiers: + // + // * There are more than 10 cylinders + // * Cylinder 1 has no sector data + // * The sector ID infomation in most cases contains incorrect track IDs + // * Tracks 0 (boot) and 5 appear to be pretty much the only tracks that do not have incorrect sector ID marks - if (DiskTracks[0].Sectors.Length != 10 || DiskTracks[0].Sectors[8].ActualDataByteLength != 512) - return false; + return true; + } - // check for Hexagon ident in sector 8 - string ident = Encoding.ASCII.GetString(DiskTracks[0].Sectors[8].SectorData, 0, DiskTracks[0].Sectors[8].SectorData.Length); - if (ident.ToUpper().Contains("GON DISK PROT")) - return true; + /// + /// Detect Hexagon copy protection + /// + public bool DetectHexagon(ref int[] weak) + { + try + { + var data1 = DiskTracks[0].Sectors.Length; + var data2 = DiskTracks[0].Sectors[8].ActualDataByteLength; + var data3 = DiskTracks[0].Sectors[8].SectorData; + var data4 = DiskTracks[0].Sectors[8].SectorData.Length; + var data5 = DiskTracks[1].Sectors[0]; + } + catch (Exception) + { + return false; + } - // hexagon protection may not be labelled as such - var track = DiskTracks[1]; - var sector = track.Sectors[0]; + if (DiskTracks[0].Sectors.Length != 10 || DiskTracks[0].Sectors[8].ActualDataByteLength != 512) + return false; - if (sector.SectorSize == 6 && sector.Status1 == 0x20 && sector.Status2 == 0x60) - { - if (track.Sectors.Length == 1) - return true; - } + // check for Hexagon ident in sector 8 + string ident = Encoding.ASCII.GetString(DiskTracks[0].Sectors[8].SectorData, 0, DiskTracks[0].Sectors[8].SectorData.Length); + if (ident.ToUpper().Contains("GON DISK PROT")) + return true; + + // hexagon protection may not be labelled as such + var track = DiskTracks[1]; + var sector = track.Sectors[0]; + + if (sector.SectorSize == 6 && sector.Status1 == 0x20 && sector.Status2 == 0x60) + { + if (track.Sectors.Length == 1) + return true; + } - // Hexagon Copy Protection Notes (-asni 2018-05-01) - // --------------------------------------------------- - // - // + // Hexagon Copy Protection Notes (-asni 2018-05-01) + // --------------------------------------------------- + // + // - return false; - } + return false; + } - /* + /* /// /// Should be run at the end of the ParseDisk process /// If speedlock is detected the flag is set in the disk image @@ -501,202 +501,202 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum } */ - /// - /// Returns the track count for the disk - /// - public virtual int GetTrackCount() - { - return DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides; - } + /// + /// Returns the track count for the disk + /// + public virtual int GetTrackCount() + { + return DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides; + } - /// - /// Reads the current sector ID info - /// - public virtual CHRN ReadID(byte trackIndex, byte side, int sectorIndex) - { - if (side != 0) - return null; + /// + /// Reads the current sector ID info + /// + public virtual CHRN ReadID(byte trackIndex, byte side, int sectorIndex) + { + if (side != 0) + return null; - if (DiskTracks.Length <= trackIndex || trackIndex < 0) - { - // invalid track - wrap around - trackIndex = 0; - } + if (DiskTracks.Length <= trackIndex || trackIndex < 0) + { + // invalid track - wrap around + trackIndex = 0; + } - var track = DiskTracks[trackIndex]; + var track = DiskTracks[trackIndex]; - if (track.NumberOfSectors <= sectorIndex) - { - // invalid sector - wrap around - sectorIndex = 0; - } + if (track.NumberOfSectors <= sectorIndex) + { + // invalid sector - wrap around + sectorIndex = 0; + } - var sector = track.Sectors[sectorIndex]; + var sector = track.Sectors[sectorIndex]; - CHRN chrn = new CHRN(); + CHRN chrn = new CHRN(); - chrn.C = sector.TrackNumber; - chrn.H = sector.SideNumber; - chrn.R = sector.SectorID; + chrn.C = sector.TrackNumber; + chrn.H = sector.SideNumber; + chrn.R = sector.SectorID; - // wrap around for N > 7 - if (sector.SectorSize > 7) - { - chrn.N = (byte)(sector.SectorSize - 7); - } - else if (sector.SectorSize < 0) - { - chrn.N = 0; - } - else - { - chrn.N = sector.SectorSize; - } + // wrap around for N > 7 + if (sector.SectorSize > 7) + { + chrn.N = (byte)(sector.SectorSize - 7); + } + else if (sector.SectorSize < 0) + { + chrn.N = 0; + } + else + { + chrn.N = sector.SectorSize; + } - chrn.Flag1 = (byte)(sector.Status1 & 0x25); - chrn.Flag2 = (byte)(sector.Status2 & 0x61); + chrn.Flag1 = (byte)(sector.Status1 & 0x25); + chrn.Flag2 = (byte)(sector.Status2 & 0x61); - chrn.DataBytes = sector.ActualData; + chrn.DataBytes = sector.ActualData; - return chrn; - } + return chrn; + } - /// - /// State serialization routines - /// - public abstract void SyncState(Serializer ser); + /// + /// State serialization routines + /// + public abstract void SyncState(Serializer ser); - public class Header - { - public string DiskIdent { get; set; } - public string DiskCreatorString { get; set; } - public byte NumberOfTracks { get; set; } - public byte NumberOfSides { get; set; } - public int[] TrackSizes { get; set; } - } + public class Header + { + public string DiskIdent { get; set; } + public string DiskCreatorString { get; set; } + public byte NumberOfTracks { get; set; } + public byte NumberOfSides { get; set; } + public int[] TrackSizes { get; set; } + } - public class Track - { - public string TrackIdent { get; set; } - public byte TrackNumber { get; set; } - public byte SideNumber { get; set; } - public byte DataRate { get; set; } - public byte RecordingMode { get; set; } - public byte SectorSize { get; set; } - public byte NumberOfSectors { get; set; } - public byte GAP3Length { get; set; } - public byte FillerByte { get; set; } - public virtual Sector[] Sectors { get; set; } + public class Track + { + public string TrackIdent { get; set; } + public byte TrackNumber { get; set; } + public byte SideNumber { get; set; } + public byte DataRate { get; set; } + public byte RecordingMode { get; set; } + public byte SectorSize { get; set; } + public byte NumberOfSectors { get; set; } + public byte GAP3Length { get; set; } + public byte FillerByte { get; set; } + public virtual Sector[] Sectors { get; set; } - #region UDI + #region UDI - public virtual byte TrackType { get; set; } - public virtual int TLEN { get; set; } - public virtual int CLEN { get { return TLEN / 8 + (TLEN % 8 / 7) / 8; } } - public virtual byte[] TrackData { get; set; } + public virtual byte TrackType { get; set; } + public virtual int TLEN { get; set; } + public virtual int CLEN { get { return TLEN / 8 + (TLEN % 8 / 7) / 8; } } + public virtual byte[] TrackData { get; set; } - #endregion + #endregion - /// - /// Presents a contiguous byte array of all sector data for this track - /// (including any multiple weak/random data) - /// - public virtual byte[] TrackSectorData - { - get - { - List list = new List(); + /// + /// Presents a contiguous byte array of all sector data for this track + /// (including any multiple weak/random data) + /// + public virtual byte[] TrackSectorData + { + get + { + List list = new List(); - foreach (var sec in Sectors) - { - list.AddRange(sec.ActualData); - } + foreach (var sec in Sectors) + { + list.AddRange(sec.ActualData); + } - return list.ToArray(); - } - } - } + return list.ToArray(); + } + } + } - public class Sector - { - public virtual byte TrackNumber { get; set; } - public virtual byte SideNumber { get; set; } - public virtual byte SectorID { get; set; } - public virtual byte SectorSize { get; set; } - public virtual byte Status1 { get; set; } - public virtual byte Status2 { get; set; } - public virtual int ActualDataByteLength { get; set; } - public virtual byte[] SectorData { get; set; } - public virtual bool ContainsMultipleWeakSectors { get; set; } + public class Sector + { + public virtual byte TrackNumber { get; set; } + public virtual byte SideNumber { get; set; } + public virtual byte SectorID { get; set; } + public virtual byte SectorSize { get; set; } + public virtual byte Status1 { get; set; } + public virtual byte Status2 { get; set; } + public virtual int ActualDataByteLength { get; set; } + public virtual byte[] SectorData { get; set; } + public virtual bool ContainsMultipleWeakSectors { get; set; } - public int WeakReadIndex = 0; + public int WeakReadIndex = 0; - public void SectorReadCompleted() - { - if (ContainsMultipleWeakSectors) - WeakReadIndex++; - } + public void SectorReadCompleted() + { + if (ContainsMultipleWeakSectors) + WeakReadIndex++; + } - public int DataLen - { - get - { - if (!ContainsMultipleWeakSectors) - { - return ActualDataByteLength; - } - else - { - return ActualDataByteLength / (ActualDataByteLength / (0x80 << SectorSize)); - } - } - } + public int DataLen + { + get + { + if (!ContainsMultipleWeakSectors) + { + return ActualDataByteLength; + } + else + { + return ActualDataByteLength / (ActualDataByteLength / (0x80 << SectorSize)); + } + } + } - public int RandSecCounter = 0; + public int RandSecCounter = 0; - public byte[] ActualData - { - get - { - if (!ContainsMultipleWeakSectors) - { - // check whether filler bytes are needed - int size = 0x80 << SectorSize; - if (size > ActualDataByteLength) - { - List l = new List(); - l.AddRange(SectorData); - for (int i = 0; i < size - ActualDataByteLength; i++) - { - //l.Add(SectorData[i]); - l.Add(SectorData.Last()); - } + public byte[] ActualData + { + get + { + if (!ContainsMultipleWeakSectors) + { + // check whether filler bytes are needed + int size = 0x80 << SectorSize; + if (size > ActualDataByteLength) + { + List l = new List(); + l.AddRange(SectorData); + for (int i = 0; i < size - ActualDataByteLength; i++) + { + //l.Add(SectorData[i]); + l.Add(SectorData.Last()); + } - return l.ToArray(); - } - else - { - return SectorData; - } - } - else - { - // weak read neccessary - int copies = ActualDataByteLength / (0x80 << SectorSize); + return l.ToArray(); + } + else + { + return SectorData; + } + } + else + { + // weak read neccessary + int copies = ActualDataByteLength / (0x80 << SectorSize); - // handle index wrap-around - if (WeakReadIndex > copies - 1) - WeakReadIndex = copies - 1; + // handle index wrap-around + if (WeakReadIndex > copies - 1) + WeakReadIndex = copies - 1; - // get the sector data based on the current weakreadindex - int step = WeakReadIndex * (0x80 << SectorSize); - byte[] res = new byte[(0x80 << SectorSize)]; - Array.Copy(SectorData, step, res, 0, 0x80 << SectorSize); - return res; + // get the sector data based on the current weakreadindex + int step = WeakReadIndex * (0x80 << SectorSize); + byte[] res = new byte[(0x80 << SectorSize)]; + Array.Copy(SectorData, step, res, 0, 0x80 << SectorSize); + return res; - /* + /* int copies = ActualDataByteLength / (0x80 << SectorSize); Random rnd = new Random(); int r = rnd.Next(0, copies - 1); @@ -705,40 +705,40 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum Array.Copy(SectorData, step, res, 0, 0x80 << SectorSize); return res; */ - } - } - } + } + } + } - public CHRN SectorIDInfo - { - get - { - return new CHRN - { - C = TrackNumber, - H = SideNumber, - R = SectorID, - N = SectorSize, - Flag1 = Status1, - Flag2 = Status2, - }; - } - } - } - } + public CHRN SectorIDInfo + { + get + { + return new CHRN + { + C = TrackNumber, + H = SideNumber, + R = SectorID, + N = SectorSize, + Flag1 = Status1, + Flag2 = Status2, + }; + } + } + } + } - /// - /// Defines the type of speedlock detection found - /// - public enum ProtectionType - { - None, - Speedlock, - Alkatraz, - Hexagon, - Frontier, - PaulOwens, - ShadowOfTheBeast - } + /// + /// Defines the type of speedlock detection found + /// + public enum ProtectionType + { + None, + Speedlock, + Alkatraz, + Hexagon, + Frontier, + PaulOwens, + ShadowOfTheBeast + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/IPFFormat/IPFFloppyDisk.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/IPFFormat/IPFFloppyDisk.cs index 8133e7a66b..bcf51ca14e 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/IPFFormat/IPFFloppyDisk.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/IPFFormat/IPFFloppyDisk.cs @@ -8,452 +8,452 @@ using System.Threading.Tasks; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - public class IPFFloppyDisk : FloppyDisk - { - /// - /// The format type - /// - public override DiskType DiskFormatType => DiskType.IPF; + public class IPFFloppyDisk : FloppyDisk + { + /// + /// The format type + /// + public override DiskType DiskFormatType => DiskType.IPF; - /// - /// Attempts to parse incoming disk data - /// - /// - /// TRUE: disk parsed - /// FALSE: unable to parse disk - /// - public override bool ParseDisk(byte[] data) - { - // look for standard magic string - string ident = Encoding.ASCII.GetString(data, 0, 16); + /// + /// Attempts to parse incoming disk data + /// + /// + /// TRUE: disk parsed + /// FALSE: unable to parse disk + /// + public override bool ParseDisk(byte[] data) + { + // look for standard magic string + string ident = Encoding.ASCII.GetString(data, 0, 16); - if (!ident.ToUpper().Contains("CAPS")) - { - // incorrect format - return false; - } + if (!ident.ToUpper().Contains("CAPS")) + { + // incorrect format + return false; + } - int pos = 0; + int pos = 0; - List blocks = new List(); + List blocks = new List(); - while (pos < data.Length) - { - try - { - var block = IPFBlock.ParseNextBlock(ref pos, this, data, blocks); + while (pos < data.Length) + { + try + { + var block = IPFBlock.ParseNextBlock(ref pos, this, data, blocks); - if (block == null) - { - // EOF - break; - } + if (block == null) + { + // EOF + break; + } - if (block.RecordType == RecordHeaderType.None) - { - // unknown block - } + if (block.RecordType == RecordHeaderType.None) + { + // unknown block + } - blocks.Add(block); - } - catch (Exception ex) - { + blocks.Add(block); + } + catch (Exception ex) + { var e = ex.ToString(); - } - } + } + } - // now process the blocks - var infoBlock = blocks.Where(a => a.RecordType == RecordHeaderType.INFO).FirstOrDefault(); - var IMGEblocks = blocks.Where(a => a.RecordType == RecordHeaderType.IMGE).ToList(); - var DATAblocks = blocks.Where(a => a.RecordType == RecordHeaderType.DATA); + // now process the blocks + var infoBlock = blocks.Where(a => a.RecordType == RecordHeaderType.INFO).FirstOrDefault(); + var IMGEblocks = blocks.Where(a => a.RecordType == RecordHeaderType.IMGE).ToList(); + var DATAblocks = blocks.Where(a => a.RecordType == RecordHeaderType.DATA); - DiskHeader.NumberOfTracks = (byte)(IMGEblocks.Count()); - DiskHeader.NumberOfSides = (byte)(infoBlock.INFOmaxSide + 1); - DiskTracks = new Track[DiskHeader.NumberOfTracks]; + DiskHeader.NumberOfTracks = (byte)(IMGEblocks.Count()); + DiskHeader.NumberOfSides = (byte)(infoBlock.INFOmaxSide + 1); + DiskTracks = new Track[DiskHeader.NumberOfTracks]; - for (int t = 0; t < DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides; t++) - { - // each imge block represents one track - var img = IMGEblocks[t]; - DiskTracks[t] = new Track(); - var trk = DiskTracks[t]; + for (int t = 0; t < DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides; t++) + { + // each imge block represents one track + var img = IMGEblocks[t]; + DiskTracks[t] = new Track(); + var trk = DiskTracks[t]; - var blockCount = img.IMGEblockCount; - var dataBlock = DATAblocks.Where(a => a.DATAdataKey == img.IMGEdataKey).FirstOrDefault(); + var blockCount = img.IMGEblockCount; + var dataBlock = DATAblocks.Where(a => a.DATAdataKey == img.IMGEdataKey).FirstOrDefault(); - trk.SideNumber = (byte)img.IMGEside; - trk.TrackNumber = (byte)img.IMGEtrack; + trk.SideNumber = (byte)img.IMGEside; + trk.TrackNumber = (byte)img.IMGEtrack; - trk.Sectors = new Sector[blockCount]; + trk.Sectors = new Sector[blockCount]; - // process data block descriptors - int p = 0; - for (int d = 0; d < blockCount; d++) - { - var extraDataAreaStart = 32 * blockCount; - trk.Sectors[d] = new Sector(); - var sector = trk.Sectors[d]; + // process data block descriptors + int p = 0; + for (int d = 0; d < blockCount; d++) + { + var extraDataAreaStart = 32 * blockCount; + trk.Sectors[d] = new Sector(); + var sector = trk.Sectors[d]; - int dataBits = MediaConverter.GetBEInt32(dataBlock.DATAextraDataRaw, p); p += 4; - int gapBits = MediaConverter.GetBEInt32(dataBlock.DATAextraDataRaw, p); p += 4; - int dataBytes; - int gapBytes; - int gapOffset; - int cellType; - if (infoBlock.INFOencoderType == 1) - { - dataBytes = MediaConverter.GetBEInt32(dataBlock.DATAextraDataRaw, p); p += 4; - gapBytes = MediaConverter.GetBEInt32(dataBlock.DATAextraDataRaw, p); p += 4; - } - else if (infoBlock.INFOencoderType == 2) - { - gapOffset = MediaConverter.GetBEInt32(dataBlock.DATAextraDataRaw, p); p += 4; - cellType = MediaConverter.GetBEInt32(dataBlock.DATAextraDataRaw, p); p += 4; - } - int encoderType = MediaConverter.GetBEInt32(dataBlock.DATAextraDataRaw, p); p += 4; - int? blockFlags = null; - if (infoBlock.INFOencoderType == 2) - { - blockFlags = MediaConverter.GetBEInt32(dataBlock.DATAextraDataRaw, p); - } - p += 4; + int dataBits = MediaConverter.GetBEInt32(dataBlock.DATAextraDataRaw, p); p += 4; + int gapBits = MediaConverter.GetBEInt32(dataBlock.DATAextraDataRaw, p); p += 4; + int dataBytes; + int gapBytes; + int gapOffset; + int cellType; + if (infoBlock.INFOencoderType == 1) + { + dataBytes = MediaConverter.GetBEInt32(dataBlock.DATAextraDataRaw, p); p += 4; + gapBytes = MediaConverter.GetBEInt32(dataBlock.DATAextraDataRaw, p); p += 4; + } + else if (infoBlock.INFOencoderType == 2) + { + gapOffset = MediaConverter.GetBEInt32(dataBlock.DATAextraDataRaw, p); p += 4; + cellType = MediaConverter.GetBEInt32(dataBlock.DATAextraDataRaw, p); p += 4; + } + int encoderType = MediaConverter.GetBEInt32(dataBlock.DATAextraDataRaw, p); p += 4; + int? blockFlags = null; + if (infoBlock.INFOencoderType == 2) + { + blockFlags = MediaConverter.GetBEInt32(dataBlock.DATAextraDataRaw, p); + } + p += 4; - int gapDefault = MediaConverter.GetBEInt32(dataBlock.DATAextraDataRaw, p); p += 4; - int dataOffset = MediaConverter.GetBEInt32(dataBlock.DATAextraDataRaw, p); p += 4; + int gapDefault = MediaConverter.GetBEInt32(dataBlock.DATAextraDataRaw, p); p += 4; + int dataOffset = MediaConverter.GetBEInt32(dataBlock.DATAextraDataRaw, p); p += 4; - // gap stream elements - if (infoBlock.INFOencoderType == 2 && gapBits != 0 && blockFlags != null) - { - if (!blockFlags.Value.Bit(1) && !blockFlags.Value.Bit(0)) - { - // no gap stream - } - if (!blockFlags.Value.Bit(1) && blockFlags.Value.Bit(0)) - { - // Forward gap stream list only - } - if (blockFlags.Value.Bit(1) && !blockFlags.Value.Bit(0)) - { - // Backward gap stream list only - } - if (blockFlags.Value.Bit(1) && blockFlags.Value.Bit(0)) - { - // Forward and Backward stream lists - } - } + // gap stream elements + if (infoBlock.INFOencoderType == 2 && gapBits != 0 && blockFlags != null) + { + if (!blockFlags.Value.Bit(1) && !blockFlags.Value.Bit(0)) + { + // no gap stream + } + if (!blockFlags.Value.Bit(1) && blockFlags.Value.Bit(0)) + { + // Forward gap stream list only + } + if (blockFlags.Value.Bit(1) && !blockFlags.Value.Bit(0)) + { + // Backward gap stream list only + } + if (blockFlags.Value.Bit(1) && blockFlags.Value.Bit(0)) + { + // Forward and Backward stream lists + } + } - // data stream elements - if (dataBits != 0) - { - var dsLocation = dataOffset; + // data stream elements + if (dataBits != 0) + { + var dsLocation = dataOffset; - for (;;) - { - byte dataHead = dataBlock.DATAextraDataRaw[dsLocation++]; - if (dataHead == 0) - { - // end of data stream list - break; - } + for (; ; ) + { + byte dataHead = dataBlock.DATAextraDataRaw[dsLocation++]; + if (dataHead == 0) + { + // end of data stream list + break; + } - var sampleSize = ((dataHead & 0xE0) >> 5); - var dataType = dataHead & 0x1F; - byte[] dSize = new byte[sampleSize]; - Array.Copy(dataBlock.DATAextraDataRaw, dsLocation, dSize, 0, sampleSize); - var dataSize = MediaConverter.GetBEInt32FromByteArray(dSize); - dsLocation += dSize.Length; - int dataLen; - byte[] dataStream = new byte[0]; + var sampleSize = ((dataHead & 0xE0) >> 5); + var dataType = dataHead & 0x1F; + byte[] dSize = new byte[sampleSize]; + Array.Copy(dataBlock.DATAextraDataRaw, dsLocation, dSize, 0, sampleSize); + var dataSize = MediaConverter.GetBEInt32FromByteArray(dSize); + dsLocation += dSize.Length; + int dataLen; + byte[] dataStream = new byte[0]; - if (blockFlags != null && blockFlags.Value.Bit(2)) - { - // bits - if (dataType != 5) - { - dataLen = dataSize / 8; - if (dataSize % 8 != 0) - { - // bits left over - } - dataStream = new byte[dataLen]; - Array.Copy(dataBlock.DATAextraDataRaw, dsLocation, dataStream, 0, dataLen); - } - } - else - { - // bytes - if (dataType != 5) - { - dataStream = new byte[dataSize]; - Array.Copy(dataBlock.DATAextraDataRaw, dsLocation, dataStream, 0, dataSize); - } - } + if (blockFlags != null && blockFlags.Value.Bit(2)) + { + // bits + if (dataType != 5) + { + dataLen = dataSize / 8; + if (dataSize % 8 != 0) + { + // bits left over + } + dataStream = new byte[dataLen]; + Array.Copy(dataBlock.DATAextraDataRaw, dsLocation, dataStream, 0, dataLen); + } + } + else + { + // bytes + if (dataType != 5) + { + dataStream = new byte[dataSize]; + Array.Copy(dataBlock.DATAextraDataRaw, dsLocation, dataStream, 0, dataSize); + } + } - // dataStream[] now contains the data - switch (dataType) - { - // SYNC - case 1: - break; - // DATA - case 2: - if (dataStream.Length == 7) - { - // ID - // first byte IAM - sector.TrackNumber = dataStream[1]; - sector.SideNumber = dataStream[2]; - sector.SectorID = dataStream[3]; - sector.SectorSize = dataStream[4]; - } - else if (dataStream.Length > 255) - { - // DATA - // first byte DAM - if (dataStream[0] == 0xF8) - { - // deleted address mark - //sector.Status1 - } - sector.SectorData = new byte[dataStream.Length - 1 - 2]; - Array.Copy(dataStream, 1, sector.SectorData, 0, dataStream.Length - 1 - 2); - } - break; - // GAP - case 3: - break; - // RAW - case 4: - break; - // FUZZY - case 5: - break; - default: - break; - } + // dataStream[] now contains the data + switch (dataType) + { + // SYNC + case 1: + break; + // DATA + case 2: + if (dataStream.Length == 7) + { + // ID + // first byte IAM + sector.TrackNumber = dataStream[1]; + sector.SideNumber = dataStream[2]; + sector.SectorID = dataStream[3]; + sector.SectorSize = dataStream[4]; + } + else if (dataStream.Length > 255) + { + // DATA + // first byte DAM + if (dataStream[0] == 0xF8) + { + // deleted address mark + //sector.Status1 + } + sector.SectorData = new byte[dataStream.Length - 1 - 2]; + Array.Copy(dataStream, 1, sector.SectorData, 0, dataStream.Length - 1 - 2); + } + break; + // GAP + case 3: + break; + // RAW + case 4: + break; + // FUZZY + case 5: + break; + default: + break; + } - dsLocation += dataStream.Length; - } - } - } - } + dsLocation += dataStream.Length; + } + } + } + } - return true; - } + return true; + } - public class IPFBlock - { - public RecordHeaderType RecordType; - public int BlockLength; - public int CRC; - public byte[] RawBlockData; - public int StartPos; + public class IPFBlock + { + public RecordHeaderType RecordType; + public int BlockLength; + public int CRC; + public byte[] RawBlockData; + public int StartPos; - #region INFO + #region INFO - public int INFOmediaType; - public int INFOencoderType; - public int INFOencoderRev; - public int INFOfileKey; - public int INFOfileRev; - public int INFOorigin; - public int INFOminTrack; - public int INFOmaxTrack; - public int INFOminSide; - public int INFOmaxSide; - public int INFOcreationDate; - public int INFOcreationTime; - public int INFOplatform1; - public int INFOplatform2; - public int INFOplatform3; - public int INFOplatform4; - public int INFOdiskNumber; - public int INFOcreatorId; + public int INFOmediaType; + public int INFOencoderType; + public int INFOencoderRev; + public int INFOfileKey; + public int INFOfileRev; + public int INFOorigin; + public int INFOminTrack; + public int INFOmaxTrack; + public int INFOminSide; + public int INFOmaxSide; + public int INFOcreationDate; + public int INFOcreationTime; + public int INFOplatform1; + public int INFOplatform2; + public int INFOplatform3; + public int INFOplatform4; + public int INFOdiskNumber; + public int INFOcreatorId; - #endregion + #endregion - #region IMGE + #region IMGE - public int IMGEtrack; - public int IMGEside; - public int IMGEdensity; - public int IMGEsignalType; - public int IMGEtrackBytes; - public int IMGEstartBytePos; - public int IMGEstartBitPos; - public int IMGEdataBits; - public int IMGEgapBits; - public int IMGEtrackBits; - public int IMGEblockCount; - public int IMGEencoderProcess; - public int IMGEtrackFlags; - public int IMGEdataKey; + public int IMGEtrack; + public int IMGEside; + public int IMGEdensity; + public int IMGEsignalType; + public int IMGEtrackBytes; + public int IMGEstartBytePos; + public int IMGEstartBitPos; + public int IMGEdataBits; + public int IMGEgapBits; + public int IMGEtrackBits; + public int IMGEblockCount; + public int IMGEencoderProcess; + public int IMGEtrackFlags; + public int IMGEdataKey; - #endregion + #endregion - #region DATA + #region DATA - public int DATAlength; - public int DATAbitSize; - public int DATAcrc; - public int DATAdataKey; - public byte[] DATAextraDataRaw; + public int DATAlength; + public int DATAbitSize; + public int DATAcrc; + public int DATAdataKey; + public byte[] DATAextraDataRaw; - #endregion + #endregion - public static IPFBlock ParseNextBlock(ref int startPos, FloppyDisk disk, byte[] data, List blockCollection) - { - IPFBlock ipf = new IPFBlock(); - ipf.StartPos = startPos; + public static IPFBlock ParseNextBlock(ref int startPos, FloppyDisk disk, byte[] data, List blockCollection) + { + IPFBlock ipf = new IPFBlock(); + ipf.StartPos = startPos; - if (startPos >= data.Length) - { - // EOF - return null; - } + if (startPos >= data.Length) + { + // EOF + return null; + } - // assume the startPos passed in is actually the start of a new block - // look for record header ident - string ident = Encoding.ASCII.GetString(data, startPos, 4); - startPos += 4; - try - { - ipf.RecordType = (RecordHeaderType)Enum.Parse(typeof(RecordHeaderType), ident); - } - catch - { - ipf.RecordType = RecordHeaderType.None; - } + // assume the startPos passed in is actually the start of a new block + // look for record header ident + string ident = Encoding.ASCII.GetString(data, startPos, 4); + startPos += 4; + try + { + ipf.RecordType = (RecordHeaderType)Enum.Parse(typeof(RecordHeaderType), ident); + } + catch + { + ipf.RecordType = RecordHeaderType.None; + } - // setup for actual block size - ipf.BlockLength = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.CRC = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.RawBlockData = new byte[ipf.BlockLength]; - Array.Copy(data, ipf.StartPos, ipf.RawBlockData, 0, ipf.BlockLength); + // setup for actual block size + ipf.BlockLength = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.CRC = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.RawBlockData = new byte[ipf.BlockLength]; + Array.Copy(data, ipf.StartPos, ipf.RawBlockData, 0, ipf.BlockLength); - switch (ipf.RecordType) - { - // Nothing to process / unknown - // just move ahead - case RecordHeaderType.CAPS: - case RecordHeaderType.TRCK: - case RecordHeaderType.DUMP: - case RecordHeaderType.CTEI: - case RecordHeaderType.CTEX: - default: - startPos = ipf.StartPos + ipf.BlockLength; - break; + switch (ipf.RecordType) + { + // Nothing to process / unknown + // just move ahead + case RecordHeaderType.CAPS: + case RecordHeaderType.TRCK: + case RecordHeaderType.DUMP: + case RecordHeaderType.CTEI: + case RecordHeaderType.CTEX: + default: + startPos = ipf.StartPos + ipf.BlockLength; + break; - // INFO block - case RecordHeaderType.INFO: - // INFO header is followed immediately by an INFO block - ipf.INFOmediaType = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.INFOencoderType = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.INFOencoderRev = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.INFOfileKey = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.INFOfileRev = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.INFOorigin = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.INFOminTrack = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.INFOmaxTrack = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.INFOminSide = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.INFOmaxSide = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.INFOcreationDate = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.INFOcreationTime = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.INFOplatform1 = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.INFOplatform2 = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.INFOplatform3 = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.INFOplatform4 = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.INFOdiskNumber = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.INFOcreatorId = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - startPos += 12; // reserved - break; + // INFO block + case RecordHeaderType.INFO: + // INFO header is followed immediately by an INFO block + ipf.INFOmediaType = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.INFOencoderType = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.INFOencoderRev = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.INFOfileKey = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.INFOfileRev = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.INFOorigin = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.INFOminTrack = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.INFOmaxTrack = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.INFOminSide = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.INFOmaxSide = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.INFOcreationDate = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.INFOcreationTime = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.INFOplatform1 = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.INFOplatform2 = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.INFOplatform3 = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.INFOplatform4 = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.INFOdiskNumber = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.INFOcreatorId = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + startPos += 12; // reserved + break; - case RecordHeaderType.IMGE: - ipf.IMGEtrack = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.IMGEside = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.IMGEdensity = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.IMGEsignalType = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.IMGEtrackBytes = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.IMGEstartBytePos = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.IMGEstartBitPos = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.IMGEdataBits = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.IMGEgapBits = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.IMGEtrackBits = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.IMGEblockCount = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.IMGEencoderProcess = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.IMGEtrackFlags = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.IMGEdataKey = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - startPos += 12; // reserved - break; + case RecordHeaderType.IMGE: + ipf.IMGEtrack = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.IMGEside = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.IMGEdensity = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.IMGEsignalType = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.IMGEtrackBytes = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.IMGEstartBytePos = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.IMGEstartBitPos = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.IMGEdataBits = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.IMGEgapBits = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.IMGEtrackBits = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.IMGEblockCount = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.IMGEencoderProcess = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.IMGEtrackFlags = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.IMGEdataKey = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + startPos += 12; // reserved + break; - case RecordHeaderType.DATA: - ipf.DATAlength = MediaConverter.GetBEInt32(data, startPos); - if (ipf.DATAlength == 0) - { - ipf.DATAextraDataRaw = new byte[0]; - ipf.DATAlength = 0; - } - else - { - ipf.DATAextraDataRaw = new byte[ipf.DATAlength]; - } - startPos += 4; - ipf.DATAbitSize = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.DATAcrc = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - ipf.DATAdataKey = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + case RecordHeaderType.DATA: + ipf.DATAlength = MediaConverter.GetBEInt32(data, startPos); + if (ipf.DATAlength == 0) + { + ipf.DATAextraDataRaw = new byte[0]; + ipf.DATAlength = 0; + } + else + { + ipf.DATAextraDataRaw = new byte[ipf.DATAlength]; + } + startPos += 4; + ipf.DATAbitSize = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.DATAcrc = MediaConverter.GetBEInt32(data, startPos); startPos += 4; + ipf.DATAdataKey = MediaConverter.GetBEInt32(data, startPos); startPos += 4; - if (ipf.DATAlength != 0) - { - Array.Copy(data, startPos, ipf.DATAextraDataRaw, 0, ipf.DATAlength); - } + if (ipf.DATAlength != 0) + { + Array.Copy(data, startPos, ipf.DATAextraDataRaw, 0, ipf.DATAlength); + } - startPos += ipf.DATAlength; - break; - } + startPos += ipf.DATAlength; + break; + } - return ipf; - } - } + return ipf; + } + } - public enum RecordHeaderType - { - None, - CAPS, - DUMP, - DATA, - TRCK, - INFO, - IMGE, - CTEI, - CTEX, - } + public enum RecordHeaderType + { + None, + CAPS, + DUMP, + DATA, + TRCK, + INFO, + IMGE, + CTEI, + CTEX, + } - /// - /// State serlialization - /// - public override void SyncState(Serializer ser) - { - ser.BeginSection("Plus3FloppyDisk"); + /// + /// State serlialization + /// + public override void SyncState(Serializer ser) + { + ser.BeginSection("Plus3FloppyDisk"); - ser.Sync(nameof(CylinderCount), ref CylinderCount); - ser.Sync(nameof(SideCount), ref SideCount); - ser.Sync(nameof(BytesPerTrack), ref BytesPerTrack); - ser.Sync(nameof(WriteProtected), ref WriteProtected); - ser.SyncEnum(nameof(Protection), ref Protection); + ser.Sync(nameof(CylinderCount), ref CylinderCount); + ser.Sync(nameof(SideCount), ref SideCount); + ser.Sync(nameof(BytesPerTrack), ref BytesPerTrack); + ser.Sync(nameof(WriteProtected), ref WriteProtected); + ser.SyncEnum(nameof(Protection), ref Protection); - ser.Sync(nameof(DirtyData), ref DirtyData); - if (DirtyData) - { + ser.Sync(nameof(DirtyData), ref DirtyData); + if (DirtyData) + { - } + } - // sync deterministic track and sector counters - ser.Sync(nameof( _randomCounter), ref _randomCounter); - RandomCounter = _randomCounter; + // sync deterministic track and sector counters + ser.Sync(nameof(_randomCounter), ref _randomCounter); + RandomCounter = _randomCounter; - ser.EndSection(); - } - } + ser.EndSection(); + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/UDIFormat/UDI1_0FloppyDisk.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/UDIFormat/UDI1_0FloppyDisk.cs index d38209f49b..5b6006bf9c 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/UDIFormat/UDI1_0FloppyDisk.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/UDIFormat/UDI1_0FloppyDisk.cs @@ -8,205 +8,205 @@ using System.Threading.Tasks; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - public class UDI1_0FloppyDisk : FloppyDisk - { - /// - /// The format type - /// - public override DiskType DiskFormatType => DiskType.UDI; + public class UDI1_0FloppyDisk : FloppyDisk + { + /// + /// The format type + /// + public override DiskType DiskFormatType => DiskType.UDI; - /// - /// Attempts to parse incoming disk data - /// - /// - /// TRUE: disk parsed - /// FALSE: unable to parse disk - /// - public override bool ParseDisk(byte[] data) - { - // look for standard magic string - string ident = Encoding.ASCII.GetString(data, 0, 4); + /// + /// Attempts to parse incoming disk data + /// + /// + /// TRUE: disk parsed + /// FALSE: unable to parse disk + /// + public override bool ParseDisk(byte[] data) + { + // look for standard magic string + string ident = Encoding.ASCII.GetString(data, 0, 4); - if (!ident.StartsWith("UDI!") && !ident.StartsWith("udi!")) - { - // incorrect format - return false; - } + if (!ident.StartsWith("UDI!") && !ident.StartsWith("udi!")) + { + // incorrect format + return false; + } - if (data[0x08] != 0) - { - // wrong version - return false; - } + if (data[0x08] != 0) + { + // wrong version + return false; + } - if (ident == "udi!") - { - // cant handle compression yet - return false; - } + if (ident == "udi!") + { + // cant handle compression yet + return false; + } - DiskHeader.DiskIdent = ident; - DiskHeader.NumberOfTracks = (byte)(data[0x09] + 1); - DiskHeader.NumberOfSides = (byte)(data[0x0A] + 1); + DiskHeader.DiskIdent = ident; + DiskHeader.NumberOfTracks = (byte)(data[0x09] + 1); + DiskHeader.NumberOfSides = (byte)(data[0x0A] + 1); - DiskTracks = new Track[DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides]; + DiskTracks = new Track[DiskHeader.NumberOfTracks * DiskHeader.NumberOfSides]; - int fileSize = MediaConverter.GetInt32(data, 4); // not including the final 4-byte checksum + int fileSize = MediaConverter.GetInt32(data, 4); // not including the final 4-byte checksum - // ignore extended header - var extHdrSize = MediaConverter.GetInt32(data, 0x0C); - int pos = 0x10 + extHdrSize; + // ignore extended header + var extHdrSize = MediaConverter.GetInt32(data, 0x0C); + int pos = 0x10 + extHdrSize; - // process track information - for (int t = 0; t < DiskHeader.NumberOfTracks; t++) - { - DiskTracks[t] = new UDIv1Track(); - DiskTracks[t].TrackNumber = (byte)t; - DiskTracks[t].SideNumber = 0; - DiskTracks[t].TrackType = data[pos++]; - DiskTracks[t].TLEN = MediaConverter.GetWordValue(data, pos); pos += 2; - DiskTracks[t].TrackData = new byte[DiskTracks[t].TLEN + DiskTracks[t].CLEN]; - Array.Copy(data, pos, DiskTracks[t].TrackData, 0, DiskTracks[t].TLEN + DiskTracks[t].CLEN); - pos += DiskTracks[t].TLEN + DiskTracks[t].CLEN; - } + // process track information + for (int t = 0; t < DiskHeader.NumberOfTracks; t++) + { + DiskTracks[t] = new UDIv1Track(); + DiskTracks[t].TrackNumber = (byte)t; + DiskTracks[t].SideNumber = 0; + DiskTracks[t].TrackType = data[pos++]; + DiskTracks[t].TLEN = MediaConverter.GetWordValue(data, pos); pos += 2; + DiskTracks[t].TrackData = new byte[DiskTracks[t].TLEN + DiskTracks[t].CLEN]; + Array.Copy(data, pos, DiskTracks[t].TrackData, 0, DiskTracks[t].TLEN + DiskTracks[t].CLEN); + pos += DiskTracks[t].TLEN + DiskTracks[t].CLEN; + } - return true; - } + return true; + } - /// - /// Takes a double-sided disk byte array and converts into 2 single-sided arrays - /// - public static bool SplitDoubleSided(byte[] data, List results) - { - // look for standard magic string - string ident = Encoding.ASCII.GetString(data, 0, 4); + /// + /// Takes a double-sided disk byte array and converts into 2 single-sided arrays + /// + public static bool SplitDoubleSided(byte[] data, List results) + { + // look for standard magic string + string ident = Encoding.ASCII.GetString(data, 0, 4); - if (!ident.StartsWith("UDI!") && !ident.StartsWith("udi!")) - { - // incorrect format - return false; - } + if (!ident.StartsWith("UDI!") && !ident.StartsWith("udi!")) + { + // incorrect format + return false; + } - if (data[0x08] != 0) - { - // wrong version - return false; - } + if (data[0x08] != 0) + { + // wrong version + return false; + } - if (ident == "udi!") - { - // cant handle compression yet - return false; - } + if (ident == "udi!") + { + // cant handle compression yet + return false; + } - byte[] S0 = new byte[data.Length]; - byte[] S1 = new byte[data.Length]; + byte[] S0 = new byte[data.Length]; + byte[] S1 = new byte[data.Length]; - // header - var extHdr = MediaConverter.GetInt32(data, 0x0C); - Array.Copy(data, 0, S0, 0, 0x10 + extHdr); - Array.Copy(data, 0, S1, 0, 0x10 + extHdr); - // change side number - S0[0x0A] = 0; - S1[0x0A] = 0; + // header + var extHdr = MediaConverter.GetInt32(data, 0x0C); + Array.Copy(data, 0, S0, 0, 0x10 + extHdr); + Array.Copy(data, 0, S1, 0, 0x10 + extHdr); + // change side number + S0[0x0A] = 0; + S1[0x0A] = 0; - int pos = 0x10 + extHdr; - int fileSize = MediaConverter.GetInt32(data, 4); // not including the final 4-byte checksum + int pos = 0x10 + extHdr; + int fileSize = MediaConverter.GetInt32(data, 4); // not including the final 4-byte checksum - int s0Pos = pos; - int s1Pos = pos; + int s0Pos = pos; + int s1Pos = pos; - // process track information - for (int t = 0; t < (data[0x09] + 1) * 2; t++) - { - var TLEN = MediaConverter.GetWordValue(data, pos + 1); - var CLEN = TLEN / 8 + (TLEN % 8 / 7) / 8; - var blockSize = TLEN + CLEN + 3; + // process track information + for (int t = 0; t < (data[0x09] + 1) * 2; t++) + { + var TLEN = MediaConverter.GetWordValue(data, pos + 1); + var CLEN = TLEN / 8 + (TLEN % 8 / 7) / 8; + var blockSize = TLEN + CLEN + 3; - // 2 sided image: side 0 tracks will all have t as an even number - try - { - if (t == 0 || t % 2 == 0) - { - Array.Copy(data, pos, S0, s0Pos, blockSize); - s0Pos += blockSize; - } - else - { - Array.Copy(data, pos, S1, s1Pos, blockSize); - s1Pos += blockSize; - } - } - catch (Exception) - { - - } - + // 2 sided image: side 0 tracks will all have t as an even number + try + { + if (t == 0 || t % 2 == 0) + { + Array.Copy(data, pos, S0, s0Pos, blockSize); + s0Pos += blockSize; + } + else + { + Array.Copy(data, pos, S1, s1Pos, blockSize); + s1Pos += blockSize; + } + } + catch (Exception) + { - pos += blockSize; - } - - // skip checkum bytes for now - - byte[] s0final = new byte[s0Pos]; - byte[] s1final = new byte[s1Pos]; - Array.Copy(S0, 0, s0final, 0, s0Pos); - Array.Copy(S1, 0, s1final, 0, s1Pos); - - results.Add(s0final); - results.Add(s1final); - - return true; - } - - public class UDIv1Track : Track - { - /// - /// Parse the UDI TrackData byte[] array into sector objects - /// - public override Sector[] Sectors - { - get - { - List secs = new List(); - var datas = TrackData.Skip(3).Take(TLEN).ToArray(); - var clocks = new BitArray(TrackData.Skip(3 + TLEN).Take(CLEN).ToArray()); - - return secs.ToArray(); - } - } - } - - public class UDIv1Sector : Sector - { - - } + } - /// - /// State serlialization - /// - public override void SyncState(Serializer ser) - { - ser.BeginSection("Plus3FloppyDisk"); + pos += blockSize; + } - ser.Sync(nameof(CylinderCount), ref CylinderCount); - ser.Sync(nameof(SideCount), ref SideCount); - ser.Sync(nameof(BytesPerTrack), ref BytesPerTrack); - ser.Sync(nameof(WriteProtected), ref WriteProtected); - ser.SyncEnum(nameof(Protection), ref Protection); + // skip checkum bytes for now - ser.Sync(nameof(DirtyData), ref DirtyData); - if (DirtyData) - { + byte[] s0final = new byte[s0Pos]; + byte[] s1final = new byte[s1Pos]; + Array.Copy(S0, 0, s0final, 0, s0Pos); + Array.Copy(S1, 0, s1final, 0, s1Pos); - } + results.Add(s0final); + results.Add(s1final); - // sync deterministic track and sector counters - ser.Sync(nameof( _randomCounter), ref _randomCounter); - RandomCounter = _randomCounter; + return true; + } - ser.EndSection(); - } - } + public class UDIv1Track : Track + { + /// + /// Parse the UDI TrackData byte[] array into sector objects + /// + public override Sector[] Sectors + { + get + { + List secs = new List(); + var datas = TrackData.Skip(3).Take(TLEN).ToArray(); + var clocks = new BitArray(TrackData.Skip(3 + TLEN).Take(CLEN).ToArray()); + + return secs.ToArray(); + } + } + } + + public class UDIv1Sector : Sector + { + + } + + + /// + /// State serlialization + /// + public override void SyncState(Serializer ser) + { + ser.BeginSection("Plus3FloppyDisk"); + + ser.Sync(nameof(CylinderCount), ref CylinderCount); + ser.Sync(nameof(SideCount), ref SideCount); + ser.Sync(nameof(BytesPerTrack), ref BytesPerTrack); + ser.Sync(nameof(WriteProtected), ref WriteProtected); + ser.SyncEnum(nameof(Protection), ref Protection); + + ser.Sync(nameof(DirtyData), ref DirtyData); + if (DirtyData) + { + + } + + // sync deterministic track and sector counters + ser.Sync(nameof(_randomCounter), ref _randomCounter); + RandomCounter = _randomCounter; + + ser.EndSection(); + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/MediaConverter.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/MediaConverter.cs index 1047d2fd1a..a8fa0effbd 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/MediaConverter.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/MediaConverter.cs @@ -6,205 +6,205 @@ using System.Linq; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Abtract class that represents all Media Converters - /// - public abstract class MediaConverter - { - /// - /// The type of serializer - /// - public abstract MediaConverterType FormatType { get; } + /// + /// Abtract class that represents all Media Converters + /// + public abstract class MediaConverter + { + /// + /// The type of serializer + /// + public abstract MediaConverterType FormatType { get; } - /// - /// Signs whether this class can be used to read the data format - /// - public virtual bool IsReader - { - get - { - return false; - } - } + /// + /// Signs whether this class can be used to read the data format + /// + public virtual bool IsReader + { + get + { + return false; + } + } - /// - /// Signs whether this class can be used to write the data format - /// - public virtual bool IsWriter - { - get - { - return false; - } - } + /// + /// Signs whether this class can be used to write the data format + /// + public virtual bool IsWriter + { + get + { + return false; + } + } - /// - /// Serialization method - /// - public virtual void Read(byte[] data) - { - throw new NotImplementedException(this.GetType().ToString() + - "Read operation is not implemented for this converter"); - } + /// + /// Serialization method + /// + public virtual void Read(byte[] data) + { + throw new NotImplementedException(this.GetType().ToString() + + "Read operation is not implemented for this converter"); + } - /// - /// DeSerialization method - /// - public virtual void Write(byte[] data) - { - throw new NotImplementedException(this.GetType().ToString() + - "Write operation is not implemented for this converter"); - } + /// + /// DeSerialization method + /// + public virtual void Write(byte[] data) + { + throw new NotImplementedException(this.GetType().ToString() + + "Write operation is not implemented for this converter"); + } - /// - /// Serializer does a quick check, returns TRUE if file is detected as this type - /// - public virtual bool CheckType(byte[] data) - { - throw new NotImplementedException(this.GetType().ToString() + - "Check type operation is not implemented for this converter"); - } + /// + /// Serializer does a quick check, returns TRUE if file is detected as this type + /// + public virtual bool CheckType(byte[] data) + { + throw new NotImplementedException(this.GetType().ToString() + + "Check type operation is not implemented for this converter"); + } - #region Static Tools + #region Static Tools - /// - /// Converts an int32 value into a byte array - /// - public static byte[] GetBytes(int value) - { - byte[] buf = new byte[4]; - buf[0] = (byte)value; - buf[1] = (byte)(value >> 8); - buf[2] = (byte)(value >> 16); - buf[3] = (byte)(value >> 24); - return buf; - } + /// + /// Converts an int32 value into a byte array + /// + public static byte[] GetBytes(int value) + { + byte[] buf = new byte[4]; + buf[0] = (byte)value; + buf[1] = (byte)(value >> 8); + buf[2] = (byte)(value >> 16); + buf[3] = (byte)(value >> 24); + return buf; + } - /// - /// Returns an int32 from a byte array based on offset - /// - public static int GetInt32(byte[] buf, int offsetIndex) - { - return buf[offsetIndex] | buf[offsetIndex + 1] << 8 | buf[offsetIndex + 2] << 16 | buf[offsetIndex + 3] << 24; - } + /// + /// Returns an int32 from a byte array based on offset + /// + public static int GetInt32(byte[] buf, int offsetIndex) + { + return buf[offsetIndex] | buf[offsetIndex + 1] << 8 | buf[offsetIndex + 2] << 16 | buf[offsetIndex + 3] << 24; + } - /// - /// Returns an int32 from a byte array based on offset (in BIG ENDIAN format) - /// - public static int GetBEInt32(byte[] buf, int offsetIndex) - { - byte[] b = new byte[4]; - Array.Copy(buf, offsetIndex, b, 0, 4); - byte[] buffer = b.Reverse().ToArray(); - int pos = 0; - return buffer[pos++] | buffer[pos++] << 8 | buffer[pos++] << 16 | buffer[pos++] << 24; - } + /// + /// Returns an int32 from a byte array based on offset (in BIG ENDIAN format) + /// + public static int GetBEInt32(byte[] buf, int offsetIndex) + { + byte[] b = new byte[4]; + Array.Copy(buf, offsetIndex, b, 0, 4); + byte[] buffer = b.Reverse().ToArray(); + int pos = 0; + return buffer[pos++] | buffer[pos++] << 8 | buffer[pos++] << 16 | buffer[pos++] << 24; + } - /// - /// Returns an int32 from a byte array based on the length of the byte array (in BIG ENDIAN format) - /// - public static int GetBEInt32FromByteArray(byte[] buf) - { - byte[] b = buf.Reverse().ToArray(); - if (b.Length == 0) - return 0; - int res = b[0]; - int pos = 1; - switch (b.Length) - { - case 1: - default: - return res; - case 2: - return res | b[pos] << (8 * pos++); - case 3: - return res | b[pos] << (8 * pos++) | b[pos] << (8 * pos++); - case 4: - return res | b[pos] << (8 * pos++) | b[pos] << (8 * pos++) | b[pos] << (8 * pos++); - case 5: - return res | b[pos] << (8 * pos++) | b[pos] << (8 * pos++) | b[pos] << (8 * pos++) | b[pos] << (8 * pos++); - case 6: - return res | b[pos] << (8 * pos++) | b[pos] << (8 * pos++) | b[pos] << (8 * pos++) | b[pos] << (8 * pos++) | b[pos] << (8 * pos++); - case 7: - return res | b[pos] << (8 * pos++) | b[pos] << (8 * pos++) | b[pos] << (8 * pos++) | b[pos] << (8 * pos++) | b[pos] << (8 * pos++) | b[pos] << (8 * pos++); - } - } + /// + /// Returns an int32 from a byte array based on the length of the byte array (in BIG ENDIAN format) + /// + public static int GetBEInt32FromByteArray(byte[] buf) + { + byte[] b = buf.Reverse().ToArray(); + if (b.Length == 0) + return 0; + int res = b[0]; + int pos = 1; + switch (b.Length) + { + case 1: + default: + return res; + case 2: + return res | b[pos] << (8 * pos++); + case 3: + return res | b[pos] << (8 * pos++) | b[pos] << (8 * pos++); + case 4: + return res | b[pos] << (8 * pos++) | b[pos] << (8 * pos++) | b[pos] << (8 * pos++); + case 5: + return res | b[pos] << (8 * pos++) | b[pos] << (8 * pos++) | b[pos] << (8 * pos++) | b[pos] << (8 * pos++); + case 6: + return res | b[pos] << (8 * pos++) | b[pos] << (8 * pos++) | b[pos] << (8 * pos++) | b[pos] << (8 * pos++) | b[pos] << (8 * pos++); + case 7: + return res | b[pos] << (8 * pos++) | b[pos] << (8 * pos++) | b[pos] << (8 * pos++) | b[pos] << (8 * pos++) | b[pos] << (8 * pos++) | b[pos] << (8 * pos++); + } + } - /// - /// Returns an int32 from a byte array based on offset - /// - public static uint GetUInt32(byte[] buf, int offsetIndex) - { - return (uint)(buf[offsetIndex] | buf[offsetIndex + 1] << 8 | buf[offsetIndex + 2] << 16 | buf[offsetIndex + 3] << 24); - } + /// + /// Returns an int32 from a byte array based on offset + /// + public static uint GetUInt32(byte[] buf, int offsetIndex) + { + return (uint)(buf[offsetIndex] | buf[offsetIndex + 1] << 8 | buf[offsetIndex + 2] << 16 | buf[offsetIndex + 3] << 24); + } - /// - /// Returns an uint16 from a byte array based on offset - /// - public static ushort GetWordValue(byte[] buf, int offsetIndex) - { - return (ushort)(buf[offsetIndex] | buf[offsetIndex + 1] << 8); - } + /// + /// Returns an uint16 from a byte array based on offset + /// + public static ushort GetWordValue(byte[] buf, int offsetIndex) + { + return (ushort)(buf[offsetIndex] | buf[offsetIndex + 1] << 8); + } - /// - /// Updates a byte array with a uint16 value based on offset - /// - public static void SetWordValue(byte[] buf, int offsetIndex, ushort value) - { - buf[offsetIndex] = (byte)value; - buf[offsetIndex + 1] = (byte)(value >> 8); - } + /// + /// Updates a byte array with a uint16 value based on offset + /// + public static void SetWordValue(byte[] buf, int offsetIndex, ushort value) + { + buf[offsetIndex] = (byte)value; + buf[offsetIndex + 1] = (byte)(value >> 8); + } - /// - /// Takes a PauseInMilliseconds value and returns the value in T-States - /// - public static int TranslatePause(int pauseInMS) - { - // t-states per millisecond - var tspms = (69888 * 50) / 1000; - // get value - int res = pauseInMS * tspms; + /// + /// Takes a PauseInMilliseconds value and returns the value in T-States + /// + public static int TranslatePause(int pauseInMS) + { + // t-states per millisecond + var tspms = (69888 * 50) / 1000; + // get value + int res = pauseInMS * tspms; - return res; - } + return res; + } - /// - /// Decompresses a byte array that is Z-RLE compressed - /// - public static void DecompressZRLE(byte[] sourceBuffer, ref byte[] destBuffer) - { - MemoryStream stream = new MemoryStream(); - stream.Write(sourceBuffer, 0, sourceBuffer.Length); - stream.Position = 0; - stream.ReadByte(); - stream.ReadByte(); - DeflateStream ds = new DeflateStream(stream, CompressionMode.Decompress, false); - ds.Read(destBuffer, 0, destBuffer.Length); - } + /// + /// Decompresses a byte array that is Z-RLE compressed + /// + public static void DecompressZRLE(byte[] sourceBuffer, ref byte[] destBuffer) + { + MemoryStream stream = new MemoryStream(); + stream.Write(sourceBuffer, 0, sourceBuffer.Length); + stream.Position = 0; + stream.ReadByte(); + stream.ReadByte(); + DeflateStream ds = new DeflateStream(stream, CompressionMode.Decompress, false); + ds.Read(destBuffer, 0, destBuffer.Length); + } - public static byte[] SerializeRaw(object obj) - { - int rSize = Marshal.SizeOf(obj); - IntPtr buff = Marshal.AllocHGlobal(rSize); - Marshal.StructureToPtr(obj, buff, false); - byte[] rData = new byte[rSize]; - Marshal.Copy(buff, rData, 0, rSize); - return rData; - } + public static byte[] SerializeRaw(object obj) + { + int rSize = Marshal.SizeOf(obj); + IntPtr buff = Marshal.AllocHGlobal(rSize); + Marshal.StructureToPtr(obj, buff, false); + byte[] rData = new byte[rSize]; + Marshal.Copy(buff, rData, 0, rSize); + return rData; + } - public static T DeserializeRaw(byte[] rData, int pos) - { - int rSize = Marshal.SizeOf(typeof(T)); - if (rSize > rData.Length - pos) - throw new Exception(); - IntPtr buff = Marshal.AllocHGlobal(rSize); - Marshal.Copy(rData, pos, buff, rSize); - T rObj = (T)Marshal.PtrToStructure(buff, typeof(T)); - Marshal.FreeHGlobal(buff); - return rObj; - } + public static T DeserializeRaw(byte[] rData, int pos) + { + int rSize = Marshal.SizeOf(typeof(T)); + if (rSize > rData.Length - pos) + throw new Exception(); + IntPtr buff = Marshal.AllocHGlobal(rSize); + Marshal.Copy(rData, pos, buff, rSize); + T rObj = (T)Marshal.PtrToStructure(buff, typeof(T)); + Marshal.FreeHGlobal(buff); + return rObj; + } - #endregion - } + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/MediaConverterType.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/MediaConverterType.cs index c82133037b..018e5aa2fc 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/MediaConverterType.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/MediaConverterType.cs @@ -1,17 +1,17 @@  namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Represents the different types of media serializer avaiable - /// - public enum MediaConverterType - { - NONE, - TZX, - TAP, - PZX, - CSW, - WAV, - DSK - } + /// + /// Represents the different types of media serializer avaiable + /// + public enum MediaConverterType + { + NONE, + TZX, + TAP, + PZX, + CSW, + WAV, + DSK + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Snapshot/SZX/SZX.Methods.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Snapshot/SZX/SZX.Methods.cs index 0f2c25dab0..c8f0f5e7a2 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Snapshot/SZX/SZX.Methods.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Snapshot/SZX/SZX.Methods.cs @@ -11,177 +11,177 @@ using System.Threading.Tasks; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// SZX Methods - /// Based on the work done by ArjunNair in ZERO spectrum emulator: https://github.com/ArjunNair/Zero-Emulator/blob/master/Ziggy/Peripherals/SZXFile.cs - /// - public partial class SZX - { - private SpectrumBase _machine; + /// + /// SZX Methods + /// Based on the work done by ArjunNair in ZERO spectrum emulator: https://github.com/ArjunNair/Zero-Emulator/blob/master/Ziggy/Peripherals/SZXFile.cs + /// + public partial class SZX + { + private SpectrumBase _machine; - private Z80A _cpu => _machine.CPU; + private Z80A _cpu => _machine.CPU; - private SZX(SpectrumBase machine) - { - _machine = machine; - } + private SZX(SpectrumBase machine) + { + _machine = machine; + } - /// - /// Exports state information to a byte array in ZX-State format - /// - public static byte[] ExportSZX(SpectrumBase machine) - { - var s = new SZX(machine); + /// + /// Exports state information to a byte array in ZX-State format + /// + public static byte[] ExportSZX(SpectrumBase machine) + { + var s = new SZX(machine); - byte[] result = null; + byte[] result = null; - using (MemoryStream ms = new MemoryStream()) - { - using (BinaryWriter r = new BinaryWriter(ms)) - { - // temp buffer - byte[] buff; - // working block - ZXSTBLOCK block = new ZXSTBLOCK(); + using (MemoryStream ms = new MemoryStream()) + { + using (BinaryWriter r = new BinaryWriter(ms)) + { + // temp buffer + byte[] buff; + // working block + ZXSTBLOCK block = new ZXSTBLOCK(); - // header - ZXSTHEADER header = new ZXSTHEADER(); - header.dwMagic = MediaConverter.GetUInt32(Encoding.UTF8.GetBytes("ZXST"), 0); - header.chMajorVersion = 1; - header.chMinorVersion = 4; - header.chFlags = 0; - switch (s._machine.Spectrum.MachineType) - { - case MachineType.ZXSpectrum16: header.chMachineId = (int)MachineIdentifier.ZXSTMID_16K; break; - case MachineType.ZXSpectrum48: header.chMachineId = (int)MachineIdentifier.ZXSTMID_48K; break; - case MachineType.ZXSpectrum128: header.chMachineId = (int)MachineIdentifier.ZXSTMID_128K; break; - case MachineType.ZXSpectrum128Plus2: header.chMachineId = (int)MachineIdentifier.ZXSTMID_PLUS2; break; - case MachineType.ZXSpectrum128Plus2a: header.chMachineId = (int)MachineIdentifier.ZXSTMID_PLUS2A; break; - case MachineType.ZXSpectrum128Plus3: header.chMachineId = (int)MachineIdentifier.ZXSTMID_PLUS3; break; - } - buff = MediaConverter.SerializeRaw(header); - r.Write(buff); + // header + ZXSTHEADER header = new ZXSTHEADER(); + header.dwMagic = MediaConverter.GetUInt32(Encoding.UTF8.GetBytes("ZXST"), 0); + header.chMajorVersion = 1; + header.chMinorVersion = 4; + header.chFlags = 0; + switch (s._machine.Spectrum.MachineType) + { + case MachineType.ZXSpectrum16: header.chMachineId = (int)MachineIdentifier.ZXSTMID_16K; break; + case MachineType.ZXSpectrum48: header.chMachineId = (int)MachineIdentifier.ZXSTMID_48K; break; + case MachineType.ZXSpectrum128: header.chMachineId = (int)MachineIdentifier.ZXSTMID_128K; break; + case MachineType.ZXSpectrum128Plus2: header.chMachineId = (int)MachineIdentifier.ZXSTMID_PLUS2; break; + case MachineType.ZXSpectrum128Plus2a: header.chMachineId = (int)MachineIdentifier.ZXSTMID_PLUS2A; break; + case MachineType.ZXSpectrum128Plus3: header.chMachineId = (int)MachineIdentifier.ZXSTMID_PLUS3; break; + } + buff = MediaConverter.SerializeRaw(header); + r.Write(buff); - // ZXSTCREATOR - var bStruct = s.GetZXSTCREATOR(); - block.dwId = MediaConverter.GetUInt32(Encoding.UTF8.GetBytes("CRTR"), 0); - block.dwSize = (uint)Marshal.SizeOf(bStruct); - buff = MediaConverter.SerializeRaw(block); - r.Write(buff); - buff = MediaConverter.SerializeRaw(bStruct); - r.Write(buff); + // ZXSTCREATOR + var bStruct = s.GetZXSTCREATOR(); + block.dwId = MediaConverter.GetUInt32(Encoding.UTF8.GetBytes("CRTR"), 0); + block.dwSize = (uint)Marshal.SizeOf(bStruct); + buff = MediaConverter.SerializeRaw(block); + r.Write(buff); + buff = MediaConverter.SerializeRaw(bStruct); + r.Write(buff); - // ZXSTZ80REGS - var cStruct = s.GetZXSTZ80REGS(); - block.dwId = MediaConverter.GetUInt32(Encoding.UTF8.GetBytes("Z80R"), 0); - block.dwSize = (uint)Marshal.SizeOf(cStruct); - buff = MediaConverter.SerializeRaw(block); - r.Write(buff); - buff = MediaConverter.SerializeRaw(cStruct); - r.Write(buff); + // ZXSTZ80REGS + var cStruct = s.GetZXSTZ80REGS(); + block.dwId = MediaConverter.GetUInt32(Encoding.UTF8.GetBytes("Z80R"), 0); + block.dwSize = (uint)Marshal.SizeOf(cStruct); + buff = MediaConverter.SerializeRaw(block); + r.Write(buff); + buff = MediaConverter.SerializeRaw(cStruct); + r.Write(buff); - // ZXSTSPECREGS - var dStruct = s.GetZXSTSPECREGS(); - block.dwId = MediaConverter.GetUInt32(Encoding.UTF8.GetBytes("SPCR"), 0); - block.dwSize = (uint)Marshal.SizeOf(dStruct); - buff = MediaConverter.SerializeRaw(block); - r.Write(buff); - buff = MediaConverter.SerializeRaw(dStruct); - r.Write(buff); + // ZXSTSPECREGS + var dStruct = s.GetZXSTSPECREGS(); + block.dwId = MediaConverter.GetUInt32(Encoding.UTF8.GetBytes("SPCR"), 0); + block.dwSize = (uint)Marshal.SizeOf(dStruct); + buff = MediaConverter.SerializeRaw(block); + r.Write(buff); + buff = MediaConverter.SerializeRaw(dStruct); + r.Write(buff); - // ZXSTKEYBOARD - var eStruct = s.GetZXSTKEYBOARD(); - block.dwId = MediaConverter.GetUInt32(Encoding.UTF8.GetBytes("KEYB"), 0); - block.dwSize = (uint)Marshal.SizeOf(eStruct); - buff = MediaConverter.SerializeRaw(block); - r.Write(buff); - buff = MediaConverter.SerializeRaw(eStruct); - r.Write(buff); + // ZXSTKEYBOARD + var eStruct = s.GetZXSTKEYBOARD(); + block.dwId = MediaConverter.GetUInt32(Encoding.UTF8.GetBytes("KEYB"), 0); + block.dwSize = (uint)Marshal.SizeOf(eStruct); + buff = MediaConverter.SerializeRaw(block); + r.Write(buff); + buff = MediaConverter.SerializeRaw(eStruct); + r.Write(buff); - // ZXSTJOYSTICK - var fStruct = s.GetZXSTJOYSTICK(); - block.dwId = MediaConverter.GetUInt32(Encoding.UTF8.GetBytes("JOY\0"), 0); - block.dwSize = (uint)Marshal.SizeOf(fStruct); - buff = MediaConverter.SerializeRaw(block); - r.Write(buff); - buff = MediaConverter.SerializeRaw(fStruct); - r.Write(buff); - + // ZXSTJOYSTICK + var fStruct = s.GetZXSTJOYSTICK(); + block.dwId = MediaConverter.GetUInt32(Encoding.UTF8.GetBytes("JOY\0"), 0); + block.dwSize = (uint)Marshal.SizeOf(fStruct); + buff = MediaConverter.SerializeRaw(block); + r.Write(buff); + buff = MediaConverter.SerializeRaw(fStruct); + r.Write(buff); - // ZXSTAYBLOCK - if (s._machine.Spectrum.MachineType != MachineType.ZXSpectrum16 && s._machine.Spectrum.MachineType != MachineType.ZXSpectrum48) - { - var gStruct = s.GetZXSTAYBLOCK(); - block.dwId = MediaConverter.GetUInt32(Encoding.UTF8.GetBytes("AY\0\0"), 0); - block.dwSize = (uint)Marshal.SizeOf(gStruct); - buff = MediaConverter.SerializeRaw(block); - r.Write(buff); - buff = MediaConverter.SerializeRaw(gStruct); - r.Write(buff); - } - // ZXSTRAMPAGE - switch (s._machine.Spectrum.MachineType) - { - // For 16k Spectrums, only page 5 (0x4000 - 0x7fff) is saved. - case MachineType.ZXSpectrum16: - block.dwId = MediaConverter.GetUInt32(Encoding.UTF8.GetBytes("RAMP"), 0); - var rp16 = s.GetZXSTRAMPAGE(5, s._machine.RAM0); - block.dwSize = (uint)Marshal.SizeOf(rp16); - buff = MediaConverter.SerializeRaw(block); - r.Write(buff); - buff = MediaConverter.SerializeRaw(rp16); - r.Write(buff); - break; - // For 48k Spectrums and Timex TS/TC models, pages 5, 2 (0x8000 - 0xbfff) and 0 (0xc000 - 0xffff) are saved. - case MachineType.ZXSpectrum48: - block.dwId = MediaConverter.GetUInt32(Encoding.UTF8.GetBytes("RAMP"), 0); - var rp48_0 = s.GetZXSTRAMPAGE(5, s._machine.RAM0); - block.dwSize = (uint)Marshal.SizeOf(rp48_0); - buff = MediaConverter.SerializeRaw(block); - r.Write(buff); - buff = MediaConverter.SerializeRaw(rp48_0); - r.Write(buff); + // ZXSTAYBLOCK + if (s._machine.Spectrum.MachineType != MachineType.ZXSpectrum16 && s._machine.Spectrum.MachineType != MachineType.ZXSpectrum48) + { + var gStruct = s.GetZXSTAYBLOCK(); + block.dwId = MediaConverter.GetUInt32(Encoding.UTF8.GetBytes("AY\0\0"), 0); + block.dwSize = (uint)Marshal.SizeOf(gStruct); + buff = MediaConverter.SerializeRaw(block); + r.Write(buff); + buff = MediaConverter.SerializeRaw(gStruct); + r.Write(buff); + } - block.dwId = MediaConverter.GetUInt32(Encoding.UTF8.GetBytes("RAMP"), 0); - var rp48_1 = s.GetZXSTRAMPAGE(5, s._machine.RAM1); - block.dwSize = (uint)Marshal.SizeOf(rp48_1); - buff = MediaConverter.SerializeRaw(block); - r.Write(buff); - buff = MediaConverter.SerializeRaw(rp48_1); - r.Write(buff); + // ZXSTRAMPAGE + switch (s._machine.Spectrum.MachineType) + { + // For 16k Spectrums, only page 5 (0x4000 - 0x7fff) is saved. + case MachineType.ZXSpectrum16: + block.dwId = MediaConverter.GetUInt32(Encoding.UTF8.GetBytes("RAMP"), 0); + var rp16 = s.GetZXSTRAMPAGE(5, s._machine.RAM0); + block.dwSize = (uint)Marshal.SizeOf(rp16); + buff = MediaConverter.SerializeRaw(block); + r.Write(buff); + buff = MediaConverter.SerializeRaw(rp16); + r.Write(buff); + break; + // For 48k Spectrums and Timex TS/TC models, pages 5, 2 (0x8000 - 0xbfff) and 0 (0xc000 - 0xffff) are saved. + case MachineType.ZXSpectrum48: + block.dwId = MediaConverter.GetUInt32(Encoding.UTF8.GetBytes("RAMP"), 0); + var rp48_0 = s.GetZXSTRAMPAGE(5, s._machine.RAM0); + block.dwSize = (uint)Marshal.SizeOf(rp48_0); + buff = MediaConverter.SerializeRaw(block); + r.Write(buff); + buff = MediaConverter.SerializeRaw(rp48_0); + r.Write(buff); - block.dwId = MediaConverter.GetUInt32(Encoding.UTF8.GetBytes("RAMP"), 0); - var rp48_2 = s.GetZXSTRAMPAGE(5, s._machine.RAM2); - block.dwSize = (uint)Marshal.SizeOf(rp48_2); - buff = MediaConverter.SerializeRaw(block); - r.Write(buff); - buff = MediaConverter.SerializeRaw(rp48_2); - r.Write(buff); - break; - // For 128k Spectrums and the Pentagon 128, all pages (0-7) are saved. - case MachineType.ZXSpectrum128: - case MachineType.ZXSpectrum128Plus2: - case MachineType.ZXSpectrum128Plus2a: - case MachineType.ZXSpectrum128Plus3: - List rams = new List - { - s._machine.RAM0, s._machine.RAM1, s._machine.RAM2, s._machine.RAM3, - s._machine.RAM4, s._machine.RAM5, s._machine.RAM6, s._machine.RAM7 - }; - for (byte i = 0; i < 8; i++) - { - block.dwId = MediaConverter.GetUInt32(Encoding.UTF8.GetBytes("RAMP"), 0); - var rp = s.GetZXSTRAMPAGE(i, rams[i]); - block.dwSize = (uint)Marshal.SizeOf(rp); - buff = MediaConverter.SerializeRaw(block); - r.Write(buff); - buff = MediaConverter.SerializeRaw(rp); - r.Write(buff); - } - break; - } - /* + block.dwId = MediaConverter.GetUInt32(Encoding.UTF8.GetBytes("RAMP"), 0); + var rp48_1 = s.GetZXSTRAMPAGE(5, s._machine.RAM1); + block.dwSize = (uint)Marshal.SizeOf(rp48_1); + buff = MediaConverter.SerializeRaw(block); + r.Write(buff); + buff = MediaConverter.SerializeRaw(rp48_1); + r.Write(buff); + + block.dwId = MediaConverter.GetUInt32(Encoding.UTF8.GetBytes("RAMP"), 0); + var rp48_2 = s.GetZXSTRAMPAGE(5, s._machine.RAM2); + block.dwSize = (uint)Marshal.SizeOf(rp48_2); + buff = MediaConverter.SerializeRaw(block); + r.Write(buff); + buff = MediaConverter.SerializeRaw(rp48_2); + r.Write(buff); + break; + // For 128k Spectrums and the Pentagon 128, all pages (0-7) are saved. + case MachineType.ZXSpectrum128: + case MachineType.ZXSpectrum128Plus2: + case MachineType.ZXSpectrum128Plus2a: + case MachineType.ZXSpectrum128Plus3: + List rams = new List + { + s._machine.RAM0, s._machine.RAM1, s._machine.RAM2, s._machine.RAM3, + s._machine.RAM4, s._machine.RAM5, s._machine.RAM6, s._machine.RAM7 + }; + for (byte i = 0; i < 8; i++) + { + block.dwId = MediaConverter.GetUInt32(Encoding.UTF8.GetBytes("RAMP"), 0); + var rp = s.GetZXSTRAMPAGE(i, rams[i]); + block.dwSize = (uint)Marshal.SizeOf(rp); + buff = MediaConverter.SerializeRaw(block); + r.Write(buff); + buff = MediaConverter.SerializeRaw(rp); + r.Write(buff); + } + break; + } + /* // ZXSTPLUS3 if (s._machine.Spectrum.MachineType == MachineType.ZXSpectrum128Plus3) { @@ -223,201 +223,201 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum r.Write(terminator); } */ - - } - result = ms.ToArray(); - } + } - return result; - } + result = ms.ToArray(); + } - private ZXSTRAMPAGE GetZXSTRAMPAGE(byte page, byte[] RAM) - { - var s = new ZXSTRAMPAGE(); - s.wFlags = 0; // uncompressed only at the moment - s.chPageNo = page; - s.ramPage = RAM; - return s; - } + return result; + } - private ZXSTCREATOR GetZXSTCREATOR() - { - var s = new ZXSTCREATOR(); - var str = "BIZHAWK EMULATOR".ToCharArray(); - s.szCreator = new char[32]; - for (int i = 0; i < str.Length; i++) - s.szCreator[i] = str[i]; - s.chMajorVersion = 1; - s.chMinorVersion = 4; + private ZXSTRAMPAGE GetZXSTRAMPAGE(byte page, byte[] RAM) + { + var s = new ZXSTRAMPAGE(); + s.wFlags = 0; // uncompressed only at the moment + s.chPageNo = page; + s.ramPage = RAM; + return s; + } - return s; - } + private ZXSTCREATOR GetZXSTCREATOR() + { + var s = new ZXSTCREATOR(); + var str = "BIZHAWK EMULATOR".ToCharArray(); + s.szCreator = new char[32]; + for (int i = 0; i < str.Length; i++) + s.szCreator[i] = str[i]; + s.chMajorVersion = 1; + s.chMinorVersion = 4; - private ZXSTZ80REGS GetZXSTZ80REGS() - { - var s = new ZXSTZ80REGS(); - s.AF = (ushort)_machine.Spectrum.GetCpuFlagsAndRegisters()["AF"].Value; - s.BC = (ushort)_machine.Spectrum.GetCpuFlagsAndRegisters()["BC"].Value; - s.DE = (ushort)_machine.Spectrum.GetCpuFlagsAndRegisters()["DE"].Value; - s.HL = (ushort)_machine.Spectrum.GetCpuFlagsAndRegisters()["HL"].Value; - s.AF1 = (ushort)_machine.Spectrum.GetCpuFlagsAndRegisters()["Shadow AF"].Value; - s.BC1 = (ushort)_machine.Spectrum.GetCpuFlagsAndRegisters()["Shadow BC"].Value; - s.DE1 = (ushort)_machine.Spectrum.GetCpuFlagsAndRegisters()["Shadow DE"].Value; - s.HL1 = (ushort)_machine.Spectrum.GetCpuFlagsAndRegisters()["Shadow HL"].Value; - s.IX = (ushort)_machine.Spectrum.GetCpuFlagsAndRegisters()["IX"].Value; - s.IY = (ushort)_machine.Spectrum.GetCpuFlagsAndRegisters()["IY"].Value; - s.SP = (ushort)_machine.Spectrum.GetCpuFlagsAndRegisters()["SP"].Value; - s.PC = (ushort)_machine.Spectrum.GetCpuFlagsAndRegisters()["PC"].Value; - s.I = (byte)_machine.CPU.Regs[_machine.CPU.I]; - s.R = (byte)_machine.CPU.Regs[_machine.CPU.R]; - s.IFF1 = (byte)(_machine.CPU.IFF1 ? 1 : 0); - s.IFF2 = (byte)(_machine.CPU.IFF2 ? 1 : 0); - s.IM = (byte)_machine.CPU.InterruptMode; - s.dwCyclesStart = (uint)(_machine.CurrentFrameCycle + _machine.ULADevice.InterruptStartTime); - s.wMemPtr = (ushort)(_machine.CPU.Regs[_machine.CPU.Z] + (_machine.CPU.Regs[_machine.CPU.W] << 8)); - //s.chHoldIntReqCycles = ? - - if (_machine.CPU.EIPending > 0) - { - s.chFlags |= ZXSTZF_EILAST; - } - else if (_machine.CPU.halted) - { - s.chFlags |= ZXSTZF_HALTED; - } - - return s; - } + return s; + } - private ZXSTSPECREGS GetZXSTSPECREGS() - { - var s = new ZXSTSPECREGS(); - s.chBorder = _machine.ULADevice.BorderColor > 7 ? (byte)0 : (byte)_machine.ULADevice.BorderColor; - s.chFe = _machine.LastFe; - byte x7ffd = (byte)_machine.RAMPaged; - byte x1ffd = 0; - switch (_machine.Spectrum.MachineType) - { - case MachineType.ZXSpectrum16: - case MachineType.ZXSpectrum48: - s.ch7ffd = 0; - s.unionPage = 0; - break; + private ZXSTZ80REGS GetZXSTZ80REGS() + { + var s = new ZXSTZ80REGS(); + s.AF = (ushort)_machine.Spectrum.GetCpuFlagsAndRegisters()["AF"].Value; + s.BC = (ushort)_machine.Spectrum.GetCpuFlagsAndRegisters()["BC"].Value; + s.DE = (ushort)_machine.Spectrum.GetCpuFlagsAndRegisters()["DE"].Value; + s.HL = (ushort)_machine.Spectrum.GetCpuFlagsAndRegisters()["HL"].Value; + s.AF1 = (ushort)_machine.Spectrum.GetCpuFlagsAndRegisters()["Shadow AF"].Value; + s.BC1 = (ushort)_machine.Spectrum.GetCpuFlagsAndRegisters()["Shadow BC"].Value; + s.DE1 = (ushort)_machine.Spectrum.GetCpuFlagsAndRegisters()["Shadow DE"].Value; + s.HL1 = (ushort)_machine.Spectrum.GetCpuFlagsAndRegisters()["Shadow HL"].Value; + s.IX = (ushort)_machine.Spectrum.GetCpuFlagsAndRegisters()["IX"].Value; + s.IY = (ushort)_machine.Spectrum.GetCpuFlagsAndRegisters()["IY"].Value; + s.SP = (ushort)_machine.Spectrum.GetCpuFlagsAndRegisters()["SP"].Value; + s.PC = (ushort)_machine.Spectrum.GetCpuFlagsAndRegisters()["PC"].Value; + s.I = (byte)_machine.CPU.Regs[_machine.CPU.I]; + s.R = (byte)_machine.CPU.Regs[_machine.CPU.R]; + s.IFF1 = (byte)(_machine.CPU.IFF1 ? 1 : 0); + s.IFF2 = (byte)(_machine.CPU.IFF2 ? 1 : 0); + s.IM = (byte)_machine.CPU.InterruptMode; + s.dwCyclesStart = (uint)(_machine.CurrentFrameCycle + _machine.ULADevice.InterruptStartTime); + s.wMemPtr = (ushort)(_machine.CPU.Regs[_machine.CPU.Z] + (_machine.CPU.Regs[_machine.CPU.W] << 8)); + //s.chHoldIntReqCycles = ? - case MachineType.ZXSpectrum128: - case MachineType.ZXSpectrum128Plus2: - // 7FFD - if (_machine._ROMpaged == 1) - x7ffd |= 0x10; - if (_machine.SHADOWPaged) - x7ffd |= 0x08; - if (_machine.PagingDisabled) - x7ffd |= 0x20; - break; + if (_machine.CPU.EIPending > 0) + { + s.chFlags |= ZXSTZF_EILAST; + } + else if (_machine.CPU.halted) + { + s.chFlags |= ZXSTZF_HALTED; + } - case MachineType.ZXSpectrum128Plus2a: - case MachineType.ZXSpectrum128Plus3: - if (_machine.UPDDiskDevice.FDD_FLAG_MOTOR) - x1ffd |= 0x08; - if (_machine.SpecialPagingMode) - { - x1ffd |= 0x01; - switch (_machine.PagingConfiguration) - { - case 1: - x1ffd |= 0x02; - break; - case 2: - x1ffd |= 0x04; - break; - case 3: - x1ffd |= 0x02; - x1ffd |= 0x04; - break; - } - } - else - { - if (_machine.ROMhigh) - x1ffd |= 0x04; - } - if (_machine.ROMlow) - x7ffd |= 0x10; - if (_machine.SHADOWPaged) - x7ffd |= 0x08; - if (_machine.PagingDisabled) - x7ffd |= 0x20; - break; - } + return s; + } - s.ch7ffd = x7ffd; - s.unionPage = x1ffd; - return s; - } + private ZXSTSPECREGS GetZXSTSPECREGS() + { + var s = new ZXSTSPECREGS(); + s.chBorder = _machine.ULADevice.BorderColor > 7 ? (byte)0 : (byte)_machine.ULADevice.BorderColor; + s.chFe = _machine.LastFe; + byte x7ffd = (byte)_machine.RAMPaged; + byte x1ffd = 0; + switch (_machine.Spectrum.MachineType) + { + case MachineType.ZXSpectrum16: + case MachineType.ZXSpectrum48: + s.ch7ffd = 0; + s.unionPage = 0; + break; - private ZXSTKEYBOARD GetZXSTKEYBOARD() - { - var s = new ZXSTKEYBOARD(); - s.dwFlags = 0; //no issue 2 emulation - s.chKeyboardJoystick |= (byte)JoystickTypes.ZXSTKJT_NONE; - return s; - } + case MachineType.ZXSpectrum128: + case MachineType.ZXSpectrum128Plus2: + // 7FFD + if (_machine._ROMpaged == 1) + x7ffd |= 0x10; + if (_machine.SHADOWPaged) + x7ffd |= 0x08; + if (_machine.PagingDisabled) + x7ffd |= 0x20; + break; - private ZXSTJOYSTICK GetZXSTJOYSTICK() - { - var s = new ZXSTJOYSTICK(); - s.dwFlags = 0; //depreciated - s.chTypePlayer1 |= (byte)JoystickTypes.ZXSTKJT_KEMPSTON; - s.chTypePlayer2 |= (byte)JoystickTypes.ZXSTKJT_SINCLAIR1; - return s; - } + case MachineType.ZXSpectrum128Plus2a: + case MachineType.ZXSpectrum128Plus3: + if (_machine.UPDDiskDevice.FDD_FLAG_MOTOR) + x1ffd |= 0x08; + if (_machine.SpecialPagingMode) + { + x1ffd |= 0x01; + switch (_machine.PagingConfiguration) + { + case 1: + x1ffd |= 0x02; + break; + case 2: + x1ffd |= 0x04; + break; + case 3: + x1ffd |= 0x02; + x1ffd |= 0x04; + break; + } + } + else + { + if (_machine.ROMhigh) + x1ffd |= 0x04; + } + if (_machine.ROMlow) + x7ffd |= 0x10; + if (_machine.SHADOWPaged) + x7ffd |= 0x08; + if (_machine.PagingDisabled) + x7ffd |= 0x20; + break; + } - private ZXSTAYBLOCK GetZXSTAYBLOCK() - { - var s = new ZXSTAYBLOCK(); - s.cFlags = 0; // no external units - s.chCurrentRegister = (byte)_machine.AYDevice.SelectedRegister; - var regs = _machine.AYDevice.ExportRegisters(); - s.chAyRegs = new byte[16]; - for (int i = 0; i < 16; i++) - { - s.chAyRegs[i] = (byte)regs[i]; - } - return s; - } + s.ch7ffd = x7ffd; + s.unionPage = x1ffd; + return s; + } - private ZXSTTAPE GetZXSTTAPE() - { - var s = new ZXSTTAPE(); - s.wFlags |= (int)CassetteRecorderState.ZXSTTP_EMBEDDED; - s.wCurrentBlockNo = (ushort)_machine.TapeDevice.CurrentDataBlockIndex; - s.dwCompressedSize = _machine.tapeImages[_machine.TapeDevice.CurrentDataBlockIndex].Length; - s.dwUncompressedSize = _machine.tapeImages[_machine.TapeDevice.CurrentDataBlockIndex].Length; - char[] ext = "tzx".ToCharArray(); - s.szFileExtension = new char[16]; - for (int f = 1; f < ext.Length; f++) - { - s.szFileExtension[f - 1] = ext[f]; - } - return s; - } + private ZXSTKEYBOARD GetZXSTKEYBOARD() + { + var s = new ZXSTKEYBOARD(); + s.dwFlags = 0; //no issue 2 emulation + s.chKeyboardJoystick |= (byte)JoystickTypes.ZXSTKJT_NONE; + return s; + } - private ZXSTPLUS3 GetZXSTPLUS3() - { - var s = new ZXSTPLUS3(); - s.chNumDrives = 1; - s.fMotorOn = _machine.UPDDiskDevice.FDD_FLAG_MOTOR ? (byte)1 : (byte)0; - return s; - } + private ZXSTJOYSTICK GetZXSTJOYSTICK() + { + var s = new ZXSTJOYSTICK(); + s.dwFlags = 0; //depreciated + s.chTypePlayer1 |= (byte)JoystickTypes.ZXSTKJT_KEMPSTON; + s.chTypePlayer2 |= (byte)JoystickTypes.ZXSTKJT_SINCLAIR1; + return s; + } - private ZXSTDSKFILE GetZXSTDSKFILE() - { - var s = new ZXSTDSKFILE(); - s.wFlags = 0; - s.chDriveNum = 0; - s.dwUncompressedSize = 0; - return s; - } - } + private ZXSTAYBLOCK GetZXSTAYBLOCK() + { + var s = new ZXSTAYBLOCK(); + s.cFlags = 0; // no external units + s.chCurrentRegister = (byte)_machine.AYDevice.SelectedRegister; + var regs = _machine.AYDevice.ExportRegisters(); + s.chAyRegs = new byte[16]; + for (int i = 0; i < 16; i++) + { + s.chAyRegs[i] = (byte)regs[i]; + } + return s; + } + + private ZXSTTAPE GetZXSTTAPE() + { + var s = new ZXSTTAPE(); + s.wFlags |= (int)CassetteRecorderState.ZXSTTP_EMBEDDED; + s.wCurrentBlockNo = (ushort)_machine.TapeDevice.CurrentDataBlockIndex; + s.dwCompressedSize = _machine.tapeImages[_machine.TapeDevice.CurrentDataBlockIndex].Length; + s.dwUncompressedSize = _machine.tapeImages[_machine.TapeDevice.CurrentDataBlockIndex].Length; + char[] ext = "tzx".ToCharArray(); + s.szFileExtension = new char[16]; + for (int f = 1; f < ext.Length; f++) + { + s.szFileExtension[f - 1] = ext[f]; + } + return s; + } + + private ZXSTPLUS3 GetZXSTPLUS3() + { + var s = new ZXSTPLUS3(); + s.chNumDrives = 1; + s.fMotorOn = _machine.UPDDiskDevice.FDD_FLAG_MOTOR ? (byte)1 : (byte)0; + return s; + } + + private ZXSTDSKFILE GetZXSTDSKFILE() + { + var s = new ZXSTDSKFILE(); + s.wFlags = 0; + s.chDriveNum = 0; + s.dwUncompressedSize = 0; + return s; + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Snapshot/SZX/SZX.Objects.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Snapshot/SZX/SZX.Objects.cs index 3bbe9acead..50699fa0bc 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Snapshot/SZX/SZX.Objects.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Snapshot/SZX/SZX.Objects.cs @@ -7,404 +7,404 @@ using System.Threading.Tasks; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Structs, Constants and Enums - /// http://www.spectaculator.com/docs/zx-state/intro.shtml - /// - public partial class SZX - { - #region ZX-State Header - - public enum MachineIdentifier : byte - { - ZXSTMID_16K = 0, - ZXSTMID_48K = 1, - ZXSTMID_128K = 2, - ZXSTMID_PLUS2 = 3, - ZXSTMID_PLUS2A = 4, - ZXSTMID_PLUS3 = 5, - ZXSTMID_PLUS3E = 6, - ZXSTMID_PENTAGON128 = 7, - ZXSTMID_TC2048 = 8, - ZXSTMID_TC2068 = 9, - ZXSTMID_SCORPION = 10, - ZXSTMID_SE = 11, - ZXSTMID_TS2068 = 12, - ZXSTMID_PENTAGON512 = 13, - ZXSTMID_PENTAGON1024 = 14, - ZXSTMID_NTSC48K = 15, - ZXSTMID_128KE = 16 - } - - /// - /// If set, the emulated Spectrum uses alternate timings (one cycle later than normal timings). If reset, the emulated Spectrum uses standard timings. - /// This flag is only applicable for the ZXSTMID_16K, ZXSTMID_48K and ZXSTMID_128K models. - /// - public const int ZXSTMF_ALTERNATETIMINGS = 1; - - /// - /// The zx-state header appears right at the start of a zx-state (.szx) file. - /// - [StructLayout(LayoutKind.Sequential, Pack = 1)] - public struct ZXSTHEADER - { - public uint dwMagic; - public byte chMajorVersion; - public byte chMinorVersion; - public byte chMachineId; - public byte chFlags; - } - - #endregion - - #region ZXSTBLOCK Header - - /// - /// Block Header. Each real block starts with this header. - /// - public struct ZXSTBLOCK - { - public uint dwId; - public uint dwSize; - } - - #endregion - - #region ZXSTCREATOR - - /// - /// This block identifies the program that created this zx-state file. - /// - [StructLayout(LayoutKind.Sequential, Pack = 1)] - public struct ZXSTCREATOR - { - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] - public char[] szCreator; - public short chMajorVersion; - public short chMinorVersion; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] - public byte[] chData; - } - - #endregion - - #region ZXSTZ80REGS - - /// - /// The last instruction executed was an EI instruction or an invalid $DD or $FD prefix. - /// - public const int ZXSTZF_EILAST = 1; - /// - /// The last instruction executed was a HALT instruction. The CPU is currently executing NOPs and will continue to do so until the next interrupt occurs. - /// This flag is mutually exclusive with ZXSTZF_EILAST. - /// - public const int ZXSTZF_HALTED = 2; - - /// - /// Contains the Z80 registers and other internal state values. It does not contain any specific model registers. - /// - [StructLayout(LayoutKind.Sequential, Pack = 1)] - public struct ZXSTZ80REGS - { - public ushort AF, BC, DE, HL; - public ushort AF1, BC1, DE1, HL1; - public ushort IX, IY, SP, PC; - public byte I; - public byte R; - public byte IFF1, IFF2; - public byte IM; - public uint dwCyclesStart; - public byte chHoldIntReqCycles; - public byte chFlags; - public ushort wMemPtr; - } - - #endregion - - #region ZXSTSPECREGS - - [StructLayout(LayoutKind.Sequential, Pack = 1)] - public struct ZXSTSPECREGS - { - public byte chBorder; - public byte ch7ffd; - public byte unionPage; - public byte chFe; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] - public byte[] chReserved; - } - - #endregion - - #region ZXSTAYBLOCK - - /// - /// Fuller Box emulation - /// - public const int ZXSTAYF_FULLERBOX = 1; - /// - /// Melodik Soundbox emulation. - /// This is essentially an AY chip for older Spectrums that uses the same ports as that found in 128k Spectrums - /// - public const int ZXSTAYF_128AY = 2; - - /// - /// The state of the AY chip found in all 128k Spectrums, Pentagons, Scorpions and Timex machines. - /// This block may also be present for 16k/48k Spectrums if Fuller Box or Melodik emulation is enabled. - /// - [StructLayout(LayoutKind.Sequential, Pack = 1)] - public struct ZXSTAYBLOCK - { - public byte cFlags; - public byte chCurrentRegister; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] - public byte[] chAyRegs; - } - - #endregion - - #region ZXSTRAMPAGE - - /// - /// Ram pages are compressed using Zlib - /// - public const int ZXSTRF_COMPRESSED = 1; - - /// - /// zx-state files will contain a number of 16KB RAM page blocks, depending on the specific Spectrum model. - /// - [StructLayout(LayoutKind.Sequential, Pack = 1)] - public struct ZXSTRAMPAGE - { - public ushort wFlags; - public byte chPageNo; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x4000)] - public byte[] ramPage; - } - - #endregion - - #region ZXSTKEYBOARD - - /// - /// Keyboard state - /// - public const int ZXSTKF_ISSUE2 = 1; - - /// - /// Supported joystick types - /// - public enum JoystickTypes - { - ZXSTKJT_KEMPSTON = 0, - ZXSTKJT_FULLER = 1, - ZXSTKJT_CURSOR = 2, - ZXSTKJT_SINCLAIR1 = 3, - ZXSTKJT_SINCLAIR2 = 4, - ZXSTKJT_SPECTRUMPLUS = 5, - ZXSTKJT_TIMEX1 = 6, - ZXSTKJT_TIMEX2 = 7, - ZXSTKJT_NONE = 8 - } - - /// - /// The state of the Spectrum keyboard and any keyboard joystick emulation. - /// - [StructLayout(LayoutKind.Sequential, Pack = 1)] - public struct ZXSTKEYBOARD - { - public uint dwFlags; - public byte chKeyboardJoystick; - } - - #endregion - - #region ZXSTJOYSTICK - - /// - /// Joystick setup for both players. - /// - [StructLayout(LayoutKind.Sequential, Pack = 1)] - public struct ZXSTJOYSTICK - { - public uint dwFlags; - public byte chTypePlayer1; - public byte chTypePlayer2; - } - - #endregion - - #region ZXSTTAPE - - /// - /// Cassette Recorder state - /// - public enum CassetteRecorderState - { - ZXSTTP_EMBEDDED = 1, - ZXSTTP_COMPRESSED = 2 - } - - [StructLayout(LayoutKind.Sequential, Pack = 1)] - public struct ZXSTTAPE - { - public ushort wCurrentBlockNo; - public ushort wFlags; - public int dwUncompressedSize; - public int dwCompressedSize; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] - public char[] szFileExtension; - } + /// + /// Structs, Constants and Enums + /// http://www.spectaculator.com/docs/zx-state/intro.shtml + /// + public partial class SZX + { + #region ZX-State Header + + public enum MachineIdentifier : byte + { + ZXSTMID_16K = 0, + ZXSTMID_48K = 1, + ZXSTMID_128K = 2, + ZXSTMID_PLUS2 = 3, + ZXSTMID_PLUS2A = 4, + ZXSTMID_PLUS3 = 5, + ZXSTMID_PLUS3E = 6, + ZXSTMID_PENTAGON128 = 7, + ZXSTMID_TC2048 = 8, + ZXSTMID_TC2068 = 9, + ZXSTMID_SCORPION = 10, + ZXSTMID_SE = 11, + ZXSTMID_TS2068 = 12, + ZXSTMID_PENTAGON512 = 13, + ZXSTMID_PENTAGON1024 = 14, + ZXSTMID_NTSC48K = 15, + ZXSTMID_128KE = 16 + } + + /// + /// If set, the emulated Spectrum uses alternate timings (one cycle later than normal timings). If reset, the emulated Spectrum uses standard timings. + /// This flag is only applicable for the ZXSTMID_16K, ZXSTMID_48K and ZXSTMID_128K models. + /// + public const int ZXSTMF_ALTERNATETIMINGS = 1; + + /// + /// The zx-state header appears right at the start of a zx-state (.szx) file. + /// + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct ZXSTHEADER + { + public uint dwMagic; + public byte chMajorVersion; + public byte chMinorVersion; + public byte chMachineId; + public byte chFlags; + } + + #endregion + + #region ZXSTBLOCK Header + + /// + /// Block Header. Each real block starts with this header. + /// + public struct ZXSTBLOCK + { + public uint dwId; + public uint dwSize; + } + + #endregion + + #region ZXSTCREATOR + + /// + /// This block identifies the program that created this zx-state file. + /// + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct ZXSTCREATOR + { + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] + public char[] szCreator; + public short chMajorVersion; + public short chMinorVersion; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] + public byte[] chData; + } + + #endregion + + #region ZXSTZ80REGS + + /// + /// The last instruction executed was an EI instruction or an invalid $DD or $FD prefix. + /// + public const int ZXSTZF_EILAST = 1; + /// + /// The last instruction executed was a HALT instruction. The CPU is currently executing NOPs and will continue to do so until the next interrupt occurs. + /// This flag is mutually exclusive with ZXSTZF_EILAST. + /// + public const int ZXSTZF_HALTED = 2; + + /// + /// Contains the Z80 registers and other internal state values. It does not contain any specific model registers. + /// + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct ZXSTZ80REGS + { + public ushort AF, BC, DE, HL; + public ushort AF1, BC1, DE1, HL1; + public ushort IX, IY, SP, PC; + public byte I; + public byte R; + public byte IFF1, IFF2; + public byte IM; + public uint dwCyclesStart; + public byte chHoldIntReqCycles; + public byte chFlags; + public ushort wMemPtr; + } + + #endregion + + #region ZXSTSPECREGS + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct ZXSTSPECREGS + { + public byte chBorder; + public byte ch7ffd; + public byte unionPage; + public byte chFe; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] + public byte[] chReserved; + } + + #endregion + + #region ZXSTAYBLOCK + + /// + /// Fuller Box emulation + /// + public const int ZXSTAYF_FULLERBOX = 1; + /// + /// Melodik Soundbox emulation. + /// This is essentially an AY chip for older Spectrums that uses the same ports as that found in 128k Spectrums + /// + public const int ZXSTAYF_128AY = 2; + + /// + /// The state of the AY chip found in all 128k Spectrums, Pentagons, Scorpions and Timex machines. + /// This block may also be present for 16k/48k Spectrums if Fuller Box or Melodik emulation is enabled. + /// + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct ZXSTAYBLOCK + { + public byte cFlags; + public byte chCurrentRegister; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] + public byte[] chAyRegs; + } + + #endregion + + #region ZXSTRAMPAGE + + /// + /// Ram pages are compressed using Zlib + /// + public const int ZXSTRF_COMPRESSED = 1; + + /// + /// zx-state files will contain a number of 16KB RAM page blocks, depending on the specific Spectrum model. + /// + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct ZXSTRAMPAGE + { + public ushort wFlags; + public byte chPageNo; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x4000)] + public byte[] ramPage; + } + + #endregion + + #region ZXSTKEYBOARD + + /// + /// Keyboard state + /// + public const int ZXSTKF_ISSUE2 = 1; + + /// + /// Supported joystick types + /// + public enum JoystickTypes + { + ZXSTKJT_KEMPSTON = 0, + ZXSTKJT_FULLER = 1, + ZXSTKJT_CURSOR = 2, + ZXSTKJT_SINCLAIR1 = 3, + ZXSTKJT_SINCLAIR2 = 4, + ZXSTKJT_SPECTRUMPLUS = 5, + ZXSTKJT_TIMEX1 = 6, + ZXSTKJT_TIMEX2 = 7, + ZXSTKJT_NONE = 8 + } + + /// + /// The state of the Spectrum keyboard and any keyboard joystick emulation. + /// + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct ZXSTKEYBOARD + { + public uint dwFlags; + public byte chKeyboardJoystick; + } + + #endregion + + #region ZXSTJOYSTICK + + /// + /// Joystick setup for both players. + /// + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct ZXSTJOYSTICK + { + public uint dwFlags; + public byte chTypePlayer1; + public byte chTypePlayer2; + } + + #endregion + + #region ZXSTTAPE + + /// + /// Cassette Recorder state + /// + public enum CassetteRecorderState + { + ZXSTTP_EMBEDDED = 1, + ZXSTTP_COMPRESSED = 2 + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct ZXSTTAPE + { + public ushort wCurrentBlockNo; + public ushort wFlags; + public int dwUncompressedSize; + public int dwCompressedSize; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] + public char[] szFileExtension; + } - #endregion + #endregion - #region ZXSTPLUS3 + #region ZXSTPLUS3 - /// - /// The number of drives connected to the Spectrum +3 and whether their motors are turned on. - /// Any blocks specifying which disk files are in which drive will follow this one. - /// - [StructLayout(LayoutKind.Sequential, Pack = 1)] - public struct ZXSTPLUS3 - { - public byte chNumDrives; - public byte fMotorOn; - } + /// + /// The number of drives connected to the Spectrum +3 and whether their motors are turned on. + /// Any blocks specifying which disk files are in which drive will follow this one. + /// + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct ZXSTPLUS3 + { + public byte chNumDrives; + public byte fMotorOn; + } - #endregion + #endregion - #region ZXSTDSKFILE + #region ZXSTDSKFILE - /// - /// Not implemented. All disk images are currently links to external .dsk or .ipf files - /// - public const int ZXSTDSKF_COMPRESSED = 1; - /// - /// Not implemented. All disk images are currently links to external .dsk or .ipf files - /// - public const int ZXSTDSKF_EMBEDDED = 2; - /// - /// When a double-sided disk is inserted into a single-sided drive, specifies the side being read from/written to. - /// If set, Side B is the active side, otherwise it is Side A. - /// - public const int ZXSTDSKF_SIDEB = 3; + /// + /// Not implemented. All disk images are currently links to external .dsk or .ipf files + /// + public const int ZXSTDSKF_COMPRESSED = 1; + /// + /// Not implemented. All disk images are currently links to external .dsk or .ipf files + /// + public const int ZXSTDSKF_EMBEDDED = 2; + /// + /// When a double-sided disk is inserted into a single-sided drive, specifies the side being read from/written to. + /// If set, Side B is the active side, otherwise it is Side A. + /// + public const int ZXSTDSKF_SIDEB = 3; - /// - /// Each +3 disk drive that has a disk inserted in it will have one of these blocks. - /// They follow the ZXSTPLUS3 block which identifies the number of drives. - /// - [StructLayout(LayoutKind.Sequential, Pack = 1)] - public struct ZXSTDSKFILE - { - public ushort wFlags; - public byte chDriveNum; - public int dwUncompressedSize; - } + /// + /// Each +3 disk drive that has a disk inserted in it will have one of these blocks. + /// They follow the ZXSTPLUS3 block which identifies the number of drives. + /// + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct ZXSTDSKFILE + { + public ushort wFlags; + public byte chDriveNum; + public int dwUncompressedSize; + } - #endregion + #endregion - #region Not Yet Implemented + #region Not Yet Implemented - #region ZXSTATASP + #region ZXSTATASP - #endregion + #endregion - #region ZXSTATARAM + #region ZXSTATARAM - #endregion + #endregion - #region ZXSTCF + #region ZXSTCF - #endregion + #endregion - #region ZXSTCFRAM + #region ZXSTCFRAM - #endregion + #endregion - #region ZXSTCOVOX + #region ZXSTCOVOX - #endregion + #endregion - #region ZXSTBETA128 + #region ZXSTBETA128 - #endregion + #endregion - #region ZXSTBETADISK + #region ZXSTBETADISK - #endregion + #endregion - #region ZXSTDOCK + #region ZXSTDOCK - #endregion + #endregion - #region ZXSTGS + #region ZXSTGS - #endregion + #endregion - #region ZXSTGSRAMPAGE + #region ZXSTGSRAMPAGE - #endregion + #endregion - #region ZXSTIF1 + #region ZXSTIF1 - #endregion + #endregion - #region ZXSTIF2ROM + #region ZXSTIF2ROM - #endregion + #endregion - #region ZXSTMCART + #region ZXSTMCART - #endregion + #endregion - #region ZXSTMOUSE + #region ZXSTMOUSE - #endregion + #endregion - #region ZXSTMULTIFACE + #region ZXSTMULTIFACE - #endregion + #endregion - #region ZXSTOPUS + #region ZXSTOPUS - #endregion + #endregion - #region ZXSTOPUSDISK + #region ZXSTOPUSDISK - #endregion + #endregion - #region ZXSTPLUSD + #region ZXSTPLUSD - #endregion + #endregion - #region ZXSTPLUSDDISK + #region ZXSTPLUSDDISK - #endregion + #endregion - #region ZXSTROM + #region ZXSTROM - #endregion + #endregion - #region ZXSTSCLDREGS + #region ZXSTSCLDREGS - #endregion + #endregion - #region ZXSTSIDE + #region ZXSTSIDE - #endregion + #endregion - #region ZXSTSPECDRUM + #region ZXSTSPECDRUM - #endregion + #endregion - #region ZXSTUSPEECH + #region ZXSTUSPEECH - #endregion + #endregion - #region ZXSTZXPRINTER + #region ZXSTZXPRINTER - #endregion + #endregion - #endregion - } + #endregion + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/CSW/CswConverter.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/CSW/CswConverter.cs index 0506b1e232..679370c5ca 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/CSW/CswConverter.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/CSW/CswConverter.cs @@ -6,121 +6,121 @@ using System.Text; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Responsible for Compressed Square Wave conversion - /// https://web.archive.org/web/20171024182530/http://ramsoft.bbk.org.omegahg.com/csw.html - /// - public class CswConverter : MediaConverter - { - /// - /// The type of serializer - /// - private MediaConverterType _formatType = MediaConverterType.CSW; - public override MediaConverterType FormatType - { - get - { - return _formatType; - } - } + /// + /// Responsible for Compressed Square Wave conversion + /// https://web.archive.org/web/20171024182530/http://ramsoft.bbk.org.omegahg.com/csw.html + /// + public class CswConverter : MediaConverter + { + /// + /// The type of serializer + /// + private MediaConverterType _formatType = MediaConverterType.CSW; + public override MediaConverterType FormatType + { + get + { + return _formatType; + } + } - /// - /// Position counter - /// - private int _position = 0; + /// + /// Position counter + /// + private int _position = 0; - /// - /// Signs whether this class can be used to read the data format - /// - public override bool IsReader { get { return true; } } + /// + /// Signs whether this class can be used to read the data format + /// + public override bool IsReader { get { return true; } } - /// - /// Signs whether this class can be used to write the data format - /// - public override bool IsWriter { get { return false; } } + /// + /// Signs whether this class can be used to write the data format + /// + public override bool IsWriter { get { return false; } } - #region Construction + #region Construction - private DatacorderDevice _datacorder; + private DatacorderDevice _datacorder; - public CswConverter(DatacorderDevice _tapeDevice) - { - _datacorder = _tapeDevice; - } + public CswConverter(DatacorderDevice _tapeDevice) + { + _datacorder = _tapeDevice; + } - #endregion + #endregion - /// - /// Returns TRUE if pzx header is detected - /// - public override bool CheckType(byte[] data) - { - // CSW Header + /// + /// Returns TRUE if pzx header is detected + /// + public override bool CheckType(byte[] data) + { + // CSW Header - // check whether this is a valid csw format file by looking at the identifier in the header - // (first 22 bytes of the file) - string ident = Encoding.ASCII.GetString(data, 0, 22); + // check whether this is a valid csw format file by looking at the identifier in the header + // (first 22 bytes of the file) + string ident = Encoding.ASCII.GetString(data, 0, 22); - // version info - int majorVer = data[8]; - int minorVer = data[9]; + // version info + int majorVer = data[8]; + int minorVer = data[9]; - if (ident.ToUpper() != "COMPRESSED SQUARE WAVE") - { - // this is not a valid CSW format file - return false; - } - else - { - return true; - } - } + if (ident.ToUpper() != "COMPRESSED SQUARE WAVE") + { + // this is not a valid CSW format file + return false; + } + else + { + return true; + } + } - /// - /// DeSerialization method - /// - public override void Read(byte[] data) - { - // clear existing tape blocks - _datacorder.DataBlocks.Clear(); + /// + /// DeSerialization method + /// + public override void Read(byte[] data) + { + // clear existing tape blocks + _datacorder.DataBlocks.Clear(); - // CSW Header + // CSW Header - // check whether this is a valid csw format file by looking at the identifier in the header - // (first 22 bytes of the file) - string ident = Encoding.ASCII.GetString(data, 0, 22); + // check whether this is a valid csw format file by looking at the identifier in the header + // (first 22 bytes of the file) + string ident = Encoding.ASCII.GetString(data, 0, 22); - if (ident.ToUpper() != "COMPRESSED SQUARE WAVE") - { - // this is not a valid CSW format file - throw new Exception(this.GetType().ToString() + - "This is not a valid CSW format file"); - } + if (ident.ToUpper() != "COMPRESSED SQUARE WAVE") + { + // this is not a valid CSW format file + throw new Exception(this.GetType().ToString() + + "This is not a valid CSW format file"); + } - if (data[0x16] != 0x1a) - { - // invalid terminator code - throw new Exception(this.GetType().ToString() + - "This image reports as a CSW but has an invalid terminator code"); - } + if (data[0x16] != 0x1a) + { + // invalid terminator code + throw new Exception(this.GetType().ToString() + + "This image reports as a CSW but has an invalid terminator code"); + } - _position = 0; + _position = 0; - // version info - int majorVer = data[0x17]; - int minorVer = data[0x18]; + // version info + int majorVer = data[0x17]; + int minorVer = data[0x18]; - int sampleRate; - int totalPulses; - byte compressionType; - byte flags; - byte headerExtensionLen; - byte[] cswData; - byte[] cswDataUncompressed; + int sampleRate; + int totalPulses; + byte compressionType; + byte flags; + byte headerExtensionLen; + byte[] cswData; + byte[] cswDataUncompressed; - if (majorVer == 2) - { - /* + if (majorVer == 2) + { + /* CSW-2 Header CSW global file header - status: required Offset Value Type Description @@ -143,28 +143,28 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum 0x34+HDR - - CSW data. */ - _position = 0x19; - sampleRate = GetInt32(data, _position); - _position += 4; + _position = 0x19; + sampleRate = GetInt32(data, _position); + _position += 4; - totalPulses = GetInt32(data, _position); - cswDataUncompressed = new byte[totalPulses + 1]; - _position += 4; + totalPulses = GetInt32(data, _position); + cswDataUncompressed = new byte[totalPulses + 1]; + _position += 4; - compressionType = data[_position++]; - flags = data[_position++]; - headerExtensionLen = data[_position++]; - - _position = 0x34 + headerExtensionLen; + compressionType = data[_position++]; + flags = data[_position++]; + headerExtensionLen = data[_position++]; - cswData = new byte[data.Length - _position]; - Array.Copy(data, _position, cswData, 0, cswData.Length); + _position = 0x34 + headerExtensionLen; - ProcessCSWV2(cswData, ref cswDataUncompressed, compressionType, totalPulses); - } - else if (majorVer == 1) - { - /* + cswData = new byte[data.Length - _position]; + Array.Copy(data, _position, cswData, 0, cswData.Length); + + ProcessCSWV2(cswData, ref cswDataUncompressed, compressionType, totalPulses); + } + else if (majorVer == 1) + { + /* CSW-1 Header CSW global file header - status: required Offset Value Type Description @@ -181,81 +181,81 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum 0x20 - - CSW data. */ - _position = 0x19; - sampleRate = GetWordValue(data, _position); - _position += 2; + _position = 0x19; + sampleRate = GetWordValue(data, _position); + _position += 2; - compressionType = data[_position++]; - flags = data[_position++]; + compressionType = data[_position++]; + flags = data[_position++]; - _position += 3; + _position += 3; - cswDataUncompressed = new byte[data.Length - _position]; + cswDataUncompressed = new byte[data.Length - _position]; - if (compressionType == 1) - Array.Copy(data, _position, cswDataUncompressed, 0, cswDataUncompressed.Length); - else - throw new Exception(this.GetType().ToString() + - "CSW Format unknown compression type"); - } - else - { - throw new Exception(this.GetType().ToString() + - "CSW Format Version " + majorVer + "." + minorVer + " is not currently supported"); - } + if (compressionType == 1) + Array.Copy(data, _position, cswDataUncompressed, 0, cswDataUncompressed.Length); + else + throw new Exception(this.GetType().ToString() + + "CSW Format unknown compression type"); + } + else + { + throw new Exception(this.GetType().ToString() + + "CSW Format Version " + majorVer + "." + minorVer + " is not currently supported"); + } - // create the single tape block - // (use DATA block for now so initial signal level is handled correctly by the datacorder device) - TapeDataBlock t = new TapeDataBlock(); - t.BlockDescription = BlockType.CSW_Recording; - t.BlockID = 0x18; - t.DataPeriods = new List(); + // create the single tape block + // (use DATA block for now so initial signal level is handled correctly by the datacorder device) + TapeDataBlock t = new TapeDataBlock(); + t.BlockDescription = BlockType.CSW_Recording; + t.BlockID = 0x18; + t.DataPeriods = new List(); - if (flags.Bit(0)) - t.InitialPulseLevel = true; - else - t.InitialPulseLevel = false; + if (flags.Bit(0)) + t.InitialPulseLevel = true; + else + t.InitialPulseLevel = false; - var rate = (69888 * 50) / sampleRate; + var rate = (69888 * 50) / sampleRate; - for (int i = 0; i < cswDataUncompressed.Length;) - { - int length = cswDataUncompressed[i++] * rate; - if (length == 0) - { - length = GetInt32(cswDataUncompressed, i) / rate; - i += 4; - } + for (int i = 0; i < cswDataUncompressed.Length;) + { + int length = cswDataUncompressed[i++] * rate; + if (length == 0) + { + length = GetInt32(cswDataUncompressed, i) / rate; + i += 4; + } - t.DataPeriods.Add(length); - } + t.DataPeriods.Add(length); + } - // add closing period - t.DataPeriods.Add((69888 * 50) / 10); + // add closing period + t.DataPeriods.Add((69888 * 50) / 10); - // add to datacorder - _datacorder.DataBlocks.Add(t); - } + // add to datacorder + _datacorder.DataBlocks.Add(t); + } - /// - /// Processes a CSW v2 data block - /// - public static void ProcessCSWV2( - byte[] srcBuff, - ref byte[] destBuff, - byte compType, - int pulseCount) - { - if (compType == 1) - { - Array.Copy(srcBuff, 0, destBuff, 0, pulseCount); - } - else if (compType == 2) - { - DecompressZRLE(srcBuff, ref destBuff); - } - else - throw new Exception("CSW Format unknown compression type"); - } - } + /// + /// Processes a CSW v2 data block + /// + public static void ProcessCSWV2( + byte[] srcBuff, + ref byte[] destBuff, + byte compType, + int pulseCount) + { + if (compType == 1) + { + Array.Copy(srcBuff, 0, destBuff, 0, pulseCount); + } + else if (compType == 2) + { + DecompressZRLE(srcBuff, ref destBuff); + } + else + throw new Exception("CSW Format unknown compression type"); + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/PZX/PzxConverter.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/PZX/PzxConverter.cs index d8eb9e1f07..6201b52b83 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/PZX/PzxConverter.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/PZX/PzxConverter.cs @@ -6,95 +6,95 @@ using System.Text; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Reponsible for PZX format serializaton - /// Based on the information here: http://zxds.raxoft.cz/docs/pzx.txt - /// - public class PzxConverter : MediaConverter - { - /// - /// The type of serializer - /// - private MediaConverterType _formatType = MediaConverterType.PZX; - public override MediaConverterType FormatType - { - get - { - return _formatType; - } - } + /// + /// Reponsible for PZX format serializaton + /// Based on the information here: http://zxds.raxoft.cz/docs/pzx.txt + /// + public class PzxConverter : MediaConverter + { + /// + /// The type of serializer + /// + private MediaConverterType _formatType = MediaConverterType.PZX; + public override MediaConverterType FormatType + { + get + { + return _formatType; + } + } - /// - /// Signs whether this class can be used to read the data format - /// - public override bool IsReader { get { return true; } } + /// + /// Signs whether this class can be used to read the data format + /// + public override bool IsReader { get { return true; } } - /// - /// Signs whether this class can be used to write the data format - /// - public override bool IsWriter { get { return false; } } + /// + /// Signs whether this class can be used to write the data format + /// + public override bool IsWriter { get { return false; } } - /// - /// Working list of generated tape data blocks - /// - private List _blocks = new List(); + /// + /// Working list of generated tape data blocks + /// + private List _blocks = new List(); - /// - /// Position counter - /// - private int _position = 0; + /// + /// Position counter + /// + private int _position = 0; - /// - /// Object to keep track of loops - this assumes there is only one loop at a time - /// - private List> _loopCounter = new List>(); + /// + /// Object to keep track of loops - this assumes there is only one loop at a time + /// + private List> _loopCounter = new List>(); - #region Construction + #region Construction - private DatacorderDevice _datacorder; + private DatacorderDevice _datacorder; - public PzxConverter(DatacorderDevice _tapeDevice) - { - _datacorder = _tapeDevice; - } + public PzxConverter(DatacorderDevice _tapeDevice) + { + _datacorder = _tapeDevice; + } - #endregion + #endregion - /// - /// Returns TRUE if pzx header is detected - /// - public override bool CheckType(byte[] data) - { - // PZX Header + /// + /// Returns TRUE if pzx header is detected + /// + public override bool CheckType(byte[] data) + { + // PZX Header - // check whether this is a valid pzx format file by looking at the identifier in the header - // (first 4 bytes of the file) - string ident = Encoding.ASCII.GetString(data, 0, 4); + // check whether this is a valid pzx format file by looking at the identifier in the header + // (first 4 bytes of the file) + string ident = Encoding.ASCII.GetString(data, 0, 4); - // version info - int majorVer = data[8]; - int minorVer = data[9]; + // version info + int majorVer = data[8]; + int minorVer = data[9]; - if (ident.ToUpper() != "PZXT") - { - // this is not a valid PZX format file - return false; - } - else - { - return true; - } - } + if (ident.ToUpper() != "PZXT") + { + // this is not a valid PZX format file + return false; + } + else + { + return true; + } + } - /// - /// DeSerialization method - /// - public override void Read(byte[] data) - { - // clear existing tape blocks - _datacorder.DataBlocks.Clear(); + /// + /// DeSerialization method + /// + public override void Read(byte[] data) + { + // clear existing tape blocks + _datacorder.DataBlocks.Clear(); - /* + /* // PZX uniform block layout offset type name meaning ------ ---- ---- ------- @@ -103,62 +103,62 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum 8 u8[size] data arbitrary amount of block data. */ - // check whether this is a valid pzx format file by looking at the identifier in the header block - string ident = Encoding.ASCII.GetString(data, 0, 4); + // check whether this is a valid pzx format file by looking at the identifier in the header block + string ident = Encoding.ASCII.GetString(data, 0, 4); - if (ident.ToUpper() != "PZXT") - { - // this is not a valid TZX format file - throw new Exception(this.GetType().ToString() + - "This is not a valid PZX format file"); - } + if (ident.ToUpper() != "PZXT") + { + // this is not a valid TZX format file + throw new Exception(this.GetType().ToString() + + "This is not a valid PZX format file"); + } - _position = 0; + _position = 0; - // parse all blocks out into seperate byte arrays first - List bDatas = new List(); + // parse all blocks out into seperate byte arrays first + List bDatas = new List(); - while (_position < data.Length) - { - int startPos = _position; + while (_position < data.Length) + { + int startPos = _position; - // data size - _position += 4; - int blockSize = GetInt32(data, _position); - _position += 4; + // data size + _position += 4; + int blockSize = GetInt32(data, _position); + _position += 4; - // block data - byte[] bd = new byte[8 + blockSize]; - Array.Copy(data, startPos, bd, 0, bd.Length); - bDatas.Add(bd); + // block data + byte[] bd = new byte[8 + blockSize]; + Array.Copy(data, startPos, bd, 0, bd.Length); + bDatas.Add(bd); - _position += blockSize; - } + _position += blockSize; + } - // process the blocks - foreach (var b in bDatas) - { - int pos = 8; - string blockId = Encoding.ASCII.GetString(b, 0, 4); - int blockSize = GetInt32(b, 4); + // process the blocks + foreach (var b in bDatas) + { + int pos = 8; + string blockId = Encoding.ASCII.GetString(b, 0, 4); + int blockSize = GetInt32(b, 4); - TapeDataBlock t = new TapeDataBlock(); + TapeDataBlock t = new TapeDataBlock(); - switch (blockId) - { - // PZXT - PZX header block - /* + switch (blockId) + { + // PZXT - PZX header block + /* offset type name meaning 0 u8 major major version number (currently 1). 1 u8 minor minor version number (currently 0). 2 u8[?] info tape info, see below. */ - case "PZXT": + case "PZXT": - break; + break; - // PULS - Pulse sequence - /* + // PULS - Pulse sequence + /* offset type name meaning 0 u16 count bits 0-14 optional repeat count (see bit 15), always greater than zero bit 15 repeat count present: 0 not present 1 present @@ -167,58 +167,58 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum 4 u16 duration2 optional low bits of pulse duration (see bit 15 of duration1) 6 ... ... ditto repeated until the end of the block */ - case "PULS": - - t.BlockID = GetInt32(b, 0); - t.DataPeriods = new List(); + case "PULS": - t.InitialPulseLevel = false; + t.BlockID = GetInt32(b, 0); + t.DataPeriods = new List(); - List pulses = new List(); + t.InitialPulseLevel = false; - while (pos < blockSize + 8) - { - ushort[] p = new ushort[2]; - p[0] = 1; - p[1] = GetWordValue(b, pos); - pos += 2; + List pulses = new List(); - if (p[1] > 0x8000) - { - p[0] = (ushort)(p[1] & 0x7fff); - p[1] = GetWordValue(b, pos); - pos += 2; - } + while (pos < blockSize + 8) + { + ushort[] p = new ushort[2]; + p[0] = 1; + p[1] = GetWordValue(b, pos); + pos += 2; - if (p[1] >= 0x8000) - { - p[1] &= 0x7fff; - p[1] <<= 16; - p[1] |= GetWordValue(b, pos); - pos += 2; - } + if (p[1] > 0x8000) + { + p[0] = (ushort)(p[1] & 0x7fff); + p[1] = GetWordValue(b, pos); + pos += 2; + } - pulses.Add(p); - } + if (p[1] >= 0x8000) + { + p[1] &= 0x7fff; + p[1] <<= 16; + p[1] |= GetWordValue(b, pos); + pos += 2; + } - // convert to tape block - t.BlockDescription = BlockType.PULS; - t.PauseInMS = 0; + pulses.Add(p); + } - foreach (var x in pulses) - { - for (int i = 0; i < x[0]; i++) - { - t.DataPeriods.Add(x[1]); - } - } + // convert to tape block + t.BlockDescription = BlockType.PULS; + t.PauseInMS = 0; - _datacorder.DataBlocks.Add(t); + foreach (var x in pulses) + { + for (int i = 0; i < x[0]; i++) + { + t.DataPeriods.Add(x[1]); + } + } - break; + _datacorder.DataBlocks.Add(t); - // DATA - Data block - /* + break; + + // DATA - Data block + /* offset type name meaning 0 u32 count bits 0-30 number of bits in the data stream bit 31 initial pulse level: 0 low 1 high @@ -229,171 +229,171 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum 8+2*p0 u16[p1] s1 sequence of pulse durations encoding bit equal to 1. 8+2*(p0+p1) u8[ceil(bits/8)] data data stream, see below. */ - case "DATA": + case "DATA": - t.BlockID = GetInt32(b, 0); - t.DataPeriods = new List(); + t.BlockID = GetInt32(b, 0); + t.DataPeriods = new List(); - List s0 = new List(); - List s1 = new List(); - List dData = new List(); + List s0 = new List(); + List s1 = new List(); + List dData = new List(); - uint initPulseLevel = 1; - int dCount = 1; - ushort tail = 0; + uint initPulseLevel = 1; + int dCount = 1; + ushort tail = 0; - while (pos < blockSize + 8) - { - dCount = GetInt32(b, pos); - initPulseLevel = (uint)((dCount & 0x80000000) == 0 ? 0 : 1); + while (pos < blockSize + 8) + { + dCount = GetInt32(b, pos); + initPulseLevel = (uint)((dCount & 0x80000000) == 0 ? 0 : 1); - t.InitialPulseLevel = initPulseLevel == 1; + t.InitialPulseLevel = initPulseLevel == 1; - dCount = (int)(dCount & 0x7FFFFFFF); - pos += 4; + dCount = (int)(dCount & 0x7FFFFFFF); + pos += 4; - tail = GetWordValue(b, pos); - pos += 2; + tail = GetWordValue(b, pos); + pos += 2; - var p0 = b[pos++]; - var p1 = b[pos++]; + var p0 = b[pos++]; + var p1 = b[pos++]; - for (int i = 0; i < p1; i++) - { - var s = GetWordValue(b, pos); - pos += 2; - s0.Add(s); - } + for (int i = 0; i < p1; i++) + { + var s = GetWordValue(b, pos); + pos += 2; + s0.Add(s); + } - for (int i = 0; i < p1; i++) - { - var s = GetWordValue(b, pos); - pos += 2; - s1.Add(s); - } + for (int i = 0; i < p1; i++) + { + var s = GetWordValue(b, pos); + pos += 2; + s1.Add(s); + } - for (int i = 0; i < Math.Ceiling((decimal)dCount / 8); i++) - { - var buff = b[pos++]; - dData.Add(buff); - } + for (int i = 0; i < Math.Ceiling((decimal)dCount / 8); i++) + { + var buff = b[pos++]; + dData.Add(buff); + } - foreach (var by in dData) - { - for (int i = 7; i >= 0; i--) - { - if (by.Bit(i) == true) - { - foreach (var pu in s1) - { - t.DataPeriods.Add((int)pu); - } - - } - else - { - foreach (var pu in s0) - { - t.DataPeriods.Add((int)pu); - } - - } - } - } - if (tail > 0) - t.DataPeriods.Add(tail); - dData.Clear(); - } + foreach (var by in dData) + { + for (int i = 7; i >= 0; i--) + { + if (by.Bit(i) == true) + { + foreach (var pu in s1) + { + t.DataPeriods.Add((int)pu); + } - // convert to tape block - t.BlockDescription = BlockType.DATA; - t.PauseInMS = 0; + } + else + { + foreach (var pu in s0) + { + t.DataPeriods.Add((int)pu); + } - // tail - //t.DataPeriods.Add(tail); + } + } + } + if (tail > 0) + t.DataPeriods.Add(tail); + dData.Clear(); + } - _datacorder.DataBlocks.Add(t); + // convert to tape block + t.BlockDescription = BlockType.DATA; + t.PauseInMS = 0; - break; + // tail + //t.DataPeriods.Add(tail); - // PAUS - Pause - /* + _datacorder.DataBlocks.Add(t); + + break; + + // PAUS - Pause + /* offset type name meaning 0 u32 duration bits 0-30 duration of the pause bit 31 initial pulse level: 0 low 1 high */ - case "PAUS": + case "PAUS": - t.BlockID = GetInt32(b, 0); - t.DataPeriods = new List(); + t.BlockID = GetInt32(b, 0); + t.DataPeriods = new List(); - int iniPulseLevel = 1; - int pCount = 0; + int iniPulseLevel = 1; + int pCount = 0; - var d = GetInt32(b, pos); - iniPulseLevel = ((d & 0x80000000) == 0 ? 0 : 1); - t.InitialPulseLevel = iniPulseLevel == 1; - pCount = (d & 0x7FFFFFFF); + var d = GetInt32(b, pos); + iniPulseLevel = ((d & 0x80000000) == 0 ? 0 : 1); + t.InitialPulseLevel = iniPulseLevel == 1; + pCount = (d & 0x7FFFFFFF); - // convert to tape block - t.BlockDescription = BlockType.PAUS; - t.DataPeriods.Add(0); - t.DataPeriods.Add(pCount); - t.DataPeriods.Add(0); + // convert to tape block + t.BlockDescription = BlockType.PAUS; + t.DataPeriods.Add(0); + t.DataPeriods.Add(pCount); + t.DataPeriods.Add(0); - _datacorder.DataBlocks.Add(t); + _datacorder.DataBlocks.Add(t); - break; + break; - // BRWS - Browse point - /* + // BRWS - Browse point + /* offset type name meaning 0 u8[?] text text describing this browse point */ - case "BRWS": + case "BRWS": - t.BlockID = GetInt32(b, 0); - t.DataPeriods = new List(); + t.BlockID = GetInt32(b, 0); + t.DataPeriods = new List(); - string info = Encoding.ASCII.GetString(b, 8, blockSize); + string info = Encoding.ASCII.GetString(b, 8, blockSize); - // convert to tape block - t.BlockDescription = BlockType.BRWS; - t.MetaData.Add(BlockDescriptorTitle.Comments, info); - t.PauseInMS = 0; + // convert to tape block + t.BlockDescription = BlockType.BRWS; + t.MetaData.Add(BlockDescriptorTitle.Comments, info); + t.PauseInMS = 0; - _datacorder.DataBlocks.Add(t); + _datacorder.DataBlocks.Add(t); - break; + break; - // STOP - Stop tape command - /* + // STOP - Stop tape command + /* offset type name meaning 0 u16 flags when exactly to stop the tape (1 48k only, other always). */ - case "STOP": + case "STOP": - - t.BlockID = GetInt32(b, 0); - t.DataPeriods = new List(); - var flags = GetWordValue(b, pos); - if (flags == 1) - { - t.BlockDescription = BlockType.Stop_the_Tape_48K; - t.Command = TapeCommand.STOP_THE_TAPE_48K; - } - else - { - t.BlockDescription = BlockType.Pause_or_Stop_the_Tape; - t.Command = TapeCommand.STOP_THE_TAPE; - } + t.BlockID = GetInt32(b, 0); + t.DataPeriods = new List(); - _datacorder.DataBlocks.Add(t); + var flags = GetWordValue(b, pos); + if (flags == 1) + { + t.BlockDescription = BlockType.Stop_the_Tape_48K; + t.Command = TapeCommand.STOP_THE_TAPE_48K; + } + else + { + t.BlockDescription = BlockType.Pause_or_Stop_the_Tape; + t.Command = TapeCommand.STOP_THE_TAPE; + } - break; - } - } - } - } + _datacorder.DataBlocks.Add(t); + + break; + } + } + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/TAP/TapConverter.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/TAP/TapConverter.cs index 901e0496ae..1e01558da6 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/TAP/TapConverter.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/TAP/TapConverter.cs @@ -6,104 +6,104 @@ using System.Text; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Reponsible for TAP format serializaton - /// - public class TapConverter : MediaConverter - { - /// - /// The type of serializer - /// - private MediaConverterType _formatType = MediaConverterType.TAP; - public override MediaConverterType FormatType - { - get - { - return _formatType; - } - } + /// + /// Reponsible for TAP format serializaton + /// + public class TapConverter : MediaConverter + { + /// + /// The type of serializer + /// + private MediaConverterType _formatType = MediaConverterType.TAP; + public override MediaConverterType FormatType + { + get + { + return _formatType; + } + } - /// - /// Signs whether this class can be used to read the data format - /// - public override bool IsReader { get { return true; } } + /// + /// Signs whether this class can be used to read the data format + /// + public override bool IsReader { get { return true; } } - /// - /// Signs whether this class can be used to write the data format - /// - public override bool IsWriter { get { return false; } } + /// + /// Signs whether this class can be used to write the data format + /// + public override bool IsWriter { get { return false; } } - #region Construction + #region Construction - private DatacorderDevice _datacorder; + private DatacorderDevice _datacorder; - public TapConverter(DatacorderDevice _tapeDevice) - { - _datacorder = _tapeDevice; - } + public TapConverter(DatacorderDevice _tapeDevice) + { + _datacorder = _tapeDevice; + } - #endregion + #endregion - #region TAP Format Constants + #region TAP Format Constants - /// - /// Pilot pulse length - /// - public const int PILOT_PL = 2168; + /// + /// Pilot pulse length + /// + public const int PILOT_PL = 2168; - /// - /// Pilot pulses in the ROM header block - /// - public const int HEADER_PILOT_COUNT = 8063; + /// + /// Pilot pulses in the ROM header block + /// + public const int HEADER_PILOT_COUNT = 8063; - /// - /// Pilot pulses in the ROM data block - /// - public const int DATA_PILOT_COUNT = 3223; + /// + /// Pilot pulses in the ROM data block + /// + public const int DATA_PILOT_COUNT = 3223; - /// - /// Sync 1 pulse length - /// - public const int SYNC_1_PL = 667; + /// + /// Sync 1 pulse length + /// + public const int SYNC_1_PL = 667; - /// - /// Sync 2 pulse lenth - /// - public const int SYNC_2_PL = 735; + /// + /// Sync 2 pulse lenth + /// + public const int SYNC_2_PL = 735; - /// - /// Bit 0 pulse length - /// - public const int BIT_0_PL = 855; + /// + /// Bit 0 pulse length + /// + public const int BIT_0_PL = 855; - /// - /// Bit 1 pulse length - /// - public const int BIT_1_PL = 1710; + /// + /// Bit 1 pulse length + /// + public const int BIT_1_PL = 1710; - /// - /// End sync pulse length - /// - public const int TERM_SYNC = 947; + /// + /// End sync pulse length + /// + public const int TERM_SYNC = 947; - /// - /// 1 millisecond pause - /// - public const int PAUSE_MS = 3500; + /// + /// 1 millisecond pause + /// + public const int PAUSE_MS = 3500; - /// - /// Used bit count in last byte - /// - public const int BIT_COUNT_IN_LAST = 8; + /// + /// Used bit count in last byte + /// + public const int BIT_COUNT_IN_LAST = 8; - #endregion + #endregion - /// - /// DeSerialization method - /// - public override void Read(byte[] data) - { - /* + /// + /// DeSerialization method + /// + public override void Read(byte[] data) + { + /* The .TAP files contain blocks of tape-saved data. All blocks start with two bytes specifying how many bytes will follow (not counting the two length bytes). Then raw tape data follows, including the flag and checksum bytes. The checksum is the bitwise XOR of all bytes including the flag byte. For example, when you execute the line SAVE "ROM" CODE 0,2 this will result: |------ Spectrum-generated data -------| |---------| @@ -123,115 +123,115 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum checksum (checkbittoggle would be a better name!).............^^ */ - // clear existing tape blocks - _datacorder.DataBlocks.Clear(); + // clear existing tape blocks + _datacorder.DataBlocks.Clear(); - // convert bytearray to memory stream - MemoryStream stream = new MemoryStream(data); + // convert bytearray to memory stream + MemoryStream stream = new MemoryStream(data); - // the first 2 bytes of the TAP file designate the length of the first data block - // this (I think) should always be 17 bytes (as this is the tape header) - byte[] blockLengthData = new byte[2]; + // the first 2 bytes of the TAP file designate the length of the first data block + // this (I think) should always be 17 bytes (as this is the tape header) + byte[] blockLengthData = new byte[2]; - // we are now going to stream through the entire file processing a block at a time - while (stream.Position < stream.Length) - { - // read and calculate the length of the block - stream.Read(blockLengthData, 0, 2); - int blockSize = BitConverter.ToUInt16(blockLengthData, 0); - if (blockSize == 0) - { - // block size is 0 - this is probably invalid (but I guess could be EoF in some situations) - break; - } + // we are now going to stream through the entire file processing a block at a time + while (stream.Position < stream.Length) + { + // read and calculate the length of the block + stream.Read(blockLengthData, 0, 2); + int blockSize = BitConverter.ToUInt16(blockLengthData, 0); + if (blockSize == 0) + { + // block size is 0 - this is probably invalid (but I guess could be EoF in some situations) + break; + } - // copy the entire block into a new bytearray - byte[] blockdata = new byte[blockSize]; - stream.Read(blockdata, 0, blockSize); + // copy the entire block into a new bytearray + byte[] blockdata = new byte[blockSize]; + stream.Read(blockdata, 0, blockSize); - // create and populate a new tapedatablock object - TapeDataBlock tdb = new TapeDataBlock(); + // create and populate a new tapedatablock object + TapeDataBlock tdb = new TapeDataBlock(); - // ascertain the block description - string description = string.Empty; - byte crc = 0; - byte crcValue = 0; - byte crcFile = 0; - byte[] programData = new byte[10]; + // ascertain the block description + string description = string.Empty; + byte crc = 0; + byte crcValue = 0; + byte crcFile = 0; + byte[] programData = new byte[10]; - // calculate block checksum value - for (int i = 0; i < blockSize; i++) - { - crc ^= blockdata[i]; - if (i < blockSize - 1) - { - crcValue = crc; - } - else - { - crcFile = blockdata[i]; - } - } + // calculate block checksum value + for (int i = 0; i < blockSize; i++) + { + crc ^= blockdata[i]; + if (i < blockSize - 1) + { + crcValue = crc; + } + else + { + crcFile = blockdata[i]; + } + } - // process the type byte - /* (The type is 0,1,2 or 3 for a Program, Number array, Character array or Code file. + // process the type byte + /* (The type is 0,1,2 or 3 for a Program, Number array, Character array or Code file. A SCREEN$ file is regarded as a Code file with start address 16384 and length 6912 decimal. If the file is a Program file, parameter 1 holds the autostart line number (or a number >=32768 if no LINE parameter was given) and parameter 2 holds the start of the variable area relative to the start of the program. If it's a Code file, parameter 1 holds the start of the code block when saved, and parameter 2 holds 32768. For data files finally, the byte at position 14 decimal holds the variable name.) */ - tdb.MetaData = new Dictionary(); + tdb.MetaData = new Dictionary(); - if (blockdata[0] == 0x00 && blockSize == 19) - { - string fileName = Encoding.ASCII.GetString(blockdata.Skip(2).Take(10).ToArray()).Trim(); - string type = "Unknown Type"; - StringBuilder sb = new StringBuilder(); + if (blockdata[0] == 0x00 && blockSize == 19) + { + string fileName = Encoding.ASCII.GetString(blockdata.Skip(2).Take(10).ToArray()).Trim(); + string type = "Unknown Type"; + StringBuilder sb = new StringBuilder(); - var param1 = GetWordValue(blockdata, 12); - var param2 = GetWordValue(blockdata, 14); + var param1 = GetWordValue(blockdata, 12); + var param2 = GetWordValue(blockdata, 14); - // header block - examine first byte of header - if (blockdata[1] == 0) - { - type = "Program"; - sb.Append(type + ": "); - sb.Append(fileName + " "); - } - else if (blockdata[1] == 1) - { - type = "NumArray"; - sb.Append(type + ": "); - sb.Append(fileName + " "); - } - else if (blockdata[1] == 2) - { - type = "CharArray"; - sb.Append(type + ": "); - sb.Append(fileName + " "); - } - else if (blockdata[1] == 3) - { - type = "Code"; - sb.Append(type + ": "); - sb.Append(fileName + " "); - } - } - else if (blockdata[0] == 0xff) - { - // data block - description = "Data Block " + (blockSize - 2) + "bytes"; - tdb.AddMetaData(BlockDescriptorTitle.Data_Bytes, (blockSize - 2).ToString() + " Bytes"); - } - else - { - // some other type (turbo data etc..) - description = $"#{blockdata[0].ToString("X2")} block, {blockSize} bytes"; - //description += (crc != 0) ? $", crc bad (#{crcFile:X2}!=#{crcValue:X2})" : ", crc ok"; - tdb.AddMetaData(BlockDescriptorTitle.Undefined, description); - } - /* + // header block - examine first byte of header + if (blockdata[1] == 0) + { + type = "Program"; + sb.Append(type + ": "); + sb.Append(fileName + " "); + } + else if (blockdata[1] == 1) + { + type = "NumArray"; + sb.Append(type + ": "); + sb.Append(fileName + " "); + } + else if (blockdata[1] == 2) + { + type = "CharArray"; + sb.Append(type + ": "); + sb.Append(fileName + " "); + } + else if (blockdata[1] == 3) + { + type = "Code"; + sb.Append(type + ": "); + sb.Append(fileName + " "); + } + } + else if (blockdata[0] == 0xff) + { + // data block + description = "Data Block " + (blockSize - 2) + "bytes"; + tdb.AddMetaData(BlockDescriptorTitle.Data_Bytes, (blockSize - 2).ToString() + " Bytes"); + } + else + { + // some other type (turbo data etc..) + description = $"#{blockdata[0].ToString("X2")} block, {blockSize} bytes"; + //description += (crc != 0) ? $", crc bad (#{crcFile:X2}!=#{crcValue:X2})" : ", crc ok"; + tdb.AddMetaData(BlockDescriptorTitle.Undefined, description); + } + /* if (blockdata[0] == 0x00 && blockSize == 19 && (blockdata[1] == 0x00) || blockdata[1] == 3) { // This is the PROGRAM header @@ -274,113 +274,113 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum } */ - tdb.BlockDescription = BlockType.Standard_Speed_Data_Block; + tdb.BlockDescription = BlockType.Standard_Speed_Data_Block; - // calculate the data periods for this block - int pilotLength = 0; + // calculate the data periods for this block + int pilotLength = 0; - // work out pilot length - if (blockdata[0] < 4) - { - pilotLength = 8064; - } - else - { - pilotLength = 3220; - } + // work out pilot length + if (blockdata[0] < 4) + { + pilotLength = 8064; + } + else + { + pilotLength = 3220; + } - // create a list to hold the data periods - List dataPeriods = new List(); + // create a list to hold the data periods + List dataPeriods = new List(); - // generate pilot pulses - for (int i = 0; i < pilotLength; i++) - { - dataPeriods.Add(PILOT_PL); - } + // generate pilot pulses + for (int i = 0; i < pilotLength; i++) + { + dataPeriods.Add(PILOT_PL); + } - // add syncro pulses - dataPeriods.Add(SYNC_1_PL); - dataPeriods.Add(SYNC_2_PL); + // add syncro pulses + dataPeriods.Add(SYNC_1_PL); + dataPeriods.Add(SYNC_2_PL); - int pos = 0; + int pos = 0; - // add bit0 and bit1 periods - for (int i = 0; i < blockSize - 1; i++, pos++) - { - for (byte b = 0x80; b != 0; b >>= 1) - { - if ((blockdata[i] & b) != 0) - dataPeriods.Add(BIT_1_PL); - else - dataPeriods.Add(BIT_0_PL); - if ((blockdata[i] & b) != 0) - dataPeriods.Add(BIT_1_PL); - else - dataPeriods.Add(BIT_0_PL); - } - } + // add bit0 and bit1 periods + for (int i = 0; i < blockSize - 1; i++, pos++) + { + for (byte b = 0x80; b != 0; b >>= 1) + { + if ((blockdata[i] & b) != 0) + dataPeriods.Add(BIT_1_PL); + else + dataPeriods.Add(BIT_0_PL); + if ((blockdata[i] & b) != 0) + dataPeriods.Add(BIT_1_PL); + else + dataPeriods.Add(BIT_0_PL); + } + } - // add the last byte - for (byte c = 0x80; c != (byte)(0x80 >> BIT_COUNT_IN_LAST); c >>= 1) - { - if ((blockdata[pos] & c) != 0) - dataPeriods.Add(BIT_1_PL); - else - dataPeriods.Add(BIT_0_PL); - if ((blockdata[pos] & c) != 0) - dataPeriods.Add(BIT_1_PL); - else - dataPeriods.Add(BIT_0_PL); - } + // add the last byte + for (byte c = 0x80; c != (byte)(0x80 >> BIT_COUNT_IN_LAST); c >>= 1) + { + if ((blockdata[pos] & c) != 0) + dataPeriods.Add(BIT_1_PL); + else + dataPeriods.Add(BIT_0_PL); + if ((blockdata[pos] & c) != 0) + dataPeriods.Add(BIT_1_PL); + else + dataPeriods.Add(BIT_0_PL); + } - // add block pause - //int actualPause = PAUSE_MS * 1000; - //dataPeriods.Add(actualPause); + // add block pause + //int actualPause = PAUSE_MS * 1000; + //dataPeriods.Add(actualPause); - // default pause for tap files - tdb.PauseInMS = 1000; + // default pause for tap files + tdb.PauseInMS = 1000; - // add to the tapedatablock object - tdb.DataPeriods = dataPeriods; + // add to the tapedatablock object + tdb.DataPeriods = dataPeriods; - // add the raw data - tdb.BlockData = blockdata; + // add the raw data + tdb.BlockData = blockdata; - // generate separate PAUS block - TapeDataBlock tdbPause = new TapeDataBlock(); - tdbPause.DataPeriods = new List(); - tdbPause.BlockDescription = BlockType.PAUSE_BLOCK; - tdbPause.PauseInMS = 0; - var pauseInTStates = TranslatePause(tdb.PauseInMS); - //if (pauseInTStates > 0) - //tdbPause.DataPeriods.Add(pauseInTStates); - tdb.PauseInMS = 0; + // generate separate PAUS block + TapeDataBlock tdbPause = new TapeDataBlock(); + tdbPause.DataPeriods = new List(); + tdbPause.BlockDescription = BlockType.PAUSE_BLOCK; + tdbPause.PauseInMS = 0; + var pauseInTStates = TranslatePause(tdb.PauseInMS); + //if (pauseInTStates > 0) + //tdbPause.DataPeriods.Add(pauseInTStates); + tdb.PauseInMS = 0; - // add block to the tape - _datacorder.DataBlocks.Add(tdb); + // add block to the tape + _datacorder.DataBlocks.Add(tdb); - // PAUS block if neccessary - if (pauseInTStates > 0) - { - tdbPause.AddMetaData(BlockDescriptorTitle.Block_ID, pauseInTStates.ToString() + " cycles"); + // PAUS block if neccessary + if (pauseInTStates > 0) + { + tdbPause.AddMetaData(BlockDescriptorTitle.Block_ID, pauseInTStates.ToString() + " cycles"); - int by1000 = pauseInTStates / 70000; - int rem1000 = pauseInTStates % 70000; + int by1000 = pauseInTStates / 70000; + int rem1000 = pauseInTStates % 70000; - if (by1000 > 1) - { - tdbPause.DataPeriods.Add(35000); - tdbPause.DataPeriods.Add(pauseInTStates - 35000); - } - else - { - tdbPause.DataPeriods.Add(pauseInTStates); - tdbPause.DataPeriods.Add(0); - } + if (by1000 > 1) + { + tdbPause.DataPeriods.Add(35000); + tdbPause.DataPeriods.Add(pauseInTStates - 35000); + } + else + { + tdbPause.DataPeriods.Add(pauseInTStates); + tdbPause.DataPeriods.Add(0); + } - _datacorder.DataBlocks.Add(tdbPause); - } - } - } - } + _datacorder.DataBlocks.Add(tdbPause); + } + } + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/TZX/TzxConverter.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/TZX/TzxConverter.cs index faf1c003d1..b9e3803cd0 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/TZX/TzxConverter.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/TZX/TzxConverter.cs @@ -5,65 +5,65 @@ using System.Text; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Reponsible for TZX format serializaton - /// - public class TzxConverter : MediaConverter - { - /// - /// The type of serializer - /// - private MediaConverterType _formatType = MediaConverterType.TZX; - public override MediaConverterType FormatType - { - get - { - return _formatType; - } - } + /// + /// Reponsible for TZX format serializaton + /// + public class TzxConverter : MediaConverter + { + /// + /// The type of serializer + /// + private MediaConverterType _formatType = MediaConverterType.TZX; + public override MediaConverterType FormatType + { + get + { + return _formatType; + } + } - /// - /// Signs whether this class can be used to read the data format - /// - public override bool IsReader { get { return true; } } + /// + /// Signs whether this class can be used to read the data format + /// + public override bool IsReader { get { return true; } } - /// - /// Signs whether this class can be used to write the data format - /// - public override bool IsWriter { get { return false; } } + /// + /// Signs whether this class can be used to write the data format + /// + public override bool IsWriter { get { return false; } } - /// - /// Working list of generated tape data blocks - /// - private List _blocks = new List(); + /// + /// Working list of generated tape data blocks + /// + private List _blocks = new List(); - /// - /// Position counter - /// - private int _position = 0; + /// + /// Position counter + /// + private int _position = 0; - /// - /// Object to keep track of loops - this assumes there is only one loop at a time - /// - private List> _loopCounter = new List>(); + /// + /// Object to keep track of loops - this assumes there is only one loop at a time + /// + private List> _loopCounter = new List>(); - #region Construction + #region Construction - private DatacorderDevice _datacorder; + private DatacorderDevice _datacorder; - public TzxConverter(DatacorderDevice _tapeDevice) - { - _datacorder = _tapeDevice; - } + public TzxConverter(DatacorderDevice _tapeDevice) + { + _datacorder = _tapeDevice; + } - #endregion + #endregion - /// - /// Returns TRUE if tzx header is detected - /// - public override bool CheckType(byte[] data) - { - /* + /// + /// Returns TRUE if tzx header is detected + /// + public override bool CheckType(byte[] data) + { + /* // TZX Header length: 10 bytes Offset Value Type Description @@ -73,1499 +73,1499 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum 0x09 20 BYTE TZX minor revision number */ - // check whether this is a valid tzx format file by looking at the identifier in the header - // (first 7 bytes of the file) - string ident = Encoding.ASCII.GetString(data, 0, 7); - // and 'end of text' marker - byte eotm = data[7]; - - // version info - int majorVer = data[8]; - int minorVer = data[9]; - - if (ident != "ZXTape!" || eotm != 0x1A) - { - // this is not a valid TZX format file - return false; - } - else - { - return true; - } - } - - /// - /// DeSerialization method - /// - public override void Read(byte[] data) - { - // clear existing tape blocks - _datacorder.DataBlocks.Clear(); - -/* - // TZX Header - length: 10 bytes - Offset Value Type Description - 0x00 "ZXTape!" ASCII[7] TZX signature - 0x07 0x1A BYTE End of text file marker - 0x08 1 BYTE TZX major revision number - 0x09 20 BYTE TZX minor revision number -*/ - - // check whether this is a valid tzx format file by looking at the identifier in the header - // (first 7 bytes of the file) - string ident = Encoding.ASCII.GetString(data, 0, 7); - // and 'end of text' marker - byte eotm = data[7]; - - // version info - int majorVer = data[8]; - int minorVer = data[9]; - - if (ident != "ZXTape!" || eotm != 0x1A) - { - // this is not a valid TZX format file - throw new Exception(this.GetType().ToString() + - "This is not a valid TZX format file"); - } - - // iterate through each block - _position = 10; - while (_position < data.Length) - { - // block ID is the first byte in a new block - int ID = data[_position++]; - - // process the data - ProcessBlock(data, ID); - } - - } - - /// - /// Processes a TZX block - /// - private void ProcessBlock(byte[] data, int id) - { - // process based on detected block ID - switch (id) - { - // ID 10 - Standard Speed Data Block - case 0x10: - ProcessBlockID10(data); - break; - // ID 11 - Turbo Speed Data Block - case 0x11: - ProcessBlockID11(data); - break; - // ID 12 - Pure Tone - case 0x12: - ProcessBlockID12(data); - break; - // ID 13 - Pulse sequence - case 0x13: - ProcessBlockID13(data); - break; - // ID 14 - Pure Data Block - case 0x14: - ProcessBlockID14(data); - break; - // ID 15 - Direct Recording - case 0x15: - ProcessBlockID15(data); - break; - // ID 18 - CSW Recording - case 0x18: - ProcessBlockID18(data); - break; - // ID 19 - Generalized Data Block - case 0x19: - ProcessBlockID19(data); - break; - // ID 20 - Pause (silence) or 'Stop the Tape' command - case 0x20: - ProcessBlockID20(data); - break; - // ID 21 - Group start - case 0x21: - ProcessBlockID21(data); - break; - // ID 22 - Group end - case 0x22: - ProcessBlockID22(data); - break; - // ID 23 - Jump to block - case 0x23: - ProcessBlockID23(data); - break; - // ID 24 - Loop start - case 0x24: - ProcessBlockID24(data); - break; - // ID 25 - Loop end - case 0x25: - ProcessBlockID25(data); - break; - // ID 26 - Call sequence - case 0x26: - ProcessBlockID26(data); - break; - // ID 27 - Return from sequence - case 0x27: - ProcessBlockID27(data); - break; - // ID 28 - Select block - case 0x28: - ProcessBlockID28(data); - break; - // ID 2A - Stop the tape if in 48K mode - case 0x2A: - ProcessBlockID2A(data); - break; - // ID 2B - Set signal level - case 0x2B: - ProcessBlockID2B(data); - break; - // ID 30 - Text description - case 0x30: - ProcessBlockID30(data); - break; - // ID 31 - Message block - case 0x31: - ProcessBlockID31(data); - break; - // ID 32 - Archive info - case 0x32: - ProcessBlockID32(data); - break; - // ID 33 - Hardware type - case 0x33: - ProcessBlockID33(data); - break; - // ID 35 - Custom info block - case 0x35: - ProcessBlockID35(data); - break; - // ID 5A - "Glue" block - case 0x5A: - ProcessBlockID5A(data); - break; - - #region Depreciated Blocks - - // ID 16 - C64 ROM Type Data Block - case 0x16: - ProcessBlockID16(data); - break; - // ID 17 - C64 Turbo Tape Data Block - case 0x17: - ProcessBlockID17(data); - break; - // ID 34 - Emulation info - case 0x34: - ProcessBlockID34(data); - break; - // ID 40 - Snapshot block - case 0x40: - ProcessBlockID40(data); - break; - - #endregion - - default: - ProcessUnidentifiedBlock(data); - break; - } - } - - #region TZX Block Processors - - #region ID 10 - Standard Speed Data Block -/* length: [02,03]+04 - Offset Value Type Description - 0x00 - WORD Pause after this block (ms.) {1000} - 0x02 N WORD Length of data that follow - 0x04 - BYTE[N] Data as in .TAP files - - This block must be replayed with the standard Spectrum ROM timing values - see the values in - curly brackets in block ID 11. The pilot tone consists in 8063 pulses if the first data byte - (flag byte) is < 128, 3223 otherwise. This block can be used for the ROM loading routines AND - for custom loading routines that use the same timings as ROM ones do. */ - private void ProcessBlockID10(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x10; - t.BlockDescription = BlockType.Standard_Speed_Data_Block; - t.DataPeriods = new List(); - - int pauseLen = GetWordValue(data, _position); - if (pauseLen == 0) - pauseLen = 1000; - - t.PauseInMS = pauseLen; - - int blockLen = GetWordValue(data, _position + 2); - - _position += 4; - - byte[] tmp = new byte[blockLen]; - tmp = data.Skip(_position).Take(blockLen).ToArray(); - - var t2 = DecodeDataBlock(t, tmp, DataBlockType.Standard, pauseLen); - - // add the block - _datacorder.DataBlocks.Add(t2); - - // advance the position to the next block - _position += blockLen; - - // generate PAUSE block - CreatePauseBlock(_datacorder.DataBlocks.Last()); - } - #endregion - - #region ID 11 - Turbo Speed Data Block -/* length: [0F,10,11]+12 - Offset Value Type Description - 0x00 - WORD Length of PILOT pulse {2168} - 0x02 - WORD Length of SYNC first pulse {667} - 0x04 - WORD Length of SYNC second pulse {735} - 0x06 - WORD Length of ZERO bit pulse {855} - 0x08 - WORD Length of ONE bit pulse {1710} - 0x0A - WORD Length of PILOT tone (number of pulses) {8063 header (flag<128), 3223 data (flag>=128)} - 0x0C - BYTE Used bits in the last byte (other bits should be 0) {8} - (e.g. if this is 6, then the bits used (x) in the last byte are: xxxxxx00, - where MSb is the leftmost bit, LSb is the rightmost bit) - 0x0D - WORD Pause after this block (ms.) {1000} - 0x0F N BYTE[3] Length of data that follow - 0x12 - BYTE[N] Data as in .TAP files - - This block is very similar to the normal TAP block but with some additional info on the timings and other important - differences. The same tape encoding is used as for the standard speed data block. If a block should use some non-standard - sync or pilot tones (i.e. all sorts of protection schemes) then use the next three blocks to describe it.*/ - private void ProcessBlockID11(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x11; - t.BlockDescription = BlockType.Turbo_Speed_Data_Block; - t.DataPeriods = new List(); - - int pilotPL = GetWordValue(data, _position); - int sync1P = GetWordValue(data, _position + 2); - int sync2P = GetWordValue(data, _position + 4); - int bit0P = GetWordValue(data, _position + 6); - int bit1P = GetWordValue(data, _position + 8); - int pilotTL = GetWordValue(data, _position + 10); - int bitinbyte = data[_position + 12]; - int pause = GetWordValue(data, _position + 13); - - - int blockLen = 0xFFFFFF & GetInt32(data, _position + 0x0F); - - byte[] bLenArr = data.Skip(_position + 0x0F).Take(3).ToArray(); - - _position += 0x12; - - byte[] tmp = new byte[blockLen]; - tmp = data.Skip(_position).Take(blockLen).ToArray(); - - var t2 = DecodeDataBlock(t, tmp, DataBlockType.Turbo, pause, pilotTL, pilotPL, sync1P, sync2P, bit0P, bit1P, bitinbyte); - - t.PauseInMS = pause; - - // add the block - _datacorder.DataBlocks.Add(t2); - - // advance the position to the next block - _position += blockLen; - - // generate PAUSE block - CreatePauseBlock(_datacorder.DataBlocks.Last()); - } - #endregion - - #region ID 12 - Pure Tone -/* length: 04 - Offset Value Type Description - 0x00 - WORD Length of one pulse in T-states - 0x02 - WORD Number of pulses - - This will produce a tone which is basically the same as the pilot tone in the ID 10, ID 11 blocks. You can define how - long the pulse is and how many pulses are in the tone. */ - private void ProcessBlockID12(byte[] data) - { - int blockLen = 4; - - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x12; - t.BlockDescription = BlockType.Pure_Tone; - t.DataPeriods = new List(); - t.PauseInMS = 0; - - // get values - int pulseLength = GetWordValue(data, _position); - int pulseCount = GetWordValue(data, _position + 2); - - t.AddMetaData(BlockDescriptorTitle.Pulse_Length, pulseLength.ToString() + " T-States"); - t.AddMetaData(BlockDescriptorTitle.Pulse_Count, pulseCount.ToString()); - - // build period information - for (int p = 0; p < pulseCount; p++) - { - t.DataPeriods.Add(pulseLength); - } - - // add the block - _datacorder.DataBlocks.Add(t); - - // advance the position to the next block - _position += blockLen; - } - #endregion - - #region ID 13 - Pulse sequence -/* length: [00]*02+01 - Offset Value Type Description - 0x00 N BYTE Number of pulses - 0x01 - WORD[N] Pulses' lengths - - This will produce N pulses, each having its own timing. Up to 255 pulses can be stored in this block; this is useful for non-standard - sync tones used by some protection schemes. */ - private void ProcessBlockID13(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x13; - t.BlockDescription = BlockType.Pulse_Sequence; - t.DataPeriods = new List(); - - t.PauseInMS = 0; - - // get pulse count - int pulseCount = data[_position]; - t.AddMetaData(BlockDescriptorTitle.Pulse_Count, pulseCount.ToString()); - _position++; - - // build period information - for (int p = 0; p < pulseCount; p++, _position += 2) - { - // get pulse length - int pulseLength = GetWordValue(data, _position); - t.AddMetaData(BlockDescriptorTitle.Needs_Parsing, "Pulse " + p + " Length\t" + pulseLength.ToString() + " T-States"); - t.DataPeriods.Add(pulseLength); - } - - // add the block - _datacorder.DataBlocks.Add(t); - } - #endregion - - #region ID 14 - Pure Data Block -/* length: [07,08,09]+0A - Offset Value Type Description - 0x00 - WORD Length of ZERO bit pulse - 0x02 - WORD Length of ONE bit pulse - 0x04 - BYTE Used bits in last byte (other bits should be 0) - (e.g. if this is 6, then the bits used (x) in the last byte are: xxxxxx00, - where MSb is the leftmost bit, LSb is the rightmost bit) - 0x05 - WORD Pause after this block (ms.) - 0x07 N BYTE[3] Length of data that follow - 0x0A - BYTE[N] Data as in .TAP files - - This is the same as in the turbo loading data block, except that it has no pilot or sync pulses. */ - private void ProcessBlockID14(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x14; - t.BlockDescription = BlockType.Pure_Data_Block; - t.DataPeriods = new List(); - - int pilotPL = 0; - int sync1P = 0; - int sync2P = 0; - int bit0P = GetWordValue(data, _position + 0); - int bit1P = GetWordValue(data, _position + 2); - int pilotTL = 0; - int bitinbyte = data[_position + 4]; - int pause = GetWordValue(data, _position + 5); - - int blockLen = 0xFFFFFF & GetInt32(data, _position + 0x07); - - _position += 0x0A; - - byte[] tmp = new byte[blockLen]; - tmp = data.Skip(_position).Take(blockLen).ToArray(); - - var t2 = DecodeDataBlock(t, tmp, DataBlockType.Pure, pause, pilotTL, pilotPL, sync1P, sync2P, bit0P, bit1P, bitinbyte); - - t.PauseInMS = pause; - - // add the block - _datacorder.DataBlocks.Add(t2); - - // advance the position to the next block - _position += blockLen; - - // generate PAUSE block - CreatePauseBlock(_datacorder.DataBlocks.Last()); - } - #endregion - - #region ID 15 - Direct Recording -/* length: [05,06,07]+08 - Offset Value Type Description - 0x00 - WORD Number of T-states per sample (bit of data) - 0x02 - WORD Pause after this block in milliseconds (ms.) - 0x04 - BYTE Used bits (samples) in last byte of data (1-8) - (e.g. if this is 2, only first two samples of the last byte will be played) - 0x05 N BYTE[3] Length of samples' data - 0x08 - BYTE[N] Samples data. Each bit represents a state on the EAR port (i.e. one sample). - MSb is played first. - - This block is used for tapes which have some parts in a format such that the turbo loader block cannot be used. - This is not like a VOC file, since the information is much more compact. Each sample value is represented by one bit only - (0 for low, 1 for high) which means that the block will be at most 1/8 the size of the equivalent VOC. - The preferred sampling frequencies are 22050 or 44100 Hz (158 or 79 T-states/sample). - Please, if you can, don't use other sampling frequencies. - Please use this block only if you cannot use any other block. */ - private void ProcessBlockID15(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x15; - t.BlockDescription = BlockType.Direct_Recording; - t.DataPeriods = new List(); - - // get values - int samLen = GetInt32(data, _position + 5); - int samSize = 0xFFFFFF & samLen; - - int tStatesPerSample = GetWordValue(data, _position); - int pauseAfterBlock = GetWordValue(data, _position + 2); - int usedBitsInLastByte = data[_position + 4]; - - // skip to samples data - _position += 8; - - int pulseLength = 0; - int pulseCount = 0; - - // ascertain the pulse count - for (int i = 0; i < samSize; i++) - { - for (int p = 0x80; p != 0; p >>= 1) - { - if (((data[_position + i] ^ pulseLength) & p) != 0) - { - pulseCount++; - pulseLength ^= -1; - } - } - } - - // get the pulses - t.DataPeriods = new List(pulseCount + 2); - int tStateCount = 0; - pulseLength = 0; - for (int i = 1; i < samSize; i++) - { - for (int p = 0x80; p != 0; p >>= 1) - { - tStateCount += tStatesPerSample; - if (((data[_position] ^ pulseLength) & p) != 0) - { - t.DataPeriods.Add(tStateCount); - pulseLength ^= -1; - tStateCount = 0; - } - } - - // incrememt position - _position++; - } - - // get the pulses in the last byte of data - for (int p = 0x80; p != (byte)(0x80 >> usedBitsInLastByte); p >>= 1) - { - tStateCount += tStatesPerSample; - if (((data[_position] ^ pulseLength) & p) != 0) - { - t.DataPeriods.Add(tStateCount); - pulseLength ^= -1; - tStateCount = 0; - } - } - - // add final pulse - t.DataPeriods.Add(tStateCount); - - // add end of block pause - if (pauseAfterBlock > 0) - { - //t.DataPeriods.Add(3500 * pauseAfterBlock); - } - - t.PauseInMS = pauseAfterBlock; - - // increment position - _position++; - - // add the block - _datacorder.DataBlocks.Add(t); - - // generate PAUSE block - CreatePauseBlock(_datacorder.DataBlocks.Last()); - } - #endregion - - #region ID 18 - CSW Recording -/* length: [00,01,02,03]+04 - Offset Value Type Description - 0x00 10+N DWORD Block length (without these four bytes) - 0x04 - WORD Pause after this block (in ms). - 0x06 - BYTE[3] Sampling rate - 0x09 - BYTE Compression type - 0x01: RLE - 0x02: Z-RLE - 0x0A - DWORD Number of stored pulses (after decompression, for validation purposes) - 0x0E - BYTE[N] CSW data, encoded according to the CSW file format specification. - - This block contains a sequence of raw pulses encoded in CSW format v2 (Compressed Square Wave). */ - private void ProcessBlockID18(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x18; - t.BlockDescription = BlockType.CSW_Recording; - t.DataPeriods = new List(); - - int blockLen = GetInt32(data, _position); - _position += 4; - - t.PauseInMS = GetWordValue(data, _position); - - _position += 2; - - int sampleRate = data[_position++] << 16 | data[_position++] << 8 | data[_position++]; - byte compType = data[_position++]; - int pulses = GetInt32(data, _position); - _position += 4; - - int dataLen = blockLen - 10; - - // build source array - byte[] src = new byte[dataLen]; - // build destination array - byte[] dest = new byte[pulses + 1]; - - // process the CSW data - CswConverter.ProcessCSWV2(src, ref dest, compType, pulses); - - // create the periods - var rate = (69888 * 50) / sampleRate; - - for (int i = 0; i < dest.Length;) - { - int length = dest[i++] * rate; - if (length == 0) - { - length = GetInt32(dest, i) / rate; - i += 4; - } - - t.DataPeriods.Add(length); - } - - // add closing period - t.DataPeriods.Add((69888 * 50) / 10); - - _position += dataLen; - //_position += blockLen; - - // add the block - _datacorder.DataBlocks.Add(t); - - // generate PAUSE block - CreatePauseBlock(_datacorder.DataBlocks.Last()); - } - #endregion - - #region ID 19 - Generalized Data Block -/* length: [00,01,02,03]+04 - Offset Value Type Description - 0x00 - DWORD Block length (without these four bytes) - 0x04 - WORD Pause after this block (ms) - 0x06 TOTP DWORD Total number of symbols in pilot/sync block (can be 0) - 0x0A NPP BYTE Maximum number of pulses per pilot/sync symbol - 0x0B ASP BYTE Number of pilot/sync symbols in the alphabet table (0=256) - 0x0C TOTD DWORD Total number of symbols in data stream (can be 0) - 0x10 NPD BYTE Maximum number of pulses per data symbol - 0x11 ASD BYTE Number of data symbols in the alphabet table (0=256) - 0x12 - SYMDEF[ASP] Pilot and sync symbols definition table - This field is present only if TOTP>0 - 0x12+ - (2*NPP+1)*ASP - PRLE[TOTP] Pilot and sync data stream - This field is present only if TOTP>0 - 0x12+ - (TOTP>0)*((2*NPP+1)*ASP)+ - TOTP*3 - SYMDEF[ASD] Data symbols definition table - This field is present only if TOTD>0 - 0x12+ - (TOTP>0)*((2*NPP+1)*ASP)+ - TOTP*3+ - (2*NPD+1)*ASD - BYTE[DS] Data stream - This field is present only if TOTD>0 - - This block has been specifically developed to represent an extremely wide range of data encoding techniques. - The basic idea is that each loading component (pilot tone, sync pulses, data) is associated to a specific sequence - of pulses, where each sequence (wave) can contain a different number of pulses from the others. - In this way we can have a situation where bit 0 is represented with 4 pulses and bit 1 with 8 pulses. - - ---- - SYMDEF structure format - Offset Value Type Description - 0x00 - BYTE Symbol flags - b0-b1: starting symbol polarity - 00: opposite to the current level (make an edge, as usual) - default - 01: same as the current level (no edge - prolongs the previous pulse) - 10: force low level - 11: force high level - 0x01 - WORD[MAXP] Array of pulse lengths. - - The alphabet is stored using a table where each symbol is a row of pulses. The number of columns (i.e. pulses) of the table is the - length of the longest sequence amongst all (MAXP=NPP or NPD, for pilot/sync or data blocks respectively); shorter waves are terminated by a - zero-length pulse in the sequence. - Any number of data symbols is allowed, so we can have more than two distinct waves; for example, imagine a loader which writes two bits at a - time by encoding them with four distinct pulse lengths: this loader would have an alphabet of four symbols, each associated to a specific - sequence of pulses (wave). - ---- - ---- - PRLE structure format - Offset Value Type Description - 0x00 - BYTE Symbol to be represented - 0x01 - WORD Number of repetitions - - Most commonly, pilot and sync are repetitions of the same pulse, thus they are represented using a very simple RLE encoding structure which stores - the symbol and the number of times it must be repeated. - Each symbol in the data stream is represented by a string of NB bits of the block data, where NB = ceiling(Log2(ASD)). - Thus the length of the whole data stream in bits is NB*TOTD, or in bytes DS=ceil(NB*TOTD/8). - ---- */ - private void ProcessBlockID19(byte[] data) - { - // not currently implemented properly - - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x19; - t.BlockDescription = BlockType.Generalized_Data_Block; - t.DataPeriods = new List(); - - int blockLen = GetInt32(data, _position); - _position += 4; - - int pause = GetWordValue(data, _position); - _position += 2; - - int totp = GetInt32(data, _position); - _position += 4; - - int npp = data[_position++]; - - int asp = data[_position++]; - - int totd = GetInt32(data, _position); - _position += 4; - - int npd = data[_position++]; - - int asd = data[_position++]; - - // add the block - _datacorder.DataBlocks.Add(t); - - // advance the position to the next block - _position += blockLen; - } - #endregion - - #region ID 20 - Pause (silence) or 'Stop the Tape' command -/* length: 02 - Offset Value Type Description - 0x00 - WORD Pause duration (ms.) - - This will make a silence (low amplitude level (0)) for a given time in milliseconds. If the value is 0 then the - emulator or utility should (in effect) STOP THE TAPE, i.e. should not continue loading until the user or emulator requests it. */ - private void ProcessBlockID20(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x20; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Pause_or_Stop_the_Tape; - - int pauseDuration = GetWordValue(data, _position); - if (pauseDuration != 0) - { - //t.BlockDescription = "Pause: " + pauseDuration + " ms"; - } - else - { - //t.BlockDescription = "[STOP THE TAPE]"; - } - - t.PauseInMS = pauseDuration; - - if (pauseDuration == 0) - { - // issue stop the tape command - t.Command = TapeCommand.STOP_THE_TAPE; - // add 1ms period - //t.DataPeriods.Add(3500); - //pauseDuration = -1; - - } - else - { - // this is actually just a pause - //pauseDuration = 3500 * pauseDuration; - //t.DataPeriods.Add(pauseDuration); - } - - // add end of block pause - //t.DataPeriods.Add(pauseDuration); - - // add to tape - _datacorder.DataBlocks.Add(t); - - // advanced position to next block - _position += 2; - - // generate PAUSE block - CreatePauseBlock(_datacorder.DataBlocks.Last()); - - } - #endregion - - #region ID 21 - Group start -/* length: [00]+01 - Offset Value Type Description - 0x00 L BYTE Length of the group name string - 0x01 - CHAR[L] Group name in ASCII format (please keep it under 30 characters long) - - This block marks the start of a group of blocks which are to be treated as one single (composite) block. - This is very handy for tapes that use lots of subblocks like Bleepload (which may well have over 160 custom loading blocks). - You can also give the group a name (example 'Bleepload Block 1'). - For each group start block, there must be a group end block. Nesting of groups is not allowed. */ - private void ProcessBlockID21(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x21; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Group_Start; - - int nameLength = data[_position]; - _position++; - - string name = Encoding.ASCII.GetString(data, _position, nameLength); - //t.BlockDescription = "[GROUP: " + name + "]"; - t.Command = TapeCommand.BEGIN_GROUP; - - t.PauseInMS = 0; - - // add to tape - _datacorder.DataBlocks.Add(t); - - // advance to next block - _position += nameLength; - } - #endregion - - #region ID 22 - Group end -/* length: 00 - - This indicates the end of a group. This block has no body. */ - private void ProcessBlockID22(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x22; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Group_End; - t.Command = TapeCommand.END_GROUP; - - t.PauseInMS = 0; - - // add to tape - _datacorder.DataBlocks.Add(t); - } - #endregion - - #region ID 23 - Jump to block -/* length: 02 - Offset Value Type Description - 0x00 - WORD Relative jump value - - This block will enable you to jump from one block to another within the file. The value is a signed short word - (usually 'signed short' in C); Some examples: - Jump 0 = 'Loop Forever' - this should never happen - Jump 1 = 'Go to the next block' - it is like NOP in assembler ;) - Jump 2 = 'Skip one block' - Jump -1 = 'Go to the previous block' - All blocks are included in the block count!. */ - private void ProcessBlockID23(byte[] data) - { - // not implemented properly - - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x23; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Jump_to_Block; - - int relativeJumpValue = GetWordValue(data, _position); - string result = string.Empty; - - switch(relativeJumpValue) - { - case 0: - result = "Loop Forever"; - break; - case 1: - result = "To Next Block"; - break; - case 2: - result = "Skip One Block"; - break; - case -1: - result = "Go to Previous Block"; - break; - } - - //t.BlockDescription = "[JUMP BLOCK - " + result +"]"; - - t.PauseInMS = 0; - - // add to tape - _datacorder.DataBlocks.Add(t); - - // advance to next block - _position += 2; - } - #endregion - - #region ID 24 - Loop start -/* length: 02 - Offset Value Type Description - 0x00 - WORD Number of repetitions (greater than 1) - - If you have a sequence of identical blocks, or of identical groups of blocks, you can use this block to tell how many times they should - be repeated. This block is the same as the FOR statement in BASIC. - For simplicity reasons don't nest loop blocks! */ - private void ProcessBlockID24(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x24; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Loop_Start; - - // loop should start from the next block - int loopStart = _datacorder.DataBlocks.Count() + 1; - - int numberOfRepetitions = GetWordValue(data, _position); - - // update loop counter - _loopCounter.Add( - new KeyValuePair( - loopStart, - numberOfRepetitions)); - - // update description - //t.BlockDescription = "[LOOP START - " + numberOfRepetitions + " times]"; - - t.PauseInMS = 0; - - // add to tape - _datacorder.DataBlocks.Add(t); - - // advance to next block - _position += 2; - } - #endregion - - #region ID 25 - Loop end -/* length: 00 - - This is the same as BASIC's NEXT statement. It means that the utility should jump back to the start of the loop if it hasn't - been run for the specified number of times. - This block has no body. */ - private void ProcessBlockID25(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x25; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Loop_End; - - // get the most recent loop info - var loop = _loopCounter.LastOrDefault(); - - int loopStart = loop.Key; - int numberOfRepetitions = loop.Value; - - if (numberOfRepetitions == 0) - { - return; - } - - // get the number of blocks to loop - int blockCnt = _datacorder.DataBlocks.Count() - loopStart; - - // loop through each group to repeat - for (int b = 0; b < numberOfRepetitions; b++) - { - TapeDataBlock repeater = new TapeDataBlock(); - //repeater.BlockDescription = "[LOOP REPEAT - " + (b + 1) + "]"; - repeater.DataPeriods = new List(); - - // add the repeat block - _datacorder.DataBlocks.Add(repeater); - - // now iterate through and add the blocks to be repeated - for (int i = 0; i < blockCnt; i++) - { - var block = _datacorder.DataBlocks[loopStart + i]; - _datacorder.DataBlocks.Add(block); - } - } - } - #endregion - - #region ID 26 - Call sequence -/* length: [00,01]*02+02 - Offset Value Type Description - 0x00 N WORD Number of calls to be made - 0x02 - WORD[N] Array of call block numbers (relative-signed offsets) - - This block is an analogue of the CALL Subroutine statement. It basically executes a sequence of blocks that are somewhere else and - then goes back to the next block. Because more than one call can be normally used you can include a list of sequences to be called. - The 'nesting' of call blocks is also not allowed for the simplicity reasons. You can, of course, use the CALL blocks in the LOOP - sequences and vice versa. The value is relative for the obvious reasons - so that you can add some blocks in the beginning of the - file without disturbing the call values. Please take a look at 'Jump To Block' for reference on the values. */ - private void ProcessBlockID26(byte[] data) - { - // block processing not implemented for this - just gets added for informational purposes only - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x26; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Call_Sequence; - - int blockSize = 2 + 2 * GetWordValue(data, _position); - t.PauseInMS = 0; - - - // add to tape - _datacorder.DataBlocks.Add(t); - - // advance to next block - _position += blockSize; - } - #endregion - - #region ID 27 - Return from sequence -/* length: 00 - - This block indicates the end of the Called Sequence. The next block played will be the block after the last CALL block (or the next Call, - if the Call block had multiple calls). - Again, this block has no body. */ - private void ProcessBlockID27(byte[] data) - { - // block processing not implemented for this - just gets added for informational purposes only - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x27; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Return_From_Sequence; - t.PauseInMS = 0; - - - // add to tape - _datacorder.DataBlocks.Add(t); - } - #endregion - - #region ID 28 - Select block -/* length: [00,01]+02 - Offset Value Type Description - 0x00 - WORD Length of the whole block (without these two bytes) - 0x02 N BYTE Number of selections - 0x03 - SELECT[N] List of selections - - ---- - SELECT structure format - Offset Value Type Description - 0x00 - WORD Relative Offset - 0x02 L BYTE Length of description text - 0x03 - CHAR[L] Description text (please use single line and max. 30 chars) - ---- - - This block is useful when the tape consists of two or more separately-loadable parts. With this block, you are able to select - one of the parts and the utility/emulator will start loading from that block. For example you can use it when the game has a - separate Trainer or when it is a multiload. Of course, to make some use of it the emulator/utility has to show a menu with the - selections when it encounters such a block. All offsets are relative signed words. */ - private void ProcessBlockID28(byte[] data) - { - // block processing not implemented for this - just gets added for informational purposes only - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x28; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Select_Block; - - int blockSize = 2 + GetWordValue(data, _position); - - t.PauseInMS = 0; - - // add to tape - _datacorder.DataBlocks.Add(t); - - // advance to next block - _position += blockSize; - } - #endregion - - #region ID 2A - Stop the tape if in 48K mode -/* length: 04 - Offset Value Type Description - 0x00 0 DWORD Length of the block without these four bytes (0) - - When this block is encountered, the tape will stop ONLY if the machine is an 48K Spectrum. This block is to be used for - multiloading games that load one level at a time in 48K mode, but load the entire tape at once if in 128K mode. - This block has no body of its own, but follows the extension rule. */ - private void ProcessBlockID2A(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x2A; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Stop_the_Tape_48K; - t.Command = TapeCommand.STOP_THE_TAPE_48K; - - int blockSize = 4 + GetWordValue(data, _position); - - t.PauseInMS = 0; - - // add to tape - _datacorder.DataBlocks.Add(t); - - // advance to next block - _position += blockSize; - } - #endregion - - #region ID 2B - Set signal level -/* length: 05 - Offset Value Type Description - 0x00 1 DWORD Block length (without these four bytes) - 0x04 - BYTE Signal level (0=low, 1=high) - - This block sets the current signal level to the specified value (high or low). It should be used whenever it is necessary to avoid any - ambiguities, e.g. with custom loaders which are level-sensitive. */ - private void ProcessBlockID2B(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x2B; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Set_Signal_Level; - - t.PauseInMS = 0; - - // add to tape - _datacorder.DataBlocks.Add(t); - - // advance to next block - _position += 5; - } - #endregion - - #region ID 30 - Text description -/* length: [00]+01 - Offset Value Type Description - 0x00 N BYTE Length of the text description - 0x01 - CHAR[N] Text description in ASCII format - - This is meant to identify parts of the tape, so you know where level 1 starts, where to rewind to when the game ends, etc. - This description is not guaranteed to be shown while the tape is playing, but can be read while browsing the tape or changing - the tape pointer. - The description can be up to 255 characters long but please keep it down to about 30 so the programs can show it in one line - (where this is appropriate). - Please use 'Archive Info' block for title, authors, publisher, etc. */ - private void ProcessBlockID30(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x30; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Text_Description; - - int textLen = data[_position]; - _position++; - - string desc = Encoding.ASCII.GetString(data, _position, textLen); - - t.PauseInMS = 0; - - //t.BlockDescription = "[" + desc + "]"; - - // add to tape - _datacorder.DataBlocks.Add(t); - - // advance to next block - _position += textLen; - } - #endregion - - #region ID 31 - Message block -/* length: [01]+02 - Offset Value Type Description - 0x00 - BYTE Time (in seconds) for which the message should be displayed - 0x01 N BYTE Length of the text message - 0x02 - CHAR[N] Message that should be displayed in ASCII format - - This will enable the emulators to display a message for a given time. This should not stop the tape and it should not make silence. - If the time is 0 then the emulator should wait for the user to press a key. - The text message should: - stick to a maximum of 30 chars per line; - use single 0x0D (13 decimal) to separate lines; - stick to a maximum of 8 lines. - If you do not obey these rules, emulators may display your message in any way they like. */ - private void ProcessBlockID31(byte[] data) - { - // currently not implemented properly in ZXHawk - - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x31; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Message_Block; - - _position++; - - int msgLen = data[_position]; - _position++; - - string desc = Encoding.ASCII.GetString(data, _position, msgLen); - - t.Command = TapeCommand.SHOW_MESSAGE; - - //t.BlockDescription = "[MESSAGE: " + desc + "]"; - - t.PauseInMS = 0; - - // add to tape - _datacorder.DataBlocks.Add(t); - - // advance to next block - _position += msgLen; - } - #endregion - - #region ID 32 - Archive info -/* length: [00,01]+02 - Offset Value Type Description - 0x00 - WORD Length of the whole block (without these two bytes) - 0x02 N BYTE Number of text strings - 0x03 - TEXT[N] List of text strings - - ---- - TEXT structure format - Offset Value Type Description - 0x00 - BYTE Text identification byte: - 00 - Full title - 01 - Software house/publisher - 02 - Author(s) - 03 - Year of publication - 04 - Language - 05 - Game/utility type - 06 - Price - 07 - Protection scheme/loader - 08 - Origin - FF - Comment(s) - 0x01 L BYTE Length of text string - 0x02 - CHAR[L] Text string in ASCII format - ---- - - Use this block at the beginning of the tape to identify the title of the game, author, publisher, year of publication, price (including - the currency), type of software (arcade adventure, puzzle, word processor, ...), protection scheme it uses (Speedlock 1, Alkatraz, ...) - and its origin (Original, Budget re-release, ...), etc. This block is built in a way that allows easy future expansion. - The block consists of a series of text strings. Each text has its identification number (which tells us what the text means) and then - the ASCII text. To make it possible to skip this block, if needed, the length of the whole block is at the beginning of it. - If all texts on the tape are in English language then you don't have to supply the 'Language' field - The information about what hardware the tape uses is in the 'Hardware Type' block, so no need for it here. */ - private void ProcessBlockID32(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x32; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Archive_Info; - - int blockLen = GetWordValue(data, _position); - _position += 2; - int stringCount = data[_position++]; - - // iterate through each string - for (int s = 0; s < stringCount; s++) - { - // identify the type of text - int type = data[_position++]; - - // get text length - int strLen = data[_position++]; + // check whether this is a valid tzx format file by looking at the identifier in the header + // (first 7 bytes of the file) + string ident = Encoding.ASCII.GetString(data, 0, 7); + // and 'end of text' marker + byte eotm = data[7]; + + // version info + int majorVer = data[8]; + int minorVer = data[9]; + + if (ident != "ZXTape!" || eotm != 0x1A) + { + // this is not a valid TZX format file + return false; + } + else + { + return true; + } + } + + /// + /// DeSerialization method + /// + public override void Read(byte[] data) + { + // clear existing tape blocks + _datacorder.DataBlocks.Clear(); + + /* + // TZX Header + length: 10 bytes + Offset Value Type Description + 0x00 "ZXTape!" ASCII[7] TZX signature + 0x07 0x1A BYTE End of text file marker + 0x08 1 BYTE TZX major revision number + 0x09 20 BYTE TZX minor revision number + */ + + // check whether this is a valid tzx format file by looking at the identifier in the header + // (first 7 bytes of the file) + string ident = Encoding.ASCII.GetString(data, 0, 7); + // and 'end of text' marker + byte eotm = data[7]; + + // version info + int majorVer = data[8]; + int minorVer = data[9]; + + if (ident != "ZXTape!" || eotm != 0x1A) + { + // this is not a valid TZX format file + throw new Exception(this.GetType().ToString() + + "This is not a valid TZX format file"); + } + + // iterate through each block + _position = 10; + while (_position < data.Length) + { + // block ID is the first byte in a new block + int ID = data[_position++]; + + // process the data + ProcessBlock(data, ID); + } + + } + + /// + /// Processes a TZX block + /// + private void ProcessBlock(byte[] data, int id) + { + // process based on detected block ID + switch (id) + { + // ID 10 - Standard Speed Data Block + case 0x10: + ProcessBlockID10(data); + break; + // ID 11 - Turbo Speed Data Block + case 0x11: + ProcessBlockID11(data); + break; + // ID 12 - Pure Tone + case 0x12: + ProcessBlockID12(data); + break; + // ID 13 - Pulse sequence + case 0x13: + ProcessBlockID13(data); + break; + // ID 14 - Pure Data Block + case 0x14: + ProcessBlockID14(data); + break; + // ID 15 - Direct Recording + case 0x15: + ProcessBlockID15(data); + break; + // ID 18 - CSW Recording + case 0x18: + ProcessBlockID18(data); + break; + // ID 19 - Generalized Data Block + case 0x19: + ProcessBlockID19(data); + break; + // ID 20 - Pause (silence) or 'Stop the Tape' command + case 0x20: + ProcessBlockID20(data); + break; + // ID 21 - Group start + case 0x21: + ProcessBlockID21(data); + break; + // ID 22 - Group end + case 0x22: + ProcessBlockID22(data); + break; + // ID 23 - Jump to block + case 0x23: + ProcessBlockID23(data); + break; + // ID 24 - Loop start + case 0x24: + ProcessBlockID24(data); + break; + // ID 25 - Loop end + case 0x25: + ProcessBlockID25(data); + break; + // ID 26 - Call sequence + case 0x26: + ProcessBlockID26(data); + break; + // ID 27 - Return from sequence + case 0x27: + ProcessBlockID27(data); + break; + // ID 28 - Select block + case 0x28: + ProcessBlockID28(data); + break; + // ID 2A - Stop the tape if in 48K mode + case 0x2A: + ProcessBlockID2A(data); + break; + // ID 2B - Set signal level + case 0x2B: + ProcessBlockID2B(data); + break; + // ID 30 - Text description + case 0x30: + ProcessBlockID30(data); + break; + // ID 31 - Message block + case 0x31: + ProcessBlockID31(data); + break; + // ID 32 - Archive info + case 0x32: + ProcessBlockID32(data); + break; + // ID 33 - Hardware type + case 0x33: + ProcessBlockID33(data); + break; + // ID 35 - Custom info block + case 0x35: + ProcessBlockID35(data); + break; + // ID 5A - "Glue" block + case 0x5A: + ProcessBlockID5A(data); + break; + + #region Depreciated Blocks + + // ID 16 - C64 ROM Type Data Block + case 0x16: + ProcessBlockID16(data); + break; + // ID 17 - C64 Turbo Tape Data Block + case 0x17: + ProcessBlockID17(data); + break; + // ID 34 - Emulation info + case 0x34: + ProcessBlockID34(data); + break; + // ID 40 - Snapshot block + case 0x40: + ProcessBlockID40(data); + break; + + #endregion + + default: + ProcessUnidentifiedBlock(data); + break; + } + } + + #region TZX Block Processors + + #region ID 10 - Standard Speed Data Block + /* length: [02,03]+04 + Offset Value Type Description + 0x00 - WORD Pause after this block (ms.) {1000} + 0x02 N WORD Length of data that follow + 0x04 - BYTE[N] Data as in .TAP files + + This block must be replayed with the standard Spectrum ROM timing values - see the values in + curly brackets in block ID 11. The pilot tone consists in 8063 pulses if the first data byte + (flag byte) is < 128, 3223 otherwise. This block can be used for the ROM loading routines AND + for custom loading routines that use the same timings as ROM ones do. */ + private void ProcessBlockID10(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x10; + t.BlockDescription = BlockType.Standard_Speed_Data_Block; + t.DataPeriods = new List(); + + int pauseLen = GetWordValue(data, _position); + if (pauseLen == 0) + pauseLen = 1000; + + t.PauseInMS = pauseLen; + + int blockLen = GetWordValue(data, _position + 2); + + _position += 4; + + byte[] tmp = new byte[blockLen]; + tmp = data.Skip(_position).Take(blockLen).ToArray(); + + var t2 = DecodeDataBlock(t, tmp, DataBlockType.Standard, pauseLen); + + // add the block + _datacorder.DataBlocks.Add(t2); + + // advance the position to the next block + _position += blockLen; + + // generate PAUSE block + CreatePauseBlock(_datacorder.DataBlocks.Last()); + } + #endregion + + #region ID 11 - Turbo Speed Data Block + /* length: [0F,10,11]+12 + Offset Value Type Description + 0x00 - WORD Length of PILOT pulse {2168} + 0x02 - WORD Length of SYNC first pulse {667} + 0x04 - WORD Length of SYNC second pulse {735} + 0x06 - WORD Length of ZERO bit pulse {855} + 0x08 - WORD Length of ONE bit pulse {1710} + 0x0A - WORD Length of PILOT tone (number of pulses) {8063 header (flag<128), 3223 data (flag>=128)} + 0x0C - BYTE Used bits in the last byte (other bits should be 0) {8} + (e.g. if this is 6, then the bits used (x) in the last byte are: xxxxxx00, + where MSb is the leftmost bit, LSb is the rightmost bit) + 0x0D - WORD Pause after this block (ms.) {1000} + 0x0F N BYTE[3] Length of data that follow + 0x12 - BYTE[N] Data as in .TAP files + + This block is very similar to the normal TAP block but with some additional info on the timings and other important + differences. The same tape encoding is used as for the standard speed data block. If a block should use some non-standard + sync or pilot tones (i.e. all sorts of protection schemes) then use the next three blocks to describe it.*/ + private void ProcessBlockID11(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x11; + t.BlockDescription = BlockType.Turbo_Speed_Data_Block; + t.DataPeriods = new List(); + + int pilotPL = GetWordValue(data, _position); + int sync1P = GetWordValue(data, _position + 2); + int sync2P = GetWordValue(data, _position + 4); + int bit0P = GetWordValue(data, _position + 6); + int bit1P = GetWordValue(data, _position + 8); + int pilotTL = GetWordValue(data, _position + 10); + int bitinbyte = data[_position + 12]; + int pause = GetWordValue(data, _position + 13); + + + int blockLen = 0xFFFFFF & GetInt32(data, _position + 0x0F); + + byte[] bLenArr = data.Skip(_position + 0x0F).Take(3).ToArray(); + + _position += 0x12; + + byte[] tmp = new byte[blockLen]; + tmp = data.Skip(_position).Take(blockLen).ToArray(); + + var t2 = DecodeDataBlock(t, tmp, DataBlockType.Turbo, pause, pilotTL, pilotPL, sync1P, sync2P, bit0P, bit1P, bitinbyte); + + t.PauseInMS = pause; + + // add the block + _datacorder.DataBlocks.Add(t2); + + // advance the position to the next block + _position += blockLen; + + // generate PAUSE block + CreatePauseBlock(_datacorder.DataBlocks.Last()); + } + #endregion + + #region ID 12 - Pure Tone + /* length: 04 + Offset Value Type Description + 0x00 - WORD Length of one pulse in T-states + 0x02 - WORD Number of pulses + + This will produce a tone which is basically the same as the pilot tone in the ID 10, ID 11 blocks. You can define how + long the pulse is and how many pulses are in the tone. */ + private void ProcessBlockID12(byte[] data) + { + int blockLen = 4; + + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x12; + t.BlockDescription = BlockType.Pure_Tone; + t.DataPeriods = new List(); + t.PauseInMS = 0; + + // get values + int pulseLength = GetWordValue(data, _position); + int pulseCount = GetWordValue(data, _position + 2); + + t.AddMetaData(BlockDescriptorTitle.Pulse_Length, pulseLength.ToString() + " T-States"); + t.AddMetaData(BlockDescriptorTitle.Pulse_Count, pulseCount.ToString()); + + // build period information + for (int p = 0; p < pulseCount; p++) + { + t.DataPeriods.Add(pulseLength); + } + + // add the block + _datacorder.DataBlocks.Add(t); + + // advance the position to the next block + _position += blockLen; + } + #endregion + + #region ID 13 - Pulse sequence + /* length: [00]*02+01 + Offset Value Type Description + 0x00 N BYTE Number of pulses + 0x01 - WORD[N] Pulses' lengths + + This will produce N pulses, each having its own timing. Up to 255 pulses can be stored in this block; this is useful for non-standard + sync tones used by some protection schemes. */ + private void ProcessBlockID13(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x13; + t.BlockDescription = BlockType.Pulse_Sequence; + t.DataPeriods = new List(); + + t.PauseInMS = 0; + + // get pulse count + int pulseCount = data[_position]; + t.AddMetaData(BlockDescriptorTitle.Pulse_Count, pulseCount.ToString()); + _position++; + + // build period information + for (int p = 0; p < pulseCount; p++, _position += 2) + { + // get pulse length + int pulseLength = GetWordValue(data, _position); + t.AddMetaData(BlockDescriptorTitle.Needs_Parsing, "Pulse " + p + " Length\t" + pulseLength.ToString() + " T-States"); + t.DataPeriods.Add(pulseLength); + } + + // add the block + _datacorder.DataBlocks.Add(t); + } + #endregion + + #region ID 14 - Pure Data Block + /* length: [07,08,09]+0A + Offset Value Type Description + 0x00 - WORD Length of ZERO bit pulse + 0x02 - WORD Length of ONE bit pulse + 0x04 - BYTE Used bits in last byte (other bits should be 0) + (e.g. if this is 6, then the bits used (x) in the last byte are: xxxxxx00, + where MSb is the leftmost bit, LSb is the rightmost bit) + 0x05 - WORD Pause after this block (ms.) + 0x07 N BYTE[3] Length of data that follow + 0x0A - BYTE[N] Data as in .TAP files + + This is the same as in the turbo loading data block, except that it has no pilot or sync pulses. */ + private void ProcessBlockID14(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x14; + t.BlockDescription = BlockType.Pure_Data_Block; + t.DataPeriods = new List(); + + int pilotPL = 0; + int sync1P = 0; + int sync2P = 0; + int bit0P = GetWordValue(data, _position + 0); + int bit1P = GetWordValue(data, _position + 2); + int pilotTL = 0; + int bitinbyte = data[_position + 4]; + int pause = GetWordValue(data, _position + 5); + + int blockLen = 0xFFFFFF & GetInt32(data, _position + 0x07); + + _position += 0x0A; + + byte[] tmp = new byte[blockLen]; + tmp = data.Skip(_position).Take(blockLen).ToArray(); + + var t2 = DecodeDataBlock(t, tmp, DataBlockType.Pure, pause, pilotTL, pilotPL, sync1P, sync2P, bit0P, bit1P, bitinbyte); + + t.PauseInMS = pause; + + // add the block + _datacorder.DataBlocks.Add(t2); + + // advance the position to the next block + _position += blockLen; + + // generate PAUSE block + CreatePauseBlock(_datacorder.DataBlocks.Last()); + } + #endregion + + #region ID 15 - Direct Recording + /* length: [05,06,07]+08 + Offset Value Type Description + 0x00 - WORD Number of T-states per sample (bit of data) + 0x02 - WORD Pause after this block in milliseconds (ms.) + 0x04 - BYTE Used bits (samples) in last byte of data (1-8) + (e.g. if this is 2, only first two samples of the last byte will be played) + 0x05 N BYTE[3] Length of samples' data + 0x08 - BYTE[N] Samples data. Each bit represents a state on the EAR port (i.e. one sample). + MSb is played first. + + This block is used for tapes which have some parts in a format such that the turbo loader block cannot be used. + This is not like a VOC file, since the information is much more compact. Each sample value is represented by one bit only + (0 for low, 1 for high) which means that the block will be at most 1/8 the size of the equivalent VOC. + The preferred sampling frequencies are 22050 or 44100 Hz (158 or 79 T-states/sample). + Please, if you can, don't use other sampling frequencies. + Please use this block only if you cannot use any other block. */ + private void ProcessBlockID15(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x15; + t.BlockDescription = BlockType.Direct_Recording; + t.DataPeriods = new List(); + + // get values + int samLen = GetInt32(data, _position + 5); + int samSize = 0xFFFFFF & samLen; + + int tStatesPerSample = GetWordValue(data, _position); + int pauseAfterBlock = GetWordValue(data, _position + 2); + int usedBitsInLastByte = data[_position + 4]; + + // skip to samples data + _position += 8; + + int pulseLength = 0; + int pulseCount = 0; + + // ascertain the pulse count + for (int i = 0; i < samSize; i++) + { + for (int p = 0x80; p != 0; p >>= 1) + { + if (((data[_position + i] ^ pulseLength) & p) != 0) + { + pulseCount++; + pulseLength ^= -1; + } + } + } + + // get the pulses + t.DataPeriods = new List(pulseCount + 2); + int tStateCount = 0; + pulseLength = 0; + for (int i = 1; i < samSize; i++) + { + for (int p = 0x80; p != 0; p >>= 1) + { + tStateCount += tStatesPerSample; + if (((data[_position] ^ pulseLength) & p) != 0) + { + t.DataPeriods.Add(tStateCount); + pulseLength ^= -1; + tStateCount = 0; + } + } + + // incrememt position + _position++; + } + + // get the pulses in the last byte of data + for (int p = 0x80; p != (byte)(0x80 >> usedBitsInLastByte); p >>= 1) + { + tStateCount += tStatesPerSample; + if (((data[_position] ^ pulseLength) & p) != 0) + { + t.DataPeriods.Add(tStateCount); + pulseLength ^= -1; + tStateCount = 0; + } + } + + // add final pulse + t.DataPeriods.Add(tStateCount); + + // add end of block pause + if (pauseAfterBlock > 0) + { + //t.DataPeriods.Add(3500 * pauseAfterBlock); + } + + t.PauseInMS = pauseAfterBlock; + + // increment position + _position++; + + // add the block + _datacorder.DataBlocks.Add(t); + + // generate PAUSE block + CreatePauseBlock(_datacorder.DataBlocks.Last()); + } + #endregion + + #region ID 18 - CSW Recording + /* length: [00,01,02,03]+04 + Offset Value Type Description + 0x00 10+N DWORD Block length (without these four bytes) + 0x04 - WORD Pause after this block (in ms). + 0x06 - BYTE[3] Sampling rate + 0x09 - BYTE Compression type + 0x01: RLE + 0x02: Z-RLE + 0x0A - DWORD Number of stored pulses (after decompression, for validation purposes) + 0x0E - BYTE[N] CSW data, encoded according to the CSW file format specification. + + This block contains a sequence of raw pulses encoded in CSW format v2 (Compressed Square Wave). */ + private void ProcessBlockID18(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x18; + t.BlockDescription = BlockType.CSW_Recording; + t.DataPeriods = new List(); + + int blockLen = GetInt32(data, _position); + _position += 4; + + t.PauseInMS = GetWordValue(data, _position); + + _position += 2; + + int sampleRate = data[_position++] << 16 | data[_position++] << 8 | data[_position++]; + byte compType = data[_position++]; + int pulses = GetInt32(data, _position); + _position += 4; + + int dataLen = blockLen - 10; + + // build source array + byte[] src = new byte[dataLen]; + // build destination array + byte[] dest = new byte[pulses + 1]; + + // process the CSW data + CswConverter.ProcessCSWV2(src, ref dest, compType, pulses); + + // create the periods + var rate = (69888 * 50) / sampleRate; + + for (int i = 0; i < dest.Length;) + { + int length = dest[i++] * rate; + if (length == 0) + { + length = GetInt32(dest, i) / rate; + i += 4; + } + + t.DataPeriods.Add(length); + } + + // add closing period + t.DataPeriods.Add((69888 * 50) / 10); + + _position += dataLen; + //_position += blockLen; + + // add the block + _datacorder.DataBlocks.Add(t); + + // generate PAUSE block + CreatePauseBlock(_datacorder.DataBlocks.Last()); + } + #endregion + + #region ID 19 - Generalized Data Block + /* length: [00,01,02,03]+04 + Offset Value Type Description + 0x00 - DWORD Block length (without these four bytes) + 0x04 - WORD Pause after this block (ms) + 0x06 TOTP DWORD Total number of symbols in pilot/sync block (can be 0) + 0x0A NPP BYTE Maximum number of pulses per pilot/sync symbol + 0x0B ASP BYTE Number of pilot/sync symbols in the alphabet table (0=256) + 0x0C TOTD DWORD Total number of symbols in data stream (can be 0) + 0x10 NPD BYTE Maximum number of pulses per data symbol + 0x11 ASD BYTE Number of data symbols in the alphabet table (0=256) + 0x12 - SYMDEF[ASP] Pilot and sync symbols definition table + This field is present only if TOTP>0 + 0x12+ + (2*NPP+1)*ASP - PRLE[TOTP] Pilot and sync data stream + This field is present only if TOTP>0 + 0x12+ + (TOTP>0)*((2*NPP+1)*ASP)+ + TOTP*3 - SYMDEF[ASD] Data symbols definition table + This field is present only if TOTD>0 + 0x12+ + (TOTP>0)*((2*NPP+1)*ASP)+ + TOTP*3+ + (2*NPD+1)*ASD - BYTE[DS] Data stream + This field is present only if TOTD>0 + + This block has been specifically developed to represent an extremely wide range of data encoding techniques. + The basic idea is that each loading component (pilot tone, sync pulses, data) is associated to a specific sequence + of pulses, where each sequence (wave) can contain a different number of pulses from the others. + In this way we can have a situation where bit 0 is represented with 4 pulses and bit 1 with 8 pulses. + + ---- + SYMDEF structure format + Offset Value Type Description + 0x00 - BYTE Symbol flags + b0-b1: starting symbol polarity + 00: opposite to the current level (make an edge, as usual) - default + 01: same as the current level (no edge - prolongs the previous pulse) + 10: force low level + 11: force high level + 0x01 - WORD[MAXP] Array of pulse lengths. + + The alphabet is stored using a table where each symbol is a row of pulses. The number of columns (i.e. pulses) of the table is the + length of the longest sequence amongst all (MAXP=NPP or NPD, for pilot/sync or data blocks respectively); shorter waves are terminated by a + zero-length pulse in the sequence. + Any number of data symbols is allowed, so we can have more than two distinct waves; for example, imagine a loader which writes two bits at a + time by encoding them with four distinct pulse lengths: this loader would have an alphabet of four symbols, each associated to a specific + sequence of pulses (wave). + ---- + ---- + PRLE structure format + Offset Value Type Description + 0x00 - BYTE Symbol to be represented + 0x01 - WORD Number of repetitions + + Most commonly, pilot and sync are repetitions of the same pulse, thus they are represented using a very simple RLE encoding structure which stores + the symbol and the number of times it must be repeated. + Each symbol in the data stream is represented by a string of NB bits of the block data, where NB = ceiling(Log2(ASD)). + Thus the length of the whole data stream in bits is NB*TOTD, or in bytes DS=ceil(NB*TOTD/8). + ---- */ + private void ProcessBlockID19(byte[] data) + { + // not currently implemented properly + + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x19; + t.BlockDescription = BlockType.Generalized_Data_Block; + t.DataPeriods = new List(); + + int blockLen = GetInt32(data, _position); + _position += 4; + + int pause = GetWordValue(data, _position); + _position += 2; + + int totp = GetInt32(data, _position); + _position += 4; + + int npp = data[_position++]; + + int asp = data[_position++]; + + int totd = GetInt32(data, _position); + _position += 4; + + int npd = data[_position++]; + + int asd = data[_position++]; + + // add the block + _datacorder.DataBlocks.Add(t); + + // advance the position to the next block + _position += blockLen; + } + #endregion + + #region ID 20 - Pause (silence) or 'Stop the Tape' command + /* length: 02 + Offset Value Type Description + 0x00 - WORD Pause duration (ms.) + + This will make a silence (low amplitude level (0)) for a given time in milliseconds. If the value is 0 then the + emulator or utility should (in effect) STOP THE TAPE, i.e. should not continue loading until the user or emulator requests it. */ + private void ProcessBlockID20(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x20; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Pause_or_Stop_the_Tape; + + int pauseDuration = GetWordValue(data, _position); + if (pauseDuration != 0) + { + //t.BlockDescription = "Pause: " + pauseDuration + " ms"; + } + else + { + //t.BlockDescription = "[STOP THE TAPE]"; + } + + t.PauseInMS = pauseDuration; + + if (pauseDuration == 0) + { + // issue stop the tape command + t.Command = TapeCommand.STOP_THE_TAPE; + // add 1ms period + //t.DataPeriods.Add(3500); + //pauseDuration = -1; + + } + else + { + // this is actually just a pause + //pauseDuration = 3500 * pauseDuration; + //t.DataPeriods.Add(pauseDuration); + } + + // add end of block pause + //t.DataPeriods.Add(pauseDuration); + + // add to tape + _datacorder.DataBlocks.Add(t); + + // advanced position to next block + _position += 2; + + // generate PAUSE block + CreatePauseBlock(_datacorder.DataBlocks.Last()); + + } + #endregion + + #region ID 21 - Group start + /* length: [00]+01 + Offset Value Type Description + 0x00 L BYTE Length of the group name string + 0x01 - CHAR[L] Group name in ASCII format (please keep it under 30 characters long) + + This block marks the start of a group of blocks which are to be treated as one single (composite) block. + This is very handy for tapes that use lots of subblocks like Bleepload (which may well have over 160 custom loading blocks). + You can also give the group a name (example 'Bleepload Block 1'). + For each group start block, there must be a group end block. Nesting of groups is not allowed. */ + private void ProcessBlockID21(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x21; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Group_Start; + + int nameLength = data[_position]; + _position++; + + string name = Encoding.ASCII.GetString(data, _position, nameLength); + //t.BlockDescription = "[GROUP: " + name + "]"; + t.Command = TapeCommand.BEGIN_GROUP; + + t.PauseInMS = 0; + + // add to tape + _datacorder.DataBlocks.Add(t); + + // advance to next block + _position += nameLength; + } + #endregion + + #region ID 22 - Group end + /* length: 00 + + This indicates the end of a group. This block has no body. */ + private void ProcessBlockID22(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x22; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Group_End; + t.Command = TapeCommand.END_GROUP; + + t.PauseInMS = 0; + + // add to tape + _datacorder.DataBlocks.Add(t); + } + #endregion + + #region ID 23 - Jump to block + /* length: 02 + Offset Value Type Description + 0x00 - WORD Relative jump value + + This block will enable you to jump from one block to another within the file. The value is a signed short word + (usually 'signed short' in C); Some examples: + Jump 0 = 'Loop Forever' - this should never happen + Jump 1 = 'Go to the next block' - it is like NOP in assembler ;) + Jump 2 = 'Skip one block' + Jump -1 = 'Go to the previous block' + All blocks are included in the block count!. */ + private void ProcessBlockID23(byte[] data) + { + // not implemented properly + + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x23; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Jump_to_Block; + + int relativeJumpValue = GetWordValue(data, _position); + string result = string.Empty; + + switch (relativeJumpValue) + { + case 0: + result = "Loop Forever"; + break; + case 1: + result = "To Next Block"; + break; + case 2: + result = "Skip One Block"; + break; + case -1: + result = "Go to Previous Block"; + break; + } + + //t.BlockDescription = "[JUMP BLOCK - " + result +"]"; + + t.PauseInMS = 0; + + // add to tape + _datacorder.DataBlocks.Add(t); + + // advance to next block + _position += 2; + } + #endregion + + #region ID 24 - Loop start + /* length: 02 + Offset Value Type Description + 0x00 - WORD Number of repetitions (greater than 1) + + If you have a sequence of identical blocks, or of identical groups of blocks, you can use this block to tell how many times they should + be repeated. This block is the same as the FOR statement in BASIC. + For simplicity reasons don't nest loop blocks! */ + private void ProcessBlockID24(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x24; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Loop_Start; + + // loop should start from the next block + int loopStart = _datacorder.DataBlocks.Count() + 1; + + int numberOfRepetitions = GetWordValue(data, _position); + + // update loop counter + _loopCounter.Add( + new KeyValuePair( + loopStart, + numberOfRepetitions)); + + // update description + //t.BlockDescription = "[LOOP START - " + numberOfRepetitions + " times]"; + + t.PauseInMS = 0; + + // add to tape + _datacorder.DataBlocks.Add(t); + + // advance to next block + _position += 2; + } + #endregion + + #region ID 25 - Loop end + /* length: 00 + + This is the same as BASIC's NEXT statement. It means that the utility should jump back to the start of the loop if it hasn't + been run for the specified number of times. + This block has no body. */ + private void ProcessBlockID25(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x25; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Loop_End; + + // get the most recent loop info + var loop = _loopCounter.LastOrDefault(); + + int loopStart = loop.Key; + int numberOfRepetitions = loop.Value; + + if (numberOfRepetitions == 0) + { + return; + } + + // get the number of blocks to loop + int blockCnt = _datacorder.DataBlocks.Count() - loopStart; + + // loop through each group to repeat + for (int b = 0; b < numberOfRepetitions; b++) + { + TapeDataBlock repeater = new TapeDataBlock(); + //repeater.BlockDescription = "[LOOP REPEAT - " + (b + 1) + "]"; + repeater.DataPeriods = new List(); + + // add the repeat block + _datacorder.DataBlocks.Add(repeater); + + // now iterate through and add the blocks to be repeated + for (int i = 0; i < blockCnt; i++) + { + var block = _datacorder.DataBlocks[loopStart + i]; + _datacorder.DataBlocks.Add(block); + } + } + } + #endregion + + #region ID 26 - Call sequence + /* length: [00,01]*02+02 + Offset Value Type Description + 0x00 N WORD Number of calls to be made + 0x02 - WORD[N] Array of call block numbers (relative-signed offsets) + + This block is an analogue of the CALL Subroutine statement. It basically executes a sequence of blocks that are somewhere else and + then goes back to the next block. Because more than one call can be normally used you can include a list of sequences to be called. + The 'nesting' of call blocks is also not allowed for the simplicity reasons. You can, of course, use the CALL blocks in the LOOP + sequences and vice versa. The value is relative for the obvious reasons - so that you can add some blocks in the beginning of the + file without disturbing the call values. Please take a look at 'Jump To Block' for reference on the values. */ + private void ProcessBlockID26(byte[] data) + { + // block processing not implemented for this - just gets added for informational purposes only + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x26; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Call_Sequence; + + int blockSize = 2 + 2 * GetWordValue(data, _position); + t.PauseInMS = 0; + + + // add to tape + _datacorder.DataBlocks.Add(t); + + // advance to next block + _position += blockSize; + } + #endregion + + #region ID 27 - Return from sequence + /* length: 00 + + This block indicates the end of the Called Sequence. The next block played will be the block after the last CALL block (or the next Call, + if the Call block had multiple calls). + Again, this block has no body. */ + private void ProcessBlockID27(byte[] data) + { + // block processing not implemented for this - just gets added for informational purposes only + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x27; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Return_From_Sequence; + t.PauseInMS = 0; + + + // add to tape + _datacorder.DataBlocks.Add(t); + } + #endregion + + #region ID 28 - Select block + /* length: [00,01]+02 + Offset Value Type Description + 0x00 - WORD Length of the whole block (without these two bytes) + 0x02 N BYTE Number of selections + 0x03 - SELECT[N] List of selections + + ---- + SELECT structure format + Offset Value Type Description + 0x00 - WORD Relative Offset + 0x02 L BYTE Length of description text + 0x03 - CHAR[L] Description text (please use single line and max. 30 chars) + ---- + + This block is useful when the tape consists of two or more separately-loadable parts. With this block, you are able to select + one of the parts and the utility/emulator will start loading from that block. For example you can use it when the game has a + separate Trainer or when it is a multiload. Of course, to make some use of it the emulator/utility has to show a menu with the + selections when it encounters such a block. All offsets are relative signed words. */ + private void ProcessBlockID28(byte[] data) + { + // block processing not implemented for this - just gets added for informational purposes only + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x28; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Select_Block; + + int blockSize = 2 + GetWordValue(data, _position); + + t.PauseInMS = 0; + + // add to tape + _datacorder.DataBlocks.Add(t); + + // advance to next block + _position += blockSize; + } + #endregion + + #region ID 2A - Stop the tape if in 48K mode + /* length: 04 + Offset Value Type Description + 0x00 0 DWORD Length of the block without these four bytes (0) + + When this block is encountered, the tape will stop ONLY if the machine is an 48K Spectrum. This block is to be used for + multiloading games that load one level at a time in 48K mode, but load the entire tape at once if in 128K mode. + This block has no body of its own, but follows the extension rule. */ + private void ProcessBlockID2A(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x2A; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Stop_the_Tape_48K; + t.Command = TapeCommand.STOP_THE_TAPE_48K; + + int blockSize = 4 + GetWordValue(data, _position); + + t.PauseInMS = 0; + + // add to tape + _datacorder.DataBlocks.Add(t); + + // advance to next block + _position += blockSize; + } + #endregion + + #region ID 2B - Set signal level + /* length: 05 + Offset Value Type Description + 0x00 1 DWORD Block length (without these four bytes) + 0x04 - BYTE Signal level (0=low, 1=high) + + This block sets the current signal level to the specified value (high or low). It should be used whenever it is necessary to avoid any + ambiguities, e.g. with custom loaders which are level-sensitive. */ + private void ProcessBlockID2B(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x2B; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Set_Signal_Level; + + t.PauseInMS = 0; + + // add to tape + _datacorder.DataBlocks.Add(t); + + // advance to next block + _position += 5; + } + #endregion + + #region ID 30 - Text description + /* length: [00]+01 + Offset Value Type Description + 0x00 N BYTE Length of the text description + 0x01 - CHAR[N] Text description in ASCII format + + This is meant to identify parts of the tape, so you know where level 1 starts, where to rewind to when the game ends, etc. + This description is not guaranteed to be shown while the tape is playing, but can be read while browsing the tape or changing + the tape pointer. + The description can be up to 255 characters long but please keep it down to about 30 so the programs can show it in one line + (where this is appropriate). + Please use 'Archive Info' block for title, authors, publisher, etc. */ + private void ProcessBlockID30(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x30; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Text_Description; + + int textLen = data[_position]; + _position++; + + string desc = Encoding.ASCII.GetString(data, _position, textLen); + + t.PauseInMS = 0; + + //t.BlockDescription = "[" + desc + "]"; + + // add to tape + _datacorder.DataBlocks.Add(t); + + // advance to next block + _position += textLen; + } + #endregion + + #region ID 31 - Message block + /* length: [01]+02 + Offset Value Type Description + 0x00 - BYTE Time (in seconds) for which the message should be displayed + 0x01 N BYTE Length of the text message + 0x02 - CHAR[N] Message that should be displayed in ASCII format + + This will enable the emulators to display a message for a given time. This should not stop the tape and it should not make silence. + If the time is 0 then the emulator should wait for the user to press a key. + The text message should: + stick to a maximum of 30 chars per line; + use single 0x0D (13 decimal) to separate lines; + stick to a maximum of 8 lines. + If you do not obey these rules, emulators may display your message in any way they like. */ + private void ProcessBlockID31(byte[] data) + { + // currently not implemented properly in ZXHawk + + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x31; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Message_Block; + + _position++; + + int msgLen = data[_position]; + _position++; + + string desc = Encoding.ASCII.GetString(data, _position, msgLen); + + t.Command = TapeCommand.SHOW_MESSAGE; + + //t.BlockDescription = "[MESSAGE: " + desc + "]"; + + t.PauseInMS = 0; + + // add to tape + _datacorder.DataBlocks.Add(t); + + // advance to next block + _position += msgLen; + } + #endregion + + #region ID 32 - Archive info + /* length: [00,01]+02 + Offset Value Type Description + 0x00 - WORD Length of the whole block (without these two bytes) + 0x02 N BYTE Number of text strings + 0x03 - TEXT[N] List of text strings + + ---- + TEXT structure format + Offset Value Type Description + 0x00 - BYTE Text identification byte: + 00 - Full title + 01 - Software house/publisher + 02 - Author(s) + 03 - Year of publication + 04 - Language + 05 - Game/utility type + 06 - Price + 07 - Protection scheme/loader + 08 - Origin + FF - Comment(s) + 0x01 L BYTE Length of text string + 0x02 - CHAR[L] Text string in ASCII format + ---- + + Use this block at the beginning of the tape to identify the title of the game, author, publisher, year of publication, price (including + the currency), type of software (arcade adventure, puzzle, word processor, ...), protection scheme it uses (Speedlock 1, Alkatraz, ...) + and its origin (Original, Budget re-release, ...), etc. This block is built in a way that allows easy future expansion. + The block consists of a series of text strings. Each text has its identification number (which tells us what the text means) and then + the ASCII text. To make it possible to skip this block, if needed, the length of the whole block is at the beginning of it. + If all texts on the tape are in English language then you don't have to supply the 'Language' field + The information about what hardware the tape uses is in the 'Hardware Type' block, so no need for it here. */ + private void ProcessBlockID32(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x32; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Archive_Info; + + int blockLen = GetWordValue(data, _position); + _position += 2; + int stringCount = data[_position++]; + + // iterate through each string + for (int s = 0; s < stringCount; s++) + { + // identify the type of text + int type = data[_position++]; + + // get text length + int strLen = data[_position++]; string title = String.Empty; title = "Info: "; switch (type) - { - case 0x00: - title = "Full Title: "; - break; - case 0x01: - title = "Software House/Publisher: "; - break; - case 0x02: - title = "Author(s): "; - break; - case 0x03: - title = "Year of Publication: "; - break; - case 0x04: - title = "Language: "; - break; - case 0x05: - title = "Game/Utility Type: "; - break; - case 0x06: - title = "Price: "; - break; - case 0x07: - title = "Protection Scheme/Loader: "; - break; - case 0x08: - title = "Origin: "; - break; - case 0xFF: - title = "Comment(s): "; - break; - default: - break; - } + { + case 0x00: + title = "Full Title: "; + break; + case 0x01: + title = "Software House/Publisher: "; + break; + case 0x02: + title = "Author(s): "; + break; + case 0x03: + title = "Year of Publication: "; + break; + case 0x04: + title = "Language: "; + break; + case 0x05: + title = "Game/Utility Type: "; + break; + case 0x06: + title = "Price: "; + break; + case 0x07: + title = "Protection Scheme/Loader: "; + break; + case 0x08: + title = "Origin: "; + break; + case 0xFF: + title = "Comment(s): "; + break; + default: + break; + } - // add title to description - //t.BlockDescription += title; + // add title to description + //t.BlockDescription += title; - // get string data - string val = Encoding.ASCII.GetString(data, _position, strLen); - //t.BlockDescription += val + " \n"; + // get string data + string val = Encoding.ASCII.GetString(data, _position, strLen); + //t.BlockDescription += val + " \n"; - t.PauseInMS = 0; + t.PauseInMS = 0; - // advance to next string block - _position += strLen; - } + // advance to next string block + _position += strLen; + } - // add to tape - _datacorder.DataBlocks.Add(t); - } - #endregion + // add to tape + _datacorder.DataBlocks.Add(t); + } + #endregion - #region ID 33 - Hardware type -/* length: [00]*03+01 - Offset Value Type Description - 0x00 N BYTE Number of machines and hardware types for which info is supplied - 0x01 - HWINFO[N] List of machines and hardware + #region ID 33 - Hardware type + /* length: [00]*03+01 + Offset Value Type Description + 0x00 N BYTE Number of machines and hardware types for which info is supplied + 0x01 - HWINFO[N] List of machines and hardware - ---- - HWINFO structure format - Offset Value Type Description - 0x00 - BYTE Hardware type - 0x01 - BYTE Hardware ID - 0x02 - BYTE Hardware information: - 00 - The tape RUNS on this machine or with this hardware, - but may or may not use the hardware or special features of the machine. - 01 - The tape USES the hardware or special features of the machine, - such as extra memory or a sound chip. - 02 - The tape RUNS but it DOESN'T use the hardware - or special features of the machine. - 03 - The tape DOESN'T RUN on this machine or with this hardware. - ---- + ---- + HWINFO structure format + Offset Value Type Description + 0x00 - BYTE Hardware type + 0x01 - BYTE Hardware ID + 0x02 - BYTE Hardware information: + 00 - The tape RUNS on this machine or with this hardware, + but may or may not use the hardware or special features of the machine. + 01 - The tape USES the hardware or special features of the machine, + such as extra memory or a sound chip. + 02 - The tape RUNS but it DOESN'T use the hardware + or special features of the machine. + 03 - The tape DOESN'T RUN on this machine or with this hardware. + ---- - This blocks contains information about the hardware that the programs on this tape use. Please include only machines and hardware for - which you are 100% sure that it either runs (or doesn't run) on or with, or you know it uses (or doesn't use) the hardware or special - features of that machine. - If the tape runs only on the ZX81 (and TS1000, etc.) then it clearly won't work on any Spectrum or Spectrum variant, so there's no - need to list this information. - If you are not sure or you haven't tested a tape on some particular machine/hardware combination then do not include it in the list. - The list of hardware types and IDs is somewhat large, and may be found at the end of the format description. */ - private void ProcessBlockID33(byte[] data) - { - // currently not implemented properly in ZXHawk + This blocks contains information about the hardware that the programs on this tape use. Please include only machines and hardware for + which you are 100% sure that it either runs (or doesn't run) on or with, or you know it uses (or doesn't use) the hardware or special + features of that machine. + If the tape runs only on the ZX81 (and TS1000, etc.) then it clearly won't work on any Spectrum or Spectrum variant, so there's no + need to list this information. + If you are not sure or you haven't tested a tape on some particular machine/hardware combination then do not include it in the list. + The list of hardware types and IDs is somewhat large, and may be found at the end of the format description. */ + private void ProcessBlockID33(byte[] data) + { + // currently not implemented properly in ZXHawk - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x33; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Hardware_Type; + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x33; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Hardware_Type; - t.PauseInMS = 0; + t.PauseInMS = 0; - // first byte contains number of HWINFOs - int infos = data[_position]; + // first byte contains number of HWINFOs + int infos = data[_position]; - _position += 1; + _position += 1; - // now starts the HW infos (each block 3 bytes) - for (int i = 0; i < infos; i++) - { - _position += 3; - } + // now starts the HW infos (each block 3 bytes) + for (int i = 0; i < infos; i++) + { + _position += 3; + } - // add to tape - _datacorder.DataBlocks.Add(t); - } - #endregion + // add to tape + _datacorder.DataBlocks.Add(t); + } + #endregion - #region ID 35 - Custom info block -/* length: [10,11,12,13]+14 - Offset Value Type Description - 0x00 - CHAR[10] Identification string (in ASCII) - 0x10 L DWORD Length of the custom info - 0x14 - BYTE[L] Custom info + #region ID 35 - Custom info block + /* length: [10,11,12,13]+14 + Offset Value Type Description + 0x00 - CHAR[10] Identification string (in ASCII) + 0x10 L DWORD Length of the custom info + 0x14 - BYTE[L] Custom info - This block can be used to save any information you want. For example, it might contain some information written by a utility, - extra settings required by a particular emulator, or even poke data. */ - private void ProcessBlockID35(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x35; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Custom_Info_Block; + This block can be used to save any information you want. For example, it might contain some information written by a utility, + extra settings required by a particular emulator, or even poke data. */ + private void ProcessBlockID35(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x35; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Custom_Info_Block; - t.PauseInMS = 0; + t.PauseInMS = 0; - string info = Encoding.ASCII.GetString(data, _position, 0x10); - //t.BlockDescription = "[CUSTOM INFO: " + info + "]"; - _position += 0x10; + string info = Encoding.ASCII.GetString(data, _position, 0x10); + //t.BlockDescription = "[CUSTOM INFO: " + info + "]"; + _position += 0x10; - int blockLen = BitConverter.ToInt32(data, _position); - _position += 4; + int blockLen = BitConverter.ToInt32(data, _position); + _position += 4; - // add to tape - _datacorder.DataBlocks.Add(t); + // add to tape + _datacorder.DataBlocks.Add(t); - // advance to next block - _position += blockLen; - } - #endregion + // advance to next block + _position += blockLen; + } + #endregion - #region ID 5A - "Glue" block -/* length: 09 - Offset Value Type Description - 0x00 - BYTE[9] Value: { "XTape!",0x1A,MajR,MinR } - Just skip these 9 bytes and you will end up on the next ID. + #region ID 5A - "Glue" block + /* length: 09 + Offset Value Type Description + 0x00 - BYTE[9] Value: { "XTape!",0x1A,MajR,MinR } + Just skip these 9 bytes and you will end up on the next ID. - This block is generated when you merge two ZX Tape files together. It is here so that you can easily copy the files together and use - them. Of course, this means that resulting file would be 10 bytes longer than if this block was not used. All you have to do - if you encounter this block ID is to skip next 9 bytes. - If you can avoid using this block for this purpose, then do so; it is preferable to use a utility to join the two files and - ensure that they are both of the higher version number. */ - private void ProcessBlockID5A(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x5A; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Glue_Block; + This block is generated when you merge two ZX Tape files together. It is here so that you can easily copy the files together and use + them. Of course, this means that resulting file would be 10 bytes longer than if this block was not used. All you have to do + if you encounter this block ID is to skip next 9 bytes. + If you can avoid using this block for this purpose, then do so; it is preferable to use a utility to join the two files and + ensure that they are both of the higher version number. */ + private void ProcessBlockID5A(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x5A; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Glue_Block; - t.PauseInMS = 0; + t.PauseInMS = 0; - // add to tape - _datacorder.DataBlocks.Add(t); + // add to tape + _datacorder.DataBlocks.Add(t); - // advance to next block - _position += 9; - } - #endregion + // advance to next block + _position += 9; + } + #endregion - #region UnDetected Blocks + #region UnDetected Blocks - private void ProcessUnidentifiedBlock(byte[] data) - { - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = -2; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Unsupported; - //t.BlockDescription = "[UNSUPPORTED - 0x" + data[_position - 1] + "]"; + private void ProcessUnidentifiedBlock(byte[] data) + { + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = -2; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Unsupported; + //t.BlockDescription = "[UNSUPPORTED - 0x" + data[_position - 1] + "]"; - _position += GetInt32(data, _position) & 0xFFFFFF; + _position += GetInt32(data, _position) & 0xFFFFFF; - // add to tape - _datacorder.DataBlocks.Add(t); + // add to tape + _datacorder.DataBlocks.Add(t); - // advance to next block - _position += 4; - } + // advance to next block + _position += 4; + } - #endregion + #endregion - #region Depreciated Blocks + #region Depreciated Blocks - // These mostly should be ignored by ZXHawk - here for completeness + // These mostly should be ignored by ZXHawk - here for completeness - #region ID 16 - C64 ROM Type Data Block - private void ProcessBlockID16(byte[] data) - { - // zxhawk will not implement this block. it will however handle it so subsequent blocks can be parsed - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x16; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.C64_ROM_Type_Data_Block; + #region ID 16 - C64 ROM Type Data Block + private void ProcessBlockID16(byte[] data) + { + // zxhawk will not implement this block. it will however handle it so subsequent blocks can be parsed + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x16; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.C64_ROM_Type_Data_Block; - t.PauseInMS = 0; + t.PauseInMS = 0; - // add to tape - _datacorder.DataBlocks.Add(t); + // add to tape + _datacorder.DataBlocks.Add(t); - // advance to next block - int blockLen = GetInt32(data, _position); - _position += blockLen; - } - #endregion + // advance to next block + int blockLen = GetInt32(data, _position); + _position += blockLen; + } + #endregion - #region ID 17 - C64 Turbo Tape Data Block - private void ProcessBlockID17(byte[] data) - { - // zxhawk will not implement this block. it will however handle it so subsequent blocks can be parsed - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x17; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.C64_Turbo_Tape_Data_Block; + #region ID 17 - C64 Turbo Tape Data Block + private void ProcessBlockID17(byte[] data) + { + // zxhawk will not implement this block. it will however handle it so subsequent blocks can be parsed + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x17; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.C64_Turbo_Tape_Data_Block; - t.PauseInMS = 0; + t.PauseInMS = 0; - // add to tape - _datacorder.DataBlocks.Add(t); + // add to tape + _datacorder.DataBlocks.Add(t); - // advance to next block - int blockLen = GetInt32(data, _position); - _position += blockLen; - } - #endregion + // advance to next block + int blockLen = GetInt32(data, _position); + _position += blockLen; + } + #endregion - #region ID 34 - Emulation info - private void ProcessBlockID34(byte[] data) - { - // currently not implemented properly in ZXHawk + #region ID 34 - Emulation info + private void ProcessBlockID34(byte[] data) + { + // currently not implemented properly in ZXHawk - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x34; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Emulation_Info; + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x34; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Emulation_Info; - // add to tape - _datacorder.DataBlocks.Add(t); + // add to tape + _datacorder.DataBlocks.Add(t); - // advance to next block - _position += 8; - } - #endregion + // advance to next block + _position += 8; + } + #endregion - #region ID 40 - Snapshot block - /* length: [01,02,03]+04 + #region ID 40 - Snapshot block + /* length: [01,02,03]+04 Offset Value Type Description 0x00 - BYTE Snapshot type: 00: .Z80 format @@ -1577,120 +1577,120 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum Only .Z80 and .SNA snapshots are supported for compatibility reasons! The emulator should take care of that the snapshot is not taken while the actual Tape loading is taking place (which doesn't do much sense). And when an emulator encounters the snapshot block it should load it and then continue with the next block. */ - private void ProcessBlockID40(byte[] data) - { - // currently not implemented properly in ZXHawk + private void ProcessBlockID40(byte[] data) + { + // currently not implemented properly in ZXHawk - TapeDataBlock t = new TapeDataBlock(); - t.BlockID = 0x40; - t.DataPeriods = new List(); - t.BlockDescription = BlockType.Snapshot_Block; + TapeDataBlock t = new TapeDataBlock(); + t.BlockID = 0x40; + t.DataPeriods = new List(); + t.BlockDescription = BlockType.Snapshot_Block; - _position++; + _position++; - int blockLen = data[_position] | - data[_position + 1] << 8 | - data[_position + 2] << 16; - _position += 3; + int blockLen = data[_position] | + data[_position + 1] << 8 | + data[_position + 2] << 16; + _position += 3; - // add to tape - _datacorder.DataBlocks.Add(t); + // add to tape + _datacorder.DataBlocks.Add(t); - // advance to next block - _position += blockLen; - } - #endregion + // advance to next block + _position += blockLen; + } + #endregion - #endregion + #endregion - #endregion + #endregion - #region DataBlockDecoder + #region DataBlockDecoder - /// - /// Used to process either a standard or turbo data block - /// - private TapeDataBlock DecodeDataBlock - ( - TapeDataBlock block, - byte[] blockdata, - DataBlockType dataBlockType, - int pauseAfterBlock, - int pilotCount, + /// + /// Used to process either a standard or turbo data block + /// + private TapeDataBlock DecodeDataBlock + ( + TapeDataBlock block, + byte[] blockdata, + DataBlockType dataBlockType, + int pauseAfterBlock, + int pilotCount, - int pilotToneLength = 2168, - int sync1PulseLength = 667, - int sync2PulseLength = 735, - int bit0PulseLength = 855, - int bit1PulseLength = 1710, - int bitsInLastByte = 8 - ) - { - // first get the block description - string description = string.Empty; + int pilotToneLength = 2168, + int sync1PulseLength = 667, + int sync2PulseLength = 735, + int bit0PulseLength = 855, + int bit1PulseLength = 1710, + int bitsInLastByte = 8 + ) + { + // first get the block description + string description = string.Empty; - // process the type byte - /* (The type is 0,1,2 or 3 for a Program, Number array, Character array or Code file. + // process the type byte + /* (The type is 0,1,2 or 3 for a Program, Number array, Character array or Code file. A SCREEN$ file is regarded as a Code file with start address 16384 and length 6912 decimal. If the file is a Program file, parameter 1 holds the autostart line number (or a number >=32768 if no LINE parameter was given) and parameter 2 holds the start of the variable area relative to the start of the program. If it's a Code file, parameter 1 holds the start of the code block when saved, and parameter 2 holds 32768. For data files finally, the byte at position 14 decimal holds the variable name.) */ - int blockSize = blockdata.Length; + int blockSize = blockdata.Length; - // dont get description info for Pure Data Blocks - if (dataBlockType != DataBlockType.Pure) - { - if (blockdata[0] == 0x00 && blockSize == 19) - { - string fileName = Encoding.ASCII.GetString(blockdata.Skip(2).Take(10).ToArray()).Trim(); - string type = "Unknown Type"; - StringBuilder sb = new StringBuilder(); + // dont get description info for Pure Data Blocks + if (dataBlockType != DataBlockType.Pure) + { + if (blockdata[0] == 0x00 && blockSize == 19) + { + string fileName = Encoding.ASCII.GetString(blockdata.Skip(2).Take(10).ToArray()).Trim(); + string type = "Unknown Type"; + StringBuilder sb = new StringBuilder(); - var param1 = GetWordValue(blockdata, 12); - var param2 = GetWordValue(blockdata, 14); + var param1 = GetWordValue(blockdata, 12); + var param2 = GetWordValue(blockdata, 14); - // header block - examine first byte of header - if (blockdata[1] == 0) - { - type = "Program"; - sb.Append(type + ": "); - sb.Append(fileName + " "); - } - else if (blockdata[1] == 1) - { - type = "NumArray"; - sb.Append(type + ": "); - sb.Append(fileName + " "); - } - else if (blockdata[1] == 2) - { - type = "CharArray"; - sb.Append(type + ": "); - sb.Append(fileName + " "); - } - else if (blockdata[1] == 3) - { - type = "Code"; - sb.Append(type + ": "); - sb.Append(fileName + " "); - } - } - else if (blockdata[0] == 0xff) - { - // data block - description = "Data Block " + (blockSize - 2) + "bytes"; - block.AddMetaData(BlockDescriptorTitle.Data_Bytes, (blockSize - 2).ToString() + " Bytes"); - } - else - { - // some other type (turbo data etc..) - description = $"#{blockdata[0].ToString("X2")} block, {blockSize} bytes"; - //description += (crc != 0) ? $", crc bad (#{crcFile:X2}!=#{crcValue:X2})" : ", crc ok"; - block.AddMetaData(BlockDescriptorTitle.Undefined, description); - } - /* + // header block - examine first byte of header + if (blockdata[1] == 0) + { + type = "Program"; + sb.Append(type + ": "); + sb.Append(fileName + " "); + } + else if (blockdata[1] == 1) + { + type = "NumArray"; + sb.Append(type + ": "); + sb.Append(fileName + " "); + } + else if (blockdata[1] == 2) + { + type = "CharArray"; + sb.Append(type + ": "); + sb.Append(fileName + " "); + } + else if (blockdata[1] == 3) + { + type = "Code"; + sb.Append(type + ": "); + sb.Append(fileName + " "); + } + } + else if (blockdata[0] == 0xff) + { + // data block + description = "Data Block " + (blockSize - 2) + "bytes"; + block.AddMetaData(BlockDescriptorTitle.Data_Bytes, (blockSize - 2).ToString() + " Bytes"); + } + else + { + // some other type (turbo data etc..) + description = $"#{blockdata[0].ToString("X2")} block, {blockSize} bytes"; + //description += (crc != 0) ? $", crc bad (#{crcFile:X2}!=#{crcValue:X2})" : ", crc ok"; + block.AddMetaData(BlockDescriptorTitle.Undefined, description); + } + /* if (blockdata[0] == 0x00 && blockSize == 19 && (blockdata[1] == 0x00) || (blockdata[1] == 3 && blockdata.Length > 3)) { if (dataBlockType != DataBlockType.Turbo) @@ -1733,192 +1733,192 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum //description += (crc != 0) ? $", crc bad (#{crcFile:X2}!=#{crcValue:X2})" : ", crc ok"; block.AddMetaData(BlockDescriptorTitle.Undefined, description); } - */ - } + */ + } - // update metadata - switch (dataBlockType) - { - case DataBlockType.Standard: - case DataBlockType.Turbo: + // update metadata + switch (dataBlockType) + { + case DataBlockType.Standard: + case DataBlockType.Turbo: - if (dataBlockType == DataBlockType.Standard) - block.BlockDescription = BlockType.Standard_Speed_Data_Block; - if (dataBlockType == DataBlockType.Turbo) - block.BlockDescription = BlockType.Turbo_Speed_Data_Block; + if (dataBlockType == DataBlockType.Standard) + block.BlockDescription = BlockType.Standard_Speed_Data_Block; + if (dataBlockType == DataBlockType.Turbo) + block.BlockDescription = BlockType.Turbo_Speed_Data_Block; - block.AddMetaData(BlockDescriptorTitle.Pilot_Pulse_Length, pilotToneLength.ToString() + " T-States"); - block.AddMetaData(BlockDescriptorTitle.Pilot_Pulse_Count, pilotCount.ToString() + " Pulses"); - block.AddMetaData(BlockDescriptorTitle.First_Sync_Length, sync1PulseLength.ToString() + " T-States"); - block.AddMetaData(BlockDescriptorTitle.Second_Sync_Length, sync2PulseLength.ToString() + " T-States"); - break; + block.AddMetaData(BlockDescriptorTitle.Pilot_Pulse_Length, pilotToneLength.ToString() + " T-States"); + block.AddMetaData(BlockDescriptorTitle.Pilot_Pulse_Count, pilotCount.ToString() + " Pulses"); + block.AddMetaData(BlockDescriptorTitle.First_Sync_Length, sync1PulseLength.ToString() + " T-States"); + block.AddMetaData(BlockDescriptorTitle.Second_Sync_Length, sync2PulseLength.ToString() + " T-States"); + break; - case DataBlockType.Pure: - block.BlockDescription = BlockType.Pure_Data_Block; - break; - } + case DataBlockType.Pure: + block.BlockDescription = BlockType.Pure_Data_Block; + break; + } - block.AddMetaData(BlockDescriptorTitle.Zero_Bit_Length, bit0PulseLength.ToString() + " T-States"); - block.AddMetaData(BlockDescriptorTitle.One_Bit_Length, bit1PulseLength.ToString() + " T-States"); - block.AddMetaData(BlockDescriptorTitle.Data_Length, blockSize.ToString() + " Bytes"); - block.AddMetaData(BlockDescriptorTitle.Bits_In_Last_Byte, bitsInLastByte.ToString() + " Bits"); - block.AddMetaData(BlockDescriptorTitle.Pause_After_Data, pauseAfterBlock.ToString() + " ms"); + block.AddMetaData(BlockDescriptorTitle.Zero_Bit_Length, bit0PulseLength.ToString() + " T-States"); + block.AddMetaData(BlockDescriptorTitle.One_Bit_Length, bit1PulseLength.ToString() + " T-States"); + block.AddMetaData(BlockDescriptorTitle.Data_Length, blockSize.ToString() + " Bytes"); + block.AddMetaData(BlockDescriptorTitle.Bits_In_Last_Byte, bitsInLastByte.ToString() + " Bits"); + block.AddMetaData(BlockDescriptorTitle.Pause_After_Data, pauseAfterBlock.ToString() + " ms"); - // calculate period information - List dataPeriods = new List(); + // calculate period information + List dataPeriods = new List(); - // generate pilot pulses + // generate pilot pulses - if (pilotCount > 0) - { - for (int i = 0; i < pilotCount; i++) - { - dataPeriods.Add(pilotToneLength); - } + if (pilotCount > 0) + { + for (int i = 0; i < pilotCount; i++) + { + dataPeriods.Add(pilotToneLength); + } - // add syncro pulses - dataPeriods.Add(sync1PulseLength); - dataPeriods.Add(sync2PulseLength); - } + // add syncro pulses + dataPeriods.Add(sync1PulseLength); + dataPeriods.Add(sync2PulseLength); + } - int pos = 0; + int pos = 0; - // add bit0 and bit1 periods - for (int i = 0; i < blockSize - 1; i++, pos++) - { - for (byte b = 0x80; b != 0; b >>= 1) - { - if ((blockdata[i] & b) != 0) - dataPeriods.Add(bit1PulseLength); - else - dataPeriods.Add(bit0PulseLength); - if ((blockdata[i] & b) != 0) - dataPeriods.Add(bit1PulseLength); - else - dataPeriods.Add(bit0PulseLength); - } - } + // add bit0 and bit1 periods + for (int i = 0; i < blockSize - 1; i++, pos++) + { + for (byte b = 0x80; b != 0; b >>= 1) + { + if ((blockdata[i] & b) != 0) + dataPeriods.Add(bit1PulseLength); + else + dataPeriods.Add(bit0PulseLength); + if ((blockdata[i] & b) != 0) + dataPeriods.Add(bit1PulseLength); + else + dataPeriods.Add(bit0PulseLength); + } + } - // add the last byte - for (byte c = 0x80; c != (byte)(0x80 >> bitsInLastByte); c >>= 1) - { - if ((blockdata[pos] & c) != 0) - dataPeriods.Add(bit1PulseLength); - else - dataPeriods.Add(bit0PulseLength); - if ((blockdata[pos] & c) != 0) - dataPeriods.Add(bit1PulseLength); - else - dataPeriods.Add(bit0PulseLength); - } + // add the last byte + for (byte c = 0x80; c != (byte)(0x80 >> bitsInLastByte); c >>= 1) + { + if ((blockdata[pos] & c) != 0) + dataPeriods.Add(bit1PulseLength); + else + dataPeriods.Add(bit0PulseLength); + if ((blockdata[pos] & c) != 0) + dataPeriods.Add(bit1PulseLength); + else + dataPeriods.Add(bit0PulseLength); + } - // add block pause if pause is not 0 - if (pauseAfterBlock != 0) - { - block.PauseInMS = pauseAfterBlock; - //int actualPause = pauseAfterBlock * 3500; - //dataPeriods.Add(actualPause); - } + // add block pause if pause is not 0 + if (pauseAfterBlock != 0) + { + block.PauseInMS = pauseAfterBlock; + //int actualPause = pauseAfterBlock * 3500; + //dataPeriods.Add(actualPause); + } - // add to the tapedatablock object - block.DataPeriods = dataPeriods; + // add to the tapedatablock object + block.DataPeriods = dataPeriods; - // add the raw data - block.BlockData = blockdata; + // add the raw data + block.BlockData = blockdata; - return block; - } + return block; + } - /// - /// Used to process either a standard or turbo data block - /// - private TapeDataBlock DecodeDataBlock - ( - TapeDataBlock block, - byte[] blockData, - DataBlockType dataBlockType, - int pauseAfterBlock, + /// + /// Used to process either a standard or turbo data block + /// + private TapeDataBlock DecodeDataBlock + ( + TapeDataBlock block, + byte[] blockData, + DataBlockType dataBlockType, + int pauseAfterBlock, - int pilotToneLength = 2168, - int sync1PulseLength = 667, - int sync2PulseLength = 735, - int bit0PulseLength = 855, - int bit1PulseLength = 1710, - int bitsInLastByte = 8 - ) - { + int pilotToneLength = 2168, + int sync1PulseLength = 667, + int sync2PulseLength = 735, + int bit0PulseLength = 855, + int bit1PulseLength = 1710, + int bitsInLastByte = 8 + ) + { - // pilot count needs to be ascertained from flag byte - int pilotCount; - if (blockData[0] < 128) - pilotCount = 8063; - else - pilotCount = 3223; + // pilot count needs to be ascertained from flag byte + int pilotCount; + if (blockData[0] < 128) + pilotCount = 8063; + else + pilotCount = 3223; - // now we can decode - var nBlock = DecodeDataBlock - ( - block, - blockData, - dataBlockType, - pauseAfterBlock, - pilotCount, - pilotToneLength, - sync1PulseLength, - sync2PulseLength, - bit0PulseLength, - bit1PulseLength, - bitsInLastByte - ); + // now we can decode + var nBlock = DecodeDataBlock + ( + block, + blockData, + dataBlockType, + pauseAfterBlock, + pilotCount, + pilotToneLength, + sync1PulseLength, + sync2PulseLength, + bit0PulseLength, + bit1PulseLength, + bitsInLastByte + ); - return nBlock; - } + return nBlock; + } - #endregion + #endregion - #region Pause Block Creator + #region Pause Block Creator - /// - /// If neccessary a seperate PAUSE block will be created - /// - private void CreatePauseBlock(TapeDataBlock original) - { - if (original.PauseInMS > 0) - { - TapeDataBlock pBlock = new TapeDataBlock(); - pBlock.DataPeriods = new List(); - pBlock.BlockDescription = BlockType.PAUSE_BLOCK; - pBlock.PauseInMS = 0; - var pauseInTStates = TranslatePause(original.PauseInMS); + /// + /// If neccessary a seperate PAUSE block will be created + /// + private void CreatePauseBlock(TapeDataBlock original) + { + if (original.PauseInMS > 0) + { + TapeDataBlock pBlock = new TapeDataBlock(); + pBlock.DataPeriods = new List(); + pBlock.BlockDescription = BlockType.PAUSE_BLOCK; + pBlock.PauseInMS = 0; + var pauseInTStates = TranslatePause(original.PauseInMS); - pBlock.AddMetaData(BlockDescriptorTitle.Block_ID, pauseInTStates.ToString() + " cycles"); + pBlock.AddMetaData(BlockDescriptorTitle.Block_ID, pauseInTStates.ToString() + " cycles"); - int by1000 = pauseInTStates / 70000; - int rem1000 = pauseInTStates % 70000; + int by1000 = pauseInTStates / 70000; + int rem1000 = pauseInTStates % 70000; - if (by1000 > 1) - { - pBlock.DataPeriods.Add(35000); - pBlock.DataPeriods.Add(pauseInTStates - 35000); - } - else - { - pBlock.DataPeriods.Add(pauseInTStates); - pBlock.DataPeriods.Add(0); - } + if (by1000 > 1) + { + pBlock.DataPeriods.Add(35000); + pBlock.DataPeriods.Add(pauseInTStates - 35000); + } + else + { + pBlock.DataPeriods.Add(pauseInTStates); + pBlock.DataPeriods.Add(0); + } - _datacorder.DataBlocks.Add(pBlock); - } - } + _datacorder.DataBlocks.Add(pBlock); + } + } - #endregion - } + #endregion + } - public enum DataBlockType - { - Standard, - Turbo, - Pure - } + public enum DataBlockType + { + Standard, + Turbo, + Pure + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/TapeCommand.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/TapeCommand.cs index f61fc49e0f..28383dd838 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/TapeCommand.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/TapeCommand.cs @@ -1,16 +1,16 @@  namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Represents the possible commands that can be raised from each tape block - /// - public enum TapeCommand - { - NONE, - STOP_THE_TAPE, - STOP_THE_TAPE_48K, - BEGIN_GROUP, - END_GROUP, - SHOW_MESSAGE, - } + /// + /// Represents the possible commands that can be raised from each tape block + /// + public enum TapeCommand + { + NONE, + STOP_THE_TAPE, + STOP_THE_TAPE_48K, + BEGIN_GROUP, + END_GROUP, + SHOW_MESSAGE, + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/TapeDataBlock.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/TapeDataBlock.cs index 489f425b6e..a810af6201 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/TapeDataBlock.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/TapeDataBlock.cs @@ -5,51 +5,53 @@ using System.Text; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Represents a tape block - /// - public class TapeDataBlock - { - /// - /// Either the TZX block ID, or -1 in the case of non-tzx blocks - /// - private int _blockID = -1; - public int BlockID - { - get { return _blockID; } - set { - _blockID = value; + /// + /// Represents a tape block + /// + public class TapeDataBlock + { + /// + /// Either the TZX block ID, or -1 in the case of non-tzx blocks + /// + private int _blockID = -1; + public int BlockID + { + get { return _blockID; } + set + { + _blockID = value; - if (MetaData == null) - MetaData = new Dictionary(); + if (MetaData == null) + MetaData = new Dictionary(); - AddMetaData(BlockDescriptorTitle.Block_ID, value.ToString()); - } - } + AddMetaData(BlockDescriptorTitle.Block_ID, value.ToString()); + } + } - /// - /// The block type - /// - private BlockType _blockType; - public BlockType BlockDescription - { - get { return _blockType; } - set { - _blockType = value; - if (MetaData == null) - MetaData = new Dictionary(); - } - } + /// + /// The block type + /// + private BlockType _blockType; + public BlockType BlockDescription + { + get { return _blockType; } + set + { + _blockType = value; + if (MetaData == null) + MetaData = new Dictionary(); + } + } - /// - /// Byte array containing the raw block data - /// - private byte[] _blockData; - public byte[] BlockData - { - get { return _blockData; } - set { _blockData = value; } - } + /// + /// Byte array containing the raw block data + /// + private byte[] _blockData; + public byte[] BlockData + { + get { return _blockData; } + set { _blockData = value; } + } /* @@ -79,203 +81,203 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum */ - #region Block Meta Data + #region Block Meta Data - /// - /// Dictionary of block related data - /// - public Dictionary MetaData { get; set; } + /// + /// Dictionary of block related data + /// + public Dictionary MetaData { get; set; } - /// - /// Adds a single metadata item to the Dictionary - /// - public void AddMetaData(BlockDescriptorTitle descriptor, string data) - { - // check whether entry already exists - bool check = MetaData.ContainsKey(descriptor); - if (check) - { - // already exists - update - MetaData[descriptor] = data; - } - else - { - // create new - MetaData.Add(descriptor, data); - } - } + /// + /// Adds a single metadata item to the Dictionary + /// + public void AddMetaData(BlockDescriptorTitle descriptor, string data) + { + // check whether entry already exists + bool check = MetaData.ContainsKey(descriptor); + if (check) + { + // already exists - update + MetaData[descriptor] = data; + } + else + { + // create new + MetaData.Add(descriptor, data); + } + } - #endregion + #endregion - /// - /// List containing the pulse timing values - /// - public List DataPeriods = new List(); + /// + /// List containing the pulse timing values + /// + public List DataPeriods = new List(); - public bool InitialPulseLevel; + public bool InitialPulseLevel; - /// - /// Command that is raised by this data block - /// (that may or may not need to be acted on) - /// - private TapeCommand _command = TapeCommand.NONE; - public TapeCommand Command - { - get { return _command; } - set { _command = value; } - } + /// + /// Command that is raised by this data block + /// (that may or may not need to be acted on) + /// + private TapeCommand _command = TapeCommand.NONE; + public TapeCommand Command + { + get { return _command; } + set { _command = value; } + } - /// - /// The defined post-block pause - /// - private int _pauseInMS; - public int PauseInMS - { - get { return _pauseInMS; } - set { _pauseInMS = value; } - } + /// + /// The defined post-block pause + /// + private int _pauseInMS; + public int PauseInMS + { + get { return _pauseInMS; } + set { _pauseInMS = value; } + } - /// - /// Returns the data periods as an array - /// (primarily to aid in bizhawk state serialization) - /// - public int[] GetDataPeriodsArray() - { - return DataPeriods.ToArray(); - } + /// + /// Returns the data periods as an array + /// (primarily to aid in bizhawk state serialization) + /// + public int[] GetDataPeriodsArray() + { + return DataPeriods.ToArray(); + } - /// - /// Accepts an array of data periods and updates the DataPeriods list accordingly - /// (primarily to aid in bizhawk state serialization) - /// - public void SetDataPeriodsArray(int[] periodArray) - { - DataPeriods = periodArray?.ToList() ?? new List(); - } + /// + /// Accepts an array of data periods and updates the DataPeriods list accordingly + /// (primarily to aid in bizhawk state serialization) + /// + public void SetDataPeriodsArray(int[] periodArray) + { + DataPeriods = periodArray?.ToList() ?? new List(); + } - /// - /// Bizhawk state serialization - /// - public void SyncState(Serializer ser, int blockPosition) - { - ser.BeginSection("DataBlock" + blockPosition); + /// + /// Bizhawk state serialization + /// + public void SyncState(Serializer ser, int blockPosition) + { + ser.BeginSection("DataBlock" + blockPosition); - ser.Sync(nameof(_blockID), ref _blockID); - //ser.SyncFixedString(nameof(_blockDescription), ref _blockDescription, 200); - ser.SyncEnum(nameof(_blockType), ref _blockType); - ser.Sync(nameof(_blockData), ref _blockData, true); - ser.SyncEnum(nameof(_command), ref _command); + ser.Sync(nameof(_blockID), ref _blockID); + //ser.SyncFixedString(nameof(_blockDescription), ref _blockDescription, 200); + ser.SyncEnum(nameof(_blockType), ref _blockType); + ser.Sync(nameof(_blockData), ref _blockData, true); + ser.SyncEnum(nameof(_command), ref _command); - int[] tempArray = null; + int[] tempArray = null; - if (ser.IsWriter) - { - tempArray = GetDataPeriodsArray(); - ser.Sync("_periods", ref tempArray, true); - } - else - { - ser.Sync("_periods", ref tempArray, true); - SetDataPeriodsArray(tempArray); - } + if (ser.IsWriter) + { + tempArray = GetDataPeriodsArray(); + ser.Sync("_periods", ref tempArray, true); + } + else + { + ser.Sync("_periods", ref tempArray, true); + SetDataPeriodsArray(tempArray); + } - ser.EndSection(); - } - } + ser.EndSection(); + } + } - /// - /// The types of TZX blocks - /// - public enum BlockType - { - Standard_Speed_Data_Block = 0x10, - Turbo_Speed_Data_Block = 0x11, - Pure_Tone = 0x12, - Pulse_Sequence = 0x13, - Pure_Data_Block = 0x14, - Direct_Recording = 0x15, - CSW_Recording = 0x18, - Generalized_Data_Block = 0x19, - Pause_or_Stop_the_Tape = 0x20, - Group_Start = 0x21, - Group_End = 0x22, - Jump_to_Block = 0x23, - Loop_Start = 0x24, - Loop_End = 0x25, - Call_Sequence = 0x26, - Return_From_Sequence = 0x27, - Select_Block = 0x28, - Stop_the_Tape_48K = 0x2A, - Set_Signal_Level = 0x2B, - Text_Description = 0x30, - Message_Block = 0x31, - Archive_Info = 0x32, - Hardware_Type = 0x33, - Custom_Info_Block = 0x35, - Glue_Block = 0x5A, + /// + /// The types of TZX blocks + /// + public enum BlockType + { + Standard_Speed_Data_Block = 0x10, + Turbo_Speed_Data_Block = 0x11, + Pure_Tone = 0x12, + Pulse_Sequence = 0x13, + Pure_Data_Block = 0x14, + Direct_Recording = 0x15, + CSW_Recording = 0x18, + Generalized_Data_Block = 0x19, + Pause_or_Stop_the_Tape = 0x20, + Group_Start = 0x21, + Group_End = 0x22, + Jump_to_Block = 0x23, + Loop_Start = 0x24, + Loop_End = 0x25, + Call_Sequence = 0x26, + Return_From_Sequence = 0x27, + Select_Block = 0x28, + Stop_the_Tape_48K = 0x2A, + Set_Signal_Level = 0x2B, + Text_Description = 0x30, + Message_Block = 0x31, + Archive_Info = 0x32, + Hardware_Type = 0x33, + Custom_Info_Block = 0x35, + Glue_Block = 0x5A, - // depreciated blocks - C64_ROM_Type_Data_Block = 0x16, - C64_Turbo_Tape_Data_Block = 0x17, - Emulation_Info = 0x34, - Snapshot_Block = 0x40, + // depreciated blocks + C64_ROM_Type_Data_Block = 0x16, + C64_Turbo_Tape_Data_Block = 0x17, + Emulation_Info = 0x34, + Snapshot_Block = 0x40, - // unsupported / undetected - Unsupported, + // unsupported / undetected + Unsupported, - // PZX blocks - PZXT, - PULS, - DATA, - BRWS, - PAUS, + // PZX blocks + PZXT, + PULS, + DATA, + BRWS, + PAUS, - // zxhawk proprietry - PAUSE_BLOCK, + // zxhawk proprietry + PAUSE_BLOCK, - WAV_Recording - } - + WAV_Recording + } - /// - /// Different title possibilities - /// - public enum BlockDescriptorTitle - { - Undefined, - Block_ID, - Program, - Data_Bytes, - Bytes, - Pilot_Pulse_Length, - Pilot_Pulse_Count, - First_Sync_Length, - Second_Sync_Length, - Zero_Bit_Length, - One_Bit_Length, - Data_Length, - Bits_In_Last_Byte, - Pause_After_Data, + /// + /// Different title possibilities + /// + public enum BlockDescriptorTitle + { + Undefined, + Block_ID, + Program, + Data_Bytes, + Bytes, - Pulse_Length, - Pulse_Count, + Pilot_Pulse_Length, + Pilot_Pulse_Count, + First_Sync_Length, + Second_Sync_Length, + Zero_Bit_Length, + One_Bit_Length, + Data_Length, + Bits_In_Last_Byte, + Pause_After_Data, - Text_Description, - Title, - Publisher, - Author, - Year, - Language, - Type, - Price, - Protection, - Origin, - Comments, + Pulse_Length, + Pulse_Count, - Needs_Parsing - } + Text_Description, + Title, + Publisher, + Author, + Year, + Language, + Type, + Price, + Protection, + Origin, + Comments, + + Needs_Parsing + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/WAV/StreamHelper.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/WAV/StreamHelper.cs index 5c24a66b61..cd4806b90d 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/WAV/StreamHelper.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/WAV/StreamHelper.cs @@ -3,102 +3,102 @@ using System.IO; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// From https://archive.codeplex.com/?p=zxmak2 - /// - public static class StreamHelper - { - public static void Write(Stream stream, Int32 value) - { - byte[] data = BitConverter.GetBytes(value); - stream.Write(data, 0, data.Length); - } + /// + /// From https://archive.codeplex.com/?p=zxmak2 + /// + public static class StreamHelper + { + public static void Write(Stream stream, Int32 value) + { + byte[] data = BitConverter.GetBytes(value); + stream.Write(data, 0, data.Length); + } - public static void Write(Stream stream, UInt32 value) - { - byte[] data = BitConverter.GetBytes(value); - stream.Write(data, 0, data.Length); - } + public static void Write(Stream stream, UInt32 value) + { + byte[] data = BitConverter.GetBytes(value); + stream.Write(data, 0, data.Length); + } - public static void Write(Stream stream, Int16 value) - { - byte[] data = BitConverter.GetBytes(value); - stream.Write(data, 0, data.Length); - } + public static void Write(Stream stream, Int16 value) + { + byte[] data = BitConverter.GetBytes(value); + stream.Write(data, 0, data.Length); + } - public static void Write(Stream stream, UInt16 value) - { - byte[] data = BitConverter.GetBytes(value); - stream.Write(data, 0, data.Length); - } + public static void Write(Stream stream, UInt16 value) + { + byte[] data = BitConverter.GetBytes(value); + stream.Write(data, 0, data.Length); + } - public static void Write(Stream stream, Byte value) - { - byte[] data = new byte[1]; - data[0] = value; - stream.Write(data, 0, data.Length); - } + public static void Write(Stream stream, Byte value) + { + byte[] data = new byte[1]; + data[0] = value; + stream.Write(data, 0, data.Length); + } - public static void Write(Stream stream, SByte value) - { - byte[] data = new byte[1]; - data[0] = (byte)value; - stream.Write(data, 0, data.Length); - } + public static void Write(Stream stream, SByte value) + { + byte[] data = new byte[1]; + data[0] = (byte)value; + stream.Write(data, 0, data.Length); + } - public static void Write(Stream stream, byte[] value) - { - stream.Write(value, 0, value.Length); - } + public static void Write(Stream stream, byte[] value) + { + stream.Write(value, 0, value.Length); + } - public static void Read(Stream stream, out Int32 value) - { - byte[] data = new byte[4]; - stream.Read(data, 0, data.Length); - //if (!BitConverter.IsLittleEndian) - // Array.Reverse(data); - value = BitConverter.ToInt32(data, 0); - } + public static void Read(Stream stream, out Int32 value) + { + byte[] data = new byte[4]; + stream.Read(data, 0, data.Length); + //if (!BitConverter.IsLittleEndian) + // Array.Reverse(data); + value = BitConverter.ToInt32(data, 0); + } - public static void Read(Stream stream, out UInt32 value) - { - byte[] data = new byte[4]; - stream.Read(data, 0, data.Length); - value = BitConverter.ToUInt32(data, 0); - } + public static void Read(Stream stream, out UInt32 value) + { + byte[] data = new byte[4]; + stream.Read(data, 0, data.Length); + value = BitConverter.ToUInt32(data, 0); + } - public static void Read(Stream stream, out Int16 value) - { - byte[] data = new byte[2]; - stream.Read(data, 0, data.Length); - value = BitConverter.ToInt16(data, 0); - } + public static void Read(Stream stream, out Int16 value) + { + byte[] data = new byte[2]; + stream.Read(data, 0, data.Length); + value = BitConverter.ToInt16(data, 0); + } - public static void Read(Stream stream, out UInt16 value) - { - byte[] data = new byte[2]; - stream.Read(data, 0, data.Length); - value = BitConverter.ToUInt16(data, 0); - } + public static void Read(Stream stream, out UInt16 value) + { + byte[] data = new byte[2]; + stream.Read(data, 0, data.Length); + value = BitConverter.ToUInt16(data, 0); + } - public static void Read(Stream stream, out Byte value) - { - byte[] data = new byte[1]; - stream.Read(data, 0, data.Length); - value = data[0]; - } + public static void Read(Stream stream, out Byte value) + { + byte[] data = new byte[1]; + stream.Read(data, 0, data.Length); + value = data[0]; + } - public static void Read(Stream stream, out SByte value) - { - byte[] data = new byte[1]; - stream.Read(data, 0, data.Length); - value = (sbyte)data[0]; - } + public static void Read(Stream stream, out SByte value) + { + byte[] data = new byte[1]; + stream.Read(data, 0, data.Length); + value = (sbyte)data[0]; + } - public static void Read(Stream stream, byte[] value) - { - stream.Read(value, 0, value.Length); - } - } + public static void Read(Stream stream, byte[] value) + { + stream.Read(value, 0, value.Length); + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/WAV/WavConverter.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/WAV/WavConverter.cs index 410155a2fa..c783ce59c5 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/WAV/WavConverter.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/WAV/WavConverter.cs @@ -6,123 +6,123 @@ using System.Text; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// Reponsible for WAV format conversion - /// Based heavily on code from zxmak2: https://archive.codeplex.com/?p=zxmak2 - /// - public class WavConverter : MediaConverter - { - /// - /// The type of serializer - /// - private MediaConverterType _formatType = MediaConverterType.WAV; - public override MediaConverterType FormatType - { - get - { - return _formatType; - } - } + /// + /// Reponsible for WAV format conversion + /// Based heavily on code from zxmak2: https://archive.codeplex.com/?p=zxmak2 + /// + public class WavConverter : MediaConverter + { + /// + /// The type of serializer + /// + private MediaConverterType _formatType = MediaConverterType.WAV; + public override MediaConverterType FormatType + { + get + { + return _formatType; + } + } - /// - /// Signs whether this class can be used to read the data format - /// - public override bool IsReader { get { return true; } } + /// + /// Signs whether this class can be used to read the data format + /// + public override bool IsReader { get { return true; } } - /// - /// Signs whether this class can be used to write the data format - /// - public override bool IsWriter { get { return false; } } + /// + /// Signs whether this class can be used to write the data format + /// + public override bool IsWriter { get { return false; } } - /// - /// Position counter - /// - //private int _position = 0; + /// + /// Position counter + /// + //private int _position = 0; - #region Construction + #region Construction - private DatacorderDevice _datacorder; + private DatacorderDevice _datacorder; - public WavConverter(DatacorderDevice _tapeDevice) - { - _datacorder = _tapeDevice; - } + public WavConverter(DatacorderDevice _tapeDevice) + { + _datacorder = _tapeDevice; + } - #endregion + #endregion - /// - /// Returns TRUE if pzx header is detected - /// - public override bool CheckType(byte[] data) - { - // WAV Header + /// + /// Returns TRUE if pzx header is detected + /// + public override bool CheckType(byte[] data) + { + // WAV Header - // check whether this is a valid wav format file by looking at the identifier in the header - string ident = Encoding.ASCII.GetString(data, 8, 4); + // check whether this is a valid wav format file by looking at the identifier in the header + string ident = Encoding.ASCII.GetString(data, 8, 4); - if (ident.ToUpper() != "WAVE") - { - // this is not a valid WAV format file - return false; - } - else - { - return true; - } - } + if (ident.ToUpper() != "WAVE") + { + // this is not a valid WAV format file + return false; + } + else + { + return true; + } + } - /// - /// DeSerialization method - /// - public override void Read(byte[] data) - { - // clear existing tape blocks - _datacorder.DataBlocks.Clear(); + /// + /// DeSerialization method + /// + public override void Read(byte[] data) + { + // clear existing tape blocks + _datacorder.DataBlocks.Clear(); - // check whether this is a valid pzx format file by looking at the identifier in the header block - string ident = Encoding.ASCII.GetString(data, 8, 4); + // check whether this is a valid pzx format file by looking at the identifier in the header block + string ident = Encoding.ASCII.GetString(data, 8, 4); - if (ident.ToUpper() != "WAVE") - { - // this is not a valid TZX format file - throw new Exception(this.GetType().ToString() + - "This is not a valid WAV format file"); - } + if (ident.ToUpper() != "WAVE") + { + // this is not a valid TZX format file + throw new Exception(this.GetType().ToString() + + "This is not a valid WAV format file"); + } - //_position = 0; + //_position = 0; - MemoryStream stream = new MemoryStream(); - stream.Write(data, 0, data.Length); - stream.Position = 0; + MemoryStream stream = new MemoryStream(); + stream.Write(data, 0, data.Length); + stream.Position = 0; - WavStreamReader reader = new WavStreamReader(stream); + WavStreamReader reader = new WavStreamReader(stream); - int rate = (69888 * 50) / reader.Header.sampleRate; - int smpCounter = 0; - int state = reader.ReadNext(); + int rate = (69888 * 50) / reader.Header.sampleRate; + int smpCounter = 0; + int state = reader.ReadNext(); - // create the single tape block - TapeDataBlock t = new TapeDataBlock(); - t.BlockDescription = BlockType.WAV_Recording; - t.BlockID = 0; - t.DataPeriods = new List(); + // create the single tape block + TapeDataBlock t = new TapeDataBlock(); + t.BlockDescription = BlockType.WAV_Recording; + t.BlockID = 0; + t.DataPeriods = new List(); - for (int i = 0; i < reader.Count; i++) - { - int sample = reader.ReadNext(); - smpCounter++; - if ((state < 0 && sample < 0) || (state >= 0 && sample >= 0)) - continue; - t.DataPeriods.Add(smpCounter * rate); - smpCounter = 0; - state = sample; - } + for (int i = 0; i < reader.Count; i++) + { + int sample = reader.ReadNext(); + smpCounter++; + if ((state < 0 && sample < 0) || (state >= 0 && sample >= 0)) + continue; + t.DataPeriods.Add(smpCounter * rate); + smpCounter = 0; + state = sample; + } - // add closing period - t.DataPeriods.Add((69888 * 50) / 10); + // add closing period + t.DataPeriods.Add((69888 * 50) / 10); - // add to datacorder - _datacorder.DataBlocks.Add(t); - } - } + // add to datacorder + _datacorder.DataBlocks.Add(t); + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/WAV/WavHeader.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/WAV/WavHeader.cs index 270ecf35ec..336d2ac64a 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/WAV/WavHeader.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/WAV/WavHeader.cs @@ -7,99 +7,99 @@ using System.Threading.Tasks; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// From https://archive.codeplex.com/?p=zxmak2 - /// - public class WavHeader - { - // RIFF chunk (12 bytes) - public Int32 chunkID; // "RIFF" - public Int32 fileSize; - public Int32 riffType; // "WAVE" + /// + /// From https://archive.codeplex.com/?p=zxmak2 + /// + public class WavHeader + { + // RIFF chunk (12 bytes) + public Int32 chunkID; // "RIFF" + public Int32 fileSize; + public Int32 riffType; // "WAVE" - // Format chunk (24 bytes) - public Int32 fmtID; // "fmt " - public Int32 fmtSize; - public Int16 fmtCode; - public Int16 channels; - public Int32 sampleRate; - public Int32 fmtAvgBPS; - public Int16 fmtBlockAlign; - public Int16 bitDepth; - public Int16 fmtExtraSize; + // Format chunk (24 bytes) + public Int32 fmtID; // "fmt " + public Int32 fmtSize; + public Int16 fmtCode; + public Int16 channels; + public Int32 sampleRate; + public Int32 fmtAvgBPS; + public Int16 fmtBlockAlign; + public Int16 bitDepth; + public Int16 fmtExtraSize; - // Data chunk - public Int32 dataID; // "data" - public Int32 dataSize; // The data size should be file size - 36 bytes. + // Data chunk + public Int32 dataID; // "data" + public Int32 dataSize; // The data size should be file size - 36 bytes. - public void Deserialize(Stream stream) - { - StreamHelper.Read(stream, out chunkID); - StreamHelper.Read(stream, out fileSize); - StreamHelper.Read(stream, out riffType); - if (chunkID != BitConverter.ToInt32(Encoding.ASCII.GetBytes("RIFF"), 0)) - { - throw new FormatException("Invalid WAV file header"); - } - if (riffType != BitConverter.ToInt32(Encoding.ASCII.GetBytes("WAVE"), 0)) - { - throw new FormatException($"Not supported RIFF type: '{Encoding.ASCII.GetString(BitConverter.GetBytes(riffType))}'"); - } - Int32 chunkId; - Int32 chunkSize; - while (stream.Position < stream.Length) - { - StreamHelper.Read(stream, out chunkId); - StreamHelper.Read(stream, out chunkSize); - string strChunkId = Encoding.ASCII.GetString( - BitConverter.GetBytes(chunkId)); - if (strChunkId == "fmt ") - { - read_fmt(stream, chunkId, chunkSize); - } - else if (strChunkId == "data") - { - read_data(stream, chunkId, chunkSize); - break; - } - else - { - stream.Seek(chunkSize, SeekOrigin.Current); - } - } - if (fmtID != BitConverter.ToInt32(Encoding.ASCII.GetBytes("fmt "), 0)) - { - throw new FormatException("WAV format chunk not found"); - } - if (dataID != BitConverter.ToInt32(Encoding.ASCII.GetBytes("data"), 0)) - { - throw new FormatException("WAV data chunk not found"); - } - } + public void Deserialize(Stream stream) + { + StreamHelper.Read(stream, out chunkID); + StreamHelper.Read(stream, out fileSize); + StreamHelper.Read(stream, out riffType); + if (chunkID != BitConverter.ToInt32(Encoding.ASCII.GetBytes("RIFF"), 0)) + { + throw new FormatException("Invalid WAV file header"); + } + if (riffType != BitConverter.ToInt32(Encoding.ASCII.GetBytes("WAVE"), 0)) + { + throw new FormatException($"Not supported RIFF type: '{Encoding.ASCII.GetString(BitConverter.GetBytes(riffType))}'"); + } + Int32 chunkId; + Int32 chunkSize; + while (stream.Position < stream.Length) + { + StreamHelper.Read(stream, out chunkId); + StreamHelper.Read(stream, out chunkSize); + string strChunkId = Encoding.ASCII.GetString( + BitConverter.GetBytes(chunkId)); + if (strChunkId == "fmt ") + { + read_fmt(stream, chunkId, chunkSize); + } + else if (strChunkId == "data") + { + read_data(stream, chunkId, chunkSize); + break; + } + else + { + stream.Seek(chunkSize, SeekOrigin.Current); + } + } + if (fmtID != BitConverter.ToInt32(Encoding.ASCII.GetBytes("fmt "), 0)) + { + throw new FormatException("WAV format chunk not found"); + } + if (dataID != BitConverter.ToInt32(Encoding.ASCII.GetBytes("data"), 0)) + { + throw new FormatException("WAV data chunk not found"); + } + } - private void read_data(Stream stream, int chunkId, int chunkSize) - { - dataID = chunkId; - dataSize = chunkSize; - } + private void read_data(Stream stream, int chunkId, int chunkSize) + { + dataID = chunkId; + dataSize = chunkSize; + } - private void read_fmt(Stream stream, int chunkId, int chunkSize) - { - fmtID = chunkId; - fmtSize = chunkSize; - StreamHelper.Read(stream, out fmtCode); - StreamHelper.Read(stream, out channels); - StreamHelper.Read(stream, out sampleRate); - StreamHelper.Read(stream, out fmtAvgBPS); - StreamHelper.Read(stream, out fmtBlockAlign); - StreamHelper.Read(stream, out bitDepth); - if (fmtSize == 18) - { - // Read any extra values - StreamHelper.Read(stream, out fmtExtraSize); - stream.Seek(fmtExtraSize, SeekOrigin.Current); - } - } - } + private void read_fmt(Stream stream, int chunkId, int chunkSize) + { + fmtID = chunkId; + fmtSize = chunkSize; + StreamHelper.Read(stream, out fmtCode); + StreamHelper.Read(stream, out channels); + StreamHelper.Read(stream, out sampleRate); + StreamHelper.Read(stream, out fmtAvgBPS); + StreamHelper.Read(stream, out fmtBlockAlign); + StreamHelper.Read(stream, out bitDepth); + if (fmtSize == 18) + { + // Read any extra values + StreamHelper.Read(stream, out fmtExtraSize); + stream.Seek(fmtExtraSize, SeekOrigin.Current); + } + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/WAV/WavStreamReader.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/WAV/WavStreamReader.cs index 15b1d06493..9be471d3de 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/WAV/WavStreamReader.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/WAV/WavStreamReader.cs @@ -3,109 +3,109 @@ using System.IO; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// From https://archive.codeplex.com/?p=zxmak2 - /// - public class WavStreamReader - { - private Stream m_stream; - private WavHeader m_header = new WavHeader(); + /// + /// From https://archive.codeplex.com/?p=zxmak2 + /// + public class WavStreamReader + { + private Stream m_stream; + private WavHeader m_header = new WavHeader(); - public WavStreamReader(Stream stream) - { - m_stream = stream; - m_header.Deserialize(stream); - } + public WavStreamReader(Stream stream) + { + m_stream = stream; + m_header.Deserialize(stream); + } - public WavHeader Header { get { return m_header; } } + public WavHeader Header { get { return m_header; } } - public int Count { get { return m_header.dataSize / m_header.fmtBlockAlign; } } + public int Count { get { return m_header.dataSize / m_header.fmtBlockAlign; } } - public Int32 ReadNext() - { - // check - sample should be in PCM format - if (m_header.fmtCode != WAVE_FORMAT_PCM && - m_header.fmtCode != WAVE_FORMAT_IEEE_FLOAT) - { - throw new FormatException($"Not supported audio format: fmtCode={m_header.fmtCode}, bitDepth={m_header.bitDepth}"); - } - byte[] data = new byte[m_header.fmtBlockAlign]; - m_stream.Read(data, 0, data.Length); - if (m_header.fmtCode == WAVE_FORMAT_PCM) - { - // use first channel only - if (m_header.bitDepth == 8) - return getSamplePcm8(data, 0, 0); - if (m_header.bitDepth == 16) - return getSamplePcm16(data, 0, 0); - if (m_header.bitDepth == 24) - return getSamplePcm24(data, 0, 0); - if (m_header.bitDepth == 32) - return getSamplePcm32(data, 0, 0); - } - else if (m_header.fmtCode == WAVE_FORMAT_IEEE_FLOAT) - { - // use first channel only - if (m_header.bitDepth == 32) - return getSampleFloat32(data, 0, 0); - if (m_header.bitDepth == 64) - return getSampleFloat64(data, 0, 0); - } - throw new NotSupportedException($"Not supported audio format ({(m_header.fmtCode == WAVE_FORMAT_PCM ? "PCM" : "FLOAT")}/{m_header.bitDepth} bit)"); - } + public Int32 ReadNext() + { + // check - sample should be in PCM format + if (m_header.fmtCode != WAVE_FORMAT_PCM && + m_header.fmtCode != WAVE_FORMAT_IEEE_FLOAT) + { + throw new FormatException($"Not supported audio format: fmtCode={m_header.fmtCode}, bitDepth={m_header.bitDepth}"); + } + byte[] data = new byte[m_header.fmtBlockAlign]; + m_stream.Read(data, 0, data.Length); + if (m_header.fmtCode == WAVE_FORMAT_PCM) + { + // use first channel only + if (m_header.bitDepth == 8) + return getSamplePcm8(data, 0, 0); + if (m_header.bitDepth == 16) + return getSamplePcm16(data, 0, 0); + if (m_header.bitDepth == 24) + return getSamplePcm24(data, 0, 0); + if (m_header.bitDepth == 32) + return getSamplePcm32(data, 0, 0); + } + else if (m_header.fmtCode == WAVE_FORMAT_IEEE_FLOAT) + { + // use first channel only + if (m_header.bitDepth == 32) + return getSampleFloat32(data, 0, 0); + if (m_header.bitDepth == 64) + return getSampleFloat64(data, 0, 0); + } + throw new NotSupportedException($"Not supported audio format ({(m_header.fmtCode == WAVE_FORMAT_PCM ? "PCM" : "FLOAT")}/{m_header.bitDepth} bit)"); + } - private Int32 getSamplePcm8(byte[] bufferRaw, int offset, int channel) - { - return bufferRaw[offset + channel] - 128; - } + private Int32 getSamplePcm8(byte[] bufferRaw, int offset, int channel) + { + return bufferRaw[offset + channel] - 128; + } - private Int32 getSamplePcm16(byte[] bufferRaw, int offset, int channel) - { - return BitConverter.ToInt16(bufferRaw, offset + 2 * channel); - } + private Int32 getSamplePcm16(byte[] bufferRaw, int offset, int channel) + { + return BitConverter.ToInt16(bufferRaw, offset + 2 * channel); + } - private Int32 getSamplePcm24(byte[] bufferRaw, int offset, int channel) - { - Int32 result; - int subOffset = offset + channel * 3; - if (BitConverter.IsLittleEndian) - { - result = ((sbyte)bufferRaw[2 + subOffset]) * 0x10000; - result |= bufferRaw[1 + subOffset] * 0x100; - result |= bufferRaw[0 + subOffset]; - } - else - { - result = ((sbyte)bufferRaw[0 + subOffset]) * 0x10000; - result |= bufferRaw[1 + subOffset] * 0x100; - result |= bufferRaw[2 + subOffset]; - } - return result; - } + private Int32 getSamplePcm24(byte[] bufferRaw, int offset, int channel) + { + Int32 result; + int subOffset = offset + channel * 3; + if (BitConverter.IsLittleEndian) + { + result = ((sbyte)bufferRaw[2 + subOffset]) * 0x10000; + result |= bufferRaw[1 + subOffset] * 0x100; + result |= bufferRaw[0 + subOffset]; + } + else + { + result = ((sbyte)bufferRaw[0 + subOffset]) * 0x10000; + result |= bufferRaw[1 + subOffset] * 0x100; + result |= bufferRaw[2 + subOffset]; + } + return result; + } - private Int32 getSamplePcm32(byte[] bufferRaw, int offset, int channel) - { - return BitConverter.ToInt32(bufferRaw, offset + 4 * channel); - } + private Int32 getSamplePcm32(byte[] bufferRaw, int offset, int channel) + { + return BitConverter.ToInt32(bufferRaw, offset + 4 * channel); + } - private Int32 getSampleFloat32(byte[] data, int offset, int channel) - { - float fSample = BitConverter.ToSingle(data, offset + 4 * channel); - // convert to 32 bit integer - return (Int32)(fSample * Int32.MaxValue); - } + private Int32 getSampleFloat32(byte[] data, int offset, int channel) + { + float fSample = BitConverter.ToSingle(data, offset + 4 * channel); + // convert to 32 bit integer + return (Int32)(fSample * Int32.MaxValue); + } - private Int32 getSampleFloat64(byte[] data, int offset, int channel) - { - double fSample = BitConverter.ToDouble(data, offset + 8 * channel); - // convert to 32 bit integer - return (Int32)(fSample * Int32.MaxValue); - } + private Int32 getSampleFloat64(byte[] data, int offset, int channel) + { + double fSample = BitConverter.ToDouble(data, offset + 8 * channel); + // convert to 32 bit integer + return (Int32)(fSample * Int32.MaxValue); + } - private const int WAVE_FORMAT_PCM = 1; /* PCM */ - private const int WAVE_FORMAT_IEEE_FLOAT = 3; /* IEEE float */ - private const int WAVE_FORMAT_ALAW = 6; /* 8-bit ITU-T G.711 A-law */ - private const int WAVE_FORMAT_MULAW = 7; /* 8-bit ITU-T G.711 µ-law */ - private const int WAVE_FORMAT_EXTENSIBLE = 0xFFFE; /* Determined by SubFormat */ - } + private const int WAVE_FORMAT_PCM = 1; /* PCM */ + private const int WAVE_FORMAT_IEEE_FLOAT = 3; /* IEEE float */ + private const int WAVE_FORMAT_ALAW = 6; /* 8-bit ITU-T G.711 A-law */ + private const int WAVE_FORMAT_MULAW = 7; /* 8-bit ITU-T G.711 µ-law */ + private const int WAVE_FORMAT_EXTENSIBLE = 0xFFFE; /* Determined by SubFormat */ + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/ZXSpectrum.IStatable.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/ZXSpectrum.IStatable.cs index c4f054fcdf..2fa6068384 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/ZXSpectrum.IStatable.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/ZXSpectrum.IStatable.cs @@ -4,94 +4,94 @@ using BizHawk.Emulation.Common; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// ZXHawk: Core Class - /// * IStatable * - /// - public partial class ZXSpectrum : IStatable - { - public bool BinarySaveStatesPreferred => true; + /// + /// ZXHawk: Core Class + /// * IStatable * + /// + public partial class ZXSpectrum : IStatable + { + public bool BinarySaveStatesPreferred => true; public void SaveStateText(TextWriter writer) - { - SyncState(new Serializer(writer)); - } + { + SyncState(new Serializer(writer)); + } - public void LoadStateText(TextReader reader) - { - SyncState(new Serializer(reader)); - } + public void LoadStateText(TextReader reader) + { + SyncState(new Serializer(reader)); + } - public void SaveStateBinary(BinaryWriter bw) - { - SyncState(new Serializer(bw)); - } + public void SaveStateBinary(BinaryWriter bw) + { + SyncState(new Serializer(bw)); + } - public void LoadStateBinary(BinaryReader br) - { - SyncState(new Serializer(br)); - } + public void LoadStateBinary(BinaryReader br) + { + SyncState(new Serializer(br)); + } - public byte[] SaveStateBinary() - { + public byte[] SaveStateBinary() + { using var ms = new MemoryStream(); using var bw = new BinaryWriter(ms); - SaveStateBinary(bw); - bw.Flush(); - return ms.ToArray(); - } + SaveStateBinary(bw); + bw.Flush(); + return ms.ToArray(); + } - private void SyncState(Serializer ser) - { - byte[] core = null; - if (ser.IsWriter) - { - var ms = new MemoryStream(); - ms.Close(); - core = ms.ToArray(); - } + private void SyncState(Serializer ser) + { + byte[] core = null; + if (ser.IsWriter) + { + var ms = new MemoryStream(); + ms.Close(); + core = ms.ToArray(); + } - if (ser.IsWriter) - { - ser.SyncEnum(nameof(_machineType), ref _machineType); + if (ser.IsWriter) + { + ser.SyncEnum(nameof(_machineType), ref _machineType); - _cpu.SyncState(ser); - ser.BeginSection(nameof(ZXSpectrum)); - _machine.SyncState(ser); - ser.Sync("Frame", ref _machine.FrameCount); - ser.Sync("LagCount", ref _lagCount); - ser.Sync("IsLag", ref _isLag); - ser.EndSection(); - } - - if (ser.IsReader) - { - var tmpM = _machineType; - ser.SyncEnum(nameof(_machineType), ref _machineType); - if (tmpM != _machineType && _machineType.ToString() != "72") - { - string msg = "SAVESTATE FAILED TO LOAD!!\n\n"; - msg += "Current Configuration: " + tmpM.ToString(); - msg += "\n"; - msg += "Saved Configuration: " + _machineType.ToString(); - msg += "\n\n"; - msg += "If you wish to load this SaveState ensure that you have the correct machine configuration selected, reboot the core, then try again."; - CoreComm.ShowMessage(msg); - _machineType = tmpM; - } - else - { - _cpu.SyncState(ser); - ser.BeginSection(nameof(ZXSpectrum)); - _machine.SyncState(ser); - ser.Sync("Frame", ref _machine.FrameCount); - ser.Sync("LagCount", ref _lagCount); - ser.Sync("IsLag", ref _isLag); - ser.EndSection(); + _cpu.SyncState(ser); + ser.BeginSection(nameof(ZXSpectrum)); + _machine.SyncState(ser); + ser.Sync("Frame", ref _machine.FrameCount); + ser.Sync("LagCount", ref _lagCount); + ser.Sync("IsLag", ref _isLag); + ser.EndSection(); + } - SyncAllByteArrayDomains(); - } - } - } - } + if (ser.IsReader) + { + var tmpM = _machineType; + ser.SyncEnum(nameof(_machineType), ref _machineType); + if (tmpM != _machineType && _machineType.ToString() != "72") + { + string msg = "SAVESTATE FAILED TO LOAD!!\n\n"; + msg += "Current Configuration: " + tmpM.ToString(); + msg += "\n"; + msg += "Saved Configuration: " + _machineType.ToString(); + msg += "\n\n"; + msg += "If you wish to load this SaveState ensure that you have the correct machine configuration selected, reboot the core, then try again."; + CoreComm.ShowMessage(msg); + _machineType = tmpM; + } + else + { + _cpu.SyncState(ser); + ser.BeginSection(nameof(ZXSpectrum)); + _machine.SyncState(ser); + ser.Sync("Frame", ref _machine.FrameCount); + ser.Sync("LagCount", ref _lagCount); + ser.Sync("IsLag", ref _isLag); + ser.EndSection(); + + SyncAllByteArrayDomains(); + } + } + } + } } diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/ZXSpectrum.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/ZXSpectrum.cs index 1c51cbbc10..33688aaaf0 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/ZXSpectrum.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/ZXSpectrum.cs @@ -11,113 +11,113 @@ using BizHawk.Emulation.Cores.Sound; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { - /// - /// ZXHawk: Core Class - /// * Main Initialization * - /// - [Core( - "ZXHawk", - "Asnivor, Alyosha", - isPorted: false, - isReleased: true)] - public partial class ZXSpectrum : IRegionable, IDriveLight - { - public ZXSpectrum(CoreComm comm, IEnumerable files, List game, object settings, object syncSettings, bool? deterministic) - { - var ser = new BasicServiceProvider(this); - ServiceProvider = ser; - InputCallbacks = new InputCallbackSystem(); - MemoryCallbacks = new MemoryCallbackSystem(new[] { "System Bus" }); + /// + /// ZXHawk: Core Class + /// * Main Initialization * + /// + [Core( + "ZXHawk", + "Asnivor, Alyosha", + isPorted: false, + isReleased: true)] + public partial class ZXSpectrum : IRegionable, IDriveLight + { + public ZXSpectrum(CoreComm comm, IEnumerable files, List game, object settings, object syncSettings, bool? deterministic) + { + var ser = new BasicServiceProvider(this); + ServiceProvider = ser; + InputCallbacks = new InputCallbackSystem(); + MemoryCallbacks = new MemoryCallbackSystem(new[] { "System Bus" }); - CoreComm = comm; + CoreComm = comm; - _gameInfo = game; + _gameInfo = game; - _cpu = new Z80A(); + _cpu = new Z80A(); - _tracer = new TraceBuffer { Header = _cpu.TraceHeader }; - - _files = files?.ToList() ?? new List(); + _tracer = new TraceBuffer { Header = _cpu.TraceHeader }; - if (settings == null) - settings = new ZXSpectrumSettings(); - if (syncSettings == null) - syncSettings = new ZXSpectrumSyncSettings(); + _files = files?.ToList() ?? new List(); - PutSyncSettings((ZXSpectrumSyncSettings)syncSettings ?? new ZXSpectrumSyncSettings()); - PutSettings((ZXSpectrumSettings)settings ?? new ZXSpectrumSettings()); + if (settings == null) + settings = new ZXSpectrumSettings(); + if (syncSettings == null) + syncSettings = new ZXSpectrumSyncSettings(); - List joysticks = new List(); - joysticks.Add(((ZXSpectrumSyncSettings)syncSettings).JoystickType1); - joysticks.Add(((ZXSpectrumSyncSettings)syncSettings).JoystickType2); - joysticks.Add(((ZXSpectrumSyncSettings)syncSettings).JoystickType3); + PutSyncSettings((ZXSpectrumSyncSettings)syncSettings ?? new ZXSpectrumSyncSettings()); + PutSettings((ZXSpectrumSettings)settings ?? new ZXSpectrumSettings()); - deterministicEmulation = ((ZXSpectrumSyncSettings)syncSettings).DeterministicEmulation; + List joysticks = new List(); + joysticks.Add(((ZXSpectrumSyncSettings)syncSettings).JoystickType1); + joysticks.Add(((ZXSpectrumSyncSettings)syncSettings).JoystickType2); + joysticks.Add(((ZXSpectrumSyncSettings)syncSettings).JoystickType3); - if (deterministic != null && deterministic == true) - { - if (deterministicEmulation == false) - { - CoreComm.Notify("Forcing Deterministic Emulation"); - } - - deterministicEmulation = deterministic.Value; - } + deterministicEmulation = ((ZXSpectrumSyncSettings)syncSettings).DeterministicEmulation; - MachineType = SyncSettings.MachineType; + if (deterministic != null && deterministic == true) + { + if (deterministicEmulation == false) + { + CoreComm.Notify("Forcing Deterministic Emulation"); + } - switch (MachineType) - { - case MachineType.ZXSpectrum16: - ControllerDefinition = ZXSpectrumControllerDefinition; - Init(MachineType.ZXSpectrum16, SyncSettings.BorderType, SyncSettings.TapeLoadSpeed, _files, joysticks); - break; - case MachineType.ZXSpectrum48: - ControllerDefinition = ZXSpectrumControllerDefinition; - Init(MachineType.ZXSpectrum48, SyncSettings.BorderType, SyncSettings.TapeLoadSpeed, _files, joysticks); - break; - case MachineType.ZXSpectrum128: - ControllerDefinition = ZXSpectrumControllerDefinition; - Init(MachineType.ZXSpectrum128, SyncSettings.BorderType, SyncSettings.TapeLoadSpeed, _files, joysticks); - break; - case MachineType.ZXSpectrum128Plus2: - ControllerDefinition = ZXSpectrumControllerDefinition; - Init(MachineType.ZXSpectrum128Plus2, SyncSettings.BorderType, SyncSettings.TapeLoadSpeed, _files, joysticks); - break; - case MachineType.ZXSpectrum128Plus2a: - ControllerDefinition = ZXSpectrumControllerDefinition; - Init(MachineType.ZXSpectrum128Plus2a, SyncSettings.BorderType, SyncSettings.TapeLoadSpeed, _files, joysticks); - break; - case MachineType.ZXSpectrum128Plus3: - ControllerDefinition = ZXSpectrumControllerDefinition; - Init(MachineType.ZXSpectrum128Plus3, SyncSettings.BorderType, SyncSettings.TapeLoadSpeed, _files, joysticks); - break; + deterministicEmulation = deterministic.Value; + } + + MachineType = SyncSettings.MachineType; + + switch (MachineType) + { + case MachineType.ZXSpectrum16: + ControllerDefinition = ZXSpectrumControllerDefinition; + Init(MachineType.ZXSpectrum16, SyncSettings.BorderType, SyncSettings.TapeLoadSpeed, _files, joysticks); + break; + case MachineType.ZXSpectrum48: + ControllerDefinition = ZXSpectrumControllerDefinition; + Init(MachineType.ZXSpectrum48, SyncSettings.BorderType, SyncSettings.TapeLoadSpeed, _files, joysticks); + break; + case MachineType.ZXSpectrum128: + ControllerDefinition = ZXSpectrumControllerDefinition; + Init(MachineType.ZXSpectrum128, SyncSettings.BorderType, SyncSettings.TapeLoadSpeed, _files, joysticks); + break; + case MachineType.ZXSpectrum128Plus2: + ControllerDefinition = ZXSpectrumControllerDefinition; + Init(MachineType.ZXSpectrum128Plus2, SyncSettings.BorderType, SyncSettings.TapeLoadSpeed, _files, joysticks); + break; + case MachineType.ZXSpectrum128Plus2a: + ControllerDefinition = ZXSpectrumControllerDefinition; + Init(MachineType.ZXSpectrum128Plus2a, SyncSettings.BorderType, SyncSettings.TapeLoadSpeed, _files, joysticks); + break; + case MachineType.ZXSpectrum128Plus3: + ControllerDefinition = ZXSpectrumControllerDefinition; + Init(MachineType.ZXSpectrum128Plus3, SyncSettings.BorderType, SyncSettings.TapeLoadSpeed, _files, joysticks); + break; case MachineType.Pentagon128: ControllerDefinition = ZXSpectrumControllerDefinition; Init(MachineType.Pentagon128, SyncSettings.BorderType, SyncSettings.TapeLoadSpeed, _files, joysticks); break; - default: - throw new InvalidOperationException("Machine not yet emulated"); - } + default: + throw new InvalidOperationException("Machine not yet emulated"); + } - _cpu.MemoryCallbacks = MemoryCallbacks; + _cpu.MemoryCallbacks = MemoryCallbacks; - HardReset = _machine.HardReset; - SoftReset = _machine.SoftReset; + HardReset = _machine.HardReset; + SoftReset = _machine.SoftReset; - _cpu.FetchMemory = _machine.ReadMemory; - _cpu.ReadMemory = _machine.ReadMemory; - _cpu.WriteMemory = _machine.WriteMemory; - _cpu.ReadHardware = _machine.ReadPort; - _cpu.WriteHardware = _machine.WritePort; - _cpu.FetchDB = _machine.PushBus; - _cpu.OnExecFetch = _machine.CPUMon.OnExecFetch; + _cpu.FetchMemory = _machine.ReadMemory; + _cpu.ReadMemory = _machine.ReadMemory; + _cpu.WriteMemory = _machine.WriteMemory; + _cpu.ReadHardware = _machine.ReadPort; + _cpu.WriteHardware = _machine.WritePort; + _cpu.FetchDB = _machine.PushBus; + _cpu.OnExecFetch = _machine.CPUMon.OnExecFetch; - ser.Register(_tracer); - ser.Register(_cpu); - ser.Register(_machine.ULADevice); + ser.Register(_tracer); + ser.Register(_cpu); + ser.Register(_machine.ULADevice); - // initialize sound mixer and attach the various ISoundProvider devices + // initialize sound mixer and attach the various ISoundProvider devices SoundMixer = new SyncSoundMixer(targetSampleCount: 882); SoundMixer.PinSource(_machine.BuzzerDevice, "System Beeper", (int)(32767 / 10)); SoundMixer.PinSource(_machine.TapeBuzzer, "Tape Audio", (int)(32767 / 10)); @@ -126,194 +126,194 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum SoundMixer.PinSource(_machine.AYDevice, "AY-3-3912"); } - // set audio device settings - if (_machine.AYDevice != null && _machine.AYDevice.GetType() == typeof(AY38912)) - { - ((AY38912)_machine.AYDevice).PanningConfiguration = ((ZXSpectrumSettings)settings).AYPanConfig; - _machine.AYDevice.Volume = ((ZXSpectrumSettings)settings).AYVolume; - } + // set audio device settings + if (_machine.AYDevice != null && _machine.AYDevice.GetType() == typeof(AY38912)) + { + ((AY38912)_machine.AYDevice).PanningConfiguration = ((ZXSpectrumSettings)settings).AYPanConfig; + _machine.AYDevice.Volume = ((ZXSpectrumSettings)settings).AYVolume; + } - if (_machine.BuzzerDevice != null) - { - _machine.BuzzerDevice.Volume = ((ZXSpectrumSettings)settings).EarVolume; - } + if (_machine.BuzzerDevice != null) + { + _machine.BuzzerDevice.Volume = ((ZXSpectrumSettings)settings).EarVolume; + } - if (_machine.TapeBuzzer != null) - { - _machine.TapeBuzzer.Volume = ((ZXSpectrumSettings)settings).TapeVolume; - } + if (_machine.TapeBuzzer != null) + { + _machine.TapeBuzzer.Volume = ((ZXSpectrumSettings)settings).TapeVolume; + } - DCFilter dc = new DCFilter(SoundMixer, 512); - ser.Register(dc); + DCFilter dc = new DCFilter(SoundMixer, 512); + ser.Register(dc); - HardReset(); - SetupMemoryDomains(); - } + HardReset(); + SetupMemoryDomains(); + } - public Action HardReset; - public Action SoftReset; + public Action HardReset; + public Action SoftReset; - private readonly Z80A _cpu; - private readonly TraceBuffer _tracer; - public IController _controller; - public SpectrumBase _machine; - public MachineType MachineType; + private readonly Z80A _cpu; + private readonly TraceBuffer _tracer; + public IController _controller; + public SpectrumBase _machine; + public MachineType MachineType; - public List _gameInfo; + public List _gameInfo; - public List _tapeInfo = new List(); - public List _diskInfo = new List(); + public List _tapeInfo = new List(); + public List _diskInfo = new List(); - private SyncSoundMixer SoundMixer; + private SyncSoundMixer SoundMixer; - private readonly List _files; + private readonly List _files; - public bool DiagRom = false; + public bool DiagRom = false; - private List diagRoms = new List - { - @"\DiagROM.v28", - @"\zx-diagnostics\testrom.bin" - }; - private int diagIndex = 1; + private List diagRoms = new List + { + @"\DiagROM.v28", + @"\zx-diagnostics\testrom.bin" + }; + private int diagIndex = 1; - private byte[] GetFirmware(int length, params string[] names) - { - if (DiagRom & File.Exists(Directory.GetCurrentDirectory() + diagRoms[diagIndex])) - { - var rom = File.ReadAllBytes(Directory.GetCurrentDirectory() + diagRoms[diagIndex]); - return rom; - } + private byte[] GetFirmware(int length, params string[] names) + { + if (DiagRom & File.Exists(Directory.GetCurrentDirectory() + diagRoms[diagIndex])) + { + var rom = File.ReadAllBytes(Directory.GetCurrentDirectory() + diagRoms[diagIndex]); + return rom; + } - // Amstrad licensed ROMs are free to distribute and shipped with BizHawk - byte[] embeddedRom = new byte[length]; - bool embeddedFound = true; - switch (names.FirstOrDefault()) - { - case "48ROM": - embeddedRom = Util.DecompressGzipFile(new MemoryStream(Resources.ZX_48_ROM)); - break; - case "128ROM": - embeddedRom = Util.DecompressGzipFile(new MemoryStream(Resources.ZX_128_ROM)); - break; - case "PLUS2ROM": - embeddedRom = Util.DecompressGzipFile(new MemoryStream(Resources.ZX_plus2_rom)); - break; - case "PLUS2AROM": - embeddedRom = Util.DecompressGzipFile(new MemoryStream(Resources.ZX_plus2a_rom)); - break; - case "PLUS3ROM": - byte[] r0 = Util.DecompressGzipFile(new MemoryStream(Resources.Spectrum3_V4_0_ROM0_bin)); - byte[] r1 = Util.DecompressGzipFile(new MemoryStream(Resources.Spectrum3_V4_0_ROM1_bin)); - byte[] r2 = Util.DecompressGzipFile(new MemoryStream(Resources.Spectrum3_V4_0_ROM2_bin)); - byte[] r3 = Util.DecompressGzipFile(new MemoryStream(Resources.Spectrum3_V4_0_ROM3_bin)); - embeddedRom = r0.Concat(r1).Concat(r2).Concat(r3).ToArray(); - break; - default: - embeddedFound = false; - break; - } + // Amstrad licensed ROMs are free to distribute and shipped with BizHawk + byte[] embeddedRom = new byte[length]; + bool embeddedFound = true; + switch (names.FirstOrDefault()) + { + case "48ROM": + embeddedRom = Util.DecompressGzipFile(new MemoryStream(Resources.ZX_48_ROM)); + break; + case "128ROM": + embeddedRom = Util.DecompressGzipFile(new MemoryStream(Resources.ZX_128_ROM)); + break; + case "PLUS2ROM": + embeddedRom = Util.DecompressGzipFile(new MemoryStream(Resources.ZX_plus2_rom)); + break; + case "PLUS2AROM": + embeddedRom = Util.DecompressGzipFile(new MemoryStream(Resources.ZX_plus2a_rom)); + break; + case "PLUS3ROM": + byte[] r0 = Util.DecompressGzipFile(new MemoryStream(Resources.Spectrum3_V4_0_ROM0_bin)); + byte[] r1 = Util.DecompressGzipFile(new MemoryStream(Resources.Spectrum3_V4_0_ROM1_bin)); + byte[] r2 = Util.DecompressGzipFile(new MemoryStream(Resources.Spectrum3_V4_0_ROM2_bin)); + byte[] r3 = Util.DecompressGzipFile(new MemoryStream(Resources.Spectrum3_V4_0_ROM3_bin)); + embeddedRom = r0.Concat(r1).Concat(r2).Concat(r3).ToArray(); + break; + default: + embeddedFound = false; + break; + } - if (embeddedFound) - return embeddedRom; + if (embeddedFound) + return embeddedRom; - // Embedded ROM not found, maybe this is a peripheral ROM? - var result = names.Select(n => CoreComm.CoreFileProvider.GetFirmware("ZXSpectrum", n, false)).FirstOrDefault(b => b != null && b.Length == length); - if (result == null) - { - throw new MissingFirmwareException($"At least one of these firmwares is required: {string.Join(", ", names)}"); - } + // Embedded ROM not found, maybe this is a peripheral ROM? + var result = names.Select(n => CoreComm.CoreFileProvider.GetFirmware("ZXSpectrum", n, false)).FirstOrDefault(b => b != null && b.Length == length); + if (result == null) + { + throw new MissingFirmwareException($"At least one of these firmwares is required: {string.Join(", ", names)}"); + } - return result; - } + return result; + } - private MachineType _machineType; + private MachineType _machineType; - private void Init(MachineType machineType, BorderType borderType, TapeLoadSpeed tapeLoadSpeed, List files, List joys) - { - _machineType = machineType; + private void Init(MachineType machineType, BorderType borderType, TapeLoadSpeed tapeLoadSpeed, List files, List joys) + { + _machineType = machineType; - // setup the emulated model based on the MachineType - switch (machineType) - { - case MachineType.ZXSpectrum16: - _machine = new ZX16(this, _cpu, borderType, files, joys); - var _systemRom16 = GetFirmware(0x4000, "48ROM"); - var romData16 = RomData.InitROM(machineType, _systemRom16); - _machine.InitROM(romData16); - break; - case MachineType.ZXSpectrum48: - _machine = new ZX48(this, _cpu, borderType, files, joys); - var _systemRom = GetFirmware(0x4000, "48ROM"); - var romData = RomData.InitROM(machineType, _systemRom); - _machine.InitROM(romData); - break; - case MachineType.ZXSpectrum128: - _machine = new ZX128(this, _cpu, borderType, files, joys); - var _systemRom128 = GetFirmware(0x8000, "128ROM"); - var romData128 = RomData.InitROM(machineType, _systemRom128); - _machine.InitROM(romData128); - break; - case MachineType.ZXSpectrum128Plus2: - _machine = new ZX128Plus2(this, _cpu, borderType, files, joys); - var _systemRomP2 = GetFirmware(0x8000, "PLUS2ROM"); - var romDataP2 = RomData.InitROM(machineType, _systemRomP2); - _machine.InitROM(romDataP2); - break; - case MachineType.ZXSpectrum128Plus2a: - _machine = new ZX128Plus2a(this, _cpu, borderType, files, joys); - var _systemRomP4 = GetFirmware(0x10000, "PLUS2AROM"); - var romDataP4 = RomData.InitROM(machineType, _systemRomP4); - _machine.InitROM(romDataP4); - break; - case MachineType.ZXSpectrum128Plus3: - _machine = new ZX128Plus3(this, _cpu, borderType, files, joys); - var _systemRomP3 = GetFirmware(0x10000, "PLUS3ROM"); - var romDataP3 = RomData.InitROM(machineType, _systemRomP3); - _machine.InitROM(romDataP3); - //System.Windows.Forms.MessageBox.Show("+3 is not working at all yet :/"); - break; + // setup the emulated model based on the MachineType + switch (machineType) + { + case MachineType.ZXSpectrum16: + _machine = new ZX16(this, _cpu, borderType, files, joys); + var _systemRom16 = GetFirmware(0x4000, "48ROM"); + var romData16 = RomData.InitROM(machineType, _systemRom16); + _machine.InitROM(romData16); + break; + case MachineType.ZXSpectrum48: + _machine = new ZX48(this, _cpu, borderType, files, joys); + var _systemRom = GetFirmware(0x4000, "48ROM"); + var romData = RomData.InitROM(machineType, _systemRom); + _machine.InitROM(romData); + break; + case MachineType.ZXSpectrum128: + _machine = new ZX128(this, _cpu, borderType, files, joys); + var _systemRom128 = GetFirmware(0x8000, "128ROM"); + var romData128 = RomData.InitROM(machineType, _systemRom128); + _machine.InitROM(romData128); + break; + case MachineType.ZXSpectrum128Plus2: + _machine = new ZX128Plus2(this, _cpu, borderType, files, joys); + var _systemRomP2 = GetFirmware(0x8000, "PLUS2ROM"); + var romDataP2 = RomData.InitROM(machineType, _systemRomP2); + _machine.InitROM(romDataP2); + break; + case MachineType.ZXSpectrum128Plus2a: + _machine = new ZX128Plus2a(this, _cpu, borderType, files, joys); + var _systemRomP4 = GetFirmware(0x10000, "PLUS2AROM"); + var romDataP4 = RomData.InitROM(machineType, _systemRomP4); + _machine.InitROM(romDataP4); + break; + case MachineType.ZXSpectrum128Plus3: + _machine = new ZX128Plus3(this, _cpu, borderType, files, joys); + var _systemRomP3 = GetFirmware(0x10000, "PLUS3ROM"); + var romDataP3 = RomData.InitROM(machineType, _systemRomP3); + _machine.InitROM(romDataP3); + //System.Windows.Forms.MessageBox.Show("+3 is not working at all yet :/"); + break; case MachineType.Pentagon128: _machine = new Pentagon128(this, _cpu, borderType, files, joys); var _systemRomPen128 = GetFirmware(0x8000, "PentagonROM"); var _systemRomTrdos = GetFirmware(0x4000, "TRDOSROM"); var conc = _systemRomPen128.Concat(_systemRomTrdos).ToArray(); - var romDataPen128 = RomData.InitROM(machineType, conc); + var romDataPen128 = RomData.InitROM(machineType, conc); _machine.InitROM(romDataPen128); break; - } - } + } + } - #region IRegionable + #region IRegionable - public DisplayType Region => DisplayType.PAL; + public DisplayType Region => DisplayType.PAL; - #endregion + #endregion - #region IDriveLight + #region IDriveLight - public bool DriveLightEnabled - { - get - { - return true; - } - } + public bool DriveLightEnabled + { + get + { + return true; + } + } - public bool DriveLightOn - { - get - { - if (_machine != null && - (_machine.TapeDevice != null && _machine.TapeDevice.TapeIsPlaying) || - (_machine.UPDDiskDevice != null && _machine.UPDDiskDevice.DriveLight)) - return true; + public bool DriveLightOn + { + get + { + if (_machine != null && + (_machine.TapeDevice != null && _machine.TapeDevice.TapeIsPlaying) || + (_machine.UPDDiskDevice != null && _machine.UPDDiskDevice.DriveLight)) + return true; - return false; - } - } + return false; + } + } - #endregion + #endregion - } + } }