VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/fdc.c@ 20087

Last change on this file since 20087 was 20087, checked in by vboxsync, 16 years ago

TM,*: Proper timer callback locking and pvUser for devices.

  • Property svn:eol-style set to native
File size: 96.0 KB
Line 
1#ifdef VBOX
2/** @file
3 *
4 * VBox storage devices:
5 * Floppy disk controller
6 */
7
8/*
9 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
20 * Clara, CA 95054 USA or visit http://www.sun.com if you need
21 * additional information or have any questions.
22 * --------------------------------------------------------------------
23 *
24 * This code is based on:
25 *
26 * QEMU Floppy disk emulator (Intel 82078)
27 *
28 * Copyright (c) 2003 Jocelyn Mayer
29 *
30 * Permission is hereby granted, free of charge, to any person obtaining a copy
31 * of this software and associated documentation files (the "Software"), to deal
32 * in the Software without restriction, including without limitation the rights
33 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
34 * copies of the Software, and to permit persons to whom the Software is
35 * furnished to do so, subject to the following conditions:
36 *
37 * The above copyright notice and this permission notice shall be included in
38 * all copies or substantial portions of the Software.
39 *
40 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
41 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
42 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
43 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
44 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
45 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
46 * THE SOFTWARE.
47 *
48 */
49
50
51/*******************************************************************************
52* Header Files *
53*******************************************************************************/
54#define LOG_GROUP LOG_GROUP_DEV_FDC
55#include <VBox/pdmdev.h>
56#include <iprt/assert.h>
57#include <iprt/uuid.h>
58#include <iprt/string.h>
59
60#include "Builtins.h"
61#include "../vl_vbox.h"
62
63#define MAX_FD 2
64
65#define PDMIBASE_2_FDRIVE(pInterface) \
66 ((fdrive_t *)((uintptr_t)(pInterface) - RT_OFFSETOF(fdrive_t, IBase)))
67
68#define PDMIMOUNTNOTIFY_2_FDRIVE(p) \
69 ((fdrive_t *)((uintptr_t)(p) - RT_OFFSETOF(fdrive_t, IMountNotify)))
70
71#endif /* VBOX */
72
73#ifndef VBOX
74#include "vl.h"
75#endif /* !VBOX */
76
77/********************************************************/
78/* debug Floppy devices */
79/* #define DEBUG_FLOPPY */
80
81#ifndef VBOX
82 #ifdef DEBUG_FLOPPY
83 #define FLOPPY_DPRINTF(fmt, args...) \
84 do { printf("FLOPPY: " fmt , ##args); } while (0)
85 #endif
86#else /* !VBOX */
87 # ifdef LOG_ENABLED
88 static void FLOPPY_DPRINTF (const char *fmt, ...)
89 {
90 if (LogIsEnabled ()) {
91 va_list args;
92 va_start (args, fmt);
93 RTLogLogger (NULL, NULL, "floppy: %N", fmt, &args); /* %N - nested va_list * type formatting call. */
94 va_end (args);
95 }
96 }
97 # else
98 DECLINLINE(void) FLOPPY_DPRINTF(const char *pszFmt, ...) {}
99 # endif
100#endif /* !VBOX */
101
102#ifndef VBOX
103#define FLOPPY_ERROR(fmt, args...) \
104 do { printf("FLOPPY ERROR: %s: " fmt, __func__ , ##args); } while (0)
105#else /* VBOX */
106# define FLOPPY_ERROR RTLogPrintf
107#endif /* VBOX */
108
109#ifdef VBOX
110typedef struct fdctrl_t fdctrl_t;
111#endif /* VBOX */
112
113/********************************************************/
114/* Floppy drive emulation */
115
116/* Will always be a fixed parameter for us */
117#define FD_SECTOR_LEN 512
118#define FD_SECTOR_SC 2 /* Sector size code */
119
120/* Floppy disk drive emulation */
121typedef enum fdisk_type_t {
122 FDRIVE_DISK_288 = 0x01, /* 2.88 MB disk */
123 FDRIVE_DISK_144 = 0x02, /* 1.44 MB disk */
124 FDRIVE_DISK_720 = 0x03, /* 720 kB disk */
125 FDRIVE_DISK_USER = 0x04, /* User defined geometry */
126 FDRIVE_DISK_NONE = 0x05 /* No disk */
127} fdisk_type_t;
128
129typedef enum fdrive_type_t {
130 FDRIVE_DRV_144 = 0x00, /* 1.44 MB 3"5 drive */
131 FDRIVE_DRV_288 = 0x01, /* 2.88 MB 3"5 drive */
132 FDRIVE_DRV_120 = 0x02, /* 1.2 MB 5"25 drive */
133 FDRIVE_DRV_NONE = 0x03 /* No drive connected */
134} fdrive_type_t;
135
136typedef enum fdrive_flags_t {
137 FDRIVE_MOTOR_ON = 0x01, /* motor on/off */
138 FDRIVE_REVALIDATE = 0x02 /* Revalidated */
139} fdrive_flags_t;
140
141typedef enum fdisk_flags_t {
142 FDISK_DBL_SIDES = 0x01
143} fdisk_flags_t;
144
145typedef struct fdrive_t {
146#ifndef VBOX
147 BlockDriverState *bs;
148#else /* VBOX */
149 /** Pointer to the attached driver's base interface. */
150 R3PTRTYPE(PPDMIBASE) pDrvBase;
151 /** Pointer to the attached driver's block interface. */
152 R3PTRTYPE(PPDMIBLOCK) pDrvBlock;
153 /** Pointer to the attached driver's block bios interface. */
154 R3PTRTYPE(PPDMIBLOCKBIOS) pDrvBlockBios;
155 /** Pointer to the attached driver's mount interface.
156 * This is NULL if the driver isn't a removable unit. */
157 R3PTRTYPE(PPDMIMOUNT) pDrvMount;
158 /** The base interface. */
159 PDMIBASE IBase;
160 /** The block port interface. */
161 PDMIBLOCKPORT IPort;
162 /** The mount notify interface. */
163 PDMIMOUNTNOTIFY IMountNotify;
164 /** The LUN #. */
165 RTUINT iLUN;
166 /** The LED for this LUN. */
167 PDMLED Led;
168 /** The Diskette present/missing flag. */
169 bool fMediaPresent;
170#endif
171 /* Drive status */
172 fdrive_type_t drive;
173 fdrive_flags_t drflags;
174 uint8_t perpendicular; /* 2.88 MB access mode */
175 /* Position */
176 uint8_t head;
177 uint8_t track;
178 uint8_t sect;
179 /* Last operation status */
180 uint8_t dir; /* Direction */
181 uint8_t rw; /* Read/write */
182 /* Media */
183 fdisk_flags_t flags;
184 uint8_t last_sect; /* Nb sector per track */
185 uint8_t max_track; /* Nb of tracks */
186 uint16_t bps; /* Bytes per sector */
187 uint8_t ro; /* Is read-only */
188} fdrive_t;
189
190#ifndef VBOX
191static void fd_init (fdrive_t *drv, BlockDriverState *bs)
192{
193 /* Drive */
194 drv->bs = bs;
195 drv->drive = FDRIVE_DRV_NONE;
196 drv->drflags = 0;
197 drv->perpendicular = 0;
198 /* Disk */
199 drv->last_sect = 0;
200 drv->max_track = 0;
201}
202#endif
203
204static unsigned _fd_sector (uint8_t head, uint8_t track,
205 uint8_t sect, uint8_t last_sect)
206{
207 return (((track * 2) + head) * last_sect) + sect - 1; /* sect >= 1 */
208}
209
210/* Returns current position, in sectors, for given drive */
211static unsigned fd_sector (fdrive_t *drv)
212{
213 return _fd_sector(drv->head, drv->track, drv->sect, drv->last_sect);
214}
215
216static int fd_seek (fdrive_t *drv, uint8_t head, uint8_t track, uint8_t sect,
217 int enable_seek)
218{
219 uint32_t sector;
220 int ret;
221
222 if (track > drv->max_track ||
223 (head != 0 && (drv->flags & FDISK_DBL_SIDES) == 0)) {
224 FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
225 head, track, sect, 1,
226 (drv->flags & FDISK_DBL_SIDES) == 0 ? 0 : 1,
227 drv->max_track, drv->last_sect);
228 return 2;
229 }
230 if (sect > drv->last_sect) {
231 FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
232 head, track, sect, 1,
233 (drv->flags & FDISK_DBL_SIDES) == 0 ? 0 : 1,
234 drv->max_track, drv->last_sect);
235 return 3;
236 }
237 sector = _fd_sector(head, track, sect, drv->last_sect);
238 ret = 0;
239 if (sector != fd_sector(drv)) {
240#if 0
241 if (!enable_seek) {
242 FLOPPY_ERROR("no implicit seek %d %02x %02x (max=%d %02x %02x)\n",
243 head, track, sect, 1, drv->max_track, drv->last_sect);
244 return 4;
245 }
246#endif
247 drv->head = head;
248 if (drv->track != track)
249 ret = 1;
250 drv->track = track;
251 drv->sect = sect;
252 }
253
254 return ret;
255}
256
257/* Set drive back to track 0 */
258static void fd_recalibrate (fdrive_t *drv)
259{
260 FLOPPY_DPRINTF("recalibrate\n");
261 drv->head = 0;
262 drv->track = 0;
263 drv->sect = 1;
264 drv->dir = 1;
265 drv->rw = 0;
266}
267
268/* Recognize floppy formats */
269typedef struct fd_format_t {
270 fdrive_type_t drive;
271 fdisk_type_t disk;
272 uint8_t last_sect;
273 uint8_t max_track;
274 uint8_t max_head;
275 const char *str;
276} fd_format_t;
277
278static fd_format_t fd_formats[] = {
279 /* First entry is default format */
280 /* 1.44 MB 3"1/2 floppy disks */
281 { FDRIVE_DRV_144, FDRIVE_DISK_144, 18, 80, 1, "1.44 MB 3\"1/2", },
282 { FDRIVE_DRV_144, FDRIVE_DISK_144, 20, 80, 1, "1.6 MB 3\"1/2", },
283 { FDRIVE_DRV_144, FDRIVE_DISK_144, 21, 80, 1, "1.68 MB 3\"1/2", },
284 { FDRIVE_DRV_144, FDRIVE_DISK_144, 21, 82, 1, "1.72 MB 3\"1/2", },
285 { FDRIVE_DRV_144, FDRIVE_DISK_144, 21, 83, 1, "1.74 MB 3\"1/2", },
286 { FDRIVE_DRV_144, FDRIVE_DISK_144, 22, 80, 1, "1.76 MB 3\"1/2", },
287 { FDRIVE_DRV_144, FDRIVE_DISK_144, 23, 80, 1, "1.84 MB 3\"1/2", },
288 { FDRIVE_DRV_144, FDRIVE_DISK_144, 24, 80, 1, "1.92 MB 3\"1/2", },
289 /* 2.88 MB 3"1/2 floppy disks */
290 { FDRIVE_DRV_288, FDRIVE_DISK_288, 36, 80, 1, "2.88 MB 3\"1/2", },
291 { FDRIVE_DRV_288, FDRIVE_DISK_288, 39, 80, 1, "3.12 MB 3\"1/2", },
292 { FDRIVE_DRV_288, FDRIVE_DISK_288, 40, 80, 1, "3.2 MB 3\"1/2", },
293 { FDRIVE_DRV_288, FDRIVE_DISK_288, 44, 80, 1, "3.52 MB 3\"1/2", },
294 { FDRIVE_DRV_288, FDRIVE_DISK_288, 48, 80, 1, "3.84 MB 3\"1/2", },
295 /* 720 kB 3"1/2 floppy disks */
296 { FDRIVE_DRV_144, FDRIVE_DISK_720, 9, 80, 1, "720 kB 3\"1/2", },
297 { FDRIVE_DRV_144, FDRIVE_DISK_720, 10, 80, 1, "800 kB 3\"1/2", },
298 { FDRIVE_DRV_144, FDRIVE_DISK_720, 10, 82, 1, "820 kB 3\"1/2", },
299 { FDRIVE_DRV_144, FDRIVE_DISK_720, 10, 83, 1, "830 kB 3\"1/2", },
300 { FDRIVE_DRV_144, FDRIVE_DISK_720, 13, 80, 1, "1.04 MB 3\"1/2", },
301 { FDRIVE_DRV_144, FDRIVE_DISK_720, 14, 80, 1, "1.12 MB 3\"1/2", },
302 /* 1.2 MB 5"1/4 floppy disks */
303 { FDRIVE_DRV_120, FDRIVE_DISK_288, 15, 80, 1, "1.2 kB 5\"1/4", },
304 { FDRIVE_DRV_120, FDRIVE_DISK_288, 18, 80, 1, "1.44 MB 5\"1/4", },
305 { FDRIVE_DRV_120, FDRIVE_DISK_288, 18, 82, 1, "1.48 MB 5\"1/4", },
306 { FDRIVE_DRV_120, FDRIVE_DISK_288, 18, 83, 1, "1.49 MB 5\"1/4", },
307 { FDRIVE_DRV_120, FDRIVE_DISK_288, 20, 80, 1, "1.6 MB 5\"1/4", },
308 /* 720 kB 5"1/4 floppy disks */
309 { FDRIVE_DRV_120, FDRIVE_DISK_288, 9, 80, 1, "720 kB 5\"1/4", },
310 { FDRIVE_DRV_120, FDRIVE_DISK_288, 11, 80, 1, "880 kB 5\"1/4", },
311 /* 360 kB 5"1/4 floppy disks */
312 { FDRIVE_DRV_120, FDRIVE_DISK_288, 9, 40, 1, "360 kB 5\"1/4", },
313 { FDRIVE_DRV_120, FDRIVE_DISK_288, 9, 40, 0, "180 kB 5\"1/4", },
314 { FDRIVE_DRV_120, FDRIVE_DISK_288, 10, 41, 1, "410 kB 5\"1/4", },
315 { FDRIVE_DRV_120, FDRIVE_DISK_288, 10, 42, 1, "420 kB 5\"1/4", },
316 /* 320 kB 5"1/4 floppy disks */
317 { FDRIVE_DRV_120, FDRIVE_DISK_288, 8, 40, 1, "320 kB 5\"1/4", },
318 { FDRIVE_DRV_120, FDRIVE_DISK_288, 8, 40, 0, "160 kB 5\"1/4", },
319 /* 360 kB must match 5"1/4 better than 3"1/2... */
320 { FDRIVE_DRV_144, FDRIVE_DISK_720, 9, 80, 0, "360 kB 3\"1/2", },
321 /* end */
322 { FDRIVE_DRV_NONE, FDRIVE_DISK_NONE, -1, -1, 0, NULL, },
323};
324
325/* Revalidate a disk drive after a disk change */
326static void fd_revalidate (fdrive_t *drv)
327{
328 fd_format_t *parse;
329 int64_t nb_sectors, size;
330 int i, first_match, match;
331 int nb_heads, max_track, last_sect, ro;
332
333 FLOPPY_DPRINTF("revalidate\n");
334 drv->drflags &= ~FDRIVE_REVALIDATE;
335#ifndef VBOX
336 if (drv->bs != NULL && bdrv_is_inserted(drv->bs)) {
337 ro = bdrv_is_read_only(drv->bs);
338 bdrv_get_geometry_hint(drv->bs, &nb_heads, &max_track, &last_sect);
339#else /* VBOX */
340 /** @todo */ /** @todo r=bird: todo what exactly? */
341 if (drv->pDrvBlock
342 && drv->pDrvMount
343 && drv->pDrvMount->pfnIsMounted (drv->pDrvMount)) {
344 ro = drv->pDrvBlock->pfnIsReadOnly (drv->pDrvBlock);
345 nb_heads = 0;
346 max_track = 0;
347 last_sect = 0;
348#endif /* VBOX */
349 if (nb_heads != 0 && max_track != 0 && last_sect != 0) {
350 FLOPPY_DPRINTF("User defined disk (%d %d %d)",
351 nb_heads - 1, max_track, last_sect);
352 } else {
353#ifndef VBOX
354 bdrv_get_geometry(drv->bs, &nb_sectors);
355#else /* VBOX */
356 /* @todo */ /** @todo r=bird: todo what exactly?!?!? */
357 {
358 uint64_t size = drv->pDrvBlock->pfnGetSize (drv->pDrvBlock);
359 nb_sectors = size / 512;
360 }
361#endif /* VBOX */
362 match = -1;
363 first_match = -1;
364 for (i = 0;; i++) {
365 parse = &fd_formats[i];
366 if (parse->drive == FDRIVE_DRV_NONE)
367 break;
368 if (drv->drive == parse->drive ||
369 drv->drive == FDRIVE_DRV_NONE) {
370 size = (parse->max_head + 1) * parse->max_track *
371 parse->last_sect;
372 if (nb_sectors == size) {
373 match = i;
374 break;
375 }
376 if (first_match == -1)
377 first_match = i;
378 }
379 }
380 if (match == -1) {
381 if (first_match == -1)
382 match = 1;
383 else
384 match = first_match;
385 parse = &fd_formats[match];
386 }
387 nb_heads = parse->max_head + 1;
388 max_track = parse->max_track;
389 last_sect = parse->last_sect;
390 drv->drive = parse->drive;
391 FLOPPY_DPRINTF("%s floppy disk (%d h %d t %d s) %s\n", parse->str,
392 nb_heads, max_track, last_sect, ro ? "ro" : "rw");
393 }
394 if (nb_heads == 1) {
395 drv->flags &= ~FDISK_DBL_SIDES;
396 } else {
397 drv->flags |= FDISK_DBL_SIDES;
398 }
399 drv->max_track = max_track;
400 drv->last_sect = last_sect;
401 drv->ro = ro;
402#ifdef VBOX
403 drv->fMediaPresent = true;
404#endif
405 } else {
406 FLOPPY_DPRINTF("No disk in drive\n");
407 drv->last_sect = 0;
408 drv->max_track = 0;
409 drv->flags &= ~FDISK_DBL_SIDES;
410#ifdef VBOX
411 drv->fMediaPresent = false;
412#endif
413 }
414 drv->drflags |= FDRIVE_REVALIDATE;
415}
416
417/* Motor control */
418static void fd_start (fdrive_t *drv)
419{
420 drv->drflags |= FDRIVE_MOTOR_ON;
421}
422
423static void fd_stop (fdrive_t *drv)
424{
425 drv->drflags &= ~FDRIVE_MOTOR_ON;
426}
427
428/* Re-initialise a drives (motor off, repositioned) */
429static void fd_reset (fdrive_t *drv)
430{
431 fd_stop(drv);
432 fd_recalibrate(drv);
433}
434
435/********************************************************/
436/* Intel 82078 floppy disk controller emulation */
437
438#define target_ulong uint32_t
439static void fdctrl_reset (fdctrl_t *fdctrl, int do_irq);
440static void fdctrl_reset_fifo (fdctrl_t *fdctrl);
441#ifndef VBOX
442static int fdctrl_transfer_handler (void *opaque, int nchan, int dma_pos, int dma_len);
443#else /* VBOX: */
444static DECLCALLBACK(uint32_t) fdctrl_transfer_handler (PPDMDEVINS pDevIns,
445 void *opaque,
446 unsigned nchan,
447 uint32_t dma_pos,
448 uint32_t dma_len);
449#endif /* VBOX */
450static void fdctrl_raise_irq (fdctrl_t *fdctrl, uint8_t status);
451static void fdctrl_result_timer(void *opaque);
452
453static uint32_t fdctrl_read_statusB (fdctrl_t *fdctrl);
454static uint32_t fdctrl_read_dor (fdctrl_t *fdctrl);
455static void fdctrl_write_dor (fdctrl_t *fdctrl, uint32_t value);
456static uint32_t fdctrl_read_tape (fdctrl_t *fdctrl);
457static void fdctrl_write_tape (fdctrl_t *fdctrl, uint32_t value);
458static uint32_t fdctrl_read_main_status (fdctrl_t *fdctrl);
459static void fdctrl_write_rate (fdctrl_t *fdctrl, uint32_t value);
460static uint32_t fdctrl_read_data (fdctrl_t *fdctrl);
461static void fdctrl_write_data (fdctrl_t *fdctrl, uint32_t value);
462static uint32_t fdctrl_read_dir (fdctrl_t *fdctrl);
463
464enum {
465 FD_CTRL_ACTIVE = 0x01, /* XXX: suppress that */
466 FD_CTRL_RESET = 0x02,
467 FD_CTRL_SLEEP = 0x04, /* XXX: suppress that */
468 FD_CTRL_BUSY = 0x08, /* dma transfer in progress */
469 FD_CTRL_INTR = 0x10
470};
471
472enum {
473 FD_DIR_WRITE = 0,
474 FD_DIR_READ = 1,
475 FD_DIR_SCANE = 2,
476 FD_DIR_SCANL = 3,
477 FD_DIR_SCANH = 4
478};
479
480enum {
481 FD_STATE_CMD = 0x00,
482 FD_STATE_STATUS = 0x01,
483 FD_STATE_DATA = 0x02,
484 FD_STATE_STATE = 0x03,
485 FD_STATE_MULTI = 0x10,
486 FD_STATE_SEEK = 0x20,
487 FD_STATE_FORMAT = 0x40
488};
489
490#define FD_STATE(state) ((state) & FD_STATE_STATE)
491#define FD_SET_STATE(state, new_state) \
492do { (state) = ((state) & ~FD_STATE_STATE) | (new_state); } while (0)
493#define FD_MULTI_TRACK(state) ((state) & FD_STATE_MULTI)
494#define FD_DID_SEEK(state) ((state) & FD_STATE_SEEK)
495#define FD_FORMAT_CMD(state) ((state) & FD_STATE_FORMAT)
496
497struct fdctrl_t {
498#ifndef VBOX
499 fdctrl_t *fdctrl;
500#endif
501 /* Controller's identification */
502 uint8_t version;
503 /* HW */
504#ifndef VBOX
505 int irq_lvl;
506 int dma_chann;
507#else
508 uint8_t irq_lvl;
509 uint8_t dma_chann;
510#endif
511 uint32_t io_base;
512 /* Controller state */
513 QEMUTimer *result_timer;
514 uint8_t state;
515 uint8_t dma_en;
516 uint8_t cur_drv;
517 uint8_t bootsel;
518 /* Command FIFO */
519 uint8_t fifo[FD_SECTOR_LEN];
520 uint32_t data_pos;
521 uint32_t data_len;
522 uint8_t data_state;
523 uint8_t data_dir;
524 uint8_t int_status;
525 uint8_t eot; /* last wanted sector */
526 /* States kept only to be returned back */
527 /* Timers state */
528 uint8_t timer0;
529 uint8_t timer1;
530 /* precompensation */
531 uint8_t precomp_trk;
532 uint8_t config;
533 uint8_t lock;
534 /* Power down config (also with status regB access mode */
535 uint8_t pwrd;
536 /* Floppy drives */
537 fdrive_t drives[2];
538#ifdef VBOX
539 /** Pointer to device instance. */
540 PPDMDEVINS pDevIns;
541
542 /** Status Port - The base interface. */
543 PDMIBASE IBaseStatus;
544 /** Status Port - The Leds interface. */
545 PDMILEDPORTS ILeds;
546 /** Status Port - The Partner of ILeds. */
547 PPDMILEDCONNECTORS pLedsConnector;
548#endif
549};
550
551static uint32_t fdctrl_read (void *opaque, uint32_t reg)
552{
553 fdctrl_t *fdctrl = opaque;
554 uint32_t retval;
555
556 switch (reg & 0x07) {
557 case 0x01:
558 retval = fdctrl_read_statusB(fdctrl);
559 break;
560 case 0x02:
561 retval = fdctrl_read_dor(fdctrl);
562 break;
563 case 0x03:
564 retval = fdctrl_read_tape(fdctrl);
565 break;
566 case 0x04:
567 retval = fdctrl_read_main_status(fdctrl);
568 break;
569 case 0x05:
570 retval = fdctrl_read_data(fdctrl);
571 break;
572 case 0x07:
573 retval = fdctrl_read_dir(fdctrl);
574 break;
575 default:
576 retval = (uint32_t)(-1);
577 break;
578 }
579 FLOPPY_DPRINTF("read reg%d: 0x%02x\n", reg & 7, retval);
580
581 return retval;
582}
583
584static void fdctrl_write (void *opaque, uint32_t reg, uint32_t value)
585{
586 fdctrl_t *fdctrl = opaque;
587
588 FLOPPY_DPRINTF("write reg%d: 0x%02x\n", reg & 7, value);
589
590 switch (reg & 0x07) {
591 case 0x02:
592 fdctrl_write_dor(fdctrl, value);
593 break;
594 case 0x03:
595 fdctrl_write_tape(fdctrl, value);
596 break;
597 case 0x04:
598 fdctrl_write_rate(fdctrl, value);
599 break;
600 case 0x05:
601 fdctrl_write_data(fdctrl, value);
602 break;
603 default:
604 break;
605 }
606}
607
608#ifndef VBOX
609static void fd_change_cb (void *opaque)
610{
611 fdrive_t *drv = opaque;
612
613 FLOPPY_DPRINTF("disk change\n");
614 fd_revalidate(drv);
615#if 0
616 fd_recalibrate(drv);
617 fdctrl_reset_fifo(drv->fdctrl);
618 fdctrl_raise_irq(drv->fdctrl, 0x20);
619#endif
620}
621
622#else /* VBOX */
623/**
624 * Called when a media is mounted.
625 *
626 * @param pInterface Pointer to the interface structure
627 * containing the called function pointer.
628 */
629static DECLCALLBACK(void) fdMountNotify(PPDMIMOUNTNOTIFY pInterface)
630{
631 fdrive_t *drv = PDMIMOUNTNOTIFY_2_FDRIVE (pInterface);
632 LogFlow(("fdMountNotify:\n"));
633 fd_revalidate (drv);
634}
635
636/**
637 * Called when a media is unmounted
638 * @param pInterface Pointer to the interface structure containing the called function pointer.
639 */
640static DECLCALLBACK(void) fdUnmountNotify(PPDMIMOUNTNOTIFY pInterface)
641{
642 fdrive_t *drv = PDMIMOUNTNOTIFY_2_FDRIVE (pInterface);
643 LogFlow(("fdUnmountNotify:\n"));
644 fd_revalidate (drv);
645}
646#endif
647
648#ifndef VBOX
649fdctrl_t *fdctrl_init (int irq_lvl, int dma_chann, int mem_mapped,
650 uint32_t io_base,
651 BlockDriverState **fds)
652{
653 fdctrl_t *fdctrl;
654/* // int io_mem; */
655 int i;
656
657 FLOPPY_DPRINTF("init controller\n");
658 fdctrl = qemu_mallocz(sizeof(fdctrl_t));
659 if (!fdctrl)
660 return NULL;
661 fdctrl->result_timer = qemu_new_timer(vm_clock,
662 fdctrl_result_timer, fdctrl);
663
664 fdctrl->version = 0x90; /* Intel 82078 controller */
665 fdctrl->irq_lvl = irq_lvl;
666 fdctrl->dma_chann = dma_chann;
667 fdctrl->io_base = io_base;
668 fdctrl->config = 0x60; /* Implicit seek, polling & FIFO enabled */
669 if (fdctrl->dma_chann != -1) {
670 fdctrl->dma_en = 1;
671 DMA_register_channel(dma_chann, &fdctrl_transfer_handler, fdctrl);
672 } else {
673 fdctrl->dma_en = 0;
674 }
675 for (i = 0; i < 2; i++) {
676 fd_init(&fdctrl->drives[i], fds[i]);
677 if (fds[i]) {
678 bdrv_set_change_cb(fds[i],
679 &fd_change_cb, &fdctrl->drives[i]);
680 }
681 }
682 fdctrl_reset(fdctrl, 0);
683 fdctrl->state = FD_CTRL_ACTIVE;
684 if (mem_mapped) {
685 FLOPPY_ERROR("memory mapped floppy not supported by now !\n");
686#if 0
687 io_mem = cpu_register_io_memory(0, fdctrl_mem_read, fdctrl_mem_write);
688 cpu_register_physical_memory(base, 0x08, io_mem);
689#endif
690 } else {
691 register_ioport_read(io_base + 0x01, 5, 1, &fdctrl_read, fdctrl);
692 register_ioport_read(io_base + 0x07, 1, 1, &fdctrl_read, fdctrl);
693 register_ioport_write(io_base + 0x01, 5, 1, &fdctrl_write, fdctrl);
694 register_ioport_write(io_base + 0x07, 1, 1, &fdctrl_write, fdctrl);
695 }
696 for (i = 0; i < 2; i++) {
697 fd_revalidate(&fdctrl->drives[i]);
698 }
699
700 return fdctrl;
701}
702
703/* XXX: may change if moved to bdrv */
704int fdctrl_get_drive_type(fdctrl_t *fdctrl, int drive_num)
705{
706 return fdctrl->drives[drive_num].drive;
707}
708#endif
709
710/* Change IRQ state */
711static void fdctrl_reset_irq (fdctrl_t *fdctrl)
712{
713 FLOPPY_DPRINTF("Reset interrupt\n");
714#ifdef VBOX
715 PDMDevHlpISASetIrq (fdctrl->pDevIns, fdctrl->irq_lvl, 0);
716#else
717 pic_set_irq(fdctrl->irq_lvl, 0);
718#endif
719 fdctrl->state &= ~FD_CTRL_INTR;
720}
721
722static void fdctrl_raise_irq (fdctrl_t *fdctrl, uint8_t status)
723{
724 if (~(fdctrl->state & FD_CTRL_INTR)) {
725#ifdef VBOX
726 PDMDevHlpISASetIrq (fdctrl->pDevIns, fdctrl->irq_lvl, 1);
727#else
728 pic_set_irq(fdctrl->irq_lvl, 1);
729#endif
730 fdctrl->state |= FD_CTRL_INTR;
731 }
732 FLOPPY_DPRINTF("Set interrupt status to 0x%02x\n", status);
733 fdctrl->int_status = status;
734}
735
736/* Reset controller */
737static void fdctrl_reset (fdctrl_t *fdctrl, int do_irq)
738{
739 int i;
740
741 FLOPPY_DPRINTF("reset controller\n");
742 fdctrl_reset_irq(fdctrl);
743 /* Initialise controller */
744 fdctrl->cur_drv = 0;
745 /* FIFO state */
746 fdctrl->data_pos = 0;
747 fdctrl->data_len = 0;
748 fdctrl->data_state = FD_STATE_CMD;
749 fdctrl->data_dir = FD_DIR_WRITE;
750 for (i = 0; i < MAX_FD; i++)
751 fd_reset(&fdctrl->drives[i]);
752 fdctrl_reset_fifo(fdctrl);
753 if (do_irq)
754 fdctrl_raise_irq(fdctrl, 0xc0);
755}
756
757static inline fdrive_t *drv0 (fdctrl_t *fdctrl)
758{
759 return &fdctrl->drives[fdctrl->bootsel];
760}
761
762static inline fdrive_t *drv1 (fdctrl_t *fdctrl)
763{
764 return &fdctrl->drives[1 - fdctrl->bootsel];
765}
766
767static fdrive_t *get_cur_drv (fdctrl_t *fdctrl)
768{
769 return fdctrl->cur_drv == 0 ? drv0(fdctrl) : drv1(fdctrl);
770}
771
772/* Status B register : 0x01 (read-only) */
773static uint32_t fdctrl_read_statusB (fdctrl_t *fdctrl)
774{
775 FLOPPY_DPRINTF("status register: 0x00\n");
776 return 0;
777}
778
779/* Digital output register : 0x02 */
780static uint32_t fdctrl_read_dor (fdctrl_t *fdctrl)
781{
782 uint32_t retval = 0;
783
784 /* Drive motors state indicators */
785#ifndef VBOX
786 if (drv0(fdctrl)->drflags & FDRIVE_MOTOR_ON)
787 retval |= 1 << 5;
788 if (drv1(fdctrl)->drflags & FDRIVE_MOTOR_ON)
789 retval |= 1 << 4;
790#else
791 /* bit4: 0 = drive 0 motor off/1 = on */
792 if (drv0(fdctrl)->drflags & FDRIVE_MOTOR_ON)
793 retval |= RT_BIT(4);
794 /* bit5: 0 = drive 1 motor off/1 = on */
795 if (drv1(fdctrl)->drflags & FDRIVE_MOTOR_ON)
796 retval |= RT_BIT(5);
797#endif
798 /* DMA enable */
799 retval |= fdctrl->dma_en << 3;
800 /* Reset indicator */
801 retval |= (fdctrl->state & FD_CTRL_RESET) == 0 ? 0x04 : 0;
802 /* Selected drive */
803 retval |= fdctrl->cur_drv;
804 FLOPPY_DPRINTF("digital output register: 0x%02x\n", retval);
805
806 return retval;
807}
808
809static void fdctrl_write_dor (fdctrl_t *fdctrl, uint32_t value)
810{
811 /* Reset mode */
812 if (fdctrl->state & FD_CTRL_RESET) {
813 if (!(value & 0x04)) {
814 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
815 return;
816 }
817 }
818 FLOPPY_DPRINTF("digital output register set to 0x%02x\n", value);
819 /* Drive motors state indicators */
820 if (value & 0x20)
821 fd_start(drv1(fdctrl));
822 else
823 fd_stop(drv1(fdctrl));
824 if (value & 0x10)
825 fd_start(drv0(fdctrl));
826 else
827 fd_stop(drv0(fdctrl));
828 /* DMA enable */
829#if 0
830 if (fdctrl->dma_chann != -1)
831 fdctrl->dma_en = 1 - ((value >> 3) & 1);
832#endif
833 /* Reset */
834 if (!(value & 0x04)) {
835 if (!(fdctrl->state & FD_CTRL_RESET)) {
836 FLOPPY_DPRINTF("controller enter RESET state\n");
837 fdctrl->state |= FD_CTRL_RESET;
838 }
839 } else {
840 if (fdctrl->state & FD_CTRL_RESET) {
841 FLOPPY_DPRINTF("controller out of RESET state\n");
842 fdctrl_reset(fdctrl, 1);
843 fdctrl->state &= ~(FD_CTRL_RESET | FD_CTRL_SLEEP);
844 }
845 }
846 /* Selected drive */
847 fdctrl->cur_drv = value & 1;
848}
849
850/* Tape drive register : 0x03 */
851static uint32_t fdctrl_read_tape (fdctrl_t *fdctrl)
852{
853 uint32_t retval = 0;
854
855 /* Disk boot selection indicator */
856 retval |= fdctrl->bootsel << 2;
857 /* Tape indicators: never allowed */
858 FLOPPY_DPRINTF("tape drive register: 0x%02x\n", retval);
859
860 return retval;
861}
862
863static void fdctrl_write_tape (fdctrl_t *fdctrl, uint32_t value)
864{
865 /* Reset mode */
866 if (fdctrl->state & FD_CTRL_RESET) {
867 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
868 return;
869 }
870 FLOPPY_DPRINTF("tape drive register set to 0x%02x\n", value);
871 /* Disk boot selection indicator */
872 fdctrl->bootsel = (value >> 2) & 1;
873 /* Tape indicators: never allow */
874}
875
876/* Main status register : 0x04 (read) */
877static uint32_t fdctrl_read_main_status (fdctrl_t *fdctrl)
878{
879 uint32_t retval = 0;
880
881 fdctrl->state &= ~(FD_CTRL_SLEEP | FD_CTRL_RESET);
882 if (!(fdctrl->state & FD_CTRL_BUSY)) {
883 /* Data transfer allowed */
884 retval |= 0x80;
885 /* Data transfer direction indicator */
886 if (fdctrl->data_dir == FD_DIR_READ)
887 retval |= 0x40;
888 }
889 /* Should handle 0x20 for SPECIFY command */
890 /* Command busy indicator */
891 if (FD_STATE(fdctrl->data_state) == FD_STATE_DATA ||
892 FD_STATE(fdctrl->data_state) == FD_STATE_STATUS)
893 retval |= 0x10;
894 FLOPPY_DPRINTF("main status register: 0x%02x\n", retval);
895
896 return retval;
897}
898
899/* Data select rate register : 0x04 (write) */
900static void fdctrl_write_rate (fdctrl_t *fdctrl, uint32_t value)
901{
902 /* Reset mode */
903 if (fdctrl->state & FD_CTRL_RESET) {
904 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
905 return;
906 }
907 FLOPPY_DPRINTF("select rate register set to 0x%02x\n", value);
908 /* Reset: autoclear */
909 if (value & 0x80) {
910 fdctrl->state |= FD_CTRL_RESET;
911 fdctrl_reset(fdctrl, 1);
912 fdctrl->state &= ~FD_CTRL_RESET;
913 }
914 if (value & 0x40) {
915 fdctrl->state |= FD_CTRL_SLEEP;
916 fdctrl_reset(fdctrl, 1);
917 }
918/* // fdctrl.precomp = (value >> 2) & 0x07; */
919}
920
921/* Digital input register : 0x07 (read-only) */
922static uint32_t fdctrl_read_dir (fdctrl_t *fdctrl)
923{
924 uint32_t retval = 0;
925
926#ifndef VBOX
927 if (drv0(fdctrl)->drflags & FDRIVE_REVALIDATE ||
928 drv1(fdctrl)->drflags & FDRIVE_REVALIDATE)
929#else
930 fdrive_t *cur_drv = get_cur_drv(fdctrl);
931 if ( drv0(fdctrl)->drflags & FDRIVE_REVALIDATE
932 || drv1(fdctrl)->drflags & FDRIVE_REVALIDATE
933 || !cur_drv->fMediaPresent)
934#endif
935 retval |= 0x80;
936 if (retval != 0)
937 FLOPPY_DPRINTF("Floppy digital input register: 0x%02x\n", retval);
938 drv0(fdctrl)->drflags &= ~FDRIVE_REVALIDATE;
939 drv1(fdctrl)->drflags &= ~FDRIVE_REVALIDATE;
940
941 return retval;
942}
943
944/* FIFO state control */
945static void fdctrl_reset_fifo (fdctrl_t *fdctrl)
946{
947 fdctrl->data_dir = FD_DIR_WRITE;
948 fdctrl->data_pos = 0;
949 FD_SET_STATE(fdctrl->data_state, FD_STATE_CMD);
950}
951
952/* Set FIFO status for the host to read */
953static void fdctrl_set_fifo (fdctrl_t *fdctrl, int fifo_len, int do_irq)
954{
955 fdctrl->data_dir = FD_DIR_READ;
956 fdctrl->data_len = fifo_len;
957 fdctrl->data_pos = 0;
958 FD_SET_STATE(fdctrl->data_state, FD_STATE_STATUS);
959 if (do_irq)
960 fdctrl_raise_irq(fdctrl, 0x00);
961}
962
963/* Set an error: unimplemented/unknown command */
964static void fdctrl_unimplemented (fdctrl_t *fdctrl)
965{
966#if 0
967 fdrive_t *cur_drv;
968
969 cur_drv = get_cur_drv(fdctrl);
970 fdctrl->fifo[0] = 0x60 | (cur_drv->head << 2) | fdctrl->cur_drv;
971 fdctrl->fifo[1] = 0x00;
972 fdctrl->fifo[2] = 0x00;
973 fdctrl_set_fifo(fdctrl, 3, 1);
974#else
975 /* // fdctrl_reset_fifo(fdctrl); */
976 fdctrl->fifo[0] = 0x80;
977 fdctrl_set_fifo(fdctrl, 1, 0);
978#endif
979}
980
981/* Callback for transfer end (stop or abort) */
982static void fdctrl_stop_transfer (fdctrl_t *fdctrl, uint8_t status0,
983 uint8_t status1, uint8_t status2)
984{
985 fdrive_t *cur_drv;
986
987 cur_drv = get_cur_drv(fdctrl);
988 FLOPPY_DPRINTF("transfer status: %02x %02x %02x (%02x)\n",
989 status0, status1, status2,
990 status0 | (cur_drv->head << 2) | fdctrl->cur_drv);
991 fdctrl->fifo[0] = status0 | (cur_drv->head << 2) | fdctrl->cur_drv;
992 fdctrl->fifo[1] = status1;
993 fdctrl->fifo[2] = status2;
994 fdctrl->fifo[3] = cur_drv->track;
995 fdctrl->fifo[4] = cur_drv->head;
996 fdctrl->fifo[5] = cur_drv->sect;
997 fdctrl->fifo[6] = FD_SECTOR_SC;
998 fdctrl->data_dir = FD_DIR_READ;
999 if (fdctrl->state & FD_CTRL_BUSY) {
1000#ifdef VBOX
1001 PDMDevHlpDMASetDREQ (fdctrl->pDevIns, fdctrl->dma_chann, 0);
1002#else
1003 DMA_release_DREQ(fdctrl->dma_chann);
1004#endif
1005 fdctrl->state &= ~FD_CTRL_BUSY;
1006 }
1007 fdctrl_set_fifo(fdctrl, 7, 1);
1008}
1009
1010/* Prepare a data transfer (either DMA or FIFO) */
1011static void fdctrl_start_transfer (fdctrl_t *fdctrl, int direction)
1012{
1013 fdrive_t *cur_drv;
1014 uint8_t kh, kt, ks;
1015 int did_seek;
1016
1017 fdctrl->cur_drv = fdctrl->fifo[1] & 1;
1018 cur_drv = get_cur_drv(fdctrl);
1019 kt = fdctrl->fifo[2];
1020 kh = fdctrl->fifo[3];
1021 ks = fdctrl->fifo[4];
1022 FLOPPY_DPRINTF("Start transfer at %d %d %02x %02x (%d)\n",
1023 fdctrl->cur_drv, kh, kt, ks,
1024 _fd_sector(kh, kt, ks, cur_drv->last_sect));
1025 did_seek = 0;
1026 switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & 0x40)) {
1027 case 2:
1028 /* sect too big */
1029 fdctrl_stop_transfer(fdctrl, 0x40, 0x00, 0x00);
1030 fdctrl->fifo[3] = kt;
1031 fdctrl->fifo[4] = kh;
1032 fdctrl->fifo[5] = ks;
1033 return;
1034 case 3:
1035 /* track too big */
1036 fdctrl_stop_transfer(fdctrl, 0x40, 0x80, 0x00);
1037 fdctrl->fifo[3] = kt;
1038 fdctrl->fifo[4] = kh;
1039 fdctrl->fifo[5] = ks;
1040 return;
1041 case 4:
1042 /* No seek enabled */
1043 fdctrl_stop_transfer(fdctrl, 0x40, 0x00, 0x00);
1044 fdctrl->fifo[3] = kt;
1045 fdctrl->fifo[4] = kh;
1046 fdctrl->fifo[5] = ks;
1047 return;
1048 case 1:
1049 did_seek = 1;
1050 break;
1051 default:
1052 break;
1053 }
1054 /* Set the FIFO state */
1055 fdctrl->data_dir = direction;
1056 fdctrl->data_pos = 0;
1057 FD_SET_STATE(fdctrl->data_state, FD_STATE_DATA); /* FIFO ready for data */
1058 if (fdctrl->fifo[0] & 0x80)
1059 fdctrl->data_state |= FD_STATE_MULTI;
1060 else
1061 fdctrl->data_state &= ~FD_STATE_MULTI;
1062 if (did_seek)
1063 fdctrl->data_state |= FD_STATE_SEEK;
1064 else
1065 fdctrl->data_state &= ~FD_STATE_SEEK;
1066 if (fdctrl->fifo[5] == 00) {
1067 fdctrl->data_len = fdctrl->fifo[8];
1068 } else {
1069 int tmp;
1070 fdctrl->data_len = 128 << fdctrl->fifo[5];
1071 tmp = (cur_drv->last_sect - ks + 1);
1072 if (fdctrl->fifo[0] & 0x80)
1073 tmp += cur_drv->last_sect;
1074 fdctrl->data_len *= tmp;
1075 }
1076 fdctrl->eot = fdctrl->fifo[6];
1077 if (fdctrl->dma_en) {
1078 int dma_mode;
1079 /* DMA transfer are enabled. Check if DMA channel is well programmed */
1080#ifndef VBOX
1081 dma_mode = DMA_get_channel_mode(fdctrl->dma_chann);
1082#else
1083 dma_mode = PDMDevHlpDMAGetChannelMode (fdctrl->pDevIns, fdctrl->dma_chann);
1084#endif
1085 dma_mode = (dma_mode >> 2) & 3;
1086 FLOPPY_DPRINTF("dma_mode=%d direction=%d (%d - %d)\n",
1087 dma_mode, direction,
1088 (128 << fdctrl->fifo[5]) *
1089 (cur_drv->last_sect - ks + 1), fdctrl->data_len);
1090 if (((direction == FD_DIR_SCANE || direction == FD_DIR_SCANL ||
1091 direction == FD_DIR_SCANH) && dma_mode == 0) ||
1092 (direction == FD_DIR_WRITE && dma_mode == 2) ||
1093 (direction == FD_DIR_READ && dma_mode == 1)) {
1094 /* No access is allowed until DMA transfer has completed */
1095 fdctrl->state |= FD_CTRL_BUSY;
1096 /* Now, we just have to wait for the DMA controller to
1097 * recall us...
1098 */
1099#ifndef VBOX
1100 DMA_hold_DREQ(fdctrl->dma_chann);
1101 DMA_schedule(fdctrl->dma_chann);
1102#else
1103 PDMDevHlpDMASetDREQ (fdctrl->pDevIns, fdctrl->dma_chann, 1);
1104 PDMDevHlpDMASchedule (fdctrl->pDevIns);
1105#endif
1106 return;
1107 } else {
1108 FLOPPY_ERROR("dma_mode=%d direction=%d\n", dma_mode, direction);
1109 }
1110 }
1111 FLOPPY_DPRINTF("start non-DMA transfer\n");
1112 /* IO based transfer: calculate len */
1113 fdctrl_raise_irq(fdctrl, 0x00);
1114
1115 return;
1116}
1117
1118/* Prepare a transfer of deleted data */
1119static void fdctrl_start_transfer_del (fdctrl_t *fdctrl, int direction)
1120{
1121 /* We don't handle deleted data,
1122 * so we don't return *ANYTHING*
1123 */
1124 fdctrl_stop_transfer(fdctrl, 0x60, 0x00, 0x00);
1125}
1126
1127#if 0
1128#include <stdio.h>
1129#include <stdlib.h>
1130
1131static FILE * f;
1132static void dump (void *p, size_t len)
1133{
1134 size_t n;
1135
1136 if (!f) {
1137 f = fopen ("dump", "wb");
1138 if (!f)
1139 exit (0);
1140 }
1141
1142 n = fwrite (p, len, 1, f);
1143 if (n != 1) {
1144 AssertMsgFailed (("failed to dump\n"));
1145 exit (0);
1146 }
1147}
1148#else
1149#define dump(a,b) do { } while (0)
1150#endif
1151
1152/* handlers for DMA transfers */
1153#ifdef VBOX
1154static DECLCALLBACK(uint32_t) fdctrl_transfer_handler (PPDMDEVINS pDevIns,
1155 void *opaque,
1156 unsigned nchan,
1157 uint32_t dma_pos,
1158 uint32_t dma_len)
1159#else
1160static int fdctrl_transfer_handler (void *opaque, int nchan,
1161 int dma_pos, int dma_len)
1162#endif
1163{
1164 fdctrl_t *fdctrl;
1165 fdrive_t *cur_drv;
1166#ifdef VBOX
1167 int rc;
1168 uint32_t len, start_pos, rel_pos;
1169#else
1170 int len, start_pos, rel_pos;
1171#endif
1172 uint8_t status0 = 0x00, status1 = 0x00, status2 = 0x00;
1173
1174 fdctrl = opaque;
1175 if (!(fdctrl->state & FD_CTRL_BUSY)) {
1176 FLOPPY_DPRINTF("Not in DMA transfer mode !\n");
1177 return 0;
1178 }
1179 cur_drv = get_cur_drv(fdctrl);
1180 if (fdctrl->data_dir == FD_DIR_SCANE || fdctrl->data_dir == FD_DIR_SCANL ||
1181 fdctrl->data_dir == FD_DIR_SCANH)
1182 status2 = 0x04;
1183 if (dma_len > fdctrl->data_len)
1184 dma_len = fdctrl->data_len;
1185#ifndef VBOX
1186 if (cur_drv->bs == NULL) {
1187#else /* !VBOX */
1188 if (cur_drv->pDrvBlock == NULL) {
1189#endif
1190 if (fdctrl->data_dir == FD_DIR_WRITE)
1191 fdctrl_stop_transfer(fdctrl, 0x60, 0x00, 0x00);
1192 else
1193 fdctrl_stop_transfer(fdctrl, 0x40, 0x00, 0x00);
1194 len = 0;
1195 goto transfer_error;
1196 }
1197 rel_pos = fdctrl->data_pos % FD_SECTOR_LEN;
1198 for (start_pos = fdctrl->data_pos; fdctrl->data_pos < dma_len;) {
1199 len = dma_len - fdctrl->data_pos;
1200 if (len + rel_pos > FD_SECTOR_LEN)
1201 len = FD_SECTOR_LEN - rel_pos;
1202 FLOPPY_DPRINTF("copy %d bytes (%d %d %d) %d pos %d %02x "
1203 "(%d-0x%08x 0x%08x)\n", len, dma_len, fdctrl->data_pos,
1204 fdctrl->data_len, fdctrl->cur_drv, cur_drv->head,
1205 cur_drv->track, cur_drv->sect, fd_sector(cur_drv),
1206 fd_sector(cur_drv) * 512);
1207 if (fdctrl->data_dir != FD_DIR_WRITE ||
1208 len < FD_SECTOR_LEN || rel_pos != 0) {
1209 /* READ & SCAN commands and realign to a sector for WRITE */
1210#ifndef VBOX
1211 if (bdrv_read(cur_drv->bs, fd_sector(cur_drv),
1212 fdctrl->fifo, 1) < 0) {
1213 FLOPPY_DPRINTF("Floppy: error getting sector %d\n",
1214 fd_sector(cur_drv));
1215 /* Sure, image size is too small... */
1216 memset(fdctrl->fifo, 0, FD_SECTOR_LEN);
1217 }
1218#else
1219 cur_drv->Led.Asserted.s.fReading
1220 = cur_drv->Led.Actual.s.fReading = 1;
1221
1222 rc = cur_drv->pDrvBlock->pfnRead (
1223 cur_drv->pDrvBlock,
1224 fd_sector (cur_drv) * 512,
1225 fdctrl->fifo,
1226 1 * 512
1227 );
1228
1229 cur_drv->Led.Actual.s.fReading = 0;
1230
1231 if (RT_FAILURE (rc)) {
1232 AssertMsgFailed (
1233 ("Floppy: error getting sector %d, rc = %Rrc",
1234 fd_sector (cur_drv), rc));
1235 memset(fdctrl->fifo, 0, FD_SECTOR_LEN);
1236 }
1237#endif
1238 }
1239 switch (fdctrl->data_dir) {
1240 case FD_DIR_READ:
1241 /* READ commands */
1242#ifdef VBOX
1243 {
1244 uint32_t read;
1245 int rc = PDMDevHlpDMAWriteMemory(fdctrl->pDevIns, nchan,
1246 fdctrl->fifo + rel_pos,
1247 fdctrl->data_pos,
1248 len, &read);
1249 dump (fdctrl->fifo + rel_pos, len);
1250 AssertMsgRC (rc, ("DMAWriteMemory -> %Rrc\n", rc));
1251 }
1252#else
1253 DMA_write_memory (nchan, fdctrl->fifo + rel_pos,
1254 fdctrl->data_pos, len);
1255#endif
1256/* cpu_physical_memory_write(addr + fdctrl->data_pos, */
1257/* fdctrl->fifo + rel_pos, len); */
1258 break;
1259 case FD_DIR_WRITE:
1260 /* WRITE commands */
1261#ifdef VBOX
1262 {
1263 uint32_t written;
1264 int rc = PDMDevHlpDMAReadMemory(fdctrl->pDevIns, nchan,
1265 fdctrl->fifo + rel_pos,
1266 fdctrl->data_pos,
1267 len, &written);
1268 AssertMsgRC (rc, ("DMAReadMemory -> %Rrc\n", rc));
1269 }
1270#else
1271 DMA_read_memory (nchan, fdctrl->fifo + rel_pos,
1272 fdctrl->data_pos, len);
1273#endif
1274/* cpu_physical_memory_read(addr + fdctrl->data_pos, */
1275/* fdctrl->fifo + rel_pos, len); */
1276#ifndef VBOX
1277 if (bdrv_write(cur_drv->bs, fd_sector(cur_drv),
1278 fdctrl->fifo, 1) < 0) {
1279 FLOPPY_ERROR("writting sector %d\n", fd_sector(cur_drv));
1280 fdctrl_stop_transfer(fdctrl, 0x60, 0x00, 0x00);
1281 goto transfer_error;
1282 }
1283#else /* VBOX */
1284 cur_drv->Led.Asserted.s.fWriting
1285 = cur_drv->Led.Actual.s.fWriting = 1;
1286
1287 rc = cur_drv->pDrvBlock->pfnWrite (
1288 cur_drv->pDrvBlock,
1289 fd_sector (cur_drv) * 512,
1290 fdctrl->fifo,
1291 1 * 512
1292 );
1293
1294 cur_drv->Led.Actual.s.fWriting = 0;
1295
1296 if (RT_FAILURE (rc)) {
1297 AssertMsgFailed (("Floppy: error writing sector %d. rc=%Rrc",
1298 fd_sector (cur_drv), rc));
1299 fdctrl_stop_transfer (fdctrl, 0x60, 0x00, 0x00);
1300 goto transfer_error;
1301 }
1302#endif
1303 break;
1304 default:
1305 /* SCAN commands */
1306 {
1307 uint8_t tmpbuf[FD_SECTOR_LEN];
1308 int ret;
1309#ifdef VBOX
1310 int rc;
1311 uint32_t read;
1312
1313 rc = PDMDevHlpDMAReadMemory (fdctrl->pDevIns, nchan, tmpbuf,
1314 fdctrl->data_pos, len, &read);
1315 AssertMsg (RT_SUCCESS (rc), ("DMAReadMemory -> %Rrc\n", rc));
1316#else
1317 DMA_read_memory (nchan, tmpbuf, fdctrl->data_pos, len);
1318#endif
1319/* cpu_physical_memory_read(addr + fdctrl->data_pos, */
1320/* tmpbuf, len); */
1321 ret = memcmp(tmpbuf, fdctrl->fifo + rel_pos, len);
1322 if (ret == 0) {
1323 status2 = 0x08;
1324 goto end_transfer;
1325 }
1326 if ((ret < 0 && fdctrl->data_dir == FD_DIR_SCANL) ||
1327 (ret > 0 && fdctrl->data_dir == FD_DIR_SCANH)) {
1328 status2 = 0x00;
1329 goto end_transfer;
1330 }
1331 }
1332 break;
1333 }
1334 fdctrl->data_pos += len;
1335 rel_pos = fdctrl->data_pos % FD_SECTOR_LEN;
1336 if (rel_pos == 0) {
1337 /* Seek to next sector */
1338 FLOPPY_DPRINTF("seek to next sector (%d %02x %02x => %d) (%d)\n",
1339 cur_drv->head, cur_drv->track, cur_drv->sect,
1340 fd_sector(cur_drv),
1341 fdctrl->data_pos - len);
1342 /* XXX: cur_drv->sect >= cur_drv->last_sect should be an
1343 error in fact */
1344 if (cur_drv->sect >= cur_drv->last_sect ||
1345 cur_drv->sect == fdctrl->eot) {
1346 cur_drv->sect = 1;
1347 if (FD_MULTI_TRACK(fdctrl->data_state)) {
1348 if (cur_drv->head == 0 &&
1349 (cur_drv->flags & FDISK_DBL_SIDES) != 0) {
1350 cur_drv->head = 1;
1351 } else {
1352 cur_drv->head = 0;
1353 cur_drv->track++;
1354 if ((cur_drv->flags & FDISK_DBL_SIDES) == 0)
1355 break;
1356 }
1357 } else {
1358 cur_drv->track++;
1359 break;
1360 }
1361 FLOPPY_DPRINTF("seek to next track (%d %02x %02x => %d)\n",
1362 cur_drv->head, cur_drv->track,
1363 cur_drv->sect, fd_sector(cur_drv));
1364 } else {
1365 cur_drv->sect++;
1366 }
1367 }
1368 }
1369end_transfer:
1370 len = fdctrl->data_pos - start_pos;
1371 FLOPPY_DPRINTF("end transfer %d %d %d\n",
1372 fdctrl->data_pos, len, fdctrl->data_len);
1373 if (fdctrl->data_dir == FD_DIR_SCANE ||
1374 fdctrl->data_dir == FD_DIR_SCANL ||
1375 fdctrl->data_dir == FD_DIR_SCANH)
1376 status2 = 0x08;
1377 if (FD_DID_SEEK(fdctrl->data_state))
1378 status0 |= 0x20;
1379 fdctrl->data_len -= len;
1380 /* if (fdctrl->data_len == 0) */
1381 fdctrl_stop_transfer(fdctrl, status0, status1, status2);
1382transfer_error:
1383
1384 return len;
1385}
1386
1387#if 0
1388{
1389 fdctrl_t *fdctrl;
1390 fdrive_t *cur_drv;
1391 int len, start_pos, rel_pos;
1392 uint8_t status0 = 0x00, status1 = 0x00, status2 = 0x00;
1393
1394 fdctrl = opaque;
1395 if (!(fdctrl->state & FD_CTRL_BUSY)) {
1396 FLOPPY_DPRINTF("Not in DMA transfer mode !\n");
1397 return 0;
1398 }
1399 cur_drv = get_cur_drv(fdctrl);
1400 if (fdctrl->data_dir == FD_DIR_SCANE || fdctrl->data_dir == FD_DIR_SCANL ||
1401 fdctrl->data_dir == FD_DIR_SCANH)
1402 status2 = 0x04;
1403 if (size > fdctrl->data_len)
1404 size = fdctrl->data_len;
1405#ifndef VBOX
1406 if (cur_drv->bs == NULL) {
1407#else
1408 if (cur_drv->pDrvBase == NULL) {
1409#endif
1410 if (fdctrl->data_dir == FD_DIR_WRITE)
1411 fdctrl_stop_transfer(fdctrl, 0x60, 0x00, 0x00);
1412 else
1413 fdctrl_stop_transfer(fdctrl, 0x40, 0x00, 0x00);
1414 len = 0;
1415 goto transfer_error;
1416 }
1417 rel_pos = fdctrl->data_pos % FD_SECTOR_LEN;
1418 for (start_pos = fdctrl->data_pos; fdctrl->data_pos < size;) {
1419 len = size - fdctrl->data_pos;
1420 if (len + rel_pos > FD_SECTOR_LEN)
1421 len = FD_SECTOR_LEN - rel_pos;
1422 FLOPPY_DPRINTF("copy %d bytes (%d %d %d) %d pos %d %02x %02x "
1423 "(%d-0x%08x 0x%08x)\n", len, size, fdctrl->data_pos,
1424 fdctrl->data_len, fdctrl->cur_drv, cur_drv->head,
1425 cur_drv->track, cur_drv->sect, fd_sector(cur_drv),
1426 fd_sector(cur_drv) * 512, addr);
1427 if (fdctrl->data_dir != FD_DIR_WRITE ||
1428 len < FD_SECTOR_LEN || rel_pos != 0) {
1429 /* READ & SCAN commands and realign to a sector for WRITE */
1430#ifndef VBOX
1431 if (bdrv_read(cur_drv->bs, fd_sector(cur_drv),
1432 fdctrl->fifo, 1) < 0) {
1433 FLOPPY_DPRINTF("Floppy: error getting sector %d\n",
1434 fd_sector(cur_drv));
1435 /* Sure, image size is too small... */
1436 memset(fdctrl->fifo, 0, FD_SECTOR_LEN);
1437 }
1438#else
1439 int rc;
1440
1441 cur_drv->Led.Asserted.s.fReading
1442 = cur_drv->Led.Actual.s.fReading = 1;
1443
1444 rc = cur_drv->pDrvBlock->pfnRead (
1445 cur_drv->pDrvBlock,
1446 fd_sector (cur_drv) * 512,
1447 fdctrl->fifo,
1448 1 * 512
1449 );
1450
1451 cur_drv->Led.Actual.s.fReading = 0;
1452
1453 if (RT_FAILURE (rc)) {
1454 AssertMsgFailed (
1455 ("Floppy: error getting sector %d. rc=%Rrc\n",
1456 fd_sector(cur_drv), rc));
1457 /* Sure, image size is too small... */
1458 memset(fdctrl->fifo, 0, FD_SECTOR_LEN);
1459 }
1460 /* *p_fd_activity = 1; */
1461#endif
1462 }
1463 switch (fdctrl->data_dir) {
1464 case FD_DIR_READ:
1465 /* READ commands */
1466#ifdef VBOX
1467 PDMDevHlpPhysWrite (fdctrl->pDevIns, addr + fdctrl->data_pos,
1468 fdctrl->fifo + rel_pos, len);
1469#else
1470 cpu_physical_memory_write(addr + fdctrl->data_pos,
1471 fdctrl->fifo + rel_pos, len);
1472#endif
1473 break;
1474 case FD_DIR_WRITE:
1475 /* WRITE commands */
1476#ifdef VBOX
1477 {
1478 int rc;
1479
1480 PDMDevHlpPhysRead (fdctrl->pDevIns, addr + fdctrl->data_pos,
1481 fdctrl->fifo + rel_pos, len);
1482
1483 cur_drv->Led.Asserted.s.fWriting
1484 = cur_drv->Led.Actual.s.fWriting = 1;
1485
1486 rc = cur_drv->pDrvBlock->pfnWrite (
1487 cur_drv->pDrvBlock,
1488 fd_sector (cur_drv) * 512,
1489 fdctrl->fifo,
1490 1 * 512
1491 );
1492
1493 cur_drv->Led.Actual.s.fWriting = 0;
1494
1495 if (RT_FAILURE (rc)) {
1496 AssertMsgFailed (
1497 ("Floppy: error writting sector %d. rc=%Rrc\n",
1498 fd_sector(cur_drv), rc));
1499 fdctrl_stop_transfer(fdctrl, 0x60, 0x00, 0x00);
1500 goto transfer_error;
1501 }
1502 }
1503 /* *p_fd_activity = 2; */
1504#else /* !VBOX */
1505 cpu_physical_memory_read(addr + fdctrl->data_pos,
1506 fdctrl->fifo + rel_pos, len);
1507 if (bdrv_write(cur_drv->bs, fd_sector(cur_drv),
1508 fdctrl->fifo, 1) < 0) {
1509 FLOPPY_ERROR("writting sector %d\n", fd_sector(cur_drv));
1510 fdctrl_stop_transfer(fdctrl, 0x60, 0x00, 0x00);
1511 goto transfer_error;
1512 }
1513#endif
1514 break;
1515 default:
1516 /* SCAN commands */
1517 {
1518 uint8_t tmpbuf[FD_SECTOR_LEN];
1519 int ret;
1520#ifdef VBOX
1521 PDMDevHlpPhysRead (fdctrl->pDevIns, addr + fdctrl->data_pos,
1522 tmpbuf, len);
1523#else
1524 cpu_physical_memory_read(addr + fdctrl->data_pos,
1525 tmpbuf, len);
1526#endif
1527 ret = memcmp(tmpbuf, fdctrl->fifo + rel_pos, len);
1528 if (ret == 0) {
1529 status2 = 0x08;
1530 goto end_transfer;
1531 }
1532 if ((ret < 0 && fdctrl->data_dir == FD_DIR_SCANL) ||
1533 (ret > 0 && fdctrl->data_dir == FD_DIR_SCANH)) {
1534 status2 = 0x00;
1535 goto end_transfer;
1536 }
1537 }
1538 break;
1539 }
1540 fdctrl->data_pos += len;
1541 rel_pos = fdctrl->data_pos % FD_SECTOR_LEN;
1542 if (rel_pos == 0) {
1543 /* Seek to next sector */
1544 FLOPPY_DPRINTF("seek to next sector (%d %02x %02x => %d) (%d)\n",
1545 cur_drv->head, cur_drv->track, cur_drv->sect,
1546 fd_sector(cur_drv),
1547 fdctrl->data_pos - size);
1548 /* XXX: cur_drv->sect >= cur_drv->last_sect should be an
1549 error in fact */
1550 if (cur_drv->sect >= cur_drv->last_sect ||
1551 cur_drv->sect == fdctrl->eot) {
1552 cur_drv->sect = 1;
1553 if (FD_MULTI_TRACK(fdctrl->data_state)) {
1554 if (cur_drv->head == 0 &&
1555 (cur_drv->flags & FDISK_DBL_SIDES) != 0) {
1556 cur_drv->head = 1;
1557 } else {
1558 cur_drv->head = 0;
1559 cur_drv->track++;
1560 if ((cur_drv->flags & FDISK_DBL_SIDES) == 0)
1561 break;
1562 }
1563 } else {
1564 cur_drv->track++;
1565 break;
1566 }
1567 FLOPPY_DPRINTF("seek to next track (%d %02x %02x => %d)\n",
1568 cur_drv->head, cur_drv->track,
1569 cur_drv->sect, fd_sector(cur_drv));
1570 } else {
1571 cur_drv->sect++;
1572 }
1573 }
1574 }
1575end_transfer:
1576 len = fdctrl->data_pos - start_pos;
1577 FLOPPY_DPRINTF("end transfer %d %d %d\n",
1578 fdctrl->data_pos, len, fdctrl->data_len);
1579 if (fdctrl->data_dir == FD_DIR_SCANE ||
1580 fdctrl->data_dir == FD_DIR_SCANL ||
1581 fdctrl->data_dir == FD_DIR_SCANH)
1582 status2 = 0x08;
1583 if (FD_DID_SEEK(fdctrl->data_state))
1584 status0 |= 0x20;
1585 fdctrl->data_len -= len;
1586 /* // if (fdctrl->data_len == 0) */
1587 fdctrl_stop_transfer(fdctrl, status0, status1, status2);
1588transfer_error:
1589
1590 return len;
1591}
1592#endif
1593
1594/* Data register : 0x05 */
1595static uint32_t fdctrl_read_data (fdctrl_t *fdctrl)
1596{
1597 fdrive_t *cur_drv;
1598 uint32_t retval = 0;
1599 int pos, len;
1600#ifdef VBOX
1601 int rc;
1602#endif
1603
1604 cur_drv = get_cur_drv(fdctrl);
1605 fdctrl->state &= ~FD_CTRL_SLEEP;
1606 if (FD_STATE(fdctrl->data_state) == FD_STATE_CMD) {
1607 FLOPPY_ERROR("can't read data in CMD state\n");
1608 return 0;
1609 }
1610 pos = fdctrl->data_pos;
1611 if (FD_STATE(fdctrl->data_state) == FD_STATE_DATA) {
1612 pos %= FD_SECTOR_LEN;
1613 if (pos == 0) {
1614 len = fdctrl->data_len - fdctrl->data_pos;
1615 if (len > FD_SECTOR_LEN)
1616 len = FD_SECTOR_LEN;
1617#ifdef VBOX
1618 cur_drv->Led.Asserted.s.fReading
1619 = cur_drv->Led.Actual.s.fReading = 1;
1620
1621 rc = cur_drv->pDrvBlock->pfnRead (
1622 cur_drv->pDrvBlock,
1623 fd_sector (cur_drv) * 512,
1624 fdctrl->fifo,
1625 len * 512
1626 );
1627
1628 cur_drv->Led.Actual.s.fReading = 0;
1629
1630 if (RT_FAILURE (rc)) {
1631 AssertMsgFailed (
1632 ("Floppy: Failure to read sector %d. rc=%Rrc",
1633 fd_sector (cur_drv), rc));
1634 }
1635#else
1636 bdrv_read(cur_drv->bs, fd_sector(cur_drv),
1637 fdctrl->fifo, len);
1638#endif
1639 }
1640 }
1641 retval = fdctrl->fifo[pos];
1642 if (++fdctrl->data_pos == fdctrl->data_len) {
1643 fdctrl->data_pos = 0;
1644 /* Switch from transfer mode to status mode
1645 * then from status mode to command mode
1646 */
1647 if (FD_STATE(fdctrl->data_state) == FD_STATE_DATA) {
1648 fdctrl_stop_transfer(fdctrl, 0x20, 0x00, 0x00);
1649 } else {
1650 fdctrl_reset_fifo(fdctrl);
1651 fdctrl_reset_irq(fdctrl);
1652 }
1653 }
1654 FLOPPY_DPRINTF("data register: 0x%02x\n", retval);
1655
1656 return retval;
1657}
1658
1659static void fdctrl_format_sector (fdctrl_t *fdctrl)
1660{
1661 fdrive_t *cur_drv;
1662 uint8_t kh, kt, ks;
1663 int did_seek;
1664#ifdef VBOX
1665 int ok = 0, rc;
1666#endif
1667
1668 fdctrl->cur_drv = fdctrl->fifo[1] & 1;
1669 cur_drv = get_cur_drv(fdctrl);
1670 kt = fdctrl->fifo[6];
1671 kh = fdctrl->fifo[7];
1672 ks = fdctrl->fifo[8];
1673 FLOPPY_DPRINTF("format sector at %d %d %02x %02x (%d)\n",
1674 fdctrl->cur_drv, kh, kt, ks,
1675 _fd_sector(kh, kt, ks, cur_drv->last_sect));
1676 did_seek = 0;
1677 switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & 0x40)) {
1678 case 2:
1679 /* sect too big */
1680 fdctrl_stop_transfer(fdctrl, 0x40, 0x00, 0x00);
1681 fdctrl->fifo[3] = kt;
1682 fdctrl->fifo[4] = kh;
1683 fdctrl->fifo[5] = ks;
1684 return;
1685 case 3:
1686 /* track too big */
1687 fdctrl_stop_transfer(fdctrl, 0x40, 0x80, 0x00);
1688 fdctrl->fifo[3] = kt;
1689 fdctrl->fifo[4] = kh;
1690 fdctrl->fifo[5] = ks;
1691 return;
1692 case 4:
1693 /* No seek enabled */
1694 fdctrl_stop_transfer(fdctrl, 0x40, 0x00, 0x00);
1695 fdctrl->fifo[3] = kt;
1696 fdctrl->fifo[4] = kh;
1697 fdctrl->fifo[5] = ks;
1698 return;
1699 case 1:
1700 did_seek = 1;
1701 fdctrl->data_state |= FD_STATE_SEEK;
1702 break;
1703 default:
1704 break;
1705 }
1706 memset(fdctrl->fifo, 0, FD_SECTOR_LEN);
1707#ifdef VBOX
1708 /* *p_fd_activity = 2; */
1709 if (cur_drv->pDrvBlock) {
1710 cur_drv->Led.Asserted.s.fWriting = cur_drv->Led.Actual.s.fWriting = 1;
1711
1712 rc = cur_drv->pDrvBlock->pfnWrite (
1713 cur_drv->pDrvBlock,
1714 fd_sector (cur_drv) * 512,
1715 fdctrl->fifo,
1716 1 * 512
1717 );
1718
1719 cur_drv->Led.Actual.s.fWriting = 0;
1720
1721 if (RT_FAILURE (rc)) {
1722 AssertMsgFailed (("Floppy: error formating sector %d. rc=%Rrc\n",
1723 fd_sector (cur_drv), rc));
1724 fdctrl_stop_transfer(fdctrl, 0x60, 0x00, 0x00);
1725 }
1726 else {
1727 ok = 1;
1728 }
1729 }
1730 if (ok) {
1731#else
1732 if (cur_drv->bs == NULL ||
1733 bdrv_write(cur_drv->bs, fd_sector(cur_drv), fdctrl->fifo, 1) < 0) {
1734 FLOPPY_ERROR("formating sector %d\n", fd_sector(cur_drv));
1735 fdctrl_stop_transfer(fdctrl, 0x60, 0x00, 0x00);
1736 } else {
1737#endif
1738 if (cur_drv->sect == cur_drv->last_sect) {
1739 fdctrl->data_state &= ~FD_STATE_FORMAT;
1740 /* Last sector done */
1741 if (FD_DID_SEEK(fdctrl->data_state))
1742 fdctrl_stop_transfer(fdctrl, 0x20, 0x00, 0x00);
1743 else
1744 fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);
1745 } else {
1746 /* More to do */
1747 fdctrl->data_pos = 0;
1748 fdctrl->data_len = 4;
1749 }
1750 }
1751}
1752
1753static void fdctrl_write_data (fdctrl_t *fdctrl, uint32_t value)
1754{
1755 fdrive_t *cur_drv;
1756
1757 cur_drv = get_cur_drv(fdctrl);
1758 /* Reset mode */
1759 if (fdctrl->state & FD_CTRL_RESET) {
1760 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
1761 return;
1762 }
1763 fdctrl->state &= ~FD_CTRL_SLEEP;
1764 if (FD_STATE(fdctrl->data_state) == FD_STATE_STATUS) {
1765 FLOPPY_ERROR("can't write data in status mode\n");
1766 return;
1767 }
1768 /* Is it write command time ? */
1769 if (FD_STATE(fdctrl->data_state) == FD_STATE_DATA) {
1770 /* FIFO data write */
1771 fdctrl->fifo[fdctrl->data_pos++] = value;
1772 if (fdctrl->data_pos % FD_SECTOR_LEN == (FD_SECTOR_LEN - 1) ||
1773 fdctrl->data_pos == fdctrl->data_len) {
1774#ifdef VBOX
1775 int rc;
1776 /* *p_fd_activity = 2; */
1777 cur_drv->Led.Asserted.s.fWriting
1778 = cur_drv->Led.Actual.s.fWriting = 1;
1779
1780 rc = cur_drv->pDrvBlock->pfnWrite (
1781 cur_drv->pDrvBlock,
1782 fd_sector (cur_drv) * 512,
1783 fdctrl->fifo,
1784 FD_SECTOR_LEN * 512
1785 );
1786
1787 cur_drv->Led.Actual.s.fWriting = 0;
1788
1789 if (RT_FAILURE (rc)) {
1790 AssertMsgFailed (
1791 ("Floppy: failed to write sector %d. rc=%Rrc\n",
1792 fd_sector (cur_drv), rc));
1793 }
1794#else
1795 bdrv_write(cur_drv->bs, fd_sector(cur_drv),
1796 fdctrl->fifo, FD_SECTOR_LEN);
1797#endif
1798 }
1799 /* Switch from transfer mode to status mode
1800 * then from status mode to command mode
1801 */
1802 if (FD_STATE(fdctrl->data_state) == FD_STATE_DATA)
1803 fdctrl_stop_transfer(fdctrl, 0x20, 0x00, 0x00);
1804 return;
1805 }
1806 if (fdctrl->data_pos == 0) {
1807 /* Command */
1808 switch (value & 0x5F) {
1809 case 0x46:
1810 /* READ variants */
1811 FLOPPY_DPRINTF("READ command\n");
1812 /* 8 parameters cmd */
1813 fdctrl->data_len = 9;
1814 goto enqueue;
1815 case 0x4C:
1816 /* READ_DELETED variants */
1817 FLOPPY_DPRINTF("READ_DELETED command\n");
1818 /* 8 parameters cmd */
1819 fdctrl->data_len = 9;
1820 goto enqueue;
1821 case 0x50:
1822 /* SCAN_EQUAL variants */
1823 FLOPPY_DPRINTF("SCAN_EQUAL command\n");
1824 /* 8 parameters cmd */
1825 fdctrl->data_len = 9;
1826 goto enqueue;
1827 case 0x56:
1828 /* VERIFY variants */
1829 FLOPPY_DPRINTF("VERIFY command\n");
1830 /* 8 parameters cmd */
1831 fdctrl->data_len = 9;
1832 goto enqueue;
1833 case 0x59:
1834 /* SCAN_LOW_OR_EQUAL variants */
1835 FLOPPY_DPRINTF("SCAN_LOW_OR_EQUAL command\n");
1836 /* 8 parameters cmd */
1837 fdctrl->data_len = 9;
1838 goto enqueue;
1839 case 0x5D:
1840 /* SCAN_HIGH_OR_EQUAL variants */
1841 FLOPPY_DPRINTF("SCAN_HIGH_OR_EQUAL command\n");
1842 /* 8 parameters cmd */
1843 fdctrl->data_len = 9;
1844 goto enqueue;
1845 default:
1846 break;
1847 }
1848 switch (value & 0x7F) {
1849 case 0x45:
1850 /* WRITE variants */
1851 FLOPPY_DPRINTF("WRITE command\n");
1852 /* 8 parameters cmd */
1853 fdctrl->data_len = 9;
1854 goto enqueue;
1855 case 0x49:
1856 /* WRITE_DELETED variants */
1857 FLOPPY_DPRINTF("WRITE_DELETED command\n");
1858 /* 8 parameters cmd */
1859 fdctrl->data_len = 9;
1860 goto enqueue;
1861 default:
1862 break;
1863 }
1864 switch (value) {
1865 case 0x03:
1866 /* SPECIFY */
1867 FLOPPY_DPRINTF("SPECIFY command\n");
1868 /* 1 parameter cmd */
1869 fdctrl->data_len = 3;
1870 goto enqueue;
1871 case 0x04:
1872 /* SENSE_DRIVE_STATUS */
1873 FLOPPY_DPRINTF("SENSE_DRIVE_STATUS command\n");
1874 /* 1 parameter cmd */
1875 fdctrl->data_len = 2;
1876 goto enqueue;
1877 case 0x07:
1878 /* RECALIBRATE */
1879 FLOPPY_DPRINTF("RECALIBRATE command\n");
1880 /* 1 parameter cmd */
1881 fdctrl->data_len = 2;
1882 goto enqueue;
1883 case 0x08:
1884 /* SENSE_INTERRUPT_STATUS */
1885 FLOPPY_DPRINTF("SENSE_INTERRUPT_STATUS command (%02x)\n",
1886 fdctrl->int_status);
1887 /* No parameters cmd: returns status if no interrupt */
1888#if 0
1889 fdctrl->fifo[0] =
1890 fdctrl->int_status | (cur_drv->head << 2) | fdctrl->cur_drv;
1891#else
1892 /* XXX: int_status handling is broken for read/write
1893 commands, so we do this hack. It should be suppressed
1894 ASAP */
1895 fdctrl->fifo[0] =
1896 0x20 | (cur_drv->head << 2) | fdctrl->cur_drv;
1897#endif
1898 fdctrl->fifo[1] = cur_drv->track;
1899 fdctrl_set_fifo(fdctrl, 2, 0);
1900 fdctrl_reset_irq(fdctrl);
1901 fdctrl->int_status = 0xC0;
1902 return;
1903 case 0x0E:
1904 /* DUMPREG */
1905 FLOPPY_DPRINTF("DUMPREG command\n");
1906 /* Drives position */
1907 fdctrl->fifo[0] = drv0(fdctrl)->track;
1908 fdctrl->fifo[1] = drv1(fdctrl)->track;
1909 fdctrl->fifo[2] = 0;
1910 fdctrl->fifo[3] = 0;
1911 /* timers */
1912 fdctrl->fifo[4] = fdctrl->timer0;
1913 fdctrl->fifo[5] = (fdctrl->timer1 << 1) | fdctrl->dma_en;
1914 fdctrl->fifo[6] = cur_drv->last_sect;
1915 fdctrl->fifo[7] = (fdctrl->lock << 7) |
1916 (cur_drv->perpendicular << 2);
1917 fdctrl->fifo[8] = fdctrl->config;
1918 fdctrl->fifo[9] = fdctrl->precomp_trk;
1919 fdctrl_set_fifo(fdctrl, 10, 0);
1920 return;
1921 case 0x0F:
1922 /* SEEK */
1923 FLOPPY_DPRINTF("SEEK command\n");
1924 /* 2 parameters cmd */
1925 fdctrl->data_len = 3;
1926 goto enqueue;
1927 case 0x10:
1928 /* VERSION */
1929 FLOPPY_DPRINTF("VERSION command\n");
1930 /* No parameters cmd */
1931 /* Controller's version */
1932 fdctrl->fifo[0] = fdctrl->version;
1933 fdctrl_set_fifo(fdctrl, 1, 1);
1934 return;
1935 case 0x12:
1936 /* PERPENDICULAR_MODE */
1937 FLOPPY_DPRINTF("PERPENDICULAR_MODE command\n");
1938 /* 1 parameter cmd */
1939 fdctrl->data_len = 2;
1940 goto enqueue;
1941 case 0x13:
1942 /* CONFIGURE */
1943 FLOPPY_DPRINTF("CONFIGURE command\n");
1944 /* 3 parameters cmd */
1945 fdctrl->data_len = 4;
1946 goto enqueue;
1947 case 0x14:
1948 /* UNLOCK */
1949 FLOPPY_DPRINTF("UNLOCK command\n");
1950 /* No parameters cmd */
1951 fdctrl->lock = 0;
1952 fdctrl->fifo[0] = 0;
1953 fdctrl_set_fifo(fdctrl, 1, 0);
1954 return;
1955 case 0x17:
1956 /* POWERDOWN_MODE */
1957 FLOPPY_DPRINTF("POWERDOWN_MODE command\n");
1958 /* 2 parameters cmd */
1959 fdctrl->data_len = 3;
1960 goto enqueue;
1961 case 0x18:
1962 /* PART_ID */
1963 FLOPPY_DPRINTF("PART_ID command\n");
1964 /* No parameters cmd */
1965 fdctrl->fifo[0] = 0x41; /* Stepping 1 */
1966 fdctrl_set_fifo(fdctrl, 1, 0);
1967 return;
1968 case 0x2C:
1969 /* SAVE */
1970 FLOPPY_DPRINTF("SAVE command\n");
1971 /* No parameters cmd */
1972 fdctrl->fifo[0] = 0;
1973 fdctrl->fifo[1] = 0;
1974 /* Drives position */
1975 fdctrl->fifo[2] = drv0(fdctrl)->track;
1976 fdctrl->fifo[3] = drv1(fdctrl)->track;
1977 fdctrl->fifo[4] = 0;
1978 fdctrl->fifo[5] = 0;
1979 /* timers */
1980 fdctrl->fifo[6] = fdctrl->timer0;
1981 fdctrl->fifo[7] = fdctrl->timer1;
1982 fdctrl->fifo[8] = cur_drv->last_sect;
1983 fdctrl->fifo[9] = (fdctrl->lock << 7) |
1984 (cur_drv->perpendicular << 2);
1985 fdctrl->fifo[10] = fdctrl->config;
1986 fdctrl->fifo[11] = fdctrl->precomp_trk;
1987 fdctrl->fifo[12] = fdctrl->pwrd;
1988 fdctrl->fifo[13] = 0;
1989 fdctrl->fifo[14] = 0;
1990 fdctrl_set_fifo(fdctrl, 15, 1);
1991 return;
1992 case 0x33:
1993 /* OPTION */
1994 FLOPPY_DPRINTF("OPTION command\n");
1995 /* 1 parameter cmd */
1996 fdctrl->data_len = 2;
1997 goto enqueue;
1998 case 0x42:
1999 /* READ_TRACK */
2000 FLOPPY_DPRINTF("READ_TRACK command\n");
2001 /* 8 parameters cmd */
2002 fdctrl->data_len = 9;
2003 goto enqueue;
2004 case 0x4A:
2005 /* READ_ID */
2006 FLOPPY_DPRINTF("READ_ID command\n");
2007 /* 1 parameter cmd */
2008 fdctrl->data_len = 2;
2009 goto enqueue;
2010 case 0x4C:
2011 /* RESTORE */
2012 FLOPPY_DPRINTF("RESTORE command\n");
2013 /* 17 parameters cmd */
2014 fdctrl->data_len = 18;
2015 goto enqueue;
2016 case 0x4D:
2017 /* FORMAT_TRACK */
2018 FLOPPY_DPRINTF("FORMAT_TRACK command\n");
2019 /* 5 parameters cmd */
2020 fdctrl->data_len = 6;
2021 goto enqueue;
2022 case 0x8E:
2023 /* DRIVE_SPECIFICATION_COMMAND */
2024 FLOPPY_DPRINTF("DRIVE_SPECIFICATION_COMMAND command\n");
2025 /* 5 parameters cmd */
2026 fdctrl->data_len = 6;
2027 goto enqueue;
2028 case 0x8F:
2029 /* RELATIVE_SEEK_OUT */
2030 FLOPPY_DPRINTF("RELATIVE_SEEK_OUT command\n");
2031 /* 2 parameters cmd */
2032 fdctrl->data_len = 3;
2033 goto enqueue;
2034 case 0x94:
2035 /* LOCK */
2036 FLOPPY_DPRINTF("LOCK command\n");
2037 /* No parameters cmd */
2038 fdctrl->lock = 1;
2039 fdctrl->fifo[0] = 0x10;
2040 fdctrl_set_fifo(fdctrl, 1, 1);
2041 return;
2042 case 0xCD:
2043 /* FORMAT_AND_WRITE */
2044 FLOPPY_DPRINTF("FORMAT_AND_WRITE command\n");
2045 /* 10 parameters cmd */
2046 fdctrl->data_len = 11;
2047 goto enqueue;
2048 case 0xCF:
2049 /* RELATIVE_SEEK_IN */
2050 FLOPPY_DPRINTF("RELATIVE_SEEK_IN command\n");
2051 /* 2 parameters cmd */
2052 fdctrl->data_len = 3;
2053 goto enqueue;
2054 default:
2055 /* Unknown command */
2056 FLOPPY_ERROR("unknown command: 0x%02x\n", value);
2057 fdctrl_unimplemented(fdctrl);
2058 return;
2059 }
2060 }
2061enqueue:
2062 FLOPPY_DPRINTF("%s: %02x\n", __func__, value);
2063 fdctrl->fifo[fdctrl->data_pos] = value;
2064 if (++fdctrl->data_pos == fdctrl->data_len) {
2065 /* We now have all parameters
2066 * and will be able to treat the command
2067 */
2068 if (fdctrl->data_state & FD_STATE_FORMAT) {
2069 fdctrl_format_sector(fdctrl);
2070 return;
2071 }
2072 switch (fdctrl->fifo[0] & 0x1F) {
2073 case 0x06:
2074 {
2075 /* READ variants */
2076 FLOPPY_DPRINTF("treat READ command\n");
2077 fdctrl_start_transfer(fdctrl, FD_DIR_READ);
2078 return;
2079 }
2080 case 0x0C:
2081 /* READ_DELETED variants */
2082/* // FLOPPY_DPRINTF("treat READ_DELETED command\n"); */
2083 FLOPPY_ERROR("treat READ_DELETED command\n");
2084 fdctrl_start_transfer_del(fdctrl, FD_DIR_READ);
2085 return;
2086 case 0x16:
2087 /* VERIFY variants */
2088/* // FLOPPY_DPRINTF("treat VERIFY command\n"); */
2089 FLOPPY_ERROR("treat VERIFY command\n");
2090 fdctrl_stop_transfer(fdctrl, 0x20, 0x00, 0x00);
2091 return;
2092 case 0x10:
2093 /* SCAN_EQUAL variants */
2094/* // FLOPPY_DPRINTF("treat SCAN_EQUAL command\n"); */
2095 FLOPPY_ERROR("treat SCAN_EQUAL command\n");
2096 fdctrl_start_transfer(fdctrl, FD_DIR_SCANE);
2097 return;
2098 case 0x19:
2099 /* SCAN_LOW_OR_EQUAL variants */
2100/* // FLOPPY_DPRINTF("treat SCAN_LOW_OR_EQUAL command\n"); */
2101 FLOPPY_ERROR("treat SCAN_LOW_OR_EQUAL command\n");
2102 fdctrl_start_transfer(fdctrl, FD_DIR_SCANL);
2103 return;
2104 case 0x1D:
2105 /* SCAN_HIGH_OR_EQUAL variants */
2106/* // FLOPPY_DPRINTF("treat SCAN_HIGH_OR_EQUAL command\n"); */
2107 FLOPPY_ERROR("treat SCAN_HIGH_OR_EQUAL command\n");
2108 fdctrl_start_transfer(fdctrl, FD_DIR_SCANH);
2109 return;
2110 default:
2111 break;
2112 }
2113 switch (fdctrl->fifo[0] & 0x3F) {
2114 case 0x05:
2115 /* WRITE variants */
2116 FLOPPY_DPRINTF("treat WRITE command (%02x)\n", fdctrl->fifo[0]);
2117 fdctrl_start_transfer(fdctrl, FD_DIR_WRITE);
2118 return;
2119 case 0x09:
2120 /* WRITE_DELETED variants */
2121/* // FLOPPY_DPRINTF("treat WRITE_DELETED command\n"); */
2122 FLOPPY_ERROR("treat WRITE_DELETED command\n");
2123 fdctrl_start_transfer_del(fdctrl, FD_DIR_WRITE);
2124 return;
2125 default:
2126 break;
2127 }
2128 switch (fdctrl->fifo[0]) {
2129 case 0x03:
2130 /* SPECIFY */
2131 FLOPPY_DPRINTF("treat SPECIFY command\n");
2132 fdctrl->timer0 = (fdctrl->fifo[1] >> 4) & 0xF;
2133 fdctrl->timer1 = fdctrl->fifo[2] >> 1;
2134 fdctrl->dma_en = 1 - (fdctrl->fifo[2] & 1) ;
2135 /* No result back */
2136 fdctrl_reset_fifo(fdctrl);
2137 break;
2138 case 0x04:
2139 /* SENSE_DRIVE_STATUS */
2140 FLOPPY_DPRINTF("treat SENSE_DRIVE_STATUS command\n");
2141 fdctrl->cur_drv = fdctrl->fifo[1] & 1;
2142 cur_drv = get_cur_drv(fdctrl);
2143 cur_drv->head = (fdctrl->fifo[1] >> 2) & 1;
2144 /* 1 Byte status back */
2145 fdctrl->fifo[0] = (cur_drv->ro << 6) |
2146 (cur_drv->track == 0 ? 0x10 : 0x00) |
2147 (cur_drv->head << 2) |
2148 fdctrl->cur_drv |
2149 0x28;
2150 fdctrl_set_fifo(fdctrl, 1, 0);
2151 break;
2152 case 0x07:
2153 /* RECALIBRATE */
2154 FLOPPY_DPRINTF("treat RECALIBRATE command\n");
2155 fdctrl->cur_drv = fdctrl->fifo[1] & 1;
2156 cur_drv = get_cur_drv(fdctrl);
2157 fd_recalibrate(cur_drv);
2158 fdctrl_reset_fifo(fdctrl);
2159 /* Raise Interrupt */
2160 fdctrl_raise_irq(fdctrl, 0x20);
2161 break;
2162 case 0x0F:
2163 /* SEEK */
2164 FLOPPY_DPRINTF("treat SEEK command\n");
2165 fdctrl->cur_drv = fdctrl->fifo[1] & 1;
2166 cur_drv = get_cur_drv(fdctrl);
2167 fd_start(cur_drv);
2168 if (fdctrl->fifo[2] <= cur_drv->track)
2169 cur_drv->dir = 1;
2170 else
2171 cur_drv->dir = 0;
2172 fdctrl_reset_fifo(fdctrl);
2173#ifndef VBOX
2174 if (fdctrl->fifo[2] > cur_drv->max_track) {
2175#else
2176 if ( fdctrl->fifo[2] > cur_drv->max_track
2177 && cur_drv->fMediaPresent) {
2178#endif
2179 fdctrl_raise_irq(fdctrl, 0x60);
2180 } else {
2181 cur_drv->track = fdctrl->fifo[2];
2182 /* Raise Interrupt */
2183 fdctrl_raise_irq(fdctrl, 0x20);
2184 }
2185 break;
2186 case 0x12:
2187 /* PERPENDICULAR_MODE */
2188 FLOPPY_DPRINTF("treat PERPENDICULAR_MODE command\n");
2189 if (fdctrl->fifo[1] & 0x80)
2190 cur_drv->perpendicular = fdctrl->fifo[1] & 0x7;
2191 /* No result back */
2192 fdctrl_reset_fifo(fdctrl);
2193 break;
2194 case 0x13:
2195 /* CONFIGURE */
2196 FLOPPY_DPRINTF("treat CONFIGURE command\n");
2197 fdctrl->config = fdctrl->fifo[2];
2198 fdctrl->precomp_trk = fdctrl->fifo[3];
2199 /* No result back */
2200 fdctrl_reset_fifo(fdctrl);
2201 break;
2202 case 0x17:
2203 /* POWERDOWN_MODE */
2204 FLOPPY_DPRINTF("treat POWERDOWN_MODE command\n");
2205 fdctrl->pwrd = fdctrl->fifo[1];
2206 fdctrl->fifo[0] = fdctrl->fifo[1];
2207 fdctrl_set_fifo(fdctrl, 1, 1);
2208 break;
2209 case 0x33:
2210 /* OPTION */
2211 FLOPPY_DPRINTF("treat OPTION command\n");
2212 /* No result back */
2213 fdctrl_reset_fifo(fdctrl);
2214 break;
2215 case 0x42:
2216 /* READ_TRACK */
2217/* // FLOPPY_DPRINTF("treat READ_TRACK command\n"); */
2218 FLOPPY_ERROR("treat READ_TRACK command\n");
2219 fdctrl_start_transfer(fdctrl, FD_DIR_READ);
2220 break;
2221 case 0x4A:
2222 /* READ_ID */
2223 FLOPPY_DPRINTF("treat READ_ID command\n");
2224 /* XXX: should set main status register to busy */
2225 cur_drv->head = (fdctrl->fifo[1] >> 2) & 1;
2226#ifdef VBOX
2227 TMTimerSetMillies(fdctrl->result_timer, 1000 / 50);
2228#else
2229 qemu_mod_timer(fdctrl->result_timer,
2230 qemu_get_clock(vm_clock) + (ticks_per_sec / 50));
2231#endif
2232 break;
2233 case 0x4C:
2234 /* RESTORE */
2235 FLOPPY_DPRINTF("treat RESTORE command\n");
2236 /* Drives position */
2237 drv0(fdctrl)->track = fdctrl->fifo[3];
2238 drv1(fdctrl)->track = fdctrl->fifo[4];
2239 /* timers */
2240 fdctrl->timer0 = fdctrl->fifo[7];
2241 fdctrl->timer1 = fdctrl->fifo[8];
2242 cur_drv->last_sect = fdctrl->fifo[9];
2243 fdctrl->lock = fdctrl->fifo[10] >> 7;
2244 cur_drv->perpendicular = (fdctrl->fifo[10] >> 2) & 0xF;
2245 fdctrl->config = fdctrl->fifo[11];
2246 fdctrl->precomp_trk = fdctrl->fifo[12];
2247 fdctrl->pwrd = fdctrl->fifo[13];
2248 fdctrl_reset_fifo(fdctrl);
2249 break;
2250 case 0x4D:
2251 /* FORMAT_TRACK */
2252 FLOPPY_DPRINTF("treat FORMAT_TRACK command\n");
2253 fdctrl->cur_drv = fdctrl->fifo[1] & 1;
2254 cur_drv = get_cur_drv(fdctrl);
2255 fdctrl->data_state |= FD_STATE_FORMAT;
2256 if (fdctrl->fifo[0] & 0x80)
2257 fdctrl->data_state |= FD_STATE_MULTI;
2258 else
2259 fdctrl->data_state &= ~FD_STATE_MULTI;
2260 fdctrl->data_state &= ~FD_STATE_SEEK;
2261 cur_drv->bps =
2262 fdctrl->fifo[2] > 7 ? 16384 : 128 << fdctrl->fifo[2];
2263#if 0
2264 cur_drv->last_sect =
2265 cur_drv->flags & FDISK_DBL_SIDES ? fdctrl->fifo[3] :
2266 fdctrl->fifo[3] / 2;
2267#else
2268 cur_drv->last_sect = fdctrl->fifo[3];
2269#endif
2270 /* Bochs BIOS is buggy and don't send format informations
2271 * for each sector. So, pretend all's done right now...
2272 */
2273 fdctrl->data_state &= ~FD_STATE_FORMAT;
2274 fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);
2275 break;
2276 case 0x8E:
2277 /* DRIVE_SPECIFICATION_COMMAND */
2278 FLOPPY_DPRINTF("treat DRIVE_SPECIFICATION_COMMAND command\n");
2279 if (fdctrl->fifo[fdctrl->data_pos - 1] & 0x80) {
2280 /* Command parameters done */
2281 if (fdctrl->fifo[fdctrl->data_pos - 1] & 0x40) {
2282 fdctrl->fifo[0] = fdctrl->fifo[1];
2283 fdctrl->fifo[2] = 0;
2284 fdctrl->fifo[3] = 0;
2285 fdctrl_set_fifo(fdctrl, 4, 1);
2286 } else {
2287 fdctrl_reset_fifo(fdctrl);
2288 }
2289 } else if (fdctrl->data_len > 7) {
2290 /* ERROR */
2291 fdctrl->fifo[0] = 0x80 |
2292 (cur_drv->head << 2) | fdctrl->cur_drv;
2293 fdctrl_set_fifo(fdctrl, 1, 1);
2294 }
2295 break;
2296 case 0x8F:
2297 /* RELATIVE_SEEK_OUT */
2298 FLOPPY_DPRINTF("treat RELATIVE_SEEK_OUT command\n");
2299 fdctrl->cur_drv = fdctrl->fifo[1] & 1;
2300 cur_drv = get_cur_drv(fdctrl);
2301 fd_start(cur_drv);
2302 cur_drv->dir = 0;
2303 if (fdctrl->fifo[2] + cur_drv->track >= cur_drv->max_track) {
2304 cur_drv->track = cur_drv->max_track - 1;
2305 } else {
2306 cur_drv->track += fdctrl->fifo[2];
2307 }
2308 fdctrl_reset_fifo(fdctrl);
2309 fdctrl_raise_irq(fdctrl, 0x20);
2310 break;
2311 case 0xCD:
2312 /* FORMAT_AND_WRITE */
2313/* // FLOPPY_DPRINTF("treat FORMAT_AND_WRITE command\n"); */
2314 FLOPPY_ERROR("treat FORMAT_AND_WRITE command\n");
2315 fdctrl_unimplemented(fdctrl);
2316 break;
2317 case 0xCF:
2318 /* RELATIVE_SEEK_IN */
2319 FLOPPY_DPRINTF("treat RELATIVE_SEEK_IN command\n");
2320 fdctrl->cur_drv = fdctrl->fifo[1] & 1;
2321 cur_drv = get_cur_drv(fdctrl);
2322 fd_start(cur_drv);
2323 cur_drv->dir = 1;
2324 if (fdctrl->fifo[2] > cur_drv->track) {
2325 cur_drv->track = 0;
2326 } else {
2327 cur_drv->track -= fdctrl->fifo[2];
2328 }
2329 fdctrl_reset_fifo(fdctrl);
2330 /* Raise Interrupt */
2331 fdctrl_raise_irq(fdctrl, 0x20);
2332 break;
2333 }
2334 }
2335}
2336
2337static void fdctrl_result_timer(void *opaque)
2338{
2339 fdctrl_t *fdctrl = opaque;
2340 fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);
2341}
2342
2343
2344#ifdef VBOX
2345static DECLCALLBACK(void) fdc_timer (PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
2346{
2347 fdctrl_t *fdctrl = (fdctrl_t *)pvUser;
2348 fdctrl_result_timer (fdctrl);
2349}
2350
2351static DECLCALLBACK(int) fdc_io_write (PPDMDEVINS pDevIns,
2352 void *pvUser,
2353 RTIOPORT Port,
2354 uint32_t u32,
2355 unsigned cb)
2356{
2357 if (cb == 1) {
2358 fdctrl_write (pvUser, Port, u32);
2359 }
2360 else {
2361 AssertMsgFailed(("Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
2362 }
2363 return VINF_SUCCESS;
2364}
2365
2366static DECLCALLBACK(int) fdc_io_read (PPDMDEVINS pDevIns,
2367 void *pvUser,
2368 RTIOPORT Port,
2369 uint32_t *pu32,
2370 unsigned cb)
2371{
2372 if (cb == 1) {
2373 *pu32 = fdctrl_read (pvUser, Port);
2374 return VINF_SUCCESS;
2375 }
2376 else {
2377 return VERR_IOM_IOPORT_UNUSED;
2378 }
2379}
2380
2381static DECLCALLBACK(int) SaveExec (PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle)
2382{
2383 fdctrl_t *s = PDMINS_2_DATA (pDevIns, fdctrl_t *);
2384 QEMUFile *f = pSSMHandle;
2385 unsigned int i;
2386
2387 qemu_put_8s (f, &s->version);
2388 qemu_put_8s (f, &s->irq_lvl);
2389 qemu_put_8s (f, &s->dma_chann);
2390 qemu_put_be32s (f, &s->io_base);
2391 qemu_put_8s (f, &s->state);
2392 qemu_put_8s (f, &s->dma_en);
2393 qemu_put_8s (f, &s->cur_drv);
2394 qemu_put_8s (f, &s->bootsel);
2395 qemu_put_buffer (f, s->fifo, FD_SECTOR_LEN);
2396 qemu_put_be32s (f, &s->data_pos);
2397 qemu_put_be32s (f, &s->data_len);
2398 qemu_put_8s (f, &s->data_state);
2399 qemu_put_8s (f, &s->data_dir);
2400 qemu_put_8s (f, &s->int_status);
2401 qemu_put_8s (f, &s->eot);
2402 qemu_put_8s (f, &s->timer0);
2403 qemu_put_8s (f, &s->timer1);
2404 qemu_put_8s (f, &s->precomp_trk);
2405 qemu_put_8s (f, &s->config);
2406 qemu_put_8s (f, &s->lock);
2407 qemu_put_8s (f, &s->pwrd);
2408
2409 for (i = 0; i < sizeof (s->drives) / sizeof (s->drives[0]); ++i) {
2410 fdrive_t *d = &s->drives[i];
2411
2412 SSMR3PutMem (pSSMHandle, &d->Led, sizeof (d->Led));
2413 qemu_put_be32s (f, &d->drive);
2414 qemu_put_be32s (f, &d->drflags);
2415 qemu_put_8s (f, &d->perpendicular);
2416 qemu_put_8s (f, &d->head);
2417 qemu_put_8s (f, &d->track);
2418 qemu_put_8s (f, &d->sect);
2419 qemu_put_8s (f, &d->dir);
2420 qemu_put_8s (f, &d->rw);
2421 qemu_put_be32s (f, &d->flags);
2422 qemu_put_8s (f, &d->last_sect);
2423 qemu_put_8s (f, &d->max_track);
2424 qemu_put_be16s (f, &d->bps);
2425 qemu_put_8s (f, &d->ro);
2426 }
2427 return TMR3TimerSave (s->result_timer, pSSMHandle);
2428}
2429
2430static DECLCALLBACK(int) LoadExec (PPDMDEVINS pDevIns,
2431 PSSMHANDLE pSSMHandle,
2432 uint32_t u32Version)
2433{
2434 fdctrl_t *s = PDMINS_2_DATA (pDevIns, fdctrl_t *);
2435 QEMUFile *f = pSSMHandle;
2436 unsigned int i;
2437
2438 if (u32Version != 1) {
2439 AssertMsgFailed(("u32Version=%d\n", u32Version));
2440 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
2441 }
2442
2443 qemu_get_8s (f, &s->version);
2444 qemu_get_8s (f, &s->irq_lvl);
2445 qemu_get_8s (f, &s->dma_chann);
2446 qemu_get_be32s (f, &s->io_base);
2447 qemu_get_8s (f, &s->state);
2448 qemu_get_8s (f, &s->dma_en);
2449 qemu_get_8s (f, &s->cur_drv);
2450 qemu_get_8s (f, &s->bootsel);
2451 qemu_get_buffer (f, s->fifo, FD_SECTOR_LEN);
2452 qemu_get_be32s (f, &s->data_pos);
2453 qemu_get_be32s (f, &s->data_len);
2454 qemu_get_8s (f, &s->data_state);
2455 qemu_get_8s (f, &s->data_dir);
2456 qemu_get_8s (f, &s->int_status);
2457 qemu_get_8s (f, &s->eot);
2458 qemu_get_8s (f, &s->timer0);
2459 qemu_get_8s (f, &s->timer1);
2460 qemu_get_8s (f, &s->precomp_trk);
2461 qemu_get_8s (f, &s->config);
2462 qemu_get_8s (f, &s->lock);
2463 qemu_get_8s (f, &s->pwrd);
2464
2465 for (i = 0; i < sizeof (s->drives) / sizeof (s->drives[0]); ++i) {
2466 fdrive_t *d = &s->drives[i];
2467
2468 SSMR3GetMem (pSSMHandle, &d->Led, sizeof (d->Led));
2469 qemu_get_be32s (f, &d->drive);
2470 qemu_get_be32s (f, &d->drflags);
2471 qemu_get_8s (f, &d->perpendicular);
2472 qemu_get_8s (f, &d->head);
2473 qemu_get_8s (f, &d->track);
2474 qemu_get_8s (f, &d->sect);
2475 qemu_get_8s (f, &d->dir);
2476 qemu_get_8s (f, &d->rw);
2477 qemu_get_be32s (f, &d->flags);
2478 qemu_get_8s (f, &d->last_sect);
2479 qemu_get_8s (f, &d->max_track);
2480 qemu_get_be16s (f, &d->bps);
2481 qemu_get_8s (f, &d->ro);
2482 }
2483 return TMR3TimerLoad (s->result_timer, pSSMHandle);
2484}
2485
2486/**
2487 * Queries an interface to the driver.
2488 *
2489 * @returns Pointer to interface.
2490 * @returns NULL if the interface was not supported by the device.
2491 * @param pInterface Pointer to IDEState::IBase.
2492 * @param enmInterface The requested interface identification.
2493 */
2494static DECLCALLBACK(void *) fdQueryInterface (PPDMIBASE pInterface,
2495 PDMINTERFACE enmInterface)
2496{
2497 fdrive_t *drv = PDMIBASE_2_FDRIVE(pInterface);
2498 switch (enmInterface) {
2499 case PDMINTERFACE_BASE:
2500 return &drv->IBase;
2501 case PDMINTERFACE_BLOCK_PORT:
2502 return &drv->IPort;
2503 case PDMINTERFACE_MOUNT_NOTIFY:
2504 return &drv->IMountNotify;
2505 default:
2506 return NULL;
2507 }
2508}
2509
2510/**
2511 * Gets the pointer to the status LED of a unit.
2512 *
2513 * @returns VBox status code.
2514 * @param pInterface Pointer to the interface structure containing the called function pointer.
2515 * @param iLUN The unit which status LED we desire.
2516 * @param ppLed Where to store the LED pointer.
2517 */
2518static DECLCALLBACK(int) fdcStatusQueryStatusLed (PPDMILEDPORTS pInterface,
2519 unsigned iLUN,
2520 PPDMLED *ppLed)
2521{
2522 fdctrl_t *fdctrl = (fdctrl_t *)
2523 ((uintptr_t )pInterface - RT_OFFSETOF (fdctrl_t, ILeds));
2524 if (iLUN < RT_ELEMENTS(fdctrl->drives)) {
2525 *ppLed = &fdctrl->drives[iLUN].Led;
2526 Assert ((*ppLed)->u32Magic == PDMLED_MAGIC);
2527 return VINF_SUCCESS;
2528 }
2529 return VERR_PDM_LUN_NOT_FOUND;
2530}
2531
2532
2533/**
2534 * Queries an interface to the status LUN.
2535 *
2536 * @returns Pointer to interface.
2537 * @returns NULL if the interface was not supported by the device.
2538 * @param pInterface Pointer to IDEState::IBase.
2539 * @param enmInterface The requested interface identification.
2540 */
2541static DECLCALLBACK(void *) fdcStatusQueryInterface (PPDMIBASE pInterface,
2542 PDMINTERFACE enmInterface)
2543{
2544 fdctrl_t *fdctrl = (fdctrl_t *)
2545 ((uintptr_t)pInterface - RT_OFFSETOF (fdctrl_t, IBaseStatus));
2546 switch (enmInterface) {
2547 case PDMINTERFACE_BASE:
2548 return &fdctrl->IBaseStatus;
2549 case PDMINTERFACE_LED_PORTS:
2550 return &fdctrl->ILeds;
2551 default:
2552 return NULL;
2553 }
2554}
2555
2556
2557/**
2558 * Configure a drive.
2559 *
2560 * @returns VBox status code.
2561 * @param drv The drive in question.
2562 * @param pDevIns The driver instance.
2563 */
2564static int fdConfig (fdrive_t *drv, PPDMDEVINS pDevIns)
2565{
2566 static const char *descs[] = {"Floppy Drive A:", "Floppy Drive B"};
2567 int rc;
2568
2569 /*
2570 * Reset the LED just to be on the safe side.
2571 */
2572 Assert (RT_ELEMENTS(descs) > drv->iLUN);
2573 Assert (drv->Led.u32Magic == PDMLED_MAGIC);
2574 drv->Led.Actual.u32 = 0;
2575 drv->Led.Asserted.u32 = 0;
2576
2577 /*
2578 * Try attach the block device and get the interfaces.
2579 */
2580 rc = PDMDevHlpDriverAttach (pDevIns, drv->iLUN, &drv->IBase, &drv->pDrvBase, descs[drv->iLUN]);
2581 if (RT_SUCCESS (rc)) {
2582 drv->pDrvBlock = drv->pDrvBase->pfnQueryInterface (
2583 drv->pDrvBase,
2584 PDMINTERFACE_BLOCK
2585 );
2586 if (drv->pDrvBlock) {
2587 drv->pDrvBlockBios = drv->pDrvBase->pfnQueryInterface (
2588 drv->pDrvBase,
2589 PDMINTERFACE_BLOCK_BIOS
2590 );
2591 if (drv->pDrvBlockBios) {
2592 drv->pDrvMount = drv->pDrvBase->pfnQueryInterface (
2593 drv->pDrvBase,
2594 PDMINTERFACE_MOUNT
2595 );
2596 if (drv->pDrvMount) {
2597 /*
2598 * Init the state.
2599 */
2600 drv->drive = FDRIVE_DRV_NONE;
2601 drv->drflags = 0;
2602 drv->perpendicular = 0;
2603 /* Disk */
2604 drv->last_sect = 0;
2605 drv->max_track = 0;
2606 drv->fMediaPresent = false;
2607 } else {
2608 AssertMsgFailed (("Configuration error: LUN#%d without mountable interface!\n", drv->iLUN));
2609 rc = VERR_PDM_MISSING_INTERFACE;
2610 }
2611
2612 } else {
2613 AssertMsgFailed (("Configuration error: LUN#%d hasn't a block BIOS interface!\n", drv->iLUN));
2614 rc = VERR_PDM_MISSING_INTERFACE;
2615 }
2616
2617 } else {
2618 AssertMsgFailed (("Configuration error: LUN#%d hasn't a block interface!\n", drv->iLUN));
2619 rc = VERR_PDM_MISSING_INTERFACE;
2620 }
2621 } else {
2622 AssertMsg (rc == VERR_PDM_NO_ATTACHED_DRIVER,
2623 ("Failed to attach LUN#%d. rc=%Rrc\n", drv->iLUN, rc));
2624 switch (rc) {
2625 case VERR_ACCESS_DENIED:
2626 /* Error already catched by DrvHostBase */
2627 break;
2628 case VERR_PDM_NO_ATTACHED_DRIVER:
2629 /* Legal on architectures without a floppy controller */
2630 break;
2631 default:
2632 rc = PDMDevHlpVMSetError (pDevIns, rc, RT_SRC_POS,
2633 N_ ("The floppy controller cannot attach to the floppy drive"));
2634 break;
2635 }
2636 }
2637
2638 if (RT_FAILURE (rc)) {
2639 drv->pDrvBase = NULL;
2640 drv->pDrvBlock = NULL;
2641 drv->pDrvBlockBios = NULL;
2642 drv->pDrvMount = NULL;
2643 }
2644 LogFlow (("fdConfig: returns %Rrc\n", rc));
2645 return rc;
2646}
2647
2648
2649/**
2650 * Attach command.
2651 *
2652 * This is called when we change block driver for a floppy drive.
2653 *
2654 * @returns VBox status code.
2655 * @param pDevIns The device instance.
2656 * @param iLUN The logical unit which is being detached.
2657 */
2658static DECLCALLBACK(int) fdcAttach (PPDMDEVINS pDevIns,
2659 unsigned iLUN)
2660{
2661 fdctrl_t *fdctrl = PDMINS_2_DATA (pDevIns, fdctrl_t *);
2662 fdrive_t *drv;
2663 int rc;
2664 LogFlow (("ideDetach: iLUN=%u\n", iLUN));
2665
2666 /*
2667 * Validate.
2668 */
2669 if (iLUN > 2) {
2670 AssertMsgFailed (("Configuration error: cannot attach or detach any but the first two LUNs - iLUN=%u\n",
2671 iLUN));
2672 return VERR_PDM_DEVINS_NO_ATTACH;
2673 }
2674
2675 /*
2676 * Locate the drive and stuff.
2677 */
2678 drv = &fdctrl->drives[iLUN];
2679
2680 /* the usual paranoia */
2681 AssertRelease (!drv->pDrvBase);
2682 AssertRelease (!drv->pDrvBlock);
2683 AssertRelease (!drv->pDrvBlockBios);
2684 AssertRelease (!drv->pDrvMount);
2685
2686 rc = fdConfig (drv, pDevIns);
2687 AssertMsg (rc != VERR_PDM_NO_ATTACHED_DRIVER,
2688 ("Configuration error: failed to configure drive %d, rc=%Rrc\n", rc));
2689 if (RT_SUCCESS(rc)) {
2690 fd_revalidate (drv);
2691 }
2692
2693 LogFlow (("floppyAttach: returns %Rrc\n", rc));
2694 return rc;
2695}
2696
2697
2698/**
2699 * Detach notification.
2700 *
2701 * The floppy drive has been temporarily 'unplugged'.
2702 *
2703 * @param pDevIns The device instance.
2704 * @param iLUN The logical unit which is being detached.
2705 */
2706static DECLCALLBACK(void) fdcDetach (PPDMDEVINS pDevIns,
2707 unsigned iLUN)
2708{
2709 fdctrl_t *fdctrl = PDMINS_2_DATA (pDevIns, fdctrl_t *);
2710 LogFlow (("ideDetach: iLUN=%u\n", iLUN));
2711
2712 switch (iLUN) {
2713 case 0:
2714 case 1: {
2715 fdrive_t *drv = &fdctrl->drives[iLUN];
2716 drv->pDrvBase = NULL;
2717 drv->pDrvBlock = NULL;
2718 drv->pDrvBlockBios = NULL;
2719 drv->pDrvMount = NULL;
2720 break;
2721 }
2722
2723 default:
2724 AssertMsgFailed (("Cannot detach LUN#%d!\n", iLUN));
2725 break;
2726 }
2727}
2728
2729
2730/**
2731 * Handle reset.
2732 *
2733 * I haven't check the specs on what's supposed to happen on reset, but we
2734 * should get any 'FATAL: floppy recal:f07 ctrl not ready' when resetting
2735 * at wrong time like we do if this was all void.
2736 *
2737 * @param pDevIns The device instance.
2738 */
2739static DECLCALLBACK(void) fdcReset (PPDMDEVINS pDevIns)
2740{
2741 fdctrl_t *fdctrl = PDMINS_2_DATA (pDevIns, fdctrl_t *);
2742 unsigned i;
2743 LogFlow (("fdcReset:\n"));
2744
2745 fdctrl_reset(fdctrl, 0);
2746 fdctrl->state = FD_CTRL_ACTIVE;
2747
2748 for (i = 0; i < RT_ELEMENTS(fdctrl->drives); i++) {
2749 fd_revalidate(&fdctrl->drives[i]);
2750 }
2751}
2752
2753
2754/**
2755 * Construct a device instance for a VM.
2756 *
2757 * @returns VBox status.
2758 * @param pDevIns The device instance data.
2759 * If the registration structure is needed, pDevIns->pDevReg points to it.
2760 * @param iInstance Instance number. Use this to figure out which registers and such to use.
2761 * The device number is also found in pDevIns->iInstance, but since it's
2762 * likely to be freqently used PDM passes it as parameter.
2763 * @param pCfgHandle Configuration node handle for the device. Use this to obtain the configuration
2764 * of the device instance. It's also found in pDevIns->pCfgHandle, but like
2765 * iInstance it's expected to be used a bit in this function.
2766 */
2767static DECLCALLBACK(int) fdcConstruct (PPDMDEVINS pDevIns,
2768 int iInstance,
2769 PCFGMNODE pCfgHandle)
2770{
2771 int rc;
2772 fdctrl_t *fdctrl = PDMINS_2_DATA(pDevIns, fdctrl_t*);
2773 unsigned i;
2774 bool mem_mapped;
2775 uint16_t io_base;
2776 uint8_t irq_lvl, dma_chann;
2777 PPDMIBASE pBase;
2778
2779 Assert(iInstance == 0);
2780
2781 /*
2782 * Validate configuration.
2783 */
2784 if (!CFGMR3AreValuesValid(pCfgHandle, "IRQ\0DMA\0MemMapped\0IOBase\0"))
2785 return VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES;
2786
2787 /*
2788 * Read the configuration.
2789 */
2790 rc = CFGMR3QueryU8 (pCfgHandle, "IRQ", &irq_lvl);
2791 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
2792 irq_lvl = 6;
2793 else if (RT_FAILURE (rc)) {
2794 AssertMsgFailed (("Configuration error: Failed to read U8 IRQ, rc=%Rrc\n", rc));
2795 return rc;
2796 }
2797
2798 rc = CFGMR3QueryU8 (pCfgHandle, "DMA", &dma_chann);
2799 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
2800 dma_chann = 2;
2801 else if (RT_FAILURE (rc)) {
2802 AssertMsgFailed (("Configuration error: Failed to read U8 DMA, rc=%Rrc\n", rc));
2803 return rc;
2804 }
2805
2806 rc = CFGMR3QueryU16 (pCfgHandle, "IOBase", &io_base);
2807 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
2808 io_base = 0x3f0;
2809 else if (RT_FAILURE (rc)) {
2810 AssertMsgFailed (("Configuration error: Failed to read U16 IOBase, rc=%Rrc\n", rc));
2811 return rc;
2812 }
2813
2814 rc = CFGMR3QueryBool (pCfgHandle, "MemMapped", &mem_mapped);
2815 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
2816 mem_mapped = false;
2817 else if (RT_FAILURE (rc)) {
2818 AssertMsgFailed (("Configuration error: Failed to read bool value MemMapped rc=%Rrc\n", rc));
2819 return rc;
2820 }
2821
2822 /*
2823 * Initialize data.
2824 */
2825 LogFlow(("fdcConstruct: irq_lvl=%d dma_chann=%d io_base=%#x\n", irq_lvl, dma_chann, io_base));
2826 fdctrl->pDevIns = pDevIns;
2827 fdctrl->version = 0x90; /* Intel 82078 controller */
2828 fdctrl->irq_lvl = irq_lvl;
2829 fdctrl->dma_chann = dma_chann;
2830 fdctrl->io_base = io_base;
2831 fdctrl->config = 0x60; /* Implicit seek, polling & FIFO enabled */
2832
2833 fdctrl->IBaseStatus.pfnQueryInterface = fdcStatusQueryInterface;
2834 fdctrl->ILeds.pfnQueryStatusLed = fdcStatusQueryStatusLed;
2835
2836 for (i = 0; i < RT_ELEMENTS(fdctrl->drives); ++i) {
2837 fdrive_t *drv = &fdctrl->drives[i];
2838
2839 drv->drive = FDRIVE_DRV_NONE;
2840 drv->iLUN = i;
2841
2842 drv->IBase.pfnQueryInterface = fdQueryInterface;
2843 drv->IMountNotify.pfnMountNotify = fdMountNotify;
2844 drv->IMountNotify.pfnUnmountNotify = fdUnmountNotify;
2845 drv->Led.u32Magic = PDMLED_MAGIC;
2846 }
2847
2848 /*
2849 * Create the FDC timer.
2850 */
2851 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, fdc_timer, fdctrl,
2852 TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "FDC Timer", &fdctrl->result_timer);
2853 if (RT_FAILURE (rc))
2854 return rc;
2855
2856 /*
2857 * Register DMA channel.
2858 */
2859 if (fdctrl->dma_chann != 0xff) {
2860 fdctrl->dma_en = 1;
2861 rc = PDMDevHlpDMARegister (pDevIns, dma_chann, &fdctrl_transfer_handler, fdctrl);
2862 if (RT_FAILURE (rc))
2863 return rc;
2864 } else
2865 fdctrl->dma_en = 0;
2866
2867 /*
2868 * IO / MMIO.
2869 */
2870 if (mem_mapped) {
2871 AssertMsgFailed (("Memory mapped floppy not support by now\n"));
2872 return VERR_NOT_SUPPORTED;
2873#if 0
2874 FLOPPY_ERROR("memory mapped floppy not supported by now !\n");
2875 io_mem = cpu_register_io_memory(0, fdctrl_mem_read, fdctrl_mem_write);
2876 cpu_register_physical_memory(base, 0x08, io_mem);
2877#endif
2878 } else {
2879 rc = PDMDevHlpIOPortRegister (pDevIns, io_base + 0x1, 5, fdctrl,
2880 fdc_io_write, fdc_io_read, NULL, NULL, "FDC#1");
2881 if (RT_FAILURE (rc))
2882 return rc;
2883
2884 rc = PDMDevHlpIOPortRegister (pDevIns, io_base + 0x7, 1, fdctrl,
2885 fdc_io_write, fdc_io_read, NULL, NULL, "FDC#2");
2886 if (RT_FAILURE (rc))
2887 return rc;
2888 }
2889
2890 /*
2891 * Register the saved state data unit.
2892 */
2893 rc = PDMDevHlpSSMRegister (pDevIns, pDevIns->pDevReg->szDeviceName, iInstance, 1, sizeof(*fdctrl),
2894 NULL, SaveExec, NULL, NULL, LoadExec, NULL);
2895 if (RT_FAILURE(rc))
2896 return rc;
2897
2898 /*
2899 * Attach the status port (optional).
2900 */
2901 rc = PDMDevHlpDriverAttach (pDevIns, PDM_STATUS_LUN, &fdctrl->IBaseStatus, &pBase, "Status Port");
2902 if (RT_SUCCESS (rc)) {
2903 fdctrl->pLedsConnector =
2904 pBase->pfnQueryInterface (pBase, PDMINTERFACE_LED_CONNECTORS);
2905 } else if (rc != VERR_PDM_NO_ATTACHED_DRIVER) {
2906 AssertMsgFailed (("Failed to attach to status driver. rc=%Rrc\n",
2907 rc));
2908 return rc;
2909 }
2910
2911 /*
2912 * Initialize drives.
2913 */
2914 for (i = 0; i < RT_ELEMENTS(fdctrl->drives); i++) {
2915 fdrive_t *drv = &fdctrl->drives[i];
2916 rc = fdConfig (drv, pDevIns);
2917 if ( RT_FAILURE (rc)
2918 && rc != VERR_PDM_NO_ATTACHED_DRIVER) {
2919 AssertMsgFailed (("Configuration error: failed to configure drive %d, rc=%Rrc\n", rc));
2920 return rc;
2921 }
2922 }
2923
2924 fdctrl_reset(fdctrl, 0);
2925 fdctrl->state = FD_CTRL_ACTIVE;
2926
2927 for (i = 0; i < RT_ELEMENTS(fdctrl->drives); i++)
2928 fd_revalidate(&fdctrl->drives[i]);
2929
2930 return VINF_SUCCESS;
2931}
2932
2933/**
2934 * The device registration structure.
2935 */
2936const PDMDEVREG g_DeviceFloppyController =
2937{
2938 /* u32Version */
2939 PDM_DEVREG_VERSION,
2940 /* szDeviceName */
2941 "i82078",
2942 /* szRCMod */
2943 "",
2944 /* szR0Mod */
2945 "",
2946 /* pszDescription */
2947 "Floppy drive controller (Intel 82078)",
2948 /* fFlags */
2949 PDM_DEVREG_FLAGS_DEFAULT_BITS,
2950 /* fClass */
2951 PDM_DEVREG_CLASS_STORAGE,
2952 /* cMaxInstances */
2953 1,
2954 /* cbInstance */
2955 sizeof(fdctrl_t),
2956 /* pfnConstruct */
2957 fdcConstruct,
2958 /* pfnDestruct */
2959 NULL,
2960 /* pfnRelocate */
2961 NULL,
2962 /* pfnIOCtl */
2963 NULL,
2964 /* pfnPowerOn */
2965 NULL,
2966 /* pfnReset */
2967 fdcReset,
2968 /* pfnSuspend */
2969 NULL,
2970 /* pfnResume */
2971 NULL,
2972 /* pfnAttach */
2973 fdcAttach,
2974 /* pfnDetach */
2975 fdcDetach,
2976 /* pfnQueryInterface. */
2977 NULL,
2978 /* pfnInitComplete */
2979 NULL,
2980 /* pfnPowerOff */
2981 NULL,
2982 /* pfnSoftReset */
2983 NULL,
2984 /* u32VersionEnd */
2985 PDM_DEVREG_VERSION
2986};
2987
2988#endif /* VBOX */
2989
2990/*
2991 * Local Variables:
2992 * mode: c
2993 * c-file-style: "k&r"
2994 * indent-tabs-mode: nil
2995 * End:
2996 */
2997
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette