aboutsummaryrefslogtreecommitdiff
path: root/src/floppy.c
blob: 6214acba21ea2d083bb646b65fd476e7342915b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
// 16bit code to access floppy drives.
//
// Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
// Copyright (C) 2002  MandrakeSoft S.A.
//
// This file may be distributed under the terms of the GNU LGPLv3 license.

#include "types.h" // u8
#include "disk.h" // DISK_RET_SUCCESS
#include "config.h" // CONFIG_FLOPPY_SUPPORT
#include "biosvar.h" // SET_BDA
#include "util.h" // irq_disable
#include "cmos.h" // inb_cmos
#include "pic.h" // eoi_pic1
#include "bregs.h" // struct bregs

#define BX_FLOPPY_ON_CNT 37   /* 2 seconds */

// New diskette parameter table adding 3 parameters from IBM
// Since no provisions are made for multiple drive types, most
// values in this table are ignored.  I set parameters for 1.44M
// floppy here
struct floppy_ext_dbt_s diskette_param_table2 VAR16_32 = {
    .dbt = {
        .specify1       = 0xAF,
        .specify2       = 0x02, // head load time 0000001, DMA used
        .shutoff_ticks  = 0x25,
        .bps_code       = 0x02,
        .sectors        = 18,
        .interblock_len = 0x1B,
        .data_len       = 0xFF,
        .gap_len        = 0x6C,
        .fill_byte      = 0xF6,
        .settle_time    = 0x0F,
        .startup_time   = 0x08,
    },
    .max_track      = 79,   // maximum track
    .data_rate      = 0,    // data transfer rate
    .drive_type     = 4,    // drive type in cmos
};

void
floppy_drive_setup()
{
    if (! CONFIG_FLOPPY_SUPPORT)
        return;
    dprintf(3, "init floppy drives\n");
    if (CONFIG_COREBOOT)
        // XXX - disable floppies on coreboot for now.
        outb_cmos(0, CMOS_FLOPPY_DRIVE_TYPE);
    u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
    u8 out = 0;
    u8 num_floppies = 0;

    if (type & 0xf0) {
        out |= 0x07;
        num_floppies++;
    }
    if (type & 0x0f) {
        out |= 0x70;
        num_floppies++;
    }
    SET_BDA(floppy_harddisk_info, out);

    // Update equipment word bits for floppy
    if (num_floppies == 1)
        // 1 drive, ready for boot
        SETBITS_BDA(equipment_list_flags, 0x01);
    else if (num_floppies == 2)
        // 2 drives, ready for boot
        SETBITS_BDA(equipment_list_flags, 0x41);

    outb(0x02, PORT_DMA1_MASK_REG);

    enable_hwirq(6, entry_0e);
}

static inline void
set_diskette_current_cyl(u8 drive, u8 cyl)
{
    if (drive)
        SET_BDA(floppy_track1, cyl);
    else
        SET_BDA(floppy_track0, cyl);
}

static u16
get_drive_type(u8 drive)
{
    // check CMOS to see if drive exists
    u8 drive_type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
    if (drive == 0)
        drive_type >>= 4;
    else
        drive_type &= 0x0f;
    return drive_type;
}

static u16
floppy_media_known(u8 drive)
{
    if (!(GET_BDA(floppy_recalibration_status) & (1<<drive)))
        return 0;
    u8 v = GET_BDA(floppy_media_state[drive]);
    if (!(v & FMS_MEDIA_DRIVE_ESTABLISHED))
        return 0;
    return 1;
}

static void
floppy_reset_controller()
{
    // Reset controller
    u8 val8 = inb(PORT_FD_DOR);
    outb(val8 & ~0x04, PORT_FD_DOR);
    outb(val8 | 0x04, PORT_FD_DOR);

    // Wait for controller to come out of reset
    while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80)
        ;
}

static void
floppy_prepare_controller(u8 drive)
{
    CLEARBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT);

    // turn on motor of selected drive, DMA & int enabled, normal operation
    u8 prev_reset = inb(PORT_FD_DOR) & 0x04;
    u8 dor = 0x10;
    if (drive)
        dor = 0x20;
    dor |= 0x0c;
    dor |= drive;
    outb(dor, PORT_FD_DOR);

    // reset the disk motor timeout value of INT 08
    SET_BDA(floppy_motor_counter, BX_FLOPPY_ON_CNT);

    // wait for drive readiness
    while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80)
        ;

    if (prev_reset == 0) {
        irq_enable();
        u8 v;
        for (;;) {
            v = GET_BDA(floppy_recalibration_status);
            if (v & FRS_TIMEOUT)
                break;
            cpu_relax();
        }
        irq_disable();

        v &= ~FRS_TIMEOUT;
        SET_BDA(floppy_recalibration_status, v);
    }
}

static int
floppy_pio(u8 *cmd, u8 cmdlen)
{
    floppy_prepare_controller(cmd[1] & 1);

    // send command to controller
    u8 i;
    for (i=0; i<cmdlen; i++)
        outb(cmd[i], PORT_FD_DATA);

    irq_enable();
    u8 v;
    for (;;) {
        if (!GET_BDA(floppy_motor_counter)) {
            irq_disable();
            floppy_reset_controller();
            return -1;
        }
        v = GET_BDA(floppy_recalibration_status);
        if (v & FRS_TIMEOUT)
            break;
        cpu_relax();
    }
    irq_disable();

    v &= ~FRS_TIMEOUT;
    SET_BDA(floppy_recalibration_status, v);

    return 0;
}

#define floppy_ret(regs, code)                                  \
    __floppy_ret((regs), (code) | (__LINE__ << 8), __func__)

void
__floppy_ret(struct bregs *regs, u32 linecode, const char *fname)
{
    u8 code = linecode;
    SET_BDA(floppy_last_status, code);
    if (code)
        __set_code_fail(regs, linecode, fname);
    else
        set_code_success(regs);
}

static int
floppy_cmd(struct bregs *regs, u16 count, u8 *cmd, u8 cmdlen)
{
    // es:bx = pointer to where to place information from diskette
    u32 addr = (u32)MAKE_FARPTR(regs->es, regs->bx);

    // check for 64K boundary overrun
    u32 last_addr = addr + count;
    if ((addr >> 16) != (last_addr >> 16)) {
        floppy_ret(regs, DISK_RET_EBOUNDARY);
        return -1;
    }

    u8 mode_register = 0x4a; // single mode, increment, autoinit disable,
    if (cmd[0] == 0xe6)
        // read
        mode_register = 0x46;

    //DEBUGF("floppy dma c2\n");
    outb(0x06, PORT_DMA1_MASK_REG);
    outb(0x00, PORT_DMA1_CLEAR_FF_REG); // clear flip-flop
    outb(addr, PORT_DMA_ADDR_2);
    outb(addr>>8, PORT_DMA_ADDR_2);
    outb(0x00, PORT_DMA1_CLEAR_FF_REG); // clear flip-flop
    outb(count, PORT_DMA_CNT_2);
    outb(count>>8, PORT_DMA_CNT_2);

    // port 0b: DMA-1 Mode Register
    // transfer type=write, channel 2
    outb(mode_register, PORT_DMA1_MODE_REG);

    // port 81: DMA-1 Page Register, channel 2
    outb(addr>>16, PORT_DMA_PAGE_2);

    outb(0x02, PORT_DMA1_MASK_REG); // unmask channel 2

    int ret = floppy_pio(cmd, cmdlen);
    if (ret) {
        floppy_ret(regs, DISK_RET_ETIMEOUT);
        return -1;
    }

    // check port 3f4 for accessibility to status bytes
    if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
        BX_PANIC("int13_diskette: ctrl not ready\n");

    // read 7 return status bytes from controller
    u8 i;
    for (i=0; i<7; i++) {
        u8 v = inb(PORT_FD_DATA);
        cmd[i] = v;
        SET_BDA(floppy_return_status[i], v);
    }

    return 0;
}

