From 6553bf05b4da3a1a9cae08120790434b845cf60c Mon Sep 17 00:00:00 2001
From: Thomas Harte <thomas.harte@gmail.com>
Date: Mon, 14 Aug 2017 22:27:31 -0400
Subject: [PATCH] Corrected multi-sector reads: ensured the incremented sector
 number isn't replaced by the original, and that the controller returns to
 scanning mode.

---
 Components/8272/i8272.cpp | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/Components/8272/i8272.cpp b/Components/8272/i8272.cpp
index 89a9cc709..03a565f16 100644
--- a/Components/8272/i8272.cpp
+++ b/Components/8272/i8272.cpp
@@ -249,6 +249,12 @@ void i8272::posit_event(int event_type) {
 			};
 
 			if(command_.size() < required_lengths[command_[0] & 0x1f]) goto wait_for_complete_command_sequence;
+			if(command_.size() == 9) {
+				cylinder_ = command_[2];
+				head_ = command_[3];
+				sector_ = command_[4];
+				size_ = command_[5];
+			}
 			ResetDataRequest();
 			status_[0] = status_[1] = status_[2] = 0;
 
@@ -303,19 +309,18 @@ void i8272::posit_event(int event_type) {
 		// Establishes the drive and head being addressed, and whether in double density mode; populates the internal
 		// cylinder, head, sector and size registers from the command stream.
 			LOAD_HEAD();
-			cylinder_ = command_[2];
-			head_ = command_[3];
-			sector_ = command_[4];
-			size_ = command_[5];
 
 		// Sets a maximum index hole limit of 2 then performs a find header/read header loop, continuing either until
 		// the index hole limit is breached or a sector is found with a cylinder, head, sector and size equal to the
 		// values in the internal registers.
 			index_hole_limit_ = 2;
+			set_data_mode(DataMode::Scanning);
+//			printf("Seeking %02x %02x %02x %02x\n", cylinder_, head_, sector_, size_);
 		find_next_sector:
 			FIND_HEADER();
 			if(!index_hole_limit_) {
 				// Two index holes have passed wihout finding the header sought.
+//				printf("Not found\n");
 				SetNoData();
 				goto abort_read_write;
 			}
@@ -324,9 +329,11 @@ void i8272::posit_event(int event_type) {
 				// This implies a CRC error in the header; mark as such but continue.
 				SetDataError();
 			}
+//			printf("Considering %02x %02x %02x %02x\n", header_[0], header_[1], header_[2], header_[3]);
 			if(header_[0] != cylinder_ || header_[1] != head_ || header_[2] != sector_ || header_[3] != size_) goto find_next_sector;
 
 			// Branch to whatever is supposed to happen next
+//			printf("Proceeding\n");
 			switch(command_[0] & 0x1f) {
 				case 0x06:	// read data
 				case 0x0c:	// read deleted data