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