static void
floppy_drive_recal(u8 drive)
{
    // send Recalibrate command (2 bytes) to controller
    u8 data[12];
    data[0] = 0x07;  // 07: Recalibrate
    data[1] = drive; // 0=drive0, 1=drive1
    floppy_pio(data, 2);

    SETBITS_BDA(floppy_recalibration_status, 1<<drive);
    set_diskette_current_cyl(drive, 0);
}

static u16
floppy_media_sense(u8 drive)
{
    u16 rv;
    u8 config_data, media_state;

    floppy_drive_recal(drive);

    // for now cheat and get drive type from CMOS,
    // assume media is same as drive type

    // ** config_data **
    // Bitfields for diskette media control:
    // Bit(s)  Description (Table M0028)
    //  7-6  last data rate set by controller
    //        00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
    //  5-4  last diskette drive step rate selected
    //        00=0Ch, 01=0Dh, 10=0Eh, 11=0Ah
    //  3-2  {data rate at start of operation}
    //  1-0  reserved

    // ** media_state **
    // Bitfields for diskette drive media state:
    // Bit(s)  Description (Table M0030)
    //  7-6  data rate
    //    00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
    //  5  double stepping required (e.g. 360kB in 1.2MB)
    //  4  media type established
    //  3  drive capable of supporting 4MB media
    //  2-0  on exit from BIOS, contains
    //    000 trying 360kB in 360kB
    //    001 trying 360kB in 1.2MB
    //    010 trying 1.2MB in 1.2MB
    //    011 360kB in 360kB established
    //    100 360kB in 1.2MB established
    //    101 1.2MB in 1.2MB established
    //    110 reserved
    //    111 all other formats/drives

    switch (get_drive_type(drive)) {
    case 1:
        // 360K 5.25" drive
        config_data = 0x00; // 0000 0000
        media_state = 0x25; // 0010 0101
        rv = 1;
        break;
    case 2:
        // 1.2 MB 5.25" drive
        config_data = 0x00; // 0000 0000
        media_state = 0x25; // 0010 0101   // need double stepping??? (bit 5)
        rv = 1;
        break;
    case 3:
        // 720K 3.5" drive
        config_data = 0x00; // 0000 0000 ???
        media_state = 0x17; // 0001 0111
        rv = 1;
        break;
    case 4:
        // 1.44 MB 3.5" drive
        config_data = 0x00; // 0000 0000
        media_state = 0x17; // 0001 0111
        rv = 1;
        break;
    case 5:
        // 2.88 MB 3.5" drive
        config_data = 0xCC; // 1100 1100
        media_state = 0xD7; // 1101 0111
        rv = 1;
        break;
    //
    // Extended floppy size uses special cmos setting
    case 6:
        // 160k 5.25" drive
        config_data = 0x00; // 0000 0000
        media_state = 0x27; // 0010 0111
        rv = 1;
        break;
    case 7:
        // 180k 5.25" drive
        config_data = 0x00; // 0000 0000
        media_state = 0x27; // 0010 0111
        rv = 1;
        break;
    case 8:
        // 320k 5.25" drive
        config_data = 0x00; // 0000 0000
        media_state = 0x27; // 0010 0111
        rv = 1;
        break;
    default:
        // not recognized
        config_data = 0x00; // 0000 0000
        media_state = 0x00; // 0000 0000
        rv = 0;
    }

    SET_BDA(floppy_last_data_rate, config_data);
    SET_BDA(floppy_media_state[drive], media_state);
    return rv;
}

