Detecting floppy drives is surprisingly tricky and poorly understood. The difficulty lies mainly in the interface between the floppy disk controller (FDC) and the actual drive(s). The interface isn’t particularly complex, but it isn’t straightforward, and it does not follow the intended design of the original NEC µPD765A FDC.
Many if not most operating systems do not even attempt to detect floppy drives and use the value configured by the user in the non-volatile CMOS memory. Even the excellent Undocumented PC (van Gilluwe) cops out and uses the BIOS to detect floppy drives. Yet a simple and reliable method for drive presence detection has been published in the mid-1980s, in the classic IBM PC/AT Technical Reference.
The code is in the PC/AT BIOS listings, although understanding how and why it works takes more than reading the code. All the hard work is done by the DRIVE_DET routine in diskette.asm.
The routine first turns on the drive motor (via MOTOR_ON), which also selects the desired drive. The RECAL routine is then called to recalibrate the drive.
The recalibration routine detects whether a drive is present as a side effect. How does that work? The FDC sends outward step pulses to the drive and looks for the TRK0 signal from the drive going active. If there is no drive attached, the TRK0 signal will never go active and the recalibrate FDC command will report failure. Note that it does not matter whether a floppy is in the drive or not, only whether a drive is physically attached.
If the recalibration succeeds, the SEEK routine is called to position the head to track 48. With an 80-track drive, this operation will behave as one might expect. With a 40-track drive, the head will be unable to move quite as far (perhaps only as far as track 43), yet the FDC will think it’s at track 48. There is no mechanism for a floppy drive to determine which track the head is on and the FDC only internally keeps track of the assumed position. Only track 0 can be detected.
At any rate, at this point the FDC thinks the head is positioned over track 48, which may or may not be true. Next the BIOS enters a loop which seeks back towards track 0, but only after first seeking to track 10. After every step, the RESULTS routine updates the status and the track 0 signal is checked.
On an 80-track drive, the TRK0 signal will only become active when the BIOS expects to reach track 0. But on a 40-track drive, the head never moved as far as it was asked to, and the TRK0 signal will go active sooner. That way, the BIOS can detect a 40-track drive.
It is an open question what this detection might do to poorly built 40-track drives. In theory, the stepping mechanism could be damaged. On the other hand, PC/AT class systems usually do have 80-track drives.
There is also no way to distinguish between 3½” and 5¼” drives. On the other hand, software may not care much. A high-density 1.44MB 3½” drive operates much the same as a high-density 1.2MB 5¼” drive. When reading and writing floppies, the physical media size does not matter. The drive capabilities do not matter either with a well-written BIOS/driver—the system will be able to read any supported medium.
The drive capabilities do matter when formatting disks. To avoid unnecessary failures, the system needs to refuse formats that cannot be supported. Even so, the user can try e.g. formatting a double-density diskette as high-density, which usually results in numerous errors.