root / hw / fdc.c @ f9e92e97
History | View | Annotate | Download (46.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_NONE = 0x04, /* 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 struct fdrive_t { |
68 |
BlockDriverState *bs; |
69 |
/* Drive status */
|
70 |
fdrive_type_t drive; |
71 |
uint8_t motor; /* on/off */
|
72 |
uint8_t perpendicular; /* 2.88 MB access mode */
|
73 |
uint8_t rv; /* Revalidated */
|
74 |
/* Position */
|
75 |
uint8_t head; |
76 |
uint8_t track; |
77 |
uint8_t sect; |
78 |
/* Last operation status */
|
79 |
uint8_t dir; /* Direction */
|
80 |
uint8_t rw; /* Read/write */
|
81 |
/* Media */
|
82 |
fdisk_type_t disk; /* Disk type */
|
83 |
uint8_t last_sect; /* Nb sector per track */
|
84 |
uint8_t max_track; /* Nb of tracks */
|
85 |
uint8_t ro; /* Is read-only */
|
86 |
} fdrive_t; |
87 |
|
88 |
static void fd_init (fdrive_t *drv) |
89 |
{ |
90 |
/* Drive */
|
91 |
drv->bs = NULL;
|
92 |
// drv->drive = FDRIVE_DRV_288;
|
93 |
drv->drive = FDRIVE_DRV_144; |
94 |
drv->motor = 0;
|
95 |
drv->perpendicular = 0;
|
96 |
drv->rv = 0;
|
97 |
/* Disk */
|
98 |
drv->disk = FDRIVE_DISK_NONE; |
99 |
drv->last_sect = 1;
|
100 |
drv->max_track = 0;
|
101 |
} |
102 |
|
103 |
static int _fd_sector (uint8_t head, uint8_t track, |
104 |
uint8_t sect, uint8_t last_sect) |
105 |
{ |
106 |
return (((track * 2) + head) * last_sect) + sect - 1; |
107 |
} |
108 |
|
109 |
/* Returns current position, in sectors, for given drive */
|
110 |
static int fd_sector (fdrive_t *drv) |
111 |
{ |
112 |
return _fd_sector(drv->head, drv->track, drv->sect, drv->last_sect);
|
113 |
} |
114 |
|
115 |
static int fd_seek (fdrive_t *drv, uint8_t head, uint8_t track, uint8_t sect, |
116 |
int enable_seek)
|
117 |
{ |
118 |
uint32_t sector; |
119 |
|
120 |
if (track > drv->max_track) {
|
121 |
FLOPPY_ERROR("try to read %d %02x %02x (max=%d %02x %02x)\n",
|
122 |
head, track, sect, 1, drv->max_track, drv->last_sect);
|
123 |
return 2; |
124 |
} |
125 |
if (sect > drv->last_sect) {
|
126 |
FLOPPY_ERROR("try to read %d %02x %02x (max=%d %02x %02x)\n",
|
127 |
head, track, sect, 1, drv->max_track, drv->last_sect);
|
128 |
return 3; |
129 |
} |
130 |
sector = _fd_sector(head, track, sect, drv->last_sect); |
131 |
if (sector != fd_sector(drv)) {
|
132 |
#if 0
|
133 |
if (!enable_seek) {
|
134 |
FLOPPY_ERROR("no implicit seek %d %02x %02x (max=%d %02x %02x)\n",
|
135 |
head, track, sect, 1, drv->max_track, drv->last_sect);
|
136 |
return 4;
|
137 |
}
|
138 |
#endif
|
139 |
drv->head = head; |
140 |
drv->track = track; |
141 |
drv->sect = sect; |
142 |
return 1; |
143 |
} |
144 |
|
145 |
return 0; |
146 |
} |
147 |
|
148 |
/* Set drive back to track 0 */
|
149 |
static void fd_recalibrate (fdrive_t *drv) |
150 |
{ |
151 |
FLOPPY_DPRINTF("recalibrate\n");
|
152 |
drv->head = 0;
|
153 |
drv->track = 0;
|
154 |
drv->sect = 1;
|
155 |
drv->dir = 1;
|
156 |
drv->rw = 0;
|
157 |
} |
158 |
|
159 |
/* Revalidate a disk drive after a disk change */
|
160 |
static void fd_revalidate (fdrive_t *drv, int ro) |
161 |
{ |
162 |
int64_t nb_sectors; |
163 |
|
164 |
FLOPPY_DPRINTF("revalidate\n");
|
165 |
drv->rv = 0;
|
166 |
if (drv->bs != NULL) { |
167 |
bdrv_get_geometry(drv->bs, &nb_sectors); |
168 |
#if 1 |
169 |
if (nb_sectors > 2880) |
170 |
#endif
|
171 |
{ |
172 |
/* Pretend we have a 2.88 MB disk */
|
173 |
drv->disk = FDRIVE_DISK_288; |
174 |
drv->last_sect = 36;
|
175 |
drv->max_track = 80;
|
176 |
#if 1 |
177 |
} else if (nb_sectors > 1440) { |
178 |
/* Pretend we have a 1.44 MB disk */
|
179 |
drv->disk = FDRIVE_DISK_144; |
180 |
drv->last_sect = 18;
|
181 |
drv->max_track = 80;
|
182 |
} else {
|
183 |
/* Pretend we have a 720 kB disk */
|
184 |
drv->disk = FDRIVE_DISK_720; |
185 |
drv->last_sect = 9;
|
186 |
drv->max_track = 80;
|
187 |
#endif
|
188 |
} |
189 |
} else {
|
190 |
drv->disk = FDRIVE_DISK_NONE; |
191 |
drv->last_sect = 1; /* Avoid eventual divide by 0 bugs */ |
192 |
} |
193 |
drv->ro = ro; |
194 |
drv->rv = 1;
|
195 |
} |
196 |
|
197 |
/* Motor control */
|
198 |
static void fd_start (fdrive_t *drv) |
199 |
{ |
200 |
drv->motor = 1;
|
201 |
} |
202 |
|
203 |
static void fd_stop (fdrive_t *drv) |
204 |
{ |
205 |
drv->motor = 0;
|
206 |
} |
207 |
|
208 |
/* Re-initialise a drives (motor off, repositioned) */
|
209 |
static void fd_reset (fdrive_t *drv) |
210 |
{ |
211 |
fd_stop(drv); |
212 |
fd_recalibrate(drv); |
213 |
} |
214 |
|
215 |
/********************************************************/
|
216 |
/* Intel 82078 floppy disk controler emulation */
|
217 |
|
218 |
static void fdctrl_reset (int do_irq); |
219 |
static void fdctrl_reset_fifo (void); |
220 |
static int fdctrl_transfer_handler (void *opaque, target_ulong addr, int size); |
221 |
static void fdctrl_raise_irq (uint8_t status); |
222 |
|
223 |
static uint32_t fdctrl_read_statusB (CPUState *env, uint32_t reg);
|
224 |
static uint32_t fdctrl_read_dor (CPUState *env, uint32_t reg);
|
225 |
static void fdctrl_write_dor (CPUState *env, uint32_t reg, uint32_t value); |
226 |
static uint32_t fdctrl_read_tape (CPUState *env, uint32_t reg);
|
227 |
static void fdctrl_write_tape (CPUState *env, uint32_t reg, uint32_t value); |
228 |
static uint32_t fdctrl_read_main_status (CPUState *env, uint32_t reg);
|
229 |
static void fdctrl_write_rate (CPUState *env, uint32_t reg, uint32_t value); |
230 |
static uint32_t fdctrl_read_data (CPUState *env, uint32_t reg);
|
231 |
static void fdctrl_write_data (CPUState *env, uint32_t reg, uint32_t value); |
232 |
static uint32_t fdctrl_read_dir (CPUState *env, uint32_t reg);
|
233 |
|
234 |
enum {
|
235 |
FD_CTRL_ACTIVE = 0x01,
|
236 |
FD_CTRL_RESET = 0x02,
|
237 |
FD_CTRL_SLEEP = 0x04,
|
238 |
FD_CTRL_BUSY = 0x08,
|
239 |
FD_CTRL_INTR = 0x10,
|
240 |
}; |
241 |
|
242 |
enum {
|
243 |
FD_DIR_WRITE = 0,
|
244 |
FD_DIR_READ = 1,
|
245 |
FD_DIR_SCANE = 2,
|
246 |
FD_DIR_SCANL = 3,
|
247 |
FD_DIR_SCANH = 4,
|
248 |
}; |
249 |
|
250 |
enum {
|
251 |
FD_STATE_CMD = 0x00,
|
252 |
FD_STATE_STATUS = 0x01,
|
253 |
FD_STATE_DATA = 0x02,
|
254 |
FD_STATE_STATE = 0x03,
|
255 |
FD_STATE_MULTI = 0x10,
|
256 |
FD_STATE_SEEK = 0x20,
|
257 |
}; |
258 |
|
259 |
#define FD_STATE(state) ((state) & FD_STATE_STATE)
|
260 |
#define FD_MULTI_TRACK(state) ((state) & FD_STATE_MULTI)
|
261 |
#define FD_DID_SEEK(state) ((state) & FD_STATE_SEEK)
|
262 |
|
263 |
typedef struct fdctrl_t { |
264 |
/* Controler's identification */
|
265 |
uint8_t version; |
266 |
/* HW */
|
267 |
int irq_lvl;
|
268 |
int dma_chann;
|
269 |
/* Controler state */
|
270 |
uint8_t state; |
271 |
uint8_t dma_en; |
272 |
uint8_t cur_drv; |
273 |
uint8_t bootsel; |
274 |
/* Command FIFO */
|
275 |
uint8_t fifo[FD_SECTOR_LEN]; |
276 |
uint32_t data_pos; |
277 |
uint32_t data_len; |
278 |
uint8_t data_state; |
279 |
uint8_t data_dir; |
280 |
uint8_t int_status; |
281 |
/* States kept only to be returned back */
|
282 |
/* Timers state */
|
283 |
uint8_t timer0; |
284 |
uint8_t timer1; |
285 |
/* precompensation */
|
286 |
uint8_t precomp_trk; |
287 |
uint8_t config; |
288 |
uint8_t lock; |
289 |
/* Power down config (also with status regB access mode */
|
290 |
uint8_t pwrd; |
291 |
/* Floppy drives */
|
292 |
fdrive_t drives[2];
|
293 |
} fdctrl_t; |
294 |
|
295 |
static fdctrl_t fdctrl;
|
296 |
|
297 |
void fdctrl_init (int irq_lvl, int dma_chann, int mem_mapped, uint32_t base, |
298 |
char boot_device)
|
299 |
{ |
300 |
// int io_mem;
|
301 |
int i;
|
302 |
|
303 |
FLOPPY_DPRINTF("init controler\n");
|
304 |
memset(&fdctrl, 0, sizeof(fdctrl)); |
305 |
fdctrl.version = 0x90; /* Intel 82078 controler */ |
306 |
fdctrl.irq_lvl = irq_lvl; |
307 |
fdctrl.dma_chann = dma_chann; |
308 |
fdctrl.config = 0x40; /* Implicit seek, polling & FIFO enabled */ |
309 |
if (fdctrl.dma_chann != -1) { |
310 |
fdctrl.dma_en = 1;
|
311 |
DMA_register_channel(dma_chann, fdctrl_transfer_handler, &fdctrl); |
312 |
} else {
|
313 |
fdctrl.dma_en = 0;
|
314 |
} |
315 |
for (i = 0; i < MAX_FD; i++) |
316 |
fd_init(&fdctrl.drives[i]); |
317 |
fdctrl_reset(0);
|
318 |
fdctrl.state = FD_CTRL_ACTIVE; |
319 |
if (mem_mapped) {
|
320 |
FLOPPY_ERROR("memory mapped floppy not supported by now !\n");
|
321 |
#if 0
|
322 |
io_mem = cpu_register_io_memory(0, fdctrl_mem_read, fdctrl_mem_write);
|
323 |
cpu_register_physical_memory(base, 0x08, io_mem);
|
324 |
#endif
|
325 |
} else {
|
326 |
register_ioport_read(base + 0x01, 1, fdctrl_read_statusB, 1); |
327 |
register_ioport_read(base + 0x02, 1, fdctrl_read_dor, 1); |
328 |
register_ioport_write(base + 0x02, 1, fdctrl_write_dor, 1); |
329 |
register_ioport_read(base + 0x03, 1, fdctrl_read_tape, 1); |
330 |
register_ioport_write(base + 0x03, 1, fdctrl_write_tape, 1); |
331 |
register_ioport_read(base + 0x04, 1, fdctrl_read_main_status, 1); |
332 |
register_ioport_write(base + 0x04, 1, fdctrl_write_rate, 1); |
333 |
register_ioport_read(base + 0x05, 1, fdctrl_read_data, 1); |
334 |
register_ioport_write(base + 0x05, 1, fdctrl_write_data, 1); |
335 |
register_ioport_read(base + 0x07, 1, fdctrl_read_dir, 1); |
336 |
} |
337 |
if (boot_device == 'b') |
338 |
fdctrl.bootsel = 1;
|
339 |
else
|
340 |
fdctrl.bootsel = 0;
|
341 |
#if defined (TARGET_I386)
|
342 |
cmos_register_fd(fdctrl.drives[0].drive, fdctrl.drives[1].drive); |
343 |
#endif
|
344 |
} |
345 |
|
346 |
int fdctrl_disk_change (int idx, const unsigned char *filename, int ro) |
347 |
{ |
348 |
fdrive_t *drv; |
349 |
|
350 |
if (idx < 0 || idx > 1) |
351 |
return -1; |
352 |
FLOPPY_DPRINTF("disk %d change: %s (%s)\n", idx, filename,
|
353 |
ro == 0 ? "rw" : "ro"); |
354 |
drv = &fdctrl.drives[idx]; |
355 |
if (fd_table[idx] != NULL) { |
356 |
bdrv_close(fd_table[idx]); |
357 |
fd_table[idx] = NULL;
|
358 |
} |
359 |
fd_table[idx] = bdrv_open(filename, ro); |
360 |
drv->bs = fd_table[idx]; |
361 |
if (fd_table[idx] == NULL) |
362 |
return -1; |
363 |
fd_revalidate(drv, ro); |
364 |
#if 0
|
365 |
fd_recalibrate(drv);
|
366 |
fdctrl_reset_fifo();
|
367 |
fdctrl_raise_irq(0x20);
|
368 |
#endif
|
369 |
|
370 |
return 0; |
371 |
} |
372 |
|
373 |
/* Change IRQ state */
|
374 |
static void fdctrl_reset_irq (void) |
375 |
{ |
376 |
if (fdctrl.state & FD_CTRL_INTR) {
|
377 |
pic_set_irq(fdctrl.irq_lvl, 0);
|
378 |
fdctrl.state &= ~(FD_CTRL_INTR | FD_CTRL_SLEEP | FD_CTRL_BUSY); |
379 |
} |
380 |
} |
381 |
|
382 |
static void fdctrl_raise_irq (uint8_t status) |
383 |
{ |
384 |
if (~(fdctrl.state & FD_CTRL_INTR)) {
|
385 |
pic_set_irq(fdctrl.irq_lvl, 1);
|
386 |
fdctrl.state |= FD_CTRL_INTR; |
387 |
} |
388 |
FLOPPY_DPRINTF("Set interrupt status to 0x%02x\n", status);
|
389 |
fdctrl.int_status = status; |
390 |
} |
391 |
|
392 |
/* Reset controler */
|
393 |
static void fdctrl_reset (int do_irq) |
394 |
{ |
395 |
int i;
|
396 |
|
397 |
FLOPPY_DPRINTF("reset controler\n");
|
398 |
fdctrl_reset_irq(); |
399 |
/* Initialise controler */
|
400 |
fdctrl.cur_drv = 0;
|
401 |
/* FIFO state */
|
402 |
fdctrl.data_pos = 0;
|
403 |
fdctrl.data_len = 0;
|
404 |
fdctrl.data_state = FD_STATE_CMD; |
405 |
fdctrl.data_dir = FD_DIR_WRITE; |
406 |
for (i = 0; i < MAX_FD; i++) |
407 |
fd_reset(&fdctrl.drives[i]); |
408 |
fdctrl_reset_fifo(); |
409 |
if (do_irq)
|
410 |
fdctrl_raise_irq(0x20);
|
411 |
} |
412 |
|
413 |
/* Status B register : 0x01 (read-only) */
|
414 |
static uint32_t fdctrl_read_statusB (CPUState *env, uint32_t reg)
|
415 |
{ |
416 |
fdctrl_reset_irq(); |
417 |
FLOPPY_DPRINTF("status register: 0x00\n");
|
418 |
|
419 |
return 0; |
420 |
} |
421 |
|
422 |
/* Digital output register : 0x02 */
|
423 |
static uint32_t fdctrl_read_dor (CPUState *env, uint32_t reg)
|
424 |
{ |
425 |
fdrive_t *cur_drv, *drv0, *drv1; |
426 |
uint32_t retval = 0;
|
427 |
|
428 |
drv0 = &fdctrl.drives[fdctrl.bootsel]; |
429 |
drv1 = &fdctrl.drives[1 - fdctrl.bootsel];
|
430 |
cur_drv = fdctrl.cur_drv == 0 ? drv0 : drv1;
|
431 |
/* Drive motors state indicators */
|
432 |
retval |= drv1->motor << 5;
|
433 |
retval |= drv0->motor << 4;
|
434 |
/* DMA enable */
|
435 |
retval |= fdctrl.dma_en << 3;
|
436 |
/* Reset indicator */
|
437 |
retval |= (fdctrl.state & FD_CTRL_RESET) == 0 ? 0x04 : 0; |
438 |
/* Selected drive */
|
439 |
retval |= fdctrl.cur_drv; |
440 |
FLOPPY_DPRINTF("digital output register: 0x%02x\n", retval);
|
441 |
|
442 |
return retval;
|
443 |
} |
444 |
|
445 |
static void fdctrl_write_dor (CPUState *env, uint32_t reg, uint32_t value) |
446 |
{ |
447 |
fdrive_t *drv0, *drv1; |
448 |
|
449 |
fdctrl_reset_irq(); |
450 |
drv0 = &fdctrl.drives[fdctrl.bootsel]; |
451 |
drv1 = &fdctrl.drives[1 - fdctrl.bootsel];
|
452 |
/* Reset mode */
|
453 |
if (fdctrl.state & FD_CTRL_RESET) {
|
454 |
if (!(value & 0x04)) { |
455 |
FLOPPY_DPRINTF("Floppy controler in RESET state !\n");
|
456 |
return;
|
457 |
} |
458 |
} |
459 |
FLOPPY_DPRINTF("digital output register set to 0x%02x\n", value);
|
460 |
/* Drive motors state indicators */
|
461 |
if (value & 0x20) |
462 |
fd_start(drv1); |
463 |
else
|
464 |
fd_stop(drv1); |
465 |
if (value & 0x10) |
466 |
fd_start(drv0); |
467 |
else
|
468 |
fd_stop(drv0); |
469 |
/* DMA enable */
|
470 |
#if 0
|
471 |
if (fdctrl.dma_chann != -1)
|
472 |
fdctrl.dma_en = 1 - ((value >> 3) & 1);
|
473 |
#endif
|
474 |
/* Reset */
|
475 |
if (!(value & 0x04)) { |
476 |
if (!(fdctrl.state & FD_CTRL_RESET)) {
|
477 |
FLOPPY_DPRINTF("controler enter RESET state\n");
|
478 |
fdctrl.state |= FD_CTRL_RESET; |
479 |
fdctrl_reset(1);
|
480 |
} |
481 |
} else {
|
482 |
if (fdctrl.state & FD_CTRL_RESET) {
|
483 |
FLOPPY_DPRINTF("controler out of RESET state\n");
|
484 |
fdctrl.state &= ~(FD_CTRL_RESET | FD_CTRL_SLEEP); |
485 |
} |
486 |
} |
487 |
/* Selected drive */
|
488 |
fdctrl.cur_drv = value & 1;
|
489 |
} |
490 |
|
491 |
/* Tape drive register : 0x03 */
|
492 |
static uint32_t fdctrl_read_tape (CPUState *env, uint32_t reg)
|
493 |
{ |
494 |
uint32_t retval = 0;
|
495 |
|
496 |
fdctrl_reset_irq(); |
497 |
/* Disk boot selection indicator */
|
498 |
retval |= fdctrl.bootsel << 2;
|
499 |
/* Tape indicators: never allowed */
|
500 |
FLOPPY_DPRINTF("tape drive register: 0x%02x\n", retval);
|
501 |
|
502 |
return retval;
|
503 |
} |
504 |
|
505 |
static void fdctrl_write_tape (CPUState *env, uint32_t reg, uint32_t value) |
506 |
{ |
507 |
fdctrl_reset_irq(); |
508 |
/* Reset mode */
|
509 |
if (fdctrl.state & FD_CTRL_RESET) {
|
510 |
FLOPPY_DPRINTF("Floppy controler in RESET state !\n");
|
511 |
return;
|
512 |
} |
513 |
FLOPPY_DPRINTF("tape drive register set to 0x%02x\n", value);
|
514 |
/* Disk boot selection indicator */
|
515 |
fdctrl.bootsel = (value >> 2) & 1; |
516 |
/* Tape indicators: never allow */
|
517 |
} |
518 |
|
519 |
/* Main status register : 0x04 (read) */
|
520 |
static uint32_t fdctrl_read_main_status (CPUState *env, uint32_t reg)
|
521 |
{ |
522 |
uint32_t retval = 0;
|
523 |
|
524 |
fdctrl_reset_irq(); |
525 |
fdctrl.state &= ~(FD_CTRL_SLEEP | FD_CTRL_RESET); |
526 |
if (!(fdctrl.state & FD_CTRL_BUSY)) {
|
527 |
/* Data transfer allowed */
|
528 |
retval |= 0x80;
|
529 |
/* Data transfer direction indicator */
|
530 |
if (fdctrl.data_dir == FD_DIR_READ)
|
531 |
retval |= 0x40;
|
532 |
} |
533 |
/* Should handle 0x20 for SPECIFY command */
|
534 |
/* Command busy indicator */
|
535 |
if (FD_STATE(fdctrl.data_state) == FD_STATE_DATA ||
|
536 |
FD_STATE(fdctrl.data_state) == FD_STATE_STATUS) |
537 |
retval |= 0x10;
|
538 |
FLOPPY_DPRINTF("main status register: 0x%02x\n", retval);
|
539 |
|
540 |
return retval;
|
541 |
} |
542 |
|
543 |
/* Data select rate register : 0x04 (write) */
|
544 |
static void fdctrl_write_rate (CPUState *env, uint32_t reg, uint32_t value) |
545 |
{ |
546 |
fdctrl_reset_irq(); |
547 |
/* Reset mode */
|
548 |
if (fdctrl.state & FD_CTRL_RESET) {
|
549 |
if (reg != 0x2 || !(value & 0x04)) { |
550 |
FLOPPY_DPRINTF("Floppy controler in RESET state !\n");
|
551 |
return;
|
552 |
} |
553 |
} |
554 |
FLOPPY_DPRINTF("select rate register set to 0x%02x\n", value);
|
555 |
/* Reset: autoclear */
|
556 |
if (value & 0x80) { |
557 |
fdctrl.state |= FD_CTRL_RESET; |
558 |
fdctrl_reset(1);
|
559 |
fdctrl.state &= ~FD_CTRL_RESET; |
560 |
} |
561 |
if (value & 0x40) { |
562 |
fdctrl.state |= FD_CTRL_SLEEP; |
563 |
fdctrl_reset(1);
|
564 |
} |
565 |
// fdctrl.precomp = (value >> 2) & 0x07;
|
566 |
} |
567 |
|
568 |
/* Digital input register : 0x07 (read-only) */
|
569 |
static uint32_t fdctrl_read_dir (CPUState *env, uint32_t reg)
|
570 |
{ |
571 |
fdrive_t *drv0, *drv1; |
572 |
uint32_t retval = 0;
|
573 |
|
574 |
fdctrl_reset_irq(); |
575 |
drv0 = &fdctrl.drives[fdctrl.bootsel]; |
576 |
drv1 = &fdctrl.drives[1 - fdctrl.bootsel];
|
577 |
if (drv0->rv || drv1->rv)
|
578 |
retval |= 0x80;
|
579 |
if (retval != 0) |
580 |
FLOPPY_ERROR("Floppy digital input register: 0x%02x\n", retval);
|
581 |
drv0->rv = 0;
|
582 |
drv1->rv = 0;
|
583 |
|
584 |
return retval;
|
585 |
} |
586 |
|
587 |
/* FIFO state control */
|
588 |
static void fdctrl_reset_fifo (void) |
589 |
{ |
590 |
fdctrl.data_dir = FD_DIR_WRITE; |
591 |
fdctrl.data_pos = 0;
|
592 |
fdctrl.data_state = FD_STATE_CMD; |
593 |
} |
594 |
|
595 |
/* Set FIFO status for the host to read */
|
596 |
static void fdctrl_set_fifo (int fifo_len, int do_irq) |
597 |
{ |
598 |
fdctrl.data_dir = FD_DIR_READ; |
599 |
fdctrl.data_len = fifo_len; |
600 |
fdctrl.data_pos = 0;
|
601 |
fdctrl.data_state = FD_STATE_STATUS; |
602 |
if (do_irq)
|
603 |
fdctrl_raise_irq(0x00);
|
604 |
} |
605 |
|
606 |
/* Set an error: unimplemented/unknown command */
|
607 |
static void fdctrl_unimplemented (void) |
608 |
{ |
609 |
#if 0
|
610 |
fdrive_t *cur_drv, *drv0, *drv1;
|
611 |
|
612 |
drv0 = &fdctrl.drives[fdctrl.bootsel];
|
613 |
drv1 = &fdctrl.drives[1 - fdctrl.bootsel];
|
614 |
cur_drv = fdctrl.cur_drv == 0 ? drv0 : drv1;
|
615 |
fdctrl.fifo[0] = 0x60 | (cur_drv->head << 1) | fdctrl.cur_drv;
|
616 |
fdctrl.fifo[1] = 0x00;
|
617 |
fdctrl.fifo[2] = 0x00;
|
618 |
fdctrl_set_fifo(3, 1);
|
619 |
#else
|
620 |
fdctrl_reset_fifo(); |
621 |
#endif
|
622 |
} |
623 |
|
624 |
/* Callback for transfer end (stop or abort) */
|
625 |
static void fdctrl_stop_transfer (uint8_t status0, uint8_t status1, |
626 |
uint8_t status2) |
627 |
{ |
628 |
fdrive_t *cur_drv, *drv0, *drv1; |
629 |
|
630 |
drv0 = &fdctrl.drives[fdctrl.bootsel]; |
631 |
drv1 = &fdctrl.drives[1 - fdctrl.bootsel];
|
632 |
cur_drv = fdctrl.cur_drv == 0 ? drv0 : drv1;
|
633 |
FLOPPY_DPRINTF("transfer status: %02x %02x %02x (%02x)\n",
|
634 |
status0, status1, status2, |
635 |
status0 | (cur_drv->head << 1) | fdctrl.cur_drv);
|
636 |
fdctrl.fifo[0] = status0 | (cur_drv->head << 1) | fdctrl.cur_drv; |
637 |
fdctrl.fifo[1] = status1;
|
638 |
fdctrl.fifo[2] = status2;
|
639 |
fdctrl.fifo[3] = cur_drv->track;
|
640 |
fdctrl.fifo[4] = cur_drv->head;
|
641 |
fdctrl.fifo[5] = cur_drv->sect;
|
642 |
fdctrl.fifo[6] = FD_SECTOR_SC;
|
643 |
fdctrl.data_dir = FD_DIR_READ; |
644 |
if (fdctrl.state & FD_CTRL_BUSY)
|
645 |
DMA_release_DREQ(fdctrl.dma_chann); |
646 |
fdctrl_set_fifo(7, 1); |
647 |
} |
648 |
|
649 |
/* Prepare a data transfer (either DMA or FIFO) */
|
650 |
static void fdctrl_start_transfer (int direction) |
651 |
{ |
652 |
fdrive_t *cur_drv, *drv0, *drv1; |
653 |
uint8_t kh, kt, ks; |
654 |
int did_seek;
|
655 |
|
656 |
drv0 = &fdctrl.drives[fdctrl.bootsel]; |
657 |
drv1 = &fdctrl.drives[1 - fdctrl.bootsel];
|
658 |
fdctrl.cur_drv = fdctrl.fifo[1] & 1; |
659 |
cur_drv = fdctrl.cur_drv == 0 ? drv0 : drv1;
|
660 |
kt = fdctrl.fifo[2];
|
661 |
kh = fdctrl.fifo[3];
|
662 |
ks = fdctrl.fifo[4];
|
663 |
FLOPPY_DPRINTF("Start tranfert at %d %d %02x %02x (%d)\n",
|
664 |
fdctrl.cur_drv, kh, kt, ks, |
665 |
_fd_sector(kh, kt, ks, cur_drv->last_sect)); |
666 |
did_seek = 0;
|
667 |
switch (fd_seek(cur_drv, kh, kt, ks, fdctrl.config & 0x40)) { |
668 |
case 2: |
669 |
/* sect too big */
|
670 |
fdctrl_stop_transfer(0x40, 0x00, 0x00); |
671 |
fdctrl.fifo[3] = kt;
|
672 |
fdctrl.fifo[4] = kh;
|
673 |
fdctrl.fifo[5] = ks;
|
674 |
return;
|
675 |
case 3: |
676 |
/* track too big */
|
677 |
fdctrl_stop_transfer(0x40, 0x80, 0x00); |
678 |
fdctrl.fifo[3] = kt;
|
679 |
fdctrl.fifo[4] = kh;
|
680 |
fdctrl.fifo[5] = ks;
|
681 |
return;
|
682 |
case 4: |
683 |
/* No seek enabled */
|
684 |
fdctrl_stop_transfer(0x40, 0x00, 0x00); |
685 |
fdctrl.fifo[3] = kt;
|
686 |
fdctrl.fifo[4] = kh;
|
687 |
fdctrl.fifo[5] = ks;
|
688 |
return;
|
689 |
case 1: |
690 |
did_seek = 1;
|
691 |
break;
|
692 |
default:
|
693 |
break;
|
694 |
} |
695 |
/* Set the FIFO state */
|
696 |
fdctrl.data_dir = direction; |
697 |
fdctrl.data_pos = 0;
|
698 |
fdctrl.data_state = FD_STATE_DATA; /* FIFO ready for data */
|
699 |
if (fdctrl.fifo[0] & 0x80) |
700 |
fdctrl.data_state |= FD_STATE_MULTI; |
701 |
if (did_seek)
|
702 |
fdctrl.data_state |= FD_STATE_SEEK; |
703 |
if (fdctrl.dma_en) {
|
704 |
int dma_mode;
|
705 |
/* DMA transfer are enabled. Check if DMA channel is well programmed */
|
706 |
dma_mode = DMA_get_channel_mode(fdctrl.dma_chann); |
707 |
dma_mode = (dma_mode >> 2) & 3; |
708 |
FLOPPY_DPRINTF("dma_mode=%d direction=%d (%d)\n", dma_mode, direction,
|
709 |
(128 << fdctrl.fifo[5]) * |
710 |
(cur_drv->last_sect - ks + 1));
|
711 |
if (((direction == FD_DIR_SCANE || direction == FD_DIR_SCANL ||
|
712 |
direction == FD_DIR_SCANH) && dma_mode == 0) ||
|
713 |
(direction == FD_DIR_WRITE && dma_mode == 2) ||
|
714 |
(direction == FD_DIR_READ && dma_mode == 1)) {
|
715 |
/* No access is allowed until DMA transfer has completed */
|
716 |
fdctrl.state |= FD_CTRL_BUSY; |
717 |
/* Now, we just have to wait for the DMA controler to
|
718 |
* recall us...
|
719 |
*/
|
720 |
DMA_hold_DREQ(fdctrl.dma_chann); |
721 |
DMA_schedule(fdctrl.dma_chann); |
722 |
return;
|
723 |
} |
724 |
} |
725 |
FLOPPY_DPRINTF("start non-DMA transfer\n");
|
726 |
/* IO based transfer: calculate len */
|
727 |
if (fdctrl.fifo[5] == 00) { |
728 |
fdctrl.data_len = fdctrl.fifo[8];
|
729 |
} else {
|
730 |
fdctrl.data_len = 128 << fdctrl.fifo[5]; |
731 |
fdctrl.data_len *= (cur_drv->last_sect - ks + 1);
|
732 |
if (fdctrl.fifo[0] & 0x80) |
733 |
fdctrl.data_len *= 2;
|
734 |
} |
735 |
fdctrl_raise_irq(0x00);
|
736 |
|
737 |
return;
|
738 |
} |
739 |
|
740 |
/* Prepare a transfer of deleted data */
|
741 |
static void fdctrl_start_transfer_del (int direction) |
742 |
{ |
743 |
/* We don't handle deleted data,
|
744 |
* so we don't return *ANYTHING*
|
745 |
*/
|
746 |
fdctrl_stop_transfer(0x60, 0x00, 0x00); |
747 |
} |
748 |
|
749 |
/* handlers for DMA transfers */
|
750 |
/* XXX: the partial transfer logic seems to be broken */
|
751 |
static int fdctrl_transfer_handler (void *opaque, target_ulong addr, int size) |
752 |
{ |
753 |
fdrive_t *cur_drv, *drv0, *drv1; |
754 |
int len;
|
755 |
uint8_t status0 = 0x00, status1 = 0x00, status2 = 0x00; |
756 |
uint8_t tmpbuf[FD_SECTOR_LEN]; |
757 |
|
758 |
fdctrl_reset_irq(); |
759 |
if (!(fdctrl.state & FD_CTRL_BUSY)) {
|
760 |
FLOPPY_DPRINTF("Not in DMA transfer mode !\n");
|
761 |
return 0; |
762 |
} |
763 |
drv0 = &fdctrl.drives[fdctrl.bootsel]; |
764 |
drv1 = &fdctrl.drives[1 - fdctrl.bootsel];
|
765 |
cur_drv = fdctrl.cur_drv == 0 ? drv0 : drv1;
|
766 |
if (fdctrl.data_dir == FD_DIR_SCANE || fdctrl.data_dir == FD_DIR_SCANL ||
|
767 |
fdctrl.data_dir == FD_DIR_SCANH) |
768 |
status2 = 0x04;
|
769 |
for (fdctrl.data_len = size; fdctrl.data_pos < fdctrl.data_len;
|
770 |
fdctrl.data_pos += len) { |
771 |
len = size - fdctrl.data_pos; |
772 |
if (len > FD_SECTOR_LEN)
|
773 |
len = FD_SECTOR_LEN; |
774 |
FLOPPY_DPRINTF("copy %d bytes (%d %d %d) %d pos %d %02x %02x "
|
775 |
"(%d-0x%08x)\n", len, size, fdctrl.data_pos,
|
776 |
fdctrl.data_len, fdctrl.cur_drv, cur_drv->head, |
777 |
cur_drv->track, cur_drv->sect, fd_sector(cur_drv), |
778 |
fd_sector(cur_drv) * 512);
|
779 |
if (fdctrl.data_dir != FD_DIR_WRITE) {
|
780 |
/* READ & SCAN commands */
|
781 |
if (cur_drv->bs == NULL) { |
782 |
fdctrl_stop_transfer(0x40, 0x00, 0x00); |
783 |
goto transfer_error;
|
784 |
} |
785 |
if (bdrv_read(cur_drv->bs, fd_sector(cur_drv), tmpbuf, 1) < 0) { |
786 |
FLOPPY_DPRINTF("Floppy: error getting sector %d\n",
|
787 |
fd_sector(cur_drv)); |
788 |
/* Sure, image size is too small... */
|
789 |
memset(tmpbuf, 0, FD_SECTOR_LEN);
|
790 |
} |
791 |
if (fdctrl.data_dir == FD_DIR_READ) {
|
792 |
cpu_physical_memory_write(addr + fdctrl.data_pos, |
793 |
tmpbuf, len); |
794 |
if (len < FD_SECTOR_LEN) {
|
795 |
memcpy(&fdctrl.fifo[0], tmpbuf + len, FD_SECTOR_LEN - len);
|
796 |
memset(&fdctrl.fifo[FD_SECTOR_LEN - len], 0, len);
|
797 |
} |
798 |
} else {
|
799 |
int ret;
|
800 |
/* XXX: what to do if not enough data ? */
|
801 |
cpu_physical_memory_read(addr + fdctrl.data_pos, |
802 |
fdctrl.fifo, len); |
803 |
if (len < FD_SECTOR_LEN) {
|
804 |
memset(&fdctrl.fifo[len], 0, FD_SECTOR_LEN - len);
|
805 |
} |
806 |
ret = memcmp(tmpbuf, fdctrl.fifo, FD_SECTOR_LEN); |
807 |
if (ret == 0) { |
808 |
status2 = 0x08;
|
809 |
goto end_transfer;
|
810 |
} |
811 |
if ((ret < 0 && fdctrl.data_dir == FD_DIR_SCANL) || |
812 |
(ret > 0 && fdctrl.data_dir == FD_DIR_SCANH)) {
|
813 |
status2 = 0x00;
|
814 |
goto end_transfer;
|
815 |
} |
816 |
} |
817 |
} else {
|
818 |
/* WRITE commands */
|
819 |
cpu_physical_memory_read(addr + fdctrl.data_pos, tmpbuf, len); |
820 |
if (len < FD_SECTOR_LEN) {
|
821 |
memset(tmpbuf + len, 0, FD_SECTOR_LEN - len);
|
822 |
} |
823 |
if (cur_drv->bs == NULL || |
824 |
bdrv_write(cur_drv->bs, fd_sector(cur_drv), tmpbuf, 1) < 0) { |
825 |
FLOPPY_ERROR("writting sector %d\n", fd_sector(cur_drv));
|
826 |
fdctrl_stop_transfer(0x60, 0x00, 0x00); |
827 |
goto transfer_error;
|
828 |
} |
829 |
} |
830 |
if (len == FD_SECTOR_LEN) {
|
831 |
/* Seek to next sector */
|
832 |
if (cur_drv->sect == cur_drv->last_sect) {
|
833 |
if (cur_drv->head == 0) { |
834 |
cur_drv->head = 1;
|
835 |
} else {
|
836 |
cur_drv->track++; |
837 |
cur_drv->head = 0;
|
838 |
} |
839 |
cur_drv->sect = 1;
|
840 |
FLOPPY_DPRINTF("seek to next sector (%d %02x %02x => %d)\n",
|
841 |
cur_drv->head, cur_drv->track, cur_drv->sect, |
842 |
fd_sector(cur_drv)); |
843 |
if (cur_drv->head == 0) { |
844 |
FLOPPY_DPRINTF("end transfer\n");
|
845 |
goto end_transfer;
|
846 |
} |
847 |
if (!FD_MULTI_TRACK(fdctrl.data_state)) {
|
848 |
/* Single track read */
|
849 |
FLOPPY_DPRINTF("single track transfert: end transfer\n");
|
850 |
// status1 |= 0x80;
|
851 |
goto end_transfer;
|
852 |
} |
853 |
} else {
|
854 |
cur_drv->sect++; |
855 |
FLOPPY_DPRINTF("seek to next sector (%d %02x %02x => %d)\n",
|
856 |
cur_drv->head, cur_drv->track, cur_drv->sect, |
857 |
fd_sector(cur_drv)); |
858 |
} |
859 |
} |
860 |
} |
861 |
end_transfer:
|
862 |
if (fdctrl.data_dir == FD_DIR_SCANE ||
|
863 |
fdctrl.data_dir == FD_DIR_SCANL || |
864 |
fdctrl.data_dir == FD_DIR_SCANH) |
865 |
status2 = 0x08;
|
866 |
if (FD_DID_SEEK(fdctrl.data_state))
|
867 |
status0 |= 0x20;
|
868 |
fdctrl_stop_transfer(status0, status1, status2); |
869 |
transfer_error:
|
870 |
|
871 |
return fdctrl.data_pos;
|
872 |
} |
873 |
|
874 |
/* Data register : 0x05 */
|
875 |
static uint32_t fdctrl_read_data (CPUState *env, uint32_t reg)
|
876 |
{ |
877 |
fdrive_t *cur_drv, *drv0, *drv1; |
878 |
uint32_t retval = 0;
|
879 |
int pos, len;
|
880 |
|
881 |
fdctrl_reset_irq(); |
882 |
drv0 = &fdctrl.drives[fdctrl.bootsel]; |
883 |
drv1 = &fdctrl.drives[1 - fdctrl.bootsel];
|
884 |
cur_drv = fdctrl.cur_drv == 0 ? drv0 : drv1;
|
885 |
fdctrl.state &= ~FD_CTRL_SLEEP; |
886 |
if (FD_STATE(fdctrl.data_state) == FD_STATE_CMD) {
|
887 |
FLOPPY_ERROR("can't read data in CMD state\n");
|
888 |
return 0; |
889 |
} |
890 |
pos = fdctrl.data_pos; |
891 |
if (FD_STATE(fdctrl.data_state) == FD_STATE_DATA) {
|
892 |
pos %= FD_SECTOR_LEN; |
893 |
if (pos == 0) { |
894 |
len = fdctrl.data_len - fdctrl.data_pos; |
895 |
if (len > FD_SECTOR_LEN)
|
896 |
len = FD_SECTOR_LEN; |
897 |
bdrv_read(cur_drv->bs, fd_sector(cur_drv), |
898 |
fdctrl.fifo, len); |
899 |
} |
900 |
} |
901 |
retval = fdctrl.fifo[pos]; |
902 |
if (++fdctrl.data_pos == fdctrl.data_len) {
|
903 |
fdctrl.data_pos = 0;
|
904 |
/* Switch from transfert mode to status mode
|
905 |
* then from status mode to command mode
|
906 |
*/
|
907 |
if (FD_STATE(fdctrl.data_state) == FD_STATE_DATA)
|
908 |
fdctrl_stop_transfer(0x20, 0x00, 0x00); |
909 |
else
|
910 |
fdctrl_reset_fifo(); |
911 |
} |
912 |
FLOPPY_DPRINTF("data register: 0x%02x\n", retval);
|
913 |
|
914 |
return retval;
|
915 |
} |
916 |
|
917 |
static void fdctrl_write_data (CPUState *env, uint32_t reg, uint32_t value) |
918 |
{ |
919 |
fdrive_t *cur_drv, *drv0, *drv1; |
920 |
|
921 |
fdctrl_reset_irq(); |
922 |
drv0 = &fdctrl.drives[fdctrl.bootsel]; |
923 |
drv1 = &fdctrl.drives[1 - fdctrl.bootsel];
|
924 |
cur_drv = fdctrl.cur_drv == 0 ? drv0 : drv1;
|
925 |
/* Reset mode */
|
926 |
if (fdctrl.state & FD_CTRL_RESET) {
|
927 |
FLOPPY_DPRINTF("Floppy controler in RESET state !\n");
|
928 |
return;
|
929 |
} |
930 |
fdctrl.state &= ~FD_CTRL_SLEEP; |
931 |
if ((fdctrl.data_state & FD_STATE_STATE) == FD_STATE_STATUS) {
|
932 |
FLOPPY_ERROR("can't write data in status mode\n");
|
933 |
return;
|
934 |
} |
935 |
/* Is it write command time ? */
|
936 |
if (FD_STATE(fdctrl.data_state) == FD_STATE_DATA) {
|
937 |
/* FIFO data write */
|
938 |
fdctrl.fifo[fdctrl.data_pos++] = value; |
939 |
if (fdctrl.data_pos % FD_SECTOR_LEN == (FD_SECTOR_LEN - 1) || |
940 |
fdctrl.data_pos == fdctrl.data_len) { |
941 |
bdrv_write(cur_drv->bs, fd_sector(cur_drv), |
942 |
fdctrl.fifo, FD_SECTOR_LEN); |
943 |
} |
944 |
/* Switch from transfert mode to status mode
|
945 |
* then from status mode to command mode
|
946 |
*/
|
947 |
if (FD_STATE(fdctrl.data_state) == FD_STATE_DATA)
|
948 |
fdctrl_stop_transfer(0x20, 0x00, 0x00); |
949 |
return;
|
950 |
} |
951 |
if (fdctrl.data_pos == 0) { |
952 |
/* Command */
|
953 |
switch (value & 0x5F) { |
954 |
case 0x46: |
955 |
/* READ variants */
|
956 |
FLOPPY_DPRINTF("READ command\n");
|
957 |
/* 8 parameters cmd */
|
958 |
fdctrl.data_len = 9;
|
959 |
goto enqueue;
|
960 |
case 0x4C: |
961 |
/* READ_DELETED variants */
|
962 |
FLOPPY_DPRINTF("READ_DELETED command\n");
|
963 |
/* 8 parameters cmd */
|
964 |
fdctrl.data_len = 9;
|
965 |
goto enqueue;
|
966 |
case 0x50: |
967 |
/* SCAN_EQUAL variants */
|
968 |
FLOPPY_DPRINTF("SCAN_EQUAL command\n");
|
969 |
/* 8 parameters cmd */
|
970 |
fdctrl.data_len = 9;
|
971 |
goto enqueue;
|
972 |
case 0x56: |
973 |
/* VERIFY variants */
|
974 |
FLOPPY_DPRINTF("VERIFY command\n");
|
975 |
/* 8 parameters cmd */
|
976 |
fdctrl.data_len = 9;
|
977 |
goto enqueue;
|
978 |
case 0x59: |
979 |
/* SCAN_LOW_OR_EQUAL variants */
|
980 |
FLOPPY_DPRINTF("SCAN_LOW_OR_EQUAL command\n");
|
981 |
/* 8 parameters cmd */
|
982 |
fdctrl.data_len = 9;
|
983 |
goto enqueue;
|
984 |
case 0x5D: |
985 |
/* SCAN_HIGH_OR_EQUAL variants */
|
986 |
FLOPPY_DPRINTF("SCAN_HIGH_OR_EQUAL command\n");
|
987 |
/* 8 parameters cmd */
|
988 |
fdctrl.data_len = 9;
|
989 |
goto enqueue;
|
990 |
default:
|
991 |
break;
|
992 |
} |
993 |
switch (value & 0x7F) { |
994 |
case 0x45: |
995 |
/* WRITE variants */
|
996 |
FLOPPY_DPRINTF("WRITE command\n");
|
997 |
/* 8 parameters cmd */
|
998 |
fdctrl.data_len = 9;
|
999 |
goto enqueue;
|
1000 |
case 0x49: |
1001 |
/* WRITE_DELETED variants */
|
1002 |
FLOPPY_DPRINTF("WRITE_DELETED command\n");
|
1003 |
/* 8 parameters cmd */
|
1004 |
fdctrl.data_len = 9;
|
1005 |
goto enqueue;
|
1006 |
default:
|
1007 |
break;
|
1008 |
} |
1009 |
switch (value) {
|
1010 |
case 0x03: |
1011 |
/* SPECIFY */
|
1012 |
FLOPPY_DPRINTF("SPECIFY command\n");
|
1013 |
/* 1 parameter cmd */
|
1014 |
fdctrl.data_len = 3;
|
1015 |
goto enqueue;
|
1016 |
case 0x04: |
1017 |
/* SENSE_DRIVE_STATUS */
|
1018 |
FLOPPY_DPRINTF("SENSE_DRIVE_STATUS command\n");
|
1019 |
/* 1 parameter cmd */
|
1020 |
fdctrl.data_len = 2;
|
1021 |
goto enqueue;
|
1022 |
case 0x07: |
1023 |
/* RECALIBRATE */
|
1024 |
FLOPPY_DPRINTF("RECALIBRATE command\n");
|
1025 |
/* 1 parameter cmd */
|
1026 |
fdctrl.data_len = 2;
|
1027 |
goto enqueue;
|
1028 |
case 0x08: |
1029 |
/* SENSE_INTERRUPT_STATUS */
|
1030 |
FLOPPY_DPRINTF("SENSE_INTERRUPT_STATUS command (%02x)\n",
|
1031 |
fdctrl.int_status); |
1032 |
/* No parameters cmd: returns status if no interrupt */
|
1033 |
fdctrl.fifo[0] =
|
1034 |
fdctrl.int_status | (cur_drv->head << 2) | fdctrl.cur_drv;
|
1035 |
fdctrl.fifo[1] = cur_drv->track;
|
1036 |
fdctrl_set_fifo(2, 0); |
1037 |
return;
|
1038 |
case 0x0E: |
1039 |
/* DUMPREG */
|
1040 |
FLOPPY_DPRINTF("DUMPREG command\n");
|
1041 |
/* Drives position */
|
1042 |
fdctrl.fifo[0] = drv0->track;
|
1043 |
fdctrl.fifo[1] = drv1->track;
|
1044 |
fdctrl.fifo[2] = 0; |
1045 |
fdctrl.fifo[3] = 0; |
1046 |
/* timers */
|
1047 |
fdctrl.fifo[4] = fdctrl.timer0;
|
1048 |
fdctrl.fifo[5] = (fdctrl.timer1 << 1) | fdctrl.dma_en; |
1049 |
fdctrl.fifo[6] = cur_drv->last_sect;
|
1050 |
fdctrl.fifo[7] = (fdctrl.lock << 7) | |
1051 |
(cur_drv->perpendicular << 2);
|
1052 |
fdctrl.fifo[8] = fdctrl.config;
|
1053 |
fdctrl.fifo[9] = fdctrl.precomp_trk;
|
1054 |
fdctrl_set_fifo(10, 0); |
1055 |
return;
|
1056 |
case 0x0F: |
1057 |
/* SEEK */
|
1058 |
FLOPPY_DPRINTF("SEEK command\n");
|
1059 |
/* 2 parameters cmd */
|
1060 |
fdctrl.data_len = 3;
|
1061 |
goto enqueue;
|
1062 |
case 0x10: |
1063 |
/* VERSION */
|
1064 |
FLOPPY_DPRINTF("VERSION command\n");
|
1065 |
/* No parameters cmd */
|
1066 |
/* Controler's version */
|
1067 |
fdctrl.fifo[0] = fdctrl.version;
|
1068 |
fdctrl_set_fifo(1, 1); |
1069 |
return;
|
1070 |
case 0x12: |
1071 |
/* PERPENDICULAR_MODE */
|
1072 |
FLOPPY_DPRINTF("PERPENDICULAR_MODE command\n");
|
1073 |
/* 1 parameter cmd */
|
1074 |
fdctrl.data_len = 2;
|
1075 |
goto enqueue;
|
1076 |
case 0x13: |
1077 |
/* CONFIGURE */
|
1078 |
FLOPPY_DPRINTF("CONFIGURE command\n");
|
1079 |
/* 3 parameters cmd */
|
1080 |
fdctrl.data_len = 4;
|
1081 |
goto enqueue;
|
1082 |
case 0x14: |
1083 |
/* UNLOCK */
|
1084 |
FLOPPY_DPRINTF("UNLOCK command\n");
|
1085 |
/* No parameters cmd */
|
1086 |
fdctrl.lock = 0;
|
1087 |
fdctrl.fifo[0] = 0; |
1088 |
fdctrl_set_fifo(1, 0); |
1089 |
return;
|
1090 |
case 0x17: |
1091 |
/* POWERDOWN_MODE */
|
1092 |
FLOPPY_DPRINTF("POWERDOWN_MODE command\n");
|
1093 |
/* 2 parameters cmd */
|
1094 |
fdctrl.data_len = 3;
|
1095 |
goto enqueue;
|
1096 |
case 0x18: |
1097 |
/* PART_ID */
|
1098 |
FLOPPY_DPRINTF("PART_ID command\n");
|
1099 |
/* No parameters cmd */
|
1100 |
fdctrl.fifo[0] = 0x41; /* Stepping 1 */ |
1101 |
fdctrl_set_fifo(1, 0); |
1102 |
return;
|
1103 |
case 0x2C: |
1104 |
/* SAVE */
|
1105 |
FLOPPY_DPRINTF("SAVE command\n");
|
1106 |
/* No parameters cmd */
|
1107 |
fdctrl.fifo[0] = 0; |
1108 |
fdctrl.fifo[1] = 0; |
1109 |
/* Drives position */
|
1110 |
fdctrl.fifo[2] = drv0->track;
|
1111 |
fdctrl.fifo[3] = drv1->track;
|
1112 |
fdctrl.fifo[4] = 0; |
1113 |
fdctrl.fifo[5] = 0; |
1114 |
/* timers */
|
1115 |
fdctrl.fifo[6] = fdctrl.timer0;
|
1116 |
fdctrl.fifo[7] = fdctrl.timer1;
|
1117 |
fdctrl.fifo[8] = cur_drv->last_sect;
|
1118 |
fdctrl.fifo[9] = (fdctrl.lock << 7) | |
1119 |
(cur_drv->perpendicular << 2);
|
1120 |
fdctrl.fifo[10] = fdctrl.config;
|
1121 |
fdctrl.fifo[11] = fdctrl.precomp_trk;
|
1122 |
fdctrl.fifo[12] = fdctrl.pwrd;
|
1123 |
fdctrl.fifo[13] = 0; |
1124 |
fdctrl.fifo[14] = 0; |
1125 |
fdctrl_set_fifo(15, 1); |
1126 |
return;
|
1127 |
case 0x33: |
1128 |
/* OPTION */
|
1129 |
FLOPPY_DPRINTF("OPTION command\n");
|
1130 |
/* 1 parameter cmd */
|
1131 |
fdctrl.data_len = 2;
|
1132 |
goto enqueue;
|
1133 |
case 0x42: |
1134 |
/* READ_TRACK */
|
1135 |
FLOPPY_DPRINTF("READ_TRACK command\n");
|
1136 |
/* 8 parameters cmd */
|
1137 |
fdctrl.data_len = 9;
|
1138 |
goto enqueue;
|
1139 |
case 0x4A: |
1140 |
/* READ_ID */
|
1141 |
FLOPPY_DPRINTF("READ_ID command\n");
|
1142 |
/* 1 parameter cmd */
|
1143 |
fdctrl.data_len = 2;
|
1144 |
goto enqueue;
|
1145 |
case 0x4C: |
1146 |
/* RESTORE */
|
1147 |
FLOPPY_DPRINTF("RESTORE command\n");
|
1148 |
/* 17 parameters cmd */
|
1149 |
fdctrl.data_len = 18;
|
1150 |
goto enqueue;
|
1151 |
case 0x4D: |
1152 |
/* FORMAT_TRACK */
|
1153 |
FLOPPY_DPRINTF("FORMAT_TRACK command\n");
|
1154 |
/* 5 parameters cmd */
|
1155 |
fdctrl.data_len = 9;
|
1156 |
goto enqueue;
|
1157 |
case 0x8E: |
1158 |
/* DRIVE_SPECIFICATION_COMMAND */
|
1159 |
FLOPPY_DPRINTF("DRIVE_SPECIFICATION_COMMAND command\n");
|
1160 |
/* 5 parameters cmd */
|
1161 |
fdctrl.data_len = 6;
|
1162 |
goto enqueue;
|
1163 |
case 0x8F: |
1164 |
/* RELATIVE_SEEK_OUT */
|
1165 |
FLOPPY_DPRINTF("RELATIVE_SEEK_OUT command\n");
|
1166 |
/* 2 parameters cmd */
|
1167 |
fdctrl.data_len = 3;
|
1168 |
goto enqueue;
|
1169 |
case 0x94: |
1170 |
/* LOCK */
|
1171 |
FLOPPY_DPRINTF("LOCK command\n");
|
1172 |
/* No parameters cmd */
|
1173 |
fdctrl.lock = 1;
|
1174 |
fdctrl.fifo[0] = 0x10; |
1175 |
fdctrl_set_fifo(1, 1); |
1176 |
return;
|
1177 |
case 0xCD: |
1178 |
/* FORMAT_AND_WRITE */
|
1179 |
FLOPPY_DPRINTF("FORMAT_AND_WRITE command\n");
|
1180 |
/* 10 parameters cmd */
|
1181 |
fdctrl.data_len = 11;
|
1182 |
goto enqueue;
|
1183 |
case 0xCF: |
1184 |
/* RELATIVE_SEEK_IN */
|
1185 |
FLOPPY_DPRINTF("RELATIVE_SEEK_IN command\n");
|
1186 |
/* 2 parameters cmd */
|
1187 |
fdctrl.data_len = 3;
|
1188 |
goto enqueue;
|
1189 |
default:
|
1190 |
/* Unknown command */
|
1191 |
FLOPPY_ERROR("unknown command: 0x%02x\n", value);
|
1192 |
fdctrl_unimplemented(); |
1193 |
return;
|
1194 |
} |
1195 |
} |
1196 |
enqueue:
|
1197 |
fdctrl.fifo[fdctrl.data_pos] = value; |
1198 |
if (++fdctrl.data_pos == fdctrl.data_len) {
|
1199 |
/* We now have all parameters
|
1200 |
* and will be able to treat the command
|
1201 |
*/
|
1202 |
switch (fdctrl.fifo[0] & 0x1F) { |
1203 |
case 0x06: |
1204 |
{ |
1205 |
/* READ variants */
|
1206 |
FLOPPY_DPRINTF("treat READ command\n");
|
1207 |
fdctrl_start_transfer(FD_DIR_READ); |
1208 |
return;
|
1209 |
} |
1210 |
case 0x0C: |
1211 |
/* READ_DELETED variants */
|
1212 |
// FLOPPY_DPRINTF("treat READ_DELETED command\n");
|
1213 |
FLOPPY_ERROR("treat READ_DELETED command\n");
|
1214 |
fdctrl_start_transfer_del(1);
|
1215 |
return;
|
1216 |
case 0x16: |
1217 |
/* VERIFY variants */
|
1218 |
// FLOPPY_DPRINTF("treat VERIFY command\n");
|
1219 |
FLOPPY_ERROR("treat VERIFY command\n");
|
1220 |
fdctrl_stop_transfer(0x20, 0x00, 0x00); |
1221 |
return;
|
1222 |
case 0x10: |
1223 |
/* SCAN_EQUAL variants */
|
1224 |
// FLOPPY_DPRINTF("treat SCAN_EQUAL command\n");
|
1225 |
FLOPPY_ERROR("treat SCAN_EQUAL command\n");
|
1226 |
fdctrl_start_transfer(FD_DIR_SCANE); |
1227 |
return;
|
1228 |
case 0x19: |
1229 |
/* SCAN_LOW_OR_EQUAL variants */
|
1230 |
// FLOPPY_DPRINTF("treat SCAN_LOW_OR_EQUAL command\n");
|
1231 |
FLOPPY_ERROR("treat SCAN_LOW_OR_EQUAL command\n");
|
1232 |
fdctrl_start_transfer(FD_DIR_SCANL); |
1233 |
return;
|
1234 |
case 0x1D: |
1235 |
/* SCAN_HIGH_OR_EQUAL variants */
|
1236 |
// FLOPPY_DPRINTF("treat SCAN_HIGH_OR_EQUAL command\n");
|
1237 |
FLOPPY_ERROR("treat SCAN_HIGH_OR_EQUAL command\n");
|
1238 |
fdctrl_start_transfer(FD_DIR_SCANH); |
1239 |
return;
|
1240 |
default:
|
1241 |
break;
|
1242 |
} |
1243 |
switch (fdctrl.fifo[0] & 0x3F) { |
1244 |
case 0x05: |
1245 |
/* WRITE variants */
|
1246 |
FLOPPY_DPRINTF("treat WRITE command (%02x)\n", fdctrl.fifo[0]); |
1247 |
fdctrl_start_transfer(FD_DIR_WRITE); |
1248 |
return;
|
1249 |
case 0x09: |
1250 |
/* WRITE_DELETED variants */
|
1251 |
// FLOPPY_DPRINTF("treat WRITE_DELETED command\n");
|
1252 |
FLOPPY_ERROR("treat WRITE_DELETED command\n");
|
1253 |
fdctrl_start_transfer_del(FD_DIR_WRITE); |
1254 |
return;
|
1255 |
default:
|
1256 |
break;
|
1257 |
} |
1258 |
switch (fdctrl.fifo[0]) { |
1259 |
case 0x03: |
1260 |
/* SPECIFY */
|
1261 |
FLOPPY_DPRINTF("treat SPECIFY command\n");
|
1262 |
fdctrl.timer0 = (fdctrl.fifo[1] >> 4) & 0xF; |
1263 |
fdctrl.timer1 = fdctrl.fifo[1] >> 1; |
1264 |
/* No result back */
|
1265 |
fdctrl_reset_fifo(); |
1266 |
break;
|
1267 |
case 0x04: |
1268 |
/* SENSE_DRIVE_STATUS */
|
1269 |
FLOPPY_DPRINTF("treat SENSE_DRIVE_STATUS command\n");
|
1270 |
fdctrl.cur_drv = fdctrl.fifo[1] & 1; |
1271 |
cur_drv = fdctrl.cur_drv == 0 ? drv0 : drv1;
|
1272 |
cur_drv->head = (fdctrl.fifo[1] >> 2) & 1; |
1273 |
/* 1 Byte status back */
|
1274 |
fdctrl.fifo[0] = (cur_drv->ro << 6) | |
1275 |
(cur_drv->track == 0 ? 0x10 : 0x00) | |
1276 |
fdctrl.cur_drv; |
1277 |
fdctrl_set_fifo(1, 0); |
1278 |
break;
|
1279 |
case 0x07: |
1280 |
/* RECALIBRATE */
|
1281 |
FLOPPY_DPRINTF("treat RECALIBRATE command\n");
|
1282 |
fdctrl.cur_drv = fdctrl.fifo[1] & 1; |
1283 |
cur_drv = fdctrl.cur_drv == 0 ? drv0 : drv1;
|
1284 |
fd_recalibrate(cur_drv); |
1285 |
fdctrl_reset_fifo(); |
1286 |
/* Raise Interrupt */
|
1287 |
fdctrl_raise_irq(0x20);
|
1288 |
break;
|
1289 |
case 0x0F: |
1290 |
/* SEEK */
|
1291 |
FLOPPY_DPRINTF("treat SEEK command\n");
|
1292 |
fdctrl.cur_drv = fdctrl.fifo[1] & 1; |
1293 |
cur_drv = fdctrl.cur_drv == 0 ? drv0 : drv1;
|
1294 |
if (fdctrl.fifo[2] <= cur_drv->track) |
1295 |
cur_drv->dir = 1;
|
1296 |
else
|
1297 |
cur_drv->dir = 0;
|
1298 |
cur_drv->head = (fdctrl.fifo[1] >> 2) & 1; |
1299 |
if (fdctrl.fifo[2] > cur_drv->max_track) { |
1300 |
fdctrl_raise_irq(0x60);
|
1301 |
} else {
|
1302 |
cur_drv->track = fdctrl.fifo[2];
|
1303 |
fdctrl_reset_fifo(); |
1304 |
/* Raise Interrupt */
|
1305 |
fdctrl_raise_irq(0x20);
|
1306 |
} |
1307 |
break;
|
1308 |
case 0x12: |
1309 |
/* PERPENDICULAR_MODE */
|
1310 |
FLOPPY_DPRINTF("treat PERPENDICULAR_MODE command\n");
|
1311 |
if (fdctrl.fifo[1] & 0x80) |
1312 |
cur_drv->perpendicular = fdctrl.fifo[1] & 0x7; |
1313 |
/* No result back */
|
1314 |
fdctrl_reset_fifo(); |
1315 |
break;
|
1316 |
case 0x13: |
1317 |
/* CONFIGURE */
|
1318 |
FLOPPY_DPRINTF("treat CONFIGURE command\n");
|
1319 |
fdctrl.config = fdctrl.fifo[2];
|
1320 |
fdctrl.precomp_trk = fdctrl.fifo[3];
|
1321 |
/* No result back */
|
1322 |
fdctrl_reset_fifo(); |
1323 |
break;
|
1324 |
case 0x17: |
1325 |
/* POWERDOWN_MODE */
|
1326 |
FLOPPY_DPRINTF("treat POWERDOWN_MODE command\n");
|
1327 |
fdctrl.pwrd = fdctrl.fifo[1];
|
1328 |
fdctrl.fifo[0] = fdctrl.fifo[1]; |
1329 |
fdctrl_set_fifo(1, 1); |
1330 |
break;
|
1331 |
case 0x33: |
1332 |
/* OPTION */
|
1333 |
FLOPPY_DPRINTF("treat OPTION command\n");
|
1334 |
/* No result back */
|
1335 |
fdctrl_reset_fifo(); |
1336 |
break;
|
1337 |
case 0x42: |
1338 |
/* READ_TRACK */
|
1339 |
// FLOPPY_DPRINTF("treat READ_TRACK command\n");
|
1340 |
FLOPPY_ERROR("treat READ_TRACK command\n");
|
1341 |
fdctrl_unimplemented(); |
1342 |
break;
|
1343 |
case 0x4A: |
1344 |
/* READ_ID */
|
1345 |
// FLOPPY_DPRINTF("treat READ_ID command\n");
|
1346 |
FLOPPY_ERROR("treat READ_ID command\n");
|
1347 |
fdctrl_stop_transfer(0x00, 0x00, 0x00); |
1348 |
break;
|
1349 |
case 0x4C: |
1350 |
/* RESTORE */
|
1351 |
FLOPPY_DPRINTF("treat RESTORE command\n");
|
1352 |
/* Drives position */
|
1353 |
drv0->track = fdctrl.fifo[3];
|
1354 |
drv1->track = fdctrl.fifo[4];
|
1355 |
/* timers */
|
1356 |
fdctrl.timer0 = fdctrl.fifo[7];
|
1357 |
fdctrl.timer1 = fdctrl.fifo[8];
|
1358 |
cur_drv->last_sect = fdctrl.fifo[9];
|
1359 |
fdctrl.lock = fdctrl.fifo[10] >> 7; |
1360 |
cur_drv->perpendicular = (fdctrl.fifo[10] >> 2) & 0xF; |
1361 |
fdctrl.config = fdctrl.fifo[11];
|
1362 |
fdctrl.precomp_trk = fdctrl.fifo[12];
|
1363 |
fdctrl.pwrd = fdctrl.fifo[13];
|
1364 |
fdctrl_reset_fifo(); |
1365 |
break;
|
1366 |
case 0x4D: |
1367 |
/* FORMAT_TRACK */
|
1368 |
// FLOPPY_DPRINTF("treat FORMAT_TRACK command\n");
|
1369 |
FLOPPY_ERROR("treat FORMAT_TRACK command\n");
|
1370 |
fdctrl_unimplemented(); |
1371 |
break;
|
1372 |
case 0x8E: |
1373 |
/* DRIVE_SPECIFICATION_COMMAND */
|
1374 |
FLOPPY_DPRINTF("treat DRIVE_SPECIFICATION_COMMAND command\n");
|
1375 |
if (fdctrl.fifo[fdctrl.data_pos - 1] & 0x80) { |
1376 |
/* Command parameters done */
|
1377 |
if (fdctrl.fifo[fdctrl.data_pos - 1] & 0x40) { |
1378 |
fdctrl.fifo[0] = fdctrl.fifo[1]; |
1379 |
fdctrl.fifo[2] = 0; |
1380 |
fdctrl.fifo[3] = 0; |
1381 |
fdctrl_set_fifo(4, 1); |
1382 |
} else {
|
1383 |
fdctrl_reset_fifo(); |
1384 |
} |
1385 |
} else if (fdctrl.data_len > 7) { |
1386 |
/* ERROR */
|
1387 |
fdctrl.fifo[0] = 0x80 | |
1388 |
(cur_drv->head << 2) | fdctrl.cur_drv;
|
1389 |
fdctrl_set_fifo(1, 1); |
1390 |
} |
1391 |
break;
|
1392 |
case 0x8F: |
1393 |
/* RELATIVE_SEEK_OUT */
|
1394 |
FLOPPY_DPRINTF("treat RELATIVE_SEEK_OUT command\n");
|
1395 |
fdctrl.cur_drv = fdctrl.fifo[1] & 1; |
1396 |
cur_drv = fdctrl.cur_drv == 0 ? drv0 : drv1;
|
1397 |
cur_drv->head = (fdctrl.fifo[1] >> 2) & 1; |
1398 |
if (fdctrl.fifo[2] + cur_drv->track > cur_drv->max_track) { |
1399 |
/* ERROR */
|
1400 |
fdctrl_raise_irq(0x70);
|
1401 |
} else {
|
1402 |
cur_drv->track += fdctrl.fifo[2];
|
1403 |
cur_drv->dir = 0;
|
1404 |
fdctrl_reset_fifo(); |
1405 |
fdctrl_raise_irq(0x20);
|
1406 |
} |
1407 |
break;
|
1408 |
case 0xCD: |
1409 |
/* FORMAT_AND_WRITE */
|
1410 |
// FLOPPY_DPRINTF("treat FORMAT_AND_WRITE command\n");
|
1411 |
FLOPPY_ERROR("treat FORMAT_AND_WRITE command\n");
|
1412 |
fdctrl_unimplemented(); |
1413 |
break;
|
1414 |
case 0xCF: |
1415 |
/* RELATIVE_SEEK_IN */
|
1416 |
FLOPPY_DPRINTF("treat RELATIVE_SEEK_IN command\n");
|
1417 |
fdctrl.cur_drv = fdctrl.fifo[1] & 1; |
1418 |
cur_drv = fdctrl.cur_drv == 0 ? drv0 : drv1;
|
1419 |
cur_drv->head = (fdctrl.fifo[1] >> 2) & 1; |
1420 |
if (fdctrl.fifo[2] > cur_drv->track) { |
1421 |
/* ERROR */
|
1422 |
fdctrl_raise_irq(0x60);
|
1423 |
} else {
|
1424 |
fdctrl_reset_fifo(); |
1425 |
cur_drv->track -= fdctrl.fifo[2];
|
1426 |
cur_drv->dir = 1;
|
1427 |
/* Raise Interrupt */
|
1428 |
fdctrl_raise_irq(0x20);
|
1429 |
} |
1430 |
break;
|
1431 |
} |
1432 |
} |
1433 |
} |