static int
check_drive(struct bregs *regs, u8 drive)
{
    // see if drive exists
    if (drive > 1 || !get_drive_type(drive)) {
        // XXX - return type doesn't match
        floppy_ret(regs, DISK_RET_ETIMEOUT);
        return -1;
    }

    // see if media in drive, and type is known
    if (floppy_media_known(drive) == 0 && floppy_media_sense(drive) == 0) {
        floppy_ret(regs, DISK_RET_EMEDIA);
        return -1;
    }
    return 0;
}

// diskette controller reset
static void
floppy_1300(struct bregs *regs, u8 drive)
{
    if (drive > 1) {
        floppy_ret(regs, DISK_RET_EPARAM);
        return;
    }
    if (!get_drive_type(drive)) {
        floppy_ret(regs, DISK_RET_ETIMEOUT);
        return;
    }
    set_diskette_current_cyl(drive, 0); // current cylinder
    floppy_ret(regs, DISK_RET_SUCCESS);
}

// Read Diskette Status
static void
floppy_1301(struct bregs *regs, u8 drive)
{
    u8 v = GET_BDA(floppy_last_status);
    regs->ah = v;
    set_cf(regs, v);
}

// Read Diskette Sectors
static void
floppy_1302(struct bregs *regs, u8 drive)
{
    if (check_drive(regs, drive))
        goto fail;

    u8 num_sectors = regs->al;
    u8 track       = regs->ch;
    u8 sector      = regs->cl;
    u8 head        = regs->dh;

    if (head > 1 || sector == 0 || num_sectors == 0
        || track > 79 || num_sectors > 72) {
        floppy_ret(regs, DISK_RET_EPARAM);
        goto fail;
    }

    // send read-normal-data command (9 bytes) to controller
    u8 data[12];
    data[0] = 0xe6; // e6: read normal data
    data[1] = (head << 2) | drive; // HD DR1 DR2
    data[2] = track;
    data[3] = head;
    data[4] = sector;
    data[5] = 2; // 512 byte sector size
    data[6] = sector + num_sectors - 1; // last sector to read on track
    data[7] = 0; // Gap length
    data[8] = 0xff; // Gap length

    int ret = floppy_cmd(regs, (num_sectors * 512) - 1, data, 9);
    if (ret)
        goto fail;

    if (data[0] & 0xc0) {
        floppy_ret(regs, DISK_RET_ECONTROLLER);
        goto fail;
    }

    // ??? should track be new val from return_status[3] ?
    set_diskette_current_cyl(drive, track);
    // AL = number of sectors read (same value as passed)
    floppy_ret(regs, DISK_RET_SUCCESS);
    return;
fail:
    regs->al = 0; // no sectors read
}

// Write Diskette Sectors
static void
floppy_1303(struct bregs *regs, u8 drive)
{
    if (check_drive(regs, drive))
        goto fail;

    u8 num_sectors = regs->al;
    u8 track       = regs->ch;
    u8 sector      = regs->cl;
    u8 head        = regs->dh;

    if (head > 1 || sector == 0 || num_sectors == 0
        || track > 79 || num_sectors > 72) {
        floppy_ret(regs, DISK_RET_EPARAM);
        goto fail;
    }

    // send write-normal-data command (9 bytes) to controller
    u8 data[12];
    data[0] = 0xc5; // c5: write normal data
    data[1] = (head << 2) | drive; // HD DR1 DR2
    data[2] = track;
    data[3] = head;
    data[4] = sector;
    data[5] = 2; // 512 byte sector size
    data[6] = sector + num_sectors - 1; // last sector to write on track
    data[7] = 0; // Gap length
    data[8] = 0xff; // Gap length

    int ret = floppy_cmd(regs, (num_sectors * 512) - 1, data, 9);
    if (ret)
        goto fail;

    if (data[0] & 0xc0) {
        if (data[1] & 0x02) {
            floppy_ret(regs, DISK_RET_EWRITEPROTECT);
            goto fail;
        }
        BX_PANIC("int13_diskette_function: read error\n");
    }

    // ??? should track be new val from return_status[3] ?
    set_diskette_current_cyl(drive, track);
    // AL = number of sectors read (same value as passed)
    floppy_ret(regs, DISK_RET_SUCCESS);
    return;
fail:
    regs->al = 0; // no sectors read
}

