VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/DevFdc.cpp@ 46155

Last change on this file since 46155 was 45302, checked in by vboxsync, 12 years ago

warning

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