From 66b7f3732739b222521b5bd627890b2beb1cd449 Mon Sep 17 00:00:00 2001 From: Asnivor Date: Wed, 9 May 2018 13:47:59 +0100 Subject: [PATCH] ZXHawk: +3 UPD WriteData command now partially implemented. Data is written to the disk object in memory (allowing castlevania SI to get ingame) but changes to the disk are not serialized in any savestates - #1158 --- .../Hardware/Disk/NECUPD765.FDC.cs | 162 +++++++++--------- .../Hardware/Disk/NECUPD765.FDD.cs | 2 +- 2 files changed, 86 insertions(+), 78 deletions(-) diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Disk/NECUPD765.FDC.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Disk/NECUPD765.FDC.cs index 742f9cf70f..09ae8ba25f 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Disk/NECUPD765.FDC.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Disk/NECUPD765.FDC.cs @@ -47,53 +47,6 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum /// The currently active interrupt /// private InterruptState ActiveInterrupt = InterruptState.None; - - /// - /// Stores the current data flow direction - /// - //private CommandDirection ActiveDirection = CommandDirection.IN; - /* - /// - /// Current raised status/error message - /// - private Status ActiveStatus - { - get { return _activeStatus; } - set - { - if (value == Status.None) - { - // clear the active status flag - _statusRaised = false; - } - else - _statusRaised = true; - _activeStatus = value; - } - } - private Status _activeStatus; - - /// - /// Signs whether there is an active status code raised - /// - private bool StatusRaised - { - get { return _statusRaised; } - set - { - if (value == false) - { - // return to none status - _activeStatus = Status.None; - } - - // dont set true here - } - } - private bool _statusRaised; - */ - - /// /// Command buffer /// This does not contain the initial command byte (only parameter bytes) @@ -170,26 +123,6 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum /// private int OverrunCounter; - /// - /// Signs that the the controller is ready - /// - //public bool FDC_FLAG_RQM; - - /// - /// When TRUE, a SCAN command is currently active - /// - //private bool FDC_FLAG_SCANNING; - - /// - /// Set when a seek operation has completed on drive 0 - /// - //private bool FDC_FLAG_SEEKCOMPLETED_0; - - /// - /// Set when a seek operation is active on drive 0 - /// - //private bool FDC_FLAG_SEEKACTIVE_0; - /// /// Contains result bytes in result phase /// @@ -348,7 +281,6 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum */ private byte Status3; - #endregion #region UPD Internal Functions @@ -1742,16 +1674,38 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum } else { - // not implemented yet - SetBit(SR0_IC0, ref Status0); - SetBit(SR1_NW, ref Status1); - CommitResultCHRN(); - CommitResultStatus(); - //ResBuffer[RS_ST0] = Status0; + // calculate the number of bytes to write + int byteCounter = 0; + byte startSecID = ActiveCommandParams.Sector; + byte endSecID = ActiveCommandParams.EOT; + bool lastSec = false; - // move to result phase - ActivePhase = Phase.Result; + // 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; break; } } @@ -1762,6 +1716,53 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum // 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; //---------------------------------------- @@ -3476,7 +3477,14 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum // store the byte LastSectorDataWriteByte = data; - ActiveCommand.CommandDelegate(); + ActiveCommand.CommandDelegate(); + + if (ExecCounter <= 0) + { + // end of execution phase + ActivePhase = Phase.Result; + } + break; //// result phase case Phase.Result: diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Disk/NECUPD765.FDD.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Disk/NECUPD765.FDD.cs index 44cd6dd9ab..bb2030ce26 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Disk/NECUPD765.FDD.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Disk/NECUPD765.FDD.cs @@ -225,7 +225,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum /// /// Disk is write protected (TRUE BY DEFAULT) /// - public bool FLAG_WRITEPROTECT = true; + public bool FLAG_WRITEPROTECT = false; /// /// Storage for recal steps