VirtualBox

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

Last change on this file since 59248 was 59248, checked in by vboxsync, 9 years ago

Storage: Get rid of the block driver and merge the the little extra functionality it had into the VD driver. Enables us to get rid of PDMIBLOCK which is basically a subset of PDMIMEDIA and makes changes to the latter interface tedious because it had to be replicated in the former. (bugref:4114)

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