VirtualBox

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

Last change on this file since 27805 was 27797, checked in by vboxsync, 15 years ago

misc compiler warning fixes, comment typos and other minor cleanups

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