VirtualBox

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

Last change on this file since 90778 was 87773, checked in by vboxsync, 3 years ago

VMM/TM,Devices: Store the timer name in the TMTIMER structure and limit it to 31 characters. Shortened most timer names. bugref:9943

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

© 2023 Oracle
ContactPrivacy policyTerms of Use