VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/fdc.c@ 35753

Last change on this file since 35753 was 35353, checked in by vboxsync, 14 years ago

Move the misc files the in src/VBox/Devices/ directory into a build/ subdirectory, changing their names to match the target module.

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