VirtualBox

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

Last change on this file since 65620 was 64846, checked in by vboxsync, 8 years ago

Devices/Storage/DevFdc: Add missing pfnQueryDeviceLocation, fixes crash after recent changes in the medium driver

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