2 * QEMU Floppy disk emulator (Intel 82078)
4 * Copyright (c) 2003, 2007 Jocelyn Mayer
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 * The controller is used in Sun4m systems in a slightly different
26 * way. There are changes in DOR register and DMA is not available.
30 /********************************************************/
31 /* debug Floppy devices */
32 //#define DEBUG_FLOPPY
35 #define FLOPPY_DPRINTF(fmt, args...) \
36 do { printf("FLOPPY: " fmt , ##args); } while (0)
38 #define FLOPPY_DPRINTF(fmt, args...)
41 #define FLOPPY_ERROR(fmt, args...) \
42 do { printf("FLOPPY ERROR: %s: " fmt, __func__ , ##args); } while (0)
44 /********************************************************/
45 /* Floppy drive emulation */
47 /* Will always be a fixed parameter for us */
48 #define FD_SECTOR_LEN 512
49 #define FD_SECTOR_SC 2 /* Sector size code */
51 /* Floppy disk drive emulation */
52 typedef enum fdisk_type_t
{
53 FDRIVE_DISK_288
= 0x01, /* 2.88 MB disk */
54 FDRIVE_DISK_144
= 0x02, /* 1.44 MB disk */
55 FDRIVE_DISK_720
= 0x03, /* 720 kB disk */
56 FDRIVE_DISK_USER
= 0x04, /* User defined geometry */
57 FDRIVE_DISK_NONE
= 0x05, /* No disk */
60 typedef enum fdrive_type_t
{
61 FDRIVE_DRV_144
= 0x00, /* 1.44 MB 3"5 drive */
62 FDRIVE_DRV_288
= 0x01, /* 2.88 MB 3"5 drive */
63 FDRIVE_DRV_120
= 0x02, /* 1.2 MB 5"25 drive */
64 FDRIVE_DRV_NONE
= 0x03, /* No drive connected */
67 typedef enum fdrive_flags_t
{
68 FDRIVE_MOTOR_ON
= 0x01, /* motor on/off */
71 typedef enum fdisk_flags_t
{
72 FDISK_DBL_SIDES
= 0x01,
75 typedef struct fdrive_t
{
79 fdrive_flags_t drflags
;
80 uint8_t perpendicular
; /* 2.88 MB access mode */
85 /* Last operation status */
86 uint8_t dir
; /* Direction */
87 uint8_t rw
; /* Read/write */
90 uint8_t last_sect
; /* Nb sector per track */
91 uint8_t max_track
; /* Nb of tracks */
92 uint16_t bps
; /* Bytes per sector */
93 uint8_t ro
; /* Is read-only */
96 static void fd_init (fdrive_t
*drv
, BlockDriverState
*bs
)
100 drv
->drive
= FDRIVE_DRV_NONE
;
102 drv
->perpendicular
= 0;
108 static int _fd_sector (uint8_t head
, uint8_t track
,
109 uint8_t sect
, uint8_t last_sect
)
111 return (((track
* 2) + head
) * last_sect
) + sect
- 1;
114 /* Returns current position, in sectors, for given drive */
115 static int fd_sector (fdrive_t
*drv
)
117 return _fd_sector(drv
->head
, drv
->track
, drv
->sect
, drv
->last_sect
);
120 static int fd_seek (fdrive_t
*drv
, uint8_t head
, uint8_t track
, uint8_t sect
,
126 if (track
> drv
->max_track
||
127 (head
!= 0 && (drv
->flags
& FDISK_DBL_SIDES
) == 0)) {
128 FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
129 head
, track
, sect
, 1,
130 (drv
->flags
& FDISK_DBL_SIDES
) == 0 ? 0 : 1,
131 drv
->max_track
, drv
->last_sect
);
134 if (sect
> drv
->last_sect
) {
135 FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
136 head
, track
, sect
, 1,
137 (drv
->flags
& FDISK_DBL_SIDES
) == 0 ? 0 : 1,
138 drv
->max_track
, drv
->last_sect
);
141 sector
= _fd_sector(head
, track
, sect
, drv
->last_sect
);
143 if (sector
!= fd_sector(drv
)) {
146 FLOPPY_ERROR("no implicit seek %d %02x %02x (max=%d %02x %02x)\n",
147 head
, track
, sect
, 1, drv
->max_track
, drv
->last_sect
);
152 if (drv
->track
!= track
)
161 /* Set drive back to track 0 */
162 static void fd_recalibrate (fdrive_t
*drv
)
164 FLOPPY_DPRINTF("recalibrate\n");
172 /* Recognize floppy formats */
173 typedef struct fd_format_t
{
179 const unsigned char *str
;
182 static fd_format_t fd_formats
[] = {
183 /* First entry is default format */
184 /* 1.44 MB 3"1/2 floppy disks */
185 { FDRIVE_DRV_144
, FDRIVE_DISK_144
, 18, 80, 1, "1.44 MB 3\"1/2", },
186 { FDRIVE_DRV_144
, FDRIVE_DISK_144
, 20, 80, 1, "1.6 MB 3\"1/2", },
187 { FDRIVE_DRV_144
, FDRIVE_DISK_144
, 21, 80, 1, "1.68 MB 3\"1/2", },
188 { FDRIVE_DRV_144
, FDRIVE_DISK_144
, 21, 82, 1, "1.72 MB 3\"1/2", },
189 { FDRIVE_DRV_144
, FDRIVE_DISK_144
, 21, 83, 1, "1.74 MB 3\"1/2", },
190 { FDRIVE_DRV_144
, FDRIVE_DISK_144
, 22, 80, 1, "1.76 MB 3\"1/2", },
191 { FDRIVE_DRV_144
, FDRIVE_DISK_144
, 23, 80, 1, "1.84 MB 3\"1/2", },
192 { FDRIVE_DRV_144
, FDRIVE_DISK_144
, 24, 80, 1, "1.92 MB 3\"1/2", },
193 /* 2.88 MB 3"1/2 floppy disks */
194 { FDRIVE_DRV_288
, FDRIVE_DISK_288
, 36, 80, 1, "2.88 MB 3\"1/2", },
195 { FDRIVE_DRV_288
, FDRIVE_DISK_288
, 39, 80, 1, "3.12 MB 3\"1/2", },
196 { FDRIVE_DRV_288
, FDRIVE_DISK_288
, 40, 80, 1, "3.2 MB 3\"1/2", },
197 { FDRIVE_DRV_288
, FDRIVE_DISK_288
, 44, 80, 1, "3.52 MB 3\"1/2", },
198 { FDRIVE_DRV_288
, FDRIVE_DISK_288
, 48, 80, 1, "3.84 MB 3\"1/2", },
199 /* 720 kB 3"1/2 floppy disks */
200 { FDRIVE_DRV_144
, FDRIVE_DISK_720
, 9, 80, 1, "720 kB 3\"1/2", },
201 { FDRIVE_DRV_144
, FDRIVE_DISK_720
, 10, 80, 1, "800 kB 3\"1/2", },
202 { FDRIVE_DRV_144
, FDRIVE_DISK_720
, 10, 82, 1, "820 kB 3\"1/2", },
203 { FDRIVE_DRV_144
, FDRIVE_DISK_720
, 10, 83, 1, "830 kB 3\"1/2", },
204 { FDRIVE_DRV_144
, FDRIVE_DISK_720
, 13, 80, 1, "1.04 MB 3\"1/2", },
205 { FDRIVE_DRV_144
, FDRIVE_DISK_720
, 14, 80, 1, "1.12 MB 3\"1/2", },
206 /* 1.2 MB 5"1/4 floppy disks */
207 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 15, 80, 1, "1.2 kB 5\"1/4", },
208 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 18, 80, 1, "1.44 MB 5\"1/4", },
209 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 18, 82, 1, "1.48 MB 5\"1/4", },
210 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 18, 83, 1, "1.49 MB 5\"1/4", },
211 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 20, 80, 1, "1.6 MB 5\"1/4", },
212 /* 720 kB 5"1/4 floppy disks */
213 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 9, 80, 1, "720 kB 5\"1/4", },
214 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 11, 80, 1, "880 kB 5\"1/4", },
215 /* 360 kB 5"1/4 floppy disks */
216 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 9, 40, 1, "360 kB 5\"1/4", },
217 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 9, 40, 0, "180 kB 5\"1/4", },
218 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 10, 41, 1, "410 kB 5\"1/4", },
219 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 10, 42, 1, "420 kB 5\"1/4", },
220 /* 320 kB 5"1/4 floppy disks */
221 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 8, 40, 1, "320 kB 5\"1/4", },
222 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 8, 40, 0, "160 kB 5\"1/4", },
223 /* 360 kB must match 5"1/4 better than 3"1/2... */
224 { FDRIVE_DRV_144
, FDRIVE_DISK_720
, 9, 80, 0, "360 kB 3\"1/2", },
226 { FDRIVE_DRV_NONE
, FDRIVE_DISK_NONE
, -1, -1, 0, NULL
, },
229 /* Revalidate a disk drive after a disk change */
230 static void fd_revalidate (fdrive_t
*drv
)
233 int64_t nb_sectors
, size
;
234 int i
, first_match
, match
;
235 int nb_heads
, max_track
, last_sect
, ro
;
237 FLOPPY_DPRINTF("revalidate\n");
238 if (drv
->bs
!= NULL
&& bdrv_is_inserted(drv
->bs
)) {
239 ro
= bdrv_is_read_only(drv
->bs
);
240 bdrv_get_geometry_hint(drv
->bs
, &nb_heads
, &max_track
, &last_sect
);
241 if (nb_heads
!= 0 && max_track
!= 0 && last_sect
!= 0) {
242 FLOPPY_DPRINTF("User defined disk (%d %d %d)",
243 nb_heads
- 1, max_track
, last_sect
);
245 bdrv_get_geometry(drv
->bs
, &nb_sectors
);
249 parse
= &fd_formats
[i
];
250 if (parse
->drive
== FDRIVE_DRV_NONE
)
252 if (drv
->drive
== parse
->drive
||
253 drv
->drive
== FDRIVE_DRV_NONE
) {
254 size
= (parse
->max_head
+ 1) * parse
->max_track
*
256 if (nb_sectors
== size
) {
260 if (first_match
== -1)
265 if (first_match
== -1)
269 parse
= &fd_formats
[match
];
271 nb_heads
= parse
->max_head
+ 1;
272 max_track
= parse
->max_track
;
273 last_sect
= parse
->last_sect
;
274 drv
->drive
= parse
->drive
;
275 FLOPPY_DPRINTF("%s floppy disk (%d h %d t %d s) %s\n", parse
->str
,
276 nb_heads
, max_track
, last_sect
, ro
? "ro" : "rw");
279 drv
->flags
&= ~FDISK_DBL_SIDES
;
281 drv
->flags
|= FDISK_DBL_SIDES
;
283 drv
->max_track
= max_track
;
284 drv
->last_sect
= last_sect
;
287 FLOPPY_DPRINTF("No disk in drive\n");
290 drv
->flags
&= ~FDISK_DBL_SIDES
;
295 static void fd_start (fdrive_t
*drv
)
297 drv
->drflags
|= FDRIVE_MOTOR_ON
;
300 static void fd_stop (fdrive_t
*drv
)
302 drv
->drflags
&= ~FDRIVE_MOTOR_ON
;
305 /* Re-initialise a drives (motor off, repositioned) */
306 static void fd_reset (fdrive_t
*drv
)
312 /********************************************************/
313 /* Intel 82078 floppy disk controller emulation */
315 static void fdctrl_reset (fdctrl_t
*fdctrl
, int do_irq
);
316 static void fdctrl_reset_fifo (fdctrl_t
*fdctrl
);
317 static int fdctrl_transfer_handler (void *opaque
, int nchan
,
318 int dma_pos
, int dma_len
);
319 static void fdctrl_raise_irq (fdctrl_t
*fdctrl
, uint8_t status
);
320 static void fdctrl_result_timer(void *opaque
);
322 static uint32_t fdctrl_read_statusB (fdctrl_t
*fdctrl
);
323 static uint32_t fdctrl_read_dor (fdctrl_t
*fdctrl
);
324 static void fdctrl_write_dor (fdctrl_t
*fdctrl
, uint32_t value
);
325 static uint32_t fdctrl_read_tape (fdctrl_t
*fdctrl
);
326 static void fdctrl_write_tape (fdctrl_t
*fdctrl
, uint32_t value
);
327 static uint32_t fdctrl_read_main_status (fdctrl_t
*fdctrl
);
328 static void fdctrl_write_rate (fdctrl_t
*fdctrl
, uint32_t value
);
329 static uint32_t fdctrl_read_data (fdctrl_t
*fdctrl
);
330 static void fdctrl_write_data (fdctrl_t
*fdctrl
, uint32_t value
);
331 static uint32_t fdctrl_read_dir (fdctrl_t
*fdctrl
);
334 FD_CTRL_ACTIVE
= 0x01, /* XXX: suppress that */
335 FD_CTRL_RESET
= 0x02,
336 FD_CTRL_SLEEP
= 0x04, /* XXX: suppress that */
337 FD_CTRL_BUSY
= 0x08, /* dma transfer in progress */
351 FD_STATE_STATUS
= 0x01,
352 FD_STATE_DATA
= 0x02,
353 FD_STATE_STATE
= 0x03,
354 FD_STATE_MULTI
= 0x10,
355 FD_STATE_SEEK
= 0x20,
356 FD_STATE_FORMAT
= 0x40,
359 #define FD_STATE(state) ((state) & FD_STATE_STATE)
360 #define FD_SET_STATE(state, new_state) \
361 do { (state) = ((state) & ~FD_STATE_STATE) | (new_state); } while (0)
362 #define FD_MULTI_TRACK(state) ((state) & FD_STATE_MULTI)
363 #define FD_DID_SEEK(state) ((state) & FD_STATE_SEEK)
364 #define FD_FORMAT_CMD(state) ((state) & FD_STATE_FORMAT)
368 /* Controller's identification */
373 target_phys_addr_t io_base
;
374 /* Controller state */
375 QEMUTimer
*result_timer
;
381 uint8_t fifo
[FD_SECTOR_LEN
];
387 uint8_t eot
; /* last wanted sector */
388 /* States kept only to be returned back */
392 /* precompensation */
396 /* Power down config (also with status regB access mode */
402 static uint32_t fdctrl_read (void *opaque
, uint32_t reg
)
404 fdctrl_t
*fdctrl
= opaque
;
407 switch (reg
& 0x07) {
410 // Identify to Linux as S82078B
411 retval
= fdctrl_read_statusB(fdctrl
);
415 retval
= fdctrl_read_statusB(fdctrl
);
418 retval
= fdctrl_read_dor(fdctrl
);
421 retval
= fdctrl_read_tape(fdctrl
);
424 retval
= fdctrl_read_main_status(fdctrl
);
427 retval
= fdctrl_read_data(fdctrl
);
430 retval
= fdctrl_read_dir(fdctrl
);
433 retval
= (uint32_t)(-1);
436 FLOPPY_DPRINTF("read reg%d: 0x%02x\n", reg
& 7, retval
);
441 static void fdctrl_write (void *opaque
, uint32_t reg
, uint32_t value
)
443 fdctrl_t
*fdctrl
= opaque
;
445 FLOPPY_DPRINTF("write reg%d: 0x%02x\n", reg
& 7, value
);
447 switch (reg
& 0x07) {
449 fdctrl_write_dor(fdctrl
, value
);
452 fdctrl_write_tape(fdctrl
, value
);
455 fdctrl_write_rate(fdctrl
, value
);
458 fdctrl_write_data(fdctrl
, value
);
465 static uint32_t fdctrl_read_mem (void *opaque
, target_phys_addr_t reg
)
467 return fdctrl_read(opaque
, (uint32_t)reg
);
470 static void fdctrl_write_mem (void *opaque
,
471 target_phys_addr_t reg
, uint32_t value
)
473 fdctrl_write(opaque
, (uint32_t)reg
, value
);
476 static CPUReadMemoryFunc
*fdctrl_mem_read
[3] = {
482 static CPUWriteMemoryFunc
*fdctrl_mem_write
[3] = {
488 static void fd_save (QEMUFile
*f
, fdrive_t
*fd
)
493 qemu_put_8s(f
, &tmp
);
494 qemu_put_8s(f
, &fd
->head
);
495 qemu_put_8s(f
, &fd
->track
);
496 qemu_put_8s(f
, &fd
->sect
);
497 qemu_put_8s(f
, &fd
->dir
);
498 qemu_put_8s(f
, &fd
->rw
);
501 static void fdc_save (QEMUFile
*f
, void *opaque
)
503 fdctrl_t
*s
= opaque
;
505 qemu_put_8s(f
, &s
->state
);
506 qemu_put_8s(f
, &s
->dma_en
);
507 qemu_put_8s(f
, &s
->cur_drv
);
508 qemu_put_8s(f
, &s
->bootsel
);
509 qemu_put_buffer(f
, s
->fifo
, FD_SECTOR_LEN
);
510 qemu_put_be32s(f
, &s
->data_pos
);
511 qemu_put_be32s(f
, &s
->data_len
);
512 qemu_put_8s(f
, &s
->data_state
);
513 qemu_put_8s(f
, &s
->data_dir
);
514 qemu_put_8s(f
, &s
->int_status
);
515 qemu_put_8s(f
, &s
->eot
);
516 qemu_put_8s(f
, &s
->timer0
);
517 qemu_put_8s(f
, &s
->timer1
);
518 qemu_put_8s(f
, &s
->precomp_trk
);
519 qemu_put_8s(f
, &s
->config
);
520 qemu_put_8s(f
, &s
->lock
);
521 qemu_put_8s(f
, &s
->pwrd
);
522 fd_save(f
, &s
->drives
[0]);
523 fd_save(f
, &s
->drives
[1]);
526 static int fd_load (QEMUFile
*f
, fdrive_t
*fd
)
530 qemu_get_8s(f
, &tmp
);
532 qemu_get_8s(f
, &fd
->head
);
533 qemu_get_8s(f
, &fd
->track
);
534 qemu_get_8s(f
, &fd
->sect
);
535 qemu_get_8s(f
, &fd
->dir
);
536 qemu_get_8s(f
, &fd
->rw
);
541 static int fdc_load (QEMUFile
*f
, void *opaque
, int version_id
)
543 fdctrl_t
*s
= opaque
;
549 qemu_get_8s(f
, &s
->state
);
550 qemu_get_8s(f
, &s
->dma_en
);
551 qemu_get_8s(f
, &s
->cur_drv
);
552 qemu_get_8s(f
, &s
->bootsel
);
553 qemu_get_buffer(f
, s
->fifo
, FD_SECTOR_LEN
);
554 qemu_get_be32s(f
, &s
->data_pos
);
555 qemu_get_be32s(f
, &s
->data_len
);
556 qemu_get_8s(f
, &s
->data_state
);
557 qemu_get_8s(f
, &s
->data_dir
);
558 qemu_get_8s(f
, &s
->int_status
);
559 qemu_get_8s(f
, &s
->eot
);
560 qemu_get_8s(f
, &s
->timer0
);
561 qemu_get_8s(f
, &s
->timer1
);
562 qemu_get_8s(f
, &s
->precomp_trk
);
563 qemu_get_8s(f
, &s
->config
);
564 qemu_get_8s(f
, &s
->lock
);
565 qemu_get_8s(f
, &s
->pwrd
);
567 ret
= fd_load(f
, &s
->drives
[0]);
569 ret
= fd_load(f
, &s
->drives
[1]);
574 static void fdctrl_external_reset(void *opaque
)
576 fdctrl_t
*s
= opaque
;
581 fdctrl_t
*fdctrl_init (qemu_irq irq
, int dma_chann
, int mem_mapped
,
582 target_phys_addr_t io_base
,
583 BlockDriverState
**fds
)
589 FLOPPY_DPRINTF("init controller\n");
590 fdctrl
= qemu_mallocz(sizeof(fdctrl_t
));
593 fdctrl
->result_timer
= qemu_new_timer(vm_clock
,
594 fdctrl_result_timer
, fdctrl
);
596 fdctrl
->version
= 0x90; /* Intel 82078 controller */
598 fdctrl
->dma_chann
= dma_chann
;
599 fdctrl
->io_base
= io_base
;
600 fdctrl
->config
= 0x60; /* Implicit seek, polling & FIFO enabled */
601 if (fdctrl
->dma_chann
!= -1) {
603 DMA_register_channel(dma_chann
, &fdctrl_transfer_handler
, fdctrl
);
607 for (i
= 0; i
< 2; i
++) {
608 fd_init(&fdctrl
->drives
[i
], fds
[i
]);
610 fdctrl_reset(fdctrl
, 0);
611 fdctrl
->state
= FD_CTRL_ACTIVE
;
613 io_mem
= cpu_register_io_memory(0, fdctrl_mem_read
, fdctrl_mem_write
, fdctrl
);
614 cpu_register_physical_memory(io_base
, 0x08, io_mem
);
616 register_ioport_read((uint32_t)io_base
+ 0x01, 5, 1, &fdctrl_read
,
618 register_ioport_read((uint32_t)io_base
+ 0x07, 1, 1, &fdctrl_read
,
620 register_ioport_write((uint32_t)io_base
+ 0x01, 5, 1, &fdctrl_write
,
622 register_ioport_write((uint32_t)io_base
+ 0x07, 1, 1, &fdctrl_write
,
625 register_savevm("fdc", io_base
, 1, fdc_save
, fdc_load
, fdctrl
);
626 qemu_register_reset(fdctrl_external_reset
, fdctrl
);
627 for (i
= 0; i
< 2; i
++) {
628 fd_revalidate(&fdctrl
->drives
[i
]);
634 /* XXX: may change if moved to bdrv */
635 int fdctrl_get_drive_type(fdctrl_t
*fdctrl
, int drive_num
)
637 return fdctrl
->drives
[drive_num
].drive
;
640 /* Change IRQ state */
641 static void fdctrl_reset_irq (fdctrl_t
*fdctrl
)
643 FLOPPY_DPRINTF("Reset interrupt\n");
644 qemu_set_irq(fdctrl
->irq
, 0);
645 fdctrl
->state
&= ~FD_CTRL_INTR
;
648 static void fdctrl_raise_irq (fdctrl_t
*fdctrl
, uint8_t status
)
652 if (!fdctrl
->dma_en
) {
653 fdctrl
->state
&= ~FD_CTRL_BUSY
;
654 fdctrl
->int_status
= status
;
658 if (~(fdctrl
->state
& FD_CTRL_INTR
)) {
659 qemu_set_irq(fdctrl
->irq
, 1);
660 fdctrl
->state
|= FD_CTRL_INTR
;
662 FLOPPY_DPRINTF("Set interrupt status to 0x%02x\n", status
);
663 fdctrl
->int_status
= status
;
666 /* Reset controller */
667 static void fdctrl_reset (fdctrl_t
*fdctrl
, int do_irq
)
671 FLOPPY_DPRINTF("reset controller\n");
672 fdctrl_reset_irq(fdctrl
);
673 /* Initialise controller */
676 fdctrl
->data_pos
= 0;
677 fdctrl
->data_len
= 0;
678 fdctrl
->data_state
= FD_STATE_CMD
;
679 fdctrl
->data_dir
= FD_DIR_WRITE
;
680 for (i
= 0; i
< MAX_FD
; i
++)
681 fd_reset(&fdctrl
->drives
[i
]);
682 fdctrl_reset_fifo(fdctrl
);
684 fdctrl_raise_irq(fdctrl
, 0xc0);
687 static inline fdrive_t
*drv0 (fdctrl_t
*fdctrl
)
689 return &fdctrl
->drives
[fdctrl
->bootsel
];
692 static inline fdrive_t
*drv1 (fdctrl_t
*fdctrl
)
694 return &fdctrl
->drives
[1 - fdctrl
->bootsel
];
697 static fdrive_t
*get_cur_drv (fdctrl_t
*fdctrl
)
699 return fdctrl
->cur_drv
== 0 ? drv0(fdctrl
) : drv1(fdctrl
);
702 /* Status B register : 0x01 (read-only) */
703 static uint32_t fdctrl_read_statusB (fdctrl_t
*fdctrl
)
705 FLOPPY_DPRINTF("status register: 0x00\n");
709 /* Digital output register : 0x02 */
710 static uint32_t fdctrl_read_dor (fdctrl_t
*fdctrl
)
714 /* Drive motors state indicators */
715 if (drv0(fdctrl
)->drflags
& FDRIVE_MOTOR_ON
)
717 if (drv1(fdctrl
)->drflags
& FDRIVE_MOTOR_ON
)
720 retval
|= fdctrl
->dma_en
<< 3;
721 /* Reset indicator */
722 retval
|= (fdctrl
->state
& FD_CTRL_RESET
) == 0 ? 0x04 : 0;
724 retval
|= fdctrl
->cur_drv
;
725 FLOPPY_DPRINTF("digital output register: 0x%02x\n", retval
);
730 static void fdctrl_write_dor (fdctrl_t
*fdctrl
, uint32_t value
)
733 if (fdctrl
->state
& FD_CTRL_RESET
) {
734 if (!(value
& 0x04)) {
735 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
739 FLOPPY_DPRINTF("digital output register set to 0x%02x\n", value
);
740 /* Drive motors state indicators */
742 fd_start(drv1(fdctrl
));
744 fd_stop(drv1(fdctrl
));
746 fd_start(drv0(fdctrl
));
748 fd_stop(drv0(fdctrl
));
751 if (fdctrl
->dma_chann
!= -1)
752 fdctrl
->dma_en
= 1 - ((value
>> 3) & 1);
755 if (!(value
& 0x04)) {
756 if (!(fdctrl
->state
& FD_CTRL_RESET
)) {
757 FLOPPY_DPRINTF("controller enter RESET state\n");
758 fdctrl
->state
|= FD_CTRL_RESET
;
761 if (fdctrl
->state
& FD_CTRL_RESET
) {
762 FLOPPY_DPRINTF("controller out of RESET state\n");
763 fdctrl_reset(fdctrl
, 1);
764 fdctrl
->state
&= ~(FD_CTRL_RESET
| FD_CTRL_SLEEP
);
768 fdctrl
->cur_drv
= value
& 1;
771 /* Tape drive register : 0x03 */
772 static uint32_t fdctrl_read_tape (fdctrl_t
*fdctrl
)
776 /* Disk boot selection indicator */
777 retval
|= fdctrl
->bootsel
<< 2;
778 /* Tape indicators: never allowed */
779 FLOPPY_DPRINTF("tape drive register: 0x%02x\n", retval
);
784 static void fdctrl_write_tape (fdctrl_t
*fdctrl
, uint32_t value
)
787 if (fdctrl
->state
& FD_CTRL_RESET
) {
788 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
791 FLOPPY_DPRINTF("tape drive register set to 0x%02x\n", value
);
792 /* Disk boot selection indicator */
793 fdctrl
->bootsel
= (value
>> 2) & 1;
794 /* Tape indicators: never allow */
797 /* Main status register : 0x04 (read) */
798 static uint32_t fdctrl_read_main_status (fdctrl_t
*fdctrl
)
802 fdctrl
->state
&= ~(FD_CTRL_SLEEP
| FD_CTRL_RESET
);
803 if (!(fdctrl
->state
& FD_CTRL_BUSY
)) {
804 /* Data transfer allowed */
806 /* Data transfer direction indicator */
807 if (fdctrl
->data_dir
== FD_DIR_READ
)
810 /* Should handle 0x20 for SPECIFY command */
811 /* Command busy indicator */
812 if (FD_STATE(fdctrl
->data_state
) == FD_STATE_DATA
||
813 FD_STATE(fdctrl
->data_state
) == FD_STATE_STATUS
)
815 FLOPPY_DPRINTF("main status register: 0x%02x\n", retval
);
820 /* Data select rate register : 0x04 (write) */
821 static void fdctrl_write_rate (fdctrl_t
*fdctrl
, uint32_t value
)
824 if (fdctrl
->state
& FD_CTRL_RESET
) {
825 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
828 FLOPPY_DPRINTF("select rate register set to 0x%02x\n", value
);
829 /* Reset: autoclear */
831 fdctrl
->state
|= FD_CTRL_RESET
;
832 fdctrl_reset(fdctrl
, 1);
833 fdctrl
->state
&= ~FD_CTRL_RESET
;
836 fdctrl
->state
|= FD_CTRL_SLEEP
;
837 fdctrl_reset(fdctrl
, 1);
839 // fdctrl.precomp = (value >> 2) & 0x07;
842 static int fdctrl_media_changed(fdrive_t
*drv
)
847 ret
= bdrv_media_changed(drv
->bs
);
854 /* Digital input register : 0x07 (read-only) */
855 static uint32_t fdctrl_read_dir (fdctrl_t
*fdctrl
)
859 if (fdctrl_media_changed(drv0(fdctrl
)) ||
860 fdctrl_media_changed(drv1(fdctrl
)))
863 FLOPPY_DPRINTF("Floppy digital input register: 0x%02x\n", retval
);
868 /* FIFO state control */
869 static void fdctrl_reset_fifo (fdctrl_t
*fdctrl
)
871 fdctrl
->data_dir
= FD_DIR_WRITE
;
872 fdctrl
->data_pos
= 0;
873 FD_SET_STATE(fdctrl
->data_state
, FD_STATE_CMD
);
876 /* Set FIFO status for the host to read */
877 static void fdctrl_set_fifo (fdctrl_t
*fdctrl
, int fifo_len
, int do_irq
)
879 fdctrl
->data_dir
= FD_DIR_READ
;
880 fdctrl
->data_len
= fifo_len
;
881 fdctrl
->data_pos
= 0;
882 FD_SET_STATE(fdctrl
->data_state
, FD_STATE_STATUS
);
884 fdctrl_raise_irq(fdctrl
, 0x00);
887 /* Set an error: unimplemented/unknown command */
888 static void fdctrl_unimplemented (fdctrl_t
*fdctrl
)
893 cur_drv
= get_cur_drv(fdctrl
);
894 fdctrl
->fifo
[0] = 0x60 | (cur_drv
->head
<< 2) | fdctrl
->cur_drv
;
895 fdctrl
->fifo
[1] = 0x00;
896 fdctrl
->fifo
[2] = 0x00;
897 fdctrl_set_fifo(fdctrl
, 3, 1);
899 // fdctrl_reset_fifo(fdctrl);
900 fdctrl
->fifo
[0] = 0x80;
901 fdctrl_set_fifo(fdctrl
, 1, 0);
905 /* Callback for transfer end (stop or abort) */
906 static void fdctrl_stop_transfer (fdctrl_t
*fdctrl
, uint8_t status0
,
907 uint8_t status1
, uint8_t status2
)
911 cur_drv
= get_cur_drv(fdctrl
);
912 FLOPPY_DPRINTF("transfer status: %02x %02x %02x (%02x)\n",
913 status0
, status1
, status2
,
914 status0
| (cur_drv
->head
<< 2) | fdctrl
->cur_drv
);
915 fdctrl
->fifo
[0] = status0
| (cur_drv
->head
<< 2) | fdctrl
->cur_drv
;
916 fdctrl
->fifo
[1] = status1
;
917 fdctrl
->fifo
[2] = status2
;
918 fdctrl
->fifo
[3] = cur_drv
->track
;
919 fdctrl
->fifo
[4] = cur_drv
->head
;
920 fdctrl
->fifo
[5] = cur_drv
->sect
;
921 fdctrl
->fifo
[6] = FD_SECTOR_SC
;
922 fdctrl
->data_dir
= FD_DIR_READ
;
923 if (fdctrl
->state
& FD_CTRL_BUSY
) {
924 DMA_release_DREQ(fdctrl
->dma_chann
);
925 fdctrl
->state
&= ~FD_CTRL_BUSY
;
927 fdctrl_set_fifo(fdctrl
, 7, 1);
930 /* Prepare a data transfer (either DMA or FIFO) */
931 static void fdctrl_start_transfer (fdctrl_t
*fdctrl
, int direction
)
937 fdctrl
->cur_drv
= fdctrl
->fifo
[1] & 1;
938 cur_drv
= get_cur_drv(fdctrl
);
939 kt
= fdctrl
->fifo
[2];
940 kh
= fdctrl
->fifo
[3];
941 ks
= fdctrl
->fifo
[4];
942 FLOPPY_DPRINTF("Start transfer at %d %d %02x %02x (%d)\n",
943 fdctrl
->cur_drv
, kh
, kt
, ks
,
944 _fd_sector(kh
, kt
, ks
, cur_drv
->last_sect
));
946 switch (fd_seek(cur_drv
, kh
, kt
, ks
, fdctrl
->config
& 0x40)) {
949 fdctrl_stop_transfer(fdctrl
, 0x40, 0x00, 0x00);
950 fdctrl
->fifo
[3] = kt
;
951 fdctrl
->fifo
[4] = kh
;
952 fdctrl
->fifo
[5] = ks
;
956 fdctrl_stop_transfer(fdctrl
, 0x40, 0x80, 0x00);
957 fdctrl
->fifo
[3] = kt
;
958 fdctrl
->fifo
[4] = kh
;
959 fdctrl
->fifo
[5] = ks
;
962 /* No seek enabled */
963 fdctrl_stop_transfer(fdctrl
, 0x40, 0x00, 0x00);
964 fdctrl
->fifo
[3] = kt
;
965 fdctrl
->fifo
[4] = kh
;
966 fdctrl
->fifo
[5] = ks
;
974 /* Set the FIFO state */
975 fdctrl
->data_dir
= direction
;
976 fdctrl
->data_pos
= 0;
977 FD_SET_STATE(fdctrl
->data_state
, FD_STATE_DATA
); /* FIFO ready for data */
978 if (fdctrl
->fifo
[0] & 0x80)
979 fdctrl
->data_state
|= FD_STATE_MULTI
;
981 fdctrl
->data_state
&= ~FD_STATE_MULTI
;
983 fdctrl
->data_state
|= FD_STATE_SEEK
;
985 fdctrl
->data_state
&= ~FD_STATE_SEEK
;
986 if (fdctrl
->fifo
[5] == 00) {
987 fdctrl
->data_len
= fdctrl
->fifo
[8];
990 fdctrl
->data_len
= 128 << (fdctrl
->fifo
[5] > 7 ? 7 : fdctrl
->fifo
[5]);
991 tmp
= (cur_drv
->last_sect
- ks
+ 1);
992 if (fdctrl
->fifo
[0] & 0x80)
993 tmp
+= cur_drv
->last_sect
;
994 fdctrl
->data_len
*= tmp
;
996 fdctrl
->eot
= fdctrl
->fifo
[6];
997 if (fdctrl
->dma_en
) {
999 /* DMA transfer are enabled. Check if DMA channel is well programmed */
1000 dma_mode
= DMA_get_channel_mode(fdctrl
->dma_chann
);
1001 dma_mode
= (dma_mode
>> 2) & 3;
1002 FLOPPY_DPRINTF("dma_mode=%d direction=%d (%d - %d)\n",
1003 dma_mode
, direction
,
1004 (128 << fdctrl
->fifo
[5]) *
1005 (cur_drv
->last_sect
- ks
+ 1), fdctrl
->data_len
);
1006 if (((direction
== FD_DIR_SCANE
|| direction
== FD_DIR_SCANL
||
1007 direction
== FD_DIR_SCANH
) && dma_mode
== 0) ||
1008 (direction
== FD_DIR_WRITE
&& dma_mode
== 2) ||
1009 (direction
== FD_DIR_READ
&& dma_mode
== 1)) {
1010 /* No access is allowed until DMA transfer has completed */
1011 fdctrl
->state
|= FD_CTRL_BUSY
;
1012 /* Now, we just have to wait for the DMA controller to
1015 DMA_hold_DREQ(fdctrl
->dma_chann
);
1016 DMA_schedule(fdctrl
->dma_chann
);
1019 FLOPPY_ERROR("dma_mode=%d direction=%d\n", dma_mode
, direction
);
1022 FLOPPY_DPRINTF("start non-DMA transfer\n");
1023 /* IO based transfer: calculate len */
1024 fdctrl_raise_irq(fdctrl
, 0x00);
1029 /* Prepare a transfer of deleted data */
1030 static void fdctrl_start_transfer_del (fdctrl_t
*fdctrl
, int direction
)
1032 /* We don't handle deleted data,
1033 * so we don't return *ANYTHING*
1035 fdctrl_stop_transfer(fdctrl
, 0x60, 0x00, 0x00);
1038 /* handlers for DMA transfers */
1039 static int fdctrl_transfer_handler (void *opaque
, int nchan
,
1040 int dma_pos
, int dma_len
)
1044 int len
, start_pos
, rel_pos
;
1045 uint8_t status0
= 0x00, status1
= 0x00, status2
= 0x00;
1048 if (!(fdctrl
->state
& FD_CTRL_BUSY
)) {
1049 FLOPPY_DPRINTF("Not in DMA transfer mode !\n");
1052 cur_drv
= get_cur_drv(fdctrl
);
1053 if (fdctrl
->data_dir
== FD_DIR_SCANE
|| fdctrl
->data_dir
== FD_DIR_SCANL
||
1054 fdctrl
->data_dir
== FD_DIR_SCANH
)
1056 if (dma_len
> fdctrl
->data_len
)
1057 dma_len
= fdctrl
->data_len
;
1058 if (cur_drv
->bs
== NULL
) {
1059 if (fdctrl
->data_dir
== FD_DIR_WRITE
)
1060 fdctrl_stop_transfer(fdctrl
, 0x60, 0x00, 0x00);
1062 fdctrl_stop_transfer(fdctrl
, 0x40, 0x00, 0x00);
1064 goto transfer_error
;
1066 rel_pos
= fdctrl
->data_pos
% FD_SECTOR_LEN
;
1067 for (start_pos
= fdctrl
->data_pos
; fdctrl
->data_pos
< dma_len
;) {
1068 len
= dma_len
- fdctrl
->data_pos
;
1069 if (len
+ rel_pos
> FD_SECTOR_LEN
)
1070 len
= FD_SECTOR_LEN
- rel_pos
;
1071 FLOPPY_DPRINTF("copy %d bytes (%d %d %d) %d pos %d %02x "
1072 "(%d-0x%08x 0x%08x)\n", len
, dma_len
, fdctrl
->data_pos
,
1073 fdctrl
->data_len
, fdctrl
->cur_drv
, cur_drv
->head
,
1074 cur_drv
->track
, cur_drv
->sect
, fd_sector(cur_drv
),
1075 fd_sector(cur_drv
) * 512);
1076 if (fdctrl
->data_dir
!= FD_DIR_WRITE
||
1077 len
< FD_SECTOR_LEN
|| rel_pos
!= 0) {
1078 /* READ & SCAN commands and realign to a sector for WRITE */
1079 if (bdrv_read(cur_drv
->bs
, fd_sector(cur_drv
),
1080 fdctrl
->fifo
, 1) < 0) {
1081 FLOPPY_DPRINTF("Floppy: error getting sector %d\n",
1082 fd_sector(cur_drv
));
1083 /* Sure, image size is too small... */
1084 memset(fdctrl
->fifo
, 0, FD_SECTOR_LEN
);
1087 switch (fdctrl
->data_dir
) {
1090 DMA_write_memory (nchan
, fdctrl
->fifo
+ rel_pos
,
1091 fdctrl
->data_pos
, len
);
1092 /* cpu_physical_memory_write(addr + fdctrl->data_pos, */
1093 /* fdctrl->fifo + rel_pos, len); */
1096 /* WRITE commands */
1097 DMA_read_memory (nchan
, fdctrl
->fifo
+ rel_pos
,
1098 fdctrl
->data_pos
, len
);
1099 /* cpu_physical_memory_read(addr + fdctrl->data_pos, */
1100 /* fdctrl->fifo + rel_pos, len); */
1101 if (bdrv_write(cur_drv
->bs
, fd_sector(cur_drv
),
1102 fdctrl
->fifo
, 1) < 0) {
1103 FLOPPY_ERROR("writting sector %d\n", fd_sector(cur_drv
));
1104 fdctrl_stop_transfer(fdctrl
, 0x60, 0x00, 0x00);
1105 goto transfer_error
;
1111 uint8_t tmpbuf
[FD_SECTOR_LEN
];
1113 DMA_read_memory (nchan
, tmpbuf
, fdctrl
->data_pos
, len
);
1114 /* cpu_physical_memory_read(addr + fdctrl->data_pos, */
1116 ret
= memcmp(tmpbuf
, fdctrl
->fifo
+ rel_pos
, len
);
1121 if ((ret
< 0 && fdctrl
->data_dir
== FD_DIR_SCANL
) ||
1122 (ret
> 0 && fdctrl
->data_dir
== FD_DIR_SCANH
)) {
1129 fdctrl
->data_pos
+= len
;
1130 rel_pos
= fdctrl
->data_pos
% FD_SECTOR_LEN
;
1132 /* Seek to next sector */
1133 FLOPPY_DPRINTF("seek to next sector (%d %02x %02x => %d) (%d)\n",
1134 cur_drv
->head
, cur_drv
->track
, cur_drv
->sect
,
1136 fdctrl
->data_pos
- len
);
1137 /* XXX: cur_drv->sect >= cur_drv->last_sect should be an
1139 if (cur_drv
->sect
>= cur_drv
->last_sect
||
1140 cur_drv
->sect
== fdctrl
->eot
) {
1142 if (FD_MULTI_TRACK(fdctrl
->data_state
)) {
1143 if (cur_drv
->head
== 0 &&
1144 (cur_drv
->flags
& FDISK_DBL_SIDES
) != 0) {
1149 if ((cur_drv
->flags
& FDISK_DBL_SIDES
) == 0)
1156 FLOPPY_DPRINTF("seek to next track (%d %02x %02x => %d)\n",
1157 cur_drv
->head
, cur_drv
->track
,
1158 cur_drv
->sect
, fd_sector(cur_drv
));
1165 len
= fdctrl
->data_pos
- start_pos
;
1166 FLOPPY_DPRINTF("end transfer %d %d %d\n",
1167 fdctrl
->data_pos
, len
, fdctrl
->data_len
);
1168 if (fdctrl
->data_dir
== FD_DIR_SCANE
||
1169 fdctrl
->data_dir
== FD_DIR_SCANL
||
1170 fdctrl
->data_dir
== FD_DIR_SCANH
)
1172 if (FD_DID_SEEK(fdctrl
->data_state
))
1174 fdctrl
->data_len
-= len
;
1175 // if (fdctrl->data_len == 0)
1176 fdctrl_stop_transfer(fdctrl
, status0
, status1
, status2
);
1182 /* Data register : 0x05 */
1183 static uint32_t fdctrl_read_data (fdctrl_t
*fdctrl
)
1186 uint32_t retval
= 0;
1189 cur_drv
= get_cur_drv(fdctrl
);
1190 fdctrl
->state
&= ~FD_CTRL_SLEEP
;
1191 if (FD_STATE(fdctrl
->data_state
) == FD_STATE_CMD
) {
1192 FLOPPY_ERROR("can't read data in CMD state\n");
1195 pos
= fdctrl
->data_pos
;
1196 if (FD_STATE(fdctrl
->data_state
) == FD_STATE_DATA
) {
1197 pos
%= FD_SECTOR_LEN
;
1199 len
= fdctrl
->data_len
- fdctrl
->data_pos
;
1200 if (len
> FD_SECTOR_LEN
)
1201 len
= FD_SECTOR_LEN
;
1202 bdrv_read(cur_drv
->bs
, fd_sector(cur_drv
),
1206 retval
= fdctrl
->fifo
[pos
];
1207 if (++fdctrl
->data_pos
== fdctrl
->data_len
) {
1208 fdctrl
->data_pos
= 0;
1209 /* Switch from transfer mode to status mode
1210 * then from status mode to command mode
1212 if (FD_STATE(fdctrl
->data_state
) == FD_STATE_DATA
) {
1213 fdctrl_stop_transfer(fdctrl
, 0x20, 0x00, 0x00);
1215 fdctrl_reset_fifo(fdctrl
);
1216 fdctrl_reset_irq(fdctrl
);
1219 FLOPPY_DPRINTF("data register: 0x%02x\n", retval
);
1224 static void fdctrl_format_sector (fdctrl_t
*fdctrl
)
1230 fdctrl
->cur_drv
= fdctrl
->fifo
[1] & 1;
1231 cur_drv
= get_cur_drv(fdctrl
);
1232 kt
= fdctrl
->fifo
[6];
1233 kh
= fdctrl
->fifo
[7];
1234 ks
= fdctrl
->fifo
[8];
1235 FLOPPY_DPRINTF("format sector at %d %d %02x %02x (%d)\n",
1236 fdctrl
->cur_drv
, kh
, kt
, ks
,
1237 _fd_sector(kh
, kt
, ks
, cur_drv
->last_sect
));
1239 switch (fd_seek(cur_drv
, kh
, kt
, ks
, fdctrl
->config
& 0x40)) {
1242 fdctrl_stop_transfer(fdctrl
, 0x40, 0x00, 0x00);
1243 fdctrl
->fifo
[3] = kt
;
1244 fdctrl
->fifo
[4] = kh
;
1245 fdctrl
->fifo
[5] = ks
;
1249 fdctrl_stop_transfer(fdctrl
, 0x40, 0x80, 0x00);
1250 fdctrl
->fifo
[3] = kt
;
1251 fdctrl
->fifo
[4] = kh
;
1252 fdctrl
->fifo
[5] = ks
;
1255 /* No seek enabled */
1256 fdctrl_stop_transfer(fdctrl
, 0x40, 0x00, 0x00);
1257 fdctrl
->fifo
[3] = kt
;
1258 fdctrl
->fifo
[4] = kh
;
1259 fdctrl
->fifo
[5] = ks
;
1263 fdctrl
->data_state
|= FD_STATE_SEEK
;
1268 memset(fdctrl
->fifo
, 0, FD_SECTOR_LEN
);
1269 if (cur_drv
->bs
== NULL
||
1270 bdrv_write(cur_drv
->bs
, fd_sector(cur_drv
), fdctrl
->fifo
, 1) < 0) {
1271 FLOPPY_ERROR("formatting sector %d\n", fd_sector(cur_drv
));
1272 fdctrl_stop_transfer(fdctrl
, 0x60, 0x00, 0x00);
1274 if (cur_drv
->sect
== cur_drv
->last_sect
) {
1275 fdctrl
->data_state
&= ~FD_STATE_FORMAT
;
1276 /* Last sector done */
1277 if (FD_DID_SEEK(fdctrl
->data_state
))
1278 fdctrl_stop_transfer(fdctrl
, 0x20, 0x00, 0x00);
1280 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);
1283 fdctrl
->data_pos
= 0;
1284 fdctrl
->data_len
= 4;
1289 static void fdctrl_write_data (fdctrl_t
*fdctrl
, uint32_t value
)
1293 cur_drv
= get_cur_drv(fdctrl
);
1295 if (fdctrl
->state
& FD_CTRL_RESET
) {
1296 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
1299 fdctrl
->state
&= ~FD_CTRL_SLEEP
;
1300 if (FD_STATE(fdctrl
->data_state
) == FD_STATE_STATUS
) {
1301 FLOPPY_ERROR("can't write data in status mode\n");
1304 /* Is it write command time ? */
1305 if (FD_STATE(fdctrl
->data_state
) == FD_STATE_DATA
) {
1306 /* FIFO data write */
1307 fdctrl
->fifo
[fdctrl
->data_pos
++] = value
;
1308 if (fdctrl
->data_pos
% FD_SECTOR_LEN
== (FD_SECTOR_LEN
- 1) ||
1309 fdctrl
->data_pos
== fdctrl
->data_len
) {
1310 bdrv_write(cur_drv
->bs
, fd_sector(cur_drv
),
1311 fdctrl
->fifo
, FD_SECTOR_LEN
);
1313 /* Switch from transfer mode to status mode
1314 * then from status mode to command mode
1316 if (FD_STATE(fdctrl
->data_state
) == FD_STATE_DATA
)
1317 fdctrl_stop_transfer(fdctrl
, 0x20, 0x00, 0x00);
1320 if (fdctrl
->data_pos
== 0) {
1322 switch (value
& 0x5F) {
1325 FLOPPY_DPRINTF("READ command\n");
1326 /* 8 parameters cmd */
1327 fdctrl
->data_len
= 9;
1330 /* READ_DELETED variants */
1331 FLOPPY_DPRINTF("READ_DELETED command\n");
1332 /* 8 parameters cmd */
1333 fdctrl
->data_len
= 9;
1336 /* SCAN_EQUAL variants */
1337 FLOPPY_DPRINTF("SCAN_EQUAL command\n");
1338 /* 8 parameters cmd */
1339 fdctrl
->data_len
= 9;
1342 /* VERIFY variants */
1343 FLOPPY_DPRINTF("VERIFY command\n");
1344 /* 8 parameters cmd */
1345 fdctrl
->data_len
= 9;
1348 /* SCAN_LOW_OR_EQUAL variants */
1349 FLOPPY_DPRINTF("SCAN_LOW_OR_EQUAL command\n");
1350 /* 8 parameters cmd */
1351 fdctrl
->data_len
= 9;
1354 /* SCAN_HIGH_OR_EQUAL variants */
1355 FLOPPY_DPRINTF("SCAN_HIGH_OR_EQUAL command\n");
1356 /* 8 parameters cmd */
1357 fdctrl
->data_len
= 9;
1362 switch (value
& 0x7F) {
1364 /* WRITE variants */
1365 FLOPPY_DPRINTF("WRITE command\n");
1366 /* 8 parameters cmd */
1367 fdctrl
->data_len
= 9;
1370 /* WRITE_DELETED variants */
1371 FLOPPY_DPRINTF("WRITE_DELETED command\n");
1372 /* 8 parameters cmd */
1373 fdctrl
->data_len
= 9;
1381 FLOPPY_DPRINTF("SPECIFY command\n");
1382 /* 1 parameter cmd */
1383 fdctrl
->data_len
= 3;
1386 /* SENSE_DRIVE_STATUS */
1387 FLOPPY_DPRINTF("SENSE_DRIVE_STATUS command\n");
1388 /* 1 parameter cmd */
1389 fdctrl
->data_len
= 2;
1393 FLOPPY_DPRINTF("RECALIBRATE command\n");
1394 /* 1 parameter cmd */
1395 fdctrl
->data_len
= 2;
1398 /* SENSE_INTERRUPT_STATUS */
1399 FLOPPY_DPRINTF("SENSE_INTERRUPT_STATUS command (%02x)\n",
1400 fdctrl
->int_status
);
1401 /* No parameters cmd: returns status if no interrupt */
1404 fdctrl
->int_status
| (cur_drv
->head
<< 2) | fdctrl
->cur_drv
;
1406 /* XXX: int_status handling is broken for read/write
1407 commands, so we do this hack. It should be suppressed
1410 0x20 | (cur_drv
->head
<< 2) | fdctrl
->cur_drv
;
1412 fdctrl
->fifo
[1] = cur_drv
->track
;
1413 fdctrl_set_fifo(fdctrl
, 2, 0);
1414 fdctrl_reset_irq(fdctrl
);
1415 fdctrl
->int_status
= 0xC0;
1419 FLOPPY_DPRINTF("DUMPREG command\n");
1420 /* Drives position */
1421 fdctrl
->fifo
[0] = drv0(fdctrl
)->track
;
1422 fdctrl
->fifo
[1] = drv1(fdctrl
)->track
;
1423 fdctrl
->fifo
[2] = 0;
1424 fdctrl
->fifo
[3] = 0;
1426 fdctrl
->fifo
[4] = fdctrl
->timer0
;
1427 fdctrl
->fifo
[5] = (fdctrl
->timer1
<< 1) | fdctrl
->dma_en
;
1428 fdctrl
->fifo
[6] = cur_drv
->last_sect
;
1429 fdctrl
->fifo
[7] = (fdctrl
->lock
<< 7) |
1430 (cur_drv
->perpendicular
<< 2);
1431 fdctrl
->fifo
[8] = fdctrl
->config
;
1432 fdctrl
->fifo
[9] = fdctrl
->precomp_trk
;
1433 fdctrl_set_fifo(fdctrl
, 10, 0);
1437 FLOPPY_DPRINTF("SEEK command\n");
1438 /* 2 parameters cmd */
1439 fdctrl
->data_len
= 3;
1443 FLOPPY_DPRINTF("VERSION command\n");
1444 /* No parameters cmd */
1445 /* Controller's version */
1446 fdctrl
->fifo
[0] = fdctrl
->version
;
1447 fdctrl_set_fifo(fdctrl
, 1, 1);
1450 /* PERPENDICULAR_MODE */
1451 FLOPPY_DPRINTF("PERPENDICULAR_MODE command\n");
1452 /* 1 parameter cmd */
1453 fdctrl
->data_len
= 2;
1457 FLOPPY_DPRINTF("CONFIGURE command\n");
1458 /* 3 parameters cmd */
1459 fdctrl
->data_len
= 4;
1463 FLOPPY_DPRINTF("UNLOCK command\n");
1464 /* No parameters cmd */
1466 fdctrl
->fifo
[0] = 0;
1467 fdctrl_set_fifo(fdctrl
, 1, 0);
1470 /* POWERDOWN_MODE */
1471 FLOPPY_DPRINTF("POWERDOWN_MODE command\n");
1472 /* 2 parameters cmd */
1473 fdctrl
->data_len
= 3;
1477 FLOPPY_DPRINTF("PART_ID command\n");
1478 /* No parameters cmd */
1479 fdctrl
->fifo
[0] = 0x41; /* Stepping 1 */
1480 fdctrl_set_fifo(fdctrl
, 1, 0);
1484 FLOPPY_DPRINTF("SAVE command\n");
1485 /* No parameters cmd */
1486 fdctrl
->fifo
[0] = 0;
1487 fdctrl
->fifo
[1] = 0;
1488 /* Drives position */
1489 fdctrl
->fifo
[2] = drv0(fdctrl
)->track
;
1490 fdctrl
->fifo
[3] = drv1(fdctrl
)->track
;
1491 fdctrl
->fifo
[4] = 0;
1492 fdctrl
->fifo
[5] = 0;
1494 fdctrl
->fifo
[6] = fdctrl
->timer0
;
1495 fdctrl
->fifo
[7] = fdctrl
->timer1
;
1496 fdctrl
->fifo
[8] = cur_drv
->last_sect
;
1497 fdctrl
->fifo
[9] = (fdctrl
->lock
<< 7) |
1498 (cur_drv
->perpendicular
<< 2);
1499 fdctrl
->fifo
[10] = fdctrl
->config
;
1500 fdctrl
->fifo
[11] = fdctrl
->precomp_trk
;
1501 fdctrl
->fifo
[12] = fdctrl
->pwrd
;
1502 fdctrl
->fifo
[13] = 0;
1503 fdctrl
->fifo
[14] = 0;
1504 fdctrl_set_fifo(fdctrl
, 15, 1);
1508 FLOPPY_DPRINTF("OPTION command\n");
1509 /* 1 parameter cmd */
1510 fdctrl
->data_len
= 2;
1514 FLOPPY_DPRINTF("READ_TRACK command\n");
1515 /* 8 parameters cmd */
1516 fdctrl
->data_len
= 9;
1520 FLOPPY_DPRINTF("READ_ID command\n");
1521 /* 1 parameter cmd */
1522 fdctrl
->data_len
= 2;
1526 FLOPPY_DPRINTF("RESTORE command\n");
1527 /* 17 parameters cmd */
1528 fdctrl
->data_len
= 18;
1532 FLOPPY_DPRINTF("FORMAT_TRACK command\n");
1533 /* 5 parameters cmd */
1534 fdctrl
->data_len
= 6;
1537 /* DRIVE_SPECIFICATION_COMMAND */
1538 FLOPPY_DPRINTF("DRIVE_SPECIFICATION_COMMAND command\n");
1539 /* 5 parameters cmd */
1540 fdctrl
->data_len
= 6;
1543 /* RELATIVE_SEEK_OUT */
1544 FLOPPY_DPRINTF("RELATIVE_SEEK_OUT command\n");
1545 /* 2 parameters cmd */
1546 fdctrl
->data_len
= 3;
1550 FLOPPY_DPRINTF("LOCK command\n");
1551 /* No parameters cmd */
1553 fdctrl
->fifo
[0] = 0x10;
1554 fdctrl_set_fifo(fdctrl
, 1, 1);
1557 /* FORMAT_AND_WRITE */
1558 FLOPPY_DPRINTF("FORMAT_AND_WRITE command\n");
1559 /* 10 parameters cmd */
1560 fdctrl
->data_len
= 11;
1563 /* RELATIVE_SEEK_IN */
1564 FLOPPY_DPRINTF("RELATIVE_SEEK_IN command\n");
1565 /* 2 parameters cmd */
1566 fdctrl
->data_len
= 3;
1569 /* Unknown command */
1570 FLOPPY_ERROR("unknown command: 0x%02x\n", value
);
1571 fdctrl_unimplemented(fdctrl
);
1576 FLOPPY_DPRINTF("%s: %02x\n", __func__
, value
);
1577 fdctrl
->fifo
[fdctrl
->data_pos
] = value
;
1578 if (++fdctrl
->data_pos
== fdctrl
->data_len
) {
1579 /* We now have all parameters
1580 * and will be able to treat the command
1582 if (fdctrl
->data_state
& FD_STATE_FORMAT
) {
1583 fdctrl_format_sector(fdctrl
);
1586 switch (fdctrl
->fifo
[0] & 0x1F) {
1590 FLOPPY_DPRINTF("treat READ command\n");
1591 fdctrl_start_transfer(fdctrl
, FD_DIR_READ
);
1595 /* READ_DELETED variants */
1596 // FLOPPY_DPRINTF("treat READ_DELETED command\n");
1597 FLOPPY_ERROR("treat READ_DELETED command\n");
1598 fdctrl_start_transfer_del(fdctrl
, FD_DIR_READ
);
1601 /* VERIFY variants */
1602 // FLOPPY_DPRINTF("treat VERIFY command\n");
1603 FLOPPY_ERROR("treat VERIFY command\n");
1604 fdctrl_stop_transfer(fdctrl
, 0x20, 0x00, 0x00);
1607 /* SCAN_EQUAL variants */
1608 // FLOPPY_DPRINTF("treat SCAN_EQUAL command\n");
1609 FLOPPY_ERROR("treat SCAN_EQUAL command\n");
1610 fdctrl_start_transfer(fdctrl
, FD_DIR_SCANE
);
1613 /* SCAN_LOW_OR_EQUAL variants */
1614 // FLOPPY_DPRINTF("treat SCAN_LOW_OR_EQUAL command\n");
1615 FLOPPY_ERROR("treat SCAN_LOW_OR_EQUAL command\n");
1616 fdctrl_start_transfer(fdctrl
, FD_DIR_SCANL
);
1619 /* SCAN_HIGH_OR_EQUAL variants */
1620 // FLOPPY_DPRINTF("treat SCAN_HIGH_OR_EQUAL command\n");
1621 FLOPPY_ERROR("treat SCAN_HIGH_OR_EQUAL command\n");
1622 fdctrl_start_transfer(fdctrl
, FD_DIR_SCANH
);
1627 switch (fdctrl
->fifo
[0] & 0x3F) {
1629 /* WRITE variants */
1630 FLOPPY_DPRINTF("treat WRITE command (%02x)\n", fdctrl
->fifo
[0]);
1631 fdctrl_start_transfer(fdctrl
, FD_DIR_WRITE
);
1634 /* WRITE_DELETED variants */
1635 // FLOPPY_DPRINTF("treat WRITE_DELETED command\n");
1636 FLOPPY_ERROR("treat WRITE_DELETED command\n");
1637 fdctrl_start_transfer_del(fdctrl
, FD_DIR_WRITE
);
1642 switch (fdctrl
->fifo
[0]) {
1645 FLOPPY_DPRINTF("treat SPECIFY command\n");
1646 fdctrl
->timer0
= (fdctrl
->fifo
[1] >> 4) & 0xF;
1647 fdctrl
->timer1
= fdctrl
->fifo
[2] >> 1;
1648 fdctrl
->dma_en
= 1 - (fdctrl
->fifo
[2] & 1) ;
1649 /* No result back */
1650 fdctrl_reset_fifo(fdctrl
);
1653 /* SENSE_DRIVE_STATUS */
1654 FLOPPY_DPRINTF("treat SENSE_DRIVE_STATUS command\n");
1655 fdctrl
->cur_drv
= fdctrl
->fifo
[1] & 1;
1656 cur_drv
= get_cur_drv(fdctrl
);
1657 cur_drv
->head
= (fdctrl
->fifo
[1] >> 2) & 1;
1658 /* 1 Byte status back */
1659 fdctrl
->fifo
[0] = (cur_drv
->ro
<< 6) |
1660 (cur_drv
->track
== 0 ? 0x10 : 0x00) |
1661 (cur_drv
->head
<< 2) |
1664 fdctrl_set_fifo(fdctrl
, 1, 0);
1668 FLOPPY_DPRINTF("treat RECALIBRATE command\n");
1669 fdctrl
->cur_drv
= fdctrl
->fifo
[1] & 1;
1670 cur_drv
= get_cur_drv(fdctrl
);
1671 fd_recalibrate(cur_drv
);
1672 fdctrl_reset_fifo(fdctrl
);
1673 /* Raise Interrupt */
1674 fdctrl_raise_irq(fdctrl
, 0x20);
1678 FLOPPY_DPRINTF("treat SEEK command\n");
1679 fdctrl
->cur_drv
= fdctrl
->fifo
[1] & 1;
1680 cur_drv
= get_cur_drv(fdctrl
);
1682 if (fdctrl
->fifo
[2] <= cur_drv
->track
)
1686 fdctrl_reset_fifo(fdctrl
);
1687 if (fdctrl
->fifo
[2] > cur_drv
->max_track
) {
1688 fdctrl_raise_irq(fdctrl
, 0x60);
1690 cur_drv
->track
= fdctrl
->fifo
[2];
1691 /* Raise Interrupt */
1692 fdctrl_raise_irq(fdctrl
, 0x20);
1696 /* PERPENDICULAR_MODE */
1697 FLOPPY_DPRINTF("treat PERPENDICULAR_MODE command\n");
1698 if (fdctrl
->fifo
[1] & 0x80)
1699 cur_drv
->perpendicular
= fdctrl
->fifo
[1] & 0x7;
1700 /* No result back */
1701 fdctrl_reset_fifo(fdctrl
);
1705 FLOPPY_DPRINTF("treat CONFIGURE command\n");
1706 fdctrl
->config
= fdctrl
->fifo
[2];
1707 fdctrl
->precomp_trk
= fdctrl
->fifo
[3];
1708 /* No result back */
1709 fdctrl_reset_fifo(fdctrl
);
1712 /* POWERDOWN_MODE */
1713 FLOPPY_DPRINTF("treat POWERDOWN_MODE command\n");
1714 fdctrl
->pwrd
= fdctrl
->fifo
[1];
1715 fdctrl
->fifo
[0] = fdctrl
->fifo
[1];
1716 fdctrl_set_fifo(fdctrl
, 1, 1);
1720 FLOPPY_DPRINTF("treat OPTION command\n");
1721 /* No result back */
1722 fdctrl_reset_fifo(fdctrl
);
1726 // FLOPPY_DPRINTF("treat READ_TRACK command\n");
1727 FLOPPY_ERROR("treat READ_TRACK command\n");
1728 fdctrl_start_transfer(fdctrl
, FD_DIR_READ
);
1732 FLOPPY_DPRINTF("treat READ_ID command\n");
1733 /* XXX: should set main status register to busy */
1734 cur_drv
->head
= (fdctrl
->fifo
[1] >> 2) & 1;
1735 qemu_mod_timer(fdctrl
->result_timer
,
1736 qemu_get_clock(vm_clock
) + (ticks_per_sec
/ 50));
1740 FLOPPY_DPRINTF("treat RESTORE command\n");
1741 /* Drives position */
1742 drv0(fdctrl
)->track
= fdctrl
->fifo
[3];
1743 drv1(fdctrl
)->track
= fdctrl
->fifo
[4];
1745 fdctrl
->timer0
= fdctrl
->fifo
[7];
1746 fdctrl
->timer1
= fdctrl
->fifo
[8];
1747 cur_drv
->last_sect
= fdctrl
->fifo
[9];
1748 fdctrl
->lock
= fdctrl
->fifo
[10] >> 7;
1749 cur_drv
->perpendicular
= (fdctrl
->fifo
[10] >> 2) & 0xF;
1750 fdctrl
->config
= fdctrl
->fifo
[11];
1751 fdctrl
->precomp_trk
= fdctrl
->fifo
[12];
1752 fdctrl
->pwrd
= fdctrl
->fifo
[13];
1753 fdctrl_reset_fifo(fdctrl
);
1757 FLOPPY_DPRINTF("treat FORMAT_TRACK command\n");
1758 fdctrl
->cur_drv
= fdctrl
->fifo
[1] & 1;
1759 cur_drv
= get_cur_drv(fdctrl
);
1760 fdctrl
->data_state
|= FD_STATE_FORMAT
;
1761 if (fdctrl
->fifo
[0] & 0x80)
1762 fdctrl
->data_state
|= FD_STATE_MULTI
;
1764 fdctrl
->data_state
&= ~FD_STATE_MULTI
;
1765 fdctrl
->data_state
&= ~FD_STATE_SEEK
;
1767 fdctrl
->fifo
[2] > 7 ? 16384 : 128 << fdctrl
->fifo
[2];
1769 cur_drv
->last_sect
=
1770 cur_drv
->flags
& FDISK_DBL_SIDES
? fdctrl
->fifo
[3] :
1771 fdctrl
->fifo
[3] / 2;
1773 cur_drv
->last_sect
= fdctrl
->fifo
[3];
1775 /* TODO: implement format using DMA expected by the Bochs BIOS
1776 * and Linux fdformat (read 3 bytes per sector via DMA and fill
1777 * the sector with the specified fill byte
1779 fdctrl
->data_state
&= ~FD_STATE_FORMAT
;
1780 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);
1783 /* DRIVE_SPECIFICATION_COMMAND */
1784 FLOPPY_DPRINTF("treat DRIVE_SPECIFICATION_COMMAND command\n");
1785 if (fdctrl
->fifo
[fdctrl
->data_pos
- 1] & 0x80) {
1786 /* Command parameters done */
1787 if (fdctrl
->fifo
[fdctrl
->data_pos
- 1] & 0x40) {
1788 fdctrl
->fifo
[0] = fdctrl
->fifo
[1];
1789 fdctrl
->fifo
[2] = 0;
1790 fdctrl
->fifo
[3] = 0;
1791 fdctrl_set_fifo(fdctrl
, 4, 1);
1793 fdctrl_reset_fifo(fdctrl
);
1795 } else if (fdctrl
->data_len
> 7) {
1797 fdctrl
->fifo
[0] = 0x80 |
1798 (cur_drv
->head
<< 2) | fdctrl
->cur_drv
;
1799 fdctrl_set_fifo(fdctrl
, 1, 1);
1803 /* RELATIVE_SEEK_OUT */
1804 FLOPPY_DPRINTF("treat RELATIVE_SEEK_OUT command\n");
1805 fdctrl
->cur_drv
= fdctrl
->fifo
[1] & 1;
1806 cur_drv
= get_cur_drv(fdctrl
);
1809 if (fdctrl
->fifo
[2] + cur_drv
->track
>= cur_drv
->max_track
) {
1810 cur_drv
->track
= cur_drv
->max_track
- 1;
1812 cur_drv
->track
+= fdctrl
->fifo
[2];
1814 fdctrl_reset_fifo(fdctrl
);
1815 fdctrl_raise_irq(fdctrl
, 0x20);
1818 /* FORMAT_AND_WRITE */
1819 // FLOPPY_DPRINTF("treat FORMAT_AND_WRITE command\n");
1820 FLOPPY_ERROR("treat FORMAT_AND_WRITE command\n");
1821 fdctrl_unimplemented(fdctrl
);
1824 /* RELATIVE_SEEK_IN */
1825 FLOPPY_DPRINTF("treat RELATIVE_SEEK_IN command\n");
1826 fdctrl
->cur_drv
= fdctrl
->fifo
[1] & 1;
1827 cur_drv
= get_cur_drv(fdctrl
);
1830 if (fdctrl
->fifo
[2] > cur_drv
->track
) {
1833 cur_drv
->track
-= fdctrl
->fifo
[2];
1835 fdctrl_reset_fifo(fdctrl
);
1836 /* Raise Interrupt */
1837 fdctrl_raise_irq(fdctrl
, 0x20);
1843 static void fdctrl_result_timer(void *opaque
)
1845 fdctrl_t
*fdctrl
= opaque
;
1846 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);