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