VirtualBox

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

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

Devices: Log & Assertion formatting fixes.

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