VirtualBox

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

最後變更 在這個檔案從47841是 47711,由 vboxsync 提交於 12 年 前

DevFdc: Improve error reporting.

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

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