Statistics
| Branch: | Revision:

root / hw / fdc.c @ 69e5bc90

History | View | Annotate | Download (46 kB)

1
/*
2
 * QEMU Floppy disk emulator
3
 * 
4
 * Copyright (c) 2003 Jocelyn Mayer
5
 * 
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <string.h>
27
#include <inttypes.h>
28

    
29
#include "cpu.h"
30
#include "vl.h"
31

    
32
/********************************************************/
33
/* debug Floppy devices */
34
//#define DEBUG_FLOPPY
35

    
36
#ifdef DEBUG_FLOPPY
37
#define FLOPPY_DPRINTF(fmt, args...) \
38
do { printf("FLOPPY: " fmt , ##args); } while (0)
39
#else
40
#define FLOPPY_DPRINTF(fmt, args...)
41
#endif
42

    
43
#define FLOPPY_ERROR(fmt, args...) \
44
do { printf("FLOPPY ERROR: %s: " fmt, __func__ , ##args); } while (0)
45

    
46
/********************************************************/
47
/* Floppy drive emulation                               */
48

    
49
/* Will always be a fixed parameter for us */
50
#define FD_SECTOR_LEN 512
51
#define FD_SECTOR_SC  2   /* Sector size code */
52

    
53
/* Floppy disk drive emulation */
54
typedef enum fdisk_type_t {
55
    FDRIVE_DISK_288   = 0x01, /* 2.88 MB disk           */
56
    FDRIVE_DISK_144   = 0x02, /* 1.44 MB disk           */
57
    FDRIVE_DISK_720   = 0x03, /* 720 kB disk            */
58
    FDRIVE_DISK_NONE  = 0x04, /* No disk                */
59
} fdisk_type_t;
60

    
61
typedef enum fdrive_type_t {
62
    FDRIVE_DRV_144  = 0x00,   /* 1.44 MB 3"5 drive      */
63
    FDRIVE_DRV_288  = 0x01,   /* 2.88 MB 3"5 drive      */
64
    FDRIVE_DRV_120  = 0x02,   /* 1.2  MB 5"25 drive     */
65
    FDRIVE_DRV_NONE = 0x03,   /* No drive connected     */
66
} fdrive_type_t;
67

    
68
typedef struct fdrive_t {
69
    BlockDriverState *bs;
70
    /* Drive status */
71
    fdrive_type_t drive;
72
    uint8_t motor;            /* on/off                 */
73
    uint8_t perpendicular;    /* 2.88 MB access mode    */
74
    uint8_t rv;               /* Revalidated            */
75
    /* Position */
76
    uint8_t head;
77
    uint8_t track;
78
    uint8_t sect;
79
    /* Last operation status */
80
    uint8_t dir;              /* Direction              */
81
    uint8_t rw;               /* Read/write             */
82
    /* Media */
83
    fdisk_type_t disk;        /* Disk type              */
84
    uint8_t last_sect;        /* Nb sector per track    */
85
    uint8_t max_track;        /* Nb of tracks           */
86
    uint8_t ro;               /* Is read-only           */
87
} fdrive_t;
88

    
89
static void fd_init (fdrive_t *drv)
90
{
91
    /* Drive */
92
    drv->bs = NULL;
93
//    drv->drive = FDRIVE_DRV_288;
94
    drv->drive = FDRIVE_DRV_144;
95
    drv->motor = 0;
96
    drv->perpendicular = 0;
97
    drv->rv = 0;
98
    /* Disk */
99
    drv->disk = FDRIVE_DISK_NONE;
100
    drv->last_sect = 1;
101
    drv->max_track = 0;
102
}
103

    
104
static int _fd_sector (uint8_t head, uint8_t track,
105
                        uint8_t sect, uint8_t last_sect)
106
{
107
    return (((track * 2) + head) * last_sect) + sect - 1;
108
}
109

    
110
/* Returns current position, in sectors, for given drive */
111
static int fd_sector (fdrive_t *drv)
112
{
113
    return _fd_sector(drv->head, drv->track, drv->sect, drv->last_sect);
114
}
115

    
116
static int fd_seek (fdrive_t *drv, uint8_t head, uint8_t track, uint8_t sect,
117
                    int enable_seek)
118
{
119
    uint32_t sector;
120

    
121
    if (track > drv->max_track) {
122
        FLOPPY_ERROR("try to read %d %02x %02x (max=%d %02x %02x)\n",
123
                     head, track, sect, 1, drv->max_track, drv->last_sect);
124
        return 2;
125
    }
126
    if (sect > drv->last_sect) {
127
        FLOPPY_ERROR("try to read %d %02x %02x (max=%d %02x %02x)\n",
128
                     head, track, sect, 1, drv->max_track, drv->last_sect);
129
        return 3;
130
    }
131
    sector = _fd_sector(head, track, sect, drv->last_sect);
132
    if (sector != fd_sector(drv)) {
133
#if 0
134
        if (!enable_seek) {
135
            FLOPPY_ERROR("no implicit seek %d %02x %02x (max=%d %02x %02x)\n",
136
                         head, track, sect, 1, drv->max_track, drv->last_sect);
137
            return 4;
138
        }
139
#endif
140
        drv->head = head;
141
        drv->track = track;
142
        drv->sect = sect;
143
        return 1;
144
    }
145

    
146
    return 0;
147
}
148

    
149
/* Set drive back to track 0 */
150
static void fd_recalibrate (fdrive_t *drv)
151
{
152
    FLOPPY_DPRINTF("recalibrate\n");
153
    drv->head = 0;
154
    drv->track = 0;
155
    drv->sect = 1;
156
    drv->dir = 1;
157
    drv->rw = 0;
158
}
159

    
160
/* Revalidate a disk drive after a disk change */
161
static void fd_revalidate (fdrive_t *drv, int ro)
162
{
163
    int64_t nb_sectors;
164

    
165
    FLOPPY_DPRINTF("revalidate\n");
166
    drv->rv = 0;
167
    if (drv->bs != NULL) {
168
        bdrv_get_geometry(drv->bs, &nb_sectors);
169
#if 1
170
        if (nb_sectors > 2880) 
171
#endif
172
        {
173
            /* Pretend we have a 2.88 MB disk */
174
            drv->disk = FDRIVE_DISK_288;
175
            drv->last_sect = 36;
176
            drv->max_track = 80;
177
#if 1
178
        } else if (nb_sectors > 1440) {
179
            /* Pretend we have a 1.44 MB disk */
180
            drv->disk = FDRIVE_DISK_144;
181
            drv->last_sect = 18;
182
            drv->max_track = 80;
183
        } else {
184
            /* Pretend we have a 720 kB disk */
185
            drv->disk = FDRIVE_DISK_720;
186
            drv->last_sect = 9;
187
            drv->max_track = 80;
188
#endif
189
        }
190
    } else {
191
        drv->disk = FDRIVE_DISK_NONE;
192
        drv->last_sect = 1; /* Avoid eventual divide by 0 bugs */
193
    }
194
    drv->ro = ro;
195
    drv->rv = 1;
196
}
197

    
198
/* Motor control */
199
static void fd_start (fdrive_t *drv)
200
{
201
    drv->motor = 1;
202
}
203

    
204
static void fd_stop (fdrive_t *drv)
205
{
206
    drv->motor = 0;
207
}
208

    
209
/* Re-initialise a drives (motor off, repositioned) */
210
static void fd_reset (fdrive_t *drv)
211
{
212
    fd_stop(drv);
213
    fd_recalibrate(drv);
214
}
215

    
216
/********************************************************/
217
/* Intel 82078 floppy disk controler emulation          */
218

    
219
static void fdctrl_reset (int do_irq);
220
static void fdctrl_reset_fifo (void);
221
static int fdctrl_transfer_handler (uint32_t addr, int size, int *irq);
222
static int fdctrl_misc_handler (int duknwo);
223
static void fdctrl_raise_irq (uint8_t status);
224

    
225
static uint32_t fdctrl_read_statusB (CPUState *env, uint32_t reg);
226
static uint32_t fdctrl_read_dor (CPUState *env, uint32_t reg);
227
static void fdctrl_write_dor (CPUState *env, uint32_t reg, uint32_t value);
228
static uint32_t fdctrl_read_tape (CPUState *env, uint32_t reg);
229
static void fdctrl_write_tape (CPUState *env, uint32_t reg, uint32_t value);
230
static uint32_t fdctrl_read_main_status (CPUState *env, uint32_t reg);
231
static void fdctrl_write_rate (CPUState *env, uint32_t reg, uint32_t value);
232
static uint32_t fdctrl_read_data (CPUState *env, uint32_t reg);
233
static void fdctrl_write_data (CPUState *env, uint32_t reg, uint32_t value);
234
static uint32_t fdctrl_read_dir (CPUState *env, uint32_t reg);
235

    
236
enum {
237
    FD_CTRL_ACTIVE = 0x01,
238
    FD_CTRL_RESET  = 0x02,
239
    FD_CTRL_SLEEP  = 0x04,
240
    FD_CTRL_BUSY   = 0x08,
241
    FD_CTRL_INTR   = 0x10,
242
};
243

    
244
enum {
245
    FD_DIR_WRITE   = 0,
246
    FD_DIR_READ    = 1,
247
    FD_DIR_SCANE   = 2,
248
    FD_DIR_SCANL   = 3,
249
    FD_DIR_SCANH   = 4,
250
};
251

    
252
enum {
253
    FD_STATE_CMD    = 0x00,
254
    FD_STATE_STATUS = 0x01,
255
    FD_STATE_DATA   = 0x02,
256
    FD_STATE_STATE  = 0x03,
257
    FD_STATE_MULTI  = 0x10,
258
    FD_STATE_SEEK   = 0x20,
259
};
260

    
261
#define FD_STATE(state) ((state) & FD_STATE_STATE)
262
#define FD_MULTI_TRACK(state) ((state) & FD_STATE_MULTI)
263
#define FD_DID_SEEK(state) ((state) & FD_STATE_SEEK)
264

    
265
typedef struct fdctrl_t {
266
    /* Controler's identification */
267
    uint8_t version;
268
    /* HW */
269
    int irq_lvl;
270
    int dma_chann;
271
    /* Controler state */
272
    uint8_t state;
273
    uint8_t dma_en;
274
    uint8_t cur_drv;
275
    uint8_t bootsel;
276
    /* Command FIFO */
277
    uint8_t fifo[FD_SECTOR_LEN];
278
    uint32_t data_pos;
279
    uint32_t data_len;
280
    uint8_t data_state;
281
    uint8_t data_dir;
282
    uint8_t int_status;
283
    /* States kept only to be returned back */
284
    /* Timers state */
285
    uint8_t timer0;
286
    uint8_t timer1;
287
    /* precompensation */
288
    uint8_t precomp_trk;
289
    uint8_t config;
290
    uint8_t lock;
291
    /* Power down config (also with status regB access mode */
292
    uint8_t pwrd;
293
    /* Floppy drives */
294
    fdrive_t drives[2];
295
} fdctrl_t;
296

    
297
static fdctrl_t fdctrl;
298

    
299
void fdctrl_init (int irq_lvl, int dma_chann, int mem_mapped, uint32_t base,
300
                  char boot_device)
301
{
302
//    int io_mem;
303
    int i;
304

    
305
    FLOPPY_DPRINTF("init controler\n");
306
    memset(&fdctrl, 0, sizeof(fdctrl));
307
    fdctrl.version = 0x90; /* Intel 82078 controler */
308
    fdctrl.irq_lvl = irq_lvl;
309
    fdctrl.dma_chann = dma_chann;
310
    fdctrl.config = 0x40; /* Implicit seek, polling & FIFO enabled */
311
    if (fdctrl.dma_chann != -1) {
312
        fdctrl.dma_en = 1;
313
        DMA_register_channel(dma_chann, &fdctrl_transfer_handler,
314
                             &fdctrl_misc_handler);
315
    } else {
316
        fdctrl.dma_en = 0;
317
    }
318
    for (i = 0; i < MAX_FD; i++)
319
        fd_init(&fdctrl.drives[i]);
320
    fdctrl_reset(0);
321
    fdctrl.state = FD_CTRL_ACTIVE;
322
    if (mem_mapped) {
323
        FLOPPY_ERROR("memory mapped floppy not supported by now !\n");
324
#if 0
325
        io_mem = cpu_register_io_memory(0, fdctrl_mem_read, fdctrl_mem_write);
326
        cpu_register_physical_memory(base, 0x08, io_mem);
327
#endif
328
    } else {
329
        register_ioport_read(base + 0x01, 1, fdctrl_read_statusB, 1);
330
        register_ioport_read(base + 0x02, 1, fdctrl_read_dor, 1);
331
        register_ioport_write(base + 0x02, 1, fdctrl_write_dor, 1);
332
        register_ioport_read(base + 0x03, 1, fdctrl_read_tape, 1);
333
        register_ioport_write(base + 0x03, 1, fdctrl_write_tape, 1);
334
        register_ioport_read(base + 0x04, 1, fdctrl_read_main_status, 1);
335
        register_ioport_write(base + 0x04, 1, fdctrl_write_rate, 1);
336
        register_ioport_read(base + 0x05, 1, fdctrl_read_data, 1);
337
        register_ioport_write(base + 0x05, 1, fdctrl_write_data, 1);
338
        register_ioport_read(base + 0x07, 1, fdctrl_read_dir, 1);
339
    }
340
    if (boot_device == 'b')
341
        fdctrl.bootsel = 1;
342
    else
343
        fdctrl.bootsel = 0;
344
#if defined (TARGET_I386)
345
    cmos_register_fd(fdctrl.drives[0].drive, fdctrl.drives[1].drive);
346
#endif
347
}
348

    
349
int fdctrl_disk_change (int idx, const unsigned char *filename, int ro)
350
{
351
    fdrive_t *drv;
352
    
353
    if (idx < 0 || idx > 1)
354
        return -1;
355
    FLOPPY_DPRINTF("disk %d change: %s (%s)\n", idx, filename,
356
                   ro == 0 ? "rw" : "ro");
357
    drv = &fdctrl.drives[idx];
358
    if (fd_table[idx] != NULL) {
359
        bdrv_close(fd_table[idx]);
360
        fd_table[idx] = NULL;
361
    }
362
    fd_table[idx] = bdrv_open(filename, ro);
363
    drv->bs = fd_table[idx];
364
    if (fd_table[idx] == NULL)
365
        return -1;
366
    fd_revalidate(drv, ro);
367
#if 0
368
    fd_recalibrate(drv);
369
    fdctrl_reset_fifo();
370
    fdctrl_raise_irq(0x20);
371
#endif
372

    
373
    return 0;
374
}
375

    
376
/* Change IRQ state */
377
static void fdctrl_reset_irq (void)
378
{
379
    if (fdctrl.state & FD_CTRL_INTR) {
380
        pic_set_irq(fdctrl.irq_lvl, 0);
381
        fdctrl.state &= ~(FD_CTRL_INTR | FD_CTRL_SLEEP | FD_CTRL_BUSY);
382
    }
383
}
384

    
385
static void fdctrl_raise_irq (uint8_t status)
386
{
387
    if (~(fdctrl.state & FD_CTRL_INTR)) {
388
        pic_set_irq(fdctrl.irq_lvl, 1);
389
        fdctrl.state |= FD_CTRL_INTR;
390
    }
391
    FLOPPY_DPRINTF("Set interrupt status to 0x%02x\n", status);
392
    fdctrl.int_status = status;
393
}
394

    
395
/* Reset controler */
396
static void fdctrl_reset (int do_irq)
397
{
398
    int i;
399

    
400
    FLOPPY_DPRINTF("reset controler\n");
401
    fdctrl_reset_irq();
402
    /* Initialise controler */
403
    fdctrl.cur_drv = 0;
404
    /* FIFO state */
405
    fdctrl.data_pos = 0;
406
    fdctrl.data_len = 0;
407
    fdctrl.data_state = FD_STATE_CMD;
408
    fdctrl.data_dir = FD_DIR_WRITE;
409
    for (i = 0; i < MAX_FD; i++)
410
        fd_reset(&fdctrl.drives[i]);
411
    fdctrl_reset_fifo();
412
    if (do_irq)
413
        fdctrl_raise_irq(0x20);
414
}
415

    
416
/* Status B register : 0x01 (read-only) */
417
static uint32_t fdctrl_read_statusB (CPUState *env, uint32_t reg)
418
{
419
    fdctrl_reset_irq();
420
    FLOPPY_DPRINTF("status register: 0x00\n");
421

    
422
    return 0;
423
}
424

    
425
/* Digital output register : 0x02 */
426
static uint32_t fdctrl_read_dor (CPUState *env, uint32_t reg)
427
{
428
    fdrive_t *cur_drv, *drv0, *drv1;
429
    uint32_t retval = 0;
430

    
431
    drv0 = &fdctrl.drives[fdctrl.bootsel];
432
    drv1 = &fdctrl.drives[1 - fdctrl.bootsel];
433
    cur_drv = fdctrl.cur_drv == 0 ? drv0 : drv1;
434
    /* Drive motors state indicators */
435
    retval |= drv1->motor << 5;
436
    retval |= drv0->motor << 4;
437
    /* DMA enable */
438
    retval |= fdctrl.dma_en << 3;
439
    /* Reset indicator */
440
    retval |= (fdctrl.state & FD_CTRL_RESET) == 0 ? 0x04 : 0;
441
    /* Selected drive */
442
    retval |= fdctrl.cur_drv;
443
    FLOPPY_DPRINTF("digital output register: 0x%02x\n", retval);
444

    
445
    return retval;
446
}
447

    
448
static void fdctrl_write_dor (CPUState *env, uint32_t reg, uint32_t value)
449
{
450
    fdrive_t *drv0, *drv1;
451
    
452
    fdctrl_reset_irq();
453
    drv0 = &fdctrl.drives[fdctrl.bootsel];
454
    drv1 = &fdctrl.drives[1 - fdctrl.bootsel];
455
    /* Reset mode */
456
    if (fdctrl.state & FD_CTRL_RESET) {
457
        if (!(value & 0x04)) {
458
            FLOPPY_DPRINTF("Floppy controler in RESET state !\n");
459
            return;
460
        }
461
    }
462
    FLOPPY_DPRINTF("digital output register set to 0x%02x\n", value);
463
    /* Drive motors state indicators */
464
    if (value & 0x20)
465
        fd_start(drv1);
466
    else
467
        fd_stop(drv1);
468
    if (value & 0x10)
469
        fd_start(drv0);
470
    else
471
        fd_stop(drv0);
472
    /* DMA enable */
473
#if 0
474
    if (fdctrl.dma_chann != -1)
475
        fdctrl.dma_en = 1 - ((value >> 3) & 1);
476
#endif
477
    /* Reset */
478
    if (!(value & 0x04)) {
479
        if (!(fdctrl.state & FD_CTRL_RESET)) {
480
            FLOPPY_DPRINTF("controler enter RESET state\n");
481
            fdctrl.state |= FD_CTRL_RESET;
482
            fdctrl_reset(1);
483
        }
484
    } else {
485
        if (fdctrl.state & FD_CTRL_RESET) {
486
            FLOPPY_DPRINTF("controler out of RESET state\n");
487
            fdctrl.state &= ~(FD_CTRL_RESET | FD_CTRL_SLEEP);
488
        }
489
    }
490
    /* Selected drive */
491
    fdctrl.cur_drv = value & 1;
492
}
493

    
494
/* Tape drive register : 0x03 */
495
static uint32_t fdctrl_read_tape (CPUState *env, uint32_t reg)
496
{
497
    uint32_t retval = 0;
498

    
499
    fdctrl_reset_irq();
500
    /* Disk boot selection indicator */
501
    retval |= fdctrl.bootsel << 2;
502
    /* Tape indicators: never allowed */
503
    FLOPPY_DPRINTF("tape drive register: 0x%02x\n", retval);
504

    
505
    return retval;
506
}
507

    
508
static void fdctrl_write_tape (CPUState *env, uint32_t reg, uint32_t value)
509
{
510
    fdctrl_reset_irq();
511
    /* Reset mode */
512
    if (fdctrl.state & FD_CTRL_RESET) {
513
        FLOPPY_DPRINTF("Floppy controler in RESET state !\n");
514
        return;
515
    }
516
    FLOPPY_DPRINTF("tape drive register set to 0x%02x\n", value);
517
    /* Disk boot selection indicator */
518
    fdctrl.bootsel = (value >> 2) & 1;
519
    /* Tape indicators: never allow */
520
}
521

    
522
/* Main status register : 0x04 (read) */
523
static uint32_t fdctrl_read_main_status (CPUState *env, uint32_t reg)
524
{
525
    uint32_t retval = 0;
526

    
527
    fdctrl_reset_irq();
528
    fdctrl.state &= ~(FD_CTRL_SLEEP | FD_CTRL_RESET);
529
    if (!(fdctrl.state & FD_CTRL_BUSY)) {
530
        /* Data transfer allowed */
531
        retval |= 0x80;
532
        /* Data transfer direction indicator */
533
        if (fdctrl.data_dir == FD_DIR_READ)
534
            retval |= 0x40;
535
    }
536
    /* Should handle 0x20 for SPECIFY command */
537
    /* Command busy indicator */
538
    if (FD_STATE(fdctrl.data_state) == FD_STATE_DATA ||
539
        FD_STATE(fdctrl.data_state) == FD_STATE_STATUS)
540
        retval |= 0x10;
541
    FLOPPY_DPRINTF("main status register: 0x%02x\n", retval);
542

    
543
    return retval;
544
}
545

    
546
/* Data select rate register : 0x04 (write) */
547
static void fdctrl_write_rate (CPUState *env, uint32_t reg, uint32_t value)
548
{
549
    fdctrl_reset_irq();
550
    /* Reset mode */
551
    if (fdctrl.state & FD_CTRL_RESET) {
552
        if (reg != 0x2 || !(value & 0x04)) {
553
            FLOPPY_DPRINTF("Floppy controler in RESET state !\n");
554
            return;
555
        }
556
    }
557
    FLOPPY_DPRINTF("select rate register set to 0x%02x\n", value);
558
    /* Reset: autoclear */
559
    if (value & 0x80) {
560
        fdctrl.state |= FD_CTRL_RESET;
561
        fdctrl_reset(1);
562
        fdctrl.state &= ~FD_CTRL_RESET;
563
    }
564
    if (value & 0x40) {
565
        fdctrl.state |= FD_CTRL_SLEEP;
566
        fdctrl_reset(1);
567
    }
568
//        fdctrl.precomp = (value >> 2) & 0x07;
569
}
570

    
571
/* Digital input register : 0x07 (read-only) */
572
static uint32_t fdctrl_read_dir (CPUState *env, uint32_t reg)
573
{
574
    fdrive_t *drv0, *drv1;
575
    uint32_t retval = 0;
576

    
577
    fdctrl_reset_irq();
578
    drv0 = &fdctrl.drives[fdctrl.bootsel];
579
    drv1 = &fdctrl.drives[1 - fdctrl.bootsel];
580
    if (drv0->rv || drv1->rv)
581
        retval |= 0x80;
582
    if (retval != 0)
583
        FLOPPY_ERROR("Floppy digital input register: 0x%02x\n", retval);
584
    drv0->rv = 0;
585
    drv1->rv = 0;
586

    
587
    return retval;
588
}
589

    
590
/* FIFO state control */
591
static void fdctrl_reset_fifo (void)
592
{
593
    fdctrl.data_dir = FD_DIR_WRITE;
594
    fdctrl.data_pos = 0;
595
    fdctrl.data_state = FD_STATE_CMD;
596
}
597

    
598
/* Set FIFO status for the host to read */
599
static void fdctrl_set_fifo (int fifo_len, int do_irq)
600
{
601
    fdctrl.data_dir = FD_DIR_READ;
602
    fdctrl.data_len = fifo_len;
603
    fdctrl.data_pos = 0;
604
    fdctrl.data_state = FD_STATE_STATUS;
605
    if (do_irq)
606
        fdctrl_raise_irq(0x00);
607
}
608

    
609
/* Set an error: unimplemented/unknown command */
610
static void fdctrl_unimplemented (void)
611
{
612
#if 0
613
    fdrive_t *cur_drv, *drv0, *drv1;
614

615
    drv0 = &fdctrl.drives[fdctrl.bootsel];
616
    drv1 = &fdctrl.drives[1 - fdctrl.bootsel];
617
    cur_drv = fdctrl.cur_drv == 0 ? drv0 : drv1;
618
    fdctrl.fifo[0] = 0x60 | (cur_drv->head << 1) | fdctrl.cur_drv;
619
    fdctrl.fifo[1] = 0x00;
620
    fdctrl.fifo[2] = 0x00;
621
    fdctrl_set_fifo(3, 1);
622
#else
623
    fdctrl_reset_fifo();
624
#endif
625
}
626

    
627
/* Callback for transfer end (stop or abort) */
628
static void fdctrl_stop_transfer (uint8_t status0, uint8_t status1,
629
                                  uint8_t status2)
630
{
631
    fdrive_t *cur_drv, *drv0, *drv1;
632

    
633
    drv0 = &fdctrl.drives[fdctrl.bootsel];
634
    drv1 = &fdctrl.drives[1 - fdctrl.bootsel];
635
    cur_drv = fdctrl.cur_drv == 0 ? drv0 : drv1;
636
    FLOPPY_DPRINTF("transfer status: %02x %02x %02x (%02x)\n",
637
                   status0, status1, status2,
638
                   status0 | (cur_drv->head << 1) | fdctrl.cur_drv);
639
    fdctrl.fifo[0] = status0 | (cur_drv->head << 1) | fdctrl.cur_drv;
640
    fdctrl.fifo[1] = status1;
641
    fdctrl.fifo[2] = status2;
642
    fdctrl.fifo[3] = cur_drv->track;
643
    fdctrl.fifo[4] = cur_drv->head;
644
    fdctrl.fifo[5] = cur_drv->sect;
645
    fdctrl.fifo[6] = FD_SECTOR_SC;
646
    fdctrl.data_dir = FD_DIR_READ;
647
    if (fdctrl.state & FD_CTRL_BUSY)
648
        DMA_release_DREQ(fdctrl.dma_chann);
649
    fdctrl_set_fifo(7, 1);
650
}
651

    
652
/* Prepare a data transfer (either DMA or FIFO) */
653
static void fdctrl_start_transfer (int direction)
654
{
655
    fdrive_t *cur_drv, *drv0, *drv1;
656
    uint8_t kh, kt, ks;
657
    int did_seek;
658

    
659
    drv0 = &fdctrl.drives[fdctrl.bootsel];
660
    drv1 = &fdctrl.drives[1 - fdctrl.bootsel];
661
    fdctrl.cur_drv = fdctrl.fifo[1] & 1;
662
    cur_drv = fdctrl.cur_drv == 0 ? drv0 : drv1;
663
    kt = fdctrl.fifo[2];
664
    kh = fdctrl.fifo[3];
665
    ks = fdctrl.fifo[4];
666
    FLOPPY_DPRINTF("Start tranfert at %d %d %02x %02x (%d)\n",
667
                   fdctrl.cur_drv, kh, kt, ks,
668
                   _fd_sector(kh, kt, ks, cur_drv->last_sect));
669
    did_seek = 0;
670
    switch (fd_seek(cur_drv, kh, kt, ks, fdctrl.config & 0x40)) {
671
    case 2:
672
        /* sect too big */
673
        fdctrl_stop_transfer(0x40, 0x00, 0x00);
674
        fdctrl.fifo[3] = kt;
675
        fdctrl.fifo[4] = kh;
676
        fdctrl.fifo[5] = ks;
677
        return;
678
    case 3:
679
        /* track too big */
680
        fdctrl_stop_transfer(0x40, 0x80, 0x00);
681
        fdctrl.fifo[3] = kt;
682
        fdctrl.fifo[4] = kh;
683
        fdctrl.fifo[5] = ks;
684
        return;
685
    case 4:
686
        /* No seek enabled */
687
        fdctrl_stop_transfer(0x40, 0x00, 0x00);
688
        fdctrl.fifo[3] = kt;
689
        fdctrl.fifo[4] = kh;
690
        fdctrl.fifo[5] = ks;
691
        return;
692
    case 1:
693
        did_seek = 1;
694
        break;
695
    default:
696
        break;
697
    }
698
    /* Set the FIFO state */
699
    fdctrl.data_dir = direction;
700
    fdctrl.data_pos = 0;
701
    fdctrl.data_state = FD_STATE_DATA; /* FIFO ready for data */
702
    if (fdctrl.fifo[0] & 0x80)
703
        fdctrl.data_state |= FD_STATE_MULTI;
704
    if (did_seek)
705
        fdctrl.data_state |= FD_STATE_SEEK;
706
    if (fdctrl.dma_en) {
707
        int dma_mode;
708
        /* DMA transfer are enabled. Check if DMA channel is well programmed */
709
        dma_mode = DMA_get_channel_mode(fdctrl.dma_chann);
710
        dma_mode = (dma_mode >> 2) & 3;
711
        FLOPPY_DPRINTF("dma_mode=%d direction=%d (%d)\n", dma_mode, direction,
712
                       (128 << fdctrl.fifo[5]) *
713
                       (cur_drv->last_sect - ks + 1));
714
        if (((direction == FD_DIR_SCANE || direction == FD_DIR_SCANL ||
715
              direction == FD_DIR_SCANH) && dma_mode == 0) ||
716
            (direction == FD_DIR_WRITE && dma_mode == 2) ||
717
            (direction == FD_DIR_READ && dma_mode == 1)) {
718
            /* No access is allowed until DMA transfer has completed */
719
            fdctrl.state |= FD_CTRL_BUSY;
720
            /* Now, we just have to wait for the DMA controler to
721
             * recall us...
722
             */
723
            DMA_hold_DREQ(fdctrl.dma_chann);
724
            return;
725
        }
726
    }
727
    FLOPPY_DPRINTF("start non-DMA transfer\n");
728
    /* IO based transfer: calculate len */
729
    if (fdctrl.fifo[5] == 00) {
730
        fdctrl.data_len = fdctrl.fifo[8];
731
    } else {
732
        fdctrl.data_len = 128 << fdctrl.fifo[5];
733
        fdctrl.data_len *= (cur_drv->last_sect - ks + 1);
734
        if (fdctrl.fifo[0] & 0x80)
735
            fdctrl.data_len *= 2;
736
    }
737
    fdctrl_raise_irq(0x00);
738

    
739
    return;
740
}
741

    
742
/* Prepare a transfer of deleted data */
743
static void fdctrl_start_transfer_del (int direction)
744
{
745
    /* We don't handle deleted data,
746
     * so we don't return *ANYTHING*
747
     */
748
    fdctrl_stop_transfer(0x60, 0x00, 0x00);
749
}
750

    
751
/* handlers for DMA transfers */
752
static int fdctrl_transfer_handler (uint32_t addr, int size, int *irq)
753
{
754
    fdrive_t *cur_drv, *drv0, *drv1;
755
    void *orig;
756
    int len;
757
    uint8_t status0 = 0x00, status1 = 0x00, status2 = 0x00;
758

    
759
    fdctrl_reset_irq();
760
    if (!(fdctrl.state & FD_CTRL_BUSY)) {
761
        FLOPPY_DPRINTF("Not in DMA transfer mode !\n");
762
        return 0;
763
    }
764
    drv0 = &fdctrl.drives[fdctrl.bootsel];
765
    drv1 = &fdctrl.drives[1 - fdctrl.bootsel];
766
    cur_drv = fdctrl.cur_drv == 0 ? drv0 : drv1;
767
//    *irq = fdctrl.irq_lvl;
768
    *irq = -1;
769
    if (fdctrl.data_dir == FD_DIR_SCANE || fdctrl.data_dir == FD_DIR_SCANL ||
770
        fdctrl.data_dir == FD_DIR_SCANH)
771
        status2 = 0x04;
772
    for (fdctrl.data_len = size; fdctrl.data_pos < fdctrl.data_len;
773
         fdctrl.data_pos += len) {
774
        len = size - fdctrl.data_pos;
775
        if (len > FD_SECTOR_LEN)
776
            len = FD_SECTOR_LEN;
777
        FLOPPY_DPRINTF("copy %d bytes (%d %d %d) %d pos %d %02x %02x "
778
                       "(%d-0x%08x)\n", len, size, fdctrl.data_pos,
779
                       fdctrl.data_len, fdctrl.cur_drv, cur_drv->head,
780
                       cur_drv->track, cur_drv->sect, fd_sector(cur_drv),
781
                       fd_sector(cur_drv) * 512);
782
        if (len < FD_SECTOR_LEN) {
783
            memset(&fdctrl.fifo[FD_SECTOR_LEN - len], 0,
784
                   FD_SECTOR_LEN - len - 1);
785
            orig = fdctrl.fifo;
786
        } else {
787
            orig = (void *)(addr + fdctrl.data_pos);
788
        }
789
        if (fdctrl.data_dir != FD_DIR_WRITE) {
790
            /* READ & SCAN commands */
791
            if (cur_drv->bs == NULL) {
792
                fdctrl_stop_transfer(0x40, 0x00, 0x00);
793
                goto transfer_error;
794
            }
795
                
796
            if (bdrv_read(cur_drv->bs, fd_sector(cur_drv), orig, 1) < 0) {
797
                FLOPPY_DPRINTF("Floppy: error getting sector %d\n",
798
                               fd_sector(cur_drv));
799
                /* Sure, image size is too small... */
800
                memset((void *)(addr + fdctrl.data_pos), 0, FD_SECTOR_LEN);
801
            }
802
            if (fdctrl.data_dir == FD_DIR_READ) {
803
                if (len < FD_SECTOR_LEN) {
804
                    memcpy((void *)(addr + fdctrl.data_pos),
805
                           fdctrl.fifo, FD_SECTOR_LEN);
806
                }
807
            } else {
808
                int ret;
809
                ret = memcmp((void *)(addr + fdctrl.data_pos),
810
                             fdctrl.fifo, FD_SECTOR_LEN);
811
                if (ret == 0) {
812
                    status2 = 0x08;
813
                    goto end_transfer;
814
                }
815
                if ((ret < 0 && fdctrl.data_dir == FD_DIR_SCANL) ||
816
                    (ret > 0 && fdctrl.data_dir == FD_DIR_SCANH)) {
817
                    status2 = 0x00;
818
                    goto end_transfer;
819
                }
820
            }
821
        } else {
822
            /* WRITE commands */
823
            if (cur_drv->bs == NULL ||
824
                bdrv_write(cur_drv->bs, fd_sector(cur_drv), orig, 1) < 0) {
825
                FLOPPY_ERROR("writting sector %d\n", fd_sector(cur_drv));
826
                fdctrl_stop_transfer(0x60, 0x00, 0x00);
827
                goto transfer_error;
828
            }
829
        }
830
        if (len == FD_SECTOR_LEN) {
831
            /* Seek to next sector */
832
            if (cur_drv->sect == cur_drv->last_sect) {
833
                if (cur_drv->head == 0) {
834
                    cur_drv->head = 1;
835
                } else {
836
                    cur_drv->track++;
837
                    cur_drv->head = 0;
838
                }
839
                cur_drv->sect = 1;
840
                FLOPPY_DPRINTF("seek to next sector (%d %02x %02x => %d)\n",
841
                               cur_drv->head, cur_drv->track, cur_drv->sect,
842
                               fd_sector(cur_drv));
843
                if (cur_drv->head == 0) {
844
                    FLOPPY_DPRINTF("end transfer\n");
845
                    goto end_transfer;
846
                }
847
                if (!FD_MULTI_TRACK(fdctrl.data_state)) {
848
                    /* Single track read */
849
                    FLOPPY_DPRINTF("single track transfert: end transfer\n");
850
//                    status1 |= 0x80;
851
                    goto end_transfer;
852
                }
853
            } else {
854
                cur_drv->sect++;
855
                FLOPPY_DPRINTF("seek to next sector (%d %02x %02x => %d)\n",
856
                               cur_drv->head, cur_drv->track, cur_drv->sect,
857
                               fd_sector(cur_drv));
858
            }
859
        }
860
    }
861
end_transfer:
862
    if (fdctrl.data_dir == FD_DIR_SCANE ||
863
        fdctrl.data_dir == FD_DIR_SCANL ||
864
        fdctrl.data_dir == FD_DIR_SCANH)
865
        status2 = 0x08;
866
    if (FD_DID_SEEK(fdctrl.data_state))
867
        status0 |= 0x20;
868
    fdctrl_stop_transfer(status0, status1, status2);
869
transfer_error:
870

    
871
    return fdctrl.data_pos;
872
}
873

    
874
/* Unused... */
875
static int fdctrl_misc_handler (int duknwo)
876
{
877
    return -1;
878
}
879

    
880
/* Data register : 0x05 */
881
static uint32_t fdctrl_read_data (CPUState *env, uint32_t reg)
882
{
883
    fdrive_t *cur_drv, *drv0, *drv1;
884
    uint32_t retval = 0;
885
    int pos, len;
886

    
887
    fdctrl_reset_irq();
888
    drv0 = &fdctrl.drives[fdctrl.bootsel];
889
    drv1 = &fdctrl.drives[1 - fdctrl.bootsel];
890
    cur_drv = fdctrl.cur_drv == 0 ? drv0 : drv1;
891
    fdctrl.state &= ~FD_CTRL_SLEEP;
892
    if (FD_STATE(fdctrl.data_state) == FD_STATE_CMD) {
893
        FLOPPY_ERROR("can't read data in CMD state\n");
894
        return 0;
895
    }
896
    pos = fdctrl.data_pos;
897
    if (FD_STATE(fdctrl.data_state) == FD_STATE_DATA) {
898
        pos %= FD_SECTOR_LEN;
899
        if (pos == 0) {
900
            len = fdctrl.data_len - fdctrl.data_pos;
901
            if (len > FD_SECTOR_LEN)
902
                len = FD_SECTOR_LEN;
903
            bdrv_read(cur_drv->bs, fd_sector(cur_drv),
904
                      fdctrl.fifo, len);
905
        }
906
    }
907
    retval = fdctrl.fifo[pos];
908
    if (++fdctrl.data_pos == fdctrl.data_len) {
909
        fdctrl.data_pos = 0;
910
        /* Switch from transfert mode to status mode
911
         * then from status mode to command mode
912
         */
913
        if (FD_STATE(fdctrl.data_state) == FD_STATE_DATA)
914
            fdctrl_stop_transfer(0x20, 0x00, 0x00);
915
        else
916
            fdctrl_reset_fifo();
917
    }
918
    FLOPPY_DPRINTF("data register: 0x%02x\n", retval);
919

    
920
    return retval;
921
}
922

    
923
static void fdctrl_write_data (CPUState *env, uint32_t reg, uint32_t value)
924
{
925
    fdrive_t *cur_drv, *drv0, *drv1;
926

    
927
    fdctrl_reset_irq();
928
    drv0 = &fdctrl.drives[fdctrl.bootsel];
929
    drv1 = &fdctrl.drives[1 - fdctrl.bootsel];
930
    cur_drv = fdctrl.cur_drv == 0 ? drv0 : drv1;
931
    /* Reset mode */
932
    if (fdctrl.state & FD_CTRL_RESET) {
933
        FLOPPY_DPRINTF("Floppy controler in RESET state !\n");
934
        return;
935
    }
936
    fdctrl.state &= ~FD_CTRL_SLEEP;
937
    if ((fdctrl.data_state & FD_STATE_STATE) == FD_STATE_STATUS) {
938
        FLOPPY_ERROR("can't write data in status mode\n");
939
        return;
940
    }
941
    /* Is it write command time ? */
942
    if (FD_STATE(fdctrl.data_state) == FD_STATE_DATA) {
943
        /* FIFO data write */
944
        fdctrl.fifo[fdctrl.data_pos++] = value;
945
        if (fdctrl.data_pos % FD_SECTOR_LEN == (FD_SECTOR_LEN - 1) ||
946
            fdctrl.data_pos == fdctrl.data_len) {
947
            bdrv_write(cur_drv->bs, fd_sector(cur_drv),
948
                       fdctrl.fifo, FD_SECTOR_LEN);
949
        }
950
        /* Switch from transfert mode to status mode
951
         * then from status mode to command mode
952
         */
953
        if (FD_STATE(fdctrl.data_state) == FD_STATE_DATA)
954
            fdctrl_stop_transfer(0x20, 0x00, 0x00);
955
        return;
956
    }
957
    if (fdctrl.data_pos == 0) {
958
        /* Command */
959
        switch (value & 0x5F) {
960
        case 0x46:
961
            /* READ variants */
962
            FLOPPY_DPRINTF("READ command\n");
963
            /* 8 parameters cmd */
964
            fdctrl.data_len = 9;
965
            goto enqueue;
966
        case 0x4C:
967
            /* READ_DELETED variants */
968
            FLOPPY_DPRINTF("READ_DELETED command\n");
969
            /* 8 parameters cmd */
970
            fdctrl.data_len = 9;
971
            goto enqueue;
972
        case 0x50:
973
            /* SCAN_EQUAL variants */
974
            FLOPPY_DPRINTF("SCAN_EQUAL command\n");
975
            /* 8 parameters cmd */
976
            fdctrl.data_len = 9;
977
            goto enqueue;
978
        case 0x56:
979
            /* VERIFY variants */
980
            FLOPPY_DPRINTF("VERIFY command\n");
981
            /* 8 parameters cmd */
982
            fdctrl.data_len = 9;
983
            goto enqueue;
984
        case 0x59:
985
            /* SCAN_LOW_OR_EQUAL variants */
986
            FLOPPY_DPRINTF("SCAN_LOW_OR_EQUAL command\n");
987
            /* 8 parameters cmd */
988
            fdctrl.data_len = 9;
989
            goto enqueue;
990
        case 0x5D:
991
            /* SCAN_HIGH_OR_EQUAL variants */
992
            FLOPPY_DPRINTF("SCAN_HIGH_OR_EQUAL command\n");
993
            /* 8 parameters cmd */
994
            fdctrl.data_len = 9;
995
            goto enqueue;
996
        default:
997
            break;
998
        }
999
        switch (value & 0x7F) {
1000
        case 0x45:
1001
            /* WRITE variants */
1002
            FLOPPY_DPRINTF("WRITE command\n");
1003
            /* 8 parameters cmd */
1004
            fdctrl.data_len = 9;
1005
            goto enqueue;
1006
        case 0x49:
1007
            /* WRITE_DELETED variants */
1008
            FLOPPY_DPRINTF("WRITE_DELETED command\n");
1009
            /* 8 parameters cmd */
1010
            fdctrl.data_len = 9;
1011
            goto enqueue;
1012
        default:
1013
            break;
1014
        }
1015
        switch (value) {
1016
        case 0x03:
1017
            /* SPECIFY */
1018
            FLOPPY_DPRINTF("SPECIFY command\n");
1019
            /* 1 parameter cmd */
1020
            fdctrl.data_len = 3;
1021
            goto enqueue;
1022
        case 0x04:
1023
            /* SENSE_DRIVE_STATUS */
1024
            FLOPPY_DPRINTF("SENSE_DRIVE_STATUS command\n");
1025
            /* 1 parameter cmd */
1026
            fdctrl.data_len = 2;
1027
            goto enqueue;
1028
        case 0x07:
1029
            /* RECALIBRATE */
1030
            FLOPPY_DPRINTF("RECALIBRATE command\n");
1031
            /* 1 parameter cmd */
1032
            fdctrl.data_len = 2;
1033
            goto enqueue;
1034
        case 0x08:
1035
            /* SENSE_INTERRUPT_STATUS */
1036
            FLOPPY_DPRINTF("SENSE_INTERRUPT_STATUS command (%02x)\n",
1037
                           fdctrl.int_status);
1038
            /* No parameters cmd: returns status if no interrupt */
1039
            fdctrl.fifo[0] =
1040
                fdctrl.int_status | (cur_drv->head << 2) | fdctrl.cur_drv;
1041
            fdctrl.fifo[1] = cur_drv->track;
1042
            fdctrl_set_fifo(2, 0);
1043
            return;
1044
        case 0x0E:
1045
            /* DUMPREG */
1046
            FLOPPY_DPRINTF("DUMPREG command\n");
1047
            /* Drives position */
1048
            fdctrl.fifo[0] = drv0->track;
1049
            fdctrl.fifo[1] = drv1->track;
1050
            fdctrl.fifo[2] = 0;
1051
            fdctrl.fifo[3] = 0;
1052
            /* timers */
1053
            fdctrl.fifo[4] = fdctrl.timer0;
1054
            fdctrl.fifo[5] = (fdctrl.timer1 << 1) | fdctrl.dma_en;
1055
            fdctrl.fifo[6] = cur_drv->last_sect;
1056
            fdctrl.fifo[7] = (fdctrl.lock << 7) |
1057
                    (cur_drv->perpendicular << 2);
1058
            fdctrl.fifo[8] = fdctrl.config;
1059
            fdctrl.fifo[9] = fdctrl.precomp_trk;
1060
            fdctrl_set_fifo(10, 0);
1061
            return;
1062
        case 0x0F:
1063
            /* SEEK */
1064
            FLOPPY_DPRINTF("SEEK command\n");
1065
            /* 2 parameters cmd */
1066
            fdctrl.data_len = 3;
1067
            goto enqueue;
1068
        case 0x10:
1069
            /* VERSION */
1070
            FLOPPY_DPRINTF("VERSION command\n");
1071
            /* No parameters cmd */
1072
            /* Controler's version */
1073
            fdctrl.fifo[0] = fdctrl.version;
1074
            fdctrl_set_fifo(1, 1);
1075
            return;
1076
        case 0x12:
1077
            /* PERPENDICULAR_MODE */
1078
            FLOPPY_DPRINTF("PERPENDICULAR_MODE command\n");
1079
            /* 1 parameter cmd */
1080
            fdctrl.data_len = 2;
1081
            goto enqueue;
1082
        case 0x13:
1083
            /* CONFIGURE */
1084
            FLOPPY_DPRINTF("CONFIGURE command\n");
1085
            /* 3 parameters cmd */
1086
            fdctrl.data_len = 4;
1087
            goto enqueue;
1088
        case 0x14:
1089
            /* UNLOCK */
1090
            FLOPPY_DPRINTF("UNLOCK command\n");
1091
            /* No parameters cmd */
1092
            fdctrl.lock = 0;
1093
            fdctrl.fifo[0] = 0;
1094
            fdctrl_set_fifo(1, 0);
1095
            return;
1096
        case 0x17:
1097
            /* POWERDOWN_MODE */
1098
            FLOPPY_DPRINTF("POWERDOWN_MODE command\n");
1099
            /* 2 parameters cmd */
1100
            fdctrl.data_len = 3;
1101
            goto enqueue;
1102
        case 0x18:
1103
            /* PART_ID */
1104
            FLOPPY_DPRINTF("PART_ID command\n");
1105
            /* No parameters cmd */
1106
            fdctrl.fifo[0] = 0x41; /* Stepping 1 */
1107
            fdctrl_set_fifo(1, 0);
1108
            return;
1109
        case 0x2C:
1110
            /* SAVE */
1111
            FLOPPY_DPRINTF("SAVE command\n");
1112
            /* No parameters cmd */
1113
            fdctrl.fifo[0] = 0;
1114
            fdctrl.fifo[1] = 0;
1115
            /* Drives position */
1116
            fdctrl.fifo[2] = drv0->track;
1117
            fdctrl.fifo[3] = drv1->track;
1118
            fdctrl.fifo[4] = 0;
1119
            fdctrl.fifo[5] = 0;
1120
            /* timers */
1121
            fdctrl.fifo[6] = fdctrl.timer0;
1122
            fdctrl.fifo[7] = fdctrl.timer1;
1123
            fdctrl.fifo[8] = cur_drv->last_sect;
1124
            fdctrl.fifo[9] = (fdctrl.lock << 7) |
1125
                    (cur_drv->perpendicular << 2);
1126
            fdctrl.fifo[10] = fdctrl.config;
1127
            fdctrl.fifo[11] = fdctrl.precomp_trk;
1128
            fdctrl.fifo[12] = fdctrl.pwrd;
1129
            fdctrl.fifo[13] = 0;
1130
            fdctrl.fifo[14] = 0;
1131
            fdctrl_set_fifo(15, 1);
1132
            return;
1133
        case 0x33:
1134
            /* OPTION */
1135
            FLOPPY_DPRINTF("OPTION command\n");
1136
            /* 1 parameter cmd */
1137
            fdctrl.data_len = 2;
1138
            goto enqueue;
1139
        case 0x42:
1140
            /* READ_TRACK */
1141
            FLOPPY_DPRINTF("READ_TRACK command\n");
1142
            /* 8 parameters cmd */
1143
            fdctrl.data_len = 9;
1144
            goto enqueue;
1145
        case 0x4A:
1146
            /* READ_ID */
1147
            FLOPPY_DPRINTF("READ_ID command\n");
1148
            /* 1 parameter cmd */
1149
            fdctrl.data_len = 2;
1150
            goto enqueue;
1151
        case 0x4C:
1152
            /* RESTORE */
1153
            FLOPPY_DPRINTF("RESTORE command\n");
1154
            /* 17 parameters cmd */
1155
            fdctrl.data_len = 18;
1156
            goto enqueue;
1157
        case 0x4D:
1158
            /* FORMAT_TRACK */
1159
            FLOPPY_DPRINTF("FORMAT_TRACK command\n");
1160
            /* 5 parameters cmd */
1161
            fdctrl.data_len = 9;
1162
            goto enqueue;
1163
        case 0x8E:
1164
            /* DRIVE_SPECIFICATION_COMMAND */
1165
            FLOPPY_DPRINTF("DRIVE_SPECIFICATION_COMMAND command\n");
1166
            /* 5 parameters cmd */
1167
            fdctrl.data_len = 6;
1168
            goto enqueue;
1169
        case 0x8F:
1170
            /* RELATIVE_SEEK_OUT */
1171
            FLOPPY_DPRINTF("RELATIVE_SEEK_OUT command\n");
1172
            /* 2 parameters cmd */
1173
            fdctrl.data_len = 3;
1174
            goto enqueue;
1175
        case 0x94:
1176
            /* LOCK */
1177
            FLOPPY_DPRINTF("LOCK command\n");
1178
            /* No parameters cmd */
1179
            fdctrl.lock = 1;
1180
            fdctrl.fifo[0] = 0x10;
1181
            fdctrl_set_fifo(1, 1);
1182
            return;
1183
        case 0xCD:
1184
            /* FORMAT_AND_WRITE */
1185
            FLOPPY_DPRINTF("FORMAT_AND_WRITE command\n");
1186
            /* 10 parameters cmd */
1187
            fdctrl.data_len = 11;
1188
            goto enqueue;
1189
        case 0xCF:
1190
            /* RELATIVE_SEEK_IN */
1191
            FLOPPY_DPRINTF("RELATIVE_SEEK_IN command\n");
1192
            /* 2 parameters cmd */
1193
            fdctrl.data_len = 3;
1194
            goto enqueue;
1195
        default:
1196
            /* Unknown command */
1197
            FLOPPY_ERROR("unknown command: 0x%02x\n", value);
1198
            fdctrl_unimplemented();
1199
            return;
1200
        }
1201
    }
1202
enqueue:
1203
    fdctrl.fifo[fdctrl.data_pos] = value;
1204
    if (++fdctrl.data_pos == fdctrl.data_len) {
1205
        /* We now have all parameters
1206
         * and will be able to treat the command
1207
         */
1208
        switch (fdctrl.fifo[0] & 0x1F) {
1209
        case 0x06:
1210
        {
1211
            /* READ variants */
1212
            FLOPPY_DPRINTF("treat READ command\n");
1213
            fdctrl_start_transfer(FD_DIR_READ);
1214
            return;
1215
        }
1216
        case 0x0C:
1217
            /* READ_DELETED variants */
1218
//            FLOPPY_DPRINTF("treat READ_DELETED command\n");
1219
            FLOPPY_ERROR("treat READ_DELETED command\n");
1220
            fdctrl_start_transfer_del(1);
1221
            return;
1222
        case 0x16:
1223
            /* VERIFY variants */
1224
//            FLOPPY_DPRINTF("treat VERIFY command\n");
1225
            FLOPPY_ERROR("treat VERIFY command\n");
1226
            fdctrl_stop_transfer(0x20, 0x00, 0x00);
1227
            return;
1228
        case 0x10:
1229
            /* SCAN_EQUAL variants */
1230
//            FLOPPY_DPRINTF("treat SCAN_EQUAL command\n");
1231
            FLOPPY_ERROR("treat SCAN_EQUAL command\n");
1232
            fdctrl_start_transfer(FD_DIR_SCANE);
1233
            return;
1234
        case 0x19:
1235
            /* SCAN_LOW_OR_EQUAL variants */
1236
//            FLOPPY_DPRINTF("treat SCAN_LOW_OR_EQUAL command\n");
1237
            FLOPPY_ERROR("treat SCAN_LOW_OR_EQUAL command\n");
1238
            fdctrl_start_transfer(FD_DIR_SCANL);
1239
            return;
1240
        case 0x1D:
1241
            /* SCAN_HIGH_OR_EQUAL variants */
1242
//            FLOPPY_DPRINTF("treat SCAN_HIGH_OR_EQUAL command\n");
1243
            FLOPPY_ERROR("treat SCAN_HIGH_OR_EQUAL command\n");
1244
            fdctrl_start_transfer(FD_DIR_SCANH);
1245
            return;
1246
        default:
1247
            break;
1248
        }
1249
        switch (fdctrl.fifo[0] & 0x3F) {
1250
        case 0x05:
1251
            /* WRITE variants */
1252
            FLOPPY_DPRINTF("treat WRITE command (%02x)\n", fdctrl.fifo[0]);
1253
            fdctrl_start_transfer(FD_DIR_WRITE);
1254
            return;
1255
        case 0x09:
1256
            /* WRITE_DELETED variants */
1257
//            FLOPPY_DPRINTF("treat WRITE_DELETED command\n");
1258
            FLOPPY_ERROR("treat WRITE_DELETED command\n");
1259
            fdctrl_start_transfer_del(FD_DIR_WRITE);
1260
            return;
1261
        default:
1262
            break;
1263
        }
1264
        switch (fdctrl.fifo[0]) {
1265
        case 0x03:
1266
            /* SPECIFY */
1267
            FLOPPY_DPRINTF("treat SPECIFY command\n");
1268
            fdctrl.timer0 = (fdctrl.fifo[1] >> 4) & 0xF;
1269
            fdctrl.timer1 = fdctrl.fifo[1] >> 1;
1270
            /* No result back */
1271
            fdctrl_reset_fifo();
1272
            break;
1273
        case 0x04:
1274
            /* SENSE_DRIVE_STATUS */
1275
            FLOPPY_DPRINTF("treat SENSE_DRIVE_STATUS command\n");
1276
            fdctrl.cur_drv = fdctrl.fifo[1] & 1;
1277
            cur_drv = fdctrl.cur_drv == 0 ? drv0 : drv1;
1278
            cur_drv->head = (fdctrl.fifo[1] >> 2) & 1;
1279
            /* 1 Byte status back */
1280
            fdctrl.fifo[0] = (cur_drv->ro << 6) |
1281
                (cur_drv->track == 0 ? 0x10 : 0x00) |
1282
                fdctrl.cur_drv;
1283
            fdctrl_set_fifo(1, 0);
1284
            break;
1285
        case 0x07:
1286
            /* RECALIBRATE */
1287
            FLOPPY_DPRINTF("treat RECALIBRATE command\n");
1288
            fdctrl.cur_drv = fdctrl.fifo[1] & 1;
1289
            cur_drv = fdctrl.cur_drv == 0 ? drv0 : drv1;
1290
            fd_recalibrate(cur_drv);
1291
            fdctrl_reset_fifo();
1292
            /* Raise Interrupt */
1293
            fdctrl_raise_irq(0x20);
1294
            break;
1295
        case 0x0F:
1296
            /* SEEK */
1297
            FLOPPY_DPRINTF("treat SEEK command\n");
1298
            fdctrl.cur_drv = fdctrl.fifo[1] & 1;
1299
            cur_drv = fdctrl.cur_drv == 0 ? drv0 : drv1;
1300
            if (fdctrl.fifo[2] <= cur_drv->track)
1301
                cur_drv->dir = 1;
1302
            else
1303
                cur_drv->dir = 0;
1304
            cur_drv->head = (fdctrl.fifo[1] >> 2) & 1;
1305
            if (fdctrl.fifo[2] > cur_drv->max_track) {
1306
                fdctrl_raise_irq(0x60);
1307
            } else {
1308
                cur_drv->track = fdctrl.fifo[2];
1309
                fdctrl_reset_fifo();
1310
                /* Raise Interrupt */
1311
                fdctrl_raise_irq(0x20);
1312
            }
1313
            break;
1314
        case 0x12:
1315
            /* PERPENDICULAR_MODE */
1316
            FLOPPY_DPRINTF("treat PERPENDICULAR_MODE command\n");
1317
            if (fdctrl.fifo[1] & 0x80)
1318
                cur_drv->perpendicular = fdctrl.fifo[1] & 0x7;
1319
            /* No result back */
1320
            fdctrl_reset_fifo();
1321
            break;
1322
        case 0x13:
1323
            /* CONFIGURE */
1324
            FLOPPY_DPRINTF("treat CONFIGURE command\n");
1325
            fdctrl.config = fdctrl.fifo[2];
1326
            fdctrl.precomp_trk =  fdctrl.fifo[3];
1327
            /* No result back */
1328
            fdctrl_reset_fifo();
1329
            break;
1330
        case 0x17:
1331
            /* POWERDOWN_MODE */
1332
            FLOPPY_DPRINTF("treat POWERDOWN_MODE command\n");
1333
            fdctrl.pwrd = fdctrl.fifo[1];
1334
            fdctrl.fifo[0] = fdctrl.fifo[1];
1335
            fdctrl_set_fifo(1, 1);
1336
            break;
1337
        case 0x33:
1338
            /* OPTION */
1339
            FLOPPY_DPRINTF("treat OPTION command\n");
1340
            /* No result back */
1341
            fdctrl_reset_fifo();
1342
            break;
1343
        case 0x42:
1344
            /* READ_TRACK */
1345
//            FLOPPY_DPRINTF("treat READ_TRACK command\n");
1346
            FLOPPY_ERROR("treat READ_TRACK command\n");
1347
            fdctrl_unimplemented();
1348
            break;
1349
        case 0x4A:
1350
                /* READ_ID */
1351
//            FLOPPY_DPRINTF("treat READ_ID command\n");
1352
            FLOPPY_ERROR("treat READ_ID command\n");
1353
            fdctrl_stop_transfer(0x00, 0x00, 0x00);
1354
            break;
1355
        case 0x4C:
1356
            /* RESTORE */
1357
            FLOPPY_DPRINTF("treat RESTORE command\n");
1358
            /* Drives position */
1359
            drv0->track = fdctrl.fifo[3];
1360
            drv1->track = fdctrl.fifo[4];
1361
            /* timers */
1362
            fdctrl.timer0 = fdctrl.fifo[7];
1363
            fdctrl.timer1 = fdctrl.fifo[8];
1364
            cur_drv->last_sect = fdctrl.fifo[9];
1365
            fdctrl.lock = fdctrl.fifo[10] >> 7;
1366
            cur_drv->perpendicular = (fdctrl.fifo[10] >> 2) & 0xF;
1367
            fdctrl.config = fdctrl.fifo[11];
1368
            fdctrl.precomp_trk = fdctrl.fifo[12];
1369
            fdctrl.pwrd = fdctrl.fifo[13];
1370
            fdctrl_reset_fifo();
1371
            break;
1372
        case 0x4D:
1373
            /* FORMAT_TRACK */
1374
//                FLOPPY_DPRINTF("treat FORMAT_TRACK command\n");
1375
            FLOPPY_ERROR("treat FORMAT_TRACK command\n");
1376
            fdctrl_unimplemented();
1377
            break;
1378
        case 0x8E:
1379
            /* DRIVE_SPECIFICATION_COMMAND */
1380
            FLOPPY_DPRINTF("treat DRIVE_SPECIFICATION_COMMAND command\n");
1381
            if (fdctrl.fifo[fdctrl.data_pos - 1] & 0x80) {
1382
                /* Command parameters done */
1383
                if (fdctrl.fifo[fdctrl.data_pos - 1] & 0x40) {
1384
                    fdctrl.fifo[0] = fdctrl.fifo[1];
1385
                    fdctrl.fifo[2] = 0;
1386
                    fdctrl.fifo[3] = 0;
1387
                    fdctrl_set_fifo(4, 1);
1388
                } else {
1389
                    fdctrl_reset_fifo();
1390
                }
1391
            } else if (fdctrl.data_len > 7) {
1392
                /* ERROR */
1393
                fdctrl.fifo[0] = 0x80 |
1394
                    (cur_drv->head << 2) | fdctrl.cur_drv;
1395
                fdctrl_set_fifo(1, 1);
1396
            }
1397
            break;
1398
        case 0x8F:
1399
            /* RELATIVE_SEEK_OUT */
1400
            FLOPPY_DPRINTF("treat RELATIVE_SEEK_OUT command\n");
1401
            fdctrl.cur_drv = fdctrl.fifo[1] & 1;
1402
            cur_drv = fdctrl.cur_drv == 0 ? drv0 : drv1;
1403
            cur_drv->head = (fdctrl.fifo[1] >> 2) & 1;
1404
            if (fdctrl.fifo[2] + cur_drv->track > cur_drv->max_track) {
1405
                /* ERROR */
1406
                fdctrl_raise_irq(0x70);
1407
            } else {
1408
                cur_drv->track += fdctrl.fifo[2];
1409
                cur_drv->dir = 0;
1410
                fdctrl_reset_fifo();
1411
                fdctrl_raise_irq(0x20);
1412
            }
1413
            break;
1414
        case 0xCD:
1415
            /* FORMAT_AND_WRITE */
1416
//                FLOPPY_DPRINTF("treat FORMAT_AND_WRITE command\n");
1417
            FLOPPY_ERROR("treat FORMAT_AND_WRITE command\n");
1418
            fdctrl_unimplemented();
1419
            break;
1420
        case 0xCF:
1421
                /* RELATIVE_SEEK_IN */
1422
            FLOPPY_DPRINTF("treat RELATIVE_SEEK_IN command\n");
1423
            fdctrl.cur_drv = fdctrl.fifo[1] & 1;
1424
            cur_drv = fdctrl.cur_drv == 0 ? drv0 : drv1;
1425
            cur_drv->head = (fdctrl.fifo[1] >> 2) & 1;
1426
            if (fdctrl.fifo[2] > cur_drv->track) {
1427
                /* ERROR */
1428
                fdctrl_raise_irq(0x60);
1429
            } else {
1430
                fdctrl_reset_fifo();
1431
                cur_drv->track -= fdctrl.fifo[2];
1432
                cur_drv->dir = 1;
1433
                /* Raise Interrupt */
1434
                fdctrl_raise_irq(0x20);
1435
            }
1436
            break;
1437
        }
1438
    }
1439
}