Hi,
tom9876543 wrote:How does the controller seek to the correct sector?
Does it have to read all the sectors on the track until it gets to the right one?
Example, to read sector 4, the the FDC must read sectors 1,2,3 first?
Internally the FDC keeps track of which cylinder it thinks the drive's heads are are on. For example, if the FDC thinks the drive's heads are on cylinder 5 and you ask it to seek to cylinder 9, then it knows it needs 4 STEP# pulses to get from where it thinks the heads are to where you want the heads to be.
Now; sometimes the FDC doesn't know where the heads actually are (and/or where it thinks the heads are is wrong). In this case it's your device driver's responsibility to issue a "recalibrate" command so that the FDC can correct itself. For "recalibrate", the FDC keeps doing STEP# pulses until the drive's TRK0 signal says that the heads are on track zero.
This means that your driver should send a "recalibrate" command when it's first started (after resetting the FDC, for each drive connected) to ensure the FDC has knows where the drive's heads are; and that your driver should also send "recalibrate" as part of your error recovery routines (especially if it gets an "address mark not found" error from the FDC, as that can be a sign that the FDC lost track of where the drive's heads are).
If you think about all of this, you'll see that it's possible for the FDC to seek correctly to any cylinder, without ever reading any data from the disk in the drive.
Once the FDC knows (or thinks it knows) that it's on the right cylinder, it uses the HDSEL signal to tell the drive which head it wants.
After that; near the start of each sector on the floppy disk there's an "address mark" (before the sector's data) - these address marks are created when the disk is formatted. The address marks are read until one of the address marks matches the "cylinder, head and sector" that you're looking for; and then the FDC can start reading or writing the sector's data. Also note that there's a hole in the floppy disk that lines up with an special "hole detector" that is connected to the INDX signal. If the FDC sees 2 pulses on the IDX signal, then it knows that the heads have read every address mark on the track and none matched what you're looking for, so it stops trying and gives you an "address mark not found" error.
Note that there's no need for sectors to be in any specific order within a track. If the sectors are created in a strange order with the format command (e.g. sector numbers 9, 1, 5, 7, 2, 6, ...) then it will all still work fine (the FDC will find the address mark it's looking for before getting 2 IDX pulses).
There are various "interleaving" schemes that use this fact to improve timing (in certain situations). For example, if you read one sector, then do something else for a little while, then read the next sector, then do something else for a little while, etc; then sequential order will mean that after reading sector 1 and doing something else you've missed the start of sector 2 and have to wait an entire disk rotation before the start of sector 2 comes around again. This situation can be improved by formatting sectors using a non-sequential order (like 1, 9, 2, 10, 3, 11, 4, ...).
Also, because you get to specify the details stored in the address marks when you format a track; this can also be abused for various non-standard purposes (e.g. disk copy protection again). For example, there's no real reason why you can't use sector numbers 11, 22, 33, 44, 55, 66, 77, ...; and this would be enough to confuse most disk drivers (but not your own which would be designed to accept this strange sector numbering scheme).
Cheers,
Brendan