+ for( i = 0; i < FDD_MAX_READWRITE_ATTEMPTS; i ++ )\r
+ {\r
+ if( Write )\r
+ FDD_int_SendByte(base, READ_SECTOR); // Was 0xE6\r
+ else\r
+ FDD_int_SendByte(base, READ_SECTOR); // Was 0xE6\r
+ FDD_int_SendByte(base, (head << 2) | (Disk&1));\r
+ FDD_int_SendByte(base, (Uint8)cyl);\r
+ FDD_int_SendByte(base, (Uint8)head);\r
+ FDD_int_SendByte(base, (Uint8)sec);\r
+ FDD_int_SendByte(base, 0x02); // Bytes Per Sector (Real BPS=128*2^{val})\r
+ FDD_int_SendByte(base, spt); // SPT\r
+ FDD_int_SendByte(base, 0x1B); // Gap Length (27 is default)\r
+ FDD_int_SendByte(base, 0xFF); // Data Length\r
+ \r
+ // Wait for IRQ\r
+ if( Write ) {\r
+ LOG("Writing Data");\r
+ DMA_WriteData(2, 512, Buffer);\r
+ LOG("Waiting for Data to be written");\r
+ FDD_WaitIRQ();\r
+ }\r
+ else {\r
+ LOG("Waiting for data to be read");\r
+ FDD_WaitIRQ();\r
+ LOG("Reading Data");\r
+ DMA_ReadData(2, 512, Buffer);\r
+ }\r
+ \r
+ // Clear Input Buffer\r
+ LOG("Clearing Input Buffer");\r
+ // Status Values\r
+ st0 = FDD_int_GetByte(base);\r
+ st1 = FDD_int_GetByte(base);\r
+ st2 = FDD_int_GetByte(base);\r
+ \r
+ // Cylinder, Head and Sector (mutilated in some way\r
+ rcy = FDD_int_GetByte(base);\r
+ rhe = FDD_int_GetByte(base);\r
+ rse = FDD_int_GetByte(base);\r
+ // Should be the BPS set above (0x02)\r
+ bps = FDD_int_GetByte(base);\r
+ \r
+ // Check Status\r
+ // - Error Code\r
+ if(st0 & 0xC0) {\r
+ LOG("Error (st0 & 0xC0) \"%s\"", cFDD_STATUSES[st0 >> 6]);\r
+ continue;\r
+ }\r
+ // - Status Flags\r
+ if(st0 & 0x08) { LOG("Drive not ready"); continue; }\r
+ if(st1 & 0x80) { LOG("End of Cylinder"); continue; }\r
+ if(st1 & 0x20) { LOG("CRC Error"); continue; }\r
+ if(st1 & 0x10) { LOG("Controller Timeout"); continue; }\r
+ if(st1 & 0x04) { LOG("No Data Found"); continue; }\r
+ if(st1 & 0x01 || st2 & 0x01) {\r
+ LOG("No Address mark found");\r
+ continue;\r
+ }\r
+ if(st2 & 0x40) { LOG("Deleted address mark"); continue; }\r
+ if(st2 & 0x20) { LOG("CRC error in data"); continue; }\r
+ if(st2 & 0x10) { LOG("Wrong Cylinder"); continue; }\r
+ if(st2 & 0x04) { LOG("uPD765 sector not found"); continue; }\r
+ if(st2 & 0x02) { LOG("Bad Cylinder"); continue; }\r
+ \r
+ if(bps != 0x2) {\r
+ LOG("Returned BPS = 0x%02x, not 0x02", bps);\r
+ continue;\r
+ }\r
+ \r
+ if(st1 & 0x02) {\r
+ LOG("Floppy not writable");\r
+ i = FDD_MAX_READWRITE_ATTEMPTS+1;\r
+ break;\r
+ }\r
+ \r
+ // Success!\r
+ break;\r
+ }\r