Fix bug in int1308 handling of floppies.
[seabios.git] / src / floppy.c
1 // 16bit code to access floppy drives.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU GPLv3 license.
7
8 #include "types.h" // u8
9 #include "disk.h" // DISK_RET_SUCCESS
10 #include "config.h" // CONFIG_FLOPPY_SUPPORT
11 #include "biosvar.h" // SET_BDA
12 #include "util.h" // irq_disable
13 #include "cmos.h" // inb_cmos
14 #include "pic.h" // unmask_pic1
15 #include "bregs.h" // struct bregs
16
17 #define BX_FLOPPY_ON_CNT 37   /* 2 seconds */
18
19 // New diskette parameter table adding 3 parameters from IBM
20 // Since no provisions are made for multiple drive types, most
21 // values in this table are ignored.  I set parameters for 1.44M
22 // floppy here
23 struct floppy_ext_dbt_s diskette_param_table2 VISIBLE16 = {
24     .dbt = {
25         .specify1       = 0xAF,
26         .specify2       = 0x02, // head load time 0000001, DMA used
27         .shutoff_ticks  = 0x25,
28         .bps_code       = 0x02,
29         .sectors        = 18,
30         .interblock_len = 0x1B,
31         .data_len       = 0xFF,
32         .gap_len        = 0x6C,
33         .fill_byte      = 0xF6,
34         .settle_time    = 0x0F,
35         .startup_time   = 0x08,
36     },
37     .max_track      = 79,   // maximum track
38     .data_rate      = 0,    // data transfer rate
39     .drive_type     = 4,    // drive type in cmos
40 };
41
42 void
43 floppy_drive_setup()
44 {
45     if (! CONFIG_FLOPPY_SUPPORT)
46         return;
47     dprintf(3, "init floppy drives\n");
48     u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
49     u8 out = 0;
50     u8 num_floppies = 0;
51
52     if (type & 0xf0) {
53         out |= 0x07;
54         num_floppies++;
55     }
56     if (type & 0x0f) {
57         out |= 0x70;
58         num_floppies++;
59     }
60     SET_BDA(floppy_harddisk_info, out);
61
62     // Update equipment word bits for floppy
63     if (num_floppies == 1)
64         // 1 drive, ready for boot
65         SETBITS_BDA(equipment_list_flags, 0x01);
66     else if (num_floppies == 2)
67         // 2 drives, ready for boot
68         SETBITS_BDA(equipment_list_flags, 0x41);
69
70     outb(0x02, PORT_DMA1_MASK_REG);
71
72     // Enable IRQ6 (handle_0e)
73     unmask_pic1(PIC1_IRQ6);
74 }
75
76 static inline void
77 set_diskette_current_cyl(u8 drive, u8 cyl)
78 {
79     if (drive)
80         SET_BDA(floppy_track1, cyl);
81     else
82         SET_BDA(floppy_track0, cyl);
83 }
84
85 static u16
86 get_drive_type(u8 drive)
87 {
88     // check CMOS to see if drive exists
89     u8 drive_type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
90     if (drive == 0)
91         drive_type >>= 4;
92     else
93         drive_type &= 0x0f;
94     return drive_type;
95 }
96
97 static u16
98 floppy_media_known(u8 drive)
99 {
100     if (!(GET_BDA(floppy_recalibration_status) & (1<<drive)))
101         return 0;
102     u8 v = GET_BDA(floppy_media_state[drive]);
103     if (!(v & FMS_MEDIA_DRIVE_ESTABLISHED))
104         return 0;
105     return 1;
106 }
107
108 static void
109 floppy_reset_controller()
110 {
111     // Reset controller
112     u8 val8 = inb(PORT_FD_DOR);
113     outb(val8 & ~0x04, PORT_FD_DOR);
114     outb(val8 | 0x04, PORT_FD_DOR);
115
116     // Wait for controller to come out of reset
117     while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80)
118         ;
119 }
120
121 static void
122 floppy_prepare_controller(u8 drive)
123 {
124     CLEARBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT);
125
126     // turn on motor of selected drive, DMA & int enabled, normal operation
127     u8 prev_reset = inb(PORT_FD_DOR) & 0x04;
128     u8 dor = 0x10;
129     if (drive)
130         dor = 0x20;
131     dor |= 0x0c;
132     dor |= drive;
133     outb(dor, PORT_FD_DOR);
134
135     // reset the disk motor timeout value of INT 08
136     SET_BDA(floppy_motor_counter, BX_FLOPPY_ON_CNT);
137
138     // wait for drive readiness
139     while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80)
140         ;
141
142     if (prev_reset == 0) {
143         irq_enable();
144         u8 v;
145         for (;;) {
146             v = GET_BDA(floppy_recalibration_status);
147             if (v & FRS_TIMEOUT)
148                 break;
149             cpu_relax();
150         }
151         irq_disable();
152
153         v &= ~FRS_TIMEOUT;
154         SET_BDA(floppy_recalibration_status, v);
155     }
156 }
157
158 static int
159 floppy_pio(u8 *cmd, u8 cmdlen)
160 {
161     floppy_prepare_controller(cmd[1] & 1);
162
163     // send command to controller
164     u8 i;
165     for (i=0; i<cmdlen; i++)
166         outb(cmd[i], PORT_FD_DATA);
167
168     irq_enable();
169     u8 v;
170     for (;;) {
171         if (!GET_BDA(floppy_motor_counter)) {
172             irq_disable();
173             floppy_reset_controller();
174             return -1;
175         }
176         v = GET_BDA(floppy_recalibration_status);
177         if (v & FRS_TIMEOUT)
178             break;
179         cpu_relax();
180     }
181     irq_disable();
182
183     v &= ~FRS_TIMEOUT;
184     SET_BDA(floppy_recalibration_status, v);
185
186     return 0;
187 }
188
189 #define floppy_ret(regs, code) \
190     __floppy_ret(__func__, __LINE__, (regs), (code))
191
192 void
193 __floppy_ret(const char *fname, int lineno, struct bregs *regs, u8 code)
194 {
195     SET_BDA(floppy_last_status, code);
196     if (code)
197         __set_code_fail(fname, lineno, regs, code);
198     else
199         set_code_success(regs);
200 }
201
202 static int
203 floppy_cmd(struct bregs *regs, u16 count, u8 *cmd, u8 cmdlen)
204 {
205     // es:bx = pointer to where to place information from diskette
206     u32 addr = (u32)MAKE_FARPTR(regs->es, regs->bx);
207
208     // check for 64K boundary overrun
209     u32 last_addr = addr + count;
210     if ((addr >> 16) != (last_addr >> 16)) {
211         floppy_ret(regs, DISK_RET_EBOUNDARY);
212         return -1;
213     }
214
215     u8 mode_register = 0x4a; // single mode, increment, autoinit disable,
216     if (cmd[0] == 0xe6)
217         // read
218         mode_register = 0x46;
219
220     //DEBUGF("floppy dma c2\n");
221     outb(0x06, PORT_DMA1_MASK_REG);
222     outb(0x00, PORT_DMA1_CLEAR_FF_REG); // clear flip-flop
223     outb(addr, PORT_DMA_ADDR_2);
224     outb(addr>>8, PORT_DMA_ADDR_2);
225     outb(0x00, PORT_DMA1_CLEAR_FF_REG); // clear flip-flop
226     outb(count, PORT_DMA_CNT_2);
227     outb(count>>8, PORT_DMA_CNT_2);
228
229     // port 0b: DMA-1 Mode Register
230     // transfer type=write, channel 2
231     outb(mode_register, PORT_DMA1_MODE_REG);
232
233     // port 81: DMA-1 Page Register, channel 2
234     outb(addr>>16, PORT_DMA_PAGE_2);
235
236     outb(0x02, PORT_DMA1_MASK_REG); // unmask channel 2
237
238     int ret = floppy_pio(cmd, cmdlen);
239     if (ret) {
240         floppy_ret(regs, DISK_RET_ETIMEOUT);
241         return -1;
242     }
243
244     // check port 3f4 for accessibility to status bytes
245     if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
246         BX_PANIC("int13_diskette: ctrl not ready\n");
247
248     // read 7 return status bytes from controller
249     u8 i;
250     for (i=0; i<7; i++) {
251         u8 v = inb(PORT_FD_DATA);
252         cmd[i] = v;
253         SET_BDA(floppy_return_status[i], v);
254     }
255
256     return 0;
257 }
258
259 static void
260 floppy_drive_recal(u8 drive)
261 {
262     // send Recalibrate command (2 bytes) to controller
263     u8 data[12];
264     data[0] = 0x07;  // 07: Recalibrate
265     data[1] = drive; // 0=drive0, 1=drive1
266     floppy_pio(data, 2);
267
268     SETBITS_BDA(floppy_recalibration_status, 1<<drive);
269     set_diskette_current_cyl(drive, 0);
270 }
271
272 static u16
273 floppy_media_sense(u8 drive)
274 {
275     u16 rv;
276     u8 config_data, media_state;
277
278     floppy_drive_recal(drive);
279
280     // for now cheat and get drive type from CMOS,
281     // assume media is same as drive type
282
283     // ** config_data **
284     // Bitfields for diskette media control:
285     // Bit(s)  Description (Table M0028)
286     //  7-6  last data rate set by controller
287     //        00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
288     //  5-4  last diskette drive step rate selected
289     //        00=0Ch, 01=0Dh, 10=0Eh, 11=0Ah
290     //  3-2  {data rate at start of operation}
291     //  1-0  reserved
292
293     // ** media_state **
294     // Bitfields for diskette drive media state:
295     // Bit(s)  Description (Table M0030)
296     //  7-6  data rate
297     //    00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
298     //  5  double stepping required (e.g. 360kB in 1.2MB)
299     //  4  media type established
300     //  3  drive capable of supporting 4MB media
301     //  2-0  on exit from BIOS, contains
302     //    000 trying 360kB in 360kB
303     //    001 trying 360kB in 1.2MB
304     //    010 trying 1.2MB in 1.2MB
305     //    011 360kB in 360kB established
306     //    100 360kB in 1.2MB established
307     //    101 1.2MB in 1.2MB established
308     //    110 reserved
309     //    111 all other formats/drives
310
311     switch (get_drive_type(drive)) {
312     case 1:
313         // 360K 5.25" drive
314         config_data = 0x00; // 0000 0000
315         media_state = 0x25; // 0010 0101
316         rv = 1;
317         break;
318     case 2:
319         // 1.2 MB 5.25" drive
320         config_data = 0x00; // 0000 0000
321         media_state = 0x25; // 0010 0101   // need double stepping??? (bit 5)
322         rv = 1;
323         break;
324     case 3:
325         // 720K 3.5" drive
326         config_data = 0x00; // 0000 0000 ???
327         media_state = 0x17; // 0001 0111
328         rv = 1;
329         break;
330     case 4:
331         // 1.44 MB 3.5" drive
332         config_data = 0x00; // 0000 0000
333         media_state = 0x17; // 0001 0111
334         rv = 1;
335         break;
336     case 5:
337         // 2.88 MB 3.5" drive
338         config_data = 0xCC; // 1100 1100
339         media_state = 0xD7; // 1101 0111
340         rv = 1;
341         break;
342     //
343     // Extended floppy size uses special cmos setting
344     case 6:
345         // 160k 5.25" drive
346         config_data = 0x00; // 0000 0000
347         media_state = 0x27; // 0010 0111
348         rv = 1;
349         break;
350     case 7:
351         // 180k 5.25" drive
352         config_data = 0x00; // 0000 0000
353         media_state = 0x27; // 0010 0111
354         rv = 1;
355         break;
356     case 8:
357         // 320k 5.25" drive
358         config_data = 0x00; // 0000 0000
359         media_state = 0x27; // 0010 0111
360         rv = 1;
361         break;
362     default:
363         // not recognized
364         config_data = 0x00; // 0000 0000
365         media_state = 0x00; // 0000 0000
366         rv = 0;
367     }
368
369     SET_BDA(floppy_last_data_rate, config_data);
370     SET_BDA(floppy_media_state[drive], media_state);
371     return rv;
372 }
373
374 static int
375 check_drive(struct bregs *regs, u8 drive)
376 {
377     // see if drive exists
378     if (drive > 1 || !get_drive_type(drive)) {
379         // XXX - return type doesn't match
380         floppy_ret(regs, DISK_RET_ETIMEOUT);
381         return -1;
382     }
383
384     // see if media in drive, and type is known
385     if (floppy_media_known(drive) == 0 && floppy_media_sense(drive) == 0) {
386         floppy_ret(regs, DISK_RET_EMEDIA);
387         return -1;
388     }
389     return 0;
390 }
391
392 // diskette controller reset
393 static void
394 floppy_1300(struct bregs *regs, u8 drive)
395 {
396     if (drive > 1) {
397         floppy_ret(regs, DISK_RET_EPARAM);
398         return;
399     }
400     if (!get_drive_type(drive)) {
401         floppy_ret(regs, DISK_RET_ETIMEOUT);
402         return;
403     }
404     set_diskette_current_cyl(drive, 0); // current cylinder
405     floppy_ret(regs, DISK_RET_SUCCESS);
406 }
407
408 // Read Diskette Status
409 static void
410 floppy_1301(struct bregs *regs, u8 drive)
411 {
412     u8 v = GET_BDA(floppy_last_status);
413     regs->ah = v;
414     set_cf(regs, v);
415 }
416
417 // Read Diskette Sectors
418 static void
419 floppy_1302(struct bregs *regs, u8 drive)
420 {
421     if (check_drive(regs, drive))
422         goto fail;
423
424     u8 num_sectors = regs->al;
425     u8 track       = regs->ch;
426     u8 sector      = regs->cl;
427     u8 head        = regs->dh;
428
429     if (head > 1 || sector == 0 || num_sectors == 0
430         || track > 79 || num_sectors > 72) {
431         floppy_ret(regs, DISK_RET_EPARAM);
432         goto fail;
433     }
434
435     // send read-normal-data command (9 bytes) to controller
436     u8 data[12];
437     data[0] = 0xe6; // e6: read normal data
438     data[1] = (head << 2) | drive; // HD DR1 DR2
439     data[2] = track;
440     data[3] = head;
441     data[4] = sector;
442     data[5] = 2; // 512 byte sector size
443     data[6] = sector + num_sectors - 1; // last sector to read on track
444     data[7] = 0; // Gap length
445     data[8] = 0xff; // Gap length
446
447     int ret = floppy_cmd(regs, (num_sectors * 512) - 1, data, 9);
448     if (ret)
449         goto fail;
450
451     if (data[0] & 0xc0) {
452         floppy_ret(regs, DISK_RET_ECONTROLLER);
453         goto fail;
454     }
455
456     // ??? should track be new val from return_status[3] ?
457     set_diskette_current_cyl(drive, track);
458     // AL = number of sectors read (same value as passed)
459     floppy_ret(regs, DISK_RET_SUCCESS);
460     return;
461 fail:
462     regs->al = 0; // no sectors read
463 }
464
465 // Write Diskette Sectors
466 static void
467 floppy_1303(struct bregs *regs, u8 drive)
468 {
469     if (check_drive(regs, drive))
470         goto fail;
471
472     u8 num_sectors = regs->al;
473     u8 track       = regs->ch;
474     u8 sector      = regs->cl;
475     u8 head        = regs->dh;
476
477     if (head > 1 || sector == 0 || num_sectors == 0
478         || track > 79 || num_sectors > 72) {
479         floppy_ret(regs, DISK_RET_EPARAM);
480         goto fail;
481     }
482
483     // send write-normal-data command (9 bytes) to controller
484     u8 data[12];
485     data[0] = 0xc5; // c5: write normal data
486     data[1] = (head << 2) | drive; // HD DR1 DR2
487     data[2] = track;
488     data[3] = head;
489     data[4] = sector;
490     data[5] = 2; // 512 byte sector size
491     data[6] = sector + num_sectors - 1; // last sector to write on track
492     data[7] = 0; // Gap length
493     data[8] = 0xff; // Gap length
494
495     int ret = floppy_cmd(regs, (num_sectors * 512) - 1, data, 9);
496     if (ret)
497         goto fail;
498
499     if (data[0] & 0xc0) {
500         if (data[1] & 0x02) {
501             floppy_ret(regs, DISK_RET_EWRITEPROTECT);
502             goto fail;
503         }
504         BX_PANIC("int13_diskette_function: read error\n");
505     }
506
507     // ??? should track be new val from return_status[3] ?
508     set_diskette_current_cyl(drive, track);
509     // AL = number of sectors read (same value as passed)
510     floppy_ret(regs, DISK_RET_SUCCESS);
511     return;
512 fail:
513     regs->al = 0; // no sectors read
514 }
515
516 // Verify Diskette Sectors
517 static void
518 floppy_1304(struct bregs *regs, u8 drive)
519 {
520     if (check_drive(regs, drive))
521         goto fail;
522
523     u8 num_sectors = regs->al;
524     u8 track       = regs->ch;
525     u8 sector      = regs->cl;
526     u8 head        = regs->dh;
527
528     if (head > 1 || sector == 0 || num_sectors == 0
529         || track > 79 || num_sectors > 72) {
530         floppy_ret(regs, DISK_RET_EPARAM);
531         goto fail;
532     }
533
534     // ??? should track be new val from return_status[3] ?
535     set_diskette_current_cyl(drive, track);
536     // AL = number of sectors verified (same value as passed)
537     floppy_ret(regs, DISK_RET_SUCCESS);
538     return;
539 fail:
540     regs->al = 0; // no sectors read
541 }
542
543 // format diskette track
544 static void
545 floppy_1305(struct bregs *regs, u8 drive)
546 {
547     dprintf(3, "floppy f05\n");
548
549     if (check_drive(regs, drive))
550         return;
551
552     u8 num_sectors = regs->al;
553     u8 head        = regs->dh;
554
555     if (head > 1 || num_sectors == 0 || num_sectors > 18) {
556         floppy_ret(regs, DISK_RET_EPARAM);
557         return;
558     }
559
560     // send format-track command (6 bytes) to controller
561     u8 data[12];
562     data[0] = 0x4d; // 4d: format track
563     data[1] = (head << 2) | drive; // HD DR1 DR2
564     data[2] = 2; // 512 byte sector size
565     data[3] = num_sectors; // number of sectors per track
566     data[4] = 0; // Gap length
567     data[5] = 0xf6; // Fill byte
568
569     int ret = floppy_cmd(regs, (num_sectors * 4) - 1, data, 6);
570     if (ret)
571         return;
572
573     if (data[0] & 0xc0) {
574         if (data[1] & 0x02) {
575             floppy_ret(regs, DISK_RET_EWRITEPROTECT);
576             return;
577         }
578         BX_PANIC("int13_diskette_function: read error\n");
579     }
580
581     set_diskette_current_cyl(drive, 0);
582     floppy_ret(regs, 0);
583 }
584
585 // read diskette drive parameters
586 static void
587 floppy_1308(struct bregs *regs, u8 drive)
588 {
589     dprintf(3, "floppy f08\n");
590
591     u8 drive_type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
592     u8 num_floppies = 0;
593     if (drive_type & 0xf0)
594         num_floppies++;
595     if (drive_type & 0x0f)
596         num_floppies++;
597
598     if (drive > 1) {
599         regs->ax = 0;
600         regs->bx = 0;
601         regs->cx = 0;
602         regs->dx = 0;
603         regs->es = 0;
604         regs->di = 0;
605         regs->dl = num_floppies;
606         set_fail(regs);
607         return;
608     }
609
610     if (drive == 0)
611         drive_type >>= 4;
612     else
613         drive_type &= 0x0f;
614
615     regs->bh = 0;
616     regs->bl = drive_type;
617     regs->ah = 0;
618     regs->al = 0;
619     regs->dl = num_floppies;
620
621     switch (drive_type) {
622     case 0: // none
623         regs->cx = 0;
624         regs->dh = 0; // max head #
625         break;
626
627     case 1: // 360KB, 5.25"
628         regs->cx = 0x2709; // 40 tracks, 9 sectors
629         regs->dh = 1; // max head #
630         break;
631
632     case 2: // 1.2MB, 5.25"
633         regs->cx = 0x4f0f; // 80 tracks, 15 sectors
634         regs->dh = 1; // max head #
635         break;
636
637     case 3: // 720KB, 3.5"
638         regs->cx = 0x4f09; // 80 tracks, 9 sectors
639         regs->dh = 1; // max head #
640         break;
641
642     case 4: // 1.44MB, 3.5"
643         regs->cx = 0x4f12; // 80 tracks, 18 sectors
644         regs->dh = 1; // max head #
645         break;
646
647     case 5: // 2.88MB, 3.5"
648         regs->cx = 0x4f24; // 80 tracks, 36 sectors
649         regs->dh = 1; // max head #
650         break;
651
652     case 6: // 160k, 5.25"
653         regs->cx = 0x2708; // 40 tracks, 8 sectors
654         regs->dh = 0; // max head #
655         break;
656
657     case 7: // 180k, 5.25"
658         regs->cx = 0x2709; // 40 tracks, 9 sectors
659         regs->dh = 0; // max head #
660         break;
661
662     case 8: // 320k, 5.25"
663         regs->cx = 0x2708; // 40 tracks, 8 sectors
664         regs->dh = 1; // max head #
665         break;
666
667     default: // ?
668         BX_PANIC("floppy: int13: bad floppy type\n");
669     }
670
671     /* set es & di to point to 11 byte diskette param table in ROM */
672     regs->es = SEG_BIOS;
673     regs->di = (u32)&diskette_param_table2;
674     /* disk status not changed upon success */
675     set_success(regs);
676 }
677
678 // read diskette drive type
679 static void
680 floppy_1315(struct bregs *regs, u8 drive)
681 {
682     dprintf(6, "floppy f15\n");
683     if (drive > 1) {
684         set_fail(regs);
685         regs->ah = 0; // only 2 drives supported
686         // set_diskette_ret_status here ???
687         return;
688     }
689     u8 drive_type = get_drive_type(drive);
690
691     regs->ah = (drive_type != 0);
692     set_success(regs);
693 }
694
695 // get diskette change line status
696 static void
697 floppy_1316(struct bregs *regs, u8 drive)
698 {
699     if (drive > 1) {
700         floppy_ret(regs, DISK_RET_EPARAM);
701         return;
702     }
703     floppy_ret(regs, DISK_RET_ECHANGED);
704 }
705
706 static void
707 floppy_13XX(struct bregs *regs, u8 drive)
708 {
709     floppy_ret(regs, DISK_RET_EPARAM);
710 }
711
712 void
713 floppy_13(struct bregs *regs, u8 drive)
714 {
715     if (! CONFIG_FLOPPY_SUPPORT) {
716         // Minimal stubs
717         switch (regs->ah) {
718         case 0x01: floppy_1301(regs, drive); break;
719         default:   floppy_13XX(regs, drive); break;
720         }
721         return;
722     }
723     switch (regs->ah) {
724     case 0x00: floppy_1300(regs, drive); break;
725     case 0x01: floppy_1301(regs, drive); break;
726     case 0x02: floppy_1302(regs, drive); break;
727     case 0x03: floppy_1303(regs, drive); break;
728     case 0x04: floppy_1304(regs, drive); break;
729     case 0x05: floppy_1305(regs, drive); break;
730     case 0x08: floppy_1308(regs, drive); break;
731     case 0x15: floppy_1315(regs, drive); break;
732     case 0x16: floppy_1316(regs, drive); break;
733     default:   floppy_13XX(regs, drive); break;
734     }
735 }
736
737 // INT 0Eh Diskette Hardware ISR Entry Point
738 void VISIBLE16
739 handle_0e()
740 {
741     debug_isr(DEBUG_ISR_0e);
742     if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0) {
743         outb(0x08, PORT_FD_DATA); // sense interrupt status
744         while ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
745             ;
746         do {
747             inb(PORT_FD_DATA);
748         } while ((inb(PORT_FD_STATUS) & 0xc0) == 0xc0);
749     }
750     eoi_pic1();
751     // diskette interrupt has occurred
752     SETBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT);
753 }
754
755 // Called from int08 handler.
756 void
757 floppy_tick()
758 {
759     // time to turn off drive(s)?
760     u8 fcount = GET_BDA(floppy_motor_counter);
761     if (fcount) {
762         fcount--;
763         SET_BDA(floppy_motor_counter, fcount);
764         if (fcount == 0)
765             // turn motor(s) off
766             outb(inb(PORT_FD_DOR) & 0xcf, PORT_FD_DOR);
767     }
768 }