// Verify Diskette Sectors
static void
floppy_1304(struct bregs *regs, u8 drive)
{
    if (check_drive(regs, drive))
        goto fail;

    u8 num_sectors = regs->al;
    u8 track       = regs->ch;
    u8 sector      = regs->cl;
    u8 head        = regs->dh;

    if (head > 1 || sector == 0 || num_sectors == 0
        || track > 79 || num_sectors > 72) {
        floppy_ret(regs, DISK_RET_EPARAM);
        goto fail;
    }

    // ??? should track be new val from return_status[3] ?
    set_diskette_current_cyl(drive, track);
    // AL = number of sectors verified (same value as passed)
    floppy_ret(regs, DISK_RET_SUCCESS);
    return;
fail:
    regs->al = 0; // no sectors read
}

// format diskette track
static void
floppy_1305(struct bregs *regs, u8 drive)
{
    dprintf(3, "floppy f05\n");

    if (check_drive(regs, drive))
        return;

    u8 num_sectors = regs->al;
    u8 head        = regs->dh;

    if (head > 1 || num_sectors == 0 || num_sectors > 18) {
        floppy_ret(regs, DISK_RET_EPARAM);
        return;
    }

    // send format-track command (6 bytes) to controller
    u8 data[12];
    data[0] = 0x4d; // 4d: format track
    data[1] = (head << 2) | drive; // HD DR1 DR2
    data[2] = 2; // 512 byte sector size
    data[3] = num_sectors; // number of sectors per track
    data[4] = 0; // Gap length
    data[5] = 0xf6; // Fill byte

    int ret = floppy_cmd(regs, (num_sectors * 4) - 1, data, 6);
    if (ret)
        return;

    if (data[0] & 0xc0) {
        if (data[1] & 0x02) {
            floppy_ret(regs, DISK_RET_EWRITEPROTECT);
            return;
        }
        BX_PANIC("int13_diskette_function: read error\n");
    }

    set_diskette_current_cyl(drive, 0);
    floppy_ret(regs, 0);
}

// read diskette drive parameters
static void
floppy_1308(struct bregs *regs, u8 drive)
{
    dprintf(3, "floppy f08\n");

    u8 drive_type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
    u8 num_floppies = 0;
    if (drive_type & 0xf0)
        num_floppies++;
    if (drive_type & 0x0f)
        num_floppies++;

    if (drive > 1) {
        regs->ax = 0;
        regs->bx = 0;
        regs->cx = 0;
        regs->dx = 0;
        regs->es = 0;
        regs->di = 0;
        regs->dl = num_floppies;
        set_fail(regs);
        return;
    }

    if (drive == 0)
        drive_type >>= 4;
    else
        drive_type &= 0x0f;

    regs->bh = 0;
    regs->bl = drive_type;
    regs->ah = 0;
    regs->al = 0;
    regs->dl = num_floppies;

    switch (drive_type) {
    case 0: // none
        regs->cx = 0;
        regs->dh = 0; // max head #
        break;

    case 1: // 360KB, 5.25"
        regs->cx = 0x2709; // 40 tracks, 9 sectors
        regs->dh = 1; // max head #
        break;

    case 2: // 1.2MB, 5.25"
        regs->cx = 0x4f0f; // 80 tracks, 15 sectors
        regs->dh = 1; // max head #
        break;

    case 3: // 720KB, 3.5"
        regs->cx = 0x4f09; // 80 tracks, 9 sectors
        regs->dh = 1; // max head #
        break;

    case 4: // 1.44MB, 3.5"
        regs->cx = 0x4f12; // 80 tracks, 18 sectors
        regs->dh = 1; // max head #
        break;

    case 5: // 2.88MB, 3.5"
        regs->cx = 0x4f24; // 80 tracks, 36 sectors
        regs->dh = 1; // max head #
        break;

    case 6: // 160k, 5.25"
        regs->cx = 0x2708; // 40 tracks, 8 sectors
        regs->dh = 0; // max head #
        break;

    case 7: // 180k, 5.25"
        regs->cx = 0x2709; // 40 tracks, 9 sectors
        regs->dh = 0; // max head #
        break;

    case 8: // 320k, 5.25"
        regs->cx = 0x2708; // 40 tracks, 8 sectors
        regs->dh = 1; // max head #
        break;

    default: // ?
        BX_PANIC("floppy: int13: bad floppy type\n");
    }

    /* set es & di to point to 11 byte diskette param table in ROM */
    regs->es = SEG_BIOS;
    regs->di = (u32)&diskette_param_table2;
    /* disk status not changed upon success */
    set_success(regs);
}

