VirtualBox

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

Last change on this file since 47530 was 47530, checked in by vboxsync, 11 years ago

FDC: Change line is never set if no drive is selected.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette