Skip to content

Commit 2ba438a

Browse files
committed
Erase track before writing - fixes checksum errors
1 parent aa14e74 commit 2ba438a

File tree

12 files changed

+132
-9
lines changed

12 files changed

+132
-9
lines changed

ArduinoFloppyReader/ArduinoFloppyReader/Main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void adf2Disk(wchar_t* argv[], bool verify) {
3737
printf("\nWrite disk from ADF mode\n\n");
3838
if (!verify) printf("WARNING: It is STRONGLY recommended to write with verify support turned on.\r\n\r\n");
3939

40-
ADFResult result = writer.ADFToDisk(argv[2],verify, [](const int currentTrack, const DiskSurface currentSide, bool isVerifyError) ->WriteResponse {
40+
ADFResult result = writer.ADFToDisk(argv[2],true, verify, [](const int currentTrack, const DiskSurface currentSide, bool isVerifyError) ->WriteResponse {
4141
if (isVerifyError) {
4242
char input;
4343
do {
Binary file not shown.
Binary file not shown.

ArduinoFloppyReader/ArduinoFloppyReaderWin/ArduinoFloppyReaderWinDlg.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ void CArduinoFloppyReaderWinDlg::DoDataExchange(CDataExchange* pDX)
279279
DDX_Control(pDX, IDC_STARTSTOP2, m_writeButton);
280280
DDX_Control(pDX, IDC_CHECK1, m_verify);
281281
DDX_Control(pDX, IDC_STARTSTOP3, m_diagnostics);
282+
DDX_Control(pDX, IDC_ERASE, m_erase);
282283
}
283284

284285
BEGIN_MESSAGE_MAP(CArduinoFloppyReaderWinDlg, CDialogEx)
@@ -351,6 +352,7 @@ BOOL CArduinoFloppyReaderWinDlg::OnInitDialog()
351352
}
352353
m_spinner.SetRange(1, 999);
353354
m_verify.SetCheck(TRUE);
355+
m_erase.SetCheck(TRUE);
354356

355357
return TRUE; // return TRUE unless you set the focus to a control
356358
}
@@ -509,7 +511,7 @@ bool CArduinoFloppyReaderWinDlg::runThreadWrite() {
509511

510512
m_inputADF.GetWindowText(filename);
511513

512-
ArduinoFloppyReader::ADFResult readerResult = writer.ADFToDisk(filename.GetBuffer(), m_verify.GetCheck()!=0,
514+
ArduinoFloppyReader::ADFResult readerResult = writer.ADFToDisk(filename.GetBuffer(), m_erase.GetCheck()!=0, m_verify.GetCheck()!=0,
513515
[this](const int currentTrack, const ArduinoFloppyReader::DiskSurface currentSide, const bool isVerifyError) -> ArduinoFloppyReader::WriteResponse {
514516

515517
if (m_cancelButtonPressed) return ArduinoFloppyReader::WriteResponse::wrAbort;
@@ -570,6 +572,7 @@ void CArduinoFloppyReaderWinDlg::enableDialog(bool enable) {
570572
m_inputADF.EnableWindow(enable);
571573
m_comport.EnableWindow(enable);
572574
m_verify.EnableWindow(enable);
575+
m_erase.EnableWindow(enable);
573576
m_diagnostics.EnableWindow(enable);
574577

575578
if (enable) {

ArduinoFloppyReader/ArduinoFloppyReaderWin/ArduinoFloppyReaderWinDlg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,5 @@ class CArduinoFloppyReaderWinDlg : public CDialogEx
9191
void saveComPort();
9292
afx_msg void OnBnClickedStartstop3();
9393
CButton m_diagnostics;
94+
CButton m_erase;
9495
};
184 Bytes
Binary file not shown.

ArduinoFloppyReader/lib/ADFWriter.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ using namespace ArduinoFloppyReader;
5252
#define NUM_SECTORS_PER_TRACK 11 // Number of sectors per track
5353
#define RAW_SECTOR_SIZE (8+56+SECTOR_BYTES+SECTOR_BYTES) // Size of a sector, *Including* the sector sync word longs
5454
#define ADF_TRACK_SIZE (SECTOR_BYTES*NUM_SECTORS_PER_TRACK) // Bytes required for a single track
55-
#define TRACK_FILLER_SIZE 256 // Theritically this would be 532 bytes, but if the disk spins slower or fast this would be wrong
5655

5756

5857
const char* TEST_BYTE_SEQUENCE = "amiga.robsmithdev.co.uk";
@@ -67,7 +66,7 @@ typedef struct alignas(8) {
6766
// Raw sector data
6867
RawEncodedSector sectors[NUM_SECTORS_PER_TRACK];
6968
// Blank "Filler" gap content. (this may get overwritten by the sectors a little)
70-
unsigned char filler2[TRACK_FILLER_SIZE];
69+
unsigned char filler2[8];
7170
} FullDiskTrack;
7271

7372
// Structure to hold data while we decode it
@@ -877,7 +876,7 @@ bool ADFWriter::runDiagnostics(const unsigned int comPort, std::function<void(bo
877876

878877

879878
// Writes an ADF file back to a floppy disk. Return FALSE in the callback to abort this operation
880-
ADFResult ADFWriter::ADFToDisk(const std::wstring inputFile, bool verify, std::function < WriteResponse(const int currentTrack, const DiskSurface currentSide, const bool isVerifyError) > callback) {
879+
ADFResult ADFWriter::ADFToDisk(const std::wstring inputFile, bool eraseFirst, bool verify, std::function < WriteResponse(const int currentTrack, const DiskSurface currentSide, const bool isVerifyError) > callback) {
881880
if (!m_device.isOpen()) return ADFResult::adfrDriveError;
882881

883882
// Upgrade to writing mode
@@ -935,7 +934,13 @@ ADFResult ADFWriter::ADFToDisk(const std::wstring inputFile, bool verify, std::f
935934
for (unsigned int a = 0; a < NUM_SECTORS_PER_TRACK; a++) trackRead.invalidSectors[a].clear();
936935

937936
int failCount = 0;
938-
while ((verify) && (trackRead.validSectors.size()<NUM_SECTORS_PER_TRACK)) {
937+
while (trackRead.validSectors.size()<NUM_SECTORS_PER_TRACK) {
938+
939+
if (eraseFirst) {
940+
if (m_device.eraseCurrentTrack() != DiagnosticResponse::drOK)
941+
return adfrDriveError;
942+
}
943+
939944
switch (m_device.writeCurrentTrack((const unsigned char*)(&disktrack), sizeof(disktrack), false)) {
940945
case DiagnosticResponse::drWriteProtected: CloseHandle(hADFFile);
941946
return ADFResult::adfrDiskWriteProtected;
@@ -996,6 +1001,7 @@ ADFResult ADFWriter::ADFToDisk(const std::wstring inputFile, bool verify, std::f
9961001
}
9971002
}
9981003
}
1004+
else break;
9991005
}
10001006

10011007

ArduinoFloppyReader/lib/ADFWriter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ namespace ArduinoFloppyReader {
8484
ADFResult DiskToADF(const std::wstring outputFile, const unsigned int numTracks, std::function < WriteResponse(const int currentTrack, const DiskSurface currentSide, const int retryCounter, const int sectorsFound, const int badSectorsFound)> callback);
8585

8686
// Writes an ADF file back to a floppy disk. Return FALSE in the callback to abort this operation. If verify is set then the track isread back and and sector checksums are checked for 11 valid sectors
87-
ADFResult ADFToDisk(const std::wstring inputFile, bool verify, std::function < WriteResponse(const int currentTrack, const DiskSurface currentSide, const bool isVerifyError) > callback);
87+
ADFResult ADFToDisk(const std::wstring inputFile, bool eraseFirst, bool verify, std::function < WriteResponse(const int currentTrack, const DiskSurface currentSide, const bool isVerifyError) > callback);
8888

8989
// Run diagnostics on the system. You do not need to call openDevice first. Return TURE if everything passed
9090
bool runDiagnostics(const unsigned int comPort, std::function<void(bool isError, const std::string message)> messageOutput, std::function<bool(bool isQuestion, const std::string question)> askQuestion);

ArduinoFloppyReader/lib/ArduinoInterface.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ using namespace ArduinoFloppyReader;
4444
#define COMMAND_DISABLE '-'
4545
#define COMMAND_WRITETRACK '>'
4646
#define COMMAND_ENABLEWRITE '~'
47+
#define COMMAND_ERASETRACK 'X'
4748
#define COMMAND_DIAGNOSTICS '&'
4849

4950

@@ -61,6 +62,7 @@ std::string lastCommandToName(LastCommand cmd) {
6162
case lcReadTrack: return "ReadTrack";
6263
case lcWriteTrack: return "WriteTrack";
6364
case lcRunDiagnostics: return "RunDiagnostics";
65+
case lcEraseTrack: return "EraseTrack";
6466
default: return "Unknown";
6567
}
6668
}
@@ -581,6 +583,50 @@ DiagnosticResponse ArduinoInterface::readCurrentTrack(RawTrackData& trackData, c
581583
return m_lastError;
582584
}
583585

586+
// Asks the Arduino to wipe the current track
587+
DiagnosticResponse ArduinoInterface::eraseCurrentTrack() {
588+
m_lastError = runCommand(COMMAND_ERASETRACK);
589+
if (m_lastError != DiagnosticResponse::drOK) {
590+
m_lastCommand = lcEraseTrack;
591+
return m_lastError;
592+
}
593+
594+
unsigned char chr;
595+
if (!deviceRead(&chr, 1)) {
596+
m_lastCommand = lcEraseTrack;
597+
m_lastError = DiagnosticResponse::drReadResponseFailed;
598+
return m_lastError;
599+
}
600+
601+
// 'N' means NO Writing, aka write protected
602+
if (chr == 'N') {
603+
m_lastCommand = lcEraseTrack;
604+
m_lastError = DiagnosticResponse::drWriteProtected;
605+
return m_lastError;
606+
}
607+
if (chr != 'Y') {
608+
m_lastCommand = lcEraseTrack;
609+
m_lastError = DiagnosticResponse::drStatusError;
610+
return m_lastError;
611+
}
612+
613+
unsigned char response;
614+
if (!deviceRead(&response, 1)) {
615+
m_lastCommand = lcEraseTrack;
616+
m_lastError = DiagnosticResponse::drReadResponseFailed;
617+
return m_lastError;
618+
}
619+
// If this is a '1' then the Arduino erased the track
620+
if (response != '1') {
621+
m_lastCommand = lcEraseTrack;
622+
m_lastError = DiagnosticResponse::drStatusError;
623+
return m_lastError;
624+
}
625+
626+
m_lastError = DiagnosticResponse::drOK;
627+
return m_lastError;
628+
}
629+
584630
// Writes RAW data onto the current track
585631
DiagnosticResponse ArduinoInterface::writeCurrentTrack(const unsigned char* data, const unsigned short numBytes, const bool writeFromIndexPulse) {
586632
m_lastError = runCommand(COMMAND_WRITETRACK);

ArduinoFloppyReader/lib/ArduinoInterface.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ namespace ArduinoFloppyReader {
107107
lcSelectSurface,
108108
lcReadTrack,
109109
lcWriteTrack,
110+
lcEraseTrack,
110111
lcRunDiagnostics
111112
};
112113

@@ -176,6 +177,9 @@ namespace ArduinoFloppyReader {
176177
// Attempts to write a sector back to the disk. This must be pre-formatted and MFM encoded correctly
177178
DiagnosticResponse writeCurrentTrack(const unsigned char*, const unsigned short numBytes, const bool writeFromIndexPulse);
178179

180+
// Asks the Arduino to wipe the current track, filling it with the 0xAA pattern
181+
DiagnosticResponse eraseCurrentTrack();
182+
179183
// Check CTS status
180184
DiagnosticResponse testCTS(const unsigned int portNumber);
181185

0 commit comments

Comments
 (0)