2 * QEMU Floppy disk emulator (Intel 82078)
4 * Copyright (c) 2003 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 */
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
, reg
);
470 static void fdctrl_write_mem (void *opaque
,
471 target_phys_addr_t reg
, uint32_t value
)
473 fdctrl_write(opaque
, reg
, value
);
476 static CPUReadMemoryFunc
*fdctrl_mem_read
[3] = {
482 static CPUWriteMemoryFunc
*fdctrl_mem_write
[3] = {
488 fdctrl_t
*fdctrl_init (int irq_lvl
, int dma_chann
, int mem_mapped
,
490 BlockDriverState
**fds
)
496 FLOPPY_DPRINTF("init controller\n");
497 fdctrl
= qemu_mallocz(sizeof(fdctrl_t
));
500 fdctrl
->result_timer
= qemu_new_timer(vm_clock
,
501 fdctrl_result_timer
, fdctrl
);
503 fdctrl
->version
= 0x90; /* Intel 82078 controller */
504 fdctrl
->irq_lvl
= irq_lvl
;
505 fdctrl
->dma_chann
= dma_chann
;
506 fdctrl
->io_base
= io_base
;
507 fdctrl
->config
= 0x60; /* Implicit seek, polling & FIFO enabled */
508 if (fdctrl
->dma_chann
!= -1) {
510 DMA_register_channel(dma_chann
, &fdctrl_transfer_handler
, fdctrl
);
514 for (i
= 0; i
< 2; i
++) {
515 fd_init(&fdctrl
->drives
[i
], fds
[i
]);
517 fdctrl_reset(fdctrl
, 0);
518 fdctrl
->state
= FD_CTRL_ACTIVE
;
520 io_mem
= cpu_register_io_memory(0, fdctrl_mem_read
, fdctrl_mem_write
, fdctrl
);
521 cpu_register_physical_memory(io_base
, 0x08, io_mem
);
523 register_ioport_read(io_base
+ 0x01, 5, 1, &fdctrl_read
, fdctrl
);
524 register_ioport_read(io_base
+ 0x07, 1, 1, &fdctrl_read
, fdctrl
);
525 register_ioport_write(io_base
+ 0x01, 5, 1, &fdctrl_write
, fdctrl
);
526 register_ioport_write(io_base
+ 0x07, 1, 1, &fdctrl_write
, fdctrl
);
528 for (i
= 0; i
< 2; i
++) {
529 fd_revalidate(&fdctrl
->drives
[i
]);
535 /* XXX: may change if moved to bdrv */
536 int fdctrl_get_drive_type(fdctrl_t
*fdctrl
, int drive_num
)
538 return fdctrl
->drives
[drive_num
].drive
;
541 /* Change IRQ state */
542 static void fdctrl_reset_irq (fdctrl_t
*fdctrl
)
544 FLOPPY_DPRINTF("Reset interrupt\n");
545 pic_set_irq(fdctrl
->irq_lvl
, 0);
546 fdctrl
->state
&= ~FD_CTRL_INTR
;
549 static void fdctrl_raise_irq (fdctrl_t
*fdctrl
, uint8_t status
)
553 if (!fdctrl
->dma_en
) {
554 fdctrl
->state
&= ~FD_CTRL_BUSY
;
555 fdctrl
->int_status
= status
;
559 if (~(fdctrl
->state
& FD_CTRL_INTR
)) {
560 pic_set_irq(fdctrl
->irq_lvl
, 1);
561 fdctrl
->state
|= FD_CTRL_INTR
;
563 FLOPPY_DPRINTF("Set interrupt status to 0x%02x\n", status
);
564 fdctrl
->int_status
= status
;
567 /* Reset controller */
568 static void fdctrl_reset (fdctrl_t
*fdctrl
, int do_irq
)
572 FLOPPY_DPRINTF("reset controller\n");
573 fdctrl_reset_irq(fdctrl
);
574 /* Initialise controller */
577 fdctrl
->data_pos
= 0;
578 fdctrl
->data_len
= 0;
579 fdctrl
->data_state
= FD_STATE_CMD
;
580 fdctrl
->data_dir
= FD_DIR_WRITE
;
581 for (i
= 0; i
< MAX_FD
; i
++)
582 fd_reset(&fdctrl
->drives
[i
]);
583 fdctrl_reset_fifo(fdctrl
);
585 fdctrl_raise_irq(fdctrl
, 0xc0);
588 static inline fdrive_t
*drv0 (fdctrl_t
*fdctrl
)
590 return &fdctrl
->drives
[fdctrl
->bootsel
];
593 static inline fdrive_t
*drv1 (fdctrl_t
*fdctrl
)
595 return &fdctrl
->drives
[1 - fdctrl
->bootsel
];
598 static fdrive_t
*get_cur_drv (fdctrl_t
*fdctrl
)
600 return fdctrl
->cur_drv
== 0 ? drv0(fdctrl
) : drv1(fdctrl
);
603 /* Status B register : 0x01 (read-only) */
604 static uint32_t fdctrl_read_statusB (fdctrl_t
*fdctrl
)
606 FLOPPY_DPRINTF("status register: 0x00\n");
610 /* Digital output register : 0x02 */
611 static uint32_t fdctrl_read_dor (fdctrl_t
*fdctrl
)
615 /* Drive motors state indicators */
616 if (drv0(fdctrl
)->drflags
& FDRIVE_MOTOR_ON
)
618 if (drv1(fdctrl
)->drflags
& FDRIVE_MOTOR_ON
)
621 retval
|= fdctrl
->dma_en
<< 3;
622 /* Reset indicator */
623 retval
|= (fdctrl
->state
& FD_CTRL_RESET
) == 0 ? 0x04 : 0;
625 retval
|= fdctrl
->cur_drv
;
626 FLOPPY_DPRINTF("digital output register: 0x%02x\n", retval
);
631 static void fdctrl_write_dor (fdctrl_t
*fdctrl
, uint32_t value
)
634 if (fdctrl
->state
& FD_CTRL_RESET
) {
635 if (!(value
& 0x04)) {
636 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
640 FLOPPY_DPRINTF("digital output register set to 0x%02x\n", value
);
641 /* Drive motors state indicators */
643 fd_start(drv1(fdctrl
));
645 fd_stop(drv1(fdctrl
));
647 fd_start(drv0(fdctrl
));
649 fd_stop(drv0(fdctrl
));
652 if (fdctrl
->dma_chann
!= -1)
653 fdctrl
->dma_en
= 1 - ((value
>> 3) & 1);
656 if (!(value
& 0x04)) {
657 if (!(fdctrl
->state
& FD_CTRL_RESET
)) {
658 FLOPPY_DPRINTF("controller enter RESET state\n");
659 fdctrl
->state
|= FD_CTRL_RESET
;
662 if (fdctrl
->state
& FD_CTRL_RESET
) {
663 FLOPPY_DPRINTF("controller out of RESET state\n");
664 fdctrl_reset(fdctrl
, 1);
665 fdctrl
->state
&= ~(FD_CTRL_RESET
| FD_CTRL_SLEEP
);
669 fdctrl
->cur_drv
= value
& 1;
672 /* Tape drive register : 0x03 */
673 static uint32_t fdctrl_read_tape (fdctrl_t
*fdctrl
)
677 /* Disk boot selection indicator */
678 retval
|= fdctrl
->bootsel
<< 2;
679 /* Tape indicators: never allowed */
680 FLOPPY_DPRINTF("tape drive register: 0x%02x\n", retval
);
685 static void fdctrl_write_tape (fdctrl_t
*fdctrl
, uint32_t value
)
688 if (fdctrl
->state
& FD_CTRL_RESET
) {
689 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
692 FLOPPY_DPRINTF("tape drive register set to 0x%02x\n", value
);
693 /* Disk boot selection indicator */
694 fdctrl
->bootsel
= (value
>> 2) & 1;
695 /* Tape indicators: never allow */
698 /* Main status register : 0x04 (read) */
699 static uint32_t fdctrl_read_main_status (fdctrl_t
*fdctrl
)
703 fdctrl
->state
&= ~(FD_CTRL_SLEEP
| FD_CTRL_RESET
);
704 if (!(fdctrl
->state
& FD_CTRL_BUSY
)) {
705 /* Data transfer allowed */
707 /* Data transfer direction indicator */
708 if (fdctrl
->data_dir
== FD_DIR_READ
)
711 /* Should handle 0x20 for SPECIFY command */
712 /* Command busy indicator */
713 if (FD_STATE(fdctrl
->data_state
) == FD_STATE_DATA
||
714 FD_STATE(fdctrl
->data_state
) == FD_STATE_STATUS
)
716 FLOPPY_DPRINTF("main status register: 0x%02x\n", retval
);
721 /* Data select rate register : 0x04 (write) */
722 static void fdctrl_write_rate (fdctrl_t
*fdctrl
, uint32_t value
)
725 if (fdctrl
->state
& FD_CTRL_RESET
) {
726 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
729 FLOPPY_DPRINTF("select rate register set to 0x%02x\n", value
);
730 /* Reset: autoclear */
732 fdctrl
->state
|= FD_CTRL_RESET
;
733 fdctrl_reset(fdctrl
, 1);
734 fdctrl
->state
&= ~FD_CTRL_RESET
;
737 fdctrl
->state
|= FD_CTRL_SLEEP
;
738 fdctrl_reset(fdctrl
, 1);
740 // fdctrl.precomp = (value >> 2) & 0x07;
743 static int fdctrl_media_changed(fdrive_t
*drv
)
748 ret
= bdrv_media_changed(drv
->bs
);
755 /* Digital input register : 0x07 (read-only) */
756 static uint32_t fdctrl_read_dir (fdctrl_t
*fdctrl
)
760 if (fdctrl_media_changed(drv0(fdctrl
)) ||
761 fdctrl_media_changed(drv1(fdctrl
)))
764 FLOPPY_DPRINTF("Floppy digital input register: 0x%02x\n", retval
);
769 /* FIFO state control */
770 static void fdctrl_reset_fifo (fdctrl_t
*fdctrl
)
772 fdctrl
->data_dir
= FD_DIR_WRITE
;
773 fdctrl
->data_pos
= 0;
774 FD_SET_STATE(fdctrl
->data_state
, FD_STATE_CMD
);
777 /* Set FIFO status for the host to read */
778 static void fdctrl_set_fifo (fdctrl_t
*fdctrl
, int fifo_len
, int do_irq
)
780 fdctrl
->data_dir
= FD_DIR_READ
;
781 fdctrl
->data_len
= fifo_len
;
782 fdctrl
->data_pos
= 0;
783 FD_SET_STATE(fdctrl
->data_state
, FD_STATE_STATUS
);
785 fdctrl_raise_irq(fdctrl
, 0x00);
788 /* Set an error: unimplemented/unknown command */
789 static void fdctrl_unimplemented (fdctrl_t
*fdctrl
)
794 cur_drv
= get_cur_drv(fdctrl
);
795 fdctrl
->fifo
[0] = 0x60 | (cur_drv
->head
<< 2) | fdctrl
->cur_drv
;
796 fdctrl
->fifo
[1] = 0x00;
797 fdctrl
->fifo
[2] = 0x00;
798 fdctrl_set_fifo(fdctrl
, 3, 1);
800 // fdctrl_reset_fifo(fdctrl);
801 fdctrl
->fifo
[0] = 0x80;
802 fdctrl_set_fifo(fdctrl
, 1, 0);
806 /* Callback for transfer end (stop or abort) */
807 static void fdctrl_stop_transfer (fdctrl_t
*fdctrl
, uint8_t status0
,
808 uint8_t status1
, uint8_t status2
)
812 cur_drv
= get_cur_drv(fdctrl
);
813 FLOPPY_DPRINTF("transfer status: %02x %02x %02x (%02x)\n",
814 status0
, status1
, status2
,
815 status0
| (cur_drv
->head
<< 2) | fdctrl
->cur_drv
);
816 fdctrl
->fifo
[0] = status0
| (cur_drv
->head
<< 2) | fdctrl
->cur_drv
;
817 fdctrl
->fifo
[1] = status1
;
818 fdctrl
->fifo
[2] = status2
;
819 fdctrl
->fifo
[3] = cur_drv
->track
;
820 fdctrl
->fifo
[4] = cur_drv
->head
;
821 fdctrl
->fifo
[5] = cur_drv
->sect
;
822 fdctrl
->fifo
[6] = FD_SECTOR_SC
;
823 fdctrl
->data_dir
= FD_DIR_READ
;
824 if (fdctrl
->state
& FD_CTRL_BUSY
) {
825 DMA_release_DREQ(fdctrl
->dma_chann
);
826 fdctrl
->state
&= ~FD_CTRL_BUSY
;
828 fdctrl_set_fifo(fdctrl
, 7, 1);
831 /* Prepare a data transfer (either DMA or FIFO) */
832 static void fdctrl_start_transfer (fdctrl_t
*fdctrl
, int direction
)
838 fdctrl
->cur_drv
= fdctrl
->fifo
[1] & 1;
839 cur_drv
= get_cur_drv(fdctrl
);
840 kt
= fdctrl
->fifo
[2];
841 kh
= fdctrl
->fifo
[3];
842 ks
= fdctrl
->fifo
[4];
843 FLOPPY_DPRINTF("Start transfer at %d %d %02x %02x (%d)\n",
844 fdctrl
->cur_drv
, kh
, kt
, ks
,
845 _fd_sector(kh
, kt
, ks
, cur_drv
->last_sect
));
847 switch (fd_seek(cur_drv
, kh
, kt
, ks
, fdctrl
->config
& 0x40)) {
850 fdctrl_stop_transfer(fdctrl
, 0x40, 0x00, 0x00);
851 fdctrl
->fifo
[3] = kt
;
852 fdctrl
->fifo
[4] = kh
;
853 fdctrl
->fifo
[5] = ks
;
857 fdctrl_stop_transfer(fdctrl
, 0x40, 0x80, 0x00);
858 fdctrl
->fifo
[3] = kt
;
859 fdctrl
->fifo
[4] = kh
;
860 fdctrl
->fifo
[5] = ks
;
863 /* No seek enabled */
864 fdctrl_stop_transfer(fdctrl
, 0x40, 0x00, 0x00);
865 fdctrl
->fifo
[3] = kt
;
866 fdctrl
->fifo
[4] = kh
;
867 fdctrl
->fifo
[5] = ks
;
875 /* Set the FIFO state */
876 fdctrl
->data_dir
= direction
;
877 fdctrl
->data_pos
= 0;
878 FD_SET_STATE(fdctrl
->data_state
, FD_STATE_DATA
); /* FIFO ready for data */
879 if (fdctrl
->fifo
[0] & 0x80)
880 fdctrl
->data_state
|= FD_STATE_MULTI
;
882 fdctrl
->data_state
&= ~FD_STATE_MULTI
;
884 fdctrl
->data_state
|= FD_STATE_SEEK
;
886 fdctrl
->data_state
&= ~FD_STATE_SEEK
;
887 if (fdctrl
->fifo
[5] == 00) {
888 fdctrl
->data_len
= fdctrl
->fifo
[8];
891 fdctrl
->data_len
= 128 << (fdctrl
->fifo
[5] > 7 ? 7 : fdctrl
->fifo
[5]);
892 tmp
= (cur_drv
->last_sect
- ks
+ 1);
893 if (fdctrl
->fifo
[0] & 0x80)
894 tmp
+= cur_drv
->last_sect
;
895 fdctrl
->data_len
*= tmp
;
897 fdctrl
->eot
= fdctrl
->fifo
[6];
898 if (fdctrl
->dma_en
) {
900 /* DMA transfer are enabled. Check if DMA channel is well programmed */
901 dma_mode
= DMA_get_channel_mode(fdctrl
->dma_chann
);
902 dma_mode
= (dma_mode
>> 2) & 3;
903 FLOPPY_DPRINTF("dma_mode=%d direction=%d (%d - %d)\n",
905 (128 << fdctrl
->fifo
[5]) *
906 (cur_drv
->last_sect
- ks
+ 1), fdctrl
->data_len
);
907 if (((direction
== FD_DIR_SCANE
|| direction
== FD_DIR_SCANL
||
908 direction
== FD_DIR_SCANH
) && dma_mode
== 0) ||
909 (direction
== FD_DIR_WRITE
&& dma_mode
== 2) ||
910 (direction
== FD_DIR_READ
&& dma_mode
== 1)) {
911 /* No access is allowed until DMA transfer has completed */
912 fdctrl
->state
|= FD_CTRL_BUSY
;
913 /* Now, we just have to wait for the DMA controller to
916 DMA_hold_DREQ(fdctrl
->dma_chann
);
917 DMA_schedule(fdctrl
->dma_chann
);
920 FLOPPY_ERROR("dma_mode=%d direction=%d\n", dma_mode
, direction
);
923 FLOPPY_DPRINTF("start non-DMA transfer\n");
924 /* IO based transfer: calculate len */
925 fdctrl_raise_irq(fdctrl
, 0x00);
930 /* Prepare a transfer of deleted data */
931 static void fdctrl_start_transfer_del (fdctrl_t
*fdctrl
, int direction
)
933 /* We don't handle deleted data,
934 * so we don't return *ANYTHING*
936 fdctrl_stop_transfer(fdctrl
, 0x60, 0x00, 0x00);
939 /* handlers for DMA transfers */
940 static int fdctrl_transfer_handler (void *opaque
, int nchan
,
941 int dma_pos
, int dma_len
)
945 int len
, start_pos
, rel_pos
;
946 uint8_t status0
= 0x00, status1
= 0x00, status2
= 0x00;
949 if (!(fdctrl
->state
& FD_CTRL_BUSY
)) {
950 FLOPPY_DPRINTF("Not in DMA transfer mode !\n");
953 cur_drv
= get_cur_drv(fdctrl
);
954 if (fdctrl
->data_dir
== FD_DIR_SCANE
|| fdctrl
->data_dir
== FD_DIR_SCANL
||
955 fdctrl
->data_dir
== FD_DIR_SCANH
)
957 if (dma_len
> fdctrl
->data_len
)
958 dma_len
= fdctrl
->data_len
;
959 if (cur_drv
->bs
== NULL
) {
960 if (fdctrl
->data_dir
== FD_DIR_WRITE
)
961 fdctrl_stop_transfer(fdctrl
, 0x60, 0x00, 0x00);
963 fdctrl_stop_transfer(fdctrl
, 0x40, 0x00, 0x00);
967 rel_pos
= fdctrl
->data_pos
% FD_SECTOR_LEN
;
968 for (start_pos
= fdctrl
->data_pos
; fdctrl
->data_pos
< dma_len
;) {
969 len
= dma_len
- fdctrl
->data_pos
;
970 if (len
+ rel_pos
> FD_SECTOR_LEN
)
971 len
= FD_SECTOR_LEN
- rel_pos
;
972 FLOPPY_DPRINTF("copy %d bytes (%d %d %d) %d pos %d %02x "
973 "(%d-0x%08x 0x%08x)\n", len
, dma_len
, fdctrl
->data_pos
,
974 fdctrl
->data_len
, fdctrl
->cur_drv
, cur_drv
->head
,
975 cur_drv
->track
, cur_drv
->sect
, fd_sector(cur_drv
),
976 fd_sector(cur_drv
) * 512);
977 if (fdctrl
->data_dir
!= FD_DIR_WRITE
||
978 len
< FD_SECTOR_LEN
|| rel_pos
!= 0) {
979 /* READ & SCAN commands and realign to a sector for WRITE */
980 if (bdrv_read(cur_drv
->bs
, fd_sector(cur_drv
),
981 fdctrl
->fifo
, 1) < 0) {
982 FLOPPY_DPRINTF("Floppy: error getting sector %d\n",
984 /* Sure, image size is too small... */
985 memset(fdctrl
->fifo
, 0, FD_SECTOR_LEN
);
988 switch (fdctrl
->data_dir
) {
991 DMA_write_memory (nchan
, fdctrl
->fifo
+ rel_pos
,
992 fdctrl
->data_pos
, len
);
993 /* cpu_physical_memory_write(addr + fdctrl->data_pos, */
994 /* fdctrl->fifo + rel_pos, len); */
998 DMA_read_memory (nchan
, fdctrl
->fifo
+ rel_pos
,
999 fdctrl
->data_pos
, len
);
1000 /* cpu_physical_memory_read(addr + fdctrl->data_pos, */
1001 /* fdctrl->fifo + rel_pos, len); */
1002 if (bdrv_write(cur_drv
->bs
, fd_sector(cur_drv
),
1003 fdctrl
->fifo
, 1) < 0) {
1004 FLOPPY_ERROR("writting sector %d\n", fd_sector(cur_drv
));
1005 fdctrl_stop_transfer(fdctrl
, 0x60, 0x00, 0x00);
1006 goto transfer_error
;
1012 uint8_t tmpbuf
[FD_SECTOR_LEN
];
1014 DMA_read_memory (nchan
, tmpbuf
, fdctrl
->data_pos
, len
);
1015 /* cpu_physical_memory_read(addr + fdctrl->data_pos, */
1017 ret
= memcmp(tmpbuf
, fdctrl
->fifo
+ rel_pos
, len
);
1022 if ((ret
< 0 && fdctrl
->data_dir
== FD_DIR_SCANL
) ||
1023 (ret
> 0 && fdctrl
->data_dir
== FD_DIR_SCANH
)) {
1030 fdctrl
->data_pos
+= len
;
1031 rel_pos
= fdctrl
->data_pos
% FD_SECTOR_LEN
;
1033 /* Seek to next sector */
1034 FLOPPY_DPRINTF("seek to next sector (%d %02x %02x => %d) (%d)\n",
1035 cur_drv
->head
, cur_drv
->track
, cur_drv
->sect
,
1037 fdctrl
->data_pos
- len
);
1038 /* XXX: cur_drv->sect >= cur_drv->last_sect should be an
1040 if (cur_drv
->sect
>= cur_drv
->last_sect
||
1041 cur_drv
->sect
== fdctrl
->eot
) {
1043 if (FD_MULTI_TRACK(fdctrl
->data_state
)) {
1044 if (cur_drv
->head
== 0 &&
1045 (cur_drv
->flags
& FDISK_DBL_SIDES
) != 0) {
1050 if ((cur_drv
->flags
& FDISK_DBL_SIDES
) == 0)
1057 FLOPPY_DPRINTF("seek to next track (%d %02x %02x => %d)\n",
1058 cur_drv
->head
, cur_drv
->track
,
1059 cur_drv
->sect
, fd_sector(cur_drv
));
1066 len
= fdctrl
->data_pos
- start_pos
;
1067 FLOPPY_DPRINTF("end transfer %d %d %d\n",
1068 fdctrl
->data_pos
, len
, fdctrl
->data_len
);
1069 if (fdctrl
->data_dir
== FD_DIR_SCANE
||
1070 fdctrl
->data_dir
== FD_DIR_SCANL
||
1071 fdctrl
->data_dir
== FD_DIR_SCANH
)
1073 if (FD_DID_SEEK(fdctrl
->data_state
))
1075 fdctrl
->data_len
-= len
;
1076 // if (fdctrl->data_len == 0)
1077 fdctrl_stop_transfer(fdctrl
, status0
, status1
, status2
);
1083 /* Data register : 0x05 */
1084 static uint32_t fdctrl_read_data (fdctrl_t
*fdctrl
)
1087 uint32_t retval
= 0;
1090 cur_drv
= get_cur_drv(fdctrl
);
1091 fdctrl
->state
&= ~FD_CTRL_SLEEP
;
1092 if (FD_STATE(fdctrl
->data_state
) == FD_STATE_CMD
) {
1093 FLOPPY_ERROR("can't read data in CMD state\n");
1096 pos
= fdctrl
->data_pos
;
1097 if (FD_STATE(fdctrl
->data_state
) == FD_STATE_DATA
) {
1098 pos
%= FD_SECTOR_LEN
;
1100 len
= fdctrl
->data_len
- fdctrl
->data_pos
;
1101 if (len
> FD_SECTOR_LEN
)
1102 len
= FD_SECTOR_LEN
;
1103 bdrv_read(cur_drv
->bs
, fd_sector(cur_drv
),
1107 retval
= fdctrl
->fifo
[pos
];
1108 if (++fdctrl
->data_pos
== fdctrl
->data_len
) {
1109 fdctrl
->data_pos
= 0;
1110 /* Switch from transfer mode to status mode
1111 * then from status mode to command mode
1113 if (FD_STATE(fdctrl
->data_state
) == FD_STATE_DATA
) {
1114 fdctrl_stop_transfer(fdctrl
, 0x20, 0x00, 0x00);
1116 fdctrl_reset_fifo(fdctrl
);
1117 fdctrl_reset_irq(fdctrl
);
1120 FLOPPY_DPRINTF("data register: 0x%02x\n", retval
);
1125 static void fdctrl_format_sector (fdctrl_t
*fdctrl
)
1131 fdctrl
->cur_drv
= fdctrl
->fifo
[1] & 1;
1132 cur_drv
= get_cur_drv(fdctrl
);
1133 kt
= fdctrl
->fifo
[6];
1134 kh
= fdctrl
->fifo
[7];
1135 ks
= fdctrl
->fifo
[8];
1136 FLOPPY_DPRINTF("format sector at %d %d %02x %02x (%d)\n",
1137 fdctrl
->cur_drv
, kh
, kt
, ks
,
1138 _fd_sector(kh
, kt
, ks
, cur_drv
->last_sect
));
1140 switch (fd_seek(cur_drv
, kh
, kt
, ks
, fdctrl
->config
& 0x40)) {
1143 fdctrl_stop_transfer(fdctrl
, 0x40, 0x00, 0x00);
1144 fdctrl
->fifo
[3] = kt
;
1145 fdctrl
->fifo
[4] = kh
;
1146 fdctrl
->fifo
[5] = ks
;
1150 fdctrl_stop_transfer(fdctrl
, 0x40, 0x80, 0x00);
1151 fdctrl
->fifo
[3] = kt
;
1152 fdctrl
->fifo
[4] = kh
;
1153 fdctrl
->fifo
[5] = ks
;
1156 /* No seek enabled */
1157 fdctrl_stop_transfer(fdctrl
, 0x40, 0x00, 0x00);
1158 fdctrl
->fifo
[3] = kt
;
1159 fdctrl
->fifo
[4] = kh
;
1160 fdctrl
->fifo
[5] = ks
;
1164 fdctrl
->data_state
|= FD_STATE_SEEK
;
1169 memset(fdctrl
->fifo
, 0, FD_SECTOR_LEN
);
1170 if (cur_drv
->bs
== NULL
||
1171 bdrv_write(cur_drv
->bs
, fd_sector(cur_drv
), fdctrl
->fifo
, 1) < 0) {
1172 FLOPPY_ERROR("formating sector %d\n", fd_sector(cur_drv
));
1173 fdctrl_stop_transfer(fdctrl
, 0x60, 0x00, 0x00);
1175 if (cur_drv
->sect
== cur_drv
->last_sect
) {
1176 fdctrl
->data_state
&= ~FD_STATE_FORMAT
;
1177 /* Last sector done */
1178 if (FD_DID_SEEK(fdctrl
->data_state
))
1179 fdctrl_stop_transfer(fdctrl
, 0x20, 0x00, 0x00);
1181 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);
1184 fdctrl
->data_pos
= 0;
1185 fdctrl
->data_len
= 4;
1190 static void fdctrl_write_data (fdctrl_t
*fdctrl
, uint32_t value
)
1194 cur_drv
= get_cur_drv(fdctrl
);
1196 if (fdctrl
->state
& FD_CTRL_RESET
) {
1197 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
1200 fdctrl
->state
&= ~FD_CTRL_SLEEP
;
1201 if (FD_STATE(fdctrl
->data_state
) == FD_STATE_STATUS
) {
1202 FLOPPY_ERROR("can't write data in status mode\n");
1205 /* Is it write command time ? */
1206 if (FD_STATE(fdctrl
->data_state
) == FD_STATE_DATA
) {
1207 /* FIFO data write */
1208 fdctrl
->fifo
[fdctrl
->data_pos
++] = value
;
1209 if (fdctrl
->data_pos
% FD_SECTOR_LEN
== (FD_SECTOR_LEN
- 1) ||
1210 fdctrl
->data_pos
== fdctrl
->data_len
) {
1211 bdrv_write(cur_drv
->bs
, fd_sector(cur_drv
),
1212 fdctrl
->fifo
, FD_SECTOR_LEN
);
1214 /* Switch from transfer mode to status mode
1215 * then from status mode to command mode
1217 if (FD_STATE(fdctrl
->data_state
) == FD_STATE_DATA
)
1218 fdctrl_stop_transfer(fdctrl
, 0x20, 0x00, 0x00);
1221 if (fdctrl
->data_pos
== 0) {
1223 switch (value
& 0x5F) {
1226 FLOPPY_DPRINTF("READ command\n");
1227 /* 8 parameters cmd */
1228 fdctrl
->data_len
= 9;
1231 /* READ_DELETED variants */
1232 FLOPPY_DPRINTF("READ_DELETED command\n");
1233 /* 8 parameters cmd */
1234 fdctrl
->data_len
= 9;
1237 /* SCAN_EQUAL variants */
1238 FLOPPY_DPRINTF("SCAN_EQUAL command\n");
1239 /* 8 parameters cmd */
1240 fdctrl
->data_len
= 9;
1243 /* VERIFY variants */
1244 FLOPPY_DPRINTF("VERIFY command\n");
1245 /* 8 parameters cmd */
1246 fdctrl
->data_len
= 9;
1249 /* SCAN_LOW_OR_EQUAL variants */
1250 FLOPPY_DPRINTF("SCAN_LOW_OR_EQUAL command\n");
1251 /* 8 parameters cmd */
1252 fdctrl
->data_len
= 9;
1255 /* SCAN_HIGH_OR_EQUAL variants */
1256 FLOPPY_DPRINTF("SCAN_HIGH_OR_EQUAL command\n");
1257 /* 8 parameters cmd */
1258 fdctrl
->data_len
= 9;
1263 switch (value
& 0x7F) {
1265 /* WRITE variants */
1266 FLOPPY_DPRINTF("WRITE command\n");
1267 /* 8 parameters cmd */
1268 fdctrl
->data_len
= 9;
1271 /* WRITE_DELETED variants */
1272 FLOPPY_DPRINTF("WRITE_DELETED command\n");
1273 /* 8 parameters cmd */
1274 fdctrl
->data_len
= 9;
1282 FLOPPY_DPRINTF("SPECIFY command\n");
1283 /* 1 parameter cmd */
1284 fdctrl
->data_len
= 3;
1287 /* SENSE_DRIVE_STATUS */
1288 FLOPPY_DPRINTF("SENSE_DRIVE_STATUS command\n");
1289 /* 1 parameter cmd */
1290 fdctrl
->data_len
= 2;
1294 FLOPPY_DPRINTF("RECALIBRATE command\n");
1295 /* 1 parameter cmd */
1296 fdctrl
->data_len
= 2;
1299 /* SENSE_INTERRUPT_STATUS */
1300 FLOPPY_DPRINTF("SENSE_INTERRUPT_STATUS command (%02x)\n",
1301 fdctrl
->int_status
);
1302 /* No parameters cmd: returns status if no interrupt */
1305 fdctrl
->int_status
| (cur_drv
->head
<< 2) | fdctrl
->cur_drv
;
1307 /* XXX: int_status handling is broken for read/write
1308 commands, so we do this hack. It should be suppressed
1311 0x20 | (cur_drv
->head
<< 2) | fdctrl
->cur_drv
;
1313 fdctrl
->fifo
[1] = cur_drv
->track
;
1314 fdctrl_set_fifo(fdctrl
, 2, 0);
1315 fdctrl_reset_irq(fdctrl
);
1316 fdctrl
->int_status
= 0xC0;
1320 FLOPPY_DPRINTF("DUMPREG command\n");
1321 /* Drives position */
1322 fdctrl
->fifo
[0] = drv0(fdctrl
)->track
;
1323 fdctrl
->fifo
[1] = drv1(fdctrl
)->track
;
1324 fdctrl
->fifo
[2] = 0;
1325 fdctrl
->fifo
[3] = 0;
1327 fdctrl
->fifo
[4] = fdctrl
->timer0
;
1328 fdctrl
->fifo
[5] = (fdctrl
->timer1
<< 1) | fdctrl
->dma_en
;
1329 fdctrl
->fifo
[6] = cur_drv
->last_sect
;
1330 fdctrl
->fifo
[7] = (fdctrl
->lock
<< 7) |
1331 (cur_drv
->perpendicular
<< 2);
1332 fdctrl
->fifo
[8] = fdctrl
->config
;
1333 fdctrl
->fifo
[9] = fdctrl
->precomp_trk
;
1334 fdctrl_set_fifo(fdctrl
, 10, 0);
1338 FLOPPY_DPRINTF("SEEK command\n");
1339 /* 2 parameters cmd */
1340 fdctrl
->data_len
= 3;
1344 FLOPPY_DPRINTF("VERSION command\n");
1345 /* No parameters cmd */
1346 /* Controller's version */
1347 fdctrl
->fifo
[0] = fdctrl
->version
;
1348 fdctrl_set_fifo(fdctrl
, 1, 1);
1351 /* PERPENDICULAR_MODE */
1352 FLOPPY_DPRINTF("PERPENDICULAR_MODE command\n");
1353 /* 1 parameter cmd */
1354 fdctrl
->data_len
= 2;
1358 FLOPPY_DPRINTF("CONFIGURE command\n");
1359 /* 3 parameters cmd */
1360 fdctrl
->data_len
= 4;
1364 FLOPPY_DPRINTF("UNLOCK command\n");
1365 /* No parameters cmd */
1367 fdctrl
->fifo
[0] = 0;
1368 fdctrl_set_fifo(fdctrl
, 1, 0);
1371 /* POWERDOWN_MODE */
1372 FLOPPY_DPRINTF("POWERDOWN_MODE command\n");
1373 /* 2 parameters cmd */
1374 fdctrl
->data_len
= 3;
1378 FLOPPY_DPRINTF("PART_ID command\n");
1379 /* No parameters cmd */
1380 fdctrl
->fifo
[0] = 0x41; /* Stepping 1 */
1381 fdctrl_set_fifo(fdctrl
, 1, 0);
1385 FLOPPY_DPRINTF("SAVE command\n");
1386 /* No parameters cmd */
1387 fdctrl
->fifo
[0] = 0;
1388 fdctrl
->fifo
[1] = 0;
1389 /* Drives position */
1390 fdctrl
->fifo
[2] = drv0(fdctrl
)->track
;
1391 fdctrl
->fifo
[3] = drv1(fdctrl
)->track
;
1392 fdctrl
->fifo
[4] = 0;
1393 fdctrl
->fifo
[5] = 0;
1395 fdctrl
->fifo
[6] = fdctrl
->timer0
;
1396 fdctrl
->fifo
[7] = fdctrl
->timer1
;
1397 fdctrl
->fifo
[8] = cur_drv
->last_sect
;
1398 fdctrl
->fifo
[9] = (fdctrl
->lock
<< 7) |
1399 (cur_drv
->perpendicular
<< 2);
1400 fdctrl
->fifo
[10] = fdctrl
->config
;
1401 fdctrl
->fifo
[11] = fdctrl
->precomp_trk
;
1402 fdctrl
->fifo
[12] = fdctrl
->pwrd
;
1403 fdctrl
->fifo
[13] = 0;
1404 fdctrl
->fifo
[14] = 0;
1405 fdctrl_set_fifo(fdctrl
, 15, 1);
1409 FLOPPY_DPRINTF("OPTION command\n");
1410 /* 1 parameter cmd */
1411 fdctrl
->data_len
= 2;
1415 FLOPPY_DPRINTF("READ_TRACK command\n");
1416 /* 8 parameters cmd */
1417 fdctrl
->data_len
= 9;
1421 FLOPPY_DPRINTF("READ_ID command\n");
1422 /* 1 parameter cmd */
1423 fdctrl
->data_len
= 2;
1427 FLOPPY_DPRINTF("RESTORE command\n");
1428 /* 17 parameters cmd */
1429 fdctrl
->data_len
= 18;
1433 FLOPPY_DPRINTF("FORMAT_TRACK command\n");
1434 /* 5 parameters cmd */
1435 fdctrl
->data_len
= 6;
1438 /* DRIVE_SPECIFICATION_COMMAND */
1439 FLOPPY_DPRINTF("DRIVE_SPECIFICATION_COMMAND command\n");
1440 /* 5 parameters cmd */
1441 fdctrl
->data_len
= 6;
1444 /* RELATIVE_SEEK_OUT */
1445 FLOPPY_DPRINTF("RELATIVE_SEEK_OUT command\n");
1446 /* 2 parameters cmd */
1447 fdctrl
->data_len
= 3;
1451 FLOPPY_DPRINTF("LOCK command\n");
1452 /* No parameters cmd */
1454 fdctrl
->fifo
[0] = 0x10;
1455 fdctrl_set_fifo(fdctrl
, 1, 1);
1458 /* FORMAT_AND_WRITE */
1459 FLOPPY_DPRINTF("FORMAT_AND_WRITE command\n");
1460 /* 10 parameters cmd */
1461 fdctrl
->data_len
= 11;
1464 /* RELATIVE_SEEK_IN */
1465 FLOPPY_DPRINTF("RELATIVE_SEEK_IN command\n");
1466 /* 2 parameters cmd */
1467 fdctrl
->data_len
= 3;
1470 /* Unknown command */
1471 FLOPPY_ERROR("unknown command: 0x%02x\n", value
);
1472 fdctrl_unimplemented(fdctrl
);
1477 FLOPPY_DPRINTF("%s: %02x\n", __func__
, value
);
1478 fdctrl
->fifo
[fdctrl
->data_pos
] = value
;
1479 if (++fdctrl
->data_pos
== fdctrl
->data_len
) {
1480 /* We now have all parameters
1481 * and will be able to treat the command
1483 if (fdctrl
->data_state
& FD_STATE_FORMAT
) {
1484 fdctrl_format_sector(fdctrl
);
1487 switch (fdctrl
->fifo
[0] & 0x1F) {
1491 FLOPPY_DPRINTF("treat READ command\n");
1492 fdctrl_start_transfer(fdctrl
, FD_DIR_READ
);
1496 /* READ_DELETED variants */
1497 // FLOPPY_DPRINTF("treat READ_DELETED command\n");
1498 FLOPPY_ERROR("treat READ_DELETED command\n");
1499 fdctrl_start_transfer_del(fdctrl
, FD_DIR_READ
);
1502 /* VERIFY variants */
1503 // FLOPPY_DPRINTF("treat VERIFY command\n");
1504 FLOPPY_ERROR("treat VERIFY command\n");
1505 fdctrl_stop_transfer(fdctrl
, 0x20, 0x00, 0x00);
1508 /* SCAN_EQUAL variants */
1509 // FLOPPY_DPRINTF("treat SCAN_EQUAL command\n");
1510 FLOPPY_ERROR("treat SCAN_EQUAL command\n");
1511 fdctrl_start_transfer(fdctrl
, FD_DIR_SCANE
);
1514 /* SCAN_LOW_OR_EQUAL variants */
1515 // FLOPPY_DPRINTF("treat SCAN_LOW_OR_EQUAL command\n");
1516 FLOPPY_ERROR("treat SCAN_LOW_OR_EQUAL command\n");
1517 fdctrl_start_transfer(fdctrl
, FD_DIR_SCANL
);
1520 /* SCAN_HIGH_OR_EQUAL variants */
1521 // FLOPPY_DPRINTF("treat SCAN_HIGH_OR_EQUAL command\n");
1522 FLOPPY_ERROR("treat SCAN_HIGH_OR_EQUAL command\n");
1523 fdctrl_start_transfer(fdctrl
, FD_DIR_SCANH
);
1528 switch (fdctrl
->fifo
[0] & 0x3F) {
1530 /* WRITE variants */
1531 FLOPPY_DPRINTF("treat WRITE command (%02x)\n", fdctrl
->fifo
[0]);
1532 fdctrl_start_transfer(fdctrl
, FD_DIR_WRITE
);
1535 /* WRITE_DELETED variants */
1536 // FLOPPY_DPRINTF("treat WRITE_DELETED command\n");
1537 FLOPPY_ERROR("treat WRITE_DELETED command\n");
1538 fdctrl_start_transfer_del(fdctrl
, FD_DIR_WRITE
);
1543 switch (fdctrl
->fifo
[0]) {
1546 FLOPPY_DPRINTF("treat SPECIFY command\n");
1547 fdctrl
->timer0
= (fdctrl
->fifo
[1] >> 4) & 0xF;
1548 fdctrl
->timer1
= fdctrl
->fifo
[2] >> 1;
1549 fdctrl
->dma_en
= 1 - (fdctrl
->fifo
[2] & 1) ;
1550 /* No result back */
1551 fdctrl_reset_fifo(fdctrl
);
1554 /* SENSE_DRIVE_STATUS */
1555 FLOPPY_DPRINTF("treat SENSE_DRIVE_STATUS command\n");
1556 fdctrl
->cur_drv
= fdctrl
->fifo
[1] & 1;
1557 cur_drv
= get_cur_drv(fdctrl
);
1558 cur_drv
->head
= (fdctrl
->fifo
[1] >> 2) & 1;
1559 /* 1 Byte status back */
1560 fdctrl
->fifo
[0] = (cur_drv
->ro
<< 6) |
1561 (cur_drv
->track
== 0 ? 0x10 : 0x00) |
1562 (cur_drv
->head
<< 2) |
1565 fdctrl_set_fifo(fdctrl
, 1, 0);
1569 FLOPPY_DPRINTF("treat RECALIBRATE command\n");
1570 fdctrl
->cur_drv
= fdctrl
->fifo
[1] & 1;
1571 cur_drv
= get_cur_drv(fdctrl
);
1572 fd_recalibrate(cur_drv
);
1573 fdctrl_reset_fifo(fdctrl
);
1574 /* Raise Interrupt */
1575 fdctrl_raise_irq(fdctrl
, 0x20);
1579 FLOPPY_DPRINTF("treat SEEK command\n");
1580 fdctrl
->cur_drv
= fdctrl
->fifo
[1] & 1;
1581 cur_drv
= get_cur_drv(fdctrl
);
1583 if (fdctrl
->fifo
[2] <= cur_drv
->track
)
1587 fdctrl_reset_fifo(fdctrl
);
1588 if (fdctrl
->fifo
[2] > cur_drv
->max_track
) {
1589 fdctrl_raise_irq(fdctrl
, 0x60);
1591 cur_drv
->track
= fdctrl
->fifo
[2];
1592 /* Raise Interrupt */
1593 fdctrl_raise_irq(fdctrl
, 0x20);
1597 /* PERPENDICULAR_MODE */
1598 FLOPPY_DPRINTF("treat PERPENDICULAR_MODE command\n");
1599 if (fdctrl
->fifo
[1] & 0x80)
1600 cur_drv
->perpendicular
= fdctrl
->fifo
[1] & 0x7;
1601 /* No result back */
1602 fdctrl_reset_fifo(fdctrl
);
1606 FLOPPY_DPRINTF("treat CONFIGURE command\n");
1607 fdctrl
->config
= fdctrl
->fifo
[2];
1608 fdctrl
->precomp_trk
= fdctrl
->fifo
[3];
1609 /* No result back */
1610 fdctrl_reset_fifo(fdctrl
);
1613 /* POWERDOWN_MODE */
1614 FLOPPY_DPRINTF("treat POWERDOWN_MODE command\n");
1615 fdctrl
->pwrd
= fdctrl
->fifo
[1];
1616 fdctrl
->fifo
[0] = fdctrl
->fifo
[1];
1617 fdctrl_set_fifo(fdctrl
, 1, 1);
1621 FLOPPY_DPRINTF("treat OPTION command\n");
1622 /* No result back */
1623 fdctrl_reset_fifo(fdctrl
);
1627 // FLOPPY_DPRINTF("treat READ_TRACK command\n");
1628 FLOPPY_ERROR("treat READ_TRACK command\n");
1629 fdctrl_start_transfer(fdctrl
, FD_DIR_READ
);
1633 FLOPPY_DPRINTF("treat READ_ID command\n");
1634 /* XXX: should set main status register to busy */
1635 cur_drv
->head
= (fdctrl
->fifo
[1] >> 2) & 1;
1636 qemu_mod_timer(fdctrl
->result_timer
,
1637 qemu_get_clock(vm_clock
) + (ticks_per_sec
/ 50));
1641 FLOPPY_DPRINTF("treat RESTORE command\n");
1642 /* Drives position */
1643 drv0(fdctrl
)->track
= fdctrl
->fifo
[3];
1644 drv1(fdctrl
)->track
= fdctrl
->fifo
[4];
1646 fdctrl
->timer0
= fdctrl
->fifo
[7];
1647 fdctrl
->timer1
= fdctrl
->fifo
[8];
1648 cur_drv
->last_sect
= fdctrl
->fifo
[9];
1649 fdctrl
->lock
= fdctrl
->fifo
[10] >> 7;
1650 cur_drv
->perpendicular
= (fdctrl
->fifo
[10] >> 2) & 0xF;
1651 fdctrl
->config
= fdctrl
->fifo
[11];
1652 fdctrl
->precomp_trk
= fdctrl
->fifo
[12];
1653 fdctrl
->pwrd
= fdctrl
->fifo
[13];
1654 fdctrl_reset_fifo(fdctrl
);
1658 FLOPPY_DPRINTF("treat FORMAT_TRACK command\n");
1659 fdctrl
->cur_drv
= fdctrl
->fifo
[1] & 1;
1660 cur_drv
= get_cur_drv(fdctrl
);
1661 fdctrl
->data_state
|= FD_STATE_FORMAT
;
1662 if (fdctrl
->fifo
[0] & 0x80)
1663 fdctrl
->data_state
|= FD_STATE_MULTI
;
1665 fdctrl
->data_state
&= ~FD_STATE_MULTI
;
1666 fdctrl
->data_state
&= ~FD_STATE_SEEK
;
1668 fdctrl
->fifo
[2] > 7 ? 16384 : 128 << fdctrl
->fifo
[2];
1670 cur_drv
->last_sect
=
1671 cur_drv
->flags
& FDISK_DBL_SIDES
? fdctrl
->fifo
[3] :
1672 fdctrl
->fifo
[3] / 2;
1674 cur_drv
->last_sect
= fdctrl
->fifo
[3];
1676 /* TODO: implement format using DMA expected by the Bochs BIOS
1677 * and Linux fdformat (read 3 bytes per sector via DMA and fill
1678 * the sector with the specified fill byte
1680 fdctrl
->data_state
&= ~FD_STATE_FORMAT
;
1681 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);
1684 /* DRIVE_SPECIFICATION_COMMAND */
1685 FLOPPY_DPRINTF("treat DRIVE_SPECIFICATION_COMMAND command\n");
1686 if (fdctrl
->fifo
[fdctrl
->data_pos
- 1] & 0x80) {
1687 /* Command parameters done */
1688 if (fdctrl
->fifo
[fdctrl
->data_pos
- 1] & 0x40) {
1689 fdctrl
->fifo
[0] = fdctrl
->fifo
[1];
1690 fdctrl
->fifo
[2] = 0;
1691 fdctrl
->fifo
[3] = 0;
1692 fdctrl_set_fifo(fdctrl
, 4, 1);
1694 fdctrl_reset_fifo(fdctrl
);
1696 } else if (fdctrl
->data_len
> 7) {
1698 fdctrl
->fifo
[0] = 0x80 |
1699 (cur_drv
->head
<< 2) | fdctrl
->cur_drv
;
1700 fdctrl_set_fifo(fdctrl
, 1, 1);
1704 /* RELATIVE_SEEK_OUT */
1705 FLOPPY_DPRINTF("treat RELATIVE_SEEK_OUT command\n");
1706 fdctrl
->cur_drv
= fdctrl
->fifo
[1] & 1;
1707 cur_drv
= get_cur_drv(fdctrl
);
1710 if (fdctrl
->fifo
[2] + cur_drv
->track
>= cur_drv
->max_track
) {
1711 cur_drv
->track
= cur_drv
->max_track
- 1;
1713 cur_drv
->track
+= fdctrl
->fifo
[2];
1715 fdctrl_reset_fifo(fdctrl
);
1716 fdctrl_raise_irq(fdctrl
, 0x20);
1719 /* FORMAT_AND_WRITE */
1720 // FLOPPY_DPRINTF("treat FORMAT_AND_WRITE command\n");
1721 FLOPPY_ERROR("treat FORMAT_AND_WRITE command\n");
1722 fdctrl_unimplemented(fdctrl
);
1725 /* RELATIVE_SEEK_IN */
1726 FLOPPY_DPRINTF("treat RELATIVE_SEEK_IN command\n");
1727 fdctrl
->cur_drv
= fdctrl
->fifo
[1] & 1;
1728 cur_drv
= get_cur_drv(fdctrl
);
1731 if (fdctrl
->fifo
[2] > cur_drv
->track
) {
1734 cur_drv
->track
-= fdctrl
->fifo
[2];
1736 fdctrl_reset_fifo(fdctrl
);
1737 /* Raise Interrupt */
1738 fdctrl_raise_irq(fdctrl
, 0x20);
1744 static void fdctrl_result_timer(void *opaque
)
1746 fdctrl_t
*fdctrl
= opaque
;
1747 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);