root / hw / fdc.c @ dff38e7b
History | View | Annotate | Download (56.3 kB)
1 |
/*
|
---|---|
2 |
* QEMU Floppy disk emulator
|
3 |
*
|
4 |
* Copyright (c) 2003 Jocelyn Mayer
|
5 |
*
|
6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 |
* of this software and associated documentation files (the "Software"), to deal
|
8 |
* in the Software without restriction, including without limitation the rights
|
9 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 |
* copies of the Software, and to permit persons to whom the Software is
|
11 |
* furnished to do so, subject to the following conditions:
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be included in
|
14 |
* all copies or substantial portions of the Software.
|
15 |
*
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 |
* THE SOFTWARE.
|
23 |
*/
|
24 |
#include <stdio.h> |
25 |
#include <stdlib.h> |
26 |
#include <string.h> |
27 |
#include <inttypes.h> |
28 |
|
29 |
#include "vl.h" |
30 |
|
31 |
/********************************************************/
|
32 |
/* debug Floppy devices */
|
33 |
//#define DEBUG_FLOPPY
|
34 |
|
35 |
#ifdef DEBUG_FLOPPY
|
36 |
#define FLOPPY_DPRINTF(fmt, args...) \
|
37 |
do { printf("FLOPPY: " fmt , ##args); } while (0) |
38 |
#else
|
39 |
#define FLOPPY_DPRINTF(fmt, args...)
|
40 |
#endif
|
41 |
|
42 |
#define FLOPPY_ERROR(fmt, args...) \
|
43 |
do { printf("FLOPPY ERROR: %s: " fmt, __func__ , ##args); } while (0) |
44 |
|
45 |
/********************************************************/
|
46 |
/* Floppy drive emulation */
|
47 |
|
48 |
/* Will always be a fixed parameter for us */
|
49 |
#define FD_SECTOR_LEN 512 |
50 |
#define FD_SECTOR_SC 2 /* Sector size code */ |
51 |
|
52 |
/* Floppy disk drive emulation */
|
53 |
typedef enum fdisk_type_t { |
54 |
FDRIVE_DISK_288 = 0x01, /* 2.88 MB disk */ |
55 |
FDRIVE_DISK_144 = 0x02, /* 1.44 MB disk */ |
56 |
FDRIVE_DISK_720 = 0x03, /* 720 kB disk */ |
57 |
FDRIVE_DISK_USER = 0x04, /* User defined geometry */ |
58 |
FDRIVE_DISK_NONE = 0x05, /* No disk */ |
59 |
} fdisk_type_t; |
60 |
|
61 |
typedef enum fdrive_type_t { |
62 |
FDRIVE_DRV_144 = 0x00, /* 1.44 MB 3"5 drive */ |
63 |
FDRIVE_DRV_288 = 0x01, /* 2.88 MB 3"5 drive */ |
64 |
FDRIVE_DRV_120 = 0x02, /* 1.2 MB 5"25 drive */ |
65 |
FDRIVE_DRV_NONE = 0x03, /* No drive connected */ |
66 |
} fdrive_type_t; |
67 |
|
68 |
typedef enum fdrive_flags_t { |
69 |
FDRIVE_MOTOR_ON = 0x01, /* motor on/off */ |
70 |
FDRIVE_REVALIDATE = 0x02, /* Revalidated */ |
71 |
} fdrive_flags_t; |
72 |
|
73 |
typedef enum fdisk_flags_t { |
74 |
FDISK_DBL_SIDES = 0x01,
|
75 |
} fdisk_flags_t; |
76 |
|
77 |
typedef struct fdrive_t { |
78 |
BlockDriverState *bs; |
79 |
/* Drive status */
|
80 |
fdrive_type_t drive; |
81 |
fdrive_flags_t drflags; |
82 |
uint8_t perpendicular; /* 2.88 MB access mode */
|
83 |
/* Position */
|
84 |
uint8_t head; |
85 |
uint8_t track; |
86 |
uint8_t sect; |
87 |
/* Last operation status */
|
88 |
uint8_t dir; /* Direction */
|
89 |
uint8_t rw; /* Read/write */
|
90 |
/* Media */
|
91 |
fdisk_type_t disk; /* Disk type */
|
92 |
fdisk_flags_t flags; |
93 |
uint8_t last_sect; /* Nb sector per track */
|
94 |
uint8_t max_track; /* Nb of tracks */
|
95 |
uint16_t bps; /* Bytes per sector */
|
96 |
uint8_t ro; /* Is read-only */
|
97 |
} fdrive_t; |
98 |
|
99 |
static void fd_init (fdrive_t *drv, BlockDriverState *bs) |
100 |
{ |
101 |
/* Drive */
|
102 |
drv->bs = bs; |
103 |
if (bs)
|
104 |
drv->drive = FDRIVE_DRV_144; |
105 |
else
|
106 |
drv->drive = FDRIVE_DRV_NONE; |
107 |
drv->drflags = 0;
|
108 |
drv->perpendicular = 0;
|
109 |
/* Disk */
|
110 |
drv->disk = FDRIVE_DISK_NONE; |
111 |
drv->last_sect = 0;
|
112 |
drv->max_track = 0;
|
113 |
} |
114 |
|
115 |
static int _fd_sector (uint8_t head, uint8_t track, |
116 |
uint8_t sect, uint8_t last_sect) |
117 |
{ |
118 |
return (((track * 2) + head) * last_sect) + sect - 1; |
119 |
} |
120 |
|
121 |
/* Returns current position, in sectors, for given drive */
|
122 |
static int fd_sector (fdrive_t *drv) |
123 |
{ |
124 |
return _fd_sector(drv->head, drv->track, drv->sect, drv->last_sect);
|
125 |
} |
126 |
|
127 |
static int fd_seek (fdrive_t *drv, uint8_t head, uint8_t track, uint8_t sect, |
128 |
int enable_seek)
|
129 |
{ |
130 |
uint32_t sector; |
131 |
int ret;
|
132 |
|
133 |
if (track > drv->max_track ||
|
134 |
(head != 0 && (drv->flags & FDISK_DBL_SIDES) == 0)) { |
135 |
FLOPPY_ERROR("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
|
136 |
head, track, sect, 1,
|
137 |
(drv->flags & FDISK_DBL_SIDES) == 0 ? 0 : 1, |
138 |
drv->max_track, drv->last_sect); |
139 |
return 2; |
140 |
} |
141 |
if (sect > drv->last_sect) {
|
142 |
FLOPPY_ERROR("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
|
143 |
head, track, sect, 1,
|
144 |
(drv->flags & FDISK_DBL_SIDES) == 0 ? 0 : 1, |
145 |
drv->max_track, drv->last_sect); |
146 |
return 3; |
147 |
} |
148 |
sector = _fd_sector(head, track, sect, drv->last_sect); |
149 |
ret = 0;
|
150 |
if (sector != fd_sector(drv)) {
|
151 |
#if 0
|
152 |
if (!enable_seek) {
|
153 |
FLOPPY_ERROR("no implicit seek %d %02x %02x (max=%d %02x %02x)\n",
|
154 |
head, track, sect, 1, drv->max_track, drv->last_sect);
|
155 |
return 4;
|
156 |
}
|
157 |
#endif
|
158 |
drv->head = head; |
159 |
if (drv->track != track)
|
160 |
ret = 1;
|
161 |
drv->track = track; |
162 |
drv->sect = sect; |
163 |
} |
164 |
|
165 |
return ret;
|
166 |
} |
167 |
|
168 |
/* Set drive back to track 0 */
|
169 |
static void fd_recalibrate (fdrive_t *drv) |
170 |
{ |
171 |
FLOPPY_DPRINTF("recalibrate\n");
|
172 |
drv->head = 0;
|
173 |
drv->track = 0;
|
174 |
drv->sect = 1;
|
175 |
drv->dir = 1;
|
176 |
drv->rw = 0;
|
177 |
} |
178 |
|
179 |
/* Revalidate a disk drive after a disk change */
|
180 |
static void fd_revalidate (fdrive_t *drv) |
181 |
{ |
182 |
int64_t nb_sectors; |
183 |
int nb_heads, max_track, last_sect, ro;
|
184 |
|
185 |
FLOPPY_DPRINTF("revalidate\n");
|
186 |
drv->drflags &= ~FDRIVE_REVALIDATE; |
187 |
|
188 |
/* if no drive present, cannot do more */
|
189 |
if (!drv->bs)
|
190 |
return;
|
191 |
|
192 |
if (bdrv_is_inserted(drv->bs)) {
|
193 |
ro = bdrv_is_read_only(drv->bs); |
194 |
bdrv_get_geometry_hint(drv->bs, &max_track, &nb_heads, &last_sect); |
195 |
if (nb_heads != 0 && max_track != 0 && last_sect != 0) { |
196 |
drv->disk = FDRIVE_DISK_USER; |
197 |
printf("User defined disk (%d %d %d)",
|
198 |
nb_heads - 1, max_track, last_sect);
|
199 |
if (nb_heads == 1) { |
200 |
drv->flags &= ~FDISK_DBL_SIDES; |
201 |
} else {
|
202 |
drv->flags |= FDISK_DBL_SIDES; |
203 |
} |
204 |
drv->max_track = max_track; |
205 |
drv->last_sect = last_sect; |
206 |
} else {
|
207 |
bdrv_get_geometry(drv->bs, &nb_sectors); |
208 |
switch (nb_sectors) {
|
209 |
/* 2.88 MB 3"1/2 drive disks */
|
210 |
case 7680: |
211 |
printf("3.84 Mb 3\"1/2 disk (1 80 48)");
|
212 |
drv->drive = FDRIVE_DRV_288; |
213 |
drv->disk = FDRIVE_DISK_288; |
214 |
drv->last_sect = 48;
|
215 |
drv->max_track = 80;
|
216 |
drv->flags |= FDISK_DBL_SIDES; |
217 |
break;
|
218 |
case 7040: |
219 |
printf("3.52 Mb 3\"1/2 disk (1 80 44)");
|
220 |
drv->drive = FDRIVE_DRV_288; |
221 |
drv->disk = FDRIVE_DISK_288; |
222 |
drv->last_sect = 44;
|
223 |
drv->max_track = 80;
|
224 |
drv->flags |= FDISK_DBL_SIDES; |
225 |
break;
|
226 |
case 6400: |
227 |
printf("3.2 Mb 3\"1/2 disk (1 80 40)");
|
228 |
drv->drive = FDRIVE_DRV_288; |
229 |
drv->disk = FDRIVE_DISK_288; |
230 |
drv->last_sect = 40;
|
231 |
drv->max_track = 80;
|
232 |
drv->flags |= FDISK_DBL_SIDES; |
233 |
break;
|
234 |
case 6240: |
235 |
printf("3.12 Mb 3\"1/2 disk (1 80 39)");
|
236 |
drv->drive = FDRIVE_DRV_288; |
237 |
drv->disk = FDRIVE_DISK_288; |
238 |
drv->last_sect = 39;
|
239 |
drv->max_track = 80;
|
240 |
drv->flags |= FDISK_DBL_SIDES; |
241 |
break;
|
242 |
case 5760: |
243 |
printf("2.88 Mb 3\"1/2 disk (1 80 36)");
|
244 |
drv->drive = FDRIVE_DRV_288; |
245 |
drv->disk = FDRIVE_DISK_288; |
246 |
drv->last_sect = 36;
|
247 |
drv->max_track = 80;
|
248 |
drv->flags |= FDISK_DBL_SIDES; |
249 |
break;
|
250 |
|
251 |
/* 1.44 MB 3"1/2 drive disks */
|
252 |
case 3840: |
253 |
printf("1.92 Mb 3\"1/2 disk (1 80 24)");
|
254 |
drv->drive = FDRIVE_DRV_144; |
255 |
drv->disk = FDRIVE_DISK_144; |
256 |
drv->last_sect = 24;
|
257 |
drv->max_track = 80;
|
258 |
drv->flags |= FDISK_DBL_SIDES; |
259 |
break;
|
260 |
case 3680: |
261 |
printf("1.84 Mb 3\"1/2 disk (1 80 23)");
|
262 |
drv->drive = FDRIVE_DRV_144; |
263 |
drv->disk = FDRIVE_DISK_144; |
264 |
drv->last_sect = 23;
|
265 |
drv->max_track = 80;
|
266 |
drv->flags |= FDISK_DBL_SIDES; |
267 |
break;
|
268 |
case 3520: |
269 |
printf("1.76 Mb 3\"1/2 disk (1 80 22)");
|
270 |
drv->drive = FDRIVE_DRV_144; |
271 |
drv->disk = FDRIVE_DISK_144; |
272 |
drv->last_sect = 22;
|
273 |
drv->max_track = 80;
|
274 |
drv->flags |= FDISK_DBL_SIDES; |
275 |
break;
|
276 |
case 3486: |
277 |
printf("1.74 Mb 3\"1/2 disk (1 83 21)");
|
278 |
drv->drive = FDRIVE_DRV_144; |
279 |
drv->disk = FDRIVE_DISK_144; |
280 |
drv->last_sect = 21;
|
281 |
drv->max_track = 83;
|
282 |
drv->flags |= FDISK_DBL_SIDES; |
283 |
break;
|
284 |
case 3444: |
285 |
printf("1.72 Mb 3\"1/2 disk (1 82 21)");
|
286 |
drv->drive = FDRIVE_DRV_144; |
287 |
drv->disk = FDRIVE_DISK_144; |
288 |
drv->last_sect = 21;
|
289 |
drv->max_track = 82;
|
290 |
drv->flags |= FDISK_DBL_SIDES; |
291 |
break;
|
292 |
case 3360: |
293 |
printf("1.68 Mb 3\"1/2 disk (1 80 21)");
|
294 |
drv->drive = FDRIVE_DRV_144; |
295 |
drv->disk = FDRIVE_DISK_144; |
296 |
drv->last_sect = 21;
|
297 |
drv->max_track = 80;
|
298 |
drv->flags |= FDISK_DBL_SIDES; |
299 |
break;
|
300 |
case 3200: |
301 |
printf("1.6 Mb 3\"1/2 disk (1 80 20)");
|
302 |
drv->drive = FDRIVE_DRV_144; |
303 |
drv->disk = FDRIVE_DISK_144; |
304 |
drv->last_sect = 20;
|
305 |
drv->max_track = 80;
|
306 |
drv->flags |= FDISK_DBL_SIDES; |
307 |
break;
|
308 |
case 2880: |
309 |
default:
|
310 |
printf("1.44 Mb 3\"1/2 disk (1 80 18)");
|
311 |
drv->drive = FDRIVE_DRV_144; |
312 |
drv->disk = FDRIVE_DISK_144; |
313 |
drv->last_sect = 18;
|
314 |
drv->max_track = 80;
|
315 |
drv->flags |= FDISK_DBL_SIDES; |
316 |
break;
|
317 |
|
318 |
/* 720 kB 3"1/2 drive disks */
|
319 |
case 2240: |
320 |
printf("1.12 Mb 3\"1/2 disk (1 80 14)");
|
321 |
drv->drive = FDRIVE_DRV_144; |
322 |
drv->disk = FDRIVE_DISK_720; |
323 |
drv->last_sect = 14;
|
324 |
drv->max_track = 80;
|
325 |
drv->flags |= FDISK_DBL_SIDES; |
326 |
break;
|
327 |
case 2080: |
328 |
printf("1.04 Mb 3\"1/2 disk (1 80 13)");
|
329 |
drv->drive = FDRIVE_DRV_144; |
330 |
drv->disk = FDRIVE_DISK_720; |
331 |
drv->last_sect = 13;
|
332 |
drv->max_track = 80;
|
333 |
drv->flags |= FDISK_DBL_SIDES; |
334 |
break;
|
335 |
case 1660: |
336 |
printf("830 kb 3\"1/2 disk (1 83 10)");
|
337 |
drv->drive = FDRIVE_DRV_144; |
338 |
drv->disk = FDRIVE_DISK_720; |
339 |
drv->last_sect = 10;
|
340 |
drv->max_track = 83;
|
341 |
drv->flags |= FDISK_DBL_SIDES; |
342 |
break;
|
343 |
case 1640: |
344 |
printf("820 kb 3\"1/2 disk (1 82 10)");
|
345 |
drv->drive = FDRIVE_DRV_144; |
346 |
drv->disk = FDRIVE_DISK_720; |
347 |
drv->last_sect = 10;
|
348 |
drv->max_track = 82;
|
349 |
drv->flags |= FDISK_DBL_SIDES; |
350 |
break;
|
351 |
case 1600: |
352 |
printf("800 kb 3\"1/2 disk (1 80 10)");
|
353 |
drv->drive = FDRIVE_DRV_144; |
354 |
drv->disk = FDRIVE_DISK_720; |
355 |
drv->last_sect = 10;
|
356 |
drv->max_track = 80;
|
357 |
drv->flags |= FDISK_DBL_SIDES; |
358 |
break;
|
359 |
case 1440: |
360 |
printf("720 kb 3\"1/2 disk (1 80 9)");
|
361 |
drv->drive = FDRIVE_DRV_144; |
362 |
drv->disk = FDRIVE_DISK_720; |
363 |
drv->last_sect = 9;
|
364 |
drv->max_track = 80;
|
365 |
drv->flags |= FDISK_DBL_SIDES; |
366 |
break;
|
367 |
|
368 |
/* 1.2 MB 5"1/4 drive disks */
|
369 |
case 2988: |
370 |
printf("1.49 Mb 5\"1/4 disk (1 83 18)");
|
371 |
drv->drive = FDRIVE_DRV_120; |
372 |
drv->disk = FDRIVE_DISK_144; /* ? */
|
373 |
drv->last_sect = 18;
|
374 |
drv->max_track = 83;
|
375 |
drv->flags |= FDISK_DBL_SIDES; |
376 |
break;
|
377 |
case 2952: |
378 |
printf("1.48 Mb 5\"1/4 disk (1 82 18)");
|
379 |
drv->drive = FDRIVE_DRV_120; |
380 |
drv->disk = FDRIVE_DISK_144; /* ? */
|
381 |
drv->last_sect = 18;
|
382 |
drv->max_track = 82;
|
383 |
drv->flags |= FDISK_DBL_SIDES; |
384 |
break;
|
385 |
case 2400: |
386 |
printf("1.2 Mb 5\"1/4 disk (1 80 15)");
|
387 |
drv->drive = FDRIVE_DRV_120; |
388 |
drv->disk = FDRIVE_DISK_144; /* ? */
|
389 |
drv->last_sect = 15;
|
390 |
drv->max_track = 80;
|
391 |
drv->flags |= FDISK_DBL_SIDES; |
392 |
break;
|
393 |
|
394 |
case 1760: |
395 |
printf("880 kb 5\"1/4 disk (1 80 11)");
|
396 |
drv->drive = FDRIVE_DRV_120; |
397 |
drv->disk = FDRIVE_DISK_144; /* ? */
|
398 |
drv->last_sect = 11;
|
399 |
drv->max_track = 80;
|
400 |
drv->flags |= FDISK_DBL_SIDES; |
401 |
break;
|
402 |
|
403 |
/* 360 kB 5"1/4 drive disks */
|
404 |
case 840: |
405 |
/* 420 kB 5"1/4 disk */
|
406 |
printf("420 kb 5\"1/4 disk (1 42 10)");
|
407 |
drv->drive = FDRIVE_DRV_120; |
408 |
drv->disk = FDRIVE_DISK_144; /* ? */
|
409 |
drv->last_sect = 10;
|
410 |
drv->max_track = 42;
|
411 |
drv->flags |= FDISK_DBL_SIDES; |
412 |
case 820: |
413 |
/* 410 kB 5"1/4 disk */
|
414 |
printf("410 kb 5\"1/4 disk (1 41 10)");
|
415 |
drv->drive = FDRIVE_DRV_120; |
416 |
drv->disk = FDRIVE_DISK_144; /* ? */
|
417 |
drv->last_sect = 10;
|
418 |
drv->max_track = 41;
|
419 |
drv->flags |= FDISK_DBL_SIDES; |
420 |
case 720: |
421 |
/* 360 kB 5"1/4 disk */
|
422 |
printf("360 kb 5\"1/4 disk (1 40 9)");
|
423 |
drv->drive = FDRIVE_DRV_120; |
424 |
drv->disk = FDRIVE_DISK_144; /* ? */
|
425 |
drv->last_sect = 9;
|
426 |
drv->max_track = 40;
|
427 |
drv->flags |= FDISK_DBL_SIDES; |
428 |
break;
|
429 |
} |
430 |
printf(" %s\n", ro == 0 ? "rw" : "ro"); |
431 |
} |
432 |
drv->ro = ro; |
433 |
} else {
|
434 |
printf("No disk in drive\n");
|
435 |
drv->disk = FDRIVE_DISK_NONE; |
436 |
drv->last_sect = 0;
|
437 |
drv->max_track = 0;
|
438 |
drv->flags &= ~FDISK_DBL_SIDES; |
439 |
} |
440 |
drv->drflags |= FDRIVE_REVALIDATE; |
441 |
} |
442 |
|
443 |
/* Motor control */
|
444 |
static void fd_start (fdrive_t *drv) |
445 |
{ |
446 |
drv->drflags |= FDRIVE_MOTOR_ON; |
447 |
} |
448 |
|
449 |
static void fd_stop (fdrive_t *drv) |
450 |
{ |
451 |
drv->drflags &= ~FDRIVE_MOTOR_ON; |
452 |
} |
453 |
|
454 |
/* Re-initialise a drives (motor off, repositioned) */
|
455 |
static void fd_reset (fdrive_t *drv) |
456 |
{ |
457 |
fd_stop(drv); |
458 |
fd_recalibrate(drv); |
459 |
} |
460 |
|
461 |
/********************************************************/
|
462 |
/* Intel 82078 floppy disk controler emulation */
|
463 |
|
464 |
static void fdctrl_reset (fdctrl_t *fdctrl, int do_irq); |
465 |
static void fdctrl_reset_fifo (fdctrl_t *fdctrl); |
466 |
static int fdctrl_transfer_handler (void *opaque, target_ulong addr, int size); |
467 |
static void fdctrl_raise_irq (fdctrl_t *fdctrl, uint8_t status); |
468 |
|
469 |
static uint32_t fdctrl_read_statusB (fdctrl_t *fdctrl);
|
470 |
static uint32_t fdctrl_read_dor (fdctrl_t *fdctrl);
|
471 |
static void fdctrl_write_dor (fdctrl_t *fdctrl, uint32_t value); |
472 |
static uint32_t fdctrl_read_tape (fdctrl_t *fdctrl);
|
473 |
static void fdctrl_write_tape (fdctrl_t *fdctrl, uint32_t value); |
474 |
static uint32_t fdctrl_read_main_status (fdctrl_t *fdctrl);
|
475 |
static void fdctrl_write_rate (fdctrl_t *fdctrl, uint32_t value); |
476 |
static uint32_t fdctrl_read_data (fdctrl_t *fdctrl);
|
477 |
static void fdctrl_write_data (fdctrl_t *fdctrl, uint32_t value); |
478 |
static uint32_t fdctrl_read_dir (fdctrl_t *fdctrl);
|
479 |
|
480 |
enum {
|
481 |
FD_CTRL_ACTIVE = 0x01,
|
482 |
FD_CTRL_RESET = 0x02,
|
483 |
FD_CTRL_SLEEP = 0x04,
|
484 |
FD_CTRL_BUSY = 0x08,
|
485 |
FD_CTRL_INTR = 0x10,
|
486 |
}; |
487 |
|
488 |
enum {
|
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 |
}; |
495 |
|
496 |
enum {
|
497 |
FD_STATE_CMD = 0x00,
|
498 |
FD_STATE_STATUS = 0x01,
|
499 |
FD_STATE_DATA = 0x02,
|
500 |
FD_STATE_STATE = 0x03,
|
501 |
FD_STATE_MULTI = 0x10,
|
502 |
FD_STATE_SEEK = 0x20,
|
503 |
FD_STATE_FORMAT = 0x40,
|
504 |
}; |
505 |
|
506 |
#define FD_STATE(state) ((state) & FD_STATE_STATE)
|
507 |
#define FD_SET_STATE(state, new_state) \
|
508 |
do { (state) = ((state) & ~FD_STATE_STATE) | (new_state); } while (0) |
509 |
#define FD_MULTI_TRACK(state) ((state) & FD_STATE_MULTI)
|
510 |
#define FD_DID_SEEK(state) ((state) & FD_STATE_SEEK)
|
511 |
#define FD_FORMAT_CMD(state) ((state) & FD_STATE_FORMAT)
|
512 |
|
513 |
struct fdctrl_t {
|
514 |
fdctrl_t *fdctrl; |
515 |
/* Controler's identification */
|
516 |
uint8_t version; |
517 |
/* HW */
|
518 |
int irq_lvl;
|
519 |
int dma_chann;
|
520 |
uint32_t io_base; |
521 |
/* Controler state */
|
522 |
uint8_t state; |
523 |
uint8_t dma_en; |
524 |
uint8_t cur_drv; |
525 |
uint8_t bootsel; |
526 |
/* Command FIFO */
|
527 |
uint8_t fifo[FD_SECTOR_LEN]; |
528 |
uint32_t data_pos; |
529 |
uint32_t data_len; |
530 |
uint8_t data_state; |
531 |
uint8_t data_dir; |
532 |
uint8_t int_status; |
533 |
/* States kept only to be returned back */
|
534 |
/* Timers state */
|
535 |
uint8_t timer0; |
536 |
uint8_t timer1; |
537 |
/* precompensation */
|
538 |
uint8_t precomp_trk; |
539 |
uint8_t config; |
540 |
uint8_t lock; |
541 |
/* Power down config (also with status regB access mode */
|
542 |
uint8_t pwrd; |
543 |
/* Floppy drives */
|
544 |
fdrive_t drives[2];
|
545 |
}; |
546 |
|
547 |
static uint32_t fdctrl_read (void *opaque, uint32_t reg) |
548 |
{ |
549 |
fdctrl_t *fdctrl = opaque; |
550 |
uint32_t retval; |
551 |
|
552 |
if (reg == fdctrl->io_base + 0x01) |
553 |
retval = fdctrl_read_statusB(fdctrl); |
554 |
else if (reg == fdctrl->io_base + 0x02) |
555 |
retval = fdctrl_read_dor(fdctrl); |
556 |
else if (reg == fdctrl->io_base + 0x03) |
557 |
retval = fdctrl_read_tape(fdctrl); |
558 |
else if (reg == fdctrl->io_base + 0x04) |
559 |
retval = fdctrl_read_main_status(fdctrl); |
560 |
else if (reg == fdctrl->io_base + 0x05) |
561 |
retval = fdctrl_read_data(fdctrl); |
562 |
else if (reg == fdctrl->io_base + 0x07) |
563 |
retval = fdctrl_read_dir(fdctrl); |
564 |
else
|
565 |
retval = (uint32_t)(-1);
|
566 |
|
567 |
return retval;
|
568 |
} |
569 |
|
570 |
static void fdctrl_write (void *opaque, uint32_t reg, uint32_t value) |
571 |
{ |
572 |
fdctrl_t *fdctrl = opaque; |
573 |
|
574 |
if (reg == fdctrl->io_base + 0x02) |
575 |
fdctrl_write_dor(fdctrl, value); |
576 |
else if (reg == fdctrl->io_base + 0x03) |
577 |
fdctrl_write_tape(fdctrl, value); |
578 |
else if (reg == fdctrl->io_base + 0x04) |
579 |
fdctrl_write_rate(fdctrl, value); |
580 |
else if (reg == fdctrl->io_base + 0x05) |
581 |
fdctrl_write_data(fdctrl, value); |
582 |
} |
583 |
|
584 |
static void fd_change_cb (void *opaque) |
585 |
{ |
586 |
fdrive_t *drv = opaque; |
587 |
|
588 |
FLOPPY_DPRINTF("disk change\n");
|
589 |
/* TODO: use command-line parameters to force geometry */
|
590 |
fd_revalidate(drv); |
591 |
#if 0
|
592 |
fd_recalibrate(drv);
|
593 |
fdctrl_reset_fifo(drv->fdctrl);
|
594 |
fdctrl_raise_irq(drv->fdctrl, 0x20);
|
595 |
#endif
|
596 |
} |
597 |
|
598 |
fdctrl_t *fdctrl_init (int irq_lvl, int dma_chann, int mem_mapped, |
599 |
uint32_t io_base, |
600 |
BlockDriverState **fds) |
601 |
{ |
602 |
fdctrl_t *fdctrl; |
603 |
// int io_mem;
|
604 |
int i;
|
605 |
|
606 |
FLOPPY_DPRINTF("init controler\n");
|
607 |
fdctrl = qemu_mallocz(sizeof(fdctrl_t));
|
608 |
if (!fdctrl)
|
609 |
return NULL; |
610 |
fdctrl->version = 0x90; /* Intel 82078 controler */ |
611 |
fdctrl->irq_lvl = irq_lvl; |
612 |
fdctrl->dma_chann = dma_chann; |
613 |
fdctrl->io_base = io_base; |
614 |
fdctrl->config = 0x40; /* Implicit seek, polling & FIFO enabled */ |
615 |
if (fdctrl->dma_chann != -1) { |
616 |
fdctrl->dma_en = 1;
|
617 |
DMA_register_channel(dma_chann, &fdctrl_transfer_handler, fdctrl); |
618 |
} else {
|
619 |
fdctrl->dma_en = 0;
|
620 |
} |
621 |
for (i = 0; i < 2; i++) { |
622 |
fd_init(&fdctrl->drives[i], fds[i]); |
623 |
if (fds[i]) {
|
624 |
bdrv_set_change_cb(fds[i], |
625 |
&fd_change_cb, &fdctrl->drives[i]); |
626 |
} |
627 |
} |
628 |
fdctrl_reset(fdctrl, 0);
|
629 |
fdctrl->state = FD_CTRL_ACTIVE; |
630 |
if (mem_mapped) {
|
631 |
FLOPPY_ERROR("memory mapped floppy not supported by now !\n");
|
632 |
#if 0
|
633 |
io_mem = cpu_register_io_memory(0, fdctrl_mem_read, fdctrl_mem_write);
|
634 |
cpu_register_physical_memory(base, 0x08, io_mem);
|
635 |
#endif
|
636 |
} else {
|
637 |
register_ioport_read(io_base + 0x01, 5, 1, &fdctrl_read, fdctrl); |
638 |
register_ioport_read(io_base + 0x07, 1, 1, &fdctrl_read, fdctrl); |
639 |
register_ioport_write(io_base + 0x01, 5, 1, &fdctrl_write, fdctrl); |
640 |
register_ioport_write(io_base + 0x07, 1, 1, &fdctrl_write, fdctrl); |
641 |
} |
642 |
for (i = 0; i < MAX_FD; i++) { |
643 |
fd_revalidate(&fdctrl->drives[i]); |
644 |
} |
645 |
return fdctrl;
|
646 |
} |
647 |
|
648 |
/* XXX: may change if moved to bdrv */
|
649 |
int fdctrl_get_drive_type(fdctrl_t *fdctrl, int drive_num) |
650 |
{ |
651 |
return fdctrl->drives[drive_num].drive;
|
652 |
} |
653 |
|
654 |
/* Change IRQ state */
|
655 |
static void fdctrl_reset_irq (fdctrl_t *fdctrl) |
656 |
{ |
657 |
if (fdctrl->state & FD_CTRL_INTR) {
|
658 |
pic_set_irq(fdctrl->irq_lvl, 0);
|
659 |
fdctrl->state &= ~(FD_CTRL_INTR | FD_CTRL_SLEEP | FD_CTRL_BUSY); |
660 |
} |
661 |
} |
662 |
|
663 |
static void fdctrl_raise_irq (fdctrl_t *fdctrl, uint8_t status) |
664 |
{ |
665 |
if (~(fdctrl->state & FD_CTRL_INTR)) {
|
666 |
pic_set_irq(fdctrl->irq_lvl, 1);
|
667 |
fdctrl->state |= FD_CTRL_INTR; |
668 |
} |
669 |
FLOPPY_DPRINTF("Set interrupt status to 0x%02x\n", status);
|
670 |
fdctrl->int_status = status; |
671 |
} |
672 |
|
673 |
/* Reset controler */
|
674 |
static void fdctrl_reset (fdctrl_t *fdctrl, int do_irq) |
675 |
{ |
676 |
int i;
|
677 |
|
678 |
FLOPPY_DPRINTF("reset controler\n");
|
679 |
fdctrl_reset_irq(fdctrl); |
680 |
/* Initialise controler */
|
681 |
fdctrl->cur_drv = 0;
|
682 |
/* FIFO state */
|
683 |
fdctrl->data_pos = 0;
|
684 |
fdctrl->data_len = 0;
|
685 |
fdctrl->data_state = FD_STATE_CMD; |
686 |
fdctrl->data_dir = FD_DIR_WRITE; |
687 |
for (i = 0; i < MAX_FD; i++) |
688 |
fd_reset(&fdctrl->drives[i]); |
689 |
fdctrl_reset_fifo(fdctrl); |
690 |
if (do_irq)
|
691 |
fdctrl_raise_irq(fdctrl, 0x20);
|
692 |
} |
693 |
|
694 |
static inline fdrive_t *drv0 (fdctrl_t *fdctrl) |
695 |
{ |
696 |
return &fdctrl->drives[fdctrl->bootsel];
|
697 |
} |
698 |
|
699 |
static inline fdrive_t *drv1 (fdctrl_t *fdctrl) |
700 |
{ |
701 |
return &fdctrl->drives[1 - fdctrl->bootsel]; |
702 |
} |
703 |
|
704 |
static fdrive_t *get_cur_drv (fdctrl_t *fdctrl)
|
705 |
{ |
706 |
return fdctrl->cur_drv == 0 ? drv0(fdctrl) : drv1(fdctrl); |
707 |
} |
708 |
|
709 |
/* Status B register : 0x01 (read-only) */
|
710 |
static uint32_t fdctrl_read_statusB (fdctrl_t *fdctrl)
|
711 |
{ |
712 |
fdctrl_reset_irq(fdctrl); |
713 |
FLOPPY_DPRINTF("status register: 0x00\n");
|
714 |
|
715 |
return 0; |
716 |
} |
717 |
|
718 |
/* Digital output register : 0x02 */
|
719 |
static uint32_t fdctrl_read_dor (fdctrl_t *fdctrl)
|
720 |
{ |
721 |
uint32_t retval = 0;
|
722 |
|
723 |
/* Drive motors state indicators */
|
724 |
if (drv0(fdctrl)->drflags & FDRIVE_MOTOR_ON)
|
725 |
retval |= 1 << 5; |
726 |
if (drv1(fdctrl)->drflags & FDRIVE_MOTOR_ON)
|
727 |
retval |= 1 << 4; |
728 |
/* DMA enable */
|
729 |
retval |= fdctrl->dma_en << 3;
|
730 |
/* Reset indicator */
|
731 |
retval |= (fdctrl->state & FD_CTRL_RESET) == 0 ? 0x04 : 0; |
732 |
/* Selected drive */
|
733 |
retval |= fdctrl->cur_drv; |
734 |
FLOPPY_DPRINTF("digital output register: 0x%02x\n", retval);
|
735 |
|
736 |
return retval;
|
737 |
} |
738 |
|
739 |
static void fdctrl_write_dor (fdctrl_t *fdctrl, uint32_t value) |
740 |
{ |
741 |
fdctrl_reset_irq(fdctrl); |
742 |
/* Reset mode */
|
743 |
if (fdctrl->state & FD_CTRL_RESET) {
|
744 |
if (!(value & 0x04)) { |
745 |
FLOPPY_DPRINTF("Floppy controler in RESET state !\n");
|
746 |
return;
|
747 |
} |
748 |
} |
749 |
FLOPPY_DPRINTF("digital output register set to 0x%02x\n", value);
|
750 |
/* Drive motors state indicators */
|
751 |
if (value & 0x20) |
752 |
fd_start(drv1(fdctrl)); |
753 |
else
|
754 |
fd_stop(drv1(fdctrl)); |
755 |
if (value & 0x10) |
756 |
fd_start(drv0(fdctrl)); |
757 |
else
|
758 |
fd_stop(drv0(fdctrl)); |
759 |
/* DMA enable */
|
760 |
#if 0
|
761 |
if (fdctrl->dma_chann != -1)
|
762 |
fdctrl->dma_en = 1 - ((value >> 3) & 1);
|
763 |
#endif
|
764 |
/* Reset */
|
765 |
if (!(value & 0x04)) { |
766 |
if (!(fdctrl->state & FD_CTRL_RESET)) {
|
767 |
FLOPPY_DPRINTF("controler enter RESET state\n");
|
768 |
fdctrl->state |= FD_CTRL_RESET; |
769 |
fdctrl_reset(fdctrl, 1);
|
770 |
} |
771 |
} else {
|
772 |
if (fdctrl->state & FD_CTRL_RESET) {
|
773 |
FLOPPY_DPRINTF("controler out of RESET state\n");
|
774 |
fdctrl->state &= ~(FD_CTRL_RESET | FD_CTRL_SLEEP); |
775 |
} |
776 |
} |
777 |
/* Selected drive */
|
778 |
fdctrl->cur_drv = value & 1;
|
779 |
} |
780 |
|
781 |
/* Tape drive register : 0x03 */
|
782 |
static uint32_t fdctrl_read_tape (fdctrl_t *fdctrl)
|
783 |
{ |
784 |
uint32_t retval = 0;
|
785 |
|
786 |
fdctrl_reset_irq(fdctrl); |
787 |
/* Disk boot selection indicator */
|
788 |
retval |= fdctrl->bootsel << 2;
|
789 |
/* Tape indicators: never allowed */
|
790 |
FLOPPY_DPRINTF("tape drive register: 0x%02x\n", retval);
|
791 |
|
792 |
return retval;
|
793 |
} |
794 |
|
795 |
static void fdctrl_write_tape (fdctrl_t *fdctrl, uint32_t value) |
796 |
{ |
797 |
fdctrl_reset_irq(fdctrl); |
798 |
/* Reset mode */
|
799 |
if (fdctrl->state & FD_CTRL_RESET) {
|
800 |
FLOPPY_DPRINTF("Floppy controler in RESET state !\n");
|
801 |
return;
|
802 |
} |
803 |
FLOPPY_DPRINTF("tape drive register set to 0x%02x\n", value);
|
804 |
/* Disk boot selection indicator */
|
805 |
fdctrl->bootsel = (value >> 2) & 1; |
806 |
/* Tape indicators: never allow */
|
807 |
} |
808 |
|
809 |
/* Main status register : 0x04 (read) */
|
810 |
static uint32_t fdctrl_read_main_status (fdctrl_t *fdctrl)
|
811 |
{ |
812 |
uint32_t retval = 0;
|
813 |
|
814 |
fdctrl_reset_irq(fdctrl); |
815 |
fdctrl->state &= ~(FD_CTRL_SLEEP | FD_CTRL_RESET); |
816 |
if (!(fdctrl->state & FD_CTRL_BUSY)) {
|
817 |
/* Data transfer allowed */
|
818 |
retval |= 0x80;
|
819 |
/* Data transfer direction indicator */
|
820 |
if (fdctrl->data_dir == FD_DIR_READ)
|
821 |
retval |= 0x40;
|
822 |
} |
823 |
/* Should handle 0x20 for SPECIFY command */
|
824 |
/* Command busy indicator */
|
825 |
if (FD_STATE(fdctrl->data_state) == FD_STATE_DATA ||
|
826 |
FD_STATE(fdctrl->data_state) == FD_STATE_STATUS) |
827 |
retval |= 0x10;
|
828 |
FLOPPY_DPRINTF("main status register: 0x%02x\n", retval);
|
829 |
|
830 |
return retval;
|
831 |
} |
832 |
|
833 |
/* Data select rate register : 0x04 (write) */
|
834 |
static void fdctrl_write_rate (fdctrl_t *fdctrl, uint32_t value) |
835 |
{ |
836 |
fdctrl_reset_irq(fdctrl); |
837 |
/* Reset mode */
|
838 |
if (fdctrl->state & FD_CTRL_RESET) {
|
839 |
FLOPPY_DPRINTF("Floppy controler in RESET state !\n");
|
840 |
return;
|
841 |
} |
842 |
FLOPPY_DPRINTF("select rate register set to 0x%02x\n", value);
|
843 |
/* Reset: autoclear */
|
844 |
if (value & 0x80) { |
845 |
fdctrl->state |= FD_CTRL_RESET; |
846 |
fdctrl_reset(fdctrl, 1);
|
847 |
fdctrl->state &= ~FD_CTRL_RESET; |
848 |
} |
849 |
if (value & 0x40) { |
850 |
fdctrl->state |= FD_CTRL_SLEEP; |
851 |
fdctrl_reset(fdctrl, 1);
|
852 |
} |
853 |
// fdctrl.precomp = (value >> 2) & 0x07;
|
854 |
} |
855 |
|
856 |
/* Digital input register : 0x07 (read-only) */
|
857 |
static uint32_t fdctrl_read_dir (fdctrl_t *fdctrl)
|
858 |
{ |
859 |
uint32_t retval = 0;
|
860 |
|
861 |
fdctrl_reset_irq(fdctrl); |
862 |
if (drv0(fdctrl)->drflags & FDRIVE_REVALIDATE ||
|
863 |
drv1(fdctrl)->drflags & FDRIVE_REVALIDATE) |
864 |
retval |= 0x80;
|
865 |
if (retval != 0) |
866 |
FLOPPY_DPRINTF("Floppy digital input register: 0x%02x\n", retval);
|
867 |
drv0(fdctrl)->drflags &= ~FDRIVE_REVALIDATE; |
868 |
drv1(fdctrl)->drflags &= ~FDRIVE_REVALIDATE; |
869 |
|
870 |
return retval;
|
871 |
} |
872 |
|
873 |
/* FIFO state control */
|
874 |
static void fdctrl_reset_fifo (fdctrl_t *fdctrl) |
875 |
{ |
876 |
fdctrl->data_dir = FD_DIR_WRITE; |
877 |
fdctrl->data_pos = 0;
|
878 |
FD_SET_STATE(fdctrl->data_state, FD_STATE_CMD); |
879 |
} |
880 |
|
881 |
/* Set FIFO status for the host to read */
|
882 |
static void fdctrl_set_fifo (fdctrl_t *fdctrl, int fifo_len, int do_irq) |
883 |
{ |
884 |
fdctrl->data_dir = FD_DIR_READ; |
885 |
fdctrl->data_len = fifo_len; |
886 |
fdctrl->data_pos = 0;
|
887 |
FD_SET_STATE(fdctrl->data_state, FD_STATE_STATUS); |
888 |
if (do_irq)
|
889 |
fdctrl_raise_irq(fdctrl, 0x00);
|
890 |
} |
891 |
|
892 |
/* Set an error: unimplemented/unknown command */
|
893 |
static void fdctrl_unimplemented (fdctrl_t *fdctrl) |
894 |
{ |
895 |
#if 0
|
896 |
fdrive_t *cur_drv;
|
897 |
|
898 |
cur_drv = get_cur_drv(fdctrl);
|
899 |
fdctrl->fifo[0] = 0x60 | (cur_drv->head << 1) | fdctrl->cur_drv;
|
900 |
fdctrl->fifo[1] = 0x00;
|
901 |
fdctrl->fifo[2] = 0x00;
|
902 |
fdctrl_set_fifo(fdctrl, 3, 1);
|
903 |
#else
|
904 |
// fdctrl_reset_fifo(fdctrl);
|
905 |
fdctrl->fifo[0] = 0x80; |
906 |
fdctrl_set_fifo(fdctrl, 1, 0); |
907 |
#endif
|
908 |
} |
909 |
|
910 |
/* Callback for transfer end (stop or abort) */
|
911 |
static void fdctrl_stop_transfer (fdctrl_t *fdctrl, uint8_t status0, |
912 |
uint8_t status1, uint8_t status2) |
913 |
{ |
914 |
fdrive_t *cur_drv; |
915 |
|
916 |
cur_drv = get_cur_drv(fdctrl); |
917 |
FLOPPY_DPRINTF("transfer status: %02x %02x %02x (%02x)\n",
|
918 |
status0, status1, status2, |
919 |
status0 | (cur_drv->head << 1) | fdctrl->cur_drv);
|
920 |
fdctrl->fifo[0] = status0 | (cur_drv->head << 1) | fdctrl->cur_drv; |
921 |
fdctrl->fifo[1] = status1;
|
922 |
fdctrl->fifo[2] = status2;
|
923 |
fdctrl->fifo[3] = cur_drv->track;
|
924 |
fdctrl->fifo[4] = cur_drv->head;
|
925 |
fdctrl->fifo[5] = cur_drv->sect;
|
926 |
fdctrl->fifo[6] = FD_SECTOR_SC;
|
927 |
fdctrl->data_dir = FD_DIR_READ; |
928 |
if (fdctrl->state & FD_CTRL_BUSY)
|
929 |
DMA_release_DREQ(fdctrl->dma_chann); |
930 |
fdctrl_set_fifo(fdctrl, 7, 1); |
931 |
} |
932 |
|
933 |
/* Prepare a data transfer (either DMA or FIFO) */
|
934 |
static void fdctrl_start_transfer (fdctrl_t *fdctrl, int direction) |
935 |
{ |
936 |
fdrive_t *cur_drv; |
937 |
uint8_t kh, kt, ks; |
938 |
int did_seek;
|
939 |
|
940 |
fdctrl->cur_drv = fdctrl->fifo[1] & 1; |
941 |
cur_drv = get_cur_drv(fdctrl); |
942 |
kt = fdctrl->fifo[2];
|
943 |
kh = fdctrl->fifo[3];
|
944 |
ks = fdctrl->fifo[4];
|
945 |
FLOPPY_DPRINTF("Start tranfert at %d %d %02x %02x (%d)\n",
|
946 |
fdctrl->cur_drv, kh, kt, ks, |
947 |
_fd_sector(kh, kt, ks, cur_drv->last_sect)); |
948 |
did_seek = 0;
|
949 |
switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & 0x40)) { |
950 |
case 2: |
951 |
/* sect too big */
|
952 |
fdctrl_stop_transfer(fdctrl, 0x40, 0x00, 0x00); |
953 |
fdctrl->fifo[3] = kt;
|
954 |
fdctrl->fifo[4] = kh;
|
955 |
fdctrl->fifo[5] = ks;
|
956 |
return;
|
957 |
case 3: |
958 |
/* track too big */
|
959 |
fdctrl_stop_transfer(fdctrl, 0x40, 0x80, 0x00); |
960 |
fdctrl->fifo[3] = kt;
|
961 |
fdctrl->fifo[4] = kh;
|
962 |
fdctrl->fifo[5] = ks;
|
963 |
return;
|
964 |
case 4: |
965 |
/* No seek enabled */
|
966 |
fdctrl_stop_transfer(fdctrl, 0x40, 0x00, 0x00); |
967 |
fdctrl->fifo[3] = kt;
|
968 |
fdctrl->fifo[4] = kh;
|
969 |
fdctrl->fifo[5] = ks;
|
970 |
return;
|
971 |
case 1: |
972 |
did_seek = 1;
|
973 |
break;
|
974 |
default:
|
975 |
break;
|
976 |
} |
977 |
/* Set the FIFO state */
|
978 |
fdctrl->data_dir = direction; |
979 |
fdctrl->data_pos = 0;
|
980 |
FD_SET_STATE(fdctrl->data_state, FD_STATE_DATA); /* FIFO ready for data */
|
981 |
if (fdctrl->fifo[0] & 0x80) |
982 |
fdctrl->data_state |= FD_STATE_MULTI; |
983 |
else
|
984 |
fdctrl->data_state &= ~FD_STATE_MULTI; |
985 |
if (did_seek)
|
986 |
fdctrl->data_state |= FD_STATE_SEEK; |
987 |
else
|
988 |
fdctrl->data_state &= ~FD_STATE_SEEK; |
989 |
if (fdctrl->fifo[5] == 00) { |
990 |
fdctrl->data_len = fdctrl->fifo[8];
|
991 |
} else {
|
992 |
int tmp;
|
993 |
fdctrl->data_len = 128 << fdctrl->fifo[5]; |
994 |
tmp = (cur_drv->last_sect - ks + 1);
|
995 |
if (fdctrl->fifo[0] & 0x80) |
996 |
tmp += cur_drv->last_sect; |
997 |
fdctrl->data_len *= tmp; |
998 |
} |
999 |
if (fdctrl->dma_en) {
|
1000 |
int dma_mode;
|
1001 |
/* DMA transfer are enabled. Check if DMA channel is well programmed */
|
1002 |
dma_mode = DMA_get_channel_mode(fdctrl->dma_chann); |
1003 |
dma_mode = (dma_mode >> 2) & 3; |
1004 |
FLOPPY_DPRINTF("dma_mode=%d direction=%d (%d - %d)\n",
|
1005 |
dma_mode, direction, |
1006 |
(128 << fdctrl->fifo[5]) * |
1007 |
(cur_drv->last_sect - ks + 1), fdctrl->data_len);
|
1008 |
if (((direction == FD_DIR_SCANE || direction == FD_DIR_SCANL ||
|
1009 |
direction == FD_DIR_SCANH) && dma_mode == 0) ||
|
1010 |
(direction == FD_DIR_WRITE && dma_mode == 2) ||
|
1011 |
(direction == FD_DIR_READ && dma_mode == 1)) {
|
1012 |
/* No access is allowed until DMA transfer has completed */
|
1013 |
fdctrl->state |= FD_CTRL_BUSY; |
1014 |
/* Now, we just have to wait for the DMA controler to
|
1015 |
* recall us...
|
1016 |
*/
|
1017 |
DMA_hold_DREQ(fdctrl->dma_chann); |
1018 |
DMA_schedule(fdctrl->dma_chann); |
1019 |
return;
|
1020 |
} else {
|
1021 |
FLOPPY_ERROR("dma_mode=%d direction=%d\n", dma_mode, direction);
|
1022 |
} |
1023 |
} |
1024 |
FLOPPY_DPRINTF("start non-DMA transfer\n");
|
1025 |
/* IO based transfer: calculate len */
|
1026 |
fdctrl_raise_irq(fdctrl, 0x00);
|
1027 |
|
1028 |
return;
|
1029 |
} |
1030 |
|
1031 |
/* Prepare a transfer of deleted data */
|
1032 |
static void fdctrl_start_transfer_del (fdctrl_t *fdctrl, int direction) |
1033 |
{ |
1034 |
/* We don't handle deleted data,
|
1035 |
* so we don't return *ANYTHING*
|
1036 |
*/
|
1037 |
fdctrl_stop_transfer(fdctrl, 0x60, 0x00, 0x00); |
1038 |
} |
1039 |
|
1040 |
/* handlers for DMA transfers */
|
1041 |
static int fdctrl_transfer_handler (void *opaque, target_ulong addr, int size) |
1042 |
{ |
1043 |
fdctrl_t *fdctrl; |
1044 |
fdrive_t *cur_drv; |
1045 |
int len, start_pos, rel_pos;
|
1046 |
uint8_t status0 = 0x00, status1 = 0x00, status2 = 0x00; |
1047 |
|
1048 |
fdctrl = opaque; |
1049 |
fdctrl_reset_irq(fdctrl); |
1050 |
if (!(fdctrl->state & FD_CTRL_BUSY)) {
|
1051 |
FLOPPY_DPRINTF("Not in DMA transfer mode !\n");
|
1052 |
return 0; |
1053 |
} |
1054 |
cur_drv = get_cur_drv(fdctrl); |
1055 |
if (fdctrl->data_dir == FD_DIR_SCANE || fdctrl->data_dir == FD_DIR_SCANL ||
|
1056 |
fdctrl->data_dir == FD_DIR_SCANH) |
1057 |
status2 = 0x04;
|
1058 |
if (size > fdctrl->data_len)
|
1059 |
size = fdctrl->data_len; |
1060 |
if (cur_drv->bs == NULL) { |
1061 |
if (fdctrl->data_dir == FD_DIR_WRITE)
|
1062 |
fdctrl_stop_transfer(fdctrl, 0x60, 0x00, 0x00); |
1063 |
else
|
1064 |
fdctrl_stop_transfer(fdctrl, 0x40, 0x00, 0x00); |
1065 |
len = 0;
|
1066 |
goto transfer_error;
|
1067 |
} |
1068 |
rel_pos = fdctrl->data_pos % FD_SECTOR_LEN; |
1069 |
for (start_pos = fdctrl->data_pos; fdctrl->data_pos < size;) {
|
1070 |
len = size - fdctrl->data_pos; |
1071 |
if (len + rel_pos > FD_SECTOR_LEN)
|
1072 |
len = FD_SECTOR_LEN - rel_pos; |
1073 |
FLOPPY_DPRINTF("copy %d bytes (%d %d %d) %d pos %d %02x %02x "
|
1074 |
"(%d-0x%08x 0x%08x)\n", len, size, fdctrl->data_pos,
|
1075 |
fdctrl->data_len, fdctrl->cur_drv, cur_drv->head, |
1076 |
cur_drv->track, cur_drv->sect, fd_sector(cur_drv), |
1077 |
fd_sector(cur_drv) * 512, addr);
|
1078 |
if (fdctrl->data_dir != FD_DIR_WRITE ||
|
1079 |
len < FD_SECTOR_LEN || rel_pos != 0) {
|
1080 |
/* READ & SCAN commands and realign to a sector for WRITE */
|
1081 |
if (bdrv_read(cur_drv->bs, fd_sector(cur_drv),
|
1082 |
fdctrl->fifo, 1) < 0) { |
1083 |
FLOPPY_DPRINTF("Floppy: error getting sector %d\n",
|
1084 |
fd_sector(cur_drv)); |
1085 |
/* Sure, image size is too small... */
|
1086 |
memset(fdctrl->fifo, 0, FD_SECTOR_LEN);
|
1087 |
} |
1088 |
} |
1089 |
switch (fdctrl->data_dir) {
|
1090 |
case FD_DIR_READ:
|
1091 |
/* READ commands */
|
1092 |
cpu_physical_memory_write(addr + fdctrl->data_pos, |
1093 |
fdctrl->fifo + rel_pos, len); |
1094 |
break;
|
1095 |
case FD_DIR_WRITE:
|
1096 |
/* WRITE commands */
|
1097 |
cpu_physical_memory_read(addr + fdctrl->data_pos, |
1098 |
fdctrl->fifo + rel_pos, len); |
1099 |
if (bdrv_write(cur_drv->bs, fd_sector(cur_drv),
|
1100 |
fdctrl->fifo, 1) < 0) { |
1101 |
FLOPPY_ERROR("writting sector %d\n", fd_sector(cur_drv));
|
1102 |
fdctrl_stop_transfer(fdctrl, 0x60, 0x00, 0x00); |
1103 |
goto transfer_error;
|
1104 |
} |
1105 |
break;
|
1106 |
default:
|
1107 |
/* SCAN commands */
|
1108 |
{ |
1109 |
uint8_t tmpbuf[FD_SECTOR_LEN]; |
1110 |
int ret;
|
1111 |
cpu_physical_memory_read(addr + fdctrl->data_pos, |
1112 |
tmpbuf, len); |
1113 |
ret = memcmp(tmpbuf, fdctrl->fifo + rel_pos, len); |
1114 |
if (ret == 0) { |
1115 |
status2 = 0x08;
|
1116 |
goto end_transfer;
|
1117 |
} |
1118 |
if ((ret < 0 && fdctrl->data_dir == FD_DIR_SCANL) || |
1119 |
(ret > 0 && fdctrl->data_dir == FD_DIR_SCANH)) {
|
1120 |
status2 = 0x00;
|
1121 |
goto end_transfer;
|
1122 |
} |
1123 |
} |
1124 |
break;
|
1125 |
} |
1126 |
fdctrl->data_pos += len; |
1127 |
rel_pos = fdctrl->data_pos % FD_SECTOR_LEN; |
1128 |
if (rel_pos == 0) { |
1129 |
/* Seek to next sector */
|
1130 |
cur_drv->sect++; |
1131 |
FLOPPY_DPRINTF("seek to next sector (%d %02x %02x => %d) (%d)\n",
|
1132 |
cur_drv->head, cur_drv->track, cur_drv->sect, |
1133 |
fd_sector(cur_drv), |
1134 |
fdctrl->data_pos - size); |
1135 |
if (cur_drv->sect > cur_drv->last_sect) {
|
1136 |
cur_drv->sect = 1;
|
1137 |
if (FD_MULTI_TRACK(fdctrl->data_state)) {
|
1138 |
if (cur_drv->head == 0 && |
1139 |
(cur_drv->flags & FDISK_DBL_SIDES) != 0) {
|
1140 |
cur_drv->head = 1;
|
1141 |
} else {
|
1142 |
cur_drv->head = 0;
|
1143 |
cur_drv->track++; |
1144 |
if ((cur_drv->flags & FDISK_DBL_SIDES) == 0) |
1145 |
break;
|
1146 |
} |
1147 |
} else {
|
1148 |
cur_drv->track++; |
1149 |
break;
|
1150 |
} |
1151 |
FLOPPY_DPRINTF("seek to next track (%d %02x %02x => %d)\n",
|
1152 |
cur_drv->head, cur_drv->track, |
1153 |
cur_drv->sect, fd_sector(cur_drv)); |
1154 |
} |
1155 |
} |
1156 |
} |
1157 |
end_transfer:
|
1158 |
len = fdctrl->data_pos - start_pos; |
1159 |
FLOPPY_DPRINTF("end transfer %d %d %d\n",
|
1160 |
fdctrl->data_pos, len, fdctrl->data_len); |
1161 |
if (fdctrl->data_dir == FD_DIR_SCANE ||
|
1162 |
fdctrl->data_dir == FD_DIR_SCANL || |
1163 |
fdctrl->data_dir == FD_DIR_SCANH) |
1164 |
status2 = 0x08;
|
1165 |
if (FD_DID_SEEK(fdctrl->data_state))
|
1166 |
status0 |= 0x20;
|
1167 |
fdctrl->data_len -= len; |
1168 |
// if (fdctrl->data_len == 0)
|
1169 |
fdctrl_stop_transfer(fdctrl, status0, status1, status2); |
1170 |
transfer_error:
|
1171 |
|
1172 |
return len;
|
1173 |
} |
1174 |
|
1175 |
/* Data register : 0x05 */
|
1176 |
static uint32_t fdctrl_read_data (fdctrl_t *fdctrl)
|
1177 |
{ |
1178 |
fdrive_t *cur_drv; |
1179 |
uint32_t retval = 0;
|
1180 |
int pos, len;
|
1181 |
|
1182 |
fdctrl_reset_irq(fdctrl); |
1183 |
cur_drv = get_cur_drv(fdctrl); |
1184 |
fdctrl->state &= ~FD_CTRL_SLEEP; |
1185 |
if (FD_STATE(fdctrl->data_state) == FD_STATE_CMD) {
|
1186 |
FLOPPY_ERROR("can't read data in CMD state\n");
|
1187 |
return 0; |
1188 |
} |
1189 |
pos = fdctrl->data_pos; |
1190 |
if (FD_STATE(fdctrl->data_state) == FD_STATE_DATA) {
|
1191 |
pos %= FD_SECTOR_LEN; |
1192 |
if (pos == 0) { |
1193 |
len = fdctrl->data_len - fdctrl->data_pos; |
1194 |
if (len > FD_SECTOR_LEN)
|
1195 |
len = FD_SECTOR_LEN; |
1196 |
bdrv_read(cur_drv->bs, fd_sector(cur_drv), |
1197 |
fdctrl->fifo, len); |
1198 |
} |
1199 |
} |
1200 |
retval = fdctrl->fifo[pos]; |
1201 |
if (++fdctrl->data_pos == fdctrl->data_len) {
|
1202 |
fdctrl->data_pos = 0;
|
1203 |
/* Switch from transfert mode to status mode
|
1204 |
* then from status mode to command mode
|
1205 |
*/
|
1206 |
if (FD_STATE(fdctrl->data_state) == FD_STATE_DATA)
|
1207 |
fdctrl_stop_transfer(fdctrl, 0x20, 0x00, 0x00); |
1208 |
else
|
1209 |
fdctrl_reset_fifo(fdctrl); |
1210 |
} |
1211 |
FLOPPY_DPRINTF("data register: 0x%02x\n", retval);
|
1212 |
|
1213 |
return retval;
|
1214 |
} |
1215 |
|
1216 |
static void fdctrl_format_sector (fdctrl_t *fdctrl) |
1217 |
{ |
1218 |
fdrive_t *cur_drv; |
1219 |
uint8_t kh, kt, ks; |
1220 |
int did_seek;
|
1221 |
|
1222 |
fdctrl->cur_drv = fdctrl->fifo[1] & 1; |
1223 |
cur_drv = get_cur_drv(fdctrl); |
1224 |
kt = fdctrl->fifo[6];
|
1225 |
kh = fdctrl->fifo[7];
|
1226 |
ks = fdctrl->fifo[8];
|
1227 |
FLOPPY_DPRINTF("format sector at %d %d %02x %02x (%d)\n",
|
1228 |
fdctrl->cur_drv, kh, kt, ks, |
1229 |
_fd_sector(kh, kt, ks, cur_drv->last_sect)); |
1230 |
did_seek = 0;
|
1231 |
switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & 0x40)) { |
1232 |
case 2: |
1233 |
/* sect too big */
|
1234 |
fdctrl_stop_transfer(fdctrl, 0x40, 0x00, 0x00); |
1235 |
fdctrl->fifo[3] = kt;
|
1236 |
fdctrl->fifo[4] = kh;
|
1237 |
fdctrl->fifo[5] = ks;
|
1238 |
return;
|
1239 |
case 3: |
1240 |
/* track too big */
|
1241 |
fdctrl_stop_transfer(fdctrl, 0x40, 0x80, 0x00); |
1242 |
fdctrl->fifo[3] = kt;
|
1243 |
fdctrl->fifo[4] = kh;
|
1244 |
fdctrl->fifo[5] = ks;
|
1245 |
return;
|
1246 |
case 4: |
1247 |
/* No seek enabled */
|
1248 |
fdctrl_stop_transfer(fdctrl, 0x40, 0x00, 0x00); |
1249 |
fdctrl->fifo[3] = kt;
|
1250 |
fdctrl->fifo[4] = kh;
|
1251 |
fdctrl->fifo[5] = ks;
|
1252 |
return;
|
1253 |
case 1: |
1254 |
did_seek = 1;
|
1255 |
fdctrl->data_state |= FD_STATE_SEEK; |
1256 |
break;
|
1257 |
default:
|
1258 |
break;
|
1259 |
} |
1260 |
memset(fdctrl->fifo, 0, FD_SECTOR_LEN);
|
1261 |
if (cur_drv->bs == NULL || |
1262 |
bdrv_write(cur_drv->bs, fd_sector(cur_drv), fdctrl->fifo, 1) < 0) { |
1263 |
FLOPPY_ERROR("formating sector %d\n", fd_sector(cur_drv));
|
1264 |
fdctrl_stop_transfer(fdctrl, 0x60, 0x00, 0x00); |
1265 |
} else {
|
1266 |
if (cur_drv->sect == cur_drv->last_sect) {
|
1267 |
fdctrl->data_state &= ~FD_STATE_FORMAT; |
1268 |
/* Last sector done */
|
1269 |
if (FD_DID_SEEK(fdctrl->data_state))
|
1270 |
fdctrl_stop_transfer(fdctrl, 0x20, 0x00, 0x00); |
1271 |
else
|
1272 |
fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00); |
1273 |
} else {
|
1274 |
/* More to do */
|
1275 |
fdctrl->data_pos = 0;
|
1276 |
fdctrl->data_len = 4;
|
1277 |
} |
1278 |
} |
1279 |
} |
1280 |
|
1281 |
static void fdctrl_write_data (fdctrl_t *fdctrl, uint32_t value) |
1282 |
{ |
1283 |
fdrive_t *cur_drv; |
1284 |
|
1285 |
fdctrl_reset_irq(fdctrl); |
1286 |
cur_drv = get_cur_drv(fdctrl); |
1287 |
/* Reset mode */
|
1288 |
if (fdctrl->state & FD_CTRL_RESET) {
|
1289 |
FLOPPY_DPRINTF("Floppy controler in RESET state !\n");
|
1290 |
return;
|
1291 |
} |
1292 |
fdctrl->state &= ~FD_CTRL_SLEEP; |
1293 |
if (FD_STATE(fdctrl->data_state) == FD_STATE_STATUS) {
|
1294 |
FLOPPY_ERROR("can't write data in status mode\n");
|
1295 |
return;
|
1296 |
} |
1297 |
/* Is it write command time ? */
|
1298 |
if (FD_STATE(fdctrl->data_state) == FD_STATE_DATA) {
|
1299 |
/* FIFO data write */
|
1300 |
fdctrl->fifo[fdctrl->data_pos++] = value; |
1301 |
if (fdctrl->data_pos % FD_SECTOR_LEN == (FD_SECTOR_LEN - 1) || |
1302 |
fdctrl->data_pos == fdctrl->data_len) { |
1303 |
bdrv_write(cur_drv->bs, fd_sector(cur_drv), |
1304 |
fdctrl->fifo, FD_SECTOR_LEN); |
1305 |
} |
1306 |
/* Switch from transfert mode to status mode
|
1307 |
* then from status mode to command mode
|
1308 |
*/
|
1309 |
if (FD_STATE(fdctrl->data_state) == FD_STATE_DATA)
|
1310 |
fdctrl_stop_transfer(fdctrl, 0x20, 0x00, 0x00); |
1311 |
return;
|
1312 |
} |
1313 |
if (fdctrl->data_pos == 0) { |
1314 |
/* Command */
|
1315 |
switch (value & 0x5F) { |
1316 |
case 0x46: |
1317 |
/* READ variants */
|
1318 |
FLOPPY_DPRINTF("READ command\n");
|
1319 |
/* 8 parameters cmd */
|
1320 |
fdctrl->data_len = 9;
|
1321 |
goto enqueue;
|
1322 |
case 0x4C: |
1323 |
/* READ_DELETED variants */
|
1324 |
FLOPPY_DPRINTF("READ_DELETED command\n");
|
1325 |
/* 8 parameters cmd */
|
1326 |
fdctrl->data_len = 9;
|
1327 |
goto enqueue;
|
1328 |
case 0x50: |
1329 |
/* SCAN_EQUAL variants */
|
1330 |
FLOPPY_DPRINTF("SCAN_EQUAL command\n");
|
1331 |
/* 8 parameters cmd */
|
1332 |
fdctrl->data_len = 9;
|
1333 |
goto enqueue;
|
1334 |
case 0x56: |
1335 |
/* VERIFY variants */
|
1336 |
FLOPPY_DPRINTF("VERIFY command\n");
|
1337 |
/* 8 parameters cmd */
|
1338 |
fdctrl->data_len = 9;
|
1339 |
goto enqueue;
|
1340 |
case 0x59: |
1341 |
/* SCAN_LOW_OR_EQUAL variants */
|
1342 |
FLOPPY_DPRINTF("SCAN_LOW_OR_EQUAL command\n");
|
1343 |
/* 8 parameters cmd */
|
1344 |
fdctrl->data_len = 9;
|
1345 |
goto enqueue;
|
1346 |
case 0x5D: |
1347 |
/* SCAN_HIGH_OR_EQUAL variants */
|
1348 |
FLOPPY_DPRINTF("SCAN_HIGH_OR_EQUAL command\n");
|
1349 |
/* 8 parameters cmd */
|
1350 |
fdctrl->data_len = 9;
|
1351 |
goto enqueue;
|
1352 |
default:
|
1353 |
break;
|
1354 |
} |
1355 |
switch (value & 0x7F) { |
1356 |
case 0x45: |
1357 |
/* WRITE variants */
|
1358 |
FLOPPY_DPRINTF("WRITE command\n");
|
1359 |
/* 8 parameters cmd */
|
1360 |
fdctrl->data_len = 9;
|
1361 |
goto enqueue;
|
1362 |
case 0x49: |
1363 |
/* WRITE_DELETED variants */
|
1364 |
FLOPPY_DPRINTF("WRITE_DELETED command\n");
|
1365 |
/* 8 parameters cmd */
|
1366 |
fdctrl->data_len = 9;
|
1367 |
goto enqueue;
|
1368 |
default:
|
1369 |
break;
|
1370 |
} |
1371 |
switch (value) {
|
1372 |
case 0x03: |
1373 |
/* SPECIFY */
|
1374 |
FLOPPY_DPRINTF("SPECIFY command\n");
|
1375 |
/* 1 parameter cmd */
|
1376 |
fdctrl->data_len = 3;
|
1377 |
goto enqueue;
|
1378 |
case 0x04: |
1379 |
/* SENSE_DRIVE_STATUS */
|
1380 |
FLOPPY_DPRINTF("SENSE_DRIVE_STATUS command\n");
|
1381 |
/* 1 parameter cmd */
|
1382 |
fdctrl->data_len = 2;
|
1383 |
goto enqueue;
|
1384 |
case 0x07: |
1385 |
/* RECALIBRATE */
|
1386 |
FLOPPY_DPRINTF("RECALIBRATE command\n");
|
1387 |
/* 1 parameter cmd */
|
1388 |
fdctrl->data_len = 2;
|
1389 |
goto enqueue;
|
1390 |
case 0x08: |
1391 |
/* SENSE_INTERRUPT_STATUS */
|
1392 |
FLOPPY_DPRINTF("SENSE_INTERRUPT_STATUS command (%02x)\n",
|
1393 |
fdctrl->int_status); |
1394 |
/* No parameters cmd: returns status if no interrupt */
|
1395 |
fdctrl->fifo[0] =
|
1396 |
fdctrl->int_status | (cur_drv->head << 2) | fdctrl->cur_drv;
|
1397 |
fdctrl->fifo[1] = cur_drv->track;
|
1398 |
fdctrl_set_fifo(fdctrl, 2, 0); |
1399 |
fdctrl_reset_irq(fdctrl); |
1400 |
fdctrl->int_status = 0xC0;
|
1401 |
return;
|
1402 |
case 0x0E: |
1403 |
/* DUMPREG */
|
1404 |
FLOPPY_DPRINTF("DUMPREG command\n");
|
1405 |
/* Drives position */
|
1406 |
fdctrl->fifo[0] = drv0(fdctrl)->track;
|
1407 |
fdctrl->fifo[1] = drv1(fdctrl)->track;
|
1408 |
fdctrl->fifo[2] = 0; |
1409 |
fdctrl->fifo[3] = 0; |
1410 |
/* timers */
|
1411 |
fdctrl->fifo[4] = fdctrl->timer0;
|
1412 |
fdctrl->fifo[5] = (fdctrl->timer1 << 1) | fdctrl->dma_en; |
1413 |
fdctrl->fifo[6] = cur_drv->last_sect;
|
1414 |
fdctrl->fifo[7] = (fdctrl->lock << 7) | |
1415 |
(cur_drv->perpendicular << 2);
|
1416 |
fdctrl->fifo[8] = fdctrl->config;
|
1417 |
fdctrl->fifo[9] = fdctrl->precomp_trk;
|
1418 |
fdctrl_set_fifo(fdctrl, 10, 0); |
1419 |
return;
|
1420 |
case 0x0F: |
1421 |
/* SEEK */
|
1422 |
FLOPPY_DPRINTF("SEEK command\n");
|
1423 |
/* 2 parameters cmd */
|
1424 |
fdctrl->data_len = 3;
|
1425 |
goto enqueue;
|
1426 |
case 0x10: |
1427 |
/* VERSION */
|
1428 |
FLOPPY_DPRINTF("VERSION command\n");
|
1429 |
/* No parameters cmd */
|
1430 |
/* Controler's version */
|
1431 |
fdctrl->fifo[0] = fdctrl->version;
|
1432 |
fdctrl_set_fifo(fdctrl, 1, 1); |
1433 |
return;
|
1434 |
case 0x12: |
1435 |
/* PERPENDICULAR_MODE */
|
1436 |
FLOPPY_DPRINTF("PERPENDICULAR_MODE command\n");
|
1437 |
/* 1 parameter cmd */
|
1438 |
fdctrl->data_len = 2;
|
1439 |
goto enqueue;
|
1440 |
case 0x13: |
1441 |
/* CONFIGURE */
|
1442 |
FLOPPY_DPRINTF("CONFIGURE command\n");
|
1443 |
/* 3 parameters cmd */
|
1444 |
fdctrl->data_len = 4;
|
1445 |
goto enqueue;
|
1446 |
case 0x14: |
1447 |
/* UNLOCK */
|
1448 |
FLOPPY_DPRINTF("UNLOCK command\n");
|
1449 |
/* No parameters cmd */
|
1450 |
fdctrl->lock = 0;
|
1451 |
fdctrl->fifo[0] = 0; |
1452 |
fdctrl_set_fifo(fdctrl, 1, 0); |
1453 |
return;
|
1454 |
case 0x17: |
1455 |
/* POWERDOWN_MODE */
|
1456 |
FLOPPY_DPRINTF("POWERDOWN_MODE command\n");
|
1457 |
/* 2 parameters cmd */
|
1458 |
fdctrl->data_len = 3;
|
1459 |
goto enqueue;
|
1460 |
case 0x18: |
1461 |
/* PART_ID */
|
1462 |
FLOPPY_DPRINTF("PART_ID command\n");
|
1463 |
/* No parameters cmd */
|
1464 |
fdctrl->fifo[0] = 0x41; /* Stepping 1 */ |
1465 |
fdctrl_set_fifo(fdctrl, 1, 0); |
1466 |
return;
|
1467 |
case 0x2C: |
1468 |
/* SAVE */
|
1469 |
FLOPPY_DPRINTF("SAVE command\n");
|
1470 |
/* No parameters cmd */
|
1471 |
fdctrl->fifo[0] = 0; |
1472 |
fdctrl->fifo[1] = 0; |
1473 |
/* Drives position */
|
1474 |
fdctrl->fifo[2] = drv0(fdctrl)->track;
|
1475 |
fdctrl->fifo[3] = drv1(fdctrl)->track;
|
1476 |
fdctrl->fifo[4] = 0; |
1477 |
fdctrl->fifo[5] = 0; |
1478 |
/* timers */
|
1479 |
fdctrl->fifo[6] = fdctrl->timer0;
|
1480 |
fdctrl->fifo[7] = fdctrl->timer1;
|
1481 |
fdctrl->fifo[8] = cur_drv->last_sect;
|
1482 |
fdctrl->fifo[9] = (fdctrl->lock << 7) | |
1483 |
(cur_drv->perpendicular << 2);
|
1484 |
fdctrl->fifo[10] = fdctrl->config;
|
1485 |
fdctrl->fifo[11] = fdctrl->precomp_trk;
|
1486 |
fdctrl->fifo[12] = fdctrl->pwrd;
|
1487 |
fdctrl->fifo[13] = 0; |
1488 |
fdctrl->fifo[14] = 0; |
1489 |
fdctrl_set_fifo(fdctrl, 15, 1); |
1490 |
return;
|
1491 |
case 0x33: |
1492 |
/* OPTION */
|
1493 |
FLOPPY_DPRINTF("OPTION command\n");
|
1494 |
/* 1 parameter cmd */
|
1495 |
fdctrl->data_len = 2;
|
1496 |
goto enqueue;
|
1497 |
case 0x42: |
1498 |
/* READ_TRACK */
|
1499 |
FLOPPY_DPRINTF("READ_TRACK command\n");
|
1500 |
/* 8 parameters cmd */
|
1501 |
fdctrl->data_len = 9;
|
1502 |
goto enqueue;
|
1503 |
case 0x4A: |
1504 |
/* READ_ID */
|
1505 |
FLOPPY_DPRINTF("READ_ID command\n");
|
1506 |
/* 1 parameter cmd */
|
1507 |
fdctrl->data_len = 2;
|
1508 |
goto enqueue;
|
1509 |
case 0x4C: |
1510 |
/* RESTORE */
|
1511 |
FLOPPY_DPRINTF("RESTORE command\n");
|
1512 |
/* 17 parameters cmd */
|
1513 |
fdctrl->data_len = 18;
|
1514 |
goto enqueue;
|
1515 |
case 0x4D: |
1516 |
/* FORMAT_TRACK */
|
1517 |
FLOPPY_DPRINTF("FORMAT_TRACK command\n");
|
1518 |
/* 5 parameters cmd */
|
1519 |
fdctrl->data_len = 6;
|
1520 |
goto enqueue;
|
1521 |
case 0x8E: |
1522 |
/* DRIVE_SPECIFICATION_COMMAND */
|
1523 |
FLOPPY_DPRINTF("DRIVE_SPECIFICATION_COMMAND command\n");
|
1524 |
/* 5 parameters cmd */
|
1525 |
fdctrl->data_len = 6;
|
1526 |
goto enqueue;
|
1527 |
case 0x8F: |
1528 |
/* RELATIVE_SEEK_OUT */
|
1529 |
FLOPPY_DPRINTF("RELATIVE_SEEK_OUT command\n");
|
1530 |
/* 2 parameters cmd */
|
1531 |
fdctrl->data_len = 3;
|
1532 |
goto enqueue;
|
1533 |
case 0x94: |
1534 |
/* LOCK */
|
1535 |
FLOPPY_DPRINTF("LOCK command\n");
|
1536 |
/* No parameters cmd */
|
1537 |
fdctrl->lock = 1;
|
1538 |
fdctrl->fifo[0] = 0x10; |
1539 |
fdctrl_set_fifo(fdctrl, 1, 1); |
1540 |
return;
|
1541 |
case 0xCD: |
1542 |
/* FORMAT_AND_WRITE */
|
1543 |
FLOPPY_DPRINTF("FORMAT_AND_WRITE command\n");
|
1544 |
/* 10 parameters cmd */
|
1545 |
fdctrl->data_len = 11;
|
1546 |
goto enqueue;
|
1547 |
case 0xCF: |
1548 |
/* RELATIVE_SEEK_IN */
|
1549 |
FLOPPY_DPRINTF("RELATIVE_SEEK_IN command\n");
|
1550 |
/* 2 parameters cmd */
|
1551 |
fdctrl->data_len = 3;
|
1552 |
goto enqueue;
|
1553 |
default:
|
1554 |
/* Unknown command */
|
1555 |
FLOPPY_ERROR("unknown command: 0x%02x\n", value);
|
1556 |
fdctrl_unimplemented(fdctrl); |
1557 |
return;
|
1558 |
} |
1559 |
} |
1560 |
enqueue:
|
1561 |
FLOPPY_DPRINTF("%s: %02x\n", __func__, value);
|
1562 |
fdctrl->fifo[fdctrl->data_pos] = value; |
1563 |
if (++fdctrl->data_pos == fdctrl->data_len) {
|
1564 |
/* We now have all parameters
|
1565 |
* and will be able to treat the command
|
1566 |
*/
|
1567 |
if (fdctrl->data_state & FD_STATE_FORMAT) {
|
1568 |
fdctrl_format_sector(fdctrl); |
1569 |
return;
|
1570 |
} |
1571 |
switch (fdctrl->fifo[0] & 0x1F) { |
1572 |
case 0x06: |
1573 |
{ |
1574 |
/* READ variants */
|
1575 |
FLOPPY_DPRINTF("treat READ command\n");
|
1576 |
fdctrl_start_transfer(fdctrl, FD_DIR_READ); |
1577 |
return;
|
1578 |
} |
1579 |
case 0x0C: |
1580 |
/* READ_DELETED variants */
|
1581 |
// FLOPPY_DPRINTF("treat READ_DELETED command\n");
|
1582 |
FLOPPY_ERROR("treat READ_DELETED command\n");
|
1583 |
fdctrl_start_transfer_del(fdctrl, FD_DIR_READ); |
1584 |
return;
|
1585 |
case 0x16: |
1586 |
/* VERIFY variants */
|
1587 |
// FLOPPY_DPRINTF("treat VERIFY command\n");
|
1588 |
FLOPPY_ERROR("treat VERIFY command\n");
|
1589 |
fdctrl_stop_transfer(fdctrl, 0x20, 0x00, 0x00); |
1590 |
return;
|
1591 |
case 0x10: |
1592 |
/* SCAN_EQUAL variants */
|
1593 |
// FLOPPY_DPRINTF("treat SCAN_EQUAL command\n");
|
1594 |
FLOPPY_ERROR("treat SCAN_EQUAL command\n");
|
1595 |
fdctrl_start_transfer(fdctrl, FD_DIR_SCANE); |
1596 |
return;
|
1597 |
case 0x19: |
1598 |
/* SCAN_LOW_OR_EQUAL variants */
|
1599 |
// FLOPPY_DPRINTF("treat SCAN_LOW_OR_EQUAL command\n");
|
1600 |
FLOPPY_ERROR("treat SCAN_LOW_OR_EQUAL command\n");
|
1601 |
fdctrl_start_transfer(fdctrl, FD_DIR_SCANL); |
1602 |
return;
|
1603 |
case 0x1D: |
1604 |
/* SCAN_HIGH_OR_EQUAL variants */
|
1605 |
// FLOPPY_DPRINTF("treat SCAN_HIGH_OR_EQUAL command\n");
|
1606 |
FLOPPY_ERROR("treat SCAN_HIGH_OR_EQUAL command\n");
|
1607 |
fdctrl_start_transfer(fdctrl, FD_DIR_SCANH); |
1608 |
return;
|
1609 |
default:
|
1610 |
break;
|
1611 |
} |
1612 |
switch (fdctrl->fifo[0] & 0x3F) { |
1613 |
case 0x05: |
1614 |
/* WRITE variants */
|
1615 |
FLOPPY_DPRINTF("treat WRITE command (%02x)\n", fdctrl->fifo[0]); |
1616 |
fdctrl_start_transfer(fdctrl, FD_DIR_WRITE); |
1617 |
return;
|
1618 |
case 0x09: |
1619 |
/* WRITE_DELETED variants */
|
1620 |
// FLOPPY_DPRINTF("treat WRITE_DELETED command\n");
|
1621 |
FLOPPY_ERROR("treat WRITE_DELETED command\n");
|
1622 |
fdctrl_start_transfer_del(fdctrl, FD_DIR_WRITE); |
1623 |
return;
|
1624 |
default:
|
1625 |
break;
|
1626 |
} |
1627 |
switch (fdctrl->fifo[0]) { |
1628 |
case 0x03: |
1629 |
/* SPECIFY */
|
1630 |
FLOPPY_DPRINTF("treat SPECIFY command\n");
|
1631 |
fdctrl->timer0 = (fdctrl->fifo[1] >> 4) & 0xF; |
1632 |
fdctrl->timer1 = fdctrl->fifo[1] >> 1; |
1633 |
fdctrl->dma_en = 1 - (fdctrl->fifo[2] & 1) ; |
1634 |
/* No result back */
|
1635 |
fdctrl_reset_fifo(fdctrl); |
1636 |
break;
|
1637 |
case 0x04: |
1638 |
/* SENSE_DRIVE_STATUS */
|
1639 |
FLOPPY_DPRINTF("treat SENSE_DRIVE_STATUS command\n");
|
1640 |
fdctrl->cur_drv = fdctrl->fifo[1] & 1; |
1641 |
cur_drv = get_cur_drv(fdctrl); |
1642 |
cur_drv->head = (fdctrl->fifo[1] >> 2) & 1; |
1643 |
/* 1 Byte status back */
|
1644 |
fdctrl->fifo[0] = (cur_drv->ro << 6) | |
1645 |
(cur_drv->track == 0 ? 0x10 : 0x00) | |
1646 |
fdctrl->cur_drv; |
1647 |
fdctrl_set_fifo(fdctrl, 1, 0); |
1648 |
break;
|
1649 |
case 0x07: |
1650 |
/* RECALIBRATE */
|
1651 |
FLOPPY_DPRINTF("treat RECALIBRATE command\n");
|
1652 |
fdctrl->cur_drv = fdctrl->fifo[1] & 1; |
1653 |
cur_drv = get_cur_drv(fdctrl); |
1654 |
fd_recalibrate(cur_drv); |
1655 |
fdctrl_reset_fifo(fdctrl); |
1656 |
/* Raise Interrupt */
|
1657 |
fdctrl_raise_irq(fdctrl, 0x20);
|
1658 |
break;
|
1659 |
case 0x0F: |
1660 |
/* SEEK */
|
1661 |
FLOPPY_DPRINTF("treat SEEK command\n");
|
1662 |
fdctrl->cur_drv = fdctrl->fifo[1] & 1; |
1663 |
cur_drv = get_cur_drv(fdctrl); |
1664 |
fd_start(cur_drv); |
1665 |
if (fdctrl->fifo[2] <= cur_drv->track) |
1666 |
cur_drv->dir = 1;
|
1667 |
else
|
1668 |
cur_drv->dir = 0;
|
1669 |
fdctrl_reset_fifo(fdctrl); |
1670 |
if (fdctrl->fifo[2] > cur_drv->max_track) { |
1671 |
fdctrl_raise_irq(fdctrl, 0x60);
|
1672 |
} else {
|
1673 |
cur_drv->track = fdctrl->fifo[2];
|
1674 |
/* Raise Interrupt */
|
1675 |
fdctrl_raise_irq(fdctrl, 0x20);
|
1676 |
} |
1677 |
break;
|
1678 |
case 0x12: |
1679 |
/* PERPENDICULAR_MODE */
|
1680 |
FLOPPY_DPRINTF("treat PERPENDICULAR_MODE command\n");
|
1681 |
if (fdctrl->fifo[1] & 0x80) |
1682 |
cur_drv->perpendicular = fdctrl->fifo[1] & 0x7; |
1683 |
/* No result back */
|
1684 |
fdctrl_reset_fifo(fdctrl); |
1685 |
break;
|
1686 |
case 0x13: |
1687 |
/* CONFIGURE */
|
1688 |
FLOPPY_DPRINTF("treat CONFIGURE command\n");
|
1689 |
fdctrl->config = fdctrl->fifo[2];
|
1690 |
fdctrl->precomp_trk = fdctrl->fifo[3];
|
1691 |
/* No result back */
|
1692 |
fdctrl_reset_fifo(fdctrl); |
1693 |
break;
|
1694 |
case 0x17: |
1695 |
/* POWERDOWN_MODE */
|
1696 |
FLOPPY_DPRINTF("treat POWERDOWN_MODE command\n");
|
1697 |
fdctrl->pwrd = fdctrl->fifo[1];
|
1698 |
fdctrl->fifo[0] = fdctrl->fifo[1]; |
1699 |
fdctrl_set_fifo(fdctrl, 1, 1); |
1700 |
break;
|
1701 |
case 0x33: |
1702 |
/* OPTION */
|
1703 |
FLOPPY_DPRINTF("treat OPTION command\n");
|
1704 |
/* No result back */
|
1705 |
fdctrl_reset_fifo(fdctrl); |
1706 |
break;
|
1707 |
case 0x42: |
1708 |
/* READ_TRACK */
|
1709 |
// FLOPPY_DPRINTF("treat READ_TRACK command\n");
|
1710 |
FLOPPY_ERROR("treat READ_TRACK command\n");
|
1711 |
fdctrl_start_transfer(fdctrl, FD_DIR_READ); |
1712 |
break;
|
1713 |
case 0x4A: |
1714 |
/* READ_ID */
|
1715 |
FLOPPY_DPRINTF("treat READ_ID command\n");
|
1716 |
fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00); |
1717 |
break;
|
1718 |
case 0x4C: |
1719 |
/* RESTORE */
|
1720 |
FLOPPY_DPRINTF("treat RESTORE command\n");
|
1721 |
/* Drives position */
|
1722 |
drv0(fdctrl)->track = fdctrl->fifo[3];
|
1723 |
drv1(fdctrl)->track = fdctrl->fifo[4];
|
1724 |
/* timers */
|
1725 |
fdctrl->timer0 = fdctrl->fifo[7];
|
1726 |
fdctrl->timer1 = fdctrl->fifo[8];
|
1727 |
cur_drv->last_sect = fdctrl->fifo[9];
|
1728 |
fdctrl->lock = fdctrl->fifo[10] >> 7; |
1729 |
cur_drv->perpendicular = (fdctrl->fifo[10] >> 2) & 0xF; |
1730 |
fdctrl->config = fdctrl->fifo[11];
|
1731 |
fdctrl->precomp_trk = fdctrl->fifo[12];
|
1732 |
fdctrl->pwrd = fdctrl->fifo[13];
|
1733 |
fdctrl_reset_fifo(fdctrl); |
1734 |
break;
|
1735 |
case 0x4D: |
1736 |
/* FORMAT_TRACK */
|
1737 |
FLOPPY_DPRINTF("treat FORMAT_TRACK command\n");
|
1738 |
fdctrl->cur_drv = fdctrl->fifo[1] & 1; |
1739 |
cur_drv = get_cur_drv(fdctrl); |
1740 |
fdctrl->data_state |= FD_STATE_FORMAT; |
1741 |
if (fdctrl->fifo[0] & 0x80) |
1742 |
fdctrl->data_state |= FD_STATE_MULTI; |
1743 |
else
|
1744 |
fdctrl->data_state &= ~FD_STATE_MULTI; |
1745 |
fdctrl->data_state &= ~FD_STATE_SEEK; |
1746 |
cur_drv->bps = |
1747 |
fdctrl->fifo[2] > 7 ? 16384 : 128 << fdctrl->fifo[2]; |
1748 |
#if 0
|
1749 |
cur_drv->last_sect =
|
1750 |
cur_drv->flags & FDISK_DBL_SIDES ? fdctrl->fifo[3] :
|
1751 |
fdctrl->fifo[3] / 2;
|
1752 |
#else
|
1753 |
cur_drv->last_sect = fdctrl->fifo[3];
|
1754 |
#endif
|
1755 |
/* Bochs BIOS is buggy and don't send format informations
|
1756 |
* for each sector. So, pretend all's done right now...
|
1757 |
*/
|
1758 |
fdctrl->data_state &= ~FD_STATE_FORMAT; |
1759 |
fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00); |
1760 |
break;
|
1761 |
case 0x8E: |
1762 |
/* DRIVE_SPECIFICATION_COMMAND */
|
1763 |
FLOPPY_DPRINTF("treat DRIVE_SPECIFICATION_COMMAND command\n");
|
1764 |
if (fdctrl->fifo[fdctrl->data_pos - 1] & 0x80) { |
1765 |
/* Command parameters done */
|
1766 |
if (fdctrl->fifo[fdctrl->data_pos - 1] & 0x40) { |
1767 |
fdctrl->fifo[0] = fdctrl->fifo[1]; |
1768 |
fdctrl->fifo[2] = 0; |
1769 |
fdctrl->fifo[3] = 0; |
1770 |
fdctrl_set_fifo(fdctrl, 4, 1); |
1771 |
} else {
|
1772 |
fdctrl_reset_fifo(fdctrl); |
1773 |
} |
1774 |
} else if (fdctrl->data_len > 7) { |
1775 |
/* ERROR */
|
1776 |
fdctrl->fifo[0] = 0x80 | |
1777 |
(cur_drv->head << 2) | fdctrl->cur_drv;
|
1778 |
fdctrl_set_fifo(fdctrl, 1, 1); |
1779 |
} |
1780 |
break;
|
1781 |
case 0x8F: |
1782 |
/* RELATIVE_SEEK_OUT */
|
1783 |
FLOPPY_DPRINTF("treat RELATIVE_SEEK_OUT command\n");
|
1784 |
fdctrl->cur_drv = fdctrl->fifo[1] & 1; |
1785 |
cur_drv = get_cur_drv(fdctrl); |
1786 |
fd_start(cur_drv); |
1787 |
cur_drv->dir = 0;
|
1788 |
if (fdctrl->fifo[2] + cur_drv->track >= cur_drv->max_track) { |
1789 |
cur_drv->track = cur_drv->max_track - 1;
|
1790 |
} else {
|
1791 |
cur_drv->track += fdctrl->fifo[2];
|
1792 |
} |
1793 |
fdctrl_reset_fifo(fdctrl); |
1794 |
fdctrl_raise_irq(fdctrl, 0x20);
|
1795 |
break;
|
1796 |
case 0xCD: |
1797 |
/* FORMAT_AND_WRITE */
|
1798 |
// FLOPPY_DPRINTF("treat FORMAT_AND_WRITE command\n");
|
1799 |
FLOPPY_ERROR("treat FORMAT_AND_WRITE command\n");
|
1800 |
fdctrl_unimplemented(fdctrl); |
1801 |
break;
|
1802 |
case 0xCF: |
1803 |
/* RELATIVE_SEEK_IN */
|
1804 |
FLOPPY_DPRINTF("treat RELATIVE_SEEK_IN command\n");
|
1805 |
fdctrl->cur_drv = fdctrl->fifo[1] & 1; |
1806 |
cur_drv = get_cur_drv(fdctrl); |
1807 |
fd_start(cur_drv); |
1808 |
cur_drv->dir = 1;
|
1809 |
if (fdctrl->fifo[2] > cur_drv->track) { |
1810 |
cur_drv->track = 0;
|
1811 |
} else {
|
1812 |
cur_drv->track -= fdctrl->fifo[2];
|
1813 |
} |
1814 |
fdctrl_reset_fifo(fdctrl); |
1815 |
/* Raise Interrupt */
|
1816 |
fdctrl_raise_irq(fdctrl, 0x20);
|
1817 |
break;
|
1818 |
} |
1819 |
} |
1820 |
} |