2 * QEMU Floppy disk emulator (Intel 82078)
4 * Copyright (c) 2003, 2007 Jocelyn Mayer
5 * Copyright (c) 2008 Hervé Poussineau
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * The controller is used in Sun4m systems in a slightly different
27 * way. There are changes in DOR register and DMA is not available.
32 #include "qemu-timer.h"
35 /********************************************************/
36 /* debug Floppy devices */
37 //#define DEBUG_FLOPPY
40 #define FLOPPY_DPRINTF(fmt, args...) \
41 do { printf("FLOPPY: " fmt , ##args); } while (0)
43 #define FLOPPY_DPRINTF(fmt, args...)
46 #define FLOPPY_ERROR(fmt, args...) \
47 do { printf("FLOPPY ERROR: %s: " fmt, __func__ , ##args); } while (0)
49 /********************************************************/
50 /* Floppy drive emulation */
52 /* Will always be a fixed parameter for us */
53 #define FD_SECTOR_LEN 512
54 #define FD_SECTOR_SC 2 /* Sector size code */
56 /* Floppy disk drive emulation */
57 typedef enum fdisk_type_t
{
58 FDRIVE_DISK_288
= 0x01, /* 2.88 MB disk */
59 FDRIVE_DISK_144
= 0x02, /* 1.44 MB disk */
60 FDRIVE_DISK_720
= 0x03, /* 720 kB disk */
61 FDRIVE_DISK_USER
= 0x04, /* User defined geometry */
62 FDRIVE_DISK_NONE
= 0x05, /* No disk */
65 typedef enum fdrive_type_t
{
66 FDRIVE_DRV_144
= 0x00, /* 1.44 MB 3"5 drive */
67 FDRIVE_DRV_288
= 0x01, /* 2.88 MB 3"5 drive */
68 FDRIVE_DRV_120
= 0x02, /* 1.2 MB 5"25 drive */
69 FDRIVE_DRV_NONE
= 0x03, /* No drive connected */
72 typedef enum fdrive_flags_t
{
73 FDRIVE_MOTOR_ON
= 0x01, /* motor on/off */
76 typedef enum fdisk_flags_t
{
77 FDISK_DBL_SIDES
= 0x01,
80 typedef struct fdrive_t
{
84 fdrive_flags_t drflags
;
85 uint8_t perpendicular
; /* 2.88 MB access mode */
90 /* Last operation status */
91 uint8_t dir
; /* Direction */
92 uint8_t rw
; /* Read/write */
95 uint8_t last_sect
; /* Nb sector per track */
96 uint8_t max_track
; /* Nb of tracks */
97 uint16_t bps
; /* Bytes per sector */
98 uint8_t ro
; /* Is read-only */
101 static void fd_init (fdrive_t
*drv
, BlockDriverState
*bs
)
105 drv
->drive
= FDRIVE_DRV_NONE
;
107 drv
->perpendicular
= 0;
113 static int _fd_sector (uint8_t head
, uint8_t track
,
114 uint8_t sect
, uint8_t last_sect
)
116 return (((track
* 2) + head
) * last_sect
) + sect
- 1;
119 /* Returns current position, in sectors, for given drive */
120 static int fd_sector (fdrive_t
*drv
)
122 return _fd_sector(drv
->head
, drv
->track
, drv
->sect
, drv
->last_sect
);
125 static int fd_seek (fdrive_t
*drv
, uint8_t head
, uint8_t track
, uint8_t sect
,
131 if (track
> drv
->max_track
||
132 (head
!= 0 && (drv
->flags
& FDISK_DBL_SIDES
) == 0)) {
133 FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
134 head
, track
, sect
, 1,
135 (drv
->flags
& FDISK_DBL_SIDES
) == 0 ? 0 : 1,
136 drv
->max_track
, drv
->last_sect
);
139 if (sect
> drv
->last_sect
) {
140 FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
141 head
, track
, sect
, 1,
142 (drv
->flags
& FDISK_DBL_SIDES
) == 0 ? 0 : 1,
143 drv
->max_track
, drv
->last_sect
);
146 sector
= _fd_sector(head
, track
, sect
, drv
->last_sect
);
148 if (sector
!= fd_sector(drv
)) {
151 FLOPPY_ERROR("no implicit seek %d %02x %02x (max=%d %02x %02x)\n",
152 head
, track
, sect
, 1, drv
->max_track
, drv
->last_sect
);
157 if (drv
->track
!= track
)
166 /* Set drive back to track 0 */
167 static void fd_recalibrate (fdrive_t
*drv
)
169 FLOPPY_DPRINTF("recalibrate\n");
177 /* Recognize floppy formats */
178 typedef struct fd_format_t
{
187 static const fd_format_t fd_formats
[] = {
188 /* First entry is default format */
189 /* 1.44 MB 3"1/2 floppy disks */
190 { FDRIVE_DRV_144
, FDRIVE_DISK_144
, 18, 80, 1, "1.44 MB 3\"1/2", },
191 { FDRIVE_DRV_144
, FDRIVE_DISK_144
, 20, 80, 1, "1.6 MB 3\"1/2", },
192 { FDRIVE_DRV_144
, FDRIVE_DISK_144
, 21, 80, 1, "1.68 MB 3\"1/2", },
193 { FDRIVE_DRV_144
, FDRIVE_DISK_144
, 21, 82, 1, "1.72 MB 3\"1/2", },
194 { FDRIVE_DRV_144
, FDRIVE_DISK_144
, 21, 83, 1, "1.74 MB 3\"1/2", },
195 { FDRIVE_DRV_144
, FDRIVE_DISK_144
, 22, 80, 1, "1.76 MB 3\"1/2", },
196 { FDRIVE_DRV_144
, FDRIVE_DISK_144
, 23, 80, 1, "1.84 MB 3\"1/2", },
197 { FDRIVE_DRV_144
, FDRIVE_DISK_144
, 24, 80, 1, "1.92 MB 3\"1/2", },
198 /* 2.88 MB 3"1/2 floppy disks */
199 { FDRIVE_DRV_288
, FDRIVE_DISK_288
, 36, 80, 1, "2.88 MB 3\"1/2", },
200 { FDRIVE_DRV_288
, FDRIVE_DISK_288
, 39, 80, 1, "3.12 MB 3\"1/2", },
201 { FDRIVE_DRV_288
, FDRIVE_DISK_288
, 40, 80, 1, "3.2 MB 3\"1/2", },
202 { FDRIVE_DRV_288
, FDRIVE_DISK_288
, 44, 80, 1, "3.52 MB 3\"1/2", },
203 { FDRIVE_DRV_288
, FDRIVE_DISK_288
, 48, 80, 1, "3.84 MB 3\"1/2", },
204 /* 720 kB 3"1/2 floppy disks */
205 { FDRIVE_DRV_144
, FDRIVE_DISK_720
, 9, 80, 1, "720 kB 3\"1/2", },
206 { FDRIVE_DRV_144
, FDRIVE_DISK_720
, 10, 80, 1, "800 kB 3\"1/2", },
207 { FDRIVE_DRV_144
, FDRIVE_DISK_720
, 10, 82, 1, "820 kB 3\"1/2", },
208 { FDRIVE_DRV_144
, FDRIVE_DISK_720
, 10, 83, 1, "830 kB 3\"1/2", },
209 { FDRIVE_DRV_144
, FDRIVE_DISK_720
, 13, 80, 1, "1.04 MB 3\"1/2", },
210 { FDRIVE_DRV_144
, FDRIVE_DISK_720
, 14, 80, 1, "1.12 MB 3\"1/2", },
211 /* 1.2 MB 5"1/4 floppy disks */
212 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 15, 80, 1, "1.2 kB 5\"1/4", },
213 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 18, 80, 1, "1.44 MB 5\"1/4", },
214 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 18, 82, 1, "1.48 MB 5\"1/4", },
215 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 18, 83, 1, "1.49 MB 5\"1/4", },
216 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 20, 80, 1, "1.6 MB 5\"1/4", },
217 /* 720 kB 5"1/4 floppy disks */
218 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 9, 80, 1, "720 kB 5\"1/4", },
219 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 11, 80, 1, "880 kB 5\"1/4", },
220 /* 360 kB 5"1/4 floppy disks */
221 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 9, 40, 1, "360 kB 5\"1/4", },
222 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 9, 40, 0, "180 kB 5\"1/4", },
223 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 10, 41, 1, "410 kB 5\"1/4", },
224 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 10, 42, 1, "420 kB 5\"1/4", },
225 /* 320 kB 5"1/4 floppy disks */
226 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 8, 40, 1, "320 kB 5\"1/4", },
227 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 8, 40, 0, "160 kB 5\"1/4", },
228 /* 360 kB must match 5"1/4 better than 3"1/2... */
229 { FDRIVE_DRV_144
, FDRIVE_DISK_720
, 9, 80, 0, "360 kB 3\"1/2", },
231 { FDRIVE_DRV_NONE
, FDRIVE_DISK_NONE
, -1, -1, 0, NULL
, },
234 /* Revalidate a disk drive after a disk change */
235 static void fd_revalidate (fdrive_t
*drv
)
237 const fd_format_t
*parse
;
238 uint64_t nb_sectors
, size
;
239 int i
, first_match
, match
;
240 int nb_heads
, max_track
, last_sect
, ro
;
242 FLOPPY_DPRINTF("revalidate\n");
243 if (drv
->bs
!= NULL
&& bdrv_is_inserted(drv
->bs
)) {
244 ro
= bdrv_is_read_only(drv
->bs
);
245 bdrv_get_geometry_hint(drv
->bs
, &nb_heads
, &max_track
, &last_sect
);
246 if (nb_heads
!= 0 && max_track
!= 0 && last_sect
!= 0) {
247 FLOPPY_DPRINTF("User defined disk (%d %d %d)",
248 nb_heads
- 1, max_track
, last_sect
);
250 bdrv_get_geometry(drv
->bs
, &nb_sectors
);
254 parse
= &fd_formats
[i
];
255 if (parse
->drive
== FDRIVE_DRV_NONE
)
257 if (drv
->drive
== parse
->drive
||
258 drv
->drive
== FDRIVE_DRV_NONE
) {
259 size
= (parse
->max_head
+ 1) * parse
->max_track
*
261 if (nb_sectors
== size
) {
265 if (first_match
== -1)
270 if (first_match
== -1)
274 parse
= &fd_formats
[match
];
276 nb_heads
= parse
->max_head
+ 1;
277 max_track
= parse
->max_track
;
278 last_sect
= parse
->last_sect
;
279 drv
->drive
= parse
->drive
;
280 FLOPPY_DPRINTF("%s floppy disk (%d h %d t %d s) %s\n", parse
->str
,
281 nb_heads
, max_track
, last_sect
, ro
? "ro" : "rw");
284 drv
->flags
&= ~FDISK_DBL_SIDES
;
286 drv
->flags
|= FDISK_DBL_SIDES
;
288 drv
->max_track
= max_track
;
289 drv
->last_sect
= last_sect
;
292 FLOPPY_DPRINTF("No disk in drive\n");
295 drv
->flags
&= ~FDISK_DBL_SIDES
;
300 static void fd_start (fdrive_t
*drv
)
302 drv
->drflags
|= FDRIVE_MOTOR_ON
;
305 static void fd_stop (fdrive_t
*drv
)
307 drv
->drflags
&= ~FDRIVE_MOTOR_ON
;
310 /* Re-initialise a drives (motor off, repositioned) */
311 static void fd_reset (fdrive_t
*drv
)
317 /********************************************************/
318 /* Intel 82078 floppy disk controller emulation */
320 static void fdctrl_reset (fdctrl_t
*fdctrl
, int do_irq
);
321 static void fdctrl_reset_fifo (fdctrl_t
*fdctrl
);
322 static int fdctrl_transfer_handler (void *opaque
, int nchan
,
323 int dma_pos
, int dma_len
);
324 static void fdctrl_raise_irq (fdctrl_t
*fdctrl
, uint8_t status
);
325 static void fdctrl_result_timer(void *opaque
);
327 static uint32_t fdctrl_read_statusB (fdctrl_t
*fdctrl
);
328 static uint32_t fdctrl_read_dor (fdctrl_t
*fdctrl
);
329 static void fdctrl_write_dor (fdctrl_t
*fdctrl
, uint32_t value
);
330 static uint32_t fdctrl_read_tape (fdctrl_t
*fdctrl
);
331 static void fdctrl_write_tape (fdctrl_t
*fdctrl
, uint32_t value
);
332 static uint32_t fdctrl_read_main_status (fdctrl_t
*fdctrl
);
333 static void fdctrl_write_rate (fdctrl_t
*fdctrl
, uint32_t value
);
334 static uint32_t fdctrl_read_data (fdctrl_t
*fdctrl
);
335 static void fdctrl_write_data (fdctrl_t
*fdctrl
, uint32_t value
);
336 static uint32_t fdctrl_read_dir (fdctrl_t
*fdctrl
);
339 FD_CTRL_ACTIVE
= 0x01, /* XXX: suppress that */
340 FD_CTRL_RESET
= 0x02,
341 FD_CTRL_SLEEP
= 0x04, /* XXX: suppress that */
342 FD_CTRL_BUSY
= 0x08, /* dma transfer in progress */
356 FD_STATE_STATUS
= 0x01,
357 FD_STATE_DATA
= 0x02,
358 FD_STATE_STATE
= 0x03,
359 FD_STATE_MULTI
= 0x10,
360 FD_STATE_SEEK
= 0x20,
361 FD_STATE_FORMAT
= 0x40,
366 FD_REG_STATUSB
= 0x01,
376 FD_CMD_READ_TRACK
= 0x02,
377 FD_CMD_SPECIFY
= 0x03,
378 FD_CMD_SENSE_DRIVE_STATUS
= 0x04,
381 FD_CMD_RECALIBRATE
= 0x07,
382 FD_CMD_SENSE_INTERRUPT_STATUS
= 0x08,
383 FD_CMD_WRITE_DELETED
= 0x09,
384 FD_CMD_READ_ID
= 0x0a,
385 FD_CMD_READ_DELETED
= 0x0c,
386 FD_CMD_FORMAT_TRACK
= 0x0d,
387 FD_CMD_DUMPREG
= 0x0e,
389 FD_CMD_VERSION
= 0x10,
390 FD_CMD_SCAN_EQUAL
= 0x11,
391 FD_CMD_PERPENDICULAR_MODE
= 0x12,
392 FD_CMD_CONFIGURE
= 0x13,
394 FD_CMD_VERIFY
= 0x16,
395 FD_CMD_POWERDOWN_MODE
= 0x17,
396 FD_CMD_PART_ID
= 0x18,
397 FD_CMD_SCAN_LOW_OR_EQUAL
= 0x19,
398 FD_CMD_SCAN_HIGH_OR_EQUAL
= 0x1d,
400 FD_CMD_OPTION
= 0x33,
401 FD_CMD_RESTORE
= 0x4c,
402 FD_CMD_DRIVE_SPECIFICATION_COMMAND
= 0x8e,
403 FD_CMD_RELATIVE_SEEK_OUT
= 0x8f,
404 FD_CMD_FORMAT_AND_WRITE
= 0xcd,
405 FD_CMD_RELATIVE_SEEK_IN
= 0xcf,
409 FD_CONFIG_PRETRK
= 0xff, /* Pre-compensation set to track 0 */
410 FD_CONFIG_FIFOTHR
= 0x0f, /* FIFO threshold set to 1 byte */
411 FD_CONFIG_POLL
= 0x10, /* Poll enabled */
412 FD_CONFIG_EFIFO
= 0x20, /* FIFO disabled */
413 FD_CONFIG_EIS
= 0x40, /* No implied seeks */
419 FD_SR0_ABNTERM
= 0x40,
420 FD_SR0_INVCMD
= 0x80,
421 FD_SR0_RDYCHG
= 0xc0,
425 FD_DOR_SELMASK
= 0x01,
426 FD_DOR_nRESET
= 0x04,
428 FD_DOR_MOTEN0
= 0x10,
429 FD_DOR_MOTEN1
= 0x20,
430 FD_DOR_MOTEN2
= 0x40,
431 FD_DOR_MOTEN3
= 0x80,
435 FD_TDR_BOOTSEL
= 0x0c,
439 FD_DSR_DRATEMASK
= 0x03,
440 FD_DSR_PWRDOWN
= 0x40,
441 FD_DSR_SWRESET
= 0x80,
445 FD_MSR_DRV0BUSY
= 0x01,
446 FD_MSR_DRV1BUSY
= 0x02,
447 FD_MSR_DRV2BUSY
= 0x04,
448 FD_MSR_DRV3BUSY
= 0x08,
449 FD_MSR_CMDBUSY
= 0x10,
450 FD_MSR_NONDMA
= 0x20,
456 FD_DIR_DSKCHG
= 0x80,
459 #define FD_STATE(state) ((state) & FD_STATE_STATE)
460 #define FD_SET_STATE(state, new_state) \
461 do { (state) = ((state) & ~FD_STATE_STATE) | (new_state); } while (0)
462 #define FD_MULTI_TRACK(state) ((state) & FD_STATE_MULTI)
463 #define FD_DID_SEEK(state) ((state) & FD_STATE_SEEK)
464 #define FD_FORMAT_CMD(state) ((state) & FD_STATE_FORMAT)
468 /* Controller's identification */
473 target_phys_addr_t io_base
;
474 /* Controller state */
475 QEMUTimer
*result_timer
;
487 uint8_t eot
; /* last wanted sector */
488 /* States kept only to be returned back */
492 /* precompensation */
496 /* Power down config (also with status regB access mode */
504 static uint32_t fdctrl_read (void *opaque
, uint32_t reg
)
506 fdctrl_t
*fdctrl
= opaque
;
509 switch (reg
& 0x07) {
512 // Identify to Linux as S82078B
513 retval
= fdctrl_read_statusB(fdctrl
);
515 retval
= (uint32_t)(-1);
519 retval
= fdctrl_read_statusB(fdctrl
);
522 retval
= fdctrl_read_dor(fdctrl
);
525 retval
= fdctrl_read_tape(fdctrl
);
528 retval
= fdctrl_read_main_status(fdctrl
);
531 retval
= fdctrl_read_data(fdctrl
);
534 retval
= fdctrl_read_dir(fdctrl
);
537 retval
= (uint32_t)(-1);
540 FLOPPY_DPRINTF("read reg%d: 0x%02x\n", reg
& 7, retval
);
545 static void fdctrl_write (void *opaque
, uint32_t reg
, uint32_t value
)
547 fdctrl_t
*fdctrl
= opaque
;
549 FLOPPY_DPRINTF("write reg%d: 0x%02x\n", reg
& 7, value
);
551 switch (reg
& 0x07) {
553 fdctrl_write_dor(fdctrl
, value
);
556 fdctrl_write_tape(fdctrl
, value
);
559 fdctrl_write_rate(fdctrl
, value
);
562 fdctrl_write_data(fdctrl
, value
);
569 static uint32_t fdctrl_read_mem (void *opaque
, target_phys_addr_t reg
)
571 return fdctrl_read(opaque
, (uint32_t)reg
);
574 static void fdctrl_write_mem (void *opaque
,
575 target_phys_addr_t reg
, uint32_t value
)
577 fdctrl_write(opaque
, (uint32_t)reg
, value
);
580 static CPUReadMemoryFunc
*fdctrl_mem_read
[3] = {
586 static CPUWriteMemoryFunc
*fdctrl_mem_write
[3] = {
592 static CPUReadMemoryFunc
*fdctrl_mem_read_strict
[3] = {
598 static CPUWriteMemoryFunc
*fdctrl_mem_write_strict
[3] = {
604 static void fd_save (QEMUFile
*f
, fdrive_t
*fd
)
609 qemu_put_8s(f
, &tmp
);
610 qemu_put_8s(f
, &fd
->head
);
611 qemu_put_8s(f
, &fd
->track
);
612 qemu_put_8s(f
, &fd
->sect
);
613 qemu_put_8s(f
, &fd
->dir
);
614 qemu_put_8s(f
, &fd
->rw
);
617 static void fdc_save (QEMUFile
*f
, void *opaque
)
619 fdctrl_t
*s
= opaque
;
621 qemu_put_8s(f
, &s
->state
);
622 qemu_put_8s(f
, &s
->dma_en
);
623 qemu_put_8s(f
, &s
->cur_drv
);
624 qemu_put_8s(f
, &s
->bootsel
);
625 qemu_put_buffer(f
, s
->fifo
, FD_SECTOR_LEN
);
626 qemu_put_be32s(f
, &s
->data_pos
);
627 qemu_put_be32s(f
, &s
->data_len
);
628 qemu_put_8s(f
, &s
->data_state
);
629 qemu_put_8s(f
, &s
->data_dir
);
630 qemu_put_8s(f
, &s
->int_status
);
631 qemu_put_8s(f
, &s
->eot
);
632 qemu_put_8s(f
, &s
->timer0
);
633 qemu_put_8s(f
, &s
->timer1
);
634 qemu_put_8s(f
, &s
->precomp_trk
);
635 qemu_put_8s(f
, &s
->config
);
636 qemu_put_8s(f
, &s
->lock
);
637 qemu_put_8s(f
, &s
->pwrd
);
638 fd_save(f
, &s
->drives
[0]);
639 fd_save(f
, &s
->drives
[1]);
642 static int fd_load (QEMUFile
*f
, fdrive_t
*fd
)
646 qemu_get_8s(f
, &tmp
);
648 qemu_get_8s(f
, &fd
->head
);
649 qemu_get_8s(f
, &fd
->track
);
650 qemu_get_8s(f
, &fd
->sect
);
651 qemu_get_8s(f
, &fd
->dir
);
652 qemu_get_8s(f
, &fd
->rw
);
657 static int fdc_load (QEMUFile
*f
, void *opaque
, int version_id
)
659 fdctrl_t
*s
= opaque
;
665 qemu_get_8s(f
, &s
->state
);
666 qemu_get_8s(f
, &s
->dma_en
);
667 qemu_get_8s(f
, &s
->cur_drv
);
668 qemu_get_8s(f
, &s
->bootsel
);
669 qemu_get_buffer(f
, s
->fifo
, FD_SECTOR_LEN
);
670 qemu_get_be32s(f
, &s
->data_pos
);
671 qemu_get_be32s(f
, &s
->data_len
);
672 qemu_get_8s(f
, &s
->data_state
);
673 qemu_get_8s(f
, &s
->data_dir
);
674 qemu_get_8s(f
, &s
->int_status
);
675 qemu_get_8s(f
, &s
->eot
);
676 qemu_get_8s(f
, &s
->timer0
);
677 qemu_get_8s(f
, &s
->timer1
);
678 qemu_get_8s(f
, &s
->precomp_trk
);
679 qemu_get_8s(f
, &s
->config
);
680 qemu_get_8s(f
, &s
->lock
);
681 qemu_get_8s(f
, &s
->pwrd
);
683 ret
= fd_load(f
, &s
->drives
[0]);
685 ret
= fd_load(f
, &s
->drives
[1]);
690 static void fdctrl_external_reset(void *opaque
)
692 fdctrl_t
*s
= opaque
;
697 static fdctrl_t
*fdctrl_init_common (qemu_irq irq
, int dma_chann
,
698 target_phys_addr_t io_base
,
699 BlockDriverState
**fds
)
704 FLOPPY_DPRINTF("init controller\n");
705 fdctrl
= qemu_mallocz(sizeof(fdctrl_t
));
708 fdctrl
->fifo
= qemu_memalign(512, FD_SECTOR_LEN
);
709 if (fdctrl
->fifo
== NULL
) {
713 fdctrl
->result_timer
= qemu_new_timer(vm_clock
,
714 fdctrl_result_timer
, fdctrl
);
716 fdctrl
->version
= 0x90; /* Intel 82078 controller */
718 fdctrl
->dma_chann
= dma_chann
;
719 fdctrl
->io_base
= io_base
;
720 fdctrl
->config
= FD_CONFIG_EIS
| FD_CONFIG_EFIFO
; /* Implicit seek, polling & FIFO enabled */
721 if (fdctrl
->dma_chann
!= -1) {
723 DMA_register_channel(dma_chann
, &fdctrl_transfer_handler
, fdctrl
);
727 for (i
= 0; i
< MAX_FD
; i
++) {
728 fd_init(&fdctrl
->drives
[i
], fds
[i
]);
730 fdctrl_reset(fdctrl
, 0);
731 fdctrl
->state
= FD_CTRL_ACTIVE
;
732 register_savevm("fdc", io_base
, 1, fdc_save
, fdc_load
, fdctrl
);
733 qemu_register_reset(fdctrl_external_reset
, fdctrl
);
734 for (i
= 0; i
< MAX_FD
; i
++) {
735 fd_revalidate(&fdctrl
->drives
[i
]);
741 fdctrl_t
*fdctrl_init (qemu_irq irq
, int dma_chann
, int mem_mapped
,
742 target_phys_addr_t io_base
,
743 BlockDriverState
**fds
)
748 fdctrl
= fdctrl_init_common(irq
, dma_chann
, io_base
, fds
);
752 io_mem
= cpu_register_io_memory(0, fdctrl_mem_read
, fdctrl_mem_write
,
754 cpu_register_physical_memory(io_base
, 0x08, io_mem
);
756 register_ioport_read((uint32_t)io_base
+ 0x01, 5, 1, &fdctrl_read
,
758 register_ioport_read((uint32_t)io_base
+ 0x07, 1, 1, &fdctrl_read
,
760 register_ioport_write((uint32_t)io_base
+ 0x01, 5, 1, &fdctrl_write
,
762 register_ioport_write((uint32_t)io_base
+ 0x07, 1, 1, &fdctrl_write
,
769 static void fdctrl_handle_tc(void *opaque
, int irq
, int level
)
771 //fdctrl_t *s = opaque;
775 FLOPPY_DPRINTF("TC pulsed\n");
779 fdctrl_t
*sun4m_fdctrl_init (qemu_irq irq
, target_phys_addr_t io_base
,
780 BlockDriverState
**fds
, qemu_irq
*fdc_tc
)
785 fdctrl
= fdctrl_init_common(irq
, 0, io_base
, fds
);
787 io_mem
= cpu_register_io_memory(0, fdctrl_mem_read_strict
,
788 fdctrl_mem_write_strict
,
790 cpu_register_physical_memory(io_base
, 0x08, io_mem
);
791 *fdc_tc
= *qemu_allocate_irqs(fdctrl_handle_tc
, fdctrl
, 1);
796 /* XXX: may change if moved to bdrv */
797 int fdctrl_get_drive_type(fdctrl_t
*fdctrl
, int drive_num
)
799 return fdctrl
->drives
[drive_num
].drive
;
802 /* Change IRQ state */
803 static void fdctrl_reset_irq (fdctrl_t
*fdctrl
)
805 FLOPPY_DPRINTF("Reset interrupt\n");
806 qemu_set_irq(fdctrl
->irq
, 0);
807 fdctrl
->state
&= ~FD_CTRL_INTR
;
810 static void fdctrl_raise_irq (fdctrl_t
*fdctrl
, uint8_t status
)
813 if (fdctrl
->sun4m
&& !fdctrl
->dma_en
) {
814 fdctrl
->state
&= ~FD_CTRL_BUSY
;
815 fdctrl
->int_status
= status
;
818 if (~(fdctrl
->state
& FD_CTRL_INTR
)) {
819 qemu_set_irq(fdctrl
->irq
, 1);
820 fdctrl
->state
|= FD_CTRL_INTR
;
822 FLOPPY_DPRINTF("Set interrupt status to 0x%02x\n", status
);
823 fdctrl
->int_status
= status
;
826 /* Reset controller */
827 static void fdctrl_reset (fdctrl_t
*fdctrl
, int do_irq
)
831 FLOPPY_DPRINTF("reset controller\n");
832 fdctrl_reset_irq(fdctrl
);
833 /* Initialise controller */
836 fdctrl
->data_pos
= 0;
837 fdctrl
->data_len
= 0;
838 fdctrl
->data_state
= FD_STATE_CMD
;
839 fdctrl
->data_dir
= FD_DIR_WRITE
;
840 for (i
= 0; i
< MAX_FD
; i
++)
841 fd_reset(&fdctrl
->drives
[i
]);
842 fdctrl_reset_fifo(fdctrl
);
844 fdctrl_raise_irq(fdctrl
, FD_SR0_RDYCHG
);
847 static inline fdrive_t
*drv0 (fdctrl_t
*fdctrl
)
849 return &fdctrl
->drives
[fdctrl
->bootsel
];
852 static inline fdrive_t
*drv1 (fdctrl_t
*fdctrl
)
854 return &fdctrl
->drives
[1 - fdctrl
->bootsel
];
857 static fdrive_t
*get_cur_drv (fdctrl_t
*fdctrl
)
859 return fdctrl
->cur_drv
== 0 ? drv0(fdctrl
) : drv1(fdctrl
);
862 /* Status B register : 0x01 (read-only) */
863 static uint32_t fdctrl_read_statusB (fdctrl_t
*fdctrl
)
865 FLOPPY_DPRINTF("status register: 0x00\n");
869 /* Digital output register : 0x02 */
870 static uint32_t fdctrl_read_dor (fdctrl_t
*fdctrl
)
874 /* Drive motors state indicators */
875 if (drv0(fdctrl
)->drflags
& FDRIVE_MOTOR_ON
)
876 retval
|= FD_DOR_MOTEN0
;
877 if (drv1(fdctrl
)->drflags
& FDRIVE_MOTOR_ON
)
878 retval
|= FD_DOR_MOTEN1
;
881 retval
|= FD_DOR_DMAEN
;
882 /* Reset indicator */
883 if (!(fdctrl
->state
& FD_CTRL_RESET
))
884 retval
|= FD_DOR_nRESET
;
886 retval
|= fdctrl
->cur_drv
;
887 FLOPPY_DPRINTF("digital output register: 0x%02x\n", retval
);
892 static void fdctrl_write_dor (fdctrl_t
*fdctrl
, uint32_t value
)
895 if (fdctrl
->state
& FD_CTRL_RESET
) {
896 if (!(value
& FD_DOR_nRESET
)) {
897 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
901 FLOPPY_DPRINTF("digital output register set to 0x%02x\n", value
);
902 /* Drive motors state indicators */
903 if (value
& FD_DOR_MOTEN1
)
904 fd_start(drv1(fdctrl
));
906 fd_stop(drv1(fdctrl
));
907 if (value
& FD_DOR_MOTEN0
)
908 fd_start(drv0(fdctrl
));
910 fd_stop(drv0(fdctrl
));
913 if (fdctrl
->dma_chann
!= -1)
914 fdctrl
->dma_en
= value
& FD_DOR_DMAEN
? 1 : 0;
917 if (!(value
& FD_DOR_nRESET
)) {
918 if (!(fdctrl
->state
& FD_CTRL_RESET
)) {
919 FLOPPY_DPRINTF("controller enter RESET state\n");
920 fdctrl
->state
|= FD_CTRL_RESET
;
923 if (fdctrl
->state
& FD_CTRL_RESET
) {
924 FLOPPY_DPRINTF("controller out of RESET state\n");
925 fdctrl_reset(fdctrl
, 1);
926 fdctrl
->state
&= ~(FD_CTRL_RESET
| FD_CTRL_SLEEP
);
930 fdctrl
->cur_drv
= value
& FD_DOR_SELMASK
;
933 /* Tape drive register : 0x03 */
934 static uint32_t fdctrl_read_tape (fdctrl_t
*fdctrl
)
938 /* Disk boot selection indicator */
939 retval
|= fdctrl
->bootsel
<< 2;
940 /* Tape indicators: never allowed */
941 FLOPPY_DPRINTF("tape drive register: 0x%02x\n", retval
);
946 static void fdctrl_write_tape (fdctrl_t
*fdctrl
, uint32_t value
)
949 if (fdctrl
->state
& FD_CTRL_RESET
) {
950 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
953 FLOPPY_DPRINTF("tape drive register set to 0x%02x\n", value
);
954 /* Disk boot selection indicator */
955 fdctrl
->bootsel
= (value
& FD_TDR_BOOTSEL
) >> 2;
956 /* Tape indicators: never allow */
959 /* Main status register : 0x04 (read) */
960 static uint32_t fdctrl_read_main_status (fdctrl_t
*fdctrl
)
964 fdctrl
->state
&= ~(FD_CTRL_SLEEP
| FD_CTRL_RESET
);
965 if (!(fdctrl
->state
& FD_CTRL_BUSY
)) {
966 /* Data transfer allowed */
967 retval
|= FD_MSR_RQM
;
968 /* Data transfer direction indicator */
969 if (fdctrl
->data_dir
== FD_DIR_READ
)
970 retval
|= FD_MSR_DIO
;
972 /* Should handle FD_MSR_NONDMA for SPECIFY command */
973 /* Command busy indicator */
974 if (FD_STATE(fdctrl
->data_state
) == FD_STATE_DATA
||
975 FD_STATE(fdctrl
->data_state
) == FD_STATE_STATUS
)
976 retval
|= FD_MSR_CMDBUSY
;
977 FLOPPY_DPRINTF("main status register: 0x%02x\n", retval
);
982 /* Data select rate register : 0x04 (write) */
983 static void fdctrl_write_rate (fdctrl_t
*fdctrl
, uint32_t value
)
986 if (fdctrl
->state
& FD_CTRL_RESET
) {
987 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
990 FLOPPY_DPRINTF("select rate register set to 0x%02x\n", value
);
991 /* Reset: autoclear */
992 if (value
& FD_DSR_SWRESET
) {
993 fdctrl
->state
|= FD_CTRL_RESET
;
994 fdctrl_reset(fdctrl
, 1);
995 fdctrl
->state
&= ~FD_CTRL_RESET
;
997 if (value
& FD_DSR_PWRDOWN
) {
998 fdctrl
->state
|= FD_CTRL_SLEEP
;
999 fdctrl_reset(fdctrl
, 1);
1003 static int fdctrl_media_changed(fdrive_t
*drv
)
1009 ret
= bdrv_media_changed(drv
->bs
);
1016 /* Digital input register : 0x07 (read-only) */
1017 static uint32_t fdctrl_read_dir (fdctrl_t
*fdctrl
)
1019 uint32_t retval
= 0;
1021 if (fdctrl_media_changed(drv0(fdctrl
)) ||
1022 fdctrl_media_changed(drv1(fdctrl
)))
1023 retval
|= FD_DIR_DSKCHG
;
1025 FLOPPY_DPRINTF("Floppy digital input register: 0x%02x\n", retval
);
1030 /* FIFO state control */
1031 static void fdctrl_reset_fifo (fdctrl_t
*fdctrl
)
1033 fdctrl
->data_dir
= FD_DIR_WRITE
;
1034 fdctrl
->data_pos
= 0;
1035 FD_SET_STATE(fdctrl
->data_state
, FD_STATE_CMD
);
1038 /* Set FIFO status for the host to read */
1039 static void fdctrl_set_fifo (fdctrl_t
*fdctrl
, int fifo_len
, int do_irq
)
1041 fdctrl
->data_dir
= FD_DIR_READ
;
1042 fdctrl
->data_len
= fifo_len
;
1043 fdctrl
->data_pos
= 0;
1044 FD_SET_STATE(fdctrl
->data_state
, FD_STATE_STATUS
);
1046 fdctrl_raise_irq(fdctrl
, 0x00);
1049 /* Set an error: unimplemented/unknown command */
1050 static void fdctrl_unimplemented (fdctrl_t
*fdctrl
, int direction
)
1055 cur_drv
= get_cur_drv(fdctrl
);
1056 fdctrl
->fifo
[0] = FD_SR0_ABNTERM
| FD_SR0_SEEK
| (cur_drv
->head
<< 2) | fdctrl
->cur_drv
;
1057 fdctrl
->fifo
[1] = 0x00;
1058 fdctrl
->fifo
[2] = 0x00;
1059 fdctrl_set_fifo(fdctrl
, 3, 1);
1061 // fdctrl_reset_fifo(fdctrl);
1062 fdctrl
->fifo
[0] = FD_SR0_INVCMD
;
1063 fdctrl_set_fifo(fdctrl
, 1, 0);
1067 /* Callback for transfer end (stop or abort) */
1068 static void fdctrl_stop_transfer (fdctrl_t
*fdctrl
, uint8_t status0
,
1069 uint8_t status1
, uint8_t status2
)
1073 cur_drv
= get_cur_drv(fdctrl
);
1074 FLOPPY_DPRINTF("transfer status: %02x %02x %02x (%02x)\n",
1075 status0
, status1
, status2
,
1076 status0
| (cur_drv
->head
<< 2) | fdctrl
->cur_drv
);
1077 fdctrl
->fifo
[0] = status0
| (cur_drv
->head
<< 2) | fdctrl
->cur_drv
;
1078 fdctrl
->fifo
[1] = status1
;
1079 fdctrl
->fifo
[2] = status2
;
1080 fdctrl
->fifo
[3] = cur_drv
->track
;
1081 fdctrl
->fifo
[4] = cur_drv
->head
;
1082 fdctrl
->fifo
[5] = cur_drv
->sect
;
1083 fdctrl
->fifo
[6] = FD_SECTOR_SC
;
1084 fdctrl
->data_dir
= FD_DIR_READ
;
1085 if (fdctrl
->state
& FD_CTRL_BUSY
) {
1086 DMA_release_DREQ(fdctrl
->dma_chann
);
1087 fdctrl
->state
&= ~FD_CTRL_BUSY
;
1089 fdctrl_set_fifo(fdctrl
, 7, 1);
1092 /* Prepare a data transfer (either DMA or FIFO) */
1093 static void fdctrl_start_transfer (fdctrl_t
*fdctrl
, int direction
)
1099 fdctrl
->cur_drv
= fdctrl
->fifo
[1] & FD_DOR_SELMASK
;
1100 cur_drv
= get_cur_drv(fdctrl
);
1101 kt
= fdctrl
->fifo
[2];
1102 kh
= fdctrl
->fifo
[3];
1103 ks
= fdctrl
->fifo
[4];
1104 FLOPPY_DPRINTF("Start transfer at %d %d %02x %02x (%d)\n",
1105 fdctrl
->cur_drv
, kh
, kt
, ks
,
1106 _fd_sector(kh
, kt
, ks
, cur_drv
->last_sect
));
1108 switch (fd_seek(cur_drv
, kh
, kt
, ks
, fdctrl
->config
& 0x40)) {
1111 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1112 fdctrl
->fifo
[3] = kt
;
1113 fdctrl
->fifo
[4] = kh
;
1114 fdctrl
->fifo
[5] = ks
;
1118 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x80, 0x00);
1119 fdctrl
->fifo
[3] = kt
;
1120 fdctrl
->fifo
[4] = kh
;
1121 fdctrl
->fifo
[5] = ks
;
1124 /* No seek enabled */
1125 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1126 fdctrl
->fifo
[3] = kt
;
1127 fdctrl
->fifo
[4] = kh
;
1128 fdctrl
->fifo
[5] = ks
;
1136 /* Set the FIFO state */
1137 fdctrl
->data_dir
= direction
;
1138 fdctrl
->data_pos
= 0;
1139 FD_SET_STATE(fdctrl
->data_state
, FD_STATE_DATA
); /* FIFO ready for data */
1140 if (fdctrl
->fifo
[0] & 0x80)
1141 fdctrl
->data_state
|= FD_STATE_MULTI
;
1143 fdctrl
->data_state
&= ~FD_STATE_MULTI
;
1145 fdctrl
->data_state
|= FD_STATE_SEEK
;
1147 fdctrl
->data_state
&= ~FD_STATE_SEEK
;
1148 if (fdctrl
->fifo
[5] == 00) {
1149 fdctrl
->data_len
= fdctrl
->fifo
[8];
1152 fdctrl
->data_len
= 128 << (fdctrl
->fifo
[5] > 7 ? 7 : fdctrl
->fifo
[5]);
1153 tmp
= (cur_drv
->last_sect
- ks
+ 1);
1154 if (fdctrl
->fifo
[0] & 0x80)
1155 tmp
+= cur_drv
->last_sect
;
1156 fdctrl
->data_len
*= tmp
;
1158 fdctrl
->eot
= fdctrl
->fifo
[6];
1159 if (fdctrl
->dma_en
) {
1161 /* DMA transfer are enabled. Check if DMA channel is well programmed */
1162 dma_mode
= DMA_get_channel_mode(fdctrl
->dma_chann
);
1163 dma_mode
= (dma_mode
>> 2) & 3;
1164 FLOPPY_DPRINTF("dma_mode=%d direction=%d (%d - %d)\n",
1165 dma_mode
, direction
,
1166 (128 << fdctrl
->fifo
[5]) *
1167 (cur_drv
->last_sect
- ks
+ 1), fdctrl
->data_len
);
1168 if (((direction
== FD_DIR_SCANE
|| direction
== FD_DIR_SCANL
||
1169 direction
== FD_DIR_SCANH
) && dma_mode
== 0) ||
1170 (direction
== FD_DIR_WRITE
&& dma_mode
== 2) ||
1171 (direction
== FD_DIR_READ
&& dma_mode
== 1)) {
1172 /* No access is allowed until DMA transfer has completed */
1173 fdctrl
->state
|= FD_CTRL_BUSY
;
1174 /* Now, we just have to wait for the DMA controller to
1177 DMA_hold_DREQ(fdctrl
->dma_chann
);
1178 DMA_schedule(fdctrl
->dma_chann
);
1181 FLOPPY_ERROR("dma_mode=%d direction=%d\n", dma_mode
, direction
);
1184 FLOPPY_DPRINTF("start non-DMA transfer\n");
1185 /* IO based transfer: calculate len */
1186 fdctrl_raise_irq(fdctrl
, 0x00);
1191 /* Prepare a transfer of deleted data */
1192 static void fdctrl_start_transfer_del (fdctrl_t
*fdctrl
, int direction
)
1194 /* We don't handle deleted data,
1195 * so we don't return *ANYTHING*
1197 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
, 0x00, 0x00);
1200 /* handlers for DMA transfers */
1201 static int fdctrl_transfer_handler (void *opaque
, int nchan
,
1202 int dma_pos
, int dma_len
)
1206 int len
, start_pos
, rel_pos
;
1207 uint8_t status0
= 0x00, status1
= 0x00, status2
= 0x00;
1210 if (!(fdctrl
->state
& FD_CTRL_BUSY
)) {
1211 FLOPPY_DPRINTF("Not in DMA transfer mode !\n");
1214 cur_drv
= get_cur_drv(fdctrl
);
1215 if (fdctrl
->data_dir
== FD_DIR_SCANE
|| fdctrl
->data_dir
== FD_DIR_SCANL
||
1216 fdctrl
->data_dir
== FD_DIR_SCANH
)
1218 if (dma_len
> fdctrl
->data_len
)
1219 dma_len
= fdctrl
->data_len
;
1220 if (cur_drv
->bs
== NULL
) {
1221 if (fdctrl
->data_dir
== FD_DIR_WRITE
)
1222 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
, 0x00, 0x00);
1224 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1226 goto transfer_error
;
1228 rel_pos
= fdctrl
->data_pos
% FD_SECTOR_LEN
;
1229 for (start_pos
= fdctrl
->data_pos
; fdctrl
->data_pos
< dma_len
;) {
1230 len
= dma_len
- fdctrl
->data_pos
;
1231 if (len
+ rel_pos
> FD_SECTOR_LEN
)
1232 len
= FD_SECTOR_LEN
- rel_pos
;
1233 FLOPPY_DPRINTF("copy %d bytes (%d %d %d) %d pos %d %02x "
1234 "(%d-0x%08x 0x%08x)\n", len
, dma_len
, fdctrl
->data_pos
,
1235 fdctrl
->data_len
, fdctrl
->cur_drv
, cur_drv
->head
,
1236 cur_drv
->track
, cur_drv
->sect
, fd_sector(cur_drv
),
1237 fd_sector(cur_drv
) * FD_SECTOR_LEN
);
1238 if (fdctrl
->data_dir
!= FD_DIR_WRITE
||
1239 len
< FD_SECTOR_LEN
|| rel_pos
!= 0) {
1240 /* READ & SCAN commands and realign to a sector for WRITE */
1241 if (bdrv_read(cur_drv
->bs
, fd_sector(cur_drv
),
1242 fdctrl
->fifo
, 1) < 0) {
1243 FLOPPY_DPRINTF("Floppy: error getting sector %d\n",
1244 fd_sector(cur_drv
));
1245 /* Sure, image size is too small... */
1246 memset(fdctrl
->fifo
, 0, FD_SECTOR_LEN
);
1249 switch (fdctrl
->data_dir
) {
1252 DMA_write_memory (nchan
, fdctrl
->fifo
+ rel_pos
,
1253 fdctrl
->data_pos
, len
);
1256 /* WRITE commands */
1257 DMA_read_memory (nchan
, fdctrl
->fifo
+ rel_pos
,
1258 fdctrl
->data_pos
, len
);
1259 if (bdrv_write(cur_drv
->bs
, fd_sector(cur_drv
),
1260 fdctrl
->fifo
, 1) < 0) {
1261 FLOPPY_ERROR("writting sector %d\n", fd_sector(cur_drv
));
1262 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
, 0x00, 0x00);
1263 goto transfer_error
;
1269 uint8_t tmpbuf
[FD_SECTOR_LEN
];
1271 DMA_read_memory (nchan
, tmpbuf
, fdctrl
->data_pos
, len
);
1272 ret
= memcmp(tmpbuf
, fdctrl
->fifo
+ rel_pos
, len
);
1277 if ((ret
< 0 && fdctrl
->data_dir
== FD_DIR_SCANL
) ||
1278 (ret
> 0 && fdctrl
->data_dir
== FD_DIR_SCANH
)) {
1285 fdctrl
->data_pos
+= len
;
1286 rel_pos
= fdctrl
->data_pos
% FD_SECTOR_LEN
;
1288 /* Seek to next sector */
1289 FLOPPY_DPRINTF("seek to next sector (%d %02x %02x => %d) (%d)\n",
1290 cur_drv
->head
, cur_drv
->track
, cur_drv
->sect
,
1292 fdctrl
->data_pos
- len
);
1293 /* XXX: cur_drv->sect >= cur_drv->last_sect should be an
1295 if (cur_drv
->sect
>= cur_drv
->last_sect
||
1296 cur_drv
->sect
== fdctrl
->eot
) {
1298 if (FD_MULTI_TRACK(fdctrl
->data_state
)) {
1299 if (cur_drv
->head
== 0 &&
1300 (cur_drv
->flags
& FDISK_DBL_SIDES
) != 0) {
1305 if ((cur_drv
->flags
& FDISK_DBL_SIDES
) == 0)
1312 FLOPPY_DPRINTF("seek to next track (%d %02x %02x => %d)\n",
1313 cur_drv
->head
, cur_drv
->track
,
1314 cur_drv
->sect
, fd_sector(cur_drv
));
1321 len
= fdctrl
->data_pos
- start_pos
;
1322 FLOPPY_DPRINTF("end transfer %d %d %d\n",
1323 fdctrl
->data_pos
, len
, fdctrl
->data_len
);
1324 if (fdctrl
->data_dir
== FD_DIR_SCANE
||
1325 fdctrl
->data_dir
== FD_DIR_SCANL
||
1326 fdctrl
->data_dir
== FD_DIR_SCANH
)
1328 if (FD_DID_SEEK(fdctrl
->data_state
))
1329 status0
|= FD_SR0_SEEK
;
1330 fdctrl
->data_len
-= len
;
1331 // if (fdctrl->data_len == 0)
1332 fdctrl_stop_transfer(fdctrl
, status0
, status1
, status2
);
1338 /* Data register : 0x05 */
1339 static uint32_t fdctrl_read_data (fdctrl_t
*fdctrl
)
1342 uint32_t retval
= 0;
1345 cur_drv
= get_cur_drv(fdctrl
);
1346 fdctrl
->state
&= ~FD_CTRL_SLEEP
;
1347 if (FD_STATE(fdctrl
->data_state
) == FD_STATE_CMD
) {
1348 FLOPPY_ERROR("can't read data in CMD state\n");
1351 pos
= fdctrl
->data_pos
;
1352 if (FD_STATE(fdctrl
->data_state
) == FD_STATE_DATA
) {
1353 pos
%= FD_SECTOR_LEN
;
1355 len
= fdctrl
->data_len
- fdctrl
->data_pos
;
1356 if (len
> FD_SECTOR_LEN
)
1357 len
= FD_SECTOR_LEN
;
1358 bdrv_read(cur_drv
->bs
, fd_sector(cur_drv
), fdctrl
->fifo
, 1);
1361 retval
= fdctrl
->fifo
[pos
];
1362 if (++fdctrl
->data_pos
== fdctrl
->data_len
) {
1363 fdctrl
->data_pos
= 0;
1364 /* Switch from transfer mode to status mode
1365 * then from status mode to command mode
1367 if (FD_STATE(fdctrl
->data_state
) == FD_STATE_DATA
) {
1368 fdctrl_stop_transfer(fdctrl
, FD_SR0_SEEK
, 0x00, 0x00);
1370 fdctrl_reset_fifo(fdctrl
);
1371 fdctrl_reset_irq(fdctrl
);
1374 FLOPPY_DPRINTF("data register: 0x%02x\n", retval
);
1379 static void fdctrl_format_sector (fdctrl_t
*fdctrl
)
1385 fdctrl
->cur_drv
= fdctrl
->fifo
[1] & FD_DOR_SELMASK
;
1386 cur_drv
= get_cur_drv(fdctrl
);
1387 kt
= fdctrl
->fifo
[6];
1388 kh
= fdctrl
->fifo
[7];
1389 ks
= fdctrl
->fifo
[8];
1390 FLOPPY_DPRINTF("format sector at %d %d %02x %02x (%d)\n",
1391 fdctrl
->cur_drv
, kh
, kt
, ks
,
1392 _fd_sector(kh
, kt
, ks
, cur_drv
->last_sect
));
1394 switch (fd_seek(cur_drv
, kh
, kt
, ks
, fdctrl
->config
& FD_CONFIG_EIS
)) {
1397 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1398 fdctrl
->fifo
[3] = kt
;
1399 fdctrl
->fifo
[4] = kh
;
1400 fdctrl
->fifo
[5] = ks
;
1404 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x80, 0x00);
1405 fdctrl
->fifo
[3] = kt
;
1406 fdctrl
->fifo
[4] = kh
;
1407 fdctrl
->fifo
[5] = ks
;
1410 /* No seek enabled */
1411 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1412 fdctrl
->fifo
[3] = kt
;
1413 fdctrl
->fifo
[4] = kh
;
1414 fdctrl
->fifo
[5] = ks
;
1418 fdctrl
->data_state
|= FD_STATE_SEEK
;
1423 memset(fdctrl
->fifo
, 0, FD_SECTOR_LEN
);
1424 if (cur_drv
->bs
== NULL
||
1425 bdrv_write(cur_drv
->bs
, fd_sector(cur_drv
), fdctrl
->fifo
, 1) < 0) {
1426 FLOPPY_ERROR("formatting sector %d\n", fd_sector(cur_drv
));
1427 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
, 0x00, 0x00);
1429 if (cur_drv
->sect
== cur_drv
->last_sect
) {
1430 fdctrl
->data_state
&= ~FD_STATE_FORMAT
;
1431 /* Last sector done */
1432 if (FD_DID_SEEK(fdctrl
->data_state
))
1433 fdctrl_stop_transfer(fdctrl
, FD_SR0_SEEK
, 0x00, 0x00);
1435 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);
1438 fdctrl
->data_pos
= 0;
1439 fdctrl
->data_len
= 4;
1444 static void fdctrl_handle_lock (fdctrl_t
*fdctrl
, int direction
)
1446 fdctrl
->lock
= (fdctrl
->fifo
[0] & 0x80) ? 1 : 0;
1447 fdctrl
->fifo
[0] = fdctrl
->lock
<< 4;
1448 fdctrl_set_fifo(fdctrl
, 1, fdctrl
->lock
);
1451 static void fdctrl_handle_dumpreg (fdctrl_t
*fdctrl
, int direction
)
1453 fdrive_t
*cur_drv
= get_cur_drv(fdctrl
);
1455 /* Drives position */
1456 fdctrl
->fifo
[0] = drv0(fdctrl
)->track
;
1457 fdctrl
->fifo
[1] = drv1(fdctrl
)->track
;
1458 fdctrl
->fifo
[2] = 0;
1459 fdctrl
->fifo
[3] = 0;
1461 fdctrl
->fifo
[4] = fdctrl
->timer0
;
1462 fdctrl
->fifo
[5] = (fdctrl
->timer1
<< 1) | fdctrl
->dma_en
;
1463 fdctrl
->fifo
[6] = cur_drv
->last_sect
;
1464 fdctrl
->fifo
[7] = (fdctrl
->lock
<< 7) |
1465 (cur_drv
->perpendicular
<< 2);
1466 fdctrl
->fifo
[8] = fdctrl
->config
;
1467 fdctrl
->fifo
[9] = fdctrl
->precomp_trk
;
1468 fdctrl_set_fifo(fdctrl
, 10, 0);
1471 static void fdctrl_handle_version (fdctrl_t
*fdctrl
, int direction
)
1473 /* Controller's version */
1474 fdctrl
->fifo
[0] = fdctrl
->version
;
1475 fdctrl_set_fifo(fdctrl
, 1, 1);
1478 static void fdctrl_handle_partid (fdctrl_t
*fdctrl
, int direction
)
1480 fdctrl
->fifo
[0] = 0x41; /* Stepping 1 */
1481 fdctrl_set_fifo(fdctrl
, 1, 0);
1484 static void fdctrl_handle_restore (fdctrl_t
*fdctrl
, int direction
)
1486 fdrive_t
*cur_drv
= get_cur_drv(fdctrl
);
1488 /* Drives position */
1489 drv0(fdctrl
)->track
= fdctrl
->fifo
[3];
1490 drv1(fdctrl
)->track
= fdctrl
->fifo
[4];
1492 fdctrl
->timer0
= fdctrl
->fifo
[7];
1493 fdctrl
->timer1
= fdctrl
->fifo
[8];
1494 cur_drv
->last_sect
= fdctrl
->fifo
[9];
1495 fdctrl
->lock
= fdctrl
->fifo
[10] >> 7;
1496 cur_drv
->perpendicular
= (fdctrl
->fifo
[10] >> 2) & 0xF;
1497 fdctrl
->config
= fdctrl
->fifo
[11];
1498 fdctrl
->precomp_trk
= fdctrl
->fifo
[12];
1499 fdctrl
->pwrd
= fdctrl
->fifo
[13];
1500 fdctrl_reset_fifo(fdctrl
);
1503 static void fdctrl_handle_save (fdctrl_t
*fdctrl
, int direction
)
1505 fdrive_t
*cur_drv
= get_cur_drv(fdctrl
);
1507 fdctrl
->fifo
[0] = 0;
1508 fdctrl
->fifo
[1] = 0;
1509 /* Drives position */
1510 fdctrl
->fifo
[2] = drv0(fdctrl
)->track
;
1511 fdctrl
->fifo
[3] = drv1(fdctrl
)->track
;
1512 fdctrl
->fifo
[4] = 0;
1513 fdctrl
->fifo
[5] = 0;
1515 fdctrl
->fifo
[6] = fdctrl
->timer0
;
1516 fdctrl
->fifo
[7] = fdctrl
->timer1
;
1517 fdctrl
->fifo
[8] = cur_drv
->last_sect
;
1518 fdctrl
->fifo
[9] = (fdctrl
->lock
<< 7) |
1519 (cur_drv
->perpendicular
<< 2);
1520 fdctrl
->fifo
[10] = fdctrl
->config
;
1521 fdctrl
->fifo
[11] = fdctrl
->precomp_trk
;
1522 fdctrl
->fifo
[12] = fdctrl
->pwrd
;
1523 fdctrl
->fifo
[13] = 0;
1524 fdctrl
->fifo
[14] = 0;
1525 fdctrl_set_fifo(fdctrl
, 15, 1);
1528 static void fdctrl_handle_readid (fdctrl_t
*fdctrl
, int direction
)
1530 fdrive_t
*cur_drv
= get_cur_drv(fdctrl
);
1532 /* XXX: should set main status register to busy */
1533 cur_drv
->head
= (fdctrl
->fifo
[1] >> 2) & 1;
1534 qemu_mod_timer(fdctrl
->result_timer
,
1535 qemu_get_clock(vm_clock
) + (ticks_per_sec
/ 50));
1538 static void fdctrl_handle_format_track (fdctrl_t
*fdctrl
, int direction
)
1542 fdctrl
->cur_drv
= fdctrl
->fifo
[1] & FD_DOR_SELMASK
;
1543 cur_drv
= get_cur_drv(fdctrl
);
1544 fdctrl
->data_state
|= FD_STATE_FORMAT
;
1545 if (fdctrl
->fifo
[0] & 0x80)
1546 fdctrl
->data_state
|= FD_STATE_MULTI
;
1548 fdctrl
->data_state
&= ~FD_STATE_MULTI
;
1549 fdctrl
->data_state
&= ~FD_STATE_SEEK
;
1551 fdctrl
->fifo
[2] > 7 ? 16384 : 128 << fdctrl
->fifo
[2];
1553 cur_drv
->last_sect
=
1554 cur_drv
->flags
& FDISK_DBL_SIDES
? fdctrl
->fifo
[3] :
1555 fdctrl
->fifo
[3] / 2;
1557 cur_drv
->last_sect
= fdctrl
->fifo
[3];
1559 /* TODO: implement format using DMA expected by the Bochs BIOS
1560 * and Linux fdformat (read 3 bytes per sector via DMA and fill
1561 * the sector with the specified fill byte
1563 fdctrl
->data_state
&= ~FD_STATE_FORMAT
;
1564 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);
1567 static void fdctrl_handle_specify (fdctrl_t
*fdctrl
, int direction
)
1569 fdctrl
->timer0
= (fdctrl
->fifo
[1] >> 4) & 0xF;
1570 fdctrl
->timer1
= fdctrl
->fifo
[2] >> 1;
1571 fdctrl
->dma_en
= 1 - (fdctrl
->fifo
[2] & 1) ;
1572 /* No result back */
1573 fdctrl_reset_fifo(fdctrl
);
1576 static void fdctrl_handle_sense_drive_status (fdctrl_t
*fdctrl
, int direction
)
1580 fdctrl
->cur_drv
= fdctrl
->fifo
[1] & FD_DOR_SELMASK
;
1581 cur_drv
= get_cur_drv(fdctrl
);
1582 cur_drv
->head
= (fdctrl
->fifo
[1] >> 2) & 1;
1583 /* 1 Byte status back */
1584 fdctrl
->fifo
[0] = (cur_drv
->ro
<< 6) |
1585 (cur_drv
->track
== 0 ? 0x10 : 0x00) |
1586 (cur_drv
->head
<< 2) |
1589 fdctrl_set_fifo(fdctrl
, 1, 0);
1592 static void fdctrl_handle_recalibrate (fdctrl_t
*fdctrl
, int direction
)
1596 fdctrl
->cur_drv
= fdctrl
->fifo
[1] & FD_DOR_SELMASK
;
1597 cur_drv
= get_cur_drv(fdctrl
);
1598 fd_recalibrate(cur_drv
);
1599 fdctrl_reset_fifo(fdctrl
);
1600 /* Raise Interrupt */
1601 fdctrl_raise_irq(fdctrl
, FD_SR0_SEEK
);
1604 static void fdctrl_handle_sense_interrupt_status (fdctrl_t
*fdctrl
, int direction
)
1606 fdrive_t
*cur_drv
= get_cur_drv(fdctrl
);
1610 fdctrl
->int_status
| (cur_drv
->head
<< 2) | fdctrl
->cur_drv
;
1612 /* XXX: int_status handling is broken for read/write
1613 commands, so we do this hack. It should be suppressed
1616 0x20 | (cur_drv
->head
<< 2) | fdctrl
->cur_drv
;
1618 fdctrl
->fifo
[1] = cur_drv
->track
;
1619 fdctrl_set_fifo(fdctrl
, 2, 0);
1620 fdctrl_reset_irq(fdctrl
);
1621 fdctrl
->int_status
= FD_SR0_RDYCHG
;
1624 static void fdctrl_handle_seek (fdctrl_t
*fdctrl
, int direction
)
1628 fdctrl
->cur_drv
= fdctrl
->fifo
[1] & FD_DOR_SELMASK
;
1629 cur_drv
= get_cur_drv(fdctrl
);
1631 if (fdctrl
->fifo
[2] <= cur_drv
->track
)
1635 fdctrl_reset_fifo(fdctrl
);
1636 if (fdctrl
->fifo
[2] > cur_drv
->max_track
) {
1637 fdctrl_raise_irq(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
);
1639 cur_drv
->track
= fdctrl
->fifo
[2];
1640 /* Raise Interrupt */
1641 fdctrl_raise_irq(fdctrl
, FD_SR0_SEEK
);
1645 static void fdctrl_handle_perpendicular_mode (fdctrl_t
*fdctrl
, int direction
)
1647 fdrive_t
*cur_drv
= get_cur_drv(fdctrl
);
1649 if (fdctrl
->fifo
[1] & 0x80)
1650 cur_drv
->perpendicular
= fdctrl
->fifo
[1] & 0x7;
1651 /* No result back */
1652 fdctrl_reset_fifo(fdctrl
);
1655 static void fdctrl_handle_configure (fdctrl_t
*fdctrl
, int direction
)
1657 fdctrl
->config
= fdctrl
->fifo
[2];
1658 fdctrl
->precomp_trk
= fdctrl
->fifo
[3];
1659 /* No result back */
1660 fdctrl_reset_fifo(fdctrl
);
1663 static void fdctrl_handle_powerdown_mode (fdctrl_t
*fdctrl
, int direction
)
1665 fdctrl
->pwrd
= fdctrl
->fifo
[1];
1666 fdctrl
->fifo
[0] = fdctrl
->fifo
[1];
1667 fdctrl_set_fifo(fdctrl
, 1, 1);
1670 static void fdctrl_handle_option (fdctrl_t
*fdctrl
, int direction
)
1672 /* No result back */
1673 fdctrl_reset_fifo(fdctrl
);
1676 static void fdctrl_handle_drive_specification_command (fdctrl_t
*fdctrl
, int direction
)
1678 fdrive_t
*cur_drv
= get_cur_drv(fdctrl
);
1680 if (fdctrl
->fifo
[fdctrl
->data_pos
- 1] & 0x80) {
1681 /* Command parameters done */
1682 if (fdctrl
->fifo
[fdctrl
->data_pos
- 1] & 0x40) {
1683 fdctrl
->fifo
[0] = fdctrl
->fifo
[1];
1684 fdctrl
->fifo
[2] = 0;
1685 fdctrl
->fifo
[3] = 0;
1686 fdctrl_set_fifo(fdctrl
, 4, 1);
1688 fdctrl_reset_fifo(fdctrl
);
1690 } else if (fdctrl
->data_len
> 7) {
1692 fdctrl
->fifo
[0] = 0x80 |
1693 (cur_drv
->head
<< 2) | fdctrl
->cur_drv
;
1694 fdctrl_set_fifo(fdctrl
, 1, 1);
1698 static void fdctrl_handle_relative_seek_out (fdctrl_t
*fdctrl
, int direction
)
1700 fdrive_t
*cur_drv
= get_cur_drv(fdctrl
);
1702 fdctrl
->cur_drv
= fdctrl
->fifo
[1] & FD_DOR_SELMASK
;
1703 cur_drv
= get_cur_drv(fdctrl
);
1706 if (fdctrl
->fifo
[2] + cur_drv
->track
>= cur_drv
->max_track
) {
1707 cur_drv
->track
= cur_drv
->max_track
- 1;
1709 cur_drv
->track
+= fdctrl
->fifo
[2];
1711 fdctrl_reset_fifo(fdctrl
);
1712 fdctrl_raise_irq(fdctrl
, FD_SR0_SEEK
);
1715 static void fdctrl_handle_relative_seek_in (fdctrl_t
*fdctrl
, int direction
)
1717 fdrive_t
*cur_drv
= get_cur_drv(fdctrl
);
1719 fdctrl
->cur_drv
= fdctrl
->fifo
[1] & FD_DOR_SELMASK
;
1720 cur_drv
= get_cur_drv(fdctrl
);
1723 if (fdctrl
->fifo
[2] > cur_drv
->track
) {
1726 cur_drv
->track
-= fdctrl
->fifo
[2];
1728 fdctrl_reset_fifo(fdctrl
);
1729 /* Raise Interrupt */
1730 fdctrl_raise_irq(fdctrl
, FD_SR0_SEEK
);
1733 static void fdctrl_write_data (fdctrl_t
*fdctrl
, uint32_t value
)
1737 static const struct {
1742 void (*handler
)(fdctrl_t
*fdctrl
, int direction
);
1745 { FD_CMD_READ
, 0x1f, "READ", 8, fdctrl_start_transfer
, FD_DIR_READ
},
1746 { FD_CMD_WRITE
, 0x3f, "WRITE", 8, fdctrl_start_transfer
, FD_DIR_WRITE
},
1747 { FD_CMD_SEEK
, 0xff, "SEEK", 2, fdctrl_handle_seek
},
1748 { FD_CMD_SENSE_INTERRUPT_STATUS
, 0xff, "SENSE INTERRUPT STATUS", 0, fdctrl_handle_sense_interrupt_status
},
1749 { FD_CMD_RECALIBRATE
, 0xff, "RECALIBRATE", 1, fdctrl_handle_recalibrate
},
1750 { FD_CMD_FORMAT_TRACK
, 0xbf, "FORMAT TRACK", 5, fdctrl_handle_format_track
},
1751 { FD_CMD_READ_TRACK
, 0xbf, "READ TRACK", 8, fdctrl_start_transfer
, FD_DIR_READ
},
1752 { FD_CMD_RESTORE
, 0xff, "RESTORE", 17, fdctrl_handle_restore
}, /* part of READ DELETED DATA */
1753 { FD_CMD_SAVE
, 0xff, "SAVE", 0, fdctrl_handle_save
}, /* part of READ DELETED DATA */
1754 { FD_CMD_READ_DELETED
, 0x1f, "READ DELETED DATA", 8, fdctrl_start_transfer_del
, FD_DIR_READ
},
1755 { FD_CMD_SCAN_EQUAL
, 0x1f, "SCAN EQUAL", 8, fdctrl_start_transfer
, FD_DIR_SCANE
},
1756 { FD_CMD_VERIFY
, 0x1f, "VERIFY", 8, fdctrl_unimplemented
},
1757 { FD_CMD_SCAN_LOW_OR_EQUAL
, 0x1f, "SCAN LOW OR EQUAL", 8, fdctrl_start_transfer
, FD_DIR_SCANL
},
1758 { FD_CMD_SCAN_HIGH_OR_EQUAL
, 0x1f, "SCAN HIGH OR EQUAL", 8, fdctrl_start_transfer
, FD_DIR_SCANH
},
1759 { FD_CMD_WRITE_DELETED
, 0x3f, "WRITE DELETED DATA", 8, fdctrl_start_transfer_del
, FD_DIR_WRITE
},
1760 { FD_CMD_READ_ID
, 0xbf, "READ ID", 1, fdctrl_handle_readid
},
1761 { FD_CMD_SPECIFY
, 0xff, "SPECIFY", 2, fdctrl_handle_specify
},
1762 { FD_CMD_SENSE_DRIVE_STATUS
, 0xff, "SENSE DRIVE STATUS", 1, fdctrl_handle_sense_drive_status
},
1763 { FD_CMD_PERPENDICULAR_MODE
, 0xff, "PERPENDICULAR MODE", 1, fdctrl_handle_perpendicular_mode
},
1764 { FD_CMD_CONFIGURE
, 0xff, "CONFIGURE", 3, fdctrl_handle_configure
},
1765 { FD_CMD_POWERDOWN_MODE
, 0xff, "POWERDOWN MODE", 2, fdctrl_handle_powerdown_mode
},
1766 { FD_CMD_OPTION
, 0xff, "OPTION", 1, fdctrl_handle_option
},
1767 { FD_CMD_DRIVE_SPECIFICATION_COMMAND
, 0xff, "DRIVE SPECIFICATION COMMAND", 5, fdctrl_handle_drive_specification_command
},
1768 { FD_CMD_RELATIVE_SEEK_OUT
, 0xff, "RELATIVE SEEK OUT", 2, fdctrl_handle_relative_seek_out
},
1769 { FD_CMD_FORMAT_AND_WRITE
, 0xff, "FORMAT AND WRITE", 10, fdctrl_unimplemented
},
1770 { FD_CMD_RELATIVE_SEEK_IN
, 0xff, "RELATIVE SEEK IN", 2, fdctrl_handle_relative_seek_in
},
1771 { FD_CMD_LOCK
, 0x7f, "LOCK", 0, fdctrl_handle_lock
},
1772 { FD_CMD_DUMPREG
, 0xff, "DUMPREG", 0, fdctrl_handle_dumpreg
},
1773 { FD_CMD_VERSION
, 0xff, "VERSION", 0, fdctrl_handle_version
},
1774 { FD_CMD_PART_ID
, 0xff, "PART ID", 0, fdctrl_handle_partid
},
1775 { FD_CMD_WRITE
, 0x1f, "WRITE (BeOS)", 8, fdctrl_start_transfer
, FD_DIR_WRITE
}, /* not in specification ; BeOS 4.5 bug */
1778 cur_drv
= get_cur_drv(fdctrl
);
1780 if (fdctrl
->state
& FD_CTRL_RESET
) {
1781 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
1784 fdctrl
->state
&= ~FD_CTRL_SLEEP
;
1785 if (FD_STATE(fdctrl
->data_state
) == FD_STATE_STATUS
) {
1786 FLOPPY_ERROR("can't write data in status mode\n");
1789 /* Is it write command time ? */
1790 if (FD_STATE(fdctrl
->data_state
) == FD_STATE_DATA
) {
1791 /* FIFO data write */
1792 fdctrl
->fifo
[fdctrl
->data_pos
++] = value
;
1793 if (fdctrl
->data_pos
% FD_SECTOR_LEN
== (FD_SECTOR_LEN
- 1) ||
1794 fdctrl
->data_pos
== fdctrl
->data_len
) {
1795 bdrv_write(cur_drv
->bs
, fd_sector(cur_drv
), fdctrl
->fifo
, 1);
1797 /* Switch from transfer mode to status mode
1798 * then from status mode to command mode
1800 if (FD_STATE(fdctrl
->data_state
) == FD_STATE_DATA
)
1801 fdctrl_stop_transfer(fdctrl
, FD_SR0_SEEK
, 0x00, 0x00);
1804 if (fdctrl
->data_pos
== 0) {
1806 for (pos
= 0; pos
< sizeof(commands
)/sizeof(commands
[0]); pos
++) {
1807 if ((value
& commands
[pos
].mask
) == commands
[pos
].value
) {
1808 FLOPPY_DPRINTF("%s command\n", commands
[pos
].name
);
1809 fdctrl
->data_len
= commands
[pos
].parameters
+ 1;
1814 /* Unknown command */
1815 FLOPPY_ERROR("unknown command: 0x%02x\n", value
);
1816 fdctrl_unimplemented(fdctrl
, 0);
1820 FLOPPY_DPRINTF("%s: %02x\n", __func__
, value
);
1821 fdctrl
->fifo
[fdctrl
->data_pos
] = value
;
1822 if (++fdctrl
->data_pos
== fdctrl
->data_len
) {
1823 /* We now have all parameters
1824 * and will be able to treat the command
1826 if (fdctrl
->data_state
& FD_STATE_FORMAT
) {
1827 fdctrl_format_sector(fdctrl
);
1831 for (pos
= 0; pos
< sizeof(commands
)/sizeof(commands
[0]); pos
++) {
1832 if ((fdctrl
->fifo
[0] & commands
[pos
].mask
) == commands
[pos
].value
) {
1833 FLOPPY_DPRINTF("treat %s command\n", commands
[pos
].name
);
1834 (*commands
[pos
].handler
)(fdctrl
, commands
[pos
].parameter
);
1841 static void fdctrl_result_timer(void *opaque
)
1843 fdctrl_t
*fdctrl
= opaque
;
1844 fdrive_t
*cur_drv
= get_cur_drv(fdctrl
);
1846 /* Pretend we are spinning.
1847 * This is needed for Coherent, which uses READ ID to check for
1848 * sector interleaving.
1850 if (cur_drv
->last_sect
!= 0) {
1851 cur_drv
->sect
= (cur_drv
->sect
% cur_drv
->last_sect
) + 1;
1853 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);