Reset buffer pointers after CR/LF, by Jim Paris.
[qemu/dscho.git] / hw / fdc.c
blob4f04b729ff179a9bf4e47b65f4089984d0318178
1 /*
2 * QEMU Floppy disk emulator (Intel 82078)
3 *
4 * Copyright (c) 2003, 2007 Jocelyn Mayer
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
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
22 * THE SOFTWARE.
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.
28 #include "vl.h"
30 /********************************************************/
31 /* debug Floppy devices */
32 //#define DEBUG_FLOPPY
34 #ifdef DEBUG_FLOPPY
35 #define FLOPPY_DPRINTF(fmt, args...) \
36 do { printf("FLOPPY: " fmt , ##args); } while (0)
37 #else
38 #define FLOPPY_DPRINTF(fmt, args...)
39 #endif
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 */
58 } fdisk_type_t;
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 */
65 } fdrive_type_t;
67 typedef enum fdrive_flags_t {
68 FDRIVE_MOTOR_ON = 0x01, /* motor on/off */
69 } fdrive_flags_t;
71 typedef enum fdisk_flags_t {
72 FDISK_DBL_SIDES = 0x01,
73 } fdisk_flags_t;
75 typedef struct fdrive_t {
76 BlockDriverState *bs;
77 /* Drive status */
78 fdrive_type_t drive;
79 fdrive_flags_t drflags;
80 uint8_t perpendicular; /* 2.88 MB access mode */
81 /* Position */
82 uint8_t head;
83 uint8_t track;
84 uint8_t sect;
85 /* Last operation status */
86 uint8_t dir; /* Direction */
87 uint8_t rw; /* Read/write */
88 /* Media */
89 fdisk_flags_t flags;
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 */
94 } fdrive_t;
96 static void fd_init (fdrive_t *drv, BlockDriverState *bs)
98 /* Drive */
99 drv->bs = bs;
100 drv->drive = FDRIVE_DRV_NONE;
101 drv->drflags = 0;
102 drv->perpendicular = 0;
103 /* Disk */
104 drv->last_sect = 0;
105 drv->max_track = 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,
121 int enable_seek)
123 uint32_t sector;
124 int ret;
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);
132 return 2;
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);
139 return 3;
141 sector = _fd_sector(head, track, sect, drv->last_sect);
142 ret = 0;
143 if (sector != fd_sector(drv)) {
144 #if 0
145 if (!enable_seek) {
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);
148 return 4;
150 #endif
151 drv->head = head;
152 if (drv->track != track)
153 ret = 1;
154 drv->track = track;
155 drv->sect = sect;
158 return ret;
161 /* Set drive back to track 0 */
162 static void fd_recalibrate (fdrive_t *drv)
164 FLOPPY_DPRINTF("recalibrate\n");
165 drv->head = 0;
166 drv->track = 0;
167 drv->sect = 1;
168 drv->dir = 1;
169 drv->rw = 0;
172 /* Recognize floppy formats */
173 typedef struct fd_format_t {
174 fdrive_type_t drive;
175 fdisk_type_t disk;
176 uint8_t last_sect;
177 uint8_t max_track;
178 uint8_t max_head;
179 const unsigned char *str;
180 } fd_format_t;
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", },
225 /* end */
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)
232 fd_format_t *parse;
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);
244 } else {
245 bdrv_get_geometry(drv->bs, &nb_sectors);
246 match = -1;
247 first_match = -1;
248 for (i = 0;; i++) {
249 parse = &fd_formats[i];
250 if (parse->drive == FDRIVE_DRV_NONE)
251 break;
252 if (drv->drive == parse->drive ||
253 drv->drive == FDRIVE_DRV_NONE) {
254 size = (parse->max_head + 1) * parse->max_track *
255 parse->last_sect;
256 if (nb_sectors == size) {
257 match = i;
258 break;
260 if (first_match == -1)
261 first_match = i;
264 if (match == -1) {
265 if (first_match == -1)
266 match = 1;
267 else
268 match = first_match;
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");
278 if (nb_heads == 1) {
279 drv->flags &= ~FDISK_DBL_SIDES;
280 } else {
281 drv->flags |= FDISK_DBL_SIDES;
283 drv->max_track = max_track;
284 drv->last_sect = last_sect;
285 drv->ro = ro;
286 } else {
287 FLOPPY_DPRINTF("No disk in drive\n");
288 drv->last_sect = 0;
289 drv->max_track = 0;
290 drv->flags &= ~FDISK_DBL_SIDES;
294 /* Motor control */
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)
308 fd_stop(drv);
309 fd_recalibrate(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);
333 enum {
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 */
338 FD_CTRL_INTR = 0x10,
341 enum {
342 FD_DIR_WRITE = 0,
343 FD_DIR_READ = 1,
344 FD_DIR_SCANE = 2,
345 FD_DIR_SCANL = 3,
346 FD_DIR_SCANH = 4,
349 enum {
350 FD_STATE_CMD = 0x00,
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)
366 struct fdctrl_t {
367 fdctrl_t *fdctrl;
368 /* Controller's identification */
369 uint8_t version;
370 /* HW */
371 qemu_irq irq;
372 int dma_chann;
373 target_phys_addr_t io_base;
374 /* Controller state */
375 QEMUTimer *result_timer;
376 uint8_t state;
377 uint8_t dma_en;
378 uint8_t cur_drv;
379 uint8_t bootsel;
380 /* Command FIFO */
381 uint8_t fifo[FD_SECTOR_LEN];
382 uint32_t data_pos;
383 uint32_t data_len;
384 uint8_t data_state;
385 uint8_t data_dir;
386 uint8_t int_status;
387 uint8_t eot; /* last wanted sector */
388 /* States kept only to be returned back */
389 /* Timers state */
390 uint8_t timer0;
391 uint8_t timer1;
392 /* precompensation */
393 uint8_t precomp_trk;
394 uint8_t config;
395 uint8_t lock;
396 /* Power down config (also with status regB access mode */
397 uint8_t pwrd;
398 /* Floppy drives */
399 fdrive_t drives[2];
402 static uint32_t fdctrl_read (void *opaque, uint32_t reg)
404 fdctrl_t *fdctrl = opaque;
405 uint32_t retval;
407 switch (reg & 0x07) {
408 #ifdef TARGET_SPARC
409 case 0x00:
410 // Identify to Linux as S82078B
411 retval = fdctrl_read_statusB(fdctrl);
412 break;
413 #endif
414 case 0x01:
415 retval = fdctrl_read_statusB(fdctrl);
416 break;
417 case 0x02:
418 retval = fdctrl_read_dor(fdctrl);
419 break;
420 case 0x03:
421 retval = fdctrl_read_tape(fdctrl);
422 break;
423 case 0x04:
424 retval = fdctrl_read_main_status(fdctrl);
425 break;
426 case 0x05:
427 retval = fdctrl_read_data(fdctrl);
428 break;
429 case 0x07:
430 retval = fdctrl_read_dir(fdctrl);
431 break;
432 default:
433 retval = (uint32_t)(-1);
434 break;
436 FLOPPY_DPRINTF("read reg%d: 0x%02x\n", reg & 7, retval);
438 return 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) {
448 case 0x02:
449 fdctrl_write_dor(fdctrl, value);
450 break;
451 case 0x03:
452 fdctrl_write_tape(fdctrl, value);
453 break;
454 case 0x04:
455 fdctrl_write_rate(fdctrl, value);
456 break;
457 case 0x05:
458 fdctrl_write_data(fdctrl, value);
459 break;
460 default:
461 break;
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] = {
477 fdctrl_read_mem,
478 fdctrl_read_mem,
479 fdctrl_read_mem,
482 static CPUWriteMemoryFunc *fdctrl_mem_write[3] = {
483 fdctrl_write_mem,
484 fdctrl_write_mem,
485 fdctrl_write_mem,
488 static void fd_save (QEMUFile *f, fdrive_t *fd)
490 uint8_t tmp;
492 tmp = fd->drflags;
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)
528 uint8_t tmp;
530 qemu_get_8s(f, &tmp);
531 fd->drflags = 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);
538 return 0;
541 static int fdc_load (QEMUFile *f, void *opaque, int version_id)
543 fdctrl_t *s = opaque;
544 int ret;
546 if (version_id != 1)
547 return -EINVAL;
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]);
568 if (ret == 0)
569 ret = fd_load(f, &s->drives[1]);
571 return ret;
574 static void fdctrl_external_reset(void *opaque)
576 fdctrl_t *s = opaque;
578 fdctrl_reset(s, 0);
581 fdctrl_t *fdctrl_init (qemu_irq irq, int dma_chann, int mem_mapped,
582 target_phys_addr_t io_base,
583 BlockDriverState **fds)
585 fdctrl_t *fdctrl;
586 int io_mem;
587 int i;
589 FLOPPY_DPRINTF("init controller\n");
590 fdctrl = qemu_mallocz(sizeof(fdctrl_t));
591 if (!fdctrl)
592 return NULL;
593 fdctrl->result_timer = qemu_new_timer(vm_clock,
594 fdctrl_result_timer, fdctrl);
596 fdctrl->version = 0x90; /* Intel 82078 controller */
597 fdctrl->irq = irq;
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) {
602 fdctrl->dma_en = 1;
603 DMA_register_channel(dma_chann, &fdctrl_transfer_handler, fdctrl);
604 } else {
605 fdctrl->dma_en = 0;
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;
612 if (mem_mapped) {
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);
615 } else {
616 register_ioport_read((uint32_t)io_base + 0x01, 5, 1, &fdctrl_read,
617 fdctrl);
618 register_ioport_read((uint32_t)io_base + 0x07, 1, 1, &fdctrl_read,
619 fdctrl);
620 register_ioport_write((uint32_t)io_base + 0x01, 5, 1, &fdctrl_write,
621 fdctrl);
622 register_ioport_write((uint32_t)io_base + 0x07, 1, 1, &fdctrl_write,
623 fdctrl);
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]);
631 return fdctrl;
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)
650 #ifdef TARGET_SPARC
651 // Sparc mutation
652 if (!fdctrl->dma_en) {
653 fdctrl->state &= ~FD_CTRL_BUSY;
654 fdctrl->int_status = status;
655 return;
657 #endif
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)
669 int i;
671 FLOPPY_DPRINTF("reset controller\n");
672 fdctrl_reset_irq(fdctrl);
673 /* Initialise controller */
674 fdctrl->cur_drv = 0;
675 /* FIFO state */
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);
683 if (do_irq)
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");
706 return 0;
709 /* Digital output register : 0x02 */
710 static uint32_t fdctrl_read_dor (fdctrl_t *fdctrl)
712 uint32_t retval = 0;
714 /* Drive motors state indicators */
715 if (drv0(fdctrl)->drflags & FDRIVE_MOTOR_ON)
716 retval |= 1 << 5;
717 if (drv1(fdctrl)->drflags & FDRIVE_MOTOR_ON)
718 retval |= 1 << 4;
719 /* DMA enable */
720 retval |= fdctrl->dma_en << 3;
721 /* Reset indicator */
722 retval |= (fdctrl->state & FD_CTRL_RESET) == 0 ? 0x04 : 0;
723 /* Selected drive */
724 retval |= fdctrl->cur_drv;
725 FLOPPY_DPRINTF("digital output register: 0x%02x\n", retval);
727 return retval;
730 static void fdctrl_write_dor (fdctrl_t *fdctrl, uint32_t value)
732 /* Reset mode */
733 if (fdctrl->state & FD_CTRL_RESET) {
734 if (!(value & 0x04)) {
735 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
736 return;
739 FLOPPY_DPRINTF("digital output register set to 0x%02x\n", value);
740 /* Drive motors state indicators */
741 if (value & 0x20)
742 fd_start(drv1(fdctrl));
743 else
744 fd_stop(drv1(fdctrl));
745 if (value & 0x10)
746 fd_start(drv0(fdctrl));
747 else
748 fd_stop(drv0(fdctrl));
749 /* DMA enable */
750 #if 0
751 if (fdctrl->dma_chann != -1)
752 fdctrl->dma_en = 1 - ((value >> 3) & 1);
753 #endif
754 /* Reset */
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;
760 } else {
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);
767 /* Selected drive */
768 fdctrl->cur_drv = value & 1;
771 /* Tape drive register : 0x03 */
772 static uint32_t fdctrl_read_tape (fdctrl_t *fdctrl)
774 uint32_t retval = 0;
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);
781 return retval;
784 static void fdctrl_write_tape (fdctrl_t *fdctrl, uint32_t value)
786 /* Reset mode */
787 if (fdctrl->state & FD_CTRL_RESET) {
788 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
789 return;
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)
800 uint32_t retval = 0;
802 fdctrl->state &= ~(FD_CTRL_SLEEP | FD_CTRL_RESET);
803 if (!(fdctrl->state & FD_CTRL_BUSY)) {
804 /* Data transfer allowed */
805 retval |= 0x80;
806 /* Data transfer direction indicator */
807 if (fdctrl->data_dir == FD_DIR_READ)
808 retval |= 0x40;
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)
814 retval |= 0x10;
815 FLOPPY_DPRINTF("main status register: 0x%02x\n", retval);
817 return retval;
820 /* Data select rate register : 0x04 (write) */
821 static void fdctrl_write_rate (fdctrl_t *fdctrl, uint32_t value)
823 /* Reset mode */
824 if (fdctrl->state & FD_CTRL_RESET) {
825 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
826 return;
828 FLOPPY_DPRINTF("select rate register set to 0x%02x\n", value);
829 /* Reset: autoclear */
830 if (value & 0x80) {
831 fdctrl->state |= FD_CTRL_RESET;
832 fdctrl_reset(fdctrl, 1);
833 fdctrl->state &= ~FD_CTRL_RESET;
835 if (value & 0x40) {
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)
844 int ret;
845 if (!drv->bs)
846 return 0;
847 ret = bdrv_media_changed(drv->bs);
848 if (ret) {
849 fd_revalidate(drv);
851 return ret;
854 /* Digital input register : 0x07 (read-only) */
855 static uint32_t fdctrl_read_dir (fdctrl_t *fdctrl)
857 uint32_t retval = 0;
859 if (fdctrl_media_changed(drv0(fdctrl)) ||
860 fdctrl_media_changed(drv1(fdctrl)))
861 retval |= 0x80;
862 if (retval != 0)
863 FLOPPY_DPRINTF("Floppy digital input register: 0x%02x\n", retval);
865 return 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);
883 if (do_irq)
884 fdctrl_raise_irq(fdctrl, 0x00);
887 /* Set an error: unimplemented/unknown command */
888 static void fdctrl_unimplemented (fdctrl_t *fdctrl)
890 #if 0
891 fdrive_t *cur_drv;
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);
898 #else
899 // fdctrl_reset_fifo(fdctrl);
900 fdctrl->fifo[0] = 0x80;
901 fdctrl_set_fifo(fdctrl, 1, 0);
902 #endif
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)
909 fdrive_t *cur_drv;
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)
933 fdrive_t *cur_drv;
934 uint8_t kh, kt, ks;
935 int did_seek;
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));
945 did_seek = 0;
946 switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & 0x40)) {
947 case 2:
948 /* sect too big */
949 fdctrl_stop_transfer(fdctrl, 0x40, 0x00, 0x00);
950 fdctrl->fifo[3] = kt;
951 fdctrl->fifo[4] = kh;
952 fdctrl->fifo[5] = ks;
953 return;
954 case 3:
955 /* track too big */
956 fdctrl_stop_transfer(fdctrl, 0x40, 0x80, 0x00);
957 fdctrl->fifo[3] = kt;
958 fdctrl->fifo[4] = kh;
959 fdctrl->fifo[5] = ks;
960 return;
961 case 4:
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;
967 return;
968 case 1:
969 did_seek = 1;
970 break;
971 default:
972 break;
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;
980 else
981 fdctrl->data_state &= ~FD_STATE_MULTI;
982 if (did_seek)
983 fdctrl->data_state |= FD_STATE_SEEK;
984 else
985 fdctrl->data_state &= ~FD_STATE_SEEK;
986 if (fdctrl->fifo[5] == 00) {
987 fdctrl->data_len = fdctrl->fifo[8];
988 } else {
989 int tmp;
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) {
998 int dma_mode;
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
1013 * recall us...
1015 DMA_hold_DREQ(fdctrl->dma_chann);
1016 DMA_schedule(fdctrl->dma_chann);
1017 return;
1018 } else {
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);
1026 return;
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)
1042 fdctrl_t *fdctrl;
1043 fdrive_t *cur_drv;
1044 int len, start_pos, rel_pos;
1045 uint8_t status0 = 0x00, status1 = 0x00, status2 = 0x00;
1047 fdctrl = opaque;
1048 if (!(fdctrl->state & FD_CTRL_BUSY)) {
1049 FLOPPY_DPRINTF("Not in DMA transfer mode !\n");
1050 return 0;
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)
1055 status2 = 0x04;
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);
1061 else
1062 fdctrl_stop_transfer(fdctrl, 0x40, 0x00, 0x00);
1063 len = 0;
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) {
1088 case FD_DIR_READ:
1089 /* READ commands */
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); */
1094 break;
1095 case FD_DIR_WRITE:
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;
1107 break;
1108 default:
1109 /* SCAN commands */
1111 uint8_t tmpbuf[FD_SECTOR_LEN];
1112 int ret;
1113 DMA_read_memory (nchan, tmpbuf, fdctrl->data_pos, len);
1114 /* cpu_physical_memory_read(addr + fdctrl->data_pos, */
1115 /* tmpbuf, len); */
1116 ret = memcmp(tmpbuf, fdctrl->fifo + rel_pos, len);
1117 if (ret == 0) {
1118 status2 = 0x08;
1119 goto end_transfer;
1121 if ((ret < 0 && fdctrl->data_dir == FD_DIR_SCANL) ||
1122 (ret > 0 && fdctrl->data_dir == FD_DIR_SCANH)) {
1123 status2 = 0x00;
1124 goto end_transfer;
1127 break;
1129 fdctrl->data_pos += len;
1130 rel_pos = fdctrl->data_pos % FD_SECTOR_LEN;
1131 if (rel_pos == 0) {
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,
1135 fd_sector(cur_drv),
1136 fdctrl->data_pos - len);
1137 /* XXX: cur_drv->sect >= cur_drv->last_sect should be an
1138 error in fact */
1139 if (cur_drv->sect >= cur_drv->last_sect ||
1140 cur_drv->sect == fdctrl->eot) {
1141 cur_drv->sect = 1;
1142 if (FD_MULTI_TRACK(fdctrl->data_state)) {
1143 if (cur_drv->head == 0 &&
1144 (cur_drv->flags & FDISK_DBL_SIDES) != 0) {
1145 cur_drv->head = 1;
1146 } else {
1147 cur_drv->head = 0;
1148 cur_drv->track++;
1149 if ((cur_drv->flags & FDISK_DBL_SIDES) == 0)
1150 break;
1152 } else {
1153 cur_drv->track++;
1154 break;
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));
1159 } else {
1160 cur_drv->sect++;
1164 end_transfer:
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)
1171 status2 = 0x08;
1172 if (FD_DID_SEEK(fdctrl->data_state))
1173 status0 |= 0x20;
1174 fdctrl->data_len -= len;
1175 // if (fdctrl->data_len == 0)
1176 fdctrl_stop_transfer(fdctrl, status0, status1, status2);
1177 transfer_error:
1179 return len;
1182 /* Data register : 0x05 */
1183 static uint32_t fdctrl_read_data (fdctrl_t *fdctrl)
1185 fdrive_t *cur_drv;
1186 uint32_t retval = 0;
1187 int pos, len;
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");
1193 return 0;
1195 pos = fdctrl->data_pos;
1196 if (FD_STATE(fdctrl->data_state) == FD_STATE_DATA) {
1197 pos %= FD_SECTOR_LEN;
1198 if (pos == 0) {
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),
1203 fdctrl->fifo, len);
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);
1214 } else {
1215 fdctrl_reset_fifo(fdctrl);
1216 fdctrl_reset_irq(fdctrl);
1219 FLOPPY_DPRINTF("data register: 0x%02x\n", retval);
1221 return retval;
1224 static void fdctrl_format_sector (fdctrl_t *fdctrl)
1226 fdrive_t *cur_drv;
1227 uint8_t kh, kt, ks;
1228 int did_seek;
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));
1238 did_seek = 0;
1239 switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & 0x40)) {
1240 case 2:
1241 /* sect too big */
1242 fdctrl_stop_transfer(fdctrl, 0x40, 0x00, 0x00);
1243 fdctrl->fifo[3] = kt;
1244 fdctrl->fifo[4] = kh;
1245 fdctrl->fifo[5] = ks;
1246 return;
1247 case 3:
1248 /* track too big */
1249 fdctrl_stop_transfer(fdctrl, 0x40, 0x80, 0x00);
1250 fdctrl->fifo[3] = kt;
1251 fdctrl->fifo[4] = kh;
1252 fdctrl->fifo[5] = ks;
1253 return;
1254 case 4:
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;
1260 return;
1261 case 1:
1262 did_seek = 1;
1263 fdctrl->data_state |= FD_STATE_SEEK;
1264 break;
1265 default:
1266 break;
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);
1273 } else {
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);
1279 else
1280 fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);
1281 } else {
1282 /* More to do */
1283 fdctrl->data_pos = 0;
1284 fdctrl->data_len = 4;
1289 static void fdctrl_write_data (fdctrl_t *fdctrl, uint32_t value)
1291 fdrive_t *cur_drv;
1293 cur_drv = get_cur_drv(fdctrl);
1294 /* Reset mode */
1295 if (fdctrl->state & FD_CTRL_RESET) {
1296 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
1297 return;
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");
1302 return;
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);
1318 return;
1320 if (fdctrl->data_pos == 0) {
1321 /* Command */
1322 switch (value & 0x5F) {
1323 case 0x46:
1324 /* READ variants */
1325 FLOPPY_DPRINTF("READ command\n");
1326 /* 8 parameters cmd */
1327 fdctrl->data_len = 9;
1328 goto enqueue;
1329 case 0x4C:
1330 /* READ_DELETED variants */
1331 FLOPPY_DPRINTF("READ_DELETED command\n");
1332 /* 8 parameters cmd */
1333 fdctrl->data_len = 9;
1334 goto enqueue;
1335 case 0x50:
1336 /* SCAN_EQUAL variants */
1337 FLOPPY_DPRINTF("SCAN_EQUAL command\n");
1338 /* 8 parameters cmd */
1339 fdctrl->data_len = 9;
1340 goto enqueue;
1341 case 0x56:
1342 /* VERIFY variants */
1343 FLOPPY_DPRINTF("VERIFY command\n");
1344 /* 8 parameters cmd */
1345 fdctrl->data_len = 9;
1346 goto enqueue;
1347 case 0x59:
1348 /* SCAN_LOW_OR_EQUAL variants */
1349 FLOPPY_DPRINTF("SCAN_LOW_OR_EQUAL command\n");
1350 /* 8 parameters cmd */
1351 fdctrl->data_len = 9;
1352 goto enqueue;
1353 case 0x5D:
1354 /* SCAN_HIGH_OR_EQUAL variants */
1355 FLOPPY_DPRINTF("SCAN_HIGH_OR_EQUAL command\n");
1356 /* 8 parameters cmd */
1357 fdctrl->data_len = 9;
1358 goto enqueue;
1359 default:
1360 break;
1362 switch (value & 0x7F) {
1363 case 0x45:
1364 /* WRITE variants */
1365 FLOPPY_DPRINTF("WRITE command\n");
1366 /* 8 parameters cmd */
1367 fdctrl->data_len = 9;
1368 goto enqueue;
1369 case 0x49:
1370 /* WRITE_DELETED variants */
1371 FLOPPY_DPRINTF("WRITE_DELETED command\n");
1372 /* 8 parameters cmd */
1373 fdctrl->data_len = 9;
1374 goto enqueue;
1375 default:
1376 break;
1378 switch (value) {
1379 case 0x03:
1380 /* SPECIFY */
1381 FLOPPY_DPRINTF("SPECIFY command\n");
1382 /* 1 parameter cmd */
1383 fdctrl->data_len = 3;
1384 goto enqueue;
1385 case 0x04:
1386 /* SENSE_DRIVE_STATUS */
1387 FLOPPY_DPRINTF("SENSE_DRIVE_STATUS command\n");
1388 /* 1 parameter cmd */
1389 fdctrl->data_len = 2;
1390 goto enqueue;
1391 case 0x07:
1392 /* RECALIBRATE */
1393 FLOPPY_DPRINTF("RECALIBRATE command\n");
1394 /* 1 parameter cmd */
1395 fdctrl->data_len = 2;
1396 goto enqueue;
1397 case 0x08:
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 */
1402 #if 0
1403 fdctrl->fifo[0] =
1404 fdctrl->int_status | (cur_drv->head << 2) | fdctrl->cur_drv;
1405 #else
1406 /* XXX: int_status handling is broken for read/write
1407 commands, so we do this hack. It should be suppressed
1408 ASAP */
1409 fdctrl->fifo[0] =
1410 0x20 | (cur_drv->head << 2) | fdctrl->cur_drv;
1411 #endif
1412 fdctrl->fifo[1] = cur_drv->track;
1413 fdctrl_set_fifo(fdctrl, 2, 0);
1414 fdctrl_reset_irq(fdctrl);
1415 fdctrl->int_status = 0xC0;
1416 return;
1417 case 0x0E:
1418 /* DUMPREG */
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;
1425 /* timers */
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);
1434 return;
1435 case 0x0F:
1436 /* SEEK */
1437 FLOPPY_DPRINTF("SEEK command\n");
1438 /* 2 parameters cmd */
1439 fdctrl->data_len = 3;
1440 goto enqueue;
1441 case 0x10:
1442 /* VERSION */
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);
1448 return;
1449 case 0x12:
1450 /* PERPENDICULAR_MODE */
1451 FLOPPY_DPRINTF("PERPENDICULAR_MODE command\n");
1452 /* 1 parameter cmd */
1453 fdctrl->data_len = 2;
1454 goto enqueue;
1455 case 0x13:
1456 /* CONFIGURE */
1457 FLOPPY_DPRINTF("CONFIGURE command\n");
1458 /* 3 parameters cmd */
1459 fdctrl->data_len = 4;
1460 goto enqueue;
1461 case 0x14:
1462 /* UNLOCK */
1463 FLOPPY_DPRINTF("UNLOCK command\n");
1464 /* No parameters cmd */
1465 fdctrl->lock = 0;
1466 fdctrl->fifo[0] = 0;
1467 fdctrl_set_fifo(fdctrl, 1, 0);
1468 return;
1469 case 0x17:
1470 /* POWERDOWN_MODE */
1471 FLOPPY_DPRINTF("POWERDOWN_MODE command\n");
1472 /* 2 parameters cmd */
1473 fdctrl->data_len = 3;
1474 goto enqueue;
1475 case 0x18:
1476 /* PART_ID */
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);
1481 return;
1482 case 0x2C:
1483 /* SAVE */
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;
1493 /* timers */
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);
1505 return;
1506 case 0x33:
1507 /* OPTION */
1508 FLOPPY_DPRINTF("OPTION command\n");
1509 /* 1 parameter cmd */
1510 fdctrl->data_len = 2;
1511 goto enqueue;
1512 case 0x42:
1513 /* READ_TRACK */
1514 FLOPPY_DPRINTF("READ_TRACK command\n");
1515 /* 8 parameters cmd */
1516 fdctrl->data_len = 9;
1517 goto enqueue;
1518 case 0x4A:
1519 /* READ_ID */
1520 FLOPPY_DPRINTF("READ_ID command\n");
1521 /* 1 parameter cmd */
1522 fdctrl->data_len = 2;
1523 goto enqueue;
1524 case 0x4C:
1525 /* RESTORE */
1526 FLOPPY_DPRINTF("RESTORE command\n");
1527 /* 17 parameters cmd */
1528 fdctrl->data_len = 18;
1529 goto enqueue;
1530 case 0x4D:
1531 /* FORMAT_TRACK */
1532 FLOPPY_DPRINTF("FORMAT_TRACK command\n");
1533 /* 5 parameters cmd */
1534 fdctrl->data_len = 6;
1535 goto enqueue;
1536 case 0x8E:
1537 /* DRIVE_SPECIFICATION_COMMAND */
1538 FLOPPY_DPRINTF("DRIVE_SPECIFICATION_COMMAND command\n");
1539 /* 5 parameters cmd */
1540 fdctrl->data_len = 6;
1541 goto enqueue;
1542 case 0x8F:
1543 /* RELATIVE_SEEK_OUT */
1544 FLOPPY_DPRINTF("RELATIVE_SEEK_OUT command\n");
1545 /* 2 parameters cmd */
1546 fdctrl->data_len = 3;
1547 goto enqueue;
1548 case 0x94:
1549 /* LOCK */
1550 FLOPPY_DPRINTF("LOCK command\n");
1551 /* No parameters cmd */
1552 fdctrl->lock = 1;
1553 fdctrl->fifo[0] = 0x10;
1554 fdctrl_set_fifo(fdctrl, 1, 1);
1555 return;
1556 case 0xCD:
1557 /* FORMAT_AND_WRITE */
1558 FLOPPY_DPRINTF("FORMAT_AND_WRITE command\n");
1559 /* 10 parameters cmd */
1560 fdctrl->data_len = 11;
1561 goto enqueue;
1562 case 0xCF:
1563 /* RELATIVE_SEEK_IN */
1564 FLOPPY_DPRINTF("RELATIVE_SEEK_IN command\n");
1565 /* 2 parameters cmd */
1566 fdctrl->data_len = 3;
1567 goto enqueue;
1568 default:
1569 /* Unknown command */
1570 FLOPPY_ERROR("unknown command: 0x%02x\n", value);
1571 fdctrl_unimplemented(fdctrl);
1572 return;
1575 enqueue:
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);
1584 return;
1586 switch (fdctrl->fifo[0] & 0x1F) {
1587 case 0x06:
1589 /* READ variants */
1590 FLOPPY_DPRINTF("treat READ command\n");
1591 fdctrl_start_transfer(fdctrl, FD_DIR_READ);
1592 return;
1594 case 0x0C:
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);
1599 return;
1600 case 0x16:
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);
1605 return;
1606 case 0x10:
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);
1611 return;
1612 case 0x19:
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);
1617 return;
1618 case 0x1D:
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);
1623 return;
1624 default:
1625 break;
1627 switch (fdctrl->fifo[0] & 0x3F) {
1628 case 0x05:
1629 /* WRITE variants */
1630 FLOPPY_DPRINTF("treat WRITE command (%02x)\n", fdctrl->fifo[0]);
1631 fdctrl_start_transfer(fdctrl, FD_DIR_WRITE);
1632 return;
1633 case 0x09:
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);
1638 return;
1639 default:
1640 break;
1642 switch (fdctrl->fifo[0]) {
1643 case 0x03:
1644 /* SPECIFY */
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);
1651 break;
1652 case 0x04:
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) |
1662 fdctrl->cur_drv |
1663 0x28;
1664 fdctrl_set_fifo(fdctrl, 1, 0);
1665 break;
1666 case 0x07:
1667 /* RECALIBRATE */
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);
1675 break;
1676 case 0x0F:
1677 /* SEEK */
1678 FLOPPY_DPRINTF("treat SEEK command\n");
1679 fdctrl->cur_drv = fdctrl->fifo[1] & 1;
1680 cur_drv = get_cur_drv(fdctrl);
1681 fd_start(cur_drv);
1682 if (fdctrl->fifo[2] <= cur_drv->track)
1683 cur_drv->dir = 1;
1684 else
1685 cur_drv->dir = 0;
1686 fdctrl_reset_fifo(fdctrl);
1687 if (fdctrl->fifo[2] > cur_drv->max_track) {
1688 fdctrl_raise_irq(fdctrl, 0x60);
1689 } else {
1690 cur_drv->track = fdctrl->fifo[2];
1691 /* Raise Interrupt */
1692 fdctrl_raise_irq(fdctrl, 0x20);
1694 break;
1695 case 0x12:
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);
1702 break;
1703 case 0x13:
1704 /* CONFIGURE */
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);
1710 break;
1711 case 0x17:
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);
1717 break;
1718 case 0x33:
1719 /* OPTION */
1720 FLOPPY_DPRINTF("treat OPTION command\n");
1721 /* No result back */
1722 fdctrl_reset_fifo(fdctrl);
1723 break;
1724 case 0x42:
1725 /* READ_TRACK */
1726 // FLOPPY_DPRINTF("treat READ_TRACK command\n");
1727 FLOPPY_ERROR("treat READ_TRACK command\n");
1728 fdctrl_start_transfer(fdctrl, FD_DIR_READ);
1729 break;
1730 case 0x4A:
1731 /* READ_ID */
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));
1737 break;
1738 case 0x4C:
1739 /* RESTORE */
1740 FLOPPY_DPRINTF("treat RESTORE command\n");
1741 /* Drives position */
1742 drv0(fdctrl)->track = fdctrl->fifo[3];
1743 drv1(fdctrl)->track = fdctrl->fifo[4];
1744 /* timers */
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);
1754 break;
1755 case 0x4D:
1756 /* FORMAT_TRACK */
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;
1763 else
1764 fdctrl->data_state &= ~FD_STATE_MULTI;
1765 fdctrl->data_state &= ~FD_STATE_SEEK;
1766 cur_drv->bps =
1767 fdctrl->fifo[2] > 7 ? 16384 : 128 << fdctrl->fifo[2];
1768 #if 0
1769 cur_drv->last_sect =
1770 cur_drv->flags & FDISK_DBL_SIDES ? fdctrl->fifo[3] :
1771 fdctrl->fifo[3] / 2;
1772 #else
1773 cur_drv->last_sect = fdctrl->fifo[3];
1774 #endif
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);
1781 break;
1782 case 0x8E:
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);
1792 } else {
1793 fdctrl_reset_fifo(fdctrl);
1795 } else if (fdctrl->data_len > 7) {
1796 /* ERROR */
1797 fdctrl->fifo[0] = 0x80 |
1798 (cur_drv->head << 2) | fdctrl->cur_drv;
1799 fdctrl_set_fifo(fdctrl, 1, 1);
1801 break;
1802 case 0x8F:
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);
1807 fd_start(cur_drv);
1808 cur_drv->dir = 0;
1809 if (fdctrl->fifo[2] + cur_drv->track >= cur_drv->max_track) {
1810 cur_drv->track = cur_drv->max_track - 1;
1811 } else {
1812 cur_drv->track += fdctrl->fifo[2];
1814 fdctrl_reset_fifo(fdctrl);
1815 fdctrl_raise_irq(fdctrl, 0x20);
1816 break;
1817 case 0xCD:
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);
1822 break;
1823 case 0xCF:
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);
1828 fd_start(cur_drv);
1829 cur_drv->dir = 1;
1830 if (fdctrl->fifo[2] > cur_drv->track) {
1831 cur_drv->track = 0;
1832 } else {
1833 cur_drv->track -= fdctrl->fifo[2];
1835 fdctrl_reset_fifo(fdctrl);
1836 /* Raise Interrupt */
1837 fdctrl_raise_irq(fdctrl, 0x20);
1838 break;
1843 static void fdctrl_result_timer(void *opaque)
1845 fdctrl_t *fdctrl = opaque;
1846 fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);