VirtualBox

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

Last change on this file since 64209 was 63562, checked in by vboxsync, 8 years ago

scm: cleaning up todos

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