floppy: detect an empty drive via the disk change line floppy_prep() recalibrates and then calls floppy_media_sense(), which issues READ ID once per data rate. A drive with no medium spins no diskette and therefore produces no index pulses, so a real controller never completes READ ID and every attempt waits out FLOPPY_IRQ_TIMEOUT. Booting with an empty drive, or an INT 13h read of an empty A:, costs about ten seconds. The disk change line answers the same question immediately. It latches when the medium is removed and is cleared only by a step pulse with a diskette in the drive. A recalibrate leaves the head on cylinder 0 and need not step at all, so seek to cylinder 1, read DIR again, and seek back if a medium turns out to be present, so that media sensing reads the track it always did. Linux's floppy driver has done exactly this since 2.0 and never issues READ ID. Only the 1.2MB 5.25" drive and the 3.5" drives have the change line (CMOS types 2..5); on the older 5.25" types the pin is not driven and reads back as a permanent "changed". Restrict the probe to those types, so that the code can only ever fall back to the current behaviour. The error code is unchanged: floppy_media_sense() already reports DISK_RET_EMEDIA once it has exhausted the data rates, so an empty drive still answers DISK_RET_EMEDIA -- just without the wait. The wait is a real-hardware effect and cannot be reproduced on QEMU as it stands: QEMU completes READ ID on an empty drive and hands back a made-up sector ID, so media sensing never blocks. That is a bug of its own, and a fix is on qemu-devel [1] -- but it makes READ ID fail immediately rather than never complete, so it does not bring the wait back either. To measure what this patch removes I made QEMU leave the command unfinished the way hardware does. That change is local and not proposed upstream: it would need this series in place first, or every access to an empty drive under QEMU would cost ten seconds. INT 13h AH=02h on an empty A:, "-machine pc -device floppy,drive-type=144": QEMU made to behave like hardware 10.13 s -> 0.11 s QEMU as it stands today 0.11 s -> 0.11 s So on stock QEMU this patch neither costs nor saves time; what it changes there is the error code, which 3/3 is about. Booting from a diskette is unaffected, including the cold start where the change line is set for a freshly inserted medium. [1] https://lore.kernel.org/qemu-devel/20260710113134.43012-1-christian@quante.one/ Signed-off-by: Christian Quante <christian@quante.one>
diff --git a/src/hw/floppy.c b/src/hw/floppy.c index 8f053fe..0469a79 100644 --- a/src/hw/floppy.c +++ b/src/hw/floppy.c
@@ -27,6 +27,9 @@ #define PORT_FD_DATA 0x03f5 #define PORT_FD_DIR 0x03f7 +// Digital Input Register (read) +#define FLOPPY_DIR_DSKCHG 0x80 + #define FLOPPY_SIZE_CODE 0x02 // 512 byte sectors #define FLOPPY_DATALEN 0xff // Not used - because size code is 0x02 #define FLOPPY_MOTOR_TICKS 37 // ~2 seconds @@ -457,6 +460,49 @@ return 0; } +// Only the 1.2MB 5.25" drive and the 3.5" drives have a disk change line. On +// the older 5.25" types the pin is not driven and reads back as a permanent +// "changed". +static int +floppy_has_changeline(u8 ftype) +{ + return ftype >= 2 && ftype <= 5; +} + +// Determine whether a diskette is present, without waiting for a command to +// time out. The disk change line latches when the medium is removed and is +// cleared only by a step pulse with a diskette in the drive. The caller has +// just recalibrated, which leaves the head on cylinder 0 and need not step at +// all, so step to cylinder 1 and look again. +// +// Report DISK_RET_EMEDIA only when the drive is known to be empty -- an older +// drive without a change line is reported as present, so this can only ever +// fall back to the previous behaviour. +static int +floppy_check_media(struct drive_s *drive_gf, u8 floppyid) +{ + if (!floppy_has_changeline(GET_GLOBALFLAT(drive_gf->floppy_type))) + return DISK_RET_SUCCESS; + + // The caller has just recalibrated this drive, so it is selected in the + // DOR and the DIR reflects its change line. + if (!(inb(PORT_FD_DIR) & FLOPPY_DIR_DSKCHG)) + return DISK_RET_SUCCESS; + + int ret = floppy_drive_seek(floppyid, 1); + if (ret) + return ret; + + if (inb(PORT_FD_DIR) & FLOPPY_DIR_DSKCHG) { + dprintf(2, "Floppy_check_media %d: no medium\n", floppyid); + return DISK_RET_EMEDIA; + } + + // A medium is present. Put the head back on cylinder 0, where the caller + // left it, so that media sensing reads the track it always did. + return floppy_drive_seek(floppyid, 0); +} + static int floppy_media_sense(struct drive_s *drive_gf) { @@ -513,6 +559,13 @@ if (ret) return ret; + // An empty drive gives no index pulses, so the READ ID that + // floppy_media_sense() issues would never complete and would wait out + // its timeout once per data rate. Ask the disk change line instead. + ret = floppy_check_media(drive_gf, floppyid); + if (ret) + return ret; + // Sense media. ret = floppy_media_sense(drive_gf); if (ret)