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