// read diskette drive type
static void
floppy_1315(struct bregs *regs, u8 drive)
{
    dprintf(6, "floppy f15\n");
    if (drive > 1) {
        set_fail(regs);
        regs->ah = 0; // only 2 drives supported
        // set_diskette_ret_status here ???
        return;
    }
    u8 drive_type = get_drive_type(drive);

    regs->ah = (drive_type != 0);
    set_success(regs);
}

// get diskette change line status
static void
floppy_1316(struct bregs *regs, u8 drive)
{
    if (drive > 1) {
        floppy_ret(regs, DISK_RET_EPARAM);
        return;
    }
    floppy_ret(regs, DISK_RET_ECHANGED);
}

static void
floppy_13XX(struct bregs *regs, u8 drive)
{
    floppy_ret(regs, DISK_RET_EPARAM);
}

void
floppy_13(struct bregs *regs, u8 drive)
{
    if (! CONFIG_FLOPPY_SUPPORT) {
        // Minimal stubs
        switch (regs->ah) {
        case 0x01: floppy_1301(regs, drive); break;
        default:   floppy_13XX(regs, drive); break;
        }
        return;
    }
    switch (regs->ah) {
    case 0x00: floppy_1300(regs, drive); break;
    case 0x01: floppy_1301(regs, drive); break;
    case 0x02: floppy_1302(regs, drive); break;
    case 0x03: floppy_1303(regs, drive); break;
    case 0x04: floppy_1304(regs, drive); break;
    case 0x05: floppy_1305(regs, drive); break;
    case 0x08: floppy_1308(regs, drive); break;
    case 0x15: floppy_1315(regs, drive); break;
    case 0x16: floppy_1316(regs, drive); break;
    default:   floppy_13XX(regs, drive); break;
    }
}

// INT 0Eh Diskette Hardware ISR Entry Point
void VISIBLE16
handle_0e()
{
    debug_isr(DEBUG_ISR_0e);
    if (! CONFIG_FLOPPY_SUPPORT)
        goto done;

    if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0) {
        outb(0x08, PORT_FD_DATA); // sense interrupt status
        while ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
            ;
        do {
            inb(PORT_FD_DATA);
        } while ((inb(PORT_FD_STATUS) & 0xc0) == 0xc0);
    }
    // diskette interrupt has occurred
    SETBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT);

done:
    eoi_pic1();
}

// Called from int08 handler.
void
floppy_tick()
{
    if (! CONFIG_FLOPPY_SUPPORT)
        return;

    // time to turn off drive(s)?
    u8 fcount = GET_BDA(floppy_motor_counter);
    if (fcount) {
        fcount--;
        SET_BDA(floppy_motor_counter, fcount);
        if (fcount == 0)
            // turn motor(s) off
            outb(inb(PORT_FD_DOR) & 0xcf, PORT_FD_DOR);
    }
}