VirtualBox

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

Last change on this file since 1 was 1, checked in by vboxsync, 55 years ago

import

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