VirtualBox

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

Last change on this file since 78374 was 78040, checked in by vboxsync, 6 years ago

DevFDC: Eliminated irrelevant ifdefs.

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

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