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