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